sl@0
|
1 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
2 |
# a legal notice, here is a blessing:
|
sl@0
|
3 |
#
|
sl@0
|
4 |
# May you do good and not evil.
|
sl@0
|
5 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
6 |
# May you share freely, never taking more than you give.
|
sl@0
|
7 |
#
|
sl@0
|
8 |
#***********************************************************************
|
sl@0
|
9 |
#
|
sl@0
|
10 |
# Tests to make sure that values returned by changes() and total_changes()
|
sl@0
|
11 |
# are updated properly, especially inside triggers
|
sl@0
|
12 |
#
|
sl@0
|
13 |
# Note 1: changes() remains constant within a statement and only updates
|
sl@0
|
14 |
# once the statement is finished (triggers count as part of
|
sl@0
|
15 |
# statement).
|
sl@0
|
16 |
# Note 2: changes() is changed within the context of a trigger much like
|
sl@0
|
17 |
# last_insert_rowid() (see lastinsert.test), but is restored once
|
sl@0
|
18 |
# the trigger exits.
|
sl@0
|
19 |
# Note 3: changes() is not changed by a change to a view (since everything
|
sl@0
|
20 |
# is done within instead of trigger context).
|
sl@0
|
21 |
#
|
sl@0
|
22 |
|
sl@0
|
23 |
set testdir [file dirname $argv0]
|
sl@0
|
24 |
source $testdir/tester.tcl
|
sl@0
|
25 |
|
sl@0
|
26 |
# ----------------------------------------------------------------------------
|
sl@0
|
27 |
# 1.x - basic tests (no triggers)
|
sl@0
|
28 |
|
sl@0
|
29 |
# changes() set properly after insert
|
sl@0
|
30 |
do_test laststmtchanges-1.1 {
|
sl@0
|
31 |
catchsql {
|
sl@0
|
32 |
create table t0 (x);
|
sl@0
|
33 |
insert into t0 values (1);
|
sl@0
|
34 |
insert into t0 values (1);
|
sl@0
|
35 |
insert into t0 values (2);
|
sl@0
|
36 |
insert into t0 values (2);
|
sl@0
|
37 |
insert into t0 values (1);
|
sl@0
|
38 |
insert into t0 values (1);
|
sl@0
|
39 |
insert into t0 values (1);
|
sl@0
|
40 |
insert into t0 values (2);
|
sl@0
|
41 |
select changes(), total_changes();
|
sl@0
|
42 |
}
|
sl@0
|
43 |
} {0 {1 8}}
|
sl@0
|
44 |
|
sl@0
|
45 |
# changes() set properly after update
|
sl@0
|
46 |
do_test laststmtchanges-1.2 {
|
sl@0
|
47 |
catchsql {
|
sl@0
|
48 |
update t0 set x=3 where x=1;
|
sl@0
|
49 |
select changes(), total_changes();
|
sl@0
|
50 |
}
|
sl@0
|
51 |
} {0 {5 13}}
|
sl@0
|
52 |
|
sl@0
|
53 |
# There was some goofy change-counting logic in sqlite3_exec() that
|
sl@0
|
54 |
# appears to have been left over from SQLite version 2. This test
|
sl@0
|
55 |
# makes sure it has been removed.
|
sl@0
|
56 |
#
|
sl@0
|
57 |
do_test laststmtchanges-1.2.1 {
|
sl@0
|
58 |
db cache flush
|
sl@0
|
59 |
sqlite3_exec_printf db {update t0 set x=4 where x=3; select 1;} {}
|
sl@0
|
60 |
execsql {select changes()}
|
sl@0
|
61 |
} {5}
|
sl@0
|
62 |
|
sl@0
|
63 |
# changes() unchanged within an update statement
|
sl@0
|
64 |
do_test laststmtchanges-1.3 {
|
sl@0
|
65 |
execsql {update t0 set x=3 where x=4}
|
sl@0
|
66 |
catchsql {
|
sl@0
|
67 |
update t0 set x=x+changes() where x=3;
|
sl@0
|
68 |
select count() from t0 where x=8;
|
sl@0
|
69 |
}
|
sl@0
|
70 |
} {0 5}
|
sl@0
|
71 |
|
sl@0
|
72 |
# changes() set properly after update on table where no rows changed
|
sl@0
|
73 |
do_test laststmtchanges-1.4 {
|
sl@0
|
74 |
catchsql {
|
sl@0
|
75 |
update t0 set x=77 where x=88;
|
sl@0
|
76 |
select changes();
|
sl@0
|
77 |
}
|
sl@0
|
78 |
} {0 0}
|
sl@0
|
79 |
|
sl@0
|
80 |
# changes() set properly after delete from table
|
sl@0
|
81 |
do_test laststmtchanges-1.5 {
|
sl@0
|
82 |
catchsql {
|
sl@0
|
83 |
delete from t0 where x=2;
|
sl@0
|
84 |
select changes();
|
sl@0
|
85 |
}
|
sl@0
|
86 |
} {0 3}
|
sl@0
|
87 |
|
sl@0
|
88 |
# All remaining tests involve triggers. Skip them if triggers are not
|
sl@0
|
89 |
# supported in this build.
|
sl@0
|
90 |
#
|
sl@0
|
91 |
ifcapable {!trigger} {
|
sl@0
|
92 |
finish_test
|
sl@0
|
93 |
return
|
sl@0
|
94 |
}
|
sl@0
|
95 |
|
sl@0
|
96 |
|
sl@0
|
97 |
# ----------------------------------------------------------------------------
|
sl@0
|
98 |
# 2.x - tests with after insert trigger
|
sl@0
|
99 |
|
sl@0
|
100 |
# changes() changed properly after insert into table containing after trigger
|
sl@0
|
101 |
do_test laststmtchanges-2.1 {
|
sl@0
|
102 |
set ::tc [db total_changes]
|
sl@0
|
103 |
catchsql {
|
sl@0
|
104 |
create table t1 (k integer primary key);
|
sl@0
|
105 |
create table t2 (k integer primary key, v1, v2);
|
sl@0
|
106 |
create trigger r1 after insert on t1 for each row begin
|
sl@0
|
107 |
insert into t2 values (NULL, changes(), NULL);
|
sl@0
|
108 |
update t0 set x=x;
|
sl@0
|
109 |
update t2 set v2=changes();
|
sl@0
|
110 |
end;
|
sl@0
|
111 |
insert into t1 values (77);
|
sl@0
|
112 |
select changes();
|
sl@0
|
113 |
}
|
sl@0
|
114 |
} {0 1}
|
sl@0
|
115 |
|
sl@0
|
116 |
# changes() unchanged upon entry into after insert trigger
|
sl@0
|
117 |
do_test laststmtchanges-2.2 {
|
sl@0
|
118 |
catchsql {
|
sl@0
|
119 |
select v1 from t2;
|
sl@0
|
120 |
}
|
sl@0
|
121 |
} {0 3}
|
sl@0
|
122 |
|
sl@0
|
123 |
# changes() changed properly by update within context of after insert trigger
|
sl@0
|
124 |
do_test laststmtchanges-2.3 {
|
sl@0
|
125 |
catchsql {
|
sl@0
|
126 |
select v2 from t2;
|
sl@0
|
127 |
}
|
sl@0
|
128 |
} {0 5}
|
sl@0
|
129 |
|
sl@0
|
130 |
# Total changes caused by firing the trigger above:
|
sl@0
|
131 |
#
|
sl@0
|
132 |
# 1 from "insert into t1 values(77)" +
|
sl@0
|
133 |
# 1 from "insert into t2 values (NULL, changes(), NULL);" +
|
sl@0
|
134 |
# 5 from "update t0 set x=x;" +
|
sl@0
|
135 |
# 1 from "update t2 set v2=changes();"
|
sl@0
|
136 |
#
|
sl@0
|
137 |
do_test laststmtchanges-2.4 {
|
sl@0
|
138 |
expr [db total_changes] - $::tc
|
sl@0
|
139 |
} {8}
|
sl@0
|
140 |
|
sl@0
|
141 |
# ----------------------------------------------------------------------------
|
sl@0
|
142 |
# 3.x - tests with after update trigger
|
sl@0
|
143 |
|
sl@0
|
144 |
# changes() changed properly after update into table containing after trigger
|
sl@0
|
145 |
do_test laststmtchanges-3.1 {
|
sl@0
|
146 |
catchsql {
|
sl@0
|
147 |
drop trigger r1;
|
sl@0
|
148 |
delete from t2; delete from t2;
|
sl@0
|
149 |
create trigger r1 after update on t1 for each row begin
|
sl@0
|
150 |
insert into t2 values (NULL, changes(), NULL);
|
sl@0
|
151 |
delete from t0 where oid=1 or oid=2;
|
sl@0
|
152 |
update t2 set v2=changes();
|
sl@0
|
153 |
end;
|
sl@0
|
154 |
update t1 set k=k;
|
sl@0
|
155 |
select changes();
|
sl@0
|
156 |
}
|
sl@0
|
157 |
} {0 1}
|
sl@0
|
158 |
|
sl@0
|
159 |
# changes() unchanged upon entry into after update trigger
|
sl@0
|
160 |
do_test laststmtchanges-3.2 {
|
sl@0
|
161 |
catchsql {
|
sl@0
|
162 |
select v1 from t2;
|
sl@0
|
163 |
}
|
sl@0
|
164 |
} {0 0}
|
sl@0
|
165 |
|
sl@0
|
166 |
# changes() changed properly by delete within context of after update trigger
|
sl@0
|
167 |
do_test laststmtchanges-3.3 {
|
sl@0
|
168 |
catchsql {
|
sl@0
|
169 |
select v2 from t2;
|
sl@0
|
170 |
}
|
sl@0
|
171 |
} {0 2}
|
sl@0
|
172 |
|
sl@0
|
173 |
# ----------------------------------------------------------------------------
|
sl@0
|
174 |
# 4.x - tests with before delete trigger
|
sl@0
|
175 |
|
sl@0
|
176 |
# changes() changed properly on delete from table containing before trigger
|
sl@0
|
177 |
do_test laststmtchanges-4.1 {
|
sl@0
|
178 |
catchsql {
|
sl@0
|
179 |
drop trigger r1;
|
sl@0
|
180 |
delete from t2; delete from t2;
|
sl@0
|
181 |
create trigger r1 before delete on t1 for each row begin
|
sl@0
|
182 |
insert into t2 values (NULL, changes(), NULL);
|
sl@0
|
183 |
insert into t0 values (5);
|
sl@0
|
184 |
update t2 set v2=changes();
|
sl@0
|
185 |
end;
|
sl@0
|
186 |
delete from t1;
|
sl@0
|
187 |
select changes();
|
sl@0
|
188 |
}
|
sl@0
|
189 |
} {0 1}
|
sl@0
|
190 |
|
sl@0
|
191 |
# changes() unchanged upon entry into before delete trigger
|
sl@0
|
192 |
do_test laststmtchanges-4.2 {
|
sl@0
|
193 |
catchsql {
|
sl@0
|
194 |
select v1 from t2;
|
sl@0
|
195 |
}
|
sl@0
|
196 |
} {0 0}
|
sl@0
|
197 |
|
sl@0
|
198 |
# changes() changed properly by insert within context of before delete trigger
|
sl@0
|
199 |
do_test laststmtchanges-4.3 {
|
sl@0
|
200 |
catchsql {
|
sl@0
|
201 |
select v2 from t2;
|
sl@0
|
202 |
}
|
sl@0
|
203 |
} {0 1}
|
sl@0
|
204 |
|
sl@0
|
205 |
# ----------------------------------------------------------------------------
|
sl@0
|
206 |
# 5.x - complex tests with temporary tables and nested instead of triggers
|
sl@0
|
207 |
# These tests cannot run if the library does not have view support enabled.
|
sl@0
|
208 |
|
sl@0
|
209 |
ifcapable view&&tempdb {
|
sl@0
|
210 |
|
sl@0
|
211 |
do_test laststmtchanges-5.1 {
|
sl@0
|
212 |
catchsql {
|
sl@0
|
213 |
drop table t0; drop table t1; drop table t2;
|
sl@0
|
214 |
create temp table t0(x);
|
sl@0
|
215 |
create temp table t1 (k integer primary key);
|
sl@0
|
216 |
create temp table t2 (k integer primary key);
|
sl@0
|
217 |
create temp view v1 as select * from t1;
|
sl@0
|
218 |
create temp view v2 as select * from t2;
|
sl@0
|
219 |
create temp table n1 (k integer primary key, n);
|
sl@0
|
220 |
create temp table n2 (k integer primary key, n);
|
sl@0
|
221 |
insert into t0 values (1);
|
sl@0
|
222 |
insert into t0 values (2);
|
sl@0
|
223 |
insert into t0 values (1);
|
sl@0
|
224 |
insert into t0 values (1);
|
sl@0
|
225 |
insert into t0 values (1);
|
sl@0
|
226 |
insert into t0 values (2);
|
sl@0
|
227 |
insert into t0 values (2);
|
sl@0
|
228 |
insert into t0 values (1);
|
sl@0
|
229 |
create temp trigger r1 instead of insert on v1 for each row begin
|
sl@0
|
230 |
insert into n1 values (NULL, changes());
|
sl@0
|
231 |
update t0 set x=x*10 where x=1;
|
sl@0
|
232 |
insert into n1 values (NULL, changes());
|
sl@0
|
233 |
insert into t1 values (NEW.k);
|
sl@0
|
234 |
insert into n1 values (NULL, changes());
|
sl@0
|
235 |
update t0 set x=x*10 where x=0;
|
sl@0
|
236 |
insert into v2 values (100+NEW.k);
|
sl@0
|
237 |
insert into n1 values (NULL, changes());
|
sl@0
|
238 |
end;
|
sl@0
|
239 |
create temp trigger r2 instead of insert on v2 for each row begin
|
sl@0
|
240 |
insert into n2 values (NULL, changes());
|
sl@0
|
241 |
insert into t2 values (1000+NEW.k);
|
sl@0
|
242 |
insert into n2 values (NULL, changes());
|
sl@0
|
243 |
update t0 set x=x*100 where x=0;
|
sl@0
|
244 |
insert into n2 values (NULL, changes());
|
sl@0
|
245 |
delete from t0 where x=2;
|
sl@0
|
246 |
insert into n2 values (NULL, changes());
|
sl@0
|
247 |
end;
|
sl@0
|
248 |
insert into t1 values (77);
|
sl@0
|
249 |
select changes();
|
sl@0
|
250 |
}
|
sl@0
|
251 |
} {0 1}
|
sl@0
|
252 |
|
sl@0
|
253 |
do_test laststmtchanges-5.2 {
|
sl@0
|
254 |
catchsql {
|
sl@0
|
255 |
delete from t1 where k=88;
|
sl@0
|
256 |
select changes();
|
sl@0
|
257 |
}
|
sl@0
|
258 |
} {0 0}
|
sl@0
|
259 |
|
sl@0
|
260 |
do_test laststmtchanges-5.3 {
|
sl@0
|
261 |
catchsql {
|
sl@0
|
262 |
insert into v1 values (5);
|
sl@0
|
263 |
select changes();
|
sl@0
|
264 |
}
|
sl@0
|
265 |
} {0 0}
|
sl@0
|
266 |
|
sl@0
|
267 |
do_test laststmtchanges-5.4 {
|
sl@0
|
268 |
catchsql {
|
sl@0
|
269 |
select n from n1;
|
sl@0
|
270 |
}
|
sl@0
|
271 |
} {0 {0 5 1 0}}
|
sl@0
|
272 |
|
sl@0
|
273 |
do_test laststmtchanges-5.5 {
|
sl@0
|
274 |
catchsql {
|
sl@0
|
275 |
select n from n2;
|
sl@0
|
276 |
}
|
sl@0
|
277 |
} {0 {0 1 0 3}}
|
sl@0
|
278 |
|
sl@0
|
279 |
} ;# ifcapable view
|
sl@0
|
280 |
|
sl@0
|
281 |
finish_test
|