@@ -24,8 +24,6 @@ module Squeal.PostgreSQL.Transaction
26 , rollback
26 , rollback
27− , transactionallySchema
28− , transactionallySchema_
29 -- * Transaction Mode
27 -- * Transaction Mode
30 , TransactionMode (..)
28 , TransactionMode (..)
31 , defaultMode
29 , defaultMode
@@ -36,18 +34,17 @@ module Squeal.PostgreSQL.Transaction
38import Control.Monad
36import Control.Monad
40import UnliftIO
37import UnliftIO
42−import qualified Database.PostgreSQL.LibPQ as LibPQ
44import Squeal.PostgreSQL.Manipulation
39import Squeal.PostgreSQL.Manipulation
45import Squeal.PostgreSQL.Render
40import Squeal.PostgreSQL.Render
46import Squeal.PostgreSQL.PQ
41import Squeal.PostgreSQL.PQ
48−{- | Run a computation `transactionally`; first `begin`,
43+{- | Run a computation `transactionally`;
49−then run the computation, then if it raises an exception
45+then run the computation,
46+`onException` `rollback` and rethrow the exception,
50−`rollback`, otherwise `commit` and `return` the result.
47+otherwise `commit` and `return` the result.
52transactionally
49transactionally
53 :: (MonadUnliftIO tx, MonadPQ schemas tx)
50 :: (MonadUnliftIO tx, MonadPQ schemas tx)
@@ -60,7 +57,7 @@ transactionally mode tx = mask $ \restore -> do
61 return result
58 return result
63−-- | Run a computation `transactionally_` in `defaultMode`.
60+-- | Run a computation `transactionally_`, in `defaultMode`.
64transactionally_
61transactionally_
65 :: (MonadUnliftIO tx, MonadPQ schemas tx)
62 :: (MonadUnliftIO tx, MonadPQ schemas tx)
66 => tx x -- ^ run inside a transaction
63 => tx x -- ^ run inside a transaction
@@ -69,11 +66,12 @@ transactionally_ = transactionally defaultMode
71`transactionallyRetry` a computation;
68`transactionallyRetry` a computation;
72−first `begin`,
70+* first `begin`,
73−then `try` the computation,
71+* then `try` the computation,
74−if it raises an serialization failure then `rollback` and restart the transaction,
72+ - if it raises a serialization failure then `rollback` and restart the transaction,
75−if it raises any other exception then `rollback` and rethrow the exception,
73+ - if it raises any other exception then `rollback` and rethrow the exception,
76−otherwise `commit` and `return` the result.
74+ - otherwise `commit` and `return` the result.
78transactionallyRetry
76transactionallyRetry
79 :: (MonadUnliftIO tx, MonadPQ schemas tx)
77 :: (MonadUnliftIO tx, MonadPQ schemas tx)
@@ -98,35 +96,14 @@ begin :: MonadPQ schemas tx => TransactionMode -> tx ()
98begin mode = void . manipulate . UnsafeManipulation $
96begin mode = void . manipulate . UnsafeManipulation $
99 "BEGIN" <+> renderSQL mode <> ";"
97 "BEGIN" <+> renderSQL mode <> ";"
101−-- | @COMMIT@ a schema invariant transaction.
99+-- | @COMMIT@ a transaction.
102commit :: MonadPQ schemas tx => tx ()
100commit :: MonadPQ schemas tx => tx ()
103commit = void . manipulate $ UnsafeManipulation "COMMIT;"
101commit = void . manipulate $ UnsafeManipulation "COMMIT;"
105−-- | @ROLLBACK@ a schema invariant transaction.
103+-- | @ROLLBACK@ a transaction.
106rollback :: MonadPQ schemas tx => tx ()
104rollback :: MonadPQ schemas tx => tx ()
107rollback = void . manipulate $ UnsafeManipulation "ROLLBACK;"
105rollback = void . manipulate $ UnsafeManipulation "ROLLBACK;"
109−-- | Run a schema changing computation `transactionallySchema`.
110−transactionallySchema
113− -> PQ schemas0 schemas1 io x
114− -> PQ schemas0 schemas1 io x
115−transactionallySchema mode u = PQ $ \ conn -> mask $ \ restore -> do
116− _ <- liftIO . LibPQ.exec (unK conn) $
117− "BEGIN" <+> renderSQL mode <> ";"
118− x <- restore (unPQ u conn)
119− `onException` (liftIO (LibPQ.exec (unK conn) "ROLLBACK"))
120− _ <- liftIO $ LibPQ.exec (unK conn) "COMMIT"
123−-- | Run a schema changing computation `transactionallySchema_` in `defaultMode`.
124−transactionallySchema_
126− => PQ schemas0 schemas1 io x
127− -> PQ schemas0 schemas1 io x
128−transactionallySchema_ = transactionallySchema defaultMode
130-- | The available transaction characteristics are the transaction `IsolationLevel`,
107-- | The available transaction characteristics are the transaction `IsolationLevel`,
131-- the transaction `AccessMode` (`ReadWrite` or `ReadOnly`), and the `DeferrableMode`.
108-- the transaction `AccessMode` (`ReadWrite` or `ReadOnly`), and the `DeferrableMode`.
132data TransactionMode = TransactionMode
109data TransactionMode = TransactionMode