@@ -102,7 +102,7 @@ CREATE statements
102-- >>> :set -XOverloadedLabels
102-- >>> :set -XOverloadedLabels
104-- renderDefinition $
104-- renderDefinition $
105−-- createTable #tab (int `As` #a :* real `As` #b :* Nil) []
105+-- createTable #tab (int `As` #a :* real `As` #b :* Nil) Nil
107-- "CREATE TABLE tab (a int, b real);"
107-- "CREATE TABLE tab (a int, b real);"
108createTable
108createTable
@@ -122,7 +122,10 @@ createTable table columns constraints = UnsafeDefinition $
122 "CREATE TABLE" <+> renderAlias table
122 "CREATE TABLE" <+> renderAlias table
123 <+> parenthesized
123 <+> parenthesized
124 ( renderCommaSeparated renderColumnDef columns
124 ( renderCommaSeparated renderColumnDef columns
125+ <> ( case constraints of
125− <> renderCommaSeparated renderConstraint constraints )
128+ renderCommaSeparated renderConstraint constraints ) )
128 renderColumnDef :: Aliased TypeExpression x -> ByteString
131 renderColumnDef :: Aliased TypeExpression x -> ByteString
@@ -163,9 +166,9 @@ newtype TableConstraintExpression
163-- createTable #tab
166-- createTable #tab
164-- ( (int & notNull) `As` #a :*
167-- ( (int & notNull) `As` #a :*
165-- (int & notNull) `As` #b :* Nil )
168-- (int & notNull) `As` #b :* Nil )
166−-- [ check (#a .> #b) ]
169+-- ( check (#a .> #b) `As` #inequality :* Nil )
168−-- "CREATE TABLE tab (a int NOT NULL, b int NOT NULL, CHECK ((a > b)));"
171+-- "CREATE TABLE tab (a int NOT NULL, b int NOT NULL, CONSTRAINT inequality CHECK ((a > b)));"
170 :: Condition '[table ::: columns] 'Ungrouped '[]
173 :: Condition '[table ::: columns] 'Ungrouped '[]
171 -- ^ condition to check
174 -- ^ condition to check
@@ -181,14 +184,14 @@ check condition = UnsafeTableConstraintExpression $
181-- createTable #tab
184-- createTable #tab
182-- ( int `As` #a :*
185-- ( int `As` #a :*
183-- int `As` #b :* Nil )
186-- int `As` #b :* Nil )
184−-- [ unique (Column #a :* Column #b :* Nil) ]
187+-- ( unique (Column #a :* Column #b :* Nil) `As` #uq_a_b :* Nil )
186−-- "CREATE TABLE tab (a int, b int, UNIQUE (a, b));"
189+-- "CREATE TABLE tab (a int, b int, CONSTRAINT uq_a_b UNIQUE (a, b));"
188 :: SOP.SListI subcolumns
191 :: SOP.SListI subcolumns
189 => NP (Column columns) subcolumns
192 => NP (Column columns) subcolumns
190 -- ^ unique column or group of columns
193 -- ^ unique column or group of columns
191− -> TableConstraintExpression schema columns ( 'Uniques (Aliases subcolumns))
194+ -> TableConstraintExpression schema columns ('Unique (Aliases subcolumns))
192unique columns = UnsafeTableConstraintExpression $
195unique columns = UnsafeTableConstraintExpression $
193 "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
196 "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
@@ -201,9 +204,9 @@ unique columns = UnsafeTableConstraintExpression $
201-- createTable #tab
204-- createTable #tab
202-- ( serial `As` #id :*
205-- ( serial `As` #id :*
203-- (text & notNull) `As` #name :* Nil )
206-- (text & notNull) `As` #name :* Nil )
204−-- [ primaryKey (Column #id :* Nil) ]
207+-- ( primaryKey (Column #id :* Nil) `As` #pk_id :* Nil )
206−-- "CREATE TABLE tab (id serial, name text NOT NULL, PRIMARY KEY (id));"
209+-- "CREATE TABLE tab (id serial, name text NOT NULL, CONSTRAINT pk_id PRIMARY KEY (id));"
207primaryKey
210primaryKey
208 :: (SOP.SListI subcolumns, AllNotNull subcolumns)
211 :: (SOP.SListI subcolumns, AllNotNull subcolumns)
209 => NP (Column columns) subcolumns
212 => NP (Column columns) subcolumns
@@ -218,33 +221,41 @@ primaryKey columns = UnsafeTableConstraintExpression $
218-- between two related tables.
221-- between two related tables.
221−-- let
224+-- type Schema =
222−-- definition :: Definition '[]
223−-- '[ "users" :::
225+-- '[ "users" :::
226+-- '[ "pk_id" ::: 'PrimaryKey '["id"] ] :=>
224−-- '[ "id" ::: 'Optional ('NotNull 'PGint4)
227+-- '[ "id" ::: 'Def :=> 'NotNull 'PGint4
225−-- , "username" ::: 'Required ('NotNull 'PGtext)
228+-- , "name" ::: 'NoDef :=> 'NotNull 'PGtext
227−-- , "emails" :::
230+-- , "emails" :::
231+-- '[ "pk_id" ::: 'PrimaryKey '["id"]
232+-- , "fk_user_id" ::: 'ForeignKey '["user_id"] "users" '["id"]
228−-- '[ "id" ::: 'Optional ('NotNull 'PGint4)
234+-- '[ "id" ::: 'Def :=> 'NotNull 'PGint4
229−-- , "userid" ::: 'Required ('NotNull 'PGint4)
235+-- , "user_id" ::: 'NoDef :=> 'NotNull 'PGint4
230−-- , "email" ::: 'Required ('NotNull 'PGtext)
236+-- , "email" ::: 'NoDef :=> 'Null 'PGtext
233−-- definition =
239+-- :}
243+-- setup :: Definition '[] Schema
234-- createTable #users
245-- createTable #users
235−-- (serial `As` #id :* (text & notNull) `As` #username :* Nil)
247+-- #name (text & notNull) :* Nil )
236−-- [primaryKey (Column #id :* Nil)] >>>
248+-- ( #pk_id (primaryKey (Column #id :* Nil)) :* Nil ) >>>
237-- createTable #emails
249-- createTable #emails
238−-- ( serial `As` #id :*
250+-- ( #id serial :*
239−-- (integer & notNull) `As` #userid :*
251+-- #user_id (int & notNull) :*
240−-- (text & notNull) `As` #email :* Nil )
252+-- #email text :* Nil )
241−-- [ primaryKey (Column #id :* Nil)
253+-- ( #pk_id (primaryKey (Column #id :* Nil)) :*
242−-- , foreignKey (Column #userid :* Nil) #users (Column #id :* Nil)
254+-- #fk_user_id (foreignKey (Column #user_id :* Nil) #users (Column #id :* Nil)
243−-- OnDeleteCascade OnUpdateRestrict
255+-- OnDeleteCascade OnUpdateCascade) :* Nil )
245−-- in renderDefinition definition
256+-- in renderDefinition setup
247−-- "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);"
258+-- "CREATE TABLE users (id serial, username text NOT NULL, CONSTRAINT pk_id PRIMARY KEY (id)); CREATE TABLE emails (id serial, userid integer NOT NULL, email text NOT NULL, CONSTRAINT pk_id PRIMARY KEY (id), CONSTRAINT fk_user_id FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT);"
248foreignKey
259foreignKey
249 :: ( HasTable tab (UnconstrainSchema schema) refcolumns
260 :: ( HasTable tab (UnconstrainSchema schema) refcolumns
250 , SameTypes subcolumns refsubcolumns
261 , SameTypes subcolumns refsubcolumns
@@ -353,33 +364,22 @@ alterTableRename table0 table1 = UnsafeDefinition $
353 "ALTER TABLE" <+> renderAlias table0
364 "ALTER TABLE" <+> renderAlias table0
354 <+> "RENAME TO" <+> renderAlias table1 <> ";"
365 <+> "RENAME TO" <+> renderAlias table1 <> ";"
356−-- | An `alterTableAddConstraint` adds a table constraint.
367+-- | An `addConstraint` adds a table constraint.
360-- definition :: Definition
371-- definition :: Definition
361−-- '["tab" ::: '["col" ::: 'Required ('NotNull 'PGint4)]]
372+-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
362−-- '["tab" ::: '["col" ::: 'Required ('NotNull 'PGint4)]]
373+-- '["tab" ::: '["positive" ::: Check ] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
363−-- definition = alterTableAddConstraint #tab (check (#col .> 0))
374+-- definition = alterTable #tab (addConstraint #positive (check (#col .> 0)))
364-- in renderDefinition definition
375-- in renderDefinition definition
366−-- "ALTER TABLE tab ADD CHECK ((col > 0));"
377+-- "ALTER TABLE tab ADD CONSTRAINT positive CHECK ((col > 0));"
367−-- alterTableAddConstraint
368−-- :: (HasTable table schema columns, (table ::: table0) `In` schema)
369−-- => Alias table -- ^ table to constrain
370−-- -> TableConstraintExpression schema columns constraint -- ^ what constraint to add
371−-- -> Definition schema (Alter table schema (AddConstraint constraint table0))
372−-- alterTableAddConstraint table constraint = UnsafeDefinition $
373−-- "ALTER TABLE" <+> renderAlias table
374−-- <+> "ADD" <+> renderTableConstraintExpression constraint <> ";"
376addConstraint
378addConstraint
377 :: KnownSymbol constraintName
379 :: KnownSymbol constraintName
378 => Alias constraintName
380 => Alias constraintName
379− -> TableConstraintExpression schema (UnconstrainColumns columns) constraint
381+ -> TableConstraintExpression schema (UnconstrainColumns (UnconstrainTable table)) constraint
380− -> AlterTable schema
381− (constraints :=> columns)
382− (Create constraintName constraint constraints :=> columns)
382+ -> AlterTable schema table (AddConstraint constraintName constraint table)
383addConstraint constraintName constraint = UnsafeAlterTable $
383addConstraint constraintName constraint = UnsafeAlterTable $
384 "ADD" <+> "CONSTRAINT" <+> renderAlias constraintName
384 "ADD" <+> "CONSTRAINT" <+> renderAlias constraintName
385 <+> renderTableConstraintExpression constraint
385 <+> renderTableConstraintExpression constraint
@@ -421,7 +421,7 @@ addColumnDefault
421 :: KnownSymbol column
421 :: KnownSymbol column
422 => Alias column -- ^ column to add
422 => Alias column -- ^ column to add
423 -> TypeExpression ('Def :=> ty) -- ^ type of the new column
423 -> TypeExpression ('Def :=> ty) -- ^ type of the new column
424− -> AlterTable schema table (Add column ('Def :=> ty) table)
424+ -> AlterTable schema table (AddColumn column ('Def :=> ty) table)
425addColumnDefault column ty = UnsafeAlterTable $
425addColumnDefault column ty = UnsafeAlterTable $
426 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
426 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
@@ -443,7 +443,7 @@ addColumnNull
443 :: KnownSymbol column
443 :: KnownSymbol column
444 => Alias column -- ^ column to add
444 => Alias column -- ^ column to add
445 -> TypeExpression ('NoDef :=> 'Null ty) -- ^ type of the new column
445 -> TypeExpression ('NoDef :=> 'Null ty) -- ^ type of the new column
446− -> AlterTable schema table (Add column ('NoDef :=> 'Null ty) table)
446+ -> AlterTable schema table (AddColumn column ('NoDef :=> 'Null ty) table)
447addColumnNull column ty = UnsafeAlterTable $
447addColumnNull column ty = UnsafeAlterTable $
448 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
448 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty