-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathpostgres_qq.hs
More file actions
45 lines (38 loc) · 978 Bytes
/
postgres_qq.hs
File metadata and controls
45 lines (38 loc) · 978 Bytes
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
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
import qualified Data.Text as T
import qualified Database.PostgreSQL.Simple as SQL
import Database.PostgreSQL.Simple.SqlQQ (sql)
import Database.PostgreSQL.Simple.FromRow (FromRow(..), field)
data Book = Book
{ id_ :: Int
, title :: T.Text
, first_name :: T.Text
, last_name :: T.Text
} deriving (Show)
instance FromRow Book where
fromRow = Book <$> field <*> field <*> field <*> field
creds :: SQL.ConnectInfo
creds = SQL.defaultConnectInfo
{ SQL.connectUser = "example"
, SQL.connectPassword = "example"
, SQL.connectDatabase = "booktown"
}
selectBooks :: SQL.Query
selectBooks = [sql|
select
books.id,
books.title,
authors.first_name,
authors.last_name
from books
join authors on
authors.id = books.author_id
limit 5
|]
main :: IO ()
main = do
conn <- SQL.connect creds
(books :: [Book]) <- SQL.query_ conn selectBooks
print books