Home How To Online Documentation Support Download Order
 

 

 

SACommand
SAConnection
SAException
SAField
SAParam

SAString
SADateTime
SANumeric
SAInterval

Enums and Typedefs


Class Members

Usage

SAConnection Examples


SAConnection constructor

/*
Default constructor creates SAConnection object that is not initially connected to database or associated with specific client. You have to call SAConnection::setClient() and/or SAConnection::Connect() later.
*/

SAConnection con, con2;


SAConnection destructor

/*
Destructor deletes SAConnection object. It also disconnect object from database if needed.
*/

SAConnection *pCon = new SAConnection ;

...

delete pCon;


setClient

/*
Exception handling is omitted for simplicity.
*/

SAConnection Con;

Con.setClient(SA_Oracle_Client);


Client

/*
Queries current DBMS client set by SAConnection::setClient or SAConnection::Connect.
*/

SAConnection Con;

/* connect to database and so on */
...

switch(Con.Client())
{
    case SA_Client_NotSpecified:
        break;
    case SA_ODBC_Client:
        /*do smth. specific for ODBC*/
        break;
    case SA_Oracle_Client:
        /*do smth. specific for Oracle*/
        break;
    case SA_SQLServer_Client:
        /*do smth. specific for SQLServer*/
        break;
    case SA_Interbase_Client:
        /*do smth. specific for Interbase*/
        break;
    case SA_SQLBase_Client:
        /*do smth. specific for SQLBase*/
        break;
    case SA_DB2_Client:
        /*do smth. specific for DB2*/
        break;
    case SA_Informix_Client:
        /*do smth. specific for Informix*/
        break;
    case SA_Sybase_Client:
        /*do smth. specific for Sybase*/
        break;

}


ClientVersion

/*
Query and print client version.
Exception handling is omitted for simplicity.
*/

SAConnection con;

con.setClient(SA_Oracle_Client);

short minor = (short)(con.ClientVersion() & 0xFFFF);

short major = (short)(con.ClientVersion() >> 16);

printf("Client version: %hd.%hd\n", major, minor);


ServerVersion

/*
Query and print server version.
Exception handling is omitted for simplicity.
*/

SAConnection con;

con.Connect("dbname", "username", "password", SA_Oracle_Client);

short minor = (short)(con.ServerVersion() & 0xFFFF);

short major = (short)(con.ServerVersion() >> 16);

printf("Server version: %hd.%hd\n", major, minor);


ServerVersionString

/*
Query and print server version string.
Exception handling is omitted for simplicity.
*/

SAConnection con;

con.Connect("dbname", "username", "password", SA_Oracle_Client);

printf("Server: %s\n", (const char*)con.ServerVersionString());


isConnected

/*
Disconnect only if we are really connected.
*/

SAConnection con;

/* connect to database and so on */
...

if(con.isConnected())

    con.Disconnect();


Connect

/*
First example.
Initialize client and connect to server with one call.
Exception handling is omitted for simplicity.
*/

SAConnection con, con2;

con.Connect("dbname", "username", "password", SA_Oracle_Client);

con2.Connect("srvname@dbname", "username", "password", SA_Sybase_Client);

/*
Second example.
First initialize client then connect to server.
Exception handling is omitted for simplicity.
*/

SAConnection con, con2;

con.setClient(SA_Oracle_Client);

con2.setClient(SA_Sybase_Client);

con.Connect("dbname", "username", "password");

con2.Connect("srvname@dbname", "username", "password");


Disconnect

/*
First example.
Disconnect only if we are really connected.
*/

SAConnection con;

/* connect to database and so on */
...

if(con.isConnected())

    con.Disconnect();

/*
Second example.
Disconnect is made implicitly in destructor.
*/

{

    SAConnection con;

/* connect to database and so on */
...

}

/* At this point con is physically disconnected from database in SAConnection destructor */

SAConnection *pCon = new SAConnection;

/* connect to database and so on */
...

/* next line will disconnect from database if needed */

delete pCon;


setIsolationLevel

/*
Sets transaction isolation level.
Exception handling is omitted for simplicity.
*/

SAConnection con;

con.Connect("dbname", "username", "password", SA_Oracle_Client);

con.setIsolationLevel(SA_Serializable);


IsolationLevel

/*
Query current isolation level.
Exception handling is omitted for simplicity.
*/

SAConnection con;

/* connect to database and so on */
...

