IBM Books

Application Building Guide


IBM VisualAge C++ Version 3.5

This section contains the following topics:

Note:The VisualAge C++ compiler is used for both C and C++ sample programs supplied in the %DB2PATH\samples\c and %DB2PATH\samples\cpp directories. The batch files in both these directories contain commands to accept either a C or C++ source file, depending on the file extension. By default, the C++ commands are commented out in the batch files in %DB2PATH\samples\c, and the C commands are commented out in the batch files in %DB2PATH\samples\cpp. Except for the first topic where batch files are not used, this section demonstrates building programs using the C batch files.

DB2 API Applications

The batch file bldvaapi.bat, in %DB2PATH%\samples\c, and in %DB2PATH%\samples\cpp, contains the commands to build a DB2 API program.

Ensure the LIB environment variable points to %DB2PATH%\lib like this:

   set LIB=%DB2PATH%\lib;%LIB%

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

@echo off
rem bldvaapi.bat file
rem Builds a DB2 API program using the IBM VisualAge C++ compiler.
rem USAGE: bldvaapi <prog_name>
 
rem  Compile the program. 
icc -c -Ti -W1 %1.c util.c
rem For C++, comment out the above line and uncomment the following:
rem icc -c -Ti -W1 %1.c util.cxx
 
rem  Link the program.
ilink /MAP /DEBUG /ST:32000 /PM:VIO %1.obj util.obj db2api.lib
 
goto exit
 
:error
echo Usage: bldvaapi <prog_name>
:exit
@echo on

Compile and Link Options for bldvaapi

The batch file contains the following compile options:

icc
The IBM VisualAge C++ compiler.

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

-Ti
Generate debugger information.

-W1
Output warning, error, and severe and unrecoverable error messages.

The batch file contains the following link options:

ilink
Use the resource linker to link edit.

/MAP
Generate a map file.

/DEBUG
Include debugging information.

/ST:32000
Specify a stack size of at least 32 000.

/PM:VIO
Enable the program to run in a window or in a full screen.

%1.obj
Include the object file.

util.obj
Include the error-checking utility object file.

db2api.lib
Link with the DB2 library.

Refer to your compiler documentation for additional compiler options.

To build the sample program client from the C source file client.c , or the C++ file client.cxx , enter:

   bldvaapi client

The result is an executable file client.exe. You can run the executable file against the SAMPLE database by entering the executable name (without the extension):

   client

DB2 CLI Applications

The batch file bldvcli.bat, in %DB2PATH%\samples\cli, contains the commands to build a DB2 CLI program in IBM VisualAge C++.

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

@echo off
rem  bldvcli batch file - Windows 32-bit Operating Systems
rem  Builds a CLI program with IBM VisualAge C++.
rem  Usage: bldvcli prog_name
 
if "%1" == "" goto error
 
rem Compile the error-checking utility.
icc -c -Ti -W1 samputil.c
 
rem  Compile the program.
icc -c -Ti -W1 %1.c
 
rem  Link the program.
ilink /MAP /DEBUG /ST:32000 /PM:VIO %1.obj samputil.obj db2cli.lib
 
goto exit
 
:error
echo Usage: bldvcli prog_name
 
:exit
@echo on


Compile and Link Options for bldvcli

The batch file contains the following compile options:

icc
The IBM VisualAge C++ compiler.

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

-Ti
Generate debugger information.

-W1
Output warning, error, and severe and unrecoverable error messages.

The batch file contains the following link options:

ilink
Use the resource linker to link edit.

/MAP
Generate a map file.

/DEBUG
Include debugging information.

/ST:32000
Specify a stack size of at least 32 000.

/PM:VIO
Enable the program to run in a window or in a full screen.

%1.obj
Include the object file.

samputil.obj
Include the utility object file for error checking.

db2cli.lib
Link with the DB2 CLI library.

Refer to your compiler documentation for additional compiler options.

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

   bldvcli basiccon

The result is an executable file basiccon.exe. You can run the executable file against the SAMPLE database by entering the executable name (without the extension):

   basiccon

DB2 CLI Stored Procedures

The batch file bldvclis.bat, in %DB2PATH%\samples\cli, contains the commands to build a CLI stored procedure. The batch file builds the stored procedure into a DLL on the server.

The parameter, %1, specifies the name of your source file. The batch file uses the source file name, %1, for the DLL name.

@echo off
rem bldvclis.bat file - Windows 32-bit Operating Systems
rem Builds a CLI stored procedure using the IBM VisualAge C++ compiler.
rem Usage: bldvclis prog_name
 
if "%1" == "" goto error
 
rem Compile the program.
icc -c+ -Ti -Ge- -Gm+ -W1 %1.c  samputil.c
 
