-
Notifications
You must be signed in to change notification settings - Fork 1
/
Util.hs
61 lines (50 loc) · 1.66 KB
/
Util.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
module OpalLib.Util where
import Control.Applicative ((<$>))
import Control.Monad.Reader (ReaderT,runReaderT)
import Control.Monad.Except (ExceptT,runExceptT)
import Data.Monoid ((<>))
import Data.Profunctor.Product.Default (Default)
import Database.PostgreSQL.Simple (ConnectInfo(..),connect,defaultConnectInfo)
import Opaleye (Unpackspec,Query,showSqlForPostgres)
import Opaleye.Classy (OpaleyeEnv(..),OpaleyeError)
import Text.Groom
printSql :: Default Unpackspec a a => Query a -> IO ()
printSql = putStrLn . showSqlForPostgres
ourConnectInfo :: ConnectInfo
ourConnectInfo = defaultConnectInfo
{ connectDatabase = "opallib"
, connectUser = "bkolera"
}
type Opaleye = ReaderT OpaleyeEnv (ExceptT OpaleyeError IO)
printOpaleye :: Show a => Opaleye a -> IO ()
printOpaleye o= do
res <- runOpaleye o
putStrLn $ case res of
Left err -> "ERROR: " <> show err
Right a -> groom a
runOpaleye :: Opaleye a -> IO (Either OpaleyeError a)
runOpaleye o = OpaleyeEnv <$> connect ourConnectInfo >>= runExceptT . runReaderT o
opaleyeExample
:: (Default Unpackspec a a,Show b)
=> String
-> Query a
-> Opaleye b
-> IO ()
opaleyeExample title q o = do
printTitle title
printSql q
printThinLine
printOpaleye o
printEnding
printEnding :: IO ()
printEnding = do
printThinLine
putStrLn ""
printTitle :: String -> IO ()
printTitle title = printThinLine >> putStrLn title >> printThinLine
printThinLine :: IO ()
printThinLine = putStrLn $ line '-'
line :: Char -> String
line = take 80 . repeat