mirror of
https://github.com/nikita-volkov/hasql.git
synced 2024-11-22 18:22:05 +03:00
51 lines
1.8 KiB
Haskell
51 lines
1.8 KiB
Haskell
-- |
|
|
-- This module provides a low-level effectful API dealing with the connections to the database.
|
|
module Hasql.Connection.Core where
|
|
|
|
import Database.PostgreSQL.LibPQ qualified as LibPQ
|
|
import Hasql.IO qualified as IO
|
|
import Hasql.Prelude
|
|
import Hasql.PreparedStatementRegistry qualified as PreparedStatementRegistry
|
|
import Hasql.Settings qualified as Settings
|
|
|
|
-- |
|
|
-- A single connection to the database.
|
|
data Connection
|
|
= Connection !(MVar LibPQ.Connection) !Bool !PreparedStatementRegistry.PreparedStatementRegistry
|
|
|
|
-- |
|
|
-- Possible details of the connection acquistion error.
|
|
type ConnectionError =
|
|
Maybe ByteString
|
|
|
|
-- |
|
|
-- Acquire a connection using the provided settings encoded according to the PostgreSQL format.
|
|
acquire :: Settings.Settings -> IO (Either ConnectionError Connection)
|
|
acquire settings =
|
|
{-# SCC "acquire" #-}
|
|
runExceptT $ do
|
|
pqConnection <- lift (IO.acquireConnection settings)
|
|
lift (IO.checkConnectionStatus pqConnection) >>= traverse throwError
|
|
lift (IO.initConnection pqConnection)
|
|
integerDatetimes <- lift (IO.getIntegerDatetimes pqConnection)
|
|
registry <- lift (IO.acquirePreparedStatementRegistry)
|
|
pqConnectionRef <- lift (newMVar pqConnection)
|
|
pure (Connection pqConnectionRef integerDatetimes registry)
|
|
|
|
-- |
|
|
-- Release the connection.
|
|
release :: Connection -> IO ()
|
|
release (Connection pqConnectionRef _ _) =
|
|
mask_ $ do
|
|
nullConnection <- LibPQ.newNullConnection
|
|
pqConnection <- swapMVar pqConnectionRef nullConnection
|
|
IO.releaseConnection pqConnection
|
|
|
|
-- |
|
|
-- Execute an operation on the raw @libpq@ 'LibPQ.Connection'.
|
|
--
|
|
-- The access to the connection is exclusive.
|
|
withLibPQConnection :: Connection -> (LibPQ.Connection -> IO a) -> IO a
|
|
withLibPQConnection (Connection pqConnectionRef _ _) =
|
|
withMVar pqConnectionRef
|