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 the custarr_fill function, with an SQL condition as parameter. The function returns an integer, as the number of rows found.
  • Lines 6 thru 10 build the SELECT SQL statemente to query the customer table. If an SQL condition is provided, we add a WHERE clause to the query.
  • Line 12 declases the c_cust SQL cursor from the sql_text variable containing the SELECT statement.
  • Lines 14 thru 18 use the SQL cursor to fetch rows from the customer SQL table into the custarr program variable.
  • Line 20 returns the number of rows of the program array custarr.