SqlOdbcConnection Class Reference

#include <SqlOdbcConnection.h>

Inheritance diagram for SqlOdbcConnection:

Inheritance graph
[legend]
Collaboration diagram for SqlOdbcConnection:

Collaboration graph
[legend]

Public Member Functions

 SqlOdbcConnection ()
ConnectiongetConnObject ()
DbRetVal connect (char *user, char *pass)
 opens connection to the sql engine
DbRetVal disconnect ()
 closes connection to the sql engine and releases all the resources
DbRetVal commit ()
 Commits active transaction.
DbRetVal rollback ()
 Aborts the active transaction.
DbRetVal beginTrans (IsolationLevel isoLevel, TransSyncMode mode)
 Starts a new transaction.

Data Fields

SQLHENV envHdl
SQLHDBC dbHdl
IsolationLevel prevIsoLevel

Friends

class SqlFactory

Detailed Description

Definition at line 33 of file SqlOdbcConnection.h.


Constructor & Destructor Documentation

SqlOdbcConnection::SqlOdbcConnection (  )  [inline]

Definition at line 40 of file SqlOdbcConnection.h.

References AbsSqlConnection::innerConn.

00040 {innerConn = NULL; }


Member Function Documentation

DbRetVal SqlOdbcConnection::beginTrans ( IsolationLevel  isoLevel,
TransSyncMode  mode 
) [virtual]

Starts a new transaction.


The previous transaction should be either committed or rollback
before beginTrans is called.
Applications are required to start transaction before they attempt any
database operation.

Parameters:
isoLevel isolation level. Default is read committed.
Returns:
DbRetVal

Implements AbsSqlConnection.

Definition at line 88 of file SqlOdbcConnection.cxx.

References dbHdl, envHdl, ErrSysInit, OK, prevIsoLevel, READ_COMMITTED, READ_REPEATABLE, READ_UNCOMMITTED, SQL_ATTR_TXN_ISOLATION, SQL_ROLLBACK, SQL_SUCCEEDED, SQL_TXN_READ_COMMITTED, SQL_TXN_READ_UNCOMMITTED, SQL_TXN_REPEATABLE_READ, SQLSetConnectAttr(), and SQLTransact().

00089 {
00090     if (prevIsoLevel == isoLevel) return OK;
00091     DbRetVal rv = OK;
00092     int retVal =0;
00093     SQLPOINTER iso;
00094     
00095     switch(isoLevel)
00096     {
00097         case READ_UNCOMMITTED:
00098             iso = (SQLPOINTER)SQL_TXN_READ_UNCOMMITTED;
00099             break;
00100         case READ_COMMITTED:
00101             iso = (SQLPOINTER)SQL_TXN_READ_COMMITTED;
00102             break;
00103         case READ_REPEATABLE:
00104             iso = (SQLPOINTER)SQL_TXN_REPEATABLE_READ;
00105             break;
00106         default:
00107             iso = (SQLPOINTER)SQL_TXN_READ_COMMITTED;
00108             break;
00109     } 
00110 
00111     retVal = SQLSetConnectAttr(dbHdl, SQL_ATTR_TXN_ISOLATION, iso, 0);    
00112     if (!SQL_SUCCEEDED(retVal)) return ErrSysInit;
00113     prevIsoLevel = isoLevel;
00114     retVal = SQLTransact (envHdl, dbHdl, SQL_ROLLBACK);
00115     if (!SQL_SUCCEEDED(retVal)) rv = ErrSysInit;
00116     return rv;
00117 }

Here is the call graph for this function:

DbRetVal SqlOdbcConnection::commit (  )  [virtual]

Commits active transaction.


It makes all the changes made in the current transaction permanent and
it also releases the locks held by the current transaction.
After a transaction commits, application is required to start another
transaction for further database operations.

Returns:
DbRetVal

Implements AbsSqlConnection.

Definition at line 118 of file SqlOdbcConnection.cxx.

References dbHdl, envHdl, ErrSysInit, OK, SQL_COMMIT, SQL_SUCCEEDED, and SQLTransact().

00119 {
00120     DbRetVal rv = OK;
00121     int retVal = SQLTransact (envHdl, dbHdl, SQL_COMMIT);
00122     if (!SQL_SUCCEEDED(retVal)) rv = ErrSysInit;
00123     return rv;
00124 }

Here is the call graph for this function:

DbRetVal SqlOdbcConnection::connect ( char *  user,
char *  pass 
) [virtual]

opens connection to the sql engine

Parameters:
user username for authentication
pass password for authentication
Returns:
DbRetVal

Implements AbsSqlConnection.

Definition at line 23 of file SqlOdbcConnection.cxx.

References Conf::config, dbHdl, envHdl, ErrNoConnection, ErrSysInit, len, OK, printError, SQL_ATTR_AUTOCOMMIT, SQL_ATTR_ODBC_VERSION, SQL_AUTOCOMMIT_OFF, SQL_DRIVER_NOPROMPT, SQL_HANDLE_DBC, SQL_HANDLE_ENV, SQL_NTS, SQL_NULL_HANDLE, SQL_OV_ODBC3, SQL_SUCCEEDED, SQL_SUCCESS, SQLAllocHandle(), SQLDriverConnect(), SQLGetDiagRec(), SQLSetConnectAttr(), and SQLSetEnvAttr().

