module Hasql.Engine.Contexts.Session where
import Data.HashMap.Strict qualified as HashMap
import Data.HashSet qualified as HashSet
import Hasql.CodecsVocab.QualifiedTypeName qualified as CodecsVocab.QualifiedTypeName
import Hasql.Comms.Roundtrip qualified as Comms.Roundtrip
import Hasql.ConnectionState qualified as ConnectionState
import Hasql.ConnectionState.OidCache qualified as OidCache
import Hasql.ConnectionState.StatementCache qualified as StatementCache
import Hasql.Engine.Contexts.Pipeline qualified as Pipeline
import Hasql.Engine.Errors qualified as Errors
import Hasql.Engine.PqProcedures.SelectTypeInfo qualified as PqProcedures.SelectTypeInfo
import Hasql.Engine.Statement qualified as Statement
import Hasql.Platform.Prelude
import Pqi qualified as Pq
newtype Session a
= Session (ConnectionState.ConnectionState -> IO (Either Errors.UseError a, ConnectionState.ConnectionState))
run :: Session a -> ConnectionState.ConnectionState -> IO (Either Errors.UseError a, ConnectionState.ConnectionState)
run :: forall a.
Session a
-> ConnectionState -> IO (Either UseError a, ConnectionState)
run (Session ConnectionState -> IO (Either UseError a, ConnectionState)
session) ConnectionState
connectionState = ConnectionState -> IO (Either UseError a, ConnectionState)
session ConnectionState
connectionState
instance Functor Session where
fmap :: forall a b. (a -> b) -> Session a -> Session b
fmap a -> b
f (Session ConnectionState -> IO (Either UseError a, ConnectionState)
m) = (ConnectionState -> IO (Either UseError b, ConnectionState))
-> Session b
forall a.
(ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
Session \ConnectionState
connectionState -> do
(Either UseError a
result, ConnectionState
newConnectionState) <- ConnectionState -> IO (Either UseError a, ConnectionState)
m ConnectionState
connectionState
(Either UseError b, ConnectionState)
-> IO (Either UseError b, ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a -> b) -> Either UseError a -> Either UseError b
forall a b. (a -> b) -> Either UseError a -> Either UseError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Either UseError a
result, ConnectionState
newConnectionState)
instance Applicative Session where
pure :: forall a. a -> Session a
pure a
a = (ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
forall a.
(ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
Session \ConnectionState
connectionState -> (Either UseError a, ConnectionState)
-> IO (Either UseError a, ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Either UseError a
forall a b. b -> Either a b
Right a
a, ConnectionState
connectionState)
Session ConnectionState -> IO (Either UseError (a -> b), ConnectionState)
mf <*> :: forall a b. Session (a -> b) -> Session a -> Session b
<*> Session ConnectionState -> IO (Either UseError a, ConnectionState)
ma = (ConnectionState -> IO (Either UseError b, ConnectionState))
-> Session b
forall a.
(ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
Session \ConnectionState
connectionState -> do
(Either UseError (a -> b)
resultF, ConnectionState
connectionState') <- ConnectionState -> IO (Either UseError (a -> b), ConnectionState)
mf ConnectionState
connectionState
case Either UseError (a -> b)
resultF of
Left UseError
err -> (Either UseError b, ConnectionState)
-> IO (Either UseError b, ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (UseError -> Either UseError b
forall a b. a -> Either a b
Left UseError
err, ConnectionState
connectionState')
Right a -> b
f -> do
(Either UseError a
resultA, ConnectionState
connectionState'') <- ConnectionState -> IO (Either UseError a, ConnectionState)
ma ConnectionState
connectionState'
(Either UseError b, ConnectionState)
-> IO (Either UseError b, ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a -> b) -> Either UseError a -> Either UseError b
forall a b. (a -> b) -> Either UseError a -> Either UseError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Either UseError a
resultA, ConnectionState
connectionState'')
instance Monad Session where
Session ConnectionState -> IO (Either UseError a, ConnectionState)
m >>= :: forall a b. Session a -> (a -> Session b) -> Session b
>>= a -> Session b
f = (ConnectionState -> IO (Either UseError b, ConnectionState))
-> Session b
forall a.
(ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
Session \ConnectionState
connectionState -> do
(Either UseError a
result, ConnectionState
connectionState') <- ConnectionState -> IO (Either UseError a, ConnectionState)
m ConnectionState
connectionState
case Either UseError a
result of
Left UseError
err -> (Either UseError b, ConnectionState)
-> IO (Either UseError b, ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (UseError -> Either UseError b
forall a b. a -> Either a b
Left UseError
err, ConnectionState
connectionState')
Right a
a -> Session b
-> ConnectionState -> IO (Either UseError b, ConnectionState)
forall a.
Session a
-> ConnectionState -> IO (Either UseError a, ConnectionState)
run (a -> Session b
f a
a) ConnectionState
connectionState'
instance MonadIO Session where
liftIO :: forall a. IO a -> Session a
liftIO IO a
io = (ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
forall a.
(ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
Session \ConnectionState
connectionState -> do
a
a <- IO a
io
pure (a -> Either UseError a
forall a b. b -> Either a b
Right a
a, ConnectionState
connectionState)
instance MonadError Errors.SessionError Session where
throwError :: forall a. SessionError -> Session a
throwError SessionError
err = (ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
forall a.
(ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
Session \ConnectionState
connectionState -> (Either UseError a, ConnectionState)
-> IO (Either UseError a, ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (UseError -> Either UseError a
forall a b. a -> Either a b
Left (SessionError -> UseError
Errors.SessionUseError SessionError
err), ConnectionState
connectionState)
catchError :: forall a. Session a -> (SessionError -> Session a) -> Session a
catchError (Session ConnectionState -> IO (Either UseError a, ConnectionState)
m) SessionError -> Session a
handler = (ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
forall a.
(ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
Session \ConnectionState
connectionState -> do
(Either UseError a
result, ConnectionState
connectionState') <- ConnectionState -> IO (Either UseError a, ConnectionState)
m ConnectionState
connectionState
case Either UseError a
result of
Left (Errors.SessionUseError SessionError
err) -> Session a
-> ConnectionState -> IO (Either UseError a, ConnectionState)
forall a.
Session a
-> ConnectionState -> IO (Either UseError a, ConnectionState)
run (SessionError -> Session a
handler SessionError
err) ConnectionState
connectionState'
Either UseError a
other -> (Either UseError a, ConnectionState)
-> IO (Either UseError a, ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either UseError a
other, ConnectionState
connectionState')
script :: ByteString -> Session ()
script :: ByteString -> Session ()
script ByteString
sql =
(ConnectionState -> IO (Either UseError (), ConnectionState))
-> Session ()
forall a.
(ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
Session \ConnectionState
connectionState -> do
let connection :: Connection
connection = ConnectionState -> Connection
ConnectionState.connection ConnectionState
connectionState
Either (Error (Maybe ByteString)) ()
result <- Roundtrip (Maybe ByteString) ()
-> Connection -> IO (Either (Error (Maybe ByteString)) ())
forall tag a.
Roundtrip tag a -> Connection -> IO (Either (Error tag) a)
Comms.Roundtrip.toSerialIO (Maybe ByteString -> ByteString -> Roundtrip (Maybe ByteString) ()
forall tag. tag -> ByteString -> Roundtrip tag ()
Comms.Roundtrip.script (ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
sql) ByteString
sql) Connection
connection
case Either (Error (Maybe ByteString)) ()
result of
Left Error (Maybe ByteString)
err -> case Error (Maybe ByteString)
err of
Comms.Roundtrip.ClientError Maybe ByteString
_ Text
details -> do
(Either UseError (), ConnectionState)
-> IO (Either UseError (), ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
( UseError -> Either UseError ()
forall a b. a -> Either a b
Left (Text -> UseError
Errors.fromSendError Text
details),
ConnectionState
connectionState
)
Comms.Roundtrip.ServerError Error (Maybe ByteString)
recvError ->
(Either UseError (), ConnectionState)
-> IO (Either UseError (), ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
( UseError -> Either UseError ()
forall a b. a -> Either a b
Left (ByteString -> Error (Maybe ByteString) -> UseError
Errors.fromRecvErrorInScript ByteString
sql Error (Maybe ByteString)
recvError),
ConnectionState
connectionState
)
Right () ->
(Either UseError (), ConnectionState)
-> IO (Either UseError (), ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
( () -> Either UseError ()
forall a b. b -> Either a b
Right (),
ConnectionState
connectionState
)
statement ::
Statement.Statement params result ->
params ->
Session result
statement :: forall params result.
Statement params result -> params -> Session result
statement Statement params result
stmt params
params =
(ConnectionState -> IO (Either UseError result, ConnectionState))
-> Session result
forall a.
(ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
Session \ConnectionState
connectionState -> do
let usePreparedStatements :: Bool
usePreparedStatements = ConnectionState -> Bool
ConnectionState.preparedStatements ConnectionState
connectionState
statementCache :: StatementCache
statementCache = ConnectionState -> StatementCache
ConnectionState.statementCache ConnectionState
connectionState
oidCache :: OidCache
oidCache = ConnectionState -> OidCache
ConnectionState.oidCache ConnectionState
connectionState
connection :: Connection
connection = ConnectionState -> Connection
ConnectionState.connection ConnectionState
connectionState
sql :: ByteString
sql = Statement params result -> ByteString
forall params result. Statement params result -> ByteString
Statement.sql Statement params result
stmt
missingTypes :: HashSet QualifiedTypeName
missingTypes = HashSet QualifiedTypeName -> OidCache -> HashSet QualifiedTypeName
OidCache.selectUnknownNames (Statement params result -> HashSet QualifiedTypeName
forall params result.
Statement params result -> HashSet QualifiedTypeName
Statement.unknownTypes Statement params result
stmt) OidCache
oidCache
Either UseError OidCache
resolvedOidCache <-
if HashSet QualifiedTypeName -> Bool
forall a. HashSet a -> Bool
HashSet.null HashSet QualifiedTypeName
missingTypes
then Either UseError OidCache -> IO (Either UseError OidCache)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (OidCache -> Either UseError OidCache
forall a b. b -> Either a b
Right OidCache
oidCache)
else do
Either UseError SelectTypeInfoResult
oidCacheUpdates <-
Connection
-> SelectTypeInfo -> IO (Either UseError SelectTypeInfoResult)
PqProcedures.SelectTypeInfo.run Connection
connection (HashSet QualifiedTypeName -> SelectTypeInfo
PqProcedures.SelectTypeInfo.SelectTypeInfo HashSet QualifiedTypeName
missingTypes)
pure $ case Either UseError SelectTypeInfoResult
oidCacheUpdates of
Left UseError
err -> UseError -> Either UseError OidCache
forall a b. a -> Either a b
Left UseError
err
Right SelectTypeInfoResult
oidCacheUpdates ->
let foundTypes :: HashSet QualifiedTypeName
foundTypes = SelectTypeInfoResult -> HashSet QualifiedTypeName
forall k a. HashMap k a -> HashSet k
HashMap.keysSet SelectTypeInfoResult
oidCacheUpdates
notFoundTypes :: HashSet QualifiedTypeName
notFoundTypes = HashSet QualifiedTypeName
-> HashSet QualifiedTypeName -> HashSet QualifiedTypeName
forall a. Hashable a => HashSet a -> HashSet a -> HashSet a
HashSet.difference HashSet QualifiedTypeName
missingTypes HashSet QualifiedTypeName
foundTypes
in if Bool -> Bool
not (HashSet QualifiedTypeName -> Bool
forall a. HashSet a -> Bool
HashSet.null HashSet QualifiedTypeName
notFoundTypes)
then UseError -> Either UseError OidCache
forall a b. a -> Either a b
Left (SessionError -> UseError
Errors.SessionUseError (HashSet (Maybe Text, Text) -> SessionError
Errors.MissingTypesSessionError ((QualifiedTypeName -> (Maybe Text, Text))
-> HashSet QualifiedTypeName -> HashSet (Maybe Text, Text)
forall b a. Hashable b => (a -> b) -> HashSet a -> HashSet b
HashSet.map QualifiedTypeName -> (Maybe Text, Text)
CodecsVocab.QualifiedTypeName.toNameTuple HashSet QualifiedTypeName
notFoundTypes)))
else OidCache -> Either UseError OidCache
forall a b. b -> Either a b
Right (OidCache
oidCache OidCache -> OidCache -> OidCache
forall a. Semigroup a => a -> a -> a
<> SelectTypeInfoResult -> OidCache
OidCache.fromHashMap SelectTypeInfoResult
oidCacheUpdates)
case Either UseError OidCache
resolvedOidCache of
Left UseError
err -> (Either UseError result, ConnectionState)
-> IO (Either UseError result, ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (UseError -> Either UseError result
forall a b. a -> Either a b
Left UseError
err, ConnectionState
connectionState)
Right OidCache
newOidCache -> do
let resolve :: QualifiedTypeName -> TypeInfo
resolve = OidCache -> QualifiedTypeName -> TypeInfo
OidCache.toResolver OidCache
newOidCache
decoder' :: ResultDecoder result
decoder' = Statement params result
-> (QualifiedTypeName -> TypeInfo) -> ResultDecoder result
forall params result.
Statement params result
-> (QualifiedTypeName -> TypeInfo) -> ResultDecoder result
Statement.decoder Statement params result
stmt QualifiedTypeName -> TypeInfo
resolve
prepared :: Bool
prepared = Bool
usePreparedStatements Bool -> Bool -> Bool
&& Statement params result -> Bool
forall params result. Statement params result -> Bool
Statement.isPrepared Statement params result
stmt
tag :: Maybe (Int, Int, ByteString, [Text], Bool)
tag = (Int, Int, ByteString, [Text], Bool)
-> Maybe (Int, Int, ByteString, [Text], Bool)
forall a. a -> Maybe a
Just (Int
1, Int
0, ByteString
sql, Statement params result -> params -> [Text]
forall params result. Statement params result -> params -> [Text]
Statement.printer Statement params result
stmt params
params, Bool
prepared)
mapError :: Error (Maybe (Int, Int, ByteString, [Text], Bool)) -> UseError
mapError = \case
Comms.Roundtrip.ClientError Maybe (Int, Int, ByteString, [Text], Bool)
_ Text
details ->
Text -> UseError
Errors.fromSendError Text
details
Comms.Roundtrip.ServerError Error (Maybe (Int, Int, ByteString, [Text], Bool))
recvError ->
Error (Maybe (Int, Int, ByteString, [Text], Bool)) -> UseError
Errors.fromRecvError Error (Maybe (Int, Int, ByteString, [Text], Bool))
recvError
withState :: (Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result,
StatementCache)
-> (Either UseError result, ConnectionState)
withState (Either (Error (Maybe (Int, Int, ByteString, [Text], Bool))) result
result, StatementCache
newStatementCache) =
( (Error (Maybe (Int, Int, ByteString, [Text], Bool)) -> UseError)
-> Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result
-> Either UseError result
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Error (Maybe (Int, Int, ByteString, [Text], Bool)) -> UseError
mapError Either (Error (Maybe (Int, Int, ByteString, [Text], Bool))) result
result,
ConnectionState
connectionState
{ ConnectionState.oidCache = newOidCache,
ConnectionState.statementCache = newStatementCache
}
)
((Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result,
StatementCache)
-> (Either UseError result, ConnectionState))
-> IO
(Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result,
StatementCache)
-> IO (Either UseError result, ConnectionState)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result,
StatementCache)
-> (Either UseError result, ConnectionState)
withState
(IO
(Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result,
StatementCache)
-> IO (Either UseError result, ConnectionState))
-> IO
(Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result,
StatementCache)
-> IO (Either UseError result, ConnectionState)
forall a b. (a -> b) -> a -> b
$ if Bool
prepared
then do
let ([Word32]
oidList, [Maybe (ByteString, Bool)]
valueAndFormatList) =
Statement params result
-> (QualifiedTypeName -> TypeInfo)
-> params
-> ([Word32], [Maybe (ByteString, Bool)])
forall params result.
Statement params result
-> (QualifiedTypeName -> TypeInfo)
-> params
-> ([Word32], [Maybe (ByteString, Bool)])
Statement.compilePreparedStatementData Statement params result
stmt QualifiedTypeName -> TypeInfo
resolve params
params
encodedParams :: [Maybe (ByteString, Format)]
encodedParams =
[Maybe (ByteString, Bool)]
valueAndFormatList
[Maybe (ByteString, Bool)]
-> ([Maybe (ByteString, Bool)] -> [Maybe (ByteString, Format)])
-> [Maybe (ByteString, Format)]
forall a b. a -> (a -> b) -> b
& (Maybe (ByteString, Bool) -> Maybe (ByteString, Format))
-> [Maybe (ByteString, Bool)] -> [Maybe (ByteString, Format)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((ByteString, Bool) -> (ByteString, Format))
-> Maybe (ByteString, Bool) -> Maybe (ByteString, Format)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(ByteString
bytes, Bool
format) -> (ByteString
bytes, Format -> Format -> Bool -> Format
forall a. a -> a -> Bool -> a
bool Format
Pq.Binary Format
Pq.Text Bool
format)))
execute :: ByteString
-> IO
(Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result)
execute ByteString
remoteKey =
Roundtrip (Maybe (Int, Int, ByteString, [Text], Bool)) result
-> Connection
-> IO
(Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result)
forall tag a.
Roundtrip tag a -> Connection -> IO (Either (Error tag) a)
Comms.Roundtrip.toSerialIO
(Maybe (Int, Int, ByteString, [Text], Bool)
-> ByteString
-> [Maybe (ByteString, Format)]
-> Format
-> ResultDecoder result
-> Roundtrip (Maybe (Int, Int, ByteString, [Text], Bool)) result
forall tag a.
tag
-> ByteString
-> [Maybe (ByteString, Format)]
-> Format
-> ResultDecoder a
-> Roundtrip tag a
Comms.Roundtrip.queryPrepared Maybe (Int, Int, ByteString, [Text], Bool)
tag ByteString
remoteKey [Maybe (ByteString, Format)]
encodedParams Format
Pq.Binary ResultDecoder result
decoder')
Connection
connection
case ByteString -> [Word32] -> StatementCache -> Maybe ByteString
StatementCache.lookup ByteString
sql [Word32]
oidList StatementCache
statementCache of
Just ByteString
remoteKey -> do
Either (Error (Maybe (Int, Int, ByteString, [Text], Bool))) result
result <- ByteString
-> IO
(Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result)
execute ByteString
remoteKey
pure (Either (Error (Maybe (Int, Int, ByteString, [Text], Bool))) result
result, StatementCache
statementCache)
Maybe ByteString
Nothing -> do
let (ByteString
remoteKey, StatementCache
newStatementCache) = ByteString
-> [Word32] -> StatementCache -> (ByteString, StatementCache)
StatementCache.insert ByteString
sql [Word32]
oidList StatementCache
statementCache
Either (Error (Maybe (Int, Int, ByteString, [Text], Bool))) ()
prepareResult <-
Roundtrip (Maybe (Int, Int, ByteString, [Text], Bool)) ()
-> Connection
-> IO
(Either (Error (Maybe (Int, Int, ByteString, [Text], Bool))) ())
forall tag a.
Roundtrip tag a -> Connection -> IO (Either (Error tag) a)
Comms.Roundtrip.toSerialIO
(Maybe (Int, Int, ByteString, [Text], Bool)
-> ByteString
-> ByteString
-> [Word32]
-> Roundtrip (Maybe (Int, Int, ByteString, [Text], Bool)) ()
forall tag.
tag -> ByteString -> ByteString -> [Word32] -> Roundtrip tag ()
Comms.Roundtrip.prepare Maybe (Int, Int, ByteString, [Text], Bool)
tag ByteString
remoteKey ByteString
sql [Word32]
oidList)
Connection
connection
case Either (Error (Maybe (Int, Int, ByteString, [Text], Bool))) ()
prepareResult of
Left Error (Maybe (Int, Int, ByteString, [Text], Bool))
err ->
(Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result,
StatementCache)
-> IO
(Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result,
StatementCache)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
( Error (Maybe (Int, Int, ByteString, [Text], Bool))
-> Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result
forall a b. a -> Either a b
Left Error (Maybe (Int, Int, ByteString, [Text], Bool))
err,
if Error (Maybe (Int, Int, ByteString, [Text], Bool)) -> Bool
forall tag. Error tag -> Bool
Errors.isPrepareCollision Error (Maybe (Int, Int, ByteString, [Text], Bool))
err
then StatementCache
newStatementCache
else StatementCache
statementCache
)
Right () -> do
Either (Error (Maybe (Int, Int, ByteString, [Text], Bool))) result
result <- ByteString
-> IO
(Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result)
execute ByteString
remoteKey
pure (Either (Error (Maybe (Int, Int, ByteString, [Text], Bool))) result
result, StatementCache
newStatementCache)
else do
let encodedParams :: [Maybe (Word32, ByteString, Format)]
encodedParams =
Statement params result
-> (QualifiedTypeName -> TypeInfo)
-> params
-> [Maybe (Word32, ByteString, Bool)]
forall params result.
Statement params result
-> (QualifiedTypeName -> TypeInfo)
-> params
-> [Maybe (Word32, ByteString, Bool)]
Statement.compileUnpreparedStatementData Statement params result
stmt QualifiedTypeName -> TypeInfo
resolve params
params
[Maybe (Word32, ByteString, Bool)]
-> ([Maybe (Word32, ByteString, Bool)]
-> [Maybe (Word32, ByteString, Format)])
-> [Maybe (Word32, ByteString, Format)]
forall a b. a -> (a -> b) -> b
& (Maybe (Word32, ByteString, Bool)
-> Maybe (Word32, ByteString, Format))
-> [Maybe (Word32, ByteString, Bool)]
-> [Maybe (Word32, ByteString, Format)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Word32, ByteString, Bool) -> (Word32, ByteString, Format))
-> Maybe (Word32, ByteString, Bool)
-> Maybe (Word32, ByteString, Format)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Word32
oid, ByteString
bytes, Bool
format) -> (Word32
oid, ByteString
bytes, Format -> Format -> Bool -> Format
forall a. a -> a -> Bool -> a
bool Format
Pq.Binary Format
Pq.Text Bool
format)))
Either (Error (Maybe (Int, Int, ByteString, [Text], Bool))) result
result <-
Roundtrip (Maybe (Int, Int, ByteString, [Text], Bool)) result
-> Connection
-> IO
(Either
(Error (Maybe (Int, Int, ByteString, [Text], Bool))) result)
forall tag a.
Roundtrip tag a -> Connection -> IO (Either (Error tag) a)
Comms.Roundtrip.toSerialIO
(Maybe (Int, Int, ByteString, [Text], Bool)
-> ByteString
-> [Maybe (Word32, ByteString, Format)]
-> Format
-> ResultDecoder result
-> Roundtrip (Maybe (Int, Int, ByteString, [Text], Bool)) result
forall tag a.
tag
-> ByteString
-> [Maybe (Word32, ByteString, Format)]
-> Format
-> ResultDecoder a
-> Roundtrip tag a
Comms.Roundtrip.queryParams Maybe (Int, Int, ByteString, [Text], Bool)
tag ByteString
sql [Maybe (Word32, ByteString, Format)]
encodedParams Format
Pq.Binary ResultDecoder result
decoder')
Connection
connection
pure (Either (Error (Maybe (Int, Int, ByteString, [Text], Bool))) result
result, StatementCache
statementCache)
pipeline :: Pipeline.Pipeline result -> Session result
pipeline :: forall result. Pipeline result -> Session result
pipeline Pipeline result
pipeline = (ConnectionState -> IO (Either UseError result, ConnectionState))
-> Session result
forall a.
(ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
Session (Pipeline result
-> ConnectionState -> IO (Either UseError result, ConnectionState)
forall a.
Pipeline a
-> ConnectionState -> IO (Either UseError a, ConnectionState)
Pipeline.run Pipeline result
pipeline)
onLibpqConnection ::
(Pq.Connection -> IO (Either Errors.UseError a, Pq.Connection)) ->
Session a
onLibpqConnection :: forall a.
(Connection -> IO (Either UseError a, Connection)) -> Session a
onLibpqConnection Connection -> IO (Either UseError a, Connection)
f = (ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
forall a.
(ConnectionState -> IO (Either UseError a, ConnectionState))
-> Session a
Session \ConnectionState
connectionState -> do
let pqConnection :: Connection
pqConnection = ConnectionState -> Connection
ConnectionState.connection ConnectionState
connectionState
(Either UseError a
result, Connection
newConnection) <- Connection -> IO (Either UseError a, Connection)
f Connection
pqConnection
let newState :: ConnectionState
newState = Connection -> ConnectionState -> ConnectionState
ConnectionState.setConnection Connection
newConnection ConnectionState
connectionState
(Either UseError a, ConnectionState)
-> IO (Either UseError a, ConnectionState)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either UseError a
result, ConnectionState
newState)