3. Host Variable Declaration Section#
Host Variable Declaration Section#
The name, type, and length of host variables are critical information during the precompiling operation. Therefore, the host variables to be used must be defined in a form such that the C/C++ precompiler can be aware of them. This is accomplished in the host variable declaration section
In the host variable declaration secion, a host variable is declared to use in the program.
Syntax#
The syntax shown below is supported for the host variable declaration section:
EXEC SQL BEGIN DECLARE SECTION;
/* variable_declarations */
EXEC SQL END DECLARE SECTION;
The host variable declaration section begins with the EXEC SQL BEGIN DECLARE SECTION statement and ends with the EXEC SQL END DECLARE SECTION statement. Host variables to be used in the program must be declared between these two statements.
A host variable declaration section can be present both in the file (.sc) to be precompiled and in any header files (.h) that are included in the precompile operation.
Note however that when including a header file using the #include preprocessor directive, a host variable should be declared without host variable declaration section in the header file. Then it requires the -parse option set to full during the precompile operation. If the header file is included within EXEC SQL INCLUDE section, then a host variable should be declared in the host variable declaration section of the header file.
The reason for this is that a header file (.h) that is included using the #include preprocessor directive is referred to not only by the file to be precompiled (.sc), but may also be referred to by C (.c) or C++ (.cpp) source files that do not contain any embedded SQL, so errors may be raised during the compile operation. Please refer to Chapter 4: #include for more detail.
Scope of Host Variables#
Host variables can be global or local in scope, depending on the location of the host variable declaration section. The method of determining the scope of declared variables is similar to that of C/C++. If a global host variable and a local host variable have the same name, the local variable (i.e. the variable having the narrower scope) will take precedence over the global variable (i.e. the variable having the broader scope).
The precompiler is capable of handling 50 levels of overlapping variables.
Example#
The following example shows how the preprocessor handles multiple variables that have the same name but are declared such that they have different scopes. The locally declared variable name (indicated by #2) takes priority over the global variable name (indicated by #1) within the myfunc() function. Therefore, when reference is made to a variable called name within the function (at #3), this is handled as a reference to the local variable.
EXEC SQL BEGIN DECLARE SECTION;
char name[20]; <- #1
EXEC SQL END DECLARE SECTION;
int myfunc(void)
{
EXEC SQL BEGIN DECLARE SECTION;
char name[20]; <- #2
EXEC SQL END DECLARE SECTION;
EXEC SQL INSERT INTO T1 VALUES (:name); <- #3
}
Example#
The following example shows how to declare various kinds of host variables:
EXEC SQL BEGIN DECLARE SECTION; <- #1
int x, y, z; <- #2
char c1[50], c2[100]; <- #3
varchar v1[50]; <- #4
struct tag1
{
int x;
char y[50];
varchar z[50];
} st1; <- #5
struct tag1 st2; <- #6
EXEC SQL END DECLARE SECTION; <- #7
- #1: Indicates the beginning of the host variable declaration section
- #2: Declares the variables x, y, and z as int type host variables.
- #3: Declares the variables c1 and c2 as char type variables that are 50 and 100 bytes long, respectively.
- #4: Declares the variable v1 as a varchar type variable 50 bytes long.
- #5: Defines a structure type called tag1, and declares the variable st1, which is of type tag1.
- #6: Declares the variable st2, which is also of type tag1.
- #7: Indicates the end of the host variable declaration section.
Data Type Definition#
In addition to the data types that are supported for use in embedded SQL statements, it is also possible to use host variables based on user-defined types in embedded SQL statements. Such user-defined types are defined using the typedef statement.
Description#
Definitions of data types intended for use as host variable data types must be indicated in a way such that the preprocessor can recognize them. The data type definition (i.e. the typedef statement) can only be located in the host variable declaration section. Newly defined data types can be used as host variables, just like other data types.
Examples#
Various examples of data type definitions are shown below.
[Example 1] The following example shows the use of the typedef keyword:
EXEC SQL BEGIN DECLARE SECTION;
typdef unsigned int UINT;
typdef unsigned char UCHAR;
EXEC SQL END DECLARE SECTION;
[Example 2] The following examples illustrate various ways to define data types and structures.
(1) This example shows a data type definition that follows the definition of the structure on which it is based:
EXEC SQL BEGIN DECLARE SECTION;
struct department
{
short dno;
char dname[30+1];
char dep_location[9+1];
int mgr_no;
};
typedef struct department department;
EXEC SQL END DECLARE SECTION;
(2) This example shows how to define a structure and the corresponding data type at the same time:
EXEC SQL BEGIN DECLARE SECTION;
typedef struct department
{
short dno;
char dname[30+1];
char dep_location[9+1];
int mgr_no;
} department;
EXEC SQL END DECLARE SECTION;
(3) This example shows a data type definition that precedes the definition of the structure on which it is based:
EXEC SQL BEGIN DECLARE SECTION;
typedef struct department department;
struct department
{
short dno;
char dname[30+1];
char dep_location[9+1];
int mgr_no;
};
EXEC SQL END DECLARE SECTION;
Function Argument Declaration Section#
When using a function argument as a host variable, it is necessary to have a way to provide information about the function arguments to the C/C++ precompiler. The function argument declaration section plays the role of providing information about function arguments to the C/C++ precompiler.
Syntax#
The syntax of the function argument declaration section is as follows:
EXEC SQL BEGIN ARGUMENT SECTION;
/* Declare function arguments to be used as host variables */
EXEC SQL END ARGUMENT SECTION;
The function argument declaration section begins with the EXEC SQL BEGIN ARGUMENT SECTION statement and ends with the EXEC SQL END ARGUMENT SECTION statement. Function arguments to be used as host variables must be declared between these two statements.
The function arguments that are declared within the function argument declaration section must be exactly the same as the function arguments declared in the function header.
Description#
The function argument declaration section can be located only inside functions that are found inside the file to be precompiled (i.e. the file with the .sc extension). Additionally, the limitations that apply to the host variable declaration section also apply to the function argument declaration section.
Sample Program#
argument.sc#
The sample program can be found at $ALTIBASE_HOME/sample/APRE/argument.sc
Result of Execution#
$ is -f schema/schema.sql
$ make argument
$ ./argument
<ARGUMENT>