IreneKnapp/direct-sqlite — pull request #15 by joeyadams (2012-08-25 08:40:36+00:00)
Title: A few minor improvements before the next release

I'd like to slip these changes in before the next release, since there's a slight API change (`sqlErrorDetails` is now `Text` instead of `Maybe Text`).

A couple ideas taken from persistent-sqlite:

* Support optionally using the system's sqlite3 instead of the bundled one.

* Handle invalid UTF-8 a little better.

Someone [pointed out][1] that:

> `decodeUtf8` is as evil as `head`: their exceptions are very unhelpful.

persistent-sqlite uses decodeUtf8With [lenientDecode][2], which replaces invalid characters with U+FFFD (the Unicode replacement character). Because that can silently corrupt data, I took an alternative approach: throw a [DecodeError][3] whose message refers to Database.SQLite3.

I also documented that a `DecodeError` may arise, and that SQLite3 does not validate UTF-8 on input.

The last commit uses [sqlite3_db_handle][4] to get the `Database` of a `Statement` when a call fails and only a `Statement` is available. This eliminated all cases where no details were available, so I changed `sqlErrorDetails` from `Maybe Text` to `Text`.

[1]: https://github.com/yesodweb/persistent/issues/92#issuecomment-8020109
[2]: http://hackage.haskell.org/packages/archive/text/latest/doc/html/Data-Text-Encoding-Error.html#v:lenientDecode
[3]: http://hackage.haskell.org/packages/archive/text/latest/doc/html/Data-Text-Encoding-Error.html#v:DecodeError
[4]: http://www.sqlite.org/c3ref/db_handle.html