DCL 4.0
Loading...
Searching...
No Matches
IBQuery Class Reference

#include <IBQuery.h>

Inheritance diagram for IBQuery:
SQL::Query Object

Public Member Functions

 IBQuery (IBConnection *_connHandle)
virtual ~IBQuery ()
virtual void __destroy ()
virtual bool __prepare (const char *_sql, size_t _sqllen, size_t _paramCount)
virtual bool __execute ()
virtual bool __fetch ()
virtual bool __getField (size_t _index, SQL::Field **_fieldHandleOut)
virtual bool __getParam (size_t _index, SQL::Param **_paramHandleOut)
Public Member Functions inherited from Object
virtual String toString () const
virtual void destroy ()
String className () const
bool isInstanceOf (const std::type_info &typeinfo) const
virtual const std::type_info & typeInfo () const

Protected Member Functions

bool reset ()
bool initFields ()
bool initParams (size_t _paramCount)
Protected Member Functions inherited from SQL::Query
 Query (Connection *_connHandle)
virtual ~Query ()
virtual bool __nextResult ()
Protected Member Functions inherited from Object
virtual ~Object ()
 Object ()

Protected Attributes

isc_stmt_handle __stmtHandle
int __stmtType
XSQLDA * __inSQLDA
XSQLDA * __outSQLDA
char * __outBuffer
IBParam__params
IBField__fields
Protected Attributes inherited from SQL::Query
Connection__connHandle
bool __eof
int64_t __affectedRows
size_t __fieldCount
size_t __paramCount
wchar_t __placeholder
unsigned int __states

Additional Inherited Members

Public Types inherited from SQL::Query
enum  State { stStandBy = 0x0001 , stPrepared = 0x0002 , stExecuted = 0x0004 , stFetched = 0x0008 }

Detailed Description

Definition at line 11 of file IBQuery.h.

Constructor & Destructor Documentation

◆ IBQuery()

IBQuery::IBQuery ( IBConnection * _connHandle)

◆ ~IBQuery()

IBQuery::~IBQuery ( )
virtual

Definition at line 46 of file IBQuery.cpp.

47{
48#ifdef __DCL_DEBUG
49 if (!reset()) {
50 ByteString s;
51 size_t n = 512;
52 ByteBuffer* buf = ByteBuffer::create(n);
53 bool b = conn()->__getErrorMessage(buf->data(), &n);
54 if (b) {
55 buf->__dataLength = n;
56 s = buf;
57 }
58 buf->release();
59
60 if (b) {
61 __DCL_TRACE1(__T("Warning! %s\n"), s.data());
62 }
63 else {
64 __DCL_TRACE0(__T("Warning! Query reset error\n"));
65 }
66 }
67#else
68 (void)reset();
69#endif
70}
#define __DCL_TRACE0(psz)
Definition Object.h:375
#define __DCL_TRACE1(fmt, arg1)
Definition Object.h:376
#define __T(str)
Definition Object.h:44
ByteBuffer * buf
void CharsetConvertException *size_t n
Definition SQLField.cpp:253
bool reset()
Definition IBQuery.cpp:77

Member Function Documentation

◆ __destroy()

void IBQuery::__destroy ( )
virtual

Implements SQL::Query.

Definition at line 72 of file IBQuery.cpp.

73{
74 delete this;
75}

◆ __execute()

bool IBQuery::__execute ( )
virtual

Implements SQL::Query.

Definition at line 414 of file IBQuery.cpp.

