Search Postgresql Archives

Re: pqlib in c++: PQconnectStart PQconnectPoll

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I did make an error on the zero assumption, ty :)

However, the reason PGRES_POLLING_FAILED and PGRES_POLLING_OK both break the loop is because of this:

"If this call returns PGRES_POLLING_FAILED, the connection procedure has failed. If this call returns PGRES_POLLING_OK, the connection has been successfully made."

source: http://www.postgresql.org/docs/7.3/static/libpq-connect.html

I was also under the assumption that I would not need to perform my own selects on the underlying socket, and that whatever I got back would be either a null pointer, a successful connection pointer, or a broken connection pointer with an error indication.

cleary I am going to have to study this documentation more carefully ... So ... for simplicity's sake, If I just do the following, how do I get back "database does not exist" ?

////////////////////
#pragma once



#include <stdlib.h>

#include <libpq-fe.h>

#include <windows.h>



int main(int na,char** sa){



      char* host                        = "localhost";

      unsigned short port               = 5432;

      char* dbname                      = "nonexistantdb";

      char* user                        = "user";

      char* password                    = "pass";



      int e                             = 0;

      PGconn* lpcn                      = 0;

      bool keepon                       = true;

      char cs[1024];



      sprintf(

             cs,

             "host=%s port=%u dbname=%s user=%s password=%s",

             host,port,dbname,user,password

      );



      if (lpcn = PQconnectStart(cs)){

             while (keepon){

                    switch(e = PQconnectPoll(lpcn)){

                    case PGRES_POLLING_FAILED:

                    case PGRES_POLLING_OK:

                           keepon = false;

                           break;

                    };

                    Sleep(1);

             };

             printf(

"PQerrorMessage(lpcn) returns:\n\n%s\n\nPQstatus(lpcn) returns %d\n",

                   PQerrorMessage(lpcn),PQstatus(lpcn)

             );

             PQfinish(lpcn);

      } else

             printf("I am assuming we are out of memory ...\n");



      return e;

};

/////////////

----- Original Message ----- From: "Tom Lane" <tgl@xxxxxxxxxxxxx>
To: "madhtr" <madhtr@xxxxxxxxx>
Cc: <pgsql-general@xxxxxxxxxxxxxx>
Sent: Tuesday, August 14, 2007 15:53
Subject: Re:  pqlib in c++: PQconnectStart PQconnectPoll


"madhtr" <madhtr@xxxxxxxxx> writes:
 ... here's the source ... can u tell me whats wrong?

Well, your usage of "pge" seems fairly broken, in particular the random
(and wrong) assumptions about which values are or are not zero.  AFAICT
this code doesn't really distinguish between PGRES_POLLING_FAILED and
PGRES_POLLING_OK.  And if it does return failure, there's no way for the
caller to know which enum type the failure code belongs to.

You didn't show us the code that is actually reporting the error, but I
wonder whether it isn't equally confused about how to determine what the
error is.

regards, tom lane



---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux