This section covers the followintg topics:
The script file bldCCapi, in sqllib/samples/cpp, contains the commands to build a sample C++ program. The parameter, $1, specifies the name of your source file.
#! /bin/ksh # bldCCapi script file # Builds a C++ program not containing embedded SQL # Usage: bldCCapi <prog_name> # Set DB2PATH to where DB2 will be accessed. # The default is the standard instance path. DB2PATH=$HOME/sqllib # Compile the util.C error-checking utility. CC +a1 -ext -I$DB2PATH/include -c util.C # Compile the program. CC +a1 -ext -I$DB2PATH/include -c $1.C # Link the program. CC -o $1 $1.o util.o -L$DB2PATH/lib -ldb2 |
Compile and Link Options for bldCCapi |
---|
The script file contains the following compile options:
|
The script file contains the following link options:
Refer to your compiler documentation for additional compiler
options.
|
To build the sample program client from the source file client.C , enter:
bldCCapi client
The result is an executable file client. You can run the executable file against the sample database by entering:
client
The script file bldCC, in sqllib/samples/cpp, contains the commands to build a sample C++ program.
The first parameter, $1, specifies the name of your source file. The second parameter, $2, specifies the name of the database to which you want to connect. The third parameter, $3, specifies the user ID for the database, and $4 specifies the password. Only the first parameter, the source file name, is required. Database name, user ID, and password are optional. If no database name is supplied, the program uses the default sample database.
#! /bin/ksh # bldCC script file # Builds a C++ program containing embedded SQL # Usage: bldCC <prog_name> [ <db_name> [ <userid> <password> ]] # Set DB2PATH to where DB2 will be accessed. # The default is the standard instance path. DB2PATH=$HOME/sqllib # Connect to a database. if (($# < 2)) then db2 connect to sample elif (($# < 3)) then db2 connect to $2 else db2 connect to $2 user $3 using $4 fi # Precompile the program. db2 prep $1.sqC bindfile # Bind the program to the database. db2 bind $1.bnd # Disconnect from the database. db2 connect reset # Compile the util.C error-checking utility. CC +a1 -ext -I$DB2PATH/include -c util.C # Compile the program. CC +a1 -ext -I$DB2PATH/include -c $1.C # Link the program. CC -o $1 $1.o util.o -L$DB2PATH/lib -ldb2
Compile and Link Options for bldCC |
---|
The script file contains the following compile options:
|
The script file contains the following link options:
Refer to your compiler documentation for additional compiler
options.
|
To build the sample program updat from the source file updat.sqC , enter:
bldCC updat
The result is an executable file updat. You can run the executable file against the sample database by entering the executable name:
updat
Note: | Please see the information for building C++ stored procedures and UDFs in "C++ Considerations for UDFs and Stored Procedures". |
The script file bldCCsrv, in sqllib/samples/cpp, contains the commands to build an embedded SQL stored procedure. The script file compiles the stored procedure into a shared library that can be called by a client application.
The first parameter, $1, specifies the name of your source file. The second parameter, $2, specifies the name of the database to which you want to connect. The third parameter, $3, specifies the user ID for the database, and $4 specifies the password. Only the first parameter, the source file name, is required. Database name, user ID, and password are optional. If no database name is supplied, the program uses the default sample database.
The script file uses the source file name, $1, for the shared library name.
#! /bin/ksh # bldCCsrv script file # Builds a C++ stored procedure. # Usage: bldCCsrv <prog_name> [ <db_name> [ <userid> <password> ]] # Set DB2PATH to where DB2 will be accessed. # The default is the standard instance path. DB2PATH=$HOME/sqllib # Connect to a database. if (($# < 2)) then db2 connect to sample elif (($# < 3)) then db2 connect to $2 else db2 connect to $2 user $3 using $4 fi # Precompile the program. db2 prep $1.sqC bindfile # Bind the program to the database. db2 bind $1.bnd # Disconnect from the database. db2 connect reset # Compile the program. First ensure it is coded with extern "C". CC +a1 +z -ext -I$DB2PATH/include -c $1.C # Link the program to create a shared library. ld -b -o $1 $1.o -L$DB2PATH/lib -ldb2 # Copy the shared library to the DB2 instance sqllib/function subdirectory. # This assumes the user has write permission to this directory. rm -f $DB2PATH/function/$1 cp $1 $DB2PATH/function
Compile and Link Options for bldCCsrv |
---|
The script file contains the following compile options:
|
The script file contains the following link options:
Refer to your compiler documentation for additional compiler
options.
|
To build the sample program outsrv from the source file outsrv.sqC , enter:
bldCCsrv outsrv
The script file copies the stored procedure to the server in the path sqllib/function. For DB2DARI parameter style stored procedures where the invoked procedure matches the shared library name, this location indicates that the stored procedure is fenced. If you want this type of stored procedure to be unfenced, you must move it to the sqllib/function/unfenced directory. For all other types of DB2 stored procedures, you indicate whether it is fenced or not fenced with the CREATE FUNCTION statement in the calling program. For a full discussion on creating and using the different types of DB2 stored procedures, please see the "Stored Procedures" chapter in the Application Development Guide.
Note: | An unfenced stored procedure runs in the same address space as the database manager and results in increased performance when compared to a fenced stored procedure, which runs in an address space isolated from the database manager. With unfenced stored procedures there is a danger that user code could accidentally or maliciously damage the database control structures. Therefore, you should only run unfenced stored procedures when you need to maximize the performance benefits. Ensure these programs are thoroughly tested before running them as unfenced. Refer to the Application Development Guide for more information. |
If necessary, set the file mode for the stored procedure so the DB2 instance can run it.
Once you build the stored procedure outsrv, you can build the client application outcli that calls the stored procedure. You can build outcli using the script file bldCC. Refer to "Embedded SQL Applications" for details.
To call the stored procedure, run the sample client application by entering:
outcli remote_database userid password
where
The client application passes a variable to the server program outsrv, which gives it a value and then returns the variable to the client application.
Note: | Please see the information for building C++ UDFs and stored procedures in "C++ Considerations for UDFs and Stored Procedures". |
The script file bldCCudf, in sqllib/samples/c, contains the commands to build a UDF. User-defined programs cannot contain embedded SQL statements. This means to build a UDF program, you never need to connect to a database, precompile, and bind the program.
The first parameter, $1, specifies the name of your source file. The script file uses this source file name for the shared library name.
#! /bin/ksh # bldCCudf script file -- HP-UX # Builds a C++ user-defined function library. # Usage: bldCCudf <prog_name> # Set DB2PATH to where DB2 will be accessed. # The default is the standard instance path. DB2PATH=$HOME/sqllib # Compile the program. The extension ".c" is for a C source file. # Change the extension to ".C" if compiling a C++ source file. CC -ext +a1 +z -I$DB2PATH/include -c $1.c # Link the program. CC -b -o $1 $1.o -L$DB2PATH/lib -ldb2 # Copy the shared library to the sqllib/function subdirectory of the DB2 instance. # Note: this assumes the user has write permission to this directory. rm -f $DB2PATH/function/$1 cp $1 $DB2PATH/function |
Compile and Link Options for bldCCudf |
---|
The script file contains the following compile options:
|
The script file contains the following link options:
Refer to your compiler documentation for additional compiler
options.
|
To build the user-defined function program udf from the source file udf.c , enter:
bldCCudf udf
The script file copies the UDF to the server in the path sqllib/function.
Note: | If you wish to build a C++ UDF program that has a .C extension, you must modify the script file bldCCudf to accept programs with this extension. |
If necessary, set the file mode for the UDF so the DB2 instance can run it.
Once you build udf, you can build the client application, calludf, that calls it. You can build the calludf program from the calludf.sqC source file in sqllib/samples/cpp using the script file bldCC. Refer to "Embedded SQL Applications" for details.
To call the UDF, run the sample calling application by entering the executable name:
calludf
The calling application calls functions from the udf library.
Note: | Multi-threaded applications are not supported by DB2 on version 10 of the HP-UX operating system. HP-UX version 11 provides a POSIX thread library and a DCE thread library. Multi-threaded applications using the POSIX thread library are supported by DB2 on HP-UX version 11. |
Multi-threaded applications on HP-UX version 11 need to have _REENTRANT defined for their compilation. The HP-UX documentation recommends compiling with -D_POSIX_C_SOURCE=199506L. This will also ensure _REENTRANT is defined. For the HP-UX C++ compiler, -D_HPUX_SOURCE must also be used in order to define rand_r. Applications also need to be linked with -lpthread.
The script file, bldCCmt, in sqllib/samples/cpp, contains the commands to build an embedded SQL multi-threaded program. If you want to build a DB2 API multi-threaded program, comment out the connect, precompile, bind, and disconnect commands.
The first parameter, $1, specifies the name of your source file. The second parameter, $2, specifies the name of the database to which you want to connect. The third parameter, $3, specifies the user ID for the database, and $4 specifies the password. Only the first parameter, the source file name, is required. Database name, user ID, and password are optional. If no database name is supplied, the program uses the default sample database.
#! /bin/ksh # bldCCmt script file -- HP-UX # Builds an embedded SQL multi-threaded program with HP-UX C++. # Usage: bldCCmt <prog_name> [ <db_name> [ <userid> <password> ]] # Set DB2PATH to where DB2 will be accessed. # The default is the standard instance path. DB2PATH=$HOME/sqllib # Connect to a database. if (($# < 2)) then db2 connect to sample elif (($# < 3)) then db2 connect to $2 else db2 connect to $2 user $3 using $4 fi # Precompile the program. db2 prep $1.sqC bindfile # Bind the program to the database. db2 bind $1.bnd # Disconnect from the database. db2 connect reset # Compile the util.C error-checking utility. CC -ext +a1 +z -I$DB2PATH/include \ -D_HPUX_SOURCE -D_POSIX_C_SOURCE=199506L -c util.C # Compile the program. CC -ext +a1 +z -I$DB2PATH/include \ -D_HPUX_SOURCE -D_POSIX_C_SOURCE=199506L -c $1.C # Link the program CC -o $1 $1.o util.o -L$DB2PATH/lib -ldb2 -lpthread
Besides the -D_HPUX_SOURCE and -D_POSIX_C_SOURCE=199506L compile options, and the -lpthread link option, discussed above, the other compile and link options are the same as those used for the embedded SQL script file, bldCC. For information on these options, see "Embedded SQL Applications".
To build the sample program, thdsrver, from the source file thdsrver.sqC , enter:
bldCCmt thdsrver
The result is an executable file, thdsrver. To run the executable file against the sample database, enter the executable name:
thdsrver