summaryrefslogtreecommitdiff
path: root/libodbcpp/connection.cpp
blob: 087fc691710b6312e769de9d1cd5e5347601ae90 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <sqlext.h>
#include <syslog.h>
#include <stdio.h>
#include <string.h>
#include "connection.h"
#include "selectcommand.h"
#include "modifycommand.h"
#include "error.h"

ODBC::Connection::Connection(const DSN& d) :
	env(0),
	conn(0),
	thinkDelStyle(DB::BulkDeleteUsingUsing),
	thinkUpdStyle(DB::BulkUpdateUsingFromSrc),
	txDepth(0),
	txAborted(false)
{
	connectPre();
	RETCODE dberr = SQLConnect(conn, (SQLCHAR*)d.dsn.c_str(), SQL_NTS,
			(SQLCHAR*)d.username.c_str(), SQL_NTS, (SQLCHAR*)d.password.c_str(), SQL_NTS);
	if (!SQL_SUCCEEDED(dberr)) {
		throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Connect");
	}
	connectPost();
}

void
ODBC::Connection::connectPre()
{
	SQLRETURN dberr = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
	if (!SQL_SUCCEEDED(dberr)) {
		throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate handle");
	}

	dberr = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
	if (!SQL_SUCCEEDED(dberr)) {
		throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Set ODBC version");
	}

	dberr = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
	if (!SQL_SUCCEEDED(dberr)) {
		throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Allocate DBC handle");
	}

	dberr = SQLSetConnectAttr(conn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
	if (!SQL_SUCCEEDED(dberr)) {
		throw ConnectionError(dberr, SQL_HANDLE_ENV, env, "Set connection attributes");
	}
}

void
ODBC::Connection::connectPost()
{
	RETCODE dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
	if (!SQL_SUCCEEDED(dberr)) {
		throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit");
	}
	char info[1024];
	dberr = SQLGetInfo(conn, SQL_DRIVER_NAME, (SQLCHAR*)info, sizeof(info), NULL);
	if (!SQL_SUCCEEDED(dberr)) {
		throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Get info");
	}
	// Apply known DB specific tweaks
	if (strstr(info, "myodbc") != NULL) {
		thinkDelStyle = DB::BulkDeleteUsingUsingAlias;
		thinkUpdStyle = DB::BulkUpdateUsingJoin;
	}
}

ODBC::Connection::Connection(const std::string & s) :
	env(0),
	conn(0),
	thinkDelStyle(DB::BulkDeleteUsingUsing),
	thinkUpdStyle(DB::BulkUpdateUsingFromSrc),
	txDepth(0),
	txAborted(false)
{
	connectPre();
	RETCODE dberr = SQLDriverConnect(conn, NULL, (SQLCHAR*)s.c_str(), s.length(), NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
	if (!SQL_SUCCEEDED(dberr)) {
		throw ConnectionError(dberr, SQL_HANDLE_DBC, conn, "Connect");
	}
	connectPost();
}

ODBC::Connection::~Connection()
{
	if (conn) {
		if (!SQL_SUCCEEDED(SQLDisconnect(conn))) {
			syslog(LOG_WARNING, "%s: Disconnect error", __FUNCTION__);
		}
		if (!SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_DBC, conn))) {
			syslog(LOG_WARNING, "%s: Free connection handle error", __FUNCTION__);
		}
	}
	if (env) {
		if (!SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_ENV, env))) {
			syslog(LOG_WARNING, "%s: Free connection handle error", __FUNCTION__);
		}
	}
}

int
ODBC::Connection::beginTx() const
{
	if (txDepth == 0) {
		SQLRETURN dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
		if (!SQL_SUCCEEDED(dberr)) {
			throw Error(dberr, SQL_HANDLE_DBC, conn, "Set default auto commit");
		}
	}
	txDepth += 1;
	return txDepth;
}

int
ODBC::Connection::commitTx() const
{
	if (txDepth > 0) {
		if (txAborted) {
			return rollbackTx();
		}
		txDepth -= 1;
		if (txDepth == 0) {
			SQLRETURN dberr = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_COMMIT);
			if (!SQL_SUCCEEDED(dberr)) {
				throw Error(dberr, SQL_HANDLE_DBC, conn, "SQLEndTran (SQL_COMMIT)");
			}
			dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
			if (!SQL_SUCCEEDED(dberr)) {
				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() const
{
	if (txDepth > 0) {
		txDepth -= 1;
		if (txDepth == 0) {
			SQLRETURN dberr = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_ROLLBACK);
			if (!SQL_SUCCEEDED(dberr)) {
				throw Error(dberr, SQL_HANDLE_DBC, conn, "SQLEndTran (SQL_ROLLBACK)");
			}
			dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
			if (!SQL_SUCCEEDED(dberr)) {
				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;
}

bool
ODBC::Connection::inTx() const
{
	return (txDepth > 0);
}

DB::BulkDeleteStyle
ODBC::Connection::bulkDeleteStyle() const
{
	return thinkDelStyle;
}

DB::BulkUpdateStyle
ODBC::Connection::bulkUpdateStyle() const
{
	return thinkUpdStyle;
}

DB::SelectCommand *
ODBC::Connection::newSelectCommand(const std::string & sql) const
{
	return new ODBC::SelectCommand(*this, sql);
}

DB::ModifyCommand *
ODBC::Connection::newModifyCommand(const std::string & sql) const
{
	return new ODBC::ModifyCommand(*this, sql);
}

std::string
ODBC::Connection::getAttrStr(SQLINTEGER attr) const
{
	std::string rtn;
	rtn.resize(BUFSIZ);
	SQLINTEGER size = 0;
	SQLINTEGER dberr = SQLGetConnectAttr(conn, attr, (unsigned char *)rtn.c_str(), BUFSIZ, &size);
	if (!SQL_SUCCEEDED(dberr)) {
		throw ODBC::Error(dberr, SQL_HANDLE_DBC, conn, "ODBC::Connection::getAttrStr SQLGetConnectAttr");
	}
	rtn.resize(size);
	return rtn;
}

SQLINTEGER
ODBC::Connection::getAttrInt(SQLINTEGER attr) const
{
	SQLINTEGER result;
	SQLINTEGER dberr = SQLGetConnectAttr(conn, attr, &result, sizeof(result), 0);
	if (!SQL_SUCCEEDED(dberr)) {
		throw ODBC::Error(dberr, SQL_HANDLE_DBC, conn, "ODBC::Connection::getAttrInt SQLGetConnectAttr");
	}
	return result;
}

void
ODBC::Connection::ping() const
{
	SQLINTEGER dead = getAttrInt(SQL_ATTR_CONNECTION_DEAD);
	if (dead != SQL_CD_FALSE) {
		throw ODBC::Error("Connection is dead");
	}
}

ODBC::ConnectionError::ConnectionError(RETCODE err, SQLSMALLINT handletype, SQLHANDLE handle, char const * stage) :
	ODBC::Error(err, handletype, handle, stage),
	DB::ConnectionError()
{
}

ODBC::ConnectionError::ConnectionError(const ConnectionError & e) :
	ODBC::Error(strdup(e.what())),
	DB::ConnectionError(e.FailureTime)
{
}