fix sqlite triggers -- NEW.table-id not NEW.foreign-table-id

db4
Doug Coleman 2009-02-20 14:52:38 -06:00
parent 624719c18f
commit 8b5a2f4a0e
2 changed files with 22 additions and 25 deletions

View File

@ -123,12 +123,8 @@ hi "HELLO" {
] with-db ] with-db
] unit-test ] unit-test
[ ] [
test.db [ ! Test SQLite triggers
hi create-table
hi drop-table
] with-db
] unit-test
TUPLE: show id ; TUPLE: show id ;
TUPLE: user username data ; TUPLE: user username data ;
@ -144,10 +140,10 @@ show "SHOW" {
} define-persistent } define-persistent
watch "WATCH" { watch "WATCH" {
{ "user" "USER" TEXT +not-null+ { "user" "USER" TEXT +not-null+ +user-assigned-id+
{ +foreign-id+ user "USERNAME" } +user-assigned-id+ } { +foreign-id+ user "USERNAME" } }
{ "show" "SHOW" BIG-INTEGER +not-null+ { "show" "SHOW" BIG-INTEGER +not-null+ +user-assigned-id+
{ +foreign-id+ show "ID" } +user-assigned-id+ } { +foreign-id+ show "ID" } }
} define-persistent } define-persistent
[ T{ user { username "littledan" } { data "foo" } } ] [ [ T{ user { username "littledan" } { data "foo" } } ] [
@ -160,7 +156,7 @@ watch "WATCH" {
show new insert-tuple show new insert-tuple
show new select-tuple show new select-tuple
"littledan" f user boa select-tuple "littledan" f user boa select-tuple
swap [ username>> ] [ id>> ] bi* [ id>> ] [ username>> ] bi*
watch boa insert-tuple watch boa insert-tuple
watch new select-tuple watch new select-tuple
user>> f user boa select-tuple user>> f user boa select-tuple

View File

@ -204,7 +204,7 @@ M: sqlite-db-connection persistent-table ( -- assoc )
CREATE TRIGGER fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id CREATE TRIGGER fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id
BEFORE INSERT ON ${table-name} BEFORE INSERT ON ${table-name}
FOR EACH ROW BEGIN FOR EACH ROW BEGIN
SELECT RAISE(ROLLBACK, 'insert on table "${table-name}" violates foreign key constraint "fk_${foreign-table-name}_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; WHERE (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
END; END;
"> interpolate "> interpolate
@ -216,8 +216,8 @@ M: sqlite-db-connection persistent-table ( -- assoc )
CREATE TRIGGER fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id CREATE TRIGGER fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id
BEFORE INSERT ON ${table-name} BEFORE INSERT ON ${table-name}
FOR EACH ROW BEGIN FOR EACH ROW BEGIN
SELECT RAISE(ROLLBACK, 'insert on table "${table-name}" violates foreign key constraint "fk_${foreign-table-name}_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.${foreign-table-id} IS NOT NULL 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; AND (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
END; END;
"> interpolate "> interpolate
@ -236,7 +236,7 @@ M: sqlite-db-connection persistent-table ( -- assoc )
CREATE TRIGGER fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id CREATE TRIGGER fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id
BEFORE UPDATE ON ${table-name} BEFORE UPDATE ON ${table-name}
FOR EACH ROW BEGIN FOR EACH ROW BEGIN
SELECT RAISE(ROLLBACK, 'update on table "${table-name}" violates foreign key constraint "fk_${foreign-table-name}_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; WHERE (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
END; END;
"> interpolate "> interpolate
@ -248,8 +248,8 @@ M: sqlite-db-connection persistent-table ( -- assoc )
CREATE TRIGGER fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id CREATE TRIGGER fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id
BEFORE UPDATE ON ${table-name} BEFORE UPDATE ON ${table-name}
FOR EACH ROW BEGIN FOR EACH ROW BEGIN
SELECT RAISE(ROLLBACK, 'update on table "${table-name}" violates foreign key constraint "fk_${foreign-table-name}_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.${foreign-table-id} IS NOT NULL 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; AND (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
END; END;
"> interpolate "> interpolate
@ -268,7 +268,7 @@ M: sqlite-db-connection persistent-table ( -- assoc )
CREATE TRIGGER fkd_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id CREATE TRIGGER fkd_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id
BEFORE DELETE ON ${foreign-table-name} BEFORE DELETE ON ${foreign-table-name}
FOR EACH ROW BEGIN FOR EACH ROW BEGIN
SELECT RAISE(ROLLBACK, 'delete on table "${foreign-table-name}" violates foreign key constraint "fk_${foreign-table-name}_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; WHERE (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = OLD.${foreign-table-id}) IS NOT NULL;
END; END;
"> interpolate "> interpolate
@ -336,15 +336,17 @@ M: sqlite-db-connection persistent-table ( -- assoc )
[ modifiers>> [ +foreign-id+ = ] deep-any? ] filter [ modifiers>> [ +foreign-id+ = ] deep-any? ] filter
[ [
[ class>> db-table-name "db-table" set ] [ class>> db-table-name "db-table" set ]
[ column-name>> "table-id" set ]
[ [
[ "sql-spec" set ]
[ column-name>> "table-id" set ]
[ ] tri
modifiers>> [ [ +foreign-id+ = ] deep-any? ] filter modifiers>> [ [ +foreign-id+ = ] deep-any? ] filter
[ [
[ second db-table-name "foreign-table-name" set ] [ second db-table-name "foreign-table-name" set ]
[ third "foreign-table-id" set ] bi [ third "foreign-table-id" set ] bi
_ execute _ execute
] each ] each
] tri ] bi
] each ] each
] call ; ] call ;
@ -378,8 +380,7 @@ M: sqlite-db-connection create-sql-statement ( class -- statement )
M: sqlite-db-connection drop-sql-statement ( class -- statements ) M: sqlite-db-connection drop-sql-statement ( class -- statements )
[ [
[ nip "drop table " 0% 0% ";" 0% ] nip "drop table " 0% 0% ";" 0%
[ drop \ drop-sqlite-triggers db-triggers ] 2bi
] query-make ; ] query-make ;
M: sqlite-db-connection compound ( string seq -- new-string ) M: sqlite-db-connection compound ( string seq -- new-string )