/ Check-in [52449a95]
Login

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

Overview
Comment:Code simplifications, especially to the pseudo-table logic, and comment improvements.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 52449a9569b7142095cc88ee208b31cc59a3cab4
User & Date: drh 2009-09-08 01:14:49
Context
2009-09-08
02:27
Further simplifications to the VDBE - removing functionality that is no longer used. check-in: b271e166 user: drh tags: trunk
01:14
Code simplifications, especially to the pseudo-table logic, and comment improvements. check-in: 52449a95 user: drh tags: trunk
2009-09-07
18:14
Add the SQLITE_LIMIT_TRIGGER_DEPTH option to sqlite3_limit(). check-in: d7dc8b43 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/auth.c.

   124    124     }
   125    125   
   126    126     assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
   127    127     if( pExpr->op==TK_TRIGGER ){
   128    128       pTab = pParse->pTriggerTab;
   129    129     }else{
   130    130       assert( pTabList );
   131         -    for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
          131  +    for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
   132    132         if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
   133    133           pTab = pTabList->a[iSrc].pTab;
   134    134           break;
   135    135         }
   136    136       }
   137    137     }
   138    138     iCol = pExpr->iColumn;

Changes to src/btree.c.

  6416   6416   ** If the seekResult parameter is non-zero, then a successful call to
  6417   6417   ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
  6418   6418   ** been performed. seekResult is the search result returned (a negative
  6419   6419   ** number if pCur points at an entry that is smaller than (pKey, nKey), or
  6420   6420   ** a positive value if pCur points at an etry that is larger than 
  6421   6421   ** (pKey, nKey)). 
  6422   6422   **
  6423         -** If the seekResult parameter is 0, then cursor pCur may point to any 
  6424         -** entry or to no entry at all. In this case this function has to seek
         6423  +** If the seekResult parameter is non-zero, then the caller guarantees that
         6424  +** cursor pCur is pointing at the existing copy of a row that is to be
         6425  +** overwritten.  If the seekResult parameter is 0, then cursor pCur may
         6426  +** point to any entry or to no entry at all and so this function has to seek
  6425   6427   ** the cursor before the new key can be inserted.
  6426   6428   */
  6427   6429   int sqlite3BtreeInsert(
  6428   6430     BtCursor *pCur,                /* Insert data into the table of this cursor */
  6429   6431     const void *pKey, i64 nKey,    /* The key of the new record */
  6430   6432     const void *pData, int nData,  /* The data of the new record */
  6431   6433     int nZero,                     /* Number of extra 0 bytes to append to data */
  6432   6434     int appendBias,                /* True if this is likely an append */
  6433   6435     int seekResult                 /* Result of prior MovetoUnpacked() call */
  6434   6436   ){
  6435   6437     int rc;
  6436         -  int loc = seekResult;
         6438  +  int loc = seekResult;          /* -1: before desired location  +1: after */
  6437   6439     int szNew;
  6438   6440     int idx;
  6439   6441     MemPage *pPage;
  6440   6442     Btree *p = pCur->pBtree;
  6441   6443     BtShared *pBt = p->pBt;
  6442   6444     unsigned char *oldCell;
  6443   6445     unsigned char *newCell = 0;

Changes to src/select.c.

   762    762     int eDest = pDest->eDest;
   763    763     int iParm = pDest->iParm;
   764    764   
   765    765     int regRow;
   766    766     int regRowid;
   767    767   
   768    768     iTab = pOrderBy->iECursor;
          769  +  regRow = sqlite3GetTempReg(pParse);
   769    770     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
   770    771       pseudoTab = pParse->nTab++;
   771         -    sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, eDest==SRT_Output, nColumn);
          772  +    sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
          773  +    regRowid = 0;
          774  +  }else{
          775  +    regRowid = sqlite3GetTempReg(pParse);
   772    776     }
   773    777     addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
   774    778     codeOffset(v, p, addrContinue);
   775         -  regRow = sqlite3GetTempReg(pParse);
   776         -  regRowid = sqlite3GetTempReg(pParse);
   777    779     sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow);
   778    780     switch( eDest ){
   779    781       case SRT_Table:
   780    782       case SRT_EphemTab: {
   781    783         testcase( eDest==SRT_Table );
   782    784         testcase( eDest==SRT_EphemTab );
   783    785         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
................................................................................
   801    803       }
   802    804   #endif
   803    805       default: {
   804    806         int i;
   805    807         assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
   806    808         testcase( eDest==SRT_Output );
   807    809         testcase( eDest==SRT_Coroutine );
   808         -      sqlite3VdbeAddOp2(v, OP_Integer, 1, regRowid);
   809         -      sqlite3VdbeAddOp3(v, OP_Insert, pseudoTab, regRow, regRowid);
   810    810         for(i=0; i<nColumn; i++){
   811    811           assert( regRow!=pDest->iMem+i );
   812    812           sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
          813  +        if( i==0 ){
          814  +          sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
          815  +        }
   813    816         }
   814    817         if( eDest==SRT_Output ){
   815    818           sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
   816    819           sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn);
   817    820         }else{
   818    821           sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
   819    822         }

Changes to src/sqliteInt.h.

  2157   2157     const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
  2158   2158     Parse *pParse;              /* The Parse structure */
  2159   2159   };
  2160   2160   
  2161   2161   /*
  2162   2162   ** Bitfield flags for P5 value in OP_Insert and OP_Delete
  2163   2163   */
  2164         -#define OPFLAG_NCHANGE    1    /* Set to update db->nChange */
  2165         -#define OPFLAG_LASTROWID  2    /* Set to update db->lastRowid */
  2166         -#define OPFLAG_ISUPDATE   4    /* This OP_Insert is an sql UPDATE */
  2167         -#define OPFLAG_APPEND     8    /* This is likely to be an append */
  2168         -#define OPFLAG_USESEEKRESULT 16    /* Try to avoid a seek in BtreeInsert() */
         2164  +#define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
         2165  +#define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
         2166  +#define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
         2167  +#define OPFLAG_APPEND        0x08    /* This is likely to be an append */
         2168  +#define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
         2169  +#define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
  2169   2170   
  2170   2171   /*
  2171   2172    * Each trigger present in the database schema is stored as an instance of
  2172   2173    * struct Trigger. 
  2173   2174    *
  2174   2175    * Pointers to instances of struct Trigger are stored in two ways.
  2175   2176    * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 

Changes to src/vdbe.c.

   185    185   ** if we run out of memory.
   186    186   */
   187    187   static VdbeCursor *allocateCursor(
   188    188     Vdbe *p,              /* The virtual machine */
   189    189     int iCur,             /* Index of the new VdbeCursor */
   190    190     int nField,           /* Number of fields in the table or index */
   191    191     int iDb,              /* When database the cursor belongs to, or -1 */
   192         -  int isBtreeCursor     /* True for B-Tree vs. pseudo-table or vtab */
          192  +  int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
   193    193   ){
   194    194     /* Find the memory cell that will be used to store the blob of memory
   195    195     ** required for this VdbeCursor structure. It is convenient to use a 
   196    196     ** vdbe memory cell to manage the memory allocation required for a
   197    197     ** VdbeCursor structure for the following reasons:
   198    198     **
   199    199     **   * Sometimes cursor numbers are used for a couple of different
................................................................................
  1971   1971   case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
  1972   1972     if( (pIn1->flags & MEM_Null)==0 ){
  1973   1973       pc = pOp->p2 - 1;
  1974   1974     }
  1975   1975     break;
  1976   1976   }
  1977   1977   
  1978         -/* Opcode: SetNumColumns * P2 * * *
  1979         -**
  1980         -** This opcode sets the number of columns for the cursor opened by the
  1981         -** following instruction to P2.
  1982         -**
  1983         -** An OP_SetNumColumns is only useful if it occurs immediately before 
  1984         -** one of the following opcodes:
  1985         -**
  1986         -**     OpenRead
  1987         -**     OpenWrite
  1988         -**     OpenPseudo
  1989         -**
  1990         -** If the OP_Column opcode is to be executed on a cursor, then
  1991         -** this opcode must be present immediately before the opcode that
  1992         -** opens the cursor.
  1993         -*/
  1994         -#if 0
  1995         -case OP_SetNumColumns: {
  1996         -  break;
  1997         -}
  1998         -#endif
  1999         -
  2000         -/* Opcode: Column P1 P2 P3 P4 *
         1978  +/* Opcode: Column P1 P2 P3 P4 P5
  2001   1979   **
  2002   1980   ** Interpret the data that cursor P1 points to as a structure built using
  2003   1981   ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
  2004   1982   ** information about the format of the data.)  Extract the P2-th column
  2005   1983   ** from this record.  If there are less that (P2+1) 
  2006   1984   ** values in the record, extract a NULL.
  2007   1985   **
  2008   1986   ** The value extracted is stored in register P3.
  2009   1987   **
  2010   1988   ** If the column contains fewer than P2 fields, then extract a NULL.  Or,
  2011   1989   ** if the P4 argument is a P4_MEM use the value of the P4 argument as
  2012   1990   ** the result.
         1991  +**
         1992  +** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
         1993  +** then the cache of the cursor is reset prior to extracting the column.
         1994  +** The first OP_Column against a pseudo-table after the value of the content
         1995  +** register has changed should have this bit set.
  2013   1996   */
  2014   1997   case OP_Column: {
  2015   1998     u32 payloadSize;   /* Number of bytes in the record */
  2016   1999     i64 payloadSize64; /* Number of bytes in the record */
  2017   2000     int p1;            /* P1 value of the opcode */
  2018   2001     int p2;            /* column number to retrieve */
  2019   2002     VdbeCursor *pC;    /* The VDBE cursor */
................................................................................
  2029   2012     Mem sMem;          /* For storing the record being decoded */
  2030   2013     u8 *zIdx;          /* Index into header */
  2031   2014     u8 *zEndHdr;       /* Pointer to first byte after the header */
  2032   2015     u32 offset;        /* Offset into the data */
  2033   2016     u64 offset64;      /* 64-bit offset.  64 bits needed to catch overflow */
  2034   2017     int szHdr;         /* Size of the header size field at start of record */
  2035   2018     int avail;         /* Number of bytes of available data */
         2019  +  Mem *pReg;         /* PseudoTable input register */
  2036   2020   
  2037   2021   
  2038   2022     p1 = pOp->p1;
  2039   2023     p2 = pOp->p2;
  2040   2024     pC = 0;
  2041   2025     memset(&sMem, 0, sizeof(sMem));
  2042   2026     assert( p1<p->nCursor );
................................................................................
  2082   2066         assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
  2083   2067         payloadSize = (u32)payloadSize64;
  2084   2068       }else{
  2085   2069         assert( sqlite3BtreeCursorIsValid(pCrsr) );
  2086   2070         rc = sqlite3BtreeDataSize(pCrsr, &payloadSize);
  2087   2071         assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
  2088   2072       }
  2089         -  }else if( pC->pseudoTable ){
  2090         -    /* The record is the sole entry of a pseudo-table */
  2091         -    payloadSize = pC->nData;
  2092         -    zRec = pC->pData;
  2093         -    pC->cacheStatus = CACHE_STALE;
         2073  +  }else if( pC->pseudoTableReg>0 ){
         2074  +    pReg = &p->aMem[pC->pseudoTableReg];
         2075  +    assert( pReg->flags & MEM_Blob );
         2076  +    payloadSize = pReg->n;
         2077  +    zRec = pReg->z;
         2078  +    pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
  2094   2079       assert( payloadSize==0 || zRec!=0 );
  2095   2080     }else{
  2096   2081       /* Consider the row to be NULL */
  2097   2082       payloadSize = 0;
  2098   2083     }
  2099   2084   
  2100   2085     /* If payloadSize is 0, then just store a NULL */