415{
416 IBConnection* connHandle = conn();
417 ISC_STATUS* statusVector = connHandle->statusVector();
418
419 if (__stmtType == isc_info_sql_stmt_exec_procedure) {
420 if (isc_dsql_execute2(
421 statusVector,
422 connHandle->trHandlePtr(),
424 connHandle->dialect(),
425 __inSQLDA,
426 __outSQLDA)) {
428 return false;
429 }
430 return true;
431 }
432
433 if (isc_dsql_execute(
434 statusVector,
435 connHandle->trHandlePtr(),
436 &__stmtHandle,
437 connHandle->dialect(),
438 __inSQLDA)) {
439// __DCL_ASSERT(STATUS_FAILED(statusVector));
441 return false;
442 }
443
444 for(size_t i = 0; i < Query::__paramCount; i++)
445 __params[i].onAfterExecute();
446
447 // INSERT, DELETE, UPDATE statement의 실행 후
448 // affected records를 구한다.
449
450 unsigned char count_type = 0;
451 switch(__stmtType) {
452 case isc_info_sql_stmt_update: {
453 count_type = isc_info_req_update_count;
454 break;
455 }
456 case isc_info_sql_stmt_delete: {
457 count_type = isc_info_req_delete_count;
458 break;
459 }
460 case isc_info_sql_stmt_insert: {
461 count_type = isc_info_req_insert_count;
462 break;
463 }
464 case isc_info_sql_stmt_select:
465 case isc_info_sql_stmt_select_for_upd: {
466 Query::__eof = false;
467 break;
468 }
469 case isc_info_sql_stmt_exec_procedure: {
470 for (size_t i = 0; i < Query::__fieldCount; i++) {
471 if (!__fields[i].onAfterFetch())
472 return false;
473 }
474 break;
475 }
476 }
477
478 if (count_type) {
479 unsigned char count_is = 0;
480 char count_buffer[33];
481 unsigned short length;
482
483 if (isc_dsql_sql_info(
484 statusVector,
485 &__stmtHandle,
486 sizeof(count_info_item),
487 count_info_item,
488 sizeof(count_buffer),
489 count_buffer)) {
490// __DCL_ASSERT(STATUS_FAILED(statusVector));
492 return false;
493 }
494
495 for(char* pch = count_buffer + 3; *pch != isc_info_end; ) {
496 count_is = *pch++;
497 length = (unsigned short)isc_vax_integer(pch, 2);
498 pch += 2;
499 Query::__affectedRows = isc_vax_integer(pch, length);
500 pch += length;
501 if (count_is == count_type)
502 break;
503 }
504 }
505
506 return true;
507}
#define __SET_ERROR(_errorCode)
Definition SQLCore.cpp:150
short dialect() const
isc_tr_handle * trHandlePtr()
ISC_STATUS * statusVector()
IBParam * __params
Definition IBQuery.h:30
int __stmtType
Definition IBQuery.h:25
IBField * __fields
Definition IBQuery.h:31
XSQLDA * __inSQLDA
Definition IBQuery.h:26
XSQLDA * __outSQLDA
Definition IBQuery.h:27
isc_stmt_handle __stmtHandle
Definition IBQuery.h:24
@ eServerError
Definition SQLCore.h:21

◆ __fetch()

bool IBQuery::__fetch ( )
virtual

Implements SQL::Query.

Definition at line 509 of file IBQuery.cpp.

510{
511 __DCL_ASSERT(!eof());
512 __DCL_ASSERT(__stmtType == isc_info_sql_stmt_select);
513
514 ISC_STATUS rs = isc_dsql_fetch(conn()->statusVector(),
515 &__stmtHandle,
517 __outSQLDA);
518 if (rs == 0) {
519 for(size_t i = 0; i < Query::__fieldCount; i++) {
520 if (!__fields[i].onAfterFetch())
521 return false;
522 }
523 return true;
524 }
525 else if (rs == 100) {
526 Query::__eof = true;
527 return true;
528 }
529
530 __DCL_TRACE1(L"[%zd]\n", (int64_t)rs);
532 return false;
533}
#define SQLDA_CURRENT_VERSION
Definition IBQuery.cpp:23
#define __DCL_ASSERT(expr)
Definition Object.h:371

◆ __getField()

bool IBQuery::__getField ( size_t _index,
SQL::Field ** _fieldHandleOut )
virtual

Implements SQL::Query.

Definition at line 535 of file IBQuery.cpp.

536{
537 __DCL_ASSERT(Query::__fieldCount > 0);
538 __DCL_ASSERT((0 <= _index) && (_index < Query::__fieldCount));
539 *_fieldHandleOut = &__fields[_index];
540 return true;
541}

◆ __getParam()

bool IBQuery::__getParam ( size_t _index,
SQL::Param ** _paramHandleOut )
virtual

Implements SQL::Query.

Definition at line 543 of file IBQuery.cpp.

544{
545 __DCL_ASSERT(Query::__paramCount > 0);
546 __DCL_ASSERT((0 <= _index) && (_index < Query::__paramCount));
547 *_paramHandleOut = &__params[_index];
548 return true;
549}

◆ __prepare()

bool IBQuery::__prepare ( const char * _sql,
size_t _sqllen,
size_t _paramCount )
virtual

Implements SQL::Query.

Definition at line 339 of file IBQuery.cpp.

