/
statement.cpp
314 lines (267 loc) · 8.12 KB
/
statement.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//
// Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton
// Copyright (C) 2011 Denis Chapligin
// 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_DB2_SOURCE
#include "soci-db2.h"
#ifdef _MSC_VER
#pragma warning(disable:4355)
#endif
using namespace soci;
using namespace soci::details;
db2_statement_backend::db2_statement_backend(db2_session_backend &session)
: session_(session),hasVectorUseElements(false)
{
}
void db2_statement_backend::alloc()
{
SQLRETURN cliRC = SQL_SUCCESS;
cliRC = SQLAllocHandle(SQL_HANDLE_STMT,session_.hDbc,&hStmt);
if (cliRC != SQL_SUCCESS) {
throw db2_soci_error("Error while allocation statement handle",cliRC);
}
}
void db2_statement_backend::clean_up()
{
SQLRETURN cliRC = SQL_SUCCESS;
cliRC=SQLFreeStmt(hStmt,SQL_CLOSE);
if (cliRC != SQL_SUCCESS) {
throw db2_soci_error(db2_soci_error::sqlState("Statement handle close error",SQL_HANDLE_STMT,hStmt),cliRC);
}
cliRC=SQLFreeStmt(hStmt,SQL_UNBIND);
if (cliRC != SQL_SUCCESS) {
throw db2_soci_error(db2_soci_error::sqlState("Statement handle unbind error",SQL_HANDLE_STMT,hStmt),cliRC);
}
cliRC=SQLFreeHandle(SQL_HANDLE_STMT,hStmt);
if (cliRC != SQL_SUCCESS) {
throw db2_soci_error(db2_soci_error::sqlState("Statement handle clean-up error",SQL_HANDLE_STMT,hStmt),cliRC);
}
}
void db2_statement_backend::prepare(std::string const & query ,
statement_type /* eType */)
{
// rewrite the query by transforming all named parameters into
// the markers (:abc -> ?, etc.)
enum { normal, in_quotes, in_name } state = normal;
std::string name;
for (std::string::const_iterator it = query.begin(), end = query.end();
it != end; ++it)
{
switch (state)
{
case normal:
if (*it == '\'')
{
query_ += *it;
state = in_quotes;
}
else if (*it == ':')
{
// Check whether this is a cast operator (e.g. 23::float)
// and treat it as a special case, not as a named binding
const std::string::const_iterator next_it = it + 1;
if ((next_it != end) && (*next_it == ':'))
{
query_ += "::";
++it;
}
else
{
state = in_name;
}
}
else // regular character, stay in the same state
{
query_ += *it;
}
break;
case in_quotes:
if (*it == '\'')
{
query_ += *it;
state = normal;
}
else // regular quoted character
{
query_ += *it;
}
break;
case in_name:
if (std::isalnum(*it) || *it == '_')
{
name += *it;
}
else // end of name
{
names.push_back(name);
name.clear();
std::ostringstream ss;
ss << '?';
query_ += ss.str();
query_ += *it;
state = normal;
}
break;
}
}
if (state == in_name)
{
names.push_back(name);
std::ostringstream ss;
ss << '?';
query_ += ss.str();
}
SQLRETURN cliRC = SQLPrepare(hStmt,(SQLCHAR*)query_.c_str(),SQL_NTS);
if (cliRC!=SQL_SUCCESS) {
throw db2_soci_error("Error while preparing query",cliRC);
}
}
statement_backend::exec_fetch_result
db2_statement_backend::execute(int number )
{
SQLSMALLINT rows_processed = 0;
if (hasVectorUseElements)
{
SQLSetStmtAttr(hStmt, SQL_ATTR_PARAMS_PROCESSED_PTR, &rows_processed, 0);
}
// if we are called twice for the same statement we need to close the open
// cursor or an "invalid cursor state" error will occur on execute
SQLRETURN cliRC = SQLExecute(hStmt);
if (cliRC != SQL_SUCCESS)
{
throw db2_soci_error(db2_soci_error::sqlState("Statement execution error",SQL_HANDLE_STMT,hStmt),cliRC);
}
SQLSMALLINT colCount;
SQLNumResultCols(hStmt, &colCount);
if (number > 0 && colCount > 0)
{
return fetch(number);
}
SQLFreeStmt(hStmt,SQL_RESET_PARAMS); //Prepare for next call
return ef_success;
}
statement_backend::exec_fetch_result
db2_statement_backend::fetch(int number )
{
numRowsFetched = 0;
SQLSetStmtAttr(hStmt, SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN, 0);
SQLSetStmtAttr(hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)number, 0);
SQLSetStmtAttr(hStmt, SQL_ATTR_ROWS_FETCHED_PTR, &numRowsFetched, 0);
SQLRETURN cliRC = SQLFetch(hStmt);
if (SQL_NO_DATA == cliRC)
{
return ef_no_data;
}
if (cliRC != SQL_SUCCESS)
{
throw db2_soci_error("Error while fetching data",cliRC);
}
return ef_success;
}
long long db2_statement_backend::get_affected_rows()
{
// ...
return -1;
}
int db2_statement_backend::get_number_of_rows()
{
return numRowsFetched;
}
std::string db2_statement_backend::rewrite_for_procedure_call(
std::string const &query)
{
return query;
}
int db2_statement_backend::prepare_for_describe()
{
SQLSMALLINT numCols;
SQLNumResultCols(hStmt, &numCols);
return numCols;
}
void db2_statement_backend::describe_column(int colNum,
data_type & type, std::string & columnName )
{
SQLCHAR colNameBuffer[2048];
SQLSMALLINT colNameBufferOverflow;
SQLSMALLINT dataType;
SQLULEN colSize;
SQLSMALLINT decDigits;
SQLSMALLINT isNullable;
SQLRETURN cliRC = SQLDescribeCol(hStmt, static_cast<SQLUSMALLINT>(colNum),
colNameBuffer, 2048,
&colNameBufferOverflow, &dataType,
&colSize, &decDigits, &isNullable);
if (cliRC != SQL_SUCCESS)
{
throw db2_soci_error(db2_soci_error::sqlState("Error while describing column",SQL_HANDLE_STMT,hStmt),cliRC);
}
char const *name = reinterpret_cast<char const *>(colNameBuffer);
columnName.assign(name, std::strlen(name));
switch (dataType)
{
case SQL_TYPE_DATE:
case SQL_TYPE_TIME:
case SQL_TYPE_TIMESTAMP:
type = dt_date;
break;
case SQL_DOUBLE:
case SQL_DECIMAL:
case SQL_REAL:
case SQL_FLOAT:
case SQL_NUMERIC:
type = dt_double;
break;
case SQL_TINYINT:
case SQL_SMALLINT:
case SQL_INTEGER:
type = dt_integer;
break;
case SQL_BIGINT:
type = dt_long_long;
break;
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
default:
type = dt_string;
break;
}
}
std::size_t db2_statement_backend::column_size(int col) {
SQLCHAR colNameBuffer[2048];
SQLSMALLINT colNameBufferOverflow;
SQLSMALLINT dataType;
SQLULEN colSize;
SQLSMALLINT decDigits;
SQLSMALLINT isNullable;
SQLRETURN cliRC = SQLDescribeCol(hStmt, static_cast<SQLUSMALLINT>(col),
colNameBuffer, 2048,
&colNameBufferOverflow, &dataType,
&colSize, &decDigits, &isNullable);
if (cliRC != SQL_SUCCESS)
{
throw db2_soci_error(db2_soci_error::sqlState("Error while detecting column size",SQL_HANDLE_STMT,hStmt),cliRC);
}
return colSize;
}
db2_standard_into_type_backend * db2_statement_backend::make_into_type_backend()
{
return new db2_standard_into_type_backend(*this);
}
db2_standard_use_type_backend * db2_statement_backend::make_use_type_backend()
{
return new db2_standard_use_type_backend(*this);
}
db2_vector_into_type_backend *
db2_statement_backend::make_vector_into_type_backend()
{
return new db2_vector_into_type_backend(*this);
}
db2_vector_use_type_backend * db2_statement_backend::make_vector_use_type_backend()
{
hasVectorUseElements = true;
return new db2_vector_use_type_backend(*this);
}