postgresql-simple-interpolate

Safe interpolated SQL queries in Haskell


Keywords
database, library, Propose Tags, , Index, Quick Jump, Database.PostgreSQL.Simple.SqlQQ.Interpolated, Database.PostgreSQL.Simple.SqlQQ.Interpolated.Parser, postgresql-simple-interpolate-0.1.1.0.tar.gz, browse, Package description, Package maintainers, abrar, 3noch, edit package information , haskell, interpolation, postgresql, postgresql-database, quasiquoter, template-haskell
License
BSD-3-Clause
Install
cabal install postgresql-simple-interpolate-0.1.1.0

Documentation

postgresql-simple-interpolate

Write natural SQL statements in Haskell using a QuasiQuotes!

{-# LANGUAGE QuasiQuotes #-}

import Data.Char (toLower)
import qualified Database.PostgreSQL.Simple as Pg
import Database.PostgreSQL.Simple.SqlQQ.Interpolated (isql)
import Control.Exception (bracket)

main :: IO ()
main = do
  bracket (Pg.connectPostgreSQL "host=localhost") Pg.close $ \conn -> do
    let limit = 10
    ages <- uncurry (query conn) [isql|SELECT age FROM table WHERE name = ${map toLower "CLIVE"} LIMIT ${limit}|]
    print (ages :: [Pg.Only Int])
|]

Hacking

With Nix you can quickly play around with this using a PostgreSQL database:

nix-shell -p '(import ./. {}).haskellPackages.ghcWithPackages (p: [p.gargoyle-postgresql-connect p.postgresql-simple-interpolate])' --run ghci

Then run

:set -XQuasiQuotes
import Gargoyle.PostgreSQL.Connect (withDb)
import Data.Pool (withResource)
import Database.PostgreSQL.Simple (Only (..), query)
import Database.PostgreSQL.Simple.SqlQQ.Interpolated (isql)
[isql|SELECT ${1 + 1}|]
-- ("SELECT ?",[Plain "2"])
withDb "db" $ \pool -> withResource pool $ \c -> (uncurry (query c) [isql|SELECT ${1 + 1}, ${reverse "HELLO"}::text|] :: IO [(Int, String)])
-- [(2,"OLLEH")]

Acknowledgements

This library is basically just a copy of the here package by Taylor M. Hedberg with slight modifications!