on dev help sign in
use Query_ and Manipulation_
commit7b9b2afc5ac67487b4e86cbd9a660aac08bb685c authorEitan Chatav <eitan.chatav@gmail.com> dateThu 10 Jan 2019, 18:03 (8 years ago) parent8492289
1 file changed+12−15
squeal-postgresql/src/Squeal/PostgreSQL.hs+12−15
squeal-postgresql/src/Squeal/PostgreSQL.hs+12−15
@@ -115,9 +115,18 @@ need to retrieve the user id that the insert generates and insert it into
115115the emails table. Take a careful look at the type and definition of both
116116of our inserts.
117117
118+We'll need a Haskell type for users. We give the type `Generics.SOP.Generic` and
119+`Generics.SOP.HasDatatypeInfo` instances so that we can decode the rows
120+we receive when we run @getUsers@. Notice that the record fields of the
121+@User@ type match the column names of @getUsers@.
122+
123+>>> data User = User { userName :: Text, userEmail :: Maybe Text } deriving (Show, GHC.Generic)
124+>>> instance SOP.Generic User
125+>>> instance SOP.HasDatatypeInfo User
126+
118127>>> :{
119128let
120− insertUser :: Manipulation '[] Schemas '[ 'NotNull 'PGtext, 'Null 'PGtext ] '[]
129+ insertUser :: Manipulation_ Schemas User ()
121130 insertUser = with (u `as` #u) e
122131 where
123132 u = insertInto #users
@@ -137,9 +146,7 @@ need to use an inner join to get the right result. A `Query` is like a
137146
138147>>> :{
139148let
140− getUsers :: Query '[] Schemas '[]
141− '[ "userName" ::: 'NotNull 'PGtext
142− , "userEmail" ::: 'Null 'PGtext ]
149+ getUsers :: Query_ Schemas () User
143150 getUsers = select_
144151 (#u ! #name `as` #userName :* #e ! #email `as` #userEmail)
145152 ( from (table (#users `as` #u)
@@ -150,17 +157,7 @@ let
150157>>> printSQL getUsers
151158SELECT "u"."name" AS "userName", "e"."email" AS "userEmail" FROM "users" AS "u" INNER JOIN "emails" AS "e" ON ("u"."id" = "e"."user_id")
152159
153−Now that we've defined the SQL side of things, we'll need a Haskell type
154−for users. We give the type `Generics.SOP.Generic` and
155−`Generics.SOP.HasDatatypeInfo` instances so that we can decode the rows
156−we receive when we run @getUsers@. Notice that the record fields of the
157−@User@ type match the column names of @getUsers@.
158−
159−>>> data User = User { userName :: Text, userEmail :: Maybe Text } deriving (Show, GHC.Generic)
160−>>> instance SOP.Generic User
161−>>> instance SOP.HasDatatypeInfo User
162−
163−Let's also create some users to add to the database.
160+Let's create some users to add to the database.
164161
165162>>> :{
166163let