@@ -11,17 +11,15 @@ Let's see an example!
1111First, we need some language extensions because Squeal uses modern GHC
1212features.
1313
14−>>> :set -XDataKinds -XDeriveGeneric -XOverloadedLabels
14+>>> :set -XDataKinds -XDeriveGeneric -XOverloadedLabels -XFlexibleContexts
1515>>> :set -XOverloadedStrings -XTypeApplications -XTypeOperators -XGADTs
1616
1717We'll need some imports.
1818
19−>>> import Control.Monad (void)
2019>>> import Control.Monad.IO.Class (liftIO)
2120>>> import Data.Int (Int32)
2221>>> import Data.Text (Text)
2322>>> import Squeal.PostgreSQL
24−>>> import Squeal.PostgreSQL.Render
2523
2624We'll use generics to easily convert between Haskell and PostgreSQL values.
2725
@@ -104,26 +102,24 @@ let
104102DROP TABLE "emails";
105103DROP TABLE "users";
106104
107−Next, we'll write `Manipulation`s to insert data into our two tables.
108−A `Manipulation` like `insertInto`, `update` or `deleteFrom`
109−has three type parameters, the schema it refers to, a list of parameters
110−it can take as input, and a list of columns it produces as output. When
111−we insert into the users table, we will need a parameter for the @name@
112−field but not for the @id@ field. Since it's serial, we can use a default
113−value. However, since the emails table refers to the users table, we will
114−need to retrieve the user id that the insert generates and insert it into
115−the emails table. Take a careful look at the type and definition of both
116−of our inserts.
117−
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@.
105+We'll need a Haskell type for @User@s. We give the type `Generics.SOP.Generic` and
106+`Generics.SOP.HasDatatypeInfo` instances so that we can encode and decode @User@s.
122107
123108>>> data User = User { userName :: Text, userEmail :: Maybe Text } deriving (Show, GHC.Generic)
124109>>> instance SOP.Generic User
125110>>> instance SOP.HasDatatypeInfo User
126111
112+Next, we'll write `Manipulation_`s to insert @User@s into our two tables.
113+A `Manipulation_` like `insertInto`, `update` or `deleteFrom`
114+has three type parameters, the schemas it refers to, input parameters
115+and an output row. When
116+we insert into the users table, we will need a parameter for the @name@
117+field but not for the @id@ field. Since it's serial, we can use a default
118+value. However, since the emails table refers to the users table, we will
119+need to retrieve the user id that the insert generates and insert it into
120+the emails table. We can do this in a single `Manipulation_` by using a
121+`with` statement.
122+
127123>>> :{
128124let
129125 insertUser :: Manipulation_ Schemas User ()
@@ -140,10 +136,10 @@ let
140136>>> printSQL insertUser
141137WITH "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"
142138
143−Next we write a `Query` to retrieve users from the database. We're not
139+Next we write a `Query_` to retrieve users from the database. We're not
144140interested in the ids here, just the usernames and email addresses. We
145−need to use an inner join to get the right result. A `Query` is like a
146−`Manipulation` with the same kind of type parameters.
141+need to use an `innerJoin` to get the right result. A `Query_` is like a
142+`Manipulation_` with the same kind of type parameters.
147143
148144>>> :{
149145let
@@ -187,69 +183,42 @@ let
187183 usersRows <- getRows usersResult
188184 liftIO $ print (usersRows :: [User])
189185in
190− void . withConnection "host=localhost port=5432 dbname=exampledb" $
186+ withConnection "host=localhost port=5432 dbname=exampledb" $
191187 define setup
192188 & pqThen session
193189 & pqThen (define teardown)
194190:}
195191[User {userName = "Alice", userEmail = Just "alice@gmail.com"},User {userName = "Bob", userEmail = Nothing},User {userName = "Carole", userEmail = Just "carole@hotmail.com"}]
196192-}
197−module Squeal.PostgreSQL
198− ( module Squeal.PostgreSQL.Alias
199− , module Squeal.PostgreSQL.Binary
200− , module Squeal.PostgreSQL.Definition
201− , module Squeal.PostgreSQL.Expression
202− , module Squeal.PostgreSQL.Expression.Aggregate
203− , module Squeal.PostgreSQL.Expression.Collection
204− , module Squeal.PostgreSQL.Expression.Comparison
205− , module Squeal.PostgreSQL.Expression.Json
206− , module Squeal.PostgreSQL.Expression.Literal
207− , module Squeal.PostgreSQL.Expression.Logic
208− , module Squeal.PostgreSQL.Expression.Math
209− , module Squeal.PostgreSQL.Expression.Null
210− , module Squeal.PostgreSQL.Expression.Parameter
211− , module Squeal.PostgreSQL.Expression.SetOf
212− , module Squeal.PostgreSQL.Expression.Sort
213− , module Squeal.PostgreSQL.Expression.Subquery
214− , module Squeal.PostgreSQL.Expression.Text
215− , module Squeal.PostgreSQL.Expression.TextSearch
216− , module Squeal.PostgreSQL.Expression.Time
217− , module Squeal.PostgreSQL.Expression.Type
218− , module Squeal.PostgreSQL.Expression.Window
219− , module Squeal.PostgreSQL.List
220− , module Squeal.PostgreSQL.Manipulation
221− , module Squeal.PostgreSQL.PG
222− , module Squeal.PostgreSQL.PQ
223− , module Squeal.PostgreSQL.Query
224− , module Squeal.PostgreSQL.Schema
225− , module Squeal.PostgreSQL.Transaction
226− ) where
227−
228−import Squeal.PostgreSQL.Alias
229−import Squeal.PostgreSQL.Binary
230−import Squeal.PostgreSQL.Definition
231−import Squeal.PostgreSQL.Expression
232−import Squeal.PostgreSQL.Expression.Aggregate
233−import Squeal.PostgreSQL.Expression.Collection
234−import Squeal.PostgreSQL.Expression.Comparison
235−import Squeal.PostgreSQL.Expression.Json
236−import Squeal.PostgreSQL.Expression.Literal
237−import Squeal.PostgreSQL.Expression.Logic
238−import Squeal.PostgreSQL.Expression.Math
239−import Squeal.PostgreSQL.Expression.Null
240−import Squeal.PostgreSQL.Expression.Parameter
241−import Squeal.PostgreSQL.Expression.SetOf
242−import Squeal.PostgreSQL.Expression.Sort
243−import Squeal.PostgreSQL.Expression.Subquery
244−import Squeal.PostgreSQL.Expression.Text
245−import Squeal.PostgreSQL.Expression.TextSearch
246−import Squeal.PostgreSQL.Expression.Time
247−import Squeal.PostgreSQL.Expression.Type
248−import Squeal.PostgreSQL.Expression.Window
249−import Squeal.PostgreSQL.List
250−import Squeal.PostgreSQL.Manipulation
251−import Squeal.PostgreSQL.PG
252−import Squeal.PostgreSQL.PQ
253−import Squeal.PostgreSQL.Query
254−import Squeal.PostgreSQL.Schema
255−import Squeal.PostgreSQL.Transaction
193+module Squeal.PostgreSQL (module X) where
194+
195+import Squeal.PostgreSQL.Alias as X
196+import Squeal.PostgreSQL.Binary as X
197+import Squeal.PostgreSQL.Definition as X
198+import Squeal.PostgreSQL.Expression as X
199+import Squeal.PostgreSQL.Expression.Aggregate as X
200+import Squeal.PostgreSQL.Expression.Collection as X
201+import Squeal.PostgreSQL.Expression.Comparison as X
202+import Squeal.PostgreSQL.Expression.Json as X
203+import Squeal.PostgreSQL.Expression.Literal as X
204+import Squeal.PostgreSQL.Expression.Logic as X
205+import Squeal.PostgreSQL.Expression.Math as X
206+import Squeal.PostgreSQL.Expression.Null as X
207+import Squeal.PostgreSQL.Expression.Parameter as X
208+import Squeal.PostgreSQL.Expression.SetOf as X
209+import Squeal.PostgreSQL.Expression.Sort as X
210+import Squeal.PostgreSQL.Expression.Subquery as X
211+import Squeal.PostgreSQL.Expression.Text as X
212+import Squeal.PostgreSQL.Expression.TextSearch as X
213+import Squeal.PostgreSQL.Expression.Time as X
214+import Squeal.PostgreSQL.Expression.Type as X
215+import Squeal.PostgreSQL.Expression.Window as X
216+import Squeal.PostgreSQL.List as X
217+import Squeal.PostgreSQL.Manipulation as X
218+import Squeal.PostgreSQL.Migration as X
219+import Squeal.PostgreSQL.PG as X
220+import Squeal.PostgreSQL.PQ as X
221+import Squeal.PostgreSQL.Query as X
222+import Squeal.PostgreSQL.Render as X
223+import Squeal.PostgreSQL.Schema as X
224+import Squeal.PostgreSQL.Transaction as X