11module Main (main, main2) where
13import Control.Monad (void)
14import Control.Monad.Base (liftBase, MonadBase)
15import Data.Int (Int32)
16import Data.Monoid ((<>))
17import Data.Text (Text)
19import Squeal.PostgreSQL
21import qualified Data.ByteString.Char8 as Char8
22import qualified Generics.SOP as SOP
23import qualified GHC.Generics as GHC
27 '[ "id" ::: 'Optional ('NotNull 'PGint4)
28 , "name" ::: 'Required ('NotNull 'PGtext)
31 '[ "id" ::: 'Optional ('NotNull 'PGint4)
32 , "user_id" ::: 'Required ('NotNull 'PGint4)
33 , "email" ::: 'Required ('Null 'PGtext)
37setup :: Definition '[] Schema
41 (text & notNull) `As` #name :* Nil )
42 [ primaryKey (Column #id :* Nil) ]
46 (int & notNull) `As` #user_id :*
47 text `As` #email :* Nil )
48 [ primaryKey (Column #id :* Nil)
49 , foreignKey (Column #user_id :* Nil) #users (Column #id :* Nil)
50 OnDeleteCascade OnUpdateCascade ]
52teardown :: Definition Schema '[]
53teardown = dropTable #emails >>> dropTable #users
55insertUser :: Manipulation Schema
56 '[ 'Required ('NotNull 'PGtext)]
57 '[ "fromOnly" ::: 'Required ('NotNull 'PGint4) ]
58insertUser = insertInto #users
59 ( Values (def `As` #id :* param @1 `As` #name :* Nil) [] )
60 OnConflictDoNothing (Returning (#id `As` #fromOnly :* Nil))
62insertEmail :: Manipulation Schema
63 '[ 'Required ('NotNull 'PGint4), 'Required ('Null 'PGtext)] '[]
64insertEmail = insertInto #emails ( Values
66 param @1 `As` #user_id :*
67 param @2 `As` #email :* Nil) [] )
68 OnConflictDoNothing (Returning Nil)
70getUsers :: Query Schema '[]
71 '[ "userName" ::: 'Required ('NotNull 'PGtext)
72 , "userEmail" ::: 'Required ('Null 'PGtext) ]
74 (#u ! #name `As` #userName :* #e ! #email `As` #userEmail :* Nil)
75 ( from (Table (#users `As` #u)
76 & InnerJoin (Table (#emails `As` #e))
77 (#u ! #id .== #e ! #user_id)) )
79data User = User { userName :: Text, userEmail :: Maybe Text }
80 deriving (Show, GHC.Generic)
81instance SOP.Generic User
82instance SOP.HasDatatypeInfo User
86 [ User "Alice" (Just "alice@gmail.com")
88 , User "Carole" (Just "carole@hotmail.com")
91session :: (MonadBase IO pq, MonadPQ Schema pq) => pq ()
93 liftBase $ Char8.putStrLn "manipulating"
94 idResults <- traversePrepared insertUser (Only . userName <$> users)
95 ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
96 traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
97 liftBase $ Char8.putStrLn "querying"
98 usersResult <- runQuery getUsers
99 usersRows <- getRows usersResult
100 liftBase $ print (usersRows :: [User])
104 Char8.putStrLn "squeal"
105 connectionString <- pure
106 "host=localhost port=5432 dbname=exampledb"
107 Char8.putStrLn $ "connecting to " <> connectionString
108 connection0 <- connectdb connectionString
109 Char8.putStrLn "setting up schema"
110 connection1 <- execPQ (define setup) connection0
111 connection2 <- execPQ session connection1
112 Char8.putStrLn "tearing down schema"
113 connection3 <- execPQ (define teardown) connection2
118 withConnection "host=localhost port=5432 dbname=exampledb" . runPQ $
121 & thenDefine teardown
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list