Add new test file e_totalchanges.test, containing tests of the sqlite3_total_changes() interface.

FossilOrigin-Name: f84af4adcc34d7a4c72027bf5b038a1a45a4c307
This commit is contained in:
dan 2014-10-28 20:49:59 +00:00
parent 4f41b7dec5
commit aa55563d7e
4 changed files with 233 additions and 22 deletions

@ -1,5 +1,5 @@
C Update\sthe\sdocumentation\son\sthe\ssqlite3_randomness()\sinterface\sto\sconform\nto\senhancements\sassociated\swith\sthe\sSQLITE_ENABLE_API_ARMOR\schange.
D 2014-10-28T20:35:18.499
C Add\snew\stest\sfile\se_totalchanges.test,\scontaining\stests\sof\sthe\ssqlite3_total_changes()\sinterface.
D 2014-10-28T20:49:59.213
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in cf57f673d77606ab0f2d9627ca52a9ba1464146a
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -229,7 +229,7 @@ F src/resolve.c 4965007d6497b6a4d7a6d98751cc39712885f952
F src/rowset.c eccf6af6d620aaa4579bd3b72c1b6395d9e9fa1e
F src/select.c 428165951748151e87a15295b7357221433e311b
F src/shell.c 282f8f5278e0c78eb442217531172ec9e1538796
F src/sqlite.h.in 9c8090268644fba8d2b5b594b9d577b3da23905c
F src/sqlite.h.in 737b7dd0f3f81fe183646d22828b39f85ef3e68c
F src/sqlite3.rc 992c9f5fb8285ae285d6be28240a7e8d3a7f2bad
F src/sqlite3ext.h 17d487c3c91b0b8c584a32fbeb393f6f795eea7d
F src/sqliteInt.h 90519c3b3e8ee90adfce013234c4bd07275d77b5
@ -464,6 +464,7 @@ F test/e_reindex.test 396b7b4f0a66863b4e95116a67d93b227193e589
F test/e_resolve.test dcce9308fb13b934ce29591105d031d3e14fbba6
F test/e_select.test 52692ff3849541e828ad4661fe3773a9b8711763
F test/e_select2.test aceb80ab927d46fba5ce7586ebabf23e2bb0604f
F test/e_totalchanges.test b12ee5809d3e63aeb83238dd501a7bca7fd72c10
F test/e_update.test 312cb8f5ccfe41515a6bb092f8ea562a9bd54d52
F test/e_uri.test 5ae33760fb2039c61aa2d90886f1664664173585
F test/e_vacuum.test 5bfbdc21b65c0abf24398d0ba31dc88d93ca77a9
@ -1208,7 +1209,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 41cdd0c422d61533a94870cb5ad094682956d472
R d8b973f48f438632851d26e03df40631
U drh
Z d1783aa6143d26d5a0a1c09bffad4d99
P 96e9917c350dfe2069b87860bbb961424ff1105a
R ff91cb59544b367ee28dfb499ecb1ca4
U dan
Z ba0579c9ba051395a01d0b2f40d05365

@ -1 +1 @@
96e9917c350dfe2069b87860bbb961424ff1105a
f84af4adcc34d7a4c72027bf5b038a1a45a4c307

