From 8bbfa16624e57921712d350b757e11c6258b4561 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 1 Oct 2004 17:34:19 +0000
Subject: [PATCH] Don't assume PQdb() will return a valid result from a failed
 connection.

---
 doc/src/sgml/libpq.sgml        | 14 +++++++-------
 src/test/examples/testlibpq.c  |  4 ++--
 src/test/examples/testlibpq2.c |  4 ++--
 src/test/examples/testlibpq3.c |  4 ++--
 src/test/examples/testlibpq4.c |  6 +++---
 src/test/examples/testlo.c     |  8 ++++----
 6 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index 4691abb78d6..97a6ab5e712 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -1,5 +1,5 @@
 <!--
-$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">
@@ -3979,8 +3979,8 @@ main(int argc, char **argv)
         /* Check to see that the backend connection was successfully made */
         if (PQstatus(conn) != CONNECTION_OK)
         {
-                fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
-                fprintf(stderr, "%s", PQerrorMessage(conn));
+                fprintf(stderr, "Connection to database failed: %s",
+                        PQerrorMessage(conn));
                 exit_nicely(conn);
         }
 
@@ -4125,8 +4125,8 @@ main(int argc, char **argv)
         /* Check to see that the backend connection was successfully made */
         if (PQstatus(conn) != CONNECTION_OK)
         {
-                fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
-                fprintf(stderr, "%s", PQerrorMessage(conn));
+                fprintf(stderr, "Connection to database failed: %s",
+                        PQerrorMessage(conn));
                 exit_nicely(conn);
         }
 
@@ -4267,8 +4267,8 @@ main(int argc, char **argv)
         /* Check to see that the backend connection was successfully made */
         if (PQstatus(conn) != CONNECTION_OK)
         {
-                fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
-                fprintf(stderr, "%s", PQerrorMessage(conn));
+                fprintf(stderr, "Connection to database failed: %s",
+                        PQerrorMessage(conn));
                 exit_nicely(conn);
         }
 
diff --git a/src/test/examples/testlibpq.c b/src/test/examples/testlibpq.c
index f650118a29e..b9d396a2dcb 100644
--- a/src/test/examples/testlibpq.c
+++ b/src/test/examples/testlibpq.c
@@ -41,8 +41,8 @@ main(int argc, char **argv)
 	/* Check to see that the backend connection was successfully made */
 	if (PQstatus(conn) != CONNECTION_OK)
 	{
-		fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
-		fprintf(stderr, "%s", PQerrorMessage(conn));
+		fprintf(stderr, "Connection to database failed: %s",
+				PQerrorMessage(conn));
 		exit_nicely(conn);
 	}
 
diff --git a/src/test/examples/testlibpq2.c b/src/test/examples/testlibpq2.c
index 928ffbb5b48..1cb7616f24b 100644
--- a/src/test/examples/testlibpq2.c
+++ b/src/test/examples/testlibpq2.c
@@ -61,8 +61,8 @@ main(int argc, char **argv)
 	/* Check to see that the backend connection was successfully made */
 	if (PQstatus(conn) != CONNECTION_OK)
 	{
-		fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
-		fprintf(stderr, "%s", PQerrorMessage(conn));
+		fprintf(stderr, "Connection to database failed: %s",
+				PQerrorMessage(conn));
 		exit_nicely(conn);
 	}
 
diff --git a/src/test/examples/testlibpq3.c b/src/test/examples/testlibpq3.c
index 43c1068a457..7036d3e81fb 100644
--- a/src/test/examples/testlibpq3.c
+++ b/src/test/examples/testlibpq3.c
@@ -66,8 +66,8 @@ main(int argc, char **argv)
 	/* Check to see that the backend connection was successfully made */
 	if (PQstatus(conn) != CONNECTION_OK)
 	{
-		fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
-		fprintf(stderr, "%s", PQerrorMessage(conn));
+		fprintf(stderr, "Connection to database failed: %s",
+				PQerrorMessage(conn));
 		exit_nicely(conn);
 	}
 
diff --git a/src/test/examples/testlibpq4.c b/src/test/examples/testlibpq4.c
index 0eda55352ed..977e4edd996 100644
--- a/src/test/examples/testlibpq4.c
+++ b/src/test/examples/testlibpq4.c
@@ -22,10 +22,10 @@ static void
 check_conn(PGconn *conn, const char *dbName)
 {
 	/* 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, "%s", PQerrorMessage(conn));
+		fprintf(stderr, "Connection to database \"%s\" failed: %s",
+				dbName, PQerrorMessage(conn));
 		exit(1);
 	}
 }
diff --git a/src/test/examples/testlo.c b/src/test/examples/testlo.c
index 988b51171a9..019829649ac 100644
--- a/src/test/examples/testlo.c
+++ b/src/test/examples/testlo.c
@@ -8,7 +8,7 @@
  *
  *
  * 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)
 	conn = PQsetdb(NULL, NULL, NULL, NULL, database);
 
 	/* 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, "%s", PQerrorMessage(conn));
+		fprintf(stderr, "Connection to database failed: %s",
+				PQerrorMessage(conn));
 		exit_nicely(conn);
 	}
 
-- 
GitLab