Home How To Online Documentation Support Download Order
 

 

 

SACommand
SAConnection
SAException
SAField
SAParam

SAString
SADateTime
SANumeric
SAInterval

Enums and Typedefs


Class Members

Usage

SACommand Examples


SACommand constructor

/*
Example 1. Using the default constructor.
*/

SAConnection con;

...

SACommand cmd;

cmd.setConnection(&con);

cmd.setCommandText("Select * from test_tbl");

...

/*
Example 2. Using more complex constructor.
*/

SAConnection con;

...

SACommand cmd(&con, "Select * from test_tbl");


SACommand destructor

/*
Delete command object
*/

SACommand *pCmd = new SACommand;

...

delete pCmd;


Connection

/*
Return the underlying connection and commit transaction.
Exception handling is omitted for simplicity.
*/

SACommand cmd;

...

SAConnection *pCon = cmd.Connection() ;

pCon->Commit();


setConnection

/*
Associate command and connection.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd;

cmd.setConnection(&con);


Open

/*
Explicitly open command.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con);

cmd.Open();


isOpened

/*
Close the command if it is opened.
Exception handling is omitted for simplicity.
*/

SAConnection con;

SACommand cmd(&con);

...

if(cmd.isOpened() )

    cmd.Close();


Close

/*
Close the command if it is opened.
Exception handling is omitted for simplicity.
*/

SAConnection con;

SACommand cmd(&con);

...

if(cmd.isOpened())

    cmd.Close();


setCommandText

/*
Exception handling is omitted for simplicity.
*/

SAConnection con;

...

SACommand cmd(&con);

cmd.setCommandText("Select * from test_tbl");

cmd.Execute();

...


CommandText

/*
Queries current command text.
*/

SAConnection con;

SACommand cmd(&con);

...

SAString sCmd = cmd.CommandText() ;


CommandType

/*
Queries current command type.
*/

SAConnection con;

SACommand cmd(&con, "Select * from test_tbl");

if(cmd.CommandType() != SA_CmdSQLStmt)

    printf("Very strange!\n");


Prepare

/*
Explicitly prepare a command.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "Select * from test_tbl");

cmd.Prepare();

...


Execute

/*
Execute a command.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "Select * from test_tbl");

cmd.Execute();

...


isResultSet

/*
Check whether executed command have a result set pending (even empty).
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "Select * from test_tbl");

cmd.Execute();

if(cmd.isResultSet())
{
    /* Fetch result set here*/
    ...
}
else
   printf("Very strange!\n");


RowsAffected

/*
Queries number of rows affected by the last operation.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "Delete * from test_tbl");

cmd.Execute();

printf("Rows deleted: %d\n", cmd.RowsAffected());


FetchNext

/*
Fetch result set row by row.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "Select * from test_tbl");

cmd.Execute();

while(cmd.FetchNext())
{
    ...
}


CreateParam

/*
Create params explicitly.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "test_proc");

cmd.CreateParam("nInput", SA_dtLong, sizeof(long), SA_ParamInput);

cmd.CreateParam("nOutput", SA_dtLong, sizeof(long), SA_ParamOutput);

cmd << SAPos("nInput") << 1L; /*bind input*/

cmd.Execute();

printf("Output value: %ld\n", cmd.Param("nOutput").asLong());


DestroyParams

/*
Destroy params explicitly.
> Exception handling is omitted for simplicity.
*/

SAConnection con;

SACommand cmd(&con, "test_proc");

...

cmd.DestroyParams();


ParamCount

