Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/3noch/postgresql-simple-interpolate
Safe interpolated SQL queries in Haskell
https://github.com/3noch/postgresql-simple-interpolate
haskell interpolation postgresql postgresql-database quasiquoter template-haskell
Last synced: 3 months ago
JSON representation
Safe interpolated SQL queries in Haskell
- Host: GitHub
- URL: https://github.com/3noch/postgresql-simple-interpolate
- Owner: 3noch
- License: bsd-3-clause
- Created: 2019-08-17T21:58:41.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-06-16T15:20:01.000Z (over 1 year ago)
- Last Synced: 2024-10-11T23:42:29.369Z (4 months ago)
- Topics: haskell, interpolation, postgresql, postgresql-database, quasiquoter, template-haskell
- Language: Haskell
- Size: 21.5 KB
- Stars: 11
- Watchers: 5
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# `postgresql-simple-interpolate`
Write natural SQL statements in Haskell using a QuasiQuotes!
```haskell
{-# 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
```haskell
: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](https://github.com/tmhedberg/here) by Taylor M. Hedberg with slight modifications!