@@ -21,7 +21,6 @@ We'll need some imports.
2121>>> import Data.Int (Int32)
2222>>> import Data.Text (Text)
2323>>> import Squeal.PostgreSQL
24−>>> import qualified Data.ByteString.Char8 as Char8 (putStrLn)
2524
2625We'll use generics to easily convert between Haskell and PostgreSQL values.
2726
@@ -81,13 +80,13 @@ let
8180
8281We can easily see the generated SQL is unsuprising looking.
8382
84−>>> Char8.putStrLn $ renderDefinition setup
83+>>> printSQL setup
8584CREATE TABLE "users" ("id" serial, "name" text NOT NULL, CONSTRAINT "pk_users" PRIMARY KEY ("id"));
8685CREATE TABLE "emails" ("id" serial, "user_id" int NOT NULL, "email" text NULL, CONSTRAINT "pk_emails" PRIMARY KEY ("id"), CONSTRAINT "fk_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE);
8786
8887Notice that @setup@ starts with an empty schema @'[]@ and produces @Schema@.
8988In our `createTable` commands we included `TableConstraint`s to define
90−primary and foreign keys, making them somewhat complex. Our tear down
89+primary and foreign keys, making them somewhat complex. Our @teardown@
9190`Definition` is simpler.
9291
9392>>> :{
@@ -96,7 +95,7 @@ let
9695 teardown = dropTable #emails >>> dropTable #users
9796:}
9897
99−>>> Char8.putStrLn $ renderDefinition teardown
98+>>> printSQL teardown
10099DROP TABLE "emails";
101100DROP TABLE "users";
102101
@@ -130,9 +129,9 @@ let
130129 OnConflictDoNothing (Returning Nil)
131130:}
132131
133−>>> Char8.putStrLn $ renderManipulation insertUser
132+>>> printSQL insertUser
134133INSERT INTO "users" ("id", "name") VALUES (DEFAULT, ($1 :: text)) ON CONFLICT DO NOTHING RETURNING "id" AS "fromOnly"
135−>>> Char8.putStrLn $ renderManipulation insertEmail
134+>>> printSQL insertEmail
136135INSERT INTO "emails" ("id", "user_id", "email") VALUES (DEFAULT, ($1 :: int4), ($2 :: text)) ON CONFLICT DO NOTHING
137136
138137Next we write a `Query` to retrieve users from the database. We're not
@@ -152,7 +151,7 @@ let
152151 (#u ! #id .== #e ! #user_id)) )
153152:}
154153
155−>>> Char8.putStrLn $ renderQuery getUsers
154+>>> printSQL getUsers
156155SELECT "u"."name" AS "userName", "e"."email" AS "userEmail" FROM "users" AS "u" INNER JOIN "emails" AS "e" ON ("u"."id" = "e"."user_id")
157156
158157Now that we've defined the SQL side of things, we'll need a Haskell type