{-# LANGUAGE OverloadedStrings #-}
module Hasql.Private.TransactionIO where
import Control.Applicative
import Data.ByteString (ByteString)
import ByteString.TreeBuilder
import Control.Monad.Trans.Reader
import Control.Monad.Trans.Class
import Control.Monad.Error.Class
import Control.Monad.IO.Unlift
import Data.Acquire
import Control.Monad.Trans.Resource
import Hasql.Statement
import Hasql.Session
import qualified Hasql.Session as Session
import Hasql.Private.Session.UnliftIO
import Hasql.Private.Types
import qualified Hasql.Private.Statements as Statements
newtype TransactionIO a = TransactionIO (ReaderT Transaction Session a)
deriving (forall a b. a -> TransactionIO b -> TransactionIO a
forall a b. (a -> b) -> TransactionIO a -> TransactionIO b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> TransactionIO b -> TransactionIO a
$c<$ :: forall a b. a -> TransactionIO b -> TransactionIO a
fmap :: forall a b. (a -> b) -> TransactionIO a -> TransactionIO b
$cfmap :: forall a b. (a -> b) -> TransactionIO a -> TransactionIO b
Functor, Functor TransactionIO
forall a. a -> TransactionIO a
forall a b. TransactionIO a -> TransactionIO b -> TransactionIO a
forall a b. TransactionIO a -> TransactionIO b -> TransactionIO b
forall a b.
TransactionIO (a -> b) -> TransactionIO a -> TransactionIO b
forall a b c.
(a -> b -> c)
-> TransactionIO a -> TransactionIO b -> TransactionIO c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. TransactionIO a -> TransactionIO b -> TransactionIO a
$c<* :: forall a b. TransactionIO a -> TransactionIO b -> TransactionIO a
*> :: forall a b. TransactionIO a -> TransactionIO b -> TransactionIO b
$c*> :: forall a b. TransactionIO a -> TransactionIO b -> TransactionIO b
liftA2 :: forall a b c.
(a -> b -> c)
-> TransactionIO a -> TransactionIO b -> TransactionIO c
$cliftA2 :: forall a b c.
(a -> b -> c)
-> TransactionIO a -> TransactionIO b -> TransactionIO c
<*> :: forall a b.
TransactionIO (a -> b) -> TransactionIO a -> TransactionIO b
$c<*> :: forall a b.
TransactionIO (a -> b) -> TransactionIO a -> TransactionIO b
pure :: forall a. a -> TransactionIO a
$cpure :: forall a. a -> TransactionIO a
Applicative, Applicative TransactionIO
forall a. a -> TransactionIO a
forall a b. TransactionIO a -> TransactionIO b -> TransactionIO b
forall a b.
TransactionIO a -> (a -> TransactionIO b) -> TransactionIO b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> TransactionIO a
$creturn :: forall a. a -> TransactionIO a
>> :: forall a b. TransactionIO a -> TransactionIO b -> TransactionIO b
$c>> :: forall a b. TransactionIO a -> TransactionIO b -> TransactionIO b
>>= :: forall a b.
TransactionIO a -> (a -> TransactionIO b) -> TransactionIO b
$c>>= :: forall a b.
TransactionIO a -> (a -> TransactionIO b) -> TransactionIO b
Monad, Monad TransactionIO
forall a. IO a -> TransactionIO a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: forall a. IO a -> TransactionIO a
$cliftIO :: forall a. IO a -> TransactionIO a
MonadIO, MonadError QueryError, MonadIO TransactionIO
forall b.
((forall a. TransactionIO a -> IO a) -> IO b) -> TransactionIO b
forall (m :: * -> *).
MonadIO m
-> (forall b. ((forall a. m a -> IO a) -> IO b) -> m b)
-> MonadUnliftIO m
withRunInIO :: forall b.
((forall a. TransactionIO a -> IO a) -> IO b) -> TransactionIO b
$cwithRunInIO :: forall b.
((forall a. TransactionIO a -> IO a) -> IO b) -> TransactionIO b
MonadUnliftIO)
data Transaction = Transaction
instance Semigroup a => Semigroup (TransactionIO a) where
<> :: TransactionIO a -> TransactionIO a -> TransactionIO a
(<>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Semigroup a => a -> a -> a
(<>)
instance Monoid a => Monoid (TransactionIO a) where
mempty :: TransactionIO a
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty
run :: TransactionIO a -> IsolationLevel -> Mode -> Deferrable -> Bool -> Session a
run :: forall a.
TransactionIO a
-> IsolationLevel -> Mode -> Deferrable -> Bool -> Session a
run (TransactionIO ReaderT Transaction Session a
txio) IsolationLevel
isolation Mode
mode Deferrable
deferrable Bool
preparable = forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
runResourceT forall a b. (a -> b) -> a -> b
$ do
UnliftIO forall a. Session a -> IO a
runInIO <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadUnliftIO m => m (UnliftIO m)
askUnliftIO
let acq :: Acquire Transaction
acq = forall a. IO a -> (a -> ReleaseType -> IO ()) -> Acquire a
mkAcquireType (forall a. Session a -> IO a
runInIO forall a b. (a -> b) -> a -> b
$ IsolationLevel -> Mode -> Deferrable -> Bool -> Session Transaction
startTransaction IsolationLevel
isolation Mode
mode Deferrable
deferrable Bool
preparable) ((forall a. Session a -> IO a
runInIO forall b c a. (b -> c) -> (a -> b) -> a -> c
.) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Transaction -> ReleaseType -> Session ()
endTransaction Bool
preparable)
(ReleaseKey
_, Transaction
tx) <- forall (m :: * -> *) a.
MonadResource m =>
Acquire a -> m (ReleaseKey, a)
allocateAcquire Acquire Transaction
acq
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT Transaction Session a
txio Transaction
tx
sql :: ByteString -> TransactionIO ()
sql :: ByteString -> TransactionIO ()
sql = forall a. ReaderT Transaction Session a -> TransactionIO a
TransactionIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Session ()
Session.sql
statement :: params -> Statement params result -> TransactionIO result
statement :: forall params result.
params -> Statement params result -> TransactionIO result
statement params
params Statement params result
stmt = forall a. ReaderT Transaction Session a -> TransactionIO a
TransactionIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall params result.
params -> Statement params result -> Session result
Session.statement params
params Statement params result
stmt
startTransaction :: IsolationLevel -> Mode -> Deferrable -> Bool -> Session Transaction
startTransaction :: IsolationLevel -> Mode -> Deferrable -> Bool -> Session Transaction
startTransaction IsolationLevel
isolation Mode
mode Deferrable
deferrable Bool
prepare = do
forall params result.
params -> Statement params result -> Session result
Session.statement () (IsolationLevel -> Mode -> Deferrable -> Bool -> Statement () ()
Statements.startTransaction IsolationLevel
isolation Mode
mode Deferrable
deferrable Bool
prepare)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Transaction
Transaction
endTransaction :: Bool -> Transaction -> ReleaseType -> Session ()
endTransaction :: Bool -> Transaction -> ReleaseType -> Session ()
endTransaction Bool
prepare Transaction
tx = \case
ReleaseType
ReleaseEarly -> Bool -> Transaction -> Session ()
commitTransaction Bool
prepare Transaction
tx
ReleaseType
ReleaseNormal -> Bool -> Transaction -> Session ()
commitTransaction Bool
prepare Transaction
tx
ReleaseType
ReleaseException -> Bool -> Transaction -> Session ()
rollbackTransaction Bool
prepare Transaction
tx
commitTransaction :: Bool -> Transaction -> Session ()
commitTransaction :: Bool -> Transaction -> Session ()
commitTransaction Bool
prepare Transaction
Transaction = do
forall params result.
params -> Statement params result -> Session result
Session.statement () (Bool -> Statement () ()
Statements.commitTransaction Bool
prepare)
rollbackTransaction :: Bool -> Transaction -> Session ()
rollbackTransaction :: Bool -> Transaction -> Session ()
rollbackTransaction Bool
prepare Transaction
Transaction = do
forall params result.
params -> Statement params result -> Session result
Session.statement () (Bool -> Statement () ()
Statements.rollbackTransaction Bool
prepare)