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
2
definescust_cnt
, a local variable of data typeINTEGER
, to hold the number of rows returned by theSELECT
statement. - Line
3
defineswhere_clause
as a localSTRING
variable to hold the boolean condition resulting from theCONSTRUCT
statement. - Line
5
setsint_flag
toFALSE
. It is common to set this global flag toFALSE
immediately prior to the execution of an interactive dialog, so your program can test whether the user attempted to cancel the dialog. - Lines
7
: TheCONSTRUCT
statement defines the form fields for which the user may enter search criteria. TheBY NAME
syntax matches the database columns to form fields having the same name. - Line
9
is the beginning of anIF
statement testing the value ofint_flag
. This test appears immediately after theCONSTRUCT
statement, to test whether the user terminated theCONSTRUCT
statement (int_flag
would be set by the runtime system toTRUE
). - Lines
10
thru13
are executed only if the value ofint_flag
isTRUE
. Theint_flag
is 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 theRETURN
instruction terminates the function and we return to the caller block. - Lines
14
thru24
: contain the logic to be executed ifint_flag
was not set toTRUE
(the user hit the OK button to execute the query).- In lines
15
, theget_cust_cnt
function is called, to retrieve the number of rows that would be returned by the query criteria. Thewhere_clause
variable is passed to the function, and the value returned will be stored in thecust_cnt
variable. - Lines
16
is the beginning of a nestedIF
statement, testing the value ofcust_cnt
. - Lines
17
thru20
are executed if the value ofcust_cnt
is greater than zero; a message with the number of rows returned is displayed to the user, and the functioncust_select
is called, and thedisplay_cust
function is called ifcust_select
returnsTRUE
. -
Lines
22
is executed if thecust_cnt
is zero (no rows found); a message is displayed to the user.
- In lines