DCL 3.7.4
Loading...
Searching...
No Matches
IBParam.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#include <stdlib.h> // malloc, free
4#include <time.h> // struct tm
5
6#include <ibase.h>
7
8#include <dcl/Object.h>
9#if __DCL_HAVE_ALLOC_DEBUG
10#undef __DCL_ALLOC_LEVEL
11#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
12#endif
13
14#include <dcl/SQLCore.h>
16#include <dcl/InputStream.h>
17#include <dcl/Charset.h>
18
19#include "IBConnection.h"
20#include "IBQuery.h"
21#include "IBParam.h"
22
23#undef __THIS_FILE__
24static const char_t __THIS_FILE__[] = __T("dcl/sql/IBParam.cpp");
25
26__DCL_BEGIN_NAMESPACE
27
28#define __SET_ERROR(_error) \
29 conn()->setErrorHandle(_error, __THIS_FILE__, __LINE__)
30#define __SET_ERROR_MSG(_msg) \
31 conn()->setErrorMessage(_msg, __THIS_FILE__, __LINE__)
32
34
35#define __SQLTYPE_IS(_sqltype) ((__sqltype & ~1) == _sqltype)
36
38 : Param(NULL)
39{
40 __sqlvar = NULL;
41}
42
44{
45 if (__indicator == 0 && __SQLTYPE_IS(SQL_BLOB)) {
47 }
48}
49
50bool IBParam::init(SQL::Query* _queryHandle, XSQLVAR* _sqlvar)
51{
52 __DCL_ASSERT(__sqlvar == NULL);
53
54 Param::__queryHandle = _queryHandle;
55
56 __indicator = -1;
57 __sqltype = _sqlvar->sqltype;
58
59 __sqlvar = _sqlvar;
60 __sqlvar->sqldata = NULL;
61 __sqlvar->sqlind = &__indicator;
62 __sqlvar->sqlscale = 0;
63
64 return true;
65}
66
68{
69 Param::__dataType = SQL::typeUnknown;
70 __indicator = -1; // set null
71 __sqlvar->sqldata = NULL;
72}
73
74// in IBField.cpp
75const wchar_t* __dataTypeName(const XSQLVAR* _sqlvar);
76
77const wchar_t* IBParam::serverDataTypeName() const
78{
79 return __dataTypeName(__sqlvar);
80}
81
83{
84 if (__indicator == 0 && __SQLTYPE_IS(SQL_BLOB)) {
86 }
87
88 Param::__dataType = SQL::typeUnknown;
89 __indicator = -1; // set null
90 __sqlvar->sqldata = NULL;
91}
92
94 _CONST void* _val,
95 size_t _size,
96 SQL::DataType _valType,
97 SQL::DataType _sqlType
98)
99{
100 if (__indicator == 0 && __SQLTYPE_IS(SQL_BLOB)) {
101 if (!cancelBlob())
102 return false;
103 }
104
105 bool r = false;
106 switch(_valType) {
107 case SQL::typeInteger: {
108 r = setInteger(_val, _size);
109 break;
110 }
111 case SQL::typeUInteger: {
112 r = setUInteger(_val, _size);
113 break;
114 }
115 case SQL::typeFloat: {
116 r = setFloat(_val, _size);
117 break;
118 }
119 case SQL::typeDate: {
120 r = setDate((const SQL::Date*)_val, _size);
121 break;
122 }
123 case SQL::typeTime: {
124 r = setTime((const SQL::Time*)_val, _size);
125 break;
126 }
127 case SQL::typeTimeStamp: {
128 r = setTimeStamp((const SQL::TimeStamp*)_val, _size);
129 break;
130 }
131 case SQL::typeText:
132 case SQL::typeBinary: {
133 if (__SQLTYPE_IS(SQL_BLOB))
134 r = setBytesToBlob((const byte_t*)_val, _size);
135 else {
136 __sqlvar->sqldata = (char*)_val;
137 __sqlvar->sqllen = _size;
138 __sqlvar->sqltype = SQL_TEXT + 1;
139 r = true;
140 }
141 break;
142 }
144 if (__SQLTYPE_IS(SQL_BLOB))
145 r = setInputStreamToBlob((_CONST InputStream*)_val, _size);
146 else {
148 return false;
149 }
150 break;
151 }
152 // case SQL::typeInterval:
153 default: {
155 return false;
156 }
157 }
158
159 if (r) {
160 __indicator = 0;
161 Param::__dataType = _sqlType;
162 }
163
164 return r;
165}
166
167bool IBParam::setInteger(const void* _val, size_t _size)
168{
169 switch(_size) {
170 case sizeof(int16_t): {
171 __data.i16 = *(int16_t*)_val;
172 __sqlvar->sqllen = sizeof(int16_t);
173 __sqlvar->sqltype = SQL_SHORT + 1;
174 break;
175 }
176 case sizeof(int32_t): {
177 __data.i32 = *(int32_t*)_val;
178 __sqlvar->sqllen = sizeof(int32_t);
179 __sqlvar->sqltype = SQL_LONG + 1;
180 break;
181 }
182 case sizeof(int64_t): {
183 __data.i64 = *(int64_t*)_val;
184 __sqlvar->sqllen = sizeof(int64_t);
185 __sqlvar->sqltype = SQL_INT64 + 1;
186 break;
187 }
188 default: {
190 return false;
191 }
192 }
193 __sqlvar->sqldata = (char*)&__data;
194 return true;
195}
196
197bool IBParam::setUInteger(const void* _val, size_t _size)
198{
199 switch(_size) {
200 case sizeof(uint16_t): {
201 __data.i16 = (int16_t) * (uint16_t*)_val;
202 __sqlvar->sqllen = sizeof(int16_t);
203 __sqlvar->sqltype = SQL_SHORT + 1;
204 break;
205 }
206 case sizeof(uint32_t): {
207 __data.i32 = (int32_t) * (uint32_t*)_val;
208 __sqlvar->sqllen = sizeof(int32_t);
209 __sqlvar->sqltype = SQL_LONG + 1;
210 break;
211 }
212 case sizeof(uint64_t) : {
213 __data.i64 = (int64_t) * (uint64_t*)_val;
214 __sqlvar->sqllen = sizeof(int64_t);
215 __sqlvar->sqltype = SQL_INT64 + 1;
216 break;
217 }
218 default: {
220 return false;
221 }
222 }
223 __sqlvar->sqldata = (char*)&__data;
224 return true;
225}
226
227bool IBParam::setFloat(const void* _val, size_t _size)
228{
229 switch(_size) {
230 case sizeof(float) : {
231 __data.f32 = *(float*)_val;
232 __sqlvar->sqllen = sizeof(float);
233 __sqlvar->sqltype = SQL_FLOAT + 1;
234 break;
235 }
236 case sizeof(double) : {
237 __data.f64 = *(double*)_val;
238 __sqlvar->sqllen = sizeof(double);
239 __sqlvar->sqltype = SQL_DOUBLE + 1;
240 break;
241 }
242 default: {
244 return false;
245 }
246 }
247 __sqlvar->sqldata = (char*)&__data;
248 return true;
249}
250
251bool IBParam::setDate(const SQL::Date* _val, size_t _size)
252{
253 if (_size != sizeof(SQL::Date)) {
255 return false;
256 }
257
258 struct tm tm;
259 tm.tm_year = _val->year - 1900;
260 tm.tm_mon = _val->month - 1;
261 tm.tm_mday = _val->day;
262
263 isc_encode_sql_date(&tm, &__data.date);
264
265 __sqlvar->sqldata = (char*)&__data;
266 __sqlvar->sqllen = sizeof(ISC_DATE);
267 __sqlvar->sqltype = SQL_TYPE_DATE + 1;
268 return true;
269}
270
271bool IBParam::setTime(const SQL::Time* _val, size_t _size)
272{
273 if (_size != sizeof(SQL::Time)) {
275 return false;
276 }
277
278 struct tm tm;
279 tm.tm_hour = _val->hour;
280 tm.tm_min = _val->min;
281 tm.tm_sec = _val->sec;
282
283 isc_encode_sql_time(&tm, &__data.time);
284 __data.time += _val->frac / 100000;
285
286 __sqlvar->sqldata = (char*)&__data;
287 __sqlvar->sqllen = sizeof(ISC_TIME);
288 __sqlvar->sqltype = SQL_TYPE_TIME + 1;
289#if defined(FIREBIRD_IBASE_H) && FB_API_VER >= 40
290 if (_val->tzoff != INT16_MIN) {
291 __data.time_tz.time_zone = 0;
292 __data.time_tz.ext_offset = _val->tzoff;
293 __sqlvar->sqllen = sizeof(ISC_TIME_TZ_EX);
294 __sqlvar->sqltype = SQL_TIME_TZ_EX + 1;
295 }
296#endif
297 return true;
298}
299
300bool IBParam::setTimeStamp(const SQL::TimeStamp* _val, size_t _size)
301{
302 if (_size != sizeof(SQL::TimeStamp)) {
304 return false;
305 }
306
307 struct tm tm;
308 tm.tm_year = _val->year - 1900;
309 tm.tm_mon = _val->month - 1;
310 tm.tm_mday = _val->day;
311 tm.tm_hour = _val->hour;
312 tm.tm_min = _val->min;
313 tm.tm_sec = _val->sec;
314
315 isc_encode_timestamp(&tm, &__data.ts);
316 __data.ts.timestamp_time += _val->frac / 100000;
317
318 __sqlvar->sqldata = (char*)&__data;
319 __sqlvar->sqllen = sizeof(ISC_TIMESTAMP);
320 __sqlvar->sqltype = SQL_TIMESTAMP + 1;
321#if defined(FIREBIRD_IBASE_H) && FB_API_VER >= 40
322 if (_val->tzoff != INT16_MIN) {
323 __data.ts_tz.time_zone = 0;
324 __data.ts_tz.ext_offset = _val->tzoff;
325 __sqlvar->sqllen = sizeof(ISC_TIMESTAMP_TZ_EX);
326 __sqlvar->sqltype = SQL_TIMESTAMP_TZ_EX + 1;
327 }
328#endif
329 return true;
330}
331
332inline size_t __MIN(size_t x, size_t y)
333{
334 return x < y ? x : y;
335}
336
337bool IBParam::setBytesToBlob(const byte_t* _val, size_t _size)
338{
339 isc_blob_handle hBlob = NULL_HANDLE;
340 if(isc_create_blob2(
341 conn()->statusVector(),
342 conn()->dbHandlePtr(),
343 conn()->trHandlePtr(),
344 &hBlob,
345 &__data.blob_id,
346 0,
347 NULL
348 )) {
350 return false;
351 }
352
353 size_t nTotal = 0;
354 size_t nBytes = 0;
355 while(nTotal < _size) {
356 nBytes = __MIN(_size - nTotal, USHRT_MAX);
357 if (isc_put_segment(
358 conn()->statusVector(),
359 &hBlob,
360 nBytes,
361 (char*)_val
362 )) {
364 ISC_STATUS status2[ISC_STATUS_VECTOR_LENGTH];
365 isc_close_blob(status2, &hBlob);
366 return false;
367 }
368
369 nTotal += nBytes;
370 _val += nBytes;
371 }
372
373 if (isc_close_blob(conn()->statusVector(), &hBlob)) {
375 return false;
376 }
377
378 __sqlvar->sqldata = (char*)&__data.blob_id;
379 __sqlvar->sqllen = sizeof(ISC_QUAD);
380 __sqlvar->sqltype = SQL_BLOB + 1;
381 return true;
382}
383
385{
386 isc_blob_handle hBlob = NULL_HANDLE;
387 if(isc_create_blob2(
388 conn()->statusVector(),
389 conn()->dbHandlePtr(),
390 conn()->trHandlePtr(),
391 &hBlob,
392 &__data.blob_id,
393 0,
394 NULL
395 )) {
397 return false;
398 }
399
400 char* _pbuf = (char*)malloc(USHRT_MAX);
401 if (!_pbuf) {
403 ISC_STATUS status2[ISC_STATUS_VECTOR_LENGTH];
404 isc_close_blob(status2, &hBlob);
405 return false;
406 }
407
408 try {
409 size_t nTotal = 0;
410 size_t nBytes = 0;
411 for (; ; ) {
412 nBytes = __MIN(_size - nTotal, USHRT_MAX);
413 if (nBytes && (nBytes = _input->read(_pbuf, nBytes))) {
414 if (isc_put_segment(
415 conn()->statusVector(),
416 &hBlob,
417 nBytes,
418 _pbuf
419 )) {
421 free(_pbuf);
422 ISC_STATUS status2[ISC_STATUS_VECTOR_LENGTH];
423 isc_close_blob(status2, &hBlob);
424 return false;
425 }
426 nTotal += nBytes;
427 }
428 else {
429 // done
430 break;
431 }
432 }
433 }
434 catch (IOException* e) {
435 __SET_ERROR_MSG(UTF8Encoder::encode(e->toStringAll()));
436 e->destroy();
437 free(_pbuf);
438 ISC_STATUS status2[ISC_STATUS_VECTOR_LENGTH];
439 isc_close_blob(status2, &hBlob);
440 return false;
441 }
442
443 free(_pbuf);
444
445 if (isc_close_blob(conn()->statusVector(), &hBlob)) {
447 return false;
448 }
449
450 __sqlvar->sqldata = (char*)&__data.blob_id;
451 __sqlvar->sqllen = sizeof(ISC_QUAD);
452 __sqlvar->sqltype = SQL_BLOB + 1;
453 return true;
454}
455
457{
458// __DCL_ASSERT(__indicator == 0 && __SQLTYPE_IS(SQL_BLOB));
459 isc_blob_handle hBlob = NULL_HANDLE;
460
461 if(isc_open_blob2(
462 conn()->statusVector(),
463 conn()->dbHandlePtr(),
464 conn()->trHandlePtr(),
465 &hBlob,
466 &__data.blob_id,
467 0,
468 NULL
469 )) {
471 return false;
472 }
473
474 if (isc_cancel_blob(
475 conn()->statusVector(),
476 &hBlob
477 )) {
479 return false;
480 }
481
482 return true;
483}
484
485__DCL_END_NAMESPACE
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:312
wchar_t char_t
Definition Config.h:247
unsigned char byte_t
Definition Config.h:246
#define _CONST
Definition Config.h:325
#define INT16_MIN
Definition Config.h:284
#define ISC_STATUS_VECTOR_LENGTH
#define NULL_HANDLE
const wchar_t * __dataTypeName(const XSQLVAR *_sqlvar)
Definition IBField.cpp:240
size_t __MIN(size_t x, size_t y)
Definition IBParam.cpp:332
#define __SET_ERROR_MSG(_message)
const wchar_t * __dataTypeName(const ifx_sqlvar_t *_sqlvar)
Definition IFXField.cpp:304
#define __SQLTYPE_IS(_sqltype)
Definition IFXField.cpp:68
IOException *size_t r
Definition MediaInfo.cpp:82
#define __DCL_VERIFY(expr)
Definition Object.h:396
#define __DCL_ASSERT(expr)
Definition Object.h:394
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:245
#define __T(str)
Definition Object.h:60
#define __SET_ERROR(_errorCode)
Definition SQLCore.cpp:149
virtual void destroy()
Definition Exception.cpp:74
String toStringAll() const
Definition Exception.cpp:45
IBConnection * conn() const
Definition IBParam.h:72
bool cancelBlob()
Definition IBParam.cpp:456
virtual const wchar_t * serverDataTypeName() const
Definition IBParam.cpp:77
virtual void setNull()
Definition IBParam.cpp:82
void onAfterExecute()
Definition IBParam.cpp:67
bool setTimeStamp(const SQL::TimeStamp *_val, size_t _size)
Definition IBParam.cpp:300
bool init(SQL::Query *_query, XSQLVAR *_sqlvar)
Definition IBParam.cpp:50
bool setTime(const SQL::Time *_val, size_t _size)
Definition IBParam.cpp:271
virtual ~IBParam()
Definition IBParam.cpp:43
bool setInputStreamToBlob(_CONST InputStream *_input, size_t _size)
Definition IBParam.cpp:384
bool setUInteger(const void *_val, size_t _size)
Definition IBParam.cpp:197
bool setInteger(const void *_val, size_t _size)
Definition IBParam.cpp:167
bool setFloat(const void *_val, size_t _size)
Definition IBParam.cpp:227
bool setBytesToBlob(const byte_t *_val, size_t _size)
Definition IBParam.cpp:337
virtual bool __setData(_CONST void *_val, size_t _size, SQL::DataType _valType, SQL::DataType _sqlType)
Definition IBParam.cpp:93
bool setDate(const SQL::Date *_val, size_t _size)
Definition IBParam.cpp:251
DataType
Definition SQLCore.h:59
@ typeBinary
Definition SQLCore.h:74
@ typeTime
Definition SQLCore.h:66
@ typeUInteger
Definition SQLCore.h:62
@ typeUnknown
Definition SQLCore.h:60
@ typeTimeStamp
Definition SQLCore.h:68
@ typeInputStream
Definition SQLCore.h:81
@ typeDate
Definition SQLCore.h:65
@ typeText
Definition SQLCore.h:73
@ typeFloat
Definition SQLCore.h:63
@ typeInteger
Definition SQLCore.h:61
@ eOutOfMemory
Definition SQLCore.h:24
@ eNotSupportDataType
Definition SQLCore.h:48
@ eServerError
Definition SQLCore.h:21
@ eInvalidDataSize
Definition SQLCore.h:55
@ eInvalidDataType
Definition SQLCore.h:49
size_t __MIN(size_t x, size_t y)
Definition size_t.h:27
int16_t year
Definition SQLCore.h:96
uint8_t month
Definition SQLCore.h:97
uint8_t day
Definition SQLCore.h:98
uint8_t hour
Definition SQLCore.h:103
uint8_t sec
Definition SQLCore.h:105
uint32_t frac
Definition SQLCore.h:106
uint8_t min
Definition SQLCore.h:104
int16_t tzoff
Definition SQLCore.h:107
uint32_t frac
Definition SQLCore.h:118
int16_t tzoff
Definition SQLCore.h:119
uint8_t min
Definition SQLCore.h:116
uint8_t sec
Definition SQLCore.h:117
uint8_t hour
Definition SQLCore.h:115
uint8_t day
Definition SQLCore.h:114
int16_t year
Definition SQLCore.h:112
uint8_t month
Definition SQLCore.h:113