templatepg-0.1.0: A PostgreSQL access library with compile-time SQL type inference



This module exposes the high-level Template Haskell interface for querying and manipulating the PostgreSQL server.

All SQL string arguments support expression interpolation. Just enclose your expression in {} in the SQL string.

Note that transactions are messy and untested. Attempt to use them at your own risk.



queryTuples :: String -> Q ExpSource

queryTuples :: String -> (Handle -> IO [(column1, column2, ...)])

Query a PostgreSQL server and return the results as a list of tuples.

Example (where h is a handle from pgConnect):

$(queryTuples "SELECT usesysid, usename FROM pg_user") h
=> IO [(Maybe String, Maybe Integer)]

queryTuple :: String -> Q ExpSource

queryTuple :: String -> (Handle -> IO (Maybe (column1, column2, ...)))

Convenience function to query a PostgreSQL server and return the first result as a tuple. If the query produces no results, return Nothing.

Example (where h is a handle from pgConnect):

let sysid = 10::Integer;
$(queryTuple "SELECT usesysid, usename FROM pg_user WHERE usesysid = {sysid}") h
=> IO (Maybe (Maybe String, Maybe Integer))

execute :: String -> Q ExpSource

execute :: String -> (Handle -> IO ())

Convenience function to execute a statement on the PostgreSQL server.

Example (where h is a handle from pgConnect):

let rolename = "BOfH"
$(execute "CREATE ROLE {rolename}") h
=> IO ()

insertIgnore :: IO () -> IO ()Source

Run an INSERT statement, ignoring duplicate key errors. This is also limited to the IO Monad. Untested.

withTransaction :: Handle -> IO a -> IO aSource

Run a sequence of IO actions (presumably SQL statements) wrapped in a transaction. Unfortunately you're restricted to using this in the IO Monad for now due to the use of onException. I'm debating adding a MonadPeelIO version. Untested.

rollback :: Handle -> IO ()Source

Roll back a transaction. Untested.