@@ -42,6 +42,7 @@ module Squeal.PostgreSQL.Definition
42 , createTypeEnumFrom
42 , createTypeEnumFrom
43 , createTypeComposite
43 , createTypeComposite
44 , createTypeCompositeFrom
44 , createTypeCompositeFrom
45 , TableConstraintExpression (..)
46 , TableConstraintExpression (..)
@@ -980,6 +981,35 @@ createTypeCompositeFrom ty = createTypeComposite ty
980 (SOP.hcpure (SOP.Proxy :: SOP.Proxy (FieldTyped schemas)) fieldtype
981 (SOP.hcpure (SOP.Proxy :: SOP.Proxy (FieldTyped schemas)) fieldtype
981 :: NP (Aliased (TypeExpression schemas)) (RowPG hask))
982 :: NP (Aliased (TypeExpression schemas)) (RowPG hask))
985+`createDomain` creates a new domain. A domain is essentially a data type
986+with constraints (restrictions on the allowed set of values).
988+Domains are useful for abstracting common constraints on fields
989+into a single location for maintenance. For example, several tables might
990+contain email address columns, all requiring the same `check` constraint
991+to verify the address syntax. Define a domain rather than setting up
992+each table's constraint individually.
996+ createPositive :: Definition (Public '[]) (Public '["positive" ::: 'Typedef 'PGfloat4])
997+ createPositive = createDomain #positive real (#value .> 0 .&& (#value & isNotNull))
998+in printSQL createPositive
1000+CREATE DOMAIN "positive" AS real CHECK ((("value" > 0) AND "value" IS NOT NULL));
1003+ :: (Has sch schemas schema, KnownSymbol dom)
1004+ => QualifiedAlias sch dom
1005+ -> (forall nullity. TypeExpression schemas (nullity ty))
1006+ -> (forall tab. Condition '[] 'Ungrouped '[] schemas '[] '[tab ::: '["value" ::: 'Null ty]])
1007+ -> Definition schemas (Alter sch (Create alias ('Typedef ty) schema) schemas)
1008+createDomain dom ty condition =
1009+ UnsafeDefinition $ "CREATE DOMAIN" <+> renderSQL dom
1010+ <+> "AS" <+> renderTypeExpression ty
1011+ <+> "CHECK" <+> parenthesized (renderSQL condition) <> ";"
983class FieldTyped schemas ty where
1013class FieldTyped schemas ty where
984 fieldtype :: Aliased (TypeExpression schemas) ty
1014 fieldtype :: Aliased (TypeExpression schemas) ty
985instance (KnownSymbol alias, PGTyped schemas ty)
1015instance (KnownSymbol alias, PGTyped schemas ty)