@@ -129,8 +129,17 @@ CREATE statements
130>>> :set -XOverloadedLabels
130>>> :set -XOverloadedLabels
132−printSQL $
132+type Table = '[] :=>
133+ '[ "a" ::: 'NoDef :=> 'Null 'PGint4
134+ , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
139+ setup :: Definition '[] '["tab" ::: 'Table Table]
133− createTable #tab ((int & nullable) `As` #a :* (real & nullable) `As` #b :* Nil) Nil
140+ setup = createTable #tab
141+ (nullable int `as` #a :* nullable real `as` #b) Nil
135CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
144CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
@@ -155,12 +164,19 @@ Instead, the schema already has the table so if the table did not yet exist, the
155the `Category` of `Definition`s.
164the `Category` of `Definition`s.
157>>> :set -XOverloadedLabels -XTypeApplications
166>>> :set -XOverloadedLabels -XTypeApplications
169+ '[ "a" ::: 'NoDef :=> 'Null 'PGint4
158−>>> type Table = '[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGfloat4]
170+ , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
159>>> type Schema = '["tab" ::: 'Table Table]
173>>> type Schema = '["tab" ::: 'Table Table]
162 setup :: Definition Schema Schema
177 setup :: Definition Schema Schema
163− setup = createTableIfNotExists #tab ((int & nullable) `As` #a :* (real & nullable) `As` #b :* Nil) Nil
178+ setup = createTableIfNotExists #tab
179+ (nullable int `as` #a :* nullable real `as` #b) Nil
164in printSQL setup
180in printSQL setup
166CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
182CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
@@ -244,9 +260,9 @@ type Schema = '[
245 definition :: Definition '[] Schema
261 definition :: Definition '[] Schema
246 definition = createTable #tab
262 definition = createTable #tab
247− ( (int & notNullable) `As` #a :*
263+ ( (int & notNullable) `as` #a :*
248− (int & notNullable) `As` #b :* Nil )
264+ (int & notNullable) `as` #b )
249− ( check (#a :* #b :* Nil) (#a .> #b) `As` #inequality :* Nil )
265+ ( check (#a :* #b) (#a .> #b) `as` #inequality )
252>>> printSQL definition
268>>> printSQL definition
@@ -278,9 +294,9 @@ type Schema = '[
279 definition :: Definition '[] Schema
295 definition :: Definition '[] Schema
280 definition = createTable #tab
296 definition = createTable #tab
281− ( (int & nullable) `As` #a :*
297+ ( (int & nullable) `as` #a :*
282− (int & nullable) `As` #b :* Nil )
298+ (int & nullable) `as` #b )
283− ( unique (#a :* #b :* Nil) `As` #uq_a_b :* Nil )
299+ ( unique (#a :* #b) `as` #uq_a_b )
286>>> printSQL definition
302>>> printSQL definition
@@ -311,9 +327,9 @@ type Schema = '[
312 definition :: Definition '[] Schema
328 definition :: Definition '[] Schema
313 definition = createTable #tab
329 definition = createTable #tab
314− ( serial `As` #id :*
330+ ( serial `as` #id :*
315− (text & notNullable) `As` #name :* Nil )
331+ (text & notNullable) `as` #name )
316− ( primaryKey #id `As` #pk_id :* Nil )
332+ ( primaryKey #id `as` #pk_id )
319>>> printSQL definition
335>>> printSQL definition
@@ -357,16 +373,16 @@ let
357 setup :: Definition '[] Schema
373 setup :: Definition '[] Schema
359 createTable #users
375 createTable #users
360− ( serial `As` #id :*
376+ ( serial `as` #id :*
361− (text & notNullable) `As` #name :* Nil )
377+ (text & notNullable) `as` #name )
362− ( primaryKey #id `As` #pk_users :* Nil ) >>>
378+ ( primaryKey #id `as` #pk_users ) >>>
363 createTable #emails
379 createTable #emails
364− ( serial `As` #id :*
380+ ( serial `as` #id :*
365− (int & notNullable) `As` #user_id :*
381+ (int & notNullable) `as` #user_id :*
366− (text & nullable) `As` #email :* Nil )
382+ (text & nullable) `as` #email )
367− ( primaryKey #id `As` #pk_emails :*
383+ ( primaryKey #id `as` #pk_emails :*
368 foreignKey #user_id #users #id
384 foreignKey #user_id #users #id
369− OnDeleteCascade OnUpdateCascade `As` #fk_user_id :* Nil )
385+ OnDeleteCascade OnUpdateCascade `as` #fk_user_id )
370in printSQL setup
386in printSQL setup
372CREATE TABLE "users" ("id" serial, "name" text NOT NULL, CONSTRAINT "pk_users" PRIMARY KEY ("id"));
388CREATE TABLE "users" ("id" serial, "name" text NOT NULL, CONSTRAINT "pk_users" PRIMARY KEY ("id"));
@@ -392,12 +408,12 @@ let
392 setup :: Definition '[] Schema
408 setup :: Definition '[] Schema
394 createTable #employees
410 createTable #employees
395− ( serial `As` #id :*
411+ ( serial `as` #id :*
396− (text & notNullable) `As` #name :*
412+ (text & notNullable) `as` #name :*
397− (integer & nullable) `As` #employer_id :* Nil )
413+ (integer & nullable) `as` #employer_id )
398− ( primaryKey #id `As` #employees_pk :*
414+ ( primaryKey #id `as` #employees_pk :*
399 foreignKey #employer_id #employees #id
415 foreignKey #employer_id #employees #id
400− OnDeleteCascade OnUpdateCascade `As` #employees_employer_fk :* Nil )
416+ OnDeleteCascade OnUpdateCascade `as` #employees_employer_fk )
401in printSQL setup
417in printSQL setup
403CREATE TABLE "employees" ("id" serial, "name" text NOT NULL, "employer_id" integer NULL, CONSTRAINT "employees_pk" PRIMARY KEY ("id"), CONSTRAINT "employees_employer_fk" FOREIGN KEY ("employer_id") REFERENCES "employees" ("id") ON DELETE CASCADE ON UPDATE CASCADE);
419CREATE TABLE "employees" ("id" serial, "name" text NOT NULL, "employer_id" integer NULL, CONSTRAINT "employees_pk" PRIMARY KEY ("id"), CONSTRAINT "employees_employer_fk" FOREIGN KEY ("employer_id") REFERENCES "employees" ("id") ON DELETE CASCADE ON UPDATE CASCADE);
@@ -546,7 +562,7 @@ newtype AlterTable
546-- definition :: Definition
562-- definition :: Definition
547-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
563-- '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
548-- '["tab" ::: 'Table ('["positive" ::: Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
564-- '["tab" ::: 'Table ('["positive" ::: Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]
549−-- definition = alterTable #tab (addConstraint #positive (check (#col :* Nil) (#col .> 0)))
565+-- definition = alterTable #tab (addConstraint #positive (check #col (#col .> 0)))
550-- in printSQL definition
566-- in printSQL definition
552-- ALTER TABLE "tab" ADD CONSTRAINT "positive" CHECK (("col" > 0));
568-- ALTER TABLE "tab" ADD CONSTRAINT "positive" CHECK (("col" > 0));
@@ -784,7 +800,7 @@ alterType ty = UnsafeAlterColumn $ "TYPE" <+> renderColumnTypeExpression ty
784-- '[ "abc" ::: 'Table ('[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4])
800-- '[ "abc" ::: 'Table ('[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4])
785-- , "bc" ::: 'View ('["b" ::: 'Null 'PGint4, "c" ::: 'Null 'PGint4])]
801-- , "bc" ::: 'View ('["b" ::: 'Null 'PGint4, "c" ::: 'Null 'PGint4])]
786-- definition =
802-- definition =
787−-- createView #bc (select (#b :* #c :* Nil) (from (table #abc)))
803+-- createView #bc (select (#b :* #c) (from (table #abc)))
788-- in printSQL definition
804-- in printSQL definition
790-- CREATE VIEW "bc" AS SELECT "b" AS "b", "c" AS "c" FROM "abc" AS "abc";
806-- CREATE VIEW "bc" AS SELECT "b" AS "b", "c" AS "c" FROM "abc" AS "abc";
@@ -818,7 +834,7 @@ dropView v = UnsafeDefinition $ "DROP VIEW" <+> renderAlias v <> ";"
819-- | Enumerated types are created using the `createTypeEnum` command, for example
835-- | Enumerated types are created using the `createTypeEnum` command, for example
821−-- >>> printSQL $ createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy" :* Nil)
837+-- >>> printSQL $ createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy")
822-- CREATE TYPE "mood" AS ENUM ('sad', 'ok', 'happy');
838-- CREATE TYPE "mood" AS ENUM ('sad', 'ok', 'happy');
823createTypeEnum
839createTypeEnum
824 :: (KnownSymbol enum, SOP.All KnownSymbol labels)
840 :: (KnownSymbol enum, SOP.All KnownSymbol labels)
@@ -850,11 +866,20 @@ createTypeEnumFrom
850createTypeEnumFrom enum = createTypeEnum enum
866createTypeEnumFrom enum = createTypeEnum enum
851 (SOP.hpure label :: NP PGlabel (LabelsFrom hask))
867 (SOP.hpure label :: NP PGlabel (LabelsFrom hask))
853−-- | `createTypeComposite` creates a composite type. The composite type is
869+{- | `createTypeComposite` creates a composite type. The composite type is
854−-- specified by a list of attribute names and data types.
870+specified by a list of attribute names and data types.
856−-- >>> printSQL $ createTypeComposite #complex (float8 `As` #real :* float8 `As` #imaginary :* Nil)
872+>>> type PGcomplex = 'PGcomposite '["real" ::: 'PGfloat8, "imaginary" ::: 'PGfloat8]
876+ setup :: Definition '[] '["complex" ::: 'Typedef PGcomplex]
877+ setup = createTypeComposite #complex
878+ (float8 `as` #real :* float8 `as` #imaginary)
857−-- CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
881+CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
858createTypeComposite
883createTypeComposite
859 :: (KnownSymbol ty, SOP.SListI fields)
884 :: (KnownSymbol ty, SOP.SListI fields)
860 => Alias ty
885 => Alias ty