DCL 3.7.4
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 char_t __THIS_FILE__[] = __T("dcl/sql/IFXParam.cpp");
45
46__DCL_BEGIN_NAMESPACE
47
48#define __SET_ERROR(_error) \
49 conn()->setErrorHandle(_error, 0L, __THIS_FILE__, __LINE__)
50#define __SET_ERROR_HANDLE(_SQLCODE) \
51 conn()->setErrorHandle(SQL::eServerError, _SQLCODE, __THIS_FILE__, __LINE__)
52#define __SET_ERROR_MSG(_message) \
53 conn()->setErrorMessage(_message, __THIS_FILE__, __LINE__)
54
56
57IFXParam::IFXParam() : Param(NULL)
58{
59 __sqlvar = NULL;
60}
61
65
66bool IFXParam::init(SQL::Query* _queryHandle, ifx_sqlvar_t* _sqlvar)
67{
68 __DCL_ASSERT((Param::__queryHandle == NULL) && (__sqlvar == NULL));
69
70 Param::__queryHandle = _queryHandle;
71
72 __sqltype = _sqlvar->sqltype;
73 __sqllen = _sqlvar->sqllen;
74 __indicator = -1; // set null
75 __sqlvar = _sqlvar;
76 __sqlvar->sqldata = NULL;
77 __sqlvar->sqlind = &__indicator;
78
79 return true;
80}
81
83{
84 bool r = true;
85 if (__sqlvar->sqltype == CLOCATORTYPE && __sqlvar->sqldata) {
86 loc_t* loc = (loc_t*)(__sqlvar->sqldata);
87 if (loc->loc_status != 0) {
88 __SET_ERROR_HANDLE(loc->loc_status);
89 r = false;;
90 }
91 }
92
93 Param::__dataType = SQL::typeUnknown;
94 __indicator = -1; // set null
95 __sqlvar->sqldata = NULL;
96
97 return r;
98}
99
100// in IFXField.cpp
101const wchar_t* __dataTypeName(const ifx_sqlvar_t* _sqlvar);
102
103const wchar_t* IFXParam::serverDataTypeName() const
104{
105 return __dataTypeName(__sqlvar);
106}
107
109{
110 Param::__dataType = SQL::typeUnknown;
111 __indicator = -1; // set null
112 __sqlvar->sqldata = NULL;
113}
114
116 _CONST void* _val,
117 size_t _size,
118 SQL::DataType _valType,
119 SQL::DataType _sqlType
120)
121{
122 bool r = true;
123 switch(_valType) {
124 case SQL::typeInteger : {
125 r = setInteger(_val, _size);
126 break;
127 }
128 case SQL::typeUInteger: {
129 r = setUInteger(_val, _size);
130 break;
131 }
132 case SQL::typeFloat: {
133 if (_size == sizeof(float)) {
134 __data.f32 = *(float*)_val;
135 __sqlvar->sqldata = (char*)&__data;
136 __sqlvar->sqltype = CFLOATTYPE;
137 __sqlvar->sqllen = sizeof(float);
138 }
139 else if (_size == sizeof(double)) {
140 __data.f64 = *(double*)_val;
141 __sqlvar->sqldata = (char*)&__data;
142 __sqlvar->sqltype = CDOUBLETYPE;
143 __sqlvar->sqllen = sizeof(double);
144 }
145 else {
147 return false;
148 }
149 break;
150 }
151 case SQL::typeDate: {
152 if (_size == sizeof(SQL::Date)) {
153 const SQL::Date* p = (const SQL::Date*)_val;
154 short mdy[3];
155 mdy[0] = p->month;
156 mdy[1] = p->day;
157 mdy[2] = p->year;
158
159 int r = rmdyjul(mdy, &__data.date);
160 if (r) {
162 return false;
163 }
164 __sqlvar->sqldata = (char*)&__data;
165 __sqlvar->sqltype = CDATETYPE;
166 __sqlvar->sqllen = sizeof(long);
167 }
168 else {
170 return false;
171 }
172 break;
173 }
174 case SQL::typeTime: {
175 if (_size == sizeof(SQL::Time)) {
176 const SQL::Time* p = (const SQL::Time*)_val;
177 int r = __encode_dtime(p, &__data.dtime);
178 if (r) {
180 return false;
181 }
182
183 __sqlvar->sqldata = (char*)&__data;
184 __sqlvar->sqltype = CDTIMETYPE;
185 __sqlvar->sqllen = sizeof(dtime_t);
186 }
187 else {
189 return false;
190 }
191 break;
192 }
193 case SQL::typeTimeStamp: {
194 if (_size == sizeof(SQL::TimeStamp)) {
195 const SQL::TimeStamp* p = (const SQL::TimeStamp*)_val;
196 int r = __encode_dtime(p, &__data.dtime);
197 if (r) {
199 return false;
200 }
201
202 __sqlvar->sqldata = (char*)&__data;
203 __sqlvar->sqltype = CDTIMETYPE;
204 __sqlvar->sqllen = sizeof(dtime_t);
205 }
206 else {
208 return false;
209 }
210 break;
211 }
212 case SQL::typeInterval: {
213 if (_size == sizeof(SQL::Interval)) {
214 int r = __encode_intrvl(
215 (const SQL::Interval*)_val,
216 _sqlType,
217 &__data.ival
218 );
219 if (r) {
221 return false;
222 }
223
224 __sqlvar->sqldata = (char*)&__data;
225 __sqlvar->sqltype = CINVTYPE;
226 __sqlvar->sqllen = sizeof(intrvl_t);
227 }
228 else {
230 return false;
231 }
232 break;
233 }
234 case SQL::typeText : {
235 if (_sqlType == SQL::typeNumeric) {
236 if (_size > 0) {
237 int r = deccvasc((char*)_val, _size, &__data.dec);
238 if (r) {
240 return false;
241 }
242 __sqlvar->sqldata = (char*)&__data;
243 __sqlvar->sqltype = CDECIMALTYPE;
244 __sqlvar->sqllen = sizeof(dec_t);
245 }
246 else {
248 return false;
249 }
250 break;
251 }
252 }
253 case SQL::typeBinary: {
254 r = setBytes(_val, _size, _sqlType);
255 break;
256 }
258 r = setInputStream(_val, _size, _sqlType);
259 break;
260 }
261 default: {
263 return false;
264 }
265 }
266
267 if (r) {
268 __indicator = 0;
269 Param::__dataType = _sqlType;
270 }
271
272 return true;
273}
274
275bool IFXParam::setInteger(const void* _val, size_t _size)
276{
277 switch (_size) {
278 case sizeof(int8_t) : {
279 __data.i32 = (int32_t) * (int8_t*)_val;
280 __sqlvar->sqltype = CLONGTYPE;
281 __sqlvar->sqllen = sizeof(int32_t);
282 break;
283 }
284 case sizeof(int16_t) : {
285 __data.i32 = (int32_t) * (int16_t*)_val;
286 __sqlvar->sqltype = CLONGTYPE;
287 __sqlvar->sqllen = sizeof(int32_t);
288 break;
289 }
290 case sizeof(int32_t) : {
291 __data.i32 = *(int32_t*)_val;
292 __sqlvar->sqltype = CLONGTYPE;
293 __sqlvar->sqllen = sizeof(int32_t);
294 break;
295 }
296 case sizeof(int64_t) : {
297 __data.i64 = *(int64_t*)_val;
298 __sqlvar->sqltype = CBIGINTTYPE;
299 __sqlvar->sqllen = sizeof(bigint);
300 break;
301 }
302 default: {
304 return false;
305 }
306 }
307 __sqlvar->sqldata = (char*)&__data;
308 return true;
309}
310
311bool IFXParam::setUInteger(const void* _val, size_t _size)
312{
313 switch (__sqltype & SQLTYPE) {
314 case SQLSMINT:
315 case SQLINT:
316 case SQLSERIAL: {
317 switch (_size) {
318 case sizeof(uint8_t) : {
319 __data.i32 = (int32_t) * (uint8_t*)_val;
320 __sqlvar->sqltype = CLONGTYPE;
321 __sqlvar->sqllen = sizeof(int32_t);
322 break;
323 }
324 case sizeof(uint16_t) : {
325 __data.i32 = (int32_t) * (uint16_t*)_val;
326 __sqlvar->sqltype = CLONGTYPE;
327 __sqlvar->sqllen = sizeof(int32_t);
328 break;
329 }
330 case sizeof(uint32_t) : {
331 __data.i32 = (int32_t) * (uint32_t*)_val;
332 __sqlvar->sqltype = CLONGTYPE;
333 __sqlvar->sqllen = sizeof(int32_t);
334 break;
335 }
336 case sizeof(uint64_t) : {
337 __data.i64 = (int64_t) * (uint32_t*)_val;
338 __sqlvar->sqltype = CBIGINTTYPE;
339 __sqlvar->sqllen = sizeof(bigint);
340 break;
341 }
342 default: {
344 return false;
345 }
346 }
347 break;
348 }
349 case SQLINT8:
350 case SQLSERIAL8:
351 case SQLINFXBIGINT:
352 case SQLBIGSERIAL: {
353 switch (_size) {
354 case sizeof(uint8_t) : {
355 __data.i64 = (int64_t) * (uint8_t*)_val;
356 __sqlvar->sqltype = CBIGINTTYPE;
357 __sqlvar->sqllen = sizeof(int32_t);
358 break;
359 }
360 case sizeof(uint16_t) : {
361 __data.i64 = (int64_t) * (uint16_t*)_val;
362 __sqlvar->sqltype = CBIGINTTYPE;
363 __sqlvar->sqllen = sizeof(int32_t);
364 break;
365 }
366 case sizeof(uint32_t) : {
367 __data.i64 = (int64_t) * (uint32_t*)_val;
368 __sqlvar->sqltype = CBIGINTTYPE;
369 __sqlvar->sqllen = sizeof(int32_t);
370 break;
371 }
372 case sizeof(uint64_t) : {
373 __data.i64 = (int64_t) * (uint32_t*)_val;
374 __sqlvar->sqltype = CBIGINTTYPE;
375 __sqlvar->sqllen = sizeof(bigint);
376 break;
377 }
378 default: {
380 return false;
381 }
382 }
383 break;
384 }
385 default: {
387 return false;
388 }
389 }
390
391 __sqlvar->sqldata = (char*)&__data;
392 return true;
393}
394
395bool IFXParam::setBytes(const void* _val, size_t _size,
396 SQL::DataType _sqlType)
397{
398 switch (__sqltype & SQLTYPE) {
399 case SQLTEXT:
400 case SQLBYTES: {
401 if (_size > INT32_MAX) {
403 return false;
404 }
405 switch (_sqlType) {
406 case SQL::typeText:
408 case SQL::typeClob: {
409 __data.loc.loc_type = SQLTEXT;
410 break;
411 }
412 case SQL::typeBinary:
414 case SQL::typeBlob: {
415 __data.loc.loc_type = SQLBYTES;
416 break;
417 }
418 default: {
420 return false;
421 }
422 }
423 __data.loc.loc_loctype = LOCMEMORY;
424 __data.loc.loc_indicator = 0;
425 // __data.loc.loc_type = SQLTEXT or SQLBYTES;
426 __data.loc.loc_mflags = 0;
427 __data.loc.loc_oflags = LOC_WONLY;
428
429 __data.loc.loc_buffer = (char*)_val;
430 __data.loc.loc_bufsize = _size;
431 __data.loc.loc_size = _size;
432
433 __sqlvar->sqltype = CLOCATORTYPE;
434 __sqlvar->sqllen = sizeof(loc_t);
435 __sqlvar->sqldata = (char*)&__data.loc;
436 break;
437 }
438 default: {
439 switch (_sqlType) {
440 case SQL::typeText:
442 case SQL::typeClob: {
443 __sqlvar->sqltype = CFIXCHARTYPE;
444 break;
445 }
446 case SQL::typeBinary:
448 case SQL::typeBlob: {
449 __sqlvar->sqltype = CFIXBINTYPE;
450 break;
451 }
452 default: {
454 return false;
455 }
456 }
457 __sqlvar->sqldata = (char*)_val;
458 __sqlvar->sqllen = _size;
459 }
460 }
461 return true;
462}
463
464static mint on_loc_open(loc_t* _loc, mint _flag, mint _bsize);
465static mint on_loc_read(loc_t* _loc, char* _val, mint _buflen);
466static mint on_loc_close(loc_t* _loc);
467
468bool IFXParam::setInputStream(const void* _val, size_t _size,
469 SQL::DataType _sqlType)
470{
471 if (_size != (size_t)-1 && _size > INT32_MAX) {
473 return false;
474 }
475
476 switch (_sqlType) {
477 case SQL::typeText:
479 case SQL::typeClob: {
480 __data.loc.loc_type = SQLTEXT;
481 break;
482 }
483 case SQL::typeBinary:
485 case SQL::typeBlob: {
486 __data.loc.loc_type = SQLBYTES;
487 break;
488 }
489 default: {
491 return false;
492 }
493 }
494
495 __data.loc.loc_open = on_loc_open;
496 __data.loc.loc_read = on_loc_read;
497 __data.loc.loc_close = on_loc_close;
498 __data.loc.loc_write = NULL;
499
500 __data.loc.loc_loctype = LOCUSER;
501 __data.loc.loc_indicator = 0;
502 // __data.loc.loc_type = SQLTEXT or SQLBYTES;
503 __data.loc.loc_mflags = 0;
504 __data.loc.loc_oflags = LOC_WONLY;
505
506 __data.loc.loc_user_env = (char*)_val;
507 __data.loc.loc_size = _size;
508
509 __sqlvar->sqldata = (char*)&__data;
510 __sqlvar->sqltype = CLOCATORTYPE;
511 __sqlvar->sqllen = sizeof(loc_t);
512
513 return true;
514}
515
516// MSVCRTD.DLL을 사용하는 윈도우용 DEBUG 버전은 LOCFILE을 사용할 경우
517// 실패한다. DEBUG 버전에서는 LOCUSER로 대체한다.
518static mint on_loc_open(loc_t* _loc, mint _flag, mint _bsize)
519{
520 _loc->loc_status = 0;
521 _loc->loc_xfercount = 0L;
522
523 /*
524 if error {
525 _loc->loc_status = -452;
526 return -1;
527 }
528 */
529 return 0;
530}
531
532inline mint __MIN(mint _x, mint _y)
533{
534 return _x < _y ? _x : _y;
535}
536
537static mint on_loc_read(loc_t* _loc, char* _val, mint _buflen)
538{
539 mint nToRead = _buflen;
540 if (_loc->loc_size != -1)
541 nToRead = __MIN(_buflen, _loc->loc_size - _loc->loc_xfercount);
542
543 InputStream* pInput = (InputStream*)(_loc->loc_user_env);
544 size_t nRead = 0;
545 try {
546 nRead = pInput->read(_val, (size_t)nToRead);
547 }
548 catch (IOException* e) {
549 e->destroy();
550 _loc->loc_status = -454;
551 return -1;
552 }
553 _loc->loc_xfercount += nRead;
554 return nRead;
555}
556
557static mint on_loc_close(loc_t* _loc)
558{
559 _loc->loc_status = 0;
560 return 0;
561}
562
563__DCL_END_NAMESPACE
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:312
#define INT32_MAX
Definition Config.h:290
wchar_t char_t
Definition Config.h:247
#define _CONST
Definition Config.h:325
#define __SET_ERROR_HANDLE(_SQLCODE)
const wchar_t * __dataTypeName(const ifx_sqlvar_t *_sqlvar)
Definition IFXField.cpp:304
const wchar_t * __dataTypeName(const ifx_sqlvar_t *_sqlvar)
Definition IFXField.cpp:304
int mint
Definition IFXParam.cpp:11
mint __MIN(mint _x, mint _y)
Definition IFXParam.cpp:532
int __encode_intrvl(const SQL::Interval *_s, SQL::DataType _sqlType, intrvl_t *_r)
Definition IFXTypes.cpp:400
int __encode_dtime(const SQL::TimeStamp *_s, dtime_t *_r)
Definition IFXTypes.cpp:337
IOException *size_t r
Definition MediaInfo.cpp:82
#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
bool setInteger(const void *_val, size_t _size)
Definition IFXParam.cpp:275
loc_t loc
Definition IFXParam.h:23
bool init(SQL::Query *_query, ifx_sqlvar_t *_sqlvar)
Definition IFXParam.cpp:66
bool onAfterExecute()
Definition IFXParam.cpp:82
virtual bool __setData(_CONST void *_val, size_t _size, SQL::DataType _valType, SQL::DataType _sqlType)
Definition IFXParam.cpp:115
bool setInputStream(const void *_val, size_t _size, SQL::DataType _sqlType)
Definition IFXParam.cpp:468
bool setUInteger(const void *_val, size_t _size)
Definition IFXParam.cpp:311
virtual ~IFXParam()
Definition IFXParam.cpp:62
virtual const wchar_t * serverDataTypeName() const
Definition IFXParam.cpp:103
bool setBytes(const void *_val, size_t _size, SQL::DataType _sqlType)
Definition IFXParam.cpp:395
virtual void setNull()
Definition IFXParam.cpp:108
DataType
Definition SQLCore.h:59
@ typeBinary
Definition SQLCore.h:74
@ typeClob
Definition SQLCore.h:77
@ typeNumeric
Definition SQLCore.h:64
@ typeTime
Definition SQLCore.h:66
@ typeLongBinary
Definition SQLCore.h:76
@ typeUInteger
Definition SQLCore.h:62
@ typeUnknown
Definition SQLCore.h:60
@ typeTimeStamp
Definition SQLCore.h:68
@ typeBlob
Definition SQLCore.h:78
@ typeInputStream
Definition SQLCore.h:81
@ typeInterval
Definition SQLCore.h:70
@ typeDate
Definition SQLCore.h:65
@ typeText
Definition SQLCore.h:73
@ typeFloat
Definition SQLCore.h:63
@ typeInteger
Definition SQLCore.h:61
@ typeLongText
Definition SQLCore.h:75
@ eNotSupportDataType
Definition SQLCore.h:48
@ eInvalidDataSize
Definition SQLCore.h:55
@ eInvalidDataType
Definition SQLCore.h:49
int16_t year
Definition SQLCore.h:96
uint8_t month
Definition SQLCore.h:97
uint8_t day
Definition SQLCore.h:98