Re: pqlib in c++: PQconnectStart PQconnectPoll

From: "madhtr" <madhtr(at)schif(dot)org>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: pqlib in c++: PQconnectStart PQconnectPoll
Date: 2007-02-14 23:24:59
Message-ID: 006e01c7508f$58b634f0$7b55503f@useronewin2klt
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)sss(dot)pgh(dot)pa(dot)us>
To: "madhtr" <madhtr(at)schif(dot)org>
Cc: <pgsql-general(at)postgresql(dot)org>
Sent: Tuesday, August 14, 2007 15:53
Subject: Re: [GENERAL] pqlib in c++: PQconnectStart PQconnectPoll

> "madhtr" <madhtr(at)schif(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message carter ck 2007-02-15 01:11:42 How to create an archive for old records?
Previous Message Paul Lambert 2007-02-14 23:13:36 Re: Stored Procedure examples