/ Check-in [7e684785]
Login

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

Overview
Comment:When initializing the sqlite_sequence entry for an AUTOINCREMENT table, make sure the value is an reasonable integer even if the initial insert failed. Ticket #3148. (CVS 5175)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 7e6847852d4517b1d14bebb8f0ae4d938b0b6f6d
User & Date: drh 2008-05-29 03:20:59
Context
2008-05-29
03:54
Moved check and define for OS_WINCE from os_win.c to os.h (with the other OS_* defines). This allows OS_WINCE to be available for mutex_w32.c which is included earlier than os_win.c in the almagamation. (CVS 5176) check-in: ad6a782e user: shane tags: trunk
03:20
When initializing the sqlite_sequence entry for an AUTOINCREMENT table, make sure the value is an reasonable integer even if the initial insert failed. Ticket #3148. (CVS 5175) check-in: 7e684785 user: drh tags: trunk
03:12
Fix an obsolete comment on the OP_Rowid opcode in the VDBE. (CVS 5174) check-in: 0d55328e user: drh 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.238 2008/04/28 18:46:43 drh Exp $
           15  +** $Id: insert.c,v 1.239 2008/05/29 03:20:59 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Set P4 of the most recently inserted opcode to a column affinity
    21     21   ** string for index pIdx. A column affinity string has one character
    22     22   ** for each column in the table, according to the affinity of the column:
................................................................................
   171    171       assert( v );
   172    172       pParse->nMem++;         /* Holds name of table */
   173    173       memId = ++pParse->nMem;
   174    174       pParse->nMem++;
   175    175       sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
   176    176       addr = sqlite3VdbeCurrentAddr(v);
   177    177       sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, pTab->zName, 0);
   178         -    sqlite3VdbeAddOp2(v, OP_Rewind, iCur, addr+8);
          178  +    sqlite3VdbeAddOp2(v, OP_Rewind, iCur, addr+9);
   179    179       sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, memId);
   180    180       sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
   181    181       sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
   182    182       sqlite3VdbeAddOp2(v, OP_Rowid, iCur, memId+1);
   183    183       sqlite3VdbeAddOp3(v, OP_Column, iCur, 1, memId);
   184         -    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+8);
          184  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
   185    185       sqlite3VdbeAddOp2(v, OP_Next, iCur, addr+2);
          186  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
   186    187       sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
   187    188     }
   188    189     return memId;
   189    190   }
   190    191   
   191    192   /*
   192    193   ** Update the maximum rowid for an autoincrement calculation.

Changes to test/autoinc.test.

     7      7   #    May you find forgiveness for yourself and forgive others.
     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 script is testing the AUTOINCREMENT features.
    13     13   #
    14         -# $Id: autoinc.test,v 1.11 2008/01/04 19:10:29 danielk1977 Exp $
           14  +# $Id: autoinc.test,v 1.12 2008/05/29 03:20:59 drh Exp $
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20     20   # If the library is not compiled with autoincrement support then
    21     21   # skip all tests in this file.
................................................................................
   530    530     sqlite3_step $STMT
   531    531     sqlite3_finalize $STMT
   532    532     execsql {
   533    533       INSERT INTO t1 VALUES(NULL);
   534    534       SELECT * FROM t1;
   535    535     }
   536    536   } {1}
          537  +
          538  +# Ticket #3148
          539  +# Make sure the sqlite_sequence table is not damaged when doing
          540  +# an empty insert - an INSERT INTO ... SELECT ... where the SELECT
          541  +# clause returns an empty set.
          542  +#
          543  +do_test autoinc-9.1 {
          544  +  db eval {
          545  +    CREATE TABLE t2(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
          546  +    INSERT INTO t2 VALUES(NULL, 1);
          547  +    CREATE TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
          548  +    INSERT INTO t3 SELECT * FROM t2 WHERE y>1;
          549  +
          550  +    SELECT * FROM sqlite_sequence WHERE name='t3';
          551  +  }
          552  +} {t3 0}
          553  +
   537    554   
   538    555   finish_test