Application Building Guide

MIPSpro C++

This section includes the following topics:

DB2 API and Embedded SQL Applications

The script file bldapp, in sqllib/samples/cpp, contains the commands to build a DB2 application program.

The first parameter, $1, specifies the name of your source file. This is the only required parameter for non-embedded SQL applications. 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.

#! /bin/ksh
# bldapp script file -- Silicon Graphics IRIX
# 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 with n32 object support, uncomment the following line.
# IRIX_OBJECT_MODE=-n32
 
if [ "$IRIX_OBJECT_MODE" = "-n32" ] ; then
  # Link with db2 n32 object type libraries.
  DB2_LIBPATH=$DB2PATH/lib32
else
  # Link with db2 o32 object type libraries.
  DB2_LIBPATH=$DB2PATH/lib
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.
  CC $IRIX_OBJECT_MODE -I$DB2PATH/include -c utilemb.C
else
  # Compile the utilapi.c error-checking utility.
  CC $IRIX_OBJECT_MODE -I$DB2PATH/include -c utilapi.C  
fi
 
# Compile the program.                              
CC $IRIX_OBJECT_MODE -I$DB2PATH/include -c $1.C            
 
if [[ -f $1".sqc" ]]
then                     
  # Link the program with utilemb.o
  CC $IRIX_OBJECT_MODE -o $1 $1.o utilemb.o -L$DB2_LIBPATH -rpath $DB2_LIBPATH -lm -ldb2
else
  # Link the program with utilapi.o
  CC $IRIX_OBJECT_MODE -o $1 $1.o utilapi.o -L$DB2_LIBPATH -rpath $DB2_LIBPATH -lm -ldb2
fi


Compile and Link Options for bldapp

Compile Options:

CC
Use the C++ compiler.

$IRIX_OBJECT_MODE
Contains "-n32" if 'IRIX_OBJECT_MODE=-n32' 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 script has separate compile and link steps.

Link Options:

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

$IRIX_OBJECT_MODE
Contains "-n32" if 'IRIX_OBJECT_MODE=-n32' is uncommented; otherwise, it contains no value.

-o $1
Specify the executable.

$1.o
Include the program object file.

utilemb.o
If an embedded SQL program, include the embedded SQL utility object file for error checking.

utilapi.o
If a non-embedded SQL program, include the DB2 API utility object file for error checking.

-L$DB2_LIBPATH
Specify the location of the DB2 static and shared libraries at link-time. For o32 object type, it points to: $DB2PATH/lib; For n32 object type, it points to: $DB2PATH/lib32. If you do not specify the -L option, /usr/lib:/lib is assumed.

-rpath $DB2_LIBPATH
Specify the location of the DB2 shared libraries at run-time. For o32 object type, it points to: $DB2PATH/lib; For n32 object type, it points to: $DB2PATH/lib32.

-lm
Link with the math library.

-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 , include parameters for the database, and the user ID and password for the instance where the database is located:

   bldapp updat database userid password

The result is an executable file, updat, To run the executable file, enter the executable name, database name, and user ID and password for the instance where the database is located:

   updat database userid password

Embedded SQL Client Applications for Stored Procedures

Stored procedures are programs that access the database and return information to the client application. You compile and store stored procedures on the server. The server runs on another platform.

To build the embedded SQL stored procedure spserver on a DB2-supported platform server, refer to the "Building Applications" chapter for that platform in this book. For other servers accessible by DB2 clients, see "Supported Servers".

Once you build the stored procedure spserver, you can build the client application that calls the stored procedure. You can build spclient from the source file spclient.sqC , by using the script file bldapp. Refer to "DB2 API and Embedded SQL Applications" for details.

To call the stored procedure, run the client application by entering the executable name, database name, and user ID and password for the instance where the database is located:

   spclient database userid password

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

Embedded SQL Client Application for UDFs

User-defined functions (UDFs) are your own scalar functions that you compile and store on the server. The server runs on another platform. To build the user-defined function program, udfsrv , on a DB2-supported platform server, refer to the "Building Applications" chapter for that platform in this book. For other servers accessible by DB2 clients, see "Supported Servers".

Once you build udfsrv, you can build the embedded SQL client application, udfcli, that calls it, 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 program, run the calling application by entering the executable name, database name, and user ID and password for the instance where the database is located:

   udfcli database userid password

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

Multi-threaded Applications

Multi-threaded applications on Silicon Graphics IRIX need to be linked with the POSIX threads version of the DB2 library for either the o32 or n32 object types, using the -ldb2_th and -lpthread link options.

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. The third parameter, $3, specifies the user ID for the database, and $4, specifies the password.

#! /bin/ksh
# bldmt script file -- Silicon Graphics IRIX
# 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
 
# To compile with n32 object support, uncomment the following line.
# IRIX_OBJECT_MODE=-n32
 
if [ "$IRIX_OBJECT_MODE" = "-n32" ] ; then
  # Link with db2 n32 object type libraries.
  DB2_LIBPATH=$DB2PATH/lib32
else
  # Link with db2 o32 object type libraries.
  DB2_LIBPATH=$DB2PATH/lib
fi
 
# Precompile and bind the program.
embprep $1 $2 $3 $4
 
# Compile the program.                              
CC $IRIX_OBJECT_MODE -I$DB2PATH/include -c $1.C            
 
# Link the program.
CC $IRIX_OBJECT_MODE -o $1 $1.o -L$DB2_LIBPATH -rpath $DB2_LIBPATH -lm -ldb2_th -lpthread

Besides the -ldb2_th and -lpthread link options, discussed above, and the absence of a utility file linked in, the other compile and link options are the same as those used for the embedded SQL script file, bldapp. For information on these options, see "DB2 API and Embedded SQL Applications".

To build the sample program, thdsrver, from the source file thdsrver.sqC , include parameters for the database, and the user ID and password for the instance where the database is located:

   bldmt thdsrver database userid password

The result is an executable file, thdsrver.

To run the executable file against the sample database, enter the executable name, database name, and user ID and password for the instance where the database is located:

   thdsrver database userid password


[ Top of Page | Previous Page | Next Page ]