MIT licensed and maintained by Nikita Volkov
This version can be pinned in stack with:hasql-dynamic-statements-0.5.1@sha256:3df7cef514584c5e4adfa80b3793b71fc08aacd9774d04773b4d1ad465da09cd,3370

Module documentation for 0.5.1

hasql-dynamic-statements

Hackage Continuous Haddock

Hasql extension for composable, dynamic construction of SQL statements.

Overview

This library provides a Snippet API that simplifies building SQL statements where the structure depends on runtime parameters. It abstracts over placeholder management and encoder matching, eliminating boilerplate and reducing bugs.

Key Features

  • Composable: Build statements using Semigroup/Monoid operations
  • Type-safe: Automatic parameter encoding with type-driven encoder derivation
  • Dynamic: Construct SQL conditionally based on parameters
  • Clean API: No manual placeholder numbering or encoder alignment

Quick Example

import Hasql.DynamicStatements.Snippet

-- Build a dynamic substring query
selectSubstring :: Text -> Maybe Int32 -> Maybe Int32 -> Snippet
selectSubstring string from to =
  "select substring(" <> param string <>
  foldMap (\x -> " from " <> param x) from <>
  foldMap (\x -> " for " <> param x) to <>
  ")"

-- Execute in a session
result <- toSession (selectSubstring "hello" (Just 2) Nothing) 
  (singleRow $ column $ nonNullable text)

Without Snippet API

Compare the above to manual construction:

selectSubstring' :: Text -> Maybe Int32 -> Maybe Int32 -> Statement () Text
selectSubstring' string from to = 
  let sql = case (from, to) of
        (Just _, Just _)  -> "select substring($1 from $2 to $3)"
        (Just _, Nothing) -> "select substring($1 from $2)"
        (Nothing, Just _) -> "select substring($1 to $2)"
        (Nothing, Nothing) -> "select substring($1)"
      encoder = 
        Encoders.param (string >$ Encoders.text) <>
        foldMap (\x -> Encoders.param (x >$ Encoders.int8)) from <>
        foldMap (\x -> Encoders.param (x >$ Encoders.int8)) to
      decoder = singleRow $ column $ nonNullable text
  in Statement.preparable sql encoder decoder

The Snippet API eliminates placeholder numbering, pattern matching on parameter presence, and manual encoder sequencing.

Core API

Construction

  • sql - Raw SQL text
  • param - Parameter with implicit encoder
  • encoderAndParam - Parameter with explicit encoder

Execution

  • toSql - Compile to SQL text with placeholders
  • toStatement - Create a Statement
  • toSession - Execute directly in Session
  • toPipeline - Execute in Pipeline

Changes

v0.5

  • (Breaking) Removed Hasql.DynamicStatements.Session module
  • (Breaking) Removed Hasql.DynamicStatements.Statement module
  • Added toStatement, toSession, toPipeline functions to the Hasql.DynamicStatements.Snippet module instead

v0.4

  • Migrated to Hasql 1.10
  • (Breaking) Redefined Snippet.sql to use Text instead of ByteString
  • (Breaking) Dropped the preparability flags assuming that all dynamic statements should not be prepared