/ Check-in [4a1f6a3a]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Fix processing of BEFORE triggers on INSERT statements with RHS SELECTs that insert a NULL into the INTEGER PRIMARY KEY. Ticket #3832. (CVS 6583)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4a1f6a3a9ac8b476c86edac83b555adeef0be4e6
User & Date: drh 2009-05-01 02:08:04
Context
2009-05-01
05:23
Fix for tempdb.test so that it passes when running the inmemory_journal permutation test. (CVS 6584) check-in: 0256187b user: danielk1977 tags: trunk
02:08
Fix processing of BEFORE triggers on INSERT statements with RHS SELECTs that insert a NULL into the INTEGER PRIMARY KEY. Ticket #3832. (CVS 6583) check-in: 4a1f6a3a user: drh tags: trunk
2009-04-30
17:45
More cleanup, etc. to support MSVC compiles. (CVS 6582) check-in: 2cd9655e user: shane tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/insert.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle INSERT statements in SQLite.
    14     14   **
    15         -** $Id: insert.c,v 1.261 2009/04/30 00:11:10 drh Exp $
           15  +** $Id: insert.c,v 1.262 2009/05/01 02:08:04 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Generate code that will open a table for reading.
    21     21   */
    22     22   void sqlite3OpenTable(
................................................................................
   777    777       ** translated into a unique ID for the row.  But on a BEFORE trigger,
   778    778       ** we do not know what the unique ID will be (because the insert has
   779    779       ** not happened yet) so we substitute a rowid of -1
   780    780       */
   781    781       regTrigRowid = sqlite3GetTempReg(pParse);
   782    782       if( keyColumn<0 ){
   783    783         sqlite3VdbeAddOp2(v, OP_Integer, -1, regTrigRowid);
   784         -    }else if( useTempTable ){
   785         -      sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regTrigRowid);
   786    784       }else{
   787    785         int j1;
   788         -      assert( pSelect==0 );  /* Otherwise useTempTable is true */
   789         -      sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regTrigRowid);
          786  +      if( useTempTable ){
          787  +        sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regTrigRowid);
          788  +      }else{
          789  +        assert( pSelect==0 );  /* Otherwise useTempTable is true */
          790  +        sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regTrigRowid);
          791  +      }
   790    792         j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regTrigRowid);
   791    793         sqlite3VdbeAddOp2(v, OP_Integer, -1, regTrigRowid);
   792    794         sqlite3VdbeJumpHere(v, j1);
   793    795         sqlite3VdbeAddOp1(v, OP_MustBeInt, regTrigRowid);
   794    796       }
   795    797   
   796    798       /* Cannot have triggers on a virtual table. If it were possible,

Added test/tkt3832.test.

            1  +# 2009 April 30                                                            
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# Ticket #3832
           13  +#
           14  +# A segfault when using a BEFORE trigger on an INSERT and inserting
           15  +# a NULL into the INTEGER PRIMARY KEY.
           16  +#
           17  +# $Id: tkt3832.test,v 1.1 2009/05/01 02:08:04 drh Exp $
           18  +
           19  +set testdir [file dirname $argv0]
           20  +source $testdir/tester.tcl
           21  +
           22  +
           23  +do_test tkt3832-1.1 {
           24  +  db eval {
           25  +    CREATE TABLE t1(a INT, b INTEGER PRIMARY KEY);
           26  +    CREATE TABLE log(x);
           27  +    CREATE TRIGGER t1r1 BEFORE INSERT ON t1 BEGIN
           28  +      INSERT INTO log VALUES(new.b);
           29  +    END;
           30  +    INSERT INTO t1 VALUES(NULL,5);
           31  +    INSERT INTO t1 SELECT b, a FROM t1 ORDER BY b;
           32  +    SELECT rowid, * FROM t1;
           33  +    SELECT rowid, * FROM log;
           34  +  }
           35  +} {5 {} 5 6 5 6 1 5 2 -1}
           36  +
           37  +finish_test