ODBC 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 ODBC server using SQLAPI++ library in the following areas:

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
SQLAPI++ library requires ODBC version 3.x or higher.

After the connection is created you need to call SAConnection::Connect method to establish connection with ODBC server:

void Connect(
    const SAString &sDBString,
    const SAString &sUserID, 
    const SAString &sPassword, 
    SAClient_t eSAClient = SA_Client_NotSpecified);
Parameters

sDBString

Any valid ODBC connection string

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

Optional. One of the following values from SAClient_t enum:
  • SA_ODBC_Client ODBC client
  • SA_Client_NotSpecified – used by default if eSAClient parameter is omitted. You can use this default value only if you have SAConnection::setAPI method with SAPI object initialized with SA_ODBC_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 ODBC in the following way:

SA_ReadUncommittedSQL_TXN_READ_UNCOMMITTED
SA_ReadCommittedSQL_TXN_READ_COMMITTED
SA_RepeatableReadSQL_TXN_REPEATABLE_READ
SA_SerializableSQL_TXN_SERIALIZABLE

In addition to the SQL-92 levels, if you specify 'snapshot' isolation level, it will be mapped as: SA_Snapshot SQL_TXN_SERIALIZABLE.

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 ODBC server data types to Long/Lob library types:

SQL_BINARYSA_dtLongBinary
SQL_VARBINARY with ColumnSize = 0SA_dtLongBinary
SQL_CHARSA_dtLongChar
SQL_VARCHAR with ColumnSize = 0SA_dtLongChar
SQL_LONGVARBINARYSA_dtLongBinary
SQL_LONGVARCHARSA_dtLongChar
SQL_WLONGVARCHARSA_dtLongChar

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.

The table below shows how SQLAPI++ maps its internal library types to ODBC API data types:
SA_dtLongBinarySQL_LONGVARBINARY
SA_dtLongCharSQL_LONGVARCHAR, SQL_UNICODE_LONGVARCHAR
SA_dtBLobSQL_LONGVARBINARY
SA_dtCLobSQL_LONGVARCHAR, SQL_UNICODE_LONGVARCHAR

For additional information see Getting Started - Handle Long/CLob/BLob.

Returning output parameters

In ODBC processing output parameters depends on a driver.

If DBMS supports returning output parameters then SQLAPI++ stores the returned values of output parameters in the SAParam objects bound to those parameters. On some servers these returned values are not guaranteed to be available until all results returned by the procedure have been fetched (using SACommand::FetchNext method).

See ODBC driver documentation on output parameters availability on different drivers/servers.

SQLAPI++ Library automatically creates SAParam object to represent function return value. You can refer to this SAParam object using SQLAPI++ predefined name "RETURN_VALUE".

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

SQLAPI++ calls SQLCancel function to cancel a query. To get more details see SQLCancel function description in native ODBC documentation.

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 ODBC:

ODBC.LIBS
Api Scope
Configures SQLAPI++ Library to use specified ODBC manager library.
Valid values: Any valid ODBC manager library name list. Names separated by ';' on Windows or ':' on other operating systems.
Default value:
  • Windows - "odbc32.dll"
  • Linux - "libiodbc.so.:libiodbc.so.3:libiodbc.so.2:libodbc.so:libodbc.so.1"
SQL_ATTR_CONNECTION_POOLING
Api Scope
Configures SQLAPI++ to set ODBC's SQL_ATTR_CONNECTION_POOLING attribute just after the ODBC API is initialized. See ODBC documentation for more information on this attribute.
Valid values:
  • "SQL_CP_OFF"
  • "SQL_CP_ONE_PER_DRIVER"
  • "SQL_CP_ONE_PER_HENV"
Default value: none
SQL_ATTR_CP_MATCH
Api Scope
Configures SQLAPI++ to set ODBC's SQL_ATTR_CP_MATCH attribute just after the ODBC API is initialized. See ODBC documentation for more information on this attribute.
Valid values:
  • "SQL_CP_STRICT_MATCH"
  • "SQL_CP_RELAXED_MATCH"
Default value: none
ODBCUseNumeric
Connection Scope
Configures SQLAPI++ library to use SQL_NUMERIC_STRUCT program type for SQL_NUMERIC, SQL_DECIMAL and SQL_BIGINT database types. By default SQLAPI++ uses character buffer to prevent any problems with ODBC drivers those don't support SQL_NUMERIC_STRUCT.
Valid values: "True", "1"
Default value: "False"
ODBCUseBigint
Connection Scope
Configures SQLAPI++ library to use 64-bit integer program type for SQL_C_SBIGINT and SQL_C_UBIGINT database types. By default SQLAPI++ uses character buffer to prevent any problems with ODBC drivers those don't support 64-bit integer program types.
Valid values: "True", "1"
Default value: "False"
SQL_DRIVER_PROMPT
SQL_DRIVER_COMPLETE
SQL_DRIVER_COMPLETE_REQUIRED
Connection Scope
SQLAPI++ uses SQLConnect (if connection string doesn't include '=' symbol) or SQLDriverConnect ODBC's function to initiate database connection. When one of these parameters is specified SQLDriverConnect is used with appropriate driver completion flag (see ODBC documentation for more information).
Valid values: String that represent a parent window handle in a hexadecimal form
Default value: none. By default SQLAPI++ uses SQL_DRIVER_NOPROMPT driver completion flag.
DSN
Connection Scope
Readonly. This option value is set to the full connection string by ODBC Driver Manager. Requires SQL_DRIVER_PROMPT, SQL_DRIVER_COMPLETE or SQL_DRIVER_COMPLETE_REQUIRED option to be set.
SQL_ATTR_CONNECTION_TIMEOUT
Connection Scope
Configures SQLAPI++ to set ODBC's SQL_ATTR_CONNECTION_TIMEOUT attribute just before the first connection is made. See ODBC documentation for more information on this attribute.
Valid values: String that represents SQLUINTEGER value corresponding to the number of seconds to wait for any request on the connection to complete before returning to the application
Default value: none
ODBCUseSQLGetData
Command Scope
Configures SQLAPI++ library to use SQLGetData call to get the value of any result set field.
Valid values: "True", "1"
Default value: "False"
ODBCAddLongTextBufferSpace
Command Scope

As described in ODBC documentation, for text data, an extra space for '\0' should be allocated in the destination buffer for SQLGetData call. SQLAPI++ does that by default.

But some drivers ignore the fact they have to put '\0' into the buffer. As the result the first symbol of the field can be lost. For such drivers this option should be set to "False".

Valid values: "False", "0" - to not allocate extra space in the buffer
Default value: "True"
ODBCDisableLongLengthQuery
Command Scope
Configures SQLAPI++ library to not query the LONG field length. Some buggy ODBC drivers can truncate the field content with default processing.
Valid values: "True", "1"
Default value: "False"
PreFetchRows
Command Scope
Forces SQLAPI++ library to fetch rows in bulk, rather than retrieving records one by one.
Valid values: String containing number of rows in the fetch buffer
Default value: "1"
UseDynamicCursor
Scrollable
Command Scope
Forces SQLAPI++ to use scrollable dynamic command handle. Sets the command handle attributes SQL_ATTR_CURSOR_TYPE = SQL_CURSOR_DYNAMIC and SQL_ATTR_CONCURRENCY = SQL_CONCUR_LOCK.
Valid values: "True", "1"
Default value: "False"
SetCursorName
Command Scope
Allows to define the cursor name SQLAPI++ uses for the server side cursor.
Valid values: See SQLSetCursorName documentation.
Default value: none
ExecDirect
Command Scope
Forces SQLAPI++ to use SQLExecDirect instead of SQLExecute API function.
Valid values: "True", "1"
Default value: "False"
SQL_ATTR_CONCURRENCY
Command Scope
Sets the statement concurrency attribute. See ODBC documentation for additional information.
Valid values: "SQL_CONCUR_READONLY", "SQL_CONCUR_VALUES", "SQL_CONCUR_ROWVER", "SQL_CONCUR_LOCK"
SQL_ATTR_CURSOR_TYPE
Command Scope
Sets the statement cursor type. See ODBC documentation for additional information.
Valid values: "SQL_CURSOR_FORWARD_ONLY", "SQL_CURSOR_KEYSET_DRIVEN", "SQL_CURSOR_DYNAMIC", "SQL_CURSOR_STATIC"
SQL_ATTR_CURSOR_SCROLLABLE
Command Scope
Configures the statement cursor to be scrollable. See ODBC documentation for additional information.
Valid values: "SQL_NONSCROLLABLE", "SQL_SCROLLABLE"
SQL_ATTR_CURSOR_SENSITIVITY
Command Scope
Sets the statement cursor sensitivity. See ODBC documentation for additional information.
Valid values: "SQL_UNSPECIFIED", "SQL_INSENSITIVE", "SQL_SENSITIVE"
SQL_ATTR_QUERY_TIMEOUT
Command Scope
Sets the integer value corresponding to the number of seconds to wait for an SQL statement to execute before returning to the application.
Valid values: String that represents an integer value in seconds
Default value: none

For additional information see SAOptions::setOption.

Using native ODBC 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 ODBC. 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 ODBC 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 ODBC specific #include and cast the result of SAConnection::NativeAPI to class odbcAPI:

#include "odbcAPI.h"

IsaAPI *pApi = con.NativeAPI();
odbcAPI *pNativeAPI = (odbcAPI *)pApi;

To get more information about ODBC API functions see ODBC documentation.

For additional information see SAConnection::NativeAPI.

Getting native ODBC connection related handles

You have to use native API handles when you want to call specific ODBC 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 ODBC specific #include and cast the result to class odbcConnectionHandles:

#include "odbcAPI.h"

saConnectionHandles *pHandles = con.NativeHandles();
odbcConnectionHandles *pNativeHandles = (odbcConnectionHandles*)pHandles;

To get more information about ODBC API functions and handles see ODBC specific documentation.

For additional information see SAConnection::NativeHandles.

Getting native ODBC command related handles

You have to use native API handles when you want to call specific ODBC 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 ODBC specific #include and cast the result to class odbcCommandHandles:

#include "odbcAPI.h"

saCommandHandles *pHandles = cmd.NativeHandles();
odbcCommandHandles *pNativeHandles = (odbcCommandHandles*)pHandles;

To get more information about ODBC API functions and handles see ODBC 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 ODBC server SAException::ErrPos method returns -1 because ODBC does not support this function.

For additional information see Getting Started - Error Handling, SAException object.