DCL 4.0
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* _pv,
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(_pv, _size);
104 break;
105 }
106 case SQL::typeUInteger: {
107 r = setUInteger(_pv, _size);
108 break;
109 }
110 case SQL::typeFloat: {
111 r = setFloat(_pv, _size);
112 break;
113 }
114 case SQL::typeDate: {
115 r = setDate((const SQL::Date*)_pv, _size);
116 break;
117 }
118 case SQL::typeTime: {
119 r = setTime((const SQL::Time*)_pv, _size);
120 break;
121 }
122 case SQL::typeTimeStamp: {
123 r = setTimeStamp((const SQL::TimeStamp*)_pv, _size);
124 break;
125 }
126 case SQL::typeText :
127 case SQL::typeBinary :
128 case SQL::typeLongText :
129 case SQL::typeLongBinary: {
130 if (__SQLTYPE_IS(SQL_BLOB))
131 r = setBytesToBlob((const byte_t*)_pv, _size);
132 else {
133 __sqlvar->sqldata = (char*)_pv;
134 __sqlvar->sqllen = _size;
135 __sqlvar->sqltype = SQL_TEXT + 1;
136 r = true;
137 }
138 break;
139 }
141 if (__SQLTYPE_IS(SQL_BLOB))
143 else {
145 return false;
146 }
147 break;
148 }
149 // case SQL::typeTimeStampTz :
150 // case SQL::typeInterva
151 // case SQL::typeIntervalYm :
152 // case SQL::typeIntervalDs :
153 default: {
155 return false;
156 }
157 }
158
159 if (r) {
160 __indicator = 0;
161 Param::__dataType = _dataType;
162 }
163
164 return r;
165}
166
167bool IBParam::setInteger(const void* _pv, size_t _size)
168{
169 switch(_size) {
170 case sizeof(int16_t): {
171 __data.i16 = *(int16_t*)_pv;
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*)_pv;
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*)_pv;
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* _pv, size_t _size)
198{
199 switch(_size) {
200 case sizeof(uint16_t): {
201 __data.i16 = (int16_t) * (uint16_t*)_pv;
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*)_pv;
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*)_pv;
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* _pv, size_t _size)
228{
229 switch(_size) {
230 case sizeof(float) : {
231 __data.f32 = *(float*)_pv;
232 __sqlvar->sqllen = sizeof(float);
233 __sqlvar->sqltype = SQL_FLOAT + 1;
234 break;
235 }
236 case sizeof(double) : {
237 __data.f64 = *(double*)_pv;
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* p, size_t _size)
252{
253 if (_size != sizeof(SQL::Date)) {
255 return false;
256 }
257
258 struct tm tm;
259 tm.tm_year = p->nYear - 1900;
260 tm.tm_mon = p->nMonth - 1;
261 tm.tm_mday = p->nDay;
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* p, size_t _size)
272{
273 if (_size != sizeof(SQL::Time)) {
275 return false;
276 }
277
278 struct tm tm;
279 tm.tm_hour = p->nHour;
280 tm.tm_min = p->nMin;
281 tm.tm_sec = p->nSec;
282
283 isc_encode_sql_time(&tm, &__data.time);
284 __data.time += p->nFrac / 100000;
285
286 __sqlvar->sqldata = (char*)&__data;
287 __sqlvar->sqllen = sizeof(ISC_TIME);
288 __sqlvar->sqltype = SQL_TYPE_TIME + 1;
289 return true;
290}
291
292bool IBParam::setTimeStamp(const SQL::TimeStamp* p, size_t _size)
293{
294 if (_size != sizeof(SQL::TimeStamp)) {
296 return false;
297 }
298
299 struct tm tm;
300 tm.tm_year = p->nYear - 1900;
301 tm.tm_mon = p->nMonth - 1;
302 tm.tm_mday = p->nDay;
303 tm.tm_hour = p->nHour;
304 tm.tm_min = p->nMin;
305 tm.tm_sec = p->nSec;
306
307 isc_encode_timestamp(&tm, &__data.ts);
308 __data.ts.timestamp_time += p->nFrac / 100000;
309
310 __sqlvar->sqldata = (char*)&__data;
311 __sqlvar->sqllen = sizeof(ISC_TIMESTAMP);
312 __sqlvar->sqltype = SQL_TIMESTAMP + 1;
313 return true;
314}
315
316inline size_t __MIN(size_t x, size_t y)
317{
318 return x < y ? x : y;
319}
320
321bool IBParam::setBytesToBlob(const byte_t* p, size_t _size)
322{
323 IBConnection* connHandle = (IBConnection*)Param::connection();
324 ISC_STATUS* statusVector = connHandle->statusVector();
325 isc_blob_handle hBlob = NULL;
326
327 if(isc_create_blob2(
328 statusVector,
329 connHandle->dbHandlePtr(),
330 connHandle->trHandlePtr(),
331 &hBlob,
332 &__data.blob_id,
333 0,
334 NULL)) {
336 return false;
337 }
338
339 size_t nTotal = 0;
340 size_t nBytes = 0;
341 while(nTotal < _size) {
342 nBytes = __MIN(_size - nTotal, USHRT_MAX);
343 if (isc_put_segment(
344 statusVector,
345 &hBlob,
346 nBytes,
347 (char*)p
348 )) {
350 ISC_STATUS statusVector[ISC_STATUS_VECTOR_LENGTH];
351 isc_close_blob(statusVector, &hBlob);
352 return false;
353 }
354
355 nTotal += nBytes;
356 p += nBytes;
357 }
358
359 if (isc_close_blob(statusVector, &hBlob)) {
361 return false;
362 }
363
364 __sqlvar->sqldata = (char*)&__data.blob_id;
365 __sqlvar->sqllen = sizeof(ISC_QUAD);
366 __sqlvar->sqltype = SQL_BLOB + 1;
367 return true;
368}
369
371 _CONST InputStream* pInput, size_t _size)
372{
373 IBConnection* connHandle = (IBConnection*)Param::connection();
374 ISC_STATUS* statusVector = connHandle->statusVector();
375 isc_blob_handle hBlob = NULL;
376
377 if(isc_create_blob2(
378 statusVector,
379 connHandle->dbHandlePtr(),
380 connHandle->trHandlePtr(),
381 &hBlob,
382 &__data.blob_id,
383 0,
384 NULL)) {
386 return false;
387 }
388
389 char* _pbuf = (char*)malloc(USHRT_MAX);
390 if (!_pbuf) {
392 ISC_STATUS statusVector[ISC_STATUS_VECTOR_LENGTH];
393 isc_close_blob(statusVector, &hBlob);
394 return false;
395 }
396
397 try {
398 size_t nTotal = 0;
399 size_t nBytes = 0;
400 for (; ; ) {
401 nBytes = __MIN(_size - nTotal, USHRT_MAX);
402 if (nBytes && (nBytes = pInput->read(_pbuf, nBytes))) {
403 if (isc_put_segment(
404 statusVector,
405 &hBlob,
406 nBytes,
407 _pbuf
408 )) {
410 free(_pbuf);
411 ISC_STATUS statusVector[ISC_STATUS_VECTOR_LENGTH];
412 isc_close_blob(statusVector, &hBlob);
413 return false;
414 }
415
416 nTotal += nBytes;
417 }
418 else {
419 // done
420 break;
421 }
422 }
423 }
424 catch (IOException* e) {
425 __SET_ERROR_MSG(UTF8Encoder::encode(e->toStringAll()));
426 e->destroy();
427 free(_pbuf);
428 ISC_STATUS statusVector[ISC_STATUS_VECTOR_LENGTH];
429 isc_close_blob(statusVector, &hBlob);
430 return false;
431 }
432
433 free(_pbuf);
434
435 if (isc_close_blob(statusVector, &hBlob)) {
437 return false;
438 }
439
440 __sqlvar->sqldata = (char*)&__data.blob_id;
441 __sqlvar->sqllen = sizeof(ISC_QUAD);
442 __sqlvar->sqltype = SQL_BLOB + 1;
443 return true;
444}
445
447{
448// __DCL_ASSERT(__indicator == 0 && __SQLTYPE_IS(SQL_BLOB));
449
450 IBConnection* connHandle = (IBConnection*)Param::connection();
451 ISC_STATUS* statusVector = connHandle->statusVector();
452 isc_blob_handle hBlob = NULL;
453
454 if(isc_open_blob2(
455 statusVector,
456 connHandle->dbHandlePtr(),
457 connHandle->trHandlePtr(),
458 &hBlob,
459 &__data.blob_id,
460 0,
461 NULL)) {
463 return false;
464 }
465
466 if (isc_cancel_blob(
467 statusVector,
468 &hBlob
469 )) {
471 return false;
472 }
473
474 return true;
475}
476
477__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 ISC_STATUS_VECTOR_LENGTH
Definition IBConnection.h:6
const wchar_t * __dataTypeName(const XSQLVAR *_sqlvar)
Definition IBField.cpp:199
size_t __MIN(size_t x, size_t y)
Definition IBParam.cpp:316
#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:150
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:446
bool setBytesToBlob(const byte_t *p, size_t _size)
Definition IBParam.cpp:321
virtual const wchar_t * serverDataTypeName() const
Definition IBParam.cpp:72
virtual void setNull()
Definition IBParam.cpp:77
void onAfterExecute()
Definition IBParam.cpp:62
bool setTimeStamp(const SQL::TimeStamp *p, size_t _size)
Definition IBParam.cpp:292
bool init(SQL::Query *_query, XSQLVAR *_sqlvar)
Definition IBParam.cpp:45
bool setFloat(const void *_pv, size_t _size)
Definition IBParam.cpp:227
bool setUInteger(const void *_pv, size_t _size)
Definition IBParam.cpp:197
bool setDate(const SQL::Date *p, size_t _size)
Definition IBParam.cpp:251
virtual ~IBParam()
Definition IBParam.cpp:38
bool setTime(const SQL::Time *p, size_t _size)
Definition IBParam.cpp:271
bool setInputStreamToBlob(_CONST InputStream *pInput, size_t _size)
Definition IBParam.cpp:370
bool setInteger(const void *_pv, size_t _size)
Definition IBParam.cpp:167
virtual bool __setData(_CONST void *_pv, size_t _size, SQL::DataType _dataType, SQL::DataType _assignType)
Definition IBParam.cpp:88
DataType
Definition SQLCore.h:62
@ typeBinary
Definition SQLCore.h:76
@ typeTime
Definition SQLCore.h:69
@ typeLongBinary
Definition SQLCore.h:78
@ typeUInteger
Definition SQLCore.h:65
@ typeUnknown
Definition SQLCore.h:63
@ typeTimeStamp
Definition SQLCore.h:70
@ typeInputStream
Definition SQLCore.h:83
@ typeDate
Definition SQLCore.h:68
@ typeText
Definition SQLCore.h:75
@ typeFloat
Definition SQLCore.h:66
@ typeInteger
Definition SQLCore.h:64
@ typeLongText
Definition SQLCore.h:77
@ 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
uint8_t nMonth
Definition SQLCore.h:97
int16_t nYear
Definition SQLCore.h:96
uint8_t nDay
Definition SQLCore.h:98
uint8_t nHour
Definition SQLCore.h:103
uint8_t nMin
Definition SQLCore.h:104
uint8_t nSec
Definition SQLCore.h:105
uint32_t nFrac
Definition SQLCore.h:106
int16_t nYear
Definition SQLCore.h:111
uint8_t nDay
Definition SQLCore.h:113
uint8_t nHour
Definition SQLCore.h:114
uint8_t nSec
Definition SQLCore.h:116
uint8_t nMonth
Definition SQLCore.h:112
uint8_t nMin
Definition SQLCore.h:115
uint32_t nFrac
Definition SQLCore.h:117