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_totalchanges
17 # Like [do_execsql_test], except it appends the value returned by
18 # [db total_changes] to the result of executing the SQL script.
20 proc do_tc_test {tn sql res} {
22 do_test $tn "concat \[execsql {$sql}\] \[db total_changes\]" $res
27 CREATE TABLE t1(a, b);
28 CREATE INDEX t1_b ON t1(b);
29 CREATE TABLE t2(x, y, PRIMARY KEY(x, y)) WITHOUT ROWID;
30 CREATE INDEX t2_y ON t2(y);
34 #--------------------------------------------------------------------------
35 # EVIDENCE-OF: R-65438-26258 This function returns the total number of
36 # rows inserted, modified or deleted by all INSERT, UPDATE or DELETE
37 # statements completed since the database connection was opened,
38 # including those executed as part of trigger programs.
40 # 1.1.*: different types of I/U/D statements,
41 # 1.2.*: trigger programs.
44 INSERT INTO t1 VALUES(1, 2);
45 INSERT INTO t1 VALUES(3, 4);
46 UPDATE t1 SET a = a+1;
55 SELECT 0, 0 UNION ALL SELECT a+1, b+1 FROM data WHERE a<99
57 INSERT INTO t1 SELECT * FROM data;
61 INSERT INTO t2 SELECT * FROM t1 WHERE a<50;
66 DELETE FROM t2 WHERE y<=25
69 do_execsql_test 1.2.1 {
73 sqlite3 db test.db ; # To reset total_changes
75 CREATE TABLE log(detail);
76 CREATE TRIGGER t1_after_insert AFTER INSERT ON t1 BEGIN
77 INSERT INTO log VALUES('inserted into t1');
80 CREATE TRIGGER t1_before_delete BEFORE DELETE ON t1 BEGIN
81 INSERT INTO log VALUES('deleting from t1');
82 INSERT INTO log VALUES('here we go!');
85 CREATE TRIGGER t1_after_update AFTER UPDATE ON t1 BEGIN
86 INSERT INTO log VALUES('update');
90 INSERT INTO t1 VALUES('a', 'b'); -- 1 + 1
91 UPDATE t1 SET b='c'; -- 1 + 1 + 2
92 DELETE FROM t1; -- 1 + 1 + 1
95 #--------------------------------------------------------------------------
96 # EVIDENCE-OF: R-61766-15253 Executing any other type of SQL statement
97 # does not affect the value returned by sqlite3_total_changes().
99 INSERT INTO t1 VALUES(1, 2), (3, 4);
100 INSERT INTO t2 VALUES(1, 2), (3, 4);
103 SELECT count(*) FROM t1;
106 CREATE TABLE t4(a, b);
107 ALTER TABLE t4 ADD COLUMN c;
108 CREATE INDEX i4 ON t4(c);
109 ALTER TABLE t4 RENAME TO t5;
118 #--------------------------------------------------------------------------
119 # EVIDENCE-OF: R-36043-10590 Changes made as part of foreign key
120 # actions are included in the count, but those made as part of REPLACE
121 # constraint resolution are not.
123 # 3.1.*: foreign key actions
124 # 3.2.*: REPLACE constraints.
126 sqlite3 db test.db ; # To reset total_changes
128 CREATE TABLE p1(c PRIMARY KEY, d);
129 CREATE TABLE c1(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE SET NULL);
130 CREATE TABLE c2(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE CASCADE);
131 CREATE TABLE c3(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE SET DEFAULT);
133 INSERT INTO p1 VALUES(1, 'one');
134 INSERT INTO p1 VALUES(2, 'two');
135 INSERT INTO p1 VALUES(3, 'three');
136 INSERT INTO p1 VALUES(4, 'four');
138 INSERT INTO c1 VALUES(1, 'i');
139 INSERT INTO c2 VALUES(2, 'ii');
140 INSERT INTO c3 VALUES(3, 'iii');
141 PRAGMA foreign_keys = ON;
144 do_tc_test 3.1.2 { DELETE FROM p1 WHERE c=1; } {9}
145 do_tc_test 3.1.3 { DELETE FROM p1 WHERE c=2; } {11}
146 do_tc_test 3.1.4 { DELETE FROM p1 WHERE c=3; } {13}
147 do_tc_test 3.1.5 { DELETE FROM p1 WHERE c=4; } {14} ; # only 1 this time.
149 sqlite3 db test.db ; # To reset total_changes
154 CREATE TABLE c1(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET NULL);
155 CREATE TABLE c2(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE CASCADE);
156 CREATE TABLE c3(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET DEFAULT);
158 INSERT INTO p1 VALUES(1, 'one');
159 INSERT INTO p1 VALUES(2, 'two');
160 INSERT INTO p1 VALUES(3, 'three');
161 INSERT INTO p1 VALUES(4, 'four');
163 INSERT INTO c1 VALUES(1, 'i');
164 INSERT INTO c2 VALUES(2, 'ii');
165 INSERT INTO c3 VALUES(3, 'iii');
166 PRAGMA foreign_keys = ON;
169 do_tc_test 3.1.7 { UPDATE p1 SET c=c+4 WHERE c=1; } {9}
170 do_tc_test 3.1.8 { UPDATE p1 SET c=c+4 WHERE c=2; } {11}
171 do_tc_test 3.1.9 { UPDATE p1 SET c=c+4 WHERE c=3; } {13}
172 do_tc_test 3.1.10 { UPDATE p1 SET c=c+4 WHERE c=4; } {14} ; # only 1 this time.
174 sqlite3 db test.db ; # To reset total_changes
176 CREATE TABLE t3(a UNIQUE, b UNIQUE);
177 INSERT INTO t3 VALUES('one', 'one');
178 INSERT INTO t3 VALUES('two', 'two');
179 INSERT OR REPLACE INTO t3 VALUES('one', 'two');
183 INSERT INTO t3 VALUES('three', 'one');
184 UPDATE OR REPLACE t3 SET b='two' WHERE b='one';
188 #--------------------------------------------------------------------------
189 # EVIDENCE-OF: R-54872-08741 Changes to a view that are intercepted by
190 # INSTEAD OF triggers are not counted.
192 sqlite3 db test.db ; # To reset total_changes
195 CREATE VIEW v1 AS SELECT * FROM t6;
196 CREATE TRIGGER v1_tr1 INSTEAD OF INSERT ON v1 BEGIN
200 INSERT INTO v1 VALUES('a');
201 INSERT INTO v1 VALUES('b');
204 CREATE TRIGGER v1_tr2 INSTEAD OF INSERT ON v1 BEGIN
205 INSERT INTO t6 VALUES(new.x);
208 INSERT INTO v1 VALUES('c');
209 INSERT INTO v1 VALUES('d');