-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | A minimalistic general high level API for relational databases
--
-- A robust and concise yet powerful API for communication with arbitrary
-- relational databases using SQL.
--
-- Features:
--
--
-- - Concise and crisp API. Just a few functions and one monad doing
-- all the boilerplate job for you.
-- - A powerful transaction abstraction, which provides an automated
-- resolution of conflicts. The API ensures that you're only able to
-- perform a specific set of actions in the transaction context, which
-- allows Hasql to safely resolve conflicting transactions by
-- automatically retrying them. This is much inspired by STM and ST.
-- - Support for cursors. Allows to fetch virtually limitless result
-- sets in a constant memory using streaming.
-- - Employment of prepared statements. Every statement you emit gets
-- prepared and cached. This raises the performance of the backend.
-- - Automated management of resources related to connections,
-- transactions and cursors.
-- - A built-in connection pool.
-- - Compile-time generation of templates. You just can't write a
-- statement with an incorrect number of placeholders.
-- - Ability to map to any types actually supported by the
-- backend.
--
--
-- Links:
--
--
@package hasql
@version 0.6.0
-- | This is the API of the "hasql" library. For an introduction to the
-- package and links to more documentation please refer to the
-- package's index page.
--
-- This API is completely disinfected from exceptions. All
-- error-reporting is explicit and is presented using the Either
-- type.
module Hasql
-- | A connection pool.
data Pool c
-- | Given backend-specific connection settings and pool settings, acquire
-- a backend connection pool, which can then be used to work with the DB.
--
-- When combining Hasql with other libraries, which throw exceptions it
-- makes sence to utilize Control.Exception.bracket like
-- this:
--
--
-- bracket (acquirePool bkndStngs poolStngs) (releasePool) $ \pool -> do
-- session pool $ do
-- ...
-- ... any other IO code
--
acquirePool :: Cx c => CxSettings c -> PoolSettings -> IO (Pool c)
-- | Release all connections acquired by the pool.
releasePool :: Pool c -> IO ()
-- | Settings of a pool.
data PoolSettings
-- | A smart constructor for pool settings.
poolSettings :: Int -> Int -> Maybe PoolSettings
-- | A convenience wrapper around ReaderT, which provides a shared
-- context for execution and error-handling of transactions.
data Session c m r
-- | Execute a session using an established connection pool.
--
-- This is merely a wrapper around runReaderT, so you can run it
-- around every transaction, if you want.
session :: Pool c -> Session c m a -> m (Either (SessionError c) a)
data SessionError c
-- | A backend-specific connection acquisition error. E.g., a failure to
-- establish a connection.
CxError :: (CxError c) -> SessionError c
-- | A backend-specific transaction error. It should cover all possible
-- failures related to an established connection, including the loss of
-- connection, query errors and database failures.
TxError :: (TxError c) -> SessionError c
-- | Attempt to parse a result into an incompatible type. Indicates either
-- a mismatching schema or an incorrect query.
ResultError :: Text -> SessionError c
-- | A statement template packed with its values and settings.
data Stmt c :: * -> *
-- | Produces a lambda-expression, which takes as many parameters as there
-- are placeholders in the quoted text and results in a Stmt.
--
-- E.g.:
--
--
-- selectSum :: Int -> Int -> Stmt c
-- selectSum = [stmt|SELECT (? + ?)|]
--
stmt :: QuasiQuoter
-- | Execute a statement without processing the result.
unitTx :: Stmt c -> Tx c s ()
-- | Execute a statement and count the amount of affected rows. Useful for
-- resolving how many rows were updated or deleted.
countTx :: CxValue c Word64 => Stmt c -> Tx c s Word64
-- | Execute a statement, which produces exactly one result row. E.g.,
-- INSERT, which returns an autoincremented identifier, or
-- SELECT COUNT, or SELECT EXISTS.
--
-- Please note that using this executor for selecting rows is
-- conceptually wrong, since in that case the results are always
-- optional. Use maybeTx, listTx or vectorTx
-- instead.
--
-- If the result is empty this executor will raise ResultError.
singleTx :: RowParser c r => Stmt c -> Tx c s r
-- | Execute a statement, which optionally produces a single result row.
maybeTx :: RowParser c r => Stmt c -> Tx c s (Maybe r)
-- | Execute a statement, and produce a list of results.
listTx :: RowParser c r => Stmt c -> Tx c s [r]
-- | Execute a statement, and produce a vector of results.
vectorTx :: RowParser c r => Stmt c -> Tx c s (Vector r)
-- | Execute a SELECT statement with a cursor, and produce a
-- result stream.
--
-- Cursor allows you to fetch virtually limitless results in a constant
-- memory at a cost of a small overhead. Note that in most databases
-- cursors require establishing a database transaction, so depending on a
-- backend the transaction may result in an error, if you run it
-- improperly.
streamTx :: RowParser c r => Stmt c -> Tx c s (TxListT s (Tx c s) r)
-- | A transaction specialized for a backend connection c,
-- associated with its intermediate results using an anonymous
-- type-argument s (same trick as in ST) and producing a
-- result r.
--
-- Running IO in Tx is prohibited. The motivation is
-- identical to STM: the Tx block may get executed multiple
-- times if any transaction conflicts arise. This will result in your
-- effectful IO code being executed an unpredictable amount of
-- times as well, which, chances are, is not what you want.
data Tx c s r
-- | Execute a transaction in a session.
--
-- This function ensures on the type level, that it's impossible to
-- return TxListT s m r from it.
tx :: (CxTx c, MonadBaseControl IO m) => TxMode -> (forall s. Tx c s r) -> Session c m r
-- | A mode, defining how a transaction should be executed.
--
--
-- - Just (isolationLevel, write) indicates that a database
-- transaction should be established with a specified isolation level and
-- a write mode.
-- - Nothing indicates that there should be no database
-- transaction established on the backend and therefore it should be
-- executed with no ACID guarantees, but also without any induced
-- overhead.
--
type TxMode = Maybe (TxIsolationLevel, TxWriteMode)
-- | For reference see the Wikipedia info.
data TxIsolationLevel :: *
RepeatableReads :: TxIsolationLevel
Serializable :: TxIsolationLevel
ReadCommitted :: TxIsolationLevel
ReadUncommitted :: TxIsolationLevel
-- |
-- - Nothing indicates a "read" mode.
-- - Just True indicates a "write" mode.
-- - Just False indicates a "write" mode without committing.
-- This is useful for testing, allowing you to modify your database,
-- producing some result based on your changes, and to let Hasql roll all
-- the changes back on the exit from the transaction.
--
type TxWriteMode = Maybe Bool
-- | A stream of results, which fetches only those that you reach.
--
-- It's a wrapper around ListT, which uses the same trick as the
-- ST monad to associate with the context transaction and become
-- impossible to be used outside of it. This lets the library ensure that
-- it is safe to automatically release all the connections associated
-- with this stream.
--
-- All the functions of the "list-t" library are applicable to this type,
-- amongst which are head, toList, fold,
-- traverse_.
data TxListT s m r
-- | This class is only intended to be used with the supplied instances,
-- which should be enough to cover all use cases.
class RowParser c r
instance (Eq (CxError c), Eq (TxError c)) => Eq (SessionError c)
instance (Show (CxError c), Show (TxError c)) => Show (SessionError c)
instance MonadBase IO m => MonadBase IO (Session c m)
instance Functor (Tx c s)
instance Applicative (Tx c s)
instance Monad (Tx c s)
instance Monad m => Functor (Session c m)
instance Monad m => Applicative (Session c m)
instance Monad m => Monad (Session c m)
instance MonadIO m => MonadIO (Session c m)
instance Monad m => MonadError (SessionError c) (Session c m)
instance Functor m => Functor (TxListT s m)
instance (Monad m, Functor m) => Applicative (TxListT s m)
instance (Monad m, Functor m) => Alternative (TxListT s m)
instance Monad m => Monad (TxListT s m)
instance MonadTrans (TxListT s)
instance Monad m => MonadPlus (TxListT s m)
instance Monad m => Monoid (TxListT s m r)
instance Monad m => MonadCons (TxListT s m)
instance MonadTransUncons (TxListT s)
instance MFunctor (Session c)
instance MonadBaseControl IO m => MonadBaseControl IO (Session c m)
instance MonadTransControl (Session c)
instance MonadTrans (Session c)