/
statement.cpp
342 lines (300 loc) · 8.78 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
// Copyright (C) 2004-2007 Maciej Sobczak, Stephen Hutton
// 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_ORACLE_SOURCE
#include "soci-oracle.h"
#include "error.h"
#include <soci-backend.h>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <limits>
#include <sstream>
#ifdef _MSC_VER
#pragma warning(disable:4355)
#endif
using namespace soci;
using namespace soci::details;
using namespace soci::details::oracle;
oracle_statement_backend::oracle_statement_backend(oracle_session_backend &session)
: session_(session), stmtp_(NULL), boundByName_(false), boundByPos_(false),
noData_(false)
{
}
void oracle_statement_backend::alloc()
{
sword res = OCIHandleAlloc(session_.envhp_,
reinterpret_cast<dvoid**>(&stmtp_),
OCI_HTYPE_STMT, 0, 0);
if (res != OCI_SUCCESS)
{
throw soci_error("Cannot allocate statement handle");
}
}
void oracle_statement_backend::clean_up()
{
// deallocate statement handle
if (stmtp_ != NULL)
{
OCIHandleFree(stmtp_, OCI_HTYPE_STMT);
stmtp_ = NULL;
}
boundByName_ = false;
boundByPos_ = false;
}
void oracle_statement_backend::prepare(std::string const &query,
statement_type /* eType */)
{
sb4 stmtLen = static_cast<sb4>(query.size());
sword res = OCIStmtPrepare(stmtp_,
session_.errhp_,
reinterpret_cast<text*>(const_cast<char*>(query.c_str())),
stmtLen, OCI_V7_SYNTAX, OCI_DEFAULT);
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
}
statement_backend::exec_fetch_result oracle_statement_backend::execute(int number)
{
sword res = OCIStmtExecute(session_.svchp_, stmtp_, session_.errhp_,
static_cast<ub4>(number), 0, 0, 0, OCI_DEFAULT);
if (res == OCI_SUCCESS || res == OCI_SUCCESS_WITH_INFO)
{
return ef_success;
}
else if (res == OCI_NO_DATA)
{
noData_ = true;
return ef_no_data;
}
else
{
throw_oracle_soci_error(res, session_.errhp_);
return ef_no_data; // unreachable dummy return to please the compiler
}
}
statement_backend::exec_fetch_result oracle_statement_backend::fetch(int number)
{
if (noData_)
{
return ef_no_data;
}
sword res = OCIStmtFetch(stmtp_, session_.errhp_,
static_cast<ub4>(number), OCI_FETCH_NEXT, OCI_DEFAULT);
if (res == OCI_SUCCESS || res == OCI_SUCCESS_WITH_INFO)
{
return ef_success;
}
else if (res == OCI_NO_DATA)
{
noData_ = true;
return ef_no_data;
}
else
{
throw_oracle_soci_error(res, session_.errhp_);
return ef_no_data; // unreachable dummy return to please the compiler
}
}
long long oracle_statement_backend::get_affected_rows()
{
ub4 row_count;
sword res = OCIAttrGet(static_cast<dvoid*>(stmtp_),
OCI_HTYPE_STMT, &row_count,
0, OCI_ATTR_ROW_COUNT, session_.errhp_);
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
return row_count;
}
int oracle_statement_backend::get_number_of_rows()
{
int rows;
sword res = OCIAttrGet(static_cast<dvoid*>(stmtp_),
OCI_HTYPE_STMT, static_cast<dvoid*>(&rows),
0, OCI_ATTR_ROWS_FETCHED, session_.errhp_);
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
return rows;
}
std::string oracle_statement_backend::rewrite_for_procedure_call(
std::string const &query)
{
std::string newQuery("begin ");
newQuery += query;
newQuery += "; end;";
return newQuery;
}
int oracle_statement_backend::prepare_for_describe()
{
sword res = OCIStmtExecute(session_.svchp_, stmtp_, session_.errhp_,
1, 0, 0, 0, OCI_DESCRIBE_ONLY);
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
int cols;
res = OCIAttrGet(static_cast<dvoid*>(stmtp_),
static_cast<ub4>(OCI_HTYPE_STMT), static_cast<dvoid*>(&cols),
0, static_cast<ub4>(OCI_ATTR_PARAM_COUNT), session_.errhp_);
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
return cols;
}
void oracle_statement_backend::describe_column(int colNum, data_type &type,
std::string &columnName)
{
int size;
int precision;
int scale;
ub2 dbtype;
text* dbname;
ub4 nameLength;
ub2 dbsize;
sb2 dbprec;
ub1 dbscale; //sb2 in some versions of Oracle?
// Get the column handle
OCIParam* colhd;
sword res = OCIParamGet(reinterpret_cast<dvoid*>(stmtp_),
static_cast<ub4>(OCI_HTYPE_STMT),
reinterpret_cast<OCIError*>(session_.errhp_),
reinterpret_cast<dvoid**>(&colhd),
static_cast<ub4>(colNum));
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
// Get the column name
res = OCIAttrGet(reinterpret_cast<dvoid*>(colhd),
static_cast<ub4>(OCI_DTYPE_PARAM),
reinterpret_cast<dvoid**>(&dbname),
reinterpret_cast<ub4*>(&nameLength),
static_cast<ub4>(OCI_ATTR_NAME),
reinterpret_cast<OCIError*>(session_.errhp_));
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
// Get the column type
res = OCIAttrGet(reinterpret_cast<dvoid*>(colhd),
static_cast<ub4>(OCI_DTYPE_PARAM),
reinterpret_cast<dvoid*>(&dbtype),
0,
static_cast<ub4>(OCI_ATTR_DATA_TYPE),
reinterpret_cast<OCIError*>(session_.errhp_));
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
// get the data size
res = OCIAttrGet(reinterpret_cast<dvoid*>(colhd),
static_cast<ub4>(OCI_DTYPE_PARAM),
reinterpret_cast<dvoid*>(&dbsize),
0,
static_cast<ub4>(OCI_ATTR_DATA_SIZE),
reinterpret_cast<OCIError*>(session_.errhp_));
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
// get the precision
res = OCIAttrGet(reinterpret_cast<dvoid*>(colhd),
static_cast<ub4>(OCI_DTYPE_PARAM),
reinterpret_cast<dvoid*>(&dbprec),
0,
static_cast<ub4>(OCI_ATTR_PRECISION),
reinterpret_cast<OCIError*>(session_.errhp_));
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
// get the scale
res = OCIAttrGet(reinterpret_cast<dvoid*>(colhd),
static_cast<ub4>(OCI_DTYPE_PARAM),
reinterpret_cast<dvoid*>(&dbscale),
0,
static_cast<ub4>(OCI_ATTR_SCALE),
reinterpret_cast<OCIError*>(session_.errhp_));
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
columnName.assign(dbname, dbname + nameLength);
size = static_cast<int>(dbsize);
precision = static_cast<int>(dbprec);
scale = static_cast<int>(dbscale);
switch (dbtype)
{
case SQLT_CHR:
case SQLT_AFC:
type = dt_string;
break;
case SQLT_NUM:
if (scale > 0)
{
if (session_.get_option_decimals_as_strings())
type = dt_string;
else
type = dt_double;
}
else if (precision < std::numeric_limits<int>::digits10)
{
type = dt_integer;
}
else
{
type = dt_long_long;
}
break;
case SQLT_DAT:
type = dt_date;
break;
}
}
std::size_t oracle_statement_backend::column_size(int position)
{
// Note: we may want to optimize so that the OCI_DESCRIBE_ONLY call
// happens only once per statement.
// Possibly use existing statement::describe() / make column prop
// access lazy at same time
int colSize(0);
sword res = OCIStmtExecute(session_.svchp_, stmtp_,
session_.errhp_, 1, 0, 0, 0, OCI_DESCRIBE_ONLY);
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
// Get The Column Handle
OCIParam* colhd;
res = OCIParamGet(reinterpret_cast<dvoid*>(stmtp_),
static_cast<ub4>(OCI_HTYPE_STMT),
reinterpret_cast<OCIError*>(session_.errhp_),
reinterpret_cast<dvoid**>(&colhd),
static_cast<ub4>(position));
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
// Get The Data Size
res = OCIAttrGet(reinterpret_cast<dvoid*>(colhd),
static_cast<ub4>(OCI_DTYPE_PARAM),
reinterpret_cast<dvoid*>(&colSize),
0,
static_cast<ub4>(OCI_ATTR_DATA_SIZE),
reinterpret_cast<OCIError*>(session_.errhp_));
if (res != OCI_SUCCESS)
{
throw_oracle_soci_error(res, session_.errhp_);
}
return static_cast<std::size_t>(colSize);
}