Return the error message from libpq on connection failure.
authorDave Page <dpage@pgadmin.org>
Wed, 9 Nov 2005 09:38:29 +0000 (09:38 +0000)
committerDave Page <dpage@pgadmin.org>
Wed, 9 Nov 2005 09:38:29 +0000 (09:38 +0000)
connection.c

index 1ec29868a4cc7be993f39dbd87aaf2ba30e2c925..bd0fcb7c5785cda337f8e9faa5e39325554bb2e6 100644 (file)
@@ -1259,10 +1259,7 @@ CC_connect(ConnectionClass *self, char password_req, char *salt_para)
 
        connect_return = LIBPQ_connect(self);
        if(0 == connect_return)
-       {
-           CC_set_error(self, CONNECTION_COULD_NOT_ESTABLISH, "Could not connect to the server");
-           return 0;
-       }
+            return 0;
 
        mylog("connection to the database succeeded.\n");
 
@@ -1666,8 +1663,10 @@ LIBPQ_connect(ConnectionClass *self)
        conninfo = (char *)malloc((sizeof(char) * strlen(" host=") + strlen(self->connInfo.server) + 1));
        if(!conninfo)
        {
-           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(server)");
-           mylog("could not allocate memory for server \n");
+           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(server)");\r
+            CC_set_sqlstate(self, "08000");
+           mylog("could not allocate memory for server \n");\r
+            return 0;
        }
        conninfo = strcpy(conninfo," host=");
        conninfo = strcat(conninfo,self->connInfo.server);
@@ -1680,8 +1679,10 @@ LIBPQ_connect(ConnectionClass *self)
        conninfo = (char *)realloc(conninfo,size+strlen(conninfo));
        if(!conninfo)
        {
-           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(port)");
-           mylog("could not allocate memory for port \n");
+           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(port)");\r
+            CC_set_sqlstate(self, "08000");
+           mylog("could not allocate memory for port \n");\r
+            return 0;
        }
        conninfo = strcat(conninfo," port=");
        conninfo = strcat(conninfo,self->connInfo.port);
@@ -1694,8 +1695,10 @@ LIBPQ_connect(ConnectionClass *self)
        conninfo = (char *)realloc(conninfo,size+strlen(conninfo));
        if(!conninfo)
        {
-           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(database)");
-           mylog("i could not allocate memory for dbname \n");
+           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(database)");\r
+            CC_set_sqlstate(self, "08000");
+           mylog("i could not allocate memory for dbname \n");\r
+            return 0;
        }
        conninfo = strcat(conninfo," dbname=");
        conninfo = strcat(conninfo,self->connInfo.database);
@@ -1708,8 +1711,10 @@ LIBPQ_connect(ConnectionClass *self)
        conninfo = (char *)realloc(conninfo,size+strlen(conninfo));
        if(!conninfo)
        {
-           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(username)");
-           mylog("i could not allocate memory for username \n");
+           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(username)");\r
+            CC_set_sqlstate(self, "08000");
+           mylog("i could not allocate memory for username \n");\r
+            return 0;
        }
        conninfo = strcat(conninfo," user=");
        conninfo = strcat(conninfo,self->connInfo.username);
@@ -1722,8 +1727,10 @@ LIBPQ_connect(ConnectionClass *self)
        conninfo = (char *)realloc(conninfo,size+strlen(conninfo));
        if(!conninfo)
        {
-           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(sslmode)");
-           mylog("i could not allocate memory for sslmode \n");
+           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(sslmode)");\r
+            CC_set_sqlstate(self, "08000");
+           mylog("i could not allocate memory for sslmode \n");\r
+            return 0;
        }
        conninfo = strcat(conninfo," sslmode=");
        conninfo = strcat(conninfo,self->connInfo.sslmode);
@@ -1735,8 +1742,10 @@ LIBPQ_connect(ConnectionClass *self)
        conninfo = (char *)realloc(conninfo,size+strlen(conninfo));
        if(!conninfo)
        {
-           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(password)");
-           mylog("i could not allocate memory for password \n");
+           CC_set_error(self, CONN_MEMORY_ALLOCATION_FAILED,"Could not allocate memory for connection string(password)");\r
+            CC_set_sqlstate(self, "08000");
+           mylog("i could not allocate memory for password \n");\r
+            return 0;
        }
        conninfo = strcat(conninfo," password=");
        conninfo = strcat(conninfo,self->connInfo.password);
@@ -1745,8 +1754,9 @@ LIBPQ_connect(ConnectionClass *self)
    self->pgconn = PQconnectdb(conninfo);
    if (PQstatus(self->pgconn) != CONNECTION_OK)
    {
-       CC_set_error(self,CONNECTION_COULD_NOT_ESTABLISH,PQerrorMessage(self->pgconn));
-       mylog("could not establish connection to the database %s \n",PQerrorMessage(self->pgconn));
+       CC_set_error(self,CONNECTION_COULD_NOT_ESTABLISH, PQerrorMessage(self->pgconn));\r
+        CC_set_sqlstate(self, "08001");
+       mylog("Could not establish connection to the database; LIBPQ returned -> %s \n",PQerrorMessage(self->pgconn));
        PQfinish(self->pgconn);
         self->pgconn = NULL;
        free(conninfo);
@@ -1755,9 +1765,10 @@ LIBPQ_connect(ConnectionClass *self)
    /* free the conninfo structure */
    free(conninfo);
  
-        /* setup the notice handler */
-        PQsetNoticeProcessor(self->pgconn, CC_handle_notice, NULL);
-   mylog("connection to the database succeeded.\n");
+    /* setup the notice handler */
+    PQsetNoticeProcessor(self->pgconn, CC_handle_notice, NULL);
+\r
+    mylog("connection to the database succeeded.\n");
    return 1;
 }