Re: pqlib in c++: PQconnectStart PQconnectPoll - Mailing list pgsql-general

From madhtr
Subject Re: pqlib in c++: PQconnectStart PQconnectPoll
Date
Msg-id 006e01c7508f$58b634f0$7b55503f@useronewin2klt
Whole thread Raw
In response to Moving to postgresql and some ignorant questions  ("Phoenix Kiula" <phoenix.kiula@gmail.com>)
Responses Re: pqlib in c++: PQconnectStart PQconnectPoll  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
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@sss.pgh.pa.us>
To: "madhtr" <madhtr@schif.org>
Cc: <pgsql-general@postgresql.org>
Sent: Tuesday, August 14, 2007 15:53
Subject: Re: [GENERAL] pqlib in c++: PQconnectStart PQconnectPoll


> "madhtr" <madhtr@schif.org> 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



pgsql-general by date:

Previous
From: Gregory Stark
Date:
Subject: Re: MVCC cons
Next
From: Tom Lane
Date:
Subject: Re: pqlib in c++: PQconnectStart PQconnectPoll