Skip to content

Commit e80ed9a

Browse files
committed
Introduce on-demand connection acquisition mode
1 parent 203e557 commit e80ed9a

File tree

10 files changed

+433
-325
lines changed

10 files changed

+433
-325
lines changed

src/Database/PostgreSQL/PQTypes/Class.hs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,34 @@ class (Applicative m, Monad m) => MonadDB m where
3030
-- given name.
3131
runPreparedQuery :: (HasCallStack, IsSQL sql) => QueryName -> sql -> m Int
3232

33-
-- | Get last SQL query that was executed.
34-
getLastQuery :: m SomeSQL
33+
-- | Get last SQL query that was executed and ID of the server process
34+
-- attached to the session that executed it.
35+
getLastQuery :: m (BackendPid, SomeSQL)
3536

3637
-- | Subsequent queries in the callback do not alter the result of
3738
-- 'getLastQuery'.
3839
withFrozenLastQuery :: m a -> m a
3940

40-
-- | Get ID of the server process attached to the current session.
41-
getBackendPid :: m BackendPid
42-
4341
-- | Get current connection statistics.
44-
getConnectionStats :: HasCallStack => m ConnectionStats
42+
getConnectionStats :: m ConnectionStats
4543

4644
-- | Get current query result.
4745
getQueryResult :: FromRow row => m (Maybe (QueryResult row))
4846

4947
-- | Clear current query result.
5048
clearQueryResult :: m ()
5149

52-
-- | Get current transaction settings.
53-
getTransactionSettings :: m TransactionSettings
50+
-- | Get current connection acquisition mode.
51+
getConnectionAcquisitionMode :: m ConnectionAcquisitionMode
52+
53+
-- | Acquire and hold a connection with a given isolation level and
54+
-- permissions. If the connection is already held, nothing happens.
55+
acquireAndHoldConnection :: IsolationLevel -> Permissions -> m ()
5456

55-
-- | Set transaction settings to supplied ones. Note that it
56-
-- won't change any properties of currently running transaction,
57-
-- only the subsequent ones.
58-
setTransactionSettings :: TransactionSettings -> m ()
57+
-- | Unsafely switch to the 'AcquireOnDemand' mode. This function is unsafe
58+
-- because if a connection is already held, the transaction in progress is
59+
-- commited, so atomicity guarantee is lost.
60+
unsafeAcquireOnDemandConnection :: m ()
5961

6062
-- | Attempt to receive a notification from the server. This
6163
-- function waits until a notification arrives or specified
@@ -97,11 +99,11 @@ instance
9799
runPreparedQuery name = withFrozenCallStack $ lift . runPreparedQuery name
98100
getLastQuery = lift getLastQuery
99101
withFrozenLastQuery m = controlT $ \run -> withFrozenLastQuery (run m)
100-
getBackendPid = lift getBackendPid
101-
getConnectionStats = withFrozenCallStack $ lift getConnectionStats
102+
getConnectionStats = lift getConnectionStats
102103
getQueryResult = lift getQueryResult
103104
clearQueryResult = lift clearQueryResult
104-
getTransactionSettings = lift getTransactionSettings
105-
setTransactionSettings = lift . setTransactionSettings
105+
getConnectionAcquisitionMode = lift getConnectionAcquisitionMode
106+
acquireAndHoldConnection isoLevel = lift . acquireAndHoldConnection isoLevel
107+
unsafeAcquireOnDemandConnection = lift unsafeAcquireOnDemandConnection
106108
getNotification = lift . getNotification
107109
withNewConnection m = controlT $ \run -> withNewConnection (run m)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
module Database.PostgreSQL.PQTypes.Internal.BackendPid
22
( BackendPid (..)
3+
, noBackendPid
34
) where
45

56
-- | Process ID of the server process attached to the current session.
67
newtype BackendPid = BackendPid Int
78
deriving newtype (Eq, Ord, Show)
9+
10+
noBackendPid :: BackendPid
11+
noBackendPid = BackendPid 0

0 commit comments

Comments
 (0)