src/adapter/SqlOdbcConnection.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Prabakaran Thirumalai   *
00003  *   praba_tuty@yahoo.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <SqlOdbcConnection.h>
00021 #include <CSql.h>
00022 
00023 DbRetVal SqlOdbcConnection::connect (char *user, char * pass)
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 }
00080 DbRetVal SqlOdbcConnection::disconnect()
00081 {
00082     DbRetVal rv = OK;
00083     SQLDisconnect (dbHdl);
00084     SQLFreeHandle (SQL_HANDLE_DBC, dbHdl);
00085     SQLFreeHandle (SQL_HANDLE_ENV, envHdl);
00086     return rv;
00087 }
00088 DbRetVal SqlOdbcConnection::beginTrans(IsolationLevel isoLevel, TransSyncMode mode)
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 }
00118 DbRetVal SqlOdbcConnection::commit()
00119 {
00120     DbRetVal rv = OK;
00121     int retVal = SQLTransact (envHdl, dbHdl, SQL_COMMIT);
00122     if (!SQL_SUCCEEDED(retVal)) rv = ErrSysInit;
00123     return rv;
00124 }
00125 DbRetVal SqlOdbcConnection::rollback()
00126 {
00127     DbRetVal rv = OK;
00128     int retVal = SQLTransact (envHdl, dbHdl, SQL_ROLLBACK);
00129     if (!SQL_SUCCEEDED(retVal)) rv = ErrSysInit;
00130     return rv;
00131 }

Generated on Mon Jun 9 22:37:14 2008 for csql by  doxygen 1.4.7