on 7fe976d help sign in
91 lines 3.1 kB history raw
1{-# LANGUAGE
2 DataKinds
3 , MagicHash
4 , OverloadedLabels
5 , OverloadedStrings
6 , ScopedTypeVariables
7 , TypeApplications
8 , TypeOperators
9#-}
11module Main (main) where
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 "setting up database"
32 connection1 <- flip execPQ connection0 $
33 pqExec ( createTable #students
34 ( (text & notNull) `As` #name
35 :* Nil ) )
36 &>> createTable #table1
37 ( (int4 & notNull) `As` #col1
38 :* (int4 & notNull) `As` #col2
39 :* Nil )
40 &>> insertInto #table1 ( 1 `As` #col1 :* 2 `As` #col2 :* Nil )
41 &>> insertInto #table1 ( 3 `As` #col1 :* 4 `As` #col2 :* Nil )
42 Char8.putStrLn "querying"
43 connection2 <- flip execPQ connection1 $ do
44 Just result <- pqExecNil $ select $ starFrom #table1
45 runMaybeT . runExceptT . flip runValue result $ do
46 value00 <- getValue (RowNumber 0) (columnNumber @0)
47 value01 <- getValue (RowNumber 0) (columnNumber @1)
48 value10 <- getValue (RowNumber 1) (columnNumber @0)
49 value11 <- getValue (RowNumber 1) (columnNumber @1)
50 row0 <- getRow (RowNumber 0)
51 row1 <- getRow (RowNumber 1)
52 liftBase $ do
53 print (value00 :: Int32)
54 print (value01 :: Int32)
55 print (value10 :: Int32)
56 print (value11 :: Int32)
57 print (row0 :: NP I '[Int32,Int32])
58 print (row1 :: NP I '[Int32,Int32])
59 Char8.putStrLn "tearing down database"
60 connection3 :: Connection '[] <- flip execPQ connection2 $ pqExec
61 (dropTable #table1 :: Statement '[] '[] Tables '["students" ::: StudentsColumns])
62 &>> dropTable #students
63 finish connection3
65type Columns =
66 '[ "col1" ::: 'Required ('NotNull 'PGInt4)
67 , "col2" ::: 'Required ('NotNull 'PGInt4)
68 ]
69-- type SumAndCol1 = '[ "sum" ::: 'NotNull 'PGInt4, "col1" ::: 'NotNull 'PGInt4]
70type StudentsColumns = '["name" ::: 'Required ('NotNull 'PGText)]
71type Tables = '[ "table1" ::: Columns, "students" ::: StudentsColumns]
72-- type OrderColumns =
73-- [ "orderID" ::: 'NotNull 'PGInt4
74-- , "orderVal" ::: 'NotNull 'PGText
75-- , "customerID" ::: 'NotNull 'PGInt4
76-- , "shipperID" ::: 'NotNull 'PGInt4
77-- ]
78-- type CustomerColumns =
79-- [ "customerID" ::: 'NotNull 'PGInt4, "customerVal" ::: 'NotNull 'PGFloat4 ]
80-- type ShipperColumns =
81-- [ "shipperID" ::: 'NotNull 'PGInt4, "shipperVal" ::: 'NotNull 'PGBool ]
82-- type JoinTables =
83-- [ "shippers" ::: ShipperColumns
84-- , "customers" ::: CustomerColumns
85-- , "orders" ::: OrderColumns
86-- ]
87-- type ValueColumns =
88-- [ "orderVal" ::: 'NotNull 'PGText
89-- , "customerVal" ::: 'NotNull 'PGFloat4
90-- , "shipperVal" ::: 'NotNull 'PGBool
91-- ]