rem Import the library and create an export file.
rem The function name in the .def file must be decorated to be consistent 
rem with the function name in the .map file.  Typically, this is done by
rem prepending "_" and appending "@" and the number of bytes of arguments, 
rem as in: "@16". In outsrv2va.def, for example, the IBM VisualAge C++
rem compiler requires "EXPORTS _outsrv2@16" and not "EXPORTS outsrv2".
ilib /GI %1va.def
 
rem Link the program and produce a DLL.
ilink /ST:64000 /PM:VIO /MAP /DLL %1.obj samputil.obj %1.exp db2cli.lib
 
rem Copy the stored procedure DLL to the 'function' directory
copy %1.dll %DB2PATH%\function
 
goto exit
 
:error
echo Usage: bldvclis prog_name 
 
:exit
@echo on


Compile and Link Options for bldvclis

The batch file contains the following compile options:

icc
The IBM VisualAge C++ compiler.

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

-Ti
Generate debugger information.

-Ge-
Build a .DLL file. Use the version of the run-time library that is statically linked.

-Gm+
Link with multi-tasking libraries.

-W1
Output warning, error, and severe and unrecoverable error messages.

The batch file contains the following link options:

ilink
Use the resource linker to link edit.

/ST:64000
Specify a stack size of at least 64 000.

/PM:VIO
Enable the program to run in a window or in a full screen.

/MAP
Generate a map file.

/DLL
Build a .DLL file.

%1.obj
Include the object file.

%1.exp
Include the VisualAge export file.

db2cli.lib
Link with the DB2 CLI library.

Refer to your compiler documentation for additional compiler options.

To build the outsrv2 stored procedure from the source file outsrv2.c , enter:

   bldvclis outsrv2

The batch file uses the module definition file, outsrv2va.def, contained in the same directory as the CLI sample programs, to build the stored procedure. The batch file copies the stored procedure DLL, outsrv2.dll, to the server in the path %DB2PATH%\function. For DB2DARI parameter style stored procedures where the invoked procedure matches the stored procedure DLL name, this location indicates that the stored procedure is fenced. If you want this type of stored procedure to be unfenced, you must move it to the %DB2PATH%\function\unfenced directory. For all other types of DB2 stored procedures, you indicate whether it is fenced or not fenced with the CREATE FUNCTION statement in the calling program. For a full discussion on creating and using the different types of DB2 stored procedures, please see the "Stored Procedures" chapter in the Application Development Guide.
Note:An unfenced stored procedure runs in the same address space as the database manager and results in increased performance when compared to a fenced stored procedure, which runs in an address space isolated from the database manager. With unfenced stored procedures there is a danger that user code could accidentally or maliciously damage the database control structures. Therefore, you should only run unfenced stored procedures when you need to maximize the performance benefits. Ensure these programs are thoroughly tested before running them as unfenced. Refer to the Application Development Guide for more information.

Once you build the stored procedure, outsrv2, you can build the client application that calls the stored procedure. You can build outcli2 using the batch file bldvcli. See "DB2 CLI Applications" for details.

To run the stored procedure, enter:

   outcli2 remote_database userid password

where

remote_database
is the name of the database to which you want to connect, such as SAMPLE.

userid
is a valid user ID.

password
is a valid password.

The client application passes a variable to the server program, outsrv2, which gives it a value and then returns the variable to the client application.

Embedded SQL Applications

The batch file bldvaemb.bat, in %DB2PATH%\samples\c, and in %DB2PATH%\samples\cpp, contains the commands to build an embedded SQL program.

Ensure the LIB environment variable points to %DB2PATH%\lib like this:

   set LIB=%DB2PATH%\lib;%LIB%

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.

@echo off
rem bldvaemb.bat file
rem Builds a sample C or C++ program containing embedded SQL
rem using the IBM VisualAge C++ compiler.
rem USAGE: bldvaemb <prog_name> [ <db_name> [ < userid> <password> ]]
 
rem Connect to a database.
if "%1" == "" goto error
if "%2" == "" goto case1
if "%3" == "" goto case2
if "%4" == "" goto error
goto case3
:case1
   db2 connect to sample
   goto continue
:case2
   db2 connect to %2
   goto continue
:case3
   db2 connect to %2 user %3 using %4
   goto continue
:continue
 
rem Precompile the program.
db2 prep %1.sqc bindfile
rem For C++, comment out the above line and uncomment the following:
rem db2 prep %1.sqx bindfile
 
rem Bind the program to the database.
db2 bind %1.bnd
 
rem Disconnect from the database.
db2 connect reset
 
rem  Compile the program.
icc -c -Ti -W1 %1.c util.c
rem For C++, comment out the above line and uncomment the following:
rem icc -c -Ti -W1 %1.cxx util.cxx
 
