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