on dev help sign in
Merge pull request #25 from prasmussen/quote-alias

Add double quotes around aliases

commit43cf5d1c8139a75ca571a3a2cd61a8ecb80ec1ae authorEitan Chatav <eitan.chatav@gmail.com> dateSat 7 Apr 2018, 10:31 (9 years ago) parent24d53f60bd30ce
7 files changed+66−59
squeal-postgresql/src/Squeal/PostgreSQL.hs+5−5 squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+19−19 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+14−14 squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+8−8 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+10−10 squeal-postgresql/src/Squeal/PostgreSQL/Render.hs+5 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+5−3
squeal-postgresql/src/Squeal/PostgreSQL.hs+5−5
@@ -80,7 +80,7 @@
8080-- We can easily see the generated SQL is unsuprising looking.
8181--
8282-- >>> renderDefinition setup
83−-- "CREATE TABLE users (id serial, name text NOT NULL, CONSTRAINT pk_users PRIMARY KEY (id)); CREATE TABLE emails (id serial, user_id int NOT NULL, email text, CONSTRAINT pk_emails PRIMARY KEY (id), CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE);"
83+-- "CREATE TABLE \"users\" (\"id\" serial, \"name\" text NOT NULL, CONSTRAINT \"pk_users\" PRIMARY KEY (\"id\")); CREATE TABLE \"emails\" (\"id\" serial, \"user_id\" int NOT NULL, \"email\" text, CONSTRAINT \"pk_emails\" PRIMARY KEY (\"id\"), CONSTRAINT \"fk_user_id\" FOREIGN KEY (\"user_id\") REFERENCES \"users\" (\"id\") ON DELETE CASCADE ON UPDATE CASCADE);"
8484--
8585-- Notice that @setup@ starts with an empty schema @'[]@ and produces @Schema@.
8686-- In our `createTable` commands we included `TableConstraint`s to define
@@ -94,7 +94,7 @@
9494-- :}
9595--
9696-- >>> renderDefinition teardown
97−-- "DROP TABLE emails; DROP TABLE users;"
97+-- "DROP TABLE \"emails\"; DROP TABLE \"users\";"
9898--
9999-- Next, we'll write `Manipulation`s to insert data into our two tables.
100100-- A `Manipulation` is an `insertRow` (or other inserts), `update`
@@ -127,9 +127,9 @@
127127-- :}
128128--
129129-- >>> renderManipulation insertUser
130−-- "INSERT INTO users (id, name) VALUES (DEFAULT, ($1 :: text)) ON CONFLICT DO NOTHING RETURNING id AS fromOnly;"
130+-- "INSERT INTO \"users\" (\"id\", \"name\") VALUES (DEFAULT, ($1 :: text)) ON CONFLICT DO NOTHING RETURNING \"id\" AS \"fromOnly\";"
131131-- >>> renderManipulation insertEmail
132−-- "INSERT INTO emails (id, user_id, email) VALUES (DEFAULT, ($1 :: int4), ($2 :: text)) ON CONFLICT DO NOTHING;"
132+-- "INSERT INTO \"emails\" (\"id\", \"user_id\", \"email\") VALUES (DEFAULT, ($1 :: int4), ($2 :: text)) ON CONFLICT DO NOTHING;"
133133--
134134-- Next we write a `Query` to retrieve users from the database. We're not
135135-- interested in the ids here, just the usernames and email addresses. We
@@ -149,7 +149,7 @@
149149-- :}
150150--
151151-- >>> renderQuery getUsers
152−-- "SELECT u.name AS userName, e.email AS userEmail FROM users AS u INNER JOIN emails AS e ON (u.id = e.user_id)"
152+-- "SELECT \"u\".\"name\" AS \"userName\", \"e\".\"email\" AS \"userEmail\" FROM \"users\" AS \"u\" INNER JOIN \"emails\" AS \"e\" ON (\"u\".\"id\" = \"e\".\"user_id\")"
153153--
154154-- Now that we've defined the SQL side of things, we'll need a Haskell type
155155-- for users. We give the type `Generics.SOP.Generic` and
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+19−19
@@ -110,7 +110,7 @@ CREATE statements
110110-- renderDefinition $
111111-- createTable #tab (int `As` #a :* real `As` #b :* Nil) Nil
112112-- :}
113−-- "CREATE TABLE tab (a int, b real);"
113+-- "CREATE TABLE \"tab\" (\"a\" int, \"b\" real);"
114114createTable
115115 :: ( KnownSymbol table
116116 , columns ~ (col ': cols)
@@ -136,7 +136,7 @@ createTable table columns constraints = UnsafeDefinition $
136136-- renderDefinition
137137-- (createTableIfNotExists #tab (int `As` #a :* real `As` #b :* Nil) Nil :: Definition Schema Schema)
138138-- :}
139−-- "CREATE TABLE IF NOT EXISTS tab (a int, b real);"
139+-- "CREATE TABLE IF NOT EXISTS \"tab\" (\"a\" int, \"b\" real);"
140140createTableIfNotExists
141141 :: ( Has table schema (constraints :=> columns)
142142 , SOP.SListI columns
@@ -225,7 +225,7 @@ renderColumn (Column column) = renderAlias column
225225-- (int & notNull) `As` #b :* Nil )
226226-- ( check (Column #a :* Column #b :* Nil) (#a .> #b) `As` #inequality :* Nil )
227227-- :}
228−-- "CREATE TABLE tab (a int NOT NULL, b int NOT NULL, CONSTRAINT inequality CHECK ((a > b)));"
228+-- "CREATE TABLE \"tab\" (\"a\" int NOT NULL, \"b\" int NOT NULL, CONSTRAINT \"inequality\" CHECK ((\"a\" > \"b\")));"
229229check
230230 :: NP (Column columns) subcolumns
231231 -> Condition '[table ::: ColumnsToRelation subcolumns] 'Ungrouped '[]
@@ -244,7 +244,7 @@ check _cols condition = UnsafeTableConstraintExpression $
244244-- int `As` #b :* Nil )
245245-- ( unique (Column #a :* Column #b :* Nil) `As` #uq_a_b :* Nil )
246246-- :}
247−-- "CREATE TABLE tab (a int, b int, CONSTRAINT uq_a_b UNIQUE (a, b));"
247+-- "CREATE TABLE \"tab\" (\"a\" int, \"b\" int, CONSTRAINT \"uq_a_b\" UNIQUE (\"a\", \"b\"));"
248248unique
249249 :: SOP.SListI subcolumns
250250 => NP (Column columns) subcolumns
@@ -264,7 +264,7 @@ unique columns = UnsafeTableConstraintExpression $
264264-- (text & notNull) `As` #name :* Nil )
265265-- ( primaryKey (Column #id :* Nil) `As` #pk_id :* Nil )
266266-- :}
267−-- "CREATE TABLE tab (id serial, name text NOT NULL, CONSTRAINT pk_id PRIMARY KEY (id));"
267+-- "CREATE TABLE \"tab\" (\"id\" serial, \"name\" text NOT NULL, CONSTRAINT \"pk_id\" PRIMARY KEY (\"id\"));"
268268primaryKey
269269 :: (SOP.SListI subcolumns, AllNotNull subcolumns)
270270 => NP (Column columns) subcolumns
@@ -313,7 +313,7 @@ primaryKey columns = UnsafeTableConstraintExpression $
313313-- OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
314314-- in renderDefinition setup
315315-- :}
316−-- "CREATE TABLE users (id serial, name text NOT NULL, CONSTRAINT pk_users PRIMARY KEY (id)); CREATE TABLE emails (id serial, user_id int NOT NULL, email text, CONSTRAINT pk_emails PRIMARY KEY (id), CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE);"
316+-- "CREATE TABLE \"users\" (\"id\" serial, \"name\" text NOT NULL, CONSTRAINT \"pk_users\" PRIMARY KEY (\"id\")); CREATE TABLE \"emails\" (\"id\" serial, \"user_id\" int NOT NULL, \"email\" text, CONSTRAINT \"pk_emails\" PRIMARY KEY (\"id\"), CONSTRAINT \"fk_user_id\" FOREIGN KEY (\"user_id\") REFERENCES \"users\" (\"id\") ON DELETE CASCADE ON UPDATE CASCADE);"
317317foreignKey
318318 :: ForeignKeyed schema table reftable subcolumns refsubcolumns
319319 => NP (Column columns) subcolumns
@@ -390,7 +390,7 @@ DROP statements
390390-- | `dropTable` removes a table from the schema.
391391--
392392-- >>> renderDefinition $ dropTable #muh_table
393−-- "DROP TABLE muh_table;"
393+-- "DROP TABLE \"muh_table\";"
394394dropTable
395395 :: KnownSymbol table
396396 => Alias table -- ^ table to remove
@@ -416,7 +416,7 @@ alterTable table alteration = UnsafeDefinition $
416416-- | `alterTableRename` changes the name of a table from the schema.
417417--
418418-- >>> renderDefinition $ alterTableRename #foo #bar
419−-- "ALTER TABLE foo RENAME TO bar;"
419+-- "ALTER TABLE \"foo\" RENAME TO \"bar\";"
420420alterTableRename
421421 :: (KnownSymbol table0, KnownSymbol table1)
422422 => Alias table0 -- ^ table to rename
@@ -445,7 +445,7 @@ newtype AlterTable
445445-- definition = alterTable #tab (addConstraint #positive (check (Column #col :* Nil) (#col .> 0)))
446446-- in renderDefinition definition
447447-- :}
448−-- "ALTER TABLE tab ADD CONSTRAINT positive CHECK ((col > 0));"
448+-- "ALTER TABLE \"tab\" ADD CONSTRAINT \"positive\" CHECK ((\"col\" > 0));"
449449addConstraint
450450 :: KnownSymbol alias
451451 => Alias alias
@@ -467,7 +467,7 @@ addConstraint alias constraint = UnsafeAlterTable $
467467-- definition = alterTable #tab (dropConstraint #positive)
468468-- in renderDefinition definition
469469-- :}
470−-- "ALTER TABLE tab DROP CONSTRAINT positive;"
470+-- "ALTER TABLE \"tab\" DROP CONSTRAINT \"positive\";"
471471dropConstraint
472472 :: KnownSymbol constraint
473473 => Alias constraint
@@ -493,7 +493,7 @@ class AddColumn ty where
493493 -- definition = alterTable #tab (addColumn #col2 (text & default_ "foo"))
494494 -- in renderDefinition definition
495495 -- :}
496− -- "ALTER TABLE tab ADD COLUMN col2 text DEFAULT E'foo';"
496+ -- "ALTER TABLE \"tab\" ADD COLUMN \"col2\" text DEFAULT E'foo';"
497497 --
498498 -- >>> :{
499499 -- let
@@ -505,7 +505,7 @@ class AddColumn ty where
505505 -- definition = alterTable #tab (addColumn #col2 text)
506506 -- in renderDefinition definition
507507 -- :}
508− -- "ALTER TABLE tab ADD COLUMN col2 text;"
508+ -- "ALTER TABLE \"tab\" ADD COLUMN \"col2\" text;"
509509 addColumn
510510 :: KnownSymbol column
511511 => Alias column -- ^ column to add
@@ -532,7 +532,7 @@ instance {-# OVERLAPPABLE #-} AddColumn ('NoDef :=> 'Null ty)
532532-- definition = alterTable #tab (dropColumn #col2)
533533-- in renderDefinition definition
534534-- :}
535−-- "ALTER TABLE tab DROP COLUMN col2;"
535+-- "ALTER TABLE \"tab\" DROP COLUMN \"col2\";"
536536dropColumn
537537 :: KnownSymbol column
538538 => Alias column -- ^ column to remove
@@ -552,7 +552,7 @@ dropColumn column = UnsafeAlterTable $
552552-- definition = alterTable #tab (renameColumn #foo #bar)
553553-- in renderDefinition definition
554554-- :}
555−-- "ALTER TABLE tab RENAME COLUMN foo TO bar;"
555+-- "ALTER TABLE \"tab\" RENAME COLUMN \"foo\" TO \"bar\";"
556556renameColumn
557557 :: (KnownSymbol column0, KnownSymbol column1)
558558 => Alias column0 -- ^ column to rename
@@ -589,7 +589,7 @@ newtype AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
589589-- definition = alterTable #tab (alterColumn #col (setDefault 5))
590590-- in renderDefinition definition
591591-- :}
592−-- "ALTER TABLE tab ALTER COLUMN col SET DEFAULT 5;"
592+-- "ALTER TABLE \"tab\" ALTER COLUMN \"col\" SET DEFAULT 5;"
593593setDefault
594594 :: Expression '[] 'Ungrouped '[] ty -- ^ default value to set
595595 -> AlterColumn (constraint :=> ty) ('Def :=> ty)
@@ -606,7 +606,7 @@ setDefault expression = UnsafeAlterColumn $
606606-- definition = alterTable #tab (alterColumn #col dropDefault)
607607-- in renderDefinition definition
608608-- :}
609−-- "ALTER TABLE tab ALTER COLUMN col DROP DEFAULT;"
609+-- "ALTER TABLE \"tab\" ALTER COLUMN \"col\" DROP DEFAULT;"
610610dropDefault :: AlterColumn ('Def :=> ty) ('NoDef :=> ty)
611611dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
612612
@@ -622,7 +622,7 @@ dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
622622-- definition = alterTable #tab (alterColumn #col setNotNull)
623623-- in renderDefinition definition
624624-- :}
625−-- "ALTER TABLE tab ALTER COLUMN col SET NOT NULL;"
625+-- "ALTER TABLE \"tab\" ALTER COLUMN \"col\" SET NOT NULL;"
626626setNotNull
627627 :: AlterColumn (constraint :=> 'Null ty) (constraint :=> 'NotNull ty)
628628setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
@@ -637,7 +637,7 @@ setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
637637-- definition = alterTable #tab (alterColumn #col dropNotNull)
638638-- in renderDefinition definition
639639-- :}
640−-- "ALTER TABLE tab ALTER COLUMN col DROP NOT NULL;"
640+-- "ALTER TABLE \"tab\" ALTER COLUMN \"col\" DROP NOT NULL;"
641641dropNotNull
642642 :: AlterColumn (constraint :=> 'NotNull ty) (constraint :=> 'Null ty)
643643dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
@@ -655,6 +655,6 @@ dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
655655-- alterTable #tab (alterColumn #col (alterType (numeric & notNull)))
656656-- in renderDefinition definition
657657-- :}
658−-- "ALTER TABLE tab ALTER COLUMN col TYPE numeric NOT NULL;"
658+-- "ALTER TABLE \"tab\" ALTER COLUMN \"col\" TYPE numeric NOT NULL;"
659659alterType :: TypeExpression ty -> AlterColumn ty0 ty
660660alterType ty = UnsafeAlterColumn $ "TYPE" <+> renderTypeExpression ty
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+14−14
@@ -797,7 +797,7 @@ unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
797797-- expression = sum_ #col
798798-- in renderExpression expression
799799-- :}
800−-- "sum(col)"
800+-- "sum(\"col\")"
801801sum_
802802 :: PGNum ty
803803 => Expression relations 'Ungrouped params (nullity ty)
@@ -811,7 +811,7 @@ sum_ = unsafeAggregate "sum"
811811-- expression = sumDistinct #col
812812-- in renderExpression expression
813813-- :}
814−-- "sum(DISTINCT col)"
814+-- "sum(DISTINCT \"col\")"
815815sumDistinct
816816 :: PGNum ty
817817 => Expression relations 'Ungrouped params (nullity ty)
@@ -842,7 +842,7 @@ instance PGAvg 'PGinterval 'PGinterval
842842-- expression = bitAnd #col
843843-- in renderExpression expression
844844-- :}
845−-- "bit_and(col)"
845+-- "bit_and(\"col\")"
846846bitAnd
847847 :: PGIntegral int
848848 => Expression relations 'Ungrouped params (nullity int)
@@ -856,7 +856,7 @@ bitAnd = unsafeAggregate "bit_and"
856856-- expression = bitOr #col
857857-- in renderExpression expression
858858-- :}
859−-- "bit_or(col)"
859+-- "bit_or(\"col\")"
860860bitOr
861861 :: PGIntegral int
862862 => Expression relations 'Ungrouped params (nullity int)
@@ -870,7 +870,7 @@ bitOr = unsafeAggregate "bit_or"
870870-- expression = bitAndDistinct #col
871871-- in renderExpression expression
872872-- :}
873−-- "bit_and(DISTINCT col)"
873+-- "bit_and(DISTINCT \"col\")"
874874bitAndDistinct
875875 :: PGIntegral int
876876 => Expression relations 'Ungrouped params (nullity int)
@@ -884,7 +884,7 @@ bitAndDistinct = unsafeAggregateDistinct "bit_and"
884884-- expression = bitOrDistinct #col
885885-- in renderExpression expression
886886-- :}
887−-- "bit_or(DISTINCT col)"
887+-- "bit_or(DISTINCT \"col\")"
888888bitOrDistinct
889889 :: PGIntegral int
890890 => Expression relations 'Ungrouped params (nullity int)
@@ -898,7 +898,7 @@ bitOrDistinct = unsafeAggregateDistinct "bit_or"
898898-- expression = boolAnd #col
899899-- in renderExpression expression
900900-- :}
901−-- "bool_and(col)"
901+-- "bool_and(\"col\")"
902902boolAnd
903903 :: Expression relations 'Ungrouped params (nullity 'PGbool)
904904 -- ^ what to aggregate
@@ -911,7 +911,7 @@ boolAnd = unsafeAggregate "bool_and"
911911-- expression = boolOr #col
912912-- in renderExpression expression
913913-- :}
914−-- "bool_or(col)"
914+-- "bool_or(\"col\")"
915915boolOr
916916 :: Expression relations 'Ungrouped params (nullity 'PGbool)
917917 -- ^ what to aggregate
@@ -924,7 +924,7 @@ boolOr = unsafeAggregate "bool_or"
924924-- expression = boolAndDistinct #col
925925-- in renderExpression expression
926926-- :}
927−-- "bool_and(DISTINCT col)"
927+-- "bool_and(DISTINCT \"col\")"
928928boolAndDistinct
929929 :: Expression relations 'Ungrouped params (nullity 'PGbool)
930930 -- ^ what to aggregate
@@ -937,7 +937,7 @@ boolAndDistinct = unsafeAggregateDistinct "bool_and"
937937-- expression = boolOrDistinct #col
938938-- in renderExpression expression
939939-- :}
940−-- "bool_or(DISTINCT col)"
940+-- "bool_or(DISTINCT \"col\")"
941941boolOrDistinct
942942 :: Expression relations 'Ungrouped params (nullity 'PGbool)
943943 -- ^ what to aggregate
@@ -958,7 +958,7 @@ countStar = UnsafeExpression $ "count(*)"
958958-- expression = count #col
959959-- in renderExpression expression
960960-- :}
961−-- "count(col)"
961+-- "count(\"col\")"
962962count
963963 :: Expression relations 'Ungrouped params ty
964964 -- ^ what to count
@@ -971,7 +971,7 @@ count = unsafeAggregate "count"
971971-- expression = countDistinct #col
972972-- in renderExpression expression
973973-- :}
974−-- "count(DISTINCT col)"
974+-- "count(DISTINCT \"col\")"
975975countDistinct
976976 :: Expression relations 'Ungrouped params ty
977977 -- ^ what to count
@@ -986,7 +986,7 @@ countDistinct = unsafeAggregateDistinct "count"
986986-- expression = every #col
987987-- in renderExpression expression
988988-- :}
989−-- "every(col)"
989+-- "every(\"col\")"
990990every
991991 :: Expression relations 'Ungrouped params (nullity 'PGbool)
992992 -- ^ what to aggregate
@@ -1001,7 +1001,7 @@ every = unsafeAggregate "every"
10011001-- expression = everyDistinct #col
10021002-- in renderExpression expression
10031003-- :}
1004−-- "every(DISTINCT col)"
1004+-- "every(DISTINCT \"col\")"
10051005everyDistinct
10061006 :: Expression relations 'Ungrouped params (nullity 'PGbool)
10071007 -- ^ what to aggregate
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+8−8
@@ -80,7 +80,7 @@ let
8080 insertRow_ #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
8181in renderManipulation manipulation
8282:}
83−"INSERT INTO tab (col1, col2) VALUES (2, DEFAULT);"
83+"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (2, DEFAULT);"
8484
8585parameterized insert:
8686
@@ -96,7 +96,7 @@ let
9696 (Set (param @1) `As` #col1 :* Set (param @2) `As` #col2 :* Nil)
9797in renderManipulation manipulation
9898:}
99−"INSERT INTO tab (col1, col2) VALUES (($1 :: int4), ($2 :: int4));"
99+"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (($1 :: int4), ($2 :: int4));"
100100
101101returning insert:
102102
@@ -112,7 +112,7 @@ let
112112 OnConflictDoRaise (Returning (#col1 `As` #fromOnly :* Nil))
113113in renderManipulation manipulation
114114:}
115−"INSERT INTO tab (col1, col2) VALUES (2, DEFAULT) RETURNING col1 AS fromOnly;"
115+"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (2, DEFAULT) RETURNING \"col1\" AS \"fromOnly\";"
116116
117117upsert:
118118
@@ -133,7 +133,7 @@ let
133133 (Returning $ (#col1 + #col2) `As` #sum :* Nil)
134134in renderManipulation manipulation
135135:}
136−"INSERT INTO tab (col1, col2) VALUES (2, 4), (6, 8) ON CONFLICT DO UPDATE SET col1 = 2 WHERE (col1 = col2) RETURNING (col1 + col2) AS sum;"
136+"INSERT INTO \"tab\" (\"col1\", \"col2\") VALUES (2, 4), (6, 8) ON CONFLICT DO UPDATE SET \"col1\" = 2 WHERE (\"col1\" = \"col2\") RETURNING (\"col1\" + \"col2\") AS \"sum\";"
137137
138138query insert:
139139
@@ -154,7 +154,7 @@ let
154154 (selectStar (from (table (#other_tab `As` #t))))
155155in renderManipulation manipulation
156156:}
157−"INSERT INTO tab SELECT * FROM other_tab AS t;"
157+"INSERT INTO \"tab\" SELECT * FROM \"other_tab\" AS \"t\";"
158158
159159update:
160160
@@ -169,7 +169,7 @@ let
169169 (#col1 ./= #col2)
170170in renderManipulation manipulation
171171:}
172−"UPDATE tab SET col1 = 2 WHERE (col1 <> col2);"
172+"UPDATE \"tab\" SET \"col1\" = 2 WHERE (\"col1\" <> \"col2\");"
173173
174174delete:
175175
@@ -184,7 +184,7 @@ let
184184 manipulation = deleteFrom #tab (#col1 .== #col2) ReturningStar
185185in renderManipulation manipulation
186186:}
187−"DELETE FROM tab WHERE (col1 = col2) RETURNING *;"
187+"DELETE FROM \"tab\" WHERE (\"col1\" = \"col2\") RETURNING *;"
188188-}
189189newtype Manipulation
190190 (schema :: TablesType)
@@ -488,7 +488,7 @@ WITH statements
488488-- (insertQuery_ #products_deleted (selectStar (from (table (#deleted_rows `As` #t)))))
489489-- in renderManipulation manipulation
490490-- :}
491−-- "WITH deleted_rows AS (DELETE FROM products WHERE (date < ($1 :: date)) RETURNING *) INSERT INTO products_deleted SELECT * FROM deleted_rows AS t;"
491+-- "WITH \"deleted_rows\" AS (DELETE FROM \"products\" WHERE (\"date\" < ($1 :: date)) RETURNING *) INSERT INTO \"products_deleted\" SELECT * FROM \"deleted_rows\" AS \"t\";"
492492with
493493 :: SOP.SListI commons
494494 => NP (Aliased (Manipulation schema params)) commons
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+10−10
@@ -106,7 +106,7 @@ let
106106 query = selectStar (from (table (#tab `As` #t)))
107107in renderQuery query
108108:}
109−"SELECT * FROM tab AS t"
109+"SELECT * FROM \"tab\" AS \"t\""
110110
111111restricted query:
112112
@@ -127,7 +127,7 @@ let
127127 & where_ (#col2 .> 0) )
128128in renderQuery query
129129:}
130−"SELECT (col1 + col2) AS sum, col1 AS col1 FROM tab AS t WHERE ((col1 > col2) AND (col2 > 0))"
130+"SELECT (\"col1\" + \"col2\") AS \"sum\", \"col1\" AS \"col1\" FROM \"tab\" AS \"t\" WHERE ((\"col1\" > \"col2\") AND (\"col2\" > 0))"
131131
132132subquery:
133133
@@ -142,7 +142,7 @@ let
142142 (from (subquery (selectStar (from (table (#tab `As` #t))) `As` #sub)))
143143in renderQuery query
144144:}
145−"SELECT * FROM (SELECT * FROM tab AS t) AS sub"
145+"SELECT * FROM (SELECT * FROM \"tab\" AS \"t\") AS \"sub\""
146146
147147limits and offsets:
148148
@@ -156,7 +156,7 @@ let
156156 (from (table (#tab `As` #t)) & limit 100 & offset 2 & limit 50 & offset 2)
157157in renderQuery query
158158:}
159−"SELECT * FROM tab AS t LIMIT 50 OFFSET 4"
159+"SELECT * FROM \"tab\" AS \"t\" LIMIT 50 OFFSET 4"
160160
161161parameterized query:
162162
@@ -170,7 +170,7 @@ let
170170 (from (table (#tab `As` #t)) & where_ (#col .> param @1))
171171in renderQuery query
172172:}
173−"SELECT * FROM tab AS t WHERE (col > ($1 :: float8))"
173+"SELECT * FROM \"tab\" AS \"t\" WHERE (\"col\" > ($1 :: float8))"
174174
175175aggregation query:
176176
@@ -190,7 +190,7 @@ let
190190 & having (#col1 + sum_ #col2 .> 1) )
191191in renderQuery query
192192:}
193−"SELECT sum(col2) AS sum, col1 AS col1 FROM tab AS table1 GROUP BY col1 HAVING ((col1 + sum(col2)) > 1)"
193+"SELECT sum(\"col2\") AS \"sum\", \"col1\" AS \"col1\" FROM \"tab\" AS \"table1\" GROUP BY \"col1\" HAVING ((\"col1\" + sum(\"col2\")) > 1)"
194194
195195sorted query:
196196
@@ -204,7 +204,7 @@ let
204204 (from (table (#tab `As` #t)) & orderBy [#col & AscNullsFirst])
205205in renderQuery query
206206:}
207−"SELECT * FROM tab AS t ORDER BY col ASC NULLS FIRST"
207+"SELECT * FROM \"tab\" AS \"t\" ORDER BY \"col\" ASC NULLS FIRST"
208208
209209joins:
210210
@@ -248,7 +248,7 @@ let
248248 (#o ! #shipper_id .== #s ! #id)) )
249249in renderQuery query
250250:}
251−"SELECT o.price AS order_price, c.name AS customer_name, s.name AS shipper_name FROM orders AS o INNER JOIN customers AS c ON (o.customer_id = c.id) INNER JOIN shippers AS s ON (o.shipper_id = s.id)"
251+"SELECT \"o\".\"price\" AS \"order_price\", \"c\".\"name\" AS \"customer_name\", \"s\".\"name\" AS \"shipper_name\" FROM \"orders\" AS \"o\" INNER JOIN \"customers\" AS \"c\" ON (\"o\".\"customer_id\" = \"c\".\"id\") INNER JOIN \"shippers\" AS \"s\" ON (\"o\".\"shipper_id\" = \"s\".\"id\")"
252252
253253self-join:
254254
@@ -262,7 +262,7 @@ let
262262 (from (table (#tab `As` #t1) & crossJoin (table (#tab `As` #t2))))
263263in renderQuery query
264264:}
265−"SELECT t1.* FROM tab AS t1 CROSS JOIN tab AS t2"
265+"SELECT \"t1\".* FROM \"tab\" AS \"t1\" CROSS JOIN \"tab\" AS \"t2\""
266266
267267set operations:
268268
@@ -278,7 +278,7 @@ let
278278 selectStar (from (table (#tab `As` #t)))
279279in renderQuery query
280280:}
281−"(SELECT * FROM tab AS t) UNION ALL (SELECT * FROM tab AS t)"
281+"(SELECT * FROM \"tab\" AS \"t\") UNION ALL (SELECT * FROM \"tab\" AS \"t\")"
282282-}
283283newtype Query
284284 (schema :: TablesType)
squeal-postgresql/src/Squeal/PostgreSQL/Render.hs+5
@@ -22,6 +22,7 @@ module Squeal.PostgreSQL.Render
2222 parenthesized
2323 , (<+>)
2424 , commaSeparated
25+ , doubleQuoted
2526 , renderCommaSeparated
2627 , renderCommaSeparatedMaybe
2728 , renderNat
@@ -48,6 +49,10 @@ str1 <+> str2 = str1 <> " " <> str2
4849commaSeparated :: [ByteString] -> ByteString
4950commaSeparated = ByteString.intercalate ", "
5051
52+-- | Add double quotes around a `ByteString`.
53+doubleQuoted :: ByteString -> ByteString
54+doubleQuoted str = "\"" <> str <> "\""
55+
5156-- | Comma separate the renderings of a heterogeneous list.
5257renderCommaSeparated
5358 :: SListI xs
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+5−3
@@ -110,6 +110,8 @@ import GHC.TypeLits
110110
111111import qualified Generics.SOP.Type.Metadata as Type
112112
113+import Squeal.PostgreSQL.Render
114+
113115-- | `PGType` is the promoted datakind of PostgreSQL types.
114116--
115117-- >>> import Squeal.PostgreSQL.Schema
@@ -358,9 +360,9 @@ instance alias1 ~ alias2 => IsLabel alias1 (Alias alias2) where
358360 fromLabel = Alias
359361
360362-- | >>> renderAlias #jimbob
361−-- "jimbob"
363+-- "\"jimbob\""
362364renderAlias :: KnownSymbol alias => Alias alias -> ByteString
363−renderAlias = fromString . symbolVal
365+renderAlias = doubleQuoted . fromString . symbolVal
364366
365367-- | The `As` operator is used to name an expression. `As` is like a demoted
366368-- version of `:::`.
@@ -382,7 +384,7 @@ deriving instance Ord (expression ty)
382384
383385-- | >>> let renderMaybe = fromString . maybe "Nothing" (const "Just")
384386-- >>> renderAliasedAs renderMaybe (Just (3::Int) `As` #an_int)
385−-- "Just AS an_int"
387+-- "Just AS \"an_int\""
386388renderAliasedAs
387389 :: (forall ty. expression ty -> ByteString)
388390 -> Aliased expression aliased