................................................................................
  3063   3048     pCx->isIndex = !pCx->isTable;
  3064   3049     break;
  3065   3050   }
  3066   3051   
  3067   3052   /* Opcode: OpenPseudo P1 P2 P3 * *
  3068   3053   **
  3069   3054   ** Open a new cursor that points to a fake table that contains a single
  3070         -** row of data.  Any attempt to write a second row of data causes the
  3071         -** first row to be deleted.  All data is deleted when the cursor is
  3072         -** closed.
         3055  +** row of data.  The content of that one row in the content of memory
         3056  +** register P2.  In other words, cursor P1 becomes an alias for the 
         3057  +** MEM_Blob content contained in register P2.
  3073   3058   **
  3074         -** A pseudo-table created by this opcode is useful for holding the
  3075         -** NEW or OLD tables in a trigger.  Also used to hold the a single
         3059  +** A pseudo-table created by this opcode is used to hold the a single
  3076   3060   ** row output from the sorter so that the row can be decomposed into
  3077         -** individual columns using the OP_Column opcode.
  3078         -**
  3079         -** When OP_Insert is executed to insert a row in to the pseudo table,
  3080         -** the pseudo-table cursor may or may not make it's own copy of the
  3081         -** original row data. If P2 is 0, then the pseudo-table will copy the
  3082         -** original row data. Otherwise, a pointer to the original memory cell
  3083         -** is stored. In this case, the vdbe program must ensure that the 
  3084         -** memory cell containing the row data is not overwritten until the
  3085         -** pseudo table is closed (or a new row is inserted into it).
         3061  +** individual columns using the OP_Column opcode.  The OP_Column opcode
         3062  +** is the only cursor opcode that works with a pseudo-table.
  3086   3063   **
  3087   3064   ** P3 is the number of fields in the records that will be stored by
  3088   3065   ** the pseudo-table.
  3089   3066   */
  3090   3067   case OP_OpenPseudo: {
  3091   3068     VdbeCursor *pCx;
  3092   3069   
  3093   3070     assert( pOp->p1>=0 );
  3094   3071     pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
  3095   3072     if( pCx==0 ) goto no_mem;
  3096   3073     pCx->nullRow = 1;
  3097         -  pCx->pseudoTable = 1;
  3098         -  pCx->ephemPseudoTable = (u8)pOp->p2;
         3074  +  pCx->pseudoTableReg = pOp->p2;
  3099   3075     pCx->isTable = 1;
  3100   3076     pCx->isIndex = 0;
  3101   3077     break;
  3102   3078   }
  3103   3079   
  3104   3080   /* Opcode: Close P1 * * * *
  3105   3081   **
................................................................................
  3176   3152     int nField;
  3177   3153     i64 iKey;      /* The rowid we are to seek to */
  3178   3154   
  3179   3155     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3180   3156     assert( pOp->p2!=0 );
  3181   3157     pC = p->apCsr[pOp->p1];
  3182   3158     assert( pC!=0 );
         3159  +  assert( pC->pseudoTableReg==0 );
  3183   3160     if( pC->pCursor!=0 ){
  3184   3161       oc = pOp->opcode;
  3185   3162       pC->nullRow = 0;
  3186   3163       if( pC->isTable ){
  3187   3164         /* The input value in P3 might be of any type: integer, real, string,
  3188   3165         ** blob, or NULL.  But it needs to be an integer before we can do
  3189   3166         ** the seek, so covert it. */
................................................................................
  3289   3266         pc = pOp->p2 - 1;
  3290   3267       }
  3291   3268     }else{
  3292   3269       /* This happens when attempting to open the sqlite3_master table
  3293   3270       ** for read access returns SQLITE_EMPTY. In this case always
  3294   3271       ** take the jump (since there are no records in the table).
  3295   3272       */
  3296         -    assert( pC->pseudoTable==0 );
  3297   3273       pc = pOp->p2 - 1;
  3298   3274     }
  3299   3275     break;
  3300   3276   }
  3301   3277   
  3302   3278   /* Opcode: Seek P1 P2 * * *
  3303   3279   **
................................................................................
  3500   3476     u64 iKey;
  3501   3477   
  3502   3478     assert( pIn3->flags & MEM_Int );
  3503   3479     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3504   3480     pC = p->apCsr[pOp->p1];
  3505   3481     assert( pC!=0 );
  3506   3482     assert( pC->isTable );
         3483  +  assert( pC->pseudoTableReg==0 );
  3507   3484     pCrsr = pC->pCursor;
  3508   3485     if( pCrsr!=0 ){
  3509   3486       res = 0;
  3510   3487       iKey = pIn3->u.i;
  3511   3488       rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
  3512   3489       pC->lastRowid = pIn3->u.i;
  3513   3490       pC->rowidIsValid = res==0 ?1:0;
................................................................................
  3519   3496         assert( pC->rowidIsValid==0 );
  3520   3497       }
  3521   3498       pC->seekResult = res;
  3522   3499     }else{
  3523   3500       /* This happens when an attempt to open a read cursor on the 
  3524   3501       ** sqlite_master table returns SQLITE_EMPTY.
  3525   3502       */
  3526         -    assert( !pC->pseudoTable );
  3527         -    assert( pC->isTable );
  3528   3503       pc = pOp->p2 - 1;
  3529   3504       assert( pC->rowidIsValid==0 );
  3530   3505       pC->seekResult = 0;
  3531   3506     }
  3532   3507     break;
  3533   3508   }
  3534   3509   
