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