@ -1926,20 +1926,17 @@ int sqlite3_changes(sqlite3*);
/*
** CAPI3REF: Total Number Of Rows Modified
**
** ^This function returns the number of row changes caused by [INSERT],
** [UPDATE] or [DELETE] statements since the [database connection] was opened.
** ^(The count returned by sqlite3_total_changes() includes all changes
** from all [CREATE TRIGGER | trigger] contexts and changes made by
** [foreign key actions]. However,
** the count does not include changes used to implement [REPLACE] constraints,
** rollbacks or [DROP TABLE] commands. The
** count does not include rows of views that fire an [INSTEAD OF trigger],
** though if the INSTEAD OF trigger makes changes of its own, those changes
** are counted.)^
** ^The sqlite3_total_changes() function counts the changes as soon as
** the statement that makes them is completed (when the statement handle
** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
**
** ^This function returns the total number of rows inserted, modified or
** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
** since the database connection was opened, including those executed as
** part of trigger programs. ^Executing any other type of SQL statement
** does not affect the value returned by sqlite3_total_changes().
**
** ^Changes made as part of [foreign key actions] are included in the
** count, but those made as part of REPLACE constraint resolution are
** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
** are not counted.
**
** See also the [sqlite3_changes()] interface, the
** [count_changes pragma], and the [total_changes() SQL function].
**

213
test/e_totalchanges.test Normal file

@ -0,0 +1,213 @@
# 2011 May 06
#
# 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_totalchanges
# Like [do_execsql_test], except it appends the value returned by
# [db total_changes] to the result of executing the SQL script.
#
proc do_tc_test {tn sql res} {
uplevel [list \
do_test $tn "concat \[execsql {$sql}\] \[db total_changes\]" $res
]
}
do_execsql_test 1.0 {
CREATE TABLE t1(a, b);
CREATE INDEX t1_b ON t1(b);
CREATE TABLE t2(x, y, PRIMARY KEY(x, y)) WITHOUT ROWID;
CREATE INDEX t2_y ON t2(y);
}
#--------------------------------------------------------------------------
# EVIDENCE-OF: R-65438-26258 This function returns the total number of
# rows inserted, modified or deleted by all INSERT, UPDATE or DELETE
# statements completed since the database connection was opened,
# including those executed as part of trigger programs.
#
# 1.1.*: different types of I/U/D statements,
# 1.2.*: trigger programs.
#
do_tc_test 1.1.1 {
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t1 VALUES(3, 4);
UPDATE t1 SET a = a+1;
DELETE FROM t1;
} {6}
do_tc_test 1.1.2 {
DELETE FROM t1
} {6}
do_tc_test 1.1.3 {
WITH data(a,b) AS (
SELECT 0, 0 UNION ALL SELECT a+1, b+1 FROM data WHERE a<99
)
INSERT INTO t1 SELECT * FROM data;
} {106}
do_tc_test 1.1.4 {
INSERT INTO t2 SELECT * FROM t1 WHERE a<50;
UPDATE t2 SET y=y+1;
} {206}
do_tc_test 1.1.5 {
DELETE FROM t2 WHERE y<=25
} {231}
do_execsql_test 1.2.1 {
DELETE FROM t1;
DELETE FROM t2;
}
sqlite3 db test.db ; # To reset total_changes
do_tc_test 1.2.2 {
CREATE TABLE log(detail);
CREATE TRIGGER t1_after_insert AFTER INSERT ON t1 BEGIN
INSERT INTO log VALUES('inserted into t1');
END;
CREATE TRIGGER t1_before_delete BEFORE DELETE ON t1 BEGIN
INSERT INTO log VALUES('deleting from t1');
INSERT INTO log VALUES('here we go!');
END;
CREATE TRIGGER t1_after_update AFTER UPDATE ON t1 BEGIN
INSERT INTO log VALUES('update');
DELETE FROM log;
END;
INSERT INTO t1 VALUES('a', 'b'); -- 1 + 1
UPDATE t1 SET b='c'; -- 1 + 1 + 2
DELETE FROM t1; -- 1 + 1 + 1
} {9}
#--------------------------------------------------------------------------
# EVIDENCE-OF: R-61766-15253 Executing any other type of SQL statement
# does not affect the value returned by sqlite3_total_changes().
do_tc_test 2.1 {
INSERT INTO t1 VALUES(1, 2), (3, 4);
INSERT INTO t2 VALUES(1, 2), (3, 4);
} {15}
do_tc_test 2.2 {
SELECT count(*) FROM t1;
} {2 15}
do_tc_test 2.3 {
CREATE TABLE t4(a, b);
ALTER TABLE t4 ADD COLUMN c;
CREATE INDEX i4 ON t4(c);
ALTER TABLE t4 RENAME TO t5;
ANALYZE;
BEGIN;
DROP TABLE t2;
ROLLBACK;
VACUUM;
} {15}
#--------------------------------------------------------------------------
# EVIDENCE-OF: R-36043-10590 Changes made as part of foreign key
# actions are included in the count, but those made as part of REPLACE
# constraint resolution are not.
#
# 3.1.*: foreign key actions
# 3.2.*: REPLACE constraints.
#
sqlite3 db test.db ; # To reset total_changes
do_tc_test 3.1.1 {
CREATE TABLE p1(c PRIMARY KEY, d);
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 CASCADE);
CREATE TABLE c3(a, b, FOREIGN KEY(a) REFERENCES p1 ON DELETE SET DEFAULT);
INSERT INTO p1 VALUES(1, 'one');
INSERT INTO p1 VALUES(2, 'two');
INSERT INTO p1 VALUES(3, 'three');
INSERT INTO p1 VALUES(4, 'four');
INSERT INTO c1 VALUES(1, 'i');
INSERT INTO c2 VALUES(2, 'ii');
INSERT INTO c3 VALUES(3, 'iii');
PRAGMA foreign_keys = ON;
} {7}
do_tc_test 3.1.2 { DELETE FROM p1 WHERE c=1; } {9}
do_tc_test 3.1.3 { DELETE FROM p1 WHERE c=2; } {11}
do_tc_test 3.1.4 { DELETE FROM p1 WHERE c=3; } {13}
do_tc_test 3.1.5 { DELETE FROM p1 WHERE c=4; } {14} ; # only 1 this time.
sqlite3 db test.db ; # To reset total_changes
do_tc_test 3.1.6 {
DROP TABLE c1;
DROP TABLE c2;
DROP TABLE c3;
CREATE TABLE c1(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET NULL);
CREATE TABLE c2(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE CASCADE);
CREATE TABLE c3(a, b, FOREIGN KEY(a) REFERENCES p1 ON UPDATE SET DEFAULT);
INSERT INTO p1 VALUES(1, 'one');
INSERT INTO p1 VALUES(2, 'two');
INSERT INTO p1 VALUES(3, 'three');
INSERT INTO p1 VALUES(4, 'four');
INSERT INTO c1 VALUES(1, 'i');
INSERT INTO c2 VALUES(2, 'ii');
INSERT INTO c3 VALUES(3, 'iii');
PRAGMA foreign_keys = ON;
} {7}
do_tc_test 3.1.7 { UPDATE p1 SET c=c+4 WHERE c=1; } {9}
do_tc_test 3.1.8 { UPDATE p1 SET c=c+4 WHERE c=2; } {11}
do_tc_test 3.1.9 { UPDATE p1 SET c=c+4 WHERE c=3; } {13}
do_tc_test 3.1.10 { UPDATE p1 SET c=c+4 WHERE c=4; } {14} ; # only 1 this time.
sqlite3 db test.db ; # To reset total_changes
do_tc_test 3.2.1 {
CREATE TABLE t3(a UNIQUE, b UNIQUE);
INSERT INTO t3 VALUES('one', 'one');
INSERT INTO t3 VALUES('two', 'two');
INSERT OR REPLACE INTO t3 VALUES('one', 'two');
} {3}
do_tc_test 3.2.2 {
INSERT INTO t3 VALUES('three', 'one');
UPDATE OR REPLACE t3 SET b='two' WHERE b='one';
SELECT * FROM t3;
} {three two 5}
#--------------------------------------------------------------------------
# EVIDENCE-OF: R-54872-08741 Changes to a view that are intercepted by
# INSTEAD OF triggers are not counted.
#
sqlite3 db test.db ; # To reset total_changes
do_tc_test 4.1 {
CREATE TABLE t6(x);
CREATE VIEW v1 AS SELECT * FROM t6;
CREATE TRIGGER v1_tr1 INSTEAD OF INSERT ON v1 BEGIN
SELECT 'no-op';
END;
INSERT INTO v1 VALUES('a');
INSERT INTO v1 VALUES('b');
} {0}
do_tc_test 4.2 {
CREATE TRIGGER v1_tr2 INSTEAD OF INSERT ON v1 BEGIN
INSERT INTO t6 VALUES(new.x);
END;
INSERT INTO v1 VALUES('c');
INSERT INTO v1 VALUES('d');
} {2}
finish_test