on 00a665f help sign in
98 lines 3.5 kB history raw
1{-# LANGUAGE
2 DataKinds
3 , MagicHash
4 , OverloadedLabels
5 , OverloadedStrings
6 , ScopedTypeVariables
7 , TypeOperators
8#-}
10module Main (main) where
12import Control.Category ((>>>))
13import Control.Monad.Base
14import Control.Monad.Except
15import Control.Monad.Trans.Maybe
16import Data.Function ((&))
17import Data.Int
18import Data.Monoid
19import Generics.SOP
20import Squeel.PostgreSQL
22import qualified Data.ByteString.Char8 as Char8
24main :: IO ()
25main = do
26 Char8.putStrLn "squeel"
27 connectionString <- pure
28 "host=localhost port=5432 dbname=exampledb"
29 Char8.putStrLn $ "connecting to " <> connectionString
30 connection0 <- connectdb connectionString
31 Char8.putStrLn "creating tables"
32 connection1 <- flip execPQ connection0 $ pqExec $
33 createTable #students
34 ( (text & notNull) `As` #name
35 :* Nil )
36 >>>
37 createTable #table1
38 ( (int4 & notNull) `As` #col1
39 :* (int4 & notNull) `As` #col2
40 :* Nil )
41 connection2 <- flip execPQ connection1 $ do
42 _insertTable1Result <- pqExec $
43 insertInto #table1 ( 1 `As` #col1 :* 2 `As` #col2 :* Nil )
44 >>>
45 insertInto #table1 ( 3 `As` #col1 :* 4 `As` #col2 :* Nil )
46 Just selectTable1Result <- flip pqExecParams Nil $
47 (select $ starFrom #table1 :: Statement '[] Columns Tables Tables)
48 Just (Right value00) <- runMaybeT . runExceptT . flip runValue selectTable1Result $
49 getValue (RowNumber 0) colNum0
50 Just (Right value01) <- runMaybeT . runExceptT . flip runValue selectTable1Result $
51 getValue (RowNumber 0) colNum1
52 Just (Right value10) <- runMaybeT . runExceptT . flip runValue selectTable1Result $
53 getValue (RowNumber 1) colNum0
54 Just (Right value11) <- runMaybeT . runExceptT . flip runValue selectTable1Result $
55 getValue (RowNumber 1) colNum1
56 Just (Right row0) <- runMaybeT . runExceptT . flip runValue selectTable1Result $
57 getRow (RowNumber 0)
58 Just (Right row1) <- runMaybeT . runExceptT . flip runValue selectTable1Result $
59 getRow (RowNumber 1)
60 liftBase $ do
61 print (value00 :: Int32)
62 print (value01 :: Int32)
63 print (value10 :: Int32)
64 print (value11 :: Int32)
65 print (row0 :: NP I '[Int32,Int32])
66 print (row1 :: NP I '[Int32,Int32])
67 connection3 :: Connection '[] <- flip execPQ connection2 $ pqExec $
68 (dropTable #table1 :: Statement '[] '[] Tables '["students" ::: StudentsColumns])
69 >>> dropTable #students
70 finish connection3
72type Columns =
73 '[ "col1" ::: 'Required ('NotNull 'PGInt4)
74 , "col2" ::: 'Required ('NotNull 'PGInt4)
75 ]
76-- type SumAndCol1 = '[ "sum" ::: 'NotNull 'PGInt4, "col1" ::: 'NotNull 'PGInt4]
77type StudentsColumns = '["name" ::: 'Required ('NotNull 'PGText)]
78type Tables = '[ "table1" ::: Columns, "students" ::: StudentsColumns]
79-- type OrderColumns =
80-- [ "orderID" ::: 'NotNull 'PGInt4
81-- , "orderVal" ::: 'NotNull 'PGText
82-- , "customerID" ::: 'NotNull 'PGInt4
83-- , "shipperID" ::: 'NotNull 'PGInt4
84-- ]
85-- type CustomerColumns =
86-- [ "customerID" ::: 'NotNull 'PGInt4, "customerVal" ::: 'NotNull 'PGFloat4 ]
87-- type ShipperColumns =
88-- [ "shipperID" ::: 'NotNull 'PGInt4, "shipperVal" ::: 'NotNull 'PGBool ]
89-- type JoinTables =
90-- [ "shippers" ::: ShipperColumns
91-- , "customers" ::: CustomerColumns
92-- , "orders" ::: OrderColumns
93-- ]
94-- type ValueColumns =
95-- [ "orderVal" ::: 'NotNull 'PGText
96-- , "customerVal" ::: 'NotNull 'PGFloat4
97-- , "shipperVal" ::: 'NotNull 'PGBool
98-- ]