This section includes the following topics:
The script file, bldCCapi, in sqllib/samples/cpp, contains the commands to build a DB2 API program. The parameter, $1, specifies the name of your source file.
#! /bin/ksh # bldCCapi script file # Builds a C++ DB2 API program. # 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 -I$DB2PATH/include -c util.C # Compile the program. CC -I$DB2PATH/include -c $1.C # Link the program. CC -o $1 $1.o util.o -L$DB2PATH/lib -R$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 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 the executable name:
client
The script file, bldCC, in sqllib/samples/cpp, contains the commands to build an embedded SQL 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.
#! /bin/ksh # bldCC script file # Builds a C++ program. # 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 -I$DB2PATH/include -c util.C # Compile the program. CC -I$DB2PATH/include -c $1.C # Link the program. CC -o $1 $1.o util.o -L$DB2PATH/lib -R$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 util.C error-checking utility. CC -Kpic -I$DB2PATH/include -c util.C # Compile the program. # Ensure the program is coded with extern "C". CC -Kpic -I$DB2PATH/include -c $1.C # Link the program and create a shared library CC -G -o $1 $1.o -L$DB2PATH/lib -R$DB2PATH/lib -ldb2 # Copy the shared library to the sqllib/function subdirectory of the DB2 instance. # 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/cpp, contains the commands to build a UDF. UDFs do not contain embedded SQL statements. Therefore, to build a UDF program, you do not connect to a database or precompile and bind the program.
The parameter, $1, specifies the name of your source file. The script file uses the source file name for the shared library name.
#! /bin/ksh # bldCCudf script file # 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. CC -Kpic -I$DB2PATH/include -c $1.c # Link the program and create a shared library. CC -G -o $1 $1.o -L$DB2PATH/lib -R$DB2PATH/lib -ldb2 -ldb2apie # Copy the shared library to the sqllib/function subdirectory of the DB2 instance. # 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.
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.
Multi-threaded applications using SPARCompiler C++ on Solaris need to be compiled and linked with -mt. This will pass -D_REENTRANT to the preprocessor, and -lthread to the linker. POSIX threads also require -lpthread to be passed to the linker. In addition, using the compiler option -D_POSIX_PTHREAD_SEMANTICS allows POSIX variants of functions such as getpwnam_r().
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 -- Solaris # Builds an embedded SQL multi-threaded program. # Usage: bldCCmt <_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 -mt -D_POSIX_PTHREAD_SEMANTICS -I$DB2PATH/include -c util.C # Compile the program. CC -mt -D_POSIX_PTHREAD_SEMANTICS -I$DB2PATH/include -c $1.C # Link the program CC -mt -o $1 $1.o util.o -L$DB2PATH/lib -R$DB2PATH/lib -ldb2 -lpthread
Besides the -mt, -D_POSIX_PTHREAD_SEMANTICS, and -lpthread options, 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:
thdsrver