Application Building Guide

MIPSpro C

This section explains how to use MIPSpro C with the following kinds of DB2 interfaces:

DB2 CLI Applications

The script file bldcli in sqllib/samples/cli contains the commands to build a DB2 CLI program. The parameter, $1, specifies the name of your source file.

This is the only required parameter, and the only one needed for CLI 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.

If the program contains embedded SQL, indicated by the .sqc extension, then the embprep script is called to precompile the program, producing a program file with a .c extension.

#! /bin/ksh
# bldcli script file -- Silicon Graphics IRIX
# Builds a CLI program with MIPSpro C.
# Usage: bldcli <prog_name> [ <db_name> [ <userid> <password> ]] 
 
# Set DB2PATH to where DB2 will be accessed. 
# The default is the instance path. 
DB2PATH=$HOME/sqllib
 
# If an embedded SQL program, precompile and bind it.
if [[ -f $1".sqc" ]]
then
  embprep $1 $2 $3 $4
fi
 
# 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
 
# Compile the error-checking utility.
cc $IRIX_OBJECT_MODE -I$DB2PATH/include -c utilcli.c
  
# Compile the program.                              
cc $IRIX_OBJECT_MODE -I$DB2PATH/include -c $1.c
 
# Link the program.
cc $IRIX_OBJECT_MODE -o $1 $1.o utilcli.o -L$DB2_LIBPATH -rpath $DB2_LIBPATH -lm -ldb2


Compile and Link Options for bldcli

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 book assumes that compile and link are separate 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.

utilcli.o
Include the 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 tbinfo from the source file tbinfo.c , enter:

 
   bldcli tbinfo

The result is an executable file tbinfo. You can run the executable file by entering the executable name, database name, and user ID and password for the instance where the database is located:

   tbinfo database userid password

Building and Running Embedded SQL Applications

To build dbusemx from the source file dbusemx.sqc , include parameters for the database, and the user ID and password for the instance where the database is located:

   bldcli dbusemx database userid password

The result is an executable file, dbusemx.

To run an embedded SQL application, enter the executable name, database name, and user ID and password for the instance where the database is located:

   dbusemx database userid password

DB2 CLI Applications with DB2 APIs

DB2 includes CLI sample programs that use DB2 APIs to create and drop a database in order to demonstrate using CLI functions on more than one database. The descriptions of the CLI sample programs in Table 7 indicates the samples that use DB2 APIs.

The script file bldapi in sqllib/samples/cli contains the commands to build a DB2 CLI program with DB2 APIs. This file compiles and links in the utilapi utility file, which contains the DB2 APIs to create and drop a database. This is the only difference between this file and the bldcli script. Please see "DB2 CLI Applications" for the compile and link options common to both bldapi and bldcli.

To build the sample program dbmconn from the source file dbmconn.c , enter:

 
   bldapi dbmconn

The result is an executable file dbmconn. You can run the executable file by entering the executable name, database name, and user ID and password for the instance where the database is located:

   dbmconn database userid password

DB2 CLI 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 DB2 CLI 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, spclient, from the source file spclient.c , by using the script file bldcli. Refer to "DB2 CLI Applications" for details.

You can call the stored procedure 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 output is returned to the client application.

DB2 CLI Client Applications for UDFs

User-defined functions (UDFs) are your own scalar and table 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 DB2 CLI client application, udfcli, that calls it, from the udfcli.c source file in sqllib/samples/cli, using the DB2 CLI script file bldcli. Refer to "DB2 CLI 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.

DB2 API and Embedded SQL Applications

The script file bldapp, in sqllib/samples/c, 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, 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 -- 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 book assumes that compile and link are separate 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 DB2 API non-embedded SQL sample program, client, from the source file client.c , enter:

   bldapp client

The result is an executable file, client.

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

   client database userid password

Building and Running Embedded SQL Applications

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 against the sample database, 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 stored procedure library, spserver, and executes a number of stored procedure functions on the server database. The output is returned to the client application.

Client Applications for User-defined Functions (UDFs)

User-defined functions (UDFs) are your own scalar and table 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/c 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/c, 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 being 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, 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 ]