Hello all,
We have a project written in C that interfaces with Greenplum via libpq and are considering incrementally transitioning it to C++. We're wondering if we can continue to use libpq and call it from our C++ code.
Here is the header from the version of libpq that we have:
/*-------------------------------------------------------------------------
*
* libpq-fe.h
* This file contains definitions for structures and
* externs for functions used by frontend postgres applications.
*
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.152 2010/02/26 02:01:33 momjian Exp $
*
*-------------------------------------------------------------------------
*/
Here is a representative example of how we currently use libpq:
static PGresult* gpdb_execute_query(PGconn *conn, const char *query)
{
PGresult *res;
if((res = PQexec(conn, query)) == NULL) {
LTRACE("PQexec(%s): %s", query, PQresultErrorMessage(res));
return NULL;
}
/* See libpq-fe.h, enum ExecStatusType */
switch(PQresultStatus(res)) {
case PGRES_BAD_RESPONSE:
case PGRES_FATAL_ERROR:
/* we know for certain that these codes mean that the query failed,
* and we should log the error message */
LTRACE("tid 0x%lx Query '%s' not successful: %s", pthread_self(),
query, PQresultErrorMessage(res));
break;
case PGRES_NONFATAL_ERROR:
/* Log message from queries that succeeded with warnings */
LTRACE("tid 0x%lx Query '%s' succeeded with warnings: %s", pthread_self(),
query, PQresultErrorMessage(res));
break;
/* For all remaining codes, it is up to the caller to determine whether or
* not an error occured, and to log it appropriately */
default:
break;
}
return res;
}
Are there any pitfalls in calling libpq from C++ that we should be aware of?
We are aware of libpq++ and are considering switching to it, but we want to make sure that it will add value over libpq. In particular, we'd like to be able to write tests that mock out the database and return result sets (possibly also test doubles) that we construct in our tests. Does libpq++ make that easier than libpq?
Thanks,
Ben, Amil, Nikhil, and David