hasql-dynamic-statements 0.2.0.3 → 0.3
raw patch · 4 files changed
+15/−12 lines, 4 filesdep ~bytestringPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: bytestring
API changes (from Hackage documentation)
- Hasql.DynamicStatements.Session: dynamicallyParameterizedStatement :: Snippet -> Result result -> Session result
+ Hasql.DynamicStatements.Session: dynamicallyParameterizedStatement :: Snippet -> Result result -> Bool -> Session result
- Hasql.DynamicStatements.Statement: dynamicallyParameterized :: Snippet -> Result result -> Statement () result
+ Hasql.DynamicStatements.Statement: dynamicallyParameterized :: Snippet -> Result result -> Bool -> Statement () result
Files
- hasql-dynamic-statements.cabal +2/−2
- library/Hasql/DynamicStatements/Session.hs +3/−2
- library/Hasql/DynamicStatements/Statement.hs +8/−6
- test/Main.hs +2/−2
hasql-dynamic-statements.cabal view
@@ -1,5 +1,5 @@ name: hasql-dynamic-statements-version: 0.2.0.3+version: 0.3 synopsis: Toolkit for constructing Hasql statements dynamically description: This library introduces into the Hasql ecosystem a new abstraction named Snippet,@@ -34,7 +34,7 @@ Hasql.DynamicStatements.Snippet.Defs build-depends: base >=4.12 && <5,- bytestring >=0.10 && <0.11,+ bytestring >=0.10 && <0.12, containers >=0.6 && <0.7, hasql >=1.4 && <1.5, hasql-implicits >=0.1 && <0.2,
library/Hasql/DynamicStatements/Session.hs view
@@ -15,5 +15,6 @@ in @Session@. For details see the docs on that function. -}-dynamicallyParameterizedStatement :: SnippetDefs.Snippet -> Decoders.Result result -> Session result-dynamicallyParameterizedStatement snippet decoder = Session.statement () (Statement.dynamicallyParameterized snippet decoder)+dynamicallyParameterizedStatement :: SnippetDefs.Snippet -> Decoders.Result result -> Bool -> Session result+dynamicallyParameterizedStatement snippet decoder prepared =+ Session.statement () (Statement.dynamicallyParameterized snippet decoder prepared)
library/Hasql/DynamicStatements/Statement.hs view
@@ -8,9 +8,11 @@ import qualified Ptr.Poking as Poking import qualified Ptr.ByteString as ByteString + {-| Construct a statement dynamically, specifying the parameters in-place-in the declaration of snippet and providing a result decoder.+in the declaration of snippet and providing a result decoder and+specifying whether the statement should be prepared. The injection of the parameters is handled automatically, generating parametric statements with binary encoders under the hood.@@ -27,7 +29,7 @@ foldMap (mappend " for " . Snippet.'SnippetDefs.param') to <> ")" decoder = Decoders.'Decoders.singleRow' (Decoders.'Decoders.column' (Decoders.'Decoders.nonNullable' Decoders.'Decoders.text'))- in 'dynamicallyParameterized' snippet decoder+ in 'dynamicallyParameterized' snippet decoder True @ Without the Snippet API you would have had to implement the same functionality thus:@@ -45,14 +47,14 @@ foldMap (\\ x -> Encoders.'Encoders.param' (x '>$' Encoders.'Encoders.int8')) from <> foldMap (\\ x -> Encoders.'Encoders.param' (x '>$' Encoders.'Encoders.int8')) to decoder = Decoders.'Decoders.singleRow' (Decoders.'Decoders.column' Decoders.'Decoders.text')- in Statement sql encoder decoder False+ in Statement sql encoder decoder True @ As you can see, the Snippet API abstracts over placeholders and matching encoder generation, thus also protecting you from all sorts of related bugs. -}-dynamicallyParameterized :: SnippetDefs.Snippet -> Decoders.Result result -> Statement () result-dynamicallyParameterized (SnippetDefs.Snippet chunks) decoder = let+dynamicallyParameterized :: SnippetDefs.Snippet -> Decoders.Result result -> Bool -> Statement () result+dynamicallyParameterized (SnippetDefs.Snippet chunks) decoder prepared = let step (!paramId, !poking, !encoder) = \ case SnippetDefs.StringSnippetChunk sql -> (paramId, poking <> Poking.bytes sql, encoder) SnippetDefs.ParamSnippetChunk paramEncoder -> let@@ -61,4 +63,4 @@ newEncoder = encoder <> paramEncoder in (newParamId, newPoking, newEncoder) in case foldl' step (1, mempty, mempty) chunks of- (_, poking, encoder) -> Statement (ByteString.poking poking) encoder decoder False+ (_, poking, encoder) -> Statement (ByteString.poking poking) encoder decoder prepared
test/Main.hs view
@@ -33,7 +33,7 @@ foldMap (mappend " for " . Snippet.param @Int32) to <> ")" decoder = Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.text))- in runSession (Session.dynamicallyParameterizedStatement snippet decoder)+ in runSession (Session.dynamicallyParameterizedStatement snippet decoder True) in do assertEqual "" (Right (Right "bc")) =<< sample "abcd" (Just 2) (Just 2) assertEqual "" (Right (Right "bcd")) =<< sample "abcd" (Just 2) (Just 3)@@ -45,7 +45,7 @@ snippet = "SELECT 1 " <> (foldMap @[] ("," <>) $ replicate 1001 $ Snippet.param (10::Int64)) statement =- Statement.dynamicallyParameterized snippet Decoders.noResult+ Statement.dynamicallyParameterized snippet Decoders.noResult True sql = case statement of Statement.Statement x _ _ _ -> x