2 #pragma ident "%Z%%M% %I% %E% SMI"
4 # The author disclaims copyright to this source code. In place of
5 # a legal notice, here is a blessing:
7 # May you do good and not evil.
8 # May you find forgiveness for yourself and forgive others.
9 # May you share freely, never taking more than you give.
11 #***********************************************************************
13 # This file tests the RAISE() function.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Test that we can cause ROLLBACK, FAIL and ABORT correctly
20 # catchsql { DROP TABLE tbl; }
21 catchsql { CREATE TABLE tbl (a, b, c) }
24 CREATE TRIGGER before_tbl_insert BEFORE INSERT ON tbl BEGIN SELECT CASE
25 WHEN (new.a = 4) THEN RAISE(IGNORE) END;
28 CREATE TRIGGER after_tbl_insert AFTER INSERT ON tbl BEGIN SELECT CASE
29 WHEN (new.a = 1) THEN RAISE(ABORT, 'Trigger abort')
30 WHEN (new.a = 2) THEN RAISE(FAIL, 'Trigger fail')
31 WHEN (new.a = 3) THEN RAISE(ROLLBACK, 'Trigger rollback') END;
35 do_test trigger3-1.1 {
38 INSERT INTO tbl VALUES (5, 5, 6);
39 INSERT INTO tbl VALUES (1, 5, 6);
42 do_test trigger3-1.2 {
48 do_test trigger3-1.3 {
49 execsql {SELECT * FROM tbl}
53 do_test trigger3-2.1 {
56 INSERT INTO tbl VALUES (5, 5, 6);
57 INSERT INTO tbl VALUES (2, 5, 6);
60 do_test trigger3-2.2 {
67 do_test trigger3-3.1 {
70 INSERT INTO tbl VALUES (5, 5, 6);
71 INSERT INTO tbl VALUES (3, 5, 6);
73 } {1 {Trigger rollback}}
74 do_test trigger3-3.2 {
80 do_test trigger3-4.1 {
83 INSERT INTO tbl VALUES (5, 5, 6);
84 INSERT INTO tbl VALUES (4, 5, 6);
87 do_test trigger3-4.2 {
94 # Check that we can also do RAISE(IGNORE) for UPDATE and DELETE
95 execsql {DROP TABLE tbl;}
96 execsql {CREATE TABLE tbl (a, b, c);}
97 execsql {INSERT INTO tbl VALUES(1, 2, 3);}
98 execsql {INSERT INTO tbl VALUES(4, 5, 6);}
100 CREATE TRIGGER before_tbl_update BEFORE UPDATE ON tbl BEGIN
101 SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
104 CREATE TRIGGER before_tbl_delete BEFORE DELETE ON tbl BEGIN
105 SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
108 do_test trigger3-5.1 {
110 UPDATE tbl SET c = 10;
114 do_test trigger3-5.2 {
121 # Check that RAISE(IGNORE) works correctly for nested triggers:
122 execsql {CREATE TABLE tbl2(a, b, c)}
124 CREATE TRIGGER after_tbl2_insert AFTER INSERT ON tbl2 BEGIN
125 UPDATE tbl SET c = 10;
126 INSERT INTO tbl2 VALUES (new.a, new.b, new.c);
131 INSERT INTO tbl2 VALUES (1, 2, 3);
135 } {1 2 3 1 2 3 1 2 3}
137 # Check that things also work for view-triggers
138 execsql {CREATE VIEW tbl_view AS SELECT * FROM tbl}
140 CREATE TRIGGER tbl_view_insert INSTEAD OF INSERT ON tbl_view BEGIN
141 SELECT CASE WHEN (new.a = 1) THEN RAISE(ROLLBACK, 'View rollback')
142 WHEN (new.a = 2) THEN RAISE(IGNORE)
143 WHEN (new.a = 3) THEN RAISE(ABORT, 'View abort') END;
147 do_test trigger3-7.1 {
149 INSERT INTO tbl_view VALUES(1, 2, 3);
151 } {1 {View rollback}}
152 do_test trigger3-7.2 {
154 INSERT INTO tbl_view VALUES(2, 2, 3);
157 do_test trigger3-7.3 {
159 INSERT INTO tbl_view VALUES(3, 2, 3);
163 integrity_check trigger3-8.1
165 catchsql { DROP TABLE tbl; }
166 catchsql { DROP TABLE tbl2; }
167 catchsql { DROP VIEW tbl_view; }