1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include <SqlOdbcConnection.h>
23 DbRetVal
SqlOdbcConnection::connect (char *user
, char * pass
)
27 sprintf(dsn
, "DSN=%s;", Conf::config
.getDSN());
29 retVal
= SQLAllocHandle (SQL_HANDLE_ENV
, SQL_NULL_HANDLE
, &envHdl
);
32 printError(ErrSysInit
, "Unable to allocate ODBC handle \n");
35 SQLSetEnvAttr(envHdl
, SQL_ATTR_ODBC_VERSION
, (void *) SQL_OV_ODBC3
, 0);
37 retVal
= SQLAllocHandle (SQL_HANDLE_DBC
, envHdl
, &dbHdl
);
40 printError(ErrSysInit
, "Unable to allocate ODBC handle \n");
44 SQLSMALLINT outstrlen
;
45 retVal
= SQLDriverConnect(dbHdl
, NULL
, (SQLCHAR
*)dsn
, SQL_NTS
,
46 outstr
, sizeof(outstr
), &outstrlen
,
48 if (!SQL_SUCCEEDED(retVal
)) {
49 printError(ErrSysInit
, "Failed to connect to target database using dsn=%s\n", dsn
);
60 "The driver reported the following diagnostics whilst running "
65 ret
= SQLGetDiagRec(SQL_HANDLE_DBC
, dbHdl
, ++i
, state
, &native
, text
,
68 if (SQL_SUCCEEDED(ret
))
69 printf("%s:%ld:%ld:%s\n", state
, i
, native
, text
);
71 while( ret
== SQL_SUCCESS
);
73 rv
= OK
; //masking the error:tmp
75 //printError(ErrSysInit, "Connecting with dsn=%s\n", dsn);
76 SQLSetConnectAttr(dbHdl
, SQL_ATTR_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
, 0);
80 DbRetVal
SqlOdbcConnection::disconnect()
83 SQLDisconnect (dbHdl
);
84 SQLFreeHandle (SQL_HANDLE_DBC
, dbHdl
);
85 SQLFreeHandle (SQL_HANDLE_ENV
, envHdl
);
88 DbRetVal
SqlOdbcConnection::beginTrans(IsolationLevel isoLevel
, TransSyncMode mode
)
90 if (prevIsoLevel
== isoLevel
) return OK
;
97 case READ_UNCOMMITTED
:
98 iso
= (SQLPOINTER
)SQL_TXN_READ_UNCOMMITTED
;
101 iso
= (SQLPOINTER
)SQL_TXN_READ_COMMITTED
;
103 case READ_REPEATABLE
:
104 iso
= (SQLPOINTER
)SQL_TXN_REPEATABLE_READ
;
107 iso
= (SQLPOINTER
)SQL_TXN_READ_COMMITTED
;
111 retVal
= SQLSetConnectAttr(dbHdl
, SQL_ATTR_TXN_ISOLATION
, iso
, 0);
112 if (!SQL_SUCCEEDED(retVal
)) return ErrSysInit
;
113 prevIsoLevel
= isoLevel
;
114 retVal
= SQLTransact (envHdl
, dbHdl
, SQL_ROLLBACK
);
115 if (!SQL_SUCCEEDED(retVal
)) rv
= ErrSysInit
;
118 DbRetVal
SqlOdbcConnection::commit()
121 int retVal
= SQLTransact (envHdl
, dbHdl
, SQL_COMMIT
);
122 if (!SQL_SUCCEEDED(retVal
)) rv
= ErrSysInit
;
125 DbRetVal
SqlOdbcConnection::rollback()
128 int retVal
= SQLTransact (envHdl
, dbHdl
, SQL_ROLLBACK
);
129 if (!SQL_SUCCEEDED(retVal
)) rv
= ErrSysInit
;