-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSanta.hs
More file actions
61 lines (46 loc) · 1.82 KB
/
Santa.hs
File metadata and controls
61 lines (46 loc) · 1.82 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- https://ocharles.org.uk/blog/posts/2012-12-03-postgresql-simple.html
{-# LANGUAGE OverloadedStrings #-}
module Santa where
import Data.Text
import Data.Int(Int64)
import Data.ByteString (ByteString)
import Control.Applicative
import Control.Monad
import Database.PostgreSQL.Simple
import Database.PostgreSQL.Simple.ToField
import Database.PostgreSQL.Simple.FromRow
import Database.PostgreSQL.Simple.ToRow
data Present = Present { presentName :: Text} deriving Show
data Location = Location { locLat :: Double
, locLong :: Double
} deriving Show
data Child = Child { childName :: Text
, childLocation :: Location
} deriving Show
uri :: ByteString
uri = "postgres://natxo@localhost/helloworld"
instance FromRow Present where
fromRow = Present <$> field
instance ToRow Present where
toRow p = [toField (presentName p)]
instance FromRow Child where
fromRow = Child <$> field <*> liftM2 Location field field
instance ToRow Child where
toRow c = [toField (childName c), toField (locLat (childLocation c)), toField (locLong (childLocation c))]
allChildren :: Connection -> IO [Child]
allChildren c = query_ c "SELECT name, loc_lat, loc_long FROM child"
addPresent :: Connection -> Present -> IO Int64
addPresent c present = execute c "INSERT INTO present (name) VALUES (?)" present
allPresents :: Connection -> IO [Present]
allPresents c = query_ c "SELECT name FROM present"
main :: IO ()
main = do
conn <- connectPostgreSQL uri
putStrLn "all children:"
mapM_ (putStrLn . show) =<< allChildren conn
putStrLn "all presents:"
mapM_ (putStrLn . show) =<< allPresents conn
putStrLn "add new present:"
present <- getLine
addedPresents <- addPresent conn Present {presentName=(pack present)}
putStrLn $ "added presents: " ++ show addedPresents