Example: connectdb.4gl
This program connects to the custdemo
database, selects the customer
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.
1 -- connectdb.4gl
2
3 SCHEMA custdemo
4
5 MAIN
6 DEFINE l_cust_name LIKE customer.cust_name
7
8 CONNECT TO "custdemo"
9
10 LET l_cust_name = select_name(101)
11 DISPLAY l_cust_name
12
13 DISCONNECT CURRENT
14
15 END MAIN
16
17 FUNCTION select_name(f_cust_num LIKE customer.cust_num) RETURNS (LIKE customer.cust_name)
18 DEFINE f_cust_name LIKE customer.cust_name
19
20 SELECT cust_name INTO f_cust_name
21 FROM customer
22 WHERE cust_num = f_cust_num
23
24 RETURN f_cust_name
25
26 END FUNCTION
- Line
3
: TheSCHEMA
statement is used to define the database schema files to be used ascustdemo
. TheLIKE
syntax has been used to define variables in the module. - Line
6
: UsingDEFINE
the local variablem_cust_name
is declared as beingLIKE
thecustomer.cust_name
column; that is, it has the same data type definition as the column in thecustomer
table of thecustdemo
database. - Line
8
: A connection in multi-session mode is opened to thecustdemo
database, with connection parameters defined in thefglprofile
configuration file. Once connected to the database server, a current SQL session is started. Any subsequent SQL statement is executed in the context of the current SQL session. - Line
10
: Theselect_name
function is called, passing the literal value101
as an argument. The function returns a value to be stored in the local variablem_cust_name
. - Line
11
: The value ofm_cust_name
is displayed to the user on the standard output. - Line
13
: TheDISCONNECT
instruction disconnects you from the current session. As there are no additional lines in the program block, the program terminates. - Line
17
: Beginning of the definition of the functionselect_name
. The value "101" that is passed to the function will be stored in the local variablef_cust_num
, definedLIKE
thecustomer.cust_num
column. TheRETURNS
clause indicates the type and number of values returned by the function. - Line
18
defines thef_cust_name
local variable used in the function, that will hold the customer name fetched from the database table. - Lines
20
thru22
contains theSELECT
SQL statement to retrieve the customer name for the number passed as parameter. Since the customer number is unique, theWHERE
clause ensures that only a single row will be returned, so there is no need for an SQL cursor here. Note theINTO
clause using the local variable (this part will in fact be excluded from the real SQL statement sent to the database server). Thef_cust_num
local variable is used as SQL parameter in this SELECT statement (it will be converted to a?
place holder, withf_cust_num
value binding when executing the statement) - Line
24
: TheRETURN
statement causes the function to terminate, returning the value of the local variablef_cust_name
. The number of variables returned matches the number declared in theRETURNING
clause of theCALL
statement 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_cust_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.
- Define the FGLDBPATH environment variable with the path to the directory containing the
custdemo.sch schema file, to let fglcomp find SQL column
definitions for
DEFINE … LIKE
instructions:$ FGLDBPATH=/opt/gst/gst-demo/BDLTutorial/database-sqlite $ export FGLDBPATH
- Compile the single module program:
fglcomp -M connectdb.4gl
- Define the DBPATH environment variable with the path to directory containing the
custdemo.db SQLite database file, in order to connect to this database with
fglrun:
$ DBPATH=/opt/gst/gst-demo/BDLTutorial/database-sqlite $ export DBPATH
- Execute the program:
fglrun connectdb.42m
The program displays in a customer name.