Skip to content

Instantly share code, notes, and snippets.

@ajmcmiddlin
Created June 7, 2017 05:57
Show Gist options
  • Save ajmcmiddlin/36acc482b79df51f51e25c2f2c4b1471 to your computer and use it in GitHub Desktop.
Save ajmcmiddlin/36acc482b79df51f51e25c2f2c4b1471 to your computer and use it in GitHub Desktop.
#!/usr/bin/env stack
-- stack --install-ghc runghc --package hasql
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid ((<>))
import Hasql.Connection (acquire, release, settings)
import Hasql.Decoders (int8, singleRow, value)
import Hasql.Encoders (unit)
import Hasql.Query (statement)
import Hasql.Session (query, run)
main :: IO ()
main = do
let host = "localhost"
port = 5432
user = "andrew"
pass = "supersecret"
db = "postgres"
settings' = settings host port user pass db
q = statement "SELECT COUNT(*) FROM foo"
unit -- we have no parameters to encode
(singleRow (value int8)) -- we decode an int8 (Int64) from a single row
False -- don't prepare the statement
session = query () q
printResult n = putStrLn $ "Have " <> show n <> " rows"
connE <- acquire settings'
case connE of
Left e -> print e
Right conn -> do
rE <- run session conn
either print printResult rE
release conn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment