System.Data.SQLite
Check-in [a67fce442b]
Not logged in

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

Overview
Comment:Update SQLite core library to the latest trunk code.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a67fce442b49b0ed9fe7f24edbc1a1f84dc89c49
User & Date: mistachkin 2015-09-30 18:48:34
Context
2015-09-30
19:22
Update Eagle script library in externals to the latest code. check-in: 8ddbcff542 user: mistachkin tags: trunk
18:48
Update SQLite core library to the latest trunk code. check-in: a67fce442b user: mistachkin tags: trunk
2015-09-29
16:58
Changes to support the 'vtab-onepass' functionality in the SQLite core library. check-in: 80d9c54709 user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to SQLite.Interop/src/core/sqlite3.c.

   323    323   **
   324    324   ** See also: [sqlite3_libversion()],
   325    325   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   326    326   ** [sqlite_version()] and [sqlite_source_id()].
   327    327   */
   328    328   #define SQLITE_VERSION        "3.8.12"
   329    329   #define SQLITE_VERSION_NUMBER 3008012
   330         -#define SQLITE_SOURCE_ID      "2015-09-28 17:05:22 c5566bb39c8d9b58f77380b81a873429575c7d5c"
          330  +#define SQLITE_SOURCE_ID      "2015-09-30 14:30:19 e796c0efb6cf17444b53af75046daf7d8fa82f78"
   331    331   
   332    332   /*
   333    333   ** CAPI3REF: Run-Time Library Version Numbers
   334    334   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   335    335   **
   336    336   ** These interfaces provide the same information as the [SQLITE_VERSION],
   337    337   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  5843   5843   ** strategy. A cost of N indicates that the cost of the strategy is similar
  5844   5844   ** to a linear scan of an SQLite table with N rows. A cost of log(N) 
  5845   5845   ** indicates that the expense of the operation is similar to that of a
  5846   5846   ** binary search on a unique indexed field of an SQLite table with N rows.
  5847   5847   **
  5848   5848   ** ^The estimatedRows value is an estimate of the number of rows that
  5849   5849   ** will be returned by the strategy.
         5850  +**
         5851  +** The xBestIndex method may optionally populate the idxFlags field with a 
         5852  +** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
         5853  +** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
         5854  +** assumes that the strategy may visit at most one row. 
         5855  +**
         5856  +** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
         5857  +** SQLite also assumes that if a call to the xUpdate() method is made as
         5858  +** part of the same statement to delete or update a virtual table row and the
         5859  +** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
         5860  +** any database changes. In other words, if the xUpdate() returns
         5861  +** SQLITE_CONSTRAINT, the database contents must be exactly as they were
         5862  +** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
         5863  +** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
         5864  +** the xUpdate method are automatically rolled back by SQLite.
  5850   5865   **
  5851   5866   ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
  5852   5867   ** structure for SQLite version 3.8.2. If a virtual table extension is
  5853   5868   ** used with an SQLite version earlier than 3.8.2, the results of attempting 
  5854   5869   ** to read or write the estimatedRows field are undefined (but are likely 
  5855   5870   ** to included crashing the application). The estimatedRows field should
  5856   5871   ** therefore only be used if [sqlite3_libversion_number()] returns a
  5857         -** value greater than or equal to 3008002.
         5872  +** value greater than or equal to 3008002. Similarly, the idxFlags field
         5873  +** was added for version 3.8.12. It may therefore only be used if
         5874  +** sqlite3_libversion_number() returns a value greater than or equal to
         5875  +** 3008012.
  5858   5876   */
  5859   5877   struct sqlite3_index_info {
  5860   5878     /* Inputs */
  5861   5879     int nConstraint;           /* Number of entries in aConstraint */
  5862   5880     struct sqlite3_index_constraint {
  5863   5881        int iColumn;              /* Column on left-hand side of constraint */
  5864   5882        unsigned char op;         /* Constraint operator */
................................................................................
  5878   5896     int idxNum;                /* Number used to identify the index */
  5879   5897     char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
  5880   5898     int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
  5881   5899     int orderByConsumed;       /* True if output is already ordered */
  5882   5900     double estimatedCost;           /* Estimated cost of using this index */
  5883   5901     /* Fields below are only available in SQLite 3.8.2 and later */
  5884   5902     sqlite3_int64 estimatedRows;    /* Estimated number of rows returned */
         5903  +  /* Fields below are only available in SQLite 3.8.12 and later */
         5904  +  int idxFlags;              /* Mask of SQLITE_INDEX_SCAN_* flags */
  5885   5905   };
  5886   5906   
         5907  +/*
         5908  +** CAPI3REF: Virtual Table Scan Flags
         5909  +*/
         5910  +#define SQLITE_INDEX_SCAN_UNIQUE      1     /* Scan visits at most 1 row */
         5911  +
  5887   5912   /*
  5888   5913   ** CAPI3REF: Virtual Table Constraint Operator Codes
  5889   5914   **
  5890   5915   ** These macros defined the allowed values for the
  5891   5916   ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
  5892   5917   ** an operator that is part of a constraint term in the wHERE clause of
  5893   5918   ** a query that uses a [virtual table].
................................................................................
 11912  11937   
 11913  11938   /* Return true if index X is a PRIMARY KEY index */
 11914  11939   #define IsPrimaryKeyIndex(X)  ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
 11915  11940   
 11916  11941   /* Return true if index X is a UNIQUE index */
 11917  11942   #define IsUniqueIndex(X)      ((X)->onError!=OE_None)
 11918  11943   
        11944  +/* The Index.aiColumn[] values are normally positive integer.  But
        11945  +** there are some negative values that have special meaning:
        11946  +*/
        11947  +#define XN_ROWID     (-1)     /* Indexed column is the rowid */
        11948  +#define XN_EXPR      (-2)     /* Indexed column is an expression */
        11949  +
 11919  11950   /*
 11920  11951   ** Each sample stored in the sqlite_stat3 table is represented in memory 
 11921  11952   ** using a structure of this type.  See documentation at the top of the
 11922  11953   ** analyze.c source file for additional information.
 11923  11954   */
 11924  11955   struct IndexSample {
 11925  11956     void *p;          /* Pointer to sampled record */
................................................................................
 13188  13219   #ifndef SQLITE_MUTEX_OMIT
 13189  13220   SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
 13190  13221   SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
 13191  13222   SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
 13192  13223   SQLITE_PRIVATE   int sqlite3MutexInit(void);
 13193  13224   SQLITE_PRIVATE   int sqlite3MutexEnd(void);
 13194  13225   #endif
 13195         -#if defined(SQLITE_MUTEX_NOOP) || \
 13196         -   (defined(SQLITE_MUTEX_PTHREADS) && \
 13197         -   (!defined(SQLITE_COMPARE_AND_SWAP) && \
 13198         -   (!defined(__GNUC__) || GCC_VERSION<4001000)))
 13199         -SQLITE_PRIVATE   void *sqlite3NoopCompareAndSwap(void * volatile *, void *, void *);
 13200         -#endif
 13201  13226   #if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
 13202  13227   SQLITE_PRIVATE   void sqlite3MemoryBarrier(void);
 13203  13228   #else
 13204  13229   # define sqlite3MemoryBarrier()
 13205  13230   #endif
 13206  13231   
 13207  13232   SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
................................................................................
 13505  13530                                           Select*,u8);
 13506  13531   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
 13507  13532   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
 13508  13533   SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
 13509  13534   SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
 13510  13535   SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
 13511  13536   # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
        13537  +# define sqlite3IsToplevel(p) ((p)->pToplevel==0)
 13512  13538   #else
 13513  13539   # define sqlite3TriggersExist(B,C,D,E,F) 0
 13514  13540   # define sqlite3DeleteTrigger(A,B)
 13515  13541   # define sqlite3DropTriggerPtr(A,B)
 13516  13542   # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
 13517  13543   # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
 13518  13544   # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
 13519  13545   # define sqlite3TriggerList(X, Y) 0
 13520  13546   # define sqlite3ParseToplevel(p) p
        13547  +# define sqlite3IsToplevel(p) 1
 13521  13548   # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
 13522  13549   #endif
 13523  13550   
 13524  13551   SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
 13525  13552   SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
 13526  13553   SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
 13527  13554   #ifndef SQLITE_OMIT_AUTHORIZATION
................................................................................
 54603  54630   
 54604  54631     rc = saveCursorKey(pCur);
 54605  54632     if( rc==SQLITE_OK ){
 54606  54633       btreeReleaseAllCursorPages(pCur);
 54607  54634       pCur->eState = CURSOR_REQUIRESEEK;
 54608  54635     }
 54609  54636   
 54610         -  invalidateOverflowCache(pCur);
        54637  +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
 54611  54638     return rc;
 54612  54639   }
 54613  54640   
 54614  54641   /* Forward reference */
 54615  54642   static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
 54616  54643   
 54617  54644   /*
................................................................................
 61566  61593       ** for which the pointer is stored within the content being copied.
 61567  61594       **
 61568  61595       ** It is critical that the child page be defragmented before being
 61569  61596       ** copied into the parent, because if the parent is page 1 then it will
 61570  61597       ** by smaller than the child due to the database header, and so all the
 61571  61598       ** free space needs to be up front.
 61572  61599       */
 61573         -    assert( nNew==1 );
        61600  +    assert( nNew==1 || CORRUPT_DB );
 61574  61601       rc = defragmentPage(apNew[0]);
 61575  61602       testcase( rc!=SQLITE_OK );
 61576  61603       assert( apNew[0]->nFree == 
 61577  61604           (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
 61578  61605         || rc!=SQLITE_OK
 61579  61606       );
 61580  61607       copyNodeContent(apNew[0], pParent, &rc);
................................................................................
 79274  79301           }
 79275  79302         }
 79276  79303   #endif
 79277  79304         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 79278  79305           int j;
 79279  79306           for(j=0; j<pIdx->nKeyCol; j++){
 79280  79307             /* FIXME: Be smarter about indexes that use expressions */
 79281         -          if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==(-2) ){
        79308  +          if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
 79282  79309               zFault = "indexed";
 79283  79310             }
 79284  79311           }
 79285  79312         }
 79286  79313         if( zFault ){
 79287  79314           sqlite3DbFree(db, zErr);
 79288  79315           zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
................................................................................
 86852  86879     Parse *pParse,  /* The parsing context */
 86853  86880     Index *pIdx,    /* The index whose column is to be loaded */
 86854  86881     int iTabCur,    /* Cursor pointing to a table row */
 86855  86882     int iIdxCol,    /* The column of the index to be loaded */
 86856  86883     int regOut      /* Store the index column value in this register */
 86857  86884   ){
 86858  86885     i16 iTabCol = pIdx->aiColumn[iIdxCol];
 86859         -  if( iTabCol>=(-1) ){
        86886  +  if( iTabCol==XN_EXPR ){
        86887  +    assert( pIdx->aColExpr );
        86888  +    assert( pIdx->aColExpr->nExpr>iIdxCol );
        86889  +    pParse->iSelfTab = iTabCur;
        86890  +    sqlite3ExprCode(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
        86891  +  }else{
 86860  86892       sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
 86861  86893                                       iTabCol, regOut);
 86862         -    return;
 86863  86894     }
 86864         -  assert( pIdx->aColExpr );
 86865         -  assert( pIdx->aColExpr->nExpr>iIdxCol );
 86866         -  pParse->iSelfTab = iTabCur;
 86867         -  sqlite3ExprCode(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
 86868  86895   }
 86869  86896   
 86870  86897   /*
 86871  86898   ** Generate code to extract the value of the iCol-th column of a table.
 86872  86899   */
 86873  86900   SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
 86874  86901     Vdbe *v,        /* The VDBE under construction */
................................................................................
 92383  92410             OP_Transaction,                    /* Opcode */
 92384  92411             iDb,                               /* P1 */
 92385  92412             DbMaskTest(pParse->writeMask,iDb), /* P2 */
 92386  92413             pParse->cookieValue[iDb],          /* P3 */
 92387  92414             db->aDb[iDb].pSchema->iGeneration  /* P4 */
 92388  92415           );
 92389  92416           if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
        92417  +        VdbeComment((v,
        92418  +              "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
 92390  92419         }
 92391  92420   #ifndef SQLITE_OMIT_VIRTUALTABLE
 92392  92421         for(i=0; i<pParse->nVtabLock; i++){
 92393  92422           char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
 92394  92423           sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
 92395  92424         }
 92396  92425         pParse->nVtabLock = 0;
................................................................................
 95325  95354     }else{
 95326  95355       sortOrderMask = 0;    /* Ignore DESC */
 95327  95356     }
 95328  95357   
 95329  95358     /* Analyze the list of expressions that form the terms of the index and
 95330  95359     ** report any errors.  In the common case where the expression is exactly
 95331  95360     ** a table column, store that column in aiColumn[].  For general expressions,
 95332         -  ** populate pIndex->aColExpr and store -2 in aiColumn[].
        95361  +  ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
 95333  95362     **
 95334  95363     ** TODO: Issue a warning if two or more columns of the index are identical.
 95335  95364     ** TODO: Issue a warning if the table primary key is used as part of the
 95336  95365     ** index key.
 95337  95366     */
 95338  95367     for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
 95339  95368       Expr *pCExpr;                  /* The i-th index expression */
................................................................................
 95354  95383           ExprList *pCopy = sqlite3ExprListDup(db, pList, 0);
 95355  95384           pIndex->aColExpr = pCopy;
 95356  95385           if( !db->mallocFailed ){
 95357  95386             assert( pCopy!=0 );
 95358  95387             pListItem = &pCopy->a[i];
 95359  95388           }
 95360  95389         }
 95361         -      j = -2;
 95362         -      pIndex->aiColumn[i] = -2;
        95390  +      j = XN_EXPR;
        95391  +      pIndex->aiColumn[i] = XN_EXPR;
 95363  95392         pIndex->uniqNotNull = 0;
 95364  95393       }else{
 95365  95394         j = pCExpr->iColumn;
 95366  95395         assert( j<=0x7fff );
 95367  95396         if( j<0 ){
 95368  95397           j = pTab->iPKey;
 95369  95398         }else if( pTab->aCol[j].notNull==0 ){
................................................................................
 95408  95437           pIndex->azColl[i] = pPk->azColl[j];
 95409  95438           pIndex->aSortOrder[i] = pPk->aSortOrder[j];
 95410  95439           i++;
 95411  95440         }
 95412  95441       }
 95413  95442       assert( i==pIndex->nColumn );
 95414  95443     }else{
 95415         -    pIndex->aiColumn[i] = -1;
        95444  +    pIndex->aiColumn[i] = XN_ROWID;
 95416  95445       pIndex->azColl[i] = "BINARY";
 95417  95446     }
 95418  95447     sqlite3DefaultRowEst(pIndex);
 95419  95448     if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
 95420  95449   
 95421  95450     if( pTab==pParse->pNewTable ){
 95422  95451       /* This routine has been called to create an automatic index as a
................................................................................
 97512  97541       **  ONEPASS_OFF:    Two-pass approach - use a FIFO for rowids/PK values.
 97513  97542       **  ONEPASS_SINGLE: One-pass approach - at most one row deleted.
 97514  97543       **  ONEPASS_MULTI:  One-pass approach - any number of rows may be deleted.
 97515  97544       */
 97516  97545       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
 97517  97546       if( pWInfo==0 ) goto delete_from_cleanup;
 97518  97547       eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
 97519         -    assert( IsVirtual(pTab)==0 || eOnePass==ONEPASS_OFF );
        97548  +    assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
 97520  97549       assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
 97521  97550     
 97522  97551       /* Keep track of the number of rows to be deleted */
 97523  97552       if( db->flags & SQLITE_CountRows ){
 97524  97553         sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
 97525  97554       }
 97526  97555     
 97527  97556       /* Extract the rowid or primary key for the current row */
 97528  97557       if( pPk ){
 97529  97558         for(i=0; i<nPk; i++){
 97530         -        assert( pPk->aiColumn[i]>=(-1) );
        97559  +        assert( pPk->aiColumn[i]>=0 );
 97531  97560           sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
 97532  97561                                           pPk->aiColumn[i], iPk+i);
 97533  97562         }
 97534  97563         iKey = iPk;
 97535  97564       }else{
 97536  97565         iKey = pParse->nMem + 1;
 97537  97566         iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
................................................................................
 97595  97624       }
 97596  97625     
 97597  97626       /* Set up a loop over the rowids/primary-keys that were found in the
 97598  97627       ** where-clause loop above.
 97599  97628       */
 97600  97629       if( eOnePass!=ONEPASS_OFF ){
 97601  97630         assert( nKey==nPk );  /* OP_Found will use an unpacked key */
 97602         -      if( aToOpen[iDataCur-iTabCur] ){
        97631  +      if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
 97603  97632           assert( pPk!=0 || pTab->pSelect!=0 );
 97604  97633           sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
 97605  97634           VdbeCoverage(v);
 97606  97635         }
 97607  97636       }else if( pPk ){
 97608  97637         addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
 97609  97638         sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
................................................................................
 97617  97646       /* Delete the row */
 97618  97647   #ifndef SQLITE_OMIT_VIRTUALTABLE
 97619  97648       if( IsVirtual(pTab) ){
 97620  97649         const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
 97621  97650         sqlite3VtabMakeWritable(pParse, pTab);
 97622  97651         sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
 97623  97652         sqlite3VdbeChangeP5(v, OE_Abort);
        97653  +      assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
 97624  97654         sqlite3MayAbort(pParse);
        97655  +      if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){
        97656  +        pParse->isMultiWrite = 0;
        97657  +      }
 97625  97658       }else
 97626  97659   #endif
 97627  97660       {
 97628  97661         int count = (pParse->nested==0);    /* True to count changes */
 97629  97662         int iIdxNoSeek = -1;
 97630  97663         if( bComplex==0 && aiCurOnePass[1]!=iDataCur ){
 97631  97664           iIdxNoSeek = aiCurOnePass[1];
................................................................................
 97955  97988     }
 97956  97989     nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
 97957  97990     regBase = sqlite3GetTempRange(pParse, nCol);
 97958  97991     if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
 97959  97992     for(j=0; j<nCol; j++){
 97960  97993       if( pPrior
 97961  97994        && pPrior->aiColumn[j]==pIdx->aiColumn[j]
 97962         -     && pPrior->aiColumn[j]>=(-1)
        97995  +     && pPrior->aiColumn[j]!=XN_EXPR
 97963  97996       ){
 97964  97997         /* This column was already computed by the previous index */
 97965  97998         continue;
 97966  97999       }
 97967  98000       sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
 97968  98001       /* If the column affinity is REAL but the number is an integer, then it
 97969  98002       ** might be stored in the table as an integer (using a compact
................................................................................
100043 100076           ** the default collation sequences for each column. */
100044 100077           int i, j;
100045 100078           for(i=0; i<nCol; i++){
100046 100079             i16 iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
100047 100080             char *zDfltColl;                  /* Def. collation for column */
100048 100081             char *zIdxCol;                    /* Name of indexed column */
100049 100082   
       100083  +          if( iCol<0 ) break; /* No foreign keys against expression indexes */
       100084  +
100050 100085             /* If the index uses a collation sequence that is different from
100051 100086             ** the default collation sequence for the column, this index is
100052 100087             ** unusable. Bail out early in this case.  */
100053 100088             zDfltColl = pParent->aCol[iCol].zColl;
100054 100089             if( !zDfltColl ){
100055 100090               zDfltColl = "BINARY";
100056 100091             }
................................................................................
100195 100230         ** none of the child key values are).
100196 100231         */
100197 100232         if( pTab==pFKey->pFrom && nIncr==1 ){
100198 100233           int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
100199 100234           for(i=0; i<nCol; i++){
100200 100235             int iChild = aiCol[i]+1+regData;
100201 100236             int iParent = pIdx->aiColumn[i]+1+regData;
       100237  +          assert( pIdx->aiColumn[i]>=0 );
100202 100238             assert( aiCol[i]!=pTab->iPKey );
100203 100239             if( pIdx->aiColumn[i]==pTab->iPKey ){
100204 100240               /* The parent key is a composite key that includes the IPK column */
100205 100241               iParent = regData;
100206 100242             }
100207 100243             sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
100208 100244             sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
................................................................................
100403 100439         pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
100404 100440       }else{
100405 100441         Expr *pEq, *pAll = 0;
100406 100442         Index *pPk = sqlite3PrimaryKeyIndex(pTab);
100407 100443         assert( pIdx!=0 );
100408 100444         for(i=0; i<pPk->nKeyCol; i++){
100409 100445           i16 iCol = pIdx->aiColumn[i];
       100446  +        assert( iCol>=0 );
100410 100447           pLeft = exprTableRegister(pParse, pTab, regData, iCol);
100411 100448           pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
100412 100449           pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
100413 100450           pAll = sqlite3ExprAnd(db, pAll, pEq);
100414 100451         }
100415 100452         pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0);
100416 100453       }
................................................................................
100722 100759         iCol = pFKey->aCol[0].iFrom;
100723 100760         aiCol = &iCol;
100724 100761       }
100725 100762       for(i=0; i<pFKey->nCol; i++){
100726 100763         if( aiCol[i]==pTab->iPKey ){
100727 100764           aiCol[i] = -1;
100728 100765         }
       100766  +      assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
100729 100767   #ifndef SQLITE_OMIT_AUTHORIZATION
100730 100768         /* Request permission to read the parent key columns. If the 
100731 100769         ** authorization callback returns SQLITE_IGNORE, behave as if any
100732 100770         ** values read from the parent table are NULL. */
100733 100771         if( db->xAuth ){
100734 100772           int rcauth;
100735 100773           char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
................................................................................
100853 100891       for(p=pTab->pFKey; p; p=p->pNextFrom){
100854 100892         for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
100855 100893       }
100856 100894       for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
100857 100895         Index *pIdx = 0;
100858 100896         sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
100859 100897         if( pIdx ){
100860         -        for(i=0; i<pIdx->nKeyCol; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
       100898  +        for(i=0; i<pIdx->nKeyCol; i++){
       100899  +          assert( pIdx->aiColumn[i]>=0 );
       100900  +          mask |= COLUMN_MASK(pIdx->aiColumn[i]);
       100901  +        }
100861 100902         }
100862 100903       }
100863 100904     }
100864 100905     return mask;
100865 100906   }
100866 100907   
100867 100908   
................................................................................
100976 101017         Token tToCol;               /* Name of column in parent table */
100977 101018         int iFromCol;               /* Idx of column in child table */
100978 101019         Expr *pEq;                  /* tFromCol = OLD.tToCol */
100979 101020   
100980 101021         iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
100981 101022         assert( iFromCol>=0 );
100982 101023         assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
       101024  +      assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
100983 101025         tToCol.z = pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName;
100984 101026         tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
100985 101027   
100986 101028         tToCol.n = sqlite3Strlen30(tToCol.z);
100987 101029         tFromCol.n = sqlite3Strlen30(tFromCol.z);
100988 101030   
100989 101031         /* Create the expression "OLD.zToCol = zFromCol". It is important
................................................................................
101285 101327         db->mallocFailed = 1;
101286 101328         return 0;
101287 101329       }
101288 101330       for(n=0; n<pIdx->nColumn; n++){
101289 101331         i16 x = pIdx->aiColumn[n];
101290 101332         if( x>=0 ){
101291 101333           pIdx->zColAff[n] = pTab->aCol[x].affinity;
101292         -      }else if( x==(-1) ){
       101334  +      }else if( x==XN_ROWID ){
101293 101335           pIdx->zColAff[n] = SQLITE_AFF_INTEGER;
101294 101336         }else{
101295 101337           char aff;
101296         -        assert( x==(-2) );
       101338  +        assert( x==XN_EXPR );
101297 101339           assert( pIdx->aColExpr!=0 );
101298 101340           aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
101299 101341           if( aff==0 ) aff = SQLITE_AFF_BLOB;
101300 101342           pIdx->zColAff[n] = aff;
101301 101343         }
101302 101344       }
101303 101345       pIdx->zColAff[n] = 0;
................................................................................
101455 101497     int memId;                 /* Register holding max rowid */
101456 101498     int addr;                  /* A VDBE address */
101457 101499     Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
101458 101500   
101459 101501     /* This routine is never called during trigger-generation.  It is
101460 101502     ** only called from the top-level */
101461 101503     assert( pParse->pTriggerTab==0 );
101462         -  assert( pParse==sqlite3ParseToplevel(pParse) );
       101504  +  assert( sqlite3IsToplevel(pParse) );
101463 101505   
101464 101506     assert( v );   /* We failed long ago if this is not so */
101465 101507     for(p = pParse->pAinc; p; p = p->pNext){
101466 101508       pDb = &db->aDb[p->iDb];
101467 101509       memId = p->regCtr;
101468 101510       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
101469 101511       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
................................................................................
102603 102645       /* Create a record for this index entry as it should appear after
102604 102646       ** the insert or update.  Store that record in the aRegIdx[ix] register
102605 102647       */
102606 102648       regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn);
102607 102649       for(i=0; i<pIdx->nColumn; i++){
102608 102650         int iField = pIdx->aiColumn[i];
102609 102651         int x;
102610         -      if( iField==(-2) ){
       102652  +      if( iField==XN_EXPR ){
102611 102653           pParse->ckBase = regNewData+1;
102612 102654           sqlite3ExprCode(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
102613 102655           pParse->ckBase = 0;
102614 102656           VdbeComment((v, "%s column %d", pIdx->zName, i));
102615 102657         }else{
102616         -        if( iField==(-1) || iField==pTab->iPKey ){
       102658  +        if( iField==XN_ROWID || iField==pTab->iPKey ){
102617 102659             if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */
102618 102660             x = regNewData;
102619 102661             regRowid =  pIdx->pPartIdxWhere ? -1 : regIdx+i;
102620 102662           }else{
102621 102663             x = iField + regNewData + 1;
102622 102664           }
102623 102665           sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
................................................................................
102668 102710           }
102669 102711         }else{
102670 102712           int x;
102671 102713           /* Extract the PRIMARY KEY from the end of the index entry and
102672 102714           ** store it in registers regR..regR+nPk-1 */
102673 102715           if( pIdx!=pPk ){
102674 102716             for(i=0; i<pPk->nKeyCol; i++){
       102717  +            assert( pPk->aiColumn[i]>=0 );
102675 102718               x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
102676 102719               sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
102677 102720               VdbeComment((v, "%s.%s", pTab->zName,
102678 102721                            pTab->aCol[pPk->aiColumn[i]].zName));
102679 102722             }
102680 102723           }
102681 102724           if( isUpdate ){
................................................................................
102689 102732             int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
102690 102733             int op = OP_Ne;
102691 102734             int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
102692 102735     
102693 102736             for(i=0; i<pPk->nKeyCol; i++){
102694 102737               char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
102695 102738               x = pPk->aiColumn[i];
       102739  +            assert( x>=0 );
102696 102740               if( i==(pPk->nKeyCol-1) ){
102697 102741                 addrJump = addrUniqueOk;
102698 102742                 op = OP_Eq;
102699 102743               }
102700 102744               sqlite3VdbeAddOp4(v, op, 
102701 102745                   regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
102702 102746               );
................................................................................
102940 102984     if( pDest->onError!=pSrc->onError ){
102941 102985       return 0;   /* Different conflict resolution strategies */
102942 102986     }
102943 102987     for(i=0; i<pSrc->nKeyCol; i++){
102944 102988       if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
102945 102989         return 0;   /* Different columns indexed */
102946 102990       }
102947         -    if( pSrc->aiColumn[i]==(-2) ){
       102991  +    if( pSrc->aiColumn[i]==XN_EXPR ){
102948 102992         assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
102949 102993         if( sqlite3ExprCompare(pSrc->aColExpr->a[i].pExpr,
102950 102994                                pDest->aColExpr->a[i].pExpr, -1)!=0 ){
102951 102995           return 0;   /* Different expressions in the index */
102952 102996         }
102953 102997       }
102954 102998       if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
................................................................................
106745 106789             ** or (2) the next entry has a different key */
106746 106790             if( IsUniqueIndex(pIdx) ){
106747 106791               int uniqOk = sqlite3VdbeMakeLabel(v);
106748 106792               int jmp6;
106749 106793               int kk;
106750 106794               for(kk=0; kk<pIdx->nKeyCol; kk++){
106751 106795                 int iCol = pIdx->aiColumn[kk];
106752         -              assert( iCol!=(-1) && iCol<pTab->nCol );
       106796  +              assert( iCol!=XN_ROWID && iCol<pTab->nCol );
106753 106797                 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
106754 106798                 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
106755 106799                 VdbeCoverage(v);
106756 106800               }
106757 106801               jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
106758 106802               sqlite3VdbeGoto(v, uniqOk);
106759 106803               sqlite3VdbeJumpHere(v, jmp6);
................................................................................
112265 112309   
112266 112310     /* Look up every table named in the FROM clause of the select.  If
112267 112311     ** an entry of the FROM clause is a subquery instead of a table or view,
112268 112312     ** then create a transient table structure to describe the subquery.
112269 112313     */
112270 112314     for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
112271 112315       Table *pTab;
112272         -    assert( pFrom->fg.isRecursive==0 || pFrom->pTab );
       112316  +    assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
112273 112317       if( pFrom->fg.isRecursive ) continue;
112274         -    if( pFrom->pTab!=0 ){
112275         -      /* This statement has already been prepared.  There is no need
112276         -      ** to go further. */
112277         -      assert( i==0 );
112278         -#ifndef SQLITE_OMIT_CTE
112279         -      selectPopWith(pWalker, p);
112280         -#endif
112281         -      return WRC_Prune;
112282         -    }
       112318  +    assert( pFrom->pTab==0 );
112283 112319   #ifndef SQLITE_OMIT_CTE
112284 112320       if( withExpand(pWalker, pFrom) ) return WRC_Abort;
112285 112321       if( pFrom->pTab ) {} else
112286 112322   #endif
112287 112323       if( pFrom->zName==0 ){
112288 112324   #ifndef SQLITE_OMIT_SUBQUERY
112289 112325         Select *pSel = pFrom->pSelect;
................................................................................
112567 112603   static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
112568 112604     Parse *pParse;
112569 112605     int i;
112570 112606     SrcList *pTabList;
112571 112607     struct SrcList_item *pFrom;
112572 112608   
112573 112609     assert( p->selFlags & SF_Resolved );
112574         -  if( (p->selFlags & SF_HasTypeInfo)==0 ){
112575         -    p->selFlags |= SF_HasTypeInfo;
112576         -    pParse = pWalker->pParse;
112577         -    pTabList = p->pSrc;
112578         -    for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
112579         -      Table *pTab = pFrom->pTab;
112580         -      if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
112581         -        /* A sub-query in the FROM clause of a SELECT */
112582         -        Select *pSel = pFrom->pSelect;
112583         -        if( pSel ){
112584         -          while( pSel->pPrior ) pSel = pSel->pPrior;
112585         -          selectAddColumnTypeAndCollation(pParse, pTab, pSel);
112586         -        }
       112610  +  assert( (p->selFlags & SF_HasTypeInfo)==0 );
       112611  +  p->selFlags |= SF_HasTypeInfo;
       112612  +  pParse = pWalker->pParse;
       112613  +  pTabList = p->pSrc;
       112614  +  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
       112615  +    Table *pTab = pFrom->pTab;
       112616  +    assert( pTab!=0 );
       112617  +    if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
       112618  +      /* A sub-query in the FROM clause of a SELECT */
       112619  +      Select *pSel = pFrom->pSelect;
       112620  +      if( pSel ){
       112621  +        while( pSel->pPrior ) pSel = pSel->pPrior;
       112622  +        selectAddColumnTypeAndCollation(pParse, pTab, pSel);
112587 112623         }
112588 112624       }
112589 112625     }
112590 112626   }
112591 112627   #endif
112592 112628   
112593 112629   
................................................................................
115137 115173     int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
115138 115174     int iEph = 0;          /* Ephemeral table holding all primary key values */
115139 115175     int nKey = 0;          /* Number of elements in regKey for WITHOUT ROWID */
115140 115176     int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
115141 115177   
115142 115178     /* Register Allocations */
115143 115179     int regRowCount = 0;   /* A count of rows changed */
115144         -  int regOldRowid;       /* The old rowid */
115145         -  int regNewRowid;       /* The new rowid */
115146         -  int regNew;            /* Content of the NEW.* table in triggers */
       115180  +  int regOldRowid = 0;   /* The old rowid */
       115181  +  int regNewRowid = 0;   /* The new rowid */
       115182  +  int regNew = 0;        /* Content of the NEW.* table in triggers */
115147 115183     int regOld = 0;        /* Content of OLD.* table in triggers */
115148 115184     int regRowSet = 0;     /* Rowset of rows to be updated */
115149 115185     int regKey = 0;        /* composite PRIMARY KEY value */
115150 115186   
115151 115187     memset(&sContext, 0, sizeof(sContext));
115152 115188     db = pParse->db;
115153 115189     if( pParse->nErr || db->mallocFailed ){
................................................................................
115303 115339   
115304 115340     /* Begin generating code. */
115305 115341     v = sqlite3GetVdbe(pParse);
115306 115342     if( v==0 ) goto update_cleanup;
115307 115343     if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
115308 115344     sqlite3BeginWriteOperation(pParse, 1, iDb);
115309 115345   
115310         -#ifndef SQLITE_OMIT_VIRTUALTABLE
115311         -  /* Virtual tables must be handled separately */
115312         -  if( IsVirtual(pTab) ){
115313         -    updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
115314         -                       pWhere, onError);
115315         -    pWhere = 0;
115316         -    pTabList = 0;
115317         -    goto update_cleanup;
115318         -  }
115319         -#endif
115320         -
115321 115346     /* Allocate required registers. */
115322         -  regRowSet = ++pParse->nMem;
115323         -  regOldRowid = regNewRowid = ++pParse->nMem;
115324         -  if( chngPk || pTrigger || hasFK ){
115325         -    regOld = pParse->nMem + 1;
       115347  +  if( !IsVirtual(pTab) ){
       115348  +    regRowSet = ++pParse->nMem;
       115349  +    regOldRowid = regNewRowid = ++pParse->nMem;
       115350  +    if( chngPk || pTrigger || hasFK ){
       115351  +      regOld = pParse->nMem + 1;
       115352  +      pParse->nMem += pTab->nCol;
       115353  +    }
       115354  +    if( chngKey || pTrigger || hasFK ){
       115355  +      regNewRowid = ++pParse->nMem;
       115356  +    }
       115357  +    regNew = pParse->nMem + 1;
115326 115358       pParse->nMem += pTab->nCol;
115327 115359     }
115328         -  if( chngKey || pTrigger || hasFK ){
115329         -    regNewRowid = ++pParse->nMem;
115330         -  }
115331         -  regNew = pParse->nMem + 1;
115332         -  pParse->nMem += pTab->nCol;
115333 115360   
115334 115361     /* Start the view context. */
115335 115362     if( isView ){
115336 115363       sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
115337 115364     }
115338 115365   
115339 115366     /* If we are trying to update a view, realize that view into
................................................................................
115347 115374   
115348 115375     /* Resolve the column names in all the expressions in the
115349 115376     ** WHERE clause.
115350 115377     */
115351 115378     if( sqlite3ResolveExprNames(&sNC, pWhere) ){
115352 115379       goto update_cleanup;
115353 115380     }
       115381  +
       115382  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       115383  +  /* Virtual tables must be handled separately */
       115384  +  if( IsVirtual(pTab) ){
       115385  +    updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
       115386  +                       pWhere, onError);
       115387  +    goto update_cleanup;
       115388  +  }
       115389  +#endif
115354 115390   
115355 115391     /* Begin the database scan
115356 115392     */
115357 115393     if( HasRowid(pTab) ){
115358 115394       sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
115359 115395       pWInfo = sqlite3WhereBegin(
115360 115396           pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur
................................................................................
115387 115423       addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
115388 115424       sqlite3VdbeSetP4KeyInfo(pParse, pPk);
115389 115425       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 
115390 115426                                  WHERE_ONEPASS_DESIRED, iIdxCur);
115391 115427       if( pWInfo==0 ) goto update_cleanup;
115392 115428       okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
115393 115429       for(i=0; i<nPk; i++){
115394         -      assert( pPk->aiColumn[i]>=(-1) );
       115430  +      assert( pPk->aiColumn[i]>=0 );
115395 115431         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
115396 115432                                         iPk+i);
115397 115433       }
115398 115434       if( okOnePass ){
115399 115435         sqlite3VdbeChangeToNoop(v, addrOpen);
115400 115436         nKey = nPk;
115401 115437         regKey = iPk;
................................................................................
115510 115546     ** the database after the BEFORE triggers are fired anyway (as the trigger 
115511 115547     ** may have modified them). So not loading those that are not going to
115512 115548     ** be used eliminates some redundant opcodes.
115513 115549     */
115514 115550     newmask = sqlite3TriggerColmask(
115515 115551         pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
115516 115552     );
115517         -  /*sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);*/
115518 115553     for(i=0; i<pTab->nCol; i++){
115519 115554       if( i==pTab->iPKey ){
115520 115555         sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
115521 115556       }else{
115522 115557         j = aXRef[i];
115523 115558         if( j>=0 ){
115524 115559           sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
................................................................................
115688 115723    #undef pTrigger
115689 115724   #endif
115690 115725   
115691 115726   #ifndef SQLITE_OMIT_VIRTUALTABLE
115692 115727   /*
115693 115728   ** Generate code for an UPDATE of a virtual table.
115694 115729   **
115695         -** The strategy is that we create an ephemeral table that contains
       115730  +** There are two possible strategies - the default and the special 
       115731  +** "onepass" strategy. Onepass is only used if the virtual table 
       115732  +** implementation indicates that pWhere may match at most one row.
       115733  +**
       115734  +** The default strategy is to create an ephemeral table that contains
115696 115735   ** for each row to be changed:
115697 115736   **
115698 115737   **   (A)  The original rowid of that row.
115699         -**   (B)  The revised rowid for the row. (note1)
       115738  +**   (B)  The revised rowid for the row.
115700 115739   **   (C)  The content of every column in the row.
115701 115740   **
115702         -** Then we loop over this ephemeral table and for each row in
115703         -** the ephemeral table call VUpdate.
       115741  +** Then loop through the contents of this ephemeral table executing a
       115742  +** VUpdate for each row. When finished, drop the ephemeral table.
115704 115743   **
115705         -** When finished, drop the ephemeral table.
115706         -**
115707         -** (note1) Actually, if we know in advance that (A) is always the same
115708         -** as (B) we only store (A), then duplicate (A) when pulling
115709         -** it out of the ephemeral table before calling VUpdate.
       115744  +** The "onepass" strategy does not use an ephemeral table. Instead, it
       115745  +** stores the same values (A, B and C above) in a register array and
       115746  +** makes a single invocation of VUpdate.
115710 115747   */
115711 115748   static void updateVirtualTable(
115712 115749     Parse *pParse,       /* The parsing context */
115713 115750     SrcList *pSrc,       /* The virtual table to be modified */
115714 115751     Table *pTab,         /* The virtual table */
115715 115752     ExprList *pChanges,  /* The columns to change in the UPDATE statement */
115716 115753     Expr *pRowid,        /* Expression used to recompute the rowid */
115717 115754     int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
115718 115755     Expr *pWhere,        /* WHERE clause of the UPDATE statement */
115719 115756     int onError          /* ON CONFLICT strategy */
115720 115757   ){
115721 115758     Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
115722         -  ExprList *pEList = 0;     /* The result set of the SELECT statement */
115723         -  Select *pSelect = 0;      /* The SELECT statement */
115724         -  Expr *pExpr;              /* Temporary expression */
115725 115759     int ephemTab;             /* Table holding the result of the SELECT */
115726 115760     int i;                    /* Loop counter */
115727         -  int addr;                 /* Address of top of loop */
115728         -  int iReg;                 /* First register in set passed to OP_VUpdate */
115729 115761     sqlite3 *db = pParse->db; /* Database connection */
115730 115762     const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
115731         -  SelectDest dest;
       115763  +  WhereInfo *pWInfo;
       115764  +  int nArg = 2 + pTab->nCol;      /* Number of arguments to VUpdate */
       115765  +  int regArg;                     /* First register in VUpdate arg array */
       115766  +  int regRec;                     /* Register in which to assemble record */
       115767  +  int regRowid;                   /* Register for ephem table rowid */
       115768  +  int iCsr = pSrc->a[0].iCursor;  /* Cursor used for virtual table scan */
       115769  +  int aDummy[2];                  /* Unused arg for sqlite3WhereOkOnePass() */
       115770  +  int bOnePass;                   /* True to use onepass strategy */
       115771  +  int addr;                       /* Address of OP_OpenEphemeral */
115732 115772   
115733         -  /* Construct the SELECT statement that will find the new values for
115734         -  ** all updated rows. 
115735         -  */
115736         -  pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
115737         -  if( pRowid ){
115738         -    pEList = sqlite3ExprListAppend(pParse, pEList,
115739         -                                   sqlite3ExprDup(db, pRowid, 0));
115740         -  }
115741         -  assert( pTab->iPKey<0 );
115742         -  for(i=0; i<pTab->nCol; i++){
115743         -    if( aXRef[i]>=0 ){
115744         -      pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
115745         -    }else{
115746         -      pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
115747         -    }
115748         -    pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
115749         -  }
115750         -  pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
115751         -  
115752         -  /* Create the ephemeral table into which the update results will
115753         -  ** be stored.
115754         -  */
       115773  +  /* Allocate nArg registers to martial the arguments to VUpdate. Then
       115774  +  ** create and open the ephemeral table in which the records created from
       115775  +  ** these arguments will be temporarily stored. */
115755 115776     assert( v );
115756 115777     ephemTab = pParse->nTab++;
       115778  +  addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg);
       115779  +  regArg = pParse->nMem + 1;
       115780  +  pParse->nMem += nArg;
       115781  +  regRec = ++pParse->nMem;
       115782  +  regRowid = ++pParse->nMem;
115757 115783   
115758         -  /* fill the ephemeral table 
115759         -  */
115760         -  sqlite3SelectDestInit(&dest, SRT_EphemTab, ephemTab);
115761         -  sqlite3Select(pParse, pSelect, &dest);
       115784  +  /* Start scanning the virtual table */
       115785  +  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0,0,WHERE_ONEPASS_DESIRED,0);
       115786  +  if( pWInfo==0 ) return;
115762 115787   
115763         -  /* Generate code to scan the ephemeral table and call VUpdate. */
115764         -  iReg = ++pParse->nMem;
115765         -  pParse->nMem += pTab->nCol+1;
115766         -  addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); VdbeCoverage(v);
115767         -  sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
115768         -  sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
       115788  +  /* Populate the argument registers. */
       115789  +  sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg);
       115790  +  if( pRowid ){
       115791  +    sqlite3ExprCode(pParse, pRowid, regArg+1);
       115792  +  }else{
       115793  +    sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1);
       115794  +  }
115769 115795     for(i=0; i<pTab->nCol; i++){
115770         -    sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
       115796  +    if( aXRef[i]>=0 ){
       115797  +      sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
       115798  +    }else{
       115799  +      sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i);
       115800  +    }
       115801  +  }
       115802  +
       115803  +  bOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
       115804  +
       115805  +  if( bOnePass ){
       115806  +    /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
       115807  +    ** above. Also, if this is a top-level parse (not a trigger), clear the
       115808  +    ** multi-write flag so that the VM does not open a statement journal */
       115809  +    sqlite3VdbeChangeToNoop(v, addr);
       115810  +    if( sqlite3IsToplevel(pParse) ){
       115811  +      pParse->isMultiWrite = 0;
       115812  +    }
       115813  +  }else{
       115814  +    /* Create a record from the argument register contents and insert it into
       115815  +    ** the ephemeral table. */
       115816  +    sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
       115817  +    sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
       115818  +    sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
       115819  +  }
       115820  +
       115821  +
       115822  +  if( bOnePass==0 ){
       115823  +    /* End the virtual table scan */
       115824  +    sqlite3WhereEnd(pWInfo);
       115825  +
       115826  +    /* Begin scannning through the ephemeral table. */
       115827  +    addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
       115828  +
       115829  +    /* Extract arguments from the current row of the ephemeral table and 
       115830  +    ** invoke the VUpdate method.  */
       115831  +    for(i=0; i<nArg; i++){
       115832  +      sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i, regArg+i);
       115833  +    }
115771 115834     }
115772 115835     sqlite3VtabMakeWritable(pParse, pTab);
115773         -  sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
       115836  +  sqlite3VdbeAddOp4(v, OP_VUpdate, 0, nArg, regArg, pVTab, P4_VTAB);
115774 115837     sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
115775 115838     sqlite3MayAbort(pParse);
115776         -  sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
115777         -  sqlite3VdbeJumpHere(v, addr);
115778         -  sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
115779 115839   
115780         -  /* Cleanup */
115781         -  sqlite3SelectDelete(db, pSelect);  
       115840  +  /* End of the ephemeral table scan. Or, if using the onepass strategy,
       115841  +  ** jump to here if the scan visited zero rows. */
       115842  +  if( bOnePass==0 ){
       115843  +    sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
       115844  +    sqlite3VdbeJumpHere(v, addr);
       115845  +    sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
       115846  +  }else{
       115847  +    sqlite3WhereEnd(pWInfo);
       115848  +  }
115782 115849   }
115783 115850   #endif /* SQLITE_OMIT_VIRTUALTABLE */
115784 115851   
115785 115852   /************** End of update.c **********************************************/
115786 115853   /************** Begin file vacuum.c ******************************************/
115787 115854   /*
115788 115855   ** 2003 April 6
................................................................................
117095 117162   
117096 117163       /* Invoke the xBegin method. If successful, add the vtab to the 
117097 117164       ** sqlite3.aVTrans[] array. */
117098 117165       rc = growVTrans(db);
117099 117166       if( rc==SQLITE_OK ){
117100 117167         rc = pModule->xBegin(pVTab->pVtab);
117101 117168         if( rc==SQLITE_OK ){
       117169  +        int iSvpt = db->nStatement + db->nSavepoint;
117102 117170           addToVTrans(db, pVTab);
       117171  +        if( iSvpt ) rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, iSvpt-1);
117103 117172         }
117104 117173       }
117105 117174     }
117106 117175     return rc;
117107 117176   }
117108 117177   
117109 117178   /*
................................................................................
117953 118022   }
117954 118023   
117955 118024   /*
117956 118025   ** Return the name of the i-th column of the pIdx index.
117957 118026   */
117958 118027   static const char *explainIndexColumnName(Index *pIdx, int i){
117959 118028     i = pIdx->aiColumn[i];
117960         -  if( i==(-2) ) return "<expr>";
117961         -  if( i==(-1) ) return "rowid";
       118029  +  if( i==XN_EXPR ) return "<expr>";
       118030  +  if( i==XN_ROWID ) return "rowid";
117962 118031     return pIdx->pTable->aCol[i].zName;
117963 118032   }
117964 118033   
117965 118034   /*
117966 118035   ** Argument pLevel describes a strategy for scanning table pTab. This 
117967 118036   ** function appends text to pStr that describes the subset of table
117968 118037   ** rows scanned by the strategy in the form of an SQL expression.
................................................................................
118421 118490       pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
118422 118491                               iIdxCur, 0, regBase, nSkip);
118423 118492       VdbeCoverageIf(v, bRev==0);
118424 118493       VdbeCoverageIf(v, bRev!=0);
118425 118494       sqlite3VdbeJumpHere(v, j);
118426 118495       for(j=0; j<nSkip; j++){
118427 118496         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
118428         -      testcase( pIdx->aiColumn[j]==(-2) );
       118497  +      testcase( pIdx->aiColumn[j]==XN_EXPR );
118429 118498         VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
118430 118499       }
118431 118500     }    
118432 118501   
118433 118502     /* Evaluate the equality constraints
118434 118503     */
118435 118504     assert( zAff==0 || (int)strlen(zAff)>=nEq );
................................................................................
118607 118676       VdbeCoverage(v);
118608 118677       pLoop->u.vtab.needFree = 0;
118609 118678       for(j=0; j<nConstraint && j<16; j++){
118610 118679         if( (pLoop->u.vtab.omitMask>>j)&1 ){
118611 118680           disableTerm(pLevel, pLoop->aLTerm[j]);
118612 118681         }
118613 118682       }
118614         -    pLevel->op = OP_VNext;
118615 118683       pLevel->p1 = iCur;
       118684  +    pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
118616 118685       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
118617 118686       sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
118618 118687       sqlite3ExprCachePop(pParse);
118619 118688     }else
118620 118689   #endif /* SQLITE_OMIT_VIRTUALTABLE */
118621 118690   
118622 118691     if( (pLoop->wsFlags & WHERE_IPK)!=0
................................................................................
120952 121021     WhereClause *pWC;    /* Shorthand for pScan->pWC */
120953 121022     WhereTerm *pTerm;    /* The term being tested */
120954 121023     int k = pScan->k;    /* Where to start scanning */
120955 121024   
120956 121025     while( pScan->iEquiv<=pScan->nEquiv ){
120957 121026       iCur = pScan->aiCur[pScan->iEquiv-1];
120958 121027       iColumn = pScan->aiColumn[pScan->iEquiv-1];
120959         -    if( iColumn==(-2) && pScan->pIdxExpr==0 ) return 0;
       121028  +    if( iColumn==XN_EXPR && pScan->pIdxExpr==0 ) return 0;
120960 121029       while( (pWC = pScan->pWC)!=0 ){
120961 121030         for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
120962 121031           if( pTerm->leftCursor==iCur
120963 121032            && pTerm->u.leftColumn==iColumn
120964         -         && (iColumn!=(-2)
       121033  +         && (iColumn!=XN_EXPR
120965 121034                || sqlite3ExprCompare(pTerm->pExpr->pLeft,pScan->pIdxExpr,iCur)==0)
120966 121035            && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
120967 121036           ){
120968 121037             if( (pTerm->eOperator & WO_EQUIV)!=0
120969 121038              && pScan->nEquiv<ArraySize(pScan->aiCur)
120970 121039              && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN
120971 121040             ){
................................................................................
121051 121120     /* memset(pScan, 0, sizeof(*pScan)); */
121052 121121     pScan->pOrigWC = pWC;
121053 121122     pScan->pWC = pWC;
121054 121123     pScan->pIdxExpr = 0;
121055 121124     if( pIdx ){
121056 121125       j = iColumn;
121057 121126       iColumn = pIdx->aiColumn[j];
121058         -    if( iColumn==(-2) ) pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
       121127  +    if( iColumn==XN_EXPR ) pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
121059 121128     }
121060 121129     if( pIdx && iColumn>=0 ){
121061 121130       pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
121062 121131       pScan->zCollName = pIdx->azColl[j];
121063 121132     }else{
121064 121133       pScan->idxaff = 0;
121065 121134       pScan->zCollName = 0;
................................................................................
121490 121559       for(i=BMS-1; i<pTable->nCol; i++){
121491 121560         pIdx->aiColumn[n] = i;
121492 121561         pIdx->azColl[n] = "BINARY";
121493 121562         n++;
121494 121563       }
121495 121564     }
121496 121565     assert( n==nKeyCol );
121497         -  pIdx->aiColumn[n] = -1;
       121566  +  pIdx->aiColumn[n] = XN_ROWID;
121498 121567     pIdx->azColl[n] = "BINARY";
121499 121568   
121500 121569     /* Create the automatic index */
121501 121570     assert( pLevel->iIdxCur>=0 );
121502 121571     pLevel->iIdxCur = pParse->nTab++;
121503 121572     sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
121504 121573     sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
................................................................................
123005 123074         assert( nIn>0 );  /* RHS always has 2 or more terms...  The parser
123006 123075                           ** changes "x IN (?)" into "x=?". */
123007 123076   
123008 123077       }else if( eOp & (WO_EQ|WO_IS) ){
123009 123078         int iCol = pProbe->aiColumn[saved_nEq];
123010 123079         pNew->wsFlags |= WHERE_COLUMN_EQ;
123011 123080         assert( saved_nEq==pNew->u.btree.nEq );
123012         -      if( iCol==(-1) || (iCol>0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1) ){
       123081  +      if( iCol==XN_ROWID 
       123082  +       || (iCol>0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
       123083  +      ){
123013 123084           if( iCol>=0 && pProbe->uniqNotNull==0 ){
123014 123085             pNew->wsFlags |= WHERE_UNQ_WANTED;
123015 123086           }else{
123016 123087             pNew->wsFlags |= WHERE_ONEROW;
123017 123088           }
123018 123089         }
123019 123090       }else if( eOp & WO_ISNULL ){
................................................................................
123205 123276       if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
123206 123277         if( pExpr->iColumn<0 ) return 1;
123207 123278         for(jj=0; jj<pIndex->nKeyCol; jj++){
123208 123279           if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
123209 123280         }
123210 123281       }else if( (aColExpr = pIndex->aColExpr)!=0 ){
123211 123282         for(jj=0; jj<pIndex->nKeyCol; jj++){
123212         -        if( pIndex->aiColumn[jj]!=(-2) ) continue;
       123283  +        if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
123213 123284           if( sqlite3ExprCompare(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
123214 123285             return 1;
123215 123286           }
123216 123287         }
123217 123288       }
123218 123289     }
123219 123290     return 0;
................................................................................
123602 123673       if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
123603 123674       pIdxInfo->idxStr = 0;
123604 123675       pIdxInfo->idxNum = 0;
123605 123676       pIdxInfo->needToFreeIdxStr = 0;
123606 123677       pIdxInfo->orderByConsumed = 0;
123607 123678       pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
123608 123679       pIdxInfo->estimatedRows = 25;
       123680  +    pIdxInfo->idxFlags = 0;
123609 123681       rc = vtabBestIndex(pParse, pTab, pIdxInfo);
123610 123682       if( rc ) goto whereLoopAddVtab_exit;
123611 123683       pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
123612 123684       pNew->prereq = mExtra;
123613 123685       mxTerm = -1;
123614 123686       assert( pNew->nLSlot>=nConstraint );
123615 123687       for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
................................................................................
123647 123719             }
123648 123720             /* A virtual table that is constrained by an IN clause may not
123649 123721             ** consume the ORDER BY clause because (1) the order of IN terms
123650 123722             ** is not necessarily related to the order of output terms and
123651 123723             ** (2) Multiple outputs from a single IN value will not merge
123652 123724             ** together.  */
123653 123725             pIdxInfo->orderByConsumed = 0;
       123726  +          pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
123654 123727           }
123655 123728         }
123656 123729       }
123657 123730       if( i>=nConstraint ){
123658 123731         pNew->nLTerm = mxTerm+1;
123659 123732         assert( pNew->nLTerm<=pNew->nLSlot );
123660 123733         pNew->u.vtab.idxNum = pIdxInfo->idxNum;
................................................................................
123662 123735         pIdxInfo->needToFreeIdxStr = 0;
123663 123736         pNew->u.vtab.idxStr = pIdxInfo->idxStr;
123664 123737         pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
123665 123738                                         pIdxInfo->nOrderBy : 0);
123666 123739         pNew->rSetup = 0;
123667 123740         pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
123668 123741         pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
       123742  +
       123743  +      /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
       123744  +      ** that the scan will visit at most one row. Clear it otherwise. */
       123745  +      if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
       123746  +        pNew->wsFlags |= WHERE_ONEROW;
       123747  +      }else{
       123748  +        pNew->wsFlags &= ~WHERE_ONEROW;
       123749  +      }
123669 123750         whereLoopInsert(pBuilder, pNew);
123670 123751         if( pNew->u.vtab.needFree ){
123671 123752           sqlite3_free(pNew->u.vtab.idxStr);
123672 123753           pNew->u.vtab.needFree = 0;
123673 123754         }
123674 123755       }
123675 123756     }  
................................................................................
123983 124064           nColumn = 1;
123984 124065         }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
123985 124066           return 0;
123986 124067         }else{
123987 124068           nKeyCol = pIndex->nKeyCol;
123988 124069           nColumn = pIndex->nColumn;
123989 124070           assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
123990         -        assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable));
       124071  +        assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
       124072  +                          || !HasRowid(pIndex->pTable));
