2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#***********************************************************************
2N/A# Tests to make sure that value returned by last_insert_rowid() (LIRID)
2N/A# is updated properly, especially inside triggers
2N/A# Note 1: insert into table is now the only statement which changes LIRID
2N/A# Note 2: upon entry into before or instead of triggers,
2N/A# LIRID is unchanged (rather than -1)
2N/A# Note 3: LIRID is changed within the context of a trigger,
2N/A# but is restored once the trigger exits
2N/A# Note 4: LIRID is not changed by an insert into a view (since everything
2N/A# is done within instead of trigger context)
2N/Aset testdir [file dirname $argv0]
2N/A# ----------------------------------------------------------------------------
2N/A#
1.x - basic tests (no triggers)
2N/A# LIRID changed properly after an insert into a table
2N/Ado_test lastinsert-1.1 {
2N/A create table t1 (k integer primary key);
2N/A insert into t1 values (1);
2N/A insert into t1 values (NULL);
2N/A insert into t1 values (NULL);
2N/A select last_insert_rowid();
2N/A# LIRID unchanged after an update on a table
2N/Ado_test lastinsert-1.2 {
2N/A update t1 set k=4 where k=2;
2N/A select last_insert_rowid();
2N/A# LIRID unchanged after a delete from a table
2N/Ado_test lastinsert-1.3 {
2N/A delete from t1 where k=4;
2N/A select last_insert_rowid();
2N/Ado_test lastinsert-1.4 {
2N/A create table t2 (k integer primary key, val1, val2, val3);
2N/A create view v as select * from t1;
2N/A select last_insert_rowid();
2N/A# ----------------------------------------------------------------------------
2N/A#
2.x - tests with after insert trigger
2N/A# LIRID changed properly after an insert into table containing an after trigger
2N/Ado_test lastinsert-2.1 {
2N/A create trigger r1 after insert on t1 for each row begin
2N/A insert into t2 values (
NEW.k*2, last_insert_rowid(), NULL, NULL);
2N/A update t2 set k=k+10, val2=100+last_insert_rowid();
2N/A update t2 set val3=1000+last_insert_rowid();
2N/A insert into t1 values (13);
2N/A select last_insert_rowid();
2N/A# LIRID equals
NEW.k upon entry into after insert trigger
2N/Ado_test lastinsert-2.2 {
2N/A select val1 from t2;
2N/A# LIRID changed properly by insert within context of after insert trigger
2N/Ado_test lastinsert-2.3 {
2N/A select val2 from t2;
2N/A# LIRID unchanged by update within context of after insert trigger
2N/Ado_test lastinsert-2.4 {
2N/A select val3 from t2;
2N/A# ----------------------------------------------------------------------------
2N/A#
3.x - tests with after update trigger
2N/A# LIRID not changed after an update onto a table containing an after trigger
2N/Ado_test lastinsert-3.1 {
2N/A create trigger r1 after update on t1 for each row begin
2N/A insert into t2 values (
NEW.k*2, last_insert_rowid(), NULL, NULL);
2N/A update t2 set k=k+10, val2=100+last_insert_rowid();
2N/A update t2 set val3=1000+last_insert_rowid();
2N/A update t1 set k=14 where k=3;
2N/A select last_insert_rowid();
2N/A# LIRID unchanged upon entry into after update trigger
2N/Ado_test lastinsert-3.2 {
2N/A select val1 from t2;
2N/A# LIRID changed properly by insert within context of after update trigger
2N/Ado_test lastinsert-3.3 {
2N/A select val2 from t2;
2N/A# LIRID unchanged by update within context of after update trigger
2N/Ado_test lastinsert-3.4 {
2N/A select val3 from t2;
2N/A# ----------------------------------------------------------------------------
2N/A#
4.x - tests with instead of insert trigger
2N/A# LIRID not changed after an insert into view containing an instead of trigger
2N/Ado_test lastinsert-4.1 {
2N/A create trigger r1 instead of insert on v for each row begin
2N/A insert into t2 values (
NEW.k*2, last_insert_rowid(), NULL, NULL);
2N/A update t2 set k=k+10, val2=100+last_insert_rowid();
2N/A update t2 set val3=1000+last_insert_rowid();
2N/A insert into v values (15);
2N/A select last_insert_rowid();
2N/A# LIRID unchanged upon entry into instead of trigger
2N/Ado_test lastinsert-4.2 {
2N/A select val1 from t2;
2N/A# LIRID changed properly by insert within context of instead of trigger
2N/Ado_test lastinsert-4.3 {
2N/A select val2 from t2;
2N/A# LIRID unchanged by update within context of instead of trigger
2N/Ado_test lastinsert-4.4 {
2N/A select val3 from t2;
2N/A# ----------------------------------------------------------------------------
2N/A#
5.x - tests with before delete trigger
2N/A# LIRID not changed after a delete on a table containing a before trigger
2N/Ado_test lastinsert-5.1 {
2N/A create trigger r1 before delete on t1 for each row begin
2N/A insert into t2 values (77, last_insert_rowid(), NULL, NULL);
2N/A update t2 set k=k+10, val2=100+last_insert_rowid();
2N/A update t2 set val3=1000+last_insert_rowid();
2N/A delete from t1 where k=1;
2N/A select last_insert_rowid();
2N/A# LIRID unchanged upon entry into delete trigger
2N/Ado_test lastinsert-5.2 {
2N/A select val1 from t2;
2N/A# LIRID changed properly by insert within context of delete trigger
2N/Ado_test lastinsert-5.3 {
2N/A select val2 from t2;
2N/A# LIRID unchanged by update within context of delete trigger
2N/Ado_test lastinsert-5.4 {
2N/A select val3 from t2;
2N/A# ----------------------------------------------------------------------------
2N/A#
6.x - tests with instead of update trigger
2N/A# LIRID not changed after an update on a view containing an instead of trigger
2N/Ado_test lastinsert-6.1 {
2N/A create trigger r1 instead of update on v for each row begin
2N/A insert into t2 values (
NEW.k*2, last_insert_rowid(), NULL, NULL);
2N/A update t2 set k=k+10, val2=100+last_insert_rowid();
2N/A update t2 set val3=1000+last_insert_rowid();
2N/A update v set k=16 where k=14;
2N/A select last_insert_rowid();
2N/A# LIRID unchanged upon entry into instead of trigger
2N/Ado_test lastinsert-6.2 {
2N/A select val1 from t2;
2N/A# LIRID changed properly by insert within context of instead of trigger
2N/Ado_test lastinsert-6.3 {
2N/A select val2 from t2;
2N/A# LIRID unchanged by update within context of instead of trigger
2N/Ado_test lastinsert-6.4 {
2N/A select val3 from t2;
2N/A# ----------------------------------------------------------------------------
2N/A#
7.x - complex tests with temporary tables and nested instead of triggers
2N/Ado_test lastinsert-7.1 {
2N/A drop table t1; drop table t2; drop trigger r1;
2N/A create temp table t1 (k integer primary key);
2N/A create temp table t2 (k integer primary key);
2N/A create temp view v1 as select * from t1;
2N/A create temp view v2 as select * from t2;
2N/A create temp table rid (k integer primary key, rin, rout);
2N/A insert into rid values (1, NULL, NULL);
2N/A insert into rid values (2, NULL, NULL);
2N/A create temp trigger r1 instead of insert on v1 for each row begin
2N/A update rid set rin=last_insert_rowid() where k=1;
2N/A insert into v2 values (100+last_insert_rowid());
2N/A update rid set rout=last_insert_rowid() where k=1;
2N/A create temp trigger r2 instead of insert on v2 for each row begin
2N/A update rid set rin=last_insert_rowid() where k=2;
2N/A update rid set rout=last_insert_rowid() where k=2;
2N/A insert into t1 values (77);
2N/A select last_insert_rowid();
2N/Ado_test lastinsert-7.2 {
2N/A insert into v1 values (5);
2N/A select last_insert_rowid();
2N/Ado_test lastinsert-7.3 {
2N/A select rin from rid where k=1;
2N/Ado_test lastinsert-7.4 {
2N/A select rout from rid where k=1;
2N/Ado_test lastinsert-7.5 {
2N/A select rin from rid where k=2;
2N/Ado_test lastinsert-7.6 {
2N/A select rout from rid where k=2;