00024 {
00025     DbRetVal rv = OK;
00026     char dsn[72];
00027     sprintf(dsn, "DSN=%s;", Conf::config.getDSN());
00028     int retVal =0;
00029     retVal = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &envHdl);
00030     if (retVal) 
00031     {
00032         printError(ErrSysInit, "Unable to allocate ODBC handle \n"); 
00033         return ErrSysInit; 
00034     }
00035     SQLSetEnvAttr(envHdl, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
00036     
00037     retVal = SQLAllocHandle (SQL_HANDLE_DBC, envHdl, &dbHdl);
00038     if (retVal) 
00039     {
00040         printError(ErrSysInit, "Unable to allocate ODBC handle \n"); 
00041         return ErrSysInit; 
00042     }
00043     SQLCHAR outstr[1024];
00044     SQLSMALLINT outstrlen;
00045     retVal = SQLDriverConnect(dbHdl, NULL, (SQLCHAR*)dsn, SQL_NTS,
00046                          outstr, sizeof(outstr), &outstrlen,
00047                          SQL_DRIVER_NOPROMPT);
00048     if (!SQL_SUCCEEDED(retVal)) {
00049         printError(ErrSysInit, "Failed to connect to target database using dsn=%s\n", dsn);
00050 
00051     SQLINTEGER   i = 0;
00052     SQLINTEGER   native;
00053     SQLCHAR      state[ 7 ];
00054     SQLCHAR      text[256];
00055     SQLSMALLINT  len;
00056     SQLRETURN    ret;
00057 
00058     fprintf(stderr,
00059             "\n"
00060             "The driver reported the following diagnostics whilst running "
00061             "\n\n");
00062 
00063     do
00064     {
00065         ret = SQLGetDiagRec(SQL_HANDLE_DBC, dbHdl, ++i, state, &native, text,
00066                             sizeof(text), &len );
00067 
00068        if (SQL_SUCCEEDED(ret))
00069             printf("%s:%ld:%ld:%s\n", state, i, native, text);
00070     }
00071     while( ret == SQL_SUCCESS );
00072  rv = ErrNoConnection;
00073  rv = OK; //masking the error:tmp
00074     }
00075     //printError(ErrSysInit, "Connecting with dsn=%s\n", dsn);
00076     SQLSetConnectAttr(dbHdl, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, 0);
00077     return rv;
00078     
00079 }

Here is the call graph for this function:

DbRetVal SqlOdbcConnection::disconnect (  )  [virtual]

closes connection to the sql engine and releases all the resources

Returns:
DbRetVal

Implements AbsSqlConnection.

Definition at line 80 of file SqlOdbcConnection.cxx.

References dbHdl, envHdl, OK, SQL_HANDLE_DBC, SQL_HANDLE_ENV, SQLDisconnect(), and SQLFreeHandle().

00081 {
00082     DbRetVal rv = OK;
00083     SQLDisconnect (dbHdl);
00084     SQLFreeHandle (SQL_HANDLE_DBC, dbHdl);
00085     SQLFreeHandle (SQL_HANDLE_ENV, envHdl);
00086     return rv;
00087 }

Here is the call graph for this function:

Connection& SqlOdbcConnection::getConnObject (  )  [inline, virtual]

Implements AbsSqlConnection.

Definition at line 43 of file SqlOdbcConnection.h.

00043 {  return dummyConn; }

DbRetVal SqlOdbcConnection::rollback (  )  [virtual]

Aborts the active transaction.


undo all the changes made in the current transaction and it also
releases the locks held by the current transaction.
After a transaction rollback, application is required to start another
transaction for further database operations.

Returns:
DbRetVal

Implements AbsSqlConnection.

Definition at line 125 of file SqlOdbcConnection.cxx.

References dbHdl, envHdl, ErrSysInit, OK, SQL_ROLLBACK, SQL_SUCCEEDED, and SQLTransact().

00126 {
00127     DbRetVal rv = OK;
00128     int retVal = SQLTransact (envHdl, dbHdl, SQL_ROLLBACK);
00129     if (!SQL_SUCCEEDED(retVal)) rv = ErrSysInit;
00130     return rv;
00131 }

Here is the call graph for this function:


Friends And Related Function Documentation

friend class SqlFactory [friend]

Definition at line 55 of file SqlOdbcConnection.h.


Field Documentation

SQLHDBC SqlOdbcConnection::dbHdl

Definition at line 38 of file SqlOdbcConnection.h.

Referenced by beginTrans(), commit(), connect(), disconnect(), and rollback().

SQLHENV SqlOdbcConnection::envHdl

Definition at line 37 of file SqlOdbcConnection.h.

Referenced by beginTrans(), commit(), connect(), disconnect(), and rollback().

IsolationLevel SqlOdbcConnection::prevIsoLevel

Definition at line 39 of file SqlOdbcConnection.h.

Referenced by beginTrans().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 22:52:16 2008 for csql by  doxygen 1.4.7