123991 124073           isOrderDistinct = IsUniqueIndex(pIndex);
123992 124074         }
123993 124075   
123994 124076         /* Loop through all columns of the index and deal with the ones
123995 124077         ** that are not constrained by == or IN.
123996 124078         */
123997 124079         rev = revSet = 0;
................................................................................
124015 124097           ** (revIdx) for the j-th column of the index.
124016 124098           */
124017 124099           if( pIndex ){
124018 124100             iColumn = pIndex->aiColumn[j];
124019 124101             revIdx = pIndex->aSortOrder[j];
124020 124102             if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
124021 124103           }else{
124022         -          iColumn = -1;
       124104  +          iColumn = XN_ROWID;
124023 124105             revIdx = 0;
124024 124106           }
124025 124107   
124026 124108           /* An unconstrained column that might be NULL means that this
124027 124109           ** WhereLoop is not well-ordered
124028 124110           */
124029 124111           if( isOrderDistinct
................................................................................
125315 125397           if( pOp->p1!=pLevel->iTabCur ) continue;
125316 125398           if( pOp->opcode==OP_Column ){
125317 125399             int x = pOp->p2;
125318 125400             assert( pIdx->pTable==pTab );
125319 125401             if( !HasRowid(pTab) ){
125320 125402               Index *pPk = sqlite3PrimaryKeyIndex(pTab);
125321 125403               x = pPk->aiColumn[x];
       125404  +            assert( x>=0 );
125322 125405             }
125323 125406             x = sqlite3ColumnOfIndex(pIdx, x);
125324 125407             if( x>=0 ){
125325 125408               pOp->p2 = x;
125326 125409               pOp->p1 = pLevel->iIdxCur;
125327 125410             }
125328 125411             assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
................................................................................
135139 135222       int nPrefix;                  /* Prefix length (0 for main terms index) */
135140 135223       Fts3Hash hPending;            /* Pending terms table for this index */
135141 135224     } *aIndex;
135142 135225     int nMaxPendingData;            /* Max pending data before flush to disk */
135143 135226     int nPendingData;               /* Current bytes of pending data */
135144 135227     sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
135145 135228     int iPrevLangid;                /* Langid of recently inserted document */
       135229  +  int bPrevDelete;                /* True if last operation was a delete */
135146 135230   
135147 135231   #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
135148 135232     /* State variables used for validating that the transaction control
135149 135233     ** methods of the virtual table are called at appropriate times.  These
135150 135234     ** values do not contribute to FTS functionality; they are used for
135151 135235     ** verifying the operation of the SQLite core.
135152 135236     */
................................................................................
136713 136797   static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
136714 136798   #if SQLITE_VERSION_NUMBER>=3008002
136715 136799     if( sqlite3_libversion_number()>=3008002 ){
136716 136800       pIdxInfo->estimatedRows = nRow;
136717 136801     }
136718 136802   #endif
136719 136803   }
       136804  +
       136805  +/*
       136806  +** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
       136807  +** extension is currently being used by a version of SQLite too old to
       136808  +** support index-info flags. In that case this function is a no-op.
       136809  +*/
       136810  +static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
       136811  +#if SQLITE_VERSION_NUMBER>=3008012
       136812  +  if( sqlite3_libversion_number()>=3008012 ){
       136813  +    pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
       136814  +  }
       136815  +#endif
       136816  +}
