Sales Pitch

I hope you will find libpqxx flexible but easy to use. Not everything about it is easy, but at least there is a tendency to stick to the kinds of complexity that you are used to as a C++ programmer. Query results do not have their own "get next row" interfaces and so on; instead, they provide iterator classes that let you treat the result as a standard C++ container, just like the ones defined by the STL. Streams are also supported where it makes sense, and fields can be read into variables using conversion functions that are implemented as templates.

Also, simple libpqxx programs tend to be less complex than equivalent Java code while having more flexibility.

Many similar libraries have preceded libpqxx and perhaps many will follow. Below are some reasons why I believe libpqxx is worth your while.

  1. Language conformance. It tries to make sensible use of modern C++ features such as templates, standard exceptions, and various kinds of iterators to integrate more comfortably with the language. Where possible, libpqxx blends in with the standard library and the STL in particular.
  2. Structure. It also tries to make it easier for you to write robust, correct code that deals gracefully with various kinds of error conditions. This helps the library detect many problems at compile time instead of at runtime, or during a normal test instead of just in rare corner cases.
  3. Power. Built-in features such as automatic connection restoration and transaction management free you from the low-level chores that tend to take up too much time and go wrong just when you need them most.
  4. Flexibility. Despite the above, this is not an application framework. You won't be forced to squeeze your code into a poorly matched, poorly documented event loop. There are no home-grown string or exception classes. The library lives in its own namespace and completely hides all symbols of the underlying C API library, so it's less likely to clash with other libraries.
  5. Generic programming. Object-orientation is a fine thing, but let's not get obsessed. Functions are used where they make more sense than classes. Inheritance is used internally, but you are not expected to derive your own classes from libpqxx classes except in one or two specific and useful design patterns. Even then, your own code will be isolated in a well-understood environment.

If your impression is that libpqxx is nothing special, that everything about it is obvious or unsurprising, then that means it does its job.

Why Another Nonstandard API?

Some might say that libpqxx is yet another C++ database API with no basis in standards. They would have a point since, as far as I am aware, there is no standard C++ database API.

But that's only half the story. More than any other C++ database API I have seen, libpqxx adheres to API standards set by the C++ standard library, using the same iterator interfaces, implementation patterns, stylistic conventions, and so on. As in the standard library, Generic Programming is a stronger influence than Object-Orientation and conventional polymorphism are. It is my hope that someday a language-wide, database-independent API standard will develop based on this approach. An early version of libpqxx has been ported to Microsoft SQL Server and there has been some interest in a MySQL version, though a porting project failed to materialize.

At the same time, libpqxx serves a practical need as the C++ API for one database management system—PostgreSQL. This does not mean that libpqxx's approach could never form the basis for a neutral standard; good standards are based on experience from actual implementations, which is then generalized and tested against different platforms, and finally refactored into the simplest solution for all reasonable problems and stabilized.

More about libpqxx

If you want to see how simple everything was back in the days of the 1.x API, have a look at the attached slides for a presentation I gave at the FOSDEM conference in Brussels, February 2003. Beware though: they're presentation slides, not reference documentation thinly disguised as a presentation by adding bullet points. ☺

Users' Comments

Here are just some flattering comments from developers using libpqxx:

  • I love the job you've done on libpqxx - the class layouts just make perfect sense to me. I've also learned a lot about writing STL-compatible classes by looking at your code, thanks. — Murphy Pope
  • I started playing around with the pqxx library last night and I'm quite impressed by how easy it is to perform simple db tasks (The test programs helps a whole lot too). — Hannes Wagener
  • I must say I'm really impressed with the clean, good-looking code for a simple test program and with the STL container approach.
  • Congrats on your library, it is a pleasure to work with well done, open code.
  • The application I'm currently improving has long database connection times, infact it's currently been connected for almost 3 months (and has been doing avg of 100 QPS since then, and not a single memory leak in pqxx :) — great work'' — Theo Zourzouvillys
Last modified 8 years ago Last modified on Aug 8, 2011, 5:06:54 AM