Giter Site home page Giter Site logo

3noch / postgresql-simple-interpolate Goto Github PK

View Code? Open in Web Editor NEW
11.0 5.0 2.0 22 KB

Safe interpolated SQL queries in Haskell

License: BSD 3-Clause "New" or "Revised" License

Haskell 84.80% Nix 15.20%
haskell postgresql postgresql-database interpolation quasiquoter template-haskell

postgresql-simple-interpolate's Introduction

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!

postgresql-simple-interpolate's People

Contributors

3noch avatar ali-abrar avatar bergey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

postgresql-simple-interpolate's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.