39 "PGRES_NONFATAL_ERROR",
43 "PGRES_PIPELINE_SYNC",
44 "PGRES_PIPELINE_ABORTED",
52 .errMsg =
"out of memory\n",
65 const char **errmsgp);
72 const Oid *paramTypes,
73 const char *
const *paramValues,
74 const int *paramLengths,
75 const int *paramFormats,
142#define PGRESULT_DATA_BLOCKSIZE 2048
143#define PGRESULT_ALIGN_BOUNDARY MAXIMUM_ALIGNOF
144#define PGRESULT_BLOCK_OVERHEAD Max(sizeof(PGresult_data), PGRESULT_ALIGN_BOUNDARY)
145#define PGRESULT_SEP_ALLOC_THRESHOLD (PGRESULT_DATA_BLOCKSIZE / 2)
262 if (numAttributes <= 0 || !attDescs)
350 for (tup = 0; tup < src->
ntups; tup++)
383 for (
i = 0;
i <
dest->nEvents;
i++)
393 dest->events[
i].passThrough))
394 dest->events[
i].resultInitialized =
true;
414 if (!events || count <= 0)
417 msize = count *
sizeof(
PGEvent);
422 for (
i = 0;
i < count;
i++)
426 newEvents[
i].
data = NULL;
429 if (!newEvents[
i].
name)
436 msize += strlen(events[
i].
name) + 1;
455 const char *
errmsg = NULL;
466 if (tup_num < 0 || tup_num > res->
ntups)
469 "row number %d is out of range 0..%d",
470 tup_num, res->
ntups);
475 if (tup_num == res->
ntups)
499 attval = &res->
tuples[tup_num][field_num];
756 while ((block = res->
curBlock) != NULL)
956 msgBuf[
sizeof(msgBuf) - 1] =
'\0';
1026 *errmsgp =
libpq_gettext(
"PGresult cannot support more than INT_MAX tuples");
1035#if INT_MAX >= (SIZE_MAX / 2)
1076 pfield->
code = code;
1100 prev = pstatus, pstatus = pstatus->
next)
1102 if (strcmp(pstatus->
name,
name) == 0)
1123 pstatus->
name = ptr;
1125 ptr += strlen(
name) + 1;
1126 pstatus->
value = ptr;
1144 if (strcmp(
name,
"client_encoding") == 0)
1152 else if (strcmp(
name,
"standard_conforming_strings") == 0)
1157 else if (strcmp(
name,
"server_version") == 0)
1165 cnt = sscanf(
value,
"%d.%d.%d", &vmaj, &vmin, &vrev);
1193 else if (strcmp(
name,
"default_transaction_read_only") == 0)
1198 else if (strcmp(
name,
"in_hot_standby") == 0)
1203 else if (strcmp(
name,
"scram_iterations") == 0)
1267 for (
i = 0;
i < nfields;
i++)
1269 int clen = columns[
i].
len;
1341 entry->
query = NULL;
1413 entry->
query = NULL;
1483 entry->
query = strdup(query);
1509 const char *command,
1511 const Oid *paramTypes,
1512 const char *
const *paramValues,
1513 const int *paramLengths,
1514 const int *paramFormats,
1553 const char *stmtName,
const char *query,
1554 int nParams,
const Oid *paramTypes)
1589 if (nParams > 0 && paramTypes)
1595 for (
i = 0;
i < nParams;
i++)
1622 entry->
query = strdup(query);
1650 const char *stmtName,
1652 const char *
const *paramValues,
1653 const int *paramLengths,
1654 const int *paramFormats,
1774 const char *command,
1775 const char *stmtName,
1777 const Oid *paramTypes,
1778 const char *
const *paramValues,
1779 const int *paramLengths,
1780 const int *paramFormats,
1803 if (nParams > 0 && paramTypes)
1807 for (
i = 0;
i < nParams;
i++)
1829 if (nParams > 0 && paramFormats)
1833 for (
i = 0;
i < nParams;
i++)
1849 for (
i = 0;
i < nParams;
i++)
1851 if (paramValues && paramValues[
i])
1855 if (paramFormats && paramFormats[
i] != 0)
1859 nbytes = paramLengths[
i];
1869 nbytes = strlen(paramValues[
i]);
1916 entry->
query = strdup(command);
2293 const char *command,
2295 const Oid *paramTypes,
2296 const char *
const *paramValues,
2297 const int *paramLengths,
2298 const int *paramFormats,
2304 nParams, paramTypes, paramValues, paramLengths,
2305 paramFormats, resultFormat))
2323 const char *stmtName,
const char *query,
2324 int nParams,
const Oid *paramTypes)
2340 const char *stmtName,
2342 const char *
const *paramValues,
2343 const int *paramLengths,
2344 const int *paramFormats,
2350 nParams, paramValues, paramLengths,
2351 paramFormats, resultFormat))
2446 lastResult = result;
2872 if (!buffer || length <= 0)
3031 result_buf, result_len,
3187 prevquery->
next = NULL;
3330 "internal error: cannot send pipeline while in COPY\n");
3346 entry->
query = NULL;
3359 if (immediate_flush)
3445 if (!res || !res->
errMsg)
3465 return strdup(
libpq_gettext(
"PGresult is not an error result\n"));
3478 return workBuf.
data;
3488 for (pfield = res->
errFields; pfield != NULL; pfield = pfield->
next)
3490 if (pfield->
code == fieldcode)
3533 "column number %d is out of range 0..%d",
3542 int tup_num,
int field_num)
3546 if (tup_num < 0 || tup_num >= res->
ntups)
3549 "row number %d is out of range 0..%d",
3550 tup_num, res->
ntups - 1);
3556 "column number %d is out of range 0..%d",
3571 "parameter number %d is out of range 0..%d",
3609 bool all_lower =
true;
3621 if (field_name == NULL ||
3622 field_name[0] ==
'\0' ||
3630 for (iptr = field_name; *iptr; iptr++)
3653 field_case = strdup(field_name);
3654 if (field_case == NULL)
3659 for (iptr = field_case; *iptr; iptr++)
3787 static char buf[24];
3791 if (!res || strncmp(res->
cmdStatus,
"INSERT ", 7) != 0)
3795 if (
len >
sizeof(
buf) - 1)
3811 char *endptr = NULL;
3812 unsigned long result;
3815 strncmp(res->
cmdStatus,
"INSERT ", 7) != 0 ||
3820 result = strtoul(res->
cmdStatus + 7, &endptr, 10);
3822 if (!endptr || (*endptr !=
' ' && *endptr !=
'\0'))
3825 return (
Oid) result;
3846 if (strncmp(res->
cmdStatus,
"INSERT ", 7) == 0)
3850 while (*p && *p !=
' ')
3853 goto interpret_error;
3856 else if (strncmp(res->
cmdStatus,
"SELECT ", 7) == 0 ||
3857 strncmp(res->
cmdStatus,
"DELETE ", 7) == 0 ||
3858 strncmp(res->
cmdStatus,
"UPDATE ", 7) == 0)
3860 else if (strncmp(res->
cmdStatus,
"FETCH ", 6) == 0 ||
3861 strncmp(res->
cmdStatus,
"MERGE ", 6) == 0)
3863 else if (strncmp(res->
cmdStatus,
"MOVE ", 5) == 0 ||
3864 strncmp(res->
cmdStatus,
"COPY ", 5) == 0)
3870 for (
c = p; *
c;
c++)
3872 if (!isdigit((
unsigned char) *
c))
3873 goto interpret_error;
3876 goto interpret_error;
3882 "could not interpret result from server: %s",
3908 return res->
tuples[tup_num][field_num].
len;
4088 char *to,
const char *from,
size_t length,
4092 const char *
source = from;
4095 bool already_complained =
false;
4154 if (
conn && !already_complained)
4161 already_complained =
true;
4178 for (
i = 0;
i < charlen;
i++)
4194 char *to,
const char *from,
size_t length,
4236 if (result <
s1 || result <
s2)
4256 size_t num_quotes = 0;
4257 size_t num_backslashes = 0;
4260 char quote_char = as_ident ?
'"' :
'\'';
4261 bool validated_mb =
false;
4277 if (*s == quote_char)
4279 else if (*s ==
'\\')
4314 validated_mb =
true;
4331 if (!as_ident && num_backslashes > 0)
4338 result = rp = (
char *)
malloc(result_size);
4352 if (!as_ident && num_backslashes > 0)
4372 if (num_quotes == 0 && (num_backslashes == 0 || as_ident))
4374 memcpy(rp,
str, input_len);
4382 if (*s == quote_char || (!as_ident && *s ==
'\\'))
4413 "escaped string size exceeds the maximum allowed (%zu)",
4431static const char hextbl[] =
"0123456789abcdef";
4434 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4435 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4436 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4437 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
4438 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4439 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4440 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4441 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4449 if (
c > 0 &&
c < 127)
4471static unsigned char *
4473 const unsigned char *from,
size_t from_length,
4474 size_t *to_length,
bool std_strings,
bool use_hex)
4476 const unsigned char *vp;
4478 unsigned char *result;
4481 const size_t bslash_len = (std_strings ? 1 : 2);
4504 for (
i = from_length;
i > 0;
i--, vp++)
4506 if (*vp < 0x20 || *vp > 0x7e)
4511 else if (*vp ==
'\'')
4516 else if (*vp ==
'\\')
4530 rp = result = (
unsigned char *)
malloc(
len);
4547 for (
i = from_length;
i > 0;
i--, vp++)
4549 unsigned char c = *vp;
4553 *rp++ =
hextbl[(
c >> 4) & 0xF];
4561 *rp++ = (
c >> 6) +
'0';
4562 *rp++ = ((
c >> 3) & 07) +
'0';
4563 *rp++ = (
c & 07) +
'0';
4590 "escaped bytea size exceeds the maximum allowed (%zu)",
4597 const unsigned char *from,
size_t from_length,
4620#define ISFIRSTOCTDIGIT(CH) ((CH) >= '0' && (CH) <= '3')
4621#define ISOCTDIGIT(CH) ((CH) >= '0' && (CH) <= '7')
4622#define OCTVAL(CH) ((CH) - '0')
4641 unsigned char *buffer,
4646 if (strtext == NULL)
4649 strtextlen = strlen((
const char *) strtext);
4651 if (strtext[0] ==
'\\' && strtext[1] ==
'x')
4653 const unsigned char *s;
4656 buflen = (strtextlen - 2) / 2;
4658 buffer = (
unsigned char *)
malloc(buflen > 0 ? buflen : 1);
4674 if (!*s || v1 == (
char) -1)
4677 if (v2 != (
char) -1)
4678 *p++ = (v1 << 4) | v2;
4681 buflen = p - buffer;
4689 buffer = (
unsigned char *)
malloc(strtextlen + 1);
4693 for (
i =
j = 0;
i < strtextlen;)
4699 if (strtext[
i] ==
'\\')
4700 buffer[
j++] = strtext[
i++];
4710 byte = (
byte << 3) +
OCTVAL(strtext[
i++]);
4711 byte = (
byte << 3) +
OCTVAL(strtext[
i++]);
4726 buffer[
j++] = strtext[
i++];
4744 *retbuflen = buflen;
#define unconstify(underlying_type, expr)
#define IS_HIGHBIT_SET(ch)
#define SQL_STR_DOUBLE(ch, escape_backslash)
int errmsg(const char *fmt,...)
static int PQsendQueryInternal(PGconn *conn, const char *query, bool newQuery)
int PQsendQueryParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
int PQsendQueryContinue(PGconn *conn, const char *query)
int PQgetlength(const PGresult *res, int tup_num, int field_num)
int PQsetSingleRowMode(PGconn *conn)
static char get_hex(char c)
int PQbinaryTuples(const PGresult *res)
int PQflush(PGconn *conn)
PGresult * PQcopyResult(const PGresult *src, int flags)
void PQfreemem(void *ptr)
static unsigned char * PQescapeByteaInternal(PGconn *conn, const unsigned char *from, size_t from_length, size_t *to_length, bool std_strings, bool use_hex)
int PQgetline(PGconn *conn, char *buffer, int length)
void * pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary)
static bool canChangeResultMode(PGconn *conn)
static size_t PQescapeStringInternal(PGconn *conn, char *to, const char *from, size_t length, int *error, int encoding, bool std_strings)
Oid PQftype(const PGresult *res, int field_num)
char * PQresultVerboseErrorMessage(const PGresult *res, PGVerbosity verbosity, PGContextVisibility show_context)
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
PGresult * PQexecParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
int PQexitPipelineMode(PGconn *conn)
PGresult * PQexecPrepared(PGconn *conn, const char *stmtName, int nParams, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
int PQsendClosePortal(PGconn *conn, const char *portal)
static PGcmdQueueEntry * pqAllocCmdQueueEntry(PGconn *conn)
int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs)
unsigned char * PQunescapeBytea(const unsigned char *strtext, size_t *retbuflen)
void pqSaveMessageField(PGresult *res, char code, const char *value)
static int check_tuple_field_number(const PGresult *res, int tup_num, int field_num)
PGresult * pqPrepareAsyncResult(PGconn *conn)
static void pqSaveWriteError(PGconn *conn)
int PQenterPipelineMode(PGconn *conn)
void pqCommandQueueAdvance(PGconn *conn, bool isReadyForQuery, bool gotSync)
void pqSetResultError(PGresult *res, PQExpBuffer errorMessage, int offset)
size_t PQescapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error)
void pqSaveErrorResult(PGconn *conn)
PGresult * PQclosePrepared(PGconn *conn, const char *stmt)
char *const pgresStatus[]
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
#define PGRESULT_DATA_BLOCKSIZE
PGresult * PQclosePortal(PGconn *conn, const char *portal)
static int pqPipelineSyncInternal(PGconn *conn, bool immediate_flush)
PGresult * PQgetResult(PGconn *conn)
ExecStatusType PQresultStatus(const PGresult *res)
Oid PQparamtype(const PGresult *res, int param_num)
static int check_param_number(const PGresult *res, int param_num)
int pqRowProcessor(PGconn *conn, const char **errmsgp)
int PQnparams(const PGresult *res)
void PQclear(PGresult *res)
int PQsendClosePrepared(PGconn *conn, const char *stmt)
char * PQcmdTuples(PGresult *res)
static PGresult * PQexecFinish(PGconn *conn)
int PQfformat(const PGresult *res, int field_num)
static void pqAppendCmdQueueEntry(PGconn *conn, PGcmdQueueEntry *entry)
static int PQsendQueryGuts(PGconn *conn, const char *command, const char *stmtName, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
int PQendcopy(PGconn *conn)
static int pqPipelineFlush(PGconn *conn)
int PQputCopyEnd(PGconn *conn, const char *errormsg)
static int PQsendTypedCommand(PGconn *conn, char command, char type, const char *target)
int PQsendPipelineSync(PGconn *conn)
int PQntuples(const PGresult *res)
int PQputnbytes(PGconn *conn, const char *buffer, int nbytes)
int PQputline(PGconn *conn, const char *string)
int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize)
static const PGresult OOM_result
#define PGRESULT_BLOCK_OVERHEAD
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
static PGresult * getCopyResult(PGconn *conn, ExecStatusType copytype)
PGresult * PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status)
static PGEvent * dupEvents(PGEvent *events, int count, size_t *memSize)
PGresult * PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
static bool static_std_strings
char * PQresultErrorMessage(const PGresult *res)
void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...)
int PQsendDescribePrepared(PGconn *conn, const char *stmt)
char * PQfname(const PGresult *res, int field_num)
static const char hextbl[]
static bool PQexecStart(PGconn *conn)
size_t PQescapeString(char *to, const char *from, size_t length)
int PQconsumeInput(PGconn *conn)
static char * PQescapeInternal(PGconn *conn, const char *str, size_t len, bool as_ident)
#define ISFIRSTOCTDIGIT(CH)
static void parseInput(PGconn *conn)
Oid PQftable(const PGresult *res, int field_num)
int PQfnumber(const PGresult *res, const char *field_name)
unsigned char * PQescapeBytea(const unsigned char *from, size_t from_length, size_t *to_length)
int PQsetnonblocking(PGconn *conn, int arg)
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
PGresult * PQfn(PGconn *conn, int fnid, int *result_buf, int *result_len, int result_is_int, const PQArgBlock *args, int nargs)
int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
#define PGRESULT_SEP_ALLOC_THRESHOLD
PGresult * PQdescribePortal(PGconn *conn, const char *portal)
int PQfmod(const PGresult *res, int field_num)
void pqClearAsyncResult(PGconn *conn)
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
int PQftablecol(const PGresult *res, int field_num)
int PQsetChunkedRowsMode(PGconn *conn, int chunkSize)
static int static_client_encoding
char * PQresultErrorField(const PGresult *res, int fieldcode)
int PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len)
int PQsendQuery(PGconn *conn, const char *query)
char * PQcmdStatus(PGresult *res)
int PQpipelineSync(PGconn *conn)
char * PQoidStatus(const PGresult *res)
int PQsendDescribePortal(PGconn *conn, const char *portal)
char * PQresStatus(ExecStatusType status)
size_t PQresultMemorySize(const PGresult *res)
void * PQresultAlloc(PGresult *res, size_t nBytes)
int PQisBusy(PGconn *conn)
PGresult * PQexec(PGconn *conn, const char *query)
unsigned char * PQescapeByteaConn(PGconn *conn, const unsigned char *from, size_t from_length, size_t *to_length)
int PQsendQueryPrepared(PGconn *conn, const char *stmtName, int nParams, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
static void pqPipelineProcessQueue(PGconn *conn)
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
int PQsendFlushRequest(PGconn *conn)
int pqSaveParameterStatus(PGconn *conn, const char *name, const char *value)
int PQisnonblocking(const PGconn *conn)
char * pqResultStrdup(PGresult *res, const char *str)
Oid PQoidValue(const PGresult *res)
static bool pqAddTuple(PGresult *res, PGresAttValue *tup, const char **errmsgp)
#define PGRESULT_ALIGN_BOUNDARY
int PQnfields(const PGresult *res)
static int check_field_number(const PGresult *res, int field_num)
int PQfsize(const PGresult *res, int field_num)
PGnotify * PQnotifies(PGconn *conn)
static bool PQsendQueryStart(PGconn *conn, bool newQuery)
static const int8 hexlookup[128]
static bool add_size_overflow(size_t s1, size_t s2, size_t *dst)
int PQgetCopyData(PGconn *conn, char **buffer, int async)
static void pqRecycleCmdQueueEntry(PGconn *conn, PGcmdQueueEntry *entry)
void PQfreeNotify(PGnotify *notify)
int pqPutc(char c, PGconn *conn)
int pqReadData(PGconn *conn)
int pqPutInt(int value, size_t bytes, PGconn *conn)
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
int pqFlush(PGconn *conn)
int pqPutMsgStart(char msg_type, PGconn *conn)
int pqWait(int forRead, int forWrite, PGconn *conn)
int pqPutnchar(const void *s, size_t len, PGconn *conn)
int pqPuts(const char *s, PGconn *conn)
int pqPutMsgEnd(PGconn *conn)
void pqBuildErrorMessage3(PQExpBuffer msg, const PGresult *res, PGVerbosity verbosity, PGContextVisibility show_context)
void pqParseInput3(PGconn *conn)
int pqEndcopy3(PGconn *conn)
PGresult * pqFunctionCall3(PGconn *conn, Oid fnid, int *result_buf, int *actual_result_len, int result_is_int, const PQArgBlock *args, int nargs)
int pqGetlineAsync3(PGconn *conn, char *buffer, int bufsize)
int pqGetCopyData3(PGconn *conn, char **buffer, int async)
int pqGetline3(PGconn *conn, char *s, int maxlen)
Assert(PointerIsAligned(start, uint64))
if(TABLE==NULL||TABLE_index==NULL)
int PQfireResultCreateEvents(PGconn *conn, PGresult *res)
#define PG_COPYRES_TUPLES
struct pg_result PGresult
#define PG_COPYRES_EVENTS
#define PG_COPYRES_NOTICEHOOKS
#define PQ_QUERY_PARAM_MAX_LIMIT
struct pgParameterStatus pgParameterStatus
#define pqClearConnErrorState(conn)
union pgresult_data PGresult_data
#define pqIsnonblocking(conn)
#define OUTBUFFER_THRESHOLD
#define pgHavePendingResult(conn)
void libpq_append_conn_error(PGconn *conn, const char *fmt,...)
static rewind_source * source
#define pg_char_to_encoding
unsigned char pg_tolower(unsigned char ch)
size_t strnlen(const char *str, size_t maxlen)
#define PG_DIAG_SEVERITY_NONLOCALIZED
#define PG_DIAG_MESSAGE_PRIMARY
void initPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void termPQExpBuffer(PQExpBuffer str)
#define PQExpBufferBroken(str)
#define PQExpBufferDataBroken(buf)
PQnoticeReceiver noticeRec
PQnoticeProcessor noticeProc
struct PGcmdQueueEntry * next
struct pgMessageField * next
char contents[FLEXIBLE_ARRAY_MEMBER]
struct pgParameterStatus * next
PGTernaryBool in_hot_standby
PGcmdQueueEntry * cmd_queue_recycle
PGcmdQueueEntry * cmd_queue_tail
PGTernaryBool default_transaction_read_only
pgParameterStatus * pstatus
PQExpBufferData errorMessage
PGAsyncStatusType asyncStatus
int scram_sha_256_iterations
PGpipelineStatus pipelineStatus
PGNoticeHooks noticeHooks
PGcmdQueueEntry * cmd_queue_head
PGNoticeHooks noticeHooks
char cmdStatus[CMDSTATUS_LEN]
PGMessageField * errFields
PGresParamDesc * paramDescs
ExecStatusType resultStatus
static StringInfoData tmpbuf
void pg_encoding_set_invalid(int encoding, char *dst)
int pg_encoding_mblen_or_incomplete(int encoding, const char *mbstr, size_t remaining)
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
int pg_encoding_mblen(int encoding, const char *mbstr)
int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len)