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_insert_rowid() (LIRID)
14 # is updated properly, especially inside triggers
16 # Note 1: insert into table is now the only statement which changes LIRID
17 # Note 2: upon entry into before or instead of triggers,
18 # LIRID is unchanged (rather than -1)
19 # Note 3: LIRID is changed within the context of a trigger,
20 # but is restored once the trigger exits
21 # Note 4: LIRID is not changed by an insert into 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 # LIRID changed properly after an insert into a table
32 do_test lastinsert-1.1 {
34 create table t1 (k integer primary key);
35 insert into t1 values (1);
36 insert into t1 values (NULL);
37 insert into t1 values (NULL);
38 select last_insert_rowid();
42 # LIRID unchanged after an update on a table
43 do_test lastinsert-1.2 {
45 update t1 set k=4 where k=2;
46 select last_insert_rowid();
50 # LIRID unchanged after a delete from a table
51 do_test lastinsert-1.3 {
53 delete from t1 where k=4;
54 select last_insert_rowid();
58 # LIRID unchanged after create table/view statements
59 do_test lastinsert-1.4 {
61 create table t2 (k integer primary key, val1, val2, val3);
62 create view v as select * from t1;
63 select last_insert_rowid();
67 # ----------------------------------------------------------------------------
68 # 2.x - tests with after insert trigger
70 # LIRID changed properly after an insert into table containing an after trigger
71 do_test lastinsert-2.1 {
74 create trigger r1 after insert on t1 for each row begin
75 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL);
76 update t2 set k=k+10, val2=100+last_insert_rowid();
77 update t2 set val3=1000+last_insert_rowid();
79 insert into t1 values (13);
80 select last_insert_rowid();
84 # LIRID equals NEW.k upon entry into after insert trigger
85 do_test lastinsert-2.2 {
91 # LIRID changed properly by insert within context of after insert trigger
92 do_test lastinsert-2.3 {
98 # LIRID unchanged by update within context of after insert trigger
99 do_test lastinsert-2.4 {
105 # ----------------------------------------------------------------------------
106 # 3.x - tests with after update trigger
108 # LIRID not changed after an update onto a table containing an after trigger
109 do_test lastinsert-3.1 {
113 create trigger r1 after update on t1 for each row begin
114 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL);
115 update t2 set k=k+10, val2=100+last_insert_rowid();
116 update t2 set val3=1000+last_insert_rowid();
118 update t1 set k=14 where k=3;
119 select last_insert_rowid();
123 # LIRID unchanged upon entry into after update trigger
124 do_test lastinsert-3.2 {
130 # LIRID changed properly by insert within context of after update trigger
131 do_test lastinsert-3.3 {
137 # LIRID unchanged by update within context of after update trigger
138 do_test lastinsert-3.4 {
144 # ----------------------------------------------------------------------------
145 # 4.x - tests with instead of insert trigger
147 # LIRID not changed after an insert into view containing an instead of trigger
148 do_test lastinsert-4.1 {
152 create trigger r1 instead of insert on v for each row begin
153 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL);
154 update t2 set k=k+10, val2=100+last_insert_rowid();
155 update t2 set val3=1000+last_insert_rowid();
157 insert into v values (15);
158 select last_insert_rowid();
162 # LIRID unchanged upon entry into instead of trigger
163 do_test lastinsert-4.2 {
169 # LIRID changed properly by insert within context of instead of trigger
170 do_test lastinsert-4.3 {
176 # LIRID unchanged by update within context of instead of trigger
177 do_test lastinsert-4.4 {
183 # ----------------------------------------------------------------------------
184 # 5.x - tests with before delete trigger
186 # LIRID not changed after a delete on a table containing a before trigger
187 do_test lastinsert-5.1 {
191 create trigger r1 before delete on t1 for each row begin
192 insert into t2 values (77, last_insert_rowid(), NULL, NULL);
193 update t2 set k=k+10, val2=100+last_insert_rowid();
194 update t2 set val3=1000+last_insert_rowid();
196 delete from t1 where k=1;
197 select last_insert_rowid();
201 # LIRID unchanged upon entry into delete trigger
202 do_test lastinsert-5.2 {
208 # LIRID changed properly by insert within context of delete trigger
209 do_test lastinsert-5.3 {
215 # LIRID unchanged by update within context of delete trigger
216 do_test lastinsert-5.4 {
222 # ----------------------------------------------------------------------------
223 # 6.x - tests with instead of update trigger
225 # LIRID not changed after an update on a view containing an instead of trigger
226 do_test lastinsert-6.1 {
230 create trigger r1 instead of update on v for each row begin
231 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL);
232 update t2 set k=k+10, val2=100+last_insert_rowid();
233 update t2 set val3=1000+last_insert_rowid();
235 update v set k=16 where k=14;
236 select last_insert_rowid();
240 # LIRID unchanged upon entry into instead of trigger
241 do_test lastinsert-6.2 {
247 # LIRID changed properly by insert within context of instead of trigger
248 do_test lastinsert-6.3 {
254 # LIRID unchanged by update within context of instead of trigger
255 do_test lastinsert-6.4 {
261 # ----------------------------------------------------------------------------
262 # 7.x - complex tests with temporary tables and nested instead of triggers
264 do_test lastinsert-7.1 {
266 drop table t1; drop table t2; drop trigger r1;
267 create temp table t1 (k integer primary key);
268 create temp table t2 (k integer primary key);
269 create temp view v1 as select * from t1;
270 create temp view v2 as select * from t2;
271 create temp table rid (k integer primary key, rin, rout);
272 insert into rid values (1, NULL, NULL);
273 insert into rid values (2, NULL, NULL);
274 create temp trigger r1 instead of insert on v1 for each row begin
275 update rid set rin=last_insert_rowid() where k=1;
276 insert into t1 values (100+NEW.k);
277 insert into v2 values (100+last_insert_rowid());
278 update rid set rout=last_insert_rowid() where k=1;
280 create temp trigger r2 instead of insert on v2 for each row begin
281 update rid set rin=last_insert_rowid() where k=2;
282 insert into t2 values (1000+NEW.k);
283 update rid set rout=last_insert_rowid() where k=2;
285 insert into t1 values (77);
286 select last_insert_rowid();
290 do_test lastinsert-7.2 {
292 insert into v1 values (5);
293 select last_insert_rowid();
297 do_test lastinsert-7.3 {
299 select rin from rid where k=1;
303 do_test lastinsert-7.4 {
305 select rout from rid where k=1;
309 do_test lastinsert-7.5 {
311 select rin from rid where k=2;
315 do_test lastinsert-7.6 {
317 select rout from rid where k=2;