lpsmith/postgresql-simple — pull request #16 by joeyadams (2012-05-09 03:25:54+00:00)
Title: bytea compatibility fix, tests, and error handling fixes

The current mechanism for escaping data for use as a BYTEA literal does not work with PostgreSQL < 9, and only works on PostgreSQL 9.0 if you explicitly `SET standard_conforming_strings TO ON`.

It emits a hexadecimal literal like this:

'\x48656c6c6f'

There are two problems here:

* The hex format for BYTEA was introduced in PostgreSQL 9.0, and is not understood by earlier versions.

* This format assumes standard conforming strings is on, which is the default starting with PostgreSQL 9.1.

The first commit in this pull request changes it to use libpq's `PQescapeByteaConn` instead. Now it will choose hex or octal escapes depending on the server version, and will double up the backslash if standard conforming strings is off.

It turns out that PostgreSQL 8.4 will print a warning if a backslash appears in a `''` literal when standard conforming strings is off. Thus, the second commit turns on standard conforming strings by default.

The third commit adds a unit test for `bytea` conversion. `postgresql-simple` doesn't have tests yet, so I set up some scaffolding to make it easier to add more tests. It uses `HUnit`, and tests can be run with `cabal test`.

Finally, I made it so `finishQuery` actually handles error cases, rather than having non-exhaustive patterns. I also revisited `finishExecute` and eliminated the catch-all case. This way, we'll get a warning if a `PQ.CopyBoth` constructor is added to `ExecStatus` in the future.