postgresql-simple-named: Implementation of named parameters for `postgresql-simple` library

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

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

[Skip to Readme]

Properties

Versions 0.0.0.0, 0.0.1.0, 0.0.2.0, 0.0.3.0, 0.0.3.0, 0.0.4.0, 0.0.5.0
Change log CHANGELOG.md
Dependencies base (>=4.11 && <4.16), bytestring (>=0.10.8 && <0.11), mtl (>=2.2 && <2.3), postgresql-simple (>=0.5 && <0.7), text (>=1.2 && <1.3) [details]
License MPL-2.0
Copyright 2019 Holmusk
Author Veronika Romashkina, Dmitrii Kovanikov
Maintainer Holmusk <tech@holmusk.com>
Category Database, PostgreSQL
Home page https://github.com/Holmusk/postgresql-simple-named
Bug tracker https://github.com/Holmusk/postgresql-simple-named/issues
Source repo head: git clone https://github.com/Holmusk/postgresql-simple-named.git
Uploaded by HolmuskTechTeam at 2022-06-01T11:49:56Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for postgresql-simple-named-0.0.3.0

[back to package description]

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 considered 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