First public contribution.
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 # Test that we can cause ROLLBACK, FAIL and ABORT correctly
22 # catchsql { DROP TABLE tbl; }
23 catchsql { CREATE TABLE tbl (a, b, c) }
26 CREATE TRIGGER before_tbl_insert BEFORE INSERT ON tbl BEGIN SELECT CASE
27 WHEN (new.a = 4) THEN RAISE(IGNORE) END;
30 CREATE TRIGGER after_tbl_insert AFTER INSERT ON tbl BEGIN SELECT CASE
31 WHEN (new.a = 1) THEN RAISE(ABORT, 'Trigger abort')
32 WHEN (new.a = 2) THEN RAISE(FAIL, 'Trigger fail')
33 WHEN (new.a = 3) THEN RAISE(ROLLBACK, 'Trigger rollback') END;
37 do_test trigger3-1.1 {
40 INSERT INTO tbl VALUES (5, 5, 6);
41 INSERT INTO tbl VALUES (1, 5, 6);
44 do_test trigger3-1.2 {
50 do_test trigger3-1.3 {
51 execsql {SELECT * FROM tbl}
55 do_test trigger3-2.1 {
58 INSERT INTO tbl VALUES (5, 5, 6);
59 INSERT INTO tbl VALUES (2, 5, 6);
62 do_test trigger3-2.2 {
69 do_test trigger3-3.1 {
72 INSERT INTO tbl VALUES (5, 5, 6);
73 INSERT INTO tbl VALUES (3, 5, 6);
75 } {1 {Trigger rollback}}
76 do_test trigger3-3.2 {
82 # Verify that a ROLLBACK trigger works like a FAIL trigger if
83 # we are not within a transaction. Ticket #3035.
85 do_test trigger3-3.3 {
88 INSERT INTO tbl VALUES (3, 9, 10);
90 } {1 {Trigger rollback}}
91 do_test trigger3-3.4 {
92 execsql {SELECT * FROM tbl}
96 do_test trigger3-4.1 {
99 INSERT INTO tbl VALUES (5, 5, 6);
100 INSERT INTO tbl VALUES (4, 5, 6);
103 do_test trigger3-4.2 {
110 # Check that we can also do RAISE(IGNORE) for UPDATE and DELETE
111 execsql {DROP TABLE tbl;}
112 execsql {CREATE TABLE tbl (a, b, c);}
113 execsql {INSERT INTO tbl VALUES(1, 2, 3);}
114 execsql {INSERT INTO tbl VALUES(4, 5, 6);}
116 CREATE TRIGGER before_tbl_update BEFORE UPDATE ON tbl BEGIN
117 SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
120 CREATE TRIGGER before_tbl_delete BEFORE DELETE ON tbl BEGIN
121 SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
124 do_test trigger3-5.1 {
126 UPDATE tbl SET c = 10;
130 do_test trigger3-5.2 {
137 # Check that RAISE(IGNORE) works correctly for nested triggers:
138 execsql {CREATE TABLE tbl2(a, b, c)}
140 CREATE TRIGGER after_tbl2_insert AFTER INSERT ON tbl2 BEGIN
141 UPDATE tbl SET c = 10;
142 INSERT INTO tbl2 VALUES (new.a, new.b, new.c);
147 INSERT INTO tbl2 VALUES (1, 2, 3);
151 } {1 2 3 1 2 3 1 2 3}
153 # Check that things also work for view-triggers
157 execsql {CREATE VIEW tbl_view AS SELECT * FROM tbl}
159 CREATE TRIGGER tbl_view_insert INSTEAD OF INSERT ON tbl_view BEGIN
160 SELECT CASE WHEN (new.a = 1) THEN RAISE(ROLLBACK, 'View rollback')
161 WHEN (new.a = 2) THEN RAISE(IGNORE)
162 WHEN (new.a = 3) THEN RAISE(ABORT, 'View abort') END;
166 do_test trigger3-7.1 {
168 INSERT INTO tbl_view VALUES(1, 2, 3);
170 } {1 {View rollback}}
171 do_test trigger3-7.2 {
173 INSERT INTO tbl_view VALUES(2, 2, 3);
176 do_test trigger3-7.3 {
178 INSERT INTO tbl_view VALUES(3, 2, 3);
184 integrity_check trigger3-8.1
186 catchsql { DROP TABLE tbl; }
187 catchsql { DROP TABLE tbl2; }
188 catchsql { DROP VIEW tbl_view; }