@@ -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`