on dfd6885 help sign in
87 lines 2.8 kB history raw
1{-# OPTIONS_GHC -fno-warn-partial-type-signatures #-}
2{-# LANGUAGE
3 DataKinds
4 , DeriveGeneric
5 , FlexibleInstances
6 , MultiParamTypeClasses
7 , OverloadedLabels
8 , OverloadedStrings
9 , PartialTypeSignatures
10 , TypeApplications
11 , TypeOperators
12 , TypeSynonymInstances
13#-}
15module Main (main,col1,col2) where
17import Control.Category ((>>>))
18import Control.Monad.Base
19import Data.Function ((&))
20import Data.Int
21import Data.Monoid
22import Generics.SOP hiding (from)
23import Squeal.PostgreSQL
25import qualified Data.ByteString.Char8 as Char8
26import qualified GHC.Generics as GHC
28type Schema =
29 '[ "students" ::: '["name" ::: 'Required ('NotNull 'PGtext)]
30 , "table1" :::
31 '[ "col1" ::: 'Required ('NotNull 'PGint4)
32 , "col2" ::: 'Required ('NotNull 'PGint4)
33 ]
34 ]
36data Row = Row { col1 :: Int32, col2 :: Int32 }
37 deriving (Show, GHC.Generic)
38instance Generic Row
39instance HasDatatypeInfo Row
41main :: IO ()
42main = do
43 Char8.putStrLn "squeal"
44 connectionString <- pure
45 "host=localhost port=5432 dbname=exampledb"
46 Char8.putStrLn $ "connecting to " <> connectionString
47 connection0 <- connectdb connectionString
48 Char8.putStrLn "setting up schema"
49 connection1 <- flip execPQ (connection0 :: Connection '[]) $ define $
50 createTable #students ((text & notNull) `As` #name :* Nil ) []
51 >>>
52 createTable #table1
53 ((int4 & notNull) `As` #col1 :* (int4 & notNull) `As` #col2 :* Nil) []
54 connection2 <- flip execPQ (connection1 :: Connection Schema) $ do
55 let
56 insert :: Manipulation Schema '[_,_,_,_] '[]
57 insert =
58 insertInto #table1
59 ( Values
60 (param @1 `As` #col1 :* param @2 `As` #col2 :* Nil)
61 [param @3 `As` #col1 :* param @4 `As` #col2 :* Nil]
62 ) Conflict (Returning Nil)
63 liftBase $ Char8.putStrLn "manipulating"
64 _insertResult <- manipulateParams insert
65 (1::Int32,2::Int32,3::Int32,4::Int32)
66 liftBase $ Char8.putStrLn "querying"
67 result <- query $
68 selectStar (from (Table (#table1 `As` #table1)))
69 value00 <- getValue (RowNumber 0) (columnNumber @0) result
70 value01 <- getValue (RowNumber 0) (columnNumber @1) result
71 value10 <- getValue (RowNumber 1) (columnNumber @0) result
72 value11 <- getValue (RowNumber 1) (columnNumber @1) result
73 row0 <- getRow (RowNumber 0) result
74 row1 <- getRow (RowNumber 1) result
75 rows <- getRows result
76 liftBase $ do
77 print (value00 :: Int32)
78 print (value01 :: Int32)
79 print (value10 :: Int32)
80 print (value11 :: Int32)
81 print (row0 :: Row)
82 print (row1 :: Row)
83 print (rows :: [Row])
84 Char8.putStrLn "tearing down schema"
85 connection3 <- flip execPQ (connection2 :: Connection Schema) $ define $
86 dropTable #table1 >>> dropTable #students
87 finish (connection3 :: Connection '[])