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# This file tests the RAISE() function. 14*c5c4113dSnw141292# 15*c5c4113dSnw141292 16*c5c4113dSnw141292set testdir [file dirname $argv0] 17*c5c4113dSnw141292source $testdir/tester.tcl 18*c5c4113dSnw141292 19*c5c4113dSnw141292# Test that we can cause ROLLBACK, FAIL and ABORT correctly 20*c5c4113dSnw141292# catchsql { DROP TABLE tbl; } 21*c5c4113dSnw141292catchsql { CREATE TABLE tbl (a, b, c) } 22*c5c4113dSnw141292 23*c5c4113dSnw141292execsql { 24*c5c4113dSnw141292 CREATE TRIGGER before_tbl_insert BEFORE INSERT ON tbl BEGIN SELECT CASE 25*c5c4113dSnw141292 WHEN (new.a = 4) THEN RAISE(IGNORE) END; 26*c5c4113dSnw141292 END; 27*c5c4113dSnw141292 28*c5c4113dSnw141292 CREATE TRIGGER after_tbl_insert AFTER INSERT ON tbl BEGIN SELECT CASE 29*c5c4113dSnw141292 WHEN (new.a = 1) THEN RAISE(ABORT, 'Trigger abort') 30*c5c4113dSnw141292 WHEN (new.a = 2) THEN RAISE(FAIL, 'Trigger fail') 31*c5c4113dSnw141292 WHEN (new.a = 3) THEN RAISE(ROLLBACK, 'Trigger rollback') END; 32*c5c4113dSnw141292 END; 33*c5c4113dSnw141292} 34*c5c4113dSnw141292# ABORT 35*c5c4113dSnw141292do_test trigger3-1.1 { 36*c5c4113dSnw141292 catchsql { 37*c5c4113dSnw141292 BEGIN; 38*c5c4113dSnw141292 INSERT INTO tbl VALUES (5, 5, 6); 39*c5c4113dSnw141292 INSERT INTO tbl VALUES (1, 5, 6); 40*c5c4113dSnw141292 } 41*c5c4113dSnw141292} {1 {Trigger abort}} 42*c5c4113dSnw141292do_test trigger3-1.2 { 43*c5c4113dSnw141292 execsql { 44*c5c4113dSnw141292 SELECT * FROM tbl; 45*c5c4113dSnw141292 ROLLBACK; 46*c5c4113dSnw141292 } 47*c5c4113dSnw141292} {5 5 6} 48*c5c4113dSnw141292do_test trigger3-1.3 { 49*c5c4113dSnw141292 execsql {SELECT * FROM tbl} 50*c5c4113dSnw141292} {} 51*c5c4113dSnw141292 52*c5c4113dSnw141292# FAIL 53*c5c4113dSnw141292do_test trigger3-2.1 { 54*c5c4113dSnw141292 catchsql { 55*c5c4113dSnw141292 BEGIN; 56*c5c4113dSnw141292 INSERT INTO tbl VALUES (5, 5, 6); 57*c5c4113dSnw141292 INSERT INTO tbl VALUES (2, 5, 6); 58*c5c4113dSnw141292 } 59*c5c4113dSnw141292} {1 {Trigger fail}} 60*c5c4113dSnw141292do_test trigger3-2.2 { 61*c5c4113dSnw141292 execsql { 62*c5c4113dSnw141292 SELECT * FROM tbl; 63*c5c4113dSnw141292 ROLLBACK; 64*c5c4113dSnw141292 } 65*c5c4113dSnw141292} {5 5 6 2 5 6} 66*c5c4113dSnw141292# ROLLBACK 67*c5c4113dSnw141292do_test trigger3-3.1 { 68*c5c4113dSnw141292 catchsql { 69*c5c4113dSnw141292 BEGIN; 70*c5c4113dSnw141292 INSERT INTO tbl VALUES (5, 5, 6); 71*c5c4113dSnw141292 INSERT INTO tbl VALUES (3, 5, 6); 72*c5c4113dSnw141292 } 73*c5c4113dSnw141292} {1 {Trigger rollback}} 74*c5c4113dSnw141292do_test trigger3-3.2 { 75*c5c4113dSnw141292 execsql { 76*c5c4113dSnw141292 SELECT * FROM tbl; 77*c5c4113dSnw141292 } 78*c5c4113dSnw141292} {} 79*c5c4113dSnw141292# IGNORE 80*c5c4113dSnw141292do_test trigger3-4.1 { 81*c5c4113dSnw141292 catchsql { 82*c5c4113dSnw141292 BEGIN; 83*c5c4113dSnw141292 INSERT INTO tbl VALUES (5, 5, 6); 84*c5c4113dSnw141292 INSERT INTO tbl VALUES (4, 5, 6); 85*c5c4113dSnw141292 } 86*c5c4113dSnw141292} {0 {}} 87*c5c4113dSnw141292do_test trigger3-4.2 { 88*c5c4113dSnw141292 execsql { 89*c5c4113dSnw141292 SELECT * FROM tbl; 90*c5c4113dSnw141292 ROLLBACK; 91*c5c4113dSnw141292 } 92*c5c4113dSnw141292} {5 5 6} 93*c5c4113dSnw141292 94*c5c4113dSnw141292# Check that we can also do RAISE(IGNORE) for UPDATE and DELETE 95*c5c4113dSnw141292execsql {DROP TABLE tbl;} 96*c5c4113dSnw141292execsql {CREATE TABLE tbl (a, b, c);} 97*c5c4113dSnw141292execsql {INSERT INTO tbl VALUES(1, 2, 3);} 98*c5c4113dSnw141292execsql {INSERT INTO tbl VALUES(4, 5, 6);} 99*c5c4113dSnw141292execsql { 100*c5c4113dSnw141292 CREATE TRIGGER before_tbl_update BEFORE UPDATE ON tbl BEGIN 101*c5c4113dSnw141292 SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END; 102*c5c4113dSnw141292 END; 103*c5c4113dSnw141292 104*c5c4113dSnw141292 CREATE TRIGGER before_tbl_delete BEFORE DELETE ON tbl BEGIN 105*c5c4113dSnw141292 SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END; 106*c5c4113dSnw141292 END; 107*c5c4113dSnw141292} 108*c5c4113dSnw141292do_test trigger3-5.1 { 109*c5c4113dSnw141292 execsql { 110*c5c4113dSnw141292 UPDATE tbl SET c = 10; 111*c5c4113dSnw141292 SELECT * FROM tbl; 112*c5c4113dSnw141292 } 113*c5c4113dSnw141292} {1 2 3 4 5 10} 114*c5c4113dSnw141292do_test trigger3-5.2 { 115*c5c4113dSnw141292 execsql { 116*c5c4113dSnw141292 DELETE FROM tbl; 117*c5c4113dSnw141292 SELECT * FROM tbl; 118*c5c4113dSnw141292 } 119*c5c4113dSnw141292} {1 2 3} 120*c5c4113dSnw141292 121*c5c4113dSnw141292# Check that RAISE(IGNORE) works correctly for nested triggers: 122*c5c4113dSnw141292execsql {CREATE TABLE tbl2(a, b, c)} 123*c5c4113dSnw141292execsql { 124*c5c4113dSnw141292 CREATE TRIGGER after_tbl2_insert AFTER INSERT ON tbl2 BEGIN 125*c5c4113dSnw141292 UPDATE tbl SET c = 10; 126*c5c4113dSnw141292 INSERT INTO tbl2 VALUES (new.a, new.b, new.c); 127*c5c4113dSnw141292 END; 128*c5c4113dSnw141292} 129*c5c4113dSnw141292do_test trigger3-6 { 130*c5c4113dSnw141292 execsql { 131*c5c4113dSnw141292 INSERT INTO tbl2 VALUES (1, 2, 3); 132*c5c4113dSnw141292 SELECT * FROM tbl2; 133*c5c4113dSnw141292 SELECT * FROM tbl; 134*c5c4113dSnw141292 } 135*c5c4113dSnw141292} {1 2 3 1 2 3 1 2 3} 136*c5c4113dSnw141292 137*c5c4113dSnw141292# Check that things also work for view-triggers 138*c5c4113dSnw141292execsql {CREATE VIEW tbl_view AS SELECT * FROM tbl} 139*c5c4113dSnw141292execsql { 140*c5c4113dSnw141292 CREATE TRIGGER tbl_view_insert INSTEAD OF INSERT ON tbl_view BEGIN 141*c5c4113dSnw141292 SELECT CASE WHEN (new.a = 1) THEN RAISE(ROLLBACK, 'View rollback') 142*c5c4113dSnw141292 WHEN (new.a = 2) THEN RAISE(IGNORE) 143*c5c4113dSnw141292 WHEN (new.a = 3) THEN RAISE(ABORT, 'View abort') END; 144*c5c4113dSnw141292 END; 145*c5c4113dSnw141292} 146*c5c4113dSnw141292 147*c5c4113dSnw141292do_test trigger3-7.1 { 148*c5c4113dSnw141292 catchsql { 149*c5c4113dSnw141292 INSERT INTO tbl_view VALUES(1, 2, 3); 150*c5c4113dSnw141292 } 151*c5c4113dSnw141292} {1 {View rollback}} 152*c5c4113dSnw141292do_test trigger3-7.2 { 153*c5c4113dSnw141292 catchsql { 154*c5c4113dSnw141292 INSERT INTO tbl_view VALUES(2, 2, 3); 155*c5c4113dSnw141292 } 156*c5c4113dSnw141292} {0 {}} 157*c5c4113dSnw141292do_test trigger3-7.3 { 158*c5c4113dSnw141292 catchsql { 159*c5c4113dSnw141292 INSERT INTO tbl_view VALUES(3, 2, 3); 160*c5c4113dSnw141292 } 161*c5c4113dSnw141292} {1 {View abort}} 162*c5c4113dSnw141292 163*c5c4113dSnw141292integrity_check trigger3-8.1 164*c5c4113dSnw141292 165*c5c4113dSnw141292catchsql { DROP TABLE tbl; } 166*c5c4113dSnw141292catchsql { DROP TABLE tbl2; } 167*c5c4113dSnw141292catchsql { DROP VIEW tbl_view; } 168*c5c4113dSnw141292 169*c5c4113dSnw141292finish_test 170