1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#include <sqlext.h>
#include <syslog.h>
#include "connection.h"
#include "error.h"
ODBC::Connection::Connection(const DSN& d) :
DSN(d),
env(0),
conn(0),
txDepth(0),
txAborted(false)
{
SQLRETURN dberr = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
if ((dberr != SQL_SUCCESS)) {
throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate handle");
}
dberr = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
if ((dberr != SQL_SUCCESS)) {
throw Error(dberr, SQL_HANDLE_ENV, env, "Set ODBC version");
}
dberr = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
if ((dberr != SQL_SUCCESS)) {
throw Error(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle");
}
dberr = SQLSetConnectAttr(conn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
if ((dberr != SQL_SUCCESS)) {
throw Error(dberr, SQL_HANDLE_ENV, env, "Set connection attributes");
}
dberr = SQLConnect(conn, dsn, SQL_NTS, username, SQL_NTS, password, SQL_NTS);
if ((dberr != SQL_SUCCESS)) {
throw Error(dberr, SQL_HANDLE_DBC, conn, "Connect");
}
dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
if ((dberr != SQL_SUCCESS)) {
throw Error(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit");
}
}
ODBC::Connection::~Connection()
{
if (conn) {
if (SQLDisconnect(conn) != SQL_SUCCESS) {
syslog(LOG_WARNING, "%s: Disconnect error", __FUNCTION__);
}
if (SQLFreeHandle(SQL_HANDLE_DBC, conn) != SQL_SUCCESS) {
syslog(LOG_WARNING, "%s: Free connection handle error", __FUNCTION__);
}
}
if (env) {
if (SQLFreeHandle(SQL_HANDLE_ENV, env) != SQL_SUCCESS) {
syslog(LOG_WARNING, "%s: Free connection handle error", __FUNCTION__);
}
}
}
int
ODBC::Connection::beginTx()
{
SQLRETURN dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
if ((dberr != SQL_SUCCESS)) {
throw Error(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit");
}
txDepth += 1;
return txDepth;
}
int
ODBC::Connection::commitTx()
{
if (txDepth > 0) {
if (txAborted) {
return rollbackTx();
}
txDepth -= 1;
if (txDepth == 0) {
SQLRETURN dberr = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_COMMIT);
if ((dberr != SQL_SUCCESS)) {
throw Error(dberr, SQL_HANDLE_DBC, conn, "SQLEndTran (SQL_COMMIT)");
}
dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
if ((dberr != SQL_SUCCESS)) {
throw Error(dberr, SQL_HANDLE_DBC, conn, "Enable auto commit");
}
txAborted = false;
}
return txDepth;
}
throw Error("Attempt to commit none existant transaction");
}
int
ODBC::Connection::rollbackTx()
{
if (txDepth > 0) {
txDepth -= 1;
if (txDepth == 0) {
SQLRETURN dberr = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_ROLLBACK);
if ((dberr != SQL_SUCCESS)) {
throw Error(dberr, SQL_HANDLE_DBC, conn, "SQLEndTran (SQL_ROLLBACK)");
}
dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
if ((dberr != SQL_SUCCESS)) {
throw Error(dberr, SQL_HANDLE_DBC, conn, "Enable auto commit");
}
txAborted = false;
}
return txDepth;
}
throw Error("Attempt to rollback none existant transaction");
}
void
ODBC::Connection::abortTx() const
{
txAborted = true;
}
bool
ODBC::Connection::txIsAborted() const
{
return txAborted;
}
|