/*
Print procedure parameters names.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "test_proc");

for(int i = 0; i < cmd.ParamCount(); ++i)
{
    printf("Param %d name: %s\n", i+1, (const char *)cmd.ParamByIndex().Name());
}


ParamByIndex

/*
Print procedure parameters names.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "test_proc");

for(int i = 0; i < cmd.ParamCount(); ++i)
{
    printf("Param %d name: %s\n", i+1, (const char *)cmd.ParamByIndex(i).Name());
}


Param

/*
Bind parameters by name and position.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "Select * from test_tbl where fid = :1 or fid = :fidvalue2");

cmd.Param(1).setAsLong() = 15;

cmd.Param("fidvalue2").setAsLong() = 32;

cmd.Execute();

...


operator <<

/*
Bind parameters by name and position.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "Select * from test_tbl where fid = :1 or fid = :fidvalue2");

cmd << 15L << SAPos("fidvalue2") << 32L;

cmd.Execute();

...


FieldCount

/*
Print select statement fields names.
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "Select * from test_tbl");

cmd.Execute();

for(int i = 0; i < cmd.FieldCount(); ++i)
{
    printf("Field %d name: %s\n", i+1, (const char *)cmd.Field(i+1).Name());
}


Field

/*
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "Select fid from test_tbl");

cmd.Execute();

if(cmd.FetchNext())
{
    if(cmd.Field(1).asLong() != cmd.Field("fid").asLong())
        printf("Very strange!\n");
}


operator []

/*
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "Select fid from test_tbl");

cmd.Execute();

if(cmd.FetchNext())
{
    if(cmd[1].asLong() != cmd["fid"].asLong())
        printf("Very strange!\n");
}


NativeHandles

/*
Exception handling is omitted for simplicity.
*/

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

SAConnection con;

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

SACommand cmd(&con, "Select * from test_tbl");

cmd.Open(); /* make sure command handles are initialized*/

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

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

    ora8CommandHandles *p_ora8CH = (ora8CommandHandles *)cmd.NativeHandles();

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

}
else /* OCI 7.x */
{

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

    ora7CommandHandles *p_ora7CH = (ora7CommandHandles *)cmd.NativeHandles();

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

}


Option

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

SAConnection con;

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

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

cmd.setOption("SQLDialect")= "1";

cmd.Execute(); // returns an error
             // because there is no TIMESTAMP
             // data type in SQL Dialect 1.

cmd.setOption("SQLDialect")="3";

cmd.setCommandText("Select t_field from test_tbl");

cmd.Execute(); // no error

while(cmd.FetchNext())
{
    ...
}

/*
Example 2. SQLBase server option SQLPPCX (cursor-context preservation).
Exception handling is omitted for simplicity.
*/

SAConnection con;

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

SACommand cmd(&con, "Select fid from test_tbl");

cmd.setOption("SQLPPCX")="on";

cmd.Execute();

con.Commit(); // result set will not be destroyed
              // after that command because SQLPPCX
              // option is "on" 

while(cmd.FetchNext())
{   
    printf("%d\n", cmd[1].asLong());
}

cmd.setOption("SQLPPCX")="off";

cmd.setCommandText("Select t_field from test_tbl");

cmd.Execute();

con.Commit(); // result set will be destroyed
              // after that command because SQPPCX
              // option is "off" 

/*
Example 3. SQLServer command option OpenCursor.
Exception handling is omitted for simplicity.
*/

/*The following code of Example 3 is incorrect,
it cause an error. See the right code below.*/

SAConnection con;

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

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

SACommand cmd2(&con, "Insert into temp_tbl (fid_tmp) values (:1)");

cmd1.Execute();

while(cmd1.FetchNext())
{   
    cmd2 << cmd1[1].asLong();
    cmd2.Execute(); // returns an error
                   // "Attempt to initiate
                   // a new SQLServer operation with
                   // results pending"
}
/* end of incorrect code */

/*The following code of Example 3 is correct.*/

SAConnection con;

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

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

cmd1.setOption("OpenCursor")="100";

SACommand cmd2(&con, "Insert into temp_tbl (fid_tmp) values (:1)");

cmd1.Execute();

while(cmd1.FetchNext())
{   
    cmd2 << cmd1[1].asLong();
    cmd2.Execute(); // no error
}

con.Commit();  


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.