@@ -4,13 +4,12 @@ module Database.PostgreSQL.Query.Functions
4
4
, pgQueryWithMasker
5
5
, pgExecute
6
6
, pgExecuteWithMasker
7
- , pgQueryEntities
8
7
-- * Transactions
9
- , pgAtomically
8
+ , pgWithTransaction
10
9
, pgWithSavepoint
11
10
, pgWithTransactionMode
12
- -- , pgWithTransactionModeRetry
13
- -- , pgWithTransactionSerializable
11
+ , pgWithTransactionModeRetry
12
+ , pgWithTransactionSerializable
14
13
-- * Auxiliary
15
14
, pgRepsertRow
16
15
) where
@@ -22,16 +21,11 @@ import Control.Monad.Logger
22
21
import Control.Monad.Trans.Control
23
22
import Data.Int ( Int64 )
24
23
import Data.Monoid
25
- import Data.Proxy ( Proxy (.. ) )
26
- import Data.Typeable ( Typeable )
27
- import Database.PostgreSQL.Query.Entity
28
24
import Database.PostgreSQL.Query.Internal
29
25
import Database.PostgreSQL.Query.SqlBuilder
30
26
import Database.PostgreSQL.Query.TH
31
27
import Database.PostgreSQL.Query.Types
32
28
import Database.PostgreSQL.Simple
33
- import Database.PostgreSQL.Simple.FromField
34
- import Database.PostgreSQL.Simple.ToField
35
29
import Database.PostgreSQL.Simple.Transaction
36
30
37
31
import qualified Data.Text.Encoding as T
@@ -88,74 +82,62 @@ pgExecuteWithMasker masker q = withPGConnection $ \c -> do
88
82
logDebugN $ T. decodeUtf8 logBs
89
83
liftBase $ execute_ c queryBs
90
84
91
- -- | Executes arbitrary query and parses it as entities and their ids
92
- pgQueryEntities :: ( ToSqlBuilder q , HasPostgres m , MonadLogger m , Entity a
93
- , FromRow a , FromField (EntityId a ))
94
- => q -> m [Ent a ]
95
- pgQueryEntities q =
96
- map toTuples <$> pgQuery q
97
- where
98
- runTrans con = liftLevel $ control $ \ runInIO -> do
99
- withTransaction con $ runInIO action
100
- runSP con = liftLevel $ control $ \ runInIO -> do
101
- withSavepoint con $ runInIO action
102
-
103
- -- | Execute query inside savepoint. Requires monad to be a monad of transaction
104
- -- or savepoint at least
105
- pgWithSavepoint
106
- :: ( MonadTransaction n m , MonadBaseControl IO n
107
- , ('S level ) ~ TransactionLevel m ) -- NOTE: we must be at least in transaction
108
- => n a
109
- -> m a
110
- pgWithSavepoint action = do
111
- con <- askConnection
112
- liftLevel $ control $ \ runInIO -> do
113
- withSavepoint con $ runInIO action
85
+ -- | Execute all queries inside one transaction. Rollback transaction on exceptions
86
+ pgWithTransaction :: (HasPostgres m , MonadBaseControl IO m , TransactionSafe m )
87
+ => m a
88
+ -> m a
89
+ pgWithTransaction action = withPGConnection $ \ con -> do
90
+ control $ \ runInIO -> do
91
+ withTransaction con $ runInIO action
92
+
93
+ -- | Same as `pgWithTransaction` but executes queries inside savepoint
94
+ pgWithSavepoint :: (HasPostgres m , MonadBaseControl IO m , TransactionSafe m ) => m a -> m a
95
+ pgWithSavepoint action = withPGConnection $ \ con -> do
96
+ control $ \ runInIO -> do
97
+ withSavepoint con $ runInIO action
114
98
115
99
-- | Wrapper for 'withTransactionMode': Execute an action inside a SQL
116
100
-- transaction with a given transaction mode.
117
- pgWithTransactionMode
118
- :: ( MonadTransaction n m , 'Z ~ TransactionLevel m
119
- , MonadBaseControl IO n )
120
- => TransactionMode
121
- -> n a
122
- -> m a
123
- pgWithTransactionMode tmode ma = do
124
- con <- askConnection
125
- liftLevel $ control $ \ runInIO -> do
126
- withTransactionMode tmode con $ runInIO ma
127
-
128
- -- -- | Wrapper for 'withTransactionModeRetry': Like 'pgWithTransactionMode',
129
- -- -- but also takes a custom callback to determine if a transaction
130
- -- -- should be retried if an SqlError occurs. If the callback returns
131
- -- -- True, then the transaction will be retried. If the callback returns
132
- -- -- False, or an exception other than an SqlError occurs then the
133
- -- -- transaction will be rolled back and the exception rethrown.
134
- -- pgWithTransactionModeRetry :: (MonadPostgres m, MonadBaseControl IO m, TransactionSafe m)
135
- -- => TransactionMode
136
- -- -> (SqlError -> Bool)
137
- -- -> m a
138
- -- -> m a
139
- -- pgWithTransactionModeRetry tmode epred ma = withPGConnection $ \con -> do
140
- -- control $ \runInIO -> do
141
- -- withTransactionModeRetry tmode epred con $ runInIO ma
142
-
143
- -- -- | Wrapper for 'withTransactionSerializable': Execute an action
144
- -- -- inside of a 'Serializable' transaction. If a serialization failure
145
- -- -- occurs, roll back the transaction and try again. Be warned that
146
- -- -- this may execute the IO action multiple times.
147
- -- --
148
- -- -- A Serializable transaction creates the illusion that your program
149
- -- -- has exclusive access to the database. This means that, even in a
150
- -- -- concurrent setting, you can perform queries in sequence without
151
- -- -- having to worry about what might happen between one statement and
152
- -- -- the next.
153
- -- pgWithTransactionSerializable :: (MonadPostgres m, MonadBaseControl IO m, TransactionSafe m)
154
- -- => m a
155
- -- -> m a
156
- -- pgWithTransactionSerializable ma = withPGConnection $ \con -> do
157
- -- control $ \runInIO -> do
158
- -- withTransactionSerializable con $ runInIO ma
101
+ pgWithTransactionMode :: (HasPostgres m , MonadBaseControl IO m , TransactionSafe m )
102
+ => TransactionMode
103
+ -> m a
104
+ -> m a
105
+ pgWithTransactionMode tmode ma = withPGConnection $ \ con -> do
106
+ control $ \ runInIO -> do
107
+ withTransactionMode tmode con $ runInIO ma
108
+
109
+ -- | Wrapper for 'withTransactionModeRetry': Like 'pgWithTransactionMode',
110
+ -- but also takes a custom callback to determine if a transaction
111
+ -- should be retried if an SqlError occurs. If the callback returns
112
+ -- True, then the transaction will be retried. If the callback returns
113
+ -- False, or an exception other than an SqlError occurs then the
114
+ -- transaction will be rolled back and the exception rethrown.
115
+ pgWithTransactionModeRetry :: (HasPostgres m , MonadBaseControl IO m , TransactionSafe m )
116
+ => TransactionMode
117
+ -> (SqlError -> Bool )
118
+ -> m a
119
+ -> m a
120
+ pgWithTransactionModeRetry tmode epred ma = withPGConnection $ \ con -> do
121
+ control $ \ runInIO -> do
122
+ withTransactionModeRetry tmode epred con $ runInIO ma
123
+
124
+ -- | Wrapper for 'withTransactionSerializable': Execute an action
125
+ -- inside of a 'Serializable' transaction. If a serialization failure
126
+ -- occurs, roll back the transaction and try again. Be warned that
127
+ -- this may execute the IO action multiple times.
128
+ --
129
+ -- A Serializable transaction creates the illusion that your program
130
+ -- has exclusive access to the database. This means that, even in a
131
+ -- concurrent setting, you can perform queries in sequence without
132
+ -- having to worry about what might happen between one statement and
133
+ -- the next.
134
+ pgWithTransactionSerializable :: (HasPostgres m , MonadBaseControl IO m , TransactionSafe m )
135
+ => m a
136
+ -> m a
137
+ pgWithTransactionSerializable ma = withPGConnection $ \ con -> do
138
+ control $ \ runInIO -> do
139
+ withTransactionSerializable con $ runInIO ma
140
+
159
141
160
142
161
143
{- | Perform repsert of the same row, first trying "update where" then
0 commit comments