InterBase / Firebird Guide
SQLAPI++ allows to seamlessly work with a variety of SQL database servers. It provides unified API to access any database, keeping your code portable. But each server has some specific features which a developer has to know in order to leverage server's unique features and avoid potential errors.
For complete information on using SQLAPI++ check out Getting Started and Documentation. This guide covers specific information related to working with InterBase / Firebird server using SQLAPI++ library in the following areas:
- Connecting to a database
- Transaction isolation levels
- Working with Long or Lob (CLob, BLob) data
- Returning output parameters
- Cancelling queries
- Connection, command, parameter and field options
- Using native API
- Getting native connection related handles
- Getting native command related handles
- Error handling
Connecting to a database
To connect to a database you need to initialize a connection object. A connection object is represented by SAConnection class.
Minimum Version
After the connection is created you need to call SAConnection::Connect method to establish connection with InterBase server:
void Connect(
const SAString &sDBString,
const SAString &sUserID,
const SAString &sPassword,
SAClient_t eSAClient = SA_Client_NotSpecified);
Parameters
sDBString
- For local InterBase databases, this can be a file name
- To connect to an InterBase database on a remote server using TCP/IP the syntax is
<server_name>[/<port_number>]:<filename>
- To connect to an InterBase database on a remote server using NetBEUI, the syntax is
\\<server_name>\<filename>
- To connect to an InterBase database on a remote server using SPX, the syntax is
<server_name>@<filename>
sUserID
A string containing a user name to use when establishing the connection.
sPassword
A string containing a password to use when establishing the connection.
eSAClient
SA_InterBase_Client
– InterBase clientSA_Client_NotSpecified
– used by default ifeSAClient
parameter is omitted. You can use this default value only if you have SAConnection::setAPI method with SAPI object initialized withSA_InterBase_Client
constant before
For more details see Getting Started - Connect to Database, SAConnection object, SAConnection::Connect.
Transaction isolation levels
SQL-92 defines four isolation levels, all of which are supported by SQLAPI++:
- Read uncommitted (the lowest level where transactions are isolated just enough to ensure that physically corrupt data is not read)
- Read committed
- Repeatable read
- Serializable (the highest level, where transactions are completely isolated from one another)
SQLAPI++ maps different isolation levels on InterBase / Firebird in the following way:
SA_ReadUncommitted | → | isc_tpb_read_committed + isc_tpb_rec_version |
SA_ReadCommitted | → | isc_tpb_read_committed + isc_tpb_no_rec_version |
SA_RepeatableRead | → | isc_tpb_consistency |
SA_Serializable | → | isc_tpb_concurrency |
In addition to the SQL-92 levels, if you specify 'snapshot' isolation level, it will be mapped as: SA_Snapshot
→ isc_tpb_concurrency
.
For more details see SAConnection::setIsolationLevel.
Working with Long or Lob (CLob, BLob) data
When fetching data SQLAPI++ detects data types of the columns in the result set and maps those types to internal library types. The mapping determines which native APIs the library will use for fetching LOB data.
The table below shows how SQLAPI++ maps InterBase server data types
to Long/Lob library types
:
ARRAY | → | SA_dtBLob |
BLOB (sub-type 0) | → | SA_dtBLob |
BLOB (sub-type 1) | → | SA_dtCLob |
When binding input data from your program the reverse mapping is taking place. The SQLAPI++ data type you use for input markers determines what native API program types will be used for sending Long/Lob data to the server.
library types
to InterBase / Firebird API data types
:SA_dtLongBinary | → | SQL_BLOB |
SA_dtLongChar | → | SQL_BLOB |
SA_dtBLob | → | SQL_BLOB |
SA_dtCLob | → | SQL_BLOB |
For additional information see Getting Started - Handle Long/CLob/BLob.
Returning output parameters
If InterBase stored procedure returns only one row of output parameters (the most common case), you can get output parameters' values immediately after calling SACommand::Execute using appropriate SAParam objects.
InterBase stored procedures can also return several rows of output parameters. In this case you should perform a select from a procedure to return them as result set.
For additional information see SACommand::Execute, SAParam object, Getting Started - Get Output Parameters.
Cancelling queries
Using SACommand::Cancel method you can cancel the following types of processing on a statement:
- function running asynchronously on the statement
- function running on the statement on another thread
InterBase does not support cancelling queries. So if you call SACommand::Cancel it will have no effect.
For additional information see SACommand::Cancel.
Connection, command, parameter and field options
Server specific options can be applied at the API, connection, command, parameter or field levels.
We recommend you specify each option at the appropriate level, although it is possible to specify them at the parent object level as well. In that case the option affects all the child objects.
API level options must be specified in SAPI object. If an internal SAPI object is used for the DBMS API initialization (implicit DBMS API initialization, see SAConnection::Connect method) the related DBMS specific options are taken from the initial connection object.
Connection level options may be specified in either SAPI object or SAConnection object. If specified in SAPI object an option affects all connections on that API.
Command level options may be specified in SAPI object, SAConnection object or SACommand object. If specified in a parent object an option affects all commands on that SAPI or SAConnection object.
Parameter level options may be specified in SAPI object, SAConnection object, SACommand object or SAParam object. If specified in a parent object an option affects all parameters on that SAPI, SAConnection or SACommand object.
Field related options may be specified in SAPI object, SAConnection object, SACommand object or SAField object. If specified in a parent object an option affects all fields on that SAPI , SAConnection or SACommand object.
Specific options applicable to InterBase / Firebird:
IBASE.LIBS
- Windows -
"gds32.dll;fbclient.dll"
- Linux -
"libgds.so:libfbclient.so"
isc_dpb_lc_ctype
isc_dpb_sql_role_name
isc_dpb_num_buffers
"75"
, see InterBase documentationCommitRetaining
Determines whether SQLAPI++ calls isc_commit_transaction()
or isc_commit_retaining()
function to commit transactions.
isc_commit_transaction()
writes transaction changes permanently to database, closes the result set associated with the transaction, and frees system resources assigned to the transaction for other uses. Active result sets are not preserved.
isc_commit_retaining()
writes all pending changes to the database, ends the current transaction without closing its record stream and cursors and without freeing its system resources, then starts a new transaction and assigns the existing record streams and system resources to the new transaction. Active result sets are preserved.
For more information see InterBase documentation for isc_commit_retaining()
and isc_commit_transaction()
functions.
"true"
– to have SQLAPI++ callisc_commit_retaining()
"false"
– to have SQLAPI++ callisc_commit_transaction()
"false"
– SQLAPI++ uses isc_commit_transaction()
function by defaultTPB_LockResolution
"isc_tpb_wait"
– specifies that the transaction should wait until locked resources are released; once the resources are released, the transaction retries its operation"isc_tpb_nowait"
– specifies that the transaction should return a lock conflict error without waiting for locks to be released
TPB_AccessMode
"isc_tpb_write"
– enables a transaction to read data from a table and write data to it"isc_tpb_read"
– restricts table access to readonly
SQLDialect
"3"
For additional information see SAOptions::setOption.
Using native InterBase / Firebird API
You can call client specific API functions which are not directly supported by SQLAPI++ library. SAConnection::NativeAPI method returns a pointer to the set of native API functions available for InterBase. To use the database API directly you have to downcast this IsaAPI pointer to the appropriate type and use its implementation-specific members. The following example shows what type cast you have to make and what additional header file you have to include to work with InterBase / Firebird API. Note that using appropriate type casting depends on an API (that generally mean that you have to explicitly check client version before casting, see SAConnection::ClientVersion method).
To use native API you need to add InterBase / Firebird specific #include
and cast the result of SAConnection::NativeAPI to class ibAPI
:
#include "ibAPI.h"
IsaAPI *pApi = con.NativeAPI();
ibAPI *pNativeAPI = (ibAPI *)pApi;
To get more information about InterBase API functions see InterBase / Firebird documentation.
For additional information see SAConnection::NativeAPI.
Getting native InterBase / Firebird connection related handles
You have to use native API handles when you want to call specific InterBase / Firebird API functions which are not directly supported by the library. API functions usually need to receive one or more active handles as parameters. SAConnection::NativeHandles method returns a pointer to the set of native API connection related handles. To use API handles directly you have to downcast saConnectionHandles pointer to the appropriate type and use its implementation-specific members.
To access native connection handles you need to add InterBase / Firebird specific #include
and cast the result to class ibConnectionHandles
:
#include "ibAPI.h"
saConnectionHandles *pHandles = con.NativeHandles();
ibConnectionHandles *pNativeHandles = (ibConnectionHandles*)pHandles;
To get more information about InterBase API functions and handles see InterBase / Firebird specific documentation.
For additional information see SAConnection::NativeHandles.
Getting native InterBase / Firebird command related handles
You have to use native API handles when you want to call specific InterBase / Firebird API functions which are not directly supported by the library. API functions usually need to receive one or more active handles as parameters. SACommand::NativeHandles method returns a pointer to the set of native API command related handles. To use API handles directly you have to downcast saCommandHandles pointer to the appropriate type and use its implementation-specific members.
To access native command handles you need to add InterBase / Firebird specific #include
and cast the result to class ibCommandHandles
:
#include "ibAPI.h"
saCommandHandles *pHandles = cmd.NativeHandles();
ibCommandHandles *pNativeHandles = (ibCommandHandles*)pHandles;
To get more information about InterBase API functions and handles see InterBase / Firebird specific documentation.
For additional information see SACommand::NativeHandles.
Error handling
When an error occurs when executing a SQL statement SQLAPI++ library throws an exception of type SAException and SAException::ErrPos method returns error position in the SQL statement.
In InterBase server SAException::ErrPos method returns -1 because InterBase does not support this function.
For additional information see Getting Started - Error Handling, SAException object.