Example: connectdb.4gl
This program connects to the custdemo database, selects the store
name from the customer table and displays it to the user.
The line numbers shown in the examples in this tutorial are not part of the BDL code; they are used here so specific lines can be easily referenced. The BDL keywords are shown in uppercase, as a convention only.
01 -- connectdb.4gl
02 SCHEMA custdemo
03
04 MAIN
05 DEFINE
06 m_store_name LIKE customer.store_name
07
08 CONNECT TO "custdemo"
09
10 CALL select_name(101)
11 RETURNING m_store_name
12 DISPLAY m_store_name
13
14 DISCONNECT CURRENT
15
16 END MAIN
17
18 FUNCTION select_name(f_store_num LIKE customer.store_num)
19 RETURNS STRING
20 DEFINE f_store_name LIKE customer.store_name
21
22 SELECT store_name INTO f_store_name
23 FROM customer
24 WHERE store_num = f_store_num
25
26 RETURN f_store_name
27
28 END FUNCTION -- select_name- Line
02TheSCHEMAstatement is used to define the database schema files to be used ascustdemo. TheLIKEsyntax has been used to define variables in the module. - Lines
05and06UsingDEFINEthe local variablem_store_nameis declared as beingLIKEthestore_namecolumn; that is, it has the same data type definition as the column in thecustomertable of thecustdemodatabase. - Line
08A connection in multi-session mode is opened to thecustdemodatabase, with connection parameters defined in thefglprofileconfiguration file. Once connected to the database server, a current database session is started. Any subsequent SQL statement is executed in the context of the current database session. - Line
10Theselect_namefunction is called, passing the literal value101as an argument. The function returns a value to be stored in the local variablem_store_name. - Line
12The value ofm_store_nameis displayed to the user on the standard output. - Line
14TheDISCONNECTinstruction disconnects you from the current session. As there are no additional lines in the program block, the program terminates. - Line
18Beginning of the definition of the functionselect_name. The value "101" that is passed to the function will be stored in the local variablef_store_num. - Lines
19thru20Defines the local variable used in the function. Notice that a variable must be declared with the same name and data type as the parameter listed within the parenthesis in the function statement, to accept the passed value. - Lines
22thru24Contains the embeddedSELECT ... INTOSQL statement to retrieve the store name for store number 101. The store name that is retrieved will be stored in thef_store_namelocal variable. Since the store number is unique, theWHEREclause ensures that only a single row will be returned. - Line
28TheRETURNstatement causes the function to terminate, returning the value of the local variablef_store_name. The number of variables returned matches the number declared in theRETURNINGclause of theCALLstatement invoking the function. Execution of the program continues with line12.
The database schema file
This program requires a database schema file because of the use of the
LIKE keyword when defining the variable
m_store_name. The database schema contains the definition of
the database tables and columns and is used to centralize column data types to
define program variables. The schema file for the BDLTutorial has already been
extracted from the custdemo database and is used at compile
time.
To learn more about database schema files see Database schema in the Genero Business Development Language User Guide.
Compiling and executing the program
You can compile and execute
the connectdb application using the Execute option in the Project
view of Genero Studio or use the command line options.
- Compile the single module
program:
fglcomp connectdb.4gl - Execute the program:
fglrun connectdb.42m