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