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} {
35 set ::testprefix trigger9
37 proc has_rowdata {sql} {
38 expr {[lsearch [execsql "explain $sql"] RowData]>=0}
41 do_test trigger9-1.1 {
43 PRAGMA page_size = 1024;
44 CREATE TABLE t1(x, y, z);
45 INSERT INTO t1 VALUES('1', randstr(10000,10000), '2');
46 INSERT INTO t1 VALUES('2', randstr(10000,10000), '4');
47 INSERT INTO t1 VALUES('3', randstr(10000,10000), '6');
52 do_test trigger9-1.2.1 {
55 CREATE TRIGGER trig1 BEFORE DELETE ON t1 BEGIN
56 INSERT INTO t2 VALUES(old.rowid);
62 do_test trigger9-1.2.3 {
63 has_rowdata {DELETE FROM t1}
65 do_test trigger9-1.2.4 { execsql { ROLLBACK } } {}
67 do_test trigger9-1.3.1 {
70 CREATE TRIGGER trig1 BEFORE DELETE ON t1 BEGIN
71 INSERT INTO t2 VALUES(old.x);
77 do_test trigger9-1.3.2 {
78 has_rowdata {DELETE FROM t1}
80 do_test trigger9-1.3.3 { execsql { ROLLBACK } } {}
82 do_test trigger9-1.4.1 {
85 CREATE TRIGGER trig1 BEFORE DELETE ON t1 WHEN old.x='1' BEGIN
86 INSERT INTO t2 VALUES(old.rowid);
92 do_test trigger9-1.4.2 {
93 has_rowdata {DELETE FROM t1}
95 do_test trigger9-1.4.3 { execsql { ROLLBACK } } {}
97 do_test trigger9-1.5.1 {
100 CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
101 INSERT INTO t2 VALUES(old.rowid);
103 UPDATE t1 SET y = '';
107 do_test trigger9-1.5.2 {
108 has_rowdata {UPDATE t1 SET y = ''}
110 do_test trigger9-1.5.3 { execsql { ROLLBACK } } {}
112 do_test trigger9-1.6.1 {
115 CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
116 INSERT INTO t2 VALUES(old.x);
118 UPDATE t1 SET y = '';
122 do_test trigger9-1.6.2 {
123 has_rowdata {UPDATE t1 SET y = ''}
125 do_test trigger9-1.6.3 { execsql { ROLLBACK } } {}
127 do_test trigger9-1.7.1 {
130 CREATE TRIGGER trig1 BEFORE UPDATE ON t1 WHEN old.x>='2' BEGIN
131 INSERT INTO t2 VALUES(old.x);
133 UPDATE t1 SET y = '';
137 do_test trigger9-1.7.2 {
138 has_rowdata {UPDATE t1 SET y = ''}
140 do_test trigger9-1.7.3 { execsql { ROLLBACK } } {}
142 do_test trigger9-3.1 {
144 CREATE TABLE t3(a, b);
145 INSERT INTO t3 VALUES(1, 'one');
146 INSERT INTO t3 VALUES(2, 'two');
147 INSERT INTO t3 VALUES(3, 'three');
150 do_test trigger9-3.2 {
153 CREATE VIEW v1 AS SELECT * FROM t3;
154 CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
155 INSERT INTO t2 VALUES(old.a);
157 UPDATE v1 SET b = 'hello';
162 do_test trigger9-3.3 {
163 # In this test the 'c' column of the view is not required by
164 # the INSTEAD OF trigger, but the expression is reused internally as
165 # part of the view's WHERE clause. Check that this does not cause
170 CREATE VIEW v1 AS SELECT a, b AS c FROM t3 WHERE c > 'one';
171 CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
172 INSERT INTO t2 VALUES(old.a);
174 UPDATE v1 SET c = 'hello';
179 do_test trigger9-3.4 {
182 INSERT INTO t3 VALUES(3, 'three');
183 INSERT INTO t3 VALUES(3, 'four');
184 CREATE VIEW v1 AS SELECT DISTINCT a, b FROM t3;
185 CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
186 INSERT INTO t2 VALUES(old.a);
188 UPDATE v1 SET b = 'hello';
195 do_test trigger9-3.5 {
198 INSERT INTO t3 VALUES(1, 'uno');
199 CREATE VIEW v1 AS SELECT a, b FROM t3 EXCEPT SELECT 1, 'one';
200 CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
201 INSERT INTO t2 VALUES(old.a);
203 UPDATE v1 SET b = 'hello';
208 do_test trigger9-3.6 {
211 INSERT INTO t3 VALUES(1, 'zero');
213 SELECT sum(a) AS a, max(b) AS b FROM t3 GROUP BY t3.a HAVING b>'two';
214 CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
215 INSERT INTO t2 VALUES(old.a);
217 UPDATE v1 SET b = 'hello';
225 do_execsql_test 4.1 {
226 CREATE TABLE t1(a, b);
228 INSERT INTO t1 VALUES(1, 2);
229 INSERT INTO t1 VALUES(3, 4);
230 CREATE VIEW v1 AS SELECT a, b FROM t1;
232 CREATE TRIGGER tr1 INSTEAD OF DELETE ON v1 BEGIN
233 INSERT INTO log VALUES('delete');
236 CREATE TRIGGER tr2 INSTEAD OF UPDATE ON v1 BEGIN
237 INSERT INTO log VALUES('update');
240 CREATE TRIGGER tr3 INSTEAD OF INSERT ON v1 BEGIN
241 INSERT INTO log VALUES('insert');
245 do_execsql_test 4.2 {
246 DELETE FROM v1 WHERE rowid=1;
249 do_execsql_test 4.3 {
250 UPDATE v1 SET a=b WHERE rowid=2;