/ Check-in [96a71766]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Do not record the inserted rowid on when doing an INSERT within a trigger. Ticket #290. (CVS 906)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 96a717661a3b7108fe0cacb588d81fd8e91eb640
User & Date: drh 2003-04-15 14:01:43
Context
2003-04-15
17:22
Added btree_rb.c (CVS 907) check-in: 93eb6c52 user: paul tags: trunk
14:01
Do not record the inserted rowid on when doing an INSERT within a trigger. Ticket #290. (CVS 906) check-in: 96a71766 user: drh tags: trunk
01:49
Change lemon to use <stdarg.h> instead of <varargs.h> because GCC no longer supports varargs.h. Tickets #288 and #280. Ironically, lemon originally used varargs.h because stdarg.h was not supported by the compiler I was using in 1989 (which was gcc if I recall correctly.) (CVS 905) check-in: 7902e477 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/vdbe.c.

    32     32   **
    33     33   ** Various scripts scan this source file in order to generate HTML
    34     34   ** documentation, headers files, or other derived files.  The formatting
    35     35   ** of the code in this file is, therefore, important.  See other comments
    36     36   ** in this file for details.  If in doubt, do not deviate from existing
    37     37   ** commenting and indentation practices when changing or adding code.
    38     38   **
    39         -** $Id: vdbe.c,v 1.213 2003/04/15 01:19:49 drh Exp $
           39  +** $Id: vdbe.c,v 1.214 2003/04/15 14:01:43 drh Exp $
    40     40   */
    41     41   #include "sqliteInt.h"
    42     42   #include <ctype.h>
    43     43   
    44     44   /*
    45     45   ** The makefile scans this source file and creates the following
    46     46   ** array of string constants which are the names of all VDBE opcodes.
................................................................................
  3912   3912   ** Write an entry into the database file P1.  A new entry is
  3913   3913   ** created if it doesn't already exist or the data for an existing
  3914   3914   ** entry is overwritten.  The data is the value on the top of the
  3915   3915   ** stack.  The key is the next value down on the stack.  The key must
  3916   3916   ** be an integer.  The stack is popped twice by this instruction.
  3917   3917   **
  3918   3918   ** If P2==1 then the row change count is incremented.  If P2==0 the
  3919         -** row change count is unmodified.
         3919  +** row change count is unmodified.  The rowid is stored for subsequent
         3920  +** return by the sqlite_last_insert_rowid() function if P2 is 1.
  3920   3921   */
  3921   3922   /* Opcode: PutStrKey P1 * *
  3922   3923   **
  3923   3924   ** Write an entry into the database file P1.  A new entry is
  3924   3925   ** created if it doesn't already exist or the data for an existing
  3925   3926   ** entry is overwritten.  The data is the value on the top of the
  3926   3927   ** stack.  The key is the next value down on the stack.  The key must
................................................................................
  3941   3942         nKey = aStack[nos].n;
  3942   3943         zKey = zStack[nos];
  3943   3944       }else{
  3944   3945         assert( aStack[nos].flags & STK_Int );
  3945   3946         nKey = sizeof(int);
  3946   3947         iKey = intToKey(aStack[nos].i);
  3947   3948         zKey = (char*)&iKey;
  3948         -      db->lastRowid = aStack[nos].i;
  3949         -      if( pOp->p2 ) db->nChange++;
         3949  +      if( pOp->p2 ){
         3950  +        db->nChange++;
         3951  +        db->lastRowid = aStack[nos].i;
         3952  +      }
  3950   3953         if( pC->nextRowidValid && aStack[nos].i>=pC->nextRowid ){
  3951   3954           pC->nextRowidValid = 0;
  3952   3955         }
  3953   3956       }
  3954   3957       rc = sqliteBtreeInsert(pC->pCursor, zKey, nKey,
  3955   3958                           zStack[tos], aStack[tos].n);
  3956   3959       pC->recnoIsValid = 0;

Changes to test/rowid.test.

     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this file is testing the magic ROWID column that is
    13     13   # found on all tables.
    14     14   #
    15         -# $Id: rowid.test,v 1.8 2002/02/19 22:42:06 drh Exp $
           15  +# $Id: rowid.test,v 1.9 2003/04/15 14:01:44 drh Exp $
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20     20   # Basic ROWID functionality tests.
    21     21   #
    22     22   do_test rowid-1.1 {
................................................................................
   348    348   do_test rowid-7.8 {
   349    349     execsql {
   350    350       DELETE FROM t2 WHERE a!=2;
   351    351       INSERT INTO t2(b) VALUES(111);
   352    352       SELECT * FROM t2;
   353    353     }
   354    354   } {2 66 3 111}
          355  +
          356  +# Make sure AFTER triggers that do INSERTs do not change the last_insert_rowid.
          357  +# Ticket #290
          358  +#
          359  +do_test rowid-8.1 {
          360  +  execsql {
          361  +    CREATE TABLE t3(a integer primary key);
          362  +    CREATE TABLE t4(x);
          363  +    INSERT INTO t4 VALUES(1);
          364  +    CREATE TRIGGER r3 AFTER INSERT on t3 FOR EACH ROW BEGIN
          365  +      INSERT INTO t4 VALUES(NEW.a+10);
          366  +    END;
          367  +    SELECT * FROM t3;
          368  +  }
          369  +} {}
          370  +do_test rowid-8.2 {
          371  +  execsql {
          372  +    SELECT rowid, * FROM t4;
          373  +  }
          374  +} {1 1}
          375  +do_test rowid-8.3 {
          376  +  execsql {
          377  +    INSERT INTO t3 VALUES(123);
          378  +    SELECT last_insert_rowid();
          379  +  }
          380  +} {123}
          381  +do_test rowid-8.4 {
          382  +  execsql {
          383  +    SELECT * FROM t3;
          384  +  }
          385  +} {123}
          386  +do_test rowid-8.5 {
          387  +  execsql {
          388  +    SELECT rowid, * FROM t4;
          389  +  }
          390  +} {1 1 2 133}
          391  +do_test rowid-8.6 {
          392  +  execsql {
          393  +    INSERT INTO t3 VALUES(NULL);
          394  +    SELECT last_insert_rowid();
          395  +  }
          396  +} {124}
          397  +do_test rowid-8.7 {
          398  +  execsql {
          399  +    SELECT * FROM t3;
          400  +  }
          401  +} {123 124}
          402  +do_test rowid-8.8 {
          403  +  execsql {
          404  +    SELECT rowid, * FROM t4;
          405  +  }
          406  +} {1 1 2 133 3 {}}
          407  +
   355    408   
   356    409   finish_test