Skip to content

Commit be22e3f

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

File tree

10 files changed

+478
-339
lines changed

10 files changed

+478
-339
lines changed

src/Database/PostgreSQL/PQTypes/Class.hs

Lines changed: 20 additions & 18 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 :: HasCallStack => 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 :: HasCallStack => 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 :: HasCallStack => m ()
5961

6062
-- | Attempt to receive a notification from the server. This
6163
-- function waits until a notification arrives or specified
@@ -72,15 +74,15 @@ class (Applicative m, Monad m) => MonadDB m where
7274
-- for further info), therefore calling this function within
7375
-- a transaction block will return 'Just' only if notifications
7476
-- were received before the transaction began.
75-
getNotification :: Int -> m (Maybe Notification)
77+
getNotification :: HasCallStack => Int -> m (Maybe Notification)
7678

7779
-- | Execute supplied monadic action with new connection
7880
-- using current connection source and transaction settings.
7981
--
8082
-- Particularly useful when you want to spawn a new thread, but
8183
-- do not want the connection in child thread to be shared with
8284
-- the parent one.
83-
withNewConnection :: m a -> m a
85+
withNewConnection :: HasCallStack => m a -> m a
8486

8587
-- | Generic, overlappable instance.
8688
instance
@@ -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)