IBM Books

Application Building Guide


Linux C++

This section covers the following topics:

DB2 API Applications

The script file bldCCapi, in sqllib/samples/cpp, contains the commands to build a sample DB2 API program. The parameter, $1, specifies the name of your source file.



#! /bin/ksh
# bldCCapi script file                                
# Builds a 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.
g++ -I$DB2PATH/include -c util.C
# Compile the program.
g++ -I$DB2PATH/include -c $1.C
# Link the program.
g++ -o $1 $1.o util.o -L$DB2PATH/lib -Wl,-rpath,$DB2PATH/lib -ldb2


Compile and Link Options for bldCC

The script file contains the following compile options:

g++
The C++ compiler.

-I$DB2PATH/include
Specify the location of the DB2 include files. For example: $HOME/sqllib/include

-c
Perform compile only; no link. This script file has separate compile and link steps.

The script file contains the following link options:

g++
Use the compiler as a front end for the linker.

-o $1
Specify the executable.

$1.o
Include the program object file.

util.o
Include the utility object file for error checking.

-L$DB2PATH/lib
Specify the location of the DB2 static and shared libraries at link-time. For example: $HOME/sqllib/lib. If you do not specify the -L option, /usr/lib:/lib is assumed.

-Wl,-rpath,$DB2PATH/lib
Specify the location of the DB2 shared libraries at run-time. For example: $HOME/sqllib/lib.

-ldb2
Link with the DB2 library.

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

Embedded SQL Applications

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 sample 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.
g++ -I$DB2PATH/include -c util.C
# Compile the program.
g++ -I$DB2PATH/include -c $1.C
# Link the program.
g++ -o $1 $1.o util.o -L$DB2PATH/lib -Wl,-rpath,$DB2PATH/lib -ldb2


Compile and Link Options for bldCC

The script file contains the following compile options:

g++
The C++ compiler.

-I$DB2PATH/include
Specify the location of the DB2 include files. For example: $HOME/sqllib/include

-c
Perform compile only; no link. This script file has separate compile and link steps.

The script file contains the following link options:

g++
Use the compiler as a front end for the linker.

-o $1
Specify the executable.

$1.o
Include the program object file.

util.o
Include the utility object file for error checking.

-L$DB2PATH/lib
Specify the location of the DB2 static and shared libraries at link-time. For example: $HOME/sqllib/lib. If you do not specify the -L option, /usr/lib:/lib is assumed.

-Wl,-rpath,$DB2PATH/lib
Specify the location of the DB2 shared libraries at run-time. For example: $HOME/sqllib/lib.

-ldb2
Link with the DB2 library.

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

Embedded SQL Stored Procedures

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 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.
g++ -I$DB2PATH/include -c $1.C
# Link the program and create a shared library.
g++ -shared -o $1 $1.o -L$DB2PATH/lib -ldb2
 
# Copy the shared library to the function subdirectory.
# The user must have 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:

g++
The C++ compiler.

-I$DB2PATH/include
Specify the location of the DB2 include files. For example: $HOME/sqllib/include

-c
Perform compile only; no link. This script file has separate compile and link steps.

The script file contains the following link options:

g++
Use the compiler as a front end for the linker.

-shared
Generate a shared library.

-o $1
Specify the executable.

$1.o
Include the program object file.

-L$DB2PATH/lib
Specify the location of the DB2 static and shared libraries at link-time. For example: $HOME/sqllib/lib. If you do not specify the -L option, /usr/lib:/lib is assumed.

-ldb2
Link with the DB2 library.

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 permissions 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

remote_database
Is the name of the database to which you want to connect. The name could be sample, or its remote alias, or some other name.

userid
Is a valid user ID.

password
Is a valid password.

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.

User-Defined Functions (UDFs)

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. A UDF does not contain embedded SQL statements. So to build a UDF progam, you do not need to connect to a database or precompile and bind the program.

The parameter, $1, specifies the name of your source file. The script file also uses this source file name for the shared library name.



#! /bin/ksh
# bldCCudf script file -- Linux
# Builds a C++ user-defined function library.
# Usage: bldCCudf <prog_name>
 
# Set DB2PATH to where DB2 will be accessed. 
# The default is the 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.
g++ -I$DB2PATH/include -c $1.c
 
# Link the program.
g++ -o $1 $1.o -shared -L$DB2PATH/lib -ldb2 -ldb2apie
 
# Copy the shared library to the function subdirectory.
# The user must have 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:

g++
The C++ compiler.

-I$DB2PATH/include
Specify the location of the DB2 include files. For example: $HOME/sqllib/include.

-c
Perform compile only; no link. This script file has separate compile and link steps.

The script file contains the following link options:

g++
Use the compiler as a front end for the linker.

-o $1
Specify the executable.

$1.o
Include the program object file.

-shared
Generate a shared library.

-L$DB2PATH/lib
Specify the location of the DB2 static and shared libraries at link-time. For example: $HOME/sqllib/lib. If you do not specify the -L option, /usr/lib:/lib is assumed.

-ldb2
Link with the DB2 library.

-ldb2apie
Link with the DB2 API Engine library to allow the use of LOB locators.

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 permissions 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:

   calludf

The calling application calls functions from the udf library.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]