................................................................................
  3681   3656     break;
  3682   3657   }
  3683   3658   
  3684   3659   /* Opcode: Insert P1 P2 P3 P4 P5
  3685   3660   **
  3686   3661   ** Write an entry into the table of cursor P1.  A new entry is
  3687   3662   ** created if it doesn't already exist or the data for an existing
  3688         -** entry is overwritten.  The data is the value stored register
         3663  +** entry is overwritten.  The data is the value MEM_Blob stored in register
  3689   3664   ** number P2. The key is stored in register P3. The key must
  3690         -** be an integer.
         3665  +** be a MEM_Int.
  3691   3666   **
  3692   3667   ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
  3693   3668   ** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
  3694   3669   ** then rowid is stored for subsequent return by the
  3695   3670   ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
         3671  +**
         3672  +** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
         3673  +** the last seek operation (OP_NotExists) was a success, then this
         3674  +** operation will not attempt to find the appropriate row before doing
         3675  +** the insert but will instead overwrite the row that the cursor is
         3676  +** currently pointing to.  Presumably, the prior OP_NotExists opcode
         3677  +** has already positioned the cursor correctly.  This is an optimization
         3678  +** that boosts performance by avoiding redundant seeks.
         3679  +**
         3680  +** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
         3681  +** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
         3682  +** is part of an INSERT operation.  The difference is only important to
         3683  +** the update hook.
  3696   3684   **
  3697   3685   ** Parameter P4 may point to a string containing the table-name, or
  3698   3686   ** may be NULL. If it is not NULL, then the update-hook 
  3699   3687   ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
  3700   3688   **
  3701   3689   ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
  3702   3690   ** allocated, then ownership of P2 is transferred to the pseudo-cursor
................................................................................
  3704   3692   ** value of register P2 will then change.  Make sure this does not
  3705   3693   ** cause any problems.)
  3706   3694   **
  3707   3695   ** This instruction only works on tables.  The equivalent instruction
  3708   3696   ** for indices is OP_IdxInsert.
  3709   3697   */
  3710   3698   case OP_Insert: {
  3711         -  Mem *pData;
  3712         -  Mem *pKey;
  3713         -  i64 iKey;   /* The integer ROWID or key for the record to be inserted */
  3714         -  VdbeCursor *pC;
  3715         -  int nZero;
  3716         -  int seekResult;
  3717         -  const char *zDb;
  3718         -  const char *zTbl;
  3719         -  int op;
         3699  +  Mem *pData;       /* MEM cell holding data for the record to be inserted */
         3700  +  Mem *pKey;        /* MEM cell holding key  for the record */
         3701  +  i64 iKey;         /* The integer ROWID or key for the record to be inserted */
         3702  +  VdbeCursor *pC;   /* Cursor to table into which insert is written */
         3703  +  int nZero;        /* Number of zero-bytes to append */
         3704  +  int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
         3705  +  const char *zDb;  /* database name - used by the update hook */
         3706  +  const char *zTbl; /* Table name - used by the opdate hook */
         3707  +  int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
  3720   3708   
  3721   3709     pData = &p->aMem[pOp->p2];
  3722   3710     pKey = &p->aMem[pOp->p3];
  3723   3711     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3724   3712     pC = p->apCsr[pOp->p1];
  3725   3713     assert( pC!=0 );
  3726         -  assert( pC->pCursor!=0 || pC->pseudoTable );
         3714  +  assert( pC->pCursor!=0 );
         3715  +  assert( pC->pseudoTableReg==0 );
  3727   3716     assert( pKey->flags & MEM_Int );
  3728   3717     assert( pC->isTable );
  3729   3718     REGISTER_TRACE(pOp->p2, pData);
  3730   3719     REGISTER_TRACE(pOp->p3, pKey);
  3731   3720   
  3732   3721     iKey = pKey->u.i;
  3733   3722     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
................................................................................
  3734   3723     if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i;
  3735   3724     if( pData->flags & MEM_Null ){
  3736   3725       pData->z = 0;
  3737   3726       pData->n = 0;
  3738   3727     }else{
  3739   3728       assert( pData->flags & (MEM_Blob|MEM_Str) );
  3740   3729     }
  3741         -  if( pC->pseudoTable ){
  3742         -    if( !pC->ephemPseudoTable ){
  3743         -      sqlite3DbFree(db, pC->pData);
  3744         -    }
  3745         -    pC->iKey = iKey;
  3746         -    pC->nData = pData->n;
  3747         -    if( pC->ephemPseudoTable || pData->z==pData->zMalloc ){
  3748         -      pC->pData = pData->z;
  3749         -      if( !pC->ephemPseudoTable ){
  3750         -        pData->flags &= ~MEM_Dyn;
  3751         -        pData->flags |= MEM_Ephem;
  3752         -        pData->zMalloc = 0;
  3753         -      }
  3754         -    }else{
  3755         -      pC->pData = sqlite3Malloc( pC->nData+2 );
  3756         -      if( !pC->pData ) goto no_mem;
  3757         -      memcpy(pC->pData, pData->z, pC->nData);
  3758         -      pC->pData[pC->nData] = 0;
  3759         -      pC->pData[pC->nData+1] = 0;
  3760         -    }
  3761         -    pC->nullRow = 0;
         3730  +  seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
         3731  +  if( pData->flags & MEM_Zero ){
         3732  +    nZero = pData->u.nZero;
  3762   3733     }else{
  3763         -    seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
  3764         -    if( pData->flags & MEM_Zero ){
  3765         -      nZero = pData->u.nZero;
  3766         -    }else{
  3767         -      nZero = 0;
  3768         -    }
  3769         -    sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
  3770         -    rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
  3771         -                            pData->z, pData->n, nZero,
  3772         -                            pOp->p5 & OPFLAG_APPEND, seekResult
  3773         -    );
         3734  +    nZero = 0;
  3774   3735     }
  3775         -  
         3736  +  sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
         3737  +  rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
         3738  +                          pData->z, pData->n, nZero,
         3739  +                          pOp->p5 & OPFLAG_APPEND, seekResult
         3740  +  );
  3776   3741     pC->rowidIsValid = 0;
  3777   3742     pC->deferredMoveto = 0;
  3778   3743     pC->cacheStatus = CACHE_STALE;
  3779   3744   
  3780   3745     /* Invoke the update-hook if required. */
  3781   3746     if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
  3782   3747       zDb = db->aDb[pC->iDb].zName;
