1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* fe-exec.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* functions related to sending a query down to the backend
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-06-20 22:29:54 +02:00
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2003-05-26 22:05:20 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.136 2003/05/26 20:05:20 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2001-02-10 03:31:31 +01:00
|
|
|
#include "postgres_fe.h"
|
|
|
|
|
1999-07-19 04:27:16 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
2000-01-18 07:09:24 +01:00
|
|
|
#include <fcntl.h>
|
1999-07-19 04:27:16 +02:00
|
|
|
|
1998-08-17 05:50:43 +02:00
|
|
|
#include "libpq-fe.h"
|
|
|
|
#include "libpq-int.h"
|
|
|
|
|
2003-04-25 21:45:10 +02:00
|
|
|
#include "mb/pg_wchar.h"
|
|
|
|
|
1998-07-03 06:24:16 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
#include "win32.h"
|
1999-07-19 08:25:40 +02:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
1998-07-03 06:24:16 +02:00
|
|
|
#endif
|
1997-11-10 06:10:50 +01:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* keep this in same order as ExecStatusType in libpq-fe.h */
|
2000-04-12 19:17:23 +02:00
|
|
|
char *const pgresStatus[] = {
|
1997-09-07 07:04:48 +02:00
|
|
|
"PGRES_EMPTY_QUERY",
|
|
|
|
"PGRES_COMMAND_OK",
|
|
|
|
"PGRES_TUPLES_OK",
|
1998-09-01 06:40:42 +02:00
|
|
|
"PGRES_COPY_OUT",
|
1998-03-15 09:11:11 +01:00
|
|
|
"PGRES_COPY_IN",
|
1997-09-07 07:04:48 +02:00
|
|
|
"PGRES_BAD_RESPONSE",
|
|
|
|
"PGRES_NONFATAL_ERROR",
|
|
|
|
"PGRES_FATAL_ERROR"
|
1996-07-09 08:22:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-03-11 04:08:37 +01:00
|
|
|
/* Note: DONOTICE macro will work if applied to either PGconn or PGresult */
|
1998-08-09 04:59:33 +02:00
|
|
|
#define DONOTICE(conn,message) \
|
|
|
|
((*(conn)->noticeHook) ((conn)->noticeArg, (message)))
|
|
|
|
|
|
|
|
|
1999-08-31 03:37:37 +02:00
|
|
|
static void pqCatenateResultError(PGresult *res, const char *msg);
|
|
|
|
static void saveErrorResult(PGconn *conn);
|
|
|
|
static PGresult *prepareAsyncResult(PGconn *conn);
|
2000-04-12 19:17:23 +02:00
|
|
|
static int addTuple(PGresult *res, PGresAttValue * tup);
|
1998-05-07 01:51:16 +02:00
|
|
|
static void parseInput(PGconn *conn);
|
1999-05-28 03:54:53 +02:00
|
|
|
static void handleSendFailure(PGconn *conn);
|
2003-04-22 02:08:07 +02:00
|
|
|
static void handleSyncLoss(PGconn *conn, char id, int msgLength);
|
1998-09-01 06:40:42 +02:00
|
|
|
static int getRowDescriptions(PGconn *conn);
|
2003-05-26 22:05:20 +02:00
|
|
|
static int getAnotherTuple(PGconn *conn, int msgLength);
|
2003-04-25 21:45:10 +02:00
|
|
|
static int getParameterStatus(PGconn *conn);
|
1998-09-01 06:40:42 +02:00
|
|
|
static int getNotify(PGconn *conn);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-03-22 04:29:06 +01:00
|
|
|
|
1998-11-18 01:47:28 +01:00
|
|
|
/* ----------------
|
|
|
|
* Space management for PGresult.
|
|
|
|
*
|
|
|
|
* Formerly, libpq did a separate malloc() for each field of each tuple
|
|
|
|
* returned by a query. This was remarkably expensive --- malloc/free
|
|
|
|
* consumed a sizable part of the application's runtime. And there is
|
|
|
|
* no real need to keep track of the fields separately, since they will
|
|
|
|
* all be freed together when the PGresult is released. So now, we grab
|
|
|
|
* large blocks of storage from malloc and allocate space for query data
|
|
|
|
* within these blocks, using a trivially simple allocator. This reduces
|
|
|
|
* the number of malloc/free calls dramatically, and it also avoids
|
|
|
|
* fragmentation of the malloc storage arena.
|
|
|
|
* The PGresult structure itself is still malloc'd separately. We could
|
|
|
|
* combine it with the first allocation block, but that would waste space
|
|
|
|
* for the common case that no extra storage is actually needed (that is,
|
|
|
|
* the SQL command did not return tuples).
|
2001-08-17 17:11:15 +02:00
|
|
|
*
|
1998-11-18 01:47:28 +01:00
|
|
|
* We also malloc the top-level array of tuple pointers separately, because
|
|
|
|
* we need to be able to enlarge it via realloc, and our trivial space
|
|
|
|
* allocator doesn't handle that effectively. (Too bad the FE/BE protocol
|
|
|
|
* doesn't tell us up front how many tuples will be returned.)
|
|
|
|
* All other subsidiary storage for a PGresult is kept in PGresult_data blocks
|
|
|
|
* of size PGRESULT_DATA_BLOCKSIZE. The overhead at the start of each block
|
1999-05-25 18:15:34 +02:00
|
|
|
* is just a link to the next one, if any. Free-space management info is
|
1998-11-18 01:47:28 +01:00
|
|
|
* kept in the owning PGresult.
|
|
|
|
* A query returning a small amount of data will thus require three malloc
|
|
|
|
* calls: one for the PGresult, one for the tuples pointer array, and one
|
|
|
|
* PGresult_data block.
|
2001-08-17 17:11:15 +02:00
|
|
|
*
|
1998-11-18 01:47:28 +01:00
|
|
|
* Only the most recently allocated PGresult_data block is a candidate to
|
|
|
|
* have more stuff added to it --- any extra space left over in older blocks
|
|
|
|
* is wasted. We could be smarter and search the whole chain, but the point
|
|
|
|
* here is to be simple and fast. Typical applications do not keep a PGresult
|
|
|
|
* around very long anyway, so some wasted space within one is not a problem.
|
|
|
|
*
|
|
|
|
* Tuning constants for the space allocator are:
|
|
|
|
* PGRESULT_DATA_BLOCKSIZE: size of a standard allocation block, in bytes
|
|
|
|
* PGRESULT_ALIGN_BOUNDARY: assumed alignment requirement for binary data
|
|
|
|
* PGRESULT_SEP_ALLOC_THRESHOLD: objects bigger than this are given separate
|
1999-05-25 18:15:34 +02:00
|
|
|
* blocks, instead of being crammed into a regular allocation block.
|
1998-11-18 01:47:28 +01:00
|
|
|
* Requirements for correct function are:
|
|
|
|
* PGRESULT_ALIGN_BOUNDARY must be a multiple of the alignment requirements
|
1999-05-25 18:15:34 +02:00
|
|
|
* of all machine data types. (Currently this is set from configure
|
1999-05-12 06:38:24 +02:00
|
|
|
* tests, so it should be OK automatically.)
|
|
|
|
* PGRESULT_SEP_ALLOC_THRESHOLD + PGRESULT_BLOCK_OVERHEAD <=
|
1998-11-18 01:47:28 +01:00
|
|
|
* PGRESULT_DATA_BLOCKSIZE
|
|
|
|
* pqResultAlloc assumes an object smaller than the threshold will fit
|
|
|
|
* in a new block.
|
|
|
|
* The amount of space wasted at the end of a block could be as much as
|
|
|
|
* PGRESULT_SEP_ALLOC_THRESHOLD, so it doesn't pay to make that too large.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
|
1999-05-12 06:38:24 +02:00
|
|
|
#ifdef MAX
|
|
|
|
#undef MAX
|
|
|
|
#endif
|
|
|
|
#define MAX(a,b) ((a) > (b) ? (a) : (b))
|
|
|
|
|
1998-11-18 01:47:28 +01:00
|
|
|
#define PGRESULT_DATA_BLOCKSIZE 2048
|
1999-05-25 18:15:34 +02:00
|
|
|
#define PGRESULT_ALIGN_BOUNDARY MAXIMUM_ALIGNOF /* from configure */
|
1999-05-12 06:38:24 +02:00
|
|
|
#define PGRESULT_BLOCK_OVERHEAD MAX(sizeof(PGresult_data), PGRESULT_ALIGN_BOUNDARY)
|
1998-11-18 01:47:28 +01:00
|
|
|
#define PGRESULT_SEP_ALLOC_THRESHOLD (PGRESULT_DATA_BLOCKSIZE / 2)
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1998-09-03 04:10:56 +02:00
|
|
|
* PQmakeEmptyPGresult
|
1998-10-01 03:40:26 +02:00
|
|
|
* returns a newly allocated, initialized PGresult with given status.
|
|
|
|
* If conn is not NULL and status indicates an error, the conn's
|
|
|
|
* errorMessage is copied.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1998-09-03 04:10:56 +02:00
|
|
|
* Note this is exported --- you wouldn't think an application would need
|
|
|
|
* to build its own PGresults, but this has proven useful in both libpgtcl
|
|
|
|
* and the Perl5 interface, so maybe it's not so unreasonable.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
PGresult *
|
1998-09-03 04:10:56 +02:00
|
|
|
PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
PGresult *result;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
result = (PGresult *) malloc(sizeof(PGresult));
|
|
|
|
|
2000-03-11 04:08:37 +01:00
|
|
|
result->xconn = conn; /* might be NULL */
|
1997-09-07 07:04:48 +02:00
|
|
|
result->ntups = 0;
|
|
|
|
result->numAttributes = 0;
|
|
|
|
result->attDescs = NULL;
|
|
|
|
result->tuples = NULL;
|
|
|
|
result->tupArrSize = 0;
|
|
|
|
result->resultStatus = status;
|
|
|
|
result->cmdStatus[0] = '\0';
|
|
|
|
result->binary = 0;
|
1998-10-01 03:40:26 +02:00
|
|
|
result->errMsg = NULL;
|
2003-04-24 23:16:45 +02:00
|
|
|
result->errSeverity = NULL;
|
|
|
|
result->errCode = NULL;
|
|
|
|
result->errPrimary = NULL;
|
|
|
|
result->errDetail = NULL;
|
|
|
|
result->errHint = NULL;
|
|
|
|
result->errPosition = NULL;
|
|
|
|
result->errContext = NULL;
|
|
|
|
result->errFilename = NULL;
|
|
|
|
result->errLineno = NULL;
|
|
|
|
result->errFuncname = NULL;
|
1998-11-18 01:47:28 +01:00
|
|
|
result->null_field[0] = '\0';
|
|
|
|
result->curBlock = NULL;
|
|
|
|
result->curOffset = 0;
|
|
|
|
result->spaceLeft = 0;
|
|
|
|
|
2000-03-11 04:08:37 +01:00
|
|
|
if (conn)
|
1998-10-01 03:40:26 +02:00
|
|
|
{
|
2000-03-15 00:59:23 +01:00
|
|
|
/* copy connection data we might need for operations on PGresult */
|
2000-03-11 04:08:37 +01:00
|
|
|
result->noticeHook = conn->noticeHook;
|
|
|
|
result->noticeArg = conn->noticeArg;
|
2000-03-15 00:59:23 +01:00
|
|
|
result->client_encoding = conn->client_encoding;
|
|
|
|
|
2000-03-11 04:08:37 +01:00
|
|
|
/* consider copying conn's errorMessage */
|
1998-10-01 03:40:26 +02:00
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case PGRES_EMPTY_QUERY:
|
|
|
|
case PGRES_COMMAND_OK:
|
|
|
|
case PGRES_TUPLES_OK:
|
|
|
|
case PGRES_COPY_OUT:
|
|
|
|
case PGRES_COPY_IN:
|
|
|
|
/* non-error cases */
|
|
|
|
break;
|
|
|
|
default:
|
1999-08-31 03:37:37 +02:00
|
|
|
pqSetResultError(result, conn->errorMessage.data);
|
1998-10-01 03:40:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-03-11 04:08:37 +01:00
|
|
|
else
|
|
|
|
{
|
2000-03-15 00:59:23 +01:00
|
|
|
/* defaults... */
|
2000-03-11 04:08:37 +01:00
|
|
|
result->noticeHook = NULL;
|
|
|
|
result->noticeArg = NULL;
|
2000-04-12 19:17:23 +02:00
|
|
|
result->client_encoding = 0; /* should be SQL_ASCII */
|
2000-03-11 04:08:37 +01:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return result;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-11-18 01:47:28 +01:00
|
|
|
/*
|
|
|
|
* pqResultAlloc -
|
|
|
|
* Allocate subsidiary storage for a PGresult.
|
|
|
|
*
|
|
|
|
* nBytes is the amount of space needed for the object.
|
|
|
|
* If isBinary is true, we assume that we need to align the object on
|
|
|
|
* a machine allocation boundary.
|
|
|
|
* If isBinary is false, we assume the object is a char string and can
|
|
|
|
* be allocated on any byte boundary.
|
|
|
|
*/
|
|
|
|
void *
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary)
|
1998-11-18 01:47:28 +01:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
char *space;
|
|
|
|
PGresult_data *block;
|
1998-11-18 01:47:28 +01:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
if (!res)
|
1998-11-18 01:47:28 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (nBytes <= 0)
|
|
|
|
return res->null_field;
|
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
|
|
|
* If alignment is needed, round up the current position to an
|
1998-11-18 01:47:28 +01:00
|
|
|
* alignment boundary.
|
|
|
|
*/
|
|
|
|
if (isBinary)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
int offset = res->curOffset % PGRESULT_ALIGN_BOUNDARY;
|
|
|
|
|
1998-11-18 01:47:28 +01:00
|
|
|
if (offset)
|
|
|
|
{
|
|
|
|
res->curOffset += PGRESULT_ALIGN_BOUNDARY - offset;
|
|
|
|
res->spaceLeft -= PGRESULT_ALIGN_BOUNDARY - offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there's enough space in the current block, no problem. */
|
2001-10-25 07:50:21 +02:00
|
|
|
if (nBytes <= (size_t) res->spaceLeft)
|
1998-11-18 01:47:28 +01:00
|
|
|
{
|
|
|
|
space = res->curBlock->space + res->curOffset;
|
|
|
|
res->curOffset += nBytes;
|
|
|
|
res->spaceLeft -= nBytes;
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
|
|
|
* If the requested object is very large, give it its own block; this
|
|
|
|
* avoids wasting what might be most of the current block to start a
|
|
|
|
* new block. (We'd have to special-case requests bigger than the
|
|
|
|
* block size anyway.) The object is always given binary alignment in
|
|
|
|
* this case.
|
1998-11-18 01:47:28 +01:00
|
|
|
*/
|
|
|
|
if (nBytes >= PGRESULT_SEP_ALLOC_THRESHOLD)
|
|
|
|
{
|
1999-05-12 06:38:24 +02:00
|
|
|
block = (PGresult_data *) malloc(nBytes + PGRESULT_BLOCK_OVERHEAD);
|
1999-05-25 18:15:34 +02:00
|
|
|
if (!block)
|
1998-11-18 01:47:28 +01:00
|
|
|
return NULL;
|
1999-05-12 06:38:24 +02:00
|
|
|
space = block->space + PGRESULT_BLOCK_OVERHEAD;
|
1998-11-18 01:47:28 +01:00
|
|
|
if (res->curBlock)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
|
|
|
* Tuck special block below the active block, so that we don't
|
1998-11-18 01:47:28 +01:00
|
|
|
* have to waste the free space in the active block.
|
|
|
|
*/
|
|
|
|
block->next = res->curBlock->next;
|
|
|
|
res->curBlock->next = block;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Must set up the new block as the first active block. */
|
|
|
|
block->next = NULL;
|
|
|
|
res->curBlock = block;
|
1999-05-25 18:15:34 +02:00
|
|
|
res->spaceLeft = 0; /* be sure it's marked full */
|
1998-11-18 01:47:28 +01:00
|
|
|
}
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, start a new block. */
|
|
|
|
block = (PGresult_data *) malloc(PGRESULT_DATA_BLOCKSIZE);
|
1999-05-25 18:15:34 +02:00
|
|
|
if (!block)
|
1998-11-18 01:47:28 +01:00
|
|
|
return NULL;
|
|
|
|
block->next = res->curBlock;
|
|
|
|
res->curBlock = block;
|
|
|
|
if (isBinary)
|
|
|
|
{
|
|
|
|
/* object needs full alignment */
|
1999-05-12 06:38:24 +02:00
|
|
|
res->curOffset = PGRESULT_BLOCK_OVERHEAD;
|
|
|
|
res->spaceLeft = PGRESULT_DATA_BLOCKSIZE - PGRESULT_BLOCK_OVERHEAD;
|
1998-11-18 01:47:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we can cram it right after the overhead pointer */
|
|
|
|
res->curOffset = sizeof(PGresult_data);
|
|
|
|
res->spaceLeft = PGRESULT_DATA_BLOCKSIZE - sizeof(PGresult_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
space = block->space + res->curOffset;
|
|
|
|
res->curOffset += nBytes;
|
|
|
|
res->spaceLeft -= nBytes;
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pqResultStrdup -
|
|
|
|
* Like strdup, but the space is subsidiary PGresult space.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
pqResultStrdup(PGresult *res, const char *str)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
char *space = (char *) pqResultAlloc(res, strlen(str) + 1, FALSE);
|
|
|
|
|
1998-11-18 01:47:28 +01:00
|
|
|
if (space)
|
|
|
|
strcpy(space, str);
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
1998-10-01 03:40:26 +02:00
|
|
|
/*
|
|
|
|
* pqSetResultError -
|
|
|
|
* assign a new error message to a PGresult
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pqSetResultError(PGresult *res, const char *msg)
|
|
|
|
{
|
|
|
|
if (!res)
|
|
|
|
return;
|
|
|
|
if (msg && *msg)
|
1998-11-18 01:47:28 +01:00
|
|
|
res->errMsg = pqResultStrdup(res, msg);
|
|
|
|
else
|
|
|
|
res->errMsg = NULL;
|
1998-10-01 03:40:26 +02:00
|
|
|
}
|
|
|
|
|
1999-08-31 03:37:37 +02:00
|
|
|
/*
|
|
|
|
* pqCatenateResultError -
|
|
|
|
* concatenate a new error message to the one already in a PGresult
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pqCatenateResultError(PGresult *res, const char *msg)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
PQExpBufferData errorBuf;
|
1999-08-31 03:37:37 +02:00
|
|
|
|
|
|
|
if (!res || !msg)
|
|
|
|
return;
|
|
|
|
initPQExpBuffer(&errorBuf);
|
|
|
|
if (res->errMsg)
|
|
|
|
appendPQExpBufferStr(&errorBuf, res->errMsg);
|
|
|
|
appendPQExpBufferStr(&errorBuf, msg);
|
|
|
|
pqSetResultError(res, errorBuf.data);
|
|
|
|
termPQExpBuffer(&errorBuf);
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1998-05-07 01:51:16 +02:00
|
|
|
* PQclear -
|
|
|
|
* free's the memory associated with a PGresult
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-05-07 01:51:16 +02:00
|
|
|
void
|
|
|
|
PQclear(PGresult *res)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
PGresult_data *block;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
if (!res)
|
|
|
|
return;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-11-18 01:47:28 +01:00
|
|
|
/* Free all the subsidiary blocks */
|
1999-05-25 18:15:34 +02:00
|
|
|
while ((block = res->curBlock) != NULL)
|
|
|
|
{
|
1998-11-18 01:47:28 +01:00
|
|
|
res->curBlock = block->next;
|
|
|
|
free(block);
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
|
|
|
|
1998-11-18 01:47:28 +01:00
|
|
|
/* Free the top-level tuple pointer array */
|
|
|
|
if (res->tuples)
|
|
|
|
free(res->tuples);
|
1998-10-01 03:40:26 +02:00
|
|
|
|
1998-11-18 01:47:28 +01:00
|
|
|
/* Free the PGresult structure itself */
|
1998-05-07 01:51:16 +02:00
|
|
|
free(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handy subroutine to deallocate any partially constructed async result.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
void
|
1998-08-17 05:50:43 +02:00
|
|
|
pqClearAsyncResult(PGconn *conn)
|
1998-05-07 01:51:16 +02:00
|
|
|
{
|
|
|
|
if (conn->result)
|
|
|
|
PQclear(conn->result);
|
|
|
|
conn->result = NULL;
|
|
|
|
conn->curTuple = NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-08-31 03:37:37 +02:00
|
|
|
/*
|
|
|
|
* This subroutine deletes any existing async result, sets conn->result
|
|
|
|
* to a PGresult with status PGRES_FATAL_ERROR, and stores the current
|
|
|
|
* contents of conn->errorMessage into that result. It differs from a
|
|
|
|
* plain call on PQmakeEmptyPGresult() in that if there is already an
|
|
|
|
* async result with status PGRES_FATAL_ERROR, the current error message
|
|
|
|
* is APPENDED to the old error message instead of replacing it. This
|
|
|
|
* behavior lets us report multiple error conditions properly, if necessary.
|
|
|
|
* (An example where this is needed is when the backend sends an 'E' message
|
|
|
|
* and immediately closes the connection --- we want to report both the
|
|
|
|
* backend error and the connection closure error.)
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
saveErrorResult(PGconn *conn)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* If no old async result, just let PQmakeEmptyPGresult make one.
|
1999-08-31 03:37:37 +02:00
|
|
|
* Likewise if old result is not an error message.
|
|
|
|
*/
|
|
|
|
if (conn->result == NULL ||
|
|
|
|
conn->result->resultStatus != PGRES_FATAL_ERROR ||
|
|
|
|
conn->result->errMsg == NULL)
|
|
|
|
{
|
|
|
|
pqClearAsyncResult(conn);
|
|
|
|
conn->result = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Else, concatenate error message to existing async result. */
|
|
|
|
pqCatenateResultError(conn->result, conn->errorMessage.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This subroutine prepares an async result object for return to the caller.
|
|
|
|
* If there is not already an async result object, build an error object
|
|
|
|
* using whatever is in conn->errorMessage. In any case, clear the async
|
|
|
|
* result storage and make sure PQerrorMessage will agree with the result's
|
|
|
|
* error string.
|
|
|
|
*/
|
|
|
|
static PGresult *
|
|
|
|
prepareAsyncResult(PGconn *conn)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* conn->result is the PGresult to return. If it is NULL (which
|
|
|
|
* probably shouldn't happen) we assume there is an appropriate error
|
|
|
|
* message in conn->errorMessage.
|
1999-08-31 03:37:37 +02:00
|
|
|
*/
|
|
|
|
res = conn->result;
|
|
|
|
conn->result = NULL; /* handing over ownership to caller */
|
|
|
|
conn->curTuple = NULL; /* just in case */
|
|
|
|
if (!res)
|
|
|
|
res = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Make sure PQerrorMessage agrees with result; it could be
|
|
|
|
* different if we have concatenated messages.
|
1999-08-31 03:37:37 +02:00
|
|
|
*/
|
|
|
|
resetPQExpBuffer(&conn->errorMessage);
|
|
|
|
appendPQExpBufferStr(&conn->errorMessage,
|
|
|
|
PQresultErrorMessage(res));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* addTuple
|
1998-11-18 01:47:28 +01:00
|
|
|
* add a row pointer to the PGresult structure, growing it if necessary
|
1998-10-01 03:40:26 +02:00
|
|
|
* Returns TRUE if OK, FALSE if not enough memory to add the row
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-10-01 03:40:26 +02:00
|
|
|
static int
|
2000-04-12 19:17:23 +02:00
|
|
|
addTuple(PGresult *res, PGresAttValue * tup)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (res->ntups >= res->tupArrSize)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
/*
|
1998-10-01 03:40:26 +02:00
|
|
|
* Try to grow the array.
|
|
|
|
*
|
|
|
|
* We can use realloc because shallow copying of the structure is
|
1998-09-01 06:40:42 +02:00
|
|
|
* okay. Note that the first time through, res->tuples is NULL.
|
1998-11-29 02:53:54 +01:00
|
|
|
* While ANSI says that realloc() should act like malloc() in that
|
|
|
|
* case, some old C libraries (like SunOS 4.1.x) coredump instead.
|
|
|
|
* On failure realloc is supposed to return NULL without damaging
|
1999-05-25 18:15:34 +02:00
|
|
|
* the existing allocation. Note that the positions beyond
|
|
|
|
* res->ntups are garbage, not necessarily NULL.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
int newSize = (res->tupArrSize > 0) ? res->tupArrSize * 2 : 128;
|
|
|
|
PGresAttValue **newTuples;
|
|
|
|
|
1998-11-29 02:53:54 +01:00
|
|
|
if (res->tuples == NULL)
|
|
|
|
newTuples = (PGresAttValue **)
|
|
|
|
malloc(newSize * sizeof(PGresAttValue *));
|
|
|
|
else
|
|
|
|
newTuples = (PGresAttValue **)
|
|
|
|
realloc(res->tuples, newSize * sizeof(PGresAttValue *));
|
1999-05-25 18:15:34 +02:00
|
|
|
if (!newTuples)
|
1998-11-29 02:53:54 +01:00
|
|
|
return FALSE; /* malloc or realloc failed */
|
1998-10-01 03:40:26 +02:00
|
|
|
res->tupArrSize = newSize;
|
|
|
|
res->tuples = newTuples;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
res->tuples[res->ntups] = tup;
|
|
|
|
res->ntups++;
|
1998-10-01 03:40:26 +02:00
|
|
|
return TRUE;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1998-05-07 01:51:16 +02:00
|
|
|
* PQsendQuery
|
1998-09-01 06:40:42 +02:00
|
|
|
* Submit a query, but don't wait for it to finish
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1998-05-07 01:51:16 +02:00
|
|
|
* Returns: 1 if successfully submitted
|
1998-09-01 06:40:42 +02:00
|
|
|
* 0 if error (conn->errorMessage is set)
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-05-07 01:51:16 +02:00
|
|
|
int
|
|
|
|
PQsendQuery(PGconn *conn, const char *query)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (!conn)
|
|
|
|
return 0;
|
1999-08-31 03:37:37 +02:00
|
|
|
|
|
|
|
/* clear the error string */
|
|
|
|
resetPQExpBuffer(&conn->errorMessage);
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
if (!query)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-08-31 03:37:37 +02:00
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
2001-10-25 07:50:21 +02:00
|
|
|
libpq_gettext("command string is a null pointer\n"));
|
1998-08-09 04:59:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Don't try to send if we know there's no live connection. */
|
|
|
|
if (conn->status != CONNECTION_OK)
|
|
|
|
{
|
1999-08-31 03:37:37 +02:00
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
2001-07-15 15:45:04 +02:00
|
|
|
libpq_gettext("no connection to the server\n"));
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
1998-08-17 05:50:43 +02:00
|
|
|
/* Can't send while already busy, either. */
|
|
|
|
if (conn->asyncStatus != PGASYNC_IDLE)
|
|
|
|
{
|
1999-08-31 03:37:37 +02:00
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
2001-10-25 07:50:21 +02:00
|
|
|
libpq_gettext("another command is already in progress\n"));
|
1998-08-17 05:50:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-08-09 04:59:33 +02:00
|
|
|
/* initialize async result-accumulation state */
|
|
|
|
conn->result = NULL;
|
|
|
|
conn->curTuple = NULL;
|
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
/* construct the outgoing Query message */
|
|
|
|
if (pqPutMsgStart('Q', conn) < 0 ||
|
|
|
|
pqPuts(query, conn) < 0 ||
|
|
|
|
pqPutMsgEnd(conn) < 0)
|
|
|
|
{
|
|
|
|
handleSendFailure(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-01-18 07:09:24 +01:00
|
|
|
|
|
|
|
/*
|
2003-04-19 02:02:30 +02:00
|
|
|
* Give the data a push. In nonblock mode, don't complain if we're
|
|
|
|
* unable to send it all; PQconsumeInput() will do any additional flushing
|
|
|
|
* needed.
|
2000-01-18 07:09:24 +01:00
|
|
|
*/
|
2003-04-19 02:02:30 +02:00
|
|
|
if (pqFlush(conn) < 0)
|
1999-05-28 03:54:53 +02:00
|
|
|
{
|
2003-04-19 02:02:30 +02:00
|
|
|
handleSendFailure(conn);
|
|
|
|
return 0;
|
1999-05-28 03:54:53 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* OK, it's launched! */
|
|
|
|
conn->asyncStatus = PGASYNC_BUSY;
|
|
|
|
return 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-05-28 03:54:53 +02:00
|
|
|
/*
|
|
|
|
* handleSendFailure: try to clean up after failure to send command.
|
|
|
|
*
|
|
|
|
* Primarily, what we want to accomplish here is to process an async
|
2003-01-07 23:23:17 +01:00
|
|
|
* NOTICE message that the backend might have sent just before it died.
|
1999-05-28 03:54:53 +02:00
|
|
|
*
|
|
|
|
* NOTE: this routine should only be called in PGASYNC_IDLE state.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
handleSendFailure(PGconn *conn)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Accept any available input data, ignoring errors. Note that if
|
|
|
|
* pqReadData decides the backend has closed the channel, it will
|
|
|
|
* close our side of the socket --- that's just what we want here.
|
|
|
|
*/
|
|
|
|
while (pqReadData(conn) > 0)
|
2000-04-12 19:17:23 +02:00
|
|
|
/* loop until no more data readable */ ;
|
1999-05-28 03:54:53 +02:00
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Parse any available input messages. Since we are in PGASYNC_IDLE
|
2003-01-07 23:23:17 +01:00
|
|
|
* state, only NOTICE and NOTIFY messages will be eaten.
|
1999-05-28 03:54:53 +02:00
|
|
|
*/
|
|
|
|
parseInput(conn);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-12-31 08:29:17 +01:00
|
|
|
/*
|
1998-05-07 01:51:16 +02:00
|
|
|
* Consume any available input from the backend
|
1998-09-03 04:10:56 +02:00
|
|
|
* 0 return: some kind of trouble
|
|
|
|
* 1 return: no problem
|
1996-12-31 08:29:17 +01:00
|
|
|
*/
|
1998-09-03 04:10:56 +02:00
|
|
|
int
|
1998-05-07 01:51:16 +02:00
|
|
|
PQconsumeInput(PGconn *conn)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (!conn)
|
1998-09-03 04:10:56 +02:00
|
|
|
return 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
/*
|
|
|
|
* for non-blocking connections try to flush the send-queue,
|
|
|
|
* otherwise we may never get a response for something that may
|
|
|
|
* not have already been sent because it's in our write buffer!
|
|
|
|
*/
|
|
|
|
if (pqIsnonblocking(conn))
|
|
|
|
{
|
|
|
|
if (pqFlush(conn) < 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
|
|
|
* Load more data, if available. We do this no matter what state we
|
|
|
|
* are in, since we are probably getting called because the
|
|
|
|
* application wants to get rid of a read-select condition. Note that
|
|
|
|
* we will NOT block waiting for more input.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1998-10-01 03:40:26 +02:00
|
|
|
if (pqReadData(conn) < 0)
|
1998-09-03 04:10:56 +02:00
|
|
|
return 0;
|
2003-04-19 02:02:30 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Parsing of the data waits till later. */
|
1998-09-03 04:10:56 +02:00
|
|
|
return 1;
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/*
|
|
|
|
* parseInput: if appropriate, parse input data from backend
|
|
|
|
* until input is exhausted or a stopping state is reached.
|
|
|
|
* Note that this function will NOT attempt to read more data from the backend.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
static void
|
|
|
|
parseInput(PGconn *conn)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
char id;
|
2003-04-22 02:08:07 +02:00
|
|
|
int msgLength;
|
|
|
|
int avail;
|
1999-08-31 03:37:37 +02:00
|
|
|
char noticeWorkspace[128];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
1998-05-07 01:51:16 +02:00
|
|
|
* Loop to parse successive complete messages available in the buffer.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1998-05-07 01:51:16 +02:00
|
|
|
for (;;)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
2003-04-22 02:08:07 +02:00
|
|
|
* Try to read a message. First get the type code and length.
|
|
|
|
* Return if not enough data.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
2003-04-22 02:08:07 +02:00
|
|
|
conn->inCursor = conn->inStart;
|
|
|
|
if (pqGetc(&id, conn))
|
|
|
|
return;
|
|
|
|
if (pqGetInt(&msgLength, 4, conn))
|
1998-05-07 01:51:16 +02:00
|
|
|
return;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/*
|
2003-04-22 02:08:07 +02:00
|
|
|
* Try to validate message type/length here. A length less than 4
|
|
|
|
* is definitely broken. Large lengths should only be believed
|
|
|
|
* for a few message types.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
2003-04-22 02:08:07 +02:00
|
|
|
if (msgLength < 4)
|
|
|
|
{
|
|
|
|
handleSyncLoss(conn, id, msgLength);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (msgLength > 30000 &&
|
2003-05-08 20:16:37 +02:00
|
|
|
!(id == 'T' || id == 'D' || id == 'd'))
|
2003-04-22 02:08:07 +02:00
|
|
|
{
|
|
|
|
handleSyncLoss(conn, id, msgLength);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Can't process if message body isn't all here yet.
|
|
|
|
*/
|
|
|
|
msgLength -= 4;
|
|
|
|
avail = conn->inEnd - conn->inCursor;
|
|
|
|
if (avail < msgLength)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Before returning, enlarge the input buffer if needed to hold
|
|
|
|
* the whole message. This is better than leaving it to
|
|
|
|
* pqReadData because we can avoid multiple cycles of realloc()
|
|
|
|
* when the message is large; also, we can implement a reasonable
|
|
|
|
* recovery strategy if we are unable to make the buffer big
|
|
|
|
* enough.
|
|
|
|
*/
|
|
|
|
if (pqCheckInBufferSpace(conn->inCursor + msgLength, conn))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* XXX add some better recovery code... plan is to skip
|
|
|
|
* over the message using its length, then report an error.
|
|
|
|
* For the moment, just treat this like loss of sync (which
|
|
|
|
* indeed it might be!)
|
|
|
|
*/
|
|
|
|
handleSyncLoss(conn, id, msgLength);
|
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
return;
|
2003-04-22 02:08:07 +02:00
|
|
|
}
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/*
|
2003-04-22 02:08:07 +02:00
|
|
|
* NOTIFY and NOTICE messages can happen in any state; always process
|
|
|
|
* them right away.
|
2000-02-24 05:50:51 +01:00
|
|
|
*
|
|
|
|
* Most other messages should only be processed while in BUSY state.
|
2000-04-12 19:17:23 +02:00
|
|
|
* (In particular, in READY state we hold off further parsing
|
|
|
|
* until the application collects the current PGresult.)
|
2000-02-24 05:50:51 +01:00
|
|
|
*
|
2000-04-12 19:17:23 +02:00
|
|
|
* However, if the state is IDLE then we got trouble; we need to deal
|
|
|
|
* with the unexpected message somehow.
|
2003-04-25 21:45:10 +02:00
|
|
|
*
|
|
|
|
* ParameterStatus ('S') messages are a special case: in IDLE state
|
|
|
|
* we must process 'em (this case could happen if a new value was
|
|
|
|
* adopted from config file due to SIGHUP), but otherwise we hold
|
|
|
|
* off until BUSY state.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
|
|
|
if (id == 'A')
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (getNotify(conn))
|
|
|
|
return;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-08-09 04:59:33 +02:00
|
|
|
else if (id == 'N')
|
|
|
|
{
|
2003-04-24 23:16:45 +02:00
|
|
|
if (pqGetErrorNotice(conn, false))
|
1998-08-09 04:59:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2000-02-24 05:50:51 +01:00
|
|
|
else if (conn->asyncStatus != PGASYNC_BUSY)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-02-24 05:50:51 +01:00
|
|
|
/* If not IDLE state, just wait ... */
|
|
|
|
if (conn->asyncStatus != PGASYNC_IDLE)
|
|
|
|
return;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/*
|
2000-02-24 05:50:51 +01:00
|
|
|
* Unexpected message in IDLE state; need to recover somehow.
|
|
|
|
* ERROR messages are displayed using the notice processor;
|
2003-04-25 21:45:10 +02:00
|
|
|
* ParameterStatus is handled normally;
|
2000-02-24 05:50:51 +01:00
|
|
|
* anything else is just dropped on the floor after displaying
|
|
|
|
* a suitable warning notice. (An ERROR is very possibly the
|
|
|
|
* backend telling us why it is about to close the connection,
|
|
|
|
* so we don't want to just discard it...)
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
2000-02-24 05:50:51 +01:00
|
|
|
if (id == 'E')
|
1998-05-07 01:51:16 +02:00
|
|
|
{
|
2003-04-24 23:16:45 +02:00
|
|
|
if (pqGetErrorNotice(conn, false /* treat as notice */))
|
2000-02-24 05:50:51 +01:00
|
|
|
return;
|
|
|
|
}
|
2003-04-25 21:45:10 +02:00
|
|
|
else if (id == 'S')
|
|
|
|
{
|
|
|
|
if (getParameterStatus(conn))
|
|
|
|
return;
|
|
|
|
}
|
2000-02-24 05:50:51 +01:00
|
|
|
else
|
|
|
|
{
|
2001-07-15 15:45:04 +02:00
|
|
|
snprintf(noticeWorkspace, sizeof(noticeWorkspace),
|
|
|
|
libpq_gettext("message type 0x%02x arrived from server while idle\n"),
|
|
|
|
id);
|
2000-02-24 05:50:51 +01:00
|
|
|
DONOTICE(conn, noticeWorkspace);
|
2003-04-22 02:08:07 +02:00
|
|
|
/* Discard the unexpected message */
|
|
|
|
conn->inCursor += msgLength;
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
2000-02-24 05:50:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* In BUSY state, we can process everything.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
switch (id)
|
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
case 'C': /* command complete */
|
1999-08-31 03:37:37 +02:00
|
|
|
if (pqGets(&conn->workBuffer, conn))
|
|
|
|
return;
|
1998-05-07 01:51:16 +02:00
|
|
|
if (conn->result == NULL)
|
1998-09-03 04:10:56 +02:00
|
|
|
conn->result = PQmakeEmptyPGresult(conn,
|
2000-04-12 19:17:23 +02:00
|
|
|
PGRES_COMMAND_OK);
|
1999-08-31 03:37:37 +02:00
|
|
|
strncpy(conn->result->cmdStatus, conn->workBuffer.data,
|
|
|
|
CMDSTATUS_LEN);
|
1999-03-14 19:12:21 +01:00
|
|
|
conn->asyncStatus = PGASYNC_READY;
|
1998-05-07 01:51:16 +02:00
|
|
|
break;
|
|
|
|
case 'E': /* error return */
|
2003-04-24 23:16:45 +02:00
|
|
|
if (pqGetErrorNotice(conn, true))
|
1998-05-07 01:51:16 +02:00
|
|
|
return;
|
1999-03-14 19:12:21 +01:00
|
|
|
conn->asyncStatus = PGASYNC_READY;
|
1998-05-07 01:51:16 +02:00
|
|
|
break;
|
|
|
|
case 'Z': /* backend is ready for new query */
|
2003-04-26 22:23:00 +02:00
|
|
|
if (pqGetc(&conn->xact_status, conn))
|
|
|
|
return;
|
1998-05-07 01:51:16 +02:00
|
|
|
conn->asyncStatus = PGASYNC_IDLE;
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1998-05-07 01:51:16 +02:00
|
|
|
case 'I': /* empty query */
|
|
|
|
if (conn->result == NULL)
|
1998-09-03 04:10:56 +02:00
|
|
|
conn->result = PQmakeEmptyPGresult(conn,
|
1999-05-25 18:15:34 +02:00
|
|
|
PGRES_EMPTY_QUERY);
|
1999-03-14 19:12:21 +01:00
|
|
|
conn->asyncStatus = PGASYNC_READY;
|
1998-05-07 01:51:16 +02:00
|
|
|
break;
|
2003-04-25 21:45:10 +02:00
|
|
|
case 'S': /* parameter status */
|
|
|
|
if (getParameterStatus(conn))
|
|
|
|
return;
|
|
|
|
break;
|
1998-07-09 05:29:11 +02:00
|
|
|
case 'K': /* secret key data from the backend */
|
1998-09-01 06:40:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is expected only during backend startup, but
|
|
|
|
* it's just as easy to handle it as part of the main
|
|
|
|
* loop. Save the data and continue processing.
|
1998-07-09 05:29:11 +02:00
|
|
|
*/
|
|
|
|
if (pqGetInt(&(conn->be_pid), 4, conn))
|
|
|
|
return;
|
|
|
|
if (pqGetInt(&(conn->be_key), 4, conn))
|
|
|
|
return;
|
|
|
|
break;
|
1998-09-01 06:40:42 +02:00
|
|
|
case 'T': /* row descriptions (start of query
|
|
|
|
* results) */
|
1998-05-07 01:51:16 +02:00
|
|
|
if (conn->result == NULL)
|
1997-09-08 04:41:22 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
/* First 'T' in a query sequence */
|
|
|
|
if (getRowDescriptions(conn))
|
|
|
|
return;
|
1997-09-08 04:41:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
|
|
|
* A new 'T' message is treated as the start of
|
1998-05-07 01:51:16 +02:00
|
|
|
* another PGresult. (It is not clear that this
|
|
|
|
* is really possible with the current backend.)
|
|
|
|
* We stop parsing until the application accepts
|
|
|
|
* the current result.
|
1997-09-08 04:41:22 +02:00
|
|
|
*/
|
1998-05-07 01:51:16 +02:00
|
|
|
conn->asyncStatus = PGASYNC_READY;
|
|
|
|
return;
|
1997-09-08 04:41:22 +02:00
|
|
|
}
|
|
|
|
break;
|
2003-05-08 20:16:37 +02:00
|
|
|
case 'D': /* Data Row */
|
2003-05-26 22:05:20 +02:00
|
|
|
if (conn->result != NULL &&
|
|
|
|
conn->result->resultStatus == PGRES_TUPLES_OK)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Read another tuple of a normal query response */
|
2003-05-26 22:05:20 +02:00
|
|
|
if (getAnotherTuple(conn, msgLength))
|
1998-05-07 01:51:16 +02:00
|
|
|
return;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-05-26 22:05:20 +02:00
|
|
|
else if (conn->result != NULL &&
|
|
|
|
conn->result->resultStatus == PGRES_FATAL_ERROR)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We've already choked for some reason. Just discard
|
|
|
|
* tuples till we get to the end of the query.
|
|
|
|
*/
|
|
|
|
conn->inCursor += msgLength;
|
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
else
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-05-26 22:05:20 +02:00
|
|
|
/* Set up to report error at end of query */
|
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
2001-07-15 15:45:04 +02:00
|
|
|
libpq_gettext("server sent data (\"D\" message) without prior row description (\"T\" message)\n"));
|
2003-05-26 22:05:20 +02:00
|
|
|
saveErrorResult(conn);
|
2003-04-22 02:08:07 +02:00
|
|
|
/* Discard the unexpected message */
|
|
|
|
conn->inCursor += msgLength;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1998-05-07 01:51:16 +02:00
|
|
|
case 'G': /* Start Copy In */
|
2003-04-22 02:08:07 +02:00
|
|
|
if (pqGetc(&conn->copy_is_binary, conn))
|
|
|
|
return;
|
2003-05-08 20:16:37 +02:00
|
|
|
/* XXX we currently ignore the rest of the message */
|
|
|
|
conn->inCursor = conn->inStart + 5 + msgLength;
|
1998-05-07 01:51:16 +02:00
|
|
|
conn->asyncStatus = PGASYNC_COPY_IN;
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1998-05-07 01:51:16 +02:00
|
|
|
case 'H': /* Start Copy Out */
|
2003-04-22 02:08:07 +02:00
|
|
|
if (pqGetc(&conn->copy_is_binary, conn))
|
|
|
|
return;
|
2003-05-08 20:16:37 +02:00
|
|
|
/* XXX we currently ignore the rest of the message */
|
|
|
|
conn->inCursor = conn->inStart + 5 + msgLength;
|
1998-05-07 01:51:16 +02:00
|
|
|
conn->asyncStatus = PGASYNC_COPY_OUT;
|
2003-04-22 02:08:07 +02:00
|
|
|
conn->copy_already_done = 0;
|
|
|
|
break;
|
|
|
|
case 'd': /* Copy Data */
|
|
|
|
/*
|
|
|
|
* If we see Copy Data, just silently drop it. This
|
|
|
|
* would only occur if application exits COPY OUT mode
|
|
|
|
* too early.
|
|
|
|
*/
|
|
|
|
conn->inCursor += msgLength;
|
|
|
|
break;
|
|
|
|
case 'c': /* Copy Done */
|
|
|
|
/*
|
|
|
|
* If we see Copy Done, just silently drop it. This
|
|
|
|
* is the normal case during PQendcopy. We will keep
|
|
|
|
* swallowing data, expecting to see command-complete
|
|
|
|
* for the COPY command.
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
default:
|
1999-08-31 03:37:37 +02:00
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
2001-07-15 15:45:04 +02:00
|
|
|
libpq_gettext(
|
2001-10-25 07:50:21 +02:00
|
|
|
"unexpected response from server; first received character was \"%c\"\n"),
|
2000-04-12 19:17:23 +02:00
|
|
|
id);
|
1999-08-31 03:37:37 +02:00
|
|
|
/* build an error result holding the error message */
|
|
|
|
saveErrorResult(conn);
|
2003-05-26 22:05:20 +02:00
|
|
|
/* not sure if we will see more, so go to ready state */
|
1998-05-07 01:51:16 +02:00
|
|
|
conn->asyncStatus = PGASYNC_READY;
|
2003-04-22 02:08:07 +02:00
|
|
|
/* Discard the unexpected message */
|
|
|
|
conn->inCursor += msgLength;
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
} /* switch on protocol character */
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
|
|
|
/* Successfully consumed this message */
|
2003-04-22 02:08:07 +02:00
|
|
|
if (conn->inCursor == conn->inStart + 5 + msgLength)
|
|
|
|
{
|
|
|
|
/* Normal case: parsing agrees with specified length */
|
|
|
|
conn->inStart = conn->inCursor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Trouble --- report it */
|
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
|
|
|
libpq_gettext("Message contents do not agree with length in message type \"%c\"\n"),
|
|
|
|
id);
|
|
|
|
/* build an error result holding the error message */
|
|
|
|
saveErrorResult(conn);
|
|
|
|
conn->asyncStatus = PGASYNC_READY;
|
|
|
|
/* trust the specified message length as what to skip */
|
|
|
|
conn->inStart += 5 + msgLength;
|
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
1996-12-31 08:29:17 +01:00
|
|
|
}
|
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
/*
|
|
|
|
* handleSyncLoss: clean up after loss of message-boundary sync
|
|
|
|
*
|
|
|
|
* There isn't really a lot we can do here except abandon the connection.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
handleSyncLoss(PGconn *conn, char id, int msgLength)
|
|
|
|
{
|
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
|
|
|
libpq_gettext(
|
|
|
|
"lost synchronization with server: got message type \"%c\", length %d\n"),
|
|
|
|
id, msgLength);
|
|
|
|
conn->status = CONNECTION_BAD; /* No more connection to backend */
|
|
|
|
pqsecure_close(conn);
|
|
|
|
closesocket(conn->sock);
|
|
|
|
conn->sock = -1;
|
2003-05-26 22:05:20 +02:00
|
|
|
conn->asyncStatus = PGASYNC_READY; /* drop out of GetResult wait loop */
|
2003-04-22 02:08:07 +02:00
|
|
|
}
|
1996-12-31 08:29:17 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1998-05-07 01:51:16 +02:00
|
|
|
* parseInput subroutine to read a 'T' (row descriptions) message.
|
|
|
|
* We build a PGresult structure containing the attribute data.
|
|
|
|
* Returns: 0 if completed message, EOF if not enough data yet.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1998-05-07 01:51:16 +02:00
|
|
|
* Note that if we run out of data, we have to release the partially
|
|
|
|
* constructed PGresult, and rebuild it again next time. Fortunately,
|
|
|
|
* that shouldn't happen often, since 'T' messages usually fit in a packet.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
static int
|
|
|
|
getRowDescriptions(PGconn *conn)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
PGresult *result;
|
1998-05-07 01:51:16 +02:00
|
|
|
int nfields;
|
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-03 04:10:56 +02:00
|
|
|
result = PQmakeEmptyPGresult(conn, PGRES_TUPLES_OK);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
/* parseInput already read the 'T' label and message length. */
|
1998-05-07 01:51:16 +02:00
|
|
|
/* the next two bytes are the number of fields */
|
|
|
|
if (pqGetInt(&(result->numAttributes), 2, conn))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
PQclear(result);
|
|
|
|
return EOF;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
nfields = result->numAttributes;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* allocate space for the attribute descriptors */
|
|
|
|
if (nfields > 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
result->attDescs = (PGresAttDesc *)
|
1998-11-18 01:47:28 +01:00
|
|
|
pqResultAlloc(result, nfields * sizeof(PGresAttDesc), TRUE);
|
1998-05-07 01:51:16 +02:00
|
|
|
MemSet((char *) result->attDescs, 0, nfields * sizeof(PGresAttDesc));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* get type info */
|
|
|
|
for (i = 0; i < nfields; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-04-26 22:23:00 +02:00
|
|
|
int tableid;
|
|
|
|
int columnid;
|
1998-07-13 18:35:04 +02:00
|
|
|
int typid;
|
1998-07-13 04:41:59 +02:00
|
|
|
int typlen;
|
1998-11-18 01:47:28 +01:00
|
|
|
int atttypmod;
|
2003-05-08 20:16:37 +02:00
|
|
|
int format;
|
1998-05-07 01:51:16 +02:00
|
|
|
|
1999-08-31 03:37:37 +02:00
|
|
|
if (pqGets(&conn->workBuffer, conn) ||
|
2003-04-26 22:23:00 +02:00
|
|
|
pqGetInt(&tableid, 4, conn) ||
|
|
|
|
pqGetInt(&columnid, 2, conn) ||
|
1998-07-13 18:35:04 +02:00
|
|
|
pqGetInt(&typid, 4, conn) ||
|
1998-07-13 04:41:59 +02:00
|
|
|
pqGetInt(&typlen, 2, conn) ||
|
2003-05-08 20:16:37 +02:00
|
|
|
pqGetInt(&atttypmod, 4, conn) ||
|
|
|
|
pqGetInt(&format, 2, conn))
|
1998-05-07 01:51:16 +02:00
|
|
|
{
|
|
|
|
PQclear(result);
|
|
|
|
return EOF;
|
|
|
|
}
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1998-09-10 17:18:06 +02:00
|
|
|
/*
|
|
|
|
* Since pqGetInt treats 2-byte integers as unsigned, we need to
|
2003-04-26 22:23:00 +02:00
|
|
|
* coerce these results to signed form.
|
1998-09-10 17:18:06 +02:00
|
|
|
*/
|
2003-04-26 22:23:00 +02:00
|
|
|
columnid = (int) ((int16) columnid);
|
2002-08-24 17:00:47 +02:00
|
|
|
typlen = (int) ((int16) typlen);
|
2003-05-08 20:16:37 +02:00
|
|
|
format = (int) ((int16) format);
|
2002-08-24 17:00:47 +02:00
|
|
|
|
1999-08-31 03:37:37 +02:00
|
|
|
result->attDescs[i].name = pqResultStrdup(result,
|
|
|
|
conn->workBuffer.data);
|
1998-07-13 18:35:04 +02:00
|
|
|
result->attDescs[i].typid = typid;
|
1998-09-03 04:10:56 +02:00
|
|
|
result->attDescs[i].typlen = typlen;
|
1998-07-13 04:41:59 +02:00
|
|
|
result->attDescs[i].atttypmod = atttypmod;
|
2003-05-08 20:16:37 +02:00
|
|
|
/* XXX todo: save tableid/columnid, format too */
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Success! */
|
|
|
|
conn->result = result;
|
|
|
|
return 0;
|
1996-12-31 08:29:17 +01:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
2003-05-08 20:16:37 +02:00
|
|
|
* parseInput subroutine to read a 'D' (row data) message.
|
1998-05-07 01:51:16 +02:00
|
|
|
* We add another tuple to the existing PGresult structure.
|
1998-10-01 03:40:26 +02:00
|
|
|
* Returns: 0 if completed message, EOF if error or not enough data yet.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1998-05-07 01:51:16 +02:00
|
|
|
* Note that if we run out of data, we have to suspend and reprocess
|
|
|
|
* the message after more data is received. We keep a partially constructed
|
|
|
|
* tuple in conn->curTuple, and avoid reallocating already-allocated storage.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
static int
|
2003-05-26 22:05:20 +02:00
|
|
|
getAnotherTuple(PGconn *conn, int msgLength)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-11-18 01:47:28 +01:00
|
|
|
PGresult *result = conn->result;
|
|
|
|
int nfields = result->numAttributes;
|
1998-05-07 01:51:16 +02:00
|
|
|
PGresAttValue *tup;
|
2003-05-08 20:16:37 +02:00
|
|
|
int tupnfields; /* # fields from tuple */
|
1998-05-07 01:51:16 +02:00
|
|
|
int vlen; /* length of the current field value */
|
2003-05-08 20:16:37 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Allocate tuple space if first time for this data message */
|
|
|
|
if (conn->curTuple == NULL)
|
|
|
|
{
|
|
|
|
conn->curTuple = (PGresAttValue *)
|
1998-11-18 01:47:28 +01:00
|
|
|
pqResultAlloc(result, nfields * sizeof(PGresAttValue), TRUE);
|
1998-10-01 03:40:26 +02:00
|
|
|
if (conn->curTuple == NULL)
|
|
|
|
goto outOfMemory;
|
1998-05-07 01:51:16 +02:00
|
|
|
MemSet((char *) conn->curTuple, 0, nfields * sizeof(PGresAttValue));
|
|
|
|
}
|
|
|
|
tup = conn->curTuple;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
/* Get the field count and make sure it's what we expect */
|
|
|
|
if (pqGetInt(&tupnfields, 2, conn))
|
|
|
|
return EOF;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
if (tupnfields != nfields)
|
|
|
|
{
|
|
|
|
/* Replace partially constructed result with an error result */
|
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
|
|
|
libpq_gettext("unexpected field count in D message\n"));
|
|
|
|
saveErrorResult(conn);
|
|
|
|
/* Discard the failed message by pretending we read it */
|
2003-05-26 22:05:20 +02:00
|
|
|
conn->inCursor = conn->inStart + 5 + msgLength;
|
2003-05-08 20:16:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Scan the fields */
|
|
|
|
for (i = 0; i < nfields; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-05-08 20:16:37 +02:00
|
|
|
/* get the value length */
|
|
|
|
if (pqGetInt(&vlen, 4, conn))
|
|
|
|
return EOF;
|
|
|
|
if (vlen == -1)
|
1998-05-07 01:51:16 +02:00
|
|
|
{
|
2003-05-08 20:16:37 +02:00
|
|
|
/* null field */
|
1998-11-18 01:47:28 +01:00
|
|
|
tup[i].value = result->null_field;
|
1998-05-07 01:51:16 +02:00
|
|
|
tup[i].len = NULL_LEN;
|
2003-05-08 20:16:37 +02:00
|
|
|
continue;
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
2003-05-08 20:16:37 +02:00
|
|
|
if (vlen < 0)
|
|
|
|
vlen = 0;
|
|
|
|
if (tup[i].value == NULL)
|
1998-05-07 01:51:16 +02:00
|
|
|
{
|
2003-05-08 20:16:37 +02:00
|
|
|
tup[i].value = (char *) pqResultAlloc(result, vlen + 1, false);
|
1998-05-07 01:51:16 +02:00
|
|
|
if (tup[i].value == NULL)
|
2003-05-08 20:16:37 +02:00
|
|
|
goto outOfMemory;
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
2003-05-08 20:16:37 +02:00
|
|
|
tup[i].len = vlen;
|
|
|
|
/* read in the value */
|
|
|
|
if (vlen > 0)
|
|
|
|
if (pqGetnchar((char *) (tup[i].value), vlen, conn))
|
|
|
|
return EOF;
|
|
|
|
/* we have to terminate this ourselves */
|
|
|
|
tup[i].value[vlen] = '\0';
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Success! Store the completed tuple in the result */
|
1999-05-25 18:15:34 +02:00
|
|
|
if (!addTuple(result, tup))
|
1998-10-01 03:40:26 +02:00
|
|
|
goto outOfMemory;
|
1998-05-07 01:51:16 +02:00
|
|
|
/* and reset for a new message */
|
|
|
|
conn->curTuple = NULL;
|
1999-08-31 03:37:37 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1998-10-01 03:40:26 +02:00
|
|
|
|
|
|
|
outOfMemory:
|
|
|
|
/* Replace partially constructed result with an error result */
|
2000-04-12 19:17:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* we do NOT use saveErrorResult() here, because of the likelihood
|
2003-05-26 22:05:20 +02:00
|
|
|
* that there's not enough memory to concatenate messages. Instead,
|
|
|
|
* discard the old result first to try to win back some memory.
|
1999-08-31 03:37:37 +02:00
|
|
|
*/
|
1998-10-01 03:40:26 +02:00
|
|
|
pqClearAsyncResult(conn);
|
1999-08-31 03:37:37 +02:00
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
2003-05-26 22:05:20 +02:00
|
|
|
libpq_gettext("out of memory for query result\n"));
|
1998-10-01 03:40:26 +02:00
|
|
|
conn->result = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR);
|
2003-05-08 20:16:37 +02:00
|
|
|
/* Discard the failed message by pretending we read it */
|
2003-05-26 22:05:20 +02:00
|
|
|
conn->inCursor = conn->inStart + 5 + msgLength;
|
2003-05-08 20:16:37 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1998-05-07 01:51:16 +02:00
|
|
|
* PQisBusy
|
1998-09-01 06:40:42 +02:00
|
|
|
* Return TRUE if PQgetResult would block waiting for input.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-05-07 01:51:16 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
int
|
1998-05-07 01:51:16 +02:00
|
|
|
PQisBusy(PGconn *conn)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!conn)
|
1998-05-07 01:51:16 +02:00
|
|
|
return FALSE;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Parse any available data, if our state permits. */
|
|
|
|
parseInput(conn);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* PQgetResult will return immediately in all states except BUSY. */
|
1998-09-01 05:29:17 +02:00
|
|
|
return conn->asyncStatus == PGASYNC_BUSY;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1996-07-23 05:35:14 +02:00
|
|
|
|
|
|
|
/*
|
1998-05-07 01:51:16 +02:00
|
|
|
* PQgetResult
|
1998-09-01 06:40:42 +02:00
|
|
|
* Get the next PGresult produced by a query.
|
|
|
|
* Returns NULL if and only if no query work remains.
|
1996-07-23 05:35:14 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
PGresult *
|
1998-05-07 01:51:16 +02:00
|
|
|
PQgetResult(PGconn *conn)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
PGresult *res;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
if (!conn)
|
|
|
|
return NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Parse any available data, if our state permits. */
|
|
|
|
parseInput(conn);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* If not ready to return something, block until we are. */
|
|
|
|
while (conn->asyncStatus == PGASYNC_BUSY)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Wait for some more data, and load it. */
|
|
|
|
if (pqWait(TRUE, FALSE, conn) ||
|
|
|
|
pqReadData(conn) < 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* conn->errorMessage has been set by pqWait or pqReadData. We
|
|
|
|
* want to append it to any already-received error message.
|
1999-08-31 03:37:37 +02:00
|
|
|
*/
|
|
|
|
saveErrorResult(conn);
|
1998-05-07 01:51:16 +02:00
|
|
|
conn->asyncStatus = PGASYNC_IDLE;
|
1999-08-31 03:37:37 +02:00
|
|
|
return prepareAsyncResult(conn);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Parse it. */
|
|
|
|
parseInput(conn);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Return the appropriate thing. */
|
|
|
|
switch (conn->asyncStatus)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
case PGASYNC_IDLE:
|
|
|
|
res = NULL; /* query is complete */
|
|
|
|
break;
|
|
|
|
case PGASYNC_READY:
|
1999-08-31 03:37:37 +02:00
|
|
|
res = prepareAsyncResult(conn);
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Set the state back to BUSY, allowing parsing to proceed. */
|
|
|
|
conn->asyncStatus = PGASYNC_BUSY;
|
|
|
|
break;
|
|
|
|
case PGASYNC_COPY_IN:
|
1998-09-03 04:10:56 +02:00
|
|
|
res = PQmakeEmptyPGresult(conn, PGRES_COPY_IN);
|
1998-05-07 01:51:16 +02:00
|
|
|
break;
|
|
|
|
case PGASYNC_COPY_OUT:
|
1998-09-03 04:10:56 +02:00
|
|
|
res = PQmakeEmptyPGresult(conn, PGRES_COPY_OUT);
|
1998-05-07 01:51:16 +02:00
|
|
|
break;
|
|
|
|
default:
|
1999-08-31 03:37:37 +02:00
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
2001-10-25 07:50:21 +02:00
|
|
|
libpq_gettext("unexpected asyncStatus: %d\n"),
|
1999-08-31 03:37:37 +02:00
|
|
|
(int) conn->asyncStatus);
|
1998-09-03 04:10:56 +02:00
|
|
|
res = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR);
|
1998-05-07 01:51:16 +02:00
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
return res;
|
1996-07-23 05:35:14 +02:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
1998-05-07 01:51:16 +02:00
|
|
|
* PQexec
|
|
|
|
* send a query to the backend and package up the result in a PGresult
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1998-10-01 03:40:26 +02:00
|
|
|
* If the query was not even sent, return NULL; conn->errorMessage is set to
|
|
|
|
* a relevant message.
|
|
|
|
* If the query was sent, a new PGresult is returned (which could indicate
|
|
|
|
* either success or failure).
|
|
|
|
* The user is responsible for freeing the PGresult via PQclear()
|
|
|
|
* when done with it.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
PGresult *
|
1998-05-07 01:51:16 +02:00
|
|
|
PQexec(PGconn *conn, const char *query)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
PGresult *result;
|
|
|
|
PGresult *lastResult;
|
2000-04-12 19:17:23 +02:00
|
|
|
bool savedblocking;
|
2000-01-18 07:09:24 +01:00
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* we assume anyone calling PQexec wants blocking behaviour, we force
|
|
|
|
* the blocking status of the connection to blocking for the duration
|
|
|
|
* of this function and restore it on return
|
2000-01-18 07:09:24 +01:00
|
|
|
*/
|
|
|
|
savedblocking = pqIsnonblocking(conn);
|
|
|
|
if (PQsetnonblocking(conn, FALSE) == -1)
|
|
|
|
return NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
|
|
|
* Silently discard any prior query result that application didn't
|
|
|
|
* eat. This is probably poor design, but it's here for backward
|
|
|
|
* compatibility.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
|
|
|
while ((result = PQgetResult(conn)) != NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (result->resultStatus == PGRES_COPY_IN ||
|
|
|
|
result->resultStatus == PGRES_COPY_OUT)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
PQclear(result);
|
1999-08-31 03:37:37 +02:00
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
2001-10-25 07:50:21 +02:00
|
|
|
libpq_gettext("COPY state must be terminated first\n"));
|
2000-01-18 07:09:24 +01:00
|
|
|
/* restore blocking status */
|
|
|
|
goto errout;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
PQclear(result);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* OK to send the message */
|
1998-09-01 06:40:42 +02:00
|
|
|
if (!PQsendQuery(conn, query))
|
2000-04-12 19:17:23 +02:00
|
|
|
goto errout; /* restore blocking status */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
|
|
|
* For backwards compatibility, return the last result if there are
|
1999-08-31 03:37:37 +02:00
|
|
|
* more than one --- but merge error messages if we get more than one
|
|
|
|
* error result.
|
|
|
|
*
|
2000-04-12 19:17:23 +02:00
|
|
|
* We have to stop if we see copy in/out, however. We will resume parsing
|
|
|
|
* when application calls PQendcopy.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
|
|
|
lastResult = NULL;
|
|
|
|
while ((result = PQgetResult(conn)) != NULL)
|
|
|
|
{
|
|
|
|
if (lastResult)
|
1999-08-31 03:37:37 +02:00
|
|
|
{
|
|
|
|
if (lastResult->resultStatus == PGRES_FATAL_ERROR &&
|
|
|
|
result->resultStatus == PGRES_FATAL_ERROR)
|
|
|
|
{
|
|
|
|
pqCatenateResultError(lastResult, result->errMsg);
|
|
|
|
PQclear(result);
|
|
|
|
result = lastResult;
|
2003-03-10 23:28:22 +01:00
|
|
|
/* Make sure PQerrorMessage agrees with concatenated result */
|
1999-08-31 03:37:37 +02:00
|
|
|
resetPQExpBuffer(&conn->errorMessage);
|
|
|
|
appendPQExpBufferStr(&conn->errorMessage, result->errMsg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PQclear(lastResult);
|
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
lastResult = result;
|
1998-05-07 01:53:48 +02:00
|
|
|
if (result->resultStatus == PGRES_COPY_IN ||
|
|
|
|
result->resultStatus == PGRES_COPY_OUT)
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-01-18 07:09:24 +01:00
|
|
|
|
|
|
|
if (PQsetnonblocking(conn, savedblocking) == -1)
|
|
|
|
return NULL;
|
1998-05-07 01:51:16 +02:00
|
|
|
return lastResult;
|
2000-01-18 07:09:24 +01:00
|
|
|
|
|
|
|
errout:
|
|
|
|
if (PQsetnonblocking(conn, savedblocking) == -1)
|
|
|
|
return NULL;
|
|
|
|
return NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1996-12-13 10:25:08 +01:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/*
|
2003-04-24 23:16:45 +02:00
|
|
|
* Attempt to read an Error or Notice response message.
|
1998-05-07 01:51:16 +02:00
|
|
|
* This is possible in several places, so we break it out as a subroutine.
|
2003-04-24 23:16:45 +02:00
|
|
|
* Entry: 'E' or 'N' message type and length have already been consumed.
|
|
|
|
* Exit: returns 0 if successfully consumed message.
|
1998-09-01 06:40:42 +02:00
|
|
|
* returns EOF if not enough data.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
2003-04-24 23:16:45 +02:00
|
|
|
int
|
|
|
|
pqGetErrorNotice(PGconn *conn, bool isError)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-04-24 23:16:45 +02:00
|
|
|
PGresult *res;
|
|
|
|
PQExpBufferData workBuf;
|
|
|
|
char id;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a PGresult to hold the accumulated fields. We temporarily
|
|
|
|
* lie about the result status, so that PQmakeEmptyPGresult doesn't
|
|
|
|
* uselessly copy conn->errorMessage.
|
|
|
|
*/
|
|
|
|
res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY);
|
|
|
|
res->resultStatus = PGRES_FATAL_ERROR;
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
2003-04-24 23:16:45 +02:00
|
|
|
* Since the fields might be pretty long, we create a temporary
|
2000-04-12 19:17:23 +02:00
|
|
|
* PQExpBuffer rather than using conn->workBuffer. workBuffer is
|
2003-04-24 23:16:45 +02:00
|
|
|
* intended for stuff that is expected to be short. We shouldn't
|
|
|
|
* use conn->errorMessage either, since this might be only a notice.
|
1999-08-31 03:37:37 +02:00
|
|
|
*/
|
2003-04-24 23:16:45 +02:00
|
|
|
initPQExpBuffer(&workBuf);
|
1999-08-31 03:37:37 +02:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
|
|
|
* Read the fields and save into res.
|
|
|
|
*/
|
|
|
|
for (;;)
|
1999-08-31 03:37:37 +02:00
|
|
|
{
|
2003-04-24 23:16:45 +02:00
|
|
|
if (pqGetc(&id, conn))
|
|
|
|
goto fail;
|
|
|
|
if (id == '\0')
|
|
|
|
break; /* terminator found */
|
|
|
|
if (pqGets(&workBuf, conn))
|
|
|
|
goto fail;
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case 'S':
|
|
|
|
res->errSeverity = pqResultStrdup(res, workBuf.data);
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
res->errCode = pqResultStrdup(res, workBuf.data);
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
res->errPrimary = pqResultStrdup(res, workBuf.data);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
res->errDetail = pqResultStrdup(res, workBuf.data);
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
res->errHint = pqResultStrdup(res, workBuf.data);
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
res->errPosition = pqResultStrdup(res, workBuf.data);
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
res->errContext = pqResultStrdup(res, workBuf.data);
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
res->errFilename = pqResultStrdup(res, workBuf.data);
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
res->errLineno = pqResultStrdup(res, workBuf.data);
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
res->errFuncname = pqResultStrdup(res, workBuf.data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* silently ignore any other field type */
|
|
|
|
break;
|
|
|
|
}
|
1999-08-31 03:37:37 +02:00
|
|
|
}
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now build the "overall" error message for PQresultErrorMessage.
|
|
|
|
*
|
|
|
|
* XXX this should be configurable somehow.
|
|
|
|
*/
|
|
|
|
resetPQExpBuffer(&workBuf);
|
|
|
|
if (res->errSeverity)
|
|
|
|
appendPQExpBuffer(&workBuf, "%s: ", res->errSeverity);
|
|
|
|
if (res->errPrimary)
|
|
|
|
appendPQExpBufferStr(&workBuf, res->errPrimary);
|
|
|
|
/* translator: %s represents a digit string */
|
|
|
|
if (res->errPosition)
|
|
|
|
appendPQExpBuffer(&workBuf, libpq_gettext(" at character %s"),
|
|
|
|
res->errPosition);
|
|
|
|
appendPQExpBufferChar(&workBuf, '\n');
|
|
|
|
if (res->errDetail)
|
|
|
|
appendPQExpBuffer(&workBuf, libpq_gettext("DETAIL: %s\n"),
|
|
|
|
res->errDetail);
|
|
|
|
if (res->errHint)
|
|
|
|
appendPQExpBuffer(&workBuf, libpq_gettext("HINT: %s\n"),
|
|
|
|
res->errHint);
|
|
|
|
if (res->errContext)
|
|
|
|
appendPQExpBuffer(&workBuf, libpq_gettext("CONTEXT: %s\n"),
|
|
|
|
res->errContext);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Either save error as current async result, or just emit the notice.
|
|
|
|
*/
|
|
|
|
if (isError)
|
|
|
|
{
|
|
|
|
res->errMsg = pqResultStrdup(res, workBuf.data);
|
|
|
|
pqClearAsyncResult(conn);
|
|
|
|
conn->result = res;
|
|
|
|
resetPQExpBuffer(&conn->errorMessage);
|
|
|
|
appendPQExpBufferStr(&conn->errorMessage, workBuf.data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DONOTICE(conn, workBuf.data);
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
termPQExpBuffer(&workBuf);
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
PQclear(res);
|
|
|
|
termPQExpBuffer(&workBuf);
|
|
|
|
return EOF;
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-04-25 21:45:10 +02:00
|
|
|
/*
|
|
|
|
* Attempt to read a ParameterStatus message.
|
|
|
|
* This is possible in several places, so we break it out as a subroutine.
|
|
|
|
* Entry: 'S' message type and length have already been consumed.
|
|
|
|
* Exit: returns 0 if successfully consumed message.
|
|
|
|
* returns EOF if not enough data.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
getParameterStatus(PGconn *conn)
|
|
|
|
{
|
|
|
|
/* Get the parameter name */
|
|
|
|
if (pqGets(&conn->workBuffer, conn))
|
|
|
|
return EOF;
|
|
|
|
/* Is it one we care about? */
|
|
|
|
if (strcmp(conn->workBuffer.data, "client_encoding") == 0)
|
|
|
|
{
|
|
|
|
if (pqGets(&conn->workBuffer, conn))
|
|
|
|
return EOF;
|
|
|
|
conn->client_encoding = pg_char_to_encoding(conn->workBuffer.data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Uninteresting parameter, ignore it */
|
|
|
|
if (pqGets(&conn->workBuffer, conn))
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/*
|
|
|
|
* Attempt to read a Notify response message.
|
|
|
|
* This is possible in several places, so we break it out as a subroutine.
|
2003-04-22 02:08:07 +02:00
|
|
|
* Entry: 'A' message type and length have already been consumed.
|
1998-05-07 01:51:16 +02:00
|
|
|
* Exit: returns 0 if successfully consumed Notify message.
|
1998-09-01 06:40:42 +02:00
|
|
|
* returns EOF if not enough data.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
getNotify(PGconn *conn)
|
|
|
|
{
|
1999-08-31 03:37:37 +02:00
|
|
|
int be_pid;
|
1998-05-07 01:51:16 +02:00
|
|
|
PGnotify *newNotify;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-08-31 03:37:37 +02:00
|
|
|
if (pqGetInt(&be_pid, 4, conn))
|
1998-05-07 01:51:16 +02:00
|
|
|
return EOF;
|
1999-08-31 03:37:37 +02:00
|
|
|
if (pqGets(&conn->workBuffer, conn))
|
1998-05-07 01:51:16 +02:00
|
|
|
return EOF;
|
2002-04-16 01:35:51 +02:00
|
|
|
|
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* Store the relation name right after the PQnotify structure so it
|
|
|
|
* can all be freed at once. We don't use NAMEDATALEN because we
|
|
|
|
* don't want to tie this interface to a specific server name length.
|
2002-04-16 01:35:51 +02:00
|
|
|
*/
|
|
|
|
newNotify = (PGnotify *) malloc(sizeof(PGnotify) +
|
2002-09-04 22:31:48 +02:00
|
|
|
strlen(conn->workBuffer.data) +1);
|
2003-04-22 02:08:07 +02:00
|
|
|
if (newNotify)
|
|
|
|
{
|
|
|
|
newNotify->relname = (char *) newNotify + sizeof(PGnotify);
|
|
|
|
strcpy(newNotify->relname, conn->workBuffer.data);
|
|
|
|
newNotify->be_pid = be_pid;
|
|
|
|
DLAddTail(conn->notifyList, DLNewElem(newNotify));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Swallow extra string (not presently used) */
|
|
|
|
if (pqGets(&conn->workBuffer, conn))
|
|
|
|
return EOF;
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1996-12-13 10:25:08 +01:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/*
|
|
|
|
* PQnotifies
|
|
|
|
* returns a PGnotify* structure of the latest async notification
|
|
|
|
* that has not yet been handled
|
|
|
|
*
|
|
|
|
* returns NULL, if there is currently
|
|
|
|
* no unhandled async notification from the backend
|
|
|
|
*
|
|
|
|
* the CALLER is responsible for FREE'ing the structure returned
|
|
|
|
*/
|
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
PGnotify *
|
1998-05-07 01:51:16 +02:00
|
|
|
PQnotifies(PGconn *conn)
|
|
|
|
{
|
|
|
|
Dlelem *e;
|
|
|
|
PGnotify *event;
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
return NULL;
|
1996-12-13 10:25:08 +01:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Parse any available data to see if we can extract NOTIFY messages. */
|
|
|
|
parseInput(conn);
|
1996-12-13 10:25:08 +01:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* RemHead returns NULL if list is empty */
|
|
|
|
e = DLRemHead(conn->notifyList);
|
|
|
|
if (!e)
|
|
|
|
return NULL;
|
|
|
|
event = (PGnotify *) DLE_VAL(e);
|
|
|
|
DLFreeElem(e);
|
|
|
|
return event;
|
|
|
|
}
|
1996-12-13 10:25:08 +01:00
|
|
|
|
1996-07-18 07:48:57 +02:00
|
|
|
/*
|
1998-05-07 01:51:16 +02:00
|
|
|
* PQgetline - gets a newline-terminated string from the backend.
|
1996-07-27 04:55:23 +02:00
|
|
|
*
|
1998-05-07 01:51:16 +02:00
|
|
|
* Chiefly here so that applications can use "COPY <rel> to stdout"
|
|
|
|
* and read the output string. Returns a null-terminated string in s.
|
1996-12-13 10:25:08 +01:00
|
|
|
*
|
2003-04-22 02:08:07 +02:00
|
|
|
* XXX this routine is now deprecated, because it can't handle binary data.
|
|
|
|
* If called during a COPY BINARY we return EOF.
|
|
|
|
*
|
1998-05-07 01:51:16 +02:00
|
|
|
* PQgetline reads up to maxlen-1 characters (like fgets(3)) but strips
|
|
|
|
* the terminating \n (like gets(3)).
|
1996-12-13 10:25:08 +01:00
|
|
|
*
|
1998-09-03 04:10:56 +02:00
|
|
|
* CAUTION: the caller is responsible for detecting the end-of-copy signal
|
|
|
|
* (a line containing just "\.") when using this routine.
|
|
|
|
*
|
1998-05-07 01:51:16 +02:00
|
|
|
* RETURNS:
|
2003-04-22 02:08:07 +02:00
|
|
|
* EOF if error (eg, invalid arguments are given)
|
1998-05-07 01:51:16 +02:00
|
|
|
* 0 if EOL is reached (i.e., \n has been read)
|
|
|
|
* (this is required for backward-compatibility -- this
|
|
|
|
* routine used to always return EOF or 0, assuming that
|
|
|
|
* the line ended within maxlen bytes.)
|
|
|
|
* 1 in other cases (i.e., the buffer was filled before \n is reached)
|
1996-07-18 07:48:57 +02:00
|
|
|
*/
|
1998-05-07 01:51:16 +02:00
|
|
|
int
|
|
|
|
PQgetline(PGconn *conn, char *s, int maxlen)
|
1996-07-18 07:48:57 +02:00
|
|
|
{
|
2003-04-22 02:08:07 +02:00
|
|
|
int status;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
/* maxlen must be at least 3 to hold the \. terminator! */
|
|
|
|
if (!conn || !s || maxlen < 3)
|
1998-05-07 01:51:16 +02:00
|
|
|
return EOF;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
if (conn->sock < 0 ||
|
|
|
|
conn->asyncStatus != PGASYNC_COPY_OUT ||
|
|
|
|
conn->copy_is_binary)
|
1998-05-07 01:51:16 +02:00
|
|
|
{
|
2003-04-22 02:08:07 +02:00
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
|
|
|
libpq_gettext("PQgetline: not doing text COPY OUT\n"));
|
1998-05-07 01:51:16 +02:00
|
|
|
*s = '\0';
|
|
|
|
return EOF;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
while ((status = PQgetlineAsync(conn, s, maxlen-1)) == 0)
|
1998-05-07 01:51:16 +02:00
|
|
|
{
|
2003-04-22 02:08:07 +02:00
|
|
|
/* need to load more data */
|
|
|
|
if (pqWait(TRUE, FALSE, conn) ||
|
|
|
|
pqReadData(conn) < 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-04-22 02:08:07 +02:00
|
|
|
*s = '\0';
|
|
|
|
return EOF;
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
/* End of copy detected; gin up old-style terminator */
|
|
|
|
strcpy(s, "\\.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add null terminator, and strip trailing \n if present */
|
|
|
|
if (s[status-1] == '\n')
|
|
|
|
{
|
|
|
|
s[status-1] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s[status] = '\0';
|
|
|
|
return 1;
|
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-03 04:10:56 +02:00
|
|
|
/*
|
2003-04-22 02:08:07 +02:00
|
|
|
* PQgetlineAsync - gets a COPY data row without blocking.
|
1998-09-03 04:10:56 +02:00
|
|
|
*
|
|
|
|
* This routine is for applications that want to do "COPY <rel> to stdout"
|
|
|
|
* asynchronously, that is without blocking. Having issued the COPY command
|
|
|
|
* and gotten a PGRES_COPY_OUT response, the app should call PQconsumeInput
|
|
|
|
* and this routine until the end-of-data signal is detected. Unlike
|
|
|
|
* PQgetline, this routine takes responsibility for detecting end-of-data.
|
|
|
|
*
|
2003-04-22 02:08:07 +02:00
|
|
|
* On each call, PQgetlineAsync will return data if a complete data row
|
|
|
|
* is available in libpq's input buffer. Otherwise, no data is returned
|
|
|
|
* until the rest of the row arrives.
|
1998-09-03 04:10:56 +02:00
|
|
|
*
|
|
|
|
* If -1 is returned, the end-of-data signal has been recognized (and removed
|
|
|
|
* from libpq's input buffer). The caller *must* next call PQendcopy and
|
|
|
|
* then return to normal processing.
|
|
|
|
*
|
|
|
|
* RETURNS:
|
1999-05-25 18:15:34 +02:00
|
|
|
* -1 if the end-of-copy-data marker has been recognized
|
|
|
|
* 0 if no data is available
|
|
|
|
* >0 the number of bytes returned.
|
2003-04-22 02:08:07 +02:00
|
|
|
*
|
|
|
|
* The data returned will not extend beyond a data-row boundary. If possible
|
|
|
|
* a whole row will be returned at one time. But if the buffer offered by
|
|
|
|
* the caller is too small to hold a row sent by the backend, then a partial
|
|
|
|
* data row will be returned. In text mode this can be detected by testing
|
|
|
|
* whether the last returned byte is '\n' or not.
|
|
|
|
*
|
|
|
|
* The returned data is *not* null-terminated.
|
1998-09-03 04:10:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
PQgetlineAsync(PGconn *conn, char *buffer, int bufsize)
|
|
|
|
{
|
2003-04-22 02:08:07 +02:00
|
|
|
char id;
|
|
|
|
int msgLength;
|
1999-05-25 18:15:34 +02:00
|
|
|
int avail;
|
1998-09-03 04:10:56 +02:00
|
|
|
|
|
|
|
if (!conn || conn->asyncStatus != PGASYNC_COPY_OUT)
|
|
|
|
return -1; /* we are not doing a copy... */
|
|
|
|
|
|
|
|
/*
|
2003-04-22 02:08:07 +02:00
|
|
|
* Recognize the next input message. To make life simpler for async
|
|
|
|
* callers, we keep returning 0 until the next message is fully available
|
|
|
|
* even if it is not Copy Data. This should keep PQendcopy from blocking.
|
1998-09-03 04:10:56 +02:00
|
|
|
*/
|
|
|
|
conn->inCursor = conn->inStart;
|
2003-04-22 02:08:07 +02:00
|
|
|
if (pqGetc(&id, conn))
|
|
|
|
return 0;
|
|
|
|
if (pqGetInt(&msgLength, 4, conn))
|
|
|
|
return 0;
|
|
|
|
avail = conn->inEnd - conn->inCursor;
|
|
|
|
if (avail < msgLength - 4)
|
|
|
|
return 0;
|
1998-09-03 04:10:56 +02:00
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
/*
|
|
|
|
* Cannot proceed unless it's a Copy Data message. Anything else means
|
|
|
|
* end of copy mode.
|
|
|
|
*/
|
|
|
|
if (id != 'd')
|
|
|
|
return -1;
|
1998-09-03 04:10:56 +02:00
|
|
|
|
|
|
|
/*
|
2003-04-22 02:08:07 +02:00
|
|
|
* Move data from libpq's buffer to the caller's. In the case where
|
|
|
|
* a prior call found the caller's buffer too small, we use
|
|
|
|
* conn->copy_already_done to remember how much of the row was already
|
|
|
|
* returned to the caller.
|
1998-09-03 04:10:56 +02:00
|
|
|
*/
|
2003-04-22 02:08:07 +02:00
|
|
|
conn->inCursor += conn->copy_already_done;
|
|
|
|
avail = msgLength - 4 - conn->copy_already_done;
|
|
|
|
if (avail <= bufsize)
|
1998-09-03 04:10:56 +02:00
|
|
|
{
|
2003-04-22 02:08:07 +02:00
|
|
|
/* Able to consume the whole message */
|
|
|
|
memcpy(buffer, &conn->inBuffer[conn->inCursor], avail);
|
|
|
|
/* Mark message consumed */
|
|
|
|
conn->inStart = conn->inCursor + avail;
|
|
|
|
/* Reset state for next time */
|
|
|
|
conn->copy_already_done = 0;
|
|
|
|
return avail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We must return a partial message */
|
|
|
|
memcpy(buffer, &conn->inBuffer[conn->inCursor], bufsize);
|
|
|
|
/* The message is NOT consumed from libpq's buffer */
|
|
|
|
conn->copy_already_done += bufsize;
|
|
|
|
return bufsize;
|
1998-09-03 04:10:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/*
|
|
|
|
* PQputline -- sends a string to the backend.
|
1998-09-03 04:10:56 +02:00
|
|
|
* Returns 0 if OK, EOF if not.
|
1998-05-07 01:51:16 +02:00
|
|
|
*
|
2003-04-19 02:02:30 +02:00
|
|
|
* This exists to support "COPY <rel> from stdin". The backend will ignore
|
|
|
|
* the string if not doing COPY.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
1998-09-03 04:10:56 +02:00
|
|
|
int
|
1998-05-07 01:51:16 +02:00
|
|
|
PQputline(PGconn *conn, const char *s)
|
|
|
|
{
|
2003-04-19 02:02:30 +02:00
|
|
|
return PQputnbytes(conn, s, strlen(s));
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
|
|
|
|
1998-08-17 05:50:43 +02:00
|
|
|
/*
|
|
|
|
* PQputnbytes -- like PQputline, but buffer need not be null-terminated.
|
1998-09-03 04:10:56 +02:00
|
|
|
* Returns 0 if OK, EOF if not.
|
1998-08-17 05:50:43 +02:00
|
|
|
*/
|
1998-09-03 04:10:56 +02:00
|
|
|
int
|
1998-08-17 05:50:43 +02:00
|
|
|
PQputnbytes(PGconn *conn, const char *buffer, int nbytes)
|
|
|
|
{
|
1998-09-03 04:10:56 +02:00
|
|
|
if (!conn || conn->sock < 0)
|
|
|
|
return EOF;
|
2003-04-19 02:02:30 +02:00
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
|
|
|
if (pqPutMsgStart('d', conn) < 0 ||
|
|
|
|
pqPutnchar(buffer, nbytes, conn) < 0 ||
|
|
|
|
pqPutMsgEnd(conn) < 0)
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
return 0;
|
1998-08-17 05:50:43 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/*
|
|
|
|
* PQendcopy
|
|
|
|
* After completing the data transfer portion of a copy in/out,
|
|
|
|
* the application must call this routine to finish the command protocol.
|
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* 0 on success
|
|
|
|
* 1 on failure
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
PQendcopy(PGconn *conn)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
PGresult *result;
|
1998-05-07 01:51:16 +02:00
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (conn->asyncStatus != PGASYNC_COPY_IN &&
|
|
|
|
conn->asyncStatus != PGASYNC_COPY_OUT)
|
|
|
|
{
|
1999-08-31 03:37:37 +02:00
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
2001-07-15 15:45:04 +02:00
|
|
|
libpq_gettext("no COPY in progress\n"));
|
1998-05-07 01:51:16 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
/* Send the CopyDone message if needed */
|
|
|
|
if (conn->asyncStatus == PGASYNC_COPY_IN)
|
|
|
|
{
|
|
|
|
if (pqPutMsgStart('c', conn) < 0 ||
|
|
|
|
pqPutMsgEnd(conn) < 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-01-18 07:09:24 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* make sure no data is waiting to be sent, abort if we are
|
|
|
|
* non-blocking and the flush fails
|
2000-01-18 07:09:24 +01:00
|
|
|
*/
|
|
|
|
if (pqFlush(conn) && pqIsnonblocking(conn))
|
|
|
|
return (1);
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Return to active duty */
|
|
|
|
conn->asyncStatus = PGASYNC_BUSY;
|
1999-08-31 03:37:37 +02:00
|
|
|
resetPQExpBuffer(&conn->errorMessage);
|
1998-05-07 01:51:16 +02:00
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
/*
|
|
|
|
* Non blocking connections may have to abort at this point. If everyone
|
|
|
|
* played the game there should be no problem, but in error scenarios
|
|
|
|
* the expected messages may not have arrived yet. (We are assuming that
|
|
|
|
* the backend's packetizing will ensure that CommandComplete arrives
|
|
|
|
* along with the CopyDone; are there corner cases where that doesn't
|
|
|
|
* happen?)
|
|
|
|
*/
|
|
|
|
if (pqIsnonblocking(conn) && PQisBusy(conn))
|
|
|
|
return (1);
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Wait for the completion response */
|
|
|
|
result = PQgetResult(conn);
|
|
|
|
|
|
|
|
/* Expecting a successful result */
|
1998-05-07 01:53:48 +02:00
|
|
|
if (result && result->resultStatus == PGRES_COMMAND_OK)
|
1998-05-07 01:51:16 +02:00
|
|
|
{
|
|
|
|
PQclear(result);
|
|
|
|
return 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
2003-04-22 02:08:07 +02:00
|
|
|
* Trouble. For backwards-compatibility reasons, we issue the error
|
|
|
|
* message as if it were a notice (would be nice to get rid of this
|
|
|
|
* silliness, but too many apps probably don't handle errors from
|
|
|
|
* PQendcopy reasonably). Note that the app can still obtain the
|
|
|
|
* error status from the PGconn object.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
1999-08-31 03:37:37 +02:00
|
|
|
if (conn->errorMessage.len > 0)
|
|
|
|
DONOTICE(conn, conn->errorMessage.data);
|
1998-08-17 05:50:43 +02:00
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
PQclear(result);
|
1998-05-07 01:51:16 +02:00
|
|
|
|
|
|
|
return 1;
|
1996-07-18 07:48:57 +02:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* PQfn - Send a function call to the POSTGRES backend.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* conn : backend connection
|
|
|
|
* fnid : function id
|
|
|
|
* result_buf : pointer to result buffer (&int if integer)
|
|
|
|
* result_len : length of return value.
|
|
|
|
* actual_result_len: actual length returned. (differs from result_len
|
|
|
|
* for varlena structures.)
|
|
|
|
* result_type : If the result is an integer, this must be 1,
|
|
|
|
* otherwise this should be 0
|
1998-05-07 01:51:16 +02:00
|
|
|
* args : pointer to an array of function arguments.
|
|
|
|
* (each has length, if integer, and value/pointer)
|
1997-09-07 07:04:48 +02:00
|
|
|
* nargs : # of arguments in args array.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* RETURNS
|
1998-09-01 06:40:42 +02:00
|
|
|
* PGresult with status = PGRES_COMMAND_OK if successful.
|
1998-05-07 01:51:16 +02:00
|
|
|
* *actual_result_len is > 0 if there is a return value, 0 if not.
|
1998-09-01 06:40:42 +02:00
|
|
|
* PGresult with status = PGRES_FATAL_ERROR if backend returns an error.
|
1998-05-07 01:51:16 +02:00
|
|
|
* NULL on communications failure. conn->errorMessage will be set.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
PGresult *
|
1997-09-08 23:56:23 +02:00
|
|
|
PQfn(PGconn *conn,
|
1997-09-07 07:04:48 +02:00
|
|
|
int fnid,
|
|
|
|
int *result_buf,
|
|
|
|
int *actual_result_len,
|
|
|
|
int result_is_int,
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
const PQArgBlock *args,
|
1997-09-07 07:04:48 +02:00
|
|
|
int nargs)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
bool needInput = false;
|
1998-09-01 06:40:42 +02:00
|
|
|
ExecStatusType status = PGRES_FATAL_ERROR;
|
1998-05-07 01:51:16 +02:00
|
|
|
char id;
|
2003-04-22 02:08:07 +02:00
|
|
|
int msgLength;
|
|
|
|
int avail;
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
*actual_result_len = 0;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!conn)
|
|
|
|
return NULL;
|
|
|
|
|
1999-08-31 03:37:37 +02:00
|
|
|
/* clear the error string */
|
|
|
|
resetPQExpBuffer(&conn->errorMessage);
|
|
|
|
|
|
|
|
if (conn->sock < 0 || conn->asyncStatus != PGASYNC_IDLE ||
|
|
|
|
conn->result != NULL)
|
1998-05-07 01:51:16 +02:00
|
|
|
{
|
1999-08-31 03:37:37 +02:00
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
2001-07-15 15:45:04 +02:00
|
|
|
libpq_gettext("connection in wrong state\n"));
|
1998-05-07 01:51:16 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
if (pqPutMsgStart('F', conn) < 0 || /* function call msg */
|
|
|
|
pqPutInt(fnid, 4, conn) < 0 || /* function id */
|
|
|
|
pqPutInt(1, 2, conn) < 0 || /* # of format codes */
|
|
|
|
pqPutInt(1, 2, conn) < 0 || /* format code: BINARY */
|
|
|
|
pqPutInt(nargs, 2, conn) < 0) /* # of args */
|
1999-05-28 03:54:53 +02:00
|
|
|
{
|
|
|
|
handleSendFailure(conn);
|
1998-05-07 01:51:16 +02:00
|
|
|
return NULL;
|
1999-05-28 03:54:53 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (i = 0; i < nargs; ++i)
|
|
|
|
{ /* len.int4 + contents */
|
1998-05-07 01:51:16 +02:00
|
|
|
if (pqPutInt(args[i].len, 4, conn))
|
1999-05-28 03:54:53 +02:00
|
|
|
{
|
|
|
|
handleSendFailure(conn);
|
1998-05-07 01:51:16 +02:00
|
|
|
return NULL;
|
1999-05-28 03:54:53 +02:00
|
|
|
}
|
2003-05-08 20:16:37 +02:00
|
|
|
if (args[i].len == -1)
|
|
|
|
continue; /* it's NULL */
|
1998-05-07 01:51:16 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (args[i].isint)
|
|
|
|
{
|
2003-05-08 20:16:37 +02:00
|
|
|
if (pqPutInt(args[i].u.integer, args[i].len, conn))
|
1999-05-28 03:54:53 +02:00
|
|
|
{
|
|
|
|
handleSendFailure(conn);
|
1998-05-07 01:51:16 +02:00
|
|
|
return NULL;
|
1999-05-28 03:54:53 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (pqPutnchar((char *) args[i].u.ptr, args[i].len, conn))
|
1999-05-28 03:54:53 +02:00
|
|
|
{
|
|
|
|
handleSendFailure(conn);
|
1998-05-07 01:51:16 +02:00
|
|
|
return NULL;
|
1999-05-28 03:54:53 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
2003-04-19 02:02:30 +02:00
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
if (pqPutInt(1, 2, conn) < 0) /* result format code: BINARY */
|
|
|
|
{
|
|
|
|
handleSendFailure(conn);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
if (pqPutMsgEnd(conn) < 0 ||
|
|
|
|
pqFlush(conn))
|
1999-05-28 03:54:53 +02:00
|
|
|
{
|
|
|
|
handleSendFailure(conn);
|
1998-05-07 01:51:16 +02:00
|
|
|
return NULL;
|
1999-05-28 03:54:53 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
for (;;)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (needInput)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Wait for some data to arrive (or for the channel to close) */
|
|
|
|
if (pqWait(TRUE, FALSE, conn) ||
|
|
|
|
pqReadData(conn) < 0)
|
|
|
|
break;
|
1998-04-29 04:04:01 +02:00
|
|
|
}
|
1998-09-01 06:40:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan the message. If we run out of data, loop around to try
|
|
|
|
* again.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
|
|
|
needInput = true;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
conn->inCursor = conn->inStart;
|
1998-05-07 01:51:16 +02:00
|
|
|
if (pqGetc(&id, conn))
|
|
|
|
continue;
|
2003-04-22 02:08:07 +02:00
|
|
|
if (pqGetInt(&msgLength, 4, conn))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to validate message type/length here. A length less than 4
|
|
|
|
* is definitely broken. Large lengths should only be believed
|
|
|
|
* for a few message types.
|
|
|
|
*/
|
|
|
|
if (msgLength < 4)
|
|
|
|
{
|
|
|
|
handleSyncLoss(conn, id, msgLength);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (msgLength > 30000 &&
|
2003-05-08 20:16:37 +02:00
|
|
|
!(id == 'T' || id == 'D' || id == 'd' || id == 'V'))
|
2003-04-22 02:08:07 +02:00
|
|
|
{
|
|
|
|
handleSyncLoss(conn, id, msgLength);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Can't process if message body isn't all here yet.
|
|
|
|
*/
|
|
|
|
msgLength -= 4;
|
|
|
|
avail = conn->inEnd - conn->inCursor;
|
|
|
|
if (avail < msgLength)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Before looping, enlarge the input buffer if needed to hold
|
|
|
|
* the whole message. See notes in parseInput.
|
|
|
|
*/
|
|
|
|
if (pqCheckInBufferSpace(conn->inCursor + msgLength, conn))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* XXX add some better recovery code... plan is to skip
|
|
|
|
* over the message using its length, then report an error.
|
|
|
|
* For the moment, just treat this like loss of sync (which
|
|
|
|
* indeed it might be!)
|
|
|
|
*/
|
|
|
|
handleSyncLoss(conn, id, msgLength);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
|
|
|
* We should see V or E response to the command, but might get N
|
|
|
|
* and/or A notices first. We also need to swallow the final Z
|
|
|
|
* before returning.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
switch (id)
|
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
case 'V': /* function result */
|
2003-05-08 20:16:37 +02:00
|
|
|
if (pqGetInt(actual_result_len, 4, conn))
|
1998-05-07 01:51:16 +02:00
|
|
|
continue;
|
2003-05-08 20:16:37 +02:00
|
|
|
if (*actual_result_len != -1)
|
1997-09-08 04:41:22 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (result_is_int)
|
|
|
|
{
|
2003-05-08 20:16:37 +02:00
|
|
|
if (pqGetInt(result_buf, *actual_result_len, conn))
|
1998-05-07 01:51:16 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pqGetnchar((char *) result_buf,
|
|
|
|
*actual_result_len,
|
|
|
|
conn))
|
|
|
|
continue;
|
|
|
|
}
|
1997-09-08 04:41:22 +02:00
|
|
|
}
|
2003-05-08 20:16:37 +02:00
|
|
|
/* correctly finished function result message */
|
|
|
|
status = PGRES_COMMAND_OK;
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1998-05-07 01:51:16 +02:00
|
|
|
case 'E': /* error return */
|
2003-04-24 23:16:45 +02:00
|
|
|
if (pqGetErrorNotice(conn, true))
|
1998-05-07 01:51:16 +02:00
|
|
|
continue;
|
|
|
|
status = PGRES_FATAL_ERROR;
|
|
|
|
break;
|
|
|
|
case 'A': /* notify message */
|
|
|
|
/* handle notify and go back to processing return values */
|
|
|
|
if (getNotify(conn))
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case 'N': /* notice */
|
|
|
|
/* handle notice and go back to processing return values */
|
2003-04-24 23:16:45 +02:00
|
|
|
if (pqGetErrorNotice(conn, false))
|
1998-05-07 01:51:16 +02:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case 'Z': /* backend is ready for new query */
|
2003-04-26 22:23:00 +02:00
|
|
|
if (pqGetc(&conn->xact_status, conn))
|
|
|
|
continue;
|
1998-05-07 01:51:16 +02:00
|
|
|
/* consume the message and exit */
|
2003-04-22 02:08:07 +02:00
|
|
|
conn->inStart += 5 + msgLength;
|
1999-08-31 03:37:37 +02:00
|
|
|
/* if we saved a result object (probably an error), use it */
|
|
|
|
if (conn->result)
|
|
|
|
return prepareAsyncResult(conn);
|
1998-09-03 04:10:56 +02:00
|
|
|
return PQmakeEmptyPGresult(conn, status);
|
2003-04-25 21:45:10 +02:00
|
|
|
case 'S': /* parameter status */
|
|
|
|
if (getParameterStatus(conn))
|
|
|
|
continue;
|
|
|
|
break;
|
1997-09-08 04:41:22 +02:00
|
|
|
default:
|
|
|
|
/* The backend violates the protocol. */
|
1999-08-31 03:37:37 +02:00
|
|
|
printfPQExpBuffer(&conn->errorMessage,
|
2001-10-25 07:50:21 +02:00
|
|
|
libpq_gettext("protocol error: id=0x%x\n"),
|
1999-08-31 03:37:37 +02:00
|
|
|
id);
|
|
|
|
saveErrorResult(conn);
|
2003-04-22 02:08:07 +02:00
|
|
|
/* trust the specified message length as what to skip */
|
|
|
|
conn->inStart += 5 + msgLength;
|
1999-08-31 03:37:37 +02:00
|
|
|
return prepareAsyncResult(conn);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Completed this message, keep going */
|
2003-04-22 02:08:07 +02:00
|
|
|
/* trust the specified message length as what to skip */
|
|
|
|
conn->inStart += 5 + msgLength;
|
1998-05-07 01:51:16 +02:00
|
|
|
needInput = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* We fall out of the loop only upon failing to read data.
|
|
|
|
* conn->errorMessage has been set by pqWait or pqReadData. We want to
|
|
|
|
* append it to any already-received error message.
|
1999-08-31 03:37:37 +02:00
|
|
|
*/
|
|
|
|
saveErrorResult(conn);
|
|
|
|
return prepareAsyncResult(conn);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ====== accessor funcs for PGresult ======== */
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
ExecStatusType
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQresultStatus(const PGresult *res)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
if (!res)
|
|
|
|
return PGRES_NONFATAL_ERROR;
|
|
|
|
return res->resultStatus;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-02-08 00:10:11 +01:00
|
|
|
char *
|
1999-02-07 23:08:54 +01:00
|
|
|
PQresStatus(ExecStatusType status)
|
|
|
|
{
|
2000-02-08 00:10:11 +01:00
|
|
|
if (status < 0 || status >= sizeof pgresStatus / sizeof pgresStatus[0])
|
2001-07-15 15:45:04 +02:00
|
|
|
return libpq_gettext("invalid ExecStatusType code");
|
1999-02-07 23:08:54 +01:00
|
|
|
return pgresStatus[status];
|
|
|
|
}
|
|
|
|
|
2000-02-08 00:10:11 +01:00
|
|
|
char *
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQresultErrorMessage(const PGresult *res)
|
1998-10-01 03:40:26 +02:00
|
|
|
{
|
|
|
|
if (!res || !res->errMsg)
|
|
|
|
return "";
|
|
|
|
return res->errMsg;
|
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQntuples(const PGresult *res)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!res)
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
return res->ntups;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQnfields(const PGresult *res)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!res)
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
return res->numAttributes;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-09-03 04:10:56 +02:00
|
|
|
int
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQbinaryTuples(const PGresult *res)
|
1998-09-03 04:10:56 +02:00
|
|
|
{
|
|
|
|
if (!res)
|
|
|
|
return 0;
|
|
|
|
return res->binary;
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1998-08-09 04:59:33 +02:00
|
|
|
* Helper routines to range-check field numbers and tuple numbers.
|
|
|
|
* Return TRUE if OK, FALSE if not
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2001-07-15 15:45:04 +02:00
|
|
|
check_field_number(const PGresult *res, int field_num)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
char noticeBuf[128];
|
1999-08-31 03:37:37 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!res)
|
1998-08-09 04:59:33 +02:00
|
|
|
return FALSE; /* no way to display error message... */
|
|
|
|
if (field_num < 0 || field_num >= res->numAttributes)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-03-11 04:08:37 +01:00
|
|
|
if (res->noticeHook)
|
1998-11-18 01:47:28 +01:00
|
|
|
{
|
2001-07-15 15:45:04 +02:00
|
|
|
snprintf(noticeBuf, sizeof(noticeBuf),
|
2001-10-25 07:50:21 +02:00
|
|
|
libpq_gettext("column number %d is out of range 0..%d\n"),
|
2001-07-15 15:45:04 +02:00
|
|
|
field_num, res->numAttributes - 1);
|
2000-03-11 04:08:37 +01:00
|
|
|
DONOTICE(res, noticeBuf);
|
1998-11-18 01:47:28 +01:00
|
|
|
}
|
1998-08-09 04:59:33 +02:00
|
|
|
return FALSE;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-08-09 04:59:33 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-09 04:59:33 +02:00
|
|
|
static int
|
2001-07-15 15:45:04 +02:00
|
|
|
check_tuple_field_number(const PGresult *res,
|
1998-08-09 04:59:33 +02:00
|
|
|
int tup_num, int field_num)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
char noticeBuf[128];
|
1999-08-31 03:37:37 +02:00
|
|
|
|
1998-08-09 04:59:33 +02:00
|
|
|
if (!res)
|
|
|
|
return FALSE; /* no way to display error message... */
|
|
|
|
if (tup_num < 0 || tup_num >= res->ntups)
|
|
|
|
{
|
2000-03-11 04:08:37 +01:00
|
|
|
if (res->noticeHook)
|
1998-11-18 01:47:28 +01:00
|
|
|
{
|
2001-07-15 15:45:04 +02:00
|
|
|
snprintf(noticeBuf, sizeof(noticeBuf),
|
2001-10-25 07:50:21 +02:00
|
|
|
libpq_gettext("row number %d is out of range 0..%d\n"),
|
|
|
|
tup_num, res->ntups - 1);
|
2000-03-11 04:08:37 +01:00
|
|
|
DONOTICE(res, noticeBuf);
|
1998-11-18 01:47:28 +01:00
|
|
|
}
|
1998-08-09 04:59:33 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
1998-05-07 01:51:16 +02:00
|
|
|
if (field_num < 0 || field_num >= res->numAttributes)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-03-11 04:08:37 +01:00
|
|
|
if (res->noticeHook)
|
1998-11-18 01:47:28 +01:00
|
|
|
{
|
2001-07-15 15:45:04 +02:00
|
|
|
snprintf(noticeBuf, sizeof(noticeBuf),
|
2001-10-25 07:50:21 +02:00
|
|
|
libpq_gettext("column number %d is out of range 0..%d\n"),
|
|
|
|
field_num, res->numAttributes - 1);
|
2000-03-11 04:08:37 +01:00
|
|
|
DONOTICE(res, noticeBuf);
|
1998-11-18 01:47:28 +01:00
|
|
|
}
|
1998-08-09 04:59:33 +02:00
|
|
|
return FALSE;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-08-09 04:59:33 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
returns NULL if the field_num is invalid
|
|
|
|
*/
|
2000-02-08 00:10:11 +01:00
|
|
|
char *
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQfname(const PGresult *res, int field_num)
|
1998-08-09 04:59:33 +02:00
|
|
|
{
|
2001-07-15 15:45:04 +02:00
|
|
|
if (!check_field_number(res, field_num))
|
1998-08-09 04:59:33 +02:00
|
|
|
return NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
if (res->attDescs)
|
|
|
|
return res->attDescs[field_num].name;
|
|
|
|
else
|
|
|
|
return NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
returns -1 on a bad field name
|
|
|
|
*/
|
|
|
|
int
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQfnumber(const PGresult *res, const char *field_name)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-11-10 06:10:50 +01:00
|
|
|
char *field_case;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!res)
|
|
|
|
return -1;
|
1996-07-12 06:53:59 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (field_name == NULL ||
|
|
|
|
field_name[0] == '\0' ||
|
|
|
|
res->attDescs == NULL)
|
|
|
|
return -1;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-11-10 06:10:50 +01:00
|
|
|
field_case = strdup(field_name);
|
|
|
|
if (*field_case == '"')
|
|
|
|
{
|
|
|
|
strcpy(field_case, field_case + 1);
|
|
|
|
*(field_case + strlen(field_case) - 1) = '\0';
|
|
|
|
}
|
|
|
|
else
|
1997-12-05 02:13:24 +01:00
|
|
|
for (i = 0; field_case[i]; i++)
|
2000-12-03 21:45:40 +01:00
|
|
|
if (isupper((unsigned char) field_case[i]))
|
|
|
|
field_case[i] = tolower((unsigned char) field_case[i]);
|
1997-11-10 06:10:50 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
for (i = 0; i < res->numAttributes; i++)
|
|
|
|
{
|
1999-02-03 21:19:10 +01:00
|
|
|
if (strcmp(field_case, res->attDescs[i].name) == 0)
|
1997-11-10 06:10:50 +01:00
|
|
|
{
|
|
|
|
free(field_case);
|
1997-09-07 07:04:48 +02:00
|
|
|
return i;
|
1997-11-10 06:10:50 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-11-10 06:10:50 +01:00
|
|
|
free(field_case);
|
1997-09-07 07:04:48 +02:00
|
|
|
return -1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Oid
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQftype(const PGresult *res, int field_num)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-07-15 15:45:04 +02:00
|
|
|
if (!check_field_number(res, field_num))
|
1997-09-07 07:04:48 +02:00
|
|
|
return InvalidOid;
|
|
|
|
if (res->attDescs)
|
1998-07-13 18:35:04 +02:00
|
|
|
return res->attDescs[field_num].typid;
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
|
|
|
return InvalidOid;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-09-03 04:10:56 +02:00
|
|
|
int
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQfsize(const PGresult *res, int field_num)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-07-15 15:45:04 +02:00
|
|
|
if (!check_field_number(res, field_num))
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
if (res->attDescs)
|
1998-07-13 04:41:59 +02:00
|
|
|
return res->attDescs[field_num].typlen;
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-07-14 04:41:26 +02:00
|
|
|
int
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQfmod(const PGresult *res, int field_num)
|
1998-05-07 01:51:16 +02:00
|
|
|
{
|
2001-07-15 15:45:04 +02:00
|
|
|
if (!check_field_number(res, field_num))
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
|
|
|
if (res->attDescs)
|
1998-07-13 04:41:59 +02:00
|
|
|
return res->attDescs[field_num].atttypmod;
|
1998-05-07 01:51:16 +02:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-08 00:10:11 +01:00
|
|
|
char *
|
|
|
|
PQcmdStatus(PGresult *res)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
if (!res)
|
|
|
|
return NULL;
|
|
|
|
return res->cmdStatus;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
PQoidStatus -
|
1997-09-07 07:04:48 +02:00
|
|
|
if the last command was an INSERT, return the oid string
|
|
|
|
if not, return ""
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-08 00:10:11 +01:00
|
|
|
char *
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQoidStatus(const PGresult *res)
|
1997-08-27 11:05:24 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* This must be enough to hold the result. Don't laugh, this is better
|
|
|
|
* than what this function used to do.
|
2000-03-11 04:08:37 +01:00
|
|
|
*/
|
|
|
|
static char buf[24];
|
1998-05-07 01:51:16 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
size_t len;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
if (!res || !res->cmdStatus || strncmp(res->cmdStatus, "INSERT ", 7) != 0)
|
1998-08-17 05:50:43 +02:00
|
|
|
return "";
|
1997-09-07 07:04:48 +02:00
|
|
|
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
len = strspn(res->cmdStatus + 7, "0123456789");
|
|
|
|
if (len > 23)
|
2000-03-11 04:08:37 +01:00
|
|
|
len = 23;
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
strncpy(buf, res->cmdStatus + 7, len);
|
2001-02-06 03:00:09 +01:00
|
|
|
buf[len] = '\0';
|
1997-09-07 07:04:48 +02:00
|
|
|
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
return buf;
|
|
|
|
}
|
1998-08-17 05:50:43 +02:00
|
|
|
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
/*
|
|
|
|
PQoidValue -
|
2003-02-19 04:59:02 +01:00
|
|
|
a perhaps preferable form of the above which just returns
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
an Oid type
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
PQoidValue(const PGresult *res)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
char *endptr = NULL;
|
2001-02-06 03:02:27 +01:00
|
|
|
unsigned long result;
|
1998-08-17 05:50:43 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!res || !res->cmdStatus || strncmp(res->cmdStatus, "INSERT ", 7) != 0)
|
|
|
|
return InvalidOid;
|
1998-08-17 05:50:43 +02:00
|
|
|
|
2001-08-21 22:39:54 +02:00
|
|
|
#ifdef WIN32
|
2001-10-25 07:50:21 +02:00
|
|
|
SetLastError(0);
|
2001-08-21 22:39:54 +02:00
|
|
|
#else
|
2000-04-12 19:17:23 +02:00
|
|
|
errno = 0;
|
2001-08-21 22:39:54 +02:00
|
|
|
#endif
|
2000-04-12 19:17:23 +02:00
|
|
|
result = strtoul(res->cmdStatus + 7, &endptr, 10);
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!endptr || (*endptr != ' ' && *endptr != '\0') || errno == ERANGE)
|
|
|
|
return InvalidOid;
|
|
|
|
else
|
|
|
|
return (Oid) result;
|
1997-08-27 11:05:24 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-08-04 00:11:39 +02:00
|
|
|
|
1997-08-27 11:05:24 +02:00
|
|
|
/*
|
|
|
|
PQcmdTuples -
|
2003-02-19 04:59:02 +01:00
|
|
|
If the last command was an INSERT/UPDATE/DELETE/MOVE/FETCH, return a
|
|
|
|
string containing the number of inserted/affected tuples. If not,
|
|
|
|
return "".
|
|
|
|
|
|
|
|
XXX: this should probably return an int
|
1997-08-27 11:05:24 +02:00
|
|
|
*/
|
2000-02-08 00:10:11 +01:00
|
|
|
char *
|
|
|
|
PQcmdTuples(PGresult *res)
|
1997-08-27 11:05:24 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
char noticeBuf[128];
|
2003-02-19 04:59:02 +01:00
|
|
|
char *p;
|
1999-08-31 03:37:37 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!res)
|
1998-08-09 04:59:33 +02:00
|
|
|
return "";
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-02-19 04:59:02 +01:00
|
|
|
if (strncmp(res->cmdStatus, "INSERT ", 7) == 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-02-19 04:59:02 +01:00
|
|
|
p = res->cmdStatus + 6;
|
1997-09-07 07:04:48 +02:00
|
|
|
p++;
|
2003-02-19 04:59:02 +01:00
|
|
|
/* INSERT: skip oid */
|
1997-09-07 07:04:48 +02:00
|
|
|
while (*p != ' ' && *p)
|
2003-02-19 04:59:02 +01:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else if (strncmp(res->cmdStatus, "DELETE ", 7) == 0 ||
|
|
|
|
strncmp(res->cmdStatus, "UPDATE ", 7) == 0)
|
|
|
|
p = res->cmdStatus + 6;
|
|
|
|
else if (strncmp(res->cmdStatus, "FETCH ", 6) == 0)
|
|
|
|
p = res->cmdStatus + 5;
|
|
|
|
else if (strncmp(res->cmdStatus, "MOVE ", 5) == 0)
|
|
|
|
p = res->cmdStatus + 4;
|
|
|
|
else
|
|
|
|
return "";
|
|
|
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p == 0)
|
|
|
|
{
|
|
|
|
if (res->noticeHook)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-02-19 04:59:02 +01:00
|
|
|
snprintf(noticeBuf, sizeof(noticeBuf),
|
|
|
|
libpq_gettext("could not interpret result from server: %s\n"),
|
|
|
|
res->cmdStatus);
|
|
|
|
DONOTICE(res, noticeBuf);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-02-19 04:59:02 +01:00
|
|
|
return "";
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-02-19 04:59:02 +01:00
|
|
|
|
|
|
|
return p;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
PQgetvalue:
|
1997-09-07 07:04:48 +02:00
|
|
|
return the value of field 'field_num' of row 'tup_num'
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
If res is binary, then the value returned is NOT a null-terminated
|
|
|
|
ASCII string, but the binary representation in the server's native
|
|
|
|
format.
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if res is not binary, a null-terminated ASCII string is returned.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-08 00:10:11 +01:00
|
|
|
char *
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQgetvalue(const PGresult *res, int tup_num, int field_num)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-07-15 15:45:04 +02:00
|
|
|
if (!check_tuple_field_number(res, tup_num, field_num))
|
1997-09-07 07:04:48 +02:00
|
|
|
return NULL;
|
|
|
|
return res->tuples[tup_num][field_num].value;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* PQgetlength:
|
1997-09-07 07:04:48 +02:00
|
|
|
returns the length of a field value in bytes. If res is binary,
|
|
|
|
i.e. a result of a binary portal, then the length returned does
|
1998-11-18 01:47:28 +01:00
|
|
|
NOT include the size field of the varlena. (The data returned
|
|
|
|
by PQgetvalue doesn't either.)
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
int
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQgetlength(const PGresult *res, int tup_num, int field_num)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-07-15 15:45:04 +02:00
|
|
|
if (!check_tuple_field_number(res, tup_num, field_num))
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
if (res->tuples[tup_num][field_num].len != NULL_LEN)
|
|
|
|
return res->tuples[tup_num][field_num].len;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
1996-08-13 03:34:29 +02:00
|
|
|
|
|
|
|
/* PQgetisnull:
|
1997-09-07 07:04:48 +02:00
|
|
|
returns the null status of a field value.
|
1996-08-13 03:34:29 +02:00
|
|
|
*/
|
|
|
|
int
|
In the spirit of TODO item
* Add use of 'const' for varibles in source tree
(which is misspelled, btw.)
I went through the front-end libpq code and did so. This affects in
particular the various accessor functions (such as PQdb() and
PQgetvalue()) as well as, by necessity, the internal helpers they use.
I have been really thorough in that regard, perhaps some people will find
it annoying that things like
char * foo = PQgetvalue(res, 0, 0)
will generate a warning. On the other hand it _should_ generate one. This
is no real compatibility break, although a few clients will have to be
fixed to suppress warnings. (Which again would be in the spirit of the
above TODO.)
In addition I replaced some int's by size_t's and removed some warnings
(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so it
actually honors the const!) and supplied a new function PQoidValue that
returns a proper Oid type. This is only front-end stuff, none of the
communicaton stuff was touched.
The psql patch also adds some new consts to honor the new libpq situation,
as well as fixes a fatal condition that resulted when using the -V
(--version) option and there is no database listening.
So, to summarize, the psql you should definitely put in (with or without
the libpq). If you think I went too far with the const-mania in libpq, let
me know and I'll make adjustments. If you approve it, I will also update
the docs.
-Peter
--
Peter Eisentraut Sernanders vaeg 10:115
1999-11-11 01:10:14 +01:00
|
|
|
PQgetisnull(const PGresult *res, int tup_num, int field_num)
|
1996-08-13 03:34:29 +02:00
|
|
|
{
|
2001-07-15 15:45:04 +02:00
|
|
|
if (!check_tuple_field_number(res, tup_num, field_num))
|
1998-05-07 01:51:16 +02:00
|
|
|
return 1; /* pretend it is null */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (res->tuples[tup_num][field_num].len == NULL_LEN)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
1997-05-20 05:39:02 +02:00
|
|
|
}
|
2000-01-18 07:09:24 +01:00
|
|
|
|
|
|
|
/* PQsetnonblocking:
|
|
|
|
sets the PGconn's database connection non-blocking if the arg is TRUE
|
|
|
|
or makes it non-blocking if the arg is FALSE, this will not protect
|
|
|
|
you from PQexec(), you'll only be safe when using the non-blocking
|
|
|
|
API
|
|
|
|
Needs to be called only on a connected database connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
PQsetnonblocking(PGconn *conn, int arg)
|
|
|
|
{
|
|
|
|
arg = (arg == TRUE) ? 1 : 0;
|
|
|
|
/* early out if the socket is already in the state requested */
|
|
|
|
if (arg == conn->nonblocking)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* to guarantee constancy for flushing/query/result-polling behavior
|
|
|
|
* we need to flush the send queue at this point in order to guarantee
|
2000-04-12 19:17:23 +02:00
|
|
|
* proper behavior. this is ok because either they are making a
|
|
|
|
* transition _from_ or _to_ blocking mode, either way we can block
|
|
|
|
* them.
|
2000-01-18 07:09:24 +01:00
|
|
|
*/
|
|
|
|
/* if we are going from blocking to non-blocking flush here */
|
2000-01-24 03:12:58 +01:00
|
|
|
if (pqFlush(conn))
|
2000-01-18 07:09:24 +01:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
conn->nonblocking = arg;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the blocking status of the database connection, TRUE == nonblocking,
|
|
|
|
FALSE == blocking
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
PQisnonblocking(const PGconn *conn)
|
|
|
|
{
|
|
|
|
return (pqIsnonblocking(conn));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try to force data out, really only useful for non-blocking users */
|
|
|
|
int
|
|
|
|
PQflush(PGconn *conn)
|
|
|
|
{
|
|
|
|
return (pqFlush(conn));
|
|
|
|
}
|
2002-03-05 06:20:12 +01:00
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* PQfreemem - safely frees memory allocated
|
|
|
|
*
|
|
|
|
* Needed mostly by Win32, unless multithreaded DLL (/MD in VC6)
|
|
|
|
* Used for freeing memory from PQescapeByte()a/PQunescapeBytea()
|
|
|
|
*/
|
|
|
|
void PQfreemem(void *ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2003-03-25 03:44:36 +01:00
|
|
|
/*
|
|
|
|
* PQfreeNotify - free's the memory associated with a PGnotify
|
|
|
|
*
|
|
|
|
* This function is here only for binary backward compatibility.
|
|
|
|
* New code should use PQfreemem(). A macro will automatically map
|
|
|
|
* calls to PQfreemem. It should be removed in the future. bjm 2003-03-24
|
|
|
|
*/
|
|
|
|
|
|
|
|
#undef PQfreeNotify
|
|
|
|
void PQfreeNotify(PGnotify *notify);
|
|
|
|
|
|
|
|
void
|
|
|
|
PQfreeNotify(PGnotify *notify)
|
|
|
|
{
|
|
|
|
PQfreemem(notify);
|
|
|
|
}
|
2003-05-08 20:16:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* ---------------
|
|
|
|
* Escaping arbitrary strings to get valid SQL strings/identifiers.
|
|
|
|
*
|
|
|
|
* Replaces "\\" with "\\\\" and "'" with "''".
|
|
|
|
* length is the length of the buffer pointed to by
|
|
|
|
* from. The buffer at to must be at least 2*length + 1 characters
|
|
|
|
* long. A terminating NUL character is written.
|
|
|
|
* ---------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
size_t
|
|
|
|
PQescapeString(char *to, const char *from, size_t length)
|
|
|
|
{
|
|
|
|
const char *source = from;
|
|
|
|
char *target = to;
|
|
|
|
unsigned int remaining = length;
|
|
|
|
|
|
|
|
while (remaining > 0)
|
|
|
|
{
|
|
|
|
switch (*source)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
*target = '\\';
|
|
|
|
target++;
|
|
|
|
*target = '\\';
|
|
|
|
/* target and remaining are updated below. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\'':
|
|
|
|
*target = '\'';
|
|
|
|
target++;
|
|
|
|
*target = '\'';
|
|
|
|
/* target and remaining are updated below. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*target = *source;
|
|
|
|
/* target and remaining are updated below. */
|
|
|
|
}
|
|
|
|
source++;
|
|
|
|
target++;
|
|
|
|
remaining--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the terminating NUL character. */
|
|
|
|
*target = '\0';
|
|
|
|
|
|
|
|
return target - to;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PQescapeBytea - converts from binary string to the
|
|
|
|
* minimal encoding necessary to include the string in an SQL
|
|
|
|
* INSERT statement with a bytea type column as the target.
|
|
|
|
*
|
|
|
|
* The following transformations are applied
|
|
|
|
* '\0' == ASCII 0 == \\000
|
|
|
|
* '\'' == ASCII 39 == \'
|
|
|
|
* '\\' == ASCII 92 == \\\\
|
|
|
|
* anything >= 0x80 ---> \\ooo (where ooo is an octal expression)
|
|
|
|
*/
|
|
|
|
unsigned char *
|
|
|
|
PQescapeBytea(const unsigned char *bintext, size_t binlen, size_t *bytealen)
|
|
|
|
{
|
|
|
|
const unsigned char *vp;
|
|
|
|
unsigned char *rp;
|
|
|
|
unsigned char *result;
|
|
|
|
size_t i;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* empty string has 1 char ('\0')
|
|
|
|
*/
|
|
|
|
len = 1;
|
|
|
|
|
|
|
|
vp = bintext;
|
|
|
|
for (i = binlen; i > 0; i--, vp++)
|
|
|
|
{
|
|
|
|
if (*vp == 0 || *vp >= 0x80)
|
|
|
|
len += 5; /* '5' is for '\\ooo' */
|
|
|
|
else if (*vp == '\'')
|
|
|
|
len += 2;
|
|
|
|
else if (*vp == '\\')
|
|
|
|
len += 4;
|
|
|
|
else
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rp = result = (unsigned char *) malloc(len);
|
|
|
|
if (rp == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
vp = bintext;
|
|
|
|
*bytealen = len;
|
|
|
|
|
|
|
|
for (i = binlen; i > 0; i--, vp++)
|
|
|
|
{
|
|
|
|
if (*vp == 0 || *vp >= 0x80)
|
|
|
|
{
|
|
|
|
(void) sprintf(rp, "\\\\%03o", *vp);
|
|
|
|
rp += 5;
|
|
|
|
}
|
|
|
|
else if (*vp == '\'')
|
|
|
|
{
|
|
|
|
rp[0] = '\\';
|
|
|
|
rp[1] = '\'';
|
|
|
|
rp += 2;
|
|
|
|
}
|
|
|
|
else if (*vp == '\\')
|
|
|
|
{
|
|
|
|
rp[0] = '\\';
|
|
|
|
rp[1] = '\\';
|
|
|
|
rp[2] = '\\';
|
|
|
|
rp[3] = '\\';
|
|
|
|
rp += 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*rp++ = *vp;
|
|
|
|
}
|
|
|
|
*rp = '\0';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PQunescapeBytea - converts the null terminated string representation
|
|
|
|
* of a bytea, strtext, into binary, filling a buffer. It returns a
|
|
|
|
* pointer to the buffer which is NULL on error, and the size of the
|
|
|
|
* buffer in retbuflen. The pointer may subsequently be used as an
|
|
|
|
* argument to the function free(3). It is the reverse of PQescapeBytea.
|
|
|
|
*
|
|
|
|
* The following transformations are reversed:
|
|
|
|
* '\0' == ASCII 0 == \000
|
|
|
|
* '\'' == ASCII 39 == \'
|
|
|
|
* '\\' == ASCII 92 == \\
|
|
|
|
*
|
|
|
|
* States:
|
|
|
|
* 0 normal 0->1->2->3->4
|
|
|
|
* 1 \ 1->5
|
|
|
|
* 2 \0 1->6
|
|
|
|
* 3 \00
|
|
|
|
* 4 \000
|
|
|
|
* 5 \'
|
|
|
|
* 6 \\
|
|
|
|
*/
|
|
|
|
unsigned char *
|
|
|
|
PQunescapeBytea(const unsigned char *strtext, size_t *retbuflen)
|
|
|
|
{
|
|
|
|
size_t buflen;
|
|
|
|
unsigned char *buffer,
|
|
|
|
*bp;
|
|
|
|
const unsigned char *sp;
|
|
|
|
unsigned int state = 0;
|
|
|
|
|
|
|
|
if (strtext == NULL)
|
|
|
|
return NULL;
|
|
|
|
buflen = strlen(strtext); /* will shrink, also we discover if
|
|
|
|
* strtext */
|
|
|
|
buffer = (unsigned char *) malloc(buflen); /* isn't NULL terminated */
|
|
|
|
if (buffer == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (bp = buffer, sp = strtext; *sp != '\0'; bp++, sp++)
|
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
if (*sp == '\\')
|
|
|
|
state = 1;
|
|
|
|
*bp = *sp;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (*sp == '\'') /* state=5 */
|
|
|
|
{ /* replace \' with 39 */
|
|
|
|
bp--;
|
|
|
|
*bp = '\'';
|
|
|
|
buflen--;
|
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
else if (*sp == '\\') /* state=6 */
|
|
|
|
{ /* replace \\ with 92 */
|
|
|
|
bp--;
|
|
|
|
*bp = '\\';
|
|
|
|
buflen--;
|
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (isdigit(*sp))
|
|
|
|
state = 2;
|
|
|
|
else
|
|
|
|
state = 0;
|
|
|
|
*bp = *sp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (isdigit(*sp))
|
|
|
|
state = 3;
|
|
|
|
else
|
|
|
|
state = 0;
|
|
|
|
*bp = *sp;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (isdigit(*sp)) /* state=4 */
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
bp -= 3;
|
|
|
|
sscanf(sp - 2, "%03o", &v);
|
|
|
|
*bp = v;
|
|
|
|
buflen -= 3;
|
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*bp = *sp;
|
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer = realloc(buffer, buflen);
|
|
|
|
if (buffer == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*retbuflen = buflen;
|
|
|
|
return buffer;
|
|
|
|
}
|