@hackage sqlite-simple-interpolate0.1.1
Interpolated SQLite queries via quasiquotation
Categories
License
BSD-3-Clause
Maintainer
ruby0b
Links
Versions
Installation
Tested Compilers
Dependencies (6)
- base >=4.5 && <5
- haskell-src-meta >=0.6 && <0.9
- mtl >=2.1 && <2.3
- parsec ^>=3.1
- sqlite-simple >=0.1
- template-haskell >=2.16 && <2.19 Show all…
Dependents (0)
Package Flags
tests(on by default)
sqlite-simple-interpolate
Write natural SQL statements in Haskell using QuasiQuoters!
{-# LANGUAGE QuasiQuotes #-}
module Main where
import Control.Exception (bracket)
import Data.Char (toLower)
import Data.Function ((&))
import qualified Database.SQLite.Simple as SQL
import Database.SQLite.Simple.QQ.Interpolated
table :: String
table = "people"
main :: IO ()
main = bracket (SQL.open ":memory:") SQL.close $ \conn -> do
conn & [iexecute|CREATE TABLE !{table} (name TEXT, age INTEGER)|]
conn & [iexecute|INSERT INTO !{table} VALUES ("clive", 40)|]
-- you can always use 'isql' directly but you'll have to use uncurry:
(uncurry $ SQL.execute conn) [isql|INSERT INTO !{table} VALUES ("clara", 32)|]
ageSum <- conn & [ifold|SELECT age FROM !{table}|] 0 (\acc (SQL.Only x) -> pure (acc + x))
print (ageSum :: Int)
let limit = 1 :: Int
ages <- conn & [iquery|SELECT age FROM !{table} WHERE name = ${map toLower "CLIVE"} LIMIT ${limit}|]
print (ages :: [SQL.Only Int])
Acknowledgements
This library is a fork of postgresql-simple-interpolate, adapted for use with sqlite-simple.
The original itself is basically just a copy of the here package by Taylor M. Hedberg with slight modifications!