DCL 4.0
Loading...
Searching...
No Matches
PgQuery.cpp
Go to the documentation of this file.
1/* Processed by ecpg (17.4) */
2/* These include files are added by the preprocessor */
3#include <ecpglib.h>
4#include <ecpgerrno.h>
5#include <sqlca.h>
6/* End of automatic include section */
7
8#line 1 "PgQuery.pgc"
9#include <dcl/Config.h>
10
11#include <stdlib.h> // malloc, free
12
13#include <ecpgtype.h>
14#include <sqlda.h>
15#include <pgtypes_numeric.h>
16#include <pgtypes_date.h>
17#include <pgtypes_interval.h>
18#include <pgtypes_timestamp.h>
19
20#include <dcl/Object.h>
21#if __DCL_HAVE_ALLOC_DEBUG
22#undef __DCL_ALLOC_LEVEL
23#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
24#endif
25
26#include <dcl/Numeric.h>
27#include <dcl/SQLCore.h>
28
29#include "PgConnection.h"
30#include "PgQuery.h"
31#include "PgField.h"
32#include "PgParam.h"
33
34#undef __THIS_FILE__
35static const wchar_t __THIS_FILE__[] = __T("dcl/sql/PgQuery.ec");
36
37__DCL_BEGIN_NAMESPACE
38
40
42 : Query(pConnection)
43{
44 ByteString strID = ByteString::format("%p", this);
45
46 __statementID = "stmt_" + strID;
47 __cursorID = "cursor_" + strID;
48
49 __inSQLDA = NULL;
50 __outSQLDA = NULL;
51
52 __cursorOpened = false;
53 __cursorDeclared = false;
54
55 __fields = NULL;
56 __params = NULL;
57}
58
60{
61#ifdef __DCL_DEBUG
62 if (!reset()) {
63 ByteString s;
64 size_t n = 512;
65 ByteBuffer* buf = ByteBuffer::create(n);
66 bool b = conn()->__getErrorMessage(buf->data(), &n);
67 if (b) {
68 buf->__dataLength = n;
69 s = buf;
70 }
71 buf->release();
72
73 if (b) {
74 __DCL_TRACE1(__T("Warning! %s\n"), s.data());
75 }
76 else {
77 __DCL_TRACE0(__T("Warning! Query reset error\n"));
78 }
79 }
80#else
81 (void)reset();
82#endif
83}
84
86{
87// cerr << "PgQuery::destory\n";
88 delete this;
89}
90
92{
93 /* exec sql begin declare section */
94
95
96
97
98#line 86 "PgQuery.pgc"
99 char * pszConnectionID = conn () -> connectionID () ;
100
101#line 87 "PgQuery.pgc"
102 char * pszStatementID = ( _CONST char * ) __statementID . data () ;
103
104#line 88 "PgQuery.pgc"
105 char * pszCursorID = ( _CONST char * ) __cursorID . data () ;
106/* exec sql end declare section */
107#line 89 "PgQuery.pgc"
108
109
110 { ECPGsetconn(__LINE__, pszConnectionID);}
111#line 91 "PgQuery.pgc"
112
113 if (SQLCODE < 0) {
114 __SET_ERROR_SQLCODE(SQLCODE);
115 return false;
116 }
117 __DCL_ASSERT(SQLCODE == 0);
118
119 if (__cursorOpened) {
120 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close $0",
121 ECPGt_char,&(pszCursorID),(long)0,(long)1,(1)*sizeof(char),
122 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
123#line 99 "PgQuery.pgc"
124
125 __cursorOpened = false;
126 }
127
128 if (__cursorDeclared) {
129 { ECPGdeallocate(__LINE__, 0, NULL, pszCursorID);}
130#line 104 "PgQuery.pgc"
131
132 __cursorDeclared = false;
133 }
134
135 if (__outSQLDA) {
136 { ECPGdeallocate(__LINE__, 0, NULL, pszStatementID);}
137#line 109 "PgQuery.pgc"
138
139 }
140
141 Query::__eof = true;
142 Query::__affectedRows = -1;
143
144 // clear fields
145 if (__fields) {
146 __DCL_ASSERT(Query::__fieldCount > 0);
147 delete[] __fields;
148 __fields = NULL;
149 Query::__fieldCount = 0;
150 }
151
152 // clear binds
153 if (__params) {
154 __DCL_ASSERT(Query::__paramCount > 0);
155 delete[] __params;
156 __params = NULL;
157 Query::__paramCount = 0;
158 }
159
160 if (__outSQLDA) {
161 // __outSQLDA는 Informix API에서 할당됨.
162 // DCLPgFree(__outSQLDA);
164 }
165
166 if (__inSQLDA) {
167 free(__inSQLDA);
168 __inSQLDA = NULL;
169 }
170
171 return true;
172}
173
175{
177 && (Query::__fieldCount == 0)
178 && (__outSQLDA != NULL)
179 && (__outSQLDA->sqld > 0));
180
181 Query::__fieldCount = __outSQLDA->sqld;
182 __fields = new PgField[Query::__fieldCount];
183 if (__fields == NULL) {
185 return false;
186 }
187
188 sqlvar_t* sqlvar = __outSQLDA->sqlvar;
189 for(size_t i = 0; i < Query::__fieldCount; i++, sqlvar++) {
190 if (!__fields[i].init(this, sqlvar))
191 return false;
192 }
193
194 return true;
195}
196
197bool PgQuery::initParams(size_t _paramCount)
198{
200 && (Query::__paramCount == 0)
201 && (__outSQLDA != NULL));
202
203 size_t nAllocSize = sizeof(sqlda_t)
204 + (sizeof(sqlvar_t) * _paramCount);
205 __inSQLDA = (sqlda_t*)malloc(nAllocSize);
206 if (__inSQLDA == NULL) {
208 return false;
209 }
210 memset(__inSQLDA, 0, nAllocSize);
211 __inSQLDA->sqln = (short)_paramCount;
212
213 Query::__paramCount = _paramCount;
214 __params = new PgParam[Query::__paramCount];
215 if (__params == NULL) {
217 return false;
218 }
219
220 sqlvar_t* sqlvar = __inSQLDA->sqlvar;
221 for(size_t i = 0; i < Query::__paramCount; i++, sqlvar++) {
222 if (!__params[i].init(this, sqlvar))
223 return false;
224 }
225
226 return true;
227}
228
229const wchar_t* __dataTypeName(const sqlvar_t* _sqlvar);
230
231bool PgQuery::__prepare(const char* _sql, size_t _sqllen,
232 size_t _paramCount)
233{
234 if(!reset())
235 return false;
236
237 /* exec sql begin declare section */
238
239
240
241
242#line 209 "PgQuery.pgc"
243 char * pszConnectionID = conn () -> connectionID () ;
244
245#line 210 "PgQuery.pgc"
246 char * pszStatementID = ( _CONST char * ) __statementID . data () ;
247
248#line 211 "PgQuery.pgc"
249 char * pszSQL = ( _CONST char * ) _sql ;
250/* exec sql end declare section */
251#line 212 "PgQuery.pgc"
252
253
254 { ECPGsetconn(__LINE__, pszConnectionID);}
255#line 214 "PgQuery.pgc"
256
257 if (SQLCODE < 0) {
258 __SET_ERROR_SQLCODE(SQLCODE);
259 return false;
260 }
261 __DCL_ASSERT(SQLCODE == 0);
262
263 { ECPGprepare(__LINE__, NULL, 0, pszStatementID, pszSQL);}
264#line 221 "PgQuery.pgc"
265
266
267 if (SQLCODE < 0) {
268 __SET_ERROR_SQLCODE(SQLCODE);
269 return false;
270 }
271
272 { ECPGdescribe(__LINE__, 0, 0, NULL, pszStatementID,
273 ECPGt_sqlda, &__outSQLDA, 0L, 0L, 0L,
274 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
275#line 228 "PgQuery.pgc"
276
277
278 if (SQLCODE < 0) {
279 __SET_ERROR_SQLCODE(SQLCODE);
280 return false;
281 }
282
283 if (__outSQLDA) {
284#if 0
285 __DCL_TRACE1(L"[%p]\n", __outSQLDA);
286 __DCL_TRACE4(L"[%hs] [%ld] sqln[%d] sqld[%d]\n",
287 __outSQLDA->sqldaid, __outSQLDA->sqldabc,
288 __outSQLDA->sqln, __outSQLDA->sqld
289 );
290 for (int i = 0; i < __outSQLDA->sqld; i++) {
291 sqlvar_t* p = &(__outSQLDA->sqlvar[i]);
292 __DCL_TRACE4(L"[%19ls] [%hd] [%p] [%p]\n",
293 __dataTypeName(p), p->sqllen, p->sqldata, p->sqlind
294 );
295 }
296#endif
297 if (__outSQLDA->sqld > 0) {
298 if (!initFields())
299 return false;
300 }
301 }
302
303 if (_paramCount > 0) {
304 if (!initParams(_paramCount))
305 return false;
306 }
307
308 return true;
309}
310
312{
313 /* exec sql begin declare section */
314
315
316
317
318#line 266 "PgQuery.pgc"
319 char * pszConnectionID = conn () -> connectionID () ;
320
321#line 267 "PgQuery.pgc"
322 char * pszStatementID = ( _CONST char * ) __statementID . data () ;
323
324#line 268 "PgQuery.pgc"
325 char * pszCursorID = ( _CONST char * ) __cursorID . data () ;
326/* exec sql end declare section */
327#line 269 "PgQuery.pgc"
328
329
330 { ECPGsetconn(__LINE__, pszConnectionID);}
331#line 271 "PgQuery.pgc"
332
333 if (SQLCODE < 0) {
334 __SET_ERROR_SQLCODE(SQLCODE);
335 return false;
336 }
337 __DCL_ASSERT(SQLCODE == 0);
338
339 if (__outSQLDA && __outSQLDA->sqld > 0) {
340 if (!__cursorDeclared) {
341 ECPGset_var( 0, &( pszCursorID ), __LINE__);\
342 /* declare $0 cursor for $1 */
343#line 280 "PgQuery.pgc"
344
345#line 280 "PgQuery.pgc"
346
347 if (SQLCODE < 0) {
348 __SET_ERROR_SQLCODE(SQLCODE);
349 return false;
350 }
351 __cursorDeclared = true;
352 }
353
354 if (__cursorOpened) {
355 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close $0",
356 ECPGt_char,&(pszCursorID),(long)0,(long)1,(1)*sizeof(char),
357 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
358#line 289 "PgQuery.pgc"
359
360 if (SQLCODE < 0) {
361 __SET_ERROR_SQLCODE(SQLCODE);
362 return false;
363 }
364 __cursorOpened = false;
365 }
366
367 if (__inSQLDA)
368 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare $0 cursor for $1",
369 ECPGt_char,&(pszCursorID),(long)0,(long)1,(1)*sizeof(char),
370 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
371 ECPGt_char_variable,(ECPGprepared_statement(NULL, pszStatementID, __LINE__)),(long)1,(long)1,(1)*sizeof(char),
372 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
373 ECPGt_sqlda, &__inSQLDA, 0L, 0L, 0L,
374 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
375#line 298 "PgQuery.pgc"
376
377 else
378 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare $0 cursor for $1",
379 ECPGt_char,&(pszCursorID),(long)0,(long)1,(1)*sizeof(char),
380 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
381 ECPGt_char_variable,(ECPGprepared_statement(NULL, pszStatementID, __LINE__)),(long)1,(long)1,(1)*sizeof(char),
382 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
383#line 300 "PgQuery.pgc"
384
385
386 if (SQLCODE < 0) {
387 __SET_ERROR_SQLCODE(SQLCODE);
388 return false;
389 }
390
391 __cursorOpened = true;
392 __eof = false;
393 }
394 else {
395#if 0
396 // 2025.05.06 __outSQLDA 조건은 CURSOR로 처리 하므로
397 // 다음 EXEC SQL EXECUTE는 의미가 없다.
398 if (__outSQLDA && __inSQLDA)
399 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_execute, pszStatementID,
400 ECPGt_sqlda, &__inSQLDA, 0L, 0L, 0L,
401 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT,
402 ECPGt_sqlda, &__outSQLDA, 0L, 0L, 0L,
403 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
404#line 316 "PgQuery.pgc"
405
406 else if (__outSQLDA)
407 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_execute, pszStatementID, ECPGt_EOIT,
408 ECPGt_sqlda, &__outSQLDA, 0L, 0L, 0L,
409 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
410#line 318 "PgQuery.pgc"
411
412 else
413#endif
414 if (__inSQLDA)
415 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_execute, pszStatementID,
416 ECPGt_sqlda, &__inSQLDA, 0L, 0L, 0L,
417 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
418#line 322 "PgQuery.pgc"
419
420 else
421 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_execute, pszStatementID, ECPGt_EOIT, ECPGt_EORT);}
422#line 324 "PgQuery.pgc"
423
424
425 if (SQLCODE < 0) {
426 __SET_ERROR_SQLCODE(SQLCODE);
427 Query::__affectedRows = -1;
428 return false;
429 }
430
431 // INSERT, UPDATE, DELETE
432 Query::__affectedRows = sqlca.sqlerrd[2];
433 }
434
435 for(size_t i = 0; i < Query::__paramCount; i++) {
436 if (!(__params[i].onAfterExecute()))
437 return false;
438 }
439
440 return true;
441}
442
444{
445 // SELECT, CALL
446 __DCL_ASSERT(!eof());
447 __DCL_ASSERT(__outSQLDA && __outSQLDA->sqld > 0);
448
449 /* exec sql begin declare section */
450
451
452#line 351 "PgQuery.pgc"
453 char * pszCursorID = ( _CONST char * ) __cursorID . data () ;
454/* exec sql end declare section */
455#line 352 "PgQuery.pgc"
456
457
458 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch $0",
459 ECPGt_char,&(pszCursorID),(long)0,(long)1,(1)*sizeof(char),
460 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT,
461 ECPGt_sqlda, &__outSQLDA, 0L, 0L, 0L,
462 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
463#line 354 "PgQuery.pgc"
464
465
466 if (SQLCODE == ECPG_NO_ERROR) {
467#if 0
468 __DCL_TRACE1(L"[%p]\n", __outSQLDA);
469 __DCL_TRACE4(L"[%hs] [%ld] sqln[%d] sqld[%d]\n",
470 __outSQLDA->sqldaid, __outSQLDA->sqldabc,
471 __outSQLDA->sqln, __outSQLDA->sqld
472 );
473 for (int i = 0; i < __outSQLDA->sqld; i++) {
474 sqlvar_t* p = &(__outSQLDA->sqlvar[i]);
475 __DCL_TRACE4(L"[%19ls] [%hd] [%p] [%p]\n",
476 __dataTypeName(p), p->sqllen, p->sqldata, p->sqlind
477 );
478 }
479#endif
480 sqlvar_t* sqlvar = __outSQLDA->sqlvar;
481 for (size_t i = 0; i < Query::__fieldCount; i++) {
482 if (!__fields[i].onAfterFetch(sqlvar++))
483 return false;
484 }
485 return true;
486 }
487 else if (SQLCODE == ECPG_NOT_FOUND) {
488 Query::__eof = true;
489 return true;
490 }
491
492 __SET_ERROR_SQLCODE(SQLCODE);
493 return false;
494}
495
496bool PgQuery::__getField(size_t _index, SQL::Field** _fieldHandleOut)
497{
498 __DCL_ASSERT(Query::__fieldCount > 0);
499 __DCL_ASSERT((0 <= _index) && (_index < Query::__fieldCount));
500 *_fieldHandleOut = &__fields[_index];
501 return true;
502}
503
504bool PgQuery::__getParam(size_t _index, SQL::Param** _paramHandleOut)
505{
506 __DCL_ASSERT(Query::__paramCount > 0);
507 __DCL_ASSERT((0 <= _index) && (_index < Query::__paramCount));
508 *_paramHandleOut = &__params[_index];
509 return true;
510}
511
512__DCL_END_NAMESPACE
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:340
#define _CONST
Definition Config.h:353
#define __SET_ERROR_SQLCODE(SQLCODE)
const wchar_t * __dataTypeName(const ifx_sqlvar_t *_sqlvar)
Definition IFXField.cpp:290
#define __DCL_TRACE0(psz)
Definition Object.h:375
#define __DCL_TRACE1(fmt, arg1)
Definition Object.h:376
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define __DCL_TRACE4(fmt, arg1, arg2, arg3, arg4)
Definition Object.h:379
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:228
#define __T(str)
Definition Object.h:44
const wchar_t * __dataTypeName(const sqlvar_t *_sqlvar)
Definition PgField.cpp:183
ByteBuffer * buf
#define __SET_ERROR(_errorCode)
Definition SQLCore.cpp:150
void CharsetConvertException *size_t n
Definition SQLField.cpp:253
PgQuery(PgConnection *pConnection)
virtual bool __getField(size_t _index, SQL::Field **_fieldHandleOut)
Definition PgQuery.cpp:496
virtual bool __prepare(const char *_sql, size_t _sqllen, size_t _paramCount)
Definition PgQuery.cpp:231
sqlda_t * __outSQLDA
Definition PgQuery.h:24
bool initFields()
Definition PgQuery.cpp:174
PgField * __fields
Definition PgQuery.h:30
virtual ~PgQuery()
Definition PgQuery.cpp:59
bool __cursorDeclared
Definition PgQuery.h:27
virtual void __destroy()
Definition PgQuery.cpp:85
sqlda_t * __inSQLDA
Definition PgQuery.h:25
virtual bool __getParam(size_t _index, SQL::Param **_paramHandleOut)
Definition PgQuery.cpp:504
virtual bool __fetch()
Definition PgQuery.cpp:443
bool __cursorOpened
Definition PgQuery.h:28
bool reset()
Definition PgQuery.cpp:91
PgParam * __params
Definition PgQuery.h:31
bool initParams(size_t _paramCount)
Definition PgQuery.cpp:197
ByteString __statementID
Definition PgQuery.h:21
ByteString __cursorID
Definition PgQuery.h:22
virtual bool __execute()
Definition PgQuery.cpp:311
bool __eof
Definition SQLCore.h:286
@ eOutOfMemory
Definition SQLCore.h:24