on dev help sign in
fix tests
16b71bfEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL.hs+2−2
@@ -123,7 +123,7 @@ let
123 where
123 where
124 u = insertInto #users
124 u = insertInto #users
125 (Values_ (defaultAs #id :* param @1 `as` #name))
125 (Values_ (defaultAs #id :* param @1 `as` #name))
126− OnConflictDoRaise (Returning (#id :* param @2 `as` #email))
126+ OnConflictDoRaise (Returning_ (#id :* param @2 `as` #email))
127 e = insertInto_ #emails
127 e = insertInto_ #emails
128 (Select (defaultAs #id :* #u ! #id `as` #user_id :* #u ! #email) (from (common #u)))
128 (Select (defaultAs #id :* #u ! #id `as` #user_id :* #u ! #email) (from (common #u)))
129:}
129:}
@@ -141,7 +141,7 @@ let
141 getUsers :: Query DB '[]
141 getUsers :: Query DB '[]
142 '[ "userName" ::: 'NotNull 'PGtext
142 '[ "userName" ::: 'NotNull 'PGtext
143 , "userEmail" ::: 'Null 'PGtext ]
143 , "userEmail" ::: 'Null 'PGtext ]
144− getUsers = select
144+ getUsers = select_
145 (#u ! #name `as` #userName :* #e ! #email `as` #userEmail)
145 (#u ! #name `as` #userName :* #e ! #email `as` #userEmail)
146 ( from (table (#users `as` #u)
146 ( from (table (#users `as` #u)
147 & innerJoin (table (#emails `as` #e))
147 & innerJoin (table (#emails `as` #e))
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+36−37
@@ -29,7 +29,7 @@ Squeal data definition language.
29
29
30module Squeal.PostgreSQL.Definition
30module Squeal.PostgreSQL.Definition
31 ( -- * Definition
31 ( -- * Definition
32− Definition (UnsafeDefinition, renderDefinition)
32+ Definition (..)
33 , (>>>)
33 , (>>>)
34 -- * Tables
34 -- * Tables
35 -- ** Create
35 -- ** Create
@@ -43,23 +43,21 @@ module Squeal.PostgreSQL.Definition
43 , primaryKey
43 , primaryKey
44 , foreignKey
44 , foreignKey
45 , ForeignKeyed
45 , ForeignKeyed
46− , OnDeleteClause (OnDeleteNoAction, OnDeleteRestrict, OnDeleteCascade)
46+ , OnDeleteClause (..)
47− , renderOnDeleteClause
47+ , OnUpdateClause (..)
48− , OnUpdateClause (OnUpdateNoAction, OnUpdateRestrict, OnUpdateCascade)
49− , renderOnUpdateClause
50 -- ** Drop
48 -- ** Drop
51 , dropTable
49 , dropTable
52 -- ** Alter
50 -- ** Alter
53 , alterTable
51 , alterTable
54 , alterTableRename
52 , alterTableRename
55− , AlterTable (UnsafeAlterTable, renderAlterTable)
53+ , AlterTable (..)
56 , addConstraint
54 , addConstraint
57 , dropConstraint
55 , dropConstraint
58− , AddColumn (addColumn)
56+ , AddColumn (..)
59 , dropColumn
57 , dropColumn
60 , renameColumn
58 , renameColumn
61 , alterColumn
59 , alterColumn
62− , AlterColumn (UnsafeAlterColumn, renderAlterColumn)
60+ , AlterColumn (..)
63 , setDefault
61 , setDefault
64 , dropDefault
62 , dropDefault
65 , setNotNull
63 , setNotNull
@@ -132,7 +130,7 @@ createSchema
132 => Alias sch
130 => Alias sch
133 -> Definition schemas (Create sch '[] schemas)
131 -> Definition schemas (Create sch '[] schemas)
134createSchema sch = UnsafeDefinition $
132createSchema sch = UnsafeDefinition $
135− "CREATE" <+> "SCHEMA" <+> renderAlias sch <> ";"
133+ "CREATE" <+> "SCHEMA" <+> renderSQL sch <> ";"
136
134
137createSchemaIfNotExists
135createSchemaIfNotExists
138 :: (KnownSymbol sch, Has sch schemas schema)
136 :: (KnownSymbol sch, Has sch schemas schema)
@@ -140,7 +138,7 @@ createSchemaIfNotExists
140 -> Definition schemas schemas
138 -> Definition schemas schemas
141createSchemaIfNotExists sch = UnsafeDefinition $
139createSchemaIfNotExists sch = UnsafeDefinition $
142 "CREATE" <+> "SCHEMA" <+> "IF" <+> "NOT" <+> "EXISTS"
140 "CREATE" <+> "SCHEMA" <+> "IF" <+> "NOT" <+> "EXISTS"
143− <+> renderAlias sch <> ";"
141+ <+> renderSQL sch <> ";"
144
142
145{- | `createTable` adds a table to the schema.
143{- | `createTable` adds a table to the schema.
146
144
@@ -238,12 +236,12 @@ renderCreation tab columns constraints = renderQualifiedAlias tab
238 where
236 where
239 renderColumnDef :: Aliased (ColumnTypeExpression schemas) x -> ByteString
237 renderColumnDef :: Aliased (ColumnTypeExpression schemas) x -> ByteString
240 renderColumnDef (ty `As` column) =
238 renderColumnDef (ty `As` column) =
241− renderAlias column <+> renderColumnTypeExpression ty
239+ renderSQL column <+> renderColumnTypeExpression ty
242 renderConstraint
240 renderConstraint
243 :: Aliased (TableConstraintExpression sch tab schemas) constraint
241 :: Aliased (TableConstraintExpression sch tab schemas) constraint
244 -> ByteString
242 -> ByteString
245 renderConstraint (constraint `As` alias) =
243 renderConstraint (constraint `As` alias) =
246− "CONSTRAINT" <+> renderAlias alias <+> renderTableConstraintExpression constraint
244+ "CONSTRAINT" <+> renderSQL alias <+> renderTableConstraintExpression constraint
247
245
248-- | Data types are a way to limit the kind of data that can be stored in a
246-- | Data types are a way to limit the kind of data that can be stored in a
249-- table. For many applications, however, the constraint they provide is
247-- table. For many applications, however, the constraint they provide is
@@ -463,10 +461,10 @@ foreignKey
463 ('ForeignKey columns parent refcolumns)
461 ('ForeignKey columns parent refcolumns)
464foreignKey keys parent refs ondel onupd = UnsafeTableConstraintExpression $
462foreignKey keys parent refs ondel onupd = UnsafeTableConstraintExpression $
465 "FOREIGN KEY" <+> parenthesized (commaSeparated (renderAliases keys))
463 "FOREIGN KEY" <+> parenthesized (commaSeparated (renderAliases keys))
466− <+> "REFERENCES" <+> renderAlias parent
464+ <+> "REFERENCES" <+> renderSQL parent
467 <+> parenthesized (commaSeparated (renderAliases refs))
465 <+> parenthesized (commaSeparated (renderAliases refs))
468− <+> renderOnDeleteClause ondel
466+ <+> renderSQL ondel
469− <+> renderOnUpdateClause onupd
467+ <+> renderSQL onupd
470
468
471-- | A constraint synonym between types involved in a foreign key constraint.
469-- | A constraint synonym between types involved in a foreign key constraint.
472type ForeignKeyed schemas
470type ForeignKeyed schemas
@@ -497,13 +495,12 @@ data OnDeleteClause
497 -- row(s) referencing it should be automatically deleted as well
495 -- row(s) referencing it should be automatically deleted as well
498 deriving (GHC.Generic,Show,Eq,Ord)
496 deriving (GHC.Generic,Show,Eq,Ord)
499instance NFData OnDeleteClause
497instance NFData OnDeleteClause
500−
501-- | Render `OnDeleteClause`.
498-- | Render `OnDeleteClause`.
502−renderOnDeleteClause :: OnDeleteClause -> ByteString
499+instance RenderSQL OnDeleteClause where
503−renderOnDeleteClause = \case
500+ renderSQL = \case
504− OnDeleteNoAction -> "ON DELETE NO ACTION"
501+ OnDeleteNoAction -> "ON DELETE NO ACTION"
505− OnDeleteRestrict -> "ON DELETE RESTRICT"
502+ OnDeleteRestrict -> "ON DELETE RESTRICT"
506− OnDeleteCascade -> "ON DELETE CASCADE"
503+ OnDeleteCascade -> "ON DELETE CASCADE"
507
504
508-- | Analagous to `OnDeleteClause` there is also `OnUpdateClause` which is invoked
505-- | Analagous to `OnDeleteClause` there is also `OnUpdateClause` which is invoked
509-- when a referenced column is changed (updated).
506-- when a referenced column is changed (updated).
@@ -519,11 +516,11 @@ data OnUpdateClause
519instance NFData OnUpdateClause
516instance NFData OnUpdateClause
520
517
521-- | Render `OnUpdateClause`.
518-- | Render `OnUpdateClause`.
522−renderOnUpdateClause :: OnUpdateClause -> ByteString
519+instance RenderSQL OnUpdateClause where
523−renderOnUpdateClause = \case
520+ renderSQL = \case
524− OnUpdateNoAction -> "ON UPDATE NO ACTION"
521+ OnUpdateNoAction -> "ON UPDATE NO ACTION"
525− OnUpdateRestrict -> "ON UPDATE RESTRICT"
522+ OnUpdateRestrict -> "ON UPDATE RESTRICT"
526− OnUpdateCascade -> "ON UPDATE CASCADE"
523+ OnUpdateCascade -> "ON UPDATE CASCADE"
527
524
528{-----------------------------------------
525{-----------------------------------------
529DROP statements
526DROP statements
@@ -572,8 +569,8 @@ alterTableRename
572 -> Alias table1 -- ^ what to rename it
569 -> Alias table1 -- ^ what to rename it
573 -> Definition schema (Rename table0 table1 schema)
570 -> Definition schema (Rename table0 table1 schema)
574alterTableRename table0 table1 = UnsafeDefinition $
571alterTableRename table0 table1 = UnsafeDefinition $
575− "ALTER TABLE" <+> renderAlias table0
572+ "ALTER TABLE" <+> renderSQL table0
576− <+> "RENAME TO" <+> renderAlias table1 <> ";"
573+ <+> "RENAME TO" <+> renderSQL table1 <> ";"
577
574
578-- | An `AlterTable` describes the alteration to perform on the columns
575-- | An `AlterTable` describes the alteration to perform on the columns
579-- of a table.
576-- of a table.
@@ -607,7 +604,7 @@ addConstraint
607 -- ^ constraint to add
604 -- ^ constraint to add
608 -> AlterTable sch tab schemas table1
605 -> AlterTable sch tab schemas table1
609addConstraint alias constraint = UnsafeAlterTable $
606addConstraint alias constraint = UnsafeAlterTable $
610− "ADD" <+> "CONSTRAINT" <+> renderAlias alias
607+ "ADD" <+> "CONSTRAINT" <+> renderSQL alias
611 <+> renderTableConstraintExpression constraint
608 <+> renderTableConstraintExpression constraint
612
609
613-- | A `dropConstraint` drops a table constraint.
610-- | A `dropConstraint` drops a table constraint.
@@ -631,7 +628,7 @@ dropConstraint
631 -- ^ constraint to drop
628 -- ^ constraint to drop
632 -> AlterTable sch tab schemas table1
629 -> AlterTable sch tab schemas table1
633dropConstraint constraint = UnsafeAlterTable $
630dropConstraint constraint = UnsafeAlterTable $
634− "DROP" <+> "CONSTRAINT" <+> renderAlias constraint
631+ "DROP" <+> "CONSTRAINT" <+> renderSQL constraint
635
632
636-- | An `AddColumn` is either @NULL@ or has @DEFAULT@.
633-- | An `AddColumn` is either @NULL@ or has @DEFAULT@.
637class AddColumn ty where
634class AddColumn ty where
@@ -670,7 +667,7 @@ class AddColumn ty where
670 -> ColumnTypeExpression schemas ty -- ^ type of the new column
667 -> ColumnTypeExpression schemas ty -- ^ type of the new column
671 -> AlterTable sch tab schemas (constraints :=> Create column ty columns)
668 -> AlterTable sch tab schemas (constraints :=> Create column ty columns)
672 addColumn column ty = UnsafeAlterTable $
669 addColumn column ty = UnsafeAlterTable $
673− "ADD COLUMN" <+> renderAlias column <+> renderColumnTypeExpression ty
670+ "ADD COLUMN" <+> renderSQL column <+> renderColumnTypeExpression ty
674instance {-# OVERLAPPING #-} AddColumn ('Def :=> ty)
671instance {-# OVERLAPPING #-} AddColumn ('Def :=> ty)
675instance {-# OVERLAPPABLE #-} AddColumn ('NoDef :=> 'Null ty)
672instance {-# OVERLAPPABLE #-} AddColumn ('NoDef :=> 'Null ty)
676
673
@@ -699,7 +696,7 @@ dropColumn
699 => Alias column -- ^ column to remove
696 => Alias column -- ^ column to remove
700 -> AlterTable sch tab schemas table1
697 -> AlterTable sch tab schemas table1
701dropColumn column = UnsafeAlterTable $
698dropColumn column = UnsafeAlterTable $
702− "DROP COLUMN" <+> renderAlias column
699+ "DROP COLUMN" <+> renderSQL column
703
700
704-- | A `renameColumn` renames a column.
701-- | A `renameColumn` renames a column.
705--
702--
@@ -723,7 +720,7 @@ renameColumn
723 -> Alias column1 -- ^ what to rename the column
720 -> Alias column1 -- ^ what to rename the column
724 -> AlterTable sch tab schemas table1
721 -> AlterTable sch tab schemas table1
725renameColumn column0 column1 = UnsafeAlterTable $
722renameColumn column0 column1 = UnsafeAlterTable $
726− "RENAME COLUMN" <+> renderAlias column0 <+> "TO" <+> renderAlias column1
723+ "RENAME COLUMN" <+> renderSQL column0 <+> "TO" <+> renderSQL column1
727
724
728-- | An `alterColumn` alters a single column.
725-- | An `alterColumn` alters a single column.
729alterColumn
726alterColumn
@@ -737,7 +734,7 @@ alterColumn
737 -> AlterColumn schemas ty0 ty1 -- ^ alteration to perform
734 -> AlterColumn schemas ty0 ty1 -- ^ alteration to perform
738 -> AlterTable sch tab schemas table1
735 -> AlterTable sch tab schemas table1
739alterColumn column alteration = UnsafeAlterTable $
736alterColumn column alteration = UnsafeAlterTable $
740− "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
737+ "ALTER COLUMN" <+> renderSQL column <+> renderAlterColumn alteration
741
738
742-- | An `AlterColumn` describes the alteration to perform on a single column.
739-- | An `AlterColumn` describes the alteration to perform on a single column.
743newtype AlterColumn (schemas :: SchemasType) (ty0 :: ColumnType) (ty1 :: ColumnType) =
740newtype AlterColumn (schemas :: SchemasType) (ty0 :: ColumnType) (ty1 :: ColumnType) =
@@ -835,7 +832,7 @@ let
835 '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC)]]
832 '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC)]]
836 '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC), "bc" ::: 'View BC]]
833 '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC), "bc" ::: 'View BC]]
837 definition =
834 definition =
838− createView #bc (select (#b :* #c) (from (table #abc)))
835+ createView #bc (select_ (#b :* #c) (from (table #abc)))
839in printSQL definition
836in printSQL definition
840:}
837:}
841CREATE VIEW "bc" AS SELECT "b" AS "b", "c" AS "c" FROM "abc" AS "abc";
838CREATE VIEW "bc" AS SELECT "b" AS "b", "c" AS "c" FROM "abc" AS "abc";
@@ -938,7 +935,7 @@ createTypeComposite ty fields = UnsafeDefinition $
938 where
935 where
939 renderField :: Aliased (TypeExpression schemas) x -> ByteString
936 renderField :: Aliased (TypeExpression schemas) x -> ByteString
940 renderField (typ `As` alias) =
937 renderField (typ `As` alias) =
941− renderAlias alias <+> renderTypeExpression typ
938+ renderSQL alias <+> renderTypeExpression typ
942
939
943-- | Composite types can also be generated from a Haskell type, for example
940-- | Composite types can also be generated from a Haskell type, for example
944--
941--
@@ -990,6 +987,8 @@ dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderQualifiedAlias t
990newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
987newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
991 = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }
988 = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }
992 deriving (GHC.Generic,Show,Eq,Ord,NFData)
989 deriving (GHC.Generic,Show,Eq,Ord,NFData)
990+instance RenderSQL (ColumnTypeExpression schemas ty) where
991+ renderSQL = renderColumnTypeExpression
993
992
994-- | used in `createTable` commands as a column constraint to note that
993-- | used in `createTable` commands as a column constraint to note that
995-- @NULL@ may be present in a column
994-- @NULL@ may be present in a column
@@ -1011,7 +1010,7 @@ default_
1011 -> ColumnTypeExpression schemas ('NoDef :=> ty)
1010 -> ColumnTypeExpression schemas ('NoDef :=> ty)
1012 -> ColumnTypeExpression schemas ('Def :=> ty)
1011 -> ColumnTypeExpression schemas ('Def :=> ty)
1013default_ x ty = UnsafeColumnTypeExpression $
1012default_ x ty = UnsafeColumnTypeExpression $
1014− renderColumnTypeExpression ty <+> "DEFAULT" <+> renderExpression x
1013+ renderSQL ty <+> "DEFAULT" <+> renderExpression x
1015
1014
1016-- | not a true type, but merely a notational convenience for creating
1015-- | not a true type, but merely a notational convenience for creating
1017-- unique identifier columns with type `PGint2`
1016-- unique identifier columns with type `PGint2`
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+23−23
@@ -244,7 +244,7 @@ class KnownNat n => HasParameter
244 | n params -> ty where
244 | n params -> ty where
245 -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
245 -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
246 --
246 --
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)
248 -- >>> printSQL expr
248 -- >>> printSQL expr
249 -- ($1 :: int4)
249 -- ($1 :: int4)
250 parameter
250 parameter
@@ -260,7 +260,7 @@ instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
260-- | `param` takes a `Nat` using type application and for basic types,
260-- | `param` takes a `Nat` using type application and for basic types,
261-- infers a `TypeExpression`.
261-- infers a `TypeExpression`.
262--
262--
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)
264-- >>> printSQL expr
264-- >>> printSQL expr
265-- ($1 :: int4)
265-- ($1 :: int4)
266param
266param
@@ -271,7 +271,7 @@ param = parameter @n (pgtype @schemas)
271
271
272instance (HasUnique tab from row, Has col row ty)
272instance (HasUnique tab from row, Has col row ty)
273 => IsLabel col (Expression db params 'Ungrouped from ty) where
273 => IsLabel col (Expression db params 'Ungrouped from ty) where
274− fromLabel = UnsafeExpression $ renderAlias (Alias @col)
274+ fromLabel = UnsafeExpression $ renderSQL (Alias @col)
275instance (HasUnique tab from row, Has col row ty, column ~ (col ::: ty))
275instance (HasUnique tab from row, Has col row ty, column ~ (col ::: ty))
276 => IsLabel col
276 => IsLabel col
277 (Aliased (Expression db params 'Ungrouped from) column) where
277 (Aliased (Expression db params 'Ungrouped from) column) where
@@ -284,7 +284,7 @@ instance (HasUnique tab from row, Has col row ty, columns ~ '[col ::: ty])
284instance (Has tab from row, Has col row ty)
284instance (Has tab from row, Has col row ty)
285 => IsQualified tab col (Expression db params 'Ungrouped from ty) where
285 => IsQualified tab col (Expression db params 'Ungrouped from ty) where
286 tab ! col = UnsafeExpression $
286 tab ! col = UnsafeExpression $
287− renderAlias tab <> "." <> renderAlias col
287+ renderSQL tab <> "." <> renderSQL col
288instance (Has tab from row, Has col row ty, column ~ (col ::: ty))
288instance (Has tab from row, Has col row ty, column ~ (col ::: ty))
289 => IsQualified tab col
289 => IsQualified tab col
290 (Aliased (Expression db params 'Ungrouped from) column) where
290 (Aliased (Expression db params 'Ungrouped from) column) where
@@ -300,7 +300,7 @@ instance
300 , GroupedBy tab col bys
300 , GroupedBy tab col bys
301 ) => IsLabel col
301 ) => IsLabel col
302 (Expression db params ('Grouped bys) from ty) where
302 (Expression db params ('Grouped bys) from ty) where
303− fromLabel = UnsafeExpression $ renderAlias (Alias @col)
303+ fromLabel = UnsafeExpression $ renderSQL (Alias @col)
304instance
304instance
305 ( HasUnique tab from row
305 ( HasUnique tab from row
306 , Has col row ty
306 , Has col row ty
@@ -325,7 +325,7 @@ instance
325 ) => IsQualified tab col
325 ) => IsQualified tab col
326 (Expression db params ('Grouped bys) from ty) where
326 (Expression db params ('Grouped bys) from ty) where
327 tab ! col = UnsafeExpression $
327 tab ! col = UnsafeExpression $
328− renderAlias tab <> "." <> renderAlias col
328+ renderSQL tab <> "." <> renderSQL col
329instance
329instance
330 ( Has tab from row
330 ( Has tab from row
331 , Has col row ty
331 , Has col row ty
@@ -419,7 +419,7 @@ matchNull y f x = ifThenElse (isNull x) y
419`nullIf` gives @NULL@.
419`nullIf` gives @NULL@.
420
420
421>>> :set -XTypeApplications -XDataKinds
421>>> :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)
423>>> printSQL expr
423>>> printSQL expr
424NULL IF (FALSE, ($1 :: bool))
424NULL IF (FALSE, ($1 :: bool))
425-}
425-}
@@ -495,7 +495,7 @@ field
495 -> Expression (commons :=> schemas) params grp from ty
495 -> Expression (commons :=> schemas) params grp from ty
496field td fld expr = UnsafeExpression $
496field td fld expr = UnsafeExpression $
497 parenthesized (renderExpression expr <> "::" <> renderQualifiedAlias td)
497 parenthesized (renderExpression expr <> "::" <> renderQualifiedAlias td)
498− <> "." <> renderAlias fld
498+ <> "." <> renderSQL fld
499
499
500instance Semigroup
500instance Semigroup
501 (Expression db params grp from (nullity ('PGvararray ty))) where
501 (Expression db params grp from (nullity ('PGvararray ty))) where
@@ -506,7 +506,7 @@ instance Monoid
506 mempty = array []
506 mempty = array []
507 mappend = (<>)
507 mappend = (<>)
508
508
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)
510-- >>> printSQL expr
510-- >>> printSQL expr
511-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
511-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
512greatest
512greatest
@@ -1393,7 +1393,7 @@ unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
1393
1393
1394-- | >>> :{
1394-- | >>> :{
1395-- let
1395-- 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)
1397-- expression = sum_ #col
1397-- expression = sum_ #col
1398-- in printSQL expression
1398-- in printSQL expression
1399-- :}
1399-- :}
@@ -1407,7 +1407,7 @@ sum_ = unsafeAggregate "sum"
1407
1407
1408-- | >>> :{
1408-- | >>> :{
1409-- let
1409-- 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)
1411-- expression = sumDistinct #col
1411-- expression = sumDistinct #col
1412-- in printSQL expression
1412-- in printSQL expression
1413-- :}
1413-- :}
@@ -1438,7 +1438,7 @@ instance PGAvg 'PGinterval 'PGinterval
1438
1438
1439-- | >>> :{
1439-- | >>> :{
1440-- let
1440-- 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)
1442-- expression = bitAnd #col
1442-- expression = bitAnd #col
1443-- in printSQL expression
1443-- in printSQL expression
1444-- :}
1444-- :}
@@ -1452,7 +1452,7 @@ bitAnd = unsafeAggregate "bit_and"
1452
1452
1453-- | >>> :{
1453-- | >>> :{
1454-- let
1454-- 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)
1456-- expression = bitOr #col
1456-- expression = bitOr #col
1457-- in printSQL expression
1457-- in printSQL expression
1458-- :}
1458-- :}
@@ -1466,7 +1466,7 @@ bitOr = unsafeAggregate "bit_or"
1466
1466
1467-- | >>> :{
1467-- | >>> :{
1468-- let
1468-- 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)
1470-- expression = bitAndDistinct #col
1470-- expression = bitAndDistinct #col
1471-- in printSQL expression
1471-- in printSQL expression
1472-- :}
1472-- :}
@@ -1480,7 +1480,7 @@ bitAndDistinct = unsafeAggregateDistinct "bit_and"
1480
1480
1481-- | >>> :{
1481-- | >>> :{
1482-- let
1482-- 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)
1484-- expression = bitOrDistinct #col
1484-- expression = bitOrDistinct #col
1485-- in printSQL expression
1485-- in printSQL expression
1486-- :}
1486-- :}
@@ -1494,7 +1494,7 @@ bitOrDistinct = unsafeAggregateDistinct "bit_or"
1494
1494
1495-- | >>> :{
1495-- | >>> :{
1496-- let
1496-- 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)
1498-- expression = boolAnd #col
1498-- expression = boolAnd #col
1499-- in printSQL expression
1499-- in printSQL expression
1500-- :}
1500-- :}
@@ -1507,7 +1507,7 @@ boolAnd = unsafeAggregate "bool_and"
1507
1507
1508-- | >>> :{
1508-- | >>> :{
1509-- let
1509-- 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)
1511-- expression = boolOr #col
1511-- expression = boolOr #col
1512-- in printSQL expression
1512-- in printSQL expression
1513-- :}
1513-- :}
@@ -1520,7 +1520,7 @@ boolOr = unsafeAggregate "bool_or"
1520
1520
1521-- | >>> :{
1521-- | >>> :{
1522-- let
1522-- 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)
1524-- expression = boolAndDistinct #col
1524-- expression = boolAndDistinct #col
1525-- in printSQL expression
1525-- in printSQL expression
1526-- :}
1526-- :}
@@ -1533,7 +1533,7 @@ boolAndDistinct = unsafeAggregateDistinct "bool_and"
1533
1533
1534-- | >>> :{
1534-- | >>> :{
1535-- let
1535-- 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)
1537-- expression = boolOrDistinct #col
1537-- expression = boolOrDistinct #col
1538-- in printSQL expression
1538-- in printSQL expression
1539-- :}
1539-- :}
@@ -1554,7 +1554,7 @@ countStar = UnsafeExpression $ "count(*)"
1554
1554
1555-- | >>> :{
1555-- | >>> :{
1556-- let
1556-- 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)
1558-- expression = count #col
1558-- expression = count #col
1559-- in printSQL expression
1559-- in printSQL expression
1560-- :}
1560-- :}
@@ -1567,7 +1567,7 @@ count = unsafeAggregate "count"
1567
1567
1568-- | >>> :{
1568-- | >>> :{
1569-- let
1569-- 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)
1571-- expression = countDistinct #col
1571-- expression = countDistinct #col
1572-- in printSQL expression
1572-- in printSQL expression
1573-- :}
1573-- :}
@@ -1582,7 +1582,7 @@ countDistinct = unsafeAggregateDistinct "count"
1582--
1582--
1583-- >>> :{
1583-- >>> :{
1584-- let
1584-- 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)
1586-- expression = every #col
1586-- expression = every #col
1587-- in printSQL expression
1587-- in printSQL expression
1588-- :}
1588-- :}
@@ -1597,7 +1597,7 @@ every = unsafeAggregate "every"
1597--
1597--
1598-- >>> :{
1598-- >>> :{
1599-- let
1599-- 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)
1601-- expression = everyDistinct #col
1601-- expression = everyDistinct #col
1602-- in printSQL expression
1602-- in printSQL expression
1603-- :}
1603-- :}
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+20−18
@@ -123,7 +123,7 @@ let
123 (Values_ ("John Smith" `as` #name :* "john@smith.com" `as` #email))
123 (Values_ ("John Smith" `as` #name :* "john@smith.com" `as` #email))
124 (OnConflict (OnConstraint #uq)
124 (OnConflict (OnConstraint #uq)
125 (DoUpdate (((#excluded ! #email) <> "; " <> (#customers ! #email)) `as` #email) []))
125 (DoUpdate (((#excluded ! #email) <> "; " <> (#customers ! #email)) `as` #email) []))
126− (Returning Nil)
126+ (Returning_ Nil)
127in printSQL manipulation
127in printSQL manipulation
128:}
128:}
129INSERT 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"))
129INSERT 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:
133>>> :{
133>>> :{
134let
134let
135 manipulation :: Manipulation (DBof (Public Schema)) '[] '[]
135 manipulation :: Manipulation (DBof (Public Schema)) '[] '[]
136− manipulation = insertInto_ #tab (Subquery (selectStar (from (table #tab))))
136+ manipulation = insertInto_ #tab (Subquery (select Star (from (table #tab))))
137in printSQL manipulation
137in printSQL manipulation
138:}
138:}
139INSERT INTO "tab" SELECT * FROM "tab" AS "tab"
139INSERT INTO "tab" SELECT * FROM "tab" AS "tab"
@@ -155,7 +155,7 @@ let
155 manipulation :: Manipulation (DBof (Public Schema)) '[]
155 manipulation :: Manipulation (DBof (Public Schema)) '[]
156 '[ "col1" ::: 'NotNull 'PGint4
156 '[ "col1" ::: 'NotNull 'PGint4
157 , "col2" ::: 'NotNull 'PGint4 ]
157 , "col2" ::: 'NotNull 'PGint4 ]
158− manipulation = deleteFrom #tab NoUsing (#col1 .== #col2) ReturningStar
158+ manipulation = deleteFrom #tab NoUsing (#col1 .== #col2) (Returning Star)
159in printSQL manipulation
159in printSQL manipulation
160:}
160:}
161DELETE FROM "tab" WHERE ("col1" = "col2") RETURNING *
161DELETE FROM "tab" WHERE ("col1" = "col2") RETURNING *
@@ -188,8 +188,8 @@ with manipulation:
188let
188let
189 manipulation :: Manipulation (DBof (Public ProductsSchema)) '[ 'NotNull 'PGdate] '[]
189 manipulation :: Manipulation (DBof (Public ProductsSchema)) '[ 'NotNull 'PGdate] '[]
190 manipulation = with
190 manipulation = with
191− (deleteFrom #products NoUsing (#date .< param @1) ReturningStar `as` #del)
191+ (deleteFrom #products NoUsing (#date .< param @1) (Returning Star) `as` #del)
192− (insertInto_ #products_deleted (Subquery (selectStar (from (common #del)))))
192+ (insertInto_ #products_deleted (Subquery (select Star (from (common #del)))))
193in printSQL manipulation
193in printSQL manipulation
194:}
194:}
195WITH "del" AS (DELETE FROM "products" WHERE ("date" < ($1 :: date)) RETURNING *) INSERT INTO "products_deleted" SELECT * FROM "del" AS "del"
195WITH "del" AS (DELETE FROM "products" WHERE ("date" < ($1 :: date)) RETURNING *) INSERT INTO "products_deleted" SELECT * FROM "del" AS "del"
@@ -279,31 +279,31 @@ renderQueryClause
279 -> ByteString
279 -> ByteString
280renderQueryClause = \case
280renderQueryClause = \case
281 Values row0 rows ->
281 Values row0 rows ->
282− parenthesized (renderCommaSeparated renderAliasPart row0)
282+ parenthesized (renderCommaSeparated renderSQLPart row0)
283 <+> "VALUES"
283 <+> "VALUES"
284 <+> commaSeparated
284 <+> commaSeparated
285 ( parenthesized
285 ( parenthesized
286 . renderCommaSeparated renderValuePart <$> row0 : rows )
286 . renderCommaSeparated renderValuePart <$> row0 : rows )
287 Select row0 tab ->
287 Select row0 tab ->
288− parenthesized (renderCommaSeparatedMaybe renderAliasPartMaybe row0)
288+ parenthesized (renderCommaSeparatedMaybe renderSQLPartMaybe row0)
289 <+> "SELECT"
289 <+> "SELECT"
290 <+> renderCommaSeparatedMaybe renderValuePartMaybe row0
290 <+> renderCommaSeparatedMaybe renderValuePartMaybe row0
291 <+> renderTableExpression tab
291 <+> renderTableExpression tab
292 Subquery qry -> renderQuery qry
292 Subquery qry -> renderQuery qry
293 where
293 where
294− renderAliasPartMaybe, renderValuePartMaybe
294+ renderSQLPartMaybe, renderValuePartMaybe
295 :: ColumnExpression db params grp from column -> Maybe ByteString
295 :: ColumnExpression db params grp from column -> Maybe ByteString
296− renderAliasPartMaybe = \case
296+ renderSQLPartMaybe = \case
297 DefaultAs _ -> Nothing
297 DefaultAs _ -> Nothing
298− Specific (_ `As` name) -> Just $ renderAlias name
298+ Specific (_ `As` name) -> Just $ renderSQL name
299 renderValuePartMaybe = \case
299 renderValuePartMaybe = \case
300 DefaultAs _ -> Nothing
300 DefaultAs _ -> Nothing
301 Specific (value `As` _) -> Just $ renderExpression value
301 Specific (value `As` _) -> Just $ renderExpression value
302− renderAliasPart, renderValuePart
302+ renderSQLPart, renderValuePart
303 :: ColumnExpression db params grp from column -> ByteString
303 :: ColumnExpression db params grp from column -> ByteString
304− renderAliasPart = \case
304+ renderSQLPart = \case
305− DefaultAs name -> renderAlias name
305+ DefaultAs name -> renderSQL name
306− Specific (_ `As` name) -> renderAlias name
306+ Specific (_ `As` name) -> renderSQL name
307 renderValuePart = \case
307 renderValuePart = \case
308 DefaultAs _ -> "DEFAULT"
308 DefaultAs _ -> "DEFAULT"
309 Specific (value `As` _) -> renderExpression value
309 Specific (value `As` _) -> renderExpression value
@@ -394,9 +394,9 @@ renderColumnExpression
394 -> ByteString
394 -> ByteString
395renderColumnExpression = \case
395renderColumnExpression = \case
396 DefaultAs col ->
396 DefaultAs col ->
397− renderAlias col <+> "=" <+> "DEFAULT"
397+ renderSQL col <+> "=" <+> "DEFAULT"
398 Specific (expression `As` col) ->
398 Specific (expression `As` col) ->
399− renderAlias col <+> "=" <+> renderExpression expression
399+ renderSQL col <+> "=" <+> renderExpression expression
400
400
401-- | A `ReturningClause` computes and return value(s) based
401-- | A `ReturningClause` computes and return value(s) based
402-- on each row actually inserted, updated or deleted. This is primarily
402-- on each row actually inserted, updated or deleted. This is primarily
@@ -412,7 +412,9 @@ newtype ReturningClause db params from row =
412 Returning (Selection db params 'Ungrouped from row)
412 Returning (Selection db params 'Ungrouped from row)
413
413
414instance RenderSQL (ReturningClause db params from row) where
414instance RenderSQL (ReturningClause db params from row) where
415+ renderSQL = \case
416+ Returning (List Nil) -> ""
415− renderSQL (Returning selection) = "RETURNING" <+> renderSQL selection
417+ Returning selection -> " RETURNING" <+> renderSQL selection
416
418
417pattern Returning_
419pattern Returning_
418 :: SOP.SListI row
420 :: SOP.SListI row
@@ -479,7 +481,7 @@ renderConflictTarget
479 :: ConflictTarget constraints
481 :: ConflictTarget constraints
480 -> ByteString
482 -> ByteString
481renderConflictTarget (OnConstraint con) =
483renderConflictTarget (OnConstraint con) =
482− "ON" <+> "CONSTRAINT" <+> renderAlias con
484+ "ON" <+> "CONSTRAINT" <+> renderSQL con
483
485
484{-----------------------------------------
486{-----------------------------------------
485UPDATE statements
487UPDATE statements
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+1−1
@@ -71,7 +71,7 @@ let
71 :: Has "migrations" schemas MigrationsSchema
71 :: Has "migrations" schemas MigrationsSchema
72 => PQ schemas schemas IO ()
72 => PQ schemas schemas IO ()
73 numMigrations = do
73 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))))
75 num <- ntuples result
75 num <- ntuples result
76 liftBase $ print num
76 liftBase $ print num
77:}
77:}
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+24−24
@@ -151,7 +151,7 @@ simple query:
151>>> :{
151>>> :{
152let
152let
153 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
153 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
154− query = selectStar (from (table #tab))
154+ query = select Star (from (table #tab))
155in printSQL query
155in printSQL query
156:}
156:}
157SELECT * FROM "tab" AS "tab"
157SELECT * FROM "tab" AS "tab"
@@ -162,7 +162,7 @@ restricted query:
162let
162let
163 query :: Query (DBof (Public Schema)) '[] '["sum" ::: 'NotNull 'PGint4, "col1" ::: 'NotNull 'PGint4]
163 query :: Query (DBof (Public Schema)) '[] '["sum" ::: 'NotNull 'PGint4, "col1" ::: 'NotNull 'PGint4]
164 query =
164 query =
165− select ((#col1 + #col2) `as` #sum :* #col1)
165+ select_ ((#col1 + #col2) `as` #sum :* #col1)
166 ( from (table #tab)
166 ( from (table #tab)
167 & where_ (#col1 .> #col2)
167 & where_ (#col1 .> #col2)
168 & where_ (#col2 .> 0) )
168 & where_ (#col2 .> 0) )
@@ -175,7 +175,7 @@ subquery:
175>>> :{
175>>> :{
176let
176let
177 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
177 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)))
179in printSQL query
179in printSQL query
180:}
180:}
181SELECT * FROM (SELECT * FROM "tab" AS "tab") AS "sub"
181SELECT * FROM (SELECT * FROM "tab" AS "tab") AS "sub"
@@ -185,7 +185,7 @@ limits and offsets:
185>>> :{
185>>> :{
186let
186let
187 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
187 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)
189in printSQL query
189in printSQL query
190:}
190:}
191SELECT * FROM "tab" AS "tab" LIMIT 50 OFFSET 4
191SELECT * FROM "tab" AS "tab" LIMIT 50 OFFSET 4
@@ -195,7 +195,7 @@ parameterized query:
195>>> :{
195>>> :{
196let
196let
197 query :: Query (DBof (Public Schema)) '[ 'NotNull 'PGint4] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
197 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))
199in printSQL query
199in printSQL query
200:}
200:}
201SELECT * FROM "tab" AS "tab" WHERE ("col1" > ($1 :: int4))
201SELECT * FROM "tab" AS "tab" WHERE ("col1" > ($1 :: int4))
@@ -206,7 +206,7 @@ aggregation query:
206let
206let
207 query :: Query (DBof (Public Schema)) '[] '["sum" ::: 'NotNull 'PGint4, "col1" ::: 'NotNull 'PGint4 ]
207 query :: Query (DBof (Public Schema)) '[] '["sum" ::: 'NotNull 'PGint4, "col1" ::: 'NotNull 'PGint4 ]
208 query =
208 query =
209− select (sum_ #col2 `as` #sum :* #col1)
209+ select_ (sum_ #col2 `as` #sum :* #col1)
210 ( from (table (#tab `as` #table1))
210 ( from (table (#tab `as` #table1))
211 & groupBy #col1
211 & groupBy #col1
212 & having (#col1 + sum_ #col2 .> 1) )
212 & having (#col1 + sum_ #col2 .> 1) )
@@ -219,7 +219,7 @@ sorted query:
219>>> :{
219>>> :{
220let
220let
221 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
221 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])
223in printSQL query
223in printSQL query
224:}
224:}
225SELECT * FROM "tab" AS "tab" ORDER BY "col1" ASC
225SELECT * FROM "tab" AS "tab" ORDER BY "col1" ASC
@@ -261,7 +261,7 @@ let
261 , "customer_name" ::: 'NotNull 'PGtext
261 , "customer_name" ::: 'NotNull 'PGtext
262 , "shipper_name" ::: 'NotNull 'PGtext
262 , "shipper_name" ::: 'NotNull 'PGtext
263 ]
263 ]
264− query = select
264+ query = select_
265 ( #o ! #price `as` #order_price :*
265 ( #o ! #price `as` #order_price :*
266 #c ! #name `as` #customer_name :*
266 #c ! #name `as` #customer_name :*
267 #s ! #name `as` #shipper_name )
267 #s ! #name `as` #shipper_name )
@@ -279,7 +279,7 @@ self-join:
279>>> :{
279>>> :{
280let
280let
281 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
281 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))))
283in printSQL query
283in printSQL query
284:}
284:}
285SELECT "t1".* FROM "tab" AS "t1" CROSS JOIN "tab" AS "t2"
285SELECT "t1".* FROM "tab" AS "t1" CROSS JOIN "tab" AS "t2"
@@ -299,7 +299,7 @@ set operations:
299>>> :{
299>>> :{
300let
300let
301 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
301 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))
303in printSQL query
303in printSQL query
304:}
304:}
305(SELECT * FROM "tab" AS "tab") UNION ALL (SELECT * FROM "tab" AS "tab")
305(SELECT * FROM "tab" AS "tab") UNION ALL (SELECT * FROM "tab" AS "tab")
@@ -310,9 +310,9 @@ with queries:
310let
310let
311 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
311 query :: Query (DBof (Public Schema)) '[] '["col1" ::: 'NotNull 'PGint4, "col2" ::: 'NotNull 'PGint4]
312 query = with (
312 query = with (
313− selectStar (from (table #tab)) `as` #cte1 :>>
313+ select Star (from (table #tab)) `as` #cte1 :>>
314− selectStar (from (common #cte1)) `as` #cte2
314+ select Star (from (common #cte1)) `as` #cte2
315− ) (selectStar (from (common #cte2)))
315+ ) (select Star (from (common #cte2)))
316in printSQL query
316in printSQL query
317:}
317:}
318WITH "cte1" AS (SELECT * FROM "tab" AS "tab"), "cte2" AS (SELECT * FROM "cte1" AS "cte1") SELECT * FROM "cte2" AS "cte2"
318WITH "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
441 renderSQL = \case
441 renderSQL = \case
442 List list -> renderCommaSeparated (renderAliasedAs renderExpression) list
442 List list -> renderCommaSeparated (renderAliasedAs renderExpression) list
443 Star -> "*"
443 Star -> "*"
444− DotStar tab -> renderAlias tab <> ".*"
444+ DotStar tab -> renderSQL tab <> ".*"
445 Also right left -> renderSQL left <> ", " <> renderSQL right
445 Also right left -> renderSQL left <> ", " <> renderSQL right
446
446
447-- | the `TableExpression` in the `select` command constructs an intermediate
447-- | the `TableExpression` in the `select` command constructs an intermediate
@@ -514,11 +514,11 @@ values rw rws = UnsafeQuery $ "SELECT * FROM"
514 ( parenthesized
514 ( parenthesized
515 . renderCommaSeparated renderValuePart <$> rw:rws )
515 . renderCommaSeparated renderValuePart <$> rw:rws )
516 ) <+> "AS t"
516 ) <+> "AS t"
517− <+> parenthesized (renderCommaSeparated renderAliasPart rw)
517+ <+> parenthesized (renderCommaSeparated renderSQLPart rw)
518 where
518 where
519− renderAliasPart, renderValuePart
519+ renderSQLPart, renderValuePart
520 :: Aliased (Expression db params 'Ungrouped '[] ) ty -> ByteString
520 :: Aliased (Expression db params 'Ungrouped '[] ) ty -> ByteString
521− renderAliasPart (_ `As` name) = renderAlias name
521+ renderSQLPart (_ `As` name) = renderSQL name
522 renderValuePart (value `As` _) = renderExpression value
522 renderValuePart (value `As` _) = renderExpression value
523
523
524-- | `values_` computes a row value or set of row values
524-- | `values_` computes a row value or set of row values
@@ -793,7 +793,7 @@ unsafeRecordFunction fun expr types = UnsafeQuery $
793 where
793 where
794 renderTy :: Aliased (TypeExpression db) ty -> ByteString
794 renderTy :: Aliased (TypeExpression db) ty -> ByteString
795 renderTy (ty `As` alias) =
795 renderTy (ty `As` alias) =
796− renderAlias alias <+> renderTypeExpression ty
796+ renderSQL alias <+> renderTypeExpression ty
797
797
798-- | Builds an arbitrary record from a JSON object.
798-- | Builds an arbitrary record from a JSON object.
799jsonToRecord
799jsonToRecord
@@ -845,7 +845,7 @@ table
845 => Aliased (QualifiedAlias sch) (alias ::: tab)
845 => Aliased (QualifiedAlias sch) (alias ::: tab)
846 -> FromClause (commons :=> schemas) params '[alias ::: TableToRow table]
846 -> FromClause (commons :=> schemas) params '[alias ::: TableToRow table]
847table (tab `As` alias) = UnsafeFromClause $
847table (tab `As` alias) = UnsafeFromClause $
848− renderQualifiedAlias tab <+> "AS" <+> renderAlias alias
848+ renderQualifiedAlias tab <+> "AS" <+> renderSQL alias
849
849
850-- | `subquery` derives a table from a `Query`.
850-- | `subquery` derives a table from a `Query`.
851subquery
851subquery
@@ -859,14 +859,14 @@ view
859 => Aliased (QualifiedAlias sch) (alias ::: vw)
859 => Aliased (QualifiedAlias sch) (alias ::: vw)
860 -> FromClause (commons :=> schemas) params '[alias ::: view]
860 -> FromClause (commons :=> schemas) params '[alias ::: view]
861view (vw `As` alias) = UnsafeFromClause $
861view (vw `As` alias) = UnsafeFromClause $
862− renderQualifiedAlias vw <+> "AS" <+> renderAlias alias
862+ renderQualifiedAlias vw <+> "AS" <+> renderSQL alias
863
863
864common
864common
865 :: Has cte commons common
865 :: Has cte commons common
866 => Aliased Alias (alias ::: cte)
866 => Aliased Alias (alias ::: cte)
867 -> FromClause (commons :=> schemas) params '[alias ::: common]
867 -> FromClause (commons :=> schemas) params '[alias ::: common]
868common (cte `As` alias) = UnsafeFromClause $
868common (cte `As` alias) = UnsafeFromClause $
869− renderAlias cte <+> "AS" <+> renderAlias alias
869+ renderSQL cte <+> "AS" <+> renderSQL alias
870
870
871{- | @left & crossJoin right@. For every possible combination of rows from
871{- | @left & crossJoin right@. For every possible combination of rows from
872 @left@ and @right@ (i.e., a Cartesian product), the joined table will contain
872 @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)])
991-- | Renders a `By`.
991-- | Renders a `By`.
992renderBy :: By from by -> ByteString
992renderBy :: By from by -> ByteString
993renderBy = \case
993renderBy = \case
994− By1 column -> renderAlias column
994+ By1 column -> renderSQL column
995− By2 rel column -> renderAlias rel <> "." <> renderAlias column
995+ By2 rel column -> renderSQL rel <> "." <> renderSQL column
996
996
997-- | A `GroupByClause` indicates the `Grouping` of a `TableExpression`.
997-- | A `GroupByClause` indicates the `Grouping` of a `TableExpression`.
998-- A `NoGroups` indicates `Ungrouped` while a `Group` indicates `Grouped`.
998-- A `NoGroups` indicates `Ungrouped` while a `Group` indicates `Grouped`.
@@ -1380,7 +1380,7 @@ renderCommonTableExpression
1380 -> CommonTableExpression statement params db0 db1 -> ByteString
1380 -> CommonTableExpression statement params db0 db1 -> ByteString
1381renderCommonTableExpression renderStatement
1381renderCommonTableExpression renderStatement
1382 (CommonTableExpression (statement `As` alias)) =
1382 (CommonTableExpression (statement `As` alias)) =
1383− renderAlias alias <+> "AS" <+> parenthesized (renderStatement statement)
1383+ renderSQL alias <+> "AS" <+> parenthesized (renderStatement statement)
1384
1384
1385-- | render a non-empty `AlignedList` of `CommonTableExpression`s.
1385-- | render a non-empty `AlignedList` of `CommonTableExpression`s.
1386renderCommonTableExpressions
1386renderCommonTableExpressions
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+7−10
@@ -68,7 +68,6 @@ module Squeal.PostgreSQL.Schema
68 , (:::)
68 , (:::)
69 , Alias (..)
69 , Alias (..)
70 , IsLabel (..)
70 , IsLabel (..)
71− , renderAlias
72 , renderAliasString
71 , renderAliasString
73 , renderAliases
72 , renderAliases
74 , Aliased (As)
73 , Aliased (As)
@@ -377,12 +376,10 @@ instance alias1 ~ alias2 => IsLabel alias1 (Alias alias2) where
377 fromLabel = Alias
376 fromLabel = Alias
378instance aliases ~ '[alias] => IsLabel alias (NP Alias aliases) where
377instance aliases ~ '[alias] => IsLabel alias (NP Alias aliases) where
379 fromLabel = fromLabel :* Nil
378 fromLabel = fromLabel :* Nil
380−instance KnownSymbol alias => RenderSQL (Alias alias) where renderSQL = renderAlias
379+-- | >>> renderSQL #jimbob
381−
382−-- | >>> renderAlias #jimbob
383-- "\"jimbob\""
380-- "\"jimbob\""
384−renderAlias :: KnownSymbol alias => Alias alias -> ByteString
381+instance KnownSymbol alias => RenderSQL (Alias alias) where
385−renderAlias = doubleQuoted . fromString . symbolVal
382+ renderSQL = doubleQuoted . fromString . symbolVal
386
383
387-- | >>> renderAliasString #ohmahgerd
384-- | >>> renderAliasString #ohmahgerd
388-- "'ohmahgerd'"
385-- "'ohmahgerd'"
@@ -395,7 +392,7 @@ renderAliasString = singleQuotedText . fromString . symbolVal
395renderAliases
392renderAliases
396 :: All KnownSymbol aliases => NP Alias aliases -> [ByteString]
393 :: All KnownSymbol aliases => NP Alias aliases -> [ByteString]
397renderAliases = hcollapse
394renderAliases = hcollapse
398− . hcmap (Proxy @KnownSymbol) (K . renderAlias)
395+ . hcmap (Proxy @KnownSymbol) (K . renderSQL)
399
396
400-- | The `As` operator is used to name an expression. `As` is like a demoted
397-- | The `As` operator is used to name an expression. `As` is like a demoted
401-- version of `:::`.
398-- version of `:::`.
@@ -443,7 +440,7 @@ renderAliasedAs
443 -> Aliased expression aliased
440 -> Aliased expression aliased
444 -> ByteString
441 -> ByteString
445renderAliasedAs render (expression `As` alias) =
442renderAliasedAs render (expression `As` alias) =
446− render expression <> " AS " <> renderAlias alias
443+ render expression <> " AS " <> renderSQL alias
447
444
448-- | @HasUnique alias fields field@ is a constraint that proves that
445-- | @HasUnique alias fields field@ is a constraint that proves that
449-- @fields@ is a singleton of @alias ::: field@.
446-- @fields@ is a singleton of @alias ::: field@.
@@ -519,8 +516,8 @@ renderQualifiedAlias
519 => QualifiedAlias q a -> ByteString
516 => QualifiedAlias q a -> ByteString
520renderQualifiedAlias _ =
517renderQualifiedAlias _ =
521 let
518 let
522− qualifier = renderAlias (Alias @q)
519+ qualifier = renderSQL (Alias @q)
523− alias = renderAlias (Alias @a)
520+ alias = renderSQL (Alias @a)
524 in
521 in
525 if qualifier == "\"public\"" then alias else qualifier <> "." <> alias
522 if qualifier == "\"public\"" then alias else qualifier <> "." <> alias
526
523