Sybase 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 Sybase 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
The SQLAPI++ Library requires Open Client version 10.0 or higher.

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

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

sDBString

Connection string in the following format:

  • "" or "@" - empty string or '@' character, connects to a default database on a local server
  • <database_name> or @<database_name> - connects to a database with the specified name on local server
  • <serve_name>@ - connects to a default database on the specified server
  • <server_name>@<database_name> - connects to a database with the specified name on the specified server

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_Sybase_Client Sybase 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_Sybase_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 Sybase in the following way:

SA_ReadUncommittedread uncommitted
SA_ReadCommittedread committed
SA_RepeatableReadrepeatable read
SA_Serializableserializable

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

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

imageSA_dtLongBinary
textSA_dtLongChar
unitextSA_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 Sybase API data types:
SA_dtLongBinaryCS_IMAGE_TYPE
SA_dtLongCharCS_TEXT_TYPE
SA_dtBLobCS_IMAGE_TYPE
SA_dtCLobCS_TEXT_TYPE

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

Returning output parameters

Sybase ASE server does not provide information about parameter's direction type, that's why SQLAPI++ Library defines all parameters (except status result code) as input (SA_ParamInput). If you have input-output parameters in the procedure you have to call SAParam::setParamDirType method for these parameters before command execution and set parameter's direction type explicitly.

You shouldn't call SAParam::setParamDirType method for procedure status result code because it is detected correctly (as SA_ParamReturn) by the Library.

There is no need to call SAParam::setParamDirType method for Sybase ASA because all parameters are detected correctly automatically (including the direction type).

In Sybase server stored procedures can have integer return codes and output parameters. The return codes and output parameters are sent in the last packet from the server and are therefore not available to the application until all result sets from stored procedure (if any) are completely processed using SACommand::FetchNext method.

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 ct_cancel function to cancel a query. To get more details see ct_cancel function description in native Sybase 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 Sybase:

SYBINTL.LIBS
Api Scope
Linux/Unix only. Forces SQLAPI++ Library to use specified Sybase INTL library.
Valid values: Any valid library name list. Names separated by ':'.
Default value: "libsybintl.so:libintl.so"
SYBCOMN.LIBS
Api Scope
Linux/Unix only. Forces SQLAPI++ Library to use specified Sybase COMN library.
Valid values: Any valid library name list. Names separated by ':'.
Default value: "libsybcomn.so:libcomn.so"
SYBTCL.LIBS
Api Scope
Linux/Unix only. Forces SQLAPI++ Library to use specified Sybase TCL library.
Valid values: Any valid library name list. Names separated by ':'.
Default value: "libsybtcl.so:libtcl.so"
SYBCT.LIBS
Api Scope
Forces SQLAPI++ Library to use specified Sybase CT-library.
Valid values: Any valid CT-Lib library name list. Names separated by ';' on Windows or ':' on other operating systems.
Default value:
  • Windows - "libsybct.dll;libct.dll"
  • Linux - "libsybct.so:libct.so"
Special values: "STATIC", forces using the linked Sybase client API functions when the library is compiled with SA_STATIC_SYBASE build option.
SYBCS.LIBS
Api Scope
Forces SQLAPI++ Library to use specified Sybase CT-library.
Valid values: Any valid CS-Lib library name list. Names separated by ';' on Windows or ':' on other operating systems.
Default value:
  • Windows - "libsybcs.dll;libcs.dll"
  • Linux - "libsybcs.so:libcs.so"
CS_VERSION
Api Scope
Describes the version of Client-Library behavior that the application expects. For more information see Sybase documentation.
Valid values:
  • "CS_VERSION_155" – 15.5 behavior
  • "CS_VERSION_150" – 15.0 behavior
  • "CS_VERSION_125" – 12.5 behavior
  • "CS_VERSION_110" – 11.0 behavior
  • "CS_VERSION_100" – 10.0 behavior
  • "Detect" – SQLAPI++ automatically detects the maximum available version of behavior via the version of Client-Library
  • "Default" – SQLAPI++ tries to set 15.5 behavior; if it fails, tries to set 15.0 behavior; if it fails, SQLAPI++ tries to set 12.5 behavior; if it fails, SQLAPI++ tries to set 11.0 behavior and so on from the highest version to the lowest one until it succeeds
Default value: "Default"
CS_LC_ALL
CS_LOCALE
Api Scope
Allows to change the client side connection locale. Should be supported by the server side. For more information see Sybase documentation.
Valid values: Any valid Sybase locale name
Default value: none
CS_SYB_CHARSET
Api Scope
Allows to change the client side character set (applies only for non-Unicode Library build). Must be supported by the server side. For more information see Sybase documentation.
Valid values: Any valid Sybase character set name
Default value: none
CS_PACKETSIZE
Connection Scope
Determines the packet size that Client-Library uses when sending Tabular Data Stream (TDS) packets. For more information see Sybase documentation.
Valid values: String containing packet size, see Sybase documentation
Default value: See Sybase documentation
CS_APPNAME
APPNAME
Connection Scope
Defines the application name that a connection will use when connecting to a server. For more information see Sybase documentation.
Valid values: See Sybase documentation
Default value: See Sybase documentation
CS_HOSTNAME
WSID
Connection Scope
Declares the name of the host machine, used when logging in to a server. For more information see Sybase documentation.
Valid values: See Sybase documentation
Default value: See Sybase documentation
CS_BULK_LOGIN
Connection Scope
Describes whether or not a connection can perform bulk copy operations into a database. For information on Bulk Copy, see the Sybase Common Libraries Reference Manual.
Valid values: "CS_TRUE", "CS_FALSE"
Default value: See Sybase documentation
CS_SEC_ENCRYPTION
CS_SEC_EXTENDED_ENCRYPTION
CS_SEC_NON_ENCRYPTION_RETRY
Connection Scope
Describe the connection password encryption details. For information on connection password encryption, see the Sybase Common Libraries Reference Manual.
Valid values: "CS_TRUE", "CS_FALSE"
Default value: See Sybase documentation
CS_HAFAILOVER
Connection Scope
Describes whether or not a connection uses HA failover. For information on CS_HAFAILOVER, see the Sybase Common Libraries Reference Manual.
Valid values: "CS_TRUE", "CS_FALSE"
Default value: none
CS_LOGIN_TIMEOUT
Connection Scope
For more information see Sybase documentation.
Default value: none
CS_TIMEOUT
Connection Scope
For more information see Sybase documentation.
Default value: none
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 declare and open Sybase insensitive scrollable cursor.
Valid values: "True", "1"
Default value: "False"
ct_cursor
Command Scope
Instructs SQLAPI++ to use ct_cursor for current statement execution (as opposed to defaulting to ct_command). See Sybase documentation for more information on using cursors and their advantages and limitations.
Valid values: A cursor name. See Sybase documentation on what constitutes a valid cursor name.
Default value: none
SybaseResultType
Command Scope
Read only. Stores the current result type.
Valid values: "", "CS_ROW_RESULT", "CS_STATUS_RESULT", "CS_PARAM_RESULT", "CS_COMPUTE_RESULT", "CS_CURSOR_RESULT"
SybaseResultCount
Command Scope
Read only. Counts the output results, zero based.
Valid values: "0", "1", ...

For additional information see SAOptions::setOption.

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

#include "sybAPI.h"

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

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

For additional information see SAConnection::NativeAPI.

Getting native Sybase connection related handles

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

#include "sybAPI.h"

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

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

For additional information see SAConnection::NativeHandles.

Getting native Sybase command related handles

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

#include "sybAPI.h"

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

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

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