@@ -21,6 +21,8 @@ module Squeal.PostgreSQL.Transaction
2121 transactionally
2222 , transactionally_
2323 , transactionallyRetry
24+ , ephemerally
25+ , ephemerally_
2426 , begin
2527 , commit
2628 , rollback
@@ -94,6 +96,28 @@ transactionallyRetry mode tx = mask $ \restore ->
9496 throwIO err
9597 Right x -> return x
9698
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+
97121-- | @BEGIN@ a transaction.
98122begin :: TransactionMode -> Manipulation_ schemas () ()
99123begin mode = UnsafeManipulation $ "BEGIN" <+> renderSQL mode