................................................................................
  3901   3866     /* Note that RowKey and RowData are really exactly the same instruction */
  3902   3867     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3903   3868     pC = p->apCsr[pOp->p1];
  3904   3869     assert( pC->isTable || pOp->opcode==OP_RowKey );
  3905   3870     assert( pC->isIndex || pOp->opcode==OP_RowData );
  3906   3871     assert( pC!=0 );
  3907   3872     assert( pC->nullRow==0 );
  3908         -  assert( pC->pseudoTable==0 );
         3873  +  assert( pC->pseudoTableReg==0 );
  3909   3874     assert( pC->pCursor!=0 );
  3910   3875     pCrsr = pC->pCursor;
  3911   3876     assert( sqlite3BtreeCursorIsValid(pCrsr) );
  3912   3877   
  3913   3878     /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
  3914   3879     ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
  3915   3880     ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
................................................................................
  3963   3928     i64 v;
  3964   3929     sqlite3_vtab *pVtab;
  3965   3930     const sqlite3_module *pModule;
  3966   3931   
  3967   3932     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3968   3933     pC = p->apCsr[pOp->p1];
  3969   3934     assert( pC!=0 );
         3935  +  assert( pC->pseudoTableReg==0 );
  3970   3936     if( pC->nullRow ){
  3971   3937       /* Do nothing so that reg[P2] remains NULL */
  3972   3938       break;
  3973   3939     }else if( pC->deferredMoveto ){
  3974   3940       v = pC->movetoTarget;
  3975         -  }else if( pC->pseudoTable ){
  3976         -    v = pC->iKey;
  3977   3941   #ifndef SQLITE_OMIT_VIRTUALTABLE
  3978   3942     }else if( pC->pVtabCursor ){
  3979   3943       pVtab = pC->pVtabCursor->pVtab;
  3980   3944       pModule = pVtab->pModule;
  3981   3945       assert( pModule->xRowid );
  3982   3946       if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  3983   3947       rc = pModule->xRowid(pC->pVtabCursor, &v);

Changes to src/vdbeInt.h.

    52     52     int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
    53     53     i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
    54     54     Bool zeroed;          /* True if zeroed out and ready for reuse */
    55     55     Bool rowidIsValid;    /* True if lastRowid is valid */
    56     56     Bool atFirst;         /* True if pointing to first entry */
    57     57     Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
    58     58     Bool nullRow;         /* True if pointing to a row with no data */
    59         -  Bool pseudoTable;     /* This is a NEW or OLD pseudo-tables of a trigger */
    60         -  Bool ephemPseudoTable;
    61     59     Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
    62     60     Bool isTable;         /* True if a table requiring integer keys */
    63     61     Bool isIndex;         /* True if an index containing keys only - no data */
    64     62     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
    65     63     Btree *pBt;           /* Separate file holding temporary table */
    66         -  int nData;            /* Number of bytes in pData */
    67         -  char *pData;          /* Data for a NEW or OLD pseudo-table */
    68         -  i64 iKey;             /* Key for the NEW or OLD pseudo-table row */
           64  +  int pseudoTableReg;   /* Register holding pseudotable content. */
    69     65     KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
    70     66     int nField;           /* Number of fields in the header */
    71     67     i64 seqCount;         /* Sequence counter */
    72     68     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
    73     69     const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
    74     70   
    75     71     /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or 
    76     72     ** OP_IsUnique opcode on this cursor. */
    77     73     int seekResult;
    78     74   
    79     75     /* Cached information about the header for the data record that the
    80         -  ** cursor is currently pointing to.  Only valid if cacheValid is true.
           76  +  ** cursor is currently pointing to.  Only valid if cacheStatus matches
           77  +  ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
           78  +  ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
           79  +  ** the cache is out of date.
           80  +  **
    81     81     ** aRow might point to (ephemeral) data for the current row, or it might
    82     82     ** be NULL.
    83     83     */
    84         -  int cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
           84  +  u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
    85     85     int payloadSize;      /* Total number of bytes in the record */
    86     86     u32 *aType;           /* Type values for all entries in the record */
    87     87     u32 *aOffset;         /* Cached offsets to the start of each columns data */
    88     88     u8 *aRow;             /* Data for the current row, if all on one page */
    89     89   };
    90     90   typedef struct VdbeCursor VdbeCursor;
    91     91   
