Example: orders.4gl (Function order_fetch)
This function browses the ordnums
list in the direction specified by
the parameter, and fetches the order header information and order lines of the new current
row.
Function
order_query
(orders.4gl): 1 PRIVATE FUNCTION order_fetch(dir SMALLINT) RETURNS BOOLEAN
2
3 CASE dir
4 WHEN move_first
5 IF ordnums.getLength() > 0 THEN
6 LET orders_index = 1
7 ELSE
8 LET orders_index = 0
9 RETURN FALSE
10 END IF
11 WHEN move_prev
12 IF orders_index > 1 THEN
13 LET orders_index = orders_index - 1
14 ELSE
15 RETURN FALSE
16 END IF
17 WHEN move_next
18 IF orders_index < ordnums.getLength() THEN
19 LET orders_index = orders_index + 1
20 ELSE
21 RETURN FALSE
22 END IF
23 WHEN move_last
24 LET orders_index = ordnums.getLength()
25 END CASE
26
27 SELECT orders.cust_num,
28 customer.cust_name,
29 orders.order_num,
30 orders.order_date,
31 orders.fac_code,
32 orders.ship_instr,
33 orders.promo
34 INTO order_rec.*
35 FROM orders, customer
36 WHERE orders.cust_num = customer.cust_num
37 AND orders.order_num = ordnums[orders_index]
38
39 CALL items_fetch()
40
41 RETURN TRUE
42
43 END FUNCTION
Note:
- Line
1
defines the function with an parameter indicating the direction to move to. The function returnsTRUE
on success. - Lines
3
thru25
implement theCASE
block, executing code blocks depending on the direction parameter. The direction options are defined with themove_*
constants. The code increments or decrements theorder_index
variable, which defines the position of the current row in theordnums
array. - Lines
27
thru37
execute theSELECT
statement to fetch order header information into theorder_rec
variable, from the current order number defined byordnums[order_index]
. - Line
39
calls theitems_fetch
function to retrieve the order item lines for the current order, into theorditems
dynamic array. - Line
41
returnsTRUE
.