@@ -29,7 +29,7 @@ Squeal data definition language.
30module Squeal.PostgreSQL.Definition
30module Squeal.PostgreSQL.Definition
31 ( -- * Definition
31 ( -- * Definition
32− Definition (UnsafeDefinition, renderDefinition)
32+ Definition (..)
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 <> ";"
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 <> ";"
145{- | `createTable` adds a table to the schema.
143{- | `createTable` adds a table to the schema.
@@ -238,12 +236,12 @@ renderCreation tab columns constraints = renderQualifiedAlias tab
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
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
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
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"
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
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"
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 <> ";"
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
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
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)
@@ -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
704-- | A `renameColumn` renames a column.
701-- | A `renameColumn` renames a column.
@@ -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
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
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
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 $
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
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
@@ -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
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
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`