IBM Books

Building Applications for UNIX** Environments


IBM C

The script file bldxlc, in sqllib/samples/c, 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. 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
# bldxlc script file
# Builds a sample C program containing embedded SQL
# Usage: bldxlc <prog_name> [ <db_name> [ < userid> <password> ]]
 
# 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.
xlc -I/usr/lpp/db2_05_00/include -c util.c
 
# Compile the program.
xlc -I/usr/lpp/db2_05_00/include -c $1.c
 
# Link the program.
xlc -o $1 $1.o util.o -ldb2 -L/usr/lpp/db2_05_00/lib


Compile and Link Options for bldxlc

The script file contains the following compile options:

xlc
The IBM XL C compiler.

-Ipath
Specify the location of the DB2 include files. For example: -I/usr/lpp/db2_05_00/include.

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

The script file contains the following link options:

xlc
Use the compiler to link edit.

-o filename
Specify the name of the executable program.

util.o
Include the object file for error checking.

-ldb2
Link to the database manager library.

-Lpath
Specify the location of the DB2 runtime shared libraries. For example: -L/usr/lpp/db2_05_00/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 sample program updat.sqc, do the following:

  1. Go to the window in which you set your environment variables by running db2profile. See "Setting Your Environment" if you need more information.

  2. Start the database manager on the server, if it is not already running, by entering:
    db2start
    

  3. Build the sample program, connecting to the SAMPLE database, by entering:

    bldxlc updat

The result is an executable file updat. You can run the executable file against the SAMPLE database to see how it works by doing the following :

  1. Go to the window in which you set your environment variables by running db2profile.

  2. Start the database manager on the server, if it is not already running, by entering:
    db2start
    

  3. Run the program. If you built the updat sample program, enter:

    updat

Note:To build C applications that do not contain embedded SQL, you can use the script file bldxlcapi. It contains the same compile and link options as bldxlc, but does not connect, prep, bind, or disconnect from the SAMPLE database. It is used to compile and link the DB2 API sample programs written in C.

Building C Stored Procedures

The script file bldxlcsrv, in sqllib/samples/c, 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 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.

The script file uses the source file name, $1, for the shared library name, and for the main entry point to the shared library.



#! /bin/ksh
# bldxlcsrv script file
# Builds a stored procedure
# Usage: bldxlcsrv <stor_proc_name> [ <db_name> [ <userid> <password> ]]
 
# 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.
xlc -I/usr/lpp/db2_05_00/include -c util.c
 
# Compile the program.
xlc -I/usr/lpp/db2_05_00/include -c $1.c
 
# Link the program using the export file $1.exp,
# creating a shared library called $1 with the default
# entry point $1.
xlc -o $1 $1.o util.o -ldb2 -L/usr/lpp/db2_05_00/lib \
    -H512 -T512 -bE:$1.exp -e $1
 
# Copy the shared library to the sqllib/function subdirectory of the DB2 instance.
# Note: this assumes the user has write permission to this directory.
eval "H=~$DB2INSTANCE"
cp $1 $H/sqllib/function


Compile and Link Options for bldxlcsrv

The script file contains the following compile options:

xlc
The IBM XL C compiler.

-Ipath
Specify the location of the DB2 include files. For example: -I/usr/lpp/db2_05_00/include.

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

The script file contains the following link options:

xlc
Use the compiler to link edit.

-o filename
Specify the output as a shared library file.

util.o
Include the object file for error checking.

-ldb2
Link with the database manager library.

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

-H512
Specify output file alignment.

-T512
Specify output file text segment starting address.

-bE:filename.exp
Specify an export file. The export file contains a list of the stored procedures.

-e entry
Specify the default entry point to the shared library.

Refer to your compiler documentation for additional compiler options.

To build the outsrv.sqc stored procedure, do the following:

  1. Go to the window in which you set your environment variables by running db2profile. Refer to "Setting Your Environment" if you need more information.

  2. Start the database manager on the server, if it is not already running, by entering:
    db2start
    

  3. Build the stored procedure, connecting to the SAMPLE database, by entering:
    bldxlcsrv outsrv
    

    The script file copies the stored procedure to the server in the path sqllib/function to indicate that the stored procedure is fenced. If you want the stored procedure to be unfenced, you must move it to the sqllib/function/unfenced directory. These paths are in the home directory of the DB2 instance.
    Note:An unfenced stored procedure or UDF runs in the same address space as the database manager and results in increased performance when compared to a fenced stored procedure or UDF, which runs in an address space isolated from the database manager. With unfenced stored procedures or UDFs there is a danger that user code could accidentally or maliciously damage the database control structures. Therefore, you should only run unfenced stored procedures or UDFs when you need to maximize the performance benefits. Ensure these programs are thoroughly tested before running them as unfenced. Refer to the Embedded SQL Programming Guide for more information about fenced and not fenced stored procedures.

  4. If necessary, set the file mode 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 bldxlc script file. Refer to "IBM C" for details.

To run the stored procedure, do the following:

  1. Go to the window in which you set your environment variables by running db2profile.

  2. Start the database manager on the server, if it is not already running, by entering:
    db2start
    

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

Coding and Compiling Stored Procedures

This section provides a general discussion about coding stored procedures, and the compiler options you can use.

Relationship to Your CALL Statement

The Embedded SQL Programming Guide describes how to code your stored procedure. The SQL Reference describes how to invoke your stored procedure at the location of a database using the CALL statement. This section ties how you compile and link your stored procedure to the information you provide in the CALL statement.

When you compile and link your program, you can identify functions in two ways:

Building C User-Defined Functions (UDFs)

The script file bldxlcudf, in sqllib/samples/c, contains the commands to build a UDF. UDFs are compiled like stored procedures, but you do not need to connect to a database or precompile and bind the program.
Note:A UDF does not contain embedded SQL statements. Rather, the application that uses the UDF contains the statements, such as calludf.

The first parameter, $1, specifies the name of your source file.

The script file uses the source file, $1, for the shared library name, and for the default entry point to the shared library.



#! /bin/ksh
# bldxlcudf script file
# Builds a sample C UDF library.
# Usage: bldxlcudf <prog_name>
 
# Compile the program.
xlc -I/usr/lpp/db2_05_00/include -c $1.c
 
# Link the program.
xlc -o $1 $1.o -ldb2 -ldb2apie -L/usr/lpp/db2_05_00/lib -H512 -T512 -bE:$1.exp -e $1
 
# Copy the shared library to the sqllib/function subdirectory of the DB2 instance.
# Note: this assumes the user has write permission to this directory.
eval "H=~$DB2INSTANCE"
cp $1 $H/sqllib/function


Compile and Link Options for bldxlcudf

The script file contains the following compile options:

xlc
The IBM XL C compiler.

-Ipath
Specify the location of the DB2 include files. For example: -I/usr/lpp/db2_05_00/include.

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

The script file contains the following link options:

xlc
Use the compiler to link edit.

-o filename
Specify the output as a shared library file.

-ldb2
Link with the database manager library.

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

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

-H512
Specify output file alignment.

-T512
Specify output file text segment starting address.

-bE:filename.exp
Specify an export file. The export file contains a list of the UDFs.

-e entry
Specify the default entry point to the shared library.

Refer to your compiler documentation for additional compiler options. Refer to "Coding and Compiling UDFs" for a general discussion about compiler options and UDFs.

To build the user-defined function udf, do the following:

  1. Go to the window in which you set your environment variables by running db2profile. Refer to "Setting Your Environment" if you need more information.

  2. Build the UDF by entering:
    bldxlcudf udf
    

    The script file copies the UDF to the server in the path sqllib/function to indicate that the UDF is fenced. If you want the UDF to be unfenced, you must move it to the sqllib/function/unfenced directory. These paths are in the home directory of the DB2 instance.
    Note:An unfenced UDF or stored procedure runs in the same address space as the database manager and results in increased performance when compared to a fenced UDF or stored procedure, which runs in an address space isolated from the database manager. With unfenced UDFs or stored procedures there is a danger that user code could accidentally or maliciously damage the database control structures. Therefore, you should only run unfenced UDFs or stored procedures when you need to maximize the performance benefits. Ensure these programs are thoroughly tested before running them as unfenced. Refer to the Embedded SQL Programming Guide for more information about fenced and not fenced UDFs.

  3. If necessary, set the file mode 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 calludf using the bldxlc script file. Refer to "IBM C" for details.

To run the UDF, do the following:

  1. Go to the window in which you set your environment variables by running db2profile.

  2. Start the database manager on the server, if it is not already running, by entering:
    db2start
    

  3. Run the sample calling application by entering:

    calludf

    The calling application calls functions from the udf library.

    After you run the calling application, you can also invoke the UDF interactively using the command line processor like this:

    db2 "SELECT name, DOLLAR(salary), SAMP_MUL(DOLLAR(salary), FACTOR(1.2)) FROM staff"
    

    You do not have to type the command line processor commands in uppercase.

Coding and Compiling UDFs

This section provides a general discussion about coding UDFs, and the compiler options you can use.

Relationship to Your CREATE FUNCTION Statement

The Embedded SQL Programming Guide describes how to code your UDF. The SQL Reference describes how to register your UDF with DB2 using the CREATE FUNCTION statement. This section ties how you compile and link your UDF to the information you provide in the EXTERNAL NAME clause of the CREATE FUNCTION statement.

When you compile and link your program, you can identify functions in two ways:

Multi-threaded Applications on AIX Version 4

Multi-threaded applications on AIX Version 4 need to be compiled and linked with the xlc_r compiler instead of the xlc compiler, or with the xlC_r compiler instead of the xlC compiler.


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

[ DB2 List of Books | Search the DB2 Books ]