2 #pragma ident "%Z%%M% %I% %E% SMI"
4 # The author disclaims copyright to this source code. In place of
5 # a legal notice, here is a blessing:
7 # May you do good and not evil.
8 # May you find forgiveness for yourself and forgive others.
9 # May you share freely, never taking more than you give.
11 #***********************************************************************
13 # Tests to make sure that value returned by last_statement_change_count()
14 # (LSCC) is updated properly, especially inside triggers
16 # Note 1: LSCC remains constant within a statement and only updates once
17 # the statement is finished (triggers count as part of statement)
18 # Note 2: LSCC is changed within the context of a trigger
19 # much like last_insert_rowid() (see lastinsert.test),
20 # but is restored once the trigger exits
21 # Note 3: LSCC is not changed by a change to a view (since everything
22 # is done within instead of trigger context)
25 set testdir [file dirname $argv0]
26 source $testdir/tester.tcl
28 # ----------------------------------------------------------------------------
29 # 1.x - basic tests (no triggers)
31 # LSCC set properly after insert
32 do_test laststmtchanges-1.1 {
35 insert into t0 values (1);
36 insert into t0 values (1);
37 insert into t0 values (2);
38 insert into t0 values (2);
39 insert into t0 values (1);
40 insert into t0 values (1);
41 insert into t0 values (1);
42 insert into t0 values (2);
43 select last_statement_change_count();
47 # LSCC set properly after update
48 do_test laststmtchanges-1.2 {
50 update t0 set x=3 where x=1;
51 select last_statement_change_count();
55 # LSCC unchanged within an update statement
56 do_test laststmtchanges-1.3 {
58 update t0 set x=x+last_statement_change_count() where x=3;
59 select count() from t0 where x=8;
63 # LSCC set properly after update on table where no rows changed
64 do_test laststmtchanges-1.4 {
66 update t0 set x=77 where x=88;
67 select last_statement_change_count();
71 # LSCC set properly after delete from table
72 do_test laststmtchanges-1.5 {
74 delete from t0 where x=2;
75 select last_statement_change_count();
79 # ----------------------------------------------------------------------------
80 # 2.x - tests with after insert trigger
82 # LSCC changed properly after insert into table containing after trigger
83 do_test laststmtchanges-2.1 {
85 create table t1 (k integer primary key);
86 create table t2 (k integer primary key, v1, v2);
87 create trigger r1 after insert on t1 for each row begin
88 insert into t2 values (NULL, last_statement_change_count(), NULL);
90 update t2 set v2=last_statement_change_count();
92 insert into t1 values (77);
93 select last_statement_change_count();
97 # LSCC unchanged upon entry into after insert trigger
98 do_test laststmtchanges-2.2 {
104 # LSCC changed properly by update within context of after insert trigger
105 do_test laststmtchanges-2.3 {
111 # ----------------------------------------------------------------------------
112 # 3.x - tests with after update trigger
114 # LSCC changed properly after update into table containing after trigger
115 do_test laststmtchanges-3.1 {
118 delete from t2; delete from t2;
119 create trigger r1 after update on t1 for each row begin
120 insert into t2 values (NULL, last_statement_change_count(), NULL);
121 delete from t0 where oid=1 or oid=2;
122 update t2 set v2=last_statement_change_count();
125 select last_statement_change_count();
129 # LSCC unchanged upon entry into after update trigger
130 do_test laststmtchanges-3.2 {
136 # LSCC changed properly by delete within context of after update trigger
137 do_test laststmtchanges-3.3 {
143 # ----------------------------------------------------------------------------
144 # 4.x - tests with before delete trigger
146 # LSCC changed properly on delete from table containing before trigger
147 do_test laststmtchanges-4.1 {
150 delete from t2; delete from t2;
151 create trigger r1 before delete on t1 for each row begin
152 insert into t2 values (NULL, last_statement_change_count(), NULL);
153 insert into t0 values (5);
154 update t2 set v2=last_statement_change_count();
157 select last_statement_change_count();
161 # LSCC unchanged upon entry into before delete trigger
162 do_test laststmtchanges-4.2 {
168 # LSCC changed properly by insert within context of before delete trigger
169 do_test laststmtchanges-4.3 {
175 # ----------------------------------------------------------------------------
176 # 5.x - complex tests with temporary tables and nested instead of triggers
178 do_test laststmtchanges-5.1 {
180 drop table t0; drop table t1; drop table t2;
181 create temp table t0(x);
182 create temp table t1 (k integer primary key);
183 create temp table t2 (k integer primary key);
184 create temp view v1 as select * from t1;
185 create temp view v2 as select * from t2;
186 create temp table n1 (k integer primary key, n);
187 create temp table n2 (k integer primary key, n);
188 insert into t0 values (1);
189 insert into t0 values (2);
190 insert into t0 values (1);
191 insert into t0 values (1);
192 insert into t0 values (1);
193 insert into t0 values (2);
194 insert into t0 values (2);
195 insert into t0 values (1);
196 create temp trigger r1 instead of insert on v1 for each row begin
197 insert into n1 values (NULL, last_statement_change_count());
198 update t0 set x=x*10 where x=1;
199 insert into n1 values (NULL, last_statement_change_count());
200 insert into t1 values (NEW.k);
201 insert into n1 values (NULL, last_statement_change_count());
202 update t0 set x=x*10 where x=0;
203 insert into v2 values (100+NEW.k);
204 insert into n1 values (NULL, last_statement_change_count());
206 create temp trigger r2 instead of insert on v2 for each row begin
207 insert into n2 values (NULL, last_statement_change_count());
208 insert into t2 values (1000+NEW.k);
209 insert into n2 values (NULL, last_statement_change_count());
210 update t0 set x=x*100 where x=0;
211 insert into n2 values (NULL, last_statement_change_count());
212 delete from t0 where x=2;
213 insert into n2 values (NULL, last_statement_change_count());
215 insert into t1 values (77);
216 select last_statement_change_count();
220 do_test laststmtchanges-5.2 {
222 delete from t1 where k=88;
223 select last_statement_change_count();
227 do_test laststmtchanges-5.3 {
229 insert into v1 values (5);
230 select last_statement_change_count();
234 do_test laststmtchanges-5.4 {
240 do_test laststmtchanges-5.5 {