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 verify_ex_errcode trigger3-1.1b SQLITE_CONSTRAINT_TRIGGER
49 do_test trigger3-1.2 {
55 do_test trigger3-1.3 {
56 execsql {SELECT * FROM tbl}
60 do_test trigger3-2.1 {
63 INSERT INTO tbl VALUES (5, 5, 6);
64 INSERT INTO tbl VALUES (2, 5, 6);
67 verify_ex_errcode trigger3-2.1b SQLITE_CONSTRAINT_TRIGGER
68 do_test trigger3-2.2 {
75 do_test trigger3-3.1 {
78 INSERT INTO tbl VALUES (5, 5, 6);
79 INSERT INTO tbl VALUES (3, 5, 6);
81 } {1 {Trigger rollback}}
82 verify_ex_errcode trigger3-3.1b SQLITE_CONSTRAINT_TRIGGER
83 do_test trigger3-3.2 {
89 # Verify that a ROLLBACK trigger works like a FAIL trigger if
90 # we are not within a transaction. Ticket #3035.
92 do_test trigger3-3.3 {
95 INSERT INTO tbl VALUES (3, 9, 10);
97 } {1 {Trigger rollback}}
98 verify_ex_errcode trigger3-3.3b SQLITE_CONSTRAINT_TRIGGER
99 do_test trigger3-3.4 {
100 execsql {SELECT * FROM tbl}
104 do_test trigger3-4.1 {
107 INSERT INTO tbl VALUES (5, 5, 6);
108 INSERT INTO tbl VALUES (4, 5, 6);
111 do_test trigger3-4.2 {
118 # Check that we can also do RAISE(IGNORE) for UPDATE and DELETE
119 execsql {DROP TABLE tbl;}
120 execsql {CREATE TABLE tbl (a, b, c);}
121 execsql {INSERT INTO tbl VALUES(1, 2, 3);}
122 execsql {INSERT INTO tbl VALUES(4, 5, 6);}
124 CREATE TRIGGER before_tbl_update BEFORE UPDATE ON tbl BEGIN
125 SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
128 CREATE TRIGGER before_tbl_delete BEFORE DELETE ON tbl BEGIN
129 SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
132 do_test trigger3-5.1 {
134 UPDATE tbl SET c = 10;
138 do_test trigger3-5.2 {
145 # Check that RAISE(IGNORE) works correctly for nested triggers:
146 execsql {CREATE TABLE tbl2(a, b, c)}
148 CREATE TRIGGER after_tbl2_insert AFTER INSERT ON tbl2 BEGIN
149 UPDATE tbl SET c = 10;
150 INSERT INTO tbl2 VALUES (new.a, new.b, new.c);
155 INSERT INTO tbl2 VALUES (1, 2, 3);
159 } {1 2 3 1 2 3 1 2 3}
161 # Check that things also work for view-triggers
165 execsql {CREATE VIEW tbl_view AS SELECT * FROM tbl}
167 CREATE TRIGGER tbl_view_insert INSTEAD OF INSERT ON tbl_view BEGIN
168 SELECT CASE WHEN (new.a = 1) THEN RAISE(ROLLBACK, 'View rollback')
169 WHEN (new.a = 2) THEN RAISE(IGNORE)
170 WHEN (new.a = 3) THEN RAISE(ABORT, 'View abort') END;
174 do_test trigger3-7.1 {
176 INSERT INTO tbl_view VALUES(1, 2, 3);
178 } {1 {View rollback}}
179 verify_ex_errcode trigger3-7.1b SQLITE_CONSTRAINT_TRIGGER
180 do_test trigger3-7.2 {
182 INSERT INTO tbl_view VALUES(2, 2, 3);
185 do_test trigger3-7.3 {
187 INSERT INTO tbl_view VALUES(3, 2, 3);
190 verify_ex_errcode trigger3-7.3b SQLITE_CONSTRAINT_TRIGGER
194 integrity_check trigger3-8.1
196 catchsql { DROP TABLE tbl; }
197 catchsql { DROP TABLE tbl2; }
198 catchsql { DROP VIEW tbl_view; }