/
session.cpp
264 lines (219 loc) · 6.95 KB
/
session.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//
// Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#define SOCI_ODBC_SOURCE
#include "soci-odbc.h"
#include "session.h"
using namespace soci;
using namespace soci::details;
odbc_session_backend::odbc_session_backend(std::string const & connectString)
: henv_(0), hdbc_(0), product_(prod_uninitialized)
{
SQLRETURN rc;
// Allocate environment handle
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv_);
if (is_odbc_error(rc))
{
throw soci_error("Unable to get environment handle");
}
// Set the ODBC version environment attribute
rc = SQLSetEnvAttr(henv_, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_ENV, henv_,
"Setting ODBC version");
}
// Allocate connection handle
rc = SQLAllocHandle(SQL_HANDLE_DBC, henv_, &hdbc_);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_,
"Allocating connection handle");
}
SQLCHAR outConnString[1024];
SQLSMALLINT strLength;
rc = SQLDriverConnect(hdbc_, NULL, // windows handle
(SQLCHAR *)connectString.c_str(),
(SQLSMALLINT)connectString.size(),
outConnString, 1024,
&strLength, SQL_DRIVER_NOPROMPT);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_,
"Error Connecting to database");
}
reset_transaction();
}
odbc_session_backend::~odbc_session_backend()
{
clean_up();
}
void odbc_session_backend::begin()
{
SQLRETURN rc = SQLSetConnectAttr( hdbc_, SQL_ATTR_AUTOCOMMIT,
(SQLPOINTER)SQL_AUTOCOMMIT_OFF, 0 );
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_,
"Begin Transaction");
}
}
void odbc_session_backend::commit()
{
SQLRETURN rc = SQLEndTran(SQL_HANDLE_DBC, hdbc_, SQL_COMMIT);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_,
"Commiting");
}
reset_transaction();
}
void odbc_session_backend::rollback()
{
SQLRETURN rc = SQLEndTran(SQL_HANDLE_DBC, hdbc_, SQL_ROLLBACK);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_,
"Rolling back");
}
reset_transaction();
}
bool odbc_session_backend::get_next_sequence_value(
session & s, std::string const & sequence, long & value)
{
std::string query;
switch ( get_database_product() )
{
case prod_firebird:
query = "select next value for " + sequence + " from rdb$database";
break;
case prod_oracle:
query = "select " + sequence + ".nextval from dual";
break;
case prod_postgresql:
query = "select nextval('" + sequence + "')";
break;
case prod_mssql:
case prod_mysql:
case prod_sqlite:
// These RDBMS implement get_last_insert_id() instead.
return false;
case prod_unknown:
// For this one we can't do anything at all.
return false;
case prod_uninitialized:
// This is not supposed to happen at all but still cover this case
// here to avoid gcc warnings about unhandled enum values in a
// switch.
return false;
}
s << query, into(value);
return true;
}
bool odbc_session_backend::get_last_insert_id(
session & s, std::string const & table, long & value)
{
std::string query;
switch ( get_database_product() )
{
case prod_mssql:
query = "select ident_current('" + table + "')";
break;
case prod_mysql:
query = "select last_insert_id()";
break;
case prod_sqlite:
query = "select last_insert_rowid()";
break;
case prod_firebird:
case prod_oracle:
case prod_postgresql:
// For these RDBMS get_next_sequence_value() should have been used.
return false;
case prod_unknown:
// For this one we can't do anything at all.
return false;
case prod_uninitialized:
// As above, this is not supposed to happen but put it here to
// mollify gcc.
return false;
}
s << query, into(value);
return true;
}
void odbc_session_backend::reset_transaction()
{
SQLRETURN rc = SQLSetConnectAttr( hdbc_, SQL_ATTR_AUTOCOMMIT,
(SQLPOINTER)SQL_AUTOCOMMIT_ON, 0 );
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_,
"Set Auto Commit");
}
}
void odbc_session_backend::clean_up()
{
SQLRETURN rc = SQLDisconnect(hdbc_);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_,
"SQLDisconnect");
}
rc = SQLFreeHandle(SQL_HANDLE_DBC, hdbc_);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_,
"SQLFreeHandle DBC");
}
rc = SQLFreeHandle(SQL_HANDLE_ENV, henv_);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_ENV, henv_,
"SQLFreeHandle ENV");
}
}
odbc_statement_backend * odbc_session_backend::make_statement_backend()
{
return new odbc_statement_backend(*this);
}
odbc_rowid_backend * odbc_session_backend::make_rowid_backend()
{
return new odbc_rowid_backend(*this);
}
odbc_blob_backend * odbc_session_backend::make_blob_backend()
{
return new odbc_blob_backend(*this);
}
odbc_session_backend::database_product
odbc_session_backend::get_database_product()
{
// Cache the product type, it's not going to change during our life time.
if (product_ != prod_uninitialized)
return product_;
char product_name[1024];
SQLSMALLINT len = sizeof(product_name);
SQLRETURN rc = SQLGetInfo(hdbc_, SQL_DBMS_NAME, product_name, len, &len);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, henv_,
"SQLGetInfo(SQL_DBMS_NAME)");
}
if (strcmp(product_name, "Firebird") == 0)
product_ = prod_firebird;
else if (strcmp(product_name, "Microsoft SQL Server") == 0)
product_ = prod_mssql;
else if (strcmp(product_name, "MySQL") == 0)
product_ = prod_mysql;
else if (strcmp(product_name, "Oracle") == 0)
product_ = prod_oracle;
else if (strcmp(product_name, "PostgreSQL") == 0)
product_ = prod_postgresql;
else if (strcmp(product_name, "SQLite") == 0)
product_ = prod_sqlite;
else
product_ = prod_unknown;
return product_;
}