sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # sl@0: # Tests to make sure that value returned by last_insert_rowid() (LIRID) sl@0: # is updated properly, especially inside triggers sl@0: # sl@0: # Note 1: insert into table is now the only statement which changes LIRID sl@0: # Note 2: upon entry into before or instead of triggers, sl@0: # LIRID is unchanged (rather than -1) sl@0: # Note 3: LIRID is changed within the context of a trigger, sl@0: # but is restored once the trigger exits sl@0: # Note 4: LIRID is not changed by an insert into a view (since everything sl@0: # is done within instead of trigger context) sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # ---------------------------------------------------------------------------- sl@0: # 1.x - basic tests (no triggers) sl@0: sl@0: # LIRID changed properly after an insert into a table sl@0: do_test lastinsert-1.1 { sl@0: catchsql { sl@0: create table t1 (k integer primary key); sl@0: insert into t1 values (1); sl@0: insert into t1 values (NULL); sl@0: insert into t1 values (NULL); sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 3} sl@0: sl@0: # LIRID unchanged after an update on a table sl@0: do_test lastinsert-1.2 { sl@0: catchsql { sl@0: update t1 set k=4 where k=2; sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 3} sl@0: sl@0: # LIRID unchanged after a delete from a table sl@0: do_test lastinsert-1.3 { sl@0: catchsql { sl@0: delete from t1 where k=4; sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 3} sl@0: sl@0: # LIRID unchanged after create table/view statements sl@0: do_test lastinsert-1.4.1 { sl@0: catchsql { sl@0: create table t2 (k integer primary key, val1, val2, val3); sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 3} sl@0: ifcapable view { sl@0: do_test lastinsert-1.4.2 { sl@0: catchsql { sl@0: create view v as select * from t1; sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 3} sl@0: } ;# ifcapable view sl@0: sl@0: # All remaining tests involve triggers. Skip them if triggers are not sl@0: # supported in this build. sl@0: # sl@0: ifcapable {!trigger} { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # ---------------------------------------------------------------------------- sl@0: # 2.x - tests with after insert trigger sl@0: sl@0: # LIRID changed properly after an insert into table containing an after trigger sl@0: do_test lastinsert-2.1 { sl@0: catchsql { sl@0: delete from t2; sl@0: create trigger r1 after insert on t1 for each row begin sl@0: insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL); sl@0: update t2 set k=k+10, val2=100+last_insert_rowid(); sl@0: update t2 set val3=1000+last_insert_rowid(); sl@0: end; sl@0: insert into t1 values (13); sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 13} sl@0: sl@0: # LIRID equals NEW.k upon entry into after insert trigger sl@0: do_test lastinsert-2.2 { sl@0: catchsql { sl@0: select val1 from t2; sl@0: } sl@0: } {0 13} sl@0: sl@0: # LIRID changed properly by insert within context of after insert trigger sl@0: do_test lastinsert-2.3 { sl@0: catchsql { sl@0: select val2 from t2; sl@0: } sl@0: } {0 126} sl@0: sl@0: # LIRID unchanged by update within context of after insert trigger sl@0: do_test lastinsert-2.4 { sl@0: catchsql { sl@0: select val3 from t2; sl@0: } sl@0: } {0 1026} sl@0: sl@0: # ---------------------------------------------------------------------------- sl@0: # 3.x - tests with after update trigger sl@0: sl@0: # LIRID not changed after an update onto a table containing an after trigger sl@0: do_test lastinsert-3.1 { sl@0: catchsql { sl@0: delete from t2; sl@0: drop trigger r1; sl@0: create trigger r1 after update on t1 for each row begin sl@0: insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL); sl@0: update t2 set k=k+10, val2=100+last_insert_rowid(); sl@0: update t2 set val3=1000+last_insert_rowid(); sl@0: end; sl@0: update t1 set k=14 where k=3; sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 13} sl@0: sl@0: # LIRID unchanged upon entry into after update trigger sl@0: do_test lastinsert-3.2 { sl@0: catchsql { sl@0: select val1 from t2; sl@0: } sl@0: } {0 13} sl@0: sl@0: # LIRID changed properly by insert within context of after update trigger sl@0: do_test lastinsert-3.3 { sl@0: catchsql { sl@0: select val2 from t2; sl@0: } sl@0: } {0 128} sl@0: sl@0: # LIRID unchanged by update within context of after update trigger sl@0: do_test lastinsert-3.4 { sl@0: catchsql { sl@0: select val3 from t2; sl@0: } sl@0: } {0 1028} sl@0: sl@0: # ---------------------------------------------------------------------------- sl@0: # 4.x - tests with instead of insert trigger sl@0: # These may not be run if either views or triggers were disabled at sl@0: # compile-time sl@0: sl@0: ifcapable {view && trigger} { sl@0: # LIRID not changed after an insert into view containing an instead of trigger sl@0: do_test lastinsert-4.1 { sl@0: catchsql { sl@0: delete from t2; sl@0: drop trigger r1; sl@0: create trigger r1 instead of insert on v for each row begin sl@0: insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL); sl@0: update t2 set k=k+10, val2=100+last_insert_rowid(); sl@0: update t2 set val3=1000+last_insert_rowid(); sl@0: end; sl@0: insert into v values (15); sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 13} sl@0: sl@0: # LIRID unchanged upon entry into instead of trigger sl@0: do_test lastinsert-4.2 { sl@0: catchsql { sl@0: select val1 from t2; sl@0: } sl@0: } {0 13} sl@0: sl@0: # LIRID changed properly by insert within context of instead of trigger sl@0: do_test lastinsert-4.3 { sl@0: catchsql { sl@0: select val2 from t2; sl@0: } sl@0: } {0 130} sl@0: sl@0: # LIRID unchanged by update within context of instead of trigger sl@0: do_test lastinsert-4.4 { sl@0: catchsql { sl@0: select val3 from t2; sl@0: } sl@0: } {0 1030} sl@0: } ;# ifcapable (view && trigger) sl@0: sl@0: # ---------------------------------------------------------------------------- sl@0: # 5.x - tests with before delete trigger sl@0: sl@0: # LIRID not changed after a delete on a table containing a before trigger sl@0: do_test lastinsert-5.1 { sl@0: catchsql { sl@0: drop trigger r1; -- This was not created if views are disabled. sl@0: } sl@0: catchsql { sl@0: delete from t2; sl@0: create trigger r1 before delete on t1 for each row begin sl@0: insert into t2 values (77, last_insert_rowid(), NULL, NULL); sl@0: update t2 set k=k+10, val2=100+last_insert_rowid(); sl@0: update t2 set val3=1000+last_insert_rowid(); sl@0: end; sl@0: delete from t1 where k=1; sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 13} sl@0: sl@0: # LIRID unchanged upon entry into delete trigger sl@0: do_test lastinsert-5.2 { sl@0: catchsql { sl@0: select val1 from t2; sl@0: } sl@0: } {0 13} sl@0: sl@0: # LIRID changed properly by insert within context of delete trigger sl@0: do_test lastinsert-5.3 { sl@0: catchsql { sl@0: select val2 from t2; sl@0: } sl@0: } {0 177} sl@0: sl@0: # LIRID unchanged by update within context of delete trigger sl@0: do_test lastinsert-5.4 { sl@0: catchsql { sl@0: select val3 from t2; sl@0: } sl@0: } {0 1077} sl@0: sl@0: # ---------------------------------------------------------------------------- sl@0: # 6.x - tests with instead of update trigger sl@0: # These tests may not run if either views or triggers are disabled. sl@0: sl@0: ifcapable {view && trigger} { sl@0: # LIRID not changed after an update on a view containing an instead of trigger sl@0: do_test lastinsert-6.1 { sl@0: catchsql { sl@0: delete from t2; sl@0: drop trigger r1; sl@0: create trigger r1 instead of update on v for each row begin sl@0: insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL); sl@0: update t2 set k=k+10, val2=100+last_insert_rowid(); sl@0: update t2 set val3=1000+last_insert_rowid(); sl@0: end; sl@0: update v set k=16 where k=14; sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 13} sl@0: sl@0: # LIRID unchanged upon entry into instead of trigger sl@0: do_test lastinsert-6.2 { sl@0: catchsql { sl@0: select val1 from t2; sl@0: } sl@0: } {0 13} sl@0: sl@0: # LIRID changed properly by insert within context of instead of trigger sl@0: do_test lastinsert-6.3 { sl@0: catchsql { sl@0: select val2 from t2; sl@0: } sl@0: } {0 132} sl@0: sl@0: # LIRID unchanged by update within context of instead of trigger sl@0: do_test lastinsert-6.4 { sl@0: catchsql { sl@0: select val3 from t2; sl@0: } sl@0: } {0 1032} sl@0: } ;# ifcapable (view && trigger) sl@0: sl@0: # ---------------------------------------------------------------------------- sl@0: # 7.x - complex tests with temporary tables and nested instead of triggers sl@0: # These do not run if views or triggers are disabled. sl@0: sl@0: ifcapable {trigger && view && tempdb} { sl@0: do_test lastinsert-7.1 { sl@0: catchsql { sl@0: drop table t1; drop table t2; drop trigger r1; sl@0: create temp table t1 (k integer primary key); sl@0: create temp table t2 (k integer primary key); sl@0: create temp view v1 as select * from t1; sl@0: create temp view v2 as select * from t2; sl@0: create temp table rid (k integer primary key, rin, rout); sl@0: insert into rid values (1, NULL, NULL); sl@0: insert into rid values (2, NULL, NULL); sl@0: create temp trigger r1 instead of insert on v1 for each row begin sl@0: update rid set rin=last_insert_rowid() where k=1; sl@0: insert into t1 values (100+NEW.k); sl@0: insert into v2 values (100+last_insert_rowid()); sl@0: update rid set rout=last_insert_rowid() where k=1; sl@0: end; sl@0: create temp trigger r2 instead of insert on v2 for each row begin sl@0: update rid set rin=last_insert_rowid() where k=2; sl@0: insert into t2 values (1000+NEW.k); sl@0: update rid set rout=last_insert_rowid() where k=2; sl@0: end; sl@0: insert into t1 values (77); sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 77} sl@0: sl@0: do_test lastinsert-7.2 { sl@0: catchsql { sl@0: insert into v1 values (5); sl@0: select last_insert_rowid(); sl@0: } sl@0: } {0 77} sl@0: sl@0: do_test lastinsert-7.3 { sl@0: catchsql { sl@0: select rin from rid where k=1; sl@0: } sl@0: } {0 77} sl@0: sl@0: do_test lastinsert-7.4 { sl@0: catchsql { sl@0: select rout from rid where k=1; sl@0: } sl@0: } {0 105} sl@0: sl@0: do_test lastinsert-7.5 { sl@0: catchsql { sl@0: select rin from rid where k=2; sl@0: } sl@0: } {0 105} sl@0: sl@0: do_test lastinsert-7.6 { sl@0: catchsql { sl@0: select rout from rid where k=2; sl@0: } sl@0: } {0 1205} sl@0: sl@0: do_test lastinsert-8.1 { sl@0: db close sl@0: sqlite3 db test.db sl@0: execsql { sl@0: CREATE TABLE t2(x INTEGER PRIMARY KEY, y); sl@0: CREATE TABLE t3(a, b); sl@0: CREATE TRIGGER after_t2 AFTER INSERT ON t2 BEGIN sl@0: INSERT INTO t3 VALUES(new.x, new.y); sl@0: END; sl@0: INSERT INTO t2 VALUES(5000000000, 1); sl@0: SELECT last_insert_rowid(); sl@0: } sl@0: } 5000000000 sl@0: sl@0: do_test lastinsert-9.1 { sl@0: db eval {INSERT INTO t2 VALUES(123456789012345,0)} sl@0: db last_insert_rowid sl@0: } {123456789012345} sl@0: sl@0: sl@0: } ;# ifcapable (view && trigger) sl@0: sl@0: finish_test