module Hasql.Transaction.Private.Transaction where

import qualified Hasql.Session as B
import qualified Hasql.Statement as A
import Hasql.Transaction.Config
import Hasql.Transaction.Private.Prelude
import qualified Hasql.Transaction.Private.Sessions as D

-- |
-- A composable abstraction over the retryable transactions.
--
-- Executes multiple queries under the specified mode and isolation level,
-- while automatically retrying the transaction in case of conflicts.
-- Thus this abstraction closely reproduces the behaviour of 'STM'.
newtype Transaction a
  = Transaction (StateT Bool B.Session a)
  deriving (forall a b. a -> Transaction b -> Transaction a
forall a b. (a -> b) -> Transaction a -> Transaction 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 -> Transaction b -> Transaction a
$c<$ :: forall a b. a -> Transaction b -> Transaction a
fmap :: forall a b. (a -> b) -> Transaction a -> Transaction b
$cfmap :: forall a b. (a -> b) -> Transaction a -> Transaction b
Functor, Functor Transaction
forall a. a -> Transaction a
forall a b. Transaction a -> Transaction b -> Transaction a
forall a b. Transaction a -> Transaction b -> Transaction b
forall a b. Transaction (a -> b) -> Transaction a -> Transaction b
forall a b c.
(a -> b -> c) -> Transaction a -> Transaction b -> Transaction 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. Transaction a -> Transaction b -> Transaction a
$c<* :: forall a b. Transaction a -> Transaction b -> Transaction a
*> :: forall a b. Transaction a -> Transaction b -> Transaction b
$c*> :: forall a b. Transaction a -> Transaction b -> Transaction b
liftA2 :: forall a b c.
(a -> b -> c) -> Transaction a -> Transaction b -> Transaction c
$cliftA2 :: forall a b c.
(a -> b -> c) -> Transaction a -> Transaction b -> Transaction c
<*> :: forall a b. Transaction (a -> b) -> Transaction a -> Transaction b
$c<*> :: forall a b. Transaction (a -> b) -> Transaction a -> Transaction b
pure :: forall a. a -> Transaction a
$cpure :: forall a. a -> Transaction a
Applicative, Applicative Transaction
forall a. a -> Transaction a
forall a b. Transaction a -> Transaction b -> Transaction b
forall a b. Transaction a -> (a -> Transaction b) -> Transaction 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 -> Transaction a
$creturn :: forall a. a -> Transaction a
>> :: forall a b. Transaction a -> Transaction b -> Transaction b
$c>> :: forall a b. Transaction a -> Transaction b -> Transaction b
>>= :: forall a b. Transaction a -> (a -> Transaction b) -> Transaction b
$c>>= :: forall a b. Transaction a -> (a -> Transaction b) -> Transaction b
Monad)

instance (Semigroup a) => Semigroup (Transaction a) where
  <> :: Transaction a -> Transaction a -> Transaction 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 (Transaction a) where
  mempty :: Transaction a
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty

-- |
-- Execute the transaction using the provided isolation level and mode.
{-# INLINE run #-}
run :: Transaction a -> IsolationLevel -> Mode -> Bool -> B.Session a
run :: forall a.
Transaction a -> IsolationLevel -> Mode -> Bool -> Session a
run (Transaction StateT Bool Session a
session) IsolationLevel
isolation Mode
mode Bool
preparable =
  forall a.
IsolationLevel -> Mode -> Session (a, Bool) -> Bool -> Session a
D.inRetryingTransaction IsolationLevel
isolation Mode
mode (forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT Bool Session a
session Bool
True) Bool
preparable

-- |
-- Possibly a multi-statement query,
-- which however cannot be parameterized or prepared,
-- nor can any results of it be collected.
{-# INLINE sql #-}
sql :: ByteString -> Transaction ()
sql :: ByteString -> Transaction ()
sql =
  forall a. StateT Bool Session a -> Transaction a
Transaction forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> Session ()
B.sql

-- |
-- Parameters and a specification of the parametric query to apply them to.
{-# INLINE statement #-}
statement :: a -> A.Statement a b -> Transaction b
statement :: forall a b. a -> Statement a b -> Transaction b
statement a
params Statement a b
statement =
  forall a. StateT Bool Session a -> Transaction a
Transaction forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat 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
B.statement a
params Statement a b
statement

-- |
-- Cause transaction to eventually roll back.
{-# INLINE condemn #-}
condemn :: Transaction ()
condemn :: Transaction ()
condemn =
  forall a. StateT Bool Session a -> Transaction a
Transaction forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put Bool
False