2008-10-27 16:59:33 +03:00
|
|
|
#
|
2004-02-21 01:54:25 +03:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
#
|
2004-06-21 10:50:26 +04:00
|
|
|
# Tests to make sure that values returned by changes() and total_changes()
|
|
|
|
# are updated properly, especially inside triggers
|
2004-02-21 01:54:25 +03:00
|
|
|
#
|
2004-06-21 10:50:26 +04:00
|
|
|
# Note 1: changes() remains constant within a statement and only updates
|
|
|
|
# once the statement is finished (triggers count as part of
|
|
|
|
# statement).
|
|
|
|
# Note 2: changes() is changed within the context of a trigger much like
|
|
|
|
# last_insert_rowid() (see lastinsert.test), but is restored once
|
|
|
|
# the trigger exits.
|
|
|
|
# Note 3: changes() is not changed by a change to a view (since everything
|
|
|
|
# is done within instead of trigger context).
|
2004-02-21 01:54:25 +03:00
|
|
|
#
|
2008-10-27 16:59:33 +03:00
|
|
|
# $Id: laststmtchanges.test,v 1.7 2008/10/27 13:59:34 danielk1977 Exp $
|
2004-02-21 01:54:25 +03:00
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# 1.x - basic tests (no triggers)
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() set properly after insert
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-1.1 {
|
|
|
|
catchsql {
|
|
|
|
create table t0 (x);
|
|
|
|
insert into t0 values (1);
|
|
|
|
insert into t0 values (1);
|
|
|
|
insert into t0 values (2);
|
|
|
|
insert into t0 values (2);
|
|
|
|
insert into t0 values (1);
|
|
|
|
insert into t0 values (1);
|
|
|
|
insert into t0 values (1);
|
|
|
|
insert into t0 values (2);
|
2004-06-21 10:50:26 +04:00
|
|
|
select changes(), total_changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
}
|
2004-06-21 10:50:26 +04:00
|
|
|
} {0 {1 8}}
|
2004-02-21 01:54:25 +03:00
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() set properly after update
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-1.2 {
|
|
|
|
catchsql {
|
|
|
|
update t0 set x=3 where x=1;
|
2004-06-21 10:50:26 +04:00
|
|
|
select changes(), total_changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
}
|
2004-06-21 10:50:26 +04:00
|
|
|
} {0 {5 13}}
|
2004-02-21 01:54:25 +03:00
|
|
|
|
2007-04-25 15:28:16 +04:00
|
|
|
# There was some goofy change-counting logic in sqlite3_exec() that
|
|
|
|
# appears to have been left over from SQLite version 2. This test
|
|
|
|
# makes sure it has been removed.
|
|
|
|
#
|
|
|
|
do_test laststmtchanges-1.2.1 {
|
|
|
|
db cache flush
|
|
|
|
sqlite3_exec_printf db {update t0 set x=4 where x=3; select 1;} {}
|
|
|
|
execsql {select changes()}
|
|
|
|
} {5}
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() unchanged within an update statement
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-1.3 {
|
2007-04-25 15:28:16 +04:00
|
|
|
execsql {update t0 set x=3 where x=4}
|
2004-02-21 01:54:25 +03:00
|
|
|
catchsql {
|
2004-06-21 10:50:26 +04:00
|
|
|
update t0 set x=x+changes() where x=3;
|
2004-02-21 01:54:25 +03:00
|
|
|
select count() from t0 where x=8;
|
|
|
|
}
|
|
|
|
} {0 5}
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() set properly after update on table where no rows changed
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-1.4 {
|
|
|
|
catchsql {
|
|
|
|
update t0 set x=77 where x=88;
|
2004-06-21 10:50:26 +04:00
|
|
|
select changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
}
|
|
|
|
} {0 0}
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() set properly after delete from table
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-1.5 {
|
|
|
|
catchsql {
|
|
|
|
delete from t0 where x=2;
|
2004-06-21 10:50:26 +04:00
|
|
|
select changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
}
|
|
|
|
} {0 3}
|
|
|
|
|
2004-11-04 07:42:28 +03:00
|
|
|
# All remaining tests involve triggers. Skip them if triggers are not
|
|
|
|
# supported in this build.
|
|
|
|
#
|
|
|
|
ifcapable {!trigger} {
|
|
|
|
finish_test
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-21 01:54:25 +03:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# 2.x - tests with after insert trigger
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() changed properly after insert into table containing after trigger
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-2.1 {
|
2004-06-21 10:50:26 +04:00
|
|
|
set ::tc [db total_changes]
|
2004-02-21 01:54:25 +03:00
|
|
|
catchsql {
|
|
|
|
create table t1 (k integer primary key);
|
|
|
|
create table t2 (k integer primary key, v1, v2);
|
|
|
|
create trigger r1 after insert on t1 for each row begin
|
2004-06-21 10:50:26 +04:00
|
|
|
insert into t2 values (NULL, changes(), NULL);
|
2004-02-21 01:54:25 +03:00
|
|
|
update t0 set x=x;
|
2004-06-21 10:50:26 +04:00
|
|
|
update t2 set v2=changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
end;
|
|
|
|
insert into t1 values (77);
|
2004-06-21 10:50:26 +04:00
|
|
|
select changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
}
|
|
|
|
} {0 1}
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() unchanged upon entry into after insert trigger
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-2.2 {
|
|
|
|
catchsql {
|
|
|
|
select v1 from t2;
|
|
|
|
}
|
|
|
|
} {0 3}
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() changed properly by update within context of after insert trigger
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-2.3 {
|
|
|
|
catchsql {
|
|
|
|
select v2 from t2;
|
|
|
|
}
|
|
|
|
} {0 5}
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# Total changes caused by firing the trigger above:
|
|
|
|
#
|
|
|
|
# 1 from "insert into t1 values(77)" +
|
|
|
|
# 1 from "insert into t2 values (NULL, changes(), NULL);" +
|
|
|
|
# 5 from "update t0 set x=x;" +
|
|
|
|
# 1 from "update t2 set v2=changes();"
|
|
|
|
#
|
|
|
|
do_test laststmtchanges-2.4 {
|
|
|
|
expr [db total_changes] - $::tc
|
|
|
|
} {8}
|
|
|
|
|
2004-02-21 01:54:25 +03:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# 3.x - tests with after update trigger
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() changed properly after update into table containing after trigger
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-3.1 {
|
|
|
|
catchsql {
|
|
|
|
drop trigger r1;
|
|
|
|
delete from t2; delete from t2;
|
|
|
|
create trigger r1 after update on t1 for each row begin
|
2004-06-21 10:50:26 +04:00
|
|
|
insert into t2 values (NULL, changes(), NULL);
|
2004-02-21 01:54:25 +03:00
|
|
|
delete from t0 where oid=1 or oid=2;
|
2004-06-21 10:50:26 +04:00
|
|
|
update t2 set v2=changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
end;
|
|
|
|
update t1 set k=k;
|
2004-06-21 10:50:26 +04:00
|
|
|
select changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
}
|
|
|
|
} {0 1}
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() unchanged upon entry into after update trigger
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-3.2 {
|
|
|
|
catchsql {
|
|
|
|
select v1 from t2;
|
|
|
|
}
|
|
|
|
} {0 0}
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() changed properly by delete within context of after update trigger
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-3.3 {
|
|
|
|
catchsql {
|
|
|
|
select v2 from t2;
|
|
|
|
}
|
|
|
|
} {0 2}
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# 4.x - tests with before delete trigger
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() changed properly on delete from table containing before trigger
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-4.1 {
|
|
|
|
catchsql {
|
|
|
|
drop trigger r1;
|
|
|
|
delete from t2; delete from t2;
|
|
|
|
create trigger r1 before delete on t1 for each row begin
|
2004-06-21 10:50:26 +04:00
|
|
|
insert into t2 values (NULL, changes(), NULL);
|
2004-02-21 01:54:25 +03:00
|
|
|
insert into t0 values (5);
|
2004-06-21 10:50:26 +04:00
|
|
|
update t2 set v2=changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
end;
|
|
|
|
delete from t1;
|
2004-06-21 10:50:26 +04:00
|
|
|
select changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
}
|
|
|
|
} {0 1}
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() unchanged upon entry into before delete trigger
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-4.2 {
|
|
|
|
catchsql {
|
|
|
|
select v1 from t2;
|
|
|
|
}
|
|
|
|
} {0 0}
|
|
|
|
|
2004-06-21 10:50:26 +04:00
|
|
|
# changes() changed properly by insert within context of before delete trigger
|
2004-02-21 01:54:25 +03:00
|
|
|
do_test laststmtchanges-4.3 {
|
|
|
|
catchsql {
|
|
|
|
select v2 from t2;
|
|
|
|
}
|
|
|
|
} {0 1}
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# 5.x - complex tests with temporary tables and nested instead of triggers
|
2004-11-22 11:43:32 +03:00
|
|
|
# These tests cannot run if the library does not have view support enabled.
|
|
|
|
|
2005-03-29 07:10:59 +04:00
|
|
|
ifcapable view&&tempdb {
|
2004-02-21 01:54:25 +03:00
|
|
|
|
|
|
|
do_test laststmtchanges-5.1 {
|
|
|
|
catchsql {
|
|
|
|
drop table t0; drop table t1; drop table t2;
|
|
|
|
create temp table t0(x);
|
|
|
|
create temp table t1 (k integer primary key);
|
|
|
|
create temp table t2 (k integer primary key);
|
|
|
|
create temp view v1 as select * from t1;
|
|
|
|
create temp view v2 as select * from t2;
|
|
|
|
create temp table n1 (k integer primary key, n);
|
|
|
|
create temp table n2 (k integer primary key, n);
|
|
|
|
insert into t0 values (1);
|
|
|
|
insert into t0 values (2);
|
|
|
|
insert into t0 values (1);
|
|
|
|
insert into t0 values (1);
|
|
|
|
insert into t0 values (1);
|
|
|
|
insert into t0 values (2);
|
|
|
|
insert into t0 values (2);
|
|
|
|
insert into t0 values (1);
|
|
|
|
create temp trigger r1 instead of insert on v1 for each row begin
|
2004-06-21 10:50:26 +04:00
|
|
|
insert into n1 values (NULL, changes());
|
2004-02-21 01:54:25 +03:00
|
|
|
update t0 set x=x*10 where x=1;
|
2004-06-21 10:50:26 +04:00
|
|
|
insert into n1 values (NULL, changes());
|
2004-02-21 01:54:25 +03:00
|
|
|
insert into t1 values (NEW.k);
|
2004-06-21 10:50:26 +04:00
|
|
|
insert into n1 values (NULL, changes());
|
2004-02-21 01:54:25 +03:00
|
|
|
update t0 set x=x*10 where x=0;
|
|
|
|
insert into v2 values (100+NEW.k);
|
2004-06-21 10:50:26 +04:00
|
|
|
insert into n1 values (NULL, changes());
|
2004-02-21 01:54:25 +03:00
|
|
|
end;
|
|
|
|
create temp trigger r2 instead of insert on v2 for each row begin
|
2004-06-21 10:50:26 +04:00
|
|
|
insert into n2 values (NULL, changes());
|
2004-02-21 01:54:25 +03:00
|
|
|
insert into t2 values (1000+NEW.k);
|
2004-06-21 10:50:26 +04:00
|
|
|
insert into n2 values (NULL, changes());
|
2004-02-21 01:54:25 +03:00
|
|
|
update t0 set x=x*100 where x=0;
|
2004-06-21 10:50:26 +04:00
|
|
|
insert into n2 values (NULL, changes());
|
2004-02-21 01:54:25 +03:00
|
|
|
delete from t0 where x=2;
|
2004-06-21 10:50:26 +04:00
|
|
|
insert into n2 values (NULL, changes());
|
2004-02-21 01:54:25 +03:00
|
|
|
end;
|
|
|
|
insert into t1 values (77);
|
2004-06-21 10:50:26 +04:00
|
|
|
select changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
}
|
|
|
|
} {0 1}
|
|
|
|
|
|
|
|
do_test laststmtchanges-5.2 {
|
|
|
|
catchsql {
|
|
|
|
delete from t1 where k=88;
|
2004-06-21 10:50:26 +04:00
|
|
|
select changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
}
|
|
|
|
} {0 0}
|
|
|
|
|
|
|
|
do_test laststmtchanges-5.3 {
|
|
|
|
catchsql {
|
|
|
|
insert into v1 values (5);
|
2004-06-21 10:50:26 +04:00
|
|
|
select changes();
|
2004-02-21 01:54:25 +03:00
|
|
|
}
|
|
|
|
} {0 0}
|
|
|
|
|
|
|
|
do_test laststmtchanges-5.4 {
|
|
|
|
catchsql {
|
|
|
|
select n from n1;
|
|
|
|
}
|
|
|
|
} {0 {0 5 1 0}}
|
|
|
|
|
|
|
|
do_test laststmtchanges-5.5 {
|
|
|
|
catchsql {
|
|
|
|
select n from n2;
|
|
|
|
}
|
|
|
|
} {0 {0 1 0 3}}
|
|
|
|
|
2004-11-22 11:43:32 +03:00
|
|
|
} ;# ifcapable view
|
|
|
|
|
2008-10-27 16:59:33 +03:00
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# 6.x - Test "DELETE FROM <table>" in the absence of triggers
|
|
|
|
#
|
|
|
|
do_test laststmtchanges-6.1 {
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE t3(a, b, c);
|
|
|
|
INSERT INTO t3 VALUES(1, 2, 3);
|
|
|
|
INSERT INTO t3 VALUES(4, 5, 6);
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
do_test laststmtchanges-6.2 {
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
DELETE FROM t3;
|
|
|
|
SELECT changes();
|
|
|
|
}
|
|
|
|
} {2}
|
|
|
|
do_test laststmtchanges-6.3 {
|
|
|
|
execsql {
|
|
|
|
ROLLBACK;
|
|
|
|
BEGIN;
|
|
|
|
DELETE FROM t3 WHERE a IS NOT NULL;
|
|
|
|
SELECT changes();
|
|
|
|
}
|
|
|
|
} {2}
|
|
|
|
do_test laststmtchanges-6.4 {
|
|
|
|
execsql {
|
|
|
|
ROLLBACK;
|
|
|
|
CREATE INDEX t3_i1 ON t3(a);
|
|
|
|
BEGIN;
|
|
|
|
DELETE FROM t3;
|
|
|
|
SELECT changes();
|
|
|
|
}
|
|
|
|
} {2}
|
|
|
|
do_test laststmtchanges-6.5 {
|
|
|
|
execsql { ROLLBACK }
|
|
|
|
set nTotalChange [execsql {SELECT total_changes()}]
|
|
|
|
expr 0
|
|
|
|
} {0}
|
|
|
|
do_test laststmtchanges-6.6 {
|
|
|
|
execsql {
|
|
|
|
SELECT total_changes();
|
|
|
|
DELETE FROM t3;
|
|
|
|
SELECT total_changes();
|
|
|
|
}
|
|
|
|
} [list $nTotalChange [expr $nTotalChange+2]]
|
|
|
|
|
2004-02-21 01:54:25 +03:00
|
|
|
finish_test
|