]> granicus.if.org Git - postgresql/commitdiff
Fixes:
authorMarc G. Fournier <scrappy@hub.org>
Mon, 19 Aug 1996 13:25:40 +0000 (13:25 +0000)
committerMarc G. Fournier <scrappy@hub.org>
Mon, 19 Aug 1996 13:25:40 +0000 (13:25 +0000)
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)

src/interfaces/libpq/fe-connect.c

index a84db7b4d0ca1b07eb69735d921b94c62a04754d..e8b47e9fada3072a44e7f60357c25e8d8cdee57d 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * 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* 
 PQsetdb(const char *pghost, const char* pgport, const char* pgoptions, const char* pgtty, const char* dbName)
 {
-    PGconn *conn;
-    const char *tmp;
+  PGconn *conn;
+  const char *tmp;
 
-    conn = (PGconn*)malloc(sizeof(PGconn));
-
-    if (!conn) {
-      fprintf(stderr,"FATAL: PQsetdb() -- unable to allocate memory for a PGconn");
-      return (PGconn*)NULL;
-    }
+  conn = (PGconn*)malloc(sizeof(PGconn));
 
+  if (conn == NULL) 
+    fprintf(stderr,
+            "FATAL: PQsetdb() -- unable to allocate memory for a PGconn");
+  else {
     conn->Pfout = NULL;
     conn->Pfin = NULL;
     conn->Pfdebug = NULL;
     conn->port = NULL;
     conn->notifyList = DLNewList();
-
+    
     if (!pghost || pghost[0] == '\0') {
-       if (!(tmp = getenv("PGHOST"))) {
-           tmp = DefaultHost;
-       }
-       conn->pghost = strdup(tmp);
+      if (!(tmp = getenv("PGHOST"))) {
+        tmp = DefaultHost;
+      }
+      conn->pghost = strdup(tmp);
     } else
-       conn->pghost = strdup(pghost);
-
+      conn->pghost = strdup(pghost);
+    
     if (!pgport || pgport[0] == '\0') {
-       if (!(tmp = getenv("PGPORT"))) {
-           tmp = POSTPORT;
-       }
-       conn->pgport = strdup(tmp);
+      if (!(tmp = getenv("PGPORT"))) {
+        tmp = POSTPORT;
+      }
+      conn->pgport = strdup(tmp);
     } else
-       conn->pgport = strdup(pgport);
-
+      conn->pgport = strdup(pgport);
+    
     if (!pgtty || pgtty[0] == '\0') {
-       if (!(tmp = getenv("PGTTY"))) {
-           tmp = DefaultTty;
-       }
-       conn->pgtty = strdup(tmp);
+      if (!(tmp = getenv("PGTTY"))) {
+        tmp = DefaultTty;
+      }
+      conn->pgtty = strdup(tmp);
     } else
-       conn->pgtty = strdup(pgtty);
-
+      conn->pgtty = strdup(pgtty);
+    
     if (!pgoptions || pgoptions[0] == '\0') {
-       if (!(tmp = getenv("PGOPTIONS"))) {
-           tmp = DefaultOption;
-       }
-       conn->pgoptions = strdup(tmp);
-    } else
-       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);
+      if (!(tmp = getenv("PGOPTIONS"))) {
+        tmp = DefaultOption;
+      }
+      conn->pgoptions = strdup(tmp);
     } else
-       conn->dbName = strdup(dbName);
-#endif
+      conn->pgoptions = strdup(pgoptions);
     if (((tmp = dbName) && (dbName[0] != '\0')) ||
-                                              ((tmp = getenv("PGDATABASE")))) {
+        ((tmp = getenv("PGDATABASE")))) {
       conn->dbName = strdup(tmp);
     } else {
       char errorMessage[ERROR_MSG_LENGTH];
       if ((tmp = fe_getauthname(errorMessage)) != 0) {
         conn->dbName = strdup(tmp);
-        free(tmp);
+        free((char*)tmp);
       } else {
         sprintf(conn->errorMessage,
-              "FATAL: PQsetdb: Unable to determine a database name!\n");
-/*      pqdebug("%s", conn->errorMessage); */
+                "FATAL: PQsetdb: Unable to determine a database name!\n");
         conn->dbName = NULL;
         return 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 -
- * 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
@@ -166,7 +164,6 @@ connectDB(PGconn *conn)
     int         laddrlen = sizeof(struct sockaddr);
     Port        *port = conn->port;
     int         portno;
-    PGresult    *res;
 
     char        *user;
     /*
@@ -275,14 +272,6 @@ connectDB(PGconn *conn)
     
     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;
 
 connect_errReturn:
@@ -319,6 +308,7 @@ closePGconn(PGconn *conn)
     if (conn->Pfout) fclose(conn->Pfout);
     if (conn->Pfin)  fclose(conn->Pfin);
     if (conn->Pfdebug) fclose(conn->Pfdebug);
+    conn->status = CONNECTION_BAD;  /* Well, not really _bad_ - just absent */
 }
 
 /*