on dev help sign in
createDomain
533eae9Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+21
@@ -42,6 +42,7 @@ module Squeal.PostgreSQL.Definition
42 , createTypeEnumFrom
42 , createTypeEnumFrom
43 , createTypeComposite
43 , createTypeComposite
44 , createTypeCompositeFrom
44 , createTypeCompositeFrom
45+ , createDomain
45 , TableConstraintExpression (..)
46 , TableConstraintExpression (..)
46 , check
47 , check
47 , unique
48 , unique
@@ -980,6 +981,26 @@ 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))
982
983
984+{-|
985+>>> :{
986+let
987+ createPositive :: Definition (Public '[]) (Public '["positive" ::: 'Typedef 'PGfloat4])
988+ createPositive = createDomain #positive real (#value .> 0 .&& (#value & isNotNull))
989+in printSQL createPositive
990+:}
991+CREATE DOMAIN "positive" AS real CHECK ((("value" > 0) AND "value" IS NOT NULL));
992+-}
993+createDomain
994+ :: (Has sch schemas schema, KnownSymbol dom)
995+ => QualifiedAlias sch dom
996+ -> (forall nullity. TypeExpression schemas (nullity ty))
997+ -> (forall tab. Condition '[] 'Ungrouped '[] schemas '[] '[tab ::: '["value" ::: 'Null ty]])
998+ -> Definition schemas (Alter sch (Create alias ('Typedef ty) schema) schemas)
999+createDomain dom ty condition =
1000+ UnsafeDefinition $ "CREATE DOMAIN" <+> renderSQL dom
1001+ <+> "AS" <+> renderTypeExpression ty
1002+ <+> "CHECK" <+> parenthesized (renderSQL condition) <> ";"
1003+
983class FieldTyped schemas ty where
1004class FieldTyped schemas ty where
984 fieldtype :: Aliased (TypeExpression schemas) ty
1005 fieldtype :: Aliased (TypeExpression schemas) ty
985instance (KnownSymbol alias, PGTyped schemas ty)
1006instance (KnownSymbol alias, PGTyped schemas ty)