rem  Link the program.
ilink /MAP /DEBUG /ST:32000 /PM:VIO %1.obj util.obj db2api.lib
 
goto exit
 
:error
echo Usage: bldvaemb <prog_name> [ <db_name> [ < userid> <password> ]]
 
:exit
@echo on

Compile and Link Options for bldvaemb

The batch file contains the following compile options:

icc
The IBM VisualAge C++ compiler.

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

-Ti
Generate debugger information.

-W1
Output warning, error, and severe and unrecoverable error messages.

The batch file contains the following link options:

ilink
Use the resource linker to link edit.

/MAP
Generate a map file.

/DEBUG
Include debugging information.

/ST:32000
Specify a stack size of at least 32 000.

/PM:VIO
Enable the program to run in a window or in a full screen.

%1.obj
Include the object file.

util.obj
Include the error-checking utility object file.

db2api.lib
Link with the DB2 library.

Refer to your compiler documentation for additional compiler options.

To build the sample program updat, from the C source file updat.sqc , or the C++ source file updat.sqx , enter:

   bldvaemb updat

The result is an executable file, updat.exe. You can run the executable file against the SAMPLE database by entering the executable name (without the extension):

   updat

Embedded SQL Stored Procedures

The batch file bldvastp.bat, in %DB2PATH%\samples\c, and in %DB2PATH%\samples\cpp, contains the commands to build an embedded SQL stored procedure. The batch file compiles the stored procedure into a DLL, and stores it on the server.

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.

The batch file uses the source file name, %1, for the DLL name.

@echo off
rem bldvastp.bat file
rem Builds a sample C or C++ stored procedure using the IBM VisualAge C++ compiler.
rem Usage: bldvastp <prog_name> [ <db_name> [ < userid> <password> ]]
 
rem Connect to a database.
if "%1" == "" goto error
if "%2" == "" goto case1
if "%3" == "" goto case2
if "%4" == "" goto error
goto case3
:case1
   db2 connect to sample
   goto continue
:case2
   db2 connect to %2
   goto continue
:case3
   db2 connect to %2 user %3 using %4
   goto continue
:continue
 
rem Precompile the program.
db2 prep %1.sqc bindfile
rem For C++, comment out the above line and uncomment the following:
rem db2 prep %1.sqx bindfile
 
rem Bind the program to the database.
db2 bind %1.bnd
 
rem Disconnect from the database.
db2 connect reset
 
rem Compile the program. 
icc -c+ -Ti -Ge- -Gm+ -W1 %1.c
rem For C++, comment out the above line and uncomment the following:
rem icc -c+ -Ti -Ge- -Gm+ -W1 %1.cxx
 
rem Import the library and create a definition file.
rem The function name in the .def file must be decorated to be consistent
rem with the function name in the .map file. Typically, this is done by
rem prepending "_" and appending "@" and the number of bytes of arguments,
rem for example, "@16". In outsrvva.def, the IBM VisualAge C++ compiler requires
rem "EXPORTS _outsrv@16" and not "EXPORTS outsrv".
ilib /GI %1va.def
 
rem Link the program and produce a DLL.
ilink /ST:64000 /PM:VIO /MAP /DLL %1.obj %1va.exp db2api.lib
 
rem Copy the Stored Procedure DLL to the 'function' directory.
copy %1.dll %DB2PATH%\function
 
goto exit
 
:error
echo Usage: bldvastp <prog_name> [ <db_name> [ < userid> <password> ]]
 
:exit
@echo on


Compile and Link Options for bldvastp

The batch file contains the following compile options:

icc
The IBM VisualAge C++ compiler.

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

-Ti
Generate debugger information.

-Ge-
Build a .DLL file. Use the version of the run-time library that is statically linked.

-Gm+
Link with multi-tasking libraries.

-W1
Output warning, error, and severe and unrecoverable error messages.

The batch file contains the following link options:

ilink
Use the resource linker to link edit.

/ST:64000
Specify a stack size of least of 64 000.

/PM:VIO
Enable the program to run in a window or full screen.

/MAP
Generate a MAP file.

/DLL
Build a .DLL file.

%1.obj
Include the object file.

%1va.exp
VisualAge export file.

db2api.lib
Link with the DB2 library.

Refer to your compiler documentation for additional compiler options.

To build the outsrv stored procedure from the C source file, outsrv.sqc , or the C++ source file, outsrv.sqx , enter:

   bldvastp outsrv

