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_insert_rowid() (LIRID) 14*c5c4113dSnw141292# is updated properly, especially inside triggers 15*c5c4113dSnw141292# 16*c5c4113dSnw141292# Note 1: insert into table is now the only statement which changes LIRID 17*c5c4113dSnw141292# Note 2: upon entry into before or instead of triggers, 18*c5c4113dSnw141292# LIRID is unchanged (rather than -1) 19*c5c4113dSnw141292# Note 3: LIRID is changed within the context of a trigger, 20*c5c4113dSnw141292# but is restored once the trigger exits 21*c5c4113dSnw141292# Note 4: LIRID is not changed by an insert into 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# LIRID changed properly after an insert into a table 32*c5c4113dSnw141292do_test lastinsert-1.1 { 33*c5c4113dSnw141292 catchsql { 34*c5c4113dSnw141292 create table t1 (k integer primary key); 35*c5c4113dSnw141292 insert into t1 values (1); 36*c5c4113dSnw141292 insert into t1 values (NULL); 37*c5c4113dSnw141292 insert into t1 values (NULL); 38*c5c4113dSnw141292 select last_insert_rowid(); 39*c5c4113dSnw141292 } 40*c5c4113dSnw141292} {0 3} 41*c5c4113dSnw141292 42*c5c4113dSnw141292# LIRID unchanged after an update on a table 43*c5c4113dSnw141292do_test lastinsert-1.2 { 44*c5c4113dSnw141292 catchsql { 45*c5c4113dSnw141292 update t1 set k=4 where k=2; 46*c5c4113dSnw141292 select last_insert_rowid(); 47*c5c4113dSnw141292 } 48*c5c4113dSnw141292} {0 3} 49*c5c4113dSnw141292 50*c5c4113dSnw141292# LIRID unchanged after a delete from a table 51*c5c4113dSnw141292do_test lastinsert-1.3 { 52*c5c4113dSnw141292 catchsql { 53*c5c4113dSnw141292 delete from t1 where k=4; 54*c5c4113dSnw141292 select last_insert_rowid(); 55*c5c4113dSnw141292 } 56*c5c4113dSnw141292} {0 3} 57*c5c4113dSnw141292 58*c5c4113dSnw141292# LIRID unchanged after create table/view statements 59*c5c4113dSnw141292do_test lastinsert-1.4 { 60*c5c4113dSnw141292 catchsql { 61*c5c4113dSnw141292 create table t2 (k integer primary key, val1, val2, val3); 62*c5c4113dSnw141292 create view v as select * from t1; 63*c5c4113dSnw141292 select last_insert_rowid(); 64*c5c4113dSnw141292 } 65*c5c4113dSnw141292} {0 3} 66*c5c4113dSnw141292 67*c5c4113dSnw141292# ---------------------------------------------------------------------------- 68*c5c4113dSnw141292# 2.x - tests with after insert trigger 69*c5c4113dSnw141292 70*c5c4113dSnw141292# LIRID changed properly after an insert into table containing an after trigger 71*c5c4113dSnw141292do_test lastinsert-2.1 { 72*c5c4113dSnw141292 catchsql { 73*c5c4113dSnw141292 delete from t2; 74*c5c4113dSnw141292 create trigger r1 after insert on t1 for each row begin 75*c5c4113dSnw141292 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL); 76*c5c4113dSnw141292 update t2 set k=k+10, val2=100+last_insert_rowid(); 77*c5c4113dSnw141292 update t2 set val3=1000+last_insert_rowid(); 78*c5c4113dSnw141292 end; 79*c5c4113dSnw141292 insert into t1 values (13); 80*c5c4113dSnw141292 select last_insert_rowid(); 81*c5c4113dSnw141292 } 82*c5c4113dSnw141292} {0 13} 83*c5c4113dSnw141292 84*c5c4113dSnw141292# LIRID equals NEW.k upon entry into after insert trigger 85*c5c4113dSnw141292do_test lastinsert-2.2 { 86*c5c4113dSnw141292 catchsql { 87*c5c4113dSnw141292 select val1 from t2; 88*c5c4113dSnw141292 } 89*c5c4113dSnw141292} {0 13} 90*c5c4113dSnw141292 91*c5c4113dSnw141292# LIRID changed properly by insert within context of after insert trigger 92*c5c4113dSnw141292do_test lastinsert-2.3 { 93*c5c4113dSnw141292 catchsql { 94*c5c4113dSnw141292 select val2 from t2; 95*c5c4113dSnw141292 } 96*c5c4113dSnw141292} {0 126} 97*c5c4113dSnw141292 98*c5c4113dSnw141292# LIRID unchanged by update within context of after insert trigger 99*c5c4113dSnw141292do_test lastinsert-2.4 { 100*c5c4113dSnw141292 catchsql { 101*c5c4113dSnw141292 select val3 from t2; 102*c5c4113dSnw141292 } 103*c5c4113dSnw141292} {0 1026} 104*c5c4113dSnw141292 105*c5c4113dSnw141292# ---------------------------------------------------------------------------- 106*c5c4113dSnw141292# 3.x - tests with after update trigger 107*c5c4113dSnw141292 108*c5c4113dSnw141292# LIRID not changed after an update onto a table containing an after trigger 109*c5c4113dSnw141292do_test lastinsert-3.1 { 110*c5c4113dSnw141292 catchsql { 111*c5c4113dSnw141292 delete from t2; 112*c5c4113dSnw141292 drop trigger r1; 113*c5c4113dSnw141292 create trigger r1 after update on t1 for each row begin 114*c5c4113dSnw141292 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL); 115*c5c4113dSnw141292 update t2 set k=k+10, val2=100+last_insert_rowid(); 116*c5c4113dSnw141292 update t2 set val3=1000+last_insert_rowid(); 117*c5c4113dSnw141292 end; 118*c5c4113dSnw141292 update t1 set k=14 where k=3; 119*c5c4113dSnw141292 select last_insert_rowid(); 120*c5c4113dSnw141292 } 121*c5c4113dSnw141292} {0 13} 122*c5c4113dSnw141292 123*c5c4113dSnw141292# LIRID unchanged upon entry into after update trigger 124*c5c4113dSnw141292do_test lastinsert-3.2 { 125*c5c4113dSnw141292 catchsql { 126*c5c4113dSnw141292 select val1 from t2; 127*c5c4113dSnw141292 } 128*c5c4113dSnw141292} {0 13} 129*c5c4113dSnw141292 130*c5c4113dSnw141292# LIRID changed properly by insert within context of after update trigger 131*c5c4113dSnw141292do_test lastinsert-3.3 { 132*c5c4113dSnw141292 catchsql { 133*c5c4113dSnw141292 select val2 from t2; 134*c5c4113dSnw141292 } 135*c5c4113dSnw141292} {0 128} 136*c5c4113dSnw141292 137*c5c4113dSnw141292# LIRID unchanged by update within context of after update trigger 138*c5c4113dSnw141292do_test lastinsert-3.4 { 139*c5c4113dSnw141292 catchsql { 140*c5c4113dSnw141292 select val3 from t2; 141*c5c4113dSnw141292 } 142*c5c4113dSnw141292} {0 1028} 143*c5c4113dSnw141292 144*c5c4113dSnw141292# ---------------------------------------------------------------------------- 145*c5c4113dSnw141292# 4.x - tests with instead of insert trigger 146*c5c4113dSnw141292 147*c5c4113dSnw141292# LIRID not changed after an insert into view containing an instead of trigger 148*c5c4113dSnw141292do_test lastinsert-4.1 { 149*c5c4113dSnw141292 catchsql { 150*c5c4113dSnw141292 delete from t2; 151*c5c4113dSnw141292 drop trigger r1; 152*c5c4113dSnw141292 create trigger r1 instead of insert on v for each row begin 153*c5c4113dSnw141292 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL); 154*c5c4113dSnw141292 update t2 set k=k+10, val2=100+last_insert_rowid(); 155*c5c4113dSnw141292 update t2 set val3=1000+last_insert_rowid(); 156*c5c4113dSnw141292 end; 157*c5c4113dSnw141292 insert into v values (15); 158*c5c4113dSnw141292 select last_insert_rowid(); 159*c5c4113dSnw141292 } 160*c5c4113dSnw141292} {0 13} 161*c5c4113dSnw141292 162*c5c4113dSnw141292# LIRID unchanged upon entry into instead of trigger 163*c5c4113dSnw141292do_test lastinsert-4.2 { 164*c5c4113dSnw141292 catchsql { 165*c5c4113dSnw141292 select val1 from t2; 166*c5c4113dSnw141292 } 167*c5c4113dSnw141292} {0 13} 168*c5c4113dSnw141292 169*c5c4113dSnw141292# LIRID changed properly by insert within context of instead of trigger 170*c5c4113dSnw141292do_test lastinsert-4.3 { 171*c5c4113dSnw141292 catchsql { 172*c5c4113dSnw141292 select val2 from t2; 173*c5c4113dSnw141292 } 174*c5c4113dSnw141292} {0 130} 175*c5c4113dSnw141292 176*c5c4113dSnw141292# LIRID unchanged by update within context of instead of trigger 177*c5c4113dSnw141292do_test lastinsert-4.4 { 178*c5c4113dSnw141292 catchsql { 179*c5c4113dSnw141292 select val3 from t2; 180*c5c4113dSnw141292 } 181*c5c4113dSnw141292} {0 1030} 182*c5c4113dSnw141292 183*c5c4113dSnw141292# ---------------------------------------------------------------------------- 184*c5c4113dSnw141292# 5.x - tests with before delete trigger 185*c5c4113dSnw141292 186*c5c4113dSnw141292# LIRID not changed after a delete on a table containing a before trigger 187*c5c4113dSnw141292do_test lastinsert-5.1 { 188*c5c4113dSnw141292 catchsql { 189*c5c4113dSnw141292 delete from t2; 190*c5c4113dSnw141292 drop trigger r1; 191*c5c4113dSnw141292 create trigger r1 before delete on t1 for each row begin 192*c5c4113dSnw141292 insert into t2 values (77, last_insert_rowid(), NULL, NULL); 193*c5c4113dSnw141292 update t2 set k=k+10, val2=100+last_insert_rowid(); 194*c5c4113dSnw141292 update t2 set val3=1000+last_insert_rowid(); 195*c5c4113dSnw141292 end; 196*c5c4113dSnw141292 delete from t1 where k=1; 197*c5c4113dSnw141292 select last_insert_rowid(); 198*c5c4113dSnw141292 } 199*c5c4113dSnw141292} {0 13} 200*c5c4113dSnw141292 201*c5c4113dSnw141292# LIRID unchanged upon entry into delete trigger 202*c5c4113dSnw141292do_test lastinsert-5.2 { 203*c5c4113dSnw141292 catchsql { 204*c5c4113dSnw141292 select val1 from t2; 205*c5c4113dSnw141292 } 206*c5c4113dSnw141292} {0 13} 207*c5c4113dSnw141292 208*c5c4113dSnw141292# LIRID changed properly by insert within context of delete trigger 209*c5c4113dSnw141292do_test lastinsert-5.3 { 210*c5c4113dSnw141292 catchsql { 211*c5c4113dSnw141292 select val2 from t2; 212*c5c4113dSnw141292 } 213*c5c4113dSnw141292} {0 177} 214*c5c4113dSnw141292 215*c5c4113dSnw141292# LIRID unchanged by update within context of delete trigger 216*c5c4113dSnw141292do_test lastinsert-5.4 { 217*c5c4113dSnw141292 catchsql { 218*c5c4113dSnw141292 select val3 from t2; 219*c5c4113dSnw141292 } 220*c5c4113dSnw141292} {0 1077} 221*c5c4113dSnw141292 222*c5c4113dSnw141292# ---------------------------------------------------------------------------- 223*c5c4113dSnw141292# 6.x - tests with instead of update trigger 224*c5c4113dSnw141292 225*c5c4113dSnw141292# LIRID not changed after an update on a view containing an instead of trigger 226*c5c4113dSnw141292do_test lastinsert-6.1 { 227*c5c4113dSnw141292 catchsql { 228*c5c4113dSnw141292 delete from t2; 229*c5c4113dSnw141292 drop trigger r1; 230*c5c4113dSnw141292 create trigger r1 instead of update on v for each row begin 231*c5c4113dSnw141292 insert into t2 values (NEW.k*2, last_insert_rowid(), NULL, NULL); 232*c5c4113dSnw141292 update t2 set k=k+10, val2=100+last_insert_rowid(); 233*c5c4113dSnw141292 update t2 set val3=1000+last_insert_rowid(); 234*c5c4113dSnw141292 end; 235*c5c4113dSnw141292 update v set k=16 where k=14; 236*c5c4113dSnw141292 select last_insert_rowid(); 237*c5c4113dSnw141292 } 238*c5c4113dSnw141292} {0 13} 239*c5c4113dSnw141292 240*c5c4113dSnw141292# LIRID unchanged upon entry into instead of trigger 241*c5c4113dSnw141292do_test lastinsert-6.2 { 242*c5c4113dSnw141292 catchsql { 243*c5c4113dSnw141292 select val1 from t2; 244*c5c4113dSnw141292 } 245*c5c4113dSnw141292} {0 13} 246*c5c4113dSnw141292 247*c5c4113dSnw141292# LIRID changed properly by insert within context of instead of trigger 248*c5c4113dSnw141292do_test lastinsert-6.3 { 249*c5c4113dSnw141292 catchsql { 250*c5c4113dSnw141292 select val2 from t2; 251*c5c4113dSnw141292 } 252*c5c4113dSnw141292} {0 132} 253*c5c4113dSnw141292 254*c5c4113dSnw141292# LIRID unchanged by update within context of instead of trigger 255*c5c4113dSnw141292do_test lastinsert-6.4 { 256*c5c4113dSnw141292 catchsql { 257*c5c4113dSnw141292 select val3 from t2; 258*c5c4113dSnw141292 } 259*c5c4113dSnw141292} {0 1032} 260*c5c4113dSnw141292 261*c5c4113dSnw141292# ---------------------------------------------------------------------------- 262*c5c4113dSnw141292# 7.x - complex tests with temporary tables and nested instead of triggers 263*c5c4113dSnw141292 264*c5c4113dSnw141292do_test lastinsert-7.1 { 265*c5c4113dSnw141292 catchsql { 266*c5c4113dSnw141292 drop table t1; drop table t2; drop trigger r1; 267*c5c4113dSnw141292 create temp table t1 (k integer primary key); 268*c5c4113dSnw141292 create temp table t2 (k integer primary key); 269*c5c4113dSnw141292 create temp view v1 as select * from t1; 270*c5c4113dSnw141292 create temp view v2 as select * from t2; 271*c5c4113dSnw141292 create temp table rid (k integer primary key, rin, rout); 272*c5c4113dSnw141292 insert into rid values (1, NULL, NULL); 273*c5c4113dSnw141292 insert into rid values (2, NULL, NULL); 274*c5c4113dSnw141292 create temp trigger r1 instead of insert on v1 for each row begin 275*c5c4113dSnw141292 update rid set rin=last_insert_rowid() where k=1; 276*c5c4113dSnw141292 insert into t1 values (100+NEW.k); 277*c5c4113dSnw141292 insert into v2 values (100+last_insert_rowid()); 278*c5c4113dSnw141292 update rid set rout=last_insert_rowid() where k=1; 279*c5c4113dSnw141292 end; 280*c5c4113dSnw141292 create temp trigger r2 instead of insert on v2 for each row begin 281*c5c4113dSnw141292 update rid set rin=last_insert_rowid() where k=2; 282*c5c4113dSnw141292 insert into t2 values (1000+NEW.k); 283*c5c4113dSnw141292 update rid set rout=last_insert_rowid() where k=2; 284*c5c4113dSnw141292 end; 285*c5c4113dSnw141292 insert into t1 values (77); 286*c5c4113dSnw141292 select last_insert_rowid(); 287*c5c4113dSnw141292 } 288*c5c4113dSnw141292} {0 77} 289*c5c4113dSnw141292 290*c5c4113dSnw141292do_test lastinsert-7.2 { 291*c5c4113dSnw141292 catchsql { 292*c5c4113dSnw141292 insert into v1 values (5); 293*c5c4113dSnw141292 select last_insert_rowid(); 294*c5c4113dSnw141292 } 295*c5c4113dSnw141292} {0 77} 296*c5c4113dSnw141292 297*c5c4113dSnw141292do_test lastinsert-7.3 { 298*c5c4113dSnw141292 catchsql { 299*c5c4113dSnw141292 select rin from rid where k=1; 300*c5c4113dSnw141292 } 301*c5c4113dSnw141292} {0 77} 302*c5c4113dSnw141292 303*c5c4113dSnw141292do_test lastinsert-7.4 { 304*c5c4113dSnw141292 catchsql { 305*c5c4113dSnw141292 select rout from rid where k=1; 306*c5c4113dSnw141292 } 307*c5c4113dSnw141292} {0 105} 308*c5c4113dSnw141292 309*c5c4113dSnw141292do_test lastinsert-7.5 { 310*c5c4113dSnw141292 catchsql { 311*c5c4113dSnw141292 select rin from rid where k=2; 312*c5c4113dSnw141292 } 313*c5c4113dSnw141292} {0 105} 314*c5c4113dSnw141292 315*c5c4113dSnw141292do_test lastinsert-7.6 { 316*c5c4113dSnw141292 catchsql { 317*c5c4113dSnw141292 select rout from rid where k=2; 318*c5c4113dSnw141292 } 319*c5c4113dSnw141292} {0 1205} 320*c5c4113dSnw141292 321*c5c4113dSnw141292finish_test 322*c5c4113dSnw141292 323