Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 77e0165

Browse files
committed
Fixes:
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 3c47cde commit 77e0165

File tree

1 file changed

+53
-63
lines changed

1 file changed

+53
-63
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 53 additions & 63 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.6 1996/08/10 00:22:44 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.7 1996/08/19 13:25:40 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -67,91 +67,89 @@ static void closePGconn(PGconn *conn);
6767
PGconn*
6868
PQsetdb(const char *pghost, const char* pgport, const char* pgoptions, const char* pgtty, const char* dbName)
6969
{
70-
PGconn *conn;
71-
const char *tmp;
70+
PGconn *conn;
71+
const char *tmp;
7272

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

75+
if (conn == NULL)
76+
fprintf(stderr,
77+
"FATAL: PQsetdb() -- unable to allocate memory for a PGconn");
78+
else {
8079
conn->Pfout = NULL;
8180
conn->Pfin = NULL;
8281
conn->Pfdebug = NULL;
8382
conn->port = NULL;
8483
conn->notifyList = DLNewList();
85-
84+
8685
if (!pghost || pghost[0] == '\0') {
87-
if (!(tmp = getenv("PGHOST"))) {
88-
tmp = DefaultHost;
89-
}
90-
conn->pghost = strdup(tmp);
86+
if (!(tmp = getenv("PGHOST"))) {
87+
tmp = DefaultHost;
88+
}
89+
conn->pghost = strdup(tmp);
9190
} else
92-
conn->pghost = strdup(pghost);
93-
91+
conn->pghost = strdup(pghost);
92+
9493
if (!pgport || pgport[0] == '\0') {
95-
if (!(tmp = getenv("PGPORT"))) {
96-
tmp = POSTPORT;
97-
}
98-
conn->pgport = strdup(tmp);
94+
if (!(tmp = getenv("PGPORT"))) {
95+
tmp = POSTPORT;
96+
}
97+
conn->pgport = strdup(tmp);
9998
} else
100-
conn->pgport = strdup(pgport);
101-
99+
conn->pgport = strdup(pgport);
100+
102101
if (!pgtty || pgtty[0] == '\0') {
103-
if (!(tmp = getenv("PGTTY"))) {
104-
tmp = DefaultTty;
105-
}
106-
conn->pgtty = strdup(tmp);
102+
if (!(tmp = getenv("PGTTY"))) {
103+
tmp = DefaultTty;
104+
}
105+
conn->pgtty = strdup(tmp);
107106
} else
108-
conn->pgtty = strdup(pgtty);
109-
107+
conn->pgtty = strdup(pgtty);
108+
110109
if (!pgoptions || pgoptions[0] == '\0') {
111-
if (!(tmp = getenv("PGOPTIONS"))) {
112-
tmp = DefaultOption;
113-
}
114-
conn->pgoptions = strdup(tmp);
115-
} else
116-
conn->pgoptions = strdup(pgoptions);
117-
#if 0
118-
if (!dbName || dbName[0] == '\0') {
119-
char errorMessage[ERROR_MSG_LENGTH];
120-
if (!(tmp = getenv("PGDATABASE")) &&
121-
!(tmp = fe_getauthname(errorMessage))) {
122-
sprintf(conn->errorMessage,
123-
"FATAL: PQsetdb: Unable to determine a database name!\n");
124-
/* pqdebug("%s", conn->errorMessage); */
125-
conn->dbName = NULL;
126-
return conn;
127-
}
128-
conn->dbName = strdup(tmp);
110+
if (!(tmp = getenv("PGOPTIONS"))) {
111+
tmp = DefaultOption;
112+
}
113+
conn->pgoptions = strdup(tmp);
129114
} else
130-
conn->dbName = strdup(dbName);
131-
#endif
115+
conn->pgoptions = strdup(pgoptions);
132116
if (((tmp = dbName) && (dbName[0] != '\0')) ||
133-
((tmp = getenv("PGDATABASE")))) {
117+
((tmp = getenv("PGDATABASE")))) {
134118
conn->dbName = strdup(tmp);
135119
} else {
136120
char errorMessage[ERROR_MSG_LENGTH];
137121
if ((tmp = fe_getauthname(errorMessage)) != 0) {
138122
conn->dbName = strdup(tmp);
139-
free(tmp);
123+
free((char*)tmp);
140124
} else {
141125
sprintf(conn->errorMessage,
142-
"FATAL: PQsetdb: Unable to determine a database name!\n");
143-
/* pqdebug("%s", conn->errorMessage); */
126+
"FATAL: PQsetdb: Unable to determine a database name!\n");
144127
conn->dbName = NULL;
145128
return conn;
146129
}
147130
}
148131
conn->status = connectDB(conn);
149-
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;
150146
}
151147

148+
152149
/*
153150
* connectDB -
154-
* make a connection to the database, returns 1 if successful or 0 if not
151+
* make a connection to the backend so it is ready to receive queries.
152+
* return CONNECTION_OK if successful, CONNECTION_BAD if not.
155153
*
156154
*/
157155
static ConnStatusType
@@ -166,7 +164,6 @@ connectDB(PGconn *conn)
166164
int laddrlen = sizeof(struct sockaddr);
167165
Port *port = conn->port;
168166
int portno;
169-
PGresult *res;
170167

171168
char *user;
172169
/*
@@ -275,14 +272,6 @@ connectDB(PGconn *conn)
275272

276273
conn->port = port;
277274

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

288277
connect_errReturn:
@@ -319,6 +308,7 @@ closePGconn(PGconn *conn)
319308
if (conn->Pfout) fclose(conn->Pfout);
320309
if (conn->Pfin) fclose(conn->Pfin);
321310
if (conn->Pfdebug) fclose(conn->Pfdebug);
311+
conn->status = CONNECTION_BAD; /* Well, not really _bad_ - just absent */
322312
}
323313

324314
/*

0 commit comments

Comments
 (0)