Application Building Guide

SPARCompiler C

This section includes the following topics:

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 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 -- Solaris
# Builds a DB2 CLI program.
# Usage: bldcli <prog_name> [ <db_name> [ <userid> <password> ]] 
 
# Set DB2PATH to where DB2 will be accessed. 
# The default is the standard 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 64 bit programs, uncomment the following line.
# BUILD_64BIT=true
 
if [ "$BUILD_64BIT" != "" ]
then
  CFLAGS_64=-xarch=v9
else
  CFLAGS_64=
fi
 
# Compile the error-checking utility.
cc $CFLAGS_64 -I$DB2PATH/include -c utilcli.c
 
# Compile the program.
cc $CFLAGS_64 -I$DB2PATH/include  -c $1.c
 
# Link the program.
cc $CFLAGS_64 -o $1 $1.o utilcli.o -L$DB2PATH/lib -R$DB2PATH/lib -ldb2


Compile and Link Options for bldcli

Compile Options:

cc
Use the C compiler.

$CFLAGS_64
Contains "-xarch=v9" 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 script has separate compile and link steps.

Link Options:

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

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

-o $1
Specify the executable program.

$1.o
Include the program object file.

utilcli.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

-R$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 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:

   tbinfo

Building and Running Embedded SQL Applications

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

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

The result is an executable file, dbusemx.

There are three ways to run the embedded SQL application:

  1. If accessing the sample database on the same instance, simply enter the executable name:
       dbusemx
    
  2. If accessing another database on the same instance, enter the executable name and the database name:
       dbusemx database
    
  3. If accessing a database on another instance, enter the executable name, database name, and user ID and password of the database instance:
       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:

   dbmconn

DB2 CLI Stored Procedures

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

#! /bin/ksh
# bldclisp script file -- Solaris
# Builds a DB2 CLI stored procedure.
# Usage: bldclisp <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=-xarch=v9
else
  CFLAGS_64=
fi
 
# Compile the error-checking utility.
cc $CFLAGS_64 -Kpic -I$DB2PATH/include -c utilcli.c
 
# Compile the program.
cc $CFLAGS_64 -Kpic -I$DB2PATH/include -c $1.c
  
# Link the program.
cc $CFLAGS_64 -G -o $1 $1.o utilcli.o -L$DB2PATH/lib -R$DB2PATH/lib -ldb2
 
# 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 bldclisp

Compile options:

cc
The C compiler.

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

-Kpic
Generate position-independent code for shared libraries.

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

$CFLAGS_64
Contains "-xarch=v9" value if 'BUILD_64BIT=true' 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$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.

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

-ldb2
Link with the DB2 library.

-G
Generate a shared library.

Refer to your compiler documentation for additional compiler options.

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

    bldclisp spserver

The script file copies the stored procedure 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 stored procedure spserver, you can build the CLI client application spclient that calls the stored procedure.

You can build spclient by using the script file, bldcli. Refer to "DB2 CLI 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 stored procedure library, spserver, which executes a number of stored procedure functions on the server database. The output is returned to the client application.

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 for 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 -- Solaris
# 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=-xarch=v9
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.
  cc $CFLAGS_64 -I$DB2PATH/include -c utilemb.c
else
  # Compile the utilapi.c error-checking utility.
  cc $CFLAGS_64 -I$DB2PATH/include -c utilapi.c
fi
 
# Compile the program.
cc $CFLAGS_64 -I$DB2PATH/include -c $1.c
 
if [[ -f $1".sqc" ]]
then
  # Link the program with utilemb.o
  cc $CFLAGS_64 -o $1 $1.o utilemb.o -L$DB2PATH/lib -R$DB2PATH/lib -ldb2
else
  # Link the program with utilapi.o
  cc $CFLAGS_64 -o $1 $1.o utilapi.o -L$DB2PATH/lib -R$DB2PATH/lib -ldb2
fi


Compile and Link Options for bldapp

Compile Options:

cc
The C compiler.

$CFLAGS_64
Contains "-xarch=v9" 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 script has separate compile and link steps.

Link Options:

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

$CFLAGS_64
Contains "-xarch=v9" value if 'BUILD_64BIT=true' 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 not an embedded SQL program, include the DB2 API 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.

-R$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 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:

   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

The script file, bldsrv, in sqllib/samples/c, 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. Since the stored procedure must be build on the same instance where the database resides, there are no parameters for user ID and password.

Only the first parameter, source file name, is required. Database name is optional. If no database name is supplied, the program uses the default sample database.

#! /bin/ksh
# bldsrv script file -- Solaris
# 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=-xarch=v9
else
  CFLAGS_64=
fi
 
# Compile the program.
cc $CFLAGS_64 -Kpic -I$DB2PATH/include -c $1.c
  
# Link the program and create a shared library
cc $CFLAGS_64 -G -o $1 $1.o -L$DB2PATH/lib -R$DB2PATH/lib -ldb2
 
# 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:

cc
The C compiler.

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

-Kpic
Generate position-independent code for shared libraries.

-I$DB2PATH/include
Specify the location of the DB2 include files. For example: -I$DB2PATH/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.

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

-G
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.

-R$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 spserver from the source file spserver.sqc , if connecting to the sample database, enter:

    bldsrv spserver

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

    bldsrv spserver database

The script file copies the stored procedure to the sqllib/function directory.

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 stored procedure spserver, you can build the client application spclient that calls the stored procedure.

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

User-Defined Functions (UDFs)

The script file, bldudf, in sqllib/samples/c, 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
# bldudf script file -- Solaris
# 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=-xarch=v9
else
  CFLAGS_64=
fi
 
# Compile the program.
cc $CFLAGS_64 -Kpic -I$DB2PATH/include -c $1.c
 
# Link the program and create a shared library.
cc $CFLAGS_64 -G -o $1 $1.o -L$DB2PATH/lib -R$DB2PATH/lib -ldb2 -ldb2apie
 
# 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:

cc
The C compiler.

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

-Kpic
Generate position-independent code for shared libraries.

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

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

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

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

-ldb2
Link with the DB2 library.

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

-G
Generate a shared library.

Refer to your compiler documentation for additional compiler options.

To build the user-defined function program udfsrv from the source file udfsrv.c , enter:

   
   bldudf udfsrv

The script file copies the UDF to the sqllib/function directory.

If necessary, set the file mode for the UDF so the client application can access it.

Once you build udfsrv, you can build the client application, udfcli, that calls it. DB2 CLI and embedded SQL versions of this program are provided.

You can build the DB2 CLI udfcli program from the source file udfcli.c , in sqllib/samples/cli, using the script file bldcli. Refer to "DB2 CLI Applications" for details.

You can build the embedded SQL udfcli program from the source file udfcli.sqc , in sqllib/samples/c, 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 from the udfsrv library.

Multi-threaded Applications

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, 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. 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 -- Solaris
# 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=-xarch=v9
else
  CFLAGS_64=
fi
 
# Compile the program.                                 
cc $CFLAGS_64 -mt -D_POSIX_PTHREAD_SEMANTICS -I$DB2PATH/include -c $1.c
 
# Link the program.                                      
cc $CFLAGS_64  -mt -o $1 $1.o -L$DB2PATH/lib -R$DB2PATH/lib -ldb2 -lpthread

Besides the -mt, -D_POSIX_PTHREAD_SEMANTICS, and -lpthread 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 , enter:

   bldmt thdsrver

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

   thdsrver


[ Top of Page | Previous Page | Next Page ]