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 # This file tests the RAISE() function.
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 ifcapable {!trigger} {
21 # The tests in this file were written before SQLite supported recursive }
22 # trigger invocation, and some tests depend on that to pass. So disable
23 # recursive triggers for this file.
24 catchsql { pragma recursive_triggers = off }
26 # Test that we can cause ROLLBACK, FAIL and ABORT correctly
28 catchsql { CREATE TABLE tbl(a, b ,c) }
30 CREATE TRIGGER before_tbl_insert BEFORE INSERT ON tbl BEGIN SELECT CASE
31 WHEN (new.a = 4) THEN RAISE(IGNORE) END;
34 CREATE TRIGGER after_tbl_insert AFTER INSERT ON tbl BEGIN SELECT CASE
35 WHEN (new.a = 1) THEN RAISE(ABORT, 'Trigger abort')
36 WHEN (new.a = 2) THEN RAISE(FAIL, 'Trigger fail')
37 WHEN (new.a = 3) THEN RAISE(ROLLBACK, 'Trigger rollback') END;
41 do_test trigger3-1.1 {
44 INSERT INTO tbl VALUES (5, 5, 6);
45 INSERT INTO tbl VALUES (1, 5, 6);
48 do_test trigger3-1.2 {
54 do_test trigger3-1.3 {
55 execsql {SELECT * FROM tbl}
59 do_test trigger3-2.1 {
62 INSERT INTO tbl VALUES (5, 5, 6);
63 INSERT INTO tbl VALUES (2, 5, 6);
66 do_test trigger3-2.2 {
73 do_test trigger3-3.1 {
76 INSERT INTO tbl VALUES (5, 5, 6);
77 INSERT INTO tbl VALUES (3, 5, 6);
79 } {1 {Trigger rollback}}
80 do_test trigger3-3.2 {
86 # Verify that a ROLLBACK trigger works like a FAIL trigger if
87 # we are not within a transaction. Ticket #3035.
89 do_test trigger3-3.3 {
92 INSERT INTO tbl VALUES (3, 9, 10);
94 } {1 {Trigger rollback}}
95 do_test trigger3-3.4 {
96 execsql {SELECT * FROM tbl}
100 do_test trigger3-4.1 {
103 INSERT INTO tbl VALUES (5, 5, 6);
104 INSERT INTO tbl VALUES (4, 5, 6);
107 do_test trigger3-4.2 {
114 # Check that we can also do RAISE(IGNORE) for UPDATE and DELETE
115 execsql {DROP TABLE tbl;}
116 execsql {CREATE TABLE tbl (a, b, c);}
117 execsql {INSERT INTO tbl VALUES(1, 2, 3);}
118 execsql {INSERT INTO tbl VALUES(4, 5, 6);}
120 CREATE TRIGGER before_tbl_update BEFORE UPDATE ON tbl BEGIN
121 SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
124 CREATE TRIGGER before_tbl_delete BEFORE DELETE ON tbl BEGIN
125 SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
128 do_test trigger3-5.1 {
130 UPDATE tbl SET c = 10;
134 do_test trigger3-5.2 {
141 # Check that RAISE(IGNORE) works correctly for nested triggers:
142 execsql {CREATE TABLE tbl2(a, b, c)}
144 CREATE TRIGGER after_tbl2_insert AFTER INSERT ON tbl2 BEGIN
145 UPDATE tbl SET c = 10;
146 INSERT INTO tbl2 VALUES (new.a, new.b, new.c);
151 INSERT INTO tbl2 VALUES (1, 2, 3);
155 } {1 2 3 1 2 3 1 2 3}
157 # Check that things also work for view-triggers
161 execsql {CREATE VIEW tbl_view AS SELECT * FROM tbl}
163 CREATE TRIGGER tbl_view_insert INSTEAD OF INSERT ON tbl_view BEGIN
164 SELECT CASE WHEN (new.a = 1) THEN RAISE(ROLLBACK, 'View rollback')
165 WHEN (new.a = 2) THEN RAISE(IGNORE)
166 WHEN (new.a = 3) THEN RAISE(ABORT, 'View abort') END;
170 do_test trigger3-7.1 {
172 INSERT INTO tbl_view VALUES(1, 2, 3);
174 } {1 {View rollback}}
175 do_test trigger3-7.2 {
177 INSERT INTO tbl_view VALUES(2, 2, 3);
180 do_test trigger3-7.3 {
182 INSERT INTO tbl_view VALUES(3, 2, 3);
188 integrity_check trigger3-8.1
190 catchsql { DROP TABLE tbl; }
191 catchsql { DROP TABLE tbl2; }
192 catchsql { DROP VIEW tbl_view; }