Escolar Documentos
Profissional Documentos
Cultura Documentos
This example uses forms to collect a customer number from the user and then display a
summary of all information about the customer. It demonstrates the value of 4GL forms, which
give the user an interface for interacting with the database. To move information from the
database to a form, you first retrieve the information into program variables and then display
the information on the form. Similarly, to update a database row, you set program variables
based on the values of form fields and then update the database row from the variables. The
following form appears in this example:
Defining Records
This example defines a record to store several kinds of information about customers.
Resembling a C struct definition or a Pascal record, a 4GL record is a variable that specifies a
set of other variables. The advantage of defining a record is that you can manipulate the
components of the record as a unit. A record differs from an array in that an array is a list of
values of the same type, whereas a record is a single collection of values that may have many
different types. The example defines the record globally so that it is available in all functions.
A 4GL function can return values to the calling statement. You list the variables that receive
the values in the RETURNING clause of the CALL statement. If the function returns a single
value, you can also call the function in an expression to supply a value for the expression. For
example, a function can return a true or false value that is tested by a CASE, IF, or WHILE
statement. Testing a single value may seem strange if you are used to testing Boolean
expressions containing an equals sign or other logical operator. You should recognize that a
Boolean expression always evaluates to the true or false value. Thus, the conditional
statement really determines whether to execute its code block based on a single value.
Use an IF statement to execute the recovery code when the built-in status variable has a
value less than zero.
4GL automatically sets the status variable to zero when a statement succeeds and to a
negative number when a statement fails. After an SQL statement, the status variable shows
the same number as the built-in SQLCA.SQLCODE variable. Unlike SQLCODE, however, 4GL also
sets the status variable after a 4GL screen statement.
The prompt_window() function uses this technique to adjust the positioning of a window, but
later examples make extensive use of this technique for SQL statements. SQL statements are
particularly appropriate for recovery because an SQL statement can be invalidated at runtime
by changes in the database schema or by locks on the desired rows.
You should always use the standard termination behavior for statements for which you are not
providing recovery code. The continuation behavior increases the size of the executable code
compiled from the 4GL code. In addition, terminating the program is the appropriate recovery
mechanism for statements for which you have not provided explicit recovery code.
Note that the WHENEVER statement is not a runtime statement that applies to statements in
the thread of execution. Instead, WHENEVER is an instruction to the compiler that takes effect
when the module is compiled. That is, the WHENEVER statement applies to all lower lines
within the module until the next WHENEVER statement or the end of the module. The
WHENEVER statement does not apply to functions called from the covered lines.
Function Name Purpose cust_summary() Loops through the action until the user is done.
get_custnum() Obtains a customer number from the user.
get_summary() Uses a series of SELECT statements to fetch from the database and summarize
customer information.
init_msgs() Initializes the members of the ga_dsplymsg array to null. See the description in
Example 2.
prompt_window() Displays a message and prompts the user for confirmation. This function is
a variation of the
1 The SCREEN and ATTRIBUTES sections of the form specification fulfill the same purpose as
described in the section The f_logo Form Specification on page 34. In contrast with Example
3, for which the database was FORMONLY, the f_custkey form and f_custsum form are defined
to work with the stores7 demonstration database. The TABLES section specifies the relevant
tables from the database. The ATTRIBUTES section can associate a field tag with a database
column to assign the data type of the database column to the corresponding screen variable.
Note that, if the data type of the database column changes, you must recompile the form.
The f001 field in the f_custkey form is specified as a member of the FORMONLY table. You
use this pseudo-table within the specification of a database form for fields that cannot or
should not correspond to a database column.
The customer_num column of the customer table has a SERIAL data type. To prevent a user
from inputting or updating a SERIAL value, 4GL automatically moves the cursor out of a screen
field that is defined like a SERIAL field. Because the user must be able to enter a customer
number to select the customer, the f_custkey form specification defines the customer_num
screen variable as a member of the FORMONLY table. The customer_num name merely
clarifies for any programmer reading the code that the information entered in the field
corresponds to the database column. The name does not apply any characteristics of the
column.
3 The f002, f003, and f004 fields in the f_custsum form are also specified as members of the
FORMONLY table. These formonly fields display information synthesized from several columns
in several tables.
3 Los F002, F003, F004 y campos en forma f_custsum tambin se especifican como
miembros de la tabla FORMONLY. Estos campos formonly muestran informacin sintetizada de
varias columnas en varias tablas.