module Hasql.Comms.Send ( Send, -- * Execution toHandler, -- * Constructors prepare, query, queryPrepared, queryParams, pipelineSync, enterPipelineMode, exitPipelineMode, )whereimport Hasql.Platform.Preludeimport Pqi qualified as Pq-- | An action that sends commands to a connection without receiving the results.---- The @err@ type parameter is a value attached to the commands,-- which the error carries if sending fails.newtype Send err = Send (Pq.Connection -> IO (Either err ()))instance Functor Send where {-# INLINE fmap #-} fmap f (Send send) = Send \connection -> first f <$> send connectioninstance Semigroup (Send err) where {-# INLINE (<>) #-} Send send1 <> Send send2 = Send \cs -> do result <- send1 cs case result of Left err -> pure (Left err) Right () -> send2 csinstance Monoid (Send err) where {-# INLINE mempty #-} mempty = Send \_ -> pure (Right ())toHandler :: Send err -> Pq.Connection -> IO (Either err ())toHandler (Send send) = sendliftPqSend :: err -> (Pq.Connection -> IO Bool) -> Send errliftPqSend err pqSend = Send \connection -> do success <- pqSend connection pure (if success then Right () else Left err)prepare :: err -> ByteString -> ByteString -> Maybe [Word32] -> Send errprepare err statementName sql oidList = liftPqSend err \connection -> Pq.sendPrepare connection statementName sql oidListquery :: err -> ByteString -> Send errquery err sql = liftPqSend err \connection -> Pq.sendQuery connection sqlqueryPrepared :: err -> ByteString -> [Maybe (ByteString, Pq.Format)] -> Pq.Format -> Send errqueryPrepared err statementName params resultFormat = liftPqSend err \connection -> Pq.sendQueryPrepared connection statementName params resultFormatqueryParams :: err -> ByteString -> [Maybe (Word32, ByteString, Pq.Format)] -> Pq.Format -> Send errqueryParams err sql params resultFormat = liftPqSend err \connection -> Pq.sendQueryParams connection sql params resultFormatpipelineSync :: err -> Send errpipelineSync err = liftPqSend err \connection -> Pq.pipelineSync connectionenterPipelineMode :: err -> Send errenterPipelineMode err = liftPqSend err \connection -> Pq.enterPipelineMode connectionexitPipelineMode :: err -> Send errexitPipelineMode err = liftPqSend err \connection -> Pq.exitPipelineMode connection