xref: /illumos-gate/usr/src/lib/libsqlite/test/laststmtchanges.test (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1#
2# The author disclaims copyright to this source code.  In place of
3# a legal notice, here is a blessing:
4#
5#    May you do good and not evil.
6#    May you find forgiveness for yourself and forgive others.
7#    May you share freely, never taking more than you give.
8#
9#***********************************************************************
10#
11# Tests to make sure that value returned by last_statement_change_count()
12# (LSCC) is updated properly, especially inside triggers
13#
14# Note 1: LSCC remains constant within a statement and only updates once
15#           the statement is finished (triggers count as part of statement)
16# Note 2: LSCC is changed within the context of a trigger
17#           much like last_insert_rowid() (see lastinsert.test),
18#           but is restored once the trigger exits
19# Note 3: LSCC is not changed by a change to a view (since everything
20#           is done within instead of trigger context)
21#
22
23set testdir [file dirname $argv0]
24source $testdir/tester.tcl
25
26# ----------------------------------------------------------------------------
27# 1.x - basic tests (no triggers)
28
29# LSCC set properly after insert
30do_test laststmtchanges-1.1 {
31    catchsql {
32        create table t0 (x);
33        insert into t0 values (1);
34        insert into t0 values (1);
35        insert into t0 values (2);
36        insert into t0 values (2);
37        insert into t0 values (1);
38        insert into t0 values (1);
39        insert into t0 values (1);
40        insert into t0 values (2);
41        select last_statement_change_count();
42    }
43} {0 1}
44
45# LSCC set properly after update
46do_test laststmtchanges-1.2 {
47    catchsql {
48        update t0 set x=3 where x=1;
49        select last_statement_change_count();
50    }
51} {0 5}
52
53# LSCC unchanged within an update statement
54do_test laststmtchanges-1.3 {
55    catchsql {
56        update t0 set x=x+last_statement_change_count() where x=3;
57        select count() from t0 where x=8;
58    }
59} {0 5}
60
61# LSCC set properly after update on table where no rows changed
62do_test laststmtchanges-1.4 {
63    catchsql {
64        update t0 set x=77 where x=88;
65        select last_statement_change_count();
66    }
67} {0 0}
68
69# LSCC set properly after delete from table
70do_test laststmtchanges-1.5 {
71    catchsql {
72        delete from t0 where x=2;
73        select last_statement_change_count();
74    }
75} {0 3}
76
77# ----------------------------------------------------------------------------
78# 2.x - tests with after insert trigger
79
80# LSCC changed properly after insert into table containing after trigger
81do_test laststmtchanges-2.1 {
82    catchsql {
83        create table t1 (k integer primary key);
84        create table t2 (k integer primary key, v1, v2);
85        create trigger r1 after insert on t1 for each row begin
86            insert into t2 values (NULL, last_statement_change_count(), NULL);
87            update t0 set x=x;
88            update t2 set v2=last_statement_change_count();
89        end;
90        insert into t1 values (77);
91        select last_statement_change_count();
92    }
93} {0 1}
94
95# LSCC unchanged upon entry into after insert trigger
96do_test laststmtchanges-2.2 {
97    catchsql {
98        select v1 from t2;
99    }
100} {0 3}
101
102# LSCC changed properly by update within context of after insert trigger
103do_test laststmtchanges-2.3 {
104    catchsql {
105        select v2 from t2;
106    }
107} {0 5}
108
109# ----------------------------------------------------------------------------
110# 3.x - tests with after update trigger
111
112# LSCC changed properly after update into table containing after trigger
113do_test laststmtchanges-3.1 {
114    catchsql {
115        drop trigger r1;
116        delete from t2; delete from t2;
117        create trigger r1 after update on t1 for each row begin
118            insert into t2 values (NULL, last_statement_change_count(), NULL);
119            delete from t0 where oid=1 or oid=2;
120            update t2 set v2=last_statement_change_count();
121        end;
122        update t1 set k=k;
123        select last_statement_change_count();
124    }
125} {0 1}
126
127# LSCC unchanged upon entry into after update trigger
128do_test laststmtchanges-3.2 {
129    catchsql {
130        select v1 from t2;
131    }
132} {0 0}
133
134# LSCC changed properly by delete within context of after update trigger
135do_test laststmtchanges-3.3 {
136    catchsql {
137        select v2 from t2;
138    }
139} {0 2}
140
141# ----------------------------------------------------------------------------
142# 4.x - tests with before delete trigger
143
144# LSCC changed properly on delete from table containing before trigger
145do_test laststmtchanges-4.1 {
146    catchsql {
147        drop trigger r1;
148        delete from t2; delete from t2;
149        create trigger r1 before delete on t1 for each row begin
150            insert into t2 values (NULL, last_statement_change_count(), NULL);
151            insert into t0 values (5);
152            update t2 set v2=last_statement_change_count();
153        end;
154        delete from t1;
155        select last_statement_change_count();
156    }
157} {0 1}
158
159# LSCC unchanged upon entry into before delete trigger
160do_test laststmtchanges-4.2 {
161    catchsql {
162        select v1 from t2;
163    }
164} {0 0}
165
166# LSCC changed properly by insert within context of before delete trigger
167do_test laststmtchanges-4.3 {
168    catchsql {
169        select v2 from t2;
170    }
171} {0 1}
172
173# ----------------------------------------------------------------------------
174# 5.x - complex tests with temporary tables and nested instead of triggers
175
176do_test laststmtchanges-5.1 {
177    catchsql {
178        drop table t0; drop table t1; drop table t2;
179        create temp table t0(x);
180        create temp table t1 (k integer primary key);
181        create temp table t2 (k integer primary key);
182        create temp view v1 as select * from t1;
183        create temp view v2 as select * from t2;
184        create temp table n1 (k integer primary key, n);
185        create temp table n2 (k integer primary key, n);
186        insert into t0 values (1);
187        insert into t0 values (2);
188        insert into t0 values (1);
189        insert into t0 values (1);
190        insert into t0 values (1);
191        insert into t0 values (2);
192        insert into t0 values (2);
193        insert into t0 values (1);
194        create temp trigger r1 instead of insert on v1 for each row begin
195            insert into n1 values (NULL, last_statement_change_count());
196            update t0 set x=x*10 where x=1;
197            insert into n1 values (NULL, last_statement_change_count());
198            insert into t1 values (NEW.k);
199            insert into n1 values (NULL, last_statement_change_count());
200            update t0 set x=x*10 where x=0;
201            insert into v2 values (100+NEW.k);
202            insert into n1 values (NULL, last_statement_change_count());
203        end;
204        create temp trigger r2 instead of insert on v2 for each row begin
205            insert into n2 values (NULL, last_statement_change_count());
206            insert into t2 values (1000+NEW.k);
207            insert into n2 values (NULL, last_statement_change_count());
208            update t0 set x=x*100 where x=0;
209            insert into n2 values (NULL, last_statement_change_count());
210            delete from t0 where x=2;
211            insert into n2 values (NULL, last_statement_change_count());
212        end;
213        insert into t1 values (77);
214        select last_statement_change_count();
215    }
216} {0 1}
217
218do_test laststmtchanges-5.2 {
219    catchsql {
220        delete from t1 where k=88;
221        select last_statement_change_count();
222    }
223} {0 0}
224
225do_test laststmtchanges-5.3 {
226    catchsql {
227        insert into v1 values (5);
228        select last_statement_change_count();
229    }
230} {0 0}
231
232do_test laststmtchanges-5.4 {
233    catchsql {
234        select n from n1;
235    }
236} {0 {0 5 1 0}}
237
238do_test laststmtchanges-5.5 {
239    catchsql {
240        select n from n2;
241    }
242} {0 {0 1 0 3}}
243
244finish_test
245
246