on dev help sign in
schema docs
a4002d5Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+32−40
@@ -58,6 +58,8 @@ module Squeal.PostgreSQL.Schema
58 , RelationsToTables
58 , RelationsToTables
59 , ColumnConstraint (..)
59 , ColumnConstraint (..)
60 , TableConstraint (..)
60 , TableConstraint (..)
61+ , TableConstraints
62+ , NilTableConstraints
61 , ConstraintInvolves
63 , ConstraintInvolves
62 , DropIfConstraintsInvolve
64 , DropIfConstraintsInvolve
63 -- * Aliases
65 -- * Aliases
@@ -201,30 +203,21 @@ data ColumnConstraint
201-- | `ColumnType` encodes the allowance of @DEFAULT@ and @NULL@ and the
203-- | `ColumnType` encodes the allowance of @DEFAULT@ and @NULL@ and the
202-- base `PGType` for a column.
204-- base `PGType` for a column.
203--
205--
204−-- >>> :kind 'NoDef :=> 'NotNull 'PGint4
206+-- >>> :set -XTypeFamilies -XTypeInType
205−-- 'NoDef :=> 'NotNull 'PGint4 :: (ColumnConstraint, NullityType)
207+-- >>> import GHC.TypeLits
206−-- >>> :kind 'NoDef :=> 'NotNull 'PGtext
208+-- >>> type family IdColumn :: ColumnType where IdColumn = 'Def :=> 'NotNull 'PGint4
207−-- 'NoDef :=> 'NotNull 'PGtext :: (ColumnConstraint, NullityType)
209+-- >>> type family EmailColumn :: ColumnType where EmailColumn = 'NoDef :=> 'Null 'PGtext
208−-- >>> import Data.Type.Equality
209−-- >>> Refl :: ColumnType :~: (ColumnConstraint, NullityType)
210−-- Refl
211type ColumnType = (ColumnConstraint,NullityType)
210type ColumnType = (ColumnConstraint,NullityType)
212
211
213-- | `ColumnsType` is a row of `ColumnType`s.
212-- | `ColumnsType` is a row of `ColumnType`s.
214--
213--
215−-- >>> import GHC.TypeLits
216-- >>> :{
214-- >>> :{
217−-- type Columns =
215+-- type family UsersColumns :: ColumnsType where
216+-- UsersColumns =
218−-- '[ "name" ::: 'NoDef :=> 'NotNull 'PGtext
217+-- '[ "name" ::: 'NoDef :=> 'NotNull 'PGtext
219−-- , "id" ::: 'Def :=> 'NotNull 'PGint4
218+-- , "id" ::: 'Def :=> 'NotNull 'PGint4
220−-- ]
219+-- ]
221-- :}
220-- :}
222−--
223−-- >>> :kind Columns
224−-- Columns :: [(Symbol, (ColumnConstraint, NullityType))]
225−-- >>> import Data.Type.Equality
226−-- >>> Refl :: ColumnsType :~: [(Symbol, (ColumnConstraint, NullityType))]
227−-- Refl
228type ColumnsType = [(Symbol,ColumnType)]
221type ColumnsType = [(Symbol,ColumnType)]
229
222
230-- | `TableConstraint` encodes various forms of data constraints
223-- | `TableConstraint` encodes various forms of data constraints
@@ -235,25 +228,27 @@ data TableConstraint
235 | PrimaryKey [Symbol]
228 | PrimaryKey [Symbol]
236 | ForeignKey [Symbol] Symbol [Symbol]
229 | ForeignKey [Symbol] Symbol [Symbol]
237
230
231+-- | A `TableConstraints` is a set of aliased `TableConstraint`s.
232+type TableConstraints = [(Symbol,TableConstraint)]
233+-- | A monokinded empty `TableConstraints`.
234+type family NilTableConstraints :: TableConstraints where
235+ NilTableConstraints = '[]
236+
238-- | `TableType` encodes a row of constraints on a table as well as the types
237-- | `TableType` encodes a row of constraints on a table as well as the types
239-- of its columns.
238-- of its columns.
240--
239--
241-- >>> :{
240-- >>> :{
241+-- type family UsersTable :: TableType where
242−-- type UsersTable =
242+-- UsersTable =
243−-- '[ "pk_users" ::: 'PrimaryKey '["id"] ] :=>
243+-- '[ "pk_users" ::: 'PrimaryKey '["id"] ] :=>
244−-- '[ "id" ::: 'Def :=> 'NotNull 'PGint4
244+-- '[ "id" ::: 'Def :=> 'NotNull 'PGint4
245−-- , "name" ::: 'NoDef :=> 'NotNull 'PGtext
245+-- , "name" ::: 'NoDef :=> 'NotNull 'PGtext
246−-- ]
246+-- ]
247-- :}
247-- :}
248−--
249−-- >>> :kind UsersTable
250−-- UsersTable :: ([(Symbol, TableConstraint)],
251−-- [(Symbol, (ColumnConstraint, NullityType))])
252−type TableType = ([(Symbol,TableConstraint)],ColumnsType)
248+type TableType = (TableConstraints,ColumnsType)
253
249
254-- | `TablesType` is a row of `TableType`s, thought of as a union.
250-- | `TablesType` is a row of `TableType`s, thought of as a union.
255--
251--
256−-- >>> :set -XTypeFamilies -XTypeInType
257-- >>> :{
252-- >>> :{
258-- type family Schema :: TablesType where
253-- type family Schema :: TablesType where
259-- Schema =
254-- Schema =
@@ -273,25 +268,22 @@ type TableType = ([(Symbol,TableConstraint)],ColumnsType)
273-- ]
268-- ]
274-- ]
269-- ]
275-- :}
270-- :}
276−--
277type TablesType = [(Symbol,TableType)]
271type TablesType = [(Symbol,TableType)]
278−-- | A monokinded `'[]` of `TablesType`.
272+-- | A monokinded empty `TablesType`.
279type family NilTables :: TablesType where NilTables = '[]
273type family NilTables :: TablesType where NilTables = '[]
280
274
281-- | `RelationType` is a row of `NullityType`
275-- | `RelationType` is a row of `NullityType`
282--
276--
283-- >>> :{
277-- >>> :{
284−-- type Relation =
278+-- type family PersonRelation :: RelationType where
279+-- PersonRelation =
285−-- '[ "name" ::: 'NotNull 'PGtext
280+-- '[ "name" ::: 'NotNull 'PGtext
286−-- , "age" ::: 'NotNull 'PGint4
281+-- , "age" ::: 'NotNull 'PGint4
287−-- , "dateOfBirth" ::: 'Null 'PGdate
282+-- , "dateOfBirth" ::: 'Null 'PGdate
288−-- ]
283+-- ]
289-- :}
284-- :}
290−--
291−-- >>> :kind Relation
292−-- Relation :: [(Symbol, NullityType)]
293type RelationType = [(Symbol,NullityType)]
285type RelationType = [(Symbol,NullityType)]
294−-- | A monokinded `'[]` of `RelationType`.
286+-- | A monokinded empty `RelationType`.
295type family NilRelation :: RelationType where NilRelation = '[]
287type family NilRelation :: RelationType where NilRelation = '[]
296
288
297-- | `RelationsType` is a row of `RelationType`s, thought of as a product.
289-- | `RelationsType` is a row of `RelationType`s, thought of as a product.