@@ -200,6 +200,20 @@ newtype TableConstraintExpression
200200 { renderTableConstraintExpression :: ByteString }
201201 deriving (GHC.Generic,Show,Eq,Ord,NFData)
202202
203+-- | @Column columns column@ is a witness that `column` is in `columns`.
204+data Column
205+ (columns :: ColumnsType)
206+ (column :: (Symbol,ColumnType))
207+ where
208+ Column
209+ :: Has column columns ty
210+ => Alias column
211+ -> Column columns (column ::: ty)
212+
213+-- | Render a `Column`.
214+renderColumn :: Column columns column -> ByteString
215+renderColumn (Column column) = renderAlias column
216+
203217-- | A `check` constraint is the most generic `TableConstraint` type.
204218-- It allows you to specify that the value in a certain column must satisfy
205219-- a Boolean (truth-value) expression.
@@ -209,30 +223,17 @@ newtype TableConstraintExpression
209223-- createTable #tab
210224-- ( (int & notNull) `As` #a :*
211225-- (int & notNull) `As` #b :* Nil )
212−-- ( check (#a .> #b) `As` #inequality :* Nil )
226+-- ( check (Column #a :* Column #b :* Nil) (#a .> #b) `As` #inequality :* Nil )
213227-- :}
214228-- "CREATE TABLE tab (a int NOT NULL, b int NOT NULL, CONSTRAINT inequality CHECK ((a > b)));"
215229check
216− :: Condition '[table ::: ColumnsToRelation columns] 'Ungrouped '[]
230+ :: NP (Column columns) subcolumns
231+ -> Condition '[table ::: ColumnsToRelation subcolumns] 'Ungrouped '[]
217232 -- ^ condition to check
218− -> TableConstraintExpression schema columns 'Check
219−check condition = UnsafeTableConstraintExpression $
233+ -> TableConstraintExpression schema columns ('Check (AliasesOf subcolumns))
234+check _cols condition = UnsafeTableConstraintExpression $
220235 "CHECK" <+> parenthesized (renderExpression condition)
221236
222−-- | @Column columns column@ is a witness that `column` is in `columns`.
223−data Column
224− (columns :: ColumnsType)
225− (column :: (Symbol,ColumnType))
226− where
227− Column
228− :: Has column columns ty
229− => Alias column
230− -> Column columns (column ::: ty)
231−
232−-- | Render a `Column`.
233−renderColumn :: Column columns column -> ByteString
234−renderColumn (Column column) = renderAlias column
235−
236237-- | A `unique` constraint ensure that the data contained in a column,
237238-- or a group of columns, is unique among all the rows in the table.
238239--
@@ -440,8 +441,8 @@ newtype AlterTable
440441-- let
441442-- definition :: Definition
442443-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
443−-- '["tab" ::: '["positive" ::: Check ] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
444−-- definition = alterTable #tab (addConstraint #positive (check (#col .> 0)))
444+-- '["tab" ::: '["positive" ::: Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
445+-- definition = alterTable #tab (addConstraint #positive (check (Column #col :* Nil) (#col .> 0)))
445446-- in renderDefinition definition
446447-- :}
447448-- "ALTER TABLE tab ADD CONSTRAINT positive CHECK ((col > 0));"
@@ -461,7 +462,7 @@ addConstraint constraintName constraint = UnsafeAlterTable $
461462-- >>> :{
462463-- let
463464-- definition :: Definition
464−-- '["tab" ::: '["positive" ::: Check ] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
465+-- '["tab" ::: '["positive" ::: Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
465466-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
466467-- definition = alterTable #tab (dropConstraint #positive)
467468-- in renderDefinition definition
@@ -537,7 +538,7 @@ dropColumn
537538 => Alias column -- ^ column to remove
538539 -> AlterTable schema
539540 (constraints :=> columns)
540− (constraints :=> Drop column columns)
541+ (DropIfConstraintsInvolve column constraints :=> Drop column columns)
541542dropColumn column = UnsafeAlterTable $
542543 "DROP COLUMN" <+> renderAlias column
543544