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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
# 2011 October 28
#
# 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.
#
#***********************************************************************
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix e_changes
# Like [do_execsql_test], except it appends the value returned by
# [db changes] to the result of executing the SQL script.
#
proc do_changes_test {tn sql res} {
uplevel [list \
do_test $tn "concat \[execsql {$sql}\] \[db changes\]" $res
]
}
#--------------------------------------------------------------------------
# EVIDENCE-OF: R-58361-29089 The changes() function returns the number
# of database rows that were changed or inserted or deleted by the most
# recently completed INSERT, DELETE, or UPDATE statement, exclusive of
# statements in lower-level triggers.
#
do_execsql_test 1.0 {
CREATE TABLE t1(a, b);
CREATE TABLE t2(x, y, PRIMARY KEY(x, y)) WITHOUT ROWID;
CREATE INDEX i1 ON t1(a);
CREATE INDEX i2 ON t2(y);
}
foreach {tn schema} {
1 {
CREATE TABLE t1(a, b);
CREATE INDEX i1 ON t1(b);
}
2 {
CREATE TABLE t1(a, b, PRIMARY KEY(a, b)) WITHOUT ROWID;
CREATE INDEX i1 ON t1(b);
}
} {
reset_db
execsql $schema
# Insert 1 row.
do_changes_test 1.$tn.1 { INSERT INTO t1 VALUES(0, 0) } 1
# Insert 10 rows.
do_changes_test 1.$tn.2 {
WITH rows(i, j) AS (
SELECT 1, 1 UNION ALL SELECT i+1, j+i FROM rows WHERE i<10
)
INSERT INTO t1 SELECT * FROM rows
} 10
# Modify 5 rows.
do_changes_test 1.$tn.3 {
UPDATE t1 SET b=b+1 WHERE a<5;
} 5
# Delete 4 rows
do_changes_test 1.$tn.4 {
DELETE FROM t1 WHERE a>6
} 4
# Check the "on the database connecton specified" part of hte
# requirement - changes made by other connections do not show up in
# the return value of sqlite3_changes().
do_test 1.$tn.5 {
sqlite3 db2 test.db
execsql { INSERT INTO t1 VALUES(-1, -1) } db2
db2 changes
} 1
do_test 1.$tn.6 {
db changes
} 4
db2 close
# Test that statements that modify no rows because they hit UNIQUE
# constraints set the sqlite3_changes() value to 0. Regardless of
# whether or not they are executed inside an explicit transaction.
#
# 1.$tn.8-9: outside of a transaction
# 1.$tn.10-12: inside a transaction
#
do_changes_test 1.$tn.7 {
CREATE UNIQUE INDEX i2 ON t1(a);
} 4
do_catchsql_test 1.$tn.8 {
INSERT INTO t1 VALUES('a', 0), ('b', 0), ('c', 0), (0, 11);
} {1 {UNIQUE constraint failed: t1.a}}
do_test 1.$tn.9 { db changes } 0
do_catchsql_test 1.$tn.10 {
BEGIN;
INSERT INTO t1 VALUES('a', 0), ('b', 0), ('c', 0), (0, 11);
} {1 {UNIQUE constraint failed: t1.a}}
do_test 1.$tn.11 { db changes } 0
do_changes_test 1.$tn.12 COMMIT 0
}
#--------------------------------------------------------------------------
# X-EVIDENCE-OF: R-44877-05564 Executing any other type of SQL statement
# does not modify the value returned by this function.
#
reset_db
do_changes_test 2.1 { CREATE TABLE t1(x) } 0
do_changes_test 2.2 {
WITH d(y) AS (SELECT 1 UNION ALL SELECT y+1 FROM d WHERE y<47)
INSERT INTO t1 SELECT y FROM d;
} 47
# The statement above set changes() to 47. Check that none of the following
# modify this.
do_changes_test 2.3 { SELECT count(x) FROM t1 } {47 47}
do_changes_test 2.4 { DROP TABLE t1 } 47
do_changes_test 2.5 { CREATE TABLE t1(x) } 47
ifcapable altertable {
do_changes_test 2.6 { ALTER TABLE t1 ADD COLUMN b } 47
}
#--------------------------------------------------------------------------
# EVIDENCE-OF: R-53938-27527 Only changes made directly by the INSERT,
# UPDATE or DELETE statement are considered - auxiliary changes caused
# by triggers, foreign key actions or REPLACE constraint resolution are
# not counted.
#
# 3.1.*: triggers
# 3.2.*: foreign key actions
# 3.3.*: replace constraints
#
reset_db
do_execsql_test 3.1.0 {
CREATE TABLE log(x);
CREATE TABLE p1(one PRIMARY KEY, two);
CREATE TRIGGER tr_ai AFTER INSERT ON p1 BEGIN
INSERT INTO log VALUES('insert');
END;
CREATE TRIGGER tr_bd BEFORE DELETE ON p1 BEGIN
INSERT INTO log VALUES('delete');
END;
CREATE TRIGGER tr_au AFTER UPDATE ON p1 BEGIN
INSERT INTO log VALUES('update');
END;
}
do_changes_test 3.1.1 {
INSERT INTO p1 VALUES('a', 'A'), ('b', 'B'), ('c', 'C');
} 3
do_changes_test 3.1.2 {
UPDATE p1 SET two = two||two;
} 3
do_changes_test 3.1.3 {
DELETE FROM p1 WHERE one IN ('a', 'c');
} 2
do_execsql_test 3.1.4 {
-- None of the inserts on table log were counted.
SELECT count(*) FROM log
} 8
do_execsql_test 3.2.0 {
DELETE FROM p1;
INSERT INTO p1 VALUES('a', 'A'), ('b', 'B'), ('c', 'C');
CREATE TABLE c1(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE SET NULL);
CREATE TABLE c2(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE SET DEFAULT);
CREATE TABLE c3(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE CASCADE);
INSERT INTO c1 VALUES('a', 'aaa');
INSERT INTO c2 VALUES('b', 'bbb');
INSERT INTO c3 VALUES('c', 'ccc');
INSERT INTO p1 VALUES('d', 'D'), ('e', 'E'), ('f', 'F');
CREATE TABLE c4(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET NULL);
CREATE TABLE c5(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET DEFAULT);
CREATE TABLE c6(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE CASCADE);
INSERT INTO c4 VALUES('d', 'aaa');
INSERT INTO c5 VALUES('e', 'bbb');
INSERT INTO c6 VALUES('f', 'ccc');
PRAGMA foreign_keys = ON;
}
do_changes_test 3.2.1 { DELETE FROM p1 WHERE one = 'a' } 1
do_changes_test 3.2.2 { DELETE FROM p1 WHERE one = 'b' } 1
do_changes_test 3.2.3 { DELETE FROM p1 WHERE one = 'c' } 1
do_execsql_test 3.2.4 {
SELECT * FROM c1;
SELECT * FROM c2;
SELECT * FROM c3;
} {{} aaa {} bbb}
do_changes_test 3.2.5 { UPDATE p1 SET one = 'g' WHERE one = 'd' } 1
do_changes_test 3.2.6 { UPDATE p1 SET one = 'h' WHERE one = 'e' } 1
do_changes_test 3.2.7 { UPDATE p1 SET one = 'i' WHERE one = 'f' } 1
do_execsql_test 3.2.8 {
SELECT * FROM c4;
SELECT * FROM c5;
SELECT * FROM c6;
} {{} aaa {} bbb i ccc}
do_execsql_test 3.3.0 {
CREATE TABLE r1(a UNIQUE, b UNIQUE);
INSERT INTO r1 VALUES('i', 'i');
INSERT INTO r1 VALUES('ii', 'ii');
INSERT INTO r1 VALUES('iii', 'iii');
INSERT INTO r1 VALUES('iv', 'iv');
INSERT INTO r1 VALUES('v', 'v');
INSERT INTO r1 VALUES('vi', 'vi');
INSERT INTO r1 VALUES('vii', 'vii');
}
do_changes_test 3.3.1 { INSERT OR REPLACE INTO r1 VALUES('i', 1) } 1
do_changes_test 3.3.2 { INSERT OR REPLACE INTO r1 VALUES('iv', 'v') } 1
do_changes_test 3.3.3 { UPDATE OR REPLACE r1 SET b='v' WHERE a='iii' } 1
do_changes_test 3.3.4 { UPDATE OR REPLACE r1 SET b='vi',a='vii' WHERE a='ii' } 1
do_execsql_test 3.3.5 {
SELECT * FROM r1 ORDER BY a;
} {i 1 iii v vii vi}
#--------------------------------------------------------------------------
# EVIDENCE-OF: R-09813-48563 The value returned by sqlite3_changes()
# immediately after an INSERT, UPDATE or DELETE statement run on a view
# is always zero.
#
reset_db
do_execsql_test 4.1 {
CREATE TABLE log(log);
CREATE TABLE t1(x, y);
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t1 VALUES(3, 4);
INSERT INTO t1 VALUES(5, 6);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE TRIGGER v1_i INSTEAD OF INSERT ON v1 BEGIN
INSERT INTO log VALUES('insert');
END;
CREATE TRIGGER v1_u INSTEAD OF UPDATE ON v1 BEGIN
INSERT INTO log VALUES('update'), ('update');
END;
CREATE TRIGGER v1_d INSTEAD OF DELETE ON v1 BEGIN
INSERT INTO log VALUES('delete'), ('delete'), ('delete');
END;
}
do_changes_test 4.2.1 { INSERT INTO t1 SELECT * FROM t1 } 3
do_changes_test 4.2.2 { INSERT INTO v1 VALUES(1, 2) } 0
do_changes_test 4.3.1 { INSERT INTO t1 SELECT * FROM t1 } 6
do_changes_test 4.3.2 { UPDATE v1 SET y='xyz' WHERE x=1 } 0
do_changes_test 4.4.1 { INSERT INTO t1 SELECT * FROM t1 } 12
do_changes_test 4.4.2 { DELETE FROM v1 WHERE x=5 } 0
#--------------------------------------------------------------------------
# EVIDENCE-OF: R-32918-61474 Before entering a trigger program the value
# returned by sqlite3_changes() function is saved. After the trigger
# program has finished, the original value is restored.
#
reset_db
db func my_changes my_changes
set ::changes [list]
proc my_changes {x} {
set res [db changes]
lappend ::changes $x $res
return $res
}
do_execsql_test 5.1.0 {
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
CREATE TABLE t2(x);
INSERT INTO t1 VALUES(1, NULL);
INSERT INTO t1 VALUES(2, NULL);
INSERT INTO t1 VALUES(3, NULL);
CREATE TRIGGER AFTER UPDATE ON t1 BEGIN
INSERT INTO t2 VALUES('a'), ('b'), ('c');
SELECT my_changes('trigger');
END;
}
do_execsql_test 5.1.1 {
INSERT INTO t2 VALUES('a'), ('b');
UPDATE t1 SET b = my_changes('update');
SELECT * FROM t1;
} {1 2 2 2 3 2}
# Value is being restored to "2" when the trigger program exits.
do_test 5.1.2 {
set ::changes
} {update 2 trigger 3 update 2 trigger 3 update 2 trigger 3}
reset_db
do_execsql_test 5.2.0 {
CREATE TABLE t1(a, b);
CREATE TABLE log(x);
INSERT INTO t1 VALUES(1, 0);
INSERT INTO t1 VALUES(2, 0);
INSERT INTO t1 VALUES(3, 0);
CREATE TRIGGER t1_a_u AFTER UPDATE ON t1 BEGIN
INSERT INTO log VALUES(old.b || ' -> ' || new.b || ' c = ' || changes() );
END;
CREATE TABLE t2(a);
INSERT INTO t2 VALUES(1), (2), (3);
UPDATE t1 SET b = changes();
}
do_execsql_test 5.2.1 {
SELECT * FROM t1;
} {1 3 2 3 3 3}
do_execsql_test 5.2.2 {
SELECT * FROM log;
} {{0 -> 3 c = 3} {0 -> 3 c = 3} {0 -> 3 c = 3}}
#--------------------------------------------------------------------------
# EVIDENCE-OF: R-17146-37073 Within a trigger program each INSERT,
# UPDATE and DELETE statement sets the value returned by
# sqlite3_changes() upon completion as normal. Of course, this value
# will not include any changes performed by sub-triggers, as the
# sqlite3_changes() value will be saved and restored after each
# sub-trigger has run.
reset_db
do_execsql_test 6.0 {
CREATE TABLE t1(a, b);
CREATE TABLE t2(a, b);
CREATE TABLE t3(a, b);
CREATE TABLE log(x);
CREATE TRIGGER t1_i BEFORE INSERT ON t1 BEGIN
INSERT INTO t2 VALUES(new.a, new.b), (new.a, new.b);
INSERT INTO log VALUES('t2->' || changes());
END;
CREATE TRIGGER t2_i AFTER INSERT ON t2 BEGIN
INSERT INTO t3 VALUES(new.a, new.b), (new.a, new.b), (new.a, new.b);
INSERT INTO log VALUES('t3->' || changes());
END;
CREATE TRIGGER t1_u AFTER UPDATE ON t1 BEGIN
UPDATE t2 SET b=new.b WHERE a=old.a;
INSERT INTO log VALUES('t2->' || changes());
END;
CREATE TRIGGER t2_u BEFORE UPDATE ON t2 BEGIN
UPDATE t3 SET b=new.b WHERE a=old.a;
INSERT INTO log VALUES('t3->' || changes());
END;
CREATE TRIGGER t1_d AFTER DELETE ON t1 BEGIN
DELETE FROM t2 WHERE a=old.a AND b=old.b;
INSERT INTO log VALUES('t2->' || changes());
END;
CREATE TRIGGER t2_d BEFORE DELETE ON t2 BEGIN
DELETE FROM t3 WHERE a=old.a AND b=old.b;
INSERT INTO log VALUES('t3->' || changes());
END;
}
do_changes_test 6.1 {
INSERT INTO t1 VALUES('+', 'o');
SELECT * FROM log;
} {t3->3 t3->3 t2->2 1}
do_changes_test 6.2 {
DELETE FROM log;
UPDATE t1 SET b='*';
SELECT * FROM log;
} {t3->6 t3->6 t2->2 1}
do_changes_test 6.3 {
DELETE FROM log;
DELETE FROM t1;
SELECT * FROM log;
} {t3->6 t3->0 t2->2 1}
#--------------------------------------------------------------------------
# EVIDENCE-OF: R-43399-09409 This means that if the changes() SQL
# function (or similar) is used by the first INSERT, UPDATE or DELETE
# statement within a trigger, it returns the value as set when the
# calling statement began executing.
#
# EVIDENCE-OF: R-53215-27584 If it is used by the second or subsequent
# such statement within a trigger program, the value returned reflects
# the number of rows modified by the previous INSERT, UPDATE or DELETE
# statement within the same trigger.
#
reset_db
do_execsql_test 7.1 {
CREATE TABLE q1(t);
CREATE TABLE q2(u, v);
CREATE TABLE q3(w);
CREATE TRIGGER q2_insert BEFORE INSERT ON q2 BEGIN
/* changes() returns value from previous I/U/D in callers context */
INSERT INTO q1 VALUES('1:' || changes());
/* changes() returns value of previous I/U/D in this context */
INSERT INTO q3 VALUES(changes()), (2), (3);
INSERT INTO q1 VALUES('2:' || changes());
INSERT INTO q3 VALUES(changes() + 3), (changes()+4);
SELECT 'this does not affect things!';
INSERT INTO q1 VALUES('3:' || changes());
UPDATE q3 SET w = w+10 WHERE w%2;
INSERT INTO q1 VALUES('4:' || changes());
DELETE FROM q3;
INSERT INTO q1 VALUES('5:' || changes());
END;
}
do_execsql_test 7.2 {
INSERT INTO q2 VALUES('x', 'y');
SELECT * FROM q1;
} {
1:0 2:3 3:2 4:3 5:5
}
do_execsql_test 7.3 {
DELETE FROM q1;
INSERT INTO q2 VALUES('x', 'y');
SELECT * FROM q1;
} {
1:5 2:3 3:2 4:3 5:5
}
finish_test
|