1 # The author disclaims copyright to this source code. In place of
2 # a legal notice, here is a blessing:
4 # May you do good and not evil.
5 # May you find forgiveness for yourself and forgive others.
6 # May you share freely, never taking more than you give.
8 #***********************************************************************
10 # Tests to make sure that value returned by last_insert_rowid() (LIRID)
11 # is updated properly, especially inside triggers
13 # Note 1: insert into table is now the only statement which changes LIRID
14 # Note 2: upon entry into before or instead of triggers,
15 # LIRID is unchanged (rather than -1)
16 # Note 3: LIRID is changed within the context of a trigger,
17 # but is restored once the trigger exits
18 # Note 4: LIRID is not changed by an insert into a view (since everything
19 # is done within instead of trigger context)
22 set testdir [file dirname $argv0]
23 source $testdir/tester.tcl
25 # ----------------------------------------------------------------------------
26 # 1.x - basic tests (no triggers)
28 # LIRID changed properly after an insert into a table
29 do_test lastinsert-1.1 {
31 create table t1 (k integer primary key);
32 insert into t1 values (1);
33 insert into t1 values (NULL);
34 insert into t1 values (NULL);
35 select last_insert_rowid();
39 # LIRID unchanged after an update on a table
40 do_test lastinsert-1.2 {
42 update t1 set k=4 where k=2;
43 select last_insert_rowid();
47 # LIRID unchanged after a delete from a table
48 do_test lastinsert-1.3 {
50 delete from t1 where k=4;
51 select last_insert_rowid();
55 # LIRID unchanged after create table/view statements
56 do_test lastinsert-1.4.1 {
58 create table t2 (k integer primary key, val1, val2, val3);
59 select last_insert_rowid();
63 do_test lastinsert-1.4.2 {
65 create view v as select * from t1;
66 select last_insert_rowid();
71 # All remaining tests involve triggers. Skip them if triggers are not
72 # supported in this build.
74 ifcapable {!trigger} {
79 # ----------------------------------------------------------------------------
80 # 2.x - tests with after insert trigger
82 # LIRID changed properly after an insert into table containing an after trigger
83 do_test lastinsert-2.1 {
86 create trigger r1 after insert on t1 for each row begin
87 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL);
88 update t2 set k=k+10, val2=100+last_insert_rowid();
89 update t2 set val3=1000+last_insert_rowid();
91 insert into t1 values (13);
92 select last_insert_rowid();
96 # LIRID equals NEW.k upon entry into after insert trigger
97 do_test lastinsert-2.2 {
103 # LIRID changed properly by insert within context of after insert trigger
104 do_test lastinsert-2.3 {
110 # LIRID unchanged by update within context of after insert trigger
111 do_test lastinsert-2.4 {
117 # ----------------------------------------------------------------------------
118 # 3.x - tests with after update trigger
120 # LIRID not changed after an update onto a table containing an after trigger
121 do_test lastinsert-3.1 {
125 create trigger r1 after update on t1 for each row begin
126 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL);
127 update t2 set k=k+10, val2=100+last_insert_rowid();
128 update t2 set val3=1000+last_insert_rowid();
130 update t1 set k=14 where k=3;
131 select last_insert_rowid();
135 # LIRID unchanged upon entry into after update trigger
136 do_test lastinsert-3.2 {
142 # LIRID changed properly by insert within context of after update trigger
143 do_test lastinsert-3.3 {
149 # LIRID unchanged by update within context of after update trigger
150 do_test lastinsert-3.4 {
156 # ----------------------------------------------------------------------------
157 # 4.x - tests with instead of insert trigger
158 # These may not be run if either views or triggers were disabled at
161 ifcapable {view && trigger} {
162 # LIRID not changed after an insert into view containing an instead of trigger
163 do_test lastinsert-4.1 {
167 create trigger r1 instead of insert on v for each row begin
168 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL);
169 update t2 set k=k+10, val2=100+last_insert_rowid();
170 update t2 set val3=1000+last_insert_rowid();
172 insert into v values (15);
173 select last_insert_rowid();
177 # LIRID unchanged upon entry into instead of trigger
178 do_test lastinsert-4.2 {
184 # LIRID changed properly by insert within context of instead of trigger
185 do_test lastinsert-4.3 {
191 # LIRID unchanged by update within context of instead of trigger
192 do_test lastinsert-4.4 {
197 } ;# ifcapable (view && trigger)
199 # ----------------------------------------------------------------------------
200 # 5.x - tests with before delete trigger
202 # LIRID not changed after a delete on a table containing a before trigger
203 do_test lastinsert-5.1 {
205 drop trigger r1; -- This was not created if views are disabled.
209 create trigger r1 before delete on t1 for each row begin
210 insert into t2 values (77, last_insert_rowid(), NULL, NULL);
211 update t2 set k=k+10, val2=100+last_insert_rowid();
212 update t2 set val3=1000+last_insert_rowid();
214 delete from t1 where k=1;
215 select last_insert_rowid();
219 # LIRID unchanged upon entry into delete trigger
220 do_test lastinsert-5.2 {
226 # LIRID changed properly by insert within context of delete trigger
227 do_test lastinsert-5.3 {
233 # LIRID unchanged by update within context of delete trigger
234 do_test lastinsert-5.4 {
240 # ----------------------------------------------------------------------------
241 # 6.x - tests with instead of update trigger
242 # These tests may not run if either views or triggers are disabled.
244 ifcapable {view && trigger} {
245 # LIRID not changed after an update on a view containing an instead of trigger
246 do_test lastinsert-6.1 {
250 create trigger r1 instead of update on v for each row begin
251 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL);
252 update t2 set k=k+10, val2=100+last_insert_rowid();
253 update t2 set val3=1000+last_insert_rowid();
255 update v set k=16 where k=14;
256 select last_insert_rowid();
260 # LIRID unchanged upon entry into instead of trigger
261 do_test lastinsert-6.2 {
267 # LIRID changed properly by insert within context of instead of trigger
268 do_test lastinsert-6.3 {
274 # LIRID unchanged by update within context of instead of trigger
275 do_test lastinsert-6.4 {
280 } ;# ifcapable (view && trigger)
282 # ----------------------------------------------------------------------------
283 # 7.x - complex tests with temporary tables and nested instead of triggers
284 # These do not run if views or triggers are disabled.
286 ifcapable {trigger && view && tempdb} {
287 do_test lastinsert-7.1 {
289 drop table t1; drop table t2; drop trigger r1;
290 create temp table t1 (k integer primary key);
291 create temp table t2 (k integer primary key);
292 create temp view v1 as select * from t1;
293 create temp view v2 as select * from t2;
294 create temp table rid (k integer primary key, rin, rout);
295 insert into rid values (1, NULL, NULL);
296 insert into rid values (2, NULL, NULL);
297 create temp trigger r1 instead of insert on v1 for each row begin
298 update rid set rin=last_insert_rowid() where k=1;
299 insert into t1 values (100+NEW.k);
300 insert into v2 values (100+last_insert_rowid());
301 update rid set rout=last_insert_rowid() where k=1;
303 create temp trigger r2 instead of insert on v2 for each row begin
304 update rid set rin=last_insert_rowid() where k=2;
305 insert into t2 values (1000+NEW.k);
306 update rid set rout=last_insert_rowid() where k=2;
308 insert into t1 values (77);
309 select last_insert_rowid();
313 do_test lastinsert-7.2 {
315 insert into v1 values (5);
316 select last_insert_rowid();
320 do_test lastinsert-7.3 {
322 select rin from rid where k=1;
326 do_test lastinsert-7.4 {
328 select rout from rid where k=1;
332 do_test lastinsert-7.5 {
334 select rin from rid where k=2;
338 do_test lastinsert-7.6 {
340 select rout from rid where k=2;
344 do_test lastinsert-8.1 {
348 CREATE TABLE t2(x INTEGER PRIMARY KEY, y);
349 CREATE TABLE t3(a, b);
350 CREATE TRIGGER after_t2 AFTER INSERT ON t2 BEGIN
351 INSERT INTO t3 VALUES(new.x, new.y);
353 INSERT INTO t2 VALUES(5000000000, 1);
354 SELECT last_insert_rowid();
358 do_test lastinsert-9.1 {
359 db eval {INSERT INTO t2 VALUES(123456789012345,0)}
364 } ;# ifcapable (view && trigger)