00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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;
00074 }
00075
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 }