ODBC - Server Specific Guide

SQLAPI++ allows to work with a number of SQL database servers. It provides common mechanisms to access database, and as a general rule they work for any database server. 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.

This page collects all specific information that concerns working with ODBC source using SQLAPI++ Library. Full information about using SQLAPI++ see in How To and Documentation .

Available topics:

Connecting to a database

To connect to a database you should create a connection object and then connect it. A connection object is represented by SAConnection class. After the connection is created you need to connect it to ODBC source using SAConnection::Connect method

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

with the following 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.

The SQLAPI++ Library requires ODBC version 3.x or higher.

For more details see How To - Connecting to databases, 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 source in the following way:

SA_ReadUncommitted = SQL_TXN_READ_UNCOMMITTED
SA_ReadCommitted = SQL_TXN_READ_COMMITTED
SA_RepeatableRead = SQL_TXN_REPEATABLE_READ
SA_Serializable = SQL_TXN_SERIALIZABLE

For more details see SAConnection::setIsolationLevel.

Working with Long or Lob(CLob, BLob) data

SQLAPI++ supports four types for working with Long or Lob(CLob, BLob) data:

Name C enum constant
LongBinary SA_dtLongBinary
LongChar SA_dtLongChar
BLob (Binary Large object) SA_dtBLob
CLob (Character Large object) SA_dtCLob

The table below shows how SQLAPI++ data types correspond with servers original data types:

SA_dtLongBinary <= > SQL_LONGVARBINARY
SA_dtLongChar <= > SQL_LONGVARCHAR
SA_dtBLob => SQL_LONGVARBINARY
SA_dtCLob => SQL_LONGVARCHAR

For more details see How To - Working with Long or Lob(CLob, BLob) data.

Returning output parameters

In ODBC processing output parameters depends on a driver.

If DBMS allows to return 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 set until all results returned by the procedure have been fetched (using SACommand::FetchNext method).

See ODBC driver documentation on output parameters availability on different DBMSs.

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 more details see SACommand::Execute, SAParam object, How To - Returning Output Parameters.

Cancelling queries

Using SACommand::Cancel method you can cancel the following types of processing on a statement:

  • A function running asynchronously on the statement.
  • A 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 ODBC documentation.

For more details see SACommand::Cancel.

Connection, command, parameter and field options

A server specific option can relate to a connection, command, parameter or field. We recommend you specify each option in an appropriate object, although it is possible to specify them in the parental object as well. In that case the option affects all the child objects.

An API related option must be specified in SAPI object. If an internal SAPI object used for the DBMS API initialization (implicit DBMS API initialization, see SAConnection::Connect method) the related DBMS specific options taken from initial connection object.

A connection related option may be specified in either SAPI object or SAConnection object. If it is specified in SAPI object it affects all the connections on that API.

A command related option may be specified in SAPI object, SAConnection object or SACommand object. If it is specified in a parent object it affects all the commands on that SAPI, SAConnection objects.

A parameter related option may be specified in SAPI object, SAConnection object, SACommand object or SAParam object. If it is specified in a parent object it affects all the parameters on that SAPI, SAConnection, SACommand objects.

A field related option may be specified in SAPI object, SAConnection object, SACommand object or SAField object. If it is specified in a parent object it affects all the fields on that on that SAPI, SAConnection, SACommand objects.

Specific options for ODBC:

Option name / ScopeDescription

ODBC.LIBS

API related. Should be specified before the API is initialized.

Forces 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 OS.
Default value : Windows - "odbc32.dll", Linux - "libiodbc.so.:libiodbc.so.3:libiodbc.so.2:libodbc.so:libodbc.so.1".

SQL_ATTR_CONNECTION_POOLING

API related. Should be specified before the API is initialized.

Forces 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" or "SQL_CP_ONE_PER_HENV".
Default value : none.

SQL_ATTR_CP_MATCH

API related. Should be specified before the API is initialized.

Forces 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" or "SQL_CP_RELAXED_MATCH".
Default value : none.

ODBCUseNumeric

Connection related. Should be specified before the first connection is made.

Forces 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 related. Should be specified before the first connection is made.

Forces 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 related. Should be specified before the first connection is made. Windows only.

