DCL 4.0
Loading...
Searching...
No Matches
IFXParam.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#include <sqlhdr.h>
4#include <sqltypes.h>
5#include <locator.h>
6#include <sqlda.h>
7
8#ifndef _IFXTYPES_H_
9// locator.h에서 ifxtypes.h가 인크루드 되어 있으면 이 문장이 필요 없다.
10// Informix Client-SDK 버전에 따라 다르다.
11typedef int mint;
12#endif
13
14#include <dcl/Object.h>
15#if __DCL_HAVE_ALLOC_DEBUG
16#undef __DCL_ALLOC_LEVEL
17#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
18#endif
19
20#include <dcl/InputStream.h>
21#include <dcl/SQLCore.h>
22
23#include "IFXConnection.h"
24#include "IFXQuery.h"
25#include "IFXParam.h"
26#include "IFXTypes_.h"
27
28#define __TRACE_THIS 0
29#if __TRACE_THIS
30#define __DCL_TRACE0_N __DCL_TRACE0
31#define __DCL_TRACE1_N __DCL_TRACE1
32#define __DCL_TRACE2_N __DCL_TRACE2
33#define __DCL_TRACE3_N __DCL_TRACE3
34#define __DCL_TRACE4_N __DCL_TRACE4
35#else
36#define __DCL_TRACE0_N(fmt)
37#define __DCL_TRACE1_N(fmt, arg)
38#define __DCL_TRACE2_N(fmt, arg1, arg2)
39#define __DCL_TRACE3_N(fmt, arg1, arg2, arg3)
40#define __DCL_TRACE4_N(fmt, arg1, arg2, arg3, arg4)
41#endif
42
43#undef __THIS_FILE__
44static const wchar_t __THIS_FILE__[] = __T("dcl/sql/IFXParam.cpp");
45
46__DCL_BEGIN_NAMESPACE
47
49
50IFXParam::IFXParam() : Param(NULL)
51{
52 __sqlvar = NULL;
53}
54
58
59bool IFXParam::init(SQL::Query* _queryHandle, ifx_sqlvar_t* _sqlvar)
60{
61 __DCL_ASSERT((Param::__queryHandle == NULL) && (__sqlvar == NULL));
62
63 Param::__queryHandle = _queryHandle;
64
65 __sqltype = _sqlvar->sqltype;
66 __sqllen = _sqlvar->sqllen;
67 __indicator = -1; // set null
68 __sqlvar = _sqlvar;
69 __sqlvar->sqldata = NULL;
70 __sqlvar->sqlind = &__indicator;
71
72 return true;
73}
74
76{
77 bool r = true;
78 if (__sqlvar->sqltype == CLOCATORTYPE && __sqlvar->sqldata) {
79 loc_t* loc = (loc_t*)(__sqlvar->sqldata);
80 if (loc->loc_status != 0) {
81 __SET_ERROR_SQLCODE(loc->loc_status);
82 r = false;;
83 }
84 }
85
86 Param::__dataType = SQL::typeUnknown;
87 __indicator = -1; // set null
88 __sqlvar->sqldata = NULL;
89
90 return r;
91}
92
93// in IFXField.cpp
94const wchar_t* __dataTypeName(const ifx_sqlvar_t* _sqlvar);
95
96const wchar_t* IFXParam::serverDataTypeName() const
97{
98 return __dataTypeName(__sqlvar);
99}
100
101
103{
104 Param::__dataType = SQL::typeUnknown;
105 __indicator = -1; // set null
106 __sqlvar->sqldata = NULL;
107}
108
110 _CONST void* _pv,
111 size_t _size,
112 SQL::DataType _dataType,
113 SQL::DataType _assignType
114 )
115{
116 bool r = true;
117 switch(_dataType) {
118 case SQL::typeInteger : {
119 r = setInteger(_pv, _size);
120 break;
121 }
122 case SQL::typeUInteger: {
123 r = setUInteger(_pv, _size);
124 break;
125 }
126 case SQL::typeFloat: {
127 if (_size == sizeof(float)) {
128 __data.f32 = *(float*)_pv;
129 __sqlvar->sqldata = (char*)&__data;
130 __sqlvar->sqltype = CFLOATTYPE;
131 __sqlvar->sqllen = sizeof(float);
132 }
133 else if (_size == sizeof(double)) {
134 __data.f64 = *(double*)_pv;
135 __sqlvar->sqldata = (char*)&__data;
136 __sqlvar->sqltype = CDOUBLETYPE;
137 __sqlvar->sqllen = sizeof(double);
138 }
139 else {
141 return false;
142 }
143 break;
144 }
145 case SQL::typeDate: {
146 if (_size == sizeof(SQL::Date)) {
147 const SQL::Date* p = (const SQL::Date*)_pv;
148 short mdy[3];
149 mdy[0] = p->nMonth;
150 mdy[1] = p->nDay;
151 mdy[2] = p->nYear;
152
153 int r = rmdyjul(mdy, &__data.date);
154 if (r) {
156 return false;
157 }
158 __sqlvar->sqldata = (char*)&__data;
159 __sqlvar->sqltype = CDATETYPE;
160 __sqlvar->sqllen = sizeof(long);
161 }
162 else {
164 return false;
165 }
166 break;
167 }
168 case SQL::typeTime: {
169 if (_size == sizeof(SQL::Time)) {
170 const SQL::Time* p = (const SQL::Time*)_pv;
171 int r = __encode_dtime(p, &__data.dtime);
172 if (r) {
174 return false;
175 }
176
177 __sqlvar->sqldata = (char*)&__data;
178 __sqlvar->sqltype = CDTIMETYPE;
179 __sqlvar->sqllen = sizeof(dtime_t);
180 }
181 else {
183 return false;
184 }
185 break;
186 }
187 case SQL::typeTimeStamp: {
188 if (_size == sizeof(SQL::TimeStamp)) {
189 const SQL::TimeStamp* p = (const SQL::TimeStamp*)_pv;
190 if (p->nTzMin) {
192 return false;
193 }
194
195 int r = __encode_dtime(p, &__data.dtime);
196 if (r) {
198 return false;
199 }
200
201 __sqlvar->sqldata = (char*)&__data;
202 __sqlvar->sqltype = CDTIMETYPE;
203 __sqlvar->sqllen = sizeof(dtime_t);
204 }
205 else {
207 return false;
208 }
209 break;
210 }
213 case SQL::typeIntervalDs: {
214 if (_size == sizeof(SQL::Interval)) {
215 int r = __encode_intrvl(
216 (const SQL::Interval*)_pv,
217 _dataType,
218 &__data.ival
219 );
220 if (r) {
222 return false;
223 }
224
225 __sqlvar->sqldata = (char*)&__data;
226 __sqlvar->sqltype = CINVTYPE;
227 __sqlvar->sqllen = sizeof(intrvl_t);
228 }
229 else {
231 return false;
232 }
233 break;
234 }
235 case SQL::typeText : {
236 if (_assignType == SQL::typeNumeric) {
237 if (_size > 0) {
238 int r = deccvasc((char*)_pv, _size, &__data.dec);
239 if (r) {
241 return false;
242 }
243 __sqlvar->sqldata = (char*)&__data;
244 __sqlvar->sqltype = CDECIMALTYPE;
245 __sqlvar->sqllen = sizeof(dec_t);
246 }
247 else {
249 return false;
250 }
251 break;
252 }
253 }
254 case SQL::typeBinary: {
255 r = setBytes(_pv, _size, _assignType);
256 break;
257 }
259 r = setInputStream(_pv, _size, _assignType);
260 break;
261 }
262 default: {
264 return false;
265 }
266 }
267
268 if (r) {
269 __indicator = 0;
270 Param::__dataType = _assignType;
271 }
272
273 return true;
274}
275
276bool IFXParam::setInteger(const void* _pv, size_t _size)
277{
278 switch (_size) {
279 case sizeof(int8_t) : {
280 __data.i32 = (int32_t) * (int8_t*)_pv;
281 __sqlvar->sqltype = CLONGTYPE;
282 __sqlvar->sqllen = sizeof(int32_t);
283 break;
284 }
285 case sizeof(int16_t) : {
286 __data.i32 = (int32_t) * (int16_t*)_pv;
287 __sqlvar->sqltype = CLONGTYPE;
288 __sqlvar->sqllen = sizeof(int32_t);
289 break;
290 }
291 case sizeof(int32_t) : {
292 __data.i32 = *(int32_t*)_pv;
293 __sqlvar->sqltype = CLONGTYPE;
294 __sqlvar->sqllen = sizeof(int32_t);
295 break;
296 }
297 case sizeof(int64_t) : {
298 __data.i64 = *(int64_t*)_pv;
299 __sqlvar->sqltype = CBIGINTTYPE;
300 __sqlvar->sqllen = sizeof(bigint);
301 break;
302 }
303 default: {
305 return false;
306 }
307 }
308 __sqlvar->sqldata = (char*)&__data;
309 return true;
310}
311
312bool IFXParam::setUInteger(const void* _pv, size_t _size)
313{
314 switch (__sqltype & SQLTYPE) {
315 case SQLSMINT:
316 case SQLINT:
317 case SQLSERIAL: {
318 switch (_size) {
319 case sizeof(uint8_t) : {
320 __data.i32 = (int32_t) * (uint8_t*)_pv;
321 __sqlvar->sqltype = CLONGTYPE;
322 __sqlvar->sqllen = sizeof(int32_t);
323 break;
324 }
325 case sizeof(uint16_t) : {
326 __data.i32 = (int32_t) * (uint16_t*)_pv;
327 __sqlvar->sqltype = CLONGTYPE;
328 __sqlvar->sqllen = sizeof(int32_t);
329 break;
330 }
331 case sizeof(uint32_t) : {
332 __data.i32 = (int32_t) * (uint32_t*)_pv;
333 __sqlvar->sqltype = CLONGTYPE;
334 __sqlvar->sqllen = sizeof(int32_t);
335 break;
336 }
337 case sizeof(uint64_t) : {
338 __data.i64 = (int64_t) * (uint32_t*)_pv;
339 __sqlvar->sqltype = CBIGINTTYPE;
340 __sqlvar->sqllen = sizeof(bigint);
341 break;
342 }
343 default: {
345 return false;
346 }
347 }
348 break;
349 }
350 case SQLINT8:
351 case SQLSERIAL8:
352 case SQLINFXBIGINT:
353 case SQLBIGSERIAL: {
354 switch (_size) {
355 case sizeof(uint8_t) : {
356 __data.i64 = (int64_t) * (uint8_t*)_pv;
357 __sqlvar->sqltype = CBIGINTTYPE;
358 __sqlvar->sqllen = sizeof(int32_t);
359 break;
360 }
361 case sizeof(uint16_t) : {
362 __data.i64 = (int64_t) * (uint16_t*)_pv;
363 __sqlvar->sqltype = CBIGINTTYPE;
364 __sqlvar->sqllen = sizeof(int32_t);
365 break;
366 }
367 case sizeof(uint32_t) : {
368 __data.i64 = (int64_t) * (uint32_t*)_pv;
369 __sqlvar->sqltype = CBIGINTTYPE;
370 __sqlvar->sqllen = sizeof(int32_t);
371 break;
372 }
373 case sizeof(uint64_t) : {
374 __data.i64 = (int64_t) * (uint32_t*)_pv;
375 __sqlvar->sqltype = CBIGINTTYPE;
376 __sqlvar->sqllen = sizeof(bigint);
377 break;
378 }
379 default: {
381 return false;
382 }
383 }
384 break;
385 }
386 default: {
388 return false;
389 }
390 }
391
392 __sqlvar->sqldata = (char*)&__data;
393 return true;
394}
395
396bool IFXParam::setBytes(const void* _pv, size_t _size,
397 SQL::DataType _assignType)
398{
399 switch (__sqltype & SQLTYPE) {
400 case SQLTEXT:
401 case SQLBYTES: {
402 if (_size > INT32_MAX) {
404 return false;
405 }
406 switch (_assignType) {
407 case SQL::typeText:
409 case SQL::typeClob: {
410 __data.loc.loc_type = SQLTEXT;
411 break;
412 }
413 case SQL::typeBinary:
415 case SQL::typeBlob: {
416 __data.loc.loc_type = SQLBYTES;
417 break;
418 }
419 default: {
421 return false;
422 }
423 }
424 __data.loc.loc_loctype = LOCMEMORY;
425 __data.loc.loc_indicator = 0;
426 // __data.loc.loc_type = SQLTEXT or SQLBYTES;
427 __data.loc.loc_mflags = 0;
428 __data.loc.loc_oflags = LOC_WONLY;
429
430 __data.loc.loc_buffer = (char*)_pv;
431 __data.loc.loc_bufsize = _size;
432 __data.loc.loc_size = _size;
433
434 __sqlvar->sqltype = CLOCATORTYPE;
435 __sqlvar->sqllen = sizeof(loc_t);
436 __sqlvar->sqldata = (char*)&__data.loc;
437 break;
438 }
439 default: {
440 switch (_assignType) {
441 case SQL::typeText:
443 case SQL::typeClob: {
444 __sqlvar->sqltype = CFIXCHARTYPE;
445 break;
446 }
447 case SQL::typeBinary:
449 case SQL::typeBlob: {
450 __sqlvar->sqltype = CFIXBINTYPE;
451 break;
452 }
453 default: {
455 return false;
456 }
457 }
458 __sqlvar->sqldata = (char*)_pv;
459 __sqlvar->sqllen = _size;
460 }
461 }
462 return true;
463}
464
465static mint on_loc_open(loc_t* loc, mint flag, mint bsize);
466static mint on_loc_read(loc_t* loc, char* buffer, mint buflen);
467static mint on_loc_close(loc_t* loc);
468
469bool IFXParam::setInputStream(const void* _pv, size_t _size,
470 SQL::DataType _assignType)
471{
472 if (_size != (size_t)-1 && _size > INT32_MAX) {
474 return false;
475 }
476
477 switch (_assignType) {
478 case SQL::typeText:
480 case SQL::typeClob: {
481 __data.loc.loc_type = SQLTEXT;
482 break;
483 }
484 case SQL::typeBinary:
486 case SQL::typeBlob: {
487 __data.loc.loc_type = SQLBYTES;
488 break;
489 }
490 default: {
492 return false;
493 }
494 }
495
496 __data.loc.loc_open = on_loc_open;
497 __data.loc.loc_read = on_loc_read;
498 __data.loc.loc_close = on_loc_close;
499 __data.loc.loc_write = NULL;
500
501 __data.loc.loc_loctype = LOCUSER;
502 __data.loc.loc_indicator = 0;
503 // __data.loc.loc_type = SQLTEXT or SQLBYTES;
504 __data.loc.loc_mflags = 0;
505 __data.loc.loc_oflags = LOC_WONLY;
506
507 __data.loc.loc_user_env = (char*)_pv;
508 __data.loc.loc_size = _size;
509
510 __sqlvar->sqldata = (char*)&__data;
511 __sqlvar->sqltype = CLOCATORTYPE;
512 __sqlvar->sqllen = sizeof(loc_t);
513
514 return true;
515}
516
517// MSVCRTD.DLL을 사용하는 윈도우용 DEBUG 버전은 LOCFILE을 사용할 경우
518// 실패한다. DEBUG 버전에서는 LOCUSER로 대체한다.
519static mint on_loc_open(loc_t* loc, mint flag, mint bsize)
520{
521 loc->loc_status = 0;
522 loc->loc_xfercount = 0L;
523
524 /*
525 if error {
526 loc->loc_status = -452;
527 return -1;
528 }
529 */
530 return 0;
531}
532
533inline mint __MIN(mint x, mint y)
534{
535 return x < y ? x : y;
536}
537
538static mint on_loc_read(loc_t* loc, char* buffer, mint buflen)
539{
540 mint nToRead = buflen;
541 if (loc->loc_size != -1)
542 nToRead = __MIN(buflen, loc->loc_size - loc->loc_xfercount);
543
544 InputStream* pInput = (InputStream*)(loc->loc_user_env);
545 size_t nRead = 0;
546 try {
547 nRead = pInput->read(buffer, (size_t)nToRead);
548 }
549 catch (IOException* e) {
550 e->destroy();
551 loc->loc_status = -454;
552 return -1;
553 }
554 loc->loc_xfercount += nRead;
555 return nRead;
556}
557
558static mint on_loc_close(loc_t* loc)
559{
560 loc->loc_status = 0;
561 return 0;
562}
563
564__DCL_END_NAMESPACE
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:340
#define INT32_MAX
Definition Config.h:318
#define _CONST
Definition Config.h:353
#define __SET_ERROR_SQLCODE(SQLCODE)
const wchar_t * __dataTypeName(const ifx_sqlvar_t *_sqlvar)
Definition IFXField.cpp:290
const wchar_t * __dataTypeName(const ifx_sqlvar_t *_sqlvar)
Definition IFXField.cpp:290
int mint
Definition IFXParam.cpp:11
mint __MIN(mint x, mint y)
Definition IFXParam.cpp:533
int __encode_dtime(const SQL::TimeStamp *_s, dtime_t *_r)
Definition IFXTypes.cpp:336
int __encode_intrvl(const SQL::Interval *_s, SQL::DataType _dataType, intrvl_t *_r)
Definition IFXTypes.cpp:399
#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
loc_t loc
Definition IFXParam.h:23
bool init(SQL::Query *_query, ifx_sqlvar_t *_sqlvar)
Definition IFXParam.cpp:59
bool setBytes(const void *_pv, size_t _size, SQL::DataType _assignType)
Definition IFXParam.cpp:396
bool onAfterExecute()
Definition IFXParam.cpp:75
bool setInteger(const void *_pv, size_t _size)
Definition IFXParam.cpp:276
virtual bool __setData(_CONST void *_pv, size_t _size, SQL::DataType _dataType, SQL::DataType _assignType)
Definition IFXParam.cpp:109
virtual ~IFXParam()
Definition IFXParam.cpp:55
virtual const wchar_t * serverDataTypeName() const
Definition IFXParam.cpp:96
bool setUInteger(const void *_pv, size_t _size)
Definition IFXParam.cpp:312
bool setInputStream(const void *_pv, size_t _size, SQL::DataType _assignType)
Definition IFXParam.cpp:469
virtual void setNull()
Definition IFXParam.cpp:102
DataType
Definition SQLCore.h:62
@ typeBinary
Definition SQLCore.h:76
@ typeClob
Definition SQLCore.h:79
@ typeNumeric
Definition SQLCore.h:67
@ 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
@ typeBlob
Definition SQLCore.h:80
@ typeInputStream
Definition SQLCore.h:83
@ typeInterval
Definition SQLCore.h:72
@ typeIntervalDs
Definition SQLCore.h:74
@ typeDate
Definition SQLCore.h:68
@ typeText
Definition SQLCore.h:75
@ typeFloat
Definition SQLCore.h:66
@ typeInteger
Definition SQLCore.h:64
@ typeIntervalYm
Definition SQLCore.h:73
@ typeLongText
Definition SQLCore.h:77
@ eInvalidData
Definition SQLCore.h:55
@ eNotSupportDataType
Definition SQLCore.h:48
@ eInvalidDataSize
Definition SQLCore.h:57
@ eInvalidDataType
Definition SQLCore.h:49
uint8_t nMonth
Definition SQLCore.h:97
int16_t nYear
Definition SQLCore.h:96
uint8_t nDay
Definition SQLCore.h:98
int16_t nTzMin
Definition SQLCore.h:118