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

Safe HaskellNone
LanguageHaskell98

Database.TemplatePG.SQL

Description

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.

Synopsis

Documentation

queryTuples :: String -> Q Exp Source

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 Exp Source

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 Exp Source

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 @

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

Ignore duplicate key errors. This is also limited to the IO Monad.

withTransaction :: Handle -> IO a -> IO a Source

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.

rollback :: Handle -> IO () Source

Roll back a transaction.

thConnection :: IO Handle Source

Grab a PostgreSQL connection for compile time. We do so through the environment variables: TPG_DB, TPG_HOST, TPG_PORT, TPG_USER, and TPG_PASS. Only TPG_DB is required.