341{
342 if(!reset())
343 return false;
344
345 IBConnection* connHandle = conn();
346 ISC_STATUS* statusVector = connHandle->statusVector();
347
348 if (isc_dsql_allocate_statement(
349 statusVector,
350 connHandle->dbHandlePtr(),
352 ) {
353// __DCL_ASSERT(STATUS_FAILED(m_statusVector));
355 return false;
356 }
357
358 ISC_STATUS rs = isc_dsql_prepare(
359 statusVector,
360 connHandle->trHandlePtr(),
361 &__stmtHandle,
362 0,
363 (_CONST char*)_sql,
364 connHandle->dialect(),
365 NULL
366 );
367 if (rs) {
368// __DCL_ASSERT(STATUS_FAILED(m_statusVector));
370 return false;
371 }
372
373
374 int nLength;
375 char res_buffer[8];
376
377 if (isc_dsql_sql_info(
378 statusVector,
380 sizeof(stmt_info_item),
381 stmt_info_item,
382 sizeof(res_buffer),
383 res_buffer)
384 ) {
385// __DCL_ASSERT(STATUS_FAILED(m_statusVector));
387 return false;
388 }
389
390 if (res_buffer[0] != isc_info_sql_stmt_type) {
392 return false;
393 }
394 nLength = isc_vax_integer(&res_buffer[1], 2);
395 __stmtType = isc_vax_integer(&res_buffer[3], nLength);
396
397 // SELECT, EXECUTE PROCEDURE 는 Row Sets을 반환한다.
398 if ((__stmtType == isc_info_sql_stmt_select)
399 || (__stmtType == isc_info_sql_stmt_exec_procedure)
400 || (__stmtType == isc_info_sql_stmt_select_for_upd)) {
401 if (!initFields())
402 return false;
403 }
404
405 if (_paramCount > 0) {
406 if (!initParams(_paramCount))
407 return false;
408 }
409
410 return true;
411// rs = isc_dsql_set_cursor_name(statusVector, &__stmtHandle, "CURSOR1", 0);
412}
#define NULL
Definition Config.h:340
#define _CONST
Definition Config.h:353
isc_db_handle * dbHandlePtr()
bool initParams(size_t _paramCount)
Definition IBQuery.cpp:277
bool initFields()
Definition IBQuery.cpp:176

◆ initFields()

bool IBQuery::initFields ( )
protected

Definition at line 176 of file IBQuery.cpp.

177{
179 && (__outBuffer == NULL)
180 && (Query::__fieldCount == 0)
181 );
182
183 ISC_STATUS* statusVector = conn()->statusVector();
184
185 __outSQLDA = (XSQLDA*)malloc(XSQLDA_LENGTH(1));
187 __outSQLDA->sqln = 1;
188
189 if(isc_dsql_describe(
190 statusVector,
191 &__stmtHandle,
194 ) {
195// __DCL_ASSERT(STATUS_FAILED(statusVector));
197 return false;
198 }
199
200 if (__outSQLDA->sqld == 0) {
201 // 필드가 없다.
202 return true;
203 }
204
205 if (__outSQLDA->sqln < __outSQLDA->sqld) {
206 __outSQLDA = (XSQLDA*)realloc(__outSQLDA, XSQLDA_LENGTH(__outSQLDA->sqld));
208 __outSQLDA->sqln = __outSQLDA->sqld;
209
210 if (isc_dsql_describe(
211 statusVector,
212 &__stmtHandle,
215 ) {
216// __DCL_ASSERT(STATUS_FAILED(statusVector));
218 return false;
219 }
220 }
221
222 Query::__fieldCount = __outSQLDA->sqld;
223 __fields = new IBField[Query::__fieldCount];
224 if (__fields == NULL) {
226 return false;
227 }
228
229 size_t offset = 0;
230 XSQLVAR* sqlvar = __outSQLDA->sqlvar;
231 for(size_t i = 0; i < Query::__fieldCount; i++, sqlvar++) {
232 offset = __TYPE_ALIGN(offset, sqlvar->sqltype);
233 switch(sqlvar->sqltype & ~1) {
234 case SQL_TEXT :
235 offset += sqlvar->sqllen + 1;
236 break;
237 case SQL_VARYING :
238 offset += sqlvar->sqllen + sizeof(short) + 1;
239 break;
240 default :
241 offset += sqlvar->sqllen;
242 }
243 }
244
245 if (offset > 0) {
246 __outBuffer = (char*)malloc(offset);
247 __DCL_ASSERT((size_t)__outBuffer % sizeof(void*) == 0);
248 if (!__outBuffer) {
250 return false;
251 }
252 }
253
254 sqlvar = __outSQLDA->sqlvar;
255 offset = 0;
256 for(size_t i = 0; i < Query::__fieldCount; i++, sqlvar++) {
257 offset = __TYPE_ALIGN(offset, sqlvar->sqltype);
258 sqlvar->sqldata = (char*)__outBuffer + offset;
259 switch(sqlvar->sqltype & ~1) {
260 case SQL_TEXT :
261 offset += sqlvar->sqllen + 1;
262 break;
263 case SQL_VARYING :
264 offset += sqlvar->sqllen + sizeof(short) + 1;
265 break;
266 default :
267 offset += sqlvar->sqllen;
268 }
269
270 if (!__fields[i].init(this, sqlvar))
271 return false;
272 }
273
274 return true;
275}
char * __outBuffer
Definition IBQuery.h:28
@ eOutOfMemory
Definition SQLCore.h:24

◆ initParams()

bool IBQuery::initParams ( size_t _paramCount)
protected

Definition at line 277 of file IBQuery.cpp.

278{
279 ISC_STATUS* statusVector = conn()->statusVector();
280
281 __inSQLDA = (XSQLDA*)malloc(XSQLDA_LENGTH(1));
283 __inSQLDA->sqln = 1;
284
285 if (isc_dsql_describe_bind(
286 statusVector,
287 &__stmtHandle,
289 __inSQLDA)
290 ) {
291// __DCL_ASSERT(STATUS_FAILED(statusVector));
293 return false;
294 }
295
296 if (__inSQLDA->sqld == 0) {
297 // 파라미터가 없다. bind 할 필요가 없다.
298 return true;
299 }
300
301 if (__inSQLDA->sqln < __inSQLDA->sqld) {
302 __inSQLDA = (XSQLDA*)realloc(__inSQLDA, XSQLDA_LENGTH(__inSQLDA->sqld));
304 __inSQLDA->sqln = __inSQLDA->sqld;
305
306 if (isc_dsql_describe_bind(
307 statusVector,
308 &__stmtHandle,
310 __inSQLDA)
311 ) {
312// __DCL_ASSERT(STATUS_FAILED(m_statusVector));
314 return false;
315 }
316 }
317 __DCL_ASSERT(__inSQLDA->sqld == _paramCount);
318
319 Query::__paramCount = _paramCount;
320 __params = new IBParam[Query::__paramCount];
321 if (__params == NULL) {
323 return false;
324 }
325
326 XSQLVAR* sqlvar = __inSQLDA->sqlvar;
327 for(size_t i = 0; i < Query::__paramCount; i++, sqlvar++) {
328 if (!__params[i].init(this, sqlvar)) {
329 return false;
330 }
331 }
332
333 return true;
334}

◆ reset()

bool IBQuery::reset ( )
protected

Definition at line 77 of file IBQuery.cpp.

78{
79 Query::__eof = true;
80 Query::__affectedRows = -1;
82
83 // clear fields
84 if (__fields) {
85 __DCL_ASSERT(Query::__fieldCount > 0);
86 delete[] __fields;
87 __fields = NULL;
88 Query::__fieldCount = 0;
89 }
90
91 // clear binds
92 if (__params) {
93 __DCL_ASSERT(Query::__paramCount > 0);
94 delete[] __params;
95 __params = NULL;
96 Query::__paramCount = 0;
97 }
98
99 if (__outBuffer) {
100 free(__outBuffer);
102 }
103
104 if (__outSQLDA) {
105 free(__outSQLDA);
107 }
108
109 if (__inSQLDA) {
110 free(__inSQLDA);
111 __inSQLDA = NULL;
112 }
113
114 ISC_STATUS* statusVector = conn()->statusVector();
115 bool r = true;
116 if (__stmtHandle) {
117 if (isc_dsql_free_statement(
118 statusVector,
119 &__stmtHandle,
120 DSQL_drop)
121 ) {
122// __DCL_ASSERT(STATUS_FAILED(statusVector));
124 r = false;
125 }
127 }
128 return r;
129}
#define _IB_STMT_TYPE_UNKNOWN
Definition IBQuery.h:6
ByteString r

Member Data Documentation

◆ __fields

IBField* IBQuery::__fields
protected

Definition at line 31 of file IBQuery.h.

◆ __inSQLDA

XSQLDA* IBQuery::__inSQLDA
protected

Definition at line 26 of file IBQuery.h.

◆ __outBuffer

char* IBQuery::__outBuffer
protected

Definition at line 28 of file IBQuery.h.

◆ __outSQLDA

XSQLDA* IBQuery::__outSQLDA
protected

Definition at line 27 of file IBQuery.h.

◆ __params

IBParam* IBQuery::__params
protected

Definition at line 30 of file IBQuery.h.

◆ __stmtHandle

isc_stmt_handle IBQuery::__stmtHandle
protected

Definition at line 24 of file IBQuery.h.

◆ __stmtType

int IBQuery::__stmtType
protected

Definition at line 25 of file IBQuery.h.


The documentation for this class was generated from the following files: