@@ -11,15 +11,13 @@ Squeal data definition language.
1111{-# LANGUAGE
1212 AllowAmbiguousTypes
1313 , ConstraintKinds
14− , DeriveAnyClass
1514 , DeriveGeneric
16− , DerivingStrategies
1715 , FlexibleContexts
1816 , FlexibleInstances
1917 , GADTs
18+ , GeneralizedNewtypeDeriving
2019 , LambdaCase
2120 , MultiParamTypeClasses
22− , OverloadedLabels
2321 , OverloadedStrings
2422 , RankNTypes
2523 , ScopedTypeVariables
@@ -27,25 +25,82 @@ Squeal data definition language.
2725 , TypeInType
2826 , TypeOperators
2927 , UndecidableSuperClasses
30− , UndecidableInstances
3128#-}
3229
3330module Squeal.PostgreSQL.Definition
3431 ( -- * Definition
3532 Definition (..)
3633 , (>>>)
37− , manipDefinition
34+ -- * Tables
35+ -- ** Create
36+ , createSchema
37+ , createSchemaIfNotExists
38+ , createTable
39+ , createTableIfNotExists
40+ , createView
41+ , createTypeEnum
42+ , createTypeEnumFrom
43+ , createTypeComposite
44+ , createTypeCompositeFrom
45+ , createDomain
46+ , TableConstraintExpression (..)
47+ , check
48+ , unique
49+ , primaryKey
50+ , foreignKey
51+ , ForeignKeyed
52+ , OnDeleteClause (..)
53+ , OnUpdateClause (..)
54+ -- ** Drop
55+ , dropSchema
56+ , dropTable
57+ , dropView
58+ , dropType
59+ -- ** Alter
60+ , alterTable
61+ , alterTableRename
62+ , AlterTable (..)
63+ , addConstraint
64+ , dropConstraint
65+ , AddColumn (..)
66+ , dropColumn
67+ , renameColumn
68+ , alterColumn
69+ , AlterColumn (..)
70+ , setDefault
71+ , dropDefault
72+ , setNotNull
73+ , dropNotNull
74+ , alterType
75+ -- * Columns
76+ , ColumnTypeExpression (..)
77+ , nullable
78+ , notNullable
79+ , default_
80+ , serial2
81+ , smallserial
82+ , serial4
83+ , serial
84+ , serial8
85+ , bigserial
3886 ) where
3987
4088import Control.Category
4189import Control.DeepSeq
4290import Data.ByteString
4391import Data.Monoid
92+import GHC.TypeLits
4493import Prelude hiding ((.), id)
4594
95+import qualified Generics.SOP as SOP
4696import qualified GHC.Generics as GHC
4797
48−import Squeal.PostgreSQL.Manipulation
98+import Squeal.PostgreSQL.Alias
99+import Squeal.PostgreSQL.Expression
100+import Squeal.PostgreSQL.Expression.Logic
101+import Squeal.PostgreSQL.List
102+import Squeal.PostgreSQL.PG
103+import Squeal.PostgreSQL.Query
49104import Squeal.PostgreSQL.Render
50105import Squeal.PostgreSQL.Schema
51106
@@ -73,10 +128,958 @@ instance Category Definition where
73128 ddl1 . ddl0 = UnsafeDefinition $
74129 renderSQL ddl0 <> "\n" <> renderSQL ddl1
75130
76−-- | A `Manipulation` without input or output can be run as a statement
77−-- along with other `Definition`s, by embedding it using `manipDefinition`.
78−manipDefinition
79− :: Manipulation '[] schemas '[] '[]
80− -- ^ no input or output
131+{-----------------------------------------
132+CREATE statements
133+-----------------------------------------}
134+
135+createSchema
136+ :: KnownSymbol sch
137+ => Alias sch
138+ -> Definition schemas (Create sch '[] schemas)
139+createSchema sch = UnsafeDefinition $
140+ "CREATE" <+> "SCHEMA" <+> renderSQL sch <> ";"
141+
142+createSchemaIfNotExists
143+ :: (KnownSymbol sch, Has sch schemas schema)
144+ => Alias sch
81145 -> Definition schemas schemas
82−manipDefinition = UnsafeDefinition . (<> ";") . renderSQL
146+createSchemaIfNotExists sch = UnsafeDefinition $
147+ "CREATE" <+> "SCHEMA" <+> "IF" <+> "NOT" <+> "EXISTS"
148+ <+> renderSQL sch <> ";"
149+
150+{- | `createTable` adds a table to the schema.
151+
152+>>> :set -XOverloadedLabels
153+>>> :{
154+type Table = '[] :=>
155+ '[ "a" ::: 'NoDef :=> 'Null 'PGint4
156+ , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
157+:}
158+
159+>>> :{
160+let
161+ setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table])
162+ setup = createTable #tab
163+ (nullable int `as` #a :* nullable real `as` #b) Nil
164+in printSQL setup
165+:}
166+CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
167+-}
168+createTable
169+ :: ( KnownSymbol sch
170+ , KnownSymbol tab
171+ , columns ~ (col ': cols)
172+ , SOP.SListI columns
173+ , SOP.SListI constraints
174+ , Has sch schemas0 schema0
175+ , schemas1 ~ Alter sch (Create tab ('Table (constraints :=> columns)) schema0) schemas0 )
176+ => QualifiedAlias sch tab -- ^ the name of the table to add
177+ -> NP (Aliased (ColumnTypeExpression schemas0)) columns
178+ -- ^ the names and datatype of each column
179+ -> NP (Aliased (TableConstraintExpression sch tab schemas1)) constraints
180+ -- ^ constraints that must hold for the table
181+ -> Definition schemas0 schemas1
182+createTable tab columns constraints = UnsafeDefinition $
183+ "CREATE TABLE" <+> renderCreation tab columns constraints
184+
185+{-| `createTableIfNotExists` creates a table if it doesn't exist, but does not add it to the schema.
186+Instead, the schema already has the table so if the table did not yet exist, the schema was wrong.
187+`createTableIfNotExists` fixes this. Interestingly, this property makes it an idempotent in
188+the `Category` of `Definition`s.
189+
190+>>> :set -XOverloadedLabels -XTypeApplications
191+>>> :{
192+type Table = '[] :=>
193+ '[ "a" ::: 'NoDef :=> 'Null 'PGint4
194+ , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
195+:}
196+
197+>>> type Schemas = Public '["tab" ::: 'Table Table]
198+
199+>>> :{
200+let
201+ setup :: Definition Schemas Schemas
202+ setup = createTableIfNotExists #tab
203+ (nullable int `as` #a :* nullable real `as` #b) Nil
204+in printSQL setup
205+:}
206+CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
207+-}
208+createTableIfNotExists
209+ :: ( Has sch schemas schema
210+ , Has tab schema ('Table (constraints :=> columns))
211+ , SOP.SListI columns
212+ , SOP.SListI constraints )
213+ => QualifiedAlias sch tab -- ^ the name of the table to add
214+ -> NP (Aliased (ColumnTypeExpression schemas)) columns
215+ -- ^ the names and datatype of each column
216+ -> NP (Aliased (TableConstraintExpression sch tab schemas)) constraints
217+ -- ^ constraints that must hold for the table
218+ -> Definition schemas schemas
219+createTableIfNotExists tab columns constraints = UnsafeDefinition $
220+ "CREATE TABLE IF NOT EXISTS"
221+ <+> renderCreation tab columns constraints
222+
223+-- helper function for `createTable` and `createTableIfNotExists`
224+renderCreation
225+ :: ( KnownSymbol sch
226+ , KnownSymbol tab
227+ , SOP.SListI columns
228+ , SOP.SListI constraints )
229+ => QualifiedAlias sch tab -- ^ the name of the table to add
230+ -> NP (Aliased (ColumnTypeExpression schemas0)) columns
231+ -- ^ the names and datatype of each column
232+ -> NP (Aliased (TableConstraintExpression sch tab schemas1)) constraints
233+ -- ^ constraints that must hold for the table
234+ -> ByteString
235+renderCreation tab columns constraints = renderSQL tab
236+ <+> parenthesized
237+ ( renderCommaSeparated renderColumnDef columns
238+ <> ( case constraints of
239+ Nil -> ""
240+ _ -> ", " <>
241+ renderCommaSeparated renderConstraint constraints ) )
242+ <> ";"
243+ where
244+ renderColumnDef :: Aliased (ColumnTypeExpression schemas) x -> ByteString
245+ renderColumnDef (ty `As` column) =
246+ renderSQL column <+> renderColumnTypeExpression ty
247+ renderConstraint
248+ :: Aliased (TableConstraintExpression sch tab schemas) constraint
249+ -> ByteString
250+ renderConstraint (constraint `As` alias) =
251+ "CONSTRAINT" <+> renderSQL alias <+> renderSQL constraint
252+
253+-- | Data types are a way to limit the kind of data that can be stored in a
254+-- table. For many applications, however, the constraint they provide is
255+-- too coarse. For example, a column containing a product price should
256+-- probably only accept positive values. But there is no standard data type
257+-- that accepts only positive numbers. Another issue is that you might want
258+-- to constrain column data with respect to other columns or rows.
259+-- For example, in a table containing product information,
260+-- there should be only one row for each product number.
261+-- `TableConstraint`s give you as much control over the data in your tables
262+-- as you wish. If a user attempts to store data in a column that would
263+-- violate a constraint, an error is raised. This applies
264+-- even if the value came from the default value definition.
265+newtype TableConstraintExpression
266+ (sch :: Symbol)
267+ (tab :: Symbol)
268+ (schemas :: SchemasType)
269+ (constraint :: TableConstraint)
270+ = UnsafeTableConstraintExpression
271+ { renderTableConstraintExpression :: ByteString }
272+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
273+instance RenderSQL
274+ (TableConstraintExpression sch tab schemas constraint) where
275+ renderSQL = renderTableConstraintExpression
276+
277+{-| A `check` constraint is the most generic `TableConstraint` type.
278+It allows you to specify that the value in a certain column must satisfy
279+a Boolean (truth-value) expression.
280+
281+>>> :{
282+type Schema = '[
283+ "tab" ::: 'Table ('[ "inequality" ::: 'Check '["a","b"]] :=> '[
284+ "a" ::: 'NoDef :=> 'NotNull 'PGint4,
285+ "b" ::: 'NoDef :=> 'NotNull 'PGint4
286+ ])]
287+:}
288+
289+>>> :{
290+let
291+ definition :: Definition (Public '[]) (Public Schema)
292+ definition = createTable #tab
293+ ( (int & notNullable) `as` #a :*
294+ (int & notNullable) `as` #b )
295+ ( check (#a :* #b) (#a .> #b) `as` #inequality )
296+:}
297+
298+>>> printSQL definition
299+CREATE TABLE "tab" ("a" int NOT NULL, "b" int NOT NULL, CONSTRAINT "inequality" CHECK (("a" > "b")));
300+-}
301+check
302+ :: ( Has sch schemas schema
303+ , Has tab schema ('Table table)
304+ , HasAll aliases (TableToRow table) subcolumns )
305+ => NP Alias aliases
306+ -- ^ specify the subcolumns which are getting checked
307+ -> (forall t. Condition '[] '[] 'Ungrouped schemas '[] '[t ::: subcolumns])
308+ -- ^ a closed `Condition` on those subcolumns
309+ -> TableConstraintExpression sch tab schemas ('Check aliases)
310+check _cols condition = UnsafeTableConstraintExpression $
311+ "CHECK" <+> parenthesized (renderSQL condition)
312+
313+{-| A `unique` constraint ensure that the data contained in a column,
314+or a group of columns, is unique among all the rows in the table.
315+
316+>>> :{
317+type Schema = '[
318+ "tab" ::: 'Table( '[ "uq_a_b" ::: 'Unique '["a","b"]] :=> '[
319+ "a" ::: 'NoDef :=> 'Null 'PGint4,
320+ "b" ::: 'NoDef :=> 'Null 'PGint4
321+ ])]
322+:}
323+
324+>>> :{
325+let
326+ definition :: Definition (Public '[]) (Public Schema)
327+ definition = createTable #tab
328+ ( (int & nullable) `as` #a :*
329+ (int & nullable) `as` #b )
330+ ( unique (#a :* #b) `as` #uq_a_b )
331+:}
332+
333+>>> printSQL definition
334+CREATE TABLE "tab" ("a" int NULL, "b" int NULL, CONSTRAINT "uq_a_b" UNIQUE ("a", "b"));
335+-}
336+unique
337+ :: ( Has sch schemas schema
338+ , Has tab schema('Table table)
339+ , HasAll aliases (TableToRow table) subcolumns )
340+ => NP Alias aliases
341+ -- ^ specify subcolumns which together are unique for each row
342+ -> TableConstraintExpression sch tab schemas ('Unique aliases)
343+unique columns = UnsafeTableConstraintExpression $
344+ "UNIQUE" <+> parenthesized (renderSQL columns)
345+
346+{-| A `primaryKey` constraint indicates that a column, or group of columns,
347+can be used as a unique identifier for rows in the table.
348+This requires that the values be both unique and not null.
349+
350+>>> :{
351+type Schema = '[
352+ "tab" ::: 'Table ('[ "pk_id" ::: 'PrimaryKey '["id"]] :=> '[
353+ "id" ::: 'Def :=> 'NotNull 'PGint4,
354+ "name" ::: 'NoDef :=> 'NotNull 'PGtext
355+ ])]
356+:}
357+
358+>>> :{
359+let
360+ definition :: Definition (Public '[]) (Public Schema)
361+ definition = createTable #tab
362+ ( serial `as` #id :*
363+ (text & notNullable) `as` #name )
364+ ( primaryKey #id `as` #pk_id )
365+:}
366+
367+>>> printSQL definition
368+CREATE TABLE "tab" ("id" serial, "name" text NOT NULL, CONSTRAINT "pk_id" PRIMARY KEY ("id"));
369+-}
370+primaryKey
371+ :: ( Has sch schemas schema
372+ , Has tab schema ('Table table)
373+ , HasAll aliases (TableToColumns table) subcolumns
374+ , AllNotNull subcolumns )
375+ => NP Alias aliases
376+ -- ^ specify the subcolumns which together form a primary key.
377+ -> TableConstraintExpression sch tab schemas ('PrimaryKey aliases)
378+primaryKey columns = UnsafeTableConstraintExpression $
379+ "PRIMARY KEY" <+> parenthesized (renderSQL columns)
380+
381+{-| A `foreignKey` specifies that the values in a column
382+(or a group of columns) must match the values appearing in some row of
383+another table. We say this maintains the referential integrity
384+between two related tables.
385+
386+>>> :{
387+type Schema =
388+ '[ "users" ::: 'Table (
389+ '[ "pk_users" ::: 'PrimaryKey '["id"] ] :=>
390+ '[ "id" ::: 'Def :=> 'NotNull 'PGint4
391+ , "name" ::: 'NoDef :=> 'NotNull 'PGtext
392+ ])
393+ , "emails" ::: 'Table (
394+ '[ "pk_emails" ::: 'PrimaryKey '["id"]
395+ , "fk_user_id" ::: 'ForeignKey '["user_id"] "users" '["id"]
396+ ] :=>
397+ '[ "id" ::: 'Def :=> 'NotNull 'PGint4
398+ , "user_id" ::: 'NoDef :=> 'NotNull 'PGint4
399+ , "email" ::: 'NoDef :=> 'Null 'PGtext
400+ ])
401+ ]
402+:}
403+
404+>>> :{
405+let
406+ setup :: Definition (Public '[]) (Public Schema)
407+ setup =
408+ createTable #users
409+ ( serial `as` #id :*
410+ (text & notNullable) `as` #name )
411+ ( primaryKey #id `as` #pk_users ) >>>
412+ createTable #emails
413+ ( serial `as` #id :*
414+ (int & notNullable) `as` #user_id :*
415+ (text & nullable) `as` #email )
416+ ( primaryKey #id `as` #pk_emails :*
417+ foreignKey #user_id #users #id
418+ OnDeleteCascade OnUpdateCascade `as` #fk_user_id )
419+in printSQL setup
420+:}
421+CREATE TABLE "users" ("id" serial, "name" text NOT NULL, CONSTRAINT "pk_users" PRIMARY KEY ("id"));
422+CREATE TABLE "emails" ("id" serial, "user_id" int NOT NULL, "email" text NULL, CONSTRAINT "pk_emails" PRIMARY KEY ("id"), CONSTRAINT "fk_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE);
423+
424+A `foreignKey` can even be a table self-reference.
425+
426+>>> :{
427+type Schema =
428+ '[ "employees" ::: 'Table (
429+ '[ "employees_pk" ::: 'PrimaryKey '["id"]
430+ , "employees_employer_fk" ::: 'ForeignKey '["employer_id"] "employees" '["id"]
431+ ] :=>
432+ '[ "id" ::: 'Def :=> 'NotNull 'PGint4
433+ , "name" ::: 'NoDef :=> 'NotNull 'PGtext
434+ , "employer_id" ::: 'NoDef :=> 'Null 'PGint4
435+ ])
436+ ]
437+:}
438+
439+>>> :{
440+let
441+ setup :: Definition (Public '[]) (Public Schema)
442+ setup =
443+ createTable #employees
444+ ( serial `as` #id :*
445+ (text & notNullable) `as` #name :*
446+ (integer & nullable) `as` #employer_id )
447+ ( primaryKey #id `as` #employees_pk :*
448+ foreignKey #employer_id #employees #id
449+ OnDeleteCascade OnUpdateCascade `as` #employees_employer_fk )
450+in printSQL setup
451+:}
452+CREATE TABLE "employees" ("id" serial, "name" text NOT NULL, "employer_id" integer NULL, CONSTRAINT "employees_pk" PRIMARY KEY ("id"), CONSTRAINT "employees_employer_fk" FOREIGN KEY ("employer_id") REFERENCES "employees" ("id") ON DELETE CASCADE ON UPDATE CASCADE);
453+-}
454+foreignKey
455+ :: (ForeignKeyed schemas sch schema child parent
456+ table reftable
457+ columns refcolumns
458+ constraints cols
459+ reftys tys )
460+ => NP Alias columns
461+ -- ^ column or columns in the table
462+ -> Alias parent
463+ -- ^ reference table
464+ -> NP Alias refcolumns
465+ -- ^ reference column or columns in the reference table
466+ -> OnDeleteClause
467+ -- ^ what to do when reference is deleted
468+ -> OnUpdateClause
469+ -- ^ what to do when reference is updated
470+ -> TableConstraintExpression sch child schemas
471+ ('ForeignKey columns parent refcolumns)
472+foreignKey keys parent refs ondel onupd = UnsafeTableConstraintExpression $
473+ "FOREIGN KEY" <+> parenthesized (renderSQL keys)
474+ <+> "REFERENCES" <+> renderSQL parent
475+ <+> parenthesized (renderSQL refs)
476+ <+> renderSQL ondel
477+ <+> renderSQL onupd
478+
479+-- | A constraint synonym between types involved in a foreign key constraint.
480+type ForeignKeyed schemas
481+ sch
482+ schema
483+ child parent
484+ table reftable
485+ columns refcolumns
486+ constraints cols
487+ reftys tys =
488+ ( Has sch schemas schema
489+ , Has child schema ('Table table)
490+ , Has parent schema ('Table reftable)
491+ , HasAll columns (TableToColumns table) tys
492+ , reftable ~ (constraints :=> cols)
493+ , HasAll refcolumns cols reftys
494+ , SOP.AllZip SamePGType tys reftys
495+ , Uniquely refcolumns constraints )
496+
497+-- | `OnDeleteClause` indicates what to do with rows that reference a deleted row.
498+data OnDeleteClause
499+ = OnDeleteNoAction
500+ -- ^ if any referencing rows still exist when the constraint is checked,
501+ -- an error is raised
502+ | OnDeleteRestrict -- ^ prevents deletion of a referenced row
503+ | OnDeleteCascade
504+ -- ^ specifies that when a referenced row is deleted,
505+ -- row(s) referencing it should be automatically deleted as well
506+ deriving (GHC.Generic,Show,Eq,Ord)
507+instance NFData OnDeleteClause
508+-- | Render `OnDeleteClause`.
509+instance RenderSQL OnDeleteClause where
510+ renderSQL = \case
511+ OnDeleteNoAction -> "ON DELETE NO ACTION"
512+ OnDeleteRestrict -> "ON DELETE RESTRICT"
513+ OnDeleteCascade -> "ON DELETE CASCADE"
514+
515+-- | Analagous to `OnDeleteClause` there is also `OnUpdateClause` which is invoked
516+-- when a referenced column is changed (updated).
517+data OnUpdateClause
518+ = OnUpdateNoAction
519+ -- ^ if any referencing rows has not changed when the constraint is checked,
520+ -- an error is raised
521+ | OnUpdateRestrict -- ^ prevents update of a referenced row
522+ | OnUpdateCascade
523+ -- ^ the updated values of the referenced column(s) should be copied
524+ -- into the referencing row(s)
525+ deriving (GHC.Generic,Show,Eq,Ord)
526+instance NFData OnUpdateClause
527+
528+-- | Render `OnUpdateClause`.
529+instance RenderSQL OnUpdateClause where
530+ renderSQL = \case
531+ OnUpdateNoAction -> "ON UPDATE NO ACTION"
532+ OnUpdateRestrict -> "ON UPDATE RESTRICT"
533+ OnUpdateCascade -> "ON UPDATE CASCADE"
534+
535+{-----------------------------------------
536+DROP statements
537+-----------------------------------------}
538+
539+-- >>> :{
540+-- let
541+-- definition :: Definition '["muh_schema" ::: schema, "public" ::: public] '["public" ::: public]
542+-- definition = dropSchema #muh_schema
543+-- :}
544+--
545+-- >>> printSQL definition
546+-- DROP SCHEMA "muh_schema";
547+dropSchema
548+ :: Has sch schemas schema
549+ => Alias sch
550+ -> Definition schemas (Drop sch schemas)
551+dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
552+
553+-- | `dropTable` removes a table from the schema.
554+--
555+-- >>> :{
556+-- let
557+-- definition :: Definition '["public" ::: '["muh_table" ::: 'Table t]] (Public '[])
558+-- definition = dropTable #muh_table
559+-- :}
560+--
561+-- >>> printSQL definition
562+-- DROP TABLE "muh_table";
563+dropTable
564+ :: ( Has sch schemas schema
565+ , Has tab schema ('Table table))
566+ => QualifiedAlias sch tab -- ^ table to remove
567+ -> Definition schemas (Alter sch (Drop tab schema) schemas)
568+dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderSQL tab <> ";"
569+
570+{-----------------------------------------
571+ALTER statements
572+-----------------------------------------}
573+
574+-- | `alterTable` changes the definition of a table from the schema.
575+alterTable
576+ :: (Has sch schemas schema, Has tab schema ('Table table0))
577+ => QualifiedAlias sch tab -- ^ table to alter
578+ -> AlterTable sch tab schemas table1 -- ^ alteration to perform
579+ -> Definition schemas (Alter sch (Alter tab ('Table table1) schema) schemas)
580+alterTable tab alteration = UnsafeDefinition $
581+ "ALTER TABLE"
582+ <+> renderSQL tab
583+ <+> renderAlterTable alteration
584+ <> ";"
585+
586+-- | `alterTableRename` changes the name of a table from the schema.
587+--
588+-- >>> printSQL $ alterTableRename #foo #bar
589+-- ALTER TABLE "foo" RENAME TO "bar";
590+alterTableRename
591+ :: (KnownSymbol table0, KnownSymbol table1)
592+ => Alias table0 -- ^ table to rename
593+ -> Alias table1 -- ^ what to rename it
594+ -> Definition schema (Rename table0 table1 schema)
595+alterTableRename table0 table1 = UnsafeDefinition $
596+ "ALTER TABLE" <+> renderSQL table0
597+ <+> "RENAME TO" <+> renderSQL table1 <> ";"
598+
599+-- | An `AlterTable` describes the alteration to perform on the columns
600+-- of a table.
601+newtype AlterTable
602+ (sch :: Symbol)
603+ (tab :: Symbol)
604+ (schemas :: SchemasType)
605+ (table :: TableType) =
606+ UnsafeAlterTable {renderAlterTable :: ByteString}
607+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
608+
609+-- | An `addConstraint` adds a table constraint.
610+--
611+-- >>> :{
612+-- let
613+-- definition :: Definition
614+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
615+-- '["public" ::: '["tab" ::: 'Table ('["positive" ::: 'Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
616+-- definition = alterTable #tab (addConstraint #positive (check #col (#col .> 0)))
617+-- in printSQL definition
618+-- :}
619+-- ALTER TABLE "tab" ADD CONSTRAINT "positive" CHECK (("col" > 0));
620+addConstraint
621+ :: ( KnownSymbol alias
622+ , Has sch schemas schema
623+ , Has tab schema ('Table table0)
624+ , table0 ~ (constraints :=> columns)
625+ , table1 ~ (Create alias constraint constraints :=> columns) )
626+ => Alias alias
627+ -> TableConstraintExpression sch tab schemas constraint
628+ -- ^ constraint to add
629+ -> AlterTable sch tab schemas table1
630+addConstraint alias constraint = UnsafeAlterTable $
631+ "ADD" <+> "CONSTRAINT" <+> renderSQL alias
632+ <+> renderSQL constraint
633+
634+-- | A `dropConstraint` drops a table constraint.
635+--
636+-- >>> :{
637+-- let
638+-- definition :: Definition
639+-- '["public" ::: '["tab" ::: 'Table ('["positive" ::: Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
640+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
641+-- definition = alterTable #tab (dropConstraint #positive)
642+-- in printSQL definition
643+-- :}
644+-- ALTER TABLE "tab" DROP CONSTRAINT "positive";
645+dropConstraint
646+ :: ( KnownSymbol constraint
647+ , Has sch schemas schema
648+ , Has tab schema ('Table table0)
649+ , table0 ~ (constraints :=> columns)
650+ , table1 ~ (Drop constraint constraints :=> columns) )
651+ => Alias constraint
652+ -- ^ constraint to drop
653+ -> AlterTable sch tab schemas table1
654+dropConstraint constraint = UnsafeAlterTable $
655+ "DROP" <+> "CONSTRAINT" <+> renderSQL constraint
656+
657+-- | An `AddColumn` is either @NULL@ or has @DEFAULT@.
658+class AddColumn ty where
659+ -- | `addColumn` adds a new column, initially filled with whatever
660+ -- default value is given or with @NULL@.
661+ --
662+ -- >>> :{
663+ -- let
664+ -- definition :: Definition
665+ -- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]]
666+ -- '["public" ::: '["tab" ::: 'Table ('[] :=>
667+ -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
668+ -- , "col2" ::: 'Def :=> 'Null 'PGtext ])]]
669+ -- definition = alterTable #tab (addColumn #col2 (text & nullable & default_ "foo"))
670+ -- in printSQL definition
671+ -- :}
672+ -- ALTER TABLE "tab" ADD COLUMN "col2" text NULL DEFAULT E'foo';
673+ --
674+ -- >>> :{
675+ -- let
676+ -- definition :: Definition
677+ -- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]]
678+ -- '["public" ::: '["tab" ::: 'Table ('[] :=>
679+ -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
680+ -- , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]]
681+ -- definition = alterTable #tab (addColumn #col2 (text & nullable))
682+ -- in printSQL definition
683+ -- :}
684+ -- ALTER TABLE "tab" ADD COLUMN "col2" text NULL;
685+ addColumn
686+ :: ( KnownSymbol column
687+ , Has sch schemas schema
688+ , Has tab schema ('Table table0)
689+ , table0 ~ (constraints :=> columns) )
690+ => Alias column -- ^ column to add
691+ -> ColumnTypeExpression schemas ty -- ^ type of the new column
692+ -> AlterTable sch tab schemas (constraints :=> Create column ty columns)
693+ addColumn column ty = UnsafeAlterTable $
694+ "ADD COLUMN" <+> renderSQL column <+> renderColumnTypeExpression ty
695+instance {-# OVERLAPPING #-} AddColumn ('Def :=> ty)
696+instance {-# OVERLAPPABLE #-} AddColumn ('NoDef :=> 'Null ty)
697+
698+-- | A `dropColumn` removes a column. Whatever data was in the column
699+-- disappears. Table constraints involving the column are dropped, too.
700+-- However, if the column is referenced by a foreign key constraint of
701+-- another table, PostgreSQL will not silently drop that constraint.
702+--
703+-- >>> :{
704+-- let
705+-- definition :: Definition
706+-- '["public" ::: '["tab" ::: 'Table ('[] :=>
707+-- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
708+-- , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]]
709+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]]
710+-- definition = alterTable #tab (dropColumn #col2)
711+-- in printSQL definition
712+-- :}
713+-- ALTER TABLE "tab" DROP COLUMN "col2";
714+dropColumn
715+ :: ( KnownSymbol column
716+ , Has sch schemas schema
717+ , Has tab schema ('Table table0)
718+ , table0 ~ (constraints :=> columns)
719+ , table1 ~ (constraints :=> Drop column columns) )
720+ => Alias column -- ^ column to remove
721+ -> AlterTable sch tab schemas table1
722+dropColumn column = UnsafeAlterTable $
723+ "DROP COLUMN" <+> renderSQL column
724+
725+-- | A `renameColumn` renames a column.
726+--
727+-- >>> :{
728+-- let
729+-- definition :: Definition
730+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["foo" ::: 'NoDef :=> 'Null 'PGint4])]]
731+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["bar" ::: 'NoDef :=> 'Null 'PGint4])]]
732+-- definition = alterTable #tab (renameColumn #foo #bar)
733+-- in printSQL definition
734+-- :}
735+-- ALTER TABLE "tab" RENAME COLUMN "foo" TO "bar";
736+renameColumn
737+ :: ( KnownSymbol column0
738+ , KnownSymbol column1
739+ , Has sch schemas schema
740+ , Has tab schema ('Table table0)
741+ , table0 ~ (constraints :=> columns)
742+ , table1 ~ (constraints :=> Rename column0 column1 columns) )
743+ => Alias column0 -- ^ column to rename
744+ -> Alias column1 -- ^ what to rename the column
745+ -> AlterTable sch tab schemas table1
746+renameColumn column0 column1 = UnsafeAlterTable $
747+ "RENAME COLUMN" <+> renderSQL column0 <+> "TO" <+> renderSQL column1
748+
749+-- | An `alterColumn` alters a single column.
750+alterColumn
751+ :: ( KnownSymbol column
752+ , Has sch schemas schema
753+ , Has tab schema ('Table table0)
754+ , table0 ~ (constraints :=> columns)
755+ , Has column columns ty0
756+ , table1 ~ (constraints :=> Alter column ty1 columns))
757+ => Alias column -- ^ column to alter
758+ -> AlterColumn schemas ty0 ty1 -- ^ alteration to perform
759+ -> AlterTable sch tab schemas table1
760+alterColumn column alteration = UnsafeAlterTable $
761+ "ALTER COLUMN" <+> renderSQL column <+> renderAlterColumn alteration
762+
763+-- | An `AlterColumn` describes the alteration to perform on a single column.
764+newtype AlterColumn (schemas :: SchemasType) (ty0 :: ColumnType) (ty1 :: ColumnType) =
765+ UnsafeAlterColumn {renderAlterColumn :: ByteString}
766+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
767+
768+-- | A `setDefault` sets a new default for a column. Note that this doesn't
769+-- affect any existing rows in the table, it just changes the default for
770+-- future insert and update commands.
771+--
772+-- >>> :{
773+-- let
774+-- definition :: Definition
775+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
776+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'Def :=> 'Null 'PGint4])]]
777+-- definition = alterTable #tab (alterColumn #col (setDefault 5))
778+-- in printSQL definition
779+-- :}
780+-- ALTER TABLE "tab" ALTER COLUMN "col" SET DEFAULT 5;
781+setDefault
782+ :: Expression '[] '[] 'Ungrouped schemas '[] '[] ty -- ^ default value to set
783+ -> AlterColumn schemas (constraint :=> ty) ('Def :=> ty)
784+setDefault expression = UnsafeAlterColumn $
785+ "SET DEFAULT" <+> renderExpression expression
786+
787+-- | A `dropDefault` removes any default value for a column.
788+--
789+-- >>> :{
790+-- let
791+-- definition :: Definition
792+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'Def :=> 'Null 'PGint4])]]
793+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
794+-- definition = alterTable #tab (alterColumn #col dropDefault)
795+-- in printSQL definition
796+-- :}
797+-- ALTER TABLE "tab" ALTER COLUMN "col" DROP DEFAULT;
798+dropDefault :: AlterColumn schemas ('Def :=> ty) ('NoDef :=> ty)
799+dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
800+
801+-- | A `setNotNull` adds a @NOT NULL@ constraint to a column.
802+-- The constraint will be checked immediately, so the table data must satisfy
803+-- the constraint before it can be added.
804+--
805+-- >>> :{
806+-- let
807+-- definition :: Definition
808+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
809+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
810+-- definition = alterTable #tab (alterColumn #col setNotNull)
811+-- in printSQL definition
812+-- :}
813+-- ALTER TABLE "tab" ALTER COLUMN "col" SET NOT NULL;
814+setNotNull
815+ :: AlterColumn schemas (constraint :=> 'Null ty) (constraint :=> 'NotNull ty)
816+setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
817+
818+-- | A `dropNotNull` drops a @NOT NULL@ constraint from a column.
819+--
820+-- >>> :{
821+-- let
822+-- definition :: Definition
823+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
824+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
825+-- definition = alterTable #tab (alterColumn #col dropNotNull)
826+-- in printSQL definition
827+-- :}
828+-- ALTER TABLE "tab" ALTER COLUMN "col" DROP NOT NULL;
829+dropNotNull
830+ :: AlterColumn schemas (constraint :=> 'NotNull ty) (constraint :=> 'Null ty)
831+dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
832+
833+-- | An `alterType` converts a column to a different data type.
834+-- This will succeed only if each existing entry in the column can be
835+-- converted to the new type by an implicit cast.
836+--
837+-- >>> :{
838+-- let
839+-- definition :: Definition
840+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
841+-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGnumeric])]]
842+-- definition =
843+-- alterTable #tab (alterColumn #col (alterType (numeric & notNullable)))
844+-- in printSQL definition
845+-- :}
846+-- ALTER TABLE "tab" ALTER COLUMN "col" TYPE numeric NOT NULL;
847+alterType :: ColumnTypeExpression schemas ty -> AlterColumn schemas ty0 ty
848+alterType ty = UnsafeAlterColumn $ "TYPE" <+> renderColumnTypeExpression ty
849+
850+{- | Create a view.
851+>>> type ABC = '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4]
852+>>> type BC = '["b" ::: 'Null 'PGint4, "c" ::: 'Null 'PGint4]
853+>>> :{
854+let
855+ definition :: Definition
856+ '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC)]]
857+ '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC), "bc" ::: 'View BC]]
858+ definition =
859+ createView #bc (select_ (#b :* #c) (from (table #abc)))
860+in printSQL definition
861+:}
862+CREATE VIEW "bc" AS SELECT "b" AS "b", "c" AS "c" FROM "abc" AS "abc";
863+-}
864+createView
865+ :: (KnownSymbol sch, KnownSymbol vw, Has sch schemas schema)
866+ => QualifiedAlias sch vw -- ^ the name of the view to add
867+ -> Query '[] '[] schemas '[] view -- ^ query
868+ -> Definition schemas (Alter sch (Create vw ('View view) schema) schemas)
869+createView alias query = UnsafeDefinition $
870+ "CREATE" <+> "VIEW" <+> renderSQL alias <+> "AS"
871+ <+> renderQuery query <> ";"
872+
873+-- | Drop a view.
874+--
875+-- >>> :{
876+-- let
877+-- definition :: Definition
878+-- '[ "public" ::: '["abc" ::: 'Table ('[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4])
879+-- , "bc" ::: 'View ('["b" ::: 'Null 'PGint4, "c" ::: 'Null 'PGint4])]]
880+-- '[ "public" ::: '["abc" ::: 'Table ('[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4])]]
881+-- definition = dropView #bc
882+-- in printSQL definition
883+-- :}
884+-- DROP VIEW "bc";
885+dropView
886+ :: (Has sch schemas schema, Has vw schema ('View view))
887+ => QualifiedAlias sch vw -- ^ view to remove
888+ -> Definition schemas (Alter sch (Drop vw schema) schemas)
889+dropView vw = UnsafeDefinition $ "DROP VIEW" <+> renderSQL vw <> ";"
890+
891+-- | Enumerated types are created using the `createTypeEnum` command, for example
892+--
893+-- >>> printSQL $ (createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy") :: Definition (Public '[]) '["public" ::: '["mood" ::: 'Typedef ('PGenum '["sad","ok","happy"])]])
894+-- CREATE TYPE "mood" AS ENUM ('sad', 'ok', 'happy');
895+createTypeEnum
896+ :: (KnownSymbol enum, Has sch schemas schema, SOP.All KnownSymbol labels)
897+ => QualifiedAlias sch enum
898+ -- ^ name of the user defined enumerated type
899+ -> NP PGlabel labels
900+ -- ^ labels of the enumerated type
901+ -> Definition schemas (Alter sch (Create enum ('Typedef ('PGenum labels)) schema) schemas)
902+createTypeEnum enum labels = UnsafeDefinition $
903+ "CREATE" <+> "TYPE" <+> renderSQL enum <+> "AS" <+> "ENUM" <+>
904+ parenthesized (renderSQL labels) <> ";"
905+
906+-- | Enumerated types can also be generated from a Haskell type, for example
907+--
908+-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
909+-- >>> instance SOP.Generic Schwarma
910+-- >>> instance SOP.HasDatatypeInfo Schwarma
911+-- >>> :{
912+-- let
913+-- createSchwarma :: Definition (Public '[]) '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]]
914+-- createSchwarma = createTypeEnumFrom @Schwarma #schwarma
915+-- in
916+-- printSQL createSchwarma
917+-- :}
918+-- CREATE TYPE "schwarma" AS ENUM ('Beef', 'Lamb', 'Chicken');
919+createTypeEnumFrom
920+ :: forall hask sch enum schemas schema.
921+ ( SOP.Generic hask
922+ , SOP.All KnownSymbol (LabelsPG hask)
923+ , KnownSymbol enum
924+ , Has sch schemas schema )
925+ => QualifiedAlias sch enum
926+ -- ^ name of the user defined enumerated type
927+ -> Definition schemas (Alter sch (Create enum ('Typedef (PG (Enumerated hask))) schema) schemas)
928+createTypeEnumFrom enum = createTypeEnum enum
929+ (SOP.hpure label :: NP PGlabel (LabelsPG hask))
930+
931+{- | `createTypeComposite` creates a composite type. The composite type is
932+specified by a list of attribute names and data types.
933+
934+>>> :{
935+type PGcomplex = 'PGcomposite
936+ '[ "real" ::: 'NotNull 'PGfloat8
937+ , "imaginary" ::: 'NotNull 'PGfloat8 ]
938+:}
939+
940+>>> :{
941+let
942+ setup :: Definition (Public '[]) '["public" ::: '["complex" ::: 'Typedef PGcomplex]]
943+ setup = createTypeComposite #complex
944+ (float8 `as` #real :* float8 `as` #imaginary)
945+in printSQL setup
946+:}
947+CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
948+-}
949+createTypeComposite
950+ :: (KnownSymbol ty, Has sch schemas schema, SOP.SListI fields)
951+ => QualifiedAlias sch ty
952+ -- ^ name of the user defined composite type
953+ -> NP (Aliased (TypeExpression schemas)) fields
954+ -- ^ list of attribute names and data types
955+ -> Definition schemas (Alter sch (Create ty ('Typedef ('PGcomposite fields)) schema) schemas)
956+createTypeComposite ty fields = UnsafeDefinition $
957+ "CREATE" <+> "TYPE" <+> renderSQL ty <+> "AS" <+> parenthesized
958+ (renderCommaSeparated renderField fields) <> ";"
959+ where
960+ renderField :: Aliased (TypeExpression schemas) x -> ByteString
961+ renderField (typ `As` alias) =
962+ renderSQL alias <+> renderSQL typ
963+
964+-- | Composite types can also be generated from a Haskell type, for example
965+--
966+-- >>> data Complex = Complex {real :: Double, imaginary :: Double} deriving GHC.Generic
967+-- >>> instance SOP.Generic Complex
968+-- >>> instance SOP.HasDatatypeInfo Complex
969+-- >>> type Schema = '["complex" ::: 'Typedef (PG (Composite Complex))]
970+-- >>> :{
971+-- let
972+-- createComplex :: Definition (Public '[]) (Public Schema)
973+-- createComplex = createTypeCompositeFrom @Complex #complex
974+-- in
975+-- printSQL createComplex
976+-- :}
977+-- CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
978+createTypeCompositeFrom
979+ :: forall hask sch ty schemas schema.
980+ ( SOP.All (FieldTyped schemas) (RowPG hask)
981+ , KnownSymbol ty
982+ , Has sch schemas schema )
983+ => QualifiedAlias sch ty
984+ -- ^ name of the user defined composite type
985+ -> Definition schemas (Alter sch (Create ty ( 'Typedef (PG (Composite hask))) schema) schemas)
986+createTypeCompositeFrom ty = createTypeComposite ty
987+ (SOP.hcpure (SOP.Proxy :: SOP.Proxy (FieldTyped schemas)) fieldtype
988+ :: NP (Aliased (TypeExpression schemas)) (RowPG hask))
989+
990+{-|
991+`createDomain` creates a new domain. A domain is essentially a data type
992+with constraints (restrictions on the allowed set of values).
993+
994+Domains are useful for abstracting common constraints on fields
995+into a single location for maintenance. For example, several tables might
996+contain email address columns, all requiring the same `check` constraint
997+to verify the address syntax. Define a domain rather than setting up
998+each table's constraint individually.
999+
1000+>>> :{
1001+let
1002+ createPositive :: Definition (Public '[]) (Public '["positive" ::: 'Typedef 'PGfloat4])
1003+ createPositive = createDomain #positive real (#value .> 0 .&& (#value & isNotNull))
1004+in printSQL createPositive
1005+:}
1006+CREATE DOMAIN "positive" AS real CHECK ((("value" > 0) AND "value" IS NOT NULL));
1007+-}
1008+createDomain
1009+ :: (Has sch schemas schema, KnownSymbol dom)
1010+ => QualifiedAlias sch dom
1011+ -> (forall nullity. TypeExpression schemas (nullity ty))
1012+ -> (forall tab. Condition '[] '[] 'Ungrouped schemas '[] '[tab ::: '["value" ::: 'Null ty]])
1013+ -> Definition schemas (Alter sch (Create alias ('Typedef ty) schema) schemas)
1014+createDomain dom ty condition =
1015+ UnsafeDefinition $ "CREATE DOMAIN" <+> renderSQL dom
1016+ <+> "AS" <+> renderTypeExpression ty
1017+ <+> "CHECK" <+> parenthesized (renderSQL condition) <> ";"
1018+
1019+class FieldTyped schemas ty where
1020+ fieldtype :: Aliased (TypeExpression schemas) ty
1021+instance (KnownSymbol alias, PGTyped schemas ty)
1022+ => FieldTyped schemas (alias ::: ty) where
1023+ fieldtype = pgtype `As` Alias
1024+
1025+-- | Drop a type.
1026+--
1027+-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
1028+-- >>> instance SOP.Generic Schwarma
1029+-- >>> instance SOP.HasDatatypeInfo Schwarma
1030+-- >>> printSQL (dropType #schwarma :: Definition '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]] (Public '[]))
1031+-- DROP TYPE "schwarma";
1032+dropType
1033+ :: (Has sch schemas schema, Has td schema ('Typedef ty))
1034+ => QualifiedAlias sch td
1035+ -- ^ name of the user defined type
1036+ -> Definition schemas (Alter sch (Drop td schema) schemas)
1037+dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderSQL tydef <> ";"
1038+
1039+-- | `ColumnTypeExpression`s are used in `createTable` commands.
1040+newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
1041+ = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }
1042+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
1043+instance RenderSQL (ColumnTypeExpression schemas ty) where
1044+ renderSQL = renderColumnTypeExpression
1045+
1046+-- | used in `createTable` commands as a column constraint to note that
1047+-- @NULL@ may be present in a column
1048+nullable
1049+ :: TypeExpression schemas (nullity ty)
1050+ -> ColumnTypeExpression schemas ('NoDef :=> 'Null ty)
1051+nullable ty = UnsafeColumnTypeExpression $ renderSQL ty <+> "NULL"
1052+
1053+-- | used in `createTable` commands as a column constraint to ensure
1054+-- @NULL@ is not present in a column
1055+notNullable
1056+ :: TypeExpression schemas (nullity ty)
1057+ -> ColumnTypeExpression schemas ('NoDef :=> 'NotNull ty)
1058+notNullable ty = UnsafeColumnTypeExpression $ renderSQL ty <+> "NOT NULL"
1059+
1060+-- | used in `createTable` commands as a column constraint to give a default
1061+default_
1062+ :: Expression '[] '[] 'Ungrouped schemas '[] '[] ty
1063+ -> ColumnTypeExpression schemas ('NoDef :=> ty)
1064+ -> ColumnTypeExpression schemas ('Def :=> ty)
1065+default_ x ty = UnsafeColumnTypeExpression $
1066+ renderSQL ty <+> "DEFAULT" <+> renderExpression x
1067+
1068+-- | not a true type, but merely a notational convenience for creating
1069+-- unique identifier columns with type `PGint2`
1070+serial2, smallserial
1071+ :: ColumnTypeExpression schemas ('Def :=> 'NotNull 'PGint2)
1072+serial2 = UnsafeColumnTypeExpression "serial2"
1073+smallserial = UnsafeColumnTypeExpression "smallserial"
1074+-- | not a true type, but merely a notational convenience for creating
1075+-- unique identifier columns with type `PGint4`
1076+serial4, serial
1077+ :: ColumnTypeExpression schemas ('Def :=> 'NotNull 'PGint4)
1078+serial4 = UnsafeColumnTypeExpression "serial4"
1079+serial = UnsafeColumnTypeExpression "serial"
1080+-- | not a true type, but merely a notational convenience for creating
1081+-- unique identifier columns with type `PGint8`
1082+serial8, bigserial
1083+ :: ColumnTypeExpression schemas ('Def :=> 'NotNull 'PGint8)
1084+serial8 = UnsafeColumnTypeExpression "serial8"
1085+bigserial = UnsafeColumnTypeExpression "bigserial"