on dev help sign in
Added hask and PGNullityTyped
commit7d5cdc46f05258fab22f1c618d86ff25aaa76301 authoriko <ilyakooo0@gmail.com> dateFri 18 Oct 2019, 23:40 (7 years ago) parent045aae6
2 files changed+22−2
squeal-postgresql/exe/Example.hs+2−2 squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+20
squeal-postgresql/exe/Example.hs+2−2
@@ -53,8 +53,8 @@ setup =
5353 >>>
5454 createTable #emails
5555 ( serial `as` #id :*
56− (int & notNullable) `as` #user_id :*
57− (text & nullable) `as` #email )
56+ hask @Int32 `as` #user_id :*
57+ hask @(Maybe Text) `as` #email )
5858 ( primaryKey #id `as` #pk_emails :*
5959 foreignKey #user_id #users #id
6060 OnDeleteCascade OnUpdateCascade `as` #fk_user_id )
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+20
@@ -27,6 +27,7 @@ Squeal data definition language.
2727 , TypeInType
2828 , TypeOperators
2929 , UndecidableSuperClasses
30+ , UndecidableInstances
3031#-}
3132
3233module Squeal.PostgreSQL.Definition
@@ -34,6 +35,8 @@ module Squeal.PostgreSQL.Definition
3435 Definition (..)
3536 , (>>>)
3637 , manipDefinition
38+ , PGNullityTyped (..)
39+ , hask
3740 ) where
3841
3942import Control.Category
@@ -44,7 +47,10 @@ import Prelude hiding ((.), id)
4447
4548import qualified GHC.Generics as GHC
4649
50+import Squeal.PostgreSQL.Definition.Table.Column
51+import Squeal.PostgreSQL.Expression.Type
4752import Squeal.PostgreSQL.Manipulation
53+import Squeal.PostgreSQL.PG
4854import Squeal.PostgreSQL.Render
4955import Squeal.PostgreSQL.Schema
5056
@@ -79,3 +85,17 @@ manipDefinition
7985 -- ^ no input or output
8086 -> Definition schemas schemas
8187manipDefinition = UnsafeDefinition . (<> ";") . renderSQL
88+
89+class PGNullityTyped schemas (nullty :: NullityType) where
90+ pgNullityType :: ColumnTypeExpression schemas ('NoDef :=> nullty)
91+
92+instance PGTyped schemas ('Null ty) => PGNullityTyped schemas ('Null ty) where
93+ pgNullityType = nullable (pgtype @_ @('Null ty))
94+
95+instance PGTyped schemas ('NotNull ty) => PGNullityTyped schemas ('NotNull ty) where
96+ pgNullityType = notNullable (pgtype @_ @('NotNull ty))
97+
98+hask
99+ :: forall h schemas. PGNullityTyped schemas (NullPG h)
100+ => ColumnTypeExpression schemas ('NoDef :=> NullPG h)
101+hask = pgNullityType