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 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix e_changes
17 # Like [do_execsql_test], except it appends the value returned by
18 # [db changes] to the result of executing the SQL script.
20 proc do_changes_test {tn sql res} {
22 do_test $tn "concat \[execsql {$sql}\] \[db changes\]" $res
27 #--------------------------------------------------------------------------
28 # EVIDENCE-OF: R-15996-49369 This function returns the number of rows
29 # modified, inserted or deleted by the most recently completed INSERT,
30 # UPDATE or DELETE statement on the database connection specified by the
34 CREATE TABLE t1(a, b);
35 CREATE TABLE t2(x, y, PRIMARY KEY(x, y)) WITHOUT ROWID;
36 CREATE INDEX i1 ON t1(a);
37 CREATE INDEX i2 ON t2(y);
41 CREATE TABLE t1(a, b);
42 CREATE INDEX i1 ON t1(b);
45 CREATE TABLE t1(a, b, PRIMARY KEY(a, b)) WITHOUT ROWID;
46 CREATE INDEX i1 ON t1(b);
53 do_changes_test 1.$tn.1 { INSERT INTO t1 VALUES(0, 0) } 1
56 do_changes_test 1.$tn.2 {
58 SELECT 1, 1 UNION ALL SELECT i+1, j+i FROM rows WHERE i<10
60 INSERT INTO t1 SELECT * FROM rows
64 do_changes_test 1.$tn.3 {
65 UPDATE t1 SET b=b+1 WHERE a<5;
69 do_changes_test 1.$tn.4 {
70 DELETE FROM t1 WHERE a>6
73 # Check the "on the database connecton specified" part of hte
74 # requirement - changes made by other connections do not show up in
75 # the return value of sqlite3_changes().
78 execsql { INSERT INTO t1 VALUES(-1, -1) } db2
86 # Test that statements that modify no rows because they hit UNIQUE
87 # constraints set the sqlite3_changes() value to 0. Regardless of
88 # whether or not they are executed inside an explicit transaction.
90 # 1.$tn.8-9: outside of a transaction
91 # 1.$tn.10-12: inside a transaction
93 do_changes_test 1.$tn.7 {
94 CREATE UNIQUE INDEX i2 ON t1(a);
96 do_catchsql_test 1.$tn.8 {
97 INSERT INTO t1 VALUES('a', 0), ('b', 0), ('c', 0), (0, 11);
98 } {1 {UNIQUE constraint failed: t1.a}}
99 do_test 1.$tn.9 { db changes } 0
100 do_catchsql_test 1.$tn.10 {
102 INSERT INTO t1 VALUES('a', 0), ('b', 0), ('c', 0), (0, 11);
103 } {1 {UNIQUE constraint failed: t1.a}}
104 do_test 1.$tn.11 { db changes } 0
105 do_changes_test 1.$tn.12 COMMIT 0
110 #--------------------------------------------------------------------------
111 # EVIDENCE-OF: R-44877-05564 Executing any other type of SQL statement
112 # does not modify the value returned by this function.
115 do_changes_test 2.1 { CREATE TABLE t1(x) } 0
116 do_changes_test 2.2 {
117 WITH d(y) AS (SELECT 1 UNION ALL SELECT y+1 FROM d WHERE y<47)
118 INSERT INTO t1 SELECT y FROM d;
121 # The statement above set changes() to 47. Check that none of the following
123 do_changes_test 2.3 { SELECT count(x) FROM t1 } {47 47}
124 do_changes_test 2.4 { DROP TABLE t1 } 47
125 do_changes_test 2.5 { CREATE TABLE t1(x) } 47
126 do_changes_test 2.6 { ALTER TABLE t1 ADD COLUMN b } 47
129 #--------------------------------------------------------------------------
130 # EVIDENCE-OF: R-53938-27527 Only changes made directly by the INSERT,
131 # UPDATE or DELETE statement are considered - auxiliary changes caused
132 # by triggers, foreign key actions or REPLACE constraint resolution are
136 # 3.2.*: foreign key actions
137 # 3.3.*: replace constraints
140 do_execsql_test 3.1.0 {
142 CREATE TABLE p1(one PRIMARY KEY, two);
144 CREATE TRIGGER tr_ai AFTER INSERT ON p1 BEGIN
145 INSERT INTO log VALUES('insert');
147 CREATE TRIGGER tr_bd BEFORE DELETE ON p1 BEGIN
148 INSERT INTO log VALUES('delete');
150 CREATE TRIGGER tr_au AFTER UPDATE ON p1 BEGIN
151 INSERT INTO log VALUES('update');
156 do_changes_test 3.1.1 {
157 INSERT INTO p1 VALUES('a', 'A'), ('b', 'B'), ('c', 'C');
159 do_changes_test 3.1.2 {
160 UPDATE p1 SET two = two||two;
162 do_changes_test 3.1.3 {
163 DELETE FROM p1 WHERE one IN ('a', 'c');
165 do_execsql_test 3.1.4 {
166 -- None of the inserts on table log were counted.
167 SELECT count(*) FROM log
170 do_execsql_test 3.2.0 {
172 INSERT INTO p1 VALUES('a', 'A'), ('b', 'B'), ('c', 'C');
174 CREATE TABLE c1(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE SET NULL);
175 CREATE TABLE c2(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE SET DEFAULT);
176 CREATE TABLE c3(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE CASCADE);
177 INSERT INTO c1 VALUES('a', 'aaa');
178 INSERT INTO c2 VALUES('b', 'bbb');
179 INSERT INTO c3 VALUES('c', 'ccc');
181 INSERT INTO p1 VALUES('d', 'D'), ('e', 'E'), ('f', 'F');
182 CREATE TABLE c4(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET NULL);
183 CREATE TABLE c5(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET DEFAULT);
184 CREATE TABLE c6(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE CASCADE);
185 INSERT INTO c4 VALUES('d', 'aaa');
186 INSERT INTO c5 VALUES('e', 'bbb');
187 INSERT INTO c6 VALUES('f', 'ccc');
189 PRAGMA foreign_keys = ON;
192 do_changes_test 3.2.1 { DELETE FROM p1 WHERE one = 'a' } 1
193 do_changes_test 3.2.2 { DELETE FROM p1 WHERE one = 'b' } 1
194 do_changes_test 3.2.3 { DELETE FROM p1 WHERE one = 'c' } 1
195 do_execsql_test 3.2.4 {
201 do_changes_test 3.2.5 { UPDATE p1 SET one = 'g' WHERE one = 'd' } 1
202 do_changes_test 3.2.6 { UPDATE p1 SET one = 'h' WHERE one = 'e' } 1
203 do_changes_test 3.2.7 { UPDATE p1 SET one = 'i' WHERE one = 'f' } 1
204 do_execsql_test 3.2.8 {
208 } {{} aaa {} bbb i ccc}
210 do_execsql_test 3.3.0 {
211 CREATE TABLE r1(a UNIQUE, b UNIQUE);
212 INSERT INTO r1 VALUES('i', 'i');
213 INSERT INTO r1 VALUES('ii', 'ii');
214 INSERT INTO r1 VALUES('iii', 'iii');
215 INSERT INTO r1 VALUES('iv', 'iv');
216 INSERT INTO r1 VALUES('v', 'v');
217 INSERT INTO r1 VALUES('vi', 'vi');
218 INSERT INTO r1 VALUES('vii', 'vii');
221 do_changes_test 3.3.1 { INSERT OR REPLACE INTO r1 VALUES('i', 1) } 1
222 do_changes_test 3.3.2 { INSERT OR REPLACE INTO r1 VALUES('iv', 'v') } 1
223 do_changes_test 3.3.3 { UPDATE OR REPLACE r1 SET b='v' WHERE a='iii' } 1
224 do_changes_test 3.3.4 { UPDATE OR REPLACE r1 SET b='vi',a='vii' WHERE a='ii' } 1
225 do_execsql_test 3.3.5 {
226 SELECT * FROM r1 ORDER BY a;
230 #--------------------------------------------------------------------------
231 # EVIDENCE-OF: R-09813-48563 The value returned by sqlite3_changes()
232 # immediately after an INSERT, UPDATE or DELETE statement run on a view
236 do_execsql_test 4.1 {
237 CREATE TABLE log(log);
238 CREATE TABLE t1(x, y);
239 INSERT INTO t1 VALUES(1, 2);
240 INSERT INTO t1 VALUES(3, 4);
241 INSERT INTO t1 VALUES(5, 6);
243 CREATE VIEW v1 AS SELECT * FROM t1;
244 CREATE TRIGGER v1_i INSTEAD OF INSERT ON v1 BEGIN
245 INSERT INTO log VALUES('insert');
247 CREATE TRIGGER v1_u INSTEAD OF UPDATE ON v1 BEGIN
248 INSERT INTO log VALUES('update'), ('update');
250 CREATE TRIGGER v1_d INSTEAD OF DELETE ON v1 BEGIN
251 INSERT INTO log VALUES('delete'), ('delete'), ('delete');
255 do_changes_test 4.2.1 { INSERT INTO t1 SELECT * FROM t1 } 3
256 do_changes_test 4.2.2 { INSERT INTO v1 VALUES(1, 2) } 0
258 do_changes_test 4.3.1 { INSERT INTO t1 SELECT * FROM t1 } 6
259 do_changes_test 4.3.2 { UPDATE v1 SET y='xyz' WHERE x=1 } 0
261 do_changes_test 4.4.1 { INSERT INTO t1 SELECT * FROM t1 } 12
262 do_changes_test 4.4.2 { DELETE FROM v1 WHERE x=5 } 0
265 #--------------------------------------------------------------------------
266 # EVIDENCE-OF: R-32918-61474 Before entering a trigger program the value
267 # returned by sqlite3_changes() function is saved. After the trigger
268 # program has finished, the original value is restored.
271 db func my_changes my_changes
273 proc my_changes {x} {
275 lappend ::changes $x $res
279 do_execsql_test 5.1.0 {
280 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
282 INSERT INTO t1 VALUES(1, NULL);
283 INSERT INTO t1 VALUES(2, NULL);
284 INSERT INTO t1 VALUES(3, NULL);
285 CREATE TRIGGER AFTER UPDATE ON t1 BEGIN
286 INSERT INTO t2 VALUES('a'), ('b'), ('c');
287 SELECT my_changes('trigger');
291 do_execsql_test 5.1.1 {
292 INSERT INTO t2 VALUES('a'), ('b');
293 UPDATE t1 SET b = my_changes('update');
297 # Value is being restored to "2" when the trigger program exits.
300 } {update 2 trigger 3 update 2 trigger 3 update 2 trigger 3}
304 do_execsql_test 5.2.0 {
305 CREATE TABLE t1(a, b);
307 INSERT INTO t1 VALUES(1, 0);
308 INSERT INTO t1 VALUES(2, 0);
309 INSERT INTO t1 VALUES(3, 0);
310 CREATE TRIGGER t1_a_u AFTER UPDATE ON t1 BEGIN
311 INSERT INTO log VALUES(old.b || ' -> ' || new.b || ' c = ' || changes() );
314 INSERT INTO t2 VALUES(1), (2), (3);
315 UPDATE t1 SET b = changes();
317 do_execsql_test 5.2.1 {
320 do_execsql_test 5.2.2 {
322 } {{0 -> 3 c = 3} {0 -> 3 c = 3} {0 -> 3 c = 3}}
325 #--------------------------------------------------------------------------
326 # EVIDENCE-OF: R-17146-37073 Within a trigger program each INSERT,
327 # UPDATE and DELETE statement sets the value returned by
328 # sqlite3_changes() upon completion as normal. Of course, this value
329 # will not include any changes performed by sub-triggers, as the
330 # sqlite3_changes() value will be saved and restored after each
331 # sub-trigger has run.
333 do_execsql_test 6.0 {
335 CREATE TABLE t1(a, b);
336 CREATE TABLE t2(a, b);
337 CREATE TABLE t3(a, b);
340 CREATE TRIGGER t1_i BEFORE INSERT ON t1 BEGIN
341 INSERT INTO t2 VALUES(new.a, new.b), (new.a, new.b);
342 INSERT INTO log VALUES('t2->' || changes());
345 CREATE TRIGGER t2_i AFTER INSERT ON t2 BEGIN
346 INSERT INTO t3 VALUES(new.a, new.b), (new.a, new.b), (new.a, new.b);
347 INSERT INTO log VALUES('t3->' || changes());
350 CREATE TRIGGER t1_u AFTER UPDATE ON t1 BEGIN
351 UPDATE t2 SET b=new.b WHERE a=old.a;
352 INSERT INTO log VALUES('t2->' || changes());
355 CREATE TRIGGER t2_u BEFORE UPDATE ON t2 BEGIN
356 UPDATE t3 SET b=new.b WHERE a=old.a;
357 INSERT INTO log VALUES('t3->' || changes());
360 CREATE TRIGGER t1_d AFTER DELETE ON t1 BEGIN
361 DELETE FROM t2 WHERE a=old.a AND b=old.b;
362 INSERT INTO log VALUES('t2->' || changes());
365 CREATE TRIGGER t2_d BEFORE DELETE ON t2 BEGIN
366 DELETE FROM t3 WHERE a=old.a AND b=old.b;
367 INSERT INTO log VALUES('t3->' || changes());
371 do_changes_test 6.1 {
372 INSERT INTO t1 VALUES('+', 'o');
374 } {t3->3 t3->3 t2->2 1}
376 do_changes_test 6.2 {
380 } {t3->6 t3->6 t2->2 1}
382 do_changes_test 6.3 {
386 } {t3->6 t3->0 t2->2 1}
389 #--------------------------------------------------------------------------
390 # EVIDENCE-OF: R-43399-09409 This means that if the changes() SQL
391 # function (or similar) is used by the first INSERT, UPDATE or DELETE
392 # statement within a trigger, it returns the value as set when the
393 # calling statement began executing.
395 # EVIDENCE-OF: R-53215-27584 If it is used by the second or subsequent
396 # such statement within a trigger program, the value returned reflects
397 # the number of rows modified by the previous INSERT, UPDATE or DELETE
398 # statement within the same trigger.
401 do_execsql_test 7.1 {
403 CREATE TABLE q2(u, v);
406 CREATE TRIGGER q2_insert BEFORE INSERT ON q2 BEGIN
408 /* changes() returns value from previous I/U/D in callers context */
409 INSERT INTO q1 VALUES('1:' || changes());
411 /* changes() returns value of previous I/U/D in this context */
412 INSERT INTO q3 VALUES(changes()), (2), (3);
413 INSERT INTO q1 VALUES('2:' || changes());
414 INSERT INTO q3 VALUES(changes() + 3), (changes()+4);
415 SELECT 'this does not affect things!';
416 INSERT INTO q1 VALUES('3:' || changes());
417 UPDATE q3 SET w = w+10 WHERE w%2;
418 INSERT INTO q1 VALUES('4:' || changes());
420 INSERT INTO q1 VALUES('5:' || changes());
424 do_execsql_test 7.2 {
425 INSERT INTO q2 VALUES('x', 'y');
431 do_execsql_test 7.3 {
433 INSERT INTO q2 VALUES('x', 'y');