1 # The author disclaims copyright to this source code. In place of
2 # a legal notice, here is a blessing:
4 # May you do good and not evil.
5 # May you find forgiveness for yourself and forgive others.
6 # May you share freely, never taking more than you give.
8 #***********************************************************************
10 # This file tests creating and dropping triggers, and interaction thereof
11 # with the database COMMIT/ROLLBACK logic.
13 # 1. CREATE and DROP TRIGGER tests
14 # trigger1-1.1: Error if table does not exist
15 # trigger1-1.2: Error if trigger already exists
16 # trigger1-1.3: Created triggers are deleted if the transaction is rolled back
17 # trigger1-1.4: DROP TRIGGER removes trigger
18 # trigger1-1.5: Dropped triggers are restored if the transaction is rolled back
19 # trigger1-1.6: Error if dropped trigger doesn't exist
20 # trigger1-1.7: Dropping the table automatically drops all triggers
21 # trigger1-1.8: A trigger created on a TEMP table is not inserted into sqlite_master
22 # trigger1-1.9: Ensure that we cannot create a trigger on sqlite_master
25 # trigger1-1.12: Ensure that INSTEAD OF triggers cannot be created on tables
26 # trigger1-1.13: Ensure that AFTER triggers cannot be created on views
27 # trigger1-1.14: Ensure that BEFORE triggers cannot be created on views
30 set testdir [file dirname $argv0]
31 source $testdir/tester.tcl
32 ifcapable !trigger||!compound {
37 do_test trigger1-1.1.1 {
39 CREATE TRIGGER trig UPDATE ON no_such_table BEGIN
40 SELECT * from sqlite_master;
43 } {1 {no such table: main.no_such_table}}
46 do_test trigger1-1.1.2 {
48 CREATE TEMP TRIGGER trig UPDATE ON no_such_table BEGIN
49 SELECT * from sqlite_master;
52 } {1 {no such table: no_such_table}}
58 do_test trigger1-1.1.3 {
60 CREATE TRIGGER trig UPDATE ON t1 FOR EACH STATEMENT BEGIN
61 SELECT * FROM sqlite_master;
64 } {1 {near "STATEMENT": syntax error}}
66 CREATE TRIGGER tr1 INSERT ON t1 BEGIN
67 INSERT INTO t1 values(1);
70 do_test trigger1-1.2.0 {
72 CREATE TRIGGER IF NOT EXISTS tr1 DELETE ON t1 BEGIN
73 SELECT * FROM sqlite_master;
77 do_test trigger1-1.2.1 {
79 CREATE TRIGGER tr1 DELETE ON t1 BEGIN
80 SELECT * FROM sqlite_master;
83 } {1 {trigger tr1 already exists}}
84 do_test trigger1-1.2.2 {
86 CREATE TRIGGER "tr1" DELETE ON t1 BEGIN
87 SELECT * FROM sqlite_master;
90 } {1 {trigger "tr1" already exists}}
91 do_test trigger1-1.2.3 {
93 CREATE TRIGGER [tr1] DELETE ON t1 BEGIN
94 SELECT * FROM sqlite_master;
97 } {1 {trigger [tr1] already exists}}
99 do_test trigger1-1.3 {
102 CREATE TRIGGER tr2 INSERT ON t1 BEGIN
103 SELECT * from sqlite_master; END;
105 CREATE TRIGGER tr2 INSERT ON t1 BEGIN
106 SELECT * from sqlite_master; END;
110 do_test trigger1-1.4 {
112 DROP TRIGGER IF EXISTS tr1;
113 CREATE TRIGGER tr1 DELETE ON t1 BEGIN
114 SELECT * FROM sqlite_master;
119 do_test trigger1-1.5 {
128 do_test trigger1-1.6.1 {
130 DROP TRIGGER IF EXISTS biggles;
134 do_test trigger1-1.6.2 {
136 DROP TRIGGER biggles;
138 } {1 {no such trigger: biggles}}
140 do_test trigger1-1.7 {
145 } {1 {no such trigger: tr1}}
149 CREATE TEMP TABLE temp_table(a);
151 do_test trigger1-1.8 {
153 CREATE TRIGGER temp_trig UPDATE ON temp_table BEGIN
154 SELECT * from sqlite_master;
156 SELECT count(*) FROM sqlite_master WHERE name = 'temp_trig';
161 do_test trigger1-1.9 {
163 CREATE TRIGGER tr1 AFTER UPDATE ON sqlite_master BEGIN
164 SELECT * FROM sqlite_master;
167 } {1 {cannot create trigger on system table}}
169 # Check to make sure that a DELETE statement within the body of
170 # a trigger does not mess up the DELETE that caused the trigger to
171 # run in the first place.
173 do_test trigger1-1.10 {
175 create table t1(a,b);
176 insert into t1 values(1,'a');
177 insert into t1 values(2,'b');
178 insert into t1 values(3,'c');
179 insert into t1 values(4,'d');
180 create trigger r1 after delete on t1 for each row begin
181 delete from t1 WHERE a=old.a+2;
183 delete from t1 where a=1 OR a=3;
189 do_test trigger1-1.11 {
191 create table t1(a,b);
192 insert into t1 values(1,'a');
193 insert into t1 values(2,'b');
194 insert into t1 values(3,'c');
195 insert into t1 values(4,'d');
196 create trigger r1 after update on t1 for each row begin
197 delete from t1 WHERE a=old.a+2;
199 update t1 set b='x-' || b where a=1 OR a=3;
205 # Ensure that we cannot create INSTEAD OF triggers on tables
206 do_test trigger1-1.12 {
208 create table t1(a,b);
209 create trigger t1t instead of update on t1 for each row begin
210 delete from t1 WHERE a=old.a+2;
213 } {1 {cannot create INSTEAD OF trigger on table: t1}}
216 # Ensure that we cannot create BEFORE triggers on views
217 do_test trigger1-1.13 {
219 create view v1 as select * from t1;
220 create trigger v1t before update on v1 for each row begin
221 delete from t1 WHERE a=old.a+2;
224 } {1 {cannot create BEFORE trigger on view: v1}}
225 # Ensure that we cannot create AFTER triggers on views
226 do_test trigger1-1.14 {
229 create view v1 as select * from t1;
230 create trigger v1t AFTER update on v1 for each row begin
231 delete from t1 WHERE a=old.a+2;
234 } {1 {cannot create AFTER trigger on view: v1}}
237 # Check for memory leaks in the trigger parser
239 do_test trigger1-2.1 {
241 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
242 SELECT * FROM; -- Syntax error
245 } {1 {near ";": syntax error}}
246 do_test trigger1-2.2 {
248 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
250 SELECT * FROM; -- Syntax error
253 } {1 {near ";": syntax error}}
255 # Create a trigger that refers to a table that might not exist.
258 do_test trigger1-3.1 {
260 CREATE TEMP TABLE t2(x,y);
263 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
264 INSERT INTO t2 VALUES(NEW.a,NEW.b);
268 do_test trigger1-3.2 {
270 INSERT INTO t1 VALUES(1,2);
273 } {1 {no such table: main.t2}}
274 do_test trigger1-3.3 {
276 set rc [catch {sqlite3 db test.db} err]
277 if {$rc} {lappend rc $err}
280 do_test trigger1-3.4 {
282 INSERT INTO t1 VALUES(1,2);
285 } {1 {no such table: main.t2}}
286 do_test trigger1-3.5 {
288 CREATE TEMP TABLE t2(x,y);
289 INSERT INTO t1 VALUES(1,2);
292 } {1 {no such table: main.t2}}
293 do_test trigger1-3.6.1 {
296 CREATE TEMP TRIGGER r1 AFTER INSERT ON t1 BEGIN
297 INSERT INTO t2 VALUES(NEW.a,NEW.b), (NEW.b*100, NEW.a*100);
299 INSERT INTO t1 VALUES(1,2);
303 do_test trigger1-3.6.2 {
308 CREATE TEMP TRIGGER r1 AFTER INSERT ON t1 BEGIN
309 INSERT INTO t2 VALUES(NEW.a,NEW.b);
311 INSERT INTO t1 VALUES(1,2);
315 do_test trigger1-3.7 {
318 CREATE TABLE t2(x,y);
323 # There are two versions of trigger1-3.8 and trigger1-3.9. One that uses
324 # compound SELECT statements, and another that does not.
326 do_test trigger1-3.8 {
328 INSERT INTO t1 VALUES(3,4);
329 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
332 do_test trigger1-3.9 {
336 INSERT INTO t1 VALUES(5,6);
337 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
340 } ;# ifcapable compound
341 ifcapable !compound {
342 do_test trigger1-3.8 {
344 INSERT INTO t1 VALUES(3,4);
349 do_test trigger1-3.9 {
353 INSERT INTO t1 VALUES(5,6);
358 } ;# ifcapable !compound
360 do_test trigger1-4.1 {
362 CREATE TEMP TRIGGER r1 BEFORE INSERT ON t1 BEGIN
363 INSERT INTO t2 VALUES(NEW.a,NEW.b);
365 INSERT INTO t1 VALUES(7,8);
369 do_test trigger1-4.2 {
372 INSERT INTO t1 VALUES(9,10);
379 do_test trigger1-4.3 {
385 do_test trigger1-4.4 {
394 CREATE TABLE t2(x,y);
396 INSERT INTO t2 VALUES(3, 4);
397 INSERT INTO t2 VALUES(7, 8);
402 integrity_check trigger1-5.1
404 # Create a trigger with the same name as a table. Make sure the
405 # trigger works. Then drop the trigger. Make sure the table is
410 set view_v1 {view v1}
412 do_test trigger1-6.1 {
413 execsql {SELECT type, name FROM sqlite_master}
414 } [concat $view_v1 {table t2}]
415 do_test trigger1-6.2 {
417 CREATE TRIGGER t2 BEFORE DELETE ON t2 BEGIN
418 SELECT RAISE(ABORT,'deletes are not permitted');
420 SELECT type, name FROM sqlite_master;
422 } [concat $view_v1 {table t2 trigger t2}]
423 do_test trigger1-6.3 {
424 catchsql {DELETE FROM t2}
425 } {1 {deletes are not permitted}}
426 verify_ex_errcode trigger1-6.3b SQLITE_CONSTRAINT_TRIGGER
427 do_test trigger1-6.4 {
428 execsql {SELECT * FROM t2}
430 do_test trigger1-6.5 {
433 execsql {SELECT type, name FROM sqlite_master}
434 } [concat $view_v1 {table t2 trigger t2}]
435 do_test trigger1-6.6 {
438 SELECT type, name FROM sqlite_master;
440 } [concat $view_v1 {table t2}]
441 do_test trigger1-6.7 {
442 execsql {SELECT * FROM t2}
444 do_test trigger1-6.8 {
447 execsql {SELECT * FROM t2}
450 integrity_check trigger1-7.1
452 # Check to make sure the name of a trigger can be quoted so that keywords
453 # can be used as trigger names. Ticket #468
455 do_test trigger1-8.1 {
457 CREATE TRIGGER 'trigger' AFTER INSERT ON t2 BEGIN SELECT 1; END;
458 SELECT name FROM sqlite_master WHERE type='trigger';
461 do_test trigger1-8.2 {
463 DROP TRIGGER 'trigger';
464 SELECT name FROM sqlite_master WHERE type='trigger';
467 do_test trigger1-8.3 {
469 CREATE TRIGGER "trigger" AFTER INSERT ON t2 BEGIN SELECT 1; END;
470 SELECT name FROM sqlite_master WHERE type='trigger';
473 do_test trigger1-8.4 {
475 DROP TRIGGER "trigger";
476 SELECT name FROM sqlite_master WHERE type='trigger';
479 do_test trigger1-8.5 {
481 CREATE TRIGGER [trigger] AFTER INSERT ON t2 BEGIN SELECT 1; END;
482 SELECT name FROM sqlite_master WHERE type='trigger';
485 do_test trigger1-8.6 {
487 DROP TRIGGER [trigger];
488 SELECT name FROM sqlite_master WHERE type='trigger';
493 # Make sure REPLACE works inside of triggers.
495 # There are two versions of trigger1-9.1 and trigger1-9.2. One that uses
496 # compound SELECT statements, and another that does not.
498 do_test trigger1-9.1 {
500 CREATE TABLE t3(a,b);
501 CREATE TABLE t4(x UNIQUE, b);
502 CREATE TRIGGER r34 AFTER INSERT ON t3 BEGIN
503 REPLACE INTO t4 VALUES(new.a,new.b);
505 INSERT INTO t3 VALUES(1,2);
506 SELECT * FROM t3 UNION ALL SELECT 99, 99 UNION ALL SELECT * FROM t4;
509 do_test trigger1-9.2 {
511 INSERT INTO t3 VALUES(1,3);
512 SELECT * FROM t3 UNION ALL SELECT 99, 99 UNION ALL SELECT * FROM t4;
514 } {1 2 1 3 99 99 1 3}
516 do_test trigger1-9.1 {
518 CREATE TABLE t3(a,b);
519 CREATE TABLE t4(x UNIQUE, b);
520 CREATE TRIGGER r34 AFTER INSERT ON t3 BEGIN
521 REPLACE INTO t4 VALUES(new.a,new.b);
523 INSERT INTO t3 VALUES(1,2);
524 SELECT * FROM t3; SELECT 99, 99; SELECT * FROM t4;
527 do_test trigger1-9.2 {
529 INSERT INTO t3 VALUES(1,3);
530 SELECT * FROM t3; SELECT 99, 99; SELECT * FROM t4;
532 } {1 2 1 3 99 99 1 3}
541 # Ticket #764. At one stage TEMP triggers would fail to re-install when the
542 # schema was reloaded. The following tests ensure that TEMP triggers are
543 # correctly re-installed.
545 # Also verify that references within trigger programs are resolved at
546 # statement compile time, not trigger installation time. This means, for
547 # example, that you can drop and re-create tables referenced by triggers.
548 ifcapable tempdb&&attach {
549 do_test trigger1-10.0 {
551 forcedelete test2.db-journal
553 ATTACH 'test2.db' AS aux;
556 do_test trigger1-10.1 {
558 CREATE TABLE main.t4(a, b, c);
559 CREATE TABLE temp.t4(a, b, c);
560 CREATE TABLE aux.t4(a, b, c);
561 CREATE TABLE insert_log(db, a, b, c);
564 do_test trigger1-10.2 {
566 CREATE TEMP TRIGGER trig1 AFTER INSERT ON main.t4 BEGIN
567 INSERT INTO insert_log VALUES('main', new.a, new.b, new.c);
569 CREATE TEMP TRIGGER trig2 AFTER INSERT ON temp.t4 BEGIN
570 INSERT INTO insert_log VALUES('temp', new.a, new.b, new.c);
572 CREATE TEMP TRIGGER trig3 AFTER INSERT ON aux.t4 BEGIN
573 INSERT INTO insert_log VALUES('aux', new.a, new.b, new.c);
577 do_test trigger1-10.3 {
579 INSERT INTO main.t4 VALUES(1, 2, 3);
580 INSERT INTO temp.t4 VALUES(4, 5, 6);
581 INSERT INTO aux.t4 VALUES(7, 8, 9);
584 do_test trigger1-10.4 {
586 SELECT * FROM insert_log;
588 } {main 1 2 3 temp 4 5 6 aux 7 8 9}
589 do_test trigger1-10.5 {
592 INSERT INTO main.t4 VALUES(1, 2, 3);
593 INSERT INTO temp.t4 VALUES(4, 5, 6);
594 INSERT INTO aux.t4 VALUES(7, 8, 9);
598 do_test trigger1-10.6 {
600 SELECT * FROM insert_log;
602 } {main 1 2 3 temp 4 5 6 aux 7 8 9}
603 do_test trigger1-10.7 {
605 DELETE FROM insert_log;
606 INSERT INTO main.t4 VALUES(11, 12, 13);
607 INSERT INTO temp.t4 VALUES(14, 15, 16);
608 INSERT INTO aux.t4 VALUES(17, 18, 19);
611 do_test trigger1-10.8 {
613 SELECT * FROM insert_log;
615 } {main 11 12 13 temp 14 15 16 aux 17 18 19}
616 do_test trigger1-10.9 {
617 # Drop and re-create the insert_log table in a different database. Note
618 # that we can change the column names because the trigger programs don't
619 # use them explicitly.
621 DROP TABLE insert_log;
622 CREATE TABLE aux.insert_log(db, d, e, f);
625 do_test trigger1-10.10 {
627 INSERT INTO main.t4 VALUES(21, 22, 23);
628 INSERT INTO temp.t4 VALUES(24, 25, 26);
629 INSERT INTO aux.t4 VALUES(27, 28, 29);
632 do_test trigger1-10.11 {
634 SELECT * FROM insert_log;
636 } {main 21 22 23 temp 24 25 26 aux 27 28 29}
639 do_test trigger1-11.1 {
640 catchsql {SELECT raise(abort,'message');}
641 } {1 {RAISE() may only be used within a trigger-program}}
643 do_test trigger1-15.1 {
645 CREATE TABLE tA(a INTEGER PRIMARY KEY, b, c);
646 CREATE TRIGGER tA_trigger BEFORE UPDATE ON "tA" BEGIN SELECT 1; END;
647 INSERT INTO tA VALUES(1, 2, 3);
649 catchsql { UPDATE tA SET a = 'abc' }
650 } {1 {datatype mismatch}}
651 do_test trigger1-15.2 {
652 catchsql { INSERT INTO tA VALUES('abc', 2, 3) }
653 } {1 {datatype mismatch}}
655 # Ticket #3947: Do not allow qualified table names on INSERT, UPDATE, and
656 # DELETE statements within triggers. Actually, this has never been allowed
657 # by the grammar. But the error message is confusing: one simply gets a
658 # "syntax error". That has now been changed to give a full error message.
660 do_test trigger1-16.1 {
662 CREATE TABLE t16(a,b,c);
663 CREATE INDEX t16a ON t16(a);
664 CREATE INDEX t16b ON t16(b);
667 CREATE TRIGGER main.t16err1 AFTER INSERT ON tA BEGIN
668 INSERT INTO main.t16 VALUES(1,2,3);
671 } {1 {qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers}}
672 do_test trigger1-16.2 {
674 CREATE TRIGGER main.t16err2 AFTER INSERT ON tA BEGIN
675 UPDATE main.t16 SET rowid=rowid+1;
678 } {1 {qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers}}
679 do_test trigger1-16.3 {
681 CREATE TRIGGER main.t16err3 AFTER INSERT ON tA BEGIN
682 DELETE FROM main.t16;
685 } {1 {qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers}}
686 do_test trigger1-16.4 {
688 CREATE TRIGGER main.t16err4 AFTER INSERT ON tA BEGIN
689 UPDATE t16 NOT INDEXED SET rowid=rowid+1;
692 } {1 {the NOT INDEXED clause is not allowed on UPDATE or DELETE statements within triggers}}
693 do_test trigger1-16.5 {
695 CREATE TRIGGER main.t16err5 AFTER INSERT ON tA BEGIN
696 UPDATE t16 INDEXED BY t16a SET rowid=rowid+1 WHERE a=1;
699 } {1 {the INDEXED BY clause is not allowed on UPDATE or DELETE statements within triggers}}
700 do_test trigger1-16.6 {
702 CREATE TRIGGER main.t16err6 AFTER INSERT ON tA BEGIN
703 DELETE FROM t16 NOT INDEXED WHERE a=123;
706 } {1 {the NOT INDEXED clause is not allowed on UPDATE or DELETE statements within triggers}}
707 do_test trigger1-16.7 {
709 CREATE TRIGGER main.t16err7 AFTER INSERT ON tA BEGIN
710 DELETE FROM t16 INDEXED BY t16a WHERE a=123;
713 } {1 {the INDEXED BY clause is not allowed on UPDATE or DELETE statements within triggers}}