-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
98 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Hasql.Pipeline | ||
( Pipeline, | ||
statement, | ||
) | ||
where | ||
|
||
import Hasql.Pipeline.Core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
module Hasql.Pipeline.Core where | ||
|
||
import Database.PostgreSQL.LibPQ qualified as Pq | ||
import Hasql.Connection.Core qualified as Connection | ||
import Hasql.Decoders.All qualified as Decoders | ||
import Hasql.Encoders.All qualified as Encoders | ||
import Hasql.Encoders.Params qualified as Encoders.Params | ||
import Hasql.Errors | ||
import Hasql.IO qualified as IO | ||
import Hasql.Prelude | ||
import Hasql.PreparedStatementRegistry qualified as PreparedStatementRegistry | ||
import Hasql.Statement qualified as Statement | ||
|
||
run :: Pipeline a -> Connection.Connection -> IO (Either QueryError a) | ||
run (Pipeline send recv) (Connection.Connection pqConnectionRef integerDatetimes registry) = | ||
withMVar pqConnectionRef \pqConnection -> do | ||
Pq.enterPipelineMode pqConnection | ||
sendResult <- send pqConnection integerDatetimes registry | ||
Pq.pipelineSync pqConnection | ||
recvResult <- recv pqConnection integerDatetimes | ||
Pq.exitPipelineMode pqConnection | ||
pure (sendResult *> recvResult) | ||
|
||
data Pipeline a | ||
= Pipeline | ||
-- | Send commands. | ||
(Pq.Connection -> Bool -> PreparedStatementRegistry.PreparedStatementRegistry -> IO (Either QueryError ())) | ||
-- | Receive results. | ||
(Pq.Connection -> Bool -> IO (Either QueryError a)) | ||
deriving (Functor) | ||
|
||
instance Applicative Pipeline where | ||
pure a = | ||
Pipeline send recv | ||
where | ||
send _ _ _ = | ||
pure (Right ()) | ||
recv _ _ = | ||
pure (Right a) | ||
|
||
Pipeline lSend lRecv <*> Pipeline rSend rRecv = | ||
Pipeline send recv | ||
where | ||
send pqConn idt pReg = do | ||
lSendRes <- lSend pqConn idt pReg | ||
rSendRes <- rSend pqConn idt pReg | ||
pure (lSendRes *> rSendRes) | ||
recv pqConn idt = do | ||
lRecvRes <- lRecv pqConn idt | ||
rRecvRes <- rRecv pqConn idt | ||
pure (lRecvRes <*> rRecvRes) | ||
|
||
statement :: params -> Statement.Statement params result -> Pipeline result | ||
statement params (Statement.Statement template (Encoders.Params paramsEncoder) (Decoders.Result decoder) preparable) = | ||
Pipeline send recv | ||
where | ||
send pqConnection integerDatetimes registry = | ||
mapLeft commandToQueryError | ||
<$> IO.sendParametricStatement pqConnection integerDatetimes registry template paramsEncoder preparable params | ||
|
||
recv pqConnection integerDatetimes = | ||
mapLeft commandToQueryError | ||
<$> IO.getResults pqConnection integerDatetimes decoder | ||
|
||
commandToQueryError = | ||
QueryError template (Encoders.Params.renderReadable paramsEncoder params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters