Tutorial Chapter 4: Query by Example / Allowing the User to Cancel the Query Operation |
The Query program consists of two modules. The custmain.4gl module must be linked with the custquery.4gl module in order for the program to be run.
The line numbers shown in the code are for reference only, and are not a part of the code.
This module contains the MAIN program block for the query program, and the MENU that drives the query actions.
01 MAIN 02 03 DEFER INTERRUPT 04 05 CONNECT TO "custdemo" 06 CLOSE WINDOW SCREEN 07 OPEN WINDOW w1 WITH FORM "custform" 08 09 MENU "Customer" 10 ON ACTION query 11 CALL query_cust() 12 ON ACTION next 13 CALL fetch_rel_cust(1) 14 ON ACTION previous 15 CALL fetch_rel_cust(-1) 16 ON ACTION exit 17 EXIT MENU 18 END MENU 19 20 CLOSE WINDOW w1 21 22 DISCONNECT CURRENT 23 24 END MAIN
There are no further statements so the Query program terminates.
This module of the Query program contains the logic for querying the database and displaying the data retrieved.
The function query_cust is called by the "query" option of the MENU in custmain.4gl.
01 -- custquery.4gl 02 03 SCHEMA custdemo 04 05 DEFINE mr_custrec RECORD 06 store_num LIKE customer.store_num, 07 store_name LIKE customer.store_name, 08 addr LIKE customer.addr, 09 addr2 LIKE customer.addr2, 10 city LIKE customer.city, 11 state LIKE customer.state, 12 zip_code LIKE customer.zip_code, 13 contact_name LIKE customer.contact_name, 14 phone LIKE customer.phone 15 END RECORD 16 17 FUNCTION query_cust() 18 DEFINE cont_ok SMALLINT, 19 cust_cnt SMALLINT, 20 where_clause STRING 21 MESSAGE "Enter search criteria" 22 LET cont_ok = FALSE 23 24 LET INT_FLAG = FALSE 25 CONSTRUCT BY NAME where_clause 26 ON customer.store_num, 27 customer.store_name, 28 customer.city, 29 customer.state, 30 customer.zip_code, 31 customer.contact_name, 32 customer.phone 33 34 IF (INT_FLAG = TRUE) THEN 35 LET INT_FLAG = FALSE 36 CLEAR FORM 37 LET cont_ok = FALSE 38 MESSAGE "Canceled by user." 39 ELSE 40 CALL get_cust_cnt(where_clause) 41 RETURNING cust_cnt 42 IF (cust_cnt > 0) THEN 43 MESSAGE cust_cnt USING "<<<<", 44 " rows found." 45 CALL cust_select(where_clause) 46 RETURNING cont_ok 47 ELSE 48 MESSAGE "No rows found." 49 LET cont_ok = FALSE 50 END IF 51 END IF 52 53 IF (cont_ok = TRUE) THEN 54 CALL display_cust() 55 END IF 56 57 END FUNCTION
Lines 48 and 49 are executed if the value is zero (no rows found); a message is displayed to the user, and cont_ok is set to FALSE. Execution continues after the END IF on line 51.
This function is called by the function query_cust to return the count of rows that would be retrieved by the SELECT statement. The criteria previously entered by the user and stored in the variable where_clause is used.
01 FUNCTION get_cust_cnt(p_where_clause) 02 DEFINE p_where_clause STRING, 03 sql_text STRING, 04 cust_cnt SMALLINT 05 06 LET sql_text = 07 "SELECT COUNT(*) FROM customer" || 08 " WHERE " || p_where_clause 09 10 PREPARE cust_cnt_stmt FROM sql_text 11 EXECUTE cust_cnt_stmt INTO cust_cnt 12 FREE cust_cnt_stmt 13 14 RETURN cust_cnt 15 16 END FUNCTION