Call Level Interface Guide and Reference
An application that uses an embedded SQL interface requires a precompiler
to convert the SQL statements into code, which is then compiled, bound to the
database, and executed. In contrast, a DB2 CLI application does not
have to be precompiled or bound, but instead uses a standard set of functions
to execute SQL statements and related services at run time.
This difference is important because, traditionally, precompilers have been
specific to each database product, which effectively ties your applications to
that product. DB2 CLI enables you to write portable applications that
are independent of any particular database product. This independence means DB2 CLI applications do not have to be
recompiled or rebound to access different DB2 databases, including DRDA
databases. They just connect to the appropriate database at run
time.
DB2 CLI and embedded SQL also differ in the following ways:
- DB2 CLI does not require the explicit declaration of cursors. DB2 CLI has a supply of cursors that get used as needed. The application can then use the generated cursor in the normal
cursor fetch model for multiple row SELECT statements and
positioned UPDATE and DELETE statements.
- The OPEN statement is not used in DB2 CLI. Instead, the
execution of a SELECT automatically causes a cursor to be
opened.
- Unlike embedded SQL, DB2 CLI allows the use of parameter markers on the
equivalent of the EXECUTE IMMEDIATE statement (the
SQLExecDirect() function).
- A COMMIT or ROLLBACK in DB2 CLI is issued via the
SQLEndTran() function call rather than by passing it as an SQL
statement.
- DB2 CLI manages statement related information on behalf of the
application, and provides a statement handle to refer to it as an
abstract object. This handle eliminates the need for the application to
use product specific data structures.
- Similar to the statement handle, the environment handle and
connection handle provide a means to refer to all global variables
and connection specific information. The descriptor handle
describes either the parameters of an SQL statement or the columns of a result
set.
- DB2 CLI uses the SQLSTATE values defined by the X/Open SQL CAE
specification. Although the format and most of the values are
consistent with values used by the IBM relational database products, there are
differences. (There are also differences between ODBC SQLSTATES and the
X/Open defined SQLSTATES). Refer to SQLState Cross Reference table for a cross reference of all DB2 CLI SQLSTATEs.
- DB2 CLI supports scrollable cursors. With scrollable cursors, you
can scroll through a static cursor as follows:
- Forward by one or more rows
- Backward by one or more rows
- From the first row by one or more rows
- From the last row by one or more rows.
Despite these differences, there is an important common concept between
embedded SQL and DB2 CLI:
DB2 CLI can execute any SQL statement that can be prepared dynamically
in embedded SQL.
Note: | DB2 CLI can also accept some SQL statements that cannot be prepared
dynamically, such as compound SQL statements.
|
Table 215 lists each SQL statement, and indicates whether or not it
can be executed using DB2 CLI. The table also indicates if the command
line processor can be used to execute the statement interactively, (useful for
prototyping SQL statements).
Each DBMS may have additional statements that you can dynamically
prepare. In this case, DB2 CLI passes the statements to the
DBMS. There is one exception: the COMMIT and ROLLBACK statement
can be dynamically prepared by some DBMSs but are not passed. In this
case, use the SQLEndTran() function to specify either the COMMIT or
ROLLBACK statement.
The DB2 CLI interface has several key advantages over embedded SQL.
- It is ideally suited for a client-server environment, in which the target
database is not known when the application is built. It provides a
consistent interface for executing SQL statements, regardless of which
database server the application is connected to.
- It increases the portability of applications by removing the dependence on
precompilers. Applications are distributed not as embedded SQL source
code which must be preprocessed for each database product, but as compiled
applications or run time libraries.
- Individual DB2 CLI applications do not need to be bound to each database,
only bind files shipped with DB2 CLI need to be bound once for all DB2 CLI
applications. This can significantly reduce the amount of management
required for the application once it is in general use.
- DB2 CLI applications can connect to multiple databases, including multiple
connections to the same database, all from the same application. Each
connection has its own commit scope. This is much simpler using CLI
than using embedded SQL where the application must make use of multi-threading
to achieve the same result.
- DB2 CLI eliminates the need for application controlled, often complex data
areas, such as the SQLDA and SQLCA, typically associated with embedded SQL
applications. Instead, DB2 CLI allocates and controls the necessary
data structures, and provides a handle for the application to
reference them.
- DB2 CLI enables the development of multi-threaded thread-safe applications
where each thread can have its own connection and a separate commit scope from
the rest. DB2 CLI achieves this by eliminating the data areas described
above, and associating all such data structures that are accessible to the
application with a specific handle. Unlike embedded SQL, a
multi-threaded CLI application does not need to call any of the context
management DB2 APIs; this is handled by the DB2 CLI driver
automatically.
- DB2 CLI provides enhanced parameter input and fetching capability,
allowing arrays of data to be specified on input, retrieving multiple rows of
a result set directly into an array, and executing statements that generate
multiple result sets.
- DB2 CLI provides a consistent interface to query catalog (Tables, Columns,
Foreign Keys, Primary Keys, etc.) information contained in the various
DBMS catalog tables. The result sets returned are consistent across
DBMSs. This shields the application from catalog changes across
releases of database servers, as well as catalog differences amongst different
database servers; thereby saving applications from writing version
specific and server specific catalog queries.
- Extended data conversion is also provided by DB2 CLI, requiring less
application code when converting information between various SQL and C data
types.
- DB2 CLI incorporates both the ODBC and X/Open CLI functions, both of which
are accepted industry specifications. DB2 CLI is also aligned with the
emerging ISO CLI standard. Knowledge that application developers invest
in these specifications can be applied directly to DB2 CLI development, and
vice versa. This interface is intuitive to grasp for those programmers
who are familiar with function libraries but know little about product
specific methods of embedding SQL statements into a host language.
- DB2 CLI provides the ability to retrieve multiple rows and result sets
generated from a stored procedure residing on a DB2 Universal Database (or DB2
for MVS/ESA version 5 or later) server. However, note that this capability exists for Version 5 DB2
Universal Database clients using embedded SQL if the stored procedure resides
on a server accessible from a DataJoiner Version 2 server.
- DB2 CLI supports server-side scrollable cursors that can be used in
conjunction with array output. This is useful in GUI applications that
display database information in scroll boxes that make use of the Page Up,
Page Down, Home and End keys. You can declare a read-only cursor as
scrollable then move forward or backward through the result set by one or more
rows. You can also fetch rows by specifying an offset from:
- The current row
- The beginning or end of the result set
- A specific row you have previously set with a bookmark.
- DB2 CLI applications can dynamically describe parameters in an SQL
statement the same way that CLI and Embedded SQL applications describe result
sets. This enables CLI applications to dynamically process SQL
statements that contain parameter markers without knowing the data type of
those parameter markers in advance. When the SQL statement is prepared,
describe information is returned detailing the data types of the
parameters.
Which interface you choose depends on your application.
DB2 CLI is ideally suited for query-based graphical user interface (GUI)
applications that require portability. The advantages listed above, may
make using DB2 CLI seem like the obvious choice for any application.
There is however, one factor that must be considered, the comparison between
static and dynamic SQL. It is much easier to use static SQL in embedded
applications.
For more information on using static SQL in CLI applications, refer to the
Web page at
http://www.ibm.com/software/data/db2/udb/staticcli
Static SQL has several advantages:
- Performance
Dynamic SQL is prepared at run time, static SQL is prepared at precompile
time. As well as requiring more processing, the preparation step may
incur additional network-traffic at run time. This additional step (and
network-traffic), however, will not be required if the DB2 CLI application
makes use of deferred prepare.
It is important to note that static SQL will not always have better
performance than dynamic SQL. Dynamic SQL can make use of changes to
the database, such as new indexes, and can use current database statistics to
choose the optimal access plan. In addition, precompilation of
statements can be avoided if they are cached.
- Encapsulation and Security
In static SQL, the authorizations to objects (such as a table, view) are
associated with a package and are validated at package binding time.
This means that database administrators need only to grant execute on a
particular package to a set of users (thus encapsulating their privileges in
the package) without having to grant them explicit access to each database
object. In dynamic SQL, the authorizations are validated at run time on
a per statement basis; therefore, users must be granted explicit access
to each database object. This permits these users access to parts of
the object that they do not have a need to access.
- Embedded SQL is supported in languages other than C or
C++.
- For fixed query selects, embedded SQL is simpler.
If an application requires the advantages of both interfaces, it is
possible to make use of static SQL within a DB2 CLI application by creating a
stored procedure that contains the static SQL. The stored procedure is
called from within a DB2 CLI application and is executed on the server.
Once the stored procedure is created, any DB2 CLI or ODBC application can call
it. For more information, refer to Using Stored Procedures.
For more information on using static SQL in CLI applications, refer to the
Web page at
http://www.ibm.com/software/data/db2/udb/staticcli
It is also possible to write a mixed application that uses both DB2 CLI and
embedded SQL, taking advantage of their respective benefits. In this
case, DB2 CLI is used to provide the base application, with key modules
written using static SQL for performance or security reasons. This
complicates the application design, and should only be used if stored
procedures do not meet the applications requirements. Refer to Mixing Embedded SQL and DB2 CLI.
Ultimately, the decision on when to use each interface, will be based on
individual preferences and previous experience rather than on any one
factor.
[ Top of Page | Previous Page | Next Page ]