Skip to content

Commit 4eac931

Browse files
committed
revert non-refactoring part of previous merge
1 parent 6fb7675 commit 4eac931

File tree

3 files changed

+173
-203
lines changed

3 files changed

+173
-203
lines changed

postgresql-query.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ library
4343
, Database.PostgreSQL.Query.Types
4444

4545
default-extensions: CPP
46+
, ConstraintKinds
4647
, DataKinds
4748
, DeriveDataTypeable
4849
, DeriveGeneric
@@ -54,7 +55,6 @@ library
5455
, GeneralizedNewtypeDeriving
5556
, LambdaCase
5657
, MultiParamTypeClasses
57-
, NoMonomorphismRestriction
5858
, OverloadedStrings
5959
, QuasiQuotes
6060
, RecordWildCards

src/Database/PostgreSQL/Query/Functions.hs

Lines changed: 56 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ module Database.PostgreSQL.Query.Functions
44
, pgQueryWithMasker
55
, pgExecute
66
, pgExecuteWithMasker
7-
, pgQueryEntities
87
-- * Transactions
9-
, pgAtomically
8+
, pgWithTransaction
109
, pgWithSavepoint
1110
, pgWithTransactionMode
12-
-- , pgWithTransactionModeRetry
13-
-- , pgWithTransactionSerializable
11+
, pgWithTransactionModeRetry
12+
, pgWithTransactionSerializable
1413
-- * Auxiliary
1514
, pgRepsertRow
1615
) where
@@ -22,16 +21,11 @@ import Control.Monad.Logger
2221
import Control.Monad.Trans.Control
2322
import Data.Int ( Int64 )
2423
import Data.Monoid
25-
import Data.Proxy ( Proxy(..) )
26-
import Data.Typeable ( Typeable )
27-
import Database.PostgreSQL.Query.Entity
2824
import Database.PostgreSQL.Query.Internal
2925
import Database.PostgreSQL.Query.SqlBuilder
3026
import Database.PostgreSQL.Query.TH
3127
import Database.PostgreSQL.Query.Types
3228
import Database.PostgreSQL.Simple
33-
import Database.PostgreSQL.Simple.FromField
34-
import Database.PostgreSQL.Simple.ToField
3529
import Database.PostgreSQL.Simple.Transaction
3630

3731
import qualified Data.Text.Encoding as T
@@ -88,74 +82,62 @@ pgExecuteWithMasker masker q = withPGConnection $ \c -> do
8882
logDebugN $ T.decodeUtf8 logBs
8983
liftBase $ execute_ c queryBs
9084

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
11498

11599
-- | Wrapper for 'withTransactionMode': Execute an action inside a SQL
116100
-- 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+
159141

160142

161143
{- | Perform repsert of the same row, first trying "update where" then

0 commit comments

Comments
 (0)