on dev help sign in
keep trying buddy
b4ef11eEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+58−45
@@ -44,7 +44,7 @@ module Squeal.PostgreSQL.Definition
44 , alterTable
44 , alterTable
45 , alterTableRename
45 , alterTableRename
46 , alterTableAddConstraint
46 , alterTableAddConstraint
47− , AlterColumns (UnsafeAlterColumns, renderAlterColumns)
47+ , AlterTable (UnsafeAlterTable, renderAlterTable)
48 , addColumnDefault
48 , addColumnDefault
49 , addColumnNull
49 , addColumnNull
50 , dropColumn
50 , dropColumn
@@ -82,8 +82,8 @@ statements
82-- database, like a `createTable`, `dropTable`, or `alterTable` command.
82-- database, like a `createTable`, `dropTable`, or `alterTable` command.
83-- `Definition`s may be composed using the `>>>` operator.
83-- `Definition`s may be composed using the `>>>` operator.
84newtype Definition
84newtype Definition
85− (schema0 :: TablesType)
85+ (schema0 :: SchemaType)
86− (schema1 :: TablesType)
86+ (schema1 :: SchemaType)
87 = UnsafeDefinition { renderDefinition :: ByteString }
87 = UnsafeDefinition { renderDefinition :: ByteString }
88 deriving (GHC.Generic,Show,Eq,Ord,NFData)
88 deriving (GHC.Generic,Show,Eq,Ord,NFData)
89
89
@@ -105,13 +105,15 @@ CREATE statements
105-- :}
105-- :}
106-- "CREATE TABLE tab (a int, b real);"
106-- "CREATE TABLE tab (a int, b real);"
107createTable
107createTable
108+ :: ( KnownSymbol tab
108− :: (KnownSymbol table, SOP.SListI columns)
109+ , SOP.SListI columns
110+ , table ~ (constraints :=> (column ': columns)))
109− => Alias table -- ^ the name of the table to add
111+ => Alias tab -- ^ the name of the table to add
110 -> NP (Aliased TypeExpression) (column ': columns)
112 -> NP (Aliased TypeExpression) (column ': columns)
111 -- ^ the names and datatype of each column
113 -- ^ the names and datatype of each column
112− -> [TableConstraint schema (column ': columns)]
114+ -> NP (TableConstraint schema (column ': columns)) constraints
113 -- ^ constraints that must hold for the table
115 -- ^ constraints that must hold for the table
114− -> Definition schema (Create table (column ': columns) schema)
116+ -> Definition schema (Create tab table schema)
115createTable table columns constraints = UnsafeDefinition $
117createTable table columns constraints = UnsafeDefinition $
116 "CREATE TABLE" <+> renderAlias table
118 "CREATE TABLE" <+> renderAlias table
117 <+> parenthesized
119 <+> parenthesized
@@ -140,8 +142,9 @@ createTable table columns constraints = UnsafeDefinition $
140-- violate a constraint, an error is raised. This applies
142-- violate a constraint, an error is raised. This applies
141-- even if the value came from the default value definition.
143-- even if the value came from the default value definition.
142newtype TableConstraint
144newtype TableConstraint
143− (schema :: TablesType)
145+ (schema :: SchemaType)
144 (columns :: ColumnsType)
146 (columns :: ColumnsType)
147+ (tableConstraint :: TableConstraint')
145 = UnsafeTableConstraint { renderTableConstraint :: ByteString }
148 = UnsafeTableConstraint { renderTableConstraint :: ByteString }
146 deriving (GHC.Generic,Show,Eq,Ord,NFData)
149 deriving (GHC.Generic,Show,Eq,Ord,NFData)
147
150
@@ -160,7 +163,7 @@ newtype TableConstraint
160check
163check
161 :: Condition '[table ::: columns] 'Ungrouped '[]
164 :: Condition '[table ::: columns] 'Ungrouped '[]
162 -- ^ condition to check
165 -- ^ condition to check
163− -> TableConstraint schema columns
166+ -> TableConstraint schema columns 'Check
164check condition = UnsafeTableConstraint $
167check condition = UnsafeTableConstraint $
165 "CHECK" <+> parenthesized (renderExpression condition)
168 "CHECK" <+> parenthesized (renderExpression condition)
166
169
@@ -179,7 +182,7 @@ unique
179 :: SOP.SListI subcolumns
182 :: SOP.SListI subcolumns
180 => NP (Column columns) subcolumns
183 => NP (Column columns) subcolumns
181 -- ^ unique column or group of columns
184 -- ^ unique column or group of columns
182− -> TableConstraint schema columns
185+ -> TableConstraint schema columns ( 'Uniques subcolumns)
183unique columns = UnsafeTableConstraint $
186unique columns = UnsafeTableConstraint $
184 "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
187 "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
185
188
@@ -199,7 +202,7 @@ primaryKey
199 :: (SOP.SListI subcolumns, AllNotNull subcolumns)
202 :: (SOP.SListI subcolumns, AllNotNull subcolumns)
200 => NP (Column columns) subcolumns
203 => NP (Column columns) subcolumns
201 -- ^ identifying column or group of columns
204 -- ^ identifying column or group of columns
202− -> TableConstraint schema columns
205+ -> TableConstraint schema columns ('Uniques subcolumns)
203primaryKey columns = UnsafeTableConstraint $
206primaryKey columns = UnsafeTableConstraint $
204 "PRIMARY KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
207 "PRIMARY KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
205
208
@@ -237,8 +240,8 @@ primaryKey columns = UnsafeTableConstraint $
237-- :}
240-- :}
238-- "CREATE TABLE users (id serial, username text NOT NULL, PRIMARY KEY (id)); CREATE TABLE emails (id serial, userid integer NOT NULL, email text NOT NULL, PRIMARY KEY (id), FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT);"
241-- "CREATE TABLE users (id serial, username text NOT NULL, PRIMARY KEY (id)); CREATE TABLE emails (id serial, userid integer NOT NULL, email text NOT NULL, PRIMARY KEY (id), FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT);"
239foreignKey
242foreignKey
240− :: ( HasTable reftable schema refcolumns
243+ :: ( {-(reftable ::: refcolumns) `In` schema
241− , SameTypes subcolumns refsubcolumns
244+ , -}SameTypes subcolumns refsubcolumns
242 , AllNotNull subcolumns
245 , AllNotNull subcolumns
243 , SOP.SListI subcolumns
246 , SOP.SListI subcolumns
244 , SOP.SListI refsubcolumns)
247 , SOP.SListI refsubcolumns)
@@ -253,6 +256,7 @@ foreignKey
253 -> OnUpdateClause
256 -> OnUpdateClause
254 -- ^ what to do when reference is updated
257 -- ^ what to do when reference is updated
255 -> TableConstraint schema columns
258 -> TableConstraint schema columns
259+ ('ForeignKey subcolumns reftable refsubcolumns)
256foreignKey columns reftable refcolumns onDelete onUpdate =
260foreignKey columns reftable refcolumns onDelete onUpdate =
257 UnsafeTableConstraint $
261 UnsafeTableConstraint $
258 "FOREIGN KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
262 "FOREIGN KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
@@ -320,14 +324,14 @@ ALTER statements
320
324
321-- | `alterTable` changes the definition of a table from the schema.
325-- | `alterTable` changes the definition of a table from the schema.
322alterTable
326alterTable
323− :: HasTable table schema columns0
327+ :: (tab ::: table0) `In` schema
324− => Alias table -- ^ table to alter
328+ => Alias tab -- ^ table to alter
325− -> AlterColumns columns0 columns1 -- ^ alteration to perform
329+ -> AlterTable schema table0 table1 -- ^ alteration to perform
326− -> Definition schema (Alter table schema columns1)
330+ -> Definition schema (Alter tab schema table1)
327alterTable table alteration = UnsafeDefinition $
331alterTable table alteration = UnsafeDefinition $
328 "ALTER TABLE"
332 "ALTER TABLE"
329 <+> renderAlias table
333 <+> renderAlias table
330− <+> renderAlterColumns alteration
334+ <+> renderAlterTable alteration
331 <> ";"
335 <> ";"
332
336
333-- | `alterTableRename` changes the name of a table from the schema.
337-- | `alterTableRename` changes the name of a table from the schema.
@@ -354,21 +358,30 @@ alterTableRename table0 table1 = UnsafeDefinition $
354-- in renderDefinition definition
358-- in renderDefinition definition
355-- :}
359-- :}
356-- "ALTER TABLE tab ADD CHECK ((col > 0));"
360-- "ALTER TABLE tab ADD CHECK ((col > 0));"
357−alterTableAddConstraint
361+-- alterTableAddConstraint
358− :: HasTable table schema columns
362+-- :: (HasTable table schema columns, (table ::: table0) `In` schema)
359− => Alias table -- ^ table to constrain
363+-- => Alias table -- ^ table to constrain
360− -> TableConstraint schema columns -- ^ what constraint to add
364+-- -> TableConstraint schema columns constraint -- ^ what constraint to add
361− -> Definition schema schema
365+-- -> Definition schema (Alter table schema (AddConstraint constraint table0))
362−alterTableAddConstraint table constraint = UnsafeDefinition $
366+-- alterTableAddConstraint table constraint = UnsafeDefinition $
363− "ALTER TABLE" <+> renderAlias table
367+-- "ALTER TABLE" <+> renderAlias table
364− <+> "ADD" <+> renderTableConstraint constraint <> ";"
368+-- <+> "ADD" <+> renderTableConstraint constraint <> ";"
365−
369+
370+addConstraint
371+ :: ( table `In` schema
372+ , table ~ (tab ::: (constraints :=> columns)))
373+ => Alias tab
374+ -> TableConstraint schema columns constraint
375+ -> AlterTable table (tab ::: AddConstraint constraint constraints columns)
376+addConstraint = undefined
377+
366−-- | An `AlterColumns` describes the alteration to perform on the columns
378+-- | An `AlterTable` describes the alteration to perform on the columns
367-- of a table.
379-- of a table.
368−newtype AlterColumns
380+newtype AlterTable
369− (columns0 :: ColumnsType)
381+ (schema :: SchemaType)
370− (columns1 :: ColumnsType) =
382+ (table0 :: TableType)
371− UnsafeAlterColumns {renderAlterColumns :: ByteString}
383+ (table1 :: TableType) =
384+ UnsafeAlterTable {renderAlterTable :: ByteString}
372 deriving (GHC.Generic,Show,Eq,Ord,NFData)
385 deriving (GHC.Generic,Show,Eq,Ord,NFData)
373
386
374-- | An `addColumnDefault` adds a new `Optional` column. The new column is
387-- | An `addColumnDefault` adds a new `Optional` column. The new column is
@@ -390,8 +403,8 @@ addColumnDefault
390 :: (KnownSymbol column, 'Default `In` constraints)
403 :: (KnownSymbol column, 'Default `In` constraints)
391 => Alias column -- ^ column to add
404 => Alias column -- ^ column to add
392 -> TypeExpression '(constraints, ty) -- ^ type of the new column
405 -> TypeExpression '(constraints, ty) -- ^ type of the new column
393− -> AlterColumns columns (Create column '(constraints, ty) columns)
406+ -> AlterTable schema columns (Create column '(constraints, ty) columns)
394−addColumnDefault column ty = UnsafeAlterColumns $
407+addColumnDefault column ty = UnsafeAlterTable $
395 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
408 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
396
409
397-- | An `addColumnNull` adds a new `Null` column. The new column is initially
410-- | An `addColumnNull` adds a new `Null` column. The new column is initially
@@ -412,8 +425,8 @@ addColumnNull
412 :: KnownSymbol column
425 :: KnownSymbol column
413 => Alias column -- ^ column to add
426 => Alias column -- ^ column to add
414 -> TypeExpression '( '[], ('Null ty)) -- ^ type of the new column
427 -> TypeExpression '( '[], ('Null ty)) -- ^ type of the new column
415− -> AlterColumns columns (Create column '( '[], ('Null ty)) columns)
428+ -> AlterTable schema columns (Create column '( '[], ('Null ty)) columns)
416−addColumnNull column ty = UnsafeAlterColumns $
429+addColumnNull column ty = UnsafeAlterTable $
417 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
430 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
418
431
419-- | A `dropColumn` removes a column. Whatever data was in the column
432-- | A `dropColumn` removes a column. Whatever data was in the column
@@ -435,8 +448,8 @@ addColumnNull column ty = UnsafeAlterColumns $
435dropColumn
448dropColumn
436 :: KnownSymbol column
449 :: KnownSymbol column
437 => Alias column -- ^ column to remove
450 => Alias column -- ^ column to remove
438− -> AlterColumns columns (Drop column columns)
451+ -> AlterTable schema columns (Drop column columns)
439−dropColumn column = UnsafeAlterColumns $
452+dropColumn column = UnsafeAlterTable $
440 "DROP COLUMN" <+> renderAlias column
453 "DROP COLUMN" <+> renderAlias column
441
454
442-- | Like `dropColumn` but authorizes dropping everything that depends
455-- | Like `dropColumn` but authorizes dropping everything that depends
@@ -456,8 +469,8 @@ dropColumn column = UnsafeAlterColumns $
456dropColumnCascade
469dropColumnCascade
457 :: KnownSymbol column
470 :: KnownSymbol column
458 => Alias column -- ^ column to remove
471 => Alias column -- ^ column to remove
459− -> AlterColumns columns (Drop column columns)
472+ -> AlterTable schema columns (Drop column columns)
460−dropColumnCascade column = UnsafeAlterColumns $
473+dropColumnCascade column = UnsafeAlterTable $
461 "DROP COLUMN" <+> renderAlias column <+> "CASCADE"
474 "DROP COLUMN" <+> renderAlias column <+> "CASCADE"
462
475
463-- | A `renameColumn` renames a column.
476-- | A `renameColumn` renames a column.
@@ -475,8 +488,8 @@ renameColumn
475 :: (KnownSymbol column0, KnownSymbol column1)
488 :: (KnownSymbol column0, KnownSymbol column1)
476 => Alias column0
489 => Alias column0
477 -> Alias column1
490 -> Alias column1
478− -> AlterColumns columns (Rename column0 column1 columns)
491+ -> AlterTable schema columns (Rename column0 column1 columns)
479−renameColumn column0 column1 = UnsafeAlterColumns $
492+renameColumn column0 column1 = UnsafeAlterTable $
480 "RENAME COLUMN" <+> renderAlias column0 <+> "TO" <+> renderAlias column1
493 "RENAME COLUMN" <+> renderAlias column0 <+> "TO" <+> renderAlias column1
481
494
482-- | An `alterColumn` alters a single column.
495-- | An `alterColumn` alters a single column.
@@ -484,8 +497,8 @@ alterColumn
484 :: (KnownSymbol column, HasColumn column columns ty0)
497 :: (KnownSymbol column, HasColumn column columns ty0)
485 => Alias column -- ^ column to alter
498 => Alias column -- ^ column to alter
486 -> AlterColumn ty0 ty1 -- ^ alteration to perform
499 -> AlterColumn ty0 ty1 -- ^ alteration to perform
487− -> AlterColumns columns (Alter column columns ty1)
500+ -> AlterTable schema columns (Alter column columns ty1)
488−alterColumn column alteration = UnsafeAlterColumns $
501+alterColumn column alteration = UnsafeAlterTable $
489 "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
502 "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
490
503
491-- | An `AlterColumn` describes the alteration to perform on a single column.
504-- | An `AlterColumn` describes the alteration to perform on a single column.
@@ -523,7 +536,7 @@ setDefault expression = UnsafeAlterColumn $
523-- in renderDefinition definition
536-- in renderDefinition definition
524-- :}
537-- :}
525-- "ALTER TABLE tab ALTER COLUMN col DROP DEFAULT;"
538-- "ALTER TABLE tab ALTER COLUMN col DROP DEFAULT;"
526−dropDefault :: AlterColumn '(constraints, ty) '(DropDefault constraints, ty)
539+dropDefault :: AlterColumn ty (DropConstraint 'Default ty)
527dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
540dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
528
541
529-- | A `setNotNull` adds a @NOT NULL@ constraint to a column.
542-- | A `setNotNull` adds a @NOT NULL@ constraint to a column.
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+3−3
@@ -181,7 +181,7 @@ values from primitive expression using arithmetic, logical,
181and other operations.
181and other operations.
182-}
182-}
183newtype Expression
183newtype Expression
184− (tables :: TablesType)
184+ (tables :: RelationType)
185 (grouping :: Grouping)
185 (grouping :: Grouping)
186 (params :: [ColumnType])
186 (params :: [ColumnType])
187 (ty :: ColumnType)
187 (ty :: ColumnType)
@@ -978,7 +978,7 @@ tables
978-- | A `Table` from a schema without its alias with an `IsLabel` instance
978-- | A `Table` from a schema without its alias with an `IsLabel` instance
979-- to call a table reference by its alias.
979-- to call a table reference by its alias.
980newtype Table
980newtype Table
981− (schema :: TablesType)
981+ (schema :: RelationType)
982 (columns :: ColumnsType)
982 (columns :: ColumnsType)
983 = UnsafeTable { renderTable :: ByteString }
983 = UnsafeTable { renderTable :: ByteString }
984 deriving (GHC.Generic,Show,Eq,Ord,NFData)
984 deriving (GHC.Generic,Show,Eq,Ord,NFData)
@@ -1043,7 +1043,7 @@ smallserial = UnsafeTypeExpression "smallserial"
1043-- | not a true type, but merely a notational convenience for creating
1043-- | not a true type, but merely a notational convenience for creating
1044-- unique identifier columns with type `'PGint4`
1044-- unique identifier columns with type `'PGint4`
1045serial4, serial
1045serial4, serial
1046− :: TypeExpression ( '[Default, 'Unique] :=> 'NotNull 'PGint4)
1046+ :: TypeExpression ( '[ 'Default, 'Unique] :=> 'NotNull 'PGint4)
1047serial4 = UnsafeTypeExpression "serial4"
1047serial4 = UnsafeTypeExpression "serial4"
1048serial = UnsafeTypeExpression "serial"
1048serial = UnsafeTypeExpression "serial"
1049-- | not a true type, but merely a notational convenience for creating
1049-- | not a true type, but merely a notational convenience for creating
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+2−2
@@ -59,7 +59,7 @@ import Squeal.PostgreSQL.Schema
59-- `deleteFrom`. A `Query` is also considered a `Manipulation` even though
59-- `deleteFrom`. A `Query` is also considered a `Manipulation` even though
60-- it does not modify data.
60-- it does not modify data.
61newtype Manipulation
61newtype Manipulation
62− (schema :: TablesType)
62+ (schema :: RelationType)
63 (params :: [ColumnType])
63 (params :: [ColumnType])
64 (columns :: ColumnsType)
64 (columns :: ColumnsType)
65 = UnsafeManipulation { renderManipulation :: ByteString }
65 = UnsafeManipulation { renderManipulation :: ByteString }
@@ -193,7 +193,7 @@ insertInto table insert conflict returning = UnsafeManipulation $
193-- | A `ValuesClause` lets you insert either values, free `Expression`s,
193-- | A `ValuesClause` lets you insert either values, free `Expression`s,
194-- or the result of a `Query`.
194-- or the result of a `Query`.
195data ValuesClause
195data ValuesClause
196− (schema :: TablesType)
196+ (schema :: RelationType)
197 (params :: [ColumnType])
197 (params :: [ColumnType])
198 (columns :: ColumnsType)
198 (columns :: ColumnsType)
199 = Values
199 = Values
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+4−4
@@ -100,8 +100,8 @@ import qualified Control.Monad.Trans.RWS.Lazy as Lazy
100import qualified Control.Monad.Trans.RWS.Strict as Strict
100import qualified Control.Monad.Trans.RWS.Strict as Strict
101
101
102-- | A `Connection` consists of a `Database.PostgreSQL.LibPQ`
102-- | A `Connection` consists of a `Database.PostgreSQL.LibPQ`
103−-- `Database.PastgreSQL.LibPQ.Connection` and a phantom `TablesType`
103+-- `Database.PastgreSQL.LibPQ.Connection` and a phantom `RelationType`
104−newtype Connection (schema :: TablesType) =
104+newtype Connection (schema :: RelationType) =
105 Connection { unConnection :: LibPQ.Connection }
105 Connection { unConnection :: LibPQ.Connection }
106
106
107{- | Makes a new connection to the database server.
107{- | Makes a new connection to the database server.
@@ -153,8 +153,8 @@ withConnection connString action = do
153-- | We keep track of the schema via an Atkey indexed state monad transformer,
153-- | We keep track of the schema via an Atkey indexed state monad transformer,
154-- `PQ`.
154-- `PQ`.
155newtype PQ
155newtype PQ
156− (schema0 :: TablesType)
156+ (schema0 :: RelationType)
157− (schema1 :: TablesType)
157+ (schema1 :: RelationType)
158 (m :: Type -> Type)
158 (m :: Type -> Type)
159 (x :: Type) =
159 (x :: Type) =
160 PQ { runPQ :: Connection schema0 -> m (x, Connection schema1) }
160 PQ { runPQ :: Connection schema0 -> m (x, Connection schema1) }
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+4−4
@@ -257,7 +257,7 @@ in renderQuery query
257-}
257-}
258
258
259newtype Query
259newtype Query
260− (schema :: TablesType)
260+ (schema :: RelationType)
261 (params :: [ColumnType])
261 (params :: [ColumnType])
262 (columns :: ColumnsType)
262 (columns :: ColumnsType)
263 = UnsafeQuery { renderQuery :: ByteString }
263 = UnsafeQuery { renderQuery :: ByteString }
@@ -419,9 +419,9 @@ Table Expressions
419-- to a table on disk, a so-called base table, but more complex expressions
419-- to a table on disk, a so-called base table, but more complex expressions
420-- can be used to modify or combine base tables in various ways.
420-- can be used to modify or combine base tables in various ways.
421data TableExpression
421data TableExpression
422− (schema :: TablesType)
422+ (schema :: RelationType)
423 (params :: [ColumnType])
423 (params :: [ColumnType])
424− (tables :: TablesType)
424+ (tables :: RelationType)
425 (grouping :: Grouping)
425 (grouping :: Grouping)
426 = TableExpression
426 = TableExpression
427 { fromClause :: FromClause schema params tables
427 { fromClause :: FromClause schema params tables
@@ -669,7 +669,7 @@ Grouping
669-- column @col@; otherwise @By2 (\#tab \! \#col)@ will reference a table
669-- column @col@; otherwise @By2 (\#tab \! \#col)@ will reference a table
670-- qualified column @tab.col@.
670-- qualified column @tab.col@.
671data By
671data By
672− (tables :: TablesType)
672+ (tables :: RelationType)
673 (by :: (Symbol,Symbol)) where
673 (by :: (Symbol,Symbol)) where
674 By
674 By
675 :: (HasUnique table tables columns, HasColumn column columns ty)
675 :: (HasUnique table tables columns, HasColumn column columns ty)
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+34−23
@@ -28,6 +28,7 @@ Embedding of PostgreSQL type and alias system
28 , TypeFamilies
28 , TypeFamilies
29 , TypeInType
29 , TypeInType
30 , TypeOperators
30 , TypeOperators
31+ , UndecidableInstances
31#-}
32#-}
32
33
33module Squeal.PostgreSQL.Schema
34module Squeal.PostgreSQL.Schema
@@ -36,8 +37,9 @@ module Squeal.PostgreSQL.Schema
36 , NullityType (..)
37 , NullityType (..)
37 , ColumnType
38 , ColumnType
38 , ColumnsType
39 , ColumnsType
40+ , RelationType
39 , TableType
41 , TableType
40− , TablesType
42+ , SchemaType
41 , Grouping (..)
43 , Grouping (..)
42 -- * Constraints
44 -- * Constraints
43 , PGNum
45 , PGNum
@@ -71,11 +73,12 @@ module Squeal.PostgreSQL.Schema
71 , SameFields
73 , SameFields
72 -- * PostgreSQL Constraints
74 -- * PostgreSQL Constraints
73 , (:=>)
75 , (:=>)
74− , (::=>)
75 , Unconstrain
76 , Unconstrain
76 , ColumnConstraint (..)
77 , ColumnConstraint (..)
77− , DropDefault
78+ -- , DropDefault
78− , DropDefaultList
79+ -- , DropDefaultList
80+ , AddConstraint
81+ , DropConstraint
79 , TableConstraint' (..)
82 , TableConstraint' (..)
80 ) where
83 ) where
81
84
@@ -133,13 +136,17 @@ type ColumnType = ([ColumnConstraint],NullityType)
133-- | `ColumnsType` is a kind synonym for a row of `ColumnType`s.
136-- | `ColumnsType` is a kind synonym for a row of `ColumnType`s.
134type ColumnsType = [(Symbol,ColumnType)]
137type ColumnsType = [(Symbol,ColumnType)]
135
138
136−type TableType = ([TableConstraint'],ColumnsType)
137−
138−-- | `TablesType` is a kind synonym for a row of `ColumnsType`s.
139+-- | `RelationType` is a kind synonym for a row of `ColumnsType`s.
139-- It is used as a kind for both a schema, a disjoint union of tables,
140-- It is used as a kind for both a schema, a disjoint union of tables,
140-- and a joined table `Squeal.PostgreSQL.Query.FromClause`,
141-- and a joined table `Squeal.PostgreSQL.Query.FromClause`,
141-- a product of tables.
142-- a product of tables.
142−type TablesType = [(Symbol,ColumnsType)]
143+type RelationType = [(Symbol,ColumnsType)]
144+
145+type TableType = ([TableConstraint'],ColumnsType)
146+type SchemaType = [(Symbol,TableType)]
147+
148+type family Unconstrain (ty :: ([constraint],kind)) :: kind where
149+ Unconstrain '(constraints,ty) = ty
143
150
144-- | `Grouping` is an auxiliary namespace, created by
151-- | `Grouping` is an auxiliary namespace, created by
145-- @GROUP BY@ clauses (`Squeal.PostgreSQL.Query.group`), and used
152-- @GROUP BY@ clauses (`Squeal.PostgreSQL.Query.group`), and used
@@ -270,10 +277,10 @@ type family NullifyColumns (columns :: ColumnsType) :: ColumnsType where
270type family NullifyTable (table :: TableType) :: TableType where
277type family NullifyTable (table :: TableType) :: TableType where
271 NullifyTable '( '[], columns) = '( '[], NullifyColumns columns)
278 NullifyTable '( '[], columns) = '( '[], NullifyColumns columns)
272
279
273−-- | `NullifyTables` is an idempotent that nullifies a `TablesType`
280+-- | `NullifyTables` is an idempotent that nullifies a `RelationType`
274-- used to nullify the left or right hand side of an outer join
281-- used to nullify the left or right hand side of an outer join
275-- in a `Squeal.PostgreSQL.Query.FromClause`.
282-- in a `Squeal.PostgreSQL.Query.FromClause`.
276−type family NullifyTables (tables :: TablesType) :: TablesType where
283+type family NullifyTables (tables :: RelationType) :: RelationType where
277 NullifyTables '[] = '[]
284 NullifyTables '[] = '[]
278 NullifyTables ((tab ::: columns) ': tables) =
285 NullifyTables ((tab ::: columns) ': tables) =
279 (tab ::: NullifyColumns columns) ': NullifyTables tables
286 (tab ::: NullifyColumns columns) ': NullifyTables tables
@@ -335,25 +342,29 @@ type family SameFields
335 = AllZip SameField fields columns
342 = AllZip SameField fields columns
336
343
337type (:=>) constraints ty = '(constraints,ty)
344type (:=>) constraints ty = '(constraints,ty)
338−type (::=>) constraints ty = '(AsSet constraints,ty)
339−
340−type family Unconstrain constrained where
341− Unconstrain '(constraints, ty) = '( '[], ty)
342
345
343data ColumnConstraint
346data ColumnConstraint
344 = Default
347 = Default
345 | Unique
348 | Unique
346 | References Symbol Symbol
349 | References Symbol Symbol
347
350
351+type family AddConstraint constraint constraints ty where
352+ AddConstraint constraint constraints ty
353+ = '(AsSet (constraint ': constraints),ty)
354+
355+type family DropConstraint constraint ty where
356+ DropConstraint constraint '(constraints,ty)
357+ = '(Delete constraint constraints,ty)
358+
348−type DropDefault constraints = AsSet (DropDefaultList constraints)
359+-- type DropDefault constraints = AsSet (DropDefaultList constraints)
349
360
350−type family DropDefaultList constraints where
361+-- type family DropDefaultList constraints where
351− DropDefaultList '[] = '[]
362+-- DropDefaultList '[] = '[]
352− DropDefaultList ( 'Default ': constraints) = constraints
363+-- DropDefaultList ( 'Default ': constraints) = constraints
353− DropDefaultList (constraint ': constraints) =
364+-- DropDefaultList (constraint ': constraints) =
354− constraint ': DropDefaultList constraints
365+-- constraint ': DropDefaultList constraints
355
366
356data TableConstraint'
367data TableConstraint'
357− = Check [Symbol]
368+ = Check
358− | Uniques [Symbol]
369+ | Uniques [(Symbol, ColumnType)]
359− | ForeignKey [Symbol] Symbol [Symbol]
370+ | ForeignKey ColumnsType Symbol ColumnsType