The batch file uses the module definition file outsrvva.def, contained in the same directory as the sample programs, to build the stored procedure. The batch file copies the stored procedure DLL, outsrv.dll, to the server in the path %DB2PATH%\function. For DB2DARI parameter style stored procedures where the invoked procedure matches the stored procedure DLL name, this location indicates that the stored procedure is fenced. If you want this type of stored procedure to be unfenced, you must move it to the %DB2PATH%\function\unfenced directory. For all other types of DB2 stored procedures, you indicate whether it is fenced or not fenced with the CREATE FUNCTION statement in the calling program. For a full discussion on creating and using the different types of DB2 stored procedures, please see the "Stored Procedures" chapter in the Application Development Guide.
Note:An unfenced stored procedure runs in the same address space as the database manager and results in increased performance when compared to a fenced stored procedure, which runs in an address space isolated from the database manager. With unfenced stored procedures there is a danger that user code could accidentally or maliciously damage the database control structures. Therefore, you should only run unfenced stored procedures when you need to maximize the performance benefits. Ensure these programs are thoroughly tested before running them as unfenced. Refer to the Application Development Guide for more information.

Once you build the stored procedure outsrv, you can build the client application that calls the stored procedure. You can build outcli using the bldvaemb file. Refer to "Embedded SQL Applications" for details.

To run the stored procedure, enter:

   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.

User-Defined Functions (UDFs)

The batch file bldvaudf, in %DB2PATH%\samples\c, and in %DB2PATH%\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 to precompile and bind the program.

The parameter, %1, specifies the name of your source file. The batch file uses the source file name, %1, for the DLL name.

@echo off
rem bldvaudf.bat file
rem Builds a C or C++ user-defined function (UDF)
rem using the IBM VisualAge C++ compiler.
rem Usage: bldvaudf <udf_program_name>
 
rem Compile the program. 
icc -Ti -c+ -Ge- -Gm+ -W1 %1.c
rem For C++, comment out the above line and uncomment the following:
rem icc -Ti -c+ -Ge- -Gm+ -W1 %1.cxx
 
rem Generate an import library and export file using a definition file.
rem Function(s) in the .def file are prepended with an underscore, and
rem appended with the @ sign and number of bytes of arguments (in decimal).
rem Parameters of less than four bytes are rounded up to four bytes.
rem Structure size is rounded up to a multiple of four bytes.
rem For example, function fred prototyped as: "int fred(int, int, short);"
rem would appear as:  "_fred@12" in the .def file.
rem These decorated function names can also be found in %1.map
rem after running the following ilink command without %1va.exp.
ilib /gI %1va.def
 
rem Create a dynamic link library
ilink /ST:64000 /PM:VIO /MAP /DLL %1.obj %1va.exp db2api.lib db2apie.lib
 
rem Copy the UDF DLL to the 'function' directory.
copy %1.dll %DB2PATH%\function
@echo on


Compile and Link Options for bldvaudf

The batch file bldvaudf contains the following compile options:

icc
The IBM VisualAge C++ compiler.

-Ti
Generate debugger information.

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

-Ge-
Build a .DLL file. Use the version of the run-time library that is statically linked.

-Gm+
Link with multi-tasking libraries.

-W1
Output warning, error, and severe and unrecoverable error messages.

The batch file contains the following link options:

ilink
Use the resource linker to link edit.

/ST:64000
Specify a stack size of at least 64000.

/PM:VIO
Enable the program to run in a window or a full screen.

/MAP
Generate a MAP file.

/DLL
Build a .DLL file.

%1.obj
Include the object file.

%1va.exp
Include the VisualAge export file.

db2api.lib
Link with the DB2 library.

db2apie.lib
Link with the DB2 API Engine library.

Refer to your compiler documentation for additional compiler options.

To build the user-defined function udf, from the source file, udf.c in %DB2PATH%\samples\c, enter:

   bldvaudf udf

The batch file uses the module definition file, udf.def, contained in the same directory as the sample programs, to build the user-defined function. The batch file copies the user-defined function DLL, udf.dll, to the server in the path %DB2PATH%\function.

Once you build udf, you can build the client application, calludf, that calls the UDF. DB2 CLI as well as embedded SQL C and C++ versions of this program are provided.

You can build the DB2 CLI calludf program from the calludf.c source file in %DB2PATH%\samples\cli using the batch file bldvcli. Refer to "DB2 CLI Applications" for details.

You can build the C embedded SQL calludf program from the calludf.sqc source file in %DB2PATH%\samples\c using the batch file bldvaemb. Refer to "Embedded SQL Applications" for details.

You can build the C++ embedded SQL calludf program from the calludf.sqx source file in %DB2PATH%\samples\cpp using the batch file bldvaemb. Refer to "Embedded SQL Applications" for details.

To run the UDF, enter:

   calludf

The 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. Connect to the database, then enter:

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

You do not have to type the SQL statement in uppercase.


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

[ DB2 List of Books | Search the DB2 Books ]