MyNixOS website logo
Description

Implementation of named parameters for postgresql-simple library.

Implementation of named parameters for postgresql-simple library.

Here is an exaple of how it could be used in your code:

queryNamed dbConnection [sql|
    SELECT *
    FROM table
    WHERE foo = ?foo
      AND bar = ?bar
      AND baz = ?foo
|] [ "foo" =? "fooBar"
   , "bar" =? "barVar"
   ]

postgresql-simple-named

Logo

Build status Hackage Stackage Lts Stackage Nightly MPL-2.0 license

This library introduces the implementation of named parameters for the postgresql-simple library. postgresql-simple-named is designed to be used along with the postgresql-simple library, so you could refer there for the original documentation of primary functions. This package solves exclusively one particular problem — gives the ability to use named parameters instead of ? in quasi-quoter queries and offers essential functions for substituting variables in queries (queryNamed, executeNamed).

Example

Operator =? binds named parameters with the corresponding values. Named parameters inside SQL query start with the '?' character and can contain lowercase and uppercase letters, digits and underscore. Below you can find a basic example of how query with named parameters could look like:

queryNamed dbConnection [sql|
    SELECT
        id, name, city
    FROM users
    WHERE name = ?nameParam
      AND age  = ?ageParam

|] [ "nameParam" =? "John"
   , "ageParam"  =? 42
   ]

This feature can be extremely helpful when the query uses some parameters more than once:

query dbConnection [sql|
    SELECT
        col1, col2
    FROM my_table
    WHERE id = ?
      AND (? IS NULL OR id > ? )
      AND (? IS NULL OR id < ? )

|] (someId, minId, minId, maxId, maxId)

This is how the query looks like with the postgresql-simple library. You can rewrite it the following way using the postgresql-simple-named library:

queryNamed dbConnection [sql|
    SELECT
        col1, col2
    FROM my_table
    WHERE id = ?someId
      AND (?minId IS NULL OR id > ?minId )
      AND (?maxId IS NULL OR id < ?maxId )

|] [ "someId" =? 42
   , "minId"  =? 1
   , "maxId"  =? 100
   ]

How to build

Build the library with either cabal new-build or stack build.

How to test locally

  • Run DB in a Docker in a separate terminal window using command:
    docker run -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres:15
    
  • Run tests using cabal new-test or stack test.
Metadata

Version

0.0.5.0

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • 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-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-windows