Home How To Online Documentation Support Download Order
 

 

 


SACommand
SAConnection
SAException
SAField
SAParam
SAString
SADateTime
SANumeric

Enums and Typedefs


Server Specific Guide

ODBC

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 server using SQLAPI++ Library. Full information about using SQLAPI++ see in How To and Online Documentation .

Available topics:

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

Getting native ODBC API

Getting native ODBC connection related handles

Getting native ODBC command related handles

Error handling

Special header files - Compile time

 

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 server 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 eSAClientparameter is omitted.  You can use this default value only if you have call SAConnection::setClient method 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 server 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

SQLAPI++ maps data types to ODBC constants. Actual mapping from constant to SQL type is ODBC driver specific. The table below shows how SQLAPI++ data types correspond with ODBC constants:

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 procedure status return code or function return value (if any). 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.

A connection related option must be specified in a  SAConnection object.

A command related option may be specified in either SAConnection object or SACommand object. If it is specified in SAConnection object it affects all the commands on that connection.

A parameter related option may be specified in SAConnection object, SACommand object or SAParam object. If it is specified in SAConnection object it affects all the commands and therefore all the parameters on that connection. If it is specified in SACommand object it affects all the parameters on that command.

A field related option may be specified in SAConnection object, SACommand object or SAField object. If it is specified in SAConnection object it affects all the commands and therefore all the fields on that connection. If it is specified in SACommand object it affects all the fields on that command.

Specific options for ODBC:

Option name / Scope 

Description

ODBC.LIBS

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

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".

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".

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: "False", "0". Default value: "true".

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".

SQL_ATTR_CONNECTION_POOLING

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

Forces SQLAPI++ to set ODBC's SQL_ATTR_CONNECTION_POOLING attribute just before the first connection is made. 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: not specified. By default SQLAPI++ does not set SQL_ATTR_CONNECTION_POOLING attribute.

SQL_ATTR_CP_MATCH

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

Forces SQLAPI++ to set ODBC's SQL_ATTR_CP_MATCH attribute just before the first connection is made. See ODBC documentation for more information on this attribute.

Valid values: "SQL_CP_STRICT_MATCH" or "SQL_CP_RELAXED_MATCH".
Default value: not specified. By default SQLAPI++ does not set SQL_ATTR_CP_MATCH attribute.

SQL_DRIVER_PROMPT
SQL_DRIVER_COMPLETE
SQL_DRIVER_COMPLETE_REQUIRED

Connection related. Should be specified before the connection is made. For Win32 platforms 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: not specified. By default SQLAPI++ uses SQL_DRIVER_NOPROMPT driver completion flag.

DSN

Connection related return only parameter. Should be specified after the connection is made. For Win32 platforms only.

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.

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".

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.

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 saAPI 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 casting

Additional
include file

Cast the result to class odbcAPI:

saAPI *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;

Available handles:

  • SQLHENV m_hevn ;
  • SQLHDBC m_hdbc ;

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;

Available handles:

  • SQLHSTMT m_hstmt ;

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 API calls are required.

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


Problems and Questions

If you haven't found the answer to you questions or have some problems on using the Library, please, send e-mail to howto@sqlapi.com.