1*c5c4113dSnw141292 2*c5c4113dSnw141292#pragma ident "%Z%%M% %I% %E% SMI" 3*c5c4113dSnw141292 4*c5c4113dSnw141292# The author disclaims copyright to this source code. In place of 5*c5c4113dSnw141292# a legal notice, here is a blessing: 6*c5c4113dSnw141292# 7*c5c4113dSnw141292# May you do good and not evil. 8*c5c4113dSnw141292# May you find forgiveness for yourself and forgive others. 9*c5c4113dSnw141292# May you share freely, never taking more than you give. 10*c5c4113dSnw141292# 11*c5c4113dSnw141292#*********************************************************************** 12*c5c4113dSnw141292# 13*c5c4113dSnw141292# Regression testing of FOR EACH ROW table triggers 14*c5c4113dSnw141292# 15*c5c4113dSnw141292# 1. Trigger execution order tests. 16*c5c4113dSnw141292# These tests ensure that BEFORE and AFTER triggers are fired at the correct 17*c5c4113dSnw141292# times relative to each other and the triggering statement. 18*c5c4113dSnw141292# 19*c5c4113dSnw141292# trigger2-1.1.*: ON UPDATE trigger execution model. 20*c5c4113dSnw141292# trigger2-1.2.*: DELETE trigger execution model. 21*c5c4113dSnw141292# trigger2-1.3.*: INSERT trigger execution model. 22*c5c4113dSnw141292# 23*c5c4113dSnw141292# 2. Trigger program execution tests. 24*c5c4113dSnw141292# These tests ensure that trigger programs execute correctly (ie. that a 25*c5c4113dSnw141292# trigger program can correctly execute INSERT, UPDATE, DELETE * SELECT 26*c5c4113dSnw141292# statements, and combinations thereof). 27*c5c4113dSnw141292# 28*c5c4113dSnw141292# 3. Selective trigger execution 29*c5c4113dSnw141292# This tests that conditional triggers (ie. UPDATE OF triggers and triggers 30*c5c4113dSnw141292# with WHEN clauses) are fired only fired when they are supposed to be. 31*c5c4113dSnw141292# 32*c5c4113dSnw141292# trigger2-3.1: UPDATE OF triggers 33*c5c4113dSnw141292# trigger2-3.2: WHEN clause 34*c5c4113dSnw141292# 35*c5c4113dSnw141292# 4. Cascaded trigger execution 36*c5c4113dSnw141292# Tests that trigger-programs may cause other triggers to fire. Also that a 37*c5c4113dSnw141292# trigger-program is never executed recursively. 38*c5c4113dSnw141292# 39*c5c4113dSnw141292# trigger2-4.1: Trivial cascading trigger 40*c5c4113dSnw141292# trigger2-4.2: Trivial recursive trigger handling 41*c5c4113dSnw141292# 42*c5c4113dSnw141292# 5. Count changes behaviour. 43*c5c4113dSnw141292# Verify that rows altered by triggers are not included in the return value 44*c5c4113dSnw141292# of the "count changes" interface. 45*c5c4113dSnw141292# 46*c5c4113dSnw141292# 6. ON CONFLICT clause handling 47*c5c4113dSnw141292# trigger2-6.1[a-f]: INSERT statements 48*c5c4113dSnw141292# trigger2-6.2[a-f]: UPDATE statements 49*c5c4113dSnw141292# 50*c5c4113dSnw141292# 7. Triggers on views fire correctly. 51*c5c4113dSnw141292# 52*c5c4113dSnw141292 53*c5c4113dSnw141292set testdir [file dirname $argv0] 54*c5c4113dSnw141292source $testdir/tester.tcl 55*c5c4113dSnw141292 56*c5c4113dSnw141292# 1. 57*c5c4113dSnw141292set ii 0 58*c5c4113dSnw141292foreach tbl_defn { 59*c5c4113dSnw141292 {CREATE TEMP TABLE tbl (a, b);} 60*c5c4113dSnw141292 {CREATE TABLE tbl (a, b);} 61*c5c4113dSnw141292 {CREATE TABLE tbl (a INTEGER PRIMARY KEY, b);} 62*c5c4113dSnw141292 {CREATE TEMPORARY TABLE tbl (a INTEGER PRIMARY KEY, b);} 63*c5c4113dSnw141292 {CREATE TABLE tbl (a, b PRIMARY KEY);} 64*c5c4113dSnw141292 {CREATE TABLE tbl (a, b); CREATE INDEX tbl_idx ON tbl(b);} 65*c5c4113dSnw141292 {CREATE TEMP TABLE tbl (a, b); CREATE INDEX tbl_idx ON tbl(b);} 66*c5c4113dSnw141292} { 67*c5c4113dSnw141292 incr ii 68*c5c4113dSnw141292 catchsql { DROP INDEX tbl_idx; } 69*c5c4113dSnw141292 catchsql { 70*c5c4113dSnw141292 DROP TABLE rlog; 71*c5c4113dSnw141292 DROP TABLE clog; 72*c5c4113dSnw141292 DROP TABLE tbl; 73*c5c4113dSnw141292 DROP TABLE other_tbl; 74*c5c4113dSnw141292 } 75*c5c4113dSnw141292 76*c5c4113dSnw141292 execsql $tbl_defn 77*c5c4113dSnw141292 78*c5c4113dSnw141292 execsql { 79*c5c4113dSnw141292 INSERT INTO tbl VALUES(1, 2); 80*c5c4113dSnw141292 INSERT INTO tbl VALUES(3, 4); 81*c5c4113dSnw141292 82*c5c4113dSnw141292 CREATE TABLE rlog (idx, old_a, old_b, db_sum_a, db_sum_b, new_a, new_b); 83*c5c4113dSnw141292 CREATE TABLE clog (idx, old_a, old_b, db_sum_a, db_sum_b, new_a, new_b); 84*c5c4113dSnw141292 85*c5c4113dSnw141292 CREATE TRIGGER before_update_row BEFORE UPDATE ON tbl FOR EACH ROW 86*c5c4113dSnw141292 BEGIN 87*c5c4113dSnw141292 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog), 88*c5c4113dSnw141292 old.a, old.b, 89*c5c4113dSnw141292 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl), 90*c5c4113dSnw141292 new.a, new.b); 91*c5c4113dSnw141292 END; 92*c5c4113dSnw141292 93*c5c4113dSnw141292 CREATE TRIGGER after_update_row AFTER UPDATE ON tbl FOR EACH ROW 94*c5c4113dSnw141292 BEGIN 95*c5c4113dSnw141292 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog), 96*c5c4113dSnw141292 old.a, old.b, 97*c5c4113dSnw141292 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl), 98*c5c4113dSnw141292 new.a, new.b); 99*c5c4113dSnw141292 END; 100*c5c4113dSnw141292 101*c5c4113dSnw141292 CREATE TRIGGER conditional_update_row AFTER UPDATE ON tbl FOR EACH ROW 102*c5c4113dSnw141292 WHEN old.a = 1 103*c5c4113dSnw141292 BEGIN 104*c5c4113dSnw141292 INSERT INTO clog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM clog), 105*c5c4113dSnw141292 old.a, old.b, 106*c5c4113dSnw141292 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl), 107*c5c4113dSnw141292 new.a, new.b); 108*c5c4113dSnw141292 END; 109*c5c4113dSnw141292 } 110*c5c4113dSnw141292 111*c5c4113dSnw141292 do_test trigger2-1.$ii.1 { 112*c5c4113dSnw141292 execsql { 113*c5c4113dSnw141292 UPDATE tbl SET a = a * 10, b = b * 10; 114*c5c4113dSnw141292 SELECT * FROM rlog ORDER BY idx; 115*c5c4113dSnw141292 SELECT * FROM clog ORDER BY idx; 116*c5c4113dSnw141292 } 117*c5c4113dSnw141292 } [list 1 1 2 4 6 10 20 \ 118*c5c4113dSnw141292 2 1 2 13 24 10 20 \ 119*c5c4113dSnw141292 3 3 4 13 24 30 40 \ 120*c5c4113dSnw141292 4 3 4 40 60 30 40 \ 121*c5c4113dSnw141292 1 1 2 13 24 10 20 ] 122*c5c4113dSnw141292 123*c5c4113dSnw141292 execsql { 124*c5c4113dSnw141292 DELETE FROM rlog; 125*c5c4113dSnw141292 DELETE FROM tbl; 126*c5c4113dSnw141292 INSERT INTO tbl VALUES (100, 100); 127*c5c4113dSnw141292 INSERT INTO tbl VALUES (300, 200); 128*c5c4113dSnw141292 CREATE TRIGGER delete_before_row BEFORE DELETE ON tbl FOR EACH ROW 129*c5c4113dSnw141292 BEGIN 130*c5c4113dSnw141292 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog), 131*c5c4113dSnw141292 old.a, old.b, 132*c5c4113dSnw141292 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl), 133*c5c4113dSnw141292 0, 0); 134*c5c4113dSnw141292 END; 135*c5c4113dSnw141292 136*c5c4113dSnw141292 CREATE TRIGGER delete_after_row AFTER DELETE ON tbl FOR EACH ROW 137*c5c4113dSnw141292 BEGIN 138*c5c4113dSnw141292 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog), 139*c5c4113dSnw141292 old.a, old.b, 140*c5c4113dSnw141292 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl), 141*c5c4113dSnw141292 0, 0); 142*c5c4113dSnw141292 END; 143*c5c4113dSnw141292 } 144*c5c4113dSnw141292 do_test trigger2-1.$ii.2 { 145*c5c4113dSnw141292 execsql { 146*c5c4113dSnw141292 DELETE FROM tbl; 147*c5c4113dSnw141292 SELECT * FROM rlog; 148*c5c4113dSnw141292 } 149*c5c4113dSnw141292 } [list 1 100 100 400 300 0 0 \ 150*c5c4113dSnw141292 2 100 100 300 200 0 0 \ 151*c5c4113dSnw141292 3 300 200 300 200 0 0 \ 152*c5c4113dSnw141292 4 300 200 0 0 0 0 ] 153*c5c4113dSnw141292 154*c5c4113dSnw141292 execsql { 155*c5c4113dSnw141292 DELETE FROM rlog; 156*c5c4113dSnw141292 CREATE TRIGGER insert_before_row BEFORE INSERT ON tbl FOR EACH ROW 157*c5c4113dSnw141292 BEGIN 158*c5c4113dSnw141292 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog), 159*c5c4113dSnw141292 0, 0, 160*c5c4113dSnw141292 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl), 161*c5c4113dSnw141292 new.a, new.b); 162*c5c4113dSnw141292 END; 163*c5c4113dSnw141292 164*c5c4113dSnw141292 CREATE TRIGGER insert_after_row AFTER INSERT ON tbl FOR EACH ROW 165*c5c4113dSnw141292 BEGIN 166*c5c4113dSnw141292 INSERT INTO rlog VALUES ( (SELECT coalesce(max(idx),0) + 1 FROM rlog), 167*c5c4113dSnw141292 0, 0, 168*c5c4113dSnw141292 (SELECT sum(a) FROM tbl), (SELECT sum(b) FROM tbl), 169*c5c4113dSnw141292 new.a, new.b); 170*c5c4113dSnw141292 END; 171*c5c4113dSnw141292 } 172*c5c4113dSnw141292 do_test trigger2-1.$ii.3 { 173*c5c4113dSnw141292 execsql { 174*c5c4113dSnw141292 175*c5c4113dSnw141292 CREATE TABLE other_tbl(a, b); 176*c5c4113dSnw141292 INSERT INTO other_tbl VALUES(1, 2); 177*c5c4113dSnw141292 INSERT INTO other_tbl VALUES(3, 4); 178*c5c4113dSnw141292 -- INSERT INTO tbl SELECT * FROM other_tbl; 179*c5c4113dSnw141292 INSERT INTO tbl VALUES(5, 6); 180*c5c4113dSnw141292 DROP TABLE other_tbl; 181*c5c4113dSnw141292 182*c5c4113dSnw141292 SELECT * FROM rlog; 183*c5c4113dSnw141292 } 184*c5c4113dSnw141292 } [list 1 0 0 0 0 5 6 \ 185*c5c4113dSnw141292 2 0 0 5 6 5 6 ] 186*c5c4113dSnw141292 187*c5c4113dSnw141292 do_test trigger2-1.$ii.4 { 188*c5c4113dSnw141292 execsql { 189*c5c4113dSnw141292 PRAGMA integrity_check; 190*c5c4113dSnw141292 } 191*c5c4113dSnw141292 } {ok} 192*c5c4113dSnw141292} 193*c5c4113dSnw141292catchsql { 194*c5c4113dSnw141292 DROP TABLE rlog; 195*c5c4113dSnw141292 DROP TABLE clog; 196*c5c4113dSnw141292 DROP TABLE tbl; 197*c5c4113dSnw141292 DROP TABLE other_tbl; 198*c5c4113dSnw141292} 199*c5c4113dSnw141292 200*c5c4113dSnw141292# 2. 201*c5c4113dSnw141292set ii 0 202*c5c4113dSnw141292foreach tr_program { 203*c5c4113dSnw141292 {UPDATE tbl SET b = old.b;} 204*c5c4113dSnw141292 {INSERT INTO log VALUES(new.c, 2, 3);} 205*c5c4113dSnw141292 {DELETE FROM log WHERE a = 1;} 206*c5c4113dSnw141292 {INSERT INTO tbl VALUES(500, new.b * 10, 700); 207*c5c4113dSnw141292 UPDATE tbl SET c = old.c; 208*c5c4113dSnw141292 DELETE FROM log;} 209*c5c4113dSnw141292 {INSERT INTO log select * from tbl;} 210*c5c4113dSnw141292} { 211*c5c4113dSnw141292 foreach test_varset [ list \ 212*c5c4113dSnw141292 { 213*c5c4113dSnw141292 set statement {UPDATE tbl SET c = 10 WHERE a = 1;} 214*c5c4113dSnw141292 set prep {INSERT INTO tbl VALUES(1, 2, 3);} 215*c5c4113dSnw141292 set newC 10 216*c5c4113dSnw141292 set newB 2 217*c5c4113dSnw141292 set newA 1 218*c5c4113dSnw141292 set oldA 1 219*c5c4113dSnw141292 set oldB 2 220*c5c4113dSnw141292 set oldC 3 221*c5c4113dSnw141292 } \ 222*c5c4113dSnw141292 { 223*c5c4113dSnw141292 set statement {DELETE FROM tbl WHERE a = 1;} 224*c5c4113dSnw141292 set prep {INSERT INTO tbl VALUES(1, 2, 3);} 225*c5c4113dSnw141292 set oldA 1 226*c5c4113dSnw141292 set oldB 2 227*c5c4113dSnw141292 set oldC 3 228*c5c4113dSnw141292 } \ 229*c5c4113dSnw141292 { 230*c5c4113dSnw141292 set statement {INSERT INTO tbl VALUES(1, 2, 3);} 231*c5c4113dSnw141292 set newA 1 232*c5c4113dSnw141292 set newB 2 233*c5c4113dSnw141292 set newC 3 234*c5c4113dSnw141292 } 235*c5c4113dSnw141292 ] \ 236*c5c4113dSnw141292 { 237*c5c4113dSnw141292 set statement {} 238*c5c4113dSnw141292 set prep {} 239*c5c4113dSnw141292 set newA {''} 240*c5c4113dSnw141292 set newB {''} 241*c5c4113dSnw141292 set newC {''} 242*c5c4113dSnw141292 set oldA {''} 243*c5c4113dSnw141292 set oldB {''} 244*c5c4113dSnw141292 set oldC {''} 245*c5c4113dSnw141292 246*c5c4113dSnw141292 incr ii 247*c5c4113dSnw141292 248*c5c4113dSnw141292 eval $test_varset 249*c5c4113dSnw141292 250*c5c4113dSnw141292 set statement_type [string range $statement 0 5] 251*c5c4113dSnw141292 set tr_program_fixed $tr_program 252*c5c4113dSnw141292 if {$statement_type == "DELETE"} { 253*c5c4113dSnw141292 regsub -all new\.a $tr_program_fixed {''} tr_program_fixed 254*c5c4113dSnw141292 regsub -all new\.b $tr_program_fixed {''} tr_program_fixed 255*c5c4113dSnw141292 regsub -all new\.c $tr_program_fixed {''} tr_program_fixed 256*c5c4113dSnw141292 } 257*c5c4113dSnw141292 if {$statement_type == "INSERT"} { 258*c5c4113dSnw141292 regsub -all old\.a $tr_program_fixed {''} tr_program_fixed 259*c5c4113dSnw141292 regsub -all old\.b $tr_program_fixed {''} tr_program_fixed 260*c5c4113dSnw141292 regsub -all old\.c $tr_program_fixed {''} tr_program_fixed 261*c5c4113dSnw141292 } 262*c5c4113dSnw141292 263*c5c4113dSnw141292 264*c5c4113dSnw141292 set tr_program_cooked $tr_program 265*c5c4113dSnw141292 regsub -all new\.a $tr_program_cooked $newA tr_program_cooked 266*c5c4113dSnw141292 regsub -all new\.b $tr_program_cooked $newB tr_program_cooked 267*c5c4113dSnw141292 regsub -all new\.c $tr_program_cooked $newC tr_program_cooked 268*c5c4113dSnw141292 regsub -all old\.a $tr_program_cooked $oldA tr_program_cooked 269*c5c4113dSnw141292 regsub -all old\.b $tr_program_cooked $oldB tr_program_cooked 270*c5c4113dSnw141292 regsub -all old\.c $tr_program_cooked $oldC tr_program_cooked 271*c5c4113dSnw141292 272*c5c4113dSnw141292 catchsql { 273*c5c4113dSnw141292 DROP TABLE tbl; 274*c5c4113dSnw141292 DROP TABLE log; 275*c5c4113dSnw141292 } 276*c5c4113dSnw141292 277*c5c4113dSnw141292 execsql { 278*c5c4113dSnw141292 CREATE TABLE tbl(a PRIMARY KEY, b, c); 279*c5c4113dSnw141292 CREATE TABLE log(a, b, c); 280*c5c4113dSnw141292 } 281*c5c4113dSnw141292 282*c5c4113dSnw141292 set query {SELECT * FROM tbl; SELECT * FROM log;} 283*c5c4113dSnw141292 set prep "$prep; INSERT INTO log VALUES(1, 2, 3);\ 284*c5c4113dSnw141292 INSERT INTO log VALUES(10, 20, 30);" 285*c5c4113dSnw141292 286*c5c4113dSnw141292# Check execution of BEFORE programs: 287*c5c4113dSnw141292 288*c5c4113dSnw141292 set before_data [ execsql "$prep $tr_program_cooked $statement $query" ] 289*c5c4113dSnw141292 290*c5c4113dSnw141292 execsql "DELETE FROM tbl; DELETE FROM log; $prep"; 291*c5c4113dSnw141292 execsql "CREATE TRIGGER the_trigger BEFORE [string range $statement 0 6]\ 292*c5c4113dSnw141292 ON tbl BEGIN $tr_program_fixed END;" 293*c5c4113dSnw141292 294*c5c4113dSnw141292 do_test trigger2-2.$ii-before "execsql {$statement $query}" $before_data 295*c5c4113dSnw141292 296*c5c4113dSnw141292 execsql "DROP TRIGGER the_trigger;" 297*c5c4113dSnw141292 execsql "DELETE FROM tbl; DELETE FROM log;" 298*c5c4113dSnw141292 299*c5c4113dSnw141292# Check execution of AFTER programs 300*c5c4113dSnw141292 set after_data [ execsql "$prep $statement $tr_program_cooked $query" ] 301*c5c4113dSnw141292 302*c5c4113dSnw141292 execsql "DELETE FROM tbl; DELETE FROM log; $prep"; 303*c5c4113dSnw141292 execsql "CREATE TRIGGER the_trigger AFTER [string range $statement 0 6]\ 304*c5c4113dSnw141292 ON tbl BEGIN $tr_program_fixed END;" 305*c5c4113dSnw141292 306*c5c4113dSnw141292 do_test trigger2-2.$ii-after "execsql {$statement $query}" $after_data 307*c5c4113dSnw141292 execsql "DROP TRIGGER the_trigger;" 308*c5c4113dSnw141292 309*c5c4113dSnw141292 do_test trigger2-2.$ii-integrity { 310*c5c4113dSnw141292 execsql { 311*c5c4113dSnw141292 PRAGMA integrity_check; 312*c5c4113dSnw141292 } 313*c5c4113dSnw141292 } {ok} 314*c5c4113dSnw141292 315*c5c4113dSnw141292 } 316*c5c4113dSnw141292} 317*c5c4113dSnw141292catchsql { 318*c5c4113dSnw141292 DROP TABLE tbl; 319*c5c4113dSnw141292 DROP TABLE log; 320*c5c4113dSnw141292} 321*c5c4113dSnw141292 322*c5c4113dSnw141292# 3. 323*c5c4113dSnw141292 324*c5c4113dSnw141292# trigger2-3.1: UPDATE OF triggers 325*c5c4113dSnw141292execsql { 326*c5c4113dSnw141292 CREATE TABLE tbl (a, b, c, d); 327*c5c4113dSnw141292 CREATE TABLE log (a); 328*c5c4113dSnw141292 INSERT INTO log VALUES (0); 329*c5c4113dSnw141292 INSERT INTO tbl VALUES (0, 0, 0, 0); 330*c5c4113dSnw141292 INSERT INTO tbl VALUES (1, 0, 0, 0); 331*c5c4113dSnw141292 CREATE TRIGGER tbl_after_update_cd BEFORE UPDATE OF c, d ON tbl 332*c5c4113dSnw141292 BEGIN 333*c5c4113dSnw141292 UPDATE log SET a = a + 1; 334*c5c4113dSnw141292 END; 335*c5c4113dSnw141292} 336*c5c4113dSnw141292do_test trigger2-3.1 { 337*c5c4113dSnw141292 execsql { 338*c5c4113dSnw141292 UPDATE tbl SET b = 1, c = 10; -- 2 339*c5c4113dSnw141292 UPDATE tbl SET b = 10; -- 0 340*c5c4113dSnw141292 UPDATE tbl SET d = 4 WHERE a = 0; --1 341*c5c4113dSnw141292 UPDATE tbl SET a = 4, b = 10; --0 342*c5c4113dSnw141292 SELECT * FROM log; 343*c5c4113dSnw141292 } 344*c5c4113dSnw141292} {3} 345*c5c4113dSnw141292execsql { 346*c5c4113dSnw141292 DROP TABLE tbl; 347*c5c4113dSnw141292 DROP TABLE log; 348*c5c4113dSnw141292} 349*c5c4113dSnw141292 350*c5c4113dSnw141292# trigger2-3.2: WHEN clause 351*c5c4113dSnw141292set when_triggers [ list \ 352*c5c4113dSnw141292 {t1 BEFORE INSERT ON tbl WHEN new.a > 20} \ 353*c5c4113dSnw141292 {t2 BEFORE INSERT ON tbl WHEN (SELECT count(*) FROM tbl) = 0} ] 354*c5c4113dSnw141292 355*c5c4113dSnw141292execsql { 356*c5c4113dSnw141292 CREATE TABLE tbl (a, b, c, d); 357*c5c4113dSnw141292 CREATE TABLE log (a); 358*c5c4113dSnw141292 INSERT INTO log VALUES (0); 359*c5c4113dSnw141292} 360*c5c4113dSnw141292 361*c5c4113dSnw141292foreach trig $when_triggers { 362*c5c4113dSnw141292 execsql "CREATE TRIGGER $trig BEGIN UPDATE log set a = a + 1; END;" 363*c5c4113dSnw141292} 364*c5c4113dSnw141292 365*c5c4113dSnw141292do_test trigger2-3.2 { 366*c5c4113dSnw141292 execsql { 367*c5c4113dSnw141292 368*c5c4113dSnw141292 INSERT INTO tbl VALUES(0, 0, 0, 0); -- 1 369*c5c4113dSnw141292 SELECT * FROM log; 370*c5c4113dSnw141292 UPDATE log SET a = 0; 371*c5c4113dSnw141292 372*c5c4113dSnw141292 INSERT INTO tbl VALUES(0, 0, 0, 0); -- 0 373*c5c4113dSnw141292 SELECT * FROM log; 374*c5c4113dSnw141292 UPDATE log SET a = 0; 375*c5c4113dSnw141292 376*c5c4113dSnw141292 INSERT INTO tbl VALUES(200, 0, 0, 0); -- 1 377*c5c4113dSnw141292 SELECT * FROM log; 378*c5c4113dSnw141292 UPDATE log SET a = 0; 379*c5c4113dSnw141292 } 380*c5c4113dSnw141292} {1 0 1} 381*c5c4113dSnw141292execsql { 382*c5c4113dSnw141292 DROP TABLE tbl; 383*c5c4113dSnw141292 DROP TABLE log; 384*c5c4113dSnw141292} 385*c5c4113dSnw141292do_test trigger2-3.3 { 386*c5c4113dSnw141292 execsql { 387*c5c4113dSnw141292 PRAGMA integrity_check; 388*c5c4113dSnw141292 } 389*c5c4113dSnw141292} {ok} 390*c5c4113dSnw141292 391*c5c4113dSnw141292# Simple cascaded trigger 392*c5c4113dSnw141292execsql { 393*c5c4113dSnw141292 CREATE TABLE tblA(a, b); 394*c5c4113dSnw141292 CREATE TABLE tblB(a, b); 395*c5c4113dSnw141292 CREATE TABLE tblC(a, b); 396*c5c4113dSnw141292 397*c5c4113dSnw141292 CREATE TRIGGER tr1 BEFORE INSERT ON tblA BEGIN 398*c5c4113dSnw141292 INSERT INTO tblB values(new.a, new.b); 399*c5c4113dSnw141292 END; 400*c5c4113dSnw141292 401*c5c4113dSnw141292 CREATE TRIGGER tr2 BEFORE INSERT ON tblB BEGIN 402*c5c4113dSnw141292 INSERT INTO tblC values(new.a, new.b); 403*c5c4113dSnw141292 END; 404*c5c4113dSnw141292} 405*c5c4113dSnw141292do_test trigger2-4.1 { 406*c5c4113dSnw141292 execsql { 407*c5c4113dSnw141292 INSERT INTO tblA values(1, 2); 408*c5c4113dSnw141292 SELECT * FROM tblA; 409*c5c4113dSnw141292 SELECT * FROM tblB; 410*c5c4113dSnw141292 SELECT * FROM tblC; 411*c5c4113dSnw141292 } 412*c5c4113dSnw141292} {1 2 1 2 1 2} 413*c5c4113dSnw141292execsql { 414*c5c4113dSnw141292 DROP TABLE tblA; 415*c5c4113dSnw141292 DROP TABLE tblB; 416*c5c4113dSnw141292 DROP TABLE tblC; 417*c5c4113dSnw141292} 418*c5c4113dSnw141292 419*c5c4113dSnw141292# Simple recursive trigger 420*c5c4113dSnw141292execsql { 421*c5c4113dSnw141292 CREATE TABLE tbl(a, b, c); 422*c5c4113dSnw141292 CREATE TRIGGER tbl_trig BEFORE INSERT ON tbl 423*c5c4113dSnw141292 BEGIN 424*c5c4113dSnw141292 INSERT INTO tbl VALUES (new.a, new.b, new.c); 425*c5c4113dSnw141292 END; 426*c5c4113dSnw141292} 427*c5c4113dSnw141292do_test trigger2-4.2 { 428*c5c4113dSnw141292 execsql { 429*c5c4113dSnw141292 INSERT INTO tbl VALUES (1, 2, 3); 430*c5c4113dSnw141292 select * from tbl; 431*c5c4113dSnw141292 } 432*c5c4113dSnw141292} {1 2 3 1 2 3} 433*c5c4113dSnw141292execsql { 434*c5c4113dSnw141292 DROP TABLE tbl; 435*c5c4113dSnw141292} 436*c5c4113dSnw141292 437*c5c4113dSnw141292# 5. 438*c5c4113dSnw141292execsql { 439*c5c4113dSnw141292 CREATE TABLE tbl(a, b, c); 440*c5c4113dSnw141292 CREATE TRIGGER tbl_trig BEFORE INSERT ON tbl 441*c5c4113dSnw141292 BEGIN 442*c5c4113dSnw141292 INSERT INTO tbl VALUES (1, 2, 3); 443*c5c4113dSnw141292 INSERT INTO tbl VALUES (2, 2, 3); 444*c5c4113dSnw141292 UPDATE tbl set b = 10 WHERE a = 1; 445*c5c4113dSnw141292 DELETE FROM tbl WHERE a = 1; 446*c5c4113dSnw141292 DELETE FROM tbl; 447*c5c4113dSnw141292 END; 448*c5c4113dSnw141292} 449*c5c4113dSnw141292do_test trigger2-5 { 450*c5c4113dSnw141292 execsql { 451*c5c4113dSnw141292 INSERT INTO tbl VALUES(100, 200, 300); 452*c5c4113dSnw141292 } 453*c5c4113dSnw141292 db changes 454*c5c4113dSnw141292} {1} 455*c5c4113dSnw141292execsql { 456*c5c4113dSnw141292 DROP TABLE tbl; 457*c5c4113dSnw141292} 458*c5c4113dSnw141292 459*c5c4113dSnw141292# Handling of ON CONFLICT by INSERT statements inside triggers 460*c5c4113dSnw141292execsql { 461*c5c4113dSnw141292 CREATE TABLE tbl (a primary key, b, c); 462*c5c4113dSnw141292 CREATE TRIGGER ai_tbl AFTER INSERT ON tbl BEGIN 463*c5c4113dSnw141292 INSERT OR IGNORE INTO tbl values (new.a, 0, 0); 464*c5c4113dSnw141292 END; 465*c5c4113dSnw141292} 466*c5c4113dSnw141292do_test trigger2-6.1a { 467*c5c4113dSnw141292 execsql { 468*c5c4113dSnw141292 BEGIN; 469*c5c4113dSnw141292 INSERT INTO tbl values (1, 2, 3); 470*c5c4113dSnw141292 SELECT * from tbl; 471*c5c4113dSnw141292 } 472*c5c4113dSnw141292} {1 2 3} 473*c5c4113dSnw141292do_test trigger2-6.1b { 474*c5c4113dSnw141292 catchsql { 475*c5c4113dSnw141292 INSERT OR ABORT INTO tbl values (2, 2, 3); 476*c5c4113dSnw141292 } 477*c5c4113dSnw141292} {1 {column a is not unique}} 478*c5c4113dSnw141292do_test trigger2-6.1c { 479*c5c4113dSnw141292 execsql { 480*c5c4113dSnw141292 SELECT * from tbl; 481*c5c4113dSnw141292 } 482*c5c4113dSnw141292} {1 2 3} 483*c5c4113dSnw141292do_test trigger2-6.1d { 484*c5c4113dSnw141292 catchsql { 485*c5c4113dSnw141292 INSERT OR FAIL INTO tbl values (2, 2, 3); 486*c5c4113dSnw141292 } 487*c5c4113dSnw141292} {1 {column a is not unique}} 488*c5c4113dSnw141292do_test trigger2-6.1e { 489*c5c4113dSnw141292 execsql { 490*c5c4113dSnw141292 SELECT * from tbl; 491*c5c4113dSnw141292 } 492*c5c4113dSnw141292} {1 2 3 2 2 3} 493*c5c4113dSnw141292do_test trigger2-6.1f { 494*c5c4113dSnw141292 execsql { 495*c5c4113dSnw141292 INSERT OR REPLACE INTO tbl values (2, 2, 3); 496*c5c4113dSnw141292 SELECT * from tbl; 497*c5c4113dSnw141292 } 498*c5c4113dSnw141292} {1 2 3 2 0 0} 499*c5c4113dSnw141292do_test trigger2-6.1g { 500*c5c4113dSnw141292 catchsql { 501*c5c4113dSnw141292 INSERT OR ROLLBACK INTO tbl values (3, 2, 3); 502*c5c4113dSnw141292 } 503*c5c4113dSnw141292} {1 {column a is not unique}} 504*c5c4113dSnw141292do_test trigger2-6.1h { 505*c5c4113dSnw141292 execsql { 506*c5c4113dSnw141292 SELECT * from tbl; 507*c5c4113dSnw141292 } 508*c5c4113dSnw141292} {} 509*c5c4113dSnw141292execsql {DELETE FROM tbl} 510*c5c4113dSnw141292 511*c5c4113dSnw141292 512*c5c4113dSnw141292# Handling of ON CONFLICT by UPDATE statements inside triggers 513*c5c4113dSnw141292execsql { 514*c5c4113dSnw141292 INSERT INTO tbl values (4, 2, 3); 515*c5c4113dSnw141292 INSERT INTO tbl values (6, 3, 4); 516*c5c4113dSnw141292 CREATE TRIGGER au_tbl AFTER UPDATE ON tbl BEGIN 517*c5c4113dSnw141292 UPDATE OR IGNORE tbl SET a = new.a, c = 10; 518*c5c4113dSnw141292 END; 519*c5c4113dSnw141292} 520*c5c4113dSnw141292do_test trigger2-6.2a { 521*c5c4113dSnw141292 execsql { 522*c5c4113dSnw141292 BEGIN; 523*c5c4113dSnw141292 UPDATE tbl SET a = 1 WHERE a = 4; 524*c5c4113dSnw141292 SELECT * from tbl; 525*c5c4113dSnw141292 } 526*c5c4113dSnw141292} {1 2 10 6 3 4} 527*c5c4113dSnw141292do_test trigger2-6.2b { 528*c5c4113dSnw141292 catchsql { 529*c5c4113dSnw141292 UPDATE OR ABORT tbl SET a = 4 WHERE a = 1; 530*c5c4113dSnw141292 } 531*c5c4113dSnw141292} {1 {column a is not unique}} 532*c5c4113dSnw141292do_test trigger2-6.2c { 533*c5c4113dSnw141292 execsql { 534*c5c4113dSnw141292 SELECT * from tbl; 535*c5c4113dSnw141292 } 536*c5c4113dSnw141292} {1 2 10 6 3 4} 537*c5c4113dSnw141292do_test trigger2-6.2d { 538*c5c4113dSnw141292 catchsql { 539*c5c4113dSnw141292 UPDATE OR FAIL tbl SET a = 4 WHERE a = 1; 540*c5c4113dSnw141292 } 541*c5c4113dSnw141292} {1 {column a is not unique}} 542*c5c4113dSnw141292do_test trigger2-6.2e { 543*c5c4113dSnw141292 execsql { 544*c5c4113dSnw141292 SELECT * from tbl; 545*c5c4113dSnw141292 } 546*c5c4113dSnw141292} {4 2 10 6 3 4} 547*c5c4113dSnw141292do_test trigger2-6.2f.1 { 548*c5c4113dSnw141292 execsql { 549*c5c4113dSnw141292 UPDATE OR REPLACE tbl SET a = 1 WHERE a = 4; 550*c5c4113dSnw141292 SELECT * from tbl; 551*c5c4113dSnw141292 } 552*c5c4113dSnw141292} {1 3 10} 553*c5c4113dSnw141292do_test trigger2-6.2f.2 { 554*c5c4113dSnw141292 execsql { 555*c5c4113dSnw141292 INSERT INTO tbl VALUES (2, 3, 4); 556*c5c4113dSnw141292 SELECT * FROM tbl; 557*c5c4113dSnw141292 } 558*c5c4113dSnw141292} {1 3 10 2 3 4} 559*c5c4113dSnw141292do_test trigger2-6.2g { 560*c5c4113dSnw141292 catchsql { 561*c5c4113dSnw141292 UPDATE OR ROLLBACK tbl SET a = 4 WHERE a = 1; 562*c5c4113dSnw141292 } 563*c5c4113dSnw141292} {1 {column a is not unique}} 564*c5c4113dSnw141292do_test trigger2-6.2h { 565*c5c4113dSnw141292 execsql { 566*c5c4113dSnw141292 SELECT * from tbl; 567*c5c4113dSnw141292 } 568*c5c4113dSnw141292} {4 2 3 6 3 4} 569*c5c4113dSnw141292execsql { 570*c5c4113dSnw141292 DROP TABLE tbl; 571*c5c4113dSnw141292} 572*c5c4113dSnw141292 573*c5c4113dSnw141292# 7. Triggers on views 574*c5c4113dSnw141292do_test trigger2-7.1 { 575*c5c4113dSnw141292 execsql { 576*c5c4113dSnw141292 CREATE TABLE ab(a, b); 577*c5c4113dSnw141292 CREATE TABLE cd(c, d); 578*c5c4113dSnw141292 INSERT INTO ab VALUES (1, 2); 579*c5c4113dSnw141292 INSERT INTO ab VALUES (0, 0); 580*c5c4113dSnw141292 INSERT INTO cd VALUES (3, 4); 581*c5c4113dSnw141292 582*c5c4113dSnw141292 CREATE TABLE tlog(ii INTEGER PRIMARY KEY, 583*c5c4113dSnw141292 olda, oldb, oldc, oldd, newa, newb, newc, newd); 584*c5c4113dSnw141292 585*c5c4113dSnw141292 CREATE VIEW abcd AS SELECT a, b, c, d FROM ab, cd; 586*c5c4113dSnw141292 587*c5c4113dSnw141292 CREATE TRIGGER before_update INSTEAD OF UPDATE ON abcd BEGIN 588*c5c4113dSnw141292 INSERT INTO tlog VALUES(NULL, 589*c5c4113dSnw141292 old.a, old.b, old.c, old.d, new.a, new.b, new.c, new.d); 590*c5c4113dSnw141292 END; 591*c5c4113dSnw141292 CREATE TRIGGER after_update INSTEAD OF UPDATE ON abcd BEGIN 592*c5c4113dSnw141292 INSERT INTO tlog VALUES(NULL, 593*c5c4113dSnw141292 old.a, old.b, old.c, old.d, new.a, new.b, new.c, new.d); 594*c5c4113dSnw141292 END; 595*c5c4113dSnw141292 596*c5c4113dSnw141292 CREATE TRIGGER before_delete INSTEAD OF DELETE ON abcd BEGIN 597*c5c4113dSnw141292 INSERT INTO tlog VALUES(NULL, 598*c5c4113dSnw141292 old.a, old.b, old.c, old.d, 0, 0, 0, 0); 599*c5c4113dSnw141292 END; 600*c5c4113dSnw141292 CREATE TRIGGER after_delete INSTEAD OF DELETE ON abcd BEGIN 601*c5c4113dSnw141292 INSERT INTO tlog VALUES(NULL, 602*c5c4113dSnw141292 old.a, old.b, old.c, old.d, 0, 0, 0, 0); 603*c5c4113dSnw141292 END; 604*c5c4113dSnw141292 605*c5c4113dSnw141292 CREATE TRIGGER before_insert INSTEAD OF INSERT ON abcd BEGIN 606*c5c4113dSnw141292 INSERT INTO tlog VALUES(NULL, 607*c5c4113dSnw141292 0, 0, 0, 0, new.a, new.b, new.c, new.d); 608*c5c4113dSnw141292 END; 609*c5c4113dSnw141292 CREATE TRIGGER after_insert INSTEAD OF INSERT ON abcd BEGIN 610*c5c4113dSnw141292 INSERT INTO tlog VALUES(NULL, 611*c5c4113dSnw141292 0, 0, 0, 0, new.a, new.b, new.c, new.d); 612*c5c4113dSnw141292 END; 613*c5c4113dSnw141292 } 614*c5c4113dSnw141292} {}; 615*c5c4113dSnw141292 616*c5c4113dSnw141292do_test trigger2-7.2 { 617*c5c4113dSnw141292 execsql { 618*c5c4113dSnw141292 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1; 619*c5c4113dSnw141292 DELETE FROM abcd WHERE a = 1; 620*c5c4113dSnw141292 INSERT INTO abcd VALUES(10, 20, 30, 40); 621*c5c4113dSnw141292 SELECT * FROM tlog; 622*c5c4113dSnw141292 } 623*c5c4113dSnw141292} [ list 1 1 2 3 4 100 25 3 4 \ 624*c5c4113dSnw141292 2 1 2 3 4 100 25 3 4 \ 625*c5c4113dSnw141292 3 1 2 3 4 0 0 0 0 \ 626*c5c4113dSnw141292 4 1 2 3 4 0 0 0 0 \ 627*c5c4113dSnw141292 5 0 0 0 0 10 20 30 40 \ 628*c5c4113dSnw141292 6 0 0 0 0 10 20 30 40 ] 629*c5c4113dSnw141292 630*c5c4113dSnw141292do_test trigger2-7.3 { 631*c5c4113dSnw141292 execsql { 632*c5c4113dSnw141292 DELETE FROM tlog; 633*c5c4113dSnw141292 INSERT INTO abcd VALUES(10, 20, 30, 40); 634*c5c4113dSnw141292 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1; 635*c5c4113dSnw141292 DELETE FROM abcd WHERE a = 1; 636*c5c4113dSnw141292 SELECT * FROM tlog; 637*c5c4113dSnw141292 } 638*c5c4113dSnw141292} [ list \ 639*c5c4113dSnw141292 1 0 0 0 0 10 20 30 40 \ 640*c5c4113dSnw141292 2 0 0 0 0 10 20 30 40 \ 641*c5c4113dSnw141292 3 1 2 3 4 100 25 3 4 \ 642*c5c4113dSnw141292 4 1 2 3 4 100 25 3 4 \ 643*c5c4113dSnw141292 5 1 2 3 4 0 0 0 0 \ 644*c5c4113dSnw141292 6 1 2 3 4 0 0 0 0 \ 645*c5c4113dSnw141292] 646*c5c4113dSnw141292do_test trigger2-7.4 { 647*c5c4113dSnw141292 execsql { 648*c5c4113dSnw141292 DELETE FROM tlog; 649*c5c4113dSnw141292 DELETE FROM abcd WHERE a = 1; 650*c5c4113dSnw141292 INSERT INTO abcd VALUES(10, 20, 30, 40); 651*c5c4113dSnw141292 UPDATE abcd SET a = 100, b = 5*5 WHERE a = 1; 652*c5c4113dSnw141292 SELECT * FROM tlog; 653*c5c4113dSnw141292 } 654*c5c4113dSnw141292} [ list \ 655*c5c4113dSnw141292 1 1 2 3 4 0 0 0 0 \ 656*c5c4113dSnw141292 2 1 2 3 4 0 0 0 0 \ 657*c5c4113dSnw141292 3 0 0 0 0 10 20 30 40 \ 658*c5c4113dSnw141292 4 0 0 0 0 10 20 30 40 \ 659*c5c4113dSnw141292 5 1 2 3 4 100 25 3 4 \ 660*c5c4113dSnw141292 6 1 2 3 4 100 25 3 4 \ 661*c5c4113dSnw141292] 662*c5c4113dSnw141292 663*c5c4113dSnw141292do_test trigger2-8.1 { 664*c5c4113dSnw141292 execsql { 665*c5c4113dSnw141292 CREATE TABLE t1(a,b,c); 666*c5c4113dSnw141292 INSERT INTO t1 VALUES(1,2,3); 667*c5c4113dSnw141292 CREATE VIEW v1 AS 668*c5c4113dSnw141292 SELECT a+b AS x, b+c AS y, a+c AS z FROM t1; 669*c5c4113dSnw141292 SELECT * FROM v1; 670*c5c4113dSnw141292 } 671*c5c4113dSnw141292} {3 5 4} 672*c5c4113dSnw141292do_test trigger2-8.2 { 673*c5c4113dSnw141292 execsql { 674*c5c4113dSnw141292 CREATE TABLE v1log(a,b,c,d,e,f); 675*c5c4113dSnw141292 CREATE TRIGGER r1 INSTEAD OF DELETE ON v1 BEGIN 676*c5c4113dSnw141292 INSERT INTO v1log VALUES(OLD.x,NULL,OLD.y,NULL,OLD.z,NULL); 677*c5c4113dSnw141292 END; 678*c5c4113dSnw141292 DELETE FROM v1 WHERE x=1; 679*c5c4113dSnw141292 SELECT * FROM v1log; 680*c5c4113dSnw141292 } 681*c5c4113dSnw141292} {} 682*c5c4113dSnw141292do_test trigger2-8.3 { 683*c5c4113dSnw141292 execsql { 684*c5c4113dSnw141292 DELETE FROM v1 WHERE x=3; 685*c5c4113dSnw141292 SELECT * FROM v1log; 686*c5c4113dSnw141292 } 687*c5c4113dSnw141292} {3 {} 5 {} 4 {}} 688*c5c4113dSnw141292do_test trigger2-8.4 { 689*c5c4113dSnw141292 execsql { 690*c5c4113dSnw141292 INSERT INTO t1 VALUES(4,5,6); 691*c5c4113dSnw141292 DELETE FROM v1log; 692*c5c4113dSnw141292 DELETE FROM v1 WHERE y=11; 693*c5c4113dSnw141292 SELECT * FROM v1log; 694*c5c4113dSnw141292 } 695*c5c4113dSnw141292} {9 {} 11 {} 10 {}} 696*c5c4113dSnw141292do_test trigger2-8.5 { 697*c5c4113dSnw141292 execsql { 698*c5c4113dSnw141292 CREATE TRIGGER r2 INSTEAD OF INSERT ON v1 BEGIN 699*c5c4113dSnw141292 INSERT INTO v1log VALUES(NULL,NEW.x,NULL,NEW.y,NULL,NEW.z); 700*c5c4113dSnw141292 END; 701*c5c4113dSnw141292 DELETE FROM v1log; 702*c5c4113dSnw141292 INSERT INTO v1 VALUES(1,2,3); 703*c5c4113dSnw141292 SELECT * FROM v1log; 704*c5c4113dSnw141292 } 705*c5c4113dSnw141292} {{} 1 {} 2 {} 3} 706*c5c4113dSnw141292do_test trigger2-8.6 { 707*c5c4113dSnw141292 execsql { 708*c5c4113dSnw141292 CREATE TRIGGER r3 INSTEAD OF UPDATE ON v1 BEGIN 709*c5c4113dSnw141292 INSERT INTO v1log VALUES(OLD.x,NEW.x,OLD.y,NEW.y,OLD.z,NEW.z); 710*c5c4113dSnw141292 END; 711*c5c4113dSnw141292 DELETE FROM v1log; 712*c5c4113dSnw141292 UPDATE v1 SET x=x+100, y=y+200, z=z+300; 713*c5c4113dSnw141292 SELECT * FROM v1log; 714*c5c4113dSnw141292 } 715*c5c4113dSnw141292} {3 103 5 205 4 304 9 109 11 211 10 310} 716*c5c4113dSnw141292 717*c5c4113dSnw141292do_test trigger2-9.9 { 718*c5c4113dSnw141292 execsql {PRAGMA integrity_check} 719*c5c4113dSnw141292} {ok} 720*c5c4113dSnw141292 721*c5c4113dSnw141292finish_test 722