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");
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);
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);
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);
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);
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);
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);
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);
/* 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;
}