xref: /titanic_51/usr/src/lib/libsqlite/test/laststmtchanges.test (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
1
2#pragma ident	"%Z%%M%	%I%	%E% SMI"
3
4# The author disclaims copyright to this source code.  In place of
5# a legal notice, here is a blessing:
6#
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.
10#
11#***********************************************************************
12#
13# Tests to make sure that value returned by last_statement_change_count()
14# (LSCC) is updated properly, especially inside triggers
15#
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)
23#
24
25set testdir [file dirname $argv0]
26source $testdir/tester.tcl
27
28# ----------------------------------------------------------------------------
29# 1.x - basic tests (no triggers)
30
31# LSCC set properly after insert
32do_test laststmtchanges-1.1 {
33    catchsql {
34        create table t0 (x);
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();
44    }
45} {0 1}
46
47# LSCC set properly after update
48do_test laststmtchanges-1.2 {
49    catchsql {
50        update t0 set x=3 where x=1;
51        select last_statement_change_count();
52    }
53} {0 5}
54
55# LSCC unchanged within an update statement
56do_test laststmtchanges-1.3 {
57    catchsql {
58        update t0 set x=x+last_statement_change_count() where x=3;
59        select count() from t0 where x=8;
60    }
61} {0 5}
62
63# LSCC set properly after update on table where no rows changed
64do_test laststmtchanges-1.4 {
65    catchsql {
66        update t0 set x=77 where x=88;
67        select last_statement_change_count();
68    }
69} {0 0}
70
71# LSCC set properly after delete from table
72do_test laststmtchanges-1.5 {
73    catchsql {
74        delete from t0 where x=2;
75        select last_statement_change_count();
76    }
77} {0 3}
78
79# ----------------------------------------------------------------------------
80# 2.x - tests with after insert trigger
81
82# LSCC changed properly after insert into table containing after trigger
83do_test laststmtchanges-2.1 {
84    catchsql {
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);
89            update t0 set x=x;
90            update t2 set v2=last_statement_change_count();
91        end;
92        insert into t1 values (77);
93        select last_statement_change_count();
94    }
95} {0 1}
96
97# LSCC unchanged upon entry into after insert trigger
98do_test laststmtchanges-2.2 {
99    catchsql {
100        select v1 from t2;
101    }
102} {0 3}
103
104# LSCC changed properly by update within context of after insert trigger
105do_test laststmtchanges-2.3 {
106    catchsql {
107        select v2 from t2;
108    }
109} {0 5}
110
111# ----------------------------------------------------------------------------
112# 3.x - tests with after update trigger
113
114# LSCC changed properly after update into table containing after trigger
115do_test laststmtchanges-3.1 {
116    catchsql {
117        drop trigger r1;
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();
123        end;
124        update t1 set k=k;
125        select last_statement_change_count();
126    }
127} {0 1}
128
129# LSCC unchanged upon entry into after update trigger
130do_test laststmtchanges-3.2 {
131    catchsql {
132        select v1 from t2;
133    }
134} {0 0}
135
136# LSCC changed properly by delete within context of after update trigger
137do_test laststmtchanges-3.3 {
138    catchsql {
139        select v2 from t2;
140    }
141} {0 2}
142
143# ----------------------------------------------------------------------------
144# 4.x - tests with before delete trigger
145
146# LSCC changed properly on delete from table containing before trigger
147do_test laststmtchanges-4.1 {
148    catchsql {
149        drop trigger r1;
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();
155        end;
156        delete from t1;
157        select last_statement_change_count();
158    }
159} {0 1}
160
161# LSCC unchanged upon entry into before delete trigger
162do_test laststmtchanges-4.2 {
163    catchsql {
164        select v1 from t2;
165    }
166} {0 0}
167
168# LSCC changed properly by insert within context of before delete trigger
169do_test laststmtchanges-4.3 {
170    catchsql {
171        select v2 from t2;
172    }
173} {0 1}
174
175# ----------------------------------------------------------------------------
176# 5.x - complex tests with temporary tables and nested instead of triggers
177
178do_test laststmtchanges-5.1 {
179    catchsql {
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());
205        end;
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());
214        end;
215        insert into t1 values (77);
216        select last_statement_change_count();
217    }
218} {0 1}
219
220do_test laststmtchanges-5.2 {
221    catchsql {
222        delete from t1 where k=88;
223        select last_statement_change_count();
224    }
225} {0 0}
226
227do_test laststmtchanges-5.3 {
228    catchsql {
229        insert into v1 values (5);
230        select last_statement_change_count();
231    }
232} {0 0}
233
234do_test laststmtchanges-5.4 {
235    catchsql {
236        select n from n1;
237    }
238} {0 {0 5 1 0}}
239
240do_test laststmtchanges-5.5 {
241    catchsql {
242        select n from n2;
243    }
244} {0 {0 1 0 3}}
245
246finish_test
247
248