@@ -24,13 +24,13 @@ import Squeal.PostgreSQL
2424import qualified Data.ByteString.Char8 as Char8
2525import qualified GHC.Generics as GHC
2626
27−-- type Schema =
28−-- '[ "students" ::: '["name" ::: 'Required ('NotNull 'PGtext)]
29−-- , "table1" :::
30−-- '[ "col1" ::: 'Required ('NotNull 'PGint4)
31−-- , "col2" ::: 'Required ('NotNull 'PGint4)
32−-- ]
33−-- ]
27+type Schema =
28+ '[ "students" ::: '["name" ::: 'Required ('NotNull 'PGtext)]
29+ , "table1" :::
30+ '[ "col1" ::: 'Required ('NotNull 'PGint4)
31+ , "col2" ::: 'Required ('NotNull 'PGint4)
32+ ]
33+ ]
3434
3535data Table1Row = Table1Row { col1 :: Int32, col2 :: Int32 }
3636 deriving (Show, GHC.Generic)
@@ -40,49 +40,52 @@ instance HasDatatypeInfo Table1Row
4040main :: IO ()
4141main = do
4242 Char8.putStrLn "squeal"
43− -- connectionString <- pure
44− -- "host=localhost port=5432 dbname=exampledb"
45− -- Char8.putStrLn $ "connecting to " <> connectionString
46− -- connection0 <- connectdb connectionString
47− -- Char8.putStrLn "setting up schema"
48− -- connection1 <- runPQ (connection0 :: Connection '[]) $ pqExec $
49− -- createTable #students ((text & notNull) `As` #name :* Nil ) []
50− -- >>>
51− -- createTable #table1
52− -- ((int4 & notNull) `As` #col1 :* (int4 & notNull) `As` #col2 :* Nil) []
53− -- connection2 <- runPQ (connection1 :: Connection Schema) $ do
54− -- let
55− -- manipulation :: Manipulation Schema
56− -- '[ 'Required ('NotNull 'PGint4)
57− -- , 'Required ('NotNull 'PGint4)
58− -- , 'Required ('NotNull 'PGint4)
59− -- , 'Required ('NotNull 'PGint4)
60− -- ] '[]
61− -- manipulation =
62− -- insertInto #table1
63− -- ( Values
64− -- (param (Proxy @1) `As` #col1 :* param (Proxy @2) `As` #col2 :* Nil)
65− -- [param (Proxy @3) `As` #col1 :* param (Proxy @4) `As` #col2 :* Nil]
66− -- ) Conflict (Returning Nil)
67− -- liftBase $ Char8.putStrLn "manipulating"
68− -- _ <- pqExecParams manipulation (1::Int32,2::Int32,3::Int32,4::Int32)
69− -- liftBase $ Char8.putStrLn "querying"
70− -- Just result <- pqExecNil . query $
71− -- selectStar (from (Table (#table1 `As` #table1)))
72− -- value00 <- getValue (RowNumber 0) (columnNumber @0) result
73− -- value01 <- getValue (RowNumber 0) (columnNumber @1) result
74− -- value10 <- getValue (RowNumber 1) (columnNumber @0) result
75− -- value11 <- getValue (RowNumber 1) (columnNumber @1) result
76− -- row0 <- getRow (RowNumber 0) result
77− -- row1 <- getRow (RowNumber 1) result
78− -- liftBase $ do
79− -- print (value00 :: Int32)
80− -- print (value01 :: Int32)
81− -- print (value10 :: Int32)
82− -- print (value11 :: Int32)
83− -- print (row0 :: Table1Row)
84− -- print (row1 :: Table1Row)
85− -- Char8.putStrLn "tearing down schema"
86− -- connection3 <- runPQ (connection2 :: Connection Schema) $ pqExec $
87− -- dropTable #table1 >>> dropTable #students
88− -- finish (connection3 :: Connection '[])
43+ connectionString <- pure
44+ "host=localhost port=5432 dbname=exampledb"
45+ Char8.putStrLn $ "connecting to " <> connectionString
46+ connection0 <- connectdb connectionString
47+ Char8.putStrLn "setting up schema"
48+ connection1 <- flip execPQ (connection0 :: Connection '[]) $ define $
49+ createTable #students ((text & notNull) `As` #name :* Nil ) []
50+ >>>
51+ createTable #table1
52+ ((int4 & notNull) `As` #col1 :* (int4 & notNull) `As` #col2 :* Nil) []
53+ connection2 <- flip execPQ (connection1 :: Connection Schema) $ do
54+ let
55+ insert :: Manipulation Schema
56+ '[ 'Required ('NotNull 'PGint4)
57+ , 'Required ('NotNull 'PGint4)
58+ , 'Required ('NotNull 'PGint4)
59+ , 'Required ('NotNull 'PGint4)
60+ ] '[]
61+ insert =
62+ insertInto #table1
63+ ( Values
64+ (param (Proxy @1) `As` #col1 :* param (Proxy @2) `As` #col2 :* Nil)
65+ [param (Proxy @3) `As` #col1 :* param (Proxy @4) `As` #col2 :* Nil]
66+ ) Conflict (Returning Nil)
67+ liftBase $ Char8.putStrLn "manipulating"
68+ _insertResult <- manipulateParams insert
69+ (1::Int32,2::Int32,3::Int32,4::Int32)
70+ liftBase $ Char8.putStrLn "querying"
71+ result <- query $
72+ selectStar (from (Table (#table1 `As` #table1)))
73+ value00 <- getValue (RowNumber 0) (columnNumber @0) result
74+ value01 <- getValue (RowNumber 0) (columnNumber @1) result
75+ value10 <- getValue (RowNumber 1) (columnNumber @0) result
76+ value11 <- getValue (RowNumber 1) (columnNumber @1) result
77+ row0 <- getRow (RowNumber 0) result
78+ row1 <- getRow (RowNumber 1) result
79+ rows <- getRows result
80+ liftBase $ do
81+ print (value00 :: Int32)
82+ print (value01 :: Int32)
83+ print (value10 :: Int32)
84+ print (value11 :: Int32)
85+ print (row0 :: Table1Row)
86+ print (row1 :: Table1Row)
87+ print (rows :: [Table1Row])
88+ Char8.putStrLn "tearing down schema"
89+ connection3 <- flip execPQ (connection2 :: Connection Schema) $ define $
90+ dropTable #table1 >>> dropTable #students
91+ finish (connection3 :: Connection '[])