on dev help sign in
ephemeral transactions
65b3341Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Transaction.hs+24
@@ -21,6 +21,8 @@ module Squeal.PostgreSQL.Transaction
21 transactionally
21 transactionally
22 , transactionally_
22 , transactionally_
23 , transactionallyRetry
23 , transactionallyRetry
24+ , ephemerally
25+ , ephemerally_
24 , begin
26 , begin
25 , commit
27 , commit
26 , rollback
28 , rollback
@@ -94,6 +96,28 @@ transactionallyRetry mode tx = mask $ \restore ->
94 throwIO err
96 throwIO err
95 Right x -> return x
97 Right x -> return x
96
98
99+{- | Run a computation `ephemerally`;
100+First `begin`, then run the computation, `rollback` and `return`
101+its result.
102+-}
103+ephemerally
104+ :: (MonadIO tx, MonadPQ schemas tx)
105+ => TransactionMode
106+ -> tx x -- ^ run inside an ephemeral transaction
107+ -> tx x
108+ephemerally mode tx = do
109+ manipulate_ $ begin mode
110+ result <- tx
111+ manipulate_ rollback
112+ return result
113+
114+{- | Run a computation `ephemerally` in `defaultMode`. -}
115+ephemerally_
116+ :: (MonadIO tx, MonadPQ schemas tx)
117+ => tx x -- ^ run inside an ephemeral transaction
118+ -> tx x
119+ephemerally_ = ephemerally defaultMode
120+
97-- | @BEGIN@ a transaction.
121-- | @BEGIN@ a transaction.
98begin :: TransactionMode -> Manipulation_ schemas () ()
122begin :: TransactionMode -> Manipulation_ schemas () ()
99begin mode = UnsafeManipulation $ "BEGIN" <+> renderSQL mode
123begin mode = UnsafeManipulation $ "BEGIN" <+> renderSQL mode