mirror of
https://github.com/nikita-volkov/hasql.git
synced 2024-11-25 17:26:28 +03:00
Make the API Session-centric
This commit is contained in:
parent
5379bf6fea
commit
81c9d9996c
@ -65,6 +65,8 @@ library
|
|||||||
loch-th == 0.2.*,
|
loch-th == 0.2.*,
|
||||||
placeholders == 0.1.*,
|
placeholders == 0.1.*,
|
||||||
-- general:
|
-- general:
|
||||||
|
monad-control == 0.3.*,
|
||||||
|
transformers-base == 0.4.*,
|
||||||
safe >= 0.3.8 && < 0.4,
|
safe >= 0.3.8 && < 0.4,
|
||||||
mmorph == 1.0.*,
|
mmorph == 1.0.*,
|
||||||
mtl-prelude == 2.*,
|
mtl-prelude == 2.*,
|
||||||
|
@ -1,22 +1,21 @@
|
|||||||
module Hasql
|
module Hasql
|
||||||
(
|
(
|
||||||
-- * Connections Pool
|
|
||||||
Pool,
|
|
||||||
PoolSettings,
|
|
||||||
poolSettings,
|
|
||||||
acquirePool,
|
|
||||||
releasePool,
|
|
||||||
|
|
||||||
-- * Session
|
-- * Session
|
||||||
Session,
|
Session,
|
||||||
sessionInner,
|
sessionInner,
|
||||||
|
|
||||||
|
-- * Error
|
||||||
|
Error(..),
|
||||||
|
|
||||||
-- * Transaction
|
-- * Transaction
|
||||||
Tx,
|
Tx,
|
||||||
Mode,
|
Mode,
|
||||||
Backend.IsolationLevel(..),
|
Backend.IsolationLevel(..),
|
||||||
txSession,
|
txSession,
|
||||||
|
|
||||||
|
-- * Statement Quasi-Quoter
|
||||||
|
QQ.q,
|
||||||
|
|
||||||
-- * Statement Execution
|
-- * Statement Execution
|
||||||
StatementTx,
|
StatementTx,
|
||||||
unitTx,
|
unitTx,
|
||||||
@ -24,17 +23,11 @@ module Hasql
|
|||||||
streamTx,
|
streamTx,
|
||||||
cursorStreamTx,
|
cursorStreamTx,
|
||||||
|
|
||||||
-- * Statement Quasi-Quoter
|
-- * Results Stream
|
||||||
QQ.q,
|
|
||||||
|
|
||||||
-- ** Error
|
|
||||||
Error(..),
|
|
||||||
|
|
||||||
-- ** Results Stream
|
|
||||||
TxStream,
|
TxStream,
|
||||||
TxStreamT,
|
TxStreamT,
|
||||||
|
|
||||||
-- ** Row parser
|
-- * Row parser
|
||||||
RowParser.RowParser(..),
|
RowParser.RowParser(..),
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
@ -49,6 +42,38 @@ import qualified ListT
|
|||||||
import qualified Data.Pool as Pool
|
import qualified Data.Pool as Pool
|
||||||
|
|
||||||
|
|
||||||
|
-- * Session
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
-- |
|
||||||
|
-- A monad transformer,
|
||||||
|
-- which executes transactions.
|
||||||
|
type Session b =
|
||||||
|
ReaderT (Pool b)
|
||||||
|
|
||||||
|
-- |
|
||||||
|
-- Given backend settings, pool settings, and a session monad transformer
|
||||||
|
-- execute it in the inner monad.
|
||||||
|
sessionInner ::
|
||||||
|
Backend.Backend b => MonadBaseControl IO m =>
|
||||||
|
b -> PoolSettings -> Session b m r -> m r
|
||||||
|
sessionInner backend settings reader =
|
||||||
|
join $ liftM restoreM $ liftBaseWith $ \runInIO ->
|
||||||
|
mask $ \unmask -> do
|
||||||
|
p <- acquirePool backend settings
|
||||||
|
r <- ($ releasePool p) $ onException $ unmask $ runInIO $ runReaderT reader p
|
||||||
|
releasePool p
|
||||||
|
return r
|
||||||
|
|
||||||
|
-- |
|
||||||
|
-- Execute a transaction in a session.
|
||||||
|
txSession ::
|
||||||
|
Backend.Backend b => MonadBase IO m =>
|
||||||
|
Mode -> (forall s. Tx b s r) -> Session b m r
|
||||||
|
txSession m t =
|
||||||
|
ReaderT $ \p -> liftBase $ usePool (\c -> runTx c m t) p
|
||||||
|
|
||||||
|
|
||||||
-- * Connections Pool
|
-- * Connections Pool
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
@ -102,22 +127,6 @@ usePool f (Pool p) =
|
|||||||
Pool.withResource p f
|
Pool.withResource p f
|
||||||
|
|
||||||
|
|
||||||
-- * Session
|
|
||||||
-------------------------
|
|
||||||
|
|
||||||
-- |
|
|
||||||
-- A session monad transformer,
|
|
||||||
-- which is an adaptation of the 'ReaderT' API over the connections pool.
|
|
||||||
type Session b =
|
|
||||||
ReaderT (Pool b)
|
|
||||||
|
|
||||||
-- |
|
|
||||||
-- Run the session monad transformer in the inner monad.
|
|
||||||
sessionInner :: Pool b -> Session b m r -> m r
|
|
||||||
sessionInner pool reader =
|
|
||||||
runReaderT reader pool
|
|
||||||
|
|
||||||
|
|
||||||
-- * Transaction
|
-- * Transaction
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
@ -142,12 +151,10 @@ newtype Tx b s r =
|
|||||||
type Mode =
|
type Mode =
|
||||||
Maybe (Backend.IsolationLevel, Bool)
|
Maybe (Backend.IsolationLevel, Bool)
|
||||||
|
|
||||||
-- |
|
runTx ::
|
||||||
-- Execute a transaction on a connection.
|
|
||||||
txIO ::
|
|
||||||
Backend b =>
|
Backend b =>
|
||||||
Backend.Connection b -> Mode -> (forall s. Tx b s r) -> IO r
|
Backend.Connection b -> Mode -> (forall s. Tx b s r) -> IO r
|
||||||
txIO connection mode (Tx reader) =
|
runTx connection mode (Tx reader) =
|
||||||
handle backendHandler $
|
handle backendHandler $
|
||||||
maybe (const id) inTransaction mode connection (runReaderT reader connection)
|
maybe (const id) inTransaction mode connection (runReaderT reader connection)
|
||||||
where
|
where
|
||||||
@ -173,14 +180,6 @@ txIO connection mode (Tx reader) =
|
|||||||
Backend.UnexpectedResultStructure t -> throwIO $ UnexpectedResultStructure t
|
Backend.UnexpectedResultStructure t -> throwIO $ UnexpectedResultStructure t
|
||||||
Backend.TransactionConflict -> $bug "Unexpected TransactionConflict exception"
|
Backend.TransactionConflict -> $bug "Unexpected TransactionConflict exception"
|
||||||
|
|
||||||
-- |
|
|
||||||
-- Execute a transaction in a session.
|
|
||||||
txSession ::
|
|
||||||
Backend.Backend b => MonadIO m =>
|
|
||||||
Mode -> (forall s. Tx b s r) -> Session b m r
|
|
||||||
txSession m t =
|
|
||||||
ReaderT $ \p -> liftIO $ usePool (\c -> txIO c m t) p
|
|
||||||
|
|
||||||
|
|
||||||
-- * Results Stream
|
-- * Results Stream
|
||||||
-------------------------
|
-------------------------
|
||||||
@ -236,7 +235,7 @@ data Error =
|
|||||||
instance Exception Error
|
instance Exception Error
|
||||||
|
|
||||||
|
|
||||||
-- * Transactions
|
-- * Statements execution
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
-- |
|
-- |
|
||||||
|
@ -19,6 +19,14 @@ import MTLPrelude as Exports hiding (shift)
|
|||||||
-------------------------
|
-------------------------
|
||||||
import Control.Monad.Morph as Exports
|
import Control.Monad.Morph as Exports
|
||||||
|
|
||||||
|
-- monad-control
|
||||||
|
-------------------------
|
||||||
|
import Control.Monad.Trans.Control as Exports
|
||||||
|
|
||||||
|
-- transformers-base
|
||||||
|
-------------------------
|
||||||
|
import Control.Monad.Base as Exports
|
||||||
|
|
||||||
-- safe
|
-- safe
|
||||||
-------------------------
|
-------------------------
|
||||||
import Safe as Exports
|
import Safe as Exports
|
||||||
|
Loading…
Reference in New Issue
Block a user