@@ -12,7 +12,7 @@ First, we need some language extensions because Squeal uses modern GHC
14>>> :set -XDataKinds -XDeriveGeneric -XOverloadedLabels
14>>> :set -XDataKinds -XDeriveGeneric -XOverloadedLabels
15−>>> :set -XOverloadedStrings -XTypeApplications -XTypeOperators
15+>>> :set -XOverloadedStrings -XTypeApplications -XTypeOperators -XGADTs
17We'll need some imports.
17We'll need some imports.
@@ -117,25 +117,18 @@ of our inserts.
120− insertUser :: Manipulation Schema '[ 'NotNull 'PGtext ] '[ "fromOnly" ::: 'NotNull 'PGint4 ]
120+ insertUser :: Manipulation Schema '[ 'NotNull 'PGtext, 'Null 'PGtext ] '[]
121− insertUser = insertInto #users
121+ insertUser = with (u `as` #u) e
122− (Values_ (defaultAs #id :* param @1 `as` #name))
122+ where
123− (OnConflict (OnConstraint #pk_users) DoNothing) (Returning (#id `as` #fromOnly))
128− insertEmail :: Manipulation Schema '[ 'NotNull 'PGint4, 'Null 'PGtext] '[]
129− insertEmail = insertInto #emails
123+ u = insertInto #users
124+ (Values_ (defaultAs #id :* param @1 `as` #name))
125+ OnConflictDoRaise (Returning (#id :* param @2 `as` #email))
126+ e = insertInto_ #emails
130− (Values_ (defaultAs #id :* param @1 `as` #user_id :* param @2 `as` #email))
127+ (Select (defaultAs #id :* #u ! #id `as` #user_id :* #u ! #email) (from (view #u)))
131− (OnConflict (OnConstraint #pk_emails) DoNothing) (Returning Nil)
134>>> printSQL insertUser
130>>> printSQL insertUser
135−INSERT INTO "users" ("id", "name") VALUES (DEFAULT, ($1 :: text)) ON CONFLICT ON CONSTRAINT "pk_users" DO NOTHING RETURNING "id" AS "fromOnly"
131+WITH "u" AS (INSERT INTO "users" ("id", "name") VALUES (DEFAULT, ($1 :: text)) RETURNING "id" AS "id", ($2 :: text) AS "email") INSERT INTO "emails" ("user_id", "email") SELECT "u"."id", "u"."email" FROM "u" AS "u"
137−>>> printSQL insertEmail
138−INSERT INTO "emails" ("id", "user_id", "email") VALUES (DEFAULT, ($1 :: int4), ($2 :: text)) ON CONFLICT ON CONSTRAINT "pk_emails" DO NOTHING
140Next we write a `Query` to retrieve users from the database. We're not
133Next we write a `Query` to retrieve users from the database. We're not
141interested in the ids here, just the usernames and email addresses. We
134interested in the ids here, just the usernames and email addresses. We
@@ -191,9 +184,7 @@ transformer and when the schema doesn't change we can use `Monad` and
192 session :: PQ Schema Schema IO ()
185 session :: PQ Schema Schema IO ()
193 session = do
186 session = do
194− idResults <- traversePrepared insertUser (Only . userName <$> users)
195− ids <- traverse (fmap fromOnly . getRow 0) idResults
196− traversePrepared_ insertEmail (zip (ids :: [Int32]) (userEmail <$> users))
187+ _ <- traversePrepared_ insertUser users
197 usersResult <- runQuery getUsers
188 usersResult <- runQuery getUsers
198 usersRows <- getRows usersResult
189 usersRows <- getRows usersResult
199 liftBase $ print (usersRows :: [User])
190 liftBase $ print (usersRows :: [User])