-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClients.hs
More file actions
43 lines (32 loc) · 1.13 KB
/
Copy pathClients.hs
File metadata and controls
43 lines (32 loc) · 1.13 KB
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
-- source: http://www.stephendiehl.com/posts/haskell_web.html
{-# LANGUAGE OverloadedStrings #-}
module Clients where
import Data.Text
import Data.ByteString (ByteString)
import Control.Applicative
import Control.Monad
import Database.PostgreSQL.Simple
import Database.PostgreSQL.Simple.FromRow
data Client = Client { firstName :: Text
, lastName :: Text
, clientLocation :: Location
} deriving (Show)
data Location = Location { address :: Text
, location :: Text
} deriving (Show)
uri :: ByteString
uri = "postgres://natxo@localhost/helloworld"
instance FromRow Location where
fromRow = Location <$> field <*> field
instance FromRow Client where
fromRow = Client <$> field <*> field <*> liftM2 Location field field
queryClients :: Connection -> IO [Client]
queryClients c = query_ c
"SELECT c.firstname, c.lastname, l.address, l.location \
\ FROM clients c, locations l \
\ WHERE c.location = l.id"
main :: IO ()
main = do
conn <- connectPostgreSQL uri
clients <- queryClients conn
mapM_ (putStrLn . show) clients