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 # trig-1.1: Error if table does not exist
15 # trig-1.2: Error if trigger already exists
16 # trig-1.3: Created triggers are deleted if the transaction is rolled back
17 # trig-1.4: DROP TRIGGER removes trigger
18 # trig-1.5: Dropped triggers are restored if the transaction is rolled back
19 # trig-1.6: Error if dropped trigger doesn't exist
20 # trig-1.7: Dropping the table automatically drops all triggers
21 # trig-1.8: A trigger created on a TEMP table is not inserted into sqlite_master
22 # trig-1.9: Ensure that we cannot create a trigger on sqlite_master
25 # trig-1.12: Ensure that INSTEAD OF triggers cannot be created on tables
26 # trig-1.13: Ensure that AFTER triggers cannot be created on views
27 # trig-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} {
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: main.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: main.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: main.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 trigger-3.2 {
270 INSERT INTO t1 VALUES(1,2);
273 } {1 {no such table: main.t2}}
274 do_test trigger-3.3 {
276 set rc [catch {sqlite3 db test.db} err]
277 if {$rc} {lappend rc $err}
280 do_test trigger-3.4 {
282 INSERT INTO t1 VALUES(1,2);
285 } {1 {no such table: main.t2}}
286 do_test trigger-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 trigger-3.6 {
296 CREATE TEMP TRIGGER r1 AFTER INSERT ON t1 BEGIN
297 INSERT INTO t2 VALUES(NEW.a,NEW.b);
299 INSERT INTO t1 VALUES(1,2);
303 do_test trigger-3.7 {
306 CREATE TABLE t2(x,y);
311 # There are two versions of trigger-3.8 and trigger-3.9. One that uses
312 # compound SELECT statements, and another that does not.
314 do_test trigger1-3.8 {
316 INSERT INTO t1 VALUES(3,4);
317 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
320 do_test trigger1-3.9 {
324 INSERT INTO t1 VALUES(5,6);
325 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
328 } ;# ifcapable compound
329 ifcapable !compound {
330 do_test trigger1-3.8 {
332 INSERT INTO t1 VALUES(3,4);
337 do_test trigger1-3.9 {
341 INSERT INTO t1 VALUES(5,6);
346 } ;# ifcapable !compound
348 do_test trigger1-4.1 {
350 CREATE TEMP TRIGGER r1 BEFORE INSERT ON t1 BEGIN
351 INSERT INTO t2 VALUES(NEW.a,NEW.b);
353 INSERT INTO t1 VALUES(7,8);
357 do_test trigger1-4.2 {
360 INSERT INTO t1 VALUES(9,10);
367 do_test trigger1-4.3 {
373 do_test trigger1-4.4 {
382 CREATE TABLE t2(x,y);
384 INSERT INTO t2 VALUES(3, 4);
385 INSERT INTO t2 VALUES(7, 8);
390 integrity_check trigger1-5.1
392 # Create a trigger with the same name as a table. Make sure the
393 # trigger works. Then drop the trigger. Make sure the table is
398 set view_v1 {view v1}
400 do_test trigger1-6.1 {
401 execsql {SELECT type, name FROM sqlite_master}
402 } [concat $view_v1 {table t2}]
403 do_test trigger1-6.2 {
405 CREATE TRIGGER t2 BEFORE DELETE ON t2 BEGIN
406 SELECT RAISE(ABORT,'deletes are not permitted');
408 SELECT type, name FROM sqlite_master;
410 } [concat $view_v1 {table t2 trigger t2}]
411 do_test trigger1-6.3 {
412 catchsql {DELETE FROM t2}
413 } {1 {deletes are not permitted}}
414 do_test trigger1-6.4 {
415 execsql {SELECT * FROM t2}
417 do_test trigger1-6.5 {
420 execsql {SELECT type, name FROM sqlite_master}
421 } [concat $view_v1 {table t2 trigger t2}]
422 do_test trigger1-6.6 {
425 SELECT type, name FROM sqlite_master;
427 } [concat $view_v1 {table t2}]
428 do_test trigger1-6.7 {
429 execsql {SELECT * FROM t2}
431 do_test trigger1-6.8 {
434 execsql {SELECT * FROM t2}
437 integrity_check trigger-7.1
439 # Check to make sure the name of a trigger can be quoted so that keywords
440 # can be used as trigger names. Ticket #468
442 do_test trigger1-8.1 {
444 CREATE TRIGGER 'trigger' AFTER INSERT ON t2 BEGIN SELECT 1; END;
445 SELECT name FROM sqlite_master WHERE type='trigger';
448 do_test trigger1-8.2 {
450 DROP TRIGGER 'trigger';
451 SELECT name FROM sqlite_master WHERE type='trigger';
454 do_test trigger1-8.3 {
456 CREATE TRIGGER "trigger" AFTER INSERT ON t2 BEGIN SELECT 1; END;
457 SELECT name FROM sqlite_master WHERE type='trigger';
460 do_test trigger1-8.4 {
462 DROP TRIGGER "trigger";
463 SELECT name FROM sqlite_master WHERE type='trigger';
466 do_test trigger1-8.5 {
468 CREATE TRIGGER [trigger] AFTER INSERT ON t2 BEGIN SELECT 1; END;
469 SELECT name FROM sqlite_master WHERE type='trigger';
472 do_test trigger1-8.6 {
474 DROP TRIGGER [trigger];
475 SELECT name FROM sqlite_master WHERE type='trigger';
480 # Make sure REPLACE works inside of triggers.
482 # There are two versions of trigger-9.1 and trigger-9.2. One that uses
483 # compound SELECT statements, and another that does not.
485 do_test trigger1-9.1 {
487 CREATE TABLE t3(a,b);
488 CREATE TABLE t4(x UNIQUE, b);
489 CREATE TRIGGER r34 AFTER INSERT ON t3 BEGIN
490 REPLACE INTO t4 VALUES(new.a,new.b);
492 INSERT INTO t3 VALUES(1,2);
493 SELECT * FROM t3 UNION ALL SELECT 99, 99 UNION ALL SELECT * FROM t4;
496 do_test trigger1-9.2 {
498 INSERT INTO t3 VALUES(1,3);
499 SELECT * FROM t3 UNION ALL SELECT 99, 99 UNION ALL SELECT * FROM t4;
501 } {1 2 1 3 99 99 1 3}
503 do_test trigger1-9.1 {
505 CREATE TABLE t3(a,b);
506 CREATE TABLE t4(x UNIQUE, b);
507 CREATE TRIGGER r34 AFTER INSERT ON t3 BEGIN
508 REPLACE INTO t4 VALUES(new.a,new.b);
510 INSERT INTO t3 VALUES(1,2);
511 SELECT * FROM t3; SELECT 99, 99; SELECT * FROM t4;
514 do_test trigger1-9.2 {
516 INSERT INTO t3 VALUES(1,3);
517 SELECT * FROM t3; SELECT 99, 99; SELECT * FROM t4;
519 } {1 2 1 3 99 99 1 3}
528 # Ticket #764. At one stage TEMP triggers would fail to re-install when the
529 # schema was reloaded. The following tests ensure that TEMP triggers are
530 # correctly re-installed.
532 # Also verify that references within trigger programs are resolved at
533 # statement compile time, not trigger installation time. This means, for
534 # example, that you can drop and re-create tables referenced by triggers.
535 ifcapable tempdb&&attach {
536 do_test trigger1-10.0 {
537 file delete -force test2.db
538 file delete -force test2.db-journal
540 ATTACH 'test2.db' AS aux;
543 do_test trigger1-10.1 {
545 CREATE TABLE main.t4(a, b, c);
546 CREATE TABLE temp.t4(a, b, c);
547 CREATE TABLE aux.t4(a, b, c);
548 CREATE TABLE insert_log(db, a, b, c);
551 do_test trigger1-10.2 {
553 CREATE TEMP TRIGGER trig1 AFTER INSERT ON main.t4 BEGIN
554 INSERT INTO insert_log VALUES('main', new.a, new.b, new.c);
556 CREATE TEMP TRIGGER trig2 AFTER INSERT ON temp.t4 BEGIN
557 INSERT INTO insert_log VALUES('temp', new.a, new.b, new.c);
559 CREATE TEMP TRIGGER trig3 AFTER INSERT ON aux.t4 BEGIN
560 INSERT INTO insert_log VALUES('aux', new.a, new.b, new.c);
564 do_test trigger1-10.3 {
566 INSERT INTO main.t4 VALUES(1, 2, 3);
567 INSERT INTO temp.t4 VALUES(4, 5, 6);
568 INSERT INTO aux.t4 VALUES(7, 8, 9);
571 do_test trigger1-10.4 {
573 SELECT * FROM insert_log;
575 } {main 1 2 3 temp 4 5 6 aux 7 8 9}
576 do_test trigger1-10.5 {
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);
585 do_test trigger1-10.6 {
587 SELECT * FROM insert_log;
589 } {main 1 2 3 temp 4 5 6 aux 7 8 9}
590 do_test trigger1-10.7 {
592 DELETE FROM insert_log;
593 INSERT INTO main.t4 VALUES(11, 12, 13);
594 INSERT INTO temp.t4 VALUES(14, 15, 16);
595 INSERT INTO aux.t4 VALUES(17, 18, 19);
598 do_test trigger1-10.8 {
600 SELECT * FROM insert_log;
602 } {main 11 12 13 temp 14 15 16 aux 17 18 19}
603 do_test trigger1-10.8 {
604 # Drop and re-create the insert_log table in a different database. Note
605 # that we can change the column names because the trigger programs don't
606 # use them explicitly.
608 DROP TABLE insert_log;
609 CREATE TABLE aux.insert_log(db, d, e, f);
612 do_test trigger1-10.10 {
614 INSERT INTO main.t4 VALUES(21, 22, 23);
615 INSERT INTO temp.t4 VALUES(24, 25, 26);
616 INSERT INTO aux.t4 VALUES(27, 28, 29);
619 do_test trigger1-10.11 {
621 SELECT * FROM insert_log;
623 } {main 21 22 23 temp 24 25 26 aux 27 28 29}
626 do_test trigger1-11.1 {
627 catchsql {SELECT raise(abort,'message');}
628 } {1 {RAISE() may only be used within a trigger-program}}
630 do_test trigger1-15.1 {
632 CREATE TABLE tA(a INTEGER PRIMARY KEY, b, c);
633 CREATE TRIGGER tA_trigger BEFORE UPDATE ON "tA" BEGIN SELECT 1; END;
634 INSERT INTO tA VALUES(1, 2, 3);
636 catchsql { UPDATE tA SET a = 'abc' }
637 } {1 {datatype mismatch}}
638 do_test trigger1-15.2 {
639 catchsql { INSERT INTO tA VALUES('abc', 2, 3) }
640 } {1 {datatype mismatch}}
642 # Ticket #3947: Do not allow qualified table names on INSERT, UPDATE, and
643 # DELETE statements within triggers. Actually, this has never been allowed
644 # by the grammar. But the error message is confusing: one simply gets a
645 # "syntax error". That has now been changed to give a full error message.
647 do_test trigger1-16.1 {
649 CREATE TABLE t16(a,b,c);
650 CREATE INDEX t16a ON t16(a);
651 CREATE INDEX t16b ON t16(b);
654 CREATE TRIGGER main.t16err1 AFTER INSERT ON tA BEGIN
655 INSERT INTO main.t16 VALUES(1,2,3);
658 } {1 {qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers}}
659 do_test trigger1-16.2 {
661 CREATE TRIGGER main.t16err2 AFTER INSERT ON tA BEGIN
662 UPDATE main.t16 SET rowid=rowid+1;
665 } {1 {qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers}}
666 do_test trigger1-16.3 {
668 CREATE TRIGGER main.t16err3 AFTER INSERT ON tA BEGIN
669 DELETE FROM main.t16;
672 } {1 {qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers}}
673 do_test trigger1-16.4 {
675 CREATE TRIGGER main.t16err4 AFTER INSERT ON tA BEGIN
676 UPDATE t16 NOT INDEXED SET rowid=rowid+1;
679 } {1 {the NOT INDEXED clause is not allowed on UPDATE or DELETE statements within triggers}}
680 do_test trigger1-16.5 {
682 CREATE TRIGGER main.t16err5 AFTER INSERT ON tA BEGIN
683 UPDATE t16 INDEXED BY t16a SET rowid=rowid+1 WHERE a=1;
686 } {1 {the INDEXED BY clause is not allowed on UPDATE or DELETE statements within triggers}}
687 do_test trigger1-16.6 {
689 CREATE TRIGGER main.t16err6 AFTER INSERT ON tA BEGIN
690 DELETE FROM t16 NOT INDEXED WHERE a=123;
693 } {1 {the NOT INDEXED clause is not allowed on UPDATE or DELETE statements within triggers}}
694 do_test trigger1-16.7 {
696 CREATE TRIGGER main.t16err7 AFTER INSERT ON tA BEGIN
697 DELETE FROM t16 INDEXED BY t16a WHERE a=123;
700 } {1 {the INDEXED BY clause is not allowed on UPDATE or DELETE statements within triggers}}