From 66083e263c8d29d193060ba86df1656fcd51cb27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= Date: Thu, 25 Aug 2016 13:10:42 +0200 Subject: [PATCH] db.sqlite: fix text of generated triggers An opening brace was missing for some variables --- basis/db/sqlite/sqlite-tests.factor | 51 ++++++++++++++++++++++++++--- basis/db/sqlite/sqlite.factor | 10 +++--- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/basis/db/sqlite/sqlite-tests.factor b/basis/db/sqlite/sqlite-tests.factor index cd804fc765..89a92621dd 100644 --- a/basis/db/sqlite/sqlite-tests.factor +++ b/basis/db/sqlite/sqlite-tests.factor @@ -1,9 +1,52 @@ -USING: io io.files io.files.temp io.directories io.launcher -kernel namespaces prettyprint tools.test db.sqlite db sequences -continuations db.types db.tuples unicode accessors arrays -sorting layouts math.parser ; +USING: accessors arrays db db.sqlite db.tuples db.types io.directories +io.files.temp kernel layouts literals math.parser namespaces sequences +sorting splitting tools.test ; IN: db.sqlite.tests +: normalize ( str -- str' ) + " \n" split harvest " " join ; + +! delete-trigger-restrict +${ + { + "CREATE TRIGGER fkd_TREE_NODE_NODE_ID_id " + "BEFORE DELETE ON NODE " + "FOR EACH ROW BEGIN " + "SELECT RAISE(ROLLBACK, " + "'delete on table \"NODE\" violates " + "foreign key constraint \"fkd_TREE_NODE_NODE_ID_id\"') " + "WHERE (SELECT ID FROM NODE WHERE ID = OLD.ID) IS NOT NULL; END;" + } concat +} [ + { + { "table-name" "TREE" } + { "table-id" "NODE" } + { "foreign-table-name" "NODE"} + { "foreign-table-id" "ID" } + } [ delete-trigger-restrict ] with-variables + normalize +] unit-test + +! insert-trigger +${ + { + "CREATE TRIGGER fki_TREE_NODE_NODE_ID_id " + "BEFORE INSERT ON TREE " + "FOR EACH ROW BEGIN " + "SELECT RAISE(ROLLBACK, " + "'insert on table \"TREE\" violates " + "foreign key constraint \"fki_TREE_NODE_NODE_ID_id\"') " + "WHERE (SELECT ID FROM NODE WHERE ID = NEW.NODE) IS NULL; END;" + } concat +} [ + { + { "table-name" "TREE" } + { "table-id" "NODE" } + { "foreign-table-name" "NODE"} + { "foreign-table-id" "ID" } + } [ insert-trigger ] with-variables normalize +] unit-test + : db-path ( -- path ) "test-" cell number>string ".db" 3append temp-file ; : test.db ( -- sqlite-db ) db-path ; diff --git a/basis/db/sqlite/sqlite.factor b/basis/db/sqlite/sqlite.factor index 4542d800ee..6ffe285522 100644 --- a/basis/db/sqlite/sqlite.factor +++ b/basis/db/sqlite/sqlite.factor @@ -200,7 +200,7 @@ M: sqlite-db-connection persistent-table ( -- assoc ) CREATE TRIGGER fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id BEFORE INSERT ON ${table-name} FOR EACH ROW BEGIN - SELECT RAISE(ROLLBACK, 'insert on table \"${table-name}\" violates foreign key constraint \"fki_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id\"') + SELECT RAISE(ROLLBACK, 'insert on table \"${table-name}\" violates foreign key constraint \"fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id\"') WHERE (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL; END; " interpolate>string ; @@ -210,7 +210,7 @@ M: sqlite-db-connection persistent-table ( -- assoc ) CREATE TRIGGER fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id BEFORE INSERT ON ${table-name} FOR EACH ROW BEGIN - SELECT RAISE(ROLLBACK, 'insert on table \"${table-name}\" violates foreign key constraint \"fki_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id\"') + SELECT RAISE(ROLLBACK, 'insert on table \"${table-name}\" violates foreign key constraint \"fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id\"') WHERE NEW.${table-id} IS NOT NULL AND (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL; END; @@ -221,7 +221,7 @@ M: sqlite-db-connection persistent-table ( -- assoc ) CREATE TRIGGER fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id BEFORE UPDATE ON ${table-name} FOR EACH ROW BEGIN - SELECT RAISE(ROLLBACK, 'update on table \"${table-name}\" violates foreign key constraint \"fku_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id\"') + SELECT RAISE(ROLLBACK, 'update on table \"${table-name}\" violates foreign key constraint \"fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id\"') WHERE (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL; END; " interpolate>string ; @@ -231,7 +231,7 @@ M: sqlite-db-connection persistent-table ( -- assoc ) CREATE TRIGGER fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id BEFORE UPDATE ON ${table-name} FOR EACH ROW BEGIN - SELECT RAISE(ROLLBACK, 'update on table \"${table-name}\" violates foreign key constraint \"fku_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id\"') + SELECT RAISE(ROLLBACK, 'update on table \"${table-name}\" violates foreign key constraint \"fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id\"') WHERE NEW.${table-id} IS NOT NULL AND (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL; END; @@ -242,7 +242,7 @@ M: sqlite-db-connection persistent-table ( -- assoc ) CREATE TRIGGER fkd_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id BEFORE DELETE ON ${foreign-table-name} FOR EACH ROW BEGIN - SELECT RAISE(ROLLBACK, 'delete on table \"${foreign-table-name}\" violates foreign key constraint \"fkd_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id\"') + SELECT RAISE(ROLLBACK, 'delete on table \"${foreign-table-name}\" violates foreign key constraint \"fkd_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id\"') WHERE (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = OLD.${foreign-table-id}) IS NOT NULL; END; " interpolate>string ;