When you connect to a database with PQsetdb, as with psql, depending on
how your uninitialized variables are set, you can get a failure with a
"There is no connection to the backend" message.

The fix is to move a call to PQexec() from inside connectDB() to
PQsetdb() after connectDB() returns to PQsetdb().  That way a connection
doesn't have to be already established in order to establish it!


From:  bryanh@giraffe.netgate.net (Bryan Henderson)
This commit is contained in:
Marc G. Fournier 1996-08-19 13:25:40 +00:00
parent 3c47cdeb5a
commit 77e01653bc
1 changed files with 53 additions and 63 deletions

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.6 1996/08/10 00:22:44 scrappy Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.7 1996/08/19 13:25:40 scrappy Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -67,91 +67,89 @@ static void closePGconn(PGconn *conn);
PGconn* PGconn*
PQsetdb(const char *pghost, const char* pgport, const char* pgoptions, const char* pgtty, const char* dbName) PQsetdb(const char *pghost, const char* pgport, const char* pgoptions, const char* pgtty, const char* dbName)
{ {
PGconn *conn; PGconn *conn;
const char *tmp; const char *tmp;
conn = (PGconn*)malloc(sizeof(PGconn)); conn = (PGconn*)malloc(sizeof(PGconn));
if (!conn) {
fprintf(stderr,"FATAL: PQsetdb() -- unable to allocate memory for a PGconn");
return (PGconn*)NULL;
}
if (conn == NULL)
fprintf(stderr,
"FATAL: PQsetdb() -- unable to allocate memory for a PGconn");
else {
conn->Pfout = NULL; conn->Pfout = NULL;
conn->Pfin = NULL; conn->Pfin = NULL;
conn->Pfdebug = NULL; conn->Pfdebug = NULL;
conn->port = NULL; conn->port = NULL;
conn->notifyList = DLNewList(); conn->notifyList = DLNewList();
if (!pghost || pghost[0] == '\0') { if (!pghost || pghost[0] == '\0') {
if (!(tmp = getenv("PGHOST"))) { if (!(tmp = getenv("PGHOST"))) {
tmp = DefaultHost; tmp = DefaultHost;
} }
conn->pghost = strdup(tmp); conn->pghost = strdup(tmp);
} else } else
conn->pghost = strdup(pghost); conn->pghost = strdup(pghost);
if (!pgport || pgport[0] == '\0') { if (!pgport || pgport[0] == '\0') {
if (!(tmp = getenv("PGPORT"))) { if (!(tmp = getenv("PGPORT"))) {
tmp = POSTPORT; tmp = POSTPORT;
} }
conn->pgport = strdup(tmp); conn->pgport = strdup(tmp);
} else } else
conn->pgport = strdup(pgport); conn->pgport = strdup(pgport);
if (!pgtty || pgtty[0] == '\0') { if (!pgtty || pgtty[0] == '\0') {
if (!(tmp = getenv("PGTTY"))) { if (!(tmp = getenv("PGTTY"))) {
tmp = DefaultTty; tmp = DefaultTty;
} }
conn->pgtty = strdup(tmp); conn->pgtty = strdup(tmp);
} else } else
conn->pgtty = strdup(pgtty); conn->pgtty = strdup(pgtty);
if (!pgoptions || pgoptions[0] == '\0') { if (!pgoptions || pgoptions[0] == '\0') {
if (!(tmp = getenv("PGOPTIONS"))) { if (!(tmp = getenv("PGOPTIONS"))) {
tmp = DefaultOption; tmp = DefaultOption;
} }
conn->pgoptions = strdup(tmp); conn->pgoptions = strdup(tmp);
} else } else
conn->pgoptions = strdup(pgoptions); conn->pgoptions = strdup(pgoptions);
#if 0
if (!dbName || dbName[0] == '\0') {
char errorMessage[ERROR_MSG_LENGTH];
if (!(tmp = getenv("PGDATABASE")) &&
!(tmp = fe_getauthname(errorMessage))) {
sprintf(conn->errorMessage,
"FATAL: PQsetdb: Unable to determine a database name!\n");
/* pqdebug("%s", conn->errorMessage); */
conn->dbName = NULL;
return conn;
}
conn->dbName = strdup(tmp);
} else
conn->dbName = strdup(dbName);
#endif
if (((tmp = dbName) && (dbName[0] != '\0')) || if (((tmp = dbName) && (dbName[0] != '\0')) ||
((tmp = getenv("PGDATABASE")))) { ((tmp = getenv("PGDATABASE")))) {
conn->dbName = strdup(tmp); conn->dbName = strdup(tmp);
} else { } else {
char errorMessage[ERROR_MSG_LENGTH]; char errorMessage[ERROR_MSG_LENGTH];
if ((tmp = fe_getauthname(errorMessage)) != 0) { if ((tmp = fe_getauthname(errorMessage)) != 0) {
conn->dbName = strdup(tmp); conn->dbName = strdup(tmp);
free(tmp); free((char*)tmp);
} else { } else {
sprintf(conn->errorMessage, sprintf(conn->errorMessage,
"FATAL: PQsetdb: Unable to determine a database name!\n"); "FATAL: PQsetdb: Unable to determine a database name!\n");
/* pqdebug("%s", conn->errorMessage); */
conn->dbName = NULL; conn->dbName = NULL;
return conn; return conn;
} }
} }
conn->status = connectDB(conn); conn->status = connectDB(conn);
return conn; if (conn->status == CONNECTION_OK) {
PGresult *res;
/* Send a blank query to make sure everything works; in particular, that
the database exists.
*/
res = PQexec(conn," ");
if (res == NULL || res->resultStatus != PGRES_EMPTY_QUERY) {
/* PQexec has put error message in conn->errorMessage */
closePGconn(conn);
}
PQclear(res);
}
}
return conn;
} }
/* /*
* connectDB - * connectDB -
* make a connection to the database, returns 1 if successful or 0 if not * make a connection to the backend so it is ready to receive queries.
* return CONNECTION_OK if successful, CONNECTION_BAD if not.
* *
*/ */
static ConnStatusType static ConnStatusType
@ -166,7 +164,6 @@ connectDB(PGconn *conn)
int laddrlen = sizeof(struct sockaddr); int laddrlen = sizeof(struct sockaddr);
Port *port = conn->port; Port *port = conn->port;
int portno; int portno;
PGresult *res;
char *user; char *user;
/* /*
@ -275,14 +272,6 @@ connectDB(PGconn *conn)
conn->port = port; conn->port = port;
/* we have a connection now,
send a blank query down to make sure the database exists*/
res = PQexec(conn," ");
if (res == NULL || res->resultStatus != PGRES_EMPTY_QUERY) {
/* error will already be in conn->errorMessage */
goto connect_errReturn;
}
free(res);
return CONNECTION_OK; return CONNECTION_OK;
connect_errReturn: connect_errReturn:
@ -319,6 +308,7 @@ closePGconn(PGconn *conn)
if (conn->Pfout) fclose(conn->Pfout); if (conn->Pfout) fclose(conn->Pfout);
if (conn->Pfin) fclose(conn->Pfin); if (conn->Pfin) fclose(conn->Pfin);
if (conn->Pfdebug) fclose(conn->Pfdebug); if (conn->Pfdebug) fclose(conn->Pfdebug);
conn->status = CONNECTION_BAD; /* Well, not really _bad_ - just absent */
} }
/* /*