DCL 4.1
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->month;
150 mdy[1] = p->day;
151 mdy[2] = p->year;
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 int r = __encode_dtime(p, &__data.dtime);
191 if (r) {
193 return false;
194 }
195
196 __sqlvar->sqldata = (char*)&__data;
197 __sqlvar->sqltype = CDTIMETYPE;
198 __sqlvar->sqllen = sizeof(dtime_t);
199 }
200 else {
202 return false;
203 }
204 break;
205 }
206 case SQL::typeInterval: {
207 if (_size == sizeof(SQL::Interval)) {
208 int r = __encode_intrvl(
209 (const SQL::Interval*)_pv,
210 _dataType,
211 &__data.ival
212 );
213 if (r) {
215 return false;
216 }
217
218 __sqlvar->sqldata = (char*)&__data;
219 __sqlvar->sqltype = CINVTYPE;
220 __sqlvar->sqllen = sizeof(intrvl_t);
221 }
222 else {
224 return false;
225 }
226 break;
227 }
228 case SQL::typeText : {
229 if (_assignType == SQL::typeNumeric) {
230 if (_size > 0) {
231 int r = deccvasc((char*)_pv, _size, &__data.dec);
232 if (r) {
234 return false;
235 }
236 __sqlvar->sqldata = (char*)&__data;
237 __sqlvar->sqltype = CDECIMALTYPE;
238 __sqlvar->sqllen = sizeof(dec_t);
239 }
240 else {
242 return false;
243 }
244 break;
245 }
246 }
247 case SQL::typeBinary: {
248 r = setBytes(_pv, _size, _assignType);
249 break;
250 }
252 r = setInputStream(_pv, _size, _assignType);
253 break;
254 }
255 default: {
257 return false;
258 }
259 }
260
261 if (r) {
262 __indicator = 0;
263 Param::__dataType = _assignType;
264 }
265
266 return true;
267}
268
269bool IFXParam::setInteger(const void* _pv, size_t _size)
270{
271 switch (_size) {
272 case sizeof(int8_t) : {
273 __data.i32 = (int32_t) * (int8_t*)_pv;
274 __sqlvar->sqltype = CLONGTYPE;
275 __sqlvar->sqllen = sizeof(int32_t);
276 break;
277 }
278 case sizeof(int16_t) : {
279 __data.i32 = (int32_t) * (int16_t*)_pv;
280 __sqlvar->sqltype = CLONGTYPE;
281 __sqlvar->sqllen = sizeof(int32_t);
282 break;
283 }
284 case sizeof(int32_t) : {
285 __data.i32 = *(int32_t*)_pv;
286 __sqlvar->sqltype = CLONGTYPE;
287 __sqlvar->sqllen = sizeof(int32_t);
288 break;
289 }
290 case sizeof(int64_t) : {
291 __data.i64 = *(int64_t*)_pv;
292 __sqlvar->sqltype = CBIGINTTYPE;
293 __sqlvar->sqllen = sizeof(bigint);
294 break;
295 }
296 default: {
298 return false;
299 }
300 }
301 __sqlvar->sqldata = (char*)&__data;
302 return true;
303}
304
305bool IFXParam::setUInteger(const void* _pv, size_t _size)
306{
307 switch (__sqltype & SQLTYPE) {
308 case SQLSMINT:
309 case SQLINT:
310 case SQLSERIAL: {
311 switch (_size) {
312 case sizeof(uint8_t) : {
313 __data.i32 = (int32_t) * (uint8_t*)_pv;
314 __sqlvar->sqltype = CLONGTYPE;
315 __sqlvar->sqllen = sizeof(int32_t);
316 break;
317 }
318 case sizeof(uint16_t) : {
319 __data.i32 = (int32_t) * (uint16_t*)_pv;
320 __sqlvar->sqltype = CLONGTYPE;
321 __sqlvar->sqllen = sizeof(int32_t);
322 break;
323 }
324 case sizeof(uint32_t) : {
325 __data.i32 = (int32_t) * (uint32_t*)_pv;
326 __sqlvar->sqltype = CLONGTYPE;
327 __sqlvar->sqllen = sizeof(int32_t);
328 break;
329 }
330 case sizeof(uint64_t) : {
331 __data.i64 = (int64_t) * (uint32_t*)_pv;
332 __sqlvar->sqltype = CBIGINTTYPE;
333 __sqlvar->sqllen = sizeof(bigint);
334 break;
335 }
336 default: {
338 return false;
339 }
340 }
341 break;
342 }
343 case SQLINT8:
344 case SQLSERIAL8:
345 case SQLINFXBIGINT:
346 case SQLBIGSERIAL: {
347 switch (_size) {
348 case sizeof(uint8_t) : {
349 __data.i64 = (int64_t) * (uint8_t*)_pv;
350 __sqlvar->sqltype = CBIGINTTYPE;
351 __sqlvar->sqllen = sizeof(int32_t);
352 break;
353 }
354 case sizeof(uint16_t) : {
355 __data.i64 = (int64_t) * (uint16_t*)_pv;
356 __sqlvar->sqltype = CBIGINTTYPE;
357 __sqlvar->sqllen = sizeof(int32_t);
358 break;
359 }
360 case sizeof(uint32_t) : {
361 __data.i64 = (int64_t) * (uint32_t*)_pv;
362 __sqlvar->sqltype = CBIGINTTYPE;
363 __sqlvar->sqllen = sizeof(int32_t);
364 break;
365 }
366 case sizeof(uint64_t) : {
367 __data.i64 = (int64_t) * (uint32_t*)_pv;
368 __sqlvar->sqltype = CBIGINTTYPE;
369 __sqlvar->sqllen = sizeof(bigint);
370 break;
371 }
372 default: {
374 return false;
375 }
376 }
377 break;
378 }
379 default: {
381 return false;
382 }
383 }
384
385 __sqlvar->sqldata = (char*)&__data;
386 return true;
387}
388
389bool IFXParam::setBytes(const void* _pv, size_t _size,
390 SQL::DataType _assignType)
391{
392 switch (__sqltype & SQLTYPE) {
393 case SQLTEXT:
394 case SQLBYTES: {
395 if (_size > INT32_MAX) {
397 return false;
398 }
399 switch (_assignType) {
400 case SQL::typeText:
402 case SQL::typeClob: {
403 __data.loc.loc_type = SQLTEXT;
404 break;
405 }
406 case SQL::typeBinary:
408 case SQL::typeBlob: {
409 __data.loc.loc_type = SQLBYTES;
410 break;
411 }
412 default: {
414 return false;
415 }
416 }
417 __data.loc.loc_loctype = LOCMEMORY;
418 __data.loc.loc_indicator = 0;
419 // __data.loc.loc_type = SQLTEXT or SQLBYTES;
420 __data.loc.loc_mflags = 0;
421 __data.loc.loc_oflags = LOC_WONLY;
422
423 __data.loc.loc_buffer = (char*)_pv;
424 __data.loc.loc_bufsize = _size;
425 __data.loc.loc_size = _size;
426
427 __sqlvar->sqltype = CLOCATORTYPE;
428 __sqlvar->sqllen = sizeof(loc_t);
429 __sqlvar->sqldata = (char*)&__data.loc;
430 break;
431 }
432 default: {
433 switch (_assignType) {
434 case SQL::typeText:
436 case SQL::typeClob: {
437 __sqlvar->sqltype = CFIXCHARTYPE;
438 break;
439 }
440 case SQL::typeBinary:
442 case SQL::typeBlob: {
443 __sqlvar->sqltype = CFIXBINTYPE;
444 break;
445 }
446 default: {
448 return false;
449 }
450 }
451 __sqlvar->sqldata = (char*)_pv;
452 __sqlvar->sqllen = _size;
453 }
454 }
455 return true;
456}
457
458static mint on_loc_open(loc_t* loc, mint flag, mint bsize);
459static mint on_loc_read(loc_t* loc, char* buffer, mint buflen);
460static mint on_loc_close(loc_t* loc);
461
462bool IFXParam::setInputStream(const void* _pv, size_t _size,
463 SQL::DataType _assignType)
464{
465 if (_size != (size_t)-1 && _size > INT32_MAX) {
467 return false;
468 }
469
470 switch (_assignType) {
471 case SQL::typeText:
473 case SQL::typeClob: {
474 __data.loc.loc_type = SQLTEXT;
475 break;
476 }
477 case SQL::typeBinary:
479 case SQL::typeBlob: {
480 __data.loc.loc_type = SQLBYTES;
481 break;
482 }
483 default: {
485 return false;
486 }
487 }
488
489 __data.loc.loc_open = on_loc_open;
490 __data.loc.loc_read = on_loc_read;
491 __data.loc.loc_close = on_loc_close;
492 __data.loc.loc_write = NULL;
493
494 __data.loc.loc_loctype = LOCUSER;
495 __data.loc.loc_indicator = 0;
496 // __data.loc.loc_type = SQLTEXT or SQLBYTES;
497 __data.loc.loc_mflags = 0;
498 __data.loc.loc_oflags = LOC_WONLY;
499
500 __data.loc.loc_user_env = (char*)_pv;
501 __data.loc.loc_size = _size;
502
503 __sqlvar->sqldata = (char*)&__data;
504 __sqlvar->sqltype = CLOCATORTYPE;
505 __sqlvar->sqllen = sizeof(loc_t);
506
507 return true;
508}
509
510// MSVCRTD.DLL을 사용하는 윈도우용 DEBUG 버전은 LOCFILE을 사용할 경우
511// 실패한다. DEBUG 버전에서는 LOCUSER로 대체한다.
512static mint on_loc_open(loc_t* loc, mint flag, mint bsize)
513{
514 loc->loc_status = 0;
515 loc->loc_xfercount = 0L;
516
517 /*
518 if error {
519 loc->loc_status = -452;
520 return -1;
521 }
522 */
523 return 0;
524}
525
526inline mint __MIN(mint x, mint y)
527{
528 return x < y ? x : y;
529}
530
531static mint on_loc_read(loc_t* loc, char* buffer, mint buflen)
532{
533 mint nToRead = buflen;
534 if (loc->loc_size != -1)
535 nToRead = __MIN(buflen, loc->loc_size - loc->loc_xfercount);
536
537 InputStream* pInput = (InputStream*)(loc->loc_user_env);
538 size_t nRead = 0;
539 try {
540 nRead = pInput->read(buffer, (size_t)nToRead);
541 }
542 catch (IOException* e) {
543 e->destroy();
544 loc->loc_status = -454;
545 return -1;
546 }
547 loc->loc_xfercount += nRead;
548 return nRead;
549}
550
551static mint on_loc_close(loc_t* loc)
552{
553 loc->loc_status = 0;
554 return 0;
555}
556
557__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:526
int __encode_dtime(const SQL::TimeStamp *_s, dtime_t *_r)
Definition IFXTypes.cpp:337
int __encode_intrvl(const SQL::Interval *_s, SQL::DataType _dataType, intrvl_t *_r)
Definition IFXTypes.cpp:400
#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
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:389
bool onAfterExecute()
Definition IFXParam.cpp:75
bool setInteger(const void *_pv, size_t _size)
Definition IFXParam.cpp:269
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:305
bool setInputStream(const void *_pv, size_t _size, SQL::DataType _assignType)
Definition IFXParam.cpp:462
virtual void setNull()
Definition IFXParam.cpp:102
DataType
Definition SQLCore.h:62
@ typeBinary
Definition SQLCore.h:77
@ typeClob
Definition SQLCore.h:80
@ typeNumeric
Definition SQLCore.h:67
@ typeTime
Definition SQLCore.h:69
@ typeLongBinary
Definition SQLCore.h:79
@ typeUInteger
Definition SQLCore.h:65
@ typeUnknown
Definition SQLCore.h:63
@ typeTimeStamp
Definition SQLCore.h:71
@ typeBlob
Definition SQLCore.h:81
@ typeInputStream
Definition SQLCore.h:84
@ typeInterval
Definition SQLCore.h:73
@ typeDate
Definition SQLCore.h:68
@ typeText
Definition SQLCore.h:76
@ typeFloat
Definition SQLCore.h:66
@ typeInteger
Definition SQLCore.h:64
@ typeLongText
Definition SQLCore.h:78
@ eNotSupportDataType
Definition SQLCore.h:48
@ eInvalidDataSize
Definition SQLCore.h:57
@ eInvalidDataType
Definition SQLCore.h:49
int16_t year
Definition SQLCore.h:97
uint8_t month
Definition SQLCore.h:98
uint8_t day
Definition SQLCore.h:99