................................................................................
   292    292     u8 okVar;               /* True if azVar[] has been initialized */
   293    293     u16 nVar;               /* Number of entries in aVar[] */
   294    294     Mem *aVar;              /* Values for the OP_Variable opcode. */
   295    295     char **azVar;           /* Name of variables */
   296    296     u32 magic;              /* Magic number for sanity checking */
   297    297     int nMem;               /* Number of memory locations currently allocated */
   298    298     Mem *aMem;              /* The memory locations */
   299         -  int cacheCtr;           /* VdbeCursor row cache generation counter */
          299  +  u32 cacheCtr;           /* VdbeCursor row cache generation counter */
   300    300     int pc;                 /* The program counter */
   301    301     int rc;                 /* Value to return */
   302    302     char *zErrMsg;          /* Error message written here */
   303    303     u8 explain;             /* True if EXPLAIN present on SQL command */
   304    304     u8 changeCntOn;         /* True to update the change-counter */
   305    305     u8 expired;             /* True if the VM needs to be recompiled */
   306    306     u8 minWriteFileFormat;  /* Minimum file format for writable database files */

Changes to src/vdbeaux.c.

  1361   1361       p->inVtabMethod = 1;
  1362   1362       (void)sqlite3SafetyOff(p->db);
  1363   1363       pModule->xClose(pVtabCursor);
  1364   1364       (void)sqlite3SafetyOn(p->db);
  1365   1365       p->inVtabMethod = 0;
  1366   1366     }
  1367   1367   #endif
  1368         -  if( !pCx->ephemPseudoTable ){
  1369         -    sqlite3DbFree(p->db, pCx->pData);
  1370         -  }
  1371   1368   }
  1372   1369   
  1373   1370   /*
  1374   1371   ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
  1375   1372   ** is used, for example, when a trigger sub-program is halted to restore
  1376   1373   ** control to the main program.
  1377   1374   */