4#include <libkern/OSByteOrder.h>
12#if __DCL_HAVE_ALLOC_DEBUG
13#undef __DCL_ALLOC_LEVEL
14#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
27#define __DCL_TRACE0_N __DCL_TRACE0
28#define __DCL_TRACE1_N __DCL_TRACE1
29#define __DCL_TRACE2_N __DCL_TRACE2
30#define __DCL_TRACE3_N __DCL_TRACE3
31#define __DCL_TRACE4_N __DCL_TRACE4
32#define __DCL_TRACE5_N __DCL_TRACE5
33#define __DCL_TRACE6_N __DCL_TRACE6
35#define __DCL_TRACE0_N(fmt)
36#define __DCL_TRACE1_N(fmt, arg)
37#define __DCL_TRACE2_N(fmt, arg1, arg2)
38#define __DCL_TRACE3_N(fmt, arg1, arg2, arg3)
39#define __DCL_TRACE4_N(fmt, arg1, arg2, arg3, arg4)
40#define __DCL_TRACE5_N(fmt, arg1, arg2, arg3, arg4, arg5)
41#define __DCL_TRACE6_N(fmt, arg1, arg2, arg3, arg4, arg5, arg6)
49#define __SET_ERROR(_error) \
50 connection()->setErrorStatus(_error, __THIS_FILE__, __LINE__)
51#define __SET_ERROR_MSG(_message) \
52 conn()->setErrorMessage(_message, __THIS_FILE__, __LINE__)
59 Query::__placeholder = L
'$';
76 size_t buflen =
sizeof(buf) - 1;
77 bool b = conn()->__getErrorMessage(buf, &buflen);
78 buf[b ? buflen : 0] =
'\0';
94 Query::__affectedRows = -1;
101 Query::__fieldCount = 0;
110 Query::__paramCount = 0;
130 PGresult* res = PQexec(conn()->connHandle(), cmd);
131 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
136#ifdef LIBPQ_HAS_CLOSE_PREPARED
137 res = PQclosePortal(conn()->connHandle(),
__cursorID);
138 if (
r && PQresultStatus(res) != PGRES_COMMAND_OK) {
148#ifdef LIBPQ_HAS_CLOSE_PREPARED
149 PGresult* res = PQclosePrepared(conn()->connHandle(),
__stmtID);
150 if (
r && PQresultStatus(res) != PGRES_COMMAND_OK) {
163 const char* _sql,
size_t _sqllen,
size_t _paramCount
173 ByteString stmtID = ByteString::format(
"stmt_%d",
__stmtNo);
174 PGresult* res = PQprepare(conn()->connHandle(), stmtID, _sql, 0,
NULL);
175 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
182 res = PQdescribePrepared(conn()->connHandle(), stmtID);
183 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
190 && (Query::__paramCount == 0)
194 int nparams = PQnparams(res);
198#if defined(__DCL_DEBUG) && __TRACE_THIS && 0
199 for (
int i = 0; i < nparams; i++) {
200 Oid oid = PQparamtype(res, i);
206 size_t alloc = nparams * (
sizeof(
char*) +
sizeof(
int) * 3);
207 __DCL_TRACE2_N(L
"__inBINDs nparams[%2d] * (8+4+4+4=20) alloc[%zd]\n",
216#define __BIND_VALUES ((const char**)((char*)__inBINDs))
217#define __BIND_TYPES ((Oid*)((char*)__inBINDs \
218 + (Query::__paramCount * (sizeof(char*) + sizeof(int) * 0))))
219#define __BIND_LENGTHS ((int*)((char*)__inBINDs \
220 + (Query::__paramCount * (sizeof(char*) + sizeof(int) * 1))))
221#define __BIND_FORMATS ((int*)((char*)__inBINDs \
222 + (Query::__paramCount * (sizeof(char*) + sizeof(int) * 2))))
224#define __SET_BIND_TYPE(_index, _type) __BIND_TYPES[_index] = _type
225#define __SET_BIND_VALUE(_index, _value) __BIND_VALUES[_index] = _value
226#define __SET_BIND_LENGTH(_index, _length) __BIND_LENGTHS[_index] = _length
227#define __SET_BIND_FORMAT(_index, _format) __BIND_FORMATS[_index] = _format
229 Query::__paramCount = nparams;
236 for (
size_t i = 0; i < Query::__paramCount; i++) {
237 if (!
__params[i].init(
this, (
int)i, res))
260 ByteString cursorID = ByteString::format(
"curs_%d",
__stmtNo);
261 ByteStringBuilder cmd(
__query.length() + 20);
262 cmd.assign(
"DECLARE ").append(cursorID).append(
" CURSOR FOR ").append(
__query);
263 PGresult* res = PQexecParams(conn()->connHandle(), cmd,
268 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
275 Query::__eof =
false;
277 res = PQdescribePortal(conn()->connHandle(), cursorID);
278 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
283#if defined(__DCL_DEBUG) && __TRACE_THIS && 0
284 for (
int i = 0; i < PQnfields(res); i++) {
285 Oid oid = PQftype(res, i);
287 PQfsize(res, i), PQfmod(res, i), PQfformat(res, i),
294 && (Query::__fieldCount == 0)
303 for (
size_t i = 0; i < Query::__fieldCount; i++) {
304 if (!
__fields[i].init(
this, (
int)i, res))
311#if defined(__DCL_DEBUG) && __TRACE_THIS && 0
312 for (
size_t i = 0; i < Query::__paramCount; i++) {
320 PGresult* res = PQexecPrepared(conn()->connHandle(),
__stmtID,
321 (
int)Query::__paramCount,
325 switch (PQresultStatus(res)) {
326 case PGRES_COMMAND_OK: {
328 char* psz = PQcmdTuples(res);
330 Query::__affectedRows = strtoll(psz,
NULL, 10);
335 case PGRES_TUPLES_OK: {
336 if (
__nfields != (
int)Query::__fieldCount) {
339 && (Query::__fieldCount == 0)
348 for (
size_t i = 0; i < Query::__fieldCount; i++) {
349 if (!
__fields[i].init(
this, (
int)i, res))
356 Query::__eof =
false;
367 for(
size_t i = 0; i < Query::__paramCount; i++) {
373 if (!(
__params[i].onAfterExecute()))
389 ByteStringBuilder cmd;
390 cmd.assign(
"FETCH ").append(conn()->cursor() == 0 ?
391 "NEXT" : ByteString::valueOf(conn()->cursor()))
394 PGresult* res = PQexecParams(conn()->connHandle(), cmd,
397 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
419 PGresult* res = PQexec(conn()->connHandle(), cmd);
420 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
425#ifdef LIBPQ_HAS_CLOSE_PREPARED
426 res = PQclosePortal(conn()->connHandle(),
__cursorID);
427 if (
r && PQresultStatus(res) != PGRES_COMMAND_OK) {
440bool PqQuery::getValue(
int _fieldIndex,
const char*& _value,
int& _length)
const
446 _value = PQgetvalue(
__res,
__row, _fieldIndex);
447 _length = PQgetlength(
__res,
__row, _fieldIndex);
451bool PqQuery::setValue(
int _paramIndex, Oid _type,
const void* _value,
unsigned _length,
int _format)
464 __DCL_ASSERT((0 <= _index) && (_index < Query::__fieldCount));
465 *_fieldHandleOut = &
__fields[_index];
472 __DCL_ASSERT((0 <= _index) && (_index < Query::__paramCount));
473 *_paramHandleOut = &
__params[_index];
#define __DCL_TRACE1_N(fmt, arg)
#define __DCL_TRACE2_N(fmt, arg1, arg2)
#define __DCL_TRACE3_N(fmt, arg1, arg2, arg3)
#define __DCL_TRACE4_N(fmt, arg1, arg2, arg3, arg4)
#define __SET_ERROR_MSG(_message)
const wchar_t * __dataTypeName(const ifx_sqlvar_t *_sqlvar)
#define __DCL_TRACE6_N(fmt, arg1, arg2, arg3, arg4, arg5, arg6)
#define __DCL_TRACE1(fmt, arg1)
#define __DCL_ASSERT(expr)
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
#define __SET_BIND_LENGTH(_index, _length)
#define __SET_BIND_VALUE(_index, _value)
#define __SET_BIND_FORMAT(_index, _format)
#define __SET_BIND_TYPE(_index, _type)
#define __CURSOR_ENABLED(_conn)
#define __SET_ERROR(_errorCode)
virtual bool __getParam(size_t _index, SQL::Param **_paramHandleOut)
PqQuery(PqConnection *pConnection)
virtual bool __prepare(const char *_sql, size_t _sqllen, size_t _paramCount)
virtual bool __getField(size_t _index, SQL::Field **_fieldHandleOut)