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