The arrow of time

Ivan Voras' blog

PHPPgSQL - A PostgreSQL library wrapper for PHP

I'm starting to do a project in PHP and as I usually start from the database definition onwards, I was also considering which approach to use for accessing PostgreSQL. I have never used PDO before so I thought it would be a good chance to learn it and use it. Unfortunately, the very first lines of code I've written with it have shown PDO has a horrible design and fails in its practically every goal - it's no easier to use than the procedural extensions and it fails to provide any significant isolation between it and the various database dialects. Designed by committee perhaps?

So I created my own library - for the last time I hope since I indend to reuse it in the forseeable future. In addition to having a sane interface, it has query string parametrizations, automatically prepares all queries for speedy execution (plan caching) and interfaces with Memcached to perform query data caching. Also, it will bring peace to the Middle East and solve the world's problems.

Another thing bad about PDO is that it cannot be subclassed in any meaningful way to introduce features like query caching. I could rant some more but what's the point?

PHPPgSQL is a SourceForge.Net project. Here is what it does:

  • Implements the Iterator interface so query results can be iterated through with foreach. This is in my opinion the best and most intuitive interface for row iteration.
  • Relies on and extensively uses query parametrization. Not only does it improve performance by allowing query plan caching, it is also a defense against SQL injection security problems.
  • Query plan caching significantly improves performance of complex SQL queries. If it is used with persistent database connections (as is recommended), query plans will only be calculated once and then reused across all PHP script invocations communicating with the same backend.
  • Is integrated with Memcached, performing query data caching on SELECT queries. This means that frequently executed queries never even touch the database but are quickly returned.

PHPPgSQL is a light-weight class in terms of code size but is definitely not light-weight in terms of memory and resource usage. It trades memory for speed. I'll emphasize it again: query parametrization is the single most important thing here - without it there the benefits of using PHPPgSQL will be greatly lessened.

PHPPgSQL documentation is included but also available here.

Here's a simple example:

$q = new PHPPgSQL("SELECT comment FROM forum WHERE forum_id=$1 AND user_id=$2", $forum_id, $uid);
foreach ($q as $row)
var_dump($row);
echo "There are {$q->reccount} records.\n";

Depending on future interest in the library, I will develop or refresh PHPPgSQL with my future development versions.

(of course, PHPPgSQL can use all these nice technologies because it doesn't even pretend to be a generic, database-agnostic wrapper, unlike PDO)

Post your comment here!

Your name:
Comment title:
Text:
Type "xxx" here:

Comments are subject to moderation and will be deleted if deemed inappropriate. All content is © Ivan Voras. Comments are owned by their authors... who agree to basically surrender all rights by publishing them here :)