/ Changes On Branch autoinc-enhancement
Login

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

Changes In Branch autoinc-enhancement Excluding Merge-Ins

This is equivalent to a diff from 1774f1c3 to 3e3849a9

2018-03-20
13:44
Avoid unnecessary write to the sqlite_sequence table when an insert is done into an autoincrement table with an application-specified rowid that is less than the maximum. check-in: ec7addc8 user: drh tags: trunk
2018-03-16
20:23
Detect databases whose schema is corrupted using a CREATE TABLE AS statement and issue an appropriate error message. check-in: d75e6765 user: drh tags: trunk
19:10
Fix a parsing issue associated with a corrupt sqlite_master table. check-in: 5f779ff6 user: mistachkin tags: corrupt-schema
18:46
Avoid writing the sqlite_sequence table when it has not actually changed. Closed-Leaf check-in: 3e3849a9 user: drh tags: autoinc-enhancement
07:49
Fix a problem in test script thread001.test causing a spurious "-1 files were left open" error when run separately. Cherrypick of [1774f1c3b]. check-in: 6cf8172d user: dan tags: branch-3.19
07:48
Fix a problem in test script thread001.test causing a spurious "-1 files were left open" error when run separately. check-in: 1774f1c3 user: dan tags: trunk
2018-03-15
17:46
Fix a typo in a comment used to generate documentation. No code changes. check-in: f1784aff user: drh tags: trunk

Changes to src/insert.c.

   206    206   **
   207    207   ** There is at most one AutoincInfo structure per table even if the
   208    208   ** same table is autoincremented multiple times due to inserts within
   209    209   ** triggers.  A new AutoincInfo structure is created if this is the
   210    210   ** first use of table pTab.  On 2nd and subsequent uses, the original
   211    211   ** AutoincInfo structure is used.
   212    212   **
   213         -** Three memory locations are allocated:
          213  +** Four consecutive registers are allocated:
   214    214   **
   215         -**   (1)  Register to hold the name of the pTab table.
   216         -**   (2)  Register to hold the maximum ROWID of pTab.
   217         -**   (3)  Register to hold the rowid in sqlite_sequence of pTab
          215  +**   (1)  The name of the pTab table.
          216  +**   (2)  The maximum ROWID of pTab.
          217  +**   (3)  The rowid in sqlite_sequence of pTab
          218  +**   (4)  The original value of the max ROWID in pTab, or NULL if none
   218    219   **
   219    220   ** The 2nd register is the one that is returned.  That is all the
   220    221   ** insert routine needs to know about.
   221    222   */
   222    223   static int autoIncBegin(
   223    224     Parse *pParse,      /* Parsing context */
   224    225     int iDb,            /* Index of the database holding pTab */
................................................................................
   238    239         if( pInfo==0 ) return 0;
   239    240         pInfo->pNext = pToplevel->pAinc;
   240    241         pToplevel->pAinc = pInfo;
   241    242         pInfo->pTab = pTab;
   242    243         pInfo->iDb = iDb;
   243    244         pToplevel->nMem++;                  /* Register to hold name of table */
   244    245         pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
   245         -      pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
          246  +      pToplevel->nMem +=2;       /* Rowid in sqlite_sequence + orig max val */
   246    247       }
   247    248       memId = pInfo->regCtr;
   248    249     }
   249    250     return memId;
   250    251   }
   251    252   
   252    253   /*
................................................................................
   266    267     assert( sqlite3IsToplevel(pParse) );
   267    268   
   268    269     assert( v );   /* We failed long ago if this is not so */
   269    270     for(p = pParse->pAinc; p; p = p->pNext){
   270    271       static const int iLn = VDBE_OFFSET_LINENO(2);
   271    272       static const VdbeOpList autoInc[] = {
   272    273         /* 0  */ {OP_Null,    0,  0, 0},
   273         -      /* 1  */ {OP_Rewind,  0,  9, 0},
          274  +      /* 1  */ {OP_Rewind,  0, 10, 0},
   274    275         /* 2  */ {OP_Column,  0,  0, 0},
   275         -      /* 3  */ {OP_Ne,      0,  7, 0},
          276  +      /* 3  */ {OP_Ne,      0,  9, 0},
   276    277         /* 4  */ {OP_Rowid,   0,  0, 0},
   277    278         /* 5  */ {OP_Column,  0,  1, 0},
   278         -      /* 6  */ {OP_Goto,    0,  9, 0},
   279         -      /* 7  */ {OP_Next,    0,  2, 0},
   280         -      /* 8  */ {OP_Integer, 0,  0, 0},
   281         -      /* 9  */ {OP_Close,   0,  0, 0} 
          279  +      /* 6  */ {OP_AddImm,  0,  0, 0},
          280  +      /* 7  */ {OP_Copy,    0,  0, 0},
          281  +      /* 8  */ {OP_Goto,    0, 11, 0},
          282  +      /* 9  */ {OP_Next,    0,  2, 0},
          283  +      /* 10 */ {OP_Integer, 0,  0, 0},
          284  +      /* 11 */ {OP_Close,   0,  0, 0} 
   282    285       };
   283    286       VdbeOp *aOp;
   284    287       pDb = &db->aDb[p->iDb];
   285    288       memId = p->regCtr;
   286    289       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
   287    290       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
   288    291       sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
   289    292       aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
   290    293       if( aOp==0 ) break;
   291    294       aOp[0].p2 = memId;
   292         -    aOp[0].p3 = memId+1;
          295  +    aOp[0].p3 = memId+2;
   293    296       aOp[2].p3 = memId;
   294    297       aOp[3].p1 = memId-1;
   295    298       aOp[3].p3 = memId;
   296    299       aOp[3].p5 = SQLITE_JUMPIFNULL;
   297    300       aOp[4].p2 = memId+1;
   298    301       aOp[5].p3 = memId;
   299         -    aOp[8].p2 = memId;
          302  +    aOp[6].p1 = memId;
          303  +    aOp[7].p2 = memId+2;
          304  +    aOp[7].p1 = memId;
          305  +    aOp[10].p2 = memId;
   300    306     }
   301    307   }
   302    308   
   303    309   /*
   304    310   ** Update the maximum rowid for an autoincrement calculation.
   305    311   **
   306    312   ** This routine should be called when the regRowid register holds a
................................................................................
   339    345       VdbeOp *aOp;
   340    346       Db *pDb = &db->aDb[p->iDb];
   341    347       int iRec;
   342    348       int memId = p->regCtr;
   343    349   
   344    350       iRec = sqlite3GetTempReg(pParse);
   345    351       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
          352  +    sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
          353  +    VdbeCoverage(v);
   346    354       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
   347    355       aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
   348    356       if( aOp==0 ) break;
   349    357       aOp[0].p1 = memId+1;
   350    358       aOp[1].p2 = memId+1;
   351    359       aOp[2].p1 = memId-1;
   352    360       aOp[2].p3 = iRec;