Commit 8bbfa166 authored by Tom Lane's avatar Tom Lane

Don't assume PQdb() will return a valid result from a failed connection.

parent bbd1e1cc
<!-- <!--
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.164 2004/09/26 22:51:49 tgl Exp $ $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.165 2004/10/01 17:34:17 tgl Exp $
--> -->
<chapter id="libpq"> <chapter id="libpq">
...@@ -3979,8 +3979,8 @@ main(int argc, char **argv) ...@@ -3979,8 +3979,8 @@ main(int argc, char **argv)
/* Check to see that the backend connection was successfully made */ /* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK) if (PQstatus(conn) != CONNECTION_OK)
{ {
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn)); fprintf(stderr, "Connection to database failed: %s",
fprintf(stderr, "%s", PQerrorMessage(conn)); PQerrorMessage(conn));
exit_nicely(conn); exit_nicely(conn);
} }
...@@ -4125,8 +4125,8 @@ main(int argc, char **argv) ...@@ -4125,8 +4125,8 @@ main(int argc, char **argv)
/* Check to see that the backend connection was successfully made */ /* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK) if (PQstatus(conn) != CONNECTION_OK)
{ {
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn)); fprintf(stderr, "Connection to database failed: %s",
fprintf(stderr, "%s", PQerrorMessage(conn)); PQerrorMessage(conn));
exit_nicely(conn); exit_nicely(conn);
} }
...@@ -4267,8 +4267,8 @@ main(int argc, char **argv) ...@@ -4267,8 +4267,8 @@ main(int argc, char **argv)
/* Check to see that the backend connection was successfully made */ /* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK) if (PQstatus(conn) != CONNECTION_OK)
{ {
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn)); fprintf(stderr, "Connection to database failed: %s",
fprintf(stderr, "%s", PQerrorMessage(conn)); PQerrorMessage(conn));
exit_nicely(conn); exit_nicely(conn);
} }
......
...@@ -41,8 +41,8 @@ main(int argc, char **argv) ...@@ -41,8 +41,8 @@ main(int argc, char **argv)
/* Check to see that the backend connection was successfully made */ /* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK) if (PQstatus(conn) != CONNECTION_OK)
{ {
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn)); fprintf(stderr, "Connection to database failed: %s",
fprintf(stderr, "%s", PQerrorMessage(conn)); PQerrorMessage(conn));
exit_nicely(conn); exit_nicely(conn);
} }
......
...@@ -61,8 +61,8 @@ main(int argc, char **argv) ...@@ -61,8 +61,8 @@ main(int argc, char **argv)
/* Check to see that the backend connection was successfully made */ /* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK) if (PQstatus(conn) != CONNECTION_OK)
{ {
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn)); fprintf(stderr, "Connection to database failed: %s",
fprintf(stderr, "%s", PQerrorMessage(conn)); PQerrorMessage(conn));
exit_nicely(conn); exit_nicely(conn);
} }
......
...@@ -66,8 +66,8 @@ main(int argc, char **argv) ...@@ -66,8 +66,8 @@ main(int argc, char **argv)
/* Check to see that the backend connection was successfully made */ /* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK) if (PQstatus(conn) != CONNECTION_OK)
{ {
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn)); fprintf(stderr, "Connection to database failed: %s",
fprintf(stderr, "%s", PQerrorMessage(conn)); PQerrorMessage(conn));
exit_nicely(conn); exit_nicely(conn);
} }
......
...@@ -22,10 +22,10 @@ static void ...@@ -22,10 +22,10 @@ static void
check_conn(PGconn *conn, const char *dbName) check_conn(PGconn *conn, const char *dbName)
{ {
/* check to see that the backend connection was successfully made */ /* check to see that the backend connection was successfully made */
if (PQstatus(conn) == CONNECTION_BAD) if (PQstatus(conn) != CONNECTION_OK)
{ {
fprintf(stderr, "Connection to database '%s' failed.\n", dbName); fprintf(stderr, "Connection to database \"%s\" failed: %s",
fprintf(stderr, "%s", PQerrorMessage(conn)); dbName, PQerrorMessage(conn));
exit(1); exit(1);
} }
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/test/examples/testlo.c,v 1.23 2004/09/22 05:12:45 neilc Exp $ * $PostgreSQL: pgsql/src/test/examples/testlo.c,v 1.24 2004/10/01 17:34:19 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -225,10 +225,10 @@ main(int argc, char **argv) ...@@ -225,10 +225,10 @@ main(int argc, char **argv)
conn = PQsetdb(NULL, NULL, NULL, NULL, database); conn = PQsetdb(NULL, NULL, NULL, NULL, database);
/* check to see that the backend connection was successfully made */ /* check to see that the backend connection was successfully made */
if (PQstatus(conn) == CONNECTION_BAD) if (PQstatus(conn) != CONNECTION_OK)
{ {
fprintf(stderr, "Connection to database '%s' failed.\n", database); fprintf(stderr, "Connection to database failed: %s",
fprintf(stderr, "%s", PQerrorMessage(conn)); PQerrorMessage(conn));
exit_nicely(conn); exit_nicely(conn);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment