3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # Regression testing of FOR EACH ROW table triggers on WITHOUT ROWID
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 # without_rowid4-1.1.*: ON UPDATE trigger execution model.
20 # without_rowid4-1.2.*: DELETE trigger execution model.
21 # without_rowid4-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 # without_rowid4-3.1: UPDATE OF triggers
33 # without_rowid4-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 # without_rowid4-4.1: Trivial cascading trigger
40 # without_rowid4-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 # without_rowid4-6.1[a-f]: INSERT statements
48 # without_rowid4-6.2[a-f]: UPDATE statements
50 # 7. & 8. Triggers on views fire correctly.
53 set testdir [file dirname $argv0]
54 source $testdir/tester.tcl
55 ifcapable {!trigger} {
60 # The tests in this file were written before SQLite supported recursive
61 # trigger invocation, and some tests depend on that to pass. So disable
62 # recursive triggers for this file.
63 catchsql { pragma recursive_triggers = off }
68 set tbl_definitions [list \
69 {CREATE TABLE tbl (a INTEGER PRIMARY KEY, b) WITHOUT rowid;} \
70 {CREATE TABLE tbl (a, b PRIMARY KEY) WITHOUT rowid;} \
71 {CREATE TABLE tbl (a PRIMARY KEY, b) WITHOUT rowid;
72 CREATE INDEX tbl_idx ON tbl(b);} \
75 lappend tbl_definitions \
76 {CREATE TEMP TABLE tbl (a PRIMARY KEY, b) WITHOUT rowid;
77 CREATE INDEX tbl_idx ON tbl(b);}
78 lappend tbl_definitions \
79 {CREATE TEMP TABLE tbl (a PRIMARY KEY, b) WITHOUT rowid}
80 lappend tbl_definitions \
81 {CREATE TEMPORARY TABLE tbl (a INTEGER PRIMARY KEY, b) WITHOUT rowid;}
83 foreach tbl_defn $tbl_definitions {
85 catchsql { DROP INDEX tbl_idx; }
96 INSERT INTO tbl VALUES(1, 2);
97 INSERT INTO tbl VALUES(3, 4);
99 CREATE TABLE rlog (idx, old_a, old_b, db_sum_a, db_sum_b, new_a, new_b);
100 CREATE TABLE clog (idx, old_a, old_b, db_sum_a, db_sum_b, new_a, new_b);
102 CREATE TRIGGER before_update_row BEFORE UPDATE ON tbl FOR EACH ROW
104 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
106 (SELECT coalesce(sum(a),0) FROM tbl),
107 (SELECT coalesce(sum(b),0) FROM tbl),
111 CREATE TRIGGER after_update_row AFTER UPDATE ON tbl FOR EACH ROW
113 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
115 (SELECT coalesce(sum(a),0) FROM tbl),
116 (SELECT coalesce(sum(b),0) FROM tbl),
120 CREATE TRIGGER conditional_update_row AFTER UPDATE ON tbl FOR EACH ROW
123 INSERT INTO clog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM clog),
125 (SELECT coalesce(sum(a),0) FROM tbl),
126 (SELECT coalesce(sum(b),0) FROM tbl),
131 do_test without_rowid4-1.$ii.1 {
134 UPDATE tbl SET a = a * 10, b = b * 10;
135 SELECT * FROM rlog ORDER BY idx;
136 SELECT * FROM clog ORDER BY idx;
138 lappend r [expr {int($v)}]
141 } [list 1 1 2 4 6 10 20 \
150 INSERT INTO tbl VALUES (100, 100);
151 INSERT INTO tbl VALUES (300, 200);
152 CREATE TRIGGER delete_before_row BEFORE DELETE ON tbl FOR EACH ROW
154 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
156 (SELECT coalesce(sum(a),0) FROM tbl),
157 (SELECT coalesce(sum(b),0) FROM tbl),
161 CREATE TRIGGER delete_after_row AFTER DELETE ON tbl FOR EACH ROW
163 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
165 (SELECT coalesce(sum(a),0) FROM tbl),
166 (SELECT coalesce(sum(b),0) FROM tbl),
170 do_test without_rowid4-1.$ii.2 {
176 lappend r [expr {int($v)}]
179 } [list 1 100 100 400 300 0 0 \
180 2 100 100 300 200 0 0 \
181 3 300 200 300 200 0 0 \
186 CREATE TRIGGER insert_before_row BEFORE INSERT ON tbl FOR EACH ROW
188 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
190 (SELECT coalesce(sum(a),0) FROM tbl),
191 (SELECT coalesce(sum(b),0) FROM tbl),
195 CREATE TRIGGER insert_after_row AFTER INSERT ON tbl FOR EACH ROW
197 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog),
199 (SELECT coalesce(sum(a),0) FROM tbl),
200 (SELECT coalesce(sum(b),0) FROM tbl),
204 do_test without_rowid4-1.$ii.3 {
207 CREATE TABLE other_tbl(a, b);
208 INSERT INTO other_tbl VALUES(1, 2);
209 INSERT INTO other_tbl VALUES(3, 4);
210 -- INSERT INTO tbl SELECT * FROM other_tbl;
211 INSERT INTO tbl VALUES(5, 6);
212 DROP TABLE other_tbl;
216 } [list 1 0 0 0 0 5 6 \
219 integrity_check without_rowid4-1.$ii.4
225 DROP TABLE other_tbl;
232 {UPDATE tbl SET b = old.b;}
233 {INSERT INTO log VALUES(new.c, 2, 3);}
234 {DELETE FROM log WHERE a = 1;}
235 {INSERT INTO tbl VALUES(500, new.b * 10, 700);
236 UPDATE tbl SET c = old.c;
238 {INSERT INTO log select * from tbl;}
240 foreach test_varset [ list \
242 set statement {UPDATE tbl SET c = 10 WHERE a = 1;}
243 set prep {INSERT INTO tbl VALUES(1, 2, 3);}
252 set statement {DELETE FROM tbl WHERE a = 1;}
253 set prep {INSERT INTO tbl VALUES(1, 2, 3);}
259 set statement {INSERT INTO tbl VALUES(1, 2, 3);}
279 set statement_type [string range $statement 0 5]
280 set tr_program_fixed $tr_program
281 if {$statement_type == "DELETE"} {
282 regsub -all new\.a $tr_program_fixed {''} tr_program_fixed
283 regsub -all new\.b $tr_program_fixed {''} tr_program_fixed
284 regsub -all new\.c $tr_program_fixed {''} tr_program_fixed
286 if {$statement_type == "INSERT"} {
287 regsub -all old\.a $tr_program_fixed {''} tr_program_fixed
288 regsub -all old\.b $tr_program_fixed {''} tr_program_fixed
289 regsub -all old\.c $tr_program_fixed {''} tr_program_fixed
293 set tr_program_cooked $tr_program
294 regsub -all new\.a $tr_program_cooked $newA tr_program_cooked
295 regsub -all new\.b $tr_program_cooked $newB tr_program_cooked
296 regsub -all new\.c $tr_program_cooked $newC tr_program_cooked
297 regsub -all old\.a $tr_program_cooked $oldA tr_program_cooked
298 regsub -all old\.b $tr_program_cooked $oldB tr_program_cooked
299 regsub -all old\.c $tr_program_cooked $oldC tr_program_cooked
307 CREATE TABLE tbl(a PRIMARY KEY, b, c) WITHOUT rowid;
308 CREATE TABLE log(a, b, c);
311 set query {SELECT * FROM tbl; SELECT * FROM log;}
312 set prep "$prep; INSERT INTO log VALUES(1, 2, 3);\
313 INSERT INTO log VALUES(10, 20, 30);"
315 # Check execution of BEFORE programs:
317 set before_data [ execsql "$prep $tr_program_cooked $statement $query" ]
319 execsql "DELETE FROM tbl; DELETE FROM log; $prep";
320 execsql "CREATE TRIGGER the_trigger BEFORE [string range $statement 0 6]\
321 ON tbl BEGIN $tr_program_fixed END;"
323 do_test without_rowid4-2.$ii-before "execsql {$statement $query}" $before_data
325 execsql "DROP TRIGGER the_trigger;"
326 execsql "DELETE FROM tbl; DELETE FROM log;"
328 # Check execution of AFTER programs
329 set after_data [ execsql "$prep $statement $tr_program_cooked $query" ]
331 execsql "DELETE FROM tbl; DELETE FROM log; $prep";
332 execsql "CREATE TRIGGER the_trigger AFTER [string range $statement 0 6]\
333 ON tbl BEGIN $tr_program_fixed END;"
335 do_test without_rowid4-2.$ii-after "execsql {$statement $query}" $after_data
336 execsql "DROP TRIGGER the_trigger;"
338 integrity_check without_rowid4-2.$ii-integrity
348 # without_rowid4-3.1: UPDATE OF triggers
350 CREATE TABLE tbl (a, b, c, d, PRIMARY KEY(a,b,c,d)) WITHOUT rowid;
351 CREATE TABLE log (a);
352 INSERT INTO log VALUES (0);
353 INSERT INTO tbl VALUES (0, 0, 0, 0);
354 INSERT INTO tbl VALUES (1, 0, 0, 0);
355 CREATE TRIGGER tbl_after_update_cd BEFORE UPDATE OF c, d ON tbl
357 UPDATE log SET a = a + 1;
360 do_test without_rowid4-3.1 {
362 UPDATE tbl SET b = 1, c = 10; -- 2
363 UPDATE tbl SET b = 10; -- 0
364 UPDATE tbl SET d = 4 WHERE a = 0; --1
365 UPDATE tbl SET a = 4, b = 10; --0
374 # without_rowid4-3.2: WHEN clause
375 set when_triggers [list {t1 BEFORE INSERT ON tbl WHEN new.a > 20}]
377 lappend when_triggers \
378 {t2 BEFORE INSERT ON tbl WHEN (SELECT count(*) FROM tbl) = 0}
382 CREATE TABLE tbl (a, b, c, d);
383 CREATE TABLE log (a);
384 INSERT INTO log VALUES (0);
387 foreach trig $when_triggers {
388 execsql "CREATE TRIGGER $trig BEGIN UPDATE log set a = a + 1; END;"
396 do_test without_rowid4-3.2 {
399 INSERT INTO tbl VALUES(0, 0, 0, 0); -- 1 (ifcapable subquery)
401 UPDATE log SET a = 0;
403 INSERT INTO tbl VALUES(0, 0, 0, 0); -- 0
405 UPDATE log SET a = 0;
407 INSERT INTO tbl VALUES(200, 0, 0, 0); -- 1
409 UPDATE log SET a = 0;
416 integrity_check without_rowid4-3.3
418 # Simple cascaded trigger
420 CREATE TABLE tblA(a, b, PRIMARY KEY(a,b)) WITHOUT rowid;
421 CREATE TABLE tblB(a, b, PRIMARY KEY(a,b)) WITHOUT rowid;
422 CREATE TABLE tblC(a, b, PRIMARY KEY(a,b)) WITHOUT rowid;
424 CREATE TRIGGER tr1 BEFORE INSERT ON tblA BEGIN
425 INSERT INTO tblB values(new.a, new.b);
428 CREATE TRIGGER tr2 BEFORE INSERT ON tblB BEGIN
429 INSERT INTO tblC values(new.a, new.b);
432 do_test without_rowid4-4.1 {
434 INSERT INTO tblA values(1, 2);
446 # Simple recursive trigger
448 CREATE TABLE tbl(a, b, c, PRIMARY KEY(c,a,b)) WITHOUT rowid;
449 CREATE TRIGGER tbl_trig BEFORE INSERT ON tbl
451 INSERT INTO tbl VALUES (new.a, new.b, new.c+1);
454 do_test without_rowid4-4.2 {
456 INSERT INTO tbl VALUES (1, 2, 3);
466 CREATE TABLE tbl(a, b, c, PRIMARY KEY(c,a,b)) WITHOUT rowid;
467 CREATE TRIGGER tbl_trig BEFORE INSERT ON tbl
469 INSERT INTO tbl VALUES (1, 2, 3);
470 INSERT INTO tbl VALUES (2, 2, 3);
471 UPDATE tbl set b = 10 WHERE a = 1;
472 DELETE FROM tbl WHERE a = 1;
476 do_test without_rowid4-5 {
478 INSERT INTO tbl VALUES(100, 200, 300);
487 # Handling of ON CONFLICT by INSERT statements inside triggers
489 CREATE TABLE tbl (a PRIMARY KEY, b, c) WITHOUT rowid;
490 CREATE TRIGGER ai_tbl AFTER INSERT ON tbl BEGIN
491 INSERT OR IGNORE INTO tbl values (new.a, 0, 0);
494 do_test without_rowid4-6.1a {
497 INSERT INTO tbl values (1, 2, 3);
501 do_test without_rowid4-6.1b {
503 INSERT OR ABORT INTO tbl values (2, 2, 3);
505 } {1 {UNIQUE constraint failed: tbl.a}}
506 do_test without_rowid4-6.1c {
511 do_test without_rowid4-6.1d {
513 INSERT OR FAIL INTO tbl values (2, 2, 3);
515 } {1 {UNIQUE constraint failed: tbl.a}}
516 do_test without_rowid4-6.1e {
521 do_test without_rowid4-6.1f {
523 INSERT OR REPLACE INTO tbl values (2, 2, 3);
527 do_test without_rowid4-6.1g {
529 INSERT OR ROLLBACK INTO tbl values (3, 2, 3);
531 } {1 {UNIQUE constraint failed: tbl.a}}
532 do_test without_rowid4-6.1h {
537 execsql {DELETE FROM tbl}
540 # Handling of ON CONFLICT by UPDATE statements inside triggers
542 INSERT INTO tbl values (4, 2, 3);
543 INSERT INTO tbl values (6, 3, 4);
544 CREATE TRIGGER au_tbl AFTER UPDATE ON tbl BEGIN
545 UPDATE OR IGNORE tbl SET a = new.a, c = 10;
548 do_test without_rowid4-6.2a {
551 UPDATE tbl SET a = 1 WHERE a = 4;
555 do_test without_rowid4-6.2b {
557 UPDATE OR ABORT tbl SET a = 4 WHERE a = 1;
559 } {1 {UNIQUE constraint failed: tbl.a}}
560 do_test without_rowid4-6.2c {
565 do_test without_rowid4-6.2d {
567 UPDATE OR FAIL tbl SET a = 4 WHERE a = 1;
569 } {1 {UNIQUE constraint failed: tbl.a}}
570 do_test without_rowid4-6.2e {
575 do_test without_rowid4-6.2f.1 {
577 UPDATE OR REPLACE tbl SET a = 1 WHERE a = 4;
581 do_test without_rowid4-6.2f.2 {
583 INSERT INTO tbl VALUES (2, 3, 4);
587 do_test without_rowid4-6.2g {
589 UPDATE OR ROLLBACK tbl SET a = 4 WHERE a = 1;
591 } {1 {UNIQUE constraint failed: tbl.a}}
592 do_test without_rowid4-6.2h {
600 } ; # ifcapable conflict
602 # 7. Triggers on views
605 do_test without_rowid4-7.1 {
607 CREATE TABLE ab(a, b, PRIMARY KEY(a,b)) WITHOUT rowid;
608 CREATE TABLE cd(c, d, PRIMARY KEY(c,d)) WITHOUT rowid;
609 INSERT INTO ab VALUES (1, 2);
610 INSERT INTO ab VALUES (0, 0);
611 INSERT INTO cd VALUES (3, 4);
613 CREATE TABLE tlog(ii INTEGER PRIMARY KEY,
614 olda, oldb, oldc, oldd, newa, newb, newc, newd);
616 CREATE VIEW abcd AS SELECT a, b, c, d FROM ab, cd;
618 CREATE TRIGGER before_update INSTEAD OF UPDATE ON abcd BEGIN
619 INSERT INTO tlog VALUES(NULL,
620 old.a, old.b, old.c, old.d, new.a, new.b, new.c, new.d);
622 CREATE TRIGGER after_update INSTEAD OF UPDATE ON abcd BEGIN
623 INSERT INTO tlog VALUES(NULL,
624 old.a, old.b, old.c, old.d, new.a, new.b, new.c, new.d);
627 CREATE TRIGGER before_delete INSTEAD OF DELETE ON abcd BEGIN
628 INSERT INTO tlog VALUES(NULL,
629 old.a, old.b, old.c, old.d, 0, 0, 0, 0);
631 CREATE TRIGGER after_delete INSTEAD OF DELETE ON abcd BEGIN
632 INSERT INTO tlog VALUES(NULL,
633 old.a, old.b, old.c, old.d, 0, 0, 0, 0);
636 CREATE TRIGGER before_insert INSTEAD OF INSERT ON abcd BEGIN
637 INSERT INTO tlog VALUES(NULL,
638 0, 0, 0, 0, new.a, new.b, new.c, new.d);
640 CREATE TRIGGER after_insert INSTEAD OF INSERT ON abcd BEGIN
641 INSERT INTO tlog VALUES(NULL,
642 0, 0, 0, 0, new.a, new.b, new.c, new.d);
647 do_test without_rowid4-7.2 {
649 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1;
650 DELETE FROM abcd WHERE a = 1;
651 INSERT INTO abcd VALUES(10, 20, 30, 40);
654 } [ list 1 1 2 3 4 100 25 3 4 \
655 2 1 2 3 4 100 25 3 4 \
658 5 0 0 0 0 10 20 30 40 \
659 6 0 0 0 0 10 20 30 40 ]
661 do_test without_rowid4-7.3 {
664 INSERT INTO abcd VALUES(10, 20, 30, 40);
665 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1;
666 DELETE FROM abcd WHERE a = 1;
670 1 0 0 0 0 10 20 30 40 \
671 2 0 0 0 0 10 20 30 40 \
672 3 1 2 3 4 100 25 3 4 \
673 4 1 2 3 4 100 25 3 4 \
677 do_test without_rowid4-7.4 {
680 DELETE FROM abcd WHERE a = 1;
681 INSERT INTO abcd VALUES(10, 20, 30, 40);
682 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1;
688 3 0 0 0 0 10 20 30 40 \
689 4 0 0 0 0 10 20 30 40 \
690 5 1 2 3 4 100 25 3 4 \
691 6 1 2 3 4 100 25 3 4 \
694 do_test without_rowid4-8.1 {
696 CREATE TABLE t1(a,b,c, PRIMARY KEY(a,b,c)) WITHOUT rowid;
697 INSERT INTO t1 VALUES(1,2,3);
699 SELECT a+b AS x, b+c AS y, a+c AS z FROM t1;
703 do_test without_rowid4-8.2 {
705 CREATE TABLE v1log(a,b,c,d,e,f);
706 CREATE TRIGGER r1 INSTEAD OF DELETE ON v1 BEGIN
707 INSERT INTO v1log VALUES(OLD.x,NULL,OLD.y,NULL,OLD.z,NULL);
709 DELETE FROM v1 WHERE x=1;
713 do_test without_rowid4-8.3 {
715 DELETE FROM v1 WHERE x=3;
719 do_test without_rowid4-8.4 {
721 INSERT INTO t1 VALUES(4,5,6);
723 DELETE FROM v1 WHERE y=11;
727 do_test without_rowid4-8.5 {
729 CREATE TRIGGER r2 INSTEAD OF INSERT ON v1 BEGIN
730 INSERT INTO v1log VALUES(NULL,NEW.x,NULL,NEW.y,NULL,NEW.z);
733 INSERT INTO v1 VALUES(1,2,3);
737 do_test without_rowid4-8.6 {
739 CREATE TRIGGER r3 INSTEAD OF UPDATE ON v1 BEGIN
740 INSERT INTO v1log VALUES(OLD.x,NEW.x,OLD.y,NEW.y,OLD.z,NEW.z);
743 UPDATE v1 SET x=x+100, y=y+200, z=z+300;
746 } {3 103 5 205 4 304 9 109 11 211 10 310}
748 # At one point the following was causing a segfault.
749 do_test without_rowid4-9.1 {
751 CREATE TABLE t3(a TEXT, b TEXT);
752 CREATE VIEW v3 AS SELECT t3.a FROM t3;
753 CREATE TRIGGER trig1 INSTEAD OF DELETE ON v3 BEGIN
756 DELETE FROM v3 WHERE a = 1;
762 integrity_check without_rowid4-9.9