Application Building Guide

IBM C Set++

This section contains the following topics:

DB2 API and Embedded SQL Applications

The build file, bldapp, in sqllib/samples/cpp, contains the commands to build DB2 API and embedded SQL applications.

The first parameter, $1, specifies the name of your source file. This is the only required parameter, and the only one needed for DB2 API programs that do not contain embedded SQL. Building embedded SQL programs requires a connection to the database so three optional parameters are also provided: 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.

For an embedded SQL program, bldapp passes the parameters to the precompile and bind file, embprep. If no database name is supplied, the default sample database is used. The user ID and password parameters are only needed if the instance where the program is built is different from the instance where the database is located.

#! /bin/ksh
# bldapp script file -- AIX
# Builds a C++ application program.
# Usage: bldapp <prog_name> [ <db_name> [ <userid> <password> ]] 
 
# Set DB2PATH to where DB2 will be accessed. 
# The default is the standard instance path. 
DB2PATH=$HOME/sqllib
 
# To compile 64 bit programs, uncomment the following line.
# BUILD_64BIT=true
 
if [ "$BUILD_64BIT" != "" ]
then
  CFLAGS_64=-q64
else 
  CFLAGS_64=
fi
 
# If an embedded SQL program, precompile and bind it.
if [[ -f $1".sqC" ]] 
then
  embprep $1 $2 $3 $4
  # Compile the utilemb.C error-checking utility.
  xlC $CFLAGS_64 -I$DB2PATH/include -c utilemb.C
else
  # Compile the utilapi.C error-checking utility.
  xlC $CFLAGS_64 -I$DB2PATH/include -c utilapi.C
fi
 
# Compile the program.
xlC $CFLAGS_64 -I$DB2PATH/include -c $1.C
 
if [[ -f $1".sqC" ]]
then
  # Link the program with utilemb.o
  xlC $CFLAGS_64 -o $1 $1.o utilemb.o -ldb2 -L$DB2PATH/lib
else
  # Link the program with utilapi.o
  xlC $CFLAGS_64 -o $1 $1.o utilapi.o -ldb2 -L$DB2PATH/lib
fi


Compile and Link Options for bldapp

Compile Options:

xlC
The IBM C Set++ compiler.

$CFLAGS_64
Contains "-q64" value if 'BUILD_64BIT=true' is uncommented; otherwise, it contains no value.

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

-c
Perform compile only; no link. Compile and link are separate steps.

Link options:

xlC
Use the compiler as a front end for the linker.

$CFLAGS_64
Contains "-q64" value if 'BUILD_64BIT=true' is uncommented; otherwise, it contains no value.

-o $1
Specify the executable program.

-o $1
Specify the program object file.

utilapi.o
Include the API utility object file for non-embedded SQL programs.

utilemb.o
Include the embedded SQL utility object file for embedded SQL programs.

-ldb2
Link with the database manager library.

-L$DB2PATH/lib
Specify the location of the DB2 runtime shared libraries. For example: $HOME/sqllib/lib. If you do not specify the -L option, the compiler assumes the following path /usr/lib:/lib.

Refer to your compiler documentation for additional compiler options.

To build the non-embedded SQL sample program client from the source file client.C , enter:

   bldapp client

The result is an executable file, client. You can run the executable file against the sample database by entering:

   client

Building and Running Embedded SQL Applications

There are three ways to build the embedded SQL application, updat, from the source file updat.sqC :

  1. If connecting to the sample database on the same instance, enter:
       bldapp updat
    
  2. If connecting to another database on the same instance, also enter the database name:
       bldapp updat database
    
  3. If connecting to a database on another instance, also enter the user ID and password of the database instance:
       bldapp updat database userid password
    

The result is an executable file, updat.

There are three ways to run this embedded SQL application:

  1. If accessing the sample database on the same instance, simply enter the executable name:
       updat
    
  2. If accessing another database on the same instance, enter the executable name and the database name:
       updat database
    
  3. If accessing a database on another instance, enter the executable name, database name, and user ID and password of the database instance:
       updat database userid password
    

Embedded SQL Stored Procedures

Note:Please see the information for building C++ stored procedures in "C++ Considerations for UDFs and Stored Procedures".

The script file bldsrv, in sqllib/samples/cpp, contains the commands to build a 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 stored procedure function that is the entry point to the shared library. The third parameter, $3, specifies the name of the database to which you want to connect. Since the stored procedure must be build on the same instance where the database resides, you do not need parameters for user ID and password.

Only the first two parameters, source file name and entry point, are required. Database name is optional. If no database name is supplied, the program uses the default sample database.

