IreneKnapp/direct-sqlite — pull request #28 by joeyadams (2013-06-08 08:29:46+00:00)
Title: Add getAutoCommit (sqlite3_get_autocommit)

I encountered a problem with this pattern:

```haskell
withTransaction :: Database -> IO a -> IO a
withTransaction conn body =
mask $ \restore -> do
exec conn "BEGIN"
a <- restore (body a) `onException` exec conn "ROLLBACK"
exec conn "COMMIT"
return a
```

Namely, `ROLLBACK` throws an exception if no transaction is in progress, and some errors automatically roll back the transaction. To address this problem precisely, I need a binding for [sqlite3_get_autocommit]:

```haskell
withTransaction :: Database -> IO a -> IO a
withTransaction conn body =
mask $ \restore -> do
exec conn "BEGIN"
a <- restore (body a) `onException` do
autocommit <- getAutoCommit conn
when (not autocommit) $ exec conn "ROLLBACK"
exec conn "COMMIT"
return a
```

I only exported this from Database.SQLite3.Direct to avoid cluttering the main API, and in case we want to use a more intuitive name than SQLite uses, like:

active <- isTransactionActive conn
when active $ exec conn "ROLLBACK"

[sqlite3_get_autocommit]: http://www.sqlite.org/c3ref/get_autocommit.html