xref: /sqlite-3.40.0/test/e_totalchanges.test (revision dd31c033)
1# 2011 May 06
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
12
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15set testprefix e_totalchanges
16
17# Like [do_execsql_test], except it appends the value returned by
18# [db total_changes] to the result of executing the SQL script.
19#
20proc do_tc_test {tn sql res} {
21  uplevel [list \
22    do_test $tn "concat \[execsql {$sql}\] \[db total_changes\]" $res
23  ]
24}
25
26do_execsql_test 1.0 {
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);
31}
32
33
34#--------------------------------------------------------------------------
35# EVIDENCE-OF: R-38914-26427 The total_changes() function returns the
36# number of row changes caused by INSERT, UPDATE or DELETE statements
37# since the current database connection was opened.
38#
39#   1.1.*: different types of I/U/D statements,
40#   1.2.*: trigger programs.
41#
42do_tc_test 1.1.1 {
43  INSERT INTO t1 VALUES(1, 2);
44  INSERT INTO t1 VALUES(3, 4);
45  UPDATE t1 SET a = a+1;
46  DELETE FROM t1;
47} {6}
48do_tc_test 1.1.2 {
49  DELETE FROM t1
50} {6}
51
52do_tc_test 1.1.3 {
53  WITH data(a,b) AS (
54      SELECT 0, 0 UNION ALL SELECT a+1, b+1 FROM data WHERE a<99
55  )
56  INSERT INTO t1 SELECT * FROM data;
57} {106}
58
59do_tc_test 1.1.4 {
60  INSERT INTO t2 SELECT * FROM t1 WHERE a<50;
61  UPDATE t2 SET y=y+1;
62} {206}
63
64do_tc_test 1.1.5 {
65  DELETE FROM t2 WHERE y<=25
66} {231}
67
68do_execsql_test 1.2.1 {
69  DELETE FROM t1;
70  DELETE FROM t2;
71}
72sqlite3 db test.db     ; # To reset total_changes
73do_tc_test 1.2.2 {
74  CREATE TABLE log(detail);
75  CREATE TRIGGER t1_after_insert AFTER INSERT ON t1 BEGIN
76    INSERT INTO log VALUES('inserted into t1');
77  END;
78
79  CREATE TRIGGER t1_before_delete BEFORE DELETE ON t1 BEGIN
80    INSERT INTO log VALUES('deleting from t1');
81    INSERT INTO log VALUES('here we go!');
82  END;
83
84  CREATE TRIGGER t1_after_update AFTER UPDATE ON t1 BEGIN
85    INSERT INTO log VALUES('update');
86    DELETE FROM log;
87  END;
88
89  INSERT INTO t1 VALUES('a', 'b');   -- 1 + 1
90  UPDATE t1 SET b='c';               -- 1 + 1 + 2
91  DELETE FROM t1;                    -- 1 + 1 + 1
92} {9}
93
94#--------------------------------------------------------------------------
95# EVIDENCE-OF: R-61766-15253 Executing any other type of SQL statement
96# does not affect the value returned by sqlite3_total_changes().
97do_tc_test 2.1 {
98  INSERT INTO t1 VALUES(1, 2), (3, 4);
99  INSERT INTO t2 VALUES(1, 2), (3, 4);
100} {15}
101do_tc_test 2.2 {
102  SELECT count(*) FROM t1;
103} {2 15}
104do_tc_test 2.3 {
105  CREATE TABLE t4(a, b);
106  ALTER TABLE t4 ADD COLUMN c;
107  CREATE INDEX i4 ON t4(c);
108  ALTER TABLE t4 RENAME TO t5;
109  ANALYZE;
110  BEGIN;
111  DROP TABLE t2;
112  ROLLBACK;
113  VACUUM;
114} {15}
115
116
117#--------------------------------------------------------------------------
118# EVIDENCE-OF: R-36043-10590 Changes made as part of foreign key
119# actions are included in the count, but those made as part of REPLACE
120# constraint resolution are not.
121#
122#   3.1.*: foreign key actions
123#   3.2.*: REPLACE constraints.
124#
125sqlite3 db test.db     ; # To reset total_changes
126do_tc_test 3.1.1 {
127  CREATE TABLE p1(c PRIMARY KEY, d);
128  CREATE TABLE c1(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE SET NULL);
129  CREATE TABLE c2(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE CASCADE);
130  CREATE TABLE c3(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE SET DEFAULT);
131
132  INSERT INTO p1 VALUES(1, 'one');
133  INSERT INTO p1 VALUES(2, 'two');
134  INSERT INTO p1 VALUES(3, 'three');
135  INSERT INTO p1 VALUES(4, 'four');
136
137  INSERT INTO c1 VALUES(1, 'i');
138  INSERT INTO c2 VALUES(2, 'ii');
139  INSERT INTO c3 VALUES(3, 'iii');
140  PRAGMA foreign_keys = ON;
141} {7}
142
143do_tc_test 3.1.2 { DELETE FROM p1 WHERE c=1; } {9}
144do_tc_test 3.1.3 { DELETE FROM p1 WHERE c=2; } {11}
145do_tc_test 3.1.4 { DELETE FROM p1 WHERE c=3; } {13}
146do_tc_test 3.1.5 { DELETE FROM p1 WHERE c=4; } {14}  ; # only 1 this time.
147
148sqlite3 db test.db     ; # To reset total_changes
149do_tc_test 3.1.6 {
150  DROP TABLE c1;
151  DROP TABLE c2;
152  DROP TABLE c3;
153  CREATE TABLE c1(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET NULL);
154  CREATE TABLE c2(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE CASCADE);
155  CREATE TABLE c3(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET DEFAULT);
156
157  INSERT INTO p1 VALUES(1, 'one');
158  INSERT INTO p1 VALUES(2, 'two');
159  INSERT INTO p1 VALUES(3, 'three');
160  INSERT INTO p1 VALUES(4, 'four');
161
162  INSERT INTO c1 VALUES(1, 'i');
163  INSERT INTO c2 VALUES(2, 'ii');
164  INSERT INTO c3 VALUES(3, 'iii');
165  PRAGMA foreign_keys = ON;
166} {7}
167
168do_tc_test 3.1.7  { UPDATE p1 SET c=c+4 WHERE c=1; } {9}
169do_tc_test 3.1.8  { UPDATE p1 SET c=c+4 WHERE c=2; } {11}
170do_tc_test 3.1.9  { UPDATE p1 SET c=c+4 WHERE c=3; } {13}
171do_tc_test 3.1.10 { UPDATE p1 SET c=c+4 WHERE c=4; } {14}  ; # only 1 this time.
172
173sqlite3 db test.db     ; # To reset total_changes
174do_tc_test 3.2.1 {
175  CREATE TABLE t3(a UNIQUE, b UNIQUE);
176  INSERT INTO t3 VALUES('one', 'one');
177  INSERT INTO t3 VALUES('two', 'two');
178  INSERT OR REPLACE INTO t3 VALUES('one', 'two');
179} {3}
180
181do_tc_test 3.2.2 {
182  INSERT INTO t3 VALUES('three', 'one');
183  UPDATE OR REPLACE t3 SET b='two' WHERE b='one';
184  SELECT * FROM t3;
185} {three two 5}
186
187#--------------------------------------------------------------------------
188# EVIDENCE-OF: R-54872-08741 Changes to a view that are intercepted by
189# INSTEAD OF triggers are not counted.
190#
191sqlite3 db test.db     ; # To reset total_changes
192do_tc_test 4.1 {
193  CREATE TABLE t6(x);
194  CREATE VIEW v1 AS SELECT * FROM t6;
195  CREATE TRIGGER v1_tr1 INSTEAD OF INSERT ON v1 BEGIN
196    SELECT 'no-op';
197  END;
198
199  INSERT INTO v1 VALUES('a');
200  INSERT INTO v1 VALUES('b');
201} {0}
202do_tc_test 4.2 {
203  CREATE TRIGGER v1_tr2 INSTEAD OF INSERT ON v1 BEGIN
204    INSERT INTO t6 VALUES(new.x);
205  END;
206
207  INSERT INTO v1 VALUES('c');
208  INSERT INTO v1 VALUES('d');
209} {2}
210
211
212finish_test
213