136720 136817   
136721 136818   /* 
136722 136819   ** Implementation of the xBestIndex method for FTS3 tables. There
136723 136820   ** are three possible strategies, in order of preference:
136724 136821   **
136725 136822   **   1. Direct lookup by rowid or docid. 
136726 136823   **   2. Full-text search using a MATCH operator on a non-docid column.
................................................................................
136803 136900           case SQLITE_INDEX_CONSTRAINT_LE:
136804 136901           case SQLITE_INDEX_CONSTRAINT_LT:
136805 136902             iDocidLe = i;
136806 136903             break;
136807 136904         }
136808 136905       }
136809 136906     }
       136907  +
       136908  +  /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
       136909  +  if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
136810 136910   
136811 136911     iIdx = 1;
136812 136912     if( iCons>=0 ){
136813 136913       pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
136814 136914       pInfo->aConstraintUsage[iCons].omit = 1;
136815 136915     } 
136816 136916     if( iLangidCons>=0 ){
................................................................................
146049 146149   /* 
146050 146150   ** Calling this function indicates that subsequent calls to 
146051 146151   ** fts3PendingTermsAdd() are to add term/position-list pairs for the
146052 146152   ** contents of the document with docid iDocid.
146053 146153   */
146054 146154   static int fts3PendingTermsDocid(
146055 146155     Fts3Table *p,                   /* Full-text table handle */
       146156  +  int bDelete,                    /* True if this op is a delete */
146056 146157     int iLangid,                    /* Language id of row being written */
146057 146158     sqlite_int64 iDocid             /* Docid of row being written */
146058 146159   ){
146059 146160     assert( iLangid>=0 );
       146161  +  assert( bDelete==1 || bDelete==0 );
146060 146162   
146061 146163     /* TODO(shess) Explore whether partially flushing the buffer on
146062 146164     ** forced-flush would provide better performance.  I suspect that if
146063 146165     ** we ordered the doclists by size and flushed the largest until the
146064 146166     ** buffer was half empty, that would let the less frequent terms
146065 146167     ** generate longer doclists.
146066 146168     */
146067         -  if( iDocid<=p->iPrevDocid 
       146169  +  if( iDocid<p->iPrevDocid 
       146170  +   || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
146068 146171      || p->iPrevLangid!=iLangid
146069 146172      || p->nPendingData>p->nMaxPendingData 
146070 146173     ){
146071 146174       int rc = sqlite3Fts3PendingTermsFlush(p);
146072 146175       if( rc!=SQLITE_OK ) return rc;
146073 146176     }
146074 146177     p->iPrevDocid = iDocid;
146075 146178     p->iPrevLangid = iLangid;
       146179  +  p->bPrevDelete = bDelete;
146076 146180     return SQLITE_OK;
146077 146181   }
146078 146182   
146079 146183   /*
146080 146184   ** Discard the contents of the pending-terms hash tables. 
146081 146185   */
146082 146186   SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
................................................................................
146258 146362     assert( *pbFound==0 );
146259 146363     if( *pRC ) return;
146260 146364     rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
146261 146365     if( rc==SQLITE_OK ){
146262 146366       if( SQLITE_ROW==sqlite3_step(pSelect) ){
146263 146367         int i;
146264 146368         int iLangid = langidFromSelect(p, pSelect);
146265         -      rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pSelect, 0));
       146369  +      i64 iDocid = sqlite3_column_int64(pSelect, 0);
       146370  +      rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
146266 146371         for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
146267 146372           int iCol = i-1;
146268 146373           if( p->abNotindexed[iCol]==0 ){
146269 146374             const char *zText = (const char *)sqlite3_column_text(pSelect, i);
146270 146375             rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
146271 146376             aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
146272 146377           }
................................................................................
148701 148806           aSzDel = &aSzIns[p->nColumn+1];
148702 148807         }
148703 148808       }
148704 148809   
148705 148810       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
148706 148811         int iCol;
148707 148812         int iLangid = langidFromSelect(p, pStmt);
148708         -      rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pStmt, 0));
       148813  +      rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
