MyNixOS website logo
Description

Hasql extension for dynamic construction of statements.

Library extending Hasql with a composable API for building SQL statements dynamically.

The core abstraction is Snippet, which allows you to construct SQL statements with parameters injected in-place, automatically handling placeholders and encoder matching. This is particularly useful when the structure of your SQL depends on runtime parameters.

Key features:

  • Composable SQL snippets using Semigroup and Monoid instances

  • Automatic placeholder generation and parameter encoding

  • Type-safe parameter injection with implicit encoder derivation

  • Protection against common SQL construction bugs

  • Support for conditional SQL structure based on parameters

Example:

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 <>
  ")"

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.
Metadata

Version

0.5.0.1

License

Platforms (78)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows