mirror of
https://github.com/nikita-volkov/hasql.git
synced 2024-11-26 23:33:02 +03:00
A dual execution model
This commit is contained in:
parent
499b0bba60
commit
25cea66d34
@ -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
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user