First public contribution.
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 # Regression testing of FOR EACH ROW table triggers
12 # 1. Trigger execution order tests.
13 # These tests ensure that BEFORE and AFTER triggers are fired at the correct
14 # times relative to each other and the triggering statement.
16 # trigger2-1.1.*: ON UPDATE trigger execution model.
17 # trigger2-1.2.*: DELETE trigger execution model.
18 # trigger2-1.3.*: INSERT trigger execution model.
20 # 2. Trigger program execution tests.
21 # These tests ensure that trigger programs execute correctly (ie. that a
22 # trigger program can correctly execute INSERT, UPDATE, DELETE * SELECT
23 # statements, and combinations thereof).
25 # 3. Selective trigger execution
26 # This tests that conditional triggers (ie. UPDATE OF triggers and triggers
27 # with WHEN clauses) are fired only fired when they are supposed to be.
29 # trigger2-3.1: UPDATE OF triggers
30 # trigger2-3.2: WHEN clause
32 # 4. Cascaded trigger execution
33 # Tests that trigger-programs may cause other triggers to fire. Also that a
34 # trigger-program is never executed recursively.
36 # trigger2-4.1: Trivial cascading trigger
37 # trigger2-4.2: Trivial recursive trigger handling
39 # 5. Count changes behaviour.
40 # Verify that rows altered by triggers are not included in the return value
41 # of the "count changes" interface.
43 # 6. ON CONFLICT clause handling
44 # trigger2-6.1[a-f]: INSERT statements
45 # trigger2-6.2[a-f]: UPDATE statements
47 # 7. & 8. Triggers on views fire correctly.
50 set testdir [file dirname $argv0]
51 source $testdir/tester.tcl
52 ifcapable {!trigger} {
60 set tbl_definitions [list \
61 {CREATE TABLE tbl (a, b);} \
62 {CREATE TABLE tbl (a INTEGER PRIMARY KEY, b);} \
63 {CREATE TABLE tbl (a, b PRIMARY KEY);} \
64 {CREATE TABLE tbl (a, b); CREATE INDEX tbl_idx ON tbl(b);} \
67 lappend tbl_definitions \
68 {CREATE TEMP TABLE tbl (a, b); CREATE INDEX tbl_idx ON tbl(b);}
69 lappend tbl_definitions {CREATE TEMP TABLE tbl (a, b);}
70 lappend tbl_definitions \
71 {CREATE TEMPORARY TABLE tbl (a INTEGER PRIMARY KEY, b);}
73 foreach tbl_defn $tbl_definitions {
75 catchsql { DROP INDEX tbl_idx; }
86 INSERT INTO tbl VALUES(1, 2);
87 INSERT INTO tbl VALUES(3, 4);
89 CREATE TABLE rlog (idx, old_a, old_b, db_sum_a, db_sum_b, new_a, new_b);
90 CREATE TABLE clog (idx, old_a, old_b, db_sum_a, db_sum_b, new_a, new_b);
92 CREATE TRIGGER before_update_row BEFORE UPDATE ON tbl FOR EACH ROW
94 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
96 (SELECT coalesce(sum(a),0) FROM tbl),
97 (SELECT coalesce(sum(b),0) FROM tbl),
101 CREATE TRIGGER after_update_row AFTER UPDATE ON tbl FOR EACH ROW
103 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
105 (SELECT coalesce(sum(a),0) FROM tbl),
106 (SELECT coalesce(sum(b),0) FROM tbl),
110 CREATE TRIGGER conditional_update_row AFTER UPDATE ON tbl FOR EACH ROW
113 INSERT INTO clog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM clog),
115 (SELECT coalesce(sum(a),0) FROM tbl),
116 (SELECT coalesce(sum(b),0) FROM tbl),
121 do_test trigger2-1.$ii.1 {
124 UPDATE tbl SET a = a * 10, b = b * 10;
125 SELECT * FROM rlog ORDER BY idx;
126 SELECT * FROM clog ORDER BY idx;
128 lappend r [expr {int($v)}]
131 } [list 1 1 2 4 6 10 20 \
140 INSERT INTO tbl VALUES (100, 100);
141 INSERT INTO tbl VALUES (300, 200);
142 CREATE TRIGGER delete_before_row BEFORE DELETE ON tbl FOR EACH ROW
144 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
146 (SELECT coalesce(sum(a),0) FROM tbl),
147 (SELECT coalesce(sum(b),0) FROM tbl),
151 CREATE TRIGGER delete_after_row AFTER DELETE ON tbl FOR EACH ROW
153 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
155 (SELECT coalesce(sum(a),0) FROM tbl),
156 (SELECT coalesce(sum(b),0) FROM tbl),
160 do_test trigger2-1.$ii.2 {
166 lappend r [expr {int($v)}]
169 } [list 1 100 100 400 300 0 0 \
170 2 100 100 300 200 0 0 \
171 3 300 200 300 200 0 0 \
176 CREATE TRIGGER insert_before_row BEFORE INSERT ON tbl FOR EACH ROW
178 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
180 (SELECT coalesce(sum(a),0) FROM tbl),
181 (SELECT coalesce(sum(b),0) FROM tbl),
185 CREATE TRIGGER insert_after_row AFTER INSERT ON tbl FOR EACH ROW
187 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
189 (SELECT coalesce(sum(a),0) FROM tbl),
190 (SELECT coalesce(sum(b),0) FROM tbl),
194 do_test trigger2-1.$ii.3 {
197 CREATE TABLE other_tbl(a, b);
198 INSERT INTO other_tbl VALUES(1, 2);
199 INSERT INTO other_tbl VALUES(3, 4);
200 -- INSERT INTO tbl SELECT * FROM other_tbl;
201 INSERT INTO tbl VALUES(5, 6);
202 DROP TABLE other_tbl;
206 } [list 1 0 0 0 0 5 6 \
209 integrity_check trigger2-1.$ii.4
215 DROP TABLE other_tbl;
222 {UPDATE tbl SET b = old.b;}
223 {INSERT INTO log VALUES(new.c, 2, 3);}
224 {DELETE FROM log WHERE a = 1;}
225 {INSERT INTO tbl VALUES(500, new.b * 10, 700);
226 UPDATE tbl SET c = old.c;
228 {INSERT INTO log select * from tbl;}
230 foreach test_varset [ list \
232 set statement {UPDATE tbl SET c = 10 WHERE a = 1;}
233 set prep {INSERT INTO tbl VALUES(1, 2, 3);}
242 set statement {DELETE FROM tbl WHERE a = 1;}
243 set prep {INSERT INTO tbl VALUES(1, 2, 3);}
249 set statement {INSERT INTO tbl VALUES(1, 2, 3);}
269 set statement_type [string range $statement 0 5]
270 set tr_program_fixed $tr_program
271 if {$statement_type == "DELETE"} {
272 regsub -all new\.a $tr_program_fixed {''} tr_program_fixed
273 regsub -all new\.b $tr_program_fixed {''} tr_program_fixed
274 regsub -all new\.c $tr_program_fixed {''} tr_program_fixed
276 if {$statement_type == "INSERT"} {
277 regsub -all old\.a $tr_program_fixed {''} tr_program_fixed
278 regsub -all old\.b $tr_program_fixed {''} tr_program_fixed
279 regsub -all old\.c $tr_program_fixed {''} tr_program_fixed
283 set tr_program_cooked $tr_program
284 regsub -all new\.a $tr_program_cooked $newA tr_program_cooked
285 regsub -all new\.b $tr_program_cooked $newB tr_program_cooked
286 regsub -all new\.c $tr_program_cooked $newC tr_program_cooked
287 regsub -all old\.a $tr_program_cooked $oldA tr_program_cooked
288 regsub -all old\.b $tr_program_cooked $oldB tr_program_cooked
289 regsub -all old\.c $tr_program_cooked $oldC tr_program_cooked
297 CREATE TABLE tbl(a PRIMARY KEY, b, c);
298 CREATE TABLE log(a, b, c);
301 set query {SELECT * FROM tbl; SELECT * FROM log;}
302 set prep "$prep; INSERT INTO log VALUES(1, 2, 3);\
303 INSERT INTO log VALUES(10, 20, 30);"
305 # Check execution of BEFORE programs:
307 set before_data [ execsql "$prep $tr_program_cooked $statement $query" ]
309 execsql "DELETE FROM tbl; DELETE FROM log; $prep";
310 execsql "CREATE TRIGGER the_trigger BEFORE [string range $statement 0 6]\
311 ON tbl BEGIN $tr_program_fixed END;"
313 do_test trigger2-2.$ii-before "execsql {$statement $query}" $before_data
315 execsql "DROP TRIGGER the_trigger;"
316 execsql "DELETE FROM tbl; DELETE FROM log;"
318 # Check execution of AFTER programs
319 set after_data [ execsql "$prep $statement $tr_program_cooked $query" ]
321 execsql "DELETE FROM tbl; DELETE FROM log; $prep";
322 execsql "CREATE TRIGGER the_trigger AFTER [string range $statement 0 6]\
323 ON tbl BEGIN $tr_program_fixed END;"
325 do_test trigger2-2.$ii-after "execsql {$statement $query}" $after_data
326 execsql "DROP TRIGGER the_trigger;"
328 integrity_check trigger2-2.$ii-integrity
338 # trigger2-3.1: UPDATE OF triggers
340 CREATE TABLE tbl (a, b, c, d);
341 CREATE TABLE log (a);
342 INSERT INTO log VALUES (0);
343 INSERT INTO tbl VALUES (0, 0, 0, 0);
344 INSERT INTO tbl VALUES (1, 0, 0, 0);
345 CREATE TRIGGER tbl_after_update_cd BEFORE UPDATE OF c, d ON tbl
347 UPDATE log SET a = a + 1;
350 do_test trigger2-3.1 {
352 UPDATE tbl SET b = 1, c = 10; -- 2
353 UPDATE tbl SET b = 10; -- 0
354 UPDATE tbl SET d = 4 WHERE a = 0; --1
355 UPDATE tbl SET a = 4, b = 10; --0
364 # trigger2-3.2: WHEN clause
365 set when_triggers [list {t1 BEFORE INSERT ON tbl WHEN new.a > 20}]
367 lappend when_triggers \
368 {t2 BEFORE INSERT ON tbl WHEN (SELECT count(*) FROM tbl) = 0}
372 CREATE TABLE tbl (a, b, c, d);
373 CREATE TABLE log (a);
374 INSERT INTO log VALUES (0);
377 foreach trig $when_triggers {
378 execsql "CREATE TRIGGER $trig BEGIN UPDATE log set a = a + 1; END;"
386 do_test trigger2-3.2 {
389 INSERT INTO tbl VALUES(0, 0, 0, 0); -- 1 (ifcapable subquery)
391 UPDATE log SET a = 0;
393 INSERT INTO tbl VALUES(0, 0, 0, 0); -- 0
395 UPDATE log SET a = 0;
397 INSERT INTO tbl VALUES(200, 0, 0, 0); -- 1
399 UPDATE log SET a = 0;
406 integrity_check trigger2-3.3
408 # Simple cascaded trigger
410 CREATE TABLE tblA(a, b);
411 CREATE TABLE tblB(a, b);
412 CREATE TABLE tblC(a, b);
414 CREATE TRIGGER tr1 BEFORE INSERT ON tblA BEGIN
415 INSERT INTO tblB values(new.a, new.b);
418 CREATE TRIGGER tr2 BEFORE INSERT ON tblB BEGIN
419 INSERT INTO tblC values(new.a, new.b);
422 do_test trigger2-4.1 {
424 INSERT INTO tblA values(1, 2);
436 # Simple recursive trigger
438 CREATE TABLE tbl(a, b, c);
439 CREATE TRIGGER tbl_trig BEFORE INSERT ON tbl
441 INSERT INTO tbl VALUES (new.a, new.b, new.c);
444 do_test trigger2-4.2 {
446 INSERT INTO tbl VALUES (1, 2, 3);
456 CREATE TABLE tbl(a, b, c);
457 CREATE TRIGGER tbl_trig BEFORE INSERT ON tbl
459 INSERT INTO tbl VALUES (1, 2, 3);
460 INSERT INTO tbl VALUES (2, 2, 3);
461 UPDATE tbl set b = 10 WHERE a = 1;
462 DELETE FROM tbl WHERE a = 1;
468 INSERT INTO tbl VALUES(100, 200, 300);
477 # Handling of ON CONFLICT by INSERT statements inside triggers
479 CREATE TABLE tbl (a primary key, b, c);
480 CREATE TRIGGER ai_tbl AFTER INSERT ON tbl BEGIN
481 INSERT OR IGNORE INTO tbl values (new.a, 0, 0);
484 do_test trigger2-6.1a {
487 INSERT INTO tbl values (1, 2, 3);
491 do_test trigger2-6.1b {
493 INSERT OR ABORT INTO tbl values (2, 2, 3);
495 } {1 {column a is not unique}}
496 do_test trigger2-6.1c {
501 do_test trigger2-6.1d {
503 INSERT OR FAIL INTO tbl values (2, 2, 3);
505 } {1 {column a is not unique}}
506 do_test trigger2-6.1e {
511 do_test trigger2-6.1f {
513 INSERT OR REPLACE INTO tbl values (2, 2, 3);
517 do_test trigger2-6.1g {
519 INSERT OR ROLLBACK INTO tbl values (3, 2, 3);
521 } {1 {column a is not unique}}
522 do_test trigger2-6.1h {
527 execsql {DELETE FROM tbl}
530 # Handling of ON CONFLICT by UPDATE statements inside triggers
532 INSERT INTO tbl values (4, 2, 3);
533 INSERT INTO tbl values (6, 3, 4);
534 CREATE TRIGGER au_tbl AFTER UPDATE ON tbl BEGIN
535 UPDATE OR IGNORE tbl SET a = new.a, c = 10;
538 do_test trigger2-6.2a {
541 UPDATE tbl SET a = 1 WHERE a = 4;
545 do_test trigger2-6.2b {
547 UPDATE OR ABORT tbl SET a = 4 WHERE a = 1;
549 } {1 {column a is not unique}}
550 do_test trigger2-6.2c {
555 do_test trigger2-6.2d {
557 UPDATE OR FAIL tbl SET a = 4 WHERE a = 1;
559 } {1 {column a is not unique}}
560 do_test trigger2-6.2e {
565 do_test trigger2-6.2f.1 {
567 UPDATE OR REPLACE tbl SET a = 1 WHERE a = 4;
571 do_test trigger2-6.2f.2 {
573 INSERT INTO tbl VALUES (2, 3, 4);
577 do_test trigger2-6.2g {
579 UPDATE OR ROLLBACK tbl SET a = 4 WHERE a = 1;
581 } {1 {column a is not unique}}
582 do_test trigger2-6.2h {
590 } ; # ifcapable conflict
592 # 7. Triggers on views
595 do_test trigger2-7.1 {
597 CREATE TABLE ab(a, b);
598 CREATE TABLE cd(c, d);
599 INSERT INTO ab VALUES (1, 2);
600 INSERT INTO ab VALUES (0, 0);
601 INSERT INTO cd VALUES (3, 4);
603 CREATE TABLE tlog(ii INTEGER PRIMARY KEY,
604 olda, oldb, oldc, oldd, newa, newb, newc, newd);
606 CREATE VIEW abcd AS SELECT a, b, c, d FROM ab, cd;
608 CREATE TRIGGER before_update INSTEAD OF UPDATE ON abcd BEGIN
609 INSERT INTO tlog VALUES(NULL,
610 old.a, old.b, old.c, old.d, new.a, new.b, new.c, new.d);
612 CREATE TRIGGER after_update INSTEAD OF UPDATE ON abcd BEGIN
613 INSERT INTO tlog VALUES(NULL,
614 old.a, old.b, old.c, old.d, new.a, new.b, new.c, new.d);
617 CREATE TRIGGER before_delete INSTEAD OF DELETE ON abcd BEGIN
618 INSERT INTO tlog VALUES(NULL,
619 old.a, old.b, old.c, old.d, 0, 0, 0, 0);
621 CREATE TRIGGER after_delete INSTEAD OF DELETE ON abcd BEGIN
622 INSERT INTO tlog VALUES(NULL,
623 old.a, old.b, old.c, old.d, 0, 0, 0, 0);
626 CREATE TRIGGER before_insert INSTEAD OF INSERT ON abcd BEGIN
627 INSERT INTO tlog VALUES(NULL,
628 0, 0, 0, 0, new.a, new.b, new.c, new.d);
630 CREATE TRIGGER after_insert INSTEAD OF INSERT ON abcd BEGIN
631 INSERT INTO tlog VALUES(NULL,
632 0, 0, 0, 0, new.a, new.b, new.c, new.d);
637 do_test trigger2-7.2 {
639 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1;
640 DELETE FROM abcd WHERE a = 1;
641 INSERT INTO abcd VALUES(10, 20, 30, 40);
644 } [ list 1 1 2 3 4 100 25 3 4 \
645 2 1 2 3 4 100 25 3 4 \
648 5 0 0 0 0 10 20 30 40 \
649 6 0 0 0 0 10 20 30 40 ]
651 do_test trigger2-7.3 {
654 INSERT INTO abcd VALUES(10, 20, 30, 40);
655 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1;
656 DELETE FROM abcd WHERE a = 1;
660 1 0 0 0 0 10 20 30 40 \
661 2 0 0 0 0 10 20 30 40 \
662 3 1 2 3 4 100 25 3 4 \
663 4 1 2 3 4 100 25 3 4 \
667 do_test trigger2-7.4 {
670 DELETE FROM abcd WHERE a = 1;
671 INSERT INTO abcd VALUES(10, 20, 30, 40);
672 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1;
678 3 0 0 0 0 10 20 30 40 \
679 4 0 0 0 0 10 20 30 40 \
680 5 1 2 3 4 100 25 3 4 \
681 6 1 2 3 4 100 25 3 4 \
684 do_test trigger2-8.1 {
686 CREATE TABLE t1(a,b,c);
687 INSERT INTO t1 VALUES(1,2,3);
689 SELECT a+b AS x, b+c AS y, a+c AS z FROM t1;
693 do_test trigger2-8.2 {
695 CREATE TABLE v1log(a,b,c,d,e,f);
696 CREATE TRIGGER r1 INSTEAD OF DELETE ON v1 BEGIN
697 INSERT INTO v1log VALUES(OLD.x,NULL,OLD.y,NULL,OLD.z,NULL);
699 DELETE FROM v1 WHERE x=1;
703 do_test trigger2-8.3 {
705 DELETE FROM v1 WHERE x=3;
709 do_test trigger2-8.4 {
711 INSERT INTO t1 VALUES(4,5,6);
713 DELETE FROM v1 WHERE y=11;
717 do_test trigger2-8.5 {
719 CREATE TRIGGER r2 INSTEAD OF INSERT ON v1 BEGIN
720 INSERT INTO v1log VALUES(NULL,NEW.x,NULL,NEW.y,NULL,NEW.z);
723 INSERT INTO v1 VALUES(1,2,3);
727 do_test trigger2-8.6 {
729 CREATE TRIGGER r3 INSTEAD OF UPDATE ON v1 BEGIN
730 INSERT INTO v1log VALUES(OLD.x,NEW.x,OLD.y,NEW.y,OLD.z,NEW.z);
733 UPDATE v1 SET x=x+100, y=y+200, z=z+300;
736 } {3 103 5 205 4 304 9 109 11 211 10 310}
740 integrity_check trigger2-9.9