*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.68 1998/09/10 15:18:02 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.69 1998/10/01 01:40:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
static void freeTuple(PGresAttValue *tuple, int numAttributes);
-static void addTuple(PGresult *res, PGresAttValue *tup);
+static int addTuple(PGresult *res, PGresAttValue *tup);
static void parseInput(PGconn *conn);
static int getRowDescriptions(PGconn *conn);
static int getAnotherTuple(PGconn *conn, int binary);
/*
* PQmakeEmptyPGresult
- * returns a newly allocated, initialized PGresult with given status
+ * 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.
*
* 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
result = (PGresult *) malloc(sizeof(PGresult));
- result->conn = conn;
+ result->conn = conn; /* should go away eventually */
result->ntups = 0;
result->numAttributes = 0;
result->attDescs = NULL;
result->resultStatus = status;
result->cmdStatus[0] = '\0';
result->binary = 0;
+ result->errMsg = NULL;
+ if (conn) /* consider copying conn's errorMessage */
+ {
+ 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:
+ pqSetResultError(result, conn->errorMessage);
+ break;
+ }
+ }
return result;
}
+/*
+ * pqSetResultError -
+ * assign a new error message to a PGresult
+ */
+void
+pqSetResultError(PGresult *res, const char *msg)
+{
+ if (!res)
+ return;
+ if (res->errMsg)
+ free(res->errMsg);
+ res->errMsg = NULL;
+ if (msg && *msg)
+ res->errMsg = strdup(msg);
+}
+
/*
* PQclear -
* free's the memory associated with a PGresult
- *
*/
void
PQclear(PGresult *res)
free(res->attDescs);
}
+ /* free the error text */
+ if (res->errMsg)
+ free(res->errMsg);
+
/* free the structure itself */
free(res);
}
/*
* addTuple
* add a row to the PGresult structure, growing it if necessary
+ * Returns TRUE if OK, FALSE if not enough memory to add the row
*/
-static void
+static int
addTuple(PGresult *res, PGresAttValue *tup)
{
if (res->ntups >= res->tupArrSize)
{
- /* grow the array */
- res->tupArrSize += TUPARR_GROW_BY;
-
/*
- * we can use realloc because shallow copying of the structure is
+ * Try to grow the array.
+ *
+ * We can use realloc because shallow copying of the structure is
* okay. Note that the first time through, res->tuples is NULL.
- * realloc is supposed to do the right thing in that case. Also
- * note that the positions beyond res->ntups are garbage, not
+ * realloc is supposed to do the right thing in that case. Also,
+ * on failure realloc is supposed to return NULL without damaging
+ * the existing allocation.
+ * Note that the positions beyond res->ntups are garbage, not
* necessarily NULL.
*/
- res->tuples = (PGresAttValue **)
- realloc(res->tuples, res->tupArrSize * sizeof(PGresAttValue *));
+ int newSize = res->tupArrSize + TUPARR_GROW_BY;
+ PGresAttValue ** newTuples = (PGresAttValue **)
+ realloc(res->tuples, newSize * sizeof(PGresAttValue *));
+ if (! newTuples)
+ return FALSE; /* realloc failed */
+ res->tupArrSize = newSize;
+ res->tuples = newTuples;
}
res->tuples[res->ntups] = tup;
res->ntups++;
+ return TRUE;
}
/* initialize async result-accumulation state */
conn->result = NULL;
conn->curTuple = NULL;
- conn->asyncErrorMessage[0] = '\0';
/* send the query to the backend; */
/* the frontend-backend protocol uses 'Q' to designate queries */
* application wants to get rid of a read-select condition. Note that
* we will NOT block waiting for more input.
*/
- if (pqReadData(conn) < 0) {
- strcpy(conn->asyncErrorMessage, conn->errorMessage);
+ if (pqReadData(conn) < 0)
return 0;
- }
/* Parsing of the data waits till later. */
return 1;
}
conn->asyncStatus = PGASYNC_READY;
break;
case 'E': /* error return */
- if (pqGets(conn->asyncErrorMessage, ERROR_MSG_LENGTH, conn))
+ if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, conn))
return;
/* delete any partially constructed result */
pqClearAsyncResult(conn);
-
- /*
- * we leave result NULL while setting
- * asyncStatus=READY; this signals an error condition
- * to PQgetResult.
- */
+ /* and build an error result holding the error message */
+ conn->result = PQmakeEmptyPGresult(conn,
+ PGRES_FATAL_ERROR);
conn->asyncStatus = PGASYNC_READY;
break;
case 'Z': /* backend is ready for new query */
conn->asyncStatus = PGASYNC_COPY_OUT;
break;
default:
- sprintf(conn->asyncErrorMessage,
+ sprintf(conn->errorMessage,
"unknown protocol character '%c' read from backend. "
"(The protocol character is the first character the "
- "backend sends in response to a query it receives).\n",
+ "backend sends in response to a query it receives).\n",
id);
/* Discard the unexpected message; good idea?? */
conn->inStart = conn->inEnd;
/* delete any partially constructed result */
pqClearAsyncResult(conn);
+ /* and build an error result holding the error message */
+ conn->result = PQmakeEmptyPGresult(conn,
+ PGRES_FATAL_ERROR);
conn->asyncStatus = PGASYNC_READY;
return;
} /* switch on protocol character */
/*
* parseInput subroutine to read a 'B' or 'D' (row data) message.
* We add another tuple to the existing PGresult structure.
- * Returns: 0 if completed message, EOF if not enough data yet.
+ * Returns: 0 if completed message, EOF if error or not enough data yet.
*
* 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
{
conn->curTuple = (PGresAttValue *)
malloc(nfields * sizeof(PGresAttValue));
+ if (conn->curTuple == NULL)
+ goto outOfMemory;
MemSet((char *) conn->curTuple, 0, nfields * sizeof(PGresAttValue));
}
tup = conn->curTuple;
nbytes = (nfields + BYTELEN - 1) / BYTELEN;
if (nbytes >= MAX_FIELDS)
{
- sprintf(conn->asyncErrorMessage,
- "getAnotherTuple() -- null-values bitmap is too large\n");
+ /* Replace partially constructed result with an error result */
pqClearAsyncResult(conn);
+ sprintf(conn->errorMessage,
+ "getAnotherTuple() -- null-values bitmap is too large\n");
+ conn->result = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR);
conn->asyncStatus = PGASYNC_READY;
/* Discard the broken message */
conn->inStart = conn->inEnd;
{
/* if the field value is absent, make it a null string */
if (tup[i].value == NULL)
+ {
tup[i].value = strdup("");
+ if (tup[i].value == NULL)
+ goto outOfMemory;
+ }
tup[i].len = NULL_LEN;
}
else
if (vlen < 0)
vlen = 0;
if (tup[i].value == NULL)
+ {
tup[i].value = (char *) malloc(vlen + 1);
+ if (tup[i].value == NULL)
+ goto outOfMemory;
+ }
tup[i].len = vlen;
/* read in the value */
if (vlen > 0)
}
/* Success! Store the completed tuple in the result */
- addTuple(conn->result, tup);
+ if (! addTuple(conn->result, tup))
+ {
+ /* Oops, not enough memory to add the tuple to conn->result,
+ * so must free it ourselves...
+ */
+ freeTuple(tup, nfields);
+ goto outOfMemory;
+ }
/* and reset for a new message */
conn->curTuple = NULL;
return 0;
+
+outOfMemory:
+ /* Replace partially constructed result with an error result */
+ pqClearAsyncResult(conn);
+ sprintf(conn->errorMessage,
+ "getAnotherTuple() -- out of memory for result\n");
+ conn->result = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR);
+ conn->asyncStatus = PGASYNC_READY;
+ /* Discard the failed message --- good idea? */
+ conn->inStart = conn->inEnd;
+ return EOF;
}
res = NULL; /* query is complete */
break;
case PGASYNC_READY:
-
/*
- * conn->result is the PGresult to return, or possibly NULL
- * indicating an error. conn->asyncErrorMessage holds the
- * errorMessage to return. (We keep it stashed there so that
- * other user calls can't overwrite it prematurely.)
+ * 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.
*/
res = conn->result;
- conn->result = NULL;/* handing over ownership to caller */
+ conn->result = NULL; /* handing over ownership to caller */
conn->curTuple = NULL; /* just in case */
if (!res)
+ {
res = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR);
- strcpy(conn->errorMessage, conn->asyncErrorMessage);
+ }
+ else
+ {
+ /* Make sure PQerrorMessage agrees with result; it could be
+ * that we have done other operations that changed
+ * errorMessage since the result's error message was saved.
+ */
+ strcpy(conn->errorMessage, PQresultErrorMessage(res));
+ }
/* Set the state back to BUSY, allowing parsing to proceed. */
conn->asyncStatus = PGASYNC_BUSY;
break;
* PQexec
* send a query to the backend and package up the result in a PGresult
*
- * if the query failed, return NULL, conn->errorMessage is set to
- * a relevant message
- * if query is successful, a new PGresult is returned
- * the user is responsible for freeing that structure when done with it
- *
+ * 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.
*/
PGresult *
return res->resultStatus;
}
+const char *
+PQresultErrorMessage(PGresult *res)
+{
+ if (!res || !res->errMsg)
+ return "";
+ return res->errMsg;
+}
+
int
PQntuples(PGresult *res)
{
*
* Copyright (c) 1994, Regents of the University of California
*
- * $Id: libpq-fe.h,v 1.43 1998/09/18 16:46:06 momjian Exp $
+ * $Id: libpq-fe.h,v 1.44 1998/10/01 01:40:23 tgl Exp $
*
*-------------------------------------------------------------------------
*/
/* PGnotify represents the occurrence of a NOTIFY message.
* Ideally this would be an opaque typedef, but it's so simple that it's
* unlikely to change.
+ * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
+ * whereas in earlier versions it was always your own backend's PID.
*/
typedef struct pgNotify
{
/* PQnoticeProcessor is the function type for the notice-message callback.
*/
-typedef void (*PQnoticeProcessor) (void * arg, const char * message);
+ typedef void (*PQnoticeProcessor) (void * arg, const char * message);
/* Print options for PQprint() */
* use
*/
extern PGresult *PQfn(PGconn *conn,
- int fnid,
- int *result_buf,
- int *result_len,
- int result_is_int,
- PQArgBlock *args,
- int nargs);
+ int fnid,
+ int *result_buf,
+ int *result_len,
+ int result_is_int,
+ PQArgBlock *args,
+ int nargs);
/* Accessor functions for PGresult objects */
extern ExecStatusType PQresultStatus(PGresult *res);
+ extern const char *PQresultErrorMessage(PGresult *res);
extern int PQntuples(PGresult *res);
extern int PQnfields(PGresult *res);
extern int PQbinaryTuples(PGresult *res);
/* Delete a PGresult */
extern void PQclear(PGresult *res);
- /* Make an empty PGresult with given status (some apps find this useful) */
+ /* Make an empty PGresult with given status (some apps find this useful).
+ * If conn is not NULL and status indicates an error, the conn's
+ * errorMessage is copied.
+ */
extern PGresult * PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
/* === in fe-print.c === */
- extern void PQprint(FILE *fout, /* output stream */
- PGresult *res,
- PQprintOpt *ps); /* option structure */
+ extern void PQprint(FILE *fout, /* output stream */
+ PGresult *res,
+ PQprintOpt *ps); /* option structure */
/*
* PQdisplayTuples() is a better version of PQprintTuples(), but both
* are obsoleted by PQprint().
*/
extern void PQdisplayTuples(PGresult *res,
- FILE *fp, /* where to send the
- * output */
- int fillAlign, /* pad the fields with
- * spaces */
- const char *fieldSep, /* field separator */
- int printHeader, /* display headers? */
- int quiet);
+ FILE *fp, /* where to send the
+ * output */
+ int fillAlign, /* pad the fields with
+ * spaces */
+ const char *fieldSep, /* field separator */
+ int printHeader, /* display headers? */
+ int quiet);
+
extern void PQprintTuples(PGresult *res,
- FILE *fout, /* output stream */
- int printAttName, /* print attribute names
- * or not */
- int terseOutput, /* delimiter bars or
- * not? */
- int width); /* width of column, if
- * 0, use variable width */
+ FILE *fout, /* output stream */
+ int printAttName, /* print attribute names
+ * or not */
+ int terseOutput, /* delimiter bars or
+ * not? */
+ int width); /* width of column, if
+ * 0, use variable width */
#ifdef MULTIBYTE
extern int PQmblen(unsigned char *s);