@@ -64,6 +64,9 @@ module Squeal.PostgreSQL.Definition
6464 , setNotNull
6565 , dropNotNull
6666 , alterType
67+ -- * Views
68+ , createView
69+ , dropView
6770 ) where
6871
6972import Control.Category
@@ -77,6 +80,7 @@ import qualified Generics.SOP as SOP
7780import qualified GHC.Generics as GHC
7881
7982import Squeal.PostgreSQL.Expression
83+import Squeal.PostgreSQL.Query
8084import Squeal.PostgreSQL.Render
8185import Squeal.PostgreSQL.Schema
8286
@@ -122,8 +126,8 @@ createTable
122126 -> NP (Aliased (TableConstraintExpression schema1 table)) constraints
123127 -- ^ constraints that must hold for the table
124128 -> Definition schema0 schema1
125−createTable table columns constraints = UnsafeDefinition $
126− "CREATE TABLE" <+> renderCreation table columns constraints
129+createTable tab columns constraints = UnsafeDefinition $
130+ "CREATE TABLE" <+> renderCreation tab columns constraints
127131
128132-- | `createTableIfNotExists` creates a table if it doesn't exist, but does not add it to the schema.
129133-- Instead, the schema already has the table so if the table did not yet exist, the schema was wrong.
@@ -147,9 +151,9 @@ createTableIfNotExists
147151 -> NP (Aliased (TableConstraintExpression schema table)) constraints
148152 -- ^ constraints that must hold for the table
149153 -> Definition schema schema
150−createTableIfNotExists table columns constraints = UnsafeDefinition $
154+createTableIfNotExists tab columns constraints = UnsafeDefinition $
151155 "CREATE TABLE IF NOT EXISTS"
152− <+> renderCreation table columns constraints
156+ <+> renderCreation tab columns constraints
153157
154158-- helper function for `createTable` and `createTableIfNotExists`
155159renderCreation
@@ -162,7 +166,7 @@ renderCreation
162166 -> NP (Aliased (TableConstraintExpression schema table)) constraints
163167 -- ^ constraints that must hold for the table
164168 -> ByteString
165−renderCreation table columns constraints = renderAlias table
169+renderCreation tab columns constraints = renderAlias tab
166170 <+> parenthesized
167171 ( renderCommaSeparated renderColumnDef columns
168172 <> ( case constraints of
@@ -453,10 +457,10 @@ DROP statements
453457-- >>> renderDefinition $ dropTable #muh_table
454458-- "DROP TABLE \"muh_table\";"
455459dropTable
456− :: KnownSymbol table
460+ :: Has table schema ('Table t)
457461 => Alias table -- ^ table to remove
458462 -> Definition schema (Drop table schema)
459−dropTable table = UnsafeDefinition $ "DROP TABLE" <+> renderAlias table <> ";"
463+dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderAlias tab <> ";"
460464
461465{-----------------------------------------
462466ALTER statements
@@ -468,9 +472,9 @@ alterTable
468472 => Alias alias -- ^ table to alter
469473 -> AlterTable alias table schema -- ^ alteration to perform
470474 -> Definition schema (Alter alias ('Table table) schema)
471−alterTable table alteration = UnsafeDefinition $
475+alterTable tab alteration = UnsafeDefinition $
472476 "ALTER TABLE"
473− <+> renderAlias table
477+ <+> renderAlias tab
474478 <+> renderAlterTable alteration
475479 <> ";"
476480
@@ -731,3 +735,19 @@ dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
731735-- "ALTER TABLE \"tab\" ALTER COLUMN \"col\" TYPE numeric NOT NULL;"
732736alterType :: TypeExpression ty -> AlterColumn ty0 ty
733737alterType ty = UnsafeAlterColumn $ "TYPE" <+> renderTypeExpression ty
738+
739+createView
740+ :: KnownSymbol view
741+ => Alias view -- ^ the name of the table to add
742+ -> Query schema '[] relation
743+ -- ^ query
744+ -> Definition schema (Create view ('View relation) schema)
745+createView alias query = UnsafeDefinition $
746+ "CREATE" <+> "VIEW" <+> renderAlias alias <+> "AS"
747+ <+> renderQuery query <> ";"
748+
749+dropView
750+ :: Has view schema ('View v)
751+ => Alias view -- ^ view to remove
752+ -> Definition schema (Drop view schema)
753+dropView v = UnsafeDefinition $ "DROP VIEW" <+> renderAlias v <> ";"