Access last_sqlstate from libpq - Mailing list pgsql-hackers

From Daniel Frey
Subject Access last_sqlstate from libpq
Date
Msg-id D0EA7D79-62C5-4733-B437-3A5C7C20746D@gmx.de
Whole thread Raw
Responses Re: Access last_sqlstate from libpq  ("David G. Johnston" <david.g.johnston@gmail.com>)
Re: Access last_sqlstate from libpq  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Hi,

I am the author of a PostgreSQL C++ client library, taoPQ (https://github.com/taocpp/taopq), wrapping the C-API of
libpq.

In case of an error when I received a PGresult*, I can access the SQLSTATE by calling

   PGresult* pgresult = ...;
   const char* sqlstate = PQresultErrorField( pgresult, PG_DIAG_SQLSTATE );

However, this is not possible in a couple of other cases where I don't have a PGresult*, only the PGconn* is available:

* PQconnectdb (and variants)

* PQputCopyData
* PQputCopyEnd
* PQgetCopyData

* lo_* (large object functions)

Obviously, I can take the error message from PQerrorMessage and throw a generic runtime error - but it would be so much
nicerif I could use the SQLSTATE to throw the correct exception class and give users more information just like I do
forPGresult*. 

After some research, it appears that PGconn* does have a field called last_sqlstate - it just can't be accessed.
Are there any problems adding a simple accessor to libpq? Or is there some way to access it that I'm missing?

Regards,
Daniel




pgsql-hackers by date:

Previous
From: Alexander Korotkov
Date:
Subject: Re: postgres.h included from relcache.h - but removing it breaks pg_upgrade
Next
From: "David G. Johnston"
Date:
Subject: Re: Access last_sqlstate from libpq