A dual execution model

This commit is contained in:
Nikita Volkov 2014-10-23 16:17:47 +04:00
parent 499b0bba60
commit 25cea66d34

View File

@ -10,8 +10,21 @@ module Hasql
Transaction.Mode,
-- ** Execution
-- | For your convenience there are two models of execution:
--
-- * A simple function on IO.
--
-- * A session monad transformer,
-- which is an adaptation of the 'ReaderT' API over the connections pool.
-- *** Simple IO
txIO,
-- *** Session
Session,
sessionInner,
txSession,
-- ** Transactions
Transaction.StatementTx,
Transaction.unitTx,
@ -50,3 +63,28 @@ txIO ::
txIO p m t =
Pool.withConnection (\c -> Transaction.txIO m c t) p
-- * Session
-------------------------
-- |
-- A convenience monad transformer,
-- which is just a simple wrapper around a 'ReaderT'.
type Session b =
ReaderT (Pool.Pool b)
-- |
-- Run the session monad transformer in the inner monad.
sessionInner :: Pool.Pool b -> Session b m r -> m r
sessionInner pool reader =
runReaderT reader pool
-- |
-- Execute a transaction in a session.
txSession ::
Backend.Backend b => MonadIO m =>
Transaction.Mode -> (forall s. Transaction.Transaction b s r) -> Session b m r
txSession m t =
ReaderT $ \p -> liftIO $ txIO p m t