Example: custquery.4gl (Function query_cust)
This module of the Query program contains the logic for querying the database and displaying the data retrieved.
The module declares that it needs to use the custdemo database schema, and declares a structured
variable with a local module scope, to hold the values of a customer
record:
1 SCHEMA custdemo
2
3 DEFINE mr_custrec RECORD LIKE customer.*The function query_cust is called by the main module:
1 FUNCTION query_cust() RETURNS ()
2 DEFINE cust_cnt INTEGER,
3 where_clause STRING
4
5 LET int_flag = FALSE
6
7 CONSTRUCT BY NAME where_clause ON customer.*
8
9 IF int_flag THEN
10 LET int_flag=FALSE
11 CLEAR FORM
12 MESSAGE "Canceled by user."
13 RETURN
14 ELSE
15 LET cust_cnt = get_cust_cnt(where_clause)
16 IF cust_cnt > 0 THEN
17 MESSAGE SFMT("%1 rows found.",cust_cnt)
18 IF cust_select(where_clause) THEN
19 CALL display_cust()
20 END IF
21 ELSE
22 MESSAGE "No rows found."
23 END IF
24 END IF
25
26 END FUNCTION
Note:
- Line
1: Functionquery_cust. This is the beginning of the functionquery_cust. - Line
2definescust_cnt, a local variable of data typeINTEGER, to hold the number of rows returned by theSELECTstatement. - Line
3defineswhere_clauseas a localSTRINGvariable to hold the boolean condition resulting from theCONSTRUCTstatement. - Line
5setsint_flagtoFALSE. It is common to set this global flag toFALSEimmediately prior to the execution of an interactive dialog, so your program can test whether the user attempted to cancel the dialog. - Lines
7: TheCONSTRUCTstatement defines the form fields for which the user may enter search criteria. TheBY NAMEsyntax matches the database columns to form fields having the same name. - Line
9is the beginning of anIFstatement testing the value ofint_flag. This test appears immediately after theCONSTRUCTstatement, to test whether the user terminated theCONSTRUCTstatement (int_flagwould be set by the runtime system toTRUE). - Lines
10thru13are executed only if the value ofint_flagisTRUE. Theint_flagis immediately reset toFALSE, since it is a global variable which other parts of your program will test. The form is cleared of any criteria that the user has entered. Then theRETURNinstruction terminates the function and we return to the caller block. - Lines
14thru24: contain the logic to be executed ifint_flagwas not set toTRUE(the user hit the OK button to execute the query).- In lines
15, theget_cust_cntfunction is called, to retrieve the number of rows that would be returned by the query criteria. Thewhere_clausevariable is passed to the function, and the value returned will be stored in thecust_cntvariable. - Lines
16is the beginning of a nestedIFstatement, testing the value ofcust_cnt. - Lines
17thru20are executed if the value ofcust_cntis greater than zero; a message with the number of rows returned is displayed to the user, and the functioncust_selectis called, and thedisplay_custfunction is called ifcust_selectreturnsTRUE. -
Lines
22is executed if thecust_cntis zero (no rows found); a message is displayed to the user.
- In lines