switch(con.IsolationLevel())
{
    case SA_LevelUnknown:
        /*do smth. specific for default isolation*/
        break;
    case SA_ReadCommitted:
        /*do smth. specific for Read Committed*/
        break;
    case SA_ReadCommitted:
        /*do smth. specific for Read Committed*/
        break;
    case SA_RepeatableRead:
        /*do smth. specific for Repeatable Read*/
        break;
    case SA_Serializable:
        /*do smth. specific for Serializable*/
        break;
}


setAutoCommit

/*
This example turns autocommit on.
Exception handling is omitted for simplicity.
*/

SAConnection con;

con.Connect("dbname", "username", "password", SA_Oracle_Client);

con.setAutoCommit(SA_AutoCommitOn);


AutoCommit

/*
Query current autocommit option
Exception handling is omitted for simplicity.
*/

SAConnection con;

con.Connect("dbname", "username", "password", SA_Oracle_Client);

...

switch(con.AutoCommit())
{
    case SA_AutoCommitUnknown:
        printf("Autocommit mode is unknown (default)");
        break;
    case SA_AutoCommitOff:
        printf("Autocommit mode is off");
        break;
    case SA_AutoCommitOn:
        printf("Autocommit mode is on");
        break;
}


Commit

/*
Commits transaction.
Exception handling is omitted for simplicity.
*/

SAConnection con;

con.Connect("dbname", "username", "password", SA_Oracle_Client);

/*Create commands, execute SQL statements*/
...

con.Commit() ;


Rollback

/*
Rollbacks transaction.
Exception handling is omitted for simplicity.
*/

SAConnection con;

con.Connect("dbname", "username", "password", SA_Oracle_Client);

/*Create commands, execute SQL statements*/
...

con.Rollback() ;


NativeAPI

/*
Exception handling is omitted for simplicity.
*/

#include <ora7API.h>
#include <oraAPI.h>

SAConnection con;

con.Connect("dbname", "username", "password", SA_Oracle_Client);

if(con.ClientVersion() >= 0x0008000) /* OCI 8.x */
{

    ora8API *p_ora8API = (ora8API *)con.NativeAPI() ;

    /* call any OCI* function(s) */
    /* f.ex., p_ora8API->OCIStmtExecute(...) */
    ...

}
else /* OCI 7.x */
{

    ora7API *p_ora7API = (ora7API *)con.NativeAPI() ;

    /* call any o* function(s) */
    /* f.ex., p_ora7API->oparse(...) */
    ...

}


NativeHandles

/*
Exception handling is omitted for simplicity.
*/

#include <ora7API.h>
#include <oraAPI.h>

SAConnection con;

con.Connect("dbname", "username", "password", SA_Oracle_Client);

if(con.ClientVersion() >= 0x0008000) /* OCI 8.x */
{

    ora8API *p_ora8API = (ora8API *)con.NativeAPI();

    ora8ConnectionHandles *p_ora8CH = (ora8ConnectionHandles *)con.NativeHandles();

    /* call any OCI* function(s) */
    /* passing required connection handles */
    /* f.ex., p_ora8CH->m_pOCIServer */
    ...

}
else /* OCI 7.x */
{

    ora7API *p_ora7API = (ora7API *)con.NativeAPI();

    ora7ConnectionHandles *p_ora7CH = (ora7ConnectionHandles *)con.NativeHandles();

    /* call any o* function(s) */
    /* passing required connection handles */
    /* f.ex., p_ora7CH->m_lda */
    ...

}


Option

/*
Example 1. InterBase server option SQL Dialect.
Exception handling is omitted for simplicity.
*/

SAConnection con;

con.Connect("dbname", "username", "password", SA_InterBase_Client);

con.setOption("SQLDialect")="1";
/* All of the following commands associated with the connection will use SQL Dialect 1
*/

SACommand cmd1(&con, "Select task from test_tbl");

cmd1.Execute();

SACommand cmd2(&con, "Update test_tbl set task=3 where task= 8");

cmd2.Execute();

/* assume t_field is a TIMESTAMP type field (introduced in SQLDialect 3)
*/
SACommand cmd3(&con, "Select t_field from test_tbl");

/* You have to change SQLDialect option to SQL Dialect 3 to execute this command correctly
*/
cmd3.setOption("SQLDialect")= "3";

cmd3.Execute(); // no error

cmd2.setCommandText("Delete from test_tbl where task=0");
/* cmd2 still use SQL Dialect 1 */

// See also SACommand::Option examples


 Problems and Questions

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.