Safe Haskell | None |
---|---|
Language | Haskell98 |
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 -> ExpQ
- queryTuple :: String -> ExpQ
- execute :: String -> ExpQ
- insertIgnore :: IO () -> IO ()
- withTransaction :: PGConnection -> IO a -> IO a
- rollback :: PGConnection -> IO ()
- type PGException = PGError
- pgConnect :: HostName -> PortID -> String -> String -> String -> IO PGConnection
- pgDisconnect :: PGConnection -> IO ()
Documentation
queryTuples :: String -> ExpQ Source
queryTuples :: String -> (PGConnection -> 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 -> ExpQ Source
queryTuple :: String -> (PGConnection -> 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 -> ExpQ Source
execute :: String -> (PGConnection -> 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 :: PGConnection -> 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 :: PGConnection -> IO () Source
Roll back a transaction.
type PGException = PGError Source
:: PGConnection | a handle from |
-> IO () |
Disconnect cleanly from the PostgreSQL server.