148709 148814         memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
148710 148815         for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
148711 148816           if( p->abNotindexed[iCol]==0 ){
148712 148817             const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
148713 148818             rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
148714 148819             aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
148715 148820           }
................................................................................
150806 150911       if( bInsertDone==0 ){
150807 150912         rc = fts3InsertData(p, apVal, pRowid);
150808 150913         if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
150809 150914           rc = FTS_CORRUPT_VTAB;
150810 150915         }
150811 150916       }
150812 150917       if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
150813         -      rc = fts3PendingTermsDocid(p, iLangid, *pRowid);
       150918  +      rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
150814 150919       }
150815 150920       if( rc==SQLITE_OK ){
150816 150921         assert( p->iPrevDocid==*pRowid );
150817 150922         rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
150818 150923       }
150819 150924       if( p->bHasDocsize ){
150820 150925         fts3InsertDocsize(&rc, p, aSzIns);

Changes to SQLite.Interop/src/core/sqlite3.h.

   109    109   **
   110    110   ** See also: [sqlite3_libversion()],
   111    111   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   112    112   ** [sqlite_version()] and [sqlite_source_id()].
   113    113   */
   114    114   #define SQLITE_VERSION        "3.8.12"
   115    115   #define SQLITE_VERSION_NUMBER 3008012
   116         -#define SQLITE_SOURCE_ID      "2015-09-28 17:05:22 c5566bb39c8d9b58f77380b81a873429575c7d5c"
          116  +#define SQLITE_SOURCE_ID      "2015-09-30 14:30:19 e796c0efb6cf17444b53af75046daf7d8fa82f78"
   117    117   
   118    118   /*
   119    119   ** CAPI3REF: Run-Time Library Version Numbers
   120    120   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   121    121   **
   122    122   ** These interfaces provide the same information as the [SQLITE_VERSION],
   123    123   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  5629   5629   ** strategy. A cost of N indicates that the cost of the strategy is similar
  5630   5630   ** to a linear scan of an SQLite table with N rows. A cost of log(N) 
  5631   5631   ** indicates that the expense of the operation is similar to that of a
  5632   5632   ** binary search on a unique indexed field of an SQLite table with N rows.
  5633   5633   **
  5634   5634   ** ^The estimatedRows value is an estimate of the number of rows that
  5635   5635   ** will be returned by the strategy.
         5636  +**
         5637  +** The xBestIndex method may optionally populate the idxFlags field with a 
         5638  +** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
         5639  +** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
         5640  +** assumes that the strategy may visit at most one row. 
         5641  +**
         5642  +** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
         5643  +** SQLite also assumes that if a call to the xUpdate() method is made as
         5644  +** part of the same statement to delete or update a virtual table row and the
         5645  +** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
         5646  +** any database changes. In other words, if the xUpdate() returns
         5647  +** SQLITE_CONSTRAINT, the database contents must be exactly as they were
         5648  +** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
         5649  +** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
         5650  +** the xUpdate method are automatically rolled back by SQLite.
  5636   5651   **
  5637   5652   ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
  5638   5653   ** structure for SQLite version 3.8.2. If a virtual table extension is
  5639   5654   ** used with an SQLite version earlier than 3.8.2, the results of attempting 
  5640   5655   ** to read or write the estimatedRows field are undefined (but are likely 
  5641   5656   ** to included crashing the application). The estimatedRows field should
  5642   5657   ** therefore only be used if [sqlite3_libversion_number()] returns a
  5643         -** value greater than or equal to 3008002.
         5658  +** value greater than or equal to 3008002. Similarly, the idxFlags field
         5659  +** was added for version 3.8.12. It may therefore only be used if
         5660  +** sqlite3_libversion_number() returns a value greater than or equal to
         5661  +** 3008012.
  5644   5662   */
  5645   5663   struct sqlite3_index_info {
  5646   5664     /* Inputs */
  5647   5665     int nConstraint;           /* Number of entries in aConstraint */
  5648   5666     struct sqlite3_index_constraint {
  5649   5667        int iColumn;              /* Column on left-hand side of constraint */
  5650   5668        unsigned char op;         /* Constraint operator */
................................................................................
  5664   5682     int idxNum;                /* Number used to identify the index */
  5665   5683     char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
  5666   5684     int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
  5667   5685     int orderByConsumed;       /* True if output is already ordered */
  5668   5686     double estimatedCost;           /* Estimated cost of using this index */
  5669   5687     /* Fields below are only available in SQLite 3.8.2 and later */
  5670   5688     sqlite3_int64 estimatedRows;    /* Estimated number of rows returned */
         5689  +  /* Fields below are only available in SQLite 3.8.12 and later */
         5690  +  int idxFlags;              /* Mask of SQLITE_INDEX_SCAN_* flags */
  5671   5691   };
  5672   5692   
         5693  +/*
         5694  +** CAPI3REF: Virtual Table Scan Flags
         5695  +*/
         5696  +#define SQLITE_INDEX_SCAN_UNIQUE      1     /* Scan visits at most 1 row */
         5697  +
  5673   5698   /*
  5674   5699   ** CAPI3REF: Virtual Table Constraint Operator Codes
  5675   5700   **
  5676   5701   ** These macros defined the allowed values for the
  5677   5702   ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
  5678   5703   ** an operator that is part of a constraint term in the wHERE clause of
  5679   5704   ** a query that uses a [virtual table].

Changes to SQLite.Interop/src/ext/fts5.c.

 11129  11129       int iRowidOff = 0;
 11130  11130       int iOff;
 11131  11131       int nDoclist;
 11132  11132   
 11133  11133       memset(&term, 0, sizeof(Fts5Buffer));
 11134  11134   
 11135  11135       if( n<4 ){
 11136         -      sqlite3Fts5BufferSet(&rc, &s, 8, (const u8*)"corrupt");
        11136  +      sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
 11137  11137         goto decode_out;
 11138  11138       }else{
 11139  11139         iRowidOff = fts5GetU16(&a[0]);
 11140  11140         iPgidxOff = szLeaf = fts5GetU16(&a[2]);
 11141  11141         if( iPgidxOff<n ){
 11142  11142           fts5GetVarint32(&a[iPgidxOff], iTermOff);
 11143  11143         }
................................................................................
 13561  13561   */
 13562  13562   static void fts5SourceIdFunc(
 13563  13563     sqlite3_context *pCtx,          /* Function call context */
 13564  13564     int nArg,                       /* Number of args */
 13565  13565     sqlite3_value **apVal           /* Function arguments */
 13566  13566   ){
 13567  13567     assert( nArg==0 );
 13568         -  sqlite3_result_text(pCtx, "fts5: 2015-09-28 17:05:22 c5566bb39c8d9b58f77380b81a873429575c7d5c", -1, SQLITE_TRANSIENT);
        13568  +  sqlite3_result_text(pCtx, "fts5: 2015-09-30 14:30:19 e796c0efb6cf17444b53af75046daf7d8fa82f78", -1, SQLITE_TRANSIENT);
 13569  13569   }
 13570  13570   
 13571  13571   #ifdef _WIN32
 13572  13572   __declspec(dllexport)
 13573  13573   #endif
 13574  13574   int sqlite3_fts5_init(
 13575  13575     sqlite3 *db,