3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. Specifically,
12 # it tests some compiler optimizations for SQL statements featuring
18 # trigger9-1.* - Test that if there are no references to OLD.* cols, or a
19 # reference to only OLD.rowid, the data is not loaded.
21 # trigger9-2.* - Test that for NEW.* records populated by UPDATE
22 # statements, unused fields are populated with NULL values.
24 # trigger9-3.* - Test that the temporary tables used for OLD.* references
25 # in "INSTEAD OF" triggers have NULL values in unused
29 set testdir [file dirname $argv0]
30 source $testdir/tester.tcl
31 ifcapable {!trigger} {
36 proc has_rowdata {sql} {
37 expr {[lsearch [execsql "explain $sql"] RowData]>=0}
40 do_test trigger9-1.1 {
42 PRAGMA page_size = 1024;
43 CREATE TABLE t1(x, y, z);
44 INSERT INTO t1 VALUES('1', randstr(10000,10000), '2');
45 INSERT INTO t1 VALUES('2', randstr(10000,10000), '4');
46 INSERT INTO t1 VALUES('3', randstr(10000,10000), '6');
51 do_test trigger9-1.2.1 {
54 CREATE TRIGGER trig1 BEFORE DELETE ON t1 BEGIN
55 INSERT INTO t2 VALUES(old.rowid);
61 do_test trigger9-1.2.3 {
62 has_rowdata {DELETE FROM t1}
64 do_test trigger9-1.2.4 { execsql { ROLLBACK } } {}
66 do_test trigger9-1.3.1 {
69 CREATE TRIGGER trig1 BEFORE DELETE ON t1 BEGIN
70 INSERT INTO t2 VALUES(old.x);
76 do_test trigger9-1.3.2 {
77 has_rowdata {DELETE FROM t1}
79 do_test trigger9-1.3.3 { execsql { ROLLBACK } } {}
81 do_test trigger9-1.4.1 {
84 CREATE TRIGGER trig1 BEFORE DELETE ON t1 WHEN old.x='1' BEGIN
85 INSERT INTO t2 VALUES(old.rowid);
91 do_test trigger9-1.4.2 {
92 has_rowdata {DELETE FROM t1}
94 do_test trigger9-1.4.3 { execsql { ROLLBACK } } {}
96 do_test trigger9-1.5.1 {
99 CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
100 INSERT INTO t2 VALUES(old.rowid);
102 UPDATE t1 SET y = '';
106 do_test trigger9-1.5.2 {
107 has_rowdata {UPDATE t1 SET y = ''}
109 do_test trigger9-1.5.3 { execsql { ROLLBACK } } {}
111 do_test trigger9-1.6.1 {
114 CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
115 INSERT INTO t2 VALUES(old.x);
117 UPDATE t1 SET y = '';
121 do_test trigger9-1.6.2 {
122 has_rowdata {UPDATE t1 SET y = ''}
124 do_test trigger9-1.6.3 { execsql { ROLLBACK } } {}
126 do_test trigger9-1.7.1 {
129 CREATE TRIGGER trig1 BEFORE UPDATE ON t1 WHEN old.x>='2' BEGIN
130 INSERT INTO t2 VALUES(old.x);
132 UPDATE t1 SET y = '';
136 do_test trigger9-1.7.2 {
137 has_rowdata {UPDATE t1 SET y = ''}
139 do_test trigger9-1.7.3 { execsql { ROLLBACK } } {}
141 do_test trigger9-3.1 {
143 CREATE TABLE t3(a, b);
144 INSERT INTO t3 VALUES(1, 'one');
145 INSERT INTO t3 VALUES(2, 'two');
146 INSERT INTO t3 VALUES(3, 'three');
149 do_test trigger9-3.2 {
152 CREATE VIEW v1 AS SELECT * FROM t3;
153 CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
154 INSERT INTO t2 VALUES(old.a);
156 UPDATE v1 SET b = 'hello';
161 do_test trigger9-3.3 {
162 # In this test the 'c' column of the view is not required by
163 # the INSTEAD OF trigger, but the expression is reused internally as
164 # part of the view's WHERE clause. Check that this does not cause
169 CREATE VIEW v1 AS SELECT a, b AS c FROM t3 WHERE c > 'one';
170 CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
171 INSERT INTO t2 VALUES(old.a);
173 UPDATE v1 SET c = 'hello';
178 do_test trigger9-3.4 {
181 INSERT INTO t3 VALUES(3, 'three');
182 INSERT INTO t3 VALUES(3, 'four');
183 CREATE VIEW v1 AS SELECT DISTINCT a, b FROM t3;
184 CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
185 INSERT INTO t2 VALUES(old.a);
187 UPDATE v1 SET b = 'hello';
194 do_test trigger9-3.5 {
197 INSERT INTO t3 VALUES(1, 'uno');
198 CREATE VIEW v1 AS SELECT a, b FROM t3 EXCEPT SELECT 1, 'one';
199 CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
200 INSERT INTO t2 VALUES(old.a);
202 UPDATE v1 SET b = 'hello';
207 do_test trigger9-3.6 {
210 INSERT INTO t3 VALUES(1, 'zero');
212 SELECT sum(a) AS a, max(b) AS b FROM t3 GROUP BY t3.a HAVING b>'two';
213 CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
214 INSERT INTO t2 VALUES(old.a);
216 UPDATE v1 SET b = 'hello';