@@ -102,7 +102,7 @@ CREATE statements
102102-- >>> :set -XOverloadedLabels
103103-- >>> :{
104104-- renderDefinition $
105−-- createTable #tab (int `As` #a :* real `As` #b :* Nil) []
105+-- createTable #tab (int `As` #a :* real `As` #b :* Nil) Nil
106106-- :}
107107-- "CREATE TABLE tab (a int, b real);"
108108createTable
@@ -122,7 +122,10 @@ createTable table columns constraints = UnsafeDefinition $
122122 "CREATE TABLE" <+> renderAlias table
123123 <+> parenthesized
124124 ( renderCommaSeparated renderColumnDef columns
125− <> renderCommaSeparated renderConstraint constraints )
125+ <> ( case constraints of
126+ Nil -> ""
127+ _ -> ", " <>
128+ renderCommaSeparated renderConstraint constraints ) )
126129 <> ";"
127130 where
128131 renderColumnDef :: Aliased TypeExpression x -> ByteString
@@ -163,9 +166,9 @@ newtype TableConstraintExpression
163166-- createTable #tab
164167-- ( (int & notNull) `As` #a :*
165168-- (int & notNull) `As` #b :* Nil )
166−-- [ check (#a .> #b) ]
169+-- ( check (#a .> #b) `As` #inequality :* Nil )
167170-- :}
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)));"
169172check
170173 :: Condition '[table ::: columns] 'Ungrouped '[]
171174 -- ^ condition to check
@@ -181,14 +184,14 @@ check condition = UnsafeTableConstraintExpression $
181184-- createTable #tab
182185-- ( int `As` #a :*
183186-- int `As` #b :* Nil )
184−-- [ unique (Column #a :* Column #b :* Nil) ]
187+-- ( unique (Column #a :* Column #b :* Nil) `As` #uq_a_b :* Nil )
185188-- :}
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));"
187190unique
188191 :: SOP.SListI subcolumns
189192 => NP (Column columns) subcolumns
190193 -- ^ unique column or group of columns
191− -> TableConstraintExpression schema columns ( 'Uniques (Aliases subcolumns))
194+ -> TableConstraintExpression schema columns ('Unique (Aliases subcolumns))
192195unique columns = UnsafeTableConstraintExpression $
193196 "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
194197
@@ -201,9 +204,9 @@ unique columns = UnsafeTableConstraintExpression $
201204-- createTable #tab
202205-- ( serial `As` #id :*
203206-- (text & notNull) `As` #name :* Nil )
204−-- [ primaryKey (Column #id :* Nil) ]
207+-- ( primaryKey (Column #id :* Nil) `As` #pk_id :* Nil )
205208-- :}
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));"
207210primaryKey
208211 :: (SOP.SListI subcolumns, AllNotNull subcolumns)
209212 => NP (Column columns) subcolumns
@@ -218,33 +221,41 @@ primaryKey columns = UnsafeTableConstraintExpression $
218221-- between two related tables.
219222--
220223-- >>> :{
221−-- let
222−-- definition :: Definition '[]
223−-- '[ "users" :::
224−-- '[ "id" ::: 'Optional ('NotNull 'PGint4)
225−-- , "username" ::: 'Required ('NotNull 'PGtext)
224+-- type Schema =
225+-- '[ "users" :::
226+-- '[ "pk_id" ::: 'PrimaryKey '["id"] ] :=>
227+-- '[ "id" ::: 'Def :=> 'NotNull 'PGint4
228+-- , "name" ::: 'NoDef :=> 'NotNull 'PGtext
226229-- ]
227−-- , "emails" :::
228−-- '[ "id" ::: 'Optional ('NotNull 'PGint4)
229−-- , "userid" ::: 'Required ('NotNull 'PGint4)
230−-- , "email" ::: 'Required ('NotNull 'PGtext)
230+-- , "emails" :::
231+-- '[ "pk_id" ::: 'PrimaryKey '["id"]
232+-- , "fk_user_id" ::: 'ForeignKey '["user_id"] "users" '["id"]
233+-- ] :=>
234+-- '[ "id" ::: 'Def :=> 'NotNull 'PGint4
235+-- , "user_id" ::: 'NoDef :=> 'NotNull 'PGint4
236+-- , "email" ::: 'NoDef :=> 'Null 'PGtext
231237-- ]
232−-- ]
233−-- definition =
238+-- ]
239+-- :}
240+--
241+-- :{
242+-- let
243+-- setup :: Definition '[] Schema
244+-- setup =
234245-- createTable #users
235−-- (serial `As` #id :* (text & notNull) `As` #username :* Nil)
236−-- [primaryKey (Column #id :* Nil)] >>>
246+-- ( #id serial :*
247+-- #name (text & notNull) :* Nil )
248+-- ( #pk_id (primaryKey (Column #id :* Nil)) :* Nil ) >>>
237249-- createTable #emails
238−-- ( serial `As` #id :*
239−-- (integer & notNull) `As` #userid :*
240−-- (text & notNull) `As` #email :* Nil )
241−-- [ primaryKey (Column #id :* Nil)
242−-- , foreignKey (Column #userid :* Nil) #users (Column #id :* Nil)
243−-- OnDeleteCascade OnUpdateRestrict
244−-- ]
245−-- in renderDefinition definition
250+-- ( #id serial :*
251+-- #user_id (int & notNull) :*
252+-- #email text :* Nil )
253+-- ( #pk_id (primaryKey (Column #id :* Nil)) :*
254+-- #fk_user_id (foreignKey (Column #user_id :* Nil) #users (Column #id :* Nil)
255+-- OnDeleteCascade OnUpdateCascade) :* Nil )
256+-- in renderDefinition setup
246257-- :}
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);"
248259foreignKey
249260 :: ( HasTable tab (UnconstrainSchema schema) refcolumns
250261 , SameTypes subcolumns refsubcolumns
@@ -353,33 +364,22 @@ alterTableRename table0 table1 = UnsafeDefinition $
353364 "ALTER TABLE" <+> renderAlias table0
354365 <+> "RENAME TO" <+> renderAlias table1 <> ";"
355366
356−-- | An `alterTableAddConstraint` adds a table constraint.
367+-- | An `addConstraint` adds a table constraint.
357368--
358369-- >>> :{
359370-- let
360371-- definition :: Definition
361−-- '["tab" ::: '["col" ::: 'Required ('NotNull 'PGint4)]]
362−-- '["tab" ::: '["col" ::: 'Required ('NotNull 'PGint4)]]
363−-- definition = alterTableAddConstraint #tab (check (#col .> 0))
372+-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
373+-- '["tab" ::: '["positive" ::: Check ] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
374+-- definition = alterTable #tab (addConstraint #positive (check (#col .> 0)))
364375-- in renderDefinition definition
365376-- :}
366−-- "ALTER TABLE tab ADD 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 <> ";"
375−
377+-- "ALTER TABLE tab ADD CONSTRAINT positive CHECK ((col > 0));"
376378addConstraint
377379 :: KnownSymbol constraintName
378380 => Alias constraintName
379− -> TableConstraintExpression schema (UnconstrainColumns columns) constraint
380− -> AlterTable schema
381− (constraints :=> columns)
382− (Create constraintName constraint constraints :=> columns)
381+ -> TableConstraintExpression schema (UnconstrainColumns (UnconstrainTable table)) constraint
382+ -> AlterTable schema table (AddConstraint constraintName constraint table)
383383addConstraint constraintName constraint = UnsafeAlterTable $
384384 "ADD" <+> "CONSTRAINT" <+> renderAlias constraintName
385385 <+> renderTableConstraintExpression constraint
@@ -421,7 +421,7 @@ addColumnDefault
421421 :: KnownSymbol column
422422 => Alias column -- ^ column to add
423423 -> 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)
425425addColumnDefault column ty = UnsafeAlterTable $
426426 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
427427
@@ -443,7 +443,7 @@ addColumnNull
443443 :: KnownSymbol column
444444 => Alias column -- ^ column to add
445445 -> 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)
447447addColumnNull column ty = UnsafeAlterTable $
448448 "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
449449