Skip to content

Commit 3e1586a

Browse files
committed
Fixes for PQsetdb():
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)
1 parent 817bf55 commit 3e1586a

File tree

1 file changed

+32
-43
lines changed

1 file changed

+32
-43
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.4 1996/07/23 03:35:12 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.4.2.1 1996/08/19 13:23:19 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -66,16 +66,16 @@ static void closePGconn(PGconn *conn);
6666
PGconn*
6767
PQsetdb(char *pghost, char* pgport, char* pgoptions, char* pgtty, char* dbName)
6868
{
69-
PGconn *conn;
70-
char *tmp;
69+
PGconn *conn;
70+
char *tmp;
7171

72-
conn = (PGconn*)malloc(sizeof(PGconn));
73-
74-
if (!conn) {
75-
fprintf(stderr,"FATAL: PQsetdb() -- unable to allocate memory for a PGconn");
76-
return (PGconn*)NULL;
77-
}
72+
conn = (PGconn*)malloc(sizeof(PGconn));
7873

74+
if (conn == NULL) {
75+
fprintf(stderr,
76+
"FATAL: PQsetdb() -- unable to allocate memory for a PGconn");
77+
return (PGconn*)NULL;
78+
}
7979
conn->Pfout = NULL;
8080
conn->Pfin = NULL;
8181
conn->Pfdebug = NULL;
@@ -113,39 +113,36 @@ PQsetdb(char *pghost, char* pgport, char* pgoptions, char* pgtty, char* dbName)
113113
conn->pgoptions = strdup(tmp);
114114
} else
115115
conn->pgoptions = strdup(pgoptions);
116-
#if 0
117-
if (!dbName || dbName[0] == '\0') {
116+
if (((tmp = dbName) && (dbName[0] != '\0')) ||
117+
((tmp = getenv("PGDATABASE")))) {
118+
conn->dbName = strdup(tmp);
119+
} else {
118120
char errorMessage[ERROR_MSG_LENGTH];
119-
if (!(tmp = getenv("PGDATABASE")) &&
120-
!(tmp = fe_getauthname(errorMessage))) {
121+
if ((tmp = fe_getauthname(errorMessage)) != 0) {
122+
conn->dbName = strdup(tmp);
123+
free((char *)tmp);
124+
} else {
121125
sprintf(conn->errorMessage,
122126
"FATAL: PQsetdb: Unable to determine a database name!\n");
123-
/* pqdebug("%s", conn->errorMessage); */
124127
conn->dbName = NULL;
125128
return conn;
126129
}
127-
conn->dbName = strdup(tmp);
128-
} else
129-
conn->dbName = strdup(dbName);
130-
#endif
131-
if (((tmp = dbName) && (dbName[0] != '\0')) ||
132-
((tmp = getenv("PGDATABASE")))) {
133-
conn->dbName = strdup(tmp);
134-
} else {
135-
char errorMessage[ERROR_MSG_LENGTH];
136-
if (tmp = fe_getauthname(errorMessage)) {
137-
conn->dbName = strdup(tmp);
138-
free(tmp);
139-
} else {
140-
sprintf(conn->errorMessage,
141-
"FATAL: PQsetdb: Unable to determine a database name!\n");
142-
/* pqdebug("%s", conn->errorMessage); */
143-
conn->dbName = NULL;
144-
return conn;
145-
}
146130
}
147131
conn->status = connectDB(conn);
148-
return conn;
132+
if (conn->status == CONNECTION_OK) {
133+
PGresult *res;
134+
/* Send a blank query to make sure everything works; in particular, that
135+
the database exists.
136+
*/
137+
res = PQexec(conn," ");
138+
if (res == NULL || res->resultStatus != PGRES_EMPTY_QUERY) {
139+
/* PQexec has put error message in conn->errorMessage */
140+
closePGconn(conn);
141+
}
142+
PQclear(res);
143+
}
144+
}
145+
return conn;
149146
}
150147

151148
/*
@@ -165,7 +162,6 @@ connectDB(PGconn *conn)
165162
int laddrlen = sizeof(struct sockaddr);
166163
Port *port = conn->port;
167164
int portno;
168-
PGresult *res;
169165

170166
char *user;
171167
/*
@@ -274,14 +270,6 @@ connectDB(PGconn *conn)
274270

275271
conn->port = port;
276272

277-
/* we have a connection now,
278-
send a blank query down to make sure the database exists*/
279-
res = PQexec(conn," ");
280-
if (res == NULL || res->resultStatus != PGRES_EMPTY_QUERY) {
281-
/* error will already be in conn->errorMessage */
282-
goto connect_errReturn;
283-
}
284-
free(res);
285273
return CONNECTION_OK;
286274

287275
connect_errReturn:
@@ -318,6 +306,7 @@ closePGconn(PGconn *conn)
318306
if (conn->Pfout) fclose(conn->Pfout);
319307
if (conn->Pfin) fclose(conn->Pfin);
320308
if (conn->Pfdebug) fclose(conn->Pfdebug);
309+
conn->status = CONNECTION_BAD; /* Well, not really _bad_ - just absent */
321310
}
322311

323312
/*

0 commit comments

Comments
 (0)