module Hasql.DynamicStatements.Statement where
import Hasql.Decoders qualified as Decoders
import Hasql.DynamicStatements.Prelude
import Hasql.DynamicStatements.Snippet.Defs qualified as SnippetDefs
import Hasql.Statement
import Ptr.ByteString qualified as ByteString
import Ptr.Poking qualified as Poking
dynamicallyParameterized :: SnippetDefs.Snippet -> Decoders.Result result -> Bool -> Statement () result
dynamicallyParameterized :: forall result.
Snippet -> Result result -> Bool -> Statement () result
dynamicallyParameterized (SnippetDefs.Snippet Seq SnippetChunk
chunks) Result result
decoder Bool
prepared =
let step :: (a, Poking, Params ()) -> SnippetChunk -> (a, Poking, Params ())
step (!a
paramId, !Poking
poking, !Params ()
encoder) = \case
SnippetDefs.StringSnippetChunk ByteString
sql -> (a
paramId, Poking
poking Poking -> Poking -> Poking
forall a. Semigroup a => a -> a -> a
<> ByteString -> Poking
Poking.bytes ByteString
sql, Params ()
encoder)
SnippetDefs.ParamSnippetChunk Params ()
paramEncoder ->
let newParamId :: a
newParamId = a
paramId a -> a -> a
forall a. Num a => a -> a -> a
+ a
1
newPoking :: Poking
newPoking = Poking
poking Poking -> Poking -> Poking
forall a. Semigroup a => a -> a -> a
<> Word8 -> Poking
Poking.word8 Word8
36 Poking -> Poking -> Poking
forall a. Semigroup a => a -> a -> a
<> a -> Poking
forall a. Integral a => a -> Poking
Poking.asciiIntegral a
paramId
newEncoder :: Params ()
newEncoder = Params ()
encoder Params () -> Params () -> Params ()
forall a. Semigroup a => a -> a -> a
<> Params ()
paramEncoder
in (a
newParamId, Poking
newPoking, Params ()
newEncoder)
in case ((Integer, Poking, Params ())
-> SnippetChunk -> (Integer, Poking, Params ()))
-> (Integer, Poking, Params ())
-> Seq SnippetChunk
-> (Integer, Poking, Params ())
forall b a. (b -> a -> b) -> b -> Seq a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (Integer, Poking, Params ())
-> SnippetChunk -> (Integer, Poking, Params ())
forall {a}.
Integral a =>
(a, Poking, Params ()) -> SnippetChunk -> (a, Poking, Params ())
step (Integer
1, Poking
forall a. Monoid a => a
mempty, Params ()
forall a. Monoid a => a
mempty) Seq SnippetChunk
chunks of
(Integer
_, Poking
poking, Params ()
encoder) -> ByteString
-> Params () -> Result result -> Bool -> Statement () result
forall params result.
ByteString
-> Params params
-> Result result
-> Bool
-> Statement params result
Statement (Poking -> ByteString
ByteString.poking Poking
poking) Params ()
encoder Result result
decoder Bool
prepared