@@ -4,6 +4,7 @@
4 , FlexibleContexts
4 , FlexibleContexts
5 , OverloadedLabels
5 , OverloadedLabels
6 , OverloadedStrings
6 , OverloadedStrings
7 , TypeApplications
8 , TypeApplications
8 , TypeOperators
9 , TypeOperators
@@ -12,9 +13,10 @@ module Main (main, main2) where
13import Control.Monad (void)
14import Control.Monad (void)
14import Control.Monad.Base (liftBase, MonadBase)
15import Control.Monad.Base (liftBase, MonadBase)
15−import Data.Int (Int32)
16+import Data.Int (Int16, Int32)
16import Data.Monoid ((<>))
17import Data.Monoid ((<>))
17import Data.Text (Text)
18import Data.Text (Text)
19+import Data.Vector (Vector)
19import Squeal.PostgreSQL
21import Squeal.PostgreSQL
@@ -27,6 +29,7 @@ type Schema =
27 '[ "pk_users" ::: 'PrimaryKey '["id"] ] :=>
29 '[ "pk_users" ::: 'PrimaryKey '["id"] ] :=>
28 '[ "id" ::: 'Def :=> 'NotNull 'PGint4
30 '[ "id" ::: 'Def :=> 'NotNull 'PGint4
29 , "name" ::: 'NoDef :=> 'NotNull 'PGtext
31 , "name" ::: 'NoDef :=> 'NotNull 'PGtext
32+ , "vec" ::: 'NoDef :=> 'NotNull ('PGarray 'PGint2)
31 , "emails" :::
34 , "emails" :::
32 '[ "pk_emails" ::: 'PrimaryKey '["id"]
35 '[ "pk_emails" ::: 'PrimaryKey '["id"]
@@ -42,7 +45,8 @@ setup :: Definition '[] Schema
43 createTable #users
46 createTable #users
44 ( serial `As` #id :*
47 ( serial `As` #id :*
45− (text & notNull) `As` #name :* Nil )
48+ (text & notNull) `As` #name :*
49+ (array int2 & notNull) `As` #vec :* Nil )
46 ( primaryKey (Column #id :* Nil) `As` #pk_users :* Nil )
50 ( primaryKey (Column #id :* Nil) `As` #pk_users :* Nil )
48 createTable #emails
52 createTable #emails
@@ -56,10 +60,10 @@ setup =
56teardown :: Definition Schema '[]
60teardown :: Definition Schema '[]
57teardown = dropTable #emails >>> dropTable #users
61teardown = dropTable #emails >>> dropTable #users
59−insertUser :: Manipulation Schema '[ 'NotNull 'PGtext]
63+insertUser :: Manipulation Schema '[ 'NotNull 'PGtext, 'NotNull ('PGarray 'PGint2)]
60 '[ "fromOnly" ::: 'NotNull 'PGint4 ]
64 '[ "fromOnly" ::: 'NotNull 'PGint4 ]
61insertUser = insertRows #users
65insertUser = insertRows #users
62− (Default `As` #id :* Set (param @1) `As` #name :* Nil) []
66+ (Default `As` #id :* Set (param @1) `As` #name :* Set (param @2) `As` #vec :* Nil) []
63 OnConflictDoNothing (Returning (#id `As` #fromOnly :* Nil))
67 OnConflictDoNothing (Returning (#id `As` #fromOnly :* Nil))
65insertEmail :: Manipulation Schema '[ 'NotNull 'PGint4, 'Null 'PGtext] '[]
69insertEmail :: Manipulation Schema '[ 'NotNull 'PGint4, 'Null 'PGtext] '[]
@@ -71,29 +75,30 @@ insertEmail = insertRows #emails
72getUsers :: Query Schema '[]
76getUsers :: Query Schema '[]
73 '[ "userName" ::: 'NotNull 'PGtext
77 '[ "userName" ::: 'NotNull 'PGtext
74− , "userEmail" ::: 'Null 'PGtext ]
78+ , "userEmail" ::: 'Null 'PGtext
79+ , "userVec" ::: 'NotNull ('PGarray 'PGint2)]
75getUsers = select
80getUsers = select
76− (#u ! #name `As` #userName :* #e ! #email `As` #userEmail :* Nil)
81+ (#u ! #name `As` #userName :* #e ! #email `As` #userEmail :* #u ! #vec `As` #userVec :* Nil)
77 ( from (Table (#users `As` #u)
82 ( from (Table (#users `As` #u)
78 & InnerJoin (Table (#emails `As` #e))
83 & InnerJoin (Table (#emails `As` #e))
79 (#u ! #id .== #e ! #user_id)) )
84 (#u ! #id .== #e ! #user_id)) )
81−data User = User { userName :: Text, userEmail :: Maybe Text }
86+data User = User { userName :: Text, userEmail :: Maybe Text, userVec :: Vector (Maybe Int16) }
82 deriving (Show, GHC.Generic)
87 deriving (Show, GHC.Generic)
83instance SOP.Generic User
88instance SOP.Generic User
84instance SOP.HasDatatypeInfo User
89instance SOP.HasDatatypeInfo User
86users :: [User]
91users :: [User]
88− [ User "Alice" (Just "alice@gmail.com")
93+ [ User "Alice" (Just "alice@gmail.com") [Nothing, Just 1]
89− , User "Bob" Nothing
94+ , User "Bob" Nothing [Just 2, Nothing]
90− , User "Carole" (Just "carole@hotmail.com")
95+ , User "Carole" (Just "carole@hotmail.com") [Just 3]
93session :: (MonadBase IO pq, MonadPQ Schema pq) => pq ()
98session :: (MonadBase IO pq, MonadPQ Schema pq) => pq ()
94session = do
99session = do
95 liftBase $ Char8.putStrLn "manipulating"
100 liftBase $ Char8.putStrLn "manipulating"
96− idResults <- traversePrepared insertUser (Only . userName <$> users)
101+ idResults <- traversePrepared insertUser ([(userName user, userVec user) | user <- users])
97 ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
102 ids <- traverse (fmap fromOnly . getRow (RowNumber 0)) idResults
98 traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
103 traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
99 liftBase $ Char8.putStrLn "querying"
104 liftBase $ Char8.putStrLn "querying"