Example: custlist.4gl (Function custarr_fill)
The custarr_fill
function fills the custarr dynamic array with rows from
the customer
SQL table.
The
custarr_fill
function: 1 PRIVATE FUNCTION custarr_fill(sql_cond STRING) RETURNS INTEGER
2 DEFINE sql_text STRING,
3 rec t_cust,
4 x INTEGER
5
6 LET sql_text = "SELECT cust_num, cust_name, city FROM customer"
7 IF sql_cond IS NOT NULL THEN
8 LET sql_text = sql_text || " WHERE " || sql_cond
9 END IF
10 LET sql_text = sql_text || " ORDER BY cust_name"
11
12 DECLARE c_cust CURSOR FROM sql_text
13
14 CALL custarr.clear()
15 FOREACH c_cust INTO rec.*
16 LET x = x + 1
17 LET custarr[x] = rec
18 END FOREACH
19
20 RETURN custarr.getLength()
21
22 END FUNCTION
Note:
- Line
1
declares thecustarr_fill
function, with an SQL condition as parameter. The function returns an integer, as the number of rows found. - Lines
6
thru10
build theSELECT
SQL statemente to query thecustomer
table. If an SQL condition is provided, we add aWHERE
clause to the query. - Line
12
declases thec_cust
SQL cursor from thesql_text
variable containing theSELECT
statement. - Lines
14
thru18
use the SQL cursor to fetch rows from thecustomer
SQL table into thecustarr
program variable. - Line
20
returns the number of rows of the program arraycustarr
.