4
4
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5
5
{-# LANGUAGE MultiParamTypeClasses #-}
6
6
7
+ {-|
8
+ Module : Database.PostgreSQL.Transaction
9
+ Copyright : (c) Helium Systems, Inc.
10
+ License : MIT
11
+
12
+ Stability : experimental
13
+ Portability : GHC
14
+
15
+ This module provdes querying with and executing SQL statements that replace
16
+ the ones found in @Database.PostgreSQL.Simple@.
17
+
18
+ Please note that the parameter order is reversed when compared to the functions
19
+ provided by postgresql-simple. This is a conscious choice made so as to ease
20
+ use of a SQL quasiquoter.
21
+
22
+ -}
23
+
7
24
module Database.PostgreSQL.Transaction
8
- ( PGTransaction
9
- , PGTransactionT
25
+ ( PGTransactionT
26
+ , PGTransaction
10
27
, runPGTransactionT
11
28
, runPGTransactionT'
12
29
, runPGTransactionIO
@@ -34,6 +51,8 @@ import Database.PostgreSQL.Simple.ToRow
34
51
import qualified Database.PostgreSQL.Simple.Transaction as Postgres.Transaction
35
52
import qualified Database.PostgreSQL.Simple.Types as PGTypes
36
53
54
+ -- | The Postgres transaction monad transformer. This is implemented as a monad transformer
55
+ -- so as to integrate properly with monadic logging libraries like @monad-logger@ or @katip@.
37
56
newtype PGTransactionT m a =
38
57
PGTransactionT (ReaderT Postgres. Connection m a )
39
58
deriving ( Functor
@@ -44,8 +63,11 @@ newtype PGTransactionT m a =
44
63
, MonadIO
45
64
)
46
65
66
+ -- | A type alias for occurrences of 'PGTransactionT' in the IO monad.
47
67
type PGTransaction = PGTransactionT IO
48
68
69
+ -- | Runs a transaction in the base monad @m@ with a provided 'IsolationLevel'.
70
+ -- An instance of MonadBaseControl is required so as to handle lifted calls to 'catch' correctly.
49
71
runPGTransactionT' :: MonadBaseControl IO m
50
72
=> Postgres.Transaction. IsolationLevel
51
73
-> PGTransactionT m a
@@ -56,6 +78,7 @@ runPGTransactionT' isolation (PGTransactionT pgTrans) conn =
56
78
Postgres.Transaction. withTransactionLevel isolation conn (run pgTrans)
57
79
in control runTransaction `runReaderT` conn
58
80
81
+ -- | As 'runPGTransactionT'', but with the 'DefaultIsolationLevel' isolation level.
59
82
runPGTransactionT :: MonadBaseControl IO m
60
83
=> PGTransactionT m a
61
84
-> Postgres. Connection
@@ -95,12 +118,15 @@ execute :: (ToRow input, MonadIO m)
95
118
-> PGTransactionT m Int64
96
119
execute params q = ask >>= (\ conn -> liftIO $ Postgres. execute conn q params)
97
120
121
+ -- | As 'Database.PostgreSQL.Simple.executeMany', but operating in the transaction monad.
122
+ -- If any one of these computations fails, the entire block will be rolled back.
98
123
executeMany :: (ToRow input , MonadIO m )
99
124
=> [input ]
100
125
-> Postgres. Query
101
126
-> PGTransactionT m Int64
102
127
executeMany params q = ask >>= (\ conn -> liftIO $ Postgres. executeMany conn q params)
103
128
129
+ -- | Identical to 'Database.PostgreSQL.Simple.returning', save parameter order.
104
130
returning :: (ToRow input , FromRow output , MonadIO m )
105
131
=> [input ]
106
132
-> Postgres. Query
@@ -132,6 +158,7 @@ queryOnly :: (ToRow input, FromField f, MonadIO m)
132
158
-> PGTransactionT m (Maybe f )
133
159
queryOnly params q = fmap Postgres. fromOnly <$> queryHead params q
134
160
161
+ -- | As 'Database.PostgreSQL.Simple.formatQuery', save parameter order.
135
162
formatQuery :: (ToRow input , MonadIO m )
136
163
=> input
137
164
-> Postgres. Query
0 commit comments