*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.124 2000/03/23 17:27:29 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.125 2000/03/24 01:39:55 tgl Exp $
*
*-------------------------------------------------------------------------
*/
}
#endif
-/* ----------
- * pg_setenv_state
- * A struct used when polling a setenv request. This is referred to externally
- * using a PGsetenvHandle.
- * ----------
- */
-struct pg_setenv_state
-{
- enum
- {
- SETENV_STATE_OPTION_SEND, /* About to send an Environment Option */
- SETENV_STATE_OPTION_WAIT, /* Waiting for above send to complete */
-#ifdef MULTIBYTE
- SETENV_STATE_ENCODINGS_SEND, /* About to send an "encodings" query */
- SETENV_STATE_ENCODINGS_WAIT, /* Waiting for query to complete */
-#endif
- SETENV_STATE_OK,
- SETENV_STATE_FAILED
- } state;
- PGconn *conn;
- PGresult *res;
- const struct EnvironmentOptions *eo;
-} ;
#ifdef USE_SSL
static SSL_CTX *SSL_context = NULL;
static int connectDBStart(PGconn *conn);
static int connectDBComplete(PGconn *conn);
+static bool PQsetenvStart(PGconn *conn);
+static PostgresPollingStatusType PQsetenvPoll(PGconn *conn);
static PGconn *makeEmptyPGconn(void);
static void freePGconn(PGconn *conn);
static void closePGconn(PGconn *conn);
* Post-connection housekeeping. Prepare to send environment
* variables to server.
*/
-
- if ((conn->setenv_handle = PQsetenvStart(conn)) == NULL)
+ if (! PQsetenvStart(conn))
goto error_return;
conn->status = CONNECTION_SETENV;
*
* ----------------
*/
-PGsetenvHandle
+static bool
PQsetenvStart(PGconn *conn)
{
- struct pg_setenv_state *handle;
-
- if (conn == NULL || conn->status == CONNECTION_BAD)
- return NULL;
-
- if ((handle = malloc(sizeof(struct pg_setenv_state))) == NULL)
- {
- printfPQExpBuffer(&conn->errorMessage,
- "PQsetenvStart() -- malloc error - %s\n",
- strerror(errno));
- return NULL;
- }
-
- handle->conn = conn;
- handle->res = NULL;
- handle->eo = EnvironmentOptions;
+ if (conn == NULL ||
+ conn->status == CONNECTION_BAD ||
+ conn->setenv_state != SETENV_STATE_IDLE)
+ return false;
#ifdef MULTIBYTE
- handle->state = SETENV_STATE_ENCODINGS_SEND;
+ conn->setenv_state = SETENV_STATE_ENCODINGS_SEND;
#else
- handle->state = SETENV_STATE_OPTION_SEND;
+ conn->setenv_state = SETENV_STATE_OPTION_SEND;
#endif
- return handle; /* Note that a struct pg_setenv_state * is the same as a
- PGsetenvHandle */
+ conn->next_eo = EnvironmentOptions;
+
+ return true;
}
/* ----------------
*
* ----------------
*/
-PostgresPollingStatusType
+static PostgresPollingStatusType
PQsetenvPoll(PGconn *conn)
{
- PGsetenvHandle handle = conn->setenv_handle;
+ PGresult *res;
#ifdef MULTIBYTE
static const char envname[] = "PGCLIENTENCODING";
#endif
- if (!handle || handle->state == SETENV_STATE_FAILED)
+ if (conn == NULL || conn->status == CONNECTION_BAD)
return PGRES_POLLING_FAILED;
/* Check whether there are any data for us */
- switch (handle->state)
+ switch (conn->setenv_state)
{
/* These are reading states */
#ifdef MULTIBYTE
case SETENV_STATE_OPTION_WAIT:
{
/* Load waiting data */
- int n = pqReadData(handle->conn);
+ int n = pqReadData(conn);
if (n < 0)
goto error_return;
#endif
case SETENV_STATE_OPTION_SEND:
break;
-
+
+ /* Should we raise an error if called when not active? */
+ case SETENV_STATE_IDLE:
+ return PGRES_POLLING_OK;
+
default:
- printfPQExpBuffer(&handle->conn->errorMessage,
+ printfPQExpBuffer(&conn->errorMessage,
"PQsetenvPoll() -- unknown state - "
"probably indicative of memory corruption!\n");
goto error_return;
keep_going: /* We will come back to here until there is nothing left to
parse. */
- switch(handle->state)
+ switch(conn->setenv_state)
{
#ifdef MULTIBYTE
env = getenv(envname);
if (!env || *env == '\0')
{
- if (!PQsendQuery(handle->conn,
+ if (!PQsendQuery(conn,
"select getdatabaseencoding()"))
goto error_return;
- handle->state = SETENV_STATE_ENCODINGS_WAIT;
+ conn->setenv_state = SETENV_STATE_ENCODINGS_WAIT;
return PGRES_POLLING_READING;
}
}
case SETENV_STATE_ENCODINGS_WAIT:
{
- if (PQisBusy(handle->conn))
+ if (PQisBusy(conn))
return PGRES_POLLING_READING;
- handle->res = PQgetResult(handle->conn);
+ res = PQgetResult(conn);
- if (handle->res)
+ if (res)
{
char *encoding;
- if (PQresultStatus(handle->res) != PGRES_TUPLES_OK)
+ if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
- PQclear(handle->res);
+ PQclear(res);
goto error_return;
}
- encoding = PQgetvalue(handle->res, 0, 0);
+ /* set client encoding in pg_conn struct */
+ encoding = PQgetvalue(res, 0, 0);
if (!encoding) /* this should not happen */
conn->client_encoding = SQL_ASCII;
else
- /* set client encoding to pg_conn struct */
conn->client_encoding = pg_char_to_encoding(encoding);
- PQclear(handle->res);
+ PQclear(res);
/* We have to keep going in order to clear up the query */
goto keep_going;
}
/* NULL result indicates that the query is finished */
/* Move on to setting the environment options */
- handle->state = SETENV_STATE_OPTION_SEND;
+ conn->setenv_state = SETENV_STATE_OPTION_SEND;
goto keep_going;
}
#endif
/* Send an Environment Option */
char setQuery[100]; /* note length limits in sprintf's below */
- if (handle->eo->envName)
+ if (conn->next_eo->envName)
{
const char *val;
- if ((val = getenv(handle->eo->envName)))
+ if ((val = getenv(conn->next_eo->envName)))
{
if (strcasecmp(val, "default") == 0)
sprintf(setQuery, "SET %s = %.60s",
- handle->eo->pgName, val);
+ conn->next_eo->pgName, val);
else
sprintf(setQuery, "SET %s = '%.60s'",
- handle->eo->pgName, val);
+ conn->next_eo->pgName, val);
#ifdef CONNECTDEBUG
printf("Use environment variable %s to send %s\n",
- handle->eo->envName, setQuery);
+ conn->next_eo->envName, setQuery);
#endif
- if (!PQsendQuery(handle->conn, setQuery))
+ if (!PQsendQuery(conn, setQuery))
goto error_return;
- handle->state = SETENV_STATE_OPTION_WAIT;
+ conn->setenv_state = SETENV_STATE_OPTION_WAIT;
}
else
{
- handle->eo++;
+ conn->next_eo++;
}
}
else
{
- /* No option to send, so we are done. */
- handle->state = SETENV_STATE_OK;
+ /* No more options to send, so we are done. */
+ conn->setenv_state = SETENV_STATE_IDLE;
}
goto keep_going;
case SETENV_STATE_OPTION_WAIT:
{
- if (PQisBusy(handle->conn))
+ if (PQisBusy(conn))
return PGRES_POLLING_READING;
- handle->res = PQgetResult(handle->conn);
+ res = PQgetResult(conn);
- if (handle->res)
+ if (res)
{
- if (PQresultStatus(handle->res) != PGRES_COMMAND_OK)
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
- PQclear(handle->res);
+ PQclear(res);
goto error_return;
}
/* Don't need the result */
- PQclear(handle->res);
+ PQclear(res);
/* We have to keep going in order to clear up the query */
goto keep_going;
}
/* NULL result indicates that the query is finished */
/* Send the next option */
- handle->eo++;
- handle->state = SETENV_STATE_OPTION_SEND;
+ conn->next_eo++;
+ conn->setenv_state = SETENV_STATE_OPTION_SEND;
goto keep_going;
}
- case SETENV_STATE_OK:
- /* Tidy up */
- free(handle);
+ case SETENV_STATE_IDLE:
return PGRES_POLLING_OK;
default:
- printfPQExpBuffer(&handle->conn->errorMessage,
+ printfPQExpBuffer(&conn->errorMessage,
"PQsetenvPoll() -- unknown state - "
"probably indicative of memory corruption!\n");
goto error_return;
/* Unreachable */
error_return:
- handle->state = SETENV_STATE_FAILED; /* This may protect us even if we
- * are called after the handle
- * has been freed. */
- free(handle);
+ conn->setenv_state = SETENV_STATE_IDLE;
return PGRES_POLLING_FAILED;
}
-/* ----------------
- * PQsetenvAbort
- *
- * Aborts the process of passing the values of a standard set of environment
- * variables to the backend.
- *
- * ----------------
- */
-void
-PQsetenvAbort(PGsetenvHandle handle)
-{
- /* We should not have been called in the FAILED state, but we can cope by
- * not freeing the handle (it has probably been freed by now anyway). */
- if (handle->state != SETENV_STATE_FAILED)
- {
- handle->state = SETENV_STATE_FAILED;
- free(handle);
- }
-}
-
+#ifdef NOT_USED
/* ----------------
* PQsetenv
* Passes the values of a standard set of environment variables to the
* backend.
*
- * Returns 1 on success, 0 on failure.
- *
- * This function used to return void. I don't think that there should be
- * compatibility problems caused by giving it a return value, especially as
- * this function has not been documented previously.
+ * Returns true on success, false on failure.
*
+ * This function used to be exported for no particularly good reason.
+ * Since it's no longer used by libpq itself, let's try #ifdef'ing it out
+ * and see if anyone complains.
* ----------------
*/
-int
+static bool
PQsetenv(PGconn *conn)
{
- PGsetenvHandle handle;
PostgresPollingStatusType flag = PGRES_POLLING_WRITING;
- if ((handle = PQsetenvStart(conn)) == NULL)
- return 0;
+ if (! PQsetenvStart(conn))
+ return false;
for (;;) {
/*
break;
case PGRES_POLLING_OK:
- return 1; /* success! */
+ return true; /* success! */
case PGRES_POLLING_READING:
if (pqWait(1, 0, conn))
{
conn->status = CONNECTION_BAD;
- return 0;
+ return false;
}
break;
if (pqWait(0, 1, conn))
{
conn->status = CONNECTION_BAD;
- return 0;
+ return false;
}
break;
default:
/* Just in case we failed to set it in PQsetenvPoll */
conn->status = CONNECTION_BAD;
- return 0;
+ return false;
}
/*
* Now try to advance the state machine.
}
}
+#endif /* NOT_USED */
+
+
/*
* makeEmptyPGconn
* - create a PGconn data structure with (as yet) no interesting data
conn->noticeHook = defaultNoticeProcessor;
conn->status = CONNECTION_BAD;
conn->asyncStatus = PGASYNC_IDLE;
+ conn->setenv_state = SETENV_STATE_IDLE;
conn->notifyList = DLNewList();
conn->sock = -1;
#ifdef USE_SSL
static void
closePGconn(PGconn *conn)
{
- if (conn->status == CONNECTION_SETENV)
- {
- /* We have to abort the setenv process as well */
- PQsetenvAbort(conn->setenv_handle);
- }
-
if (conn->sock >= 0)
{