on dev help sign in
fix tests

and other things

commit16b71bfdcfa7201feb28b4e3ce49572fc5be1fde authorEitan Chatav <eitan.chatav@gmail.com> dateFri 7 Dec 2018, 15:00 (8 years ago) parent8585d96
7 files changed+113−115
squeal-postgresql/src/Squeal/PostgreSQL.hs+2−2 squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+36−37 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+23−23 squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+20−18 squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+1−1 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+24−24 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+7−10
squeal-postgresql/src/Squeal/PostgreSQL.hs+2−2
@@ -123,7 +123,7 @@ let
123123 where
124124 u = insertInto #users
125125 (Values_ (defaultAs #id :* param @1 `as` #name))
126− OnConflictDoRaise (Returning (#id :* param @2 `as` #email))
126+ OnConflictDoRaise (Returning_ (#id :* param @2 `as` #email))
127127 e = insertInto_ #emails
128128 (Select (defaultAs #id :* #u ! #id `as` #user_id :* #u ! #email) (from (common #u)))
129129:}
@@ -141,7 +141,7 @@ let
141141 getUsers :: Query DB '[]
142142 '[ "userName" ::: 'NotNull 'PGtext
143143 , "userEmail" ::: 'Null 'PGtext ]
144− getUsers = select
144+ getUsers = select_
145145 (#u ! #name `as` #userName :* #e ! #email `as` #userEmail)
146146 ( from (table (#users `as` #u)
147147 & innerJoin (table (#emails `as` #e))
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+36−37
@@ -29,7 +29,7 @@ Squeal data definition language.
2929
3030module Squeal.PostgreSQL.Definition
3131 ( -- * Definition
32− Definition (UnsafeDefinition, renderDefinition)
32+ Definition (..)
3333 , (>>>)
3434 -- * Tables
3535 -- ** Create
@@ -43,23 +43,21 @@ module Squeal.PostgreSQL.Definition
4343 , primaryKey
4444 , foreignKey
4545 , ForeignKeyed
46− , OnDeleteClause (OnDeleteNoAction, OnDeleteRestrict, OnDeleteCascade)
47− , renderOnDeleteClause
48− , OnUpdateClause (OnUpdateNoAction, OnUpdateRestrict, OnUpdateCascade)
49− , renderOnUpdateClause
46+ , OnDeleteClause (..)
47+ , OnUpdateClause (..)
5048 -- ** Drop
5149 , dropTable
5250 -- ** Alter
5351 , alterTable
5452 , alterTableRename
55− , AlterTable (UnsafeAlterTable, renderAlterTable)
53+ , AlterTable (..)
5654 , addConstraint
5755 , dropConstraint
58− , AddColumn (addColumn)
56+ , AddColumn (..)
5957 , dropColumn
6058 , renameColumn
6159 , alterColumn
62− , AlterColumn (UnsafeAlterColumn, renderAlterColumn)
60+ , AlterColumn (..)
6361 , setDefault
6462 , dropDefault
6563 , setNotNull
@@ -132,7 +130,7 @@ createSchema
132130 => Alias sch
133131 -> Definition schemas (Create sch '[] schemas)
134132createSchema sch = UnsafeDefinition $
135− "CREATE" <+> "SCHEMA" <+> renderAlias sch <> ";"
133+ "CREATE" <+> "SCHEMA" <+> renderSQL sch <> ";"
136134
137135createSchemaIfNotExists
138136 :: (KnownSymbol sch, Has sch schemas schema)
@@ -140,7 +138,7 @@ createSchemaIfNotExists
140138 -> Definition schemas schemas
141139createSchemaIfNotExists sch = UnsafeDefinition $
142140 "CREATE" <+> "SCHEMA" <+> "IF" <+> "NOT" <+> "EXISTS"
143− <+> renderAlias sch <> ";"
141+ <+> renderSQL sch <> ";"
144142
145143{- | `createTable` adds a table to the schema.
146144
@@ -238,12 +236,12 @@ renderCreation tab columns constraints = renderQualifiedAlias tab
238236 where
239237 renderColumnDef :: Aliased (ColumnTypeExpression schemas) x -> ByteString
240238 renderColumnDef (ty `As` column) =
241− renderAlias column <+> renderColumnTypeExpression ty
239+ renderSQL column <+> renderColumnTypeExpression ty
242240 renderConstraint
243241 :: Aliased (TableConstraintExpression sch tab schemas) constraint
244242 -> ByteString
245243 renderConstraint (constraint `As` alias) =
246− "CONSTRAINT" <+> renderAlias alias <+> renderTableConstraintExpression constraint
244+ "CONSTRAINT" <+> renderSQL alias <+> renderTableConstraintExpression constraint
247245
248246-- | Data types are a way to limit the kind of data that can be stored in a
249247-- table. For many applications, however, the constraint they provide is
@@ -463,10 +461,10 @@ foreignKey
463461 ('ForeignKey columns parent refcolumns)
464462foreignKey keys parent refs ondel onupd = UnsafeTableConstraintExpression $
465463 "FOREIGN KEY" <+> parenthesized (commaSeparated (renderAliases keys))
466− <+> "REFERENCES" <+> renderAlias parent
464+ <+> "REFERENCES" <+> renderSQL parent
467465 <+> parenthesized (commaSeparated (renderAliases refs))
468− <+> renderOnDeleteClause ondel
469− <+> renderOnUpdateClause onupd
466+ <+> renderSQL ondel
467+ <+> renderSQL onupd
470468
471469-- | A constraint synonym between types involved in a foreign key constraint.
472470type ForeignKeyed schemas
@@ -497,13 +495,12 @@ data OnDeleteClause
497495 -- row(s) referencing it should be automatically deleted as well
498496 deriving (GHC.Generic,Show,Eq,Ord)
499497instance NFData OnDeleteClause
500−
501498-- | Render `OnDeleteClause`.
502−renderOnDeleteClause :: OnDeleteClause -> ByteString
503−renderOnDeleteClause = \case
504− OnDeleteNoAction -> "ON DELETE NO ACTION"
505− OnDeleteRestrict -> "ON DELETE RESTRICT"
506− OnDeleteCascade -> "ON DELETE CASCADE"
499+instance RenderSQL OnDeleteClause where
500+ renderSQL = \case
501+ OnDeleteNoAction -> "ON DELETE NO ACTION"
502+ OnDeleteRestrict -> "ON DELETE RESTRICT"
503+ OnDeleteCascade -> "ON DELETE CASCADE"
507504
508505-- | Analagous to `OnDeleteClause` there is also `OnUpdateClause` which is invoked
509506-- when a referenced column is changed (updated).
@@ -519,11 +516,11 @@ data OnUpdateClause
519516instance NFData OnUpdateClause
520517
521518-- | Render `OnUpdateClause`.
522−renderOnUpdateClause :: OnUpdateClause -> ByteString
523−renderOnUpdateClause = \case
524− OnUpdateNoAction -> "ON UPDATE NO ACTION"
525− OnUpdateRestrict -> "ON UPDATE RESTRICT"
526− OnUpdateCascade -> "ON UPDATE CASCADE"
519+instance RenderSQL OnUpdateClause where
520+ renderSQL = \case
521+ OnUpdateNoAction -> "ON UPDATE NO ACTION"
522+ OnUpdateRestrict -> "ON UPDATE RESTRICT"
523+ OnUpdateCascade -> "ON UPDATE CASCADE"
527524
528525{-----------------------------------------
529526DROP statements
@@ -572,8 +569,8 @@ alterTableRename
572569 -> Alias table1 -- ^ what to rename it
573570 -> Definition schema (Rename table0 table1 schema)
574571alterTableRename table0 table1 = UnsafeDefinition $
575− "ALTER TABLE" <+> renderAlias table0
576− <+> "RENAME TO" <+> renderAlias table1 <> ";"
572+ "ALTER TABLE" <+> renderSQL table0
573+ <+> "RENAME TO" <+> renderSQL table1 <> ";"
577574
578575-- | An `AlterTable` describes the alteration to perform on the columns
579576-- of a table.
@@ -607,7 +604,7 @@ addConstraint
607604 -- ^ constraint to add
608605 -> AlterTable sch tab schemas table1
609606addConstraint alias constraint = UnsafeAlterTable $
610− "ADD" <+> "CONSTRAINT" <+> renderAlias alias
607+ "ADD" <+> "CONSTRAINT" <+> renderSQL alias
611608 <+> renderTableConstraintExpression constraint
612609
613610-- | A `dropConstraint` drops a table constraint.
@@ -631,7 +628,7 @@ dropConstraint
631628 -- ^ constraint to drop
632629 -> AlterTable sch tab schemas table1
633630dropConstraint constraint = UnsafeAlterTable $
634− "DROP" <+> "CONSTRAINT" <+> renderAlias constraint
631+ "DROP" <+> "CONSTRAINT" <+> renderSQL constraint
635632
636633-- | An `AddColumn` is either @NULL@ or has @DEFAULT@.
637634class AddColumn ty where
@@ -670,7 +667,7 @@ class AddColumn ty where
670667 -> ColumnTypeExpression schemas ty -- ^ type of the new column
671668 -> AlterTable sch tab schemas (constraints :=> Create column ty columns)
672669 addColumn column ty = UnsafeAlterTable $
673− "ADD COLUMN" <+> renderAlias column <+> renderColumnTypeExpression ty
670+ "ADD COLUMN" <+> renderSQL column <+> renderColumnTypeExpression ty
674671instance {-# OVERLAPPING #-} AddColumn ('Def :=> ty)
675672instance {-# OVERLAPPABLE #-} AddColumn ('NoDef :=> 'Null ty)
676673
@@ -699,7 +696,7 @@ dropColumn
699696 => Alias column -- ^ column to remove
700697 -> AlterTable sch tab schemas table1
701698dropColumn column = UnsafeAlterTable $
702− "DROP COLUMN" <+> renderAlias column
699+ "DROP COLUMN" <+> renderSQL column
703700
704701-- | A `renameColumn` renames a column.
705702--
@@ -723,7 +720,7 @@ renameColumn
723720 -> Alias column1 -- ^ what to rename the column
724721 -> AlterTable sch tab schemas table1
725722renameColumn column0 column1 = UnsafeAlterTable $
726− "RENAME COLUMN" <+> renderAlias column0 <+> "TO" <+> renderAlias column1
723+ "RENAME COLUMN" <+> renderSQL column0 <+> "TO" <+> renderSQL column1
727724
728725-- | An `alterColumn` alters a single column.
729726alterColumn
@@ -737,7 +734,7 @@ alterColumn
737734 -> AlterColumn schemas ty0 ty1 -- ^ alteration to perform
738735 -> AlterTable sch tab schemas table1
739736alterColumn column alteration = UnsafeAlterTable $
740− "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
737+ "ALTER COLUMN" <+> renderSQL column <+> renderAlterColumn alteration
741738
742739-- | An `AlterColumn` describes the alteration to perform on a single column.
743740newtype AlterColumn (schemas :: SchemasType) (ty0 :: ColumnType) (ty1 :: ColumnType) =
@@ -835,7 +832,7 @@ let
835832 '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC)]]
836833 '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC), "bc" ::: 'View BC]]
837834 definition =
838− createView #bc (select (#b :* #c) (from (table #abc)))
835+ createView #bc (select_ (#b :* #c) (from (table #abc)))
839836in printSQL definition
840837:}
841838CREATE VIEW "bc" AS SELECT "b" AS "b", "c" AS "c" FROM "abc" AS "abc";
@@ -938,7 +935,7 @@ createTypeComposite ty fields = UnsafeDefinition $
938935 where
939936 renderField :: Aliased (TypeExpression schemas) x -> ByteString
940937 renderField (typ `As` alias) =
941− renderAlias alias <+> renderTypeExpression typ
938+ renderSQL alias <+> renderTypeExpression typ
942939
943940-- | Composite types can also be generated from a Haskell type, for example
944941--
@@ -990,6 +987,8 @@ dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderQualifiedAlias t
990987newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
991988 = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }
992989 deriving (GHC.Generic,Show,Eq,Ord,NFData)
990+instance RenderSQL (ColumnTypeExpression schemas ty) where
991+ renderSQL = renderColumnTypeExpression
993992
994993-- | used in `createTable` commands as a column constraint to note that
995994-- @NULL@ may be present in a column
@@ -1011,7 +1010,7 @@ default_
10111010 -> ColumnTypeExpression schemas ('NoDef :=> ty)
10121011 -> ColumnTypeExpression schemas ('Def :=> ty)
10131012default_ x ty = UnsafeColumnTypeExpression $
1014− renderColumnTypeExpression ty <+> "DEFAULT" <+> renderExpression x
1013+ renderSQL ty <+> "DEFAULT" <+> renderExpression x
10151014
10161015-- | not a true type, but merely a notational convenience for creating
10171016-- unique identifier columns with type `PGint2`
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+23−23
@@ -244,7 +244,7 @@ class KnownNat n => HasParameter
244244 | n params -> ty where
245245 -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
246246 --
247− -- >>> let expr = parameter @1 int4 :: Expression (DBof schemas) from grp '[ 'Null 'PGint4] ('Null 'PGint4)
247+ -- >>> let expr = parameter @1 int4 :: Expression (DBof schemas) '[ 'Null 'PGint4] grp from ('Null 'PGint4)
248248 -- >>> printSQL expr
249249 -- ($1 :: int4)
250250 parameter
@@ -260,7 +260,7 @@ instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
260260-- | `param` takes a `Nat` using type application and for basic types,
261261-- infers a `TypeExpression`.
262262--
263−-- >>> let expr = param @1 :: Expression (commons :=> schemas) from grp '[ 'Null 'PGint4] ('Null 'PGint4)
263+-- >>> let expr = param @1 :: Expression (commons :=> schemas) '[ 'Null 'PGint4] grp from ('Null 'PGint4)
264264-- >>> printSQL expr
265265-- ($1 :: int4)
266266param
@@ -271,7 +271,7 @@ param = parameter @n (pgtype @schemas)
271271
272272instance (HasUnique tab from row, Has col row ty)
273273 => IsLabel col (Expression db params 'Ungrouped from ty) where
274− fromLabel = UnsafeExpression $ renderAlias (Alias @col)
274+ fromLabel = UnsafeExpression $ renderSQL (Alias @col)
275275instance (HasUnique tab from row, Has col row ty, column ~ (col ::: ty))
276276 => IsLabel col
277277 (Aliased (Expression db params 'Ungrouped from) column) where
@@ -284,7 +284,7 @@ instance (HasUnique tab from row, Has col row ty, columns ~ '[col ::: ty])
284284instance (Has tab from row, Has col row ty)
285285 => IsQualified tab col (Expression db params 'Ungrouped from ty) where
286286 tab ! col = UnsafeExpression $
287− renderAlias tab <> "." <> renderAlias col
287+ renderSQL tab <> "." <> renderSQL col
288288instance (Has tab from row, Has col row ty, column ~ (col ::: ty))
289289 => IsQualified tab col
290290 (Aliased (Expression db params 'Ungrouped from) column) where
@@ -300,7 +300,7 @@ instance
300300 , GroupedBy tab col bys
301301 ) => IsLabel col
302302 (Expression db params ('Grouped bys) from ty) where
303− fromLabel = UnsafeExpression $ renderAlias (Alias @col)
303+ fromLabel = UnsafeExpression $ renderSQL (Alias @col)
304304instance
305305 ( HasUnique tab from row
306306 , Has col row ty
@@ -325,7 +325,7 @@ instance
325325 ) => IsQualified tab col
326326 (Expression db params ('Grouped bys) from ty) where
327327 tab ! col = UnsafeExpression $
328− renderAlias tab <> "." <> renderAlias col
328+ renderSQL tab <> "." <> renderSQL col
329329instance
330330 ( Has tab from row
331331 , Has col row ty
@@ -419,7 +419,7 @@ matchNull y f x = ifThenElse (isNull x) y
419419`nullIf` gives @NULL@.
420420
421421>>> :set -XTypeApplications -XDataKinds
422−>>> let expr = nullIf false (param @1) :: Expression (commons :=> schema) from grp '[ 'NotNull 'PGbool] ('Null 'PGbool)
422+>>> let expr = nullIf false (param @1) :: Expression (commons :=> schema) '[ 'NotNull 'PGbool] grp from ('Null 'PGbool)
423423>>> printSQL expr
424424NULL IF (FALSE, ($1 :: bool))
425425-}
@@ -495,7 +495,7 @@ field
495495 -> Expression (commons :=> schemas) params grp from ty
496496field td fld expr = UnsafeExpression $
497497 parenthesized (renderExpression expr <> "::" <> renderQualifiedAlias td)
498− <> "." <> renderAlias fld
498+ <> "." <> renderSQL fld
499499
500500instance Semigroup
501501 (Expression db params grp from (nullity ('PGvararray ty))) where
@@ -506,7 +506,7 @@ instance Monoid
506506 mempty = array []
507507 mappend = (<>)
508508
509−-- | >>> let expr = greatest currentTimestamp [param @1] :: Expression (commons :=> schemas) rels grp '[ 'NotNull 'PGtimestamptz] ('NotNull 'PGtimestamptz)
509+-- | >>> let expr = greatest currentTimestamp [param @1] :: Expression (commons :=> schemas) '[ 'NotNull 'PGtimestamptz] grp from ('NotNull 'PGtimestamptz)
510510-- >>> printSQL expr
511511-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
512512greatest
@@ -1393,7 +1393,7 @@ unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
13931393
13941394-- | >>> :{
13951395-- let
1396−-- expression :: Expression db '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Grouped bys) params ('Null 'PGnumeric)
1396+-- expression :: Expression db params ('Grouped bys) '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
13971397-- expression = sum_ #col
13981398-- in printSQL expression
13991399-- :}
@@ -1407,7 +1407,7 @@ sum_ = unsafeAggregate "sum"
14071407
14081408-- | >>> :{
14091409-- let
1410−-- expression :: Expression db '[tab ::: '["col" ::: nullity 'PGnumeric]] ('Grouped bys) params (nullity 'PGnumeric)
1410+-- expression :: Expression db params ('Grouped bys) '[tab ::: '["col" ::: nullity 'PGnumeric]] (nullity 'PGnumeric)
14111411-- expression = sumDistinct #col
14121412-- in printSQL expression
14131413-- :}
@@ -1438,7 +1438,7 @@ instance PGAvg 'PGinterval 'PGinterval
14381438
14391439-- | >>> :{
14401440-- let
1441−-- expression :: Expression db '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
1441+-- expression :: Expression db params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
14421442-- expression = bitAnd #col
14431443-- in printSQL expression
14441444-- :}
@@ -1452,7 +1452,7 @@ bitAnd = unsafeAggregate "bit_and"
14521452
14531453-- | >>> :{
14541454-- let
1455−-- expression :: Expression db '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
1455+-- expression :: Expression db params ('Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
14561456-- expression = bitOr #col
14571457-- in printSQL expression
14581458-- :}
@@ -1466,7 +1466,7 @@ bitOr = unsafeAggregate "bit_or"
14661466
14671467-- | >>> :{
14681468-- let
1469−-- expression :: Expression db '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
1469+-- expression :: Expression db params ('Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
14701470-- expression = bitAndDistinct #col
14711471-- in printSQL expression
14721472-- :}
@@ -1480,7 +1480,7 @@ bitAndDistinct = unsafeAggregateDistinct "bit_and"
14801480
14811481-- | >>> :{
14821482-- let
1483−-- expression :: Expression db '[tab ::: '["col" ::: nullity 'PGint4]] (Grouped bys) params (nullity 'PGint4)
1483+-- expression :: Expression db params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
14841484-- expression = bitOrDistinct #col
14851485-- in printSQL expression
14861486-- :}
@@ -1494,7 +1494,7 @@ bitOrDistinct = unsafeAggregateDistinct "bit_or"
14941494
14951495-- | >>> :{
14961496-- let
1497−-- expression :: Expression db '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1497+-- expression :: Expression db params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
14981498-- expression = boolAnd #col
14991499-- in printSQL expression
15001500-- :}
@@ -1507,7 +1507,7 @@ boolAnd = unsafeAggregate "bool_and"
15071507
15081508-- | >>> :{
15091509-- let
1510−-- expression :: Expression db '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1510+-- expression :: Expression db params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
15111511-- expression = boolOr #col
15121512-- in printSQL expression
15131513-- :}
@@ -1520,7 +1520,7 @@ boolOr = unsafeAggregate "bool_or"
15201520
15211521-- | >>> :{
15221522-- let
1523−-- expression :: Expression db '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1523+-- expression :: Expression db params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
15241524-- expression = boolAndDistinct #col
15251525-- in printSQL expression
15261526-- :}
@@ -1533,7 +1533,7 @@ boolAndDistinct = unsafeAggregateDistinct "bool_and"
15331533
15341534-- | >>> :{
15351535-- let
1536−-- expression :: Expression db '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1536+-- expression :: Expression db params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
15371537-- expression = boolOrDistinct #col
15381538-- in printSQL expression
15391539-- :}
@@ -1554,7 +1554,7 @@ countStar = UnsafeExpression $ "count(*)"
15541554
15551555-- | >>> :{
15561556-- let
1557−-- expression :: Expression db '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
1557+-- expression :: Expression db params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
15581558-- expression = count #col
15591559-- in printSQL expression
15601560-- :}
@@ -1567,7 +1567,7 @@ count = unsafeAggregate "count"
15671567
15681568-- | >>> :{
15691569-- let
1570−-- expression :: Expression db '[tab ::: '["col" ::: nullity ty]] (Grouped bys) params ('NotNull 'PGint8)
1570+-- expression :: Expression db params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
15711571-- expression = countDistinct #col
15721572-- in printSQL expression
15731573-- :}
@@ -1582,7 +1582,7 @@ countDistinct = unsafeAggregateDistinct "count"
15821582--
15831583-- >>> :{
15841584-- let
1585−-- expression :: Expression db '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1585+-- expression :: Expression db params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
15861586-- expression = every #col
15871587-- in printSQL expression
15881588-- :}
@@ -1597,7 +1597,7 @@ every = unsafeAggregate "every"
15971597--
15981598-- >>> :{
15991599-- let
1600−-- expression :: Expression db '[tab ::: '["col" ::: nullity 'PGbool]] (Grouped bys) params (nullity 'PGbool)
1600+-- expression :: Expression db params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
16011601-- expression = everyDistinct #col
16021602-- in printSQL expression
16031603-- :}
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+20−18
@@ -123,7 +123,7 @@ let
123123 (Values_ ("John Smith" `as` #name :* "john@smith.com" `as` #email))
124124 (OnConflict (OnConstraint #uq)
125125 (DoUpdate (((#excluded ! #email) <> "; " <> (#customers ! #email)) `as` #email) []))
126− (Returning Nil)
126+ (Returning_ Nil)
127127in printSQL manipulation
128128:}
129129INSERT INTO "customers" ("name", "email") VALUES (E'John Smith', E'john@smith.com') ON CONFLICT ON CONSTRAINT "uq" DO UPDATE SET "email" = ("excluded"."email" || (E'; ' || "customers"."email"))
@@ -133,7 +133,7 @@ query insert:
133133>>> :{
134134let
135135 manipulation :: Manipulation (DBof (Public Schema)) '[] '[]
136− manipulation = insertInto_ #tab (Subquery (selectStar (from (table #tab))))
136+ manipulation = insertInto_ #tab (Subquery (select Star (from (table #tab))))
137137in printSQL manipulation
138138:}
139139INSERT INTO "tab" SELECT * FROM "tab" AS "tab"
@@ -155,7 +155,7 @@ let
155155 manipulation :: Manipulation (DBof (Public Schema)) '[]
156156 '[ "col1" ::: 'NotNull 'PGint4
157157 , "col2" ::: 'NotNull 'PGint4 ]
158− manipulation = deleteFrom #tab NoUsing (#col1 .== #col2) ReturningStar
158+ manipulation = deleteFrom #tab NoUsing (#col1 .== #col2) (Returning Star)
159159in printSQL manipulation
160160:}
161161DELETE FROM "tab" WHERE ("col1" = "col2") RETURNING *
@@ -188,8 +188,8 @@ with manipulation:
188188let
189189 manipulation :: Manipulation (DBof (Public ProductsSchema)) '[ 'NotNull 'PGdate] '[]
190190 manipulation = with
191− (deleteFrom #products NoUsing (#date .< param @1) ReturningStar `as` #del)
192− (insertInto_ #products_deleted (Subquery (selectStar (from (common #del)))))
191+ (deleteFrom #products NoUsing (#date .< param @1) (Returning Star) `as` #del)
192+ (insertInto_ #products_deleted (Subquery (select Star (from (common #del)))))
193193in printSQL manipulation
194194:}
195195WITH "del" AS (DELETE FROM "products" WHERE ("date" < ($1 :: date)) RETURNING *) INSERT INTO "products_deleted" SELECT * FROM "del" AS "del"
@@ -279,31 +279,31 @@ renderQueryClause
279279 -> ByteString
280280renderQueryClause = \case
281281 Values row0 rows ->
282− parenthesized (renderCommaSeparated renderAliasPart row0)
282+ parenthesized (renderCommaSeparated renderSQLPart row0)
283283 <+> "VALUES"
284284 <+> commaSeparated
285285 ( parenthesized
286286 . renderCommaSeparated renderValuePart <$> row0 : rows )
287287 Select row0 tab ->
288− parenthesized (renderCommaSeparatedMaybe renderAliasPartMaybe row0)
288+ parenthesized (renderCommaSeparatedMaybe renderSQLPartMaybe row0)
289289 <+> "SELECT"
290290 <+> renderCommaSeparatedMaybe renderValuePartMaybe row0
291291 <+> renderTableExpression tab
292292 Subquery qry -> renderQuery qry
293293 where
294− renderAliasPartMaybe, renderValuePartMaybe
294+ renderSQLPartMaybe, renderValuePartMaybe
295295 :: ColumnExpression db params grp from column -> Maybe ByteString
296− renderAliasPartMaybe = \case
296+ renderSQLPartMaybe = \case
297297 DefaultAs _ -> Nothing
298− Specific (_ `As` name) -> Just $ renderAlias name
298+ Specific (_ `As` name) -> Just $ renderSQL name
299299 renderValuePartMaybe = \case
300300 DefaultAs _ -> Nothing
301301 Specific (value `As` _) -> Just $ renderExpression value
302− renderAliasPart, renderValuePart
302+ renderSQLPart, renderValuePart
303303 :: ColumnExpression db params grp from column -> ByteString
304− renderAliasPart = \case
305− DefaultAs name -> renderAlias name
306− Specific (_ `As` name) -> renderAlias name
304+ renderSQLPart = \case
305+ DefaultAs name -> renderSQL name
306+ Specific (_ `As` name) -> renderSQL name
307307 renderValuePart = \case
308308 DefaultAs _ -> "DEFAULT"
309309 Specific (value `As` _) -> renderExpression value
@@ -394,9 +394,9 @@ renderColumnExpression
394394 -> ByteString
395395renderColumnExpression = \case
396396 DefaultAs col ->
397− renderAlias col <+> "=" <+> "DEFAULT"
397+ renderSQL col <+> "=" <+> "DEFAULT"
398398 Specific (expression `As` col) ->
399− renderAlias col <+> "=" <+> renderExpression expression
399+ renderSQL col <+> "=" <+> renderExpression expression
400400
401401-- | A `ReturningClause` computes and return value(s) based
402402-- on each row actually inserted, updated or deleted. This is primarily
@@ -412,7 +412,9 @@ newtype ReturningClause db params from row =
412412 Returning (Selection db params 'Ungrouped from row)
413413
414414instance RenderSQL (ReturningClause db params from row) where
415− renderSQL (Returning selection) = "RETURNING" <+> renderSQL selection
415+ renderSQL = \case
416+ Returning (List Nil) -> ""
417+ Returning selection -> " RETURNING" <+> renderSQL selection
416418
417419pattern Returning_
418420 :: SOP.SListI row
@@ -479,7 +481,7 @@ renderConflictTarget
479481 :: ConflictTarget constraints
480482 -> ByteString
481483renderConflictTarget (OnConstraint con) =
482− "ON" <+> "CONSTRAINT" <+> renderAlias con
484+ "ON" <+> "CONSTRAINT" <+> renderSQL con
483485
484486{-----------------------------------------
485487UPDATE statements
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+1−1
@@ -71,7 +71,7 @@ let
7171 :: Has "migrations" schemas MigrationsSchema
7272 => PQ schemas schemas IO ()
7373 numMigrations = do
74− result <- runQuery (selectStar (from (table (#migrations ! #schema_migrations `as` #m))))
74+ result <- runQuery (select Star (from (table (#migrations ! #schema_migrations `as` #m))))
7575 num <- ntuples result
7676 liftBase $ print num
7777:}
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+24−24
@@ -151,7 +151,7 @@ simple query:
151151>>> :{
152152let
153153 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
154− query = selectStar (from (table #tab))
154+ query = select Star (from (table #tab))
155155in printSQL query
156156:}
157157SELECT * FROM "tab" AS "tab"
@@ -162,7 +162,7 @@ restricted query:
162162let
163163 query :: Query (DBof (Public Schema)) '[] '["sum" ::: 'NotNull 'PGint4, "col1" ::: 'NotNull 'PGint4]
164164 query =
165− select ((#col1 + #col2) `as` #sum :* #col1)
165+ select_ ((#col1 + #col2) `as` #sum :* #col1)
166166 ( from (table #tab)
167167 & where_ (#col1 .> #col2)
168168 & where_ (#col2 .> 0) )
@@ -175,7 +175,7 @@ subquery:
175175>>> :{
176176let
177177 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
178− query = selectStar (from (subquery (selectStar (from (table #tab)) `as` #sub)))
178+ query = select Star (from (subquery (select Star (from (table #tab)) `as` #sub)))
179179in printSQL query
180180:}
181181SELECT * FROM (SELECT * FROM "tab" AS "tab") AS "sub"
@@ -185,7 +185,7 @@ limits and offsets:
185185>>> :{
186186let
187187 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
188− query = selectStar (from (table #tab) & limit 100 & offset 2 & limit 50 & offset 2)
188+ query = select Star (from (table #tab) & limit 100 & offset 2 & limit 50 & offset 2)
189189in printSQL query
190190:}
191191SELECT * FROM "tab" AS "tab" LIMIT 50 OFFSET 4
@@ -195,7 +195,7 @@ parameterized query:
195195>>> :{
196196let
197197 query :: Query (DBof (Public Schema)) '[ 'NotNull 'PGint4] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
198− query = selectStar (from (table #tab) & where_ (#col1 .> param @1))
198+ query = select Star (from (table #tab) & where_ (#col1 .> param @1))
199199in printSQL query
200200:}
201201SELECT * FROM "tab" AS "tab" WHERE ("col1" > ($1 :: int4))
@@ -206,7 +206,7 @@ aggregation query:
206206let
207207 query :: Query (DBof (Public Schema)) '[] '["sum" ::: 'NotNull 'PGint4, "col1" ::: 'NotNull 'PGint4 ]
208208 query =
209− select (sum_ #col2 `as` #sum :* #col1)
209+ select_ (sum_ #col2 `as` #sum :* #col1)
210210 ( from (table (#tab `as` #table1))
211211 & groupBy #col1
212212 & having (#col1 + sum_ #col2 .> 1) )
@@ -219,7 +219,7 @@ sorted query:
219219>>> :{
220220let
221221 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
222− query = selectStar (from (table #tab) & orderBy [#col1 & Asc])
222+ query = select Star (from (table #tab) & orderBy [#col1 & Asc])
223223in printSQL query
224224:}
225225SELECT * FROM "tab" AS "tab" ORDER BY "col1" ASC
@@ -261,7 +261,7 @@ let
261261 , "customer_name" ::: 'NotNull 'PGtext
262262 , "shipper_name" ::: 'NotNull 'PGtext
263263 ]
264− query = select
264+ query = select_
265265 ( #o ! #price `as` #order_price :*
266266 #c ! #name `as` #customer_name :*
267267 #s ! #name `as` #shipper_name )
@@ -279,7 +279,7 @@ self-join:
279279>>> :{
280280let
281281 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
282− query = selectDotStar #t1 (from (table (#tab `as` #t1) & crossJoin (table (#tab `as` #t2))))
282+ query = select (#t1 & DotStar) (from (table (#tab `as` #t1) & crossJoin (table (#tab `as` #t2))))
283283in printSQL query
284284:}
285285SELECT "t1".* FROM "tab" AS "t1" CROSS JOIN "tab" AS "t2"
@@ -299,7 +299,7 @@ set operations:
299299>>> :{
300300let
301301 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
302− query = selectStar (from (table #tab)) `unionAll` selectStar (from (table #tab))
302+ query = select Star (from (table #tab)) `unionAll` select Star (from (table #tab))
303303in printSQL query
304304:}
305305(SELECT * FROM "tab" AS "tab") UNION ALL (SELECT * FROM "tab" AS "tab")
@@ -310,9 +310,9 @@ with queries:
310310let
311311 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
312312 query = with (
313− selectStar (from (table #tab)) `as` #cte1 :>>
314− selectStar (from (common #cte1)) `as` #cte2
315− ) (selectStar (from (common #cte2)))
313+ select Star (from (table #tab)) `as` #cte1 :>>
314+ select Star (from (common #cte1)) `as` #cte2
315+ ) (select Star (from (common #cte2)))
316316in printSQL query
317317:}
318318WITH "cte1" AS (SELECT * FROM "tab" AS "tab"), "cte2" AS (SELECT * FROM "cte1" AS "cte1") SELECT * FROM "cte2" AS "cte2"
@@ -441,7 +441,7 @@ instance RenderSQL (Selection db params grp from row) where
441441 renderSQL = \case
442442 List list -> renderCommaSeparated (renderAliasedAs renderExpression) list
443443 Star -> "*"
444− DotStar tab -> renderAlias tab <> ".*"
444+ DotStar tab -> renderSQL tab <> ".*"
445445 Also right left -> renderSQL left <> ", " <> renderSQL right
446446
447447-- | the `TableExpression` in the `select` command constructs an intermediate
@@ -514,11 +514,11 @@ values rw rws = UnsafeQuery $ "SELECT * FROM"
514514 ( parenthesized
515515 . renderCommaSeparated renderValuePart <$> rw:rws )
516516 ) <+> "AS t"
517− <+> parenthesized (renderCommaSeparated renderAliasPart rw)
517+ <+> parenthesized (renderCommaSeparated renderSQLPart rw)
518518 where
519− renderAliasPart, renderValuePart
519+ renderSQLPart, renderValuePart
520520 :: Aliased (Expression db params 'Ungrouped '[] ) ty -> ByteString
521− renderAliasPart (_ `As` name) = renderAlias name
521+ renderSQLPart (_ `As` name) = renderSQL name
522522 renderValuePart (value `As` _) = renderExpression value
523523
524524-- | `values_` computes a row value or set of row values
@@ -793,7 +793,7 @@ unsafeRecordFunction fun expr types = UnsafeQuery $
793793 where
794794 renderTy :: Aliased (TypeExpression db) ty -> ByteString
795795 renderTy (ty `As` alias) =
796− renderAlias alias <+> renderTypeExpression ty
796+ renderSQL alias <+> renderTypeExpression ty
797797
798798-- | Builds an arbitrary record from a JSON object.
799799jsonToRecord
@@ -845,7 +845,7 @@ table
845845 => Aliased (QualifiedAlias sch) (alias ::: tab)
846846 -> FromClause (commons :=> schemas) params '[alias ::: TableToRow table]
847847table (tab `As` alias) = UnsafeFromClause $
848− renderQualifiedAlias tab <+> "AS" <+> renderAlias alias
848+ renderQualifiedAlias tab <+> "AS" <+> renderSQL alias
849849
850850-- | `subquery` derives a table from a `Query`.
851851subquery
@@ -859,14 +859,14 @@ view
859859 => Aliased (QualifiedAlias sch) (alias ::: vw)
860860 -> FromClause (commons :=> schemas) params '[alias ::: view]
861861view (vw `As` alias) = UnsafeFromClause $
862− renderQualifiedAlias vw <+> "AS" <+> renderAlias alias
862+ renderQualifiedAlias vw <+> "AS" <+> renderSQL alias
863863
864864common
865865 :: Has cte commons common
866866 => Aliased Alias (alias ::: cte)
867867 -> FromClause (commons :=> schemas) params '[alias ::: common]
868868common (cte `As` alias) = UnsafeFromClause $
869− renderAlias cte <+> "AS" <+> renderAlias alias
869+ renderSQL cte <+> "AS" <+> renderSQL alias
870870
871871{- | @left & crossJoin right@. For every possible combination of rows from
872872 @left@ and @right@ (i.e., a Cartesian product), the joined table will contain
@@ -991,8 +991,8 @@ instance (Has rel rels cols, Has col cols ty, bys ~ '[ '(rel, col)])
991991-- | Renders a `By`.
992992renderBy :: By from by -> ByteString
993993renderBy = \case
994− By1 column -> renderAlias column
995− By2 rel column -> renderAlias rel <> "." <> renderAlias column
994+ By1 column -> renderSQL column
995+ By2 rel column -> renderSQL rel <> "." <> renderSQL column
996996
997997-- | A `GroupByClause` indicates the `Grouping` of a `TableExpression`.
998998-- A `NoGroups` indicates `Ungrouped` while a `Group` indicates `Grouped`.
@@ -1380,7 +1380,7 @@ renderCommonTableExpression
13801380 -> CommonTableExpression statement params db0 db1 -> ByteString
13811381renderCommonTableExpression renderStatement
13821382 (CommonTableExpression (statement `As` alias)) =
1383− renderAlias alias <+> "AS" <+> parenthesized (renderStatement statement)
1383+ renderSQL alias <+> "AS" <+> parenthesized (renderStatement statement)
13841384
13851385-- | render a non-empty `AlignedList` of `CommonTableExpression`s.
13861386renderCommonTableExpressions
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+7−10
@@ -68,7 +68,6 @@ module Squeal.PostgreSQL.Schema
6868 , (:::)
6969 , Alias (..)
7070 , IsLabel (..)
71− , renderAlias
7271 , renderAliasString
7372 , renderAliases
7473 , Aliased (As)
@@ -377,12 +376,10 @@ instance alias1 ~ alias2 => IsLabel alias1 (Alias alias2) where
377376 fromLabel = Alias
378377instance aliases ~ '[alias] => IsLabel alias (NP Alias aliases) where
379378 fromLabel = fromLabel :* Nil
380−instance KnownSymbol alias => RenderSQL (Alias alias) where renderSQL = renderAlias
381−
382−-- | >>> renderAlias #jimbob
379+-- | >>> renderSQL #jimbob
383380-- "\"jimbob\""
384−renderAlias :: KnownSymbol alias => Alias alias -> ByteString
385−renderAlias = doubleQuoted . fromString . symbolVal
381+instance KnownSymbol alias => RenderSQL (Alias alias) where
382+ renderSQL = doubleQuoted . fromString . symbolVal
386383
387384-- | >>> renderAliasString #ohmahgerd
388385-- "'ohmahgerd'"
@@ -395,7 +392,7 @@ renderAliasString = singleQuotedText . fromString . symbolVal
395392renderAliases
396393 :: All KnownSymbol aliases => NP Alias aliases -> [ByteString]
397394renderAliases = hcollapse
398− . hcmap (Proxy @KnownSymbol) (K . renderAlias)
395+ . hcmap (Proxy @KnownSymbol) (K . renderSQL)
399396
400397-- | The `As` operator is used to name an expression. `As` is like a demoted
401398-- version of `:::`.
@@ -443,7 +440,7 @@ renderAliasedAs
443440 -> Aliased expression aliased
444441 -> ByteString
445442renderAliasedAs render (expression `As` alias) =
446− render expression <> " AS " <> renderAlias alias
443+ render expression <> " AS " <> renderSQL alias
447444
448445-- | @HasUnique alias fields field@ is a constraint that proves that
449446-- @fields@ is a singleton of @alias ::: field@.
@@ -519,8 +516,8 @@ renderQualifiedAlias
519516 => QualifiedAlias q a -> ByteString
520517renderQualifiedAlias _ =
521518 let
522− qualifier = renderAlias (Alias @q)
523− alias = renderAlias (Alias @a)
519+ qualifier = renderSQL (Alias @q)
520+ alias = renderSQL (Alias @a)
524521 in
525522 if qualifier == "\"public\"" then alias else qualifier <> "." <> alias
526523