2 #pragma ident "%Z%%M% %I% %E% SMI"
4 # The author disclaims copyright to this source code. In place of
5 # a legal notice, here is a blessing:
7 # May you do good and not evil.
8 # May you find forgiveness for yourself and forgive others.
9 # May you share freely, never taking more than you give.
11 #***********************************************************************
13 # Regression testing of FOR EACH ROW table triggers
15 # 1. Trigger execution order tests.
16 # These tests ensure that BEFORE and AFTER triggers are fired at the correct
17 # times relative to each other and the triggering statement.
19 # trigger2-1.1.*: ON UPDATE trigger execution model.
20 # trigger2-1.2.*: DELETE trigger execution model.
21 # trigger2-1.3.*: INSERT trigger execution model.
23 # 2. Trigger program execution tests.
24 # These tests ensure that trigger programs execute correctly (ie. that a
25 # trigger program can correctly execute INSERT, UPDATE, DELETE * SELECT
26 # statements, and combinations thereof).
28 # 3. Selective trigger execution
29 # This tests that conditional triggers (ie. UPDATE OF triggers and triggers
30 # with WHEN clauses) are fired only fired when they are supposed to be.
32 # trigger2-3.1: UPDATE OF triggers
33 # trigger2-3.2: WHEN clause
35 # 4. Cascaded trigger execution
36 # Tests that trigger-programs may cause other triggers to fire. Also that a
37 # trigger-program is never executed recursively.
39 # trigger2-4.1: Trivial cascading trigger
40 # trigger2-4.2: Trivial recursive trigger handling
42 # 5. Count changes behaviour.
43 # Verify that rows altered by triggers are not included in the return value
44 # of the "count changes" interface.
46 # 6. ON CONFLICT clause handling
47 # trigger2-6.1[a-f]: INSERT statements
48 # trigger2-6.2[a-f]: UPDATE statements
50 # 7. Triggers on views fire correctly.
53 set testdir [file dirname $argv0]
54 source $testdir/tester.tcl
59 {CREATE TEMP TABLE tbl (a, b);}
60 {CREATE TABLE tbl (a, b);}
61 {CREATE TABLE tbl (a INTEGER PRIMARY KEY, b);}
62 {CREATE TEMPORARY 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);}
65 {CREATE TEMP TABLE tbl (a, b); CREATE INDEX tbl_idx ON tbl(b);}
68 catchsql { DROP INDEX tbl_idx; }
79 INSERT INTO tbl VALUES(1, 2);
80 INSERT INTO tbl VALUES(3, 4);
82 CREATE TABLE rlog (idx, old_a, old_b, db_sum_a, db_sum_b, new_a, new_b);
83 CREATE TABLE clog (idx, old_a, old_b, db_sum_a, db_sum_b, new_a, new_b);
85 CREATE TRIGGER before_update_row BEFORE UPDATE ON tbl FOR EACH ROW
87 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
89 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl),
93 CREATE TRIGGER after_update_row AFTER UPDATE ON tbl FOR EACH ROW
95 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
97 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl),
101 CREATE TRIGGER conditional_update_row AFTER UPDATE ON tbl FOR EACH ROW
104 INSERT INTO clog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM clog),
106 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl),
111 do_test trigger2-1.$ii.1 {
113 UPDATE tbl SET a = a * 10, b = b * 10;
114 SELECT * FROM rlog ORDER BY idx;
115 SELECT * FROM clog ORDER BY idx;
117 } [list 1 1 2 4 6 10 20 \
126 INSERT INTO tbl VALUES (100, 100);
127 INSERT INTO tbl VALUES (300, 200);
128 CREATE TRIGGER delete_before_row BEFORE DELETE ON tbl FOR EACH ROW
130 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
132 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl),
136 CREATE TRIGGER delete_after_row AFTER DELETE ON tbl FOR EACH ROW
138 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
140 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl),
144 do_test trigger2-1.$ii.2 {
149 } [list 1 100 100 400 300 0 0 \
150 2 100 100 300 200 0 0 \
151 3 300 200 300 200 0 0 \
156 CREATE TRIGGER insert_before_row BEFORE INSERT ON tbl FOR EACH ROW
158 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
160 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl),
164 CREATE TRIGGER insert_after_row AFTER INSERT ON tbl FOR EACH ROW
166 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
168 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl),
172 do_test trigger2-1.$ii.3 {
175 CREATE TABLE other_tbl(a, b);
176 INSERT INTO other_tbl VALUES(1, 2);
177 INSERT INTO other_tbl VALUES(3, 4);
178 -- INSERT INTO tbl SELECT * FROM other_tbl;
179 INSERT INTO tbl VALUES(5, 6);
180 DROP TABLE other_tbl;
184 } [list 1 0 0 0 0 5 6 \
187 do_test trigger2-1.$ii.4 {
189 PRAGMA integrity_check;
197 DROP TABLE other_tbl;
203 {UPDATE tbl SET b = old.b;}
204 {INSERT INTO log VALUES(new.c, 2, 3);}
205 {DELETE FROM log WHERE a = 1;}
206 {INSERT INTO tbl VALUES(500, new.b * 10, 700);
207 UPDATE tbl SET c = old.c;
209 {INSERT INTO log select * from tbl;}
211 foreach test_varset [ list \
213 set statement {UPDATE tbl SET c = 10 WHERE a = 1;}
214 set prep {INSERT INTO tbl VALUES(1, 2, 3);}
223 set statement {DELETE FROM tbl WHERE a = 1;}
224 set prep {INSERT INTO tbl VALUES(1, 2, 3);}
230 set statement {INSERT INTO tbl VALUES(1, 2, 3);}
250 set statement_type [string range $statement 0 5]
251 set tr_program_fixed $tr_program
252 if {$statement_type == "DELETE"} {
253 regsub -all new\.a $tr_program_fixed {''} tr_program_fixed
254 regsub -all new\.b $tr_program_fixed {''} tr_program_fixed
255 regsub -all new\.c $tr_program_fixed {''} tr_program_fixed
257 if {$statement_type == "INSERT"} {
258 regsub -all old\.a $tr_program_fixed {''} tr_program_fixed
259 regsub -all old\.b $tr_program_fixed {''} tr_program_fixed
260 regsub -all old\.c $tr_program_fixed {''} tr_program_fixed
264 set tr_program_cooked $tr_program
265 regsub -all new\.a $tr_program_cooked $newA tr_program_cooked
266 regsub -all new\.b $tr_program_cooked $newB tr_program_cooked
267 regsub -all new\.c $tr_program_cooked $newC tr_program_cooked
268 regsub -all old\.a $tr_program_cooked $oldA tr_program_cooked
269 regsub -all old\.b $tr_program_cooked $oldB tr_program_cooked
270 regsub -all old\.c $tr_program_cooked $oldC tr_program_cooked
278 CREATE TABLE tbl(a PRIMARY KEY, b, c);
279 CREATE TABLE log(a, b, c);
282 set query {SELECT * FROM tbl; SELECT * FROM log;}
283 set prep "$prep; INSERT INTO log VALUES(1, 2, 3);\
284 INSERT INTO log VALUES(10, 20, 30);"
286 # Check execution of BEFORE programs:
288 set before_data [ execsql "$prep $tr_program_cooked $statement $query" ]
290 execsql "DELETE FROM tbl; DELETE FROM log; $prep";
291 execsql "CREATE TRIGGER the_trigger BEFORE [string range $statement 0 6]\
292 ON tbl BEGIN $tr_program_fixed END;"
294 do_test trigger2-2.$ii-before "execsql {$statement $query}" $before_data
296 execsql "DROP TRIGGER the_trigger;"
297 execsql "DELETE FROM tbl; DELETE FROM log;"
299 # Check execution of AFTER programs
300 set after_data [ execsql "$prep $statement $tr_program_cooked $query" ]
302 execsql "DELETE FROM tbl; DELETE FROM log; $prep";
303 execsql "CREATE TRIGGER the_trigger AFTER [string range $statement 0 6]\
304 ON tbl BEGIN $tr_program_fixed END;"
306 do_test trigger2-2.$ii-after "execsql {$statement $query}" $after_data
307 execsql "DROP TRIGGER the_trigger;"
309 do_test trigger2-2.$ii-integrity {
311 PRAGMA integrity_check;
324 # trigger2-3.1: UPDATE OF triggers
326 CREATE TABLE tbl (a, b, c, d);
327 CREATE TABLE log (a);
328 INSERT INTO log VALUES (0);
329 INSERT INTO tbl VALUES (0, 0, 0, 0);
330 INSERT INTO tbl VALUES (1, 0, 0, 0);
331 CREATE TRIGGER tbl_after_update_cd BEFORE UPDATE OF c, d ON tbl
333 UPDATE log SET a = a + 1;
336 do_test trigger2-3.1 {
338 UPDATE tbl SET b = 1, c = 10; -- 2
339 UPDATE tbl SET b = 10; -- 0
340 UPDATE tbl SET d = 4 WHERE a = 0; --1
341 UPDATE tbl SET a = 4, b = 10; --0
350 # trigger2-3.2: WHEN clause
351 set when_triggers [ list \
352 {t1 BEFORE INSERT ON tbl WHEN new.a > 20} \
353 {t2 BEFORE INSERT ON tbl WHEN (SELECT count(*) FROM tbl) = 0} ]
356 CREATE TABLE tbl (a, b, c, d);
357 CREATE TABLE log (a);
358 INSERT INTO log VALUES (0);
361 foreach trig $when_triggers {
362 execsql "CREATE TRIGGER $trig BEGIN UPDATE log set a = a + 1; END;"
365 do_test trigger2-3.2 {
368 INSERT INTO tbl VALUES(0, 0, 0, 0); -- 1
370 UPDATE log SET a = 0;
372 INSERT INTO tbl VALUES(0, 0, 0, 0); -- 0
374 UPDATE log SET a = 0;
376 INSERT INTO tbl VALUES(200, 0, 0, 0); -- 1
378 UPDATE log SET a = 0;
385 do_test trigger2-3.3 {
387 PRAGMA integrity_check;
391 # Simple cascaded trigger
393 CREATE TABLE tblA(a, b);
394 CREATE TABLE tblB(a, b);
395 CREATE TABLE tblC(a, b);
397 CREATE TRIGGER tr1 BEFORE INSERT ON tblA BEGIN
398 INSERT INTO tblB values(new.a, new.b);
401 CREATE TRIGGER tr2 BEFORE INSERT ON tblB BEGIN
402 INSERT INTO tblC values(new.a, new.b);
405 do_test trigger2-4.1 {
407 INSERT INTO tblA values(1, 2);
419 # Simple recursive trigger
421 CREATE TABLE tbl(a, b, c);
422 CREATE TRIGGER tbl_trig BEFORE INSERT ON tbl
424 INSERT INTO tbl VALUES (new.a, new.b, new.c);
427 do_test trigger2-4.2 {
429 INSERT INTO tbl VALUES (1, 2, 3);
439 CREATE TABLE tbl(a, b, c);
440 CREATE TRIGGER tbl_trig BEFORE INSERT ON tbl
442 INSERT INTO tbl VALUES (1, 2, 3);
443 INSERT INTO tbl VALUES (2, 2, 3);
444 UPDATE tbl set b = 10 WHERE a = 1;
445 DELETE FROM tbl WHERE a = 1;
451 INSERT INTO tbl VALUES(100, 200, 300);
459 # Handling of ON CONFLICT by INSERT statements inside triggers
461 CREATE TABLE tbl (a primary key, b, c);
462 CREATE TRIGGER ai_tbl AFTER INSERT ON tbl BEGIN
463 INSERT OR IGNORE INTO tbl values (new.a, 0, 0);
466 do_test trigger2-6.1a {
469 INSERT INTO tbl values (1, 2, 3);
473 do_test trigger2-6.1b {
475 INSERT OR ABORT INTO tbl values (2, 2, 3);
477 } {1 {column a is not unique}}
478 do_test trigger2-6.1c {
483 do_test trigger2-6.1d {
485 INSERT OR FAIL INTO tbl values (2, 2, 3);
487 } {1 {column a is not unique}}
488 do_test trigger2-6.1e {
493 do_test trigger2-6.1f {
495 INSERT OR REPLACE INTO tbl values (2, 2, 3);
499 do_test trigger2-6.1g {
501 INSERT OR ROLLBACK INTO tbl values (3, 2, 3);
503 } {1 {column a is not unique}}
504 do_test trigger2-6.1h {
509 execsql {DELETE FROM tbl}
512 # Handling of ON CONFLICT by UPDATE statements inside triggers
514 INSERT INTO tbl values (4, 2, 3);
515 INSERT INTO tbl values (6, 3, 4);
516 CREATE TRIGGER au_tbl AFTER UPDATE ON tbl BEGIN
517 UPDATE OR IGNORE tbl SET a = new.a, c = 10;
520 do_test trigger2-6.2a {
523 UPDATE tbl SET a = 1 WHERE a = 4;
527 do_test trigger2-6.2b {
529 UPDATE OR ABORT tbl SET a = 4 WHERE a = 1;
531 } {1 {column a is not unique}}
532 do_test trigger2-6.2c {
537 do_test trigger2-6.2d {
539 UPDATE OR FAIL tbl SET a = 4 WHERE a = 1;
541 } {1 {column a is not unique}}
542 do_test trigger2-6.2e {
547 do_test trigger2-6.2f.1 {
549 UPDATE OR REPLACE tbl SET a = 1 WHERE a = 4;
553 do_test trigger2-6.2f.2 {
555 INSERT INTO tbl VALUES (2, 3, 4);
559 do_test trigger2-6.2g {
561 UPDATE OR ROLLBACK tbl SET a = 4 WHERE a = 1;
563 } {1 {column a is not unique}}
564 do_test trigger2-6.2h {
573 # 7. Triggers on views
574 do_test trigger2-7.1 {
576 CREATE TABLE ab(a, b);
577 CREATE TABLE cd(c, d);
578 INSERT INTO ab VALUES (1, 2);
579 INSERT INTO ab VALUES (0, 0);
580 INSERT INTO cd VALUES (3, 4);
582 CREATE TABLE tlog(ii INTEGER PRIMARY KEY,
583 olda, oldb, oldc, oldd, newa, newb, newc, newd);
585 CREATE VIEW abcd AS SELECT a, b, c, d FROM ab, cd;
587 CREATE TRIGGER before_update INSTEAD OF UPDATE ON abcd BEGIN
588 INSERT INTO tlog VALUES(NULL,
589 old.a, old.b, old.c, old.d, new.a, new.b, new.c, new.d);
591 CREATE TRIGGER after_update INSTEAD OF UPDATE ON abcd BEGIN
592 INSERT INTO tlog VALUES(NULL,
593 old.a, old.b, old.c, old.d, new.a, new.b, new.c, new.d);
596 CREATE TRIGGER before_delete INSTEAD OF DELETE ON abcd BEGIN
597 INSERT INTO tlog VALUES(NULL,
598 old.a, old.b, old.c, old.d, 0, 0, 0, 0);
600 CREATE TRIGGER after_delete INSTEAD OF DELETE ON abcd BEGIN
601 INSERT INTO tlog VALUES(NULL,
602 old.a, old.b, old.c, old.d, 0, 0, 0, 0);
605 CREATE TRIGGER before_insert INSTEAD OF INSERT ON abcd BEGIN
606 INSERT INTO tlog VALUES(NULL,
607 0, 0, 0, 0, new.a, new.b, new.c, new.d);
609 CREATE TRIGGER after_insert INSTEAD OF INSERT ON abcd BEGIN
610 INSERT INTO tlog VALUES(NULL,
611 0, 0, 0, 0, new.a, new.b, new.c, new.d);
616 do_test trigger2-7.2 {
618 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1;
619 DELETE FROM abcd WHERE a = 1;
620 INSERT INTO abcd VALUES(10, 20, 30, 40);
623 } [ list 1 1 2 3 4 100 25 3 4 \
624 2 1 2 3 4 100 25 3 4 \
627 5 0 0 0 0 10 20 30 40 \
628 6 0 0 0 0 10 20 30 40 ]
630 do_test trigger2-7.3 {
633 INSERT INTO abcd VALUES(10, 20, 30, 40);
634 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1;
635 DELETE FROM abcd WHERE a = 1;
639 1 0 0 0 0 10 20 30 40 \
640 2 0 0 0 0 10 20 30 40 \
641 3 1 2 3 4 100 25 3 4 \
642 4 1 2 3 4 100 25 3 4 \
646 do_test trigger2-7.4 {
649 DELETE FROM abcd WHERE a = 1;
650 INSERT INTO abcd VALUES(10, 20, 30, 40);
651 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1;
657 3 0 0 0 0 10 20 30 40 \
658 4 0 0 0 0 10 20 30 40 \
659 5 1 2 3 4 100 25 3 4 \
660 6 1 2 3 4 100 25 3 4 \
663 do_test trigger2-8.1 {
665 CREATE TABLE t1(a,b,c);
666 INSERT INTO t1 VALUES(1,2,3);
668 SELECT a+b AS x, b+c AS y, a+c AS z FROM t1;
672 do_test trigger2-8.2 {
674 CREATE TABLE v1log(a,b,c,d,e,f);
675 CREATE TRIGGER r1 INSTEAD OF DELETE ON v1 BEGIN
676 INSERT INTO v1log VALUES(OLD.x,NULL,OLD.y,NULL,OLD.z,NULL);
678 DELETE FROM v1 WHERE x=1;
682 do_test trigger2-8.3 {
684 DELETE FROM v1 WHERE x=3;
688 do_test trigger2-8.4 {
690 INSERT INTO t1 VALUES(4,5,6);
692 DELETE FROM v1 WHERE y=11;
696 do_test trigger2-8.5 {
698 CREATE TRIGGER r2 INSTEAD OF INSERT ON v1 BEGIN
699 INSERT INTO v1log VALUES(NULL,NEW.x,NULL,NEW.y,NULL,NEW.z);
702 INSERT INTO v1 VALUES(1,2,3);
706 do_test trigger2-8.6 {
708 CREATE TRIGGER r3 INSTEAD OF UPDATE ON v1 BEGIN
709 INSERT INTO v1log VALUES(OLD.x,NEW.x,OLD.y,NEW.y,OLD.z,NEW.z);
712 UPDATE v1 SET x=x+100, y=y+200, z=z+300;
715 } {3 103 5 205 4 304 9 109 11 211 10 310}
717 do_test trigger2-9.9 {
718 execsql {PRAGMA integrity_check}