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 triggers of views.
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
16 # If either views or triggers are disabled in this build, omit this file.
17 ifcapable {!trigger || !view} {
22 do_test trigger4-1.1 {
24 create table test1(id integer primary key,a);
25 create table test2(id integer,b);
27 select test1.id as id,a as a,b as b
28 from test1 join test2 on test2.id = test1.id;
29 create trigger I_test instead of insert on test
31 insert into test1 (id,a) values (NEW.id,NEW.a);
32 insert into test2 (id,b) values (NEW.id,NEW.b);
34 insert into test values(1,2,3);
38 do_test trigger4-1.2 {
43 do_test trigger4-1.3 {
47 insert into test values(4,5,6);
51 do_test trigger4-1.4 {
57 do_test trigger4-2.1 {
59 create trigger U_test instead of update on test
61 update test1 set a=NEW.a where id=NEW.id;
62 update test2 set b=NEW.b where id=NEW.id;
64 update test set a=22 where id=1;
68 do_test trigger4-2.2 {
73 do_test trigger4-2.3 {
77 update test set b=66 where id=4;
81 do_test trigger4-2.4 {
87 do_test trigger4-3.1 {
90 insert into test values(7,8,9);
92 } {1 {no such table: main.test2}}
93 do_test trigger4-3.2 {
97 insert into test values(7,8,9);
99 } {1 {no such table: main.test2}}
100 do_test trigger4-3.3 {
102 update test set a=222 where id=1;
104 } {1 {no such table: main.test2}}
105 do_test trigger4-3.4 {
110 do_test trigger4-3.5 {
112 create table test2(id,b);
113 insert into test values(7,8,9);
117 do_test trigger4-3.6 {
122 do_test trigger4-3.7 {
126 update test set b=99 where id=7;
131 do_test trigger4-4.1 {
133 forcedelete trigtest.db
134 forcedelete trigtest.db-journal
135 sqlite3 db trigtest.db
136 catchsql {drop table tbl; drop view vw}
138 create table tbl(a integer primary key, b integer);
139 create view vw as select * from tbl;
140 create trigger t_del_tbl instead of delete on vw for each row begin
141 delete from tbl where a = old.a;
143 create trigger t_upd_tbl instead of update on vw for each row begin
144 update tbl set a=new.a, b=new.b where a = old.a;
146 create trigger t_ins_tbl instead of insert on vw for each row begin
147 insert into tbl values (new.a,new.b);
149 insert into tbl values(101,1001);
150 insert into tbl values(102,1002);
151 insert into tbl select a+2, b+2 from tbl;
152 insert into tbl select a+4, b+4 from tbl;
153 insert into tbl select a+8, b+8 from tbl;
154 insert into tbl select a+16, b+16 from tbl;
155 insert into tbl select a+32, b+32 from tbl;
156 insert into tbl select a+64, b+64 from tbl;
157 select count(*) from vw;
160 do_test trigger4-4.2 {
161 execsql {select a, b from vw where a<103 or a>226 order by a}
162 } {101 1001 102 1002 227 1127 228 1128}
164 #test delete from view
165 do_test trigger4-5.1 {
166 catchsql {delete from vw where a>101 and a<2000}
168 do_test trigger4-5.2 {
169 execsql {select * from vw}
172 #test insert into view
173 do_test trigger4-6.1 {
175 insert into vw values(102,1002);
176 insert into vw select a+2, b+2 from vw;
177 insert into vw select a+4, b+4 from vw;
178 insert into vw select a+8, b+8 from vw;
179 insert into vw select a+16, b+16 from vw;
180 insert into vw select a+32, b+32 from vw;
181 insert into vw select a+64, b+64 from vw;
184 do_test trigger4-6.2 {
185 execsql {select count(*) from vw}
189 do_test trigger4-7.1 {
190 catchsql {update vw set b=b+1000 where a>101 and a<2000}
192 do_test trigger4-7.2 {
193 execsql {select a, b from vw where a<=102 or a>=227 order by a}
194 } {101 1001 102 2002 227 2127 228 2128}
196 integrity_check trigger4-99.9
198 forcedelete trigtest.db trigtest.db-journal