The postgresql-query package

[Tags:bsd3, library, test]

[Skip to Readme]


Versions 1.0.0, 1.0.1, 1.1.0, 1.1.1, 1.2.0, 1.2.1, 1.3.0, 1.3.1, 1.4.0, 2.0.0, 2.1.0, 2.2.0, 2.3.0, 3.0.0, 3.0.1, 3.1.0
Change log
Dependencies aeson, attoparsec, base (>=4.6 && <5), blaze-builder, bytestring, containers, data-default, either, exceptions, file-embed, haskell-src-meta, hreader (>=1.0.0 && <2.0.0), hset (>=2.0.0 && <3.0.0), inflections (>=0.2 && <0.4), monad-control (== || >, monad-logger, mtl, postgresql-simple (>=, resource-pool, semigroups, template-haskell, text, th-lift, th-lift-instances, time, transformers, transformers-base, transformers-compat (>=0.3), type-fun (>=0.1.0) [details]
License BSD3
Author Aleksey Uimanov
Category Database
Home page
Source repository head: git clone
Uploaded Tue Mar 14 14:22:55 UTC 2017 by AlekseyUymanov
Distributions NixOS:3.1.0
Downloads 1415 total (13 in the last 30 days)
1 []
Status Docs available [build log]
Last success reported on 2017-03-14 [all 1 reports]




Maintainer's Corner

For package maintainers and hackage trustees

Readme for postgresql-query

Readme for postgresql-query-3.1.0

What ?

postgresql-query is a library for more simple query generation for PostgreSQL database. It is not an ORM (but contains some part of). It contains interpolating quasiquote for simple query generation.


When you want to perform some complex SQL query using postgresql-simple you writing this query by hands like that:

let q = mconcat
        [ "SELECT,, count( "
        , "FROM users as u INNER JOIN posts as p "
        , "ON = p.user_id "
        , "WHERE like ? AND u.created BETWEEN ? AND ? "
        , "GROUP BY ORDER BY ? " ]

Well, this is not realy complex. Now what you need to perform the query is to paste parameters instead of this ? signs:

query con q ("%Peyton%", now, yesterday)

Did you see the mistake? We forgot about ordering field. To perform this query we must

query con q ("%Peyton%", now, yesterday, Identifier "...

Oups!. If we use Identifier "" we will get "" in our query which is just not right. Sql syntax assumes "u"."name" for this query. We can not use query parameter to paste optional field here.

Next example is more complex:

let name = Just "%Peyton%" -- assume we getting parameters from query
    minage = Just 10
    maxage = Just 50
    condlist = catMaybes
               [ const " like ? " <$> name
               , const " u.age > ? " <$> minage
               , const " u.age < ? " <$> maxage ]
    paramlist = catMaybes
                [ toField <$> name
                , toField <$> minage
                , toField <$> maxage ]
    cond = if L.null condlist
           then mempty
           else "WHERE " <> (mconcat $ L.intersperse " AND " condlist)
    q = "SELECT,, u.age FROM users AS u " <> cond
query con q paramlist

So much to write and so many chances to make a mistake. What if we could write this like:

let name = Just "%Peyton%" -- assume we getting parameters from query
    minage = Just 10
    maxage = Just 50
    ord = "" :: FN -- <- special type for field names!
    condlist = catMaybes
               [ (\a -> [sqlExp| like #{a}|]) <$> name
               , (\a -> [sqlExp|u.age > #{a}|])     <$> minage
               , (\a -> [sqlExp|u.age < #{a}|])     <$> maxage ]
    cond = if L.null condlist
           then mempty
           else [sqlExp|WHERE ^{mconcat $ L.intersperse " AND " condlist}|]
pqQuery [sqlExp|SELECT,, u.age
                FROM users AS u ^{cond}
                ORDER BY ^{ord}|]

Much better!


Quasiquote sqlExp has two way of interpolation:

  • #{exp} - pastes inside query arbitrary value which type is instance of ToField typeclass. It performs correct strings escaping so it is not the same as stupid string interpolation. Dont worry about sql-injections when using it.

  • ^{exp} - pastes inside query arbitrary value which type has instance of ToSqlBuilder typeclass.

sqlExp returns SqlBuilder which has a Monoid instance and made for effective concatination (bytestring builder works inside).

This quasiquote correctly handles string literals and quoted identifiers. It also removes line and block (even nested) sql comments from resulting query as well as sequences of space characters. You are free to write queries like

WHERE name SIMILAR TO '\^{2,3}' -- line comment #{ololo}

or even

WHERE "#{strange}identifier" SIMILAR TO '#{1,10}' /*nested/*^{block}*/comment*/

sqlExp will remove all comments and will not interpolate inside string literals or quoted identifiers at all.

sqlExpEmbed and sqlExpFile

If you have realy huge hardcore sql template you can

pgQuery $(sqlExpEmbed "sql/foo/bar.sql")

It works just like Yesod's templates. You can use interpolation inside templates like inside sqlExp.

pgQuery $(sqlExpFile "foo/bar")

Is absolutely the same as above. It just prepends sql/ and appends .sql to your string. If you agree to follow naming conventions you are welcome to use sqlExpFile.