SQLAPI++ uses SQLConnect (if connection string doesn't include '=' symbol) or SQLDriverConnect ODBC's function to initiate a database connection. When one of these parameters 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 related return only parameter. Can be read after the connection is made.

This option value is set with the completed connection string (by ODBC Driver Manager). Requires SQL_DRIVER_PROMPT, SQL_DRIVER_COMPLETE or SQL_DRIVER_COMPLETE_REQUIRED option used.

SQL_ATTR_CONNECTION_TIMEOUT

Connection related. Should be specified before the first connection is made.

Forces 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 related. Should be specified before command execution.

Forces SQLAPI++ Library to use SQLGetData call to get the value of the any result set field.

Valid values :"True", "1".
Default value: "false".

ODBCAddLongTextBufferSpace

Command related. Should be specified before command execution.

Forces SQLAPI++ Library to not use extra buffer for null character in the end when it tries to get the length of the long text field with SQLGetData call.

Valid values :"True", "1".
Default value: "false".

ODBCDisableLongLengthQuery

Command related. Should be specified before command execution.

Forces 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 related. Should be specified before command execution.

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 or
Scrollable

Command related. Should be specified before describing parameters or command execution.

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 related. Should be specified before describing parameters or command execution.

Allows to define the cursor name SQLAPI++ uses for the server side cursor.

Valid values : See SQLSetCursorName documentation.
Default value : none.

ExecDirect

Command related. Should be specified before describing parameters or command execution.

Forces SQLAPI++ to use SQLExecDirect instead of SQLExecute API function.

Valid values : "True", "1".
Default value : "false".

SQL_ATTR_CONCURRENCY

Command related. Should be specified before describing parameters or command execution.

Sets the statement concurrency attribute. See ODBC documentation for an additional information.

Valid values: "SQL_CONCUR_READONLY", "SQL_CONCUR_VALUES", "SQL_CONCUR_ROWVER", "SQL_CONCUR_LOCK".

SQL_ATTR_CURSOR_TYPE

Command related. Should be specified before describing parameters or command execution.

Sets the statement cursor type. See ODBC documentation for an additional information.

Valid values : "SQL_CURSOR_FORWARD_ONLY", "SQL_CURSOR_KEYSET_DRIVEN", "SQL_CURSOR_DYNAMIC", "SQL_CURSOR_STATIC".

SQL_ATTR_CURSOR_SCROLLABLE

Command related. Should be specified before describing parameters or command execution.

Sets the statement cursor scrollable. See ODBC documentation for an additional information.

Valid values: "SQL_NONSCROLLABLE", "SQL_SCROLLABLE".

SQL_ATTR_CURSOR_SENSITIVITY

Command related. Should be specified before describing parameters or command execution.

Sets the statement cursor sensitivity. See ODBC documentation for an additional information.

Valid values : "SQL_UNSPECIFIED", "SQL_INSENSITIVE", "SQL_SENSITIVE".

SQL_ATTR_QUERY_TIMEOUT

Command related. Should be specified before describing parameters or command execution.

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 a value.
Default value : none.

For more details see SAConnection::setOption, SACommand::setOption, SAField::setOption, SAParam::setOption.

Getting 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 table shows what type cast you have to make and what additional header file you have to include to work with ODBC API.

Type castingAdditional include file

Cast the result to class odbcAPI:

IsaAPI *pResult = con.NativeAPI();
odbcAPI *p_odbcAPI = (odbcAPI *)pResult;
#include "odbcAPI.h"

To get more information about DBMS API functions see this DBMS specific documentation.

For more details 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 a parameter(s). 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. The following table shows what type cast you have to make and what additional header file you have to include to work with specific ODBC API.

Type casting

Cast the result to class odbcConnectionHandles:

#include "odbcAPI.h"
saConnectionHandles *pResult = con.NativeHandles();
odbcConnectionHandles *p_odbcCH = (odbcConnectionHandles*)pResult;

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

For more details 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 a parameter(s). 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. The following table shows what type cast you have to make and what additional header file you have to include to work with specific ODBC API.

Type casting

Cast the result to class odbcCommandHandles:

#include "odbcAPI.h"
saCommandHandles *pResult = cmd.NativeHandles();
odbcCommandHandles *p_odbcCH = (odbcCommandHandles*)pResult;

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

For more details see SACommand::NativeHandles.

Error handling

When an error occurs inside SQLAPI++ Library it throws an exception of type SAException.

SAException::ErrPos method gets an error position in SQL statement. In ODBC server SAException::ErrPos method returns -1 because **ODBC ** does not support this function.

For more details see How To - Error handling, SAException object.

Special header files - Compile time

The header files are in the include subdirectory of SQLAPI++ distributions:

  • #include "SQLAPI.h" - main header, should be used whenever SQLAPI++ is used.
  • #include "odbcAPI.h" - ODBC API, should be included if direct ODBC calls are required.

For more details see Documentation - Instructions for Compiling and Linking Applications with SQLAPI++

If you haven't found the answer to your questions or have some problems on using the Library, please, send e-mail to howto@sqlapi.com
SQLAPI++ 5.0.1, Mon Oct 21 2019