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 FUNCTIONNote:
- Line
1declares thecustarr_fillfunction, with an SQL condition as parameter. The function returns an integer, as the number of rows found. - Lines
6thru10build theSELECTSQL statemente to query thecustomertable. If an SQL condition is provided, we add aWHEREclause to the query. - Line
12declases thec_custSQL cursor from thesql_textvariable containing theSELECTstatement. - Lines
14thru18use the SQL cursor to fetch rows from thecustomerSQL table into thecustarrprogram variable. - Line
20returns the number of rows of the program arraycustarr.