#! /bin/ksh
# bldsrv script file -- AIX
# Builds a C++ stored procedure
# Usage: bldsrv <prog_name> [ <db_name> ] 
 
# Set DB2PATH to where DB2 will be accessed. 
# The default is the standard instance path. 
DB2PATH=$HOME/sqllib
 
# Precompile and bind the program.
embprep $1 $2
 
# To compile 64 bit programs, uncomment the following line.
# BUILD_64BIT=true
 
if [ "$BUILD_64BIT" != "" ]
then
  CFLAGS_64=-q64
  LFLAGS_64=-X64
else 
  CFLAGS_64=
  LFLAGS_64=
fi
 
# Compile the program.
xlC $CFLAGS_64 -I$DB2PATH/include -c $1.C
 
# Link using export file $1.exp, creating shared library $1
makeC++SharedLib $LFLAGS_64 -p 1024 -o $1 $1.o -L$DB2PATH/lib -ldb2 -E $1.exp 
 
# Copy the shared library to the sqllib/function subdirectory.
# Note: the user must have write permission to this directory.
rm -f $DB2PATH/function/$1
cp $1 $DB2PATH/function


Compile and Link Options for bldsrv

Compile Options:

xlC
The IBM C Set++ compiler.

$CFLAGS_64
Contains "-q64" value if 'BUILD_64BIT=true' is uncommented; otherwise, it contains no value.

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

-c
Perform compile only; no link. Compile and link are separate steps.

Link options:

makeC++SharedLib
Linker script for stored procedures with static constructors.

$LFLAGS_64
Contains "-X64" value if 'BUILD_64BIT=true' is uncommented; otherwise, it contains no value.

-p 1024
Set the priority to the arbitrary value of 1024.

-o $1
Specify the output as a shared library file.

$1.o
Specify the program object file.

-L$DB2PATH/lib
Specify the location of the DB2 runtime shared libraries. For example: $HOME/sqllib/lib. If you do not specify the -L option, the compiler assumes the following path: /usr/lib:/lib.

-ldb2
Link with the database manager library.

-E $1.exp
Specify an export file. The export file contains a list of the stored procedures.

-e $2
Specify an entry point to the shared library.

Refer to your compiler documentation for additional compiler options.

To build the sample program spserver from the source file spserver.sqC , if connecting to the sample database, enter the build file name, program name, and the name of the stored procedure function that is the entry point to the shared library:

    bldsrv spserver outlanguage

If connecting to another database, also enter the database name:

    bldsrv spserver outlanguage database

The script file copies the shared library to the server in the path sqllib/function.

Next, catalog the stored procedures by running the spcreate.db2 script on the server. First, connect to the database:

   db2 connect to sample

If the stored procedures were previously cataloged, you can drop them with this command:

   db2 -td@ -vf spdrop.db2

Then catalog them with this command:

   db2 -td@ -vf spcreate.db2

Then, stop and restart the database to allow the new shared library to be recognized. If necessary, set the file mode for the shared library so the DB2 instance can access it.

Once you build the shared library, spserver, you can build the client application spclient that calls the stored procedures within it.

You can build spclient by using the script file, bldapp. Refer to "DB2 API and Embedded SQL Applications" for details.

To call the stored procedure, run the sample client application by entering:

spclient database userid password

where

database
Is the name of the database to which you want to connect. The name could be sample, or its alias, or another database name.

userid
Is a valid user ID.

password
Is a valid password.

The client application accesses the shared library, spserver, which executes a number of stored procedure functions on the server database, and then returns the output to the client application.

User-Defined Functions (UDFs)

Note:Please see the information for building C++ UDFs in "C++ Considerations for UDFs and Stored Procedures".

The script file bldudf, in sqllib/samples/cpp, contains the commands to build a UDF. UDFs cannot contain embedded SQL statements. Therefore, to build a UDF program, you do not need to connect to a database, precompile, and bind the program.

Parameter $1 specifies the name of your source file. Parameter $2 specifies the user-defined function that is the entry point to the shared library. The script file uses the source file name, $1, for the shared library name.

#! /bin/ksh
# bldudf script file -- AIX
# Builds a C++ UDF library
# Usage: bldudf <prog_name>
 
# Set DB2PATH to where DB2 will be accessed. 
# The default is the standard instance path. 
DB2PATH=$HOME/sqllib
 
# To compile 64 bit programs, uncomment the following line.
# BUILD_64BIT=true
 
if [ "$BUILD_64BIT" != "" ]
then
  CFLAGS_64=-q64
  LFLAGS_64=-X64
else
  CFLAGS_64=
  LFLAGS_64=
fi
 
# Compile the program. 
if [[ -f $1".c" ]]
then
  xlC $CFLAGS_64 -I$DB2PATH/include -c $1.c
elif [[ -f $1".C" ]]
then
  xlC $CFLAGS_64 -I$DB2PATH/include -c $1.C
fi
 
# Link using export file $1.exp, creating shared library $1
makeC++SharedLib $LFLAGS_64 -p 1024  -o $1 $1.o -L$DB2PATH/lib -ldb2 -ldb2apie -E $1.exp
 
# Copy the shared library to the sqllib/function subdirectory.
# Note: the user must have write permission to this directory.
rm -f $DB2PATH/function/$1
cp $1 $DB2PATH/function


Compile and Link Options for bldudf

Compile Options:

xlC
The IBM C Set++ compiler.

$CFLAGS_64
Contains "-q64" value if 'BUILD_64BIT=true' is uncommented; otherwise, it contains no value.

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

-c
Perform compile only; no link. This book assumes that compile and link are separate steps.

Link Options:

makeC++SharedLib
Linker script for stored procedures with static constructors.

$LFLAGS_64
Contains "-X64" value if 'BUILD_64BIT=true' is uncommented; otherwise, it contains no value.

-p 1024
Set the priority to the arbitrary value of 1024.

-o $1
Specify the output as a shared library file.

$1.o
Specify the program object file.

-L$DB2PATH/lib
Specify the location of the DB2 runtime shared libraries. For example: $HOME/sqllib/lib. If you do not specify the -L option, the compiler assumes the following path: /usr/lib:/lib.

-ldb2
Link with the database manager library.

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

-E $1.exp
Specify an export file. The export file contains a list of the stored procedures.

Refer to your compiler documentation for additional compiler options. Refer to "UDFs and the CREATE FUNCTION Statement" for more information on creating UDFs.

To build the user-defined function program udfsrv from the source file udfsrv.c , enter the build file name, program name, and UDF function that is the entry point to the shared library:

   bldudf udfsrv ScalarUDF

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 udfsrv, you can build the client application, udfcli, that calls it. You can build the udfcli program from the udfcli.sqC source file in sqllib/samples/cpp using the script file bldapp. Refer to "DB2 API and Embedded SQL Applications" for details.

To call the UDF, run the sample calling application by entering the executable name:

   udfcli

The calling application calls the ScalarUDF function in the udfsrv library.

Multi-threaded Applications

C++ multi-threaded applications on AIX Version 4 need to be compiled and linked with the xlC_r compiler instead of the xlC compiler or, for C, with the xlc_r compiler instead of the xlc compiler. If you are using AIX 4.3 or later for 32-bit applications, use the xlC_r7 or xlc_r7 compiler. The _r versions (as well as the other multi-threaded compiler front ends) set the appropriate preprocessor defines for multi-threaded compilation and supply the appropriate threaded library names to the linker.

Additional information about compiler and link flag settings using the multi-threaded compiler front ends can be obtained from /etc/xlC.cfg when using the 3.1 compiler, or /etc/ibmcxx.cfg when using the 3.6 or newer compilers.

The script file bldmt, in sqllib/samples/cpp, contains the commands to build an embedded SQL multi-threaded 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. 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 
# bldmt script file -- AIX
# Builds a C++ multi-threaded embedded SQL program
# Usage: bldmt <prog_name> [ <db_name> [ <userid> <password> ]] 
 
# Set DB2PATH to where DB2 will be accessed. 
# The default is the standard instance path. 
DB2PATH=$HOME/sqllib
 
# Precompile and bind the program.
embprep $1 $2 $3 $4
 
# To compile 64 bit programs, uncomment the following line.
# BUILD_64BIT=true
 
if [ "$BUILD_64BIT" != "" ]
then
  CFLAGS_64=-q64
else 
  CFLAGS_64=
fi
 
# Compile the program.
xlC_r $CFLAGS_64 -I$DB2PATH/include -c $1.C
 
# Link the program.
xlC_r $CFLAGS_64 -o $1 $1.o -L$DB2PATH/lib -ldb2

Besides the xlC_r compiler, discussed above, and no utility file linked in, the compile and link options are the same as those used in the embedded SQL script file, bldapp. For information on these options, see "DB2 API and Embedded SQL Applications".

To build the multi-threaded sample program, thdsrver, from the source file thdsrver.sqC , enter:

   bldmt thdsrver

The result is an executable file, thdsrver. To run the executable file against the sample database, enter the executable name:

   thdsrver


[ Top of Page | Previous Page | Next Page ]