Stub function

This commit is contained in:
Nikita Volkov 2024-01-27 01:11:03 +03:00
parent c6e35b64e4
commit 752f4feb81
4 changed files with 62 additions and 0 deletions

View File

@ -106,6 +106,8 @@ library
Hasql.PTI Hasql.PTI
Hasql.Session.Core Hasql.Session.Core
Hasql.Settings Hasql.Settings
Hasql.Statement.Function
Hasql.Statement.Function.SqlBuilder
build-depends: build-depends:
, aeson >=2 && <3 , aeson >=2 && <3

View File

@ -1,5 +1,6 @@
module Hasql.Statement module Hasql.Statement
( Statement (..), ( Statement (..),
function,
refineResult, refineResult,
-- * Recipies -- * Recipies
@ -16,6 +17,7 @@ import qualified Hasql.Decoders as Decoders
import qualified Hasql.Decoders.All as Decoders import qualified Hasql.Decoders.All as Decoders
import qualified Hasql.Encoders as Encoders import qualified Hasql.Encoders as Encoders
import Hasql.Prelude import Hasql.Prelude
import qualified Hasql.Statement.Function as Function
-- | -- |
-- Specification of a strictly single-statement query, which can be parameterized and prepared. -- Specification of a strictly single-statement query, which can be parameterized and prepared.
@ -117,3 +119,24 @@ refineResult refiner (Statement template encoder decoder preparable) =
-- --
-- For details see -- For details see
-- <https://www.postgresql.org/docs/9.6/static/functions-comparisons.html#AEN20944 the Postgresql docs>. -- <https://www.postgresql.org/docs/9.6/static/functions-comparisons.html#AEN20944 the Postgresql docs>.
-- |
-- Utility for execution of stored procedures and functions, which automates the SQL generation.
--
-- Produces SQL like the following:
--
-- > SELECT function_name($1, $2, ..)
--
-- with the amount of parameter placeholders derived from the encoder.
function ::
-- | Function name.
--
-- Will get automatically escaped for injection-safety.
Text ->
Encoders.Params a ->
Decoders.Result b ->
-- | Whether the statement should be prepared.
Bool ->
Statement a b
function functionName encoders =
Statement (Function.sql functionName encoders) encoders

View File

@ -0,0 +1,12 @@
module Hasql.Statement.Function where
import qualified ByteString.StrictBuilder as Builder
import qualified Hasql.Encoders.All as Encoders
import Hasql.Prelude
import qualified Hasql.Statement.Function.SqlBuilder as SqlBuilder
sql :: Text -> Encoders.Params a -> ByteString
sql name encoders =
Builder.builderBytes
$ SqlBuilder.sql name
$ error "TODO: Get size" encoders

View File

@ -0,0 +1,25 @@
module Hasql.Statement.Function.SqlBuilder where
import ByteString.StrictBuilder
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Hasql.Prelude
sql :: Text -> Int -> Builder
sql name size =
"select " <> functionName name <> "(" <> arguments size <> ")"
functionName :: Text -> Builder
functionName =
bytes . Text.encodeUtf8 . Text.replace "\"" ""
arguments :: Int -> Builder
arguments size =
[1 .. size]
& fmap argument
& intersperse ", "
& mconcat
argument :: Int -> Builder
argument num =
"$" <> asciiIntegral num