/ Check-in [8cbd4651]
Login

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

Overview
Comment:More work toward converting the VM into a register-based machine. (CVS 4704)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 8cbd46517f407b3b1ce187b623db10f00aa415ea
User & Date: drh 2008-01-10 23:50:11
Context
2008-01-11
00:06
Attempt to work around a bug in the Borland BCC 5.5.1 compiler. Ticket #2880. (CVS 4705) check-in: 6de0ee49 user: drh tags: trunk
2008-01-10
23:50
More work toward converting the VM into a register-based machine. (CVS 4704) check-in: 8cbd4651 user: drh tags: trunk
03:46
Continuing work toward registerizing the code generator. (CVS 4703) check-in: 173698c9 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/analyze.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code associated with the ANALYZE command.
    13     13   **
    14         -** @(#) $Id: analyze.c,v 1.36 2008/01/09 23:04:12 drh Exp $
           14  +** @(#) $Id: analyze.c,v 1.37 2008/01/10 23:50:11 drh Exp $
    15     15   */
    16     16   #ifndef SQLITE_OMIT_ANALYZE
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** This routine generates code that opens the sqlite_stat1 table on cursor
    21     21   ** iStatCur.
................................................................................
   116    116   
   117    117     /* Establish a read-lock on the table at the shared-cache level. */
   118    118     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
   119    119   
   120    120     iIdxCur = pParse->nTab;
   121    121     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   122    122       KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
          123  +    int regFields;    /* Register block for building records */
          124  +    int regRec;       /* Register holding completed record */
          125  +    int regTemp;      /* Temporary use register */
          126  +    int regCol;       /* Content of a column from the table being analyzed */
          127  +    int regRowid;     /* Rowid for the inserted record */
          128  +    int regF2;
   123    129   
   124    130       /* Open a cursor to the index to be analyzed
   125    131       */
   126    132       assert( iDb==sqlite3SchemaToIndex(pParse->db, pIdx->pSchema) );
   127    133       sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
   128    134           (char *)pKey, P4_KEYINFO_HANDOFF);
   129    135       VdbeComment((v, "%s", pIdx->zName));
   130    136       nCol = pIdx->nColumn;
   131         -    if( iMem+nCol*2>=pParse->nMem ){
   132         -      pParse->nMem = iMem+nCol*2+1;
          137  +    regFields = iMem+nCol*2;
          138  +    regTemp = regRowid = regCol = regFields+3;
          139  +    regRec = regCol+1;
          140  +    if( regRec>pParse->nMem ){
          141  +      pParse->nMem = regRec;
   133    142       }
   134    143       sqlite3VdbeAddOp2(v, OP_SetNumColumns, iIdxCur, nCol+1);
   135    144   
   136    145       /* Memory cells are used as follows:
   137    146       **
   138    147       **    mem[iMem]:             The total number of rows in the table.
   139    148       **    mem[iMem+1]:           Number of distinct values in column 1
................................................................................
   156    165       /* Do the analysis.
   157    166       */
   158    167       endOfLoop = sqlite3VdbeMakeLabel(v);
   159    168       sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
   160    169       topOfLoop = sqlite3VdbeCurrentAddr(v);
   161    170       sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
   162    171       for(i=0; i<nCol; i++){
   163         -      sqlite3VdbeAddOp2(v, OP_Column, iIdxCur, i);
   164         -      sqlite3VdbeAddOp1(v, OP_SCopy, iMem+nCol+i+1);
   165         -      sqlite3VdbeAddOp0(v, OP_Ne );  /* Use Collating sequence */
          172  +      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
          173  +      sqlite3VdbeAddOp3(v, OP_Ne, regCol, 0, iMem+nCol+i+1);
          174  +      /**** TODO:  add collating sequence *****/
   166    175         sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
   167    176       }
   168    177       sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
   169    178       for(i=0; i<nCol; i++){
   170         -      addr = sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
   171         -      sqlite3VdbeChangeP2(v, topOfLoop + 3*i + 3, addr);
          179  +      sqlite3VdbeJumpHere(v, topOfLoop + 2*(i + 1));
          180  +      sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
   172    181         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
   173    182       }
   174    183       sqlite3VdbeResolveLabel(v, endOfLoop);
   175    184       sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
   176    185       sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
   177    186   
   178    187       /* Store the results.  
................................................................................
   189    198       **
   190    199       **        I = (K+D-1)/D
   191    200       **
   192    201       ** If K==0 then no entry is made into the sqlite_stat1 table.  
   193    202       ** If K>0 then it is always the case the D>0 so division by zero
   194    203       ** is never possible.
   195    204       */
   196         -    sqlite3VdbeAddOp1(v, OP_SCopy, iMem);
   197         -    addr = sqlite3VdbeAddOp0(v, OP_IfNot);
   198         -    sqlite3VdbeAddOp1(v, OP_NewRowid, iStatCur);
   199         -    sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, pTab->zName, 0);
   200         -    sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, pIdx->zName, 0);
   201         -    sqlite3VdbeAddOp1(v, OP_SCopy, iMem);
          205  +    addr = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
          206  +    sqlite3VdbeAddOp4(v, OP_String8, 0, regFields, 0, pTab->zName, 0);
          207  +    sqlite3VdbeAddOp4(v, OP_String8, 0, regFields+1, 0, pIdx->zName, 0);
          208  +    regF2 = regFields+2;
          209  +    sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regF2);
   202    210       for(i=0; i<nCol; i++){
   203         -      sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, " ", 0);
   204         -      sqlite3VdbeAddOp0(v, OP_Concat);
   205         -      sqlite3VdbeAddOp2(v, OP_Add, iMem, iMem+i+1);
   206         -      sqlite3VdbeAddOp2(v, OP_AddImm, 0, -1);
   207         -      sqlite3VdbeAddOp2(v, OP_Divide, iMem+i+1, 0);
   208         -      sqlite3VdbeAddOp0(v, OP_ToInt);
   209         -      sqlite3VdbeAddOp0(v, OP_Concat);
          211  +      sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
          212  +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regF2, regF2);
          213  +      sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
          214  +      sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
          215  +      sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
          216  +      sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
          217  +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regF2, regF2);
   210    218       }
   211         -    sqlite3VdbeAddOp4(v, OP_MakeRecord, 3, 0, 0, "aaa", 0);
   212         -    sqlite3CodeInsert(pParse, iStatCur, OPFLAG_APPEND);
          219  +    sqlite3VdbeAddOp4(v, OP_RegMakeRec, regFields, 3, regRec, "aaa", 0);
          220  +    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
          221  +    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
          222  +    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   213    223       sqlite3VdbeJumpHere(v, addr);
   214    224     }
   215    225   }
   216    226   
   217    227   /*
   218    228   ** Generate code that will cause the most recent index analysis to
   219    229   ** be laoded into internal hash tables where is can be used.

Changes to src/build.c.

    18     18   **     CREATE INDEX
    19     19   **     DROP INDEX
    20     20   **     creating ID lists
    21     21   **     BEGIN TRANSACTION
    22     22   **     COMMIT
    23     23   **     ROLLBACK
    24     24   **
    25         -** $Id: build.c,v 1.462 2008/01/09 23:04:12 drh Exp $
           25  +** $Id: build.c,v 1.463 2008/01/10 23:50:11 drh Exp $
    26     26   */
    27     27   #include "sqliteInt.h"
    28     28   #include <ctype.h>
    29     29   
    30     30   /*
    31     31   ** This routine is called when a new SQL statement is beginning to
    32     32   ** be parsed.  Initialize the pParse structure as needed.
................................................................................
  2218   2218     Table *pTab = pIndex->pTable;  /* The table that is indexed */
  2219   2219     int iTab = pParse->nTab;       /* Btree cursor used for pTab */
  2220   2220     int iIdx = pParse->nTab+1;     /* Btree cursor used for pIndex */
  2221   2221     int addr1;                     /* Address of top of loop */
  2222   2222     int tnum;                      /* Root page of index */
  2223   2223     Vdbe *v;                       /* Generate code into this virtual machine */
  2224   2224     KeyInfo *pKey;                 /* KeyInfo for index */
         2225  +  int regIdxKey;                 /* Registers containing the index key */
         2226  +  int regRecord;                 /* Register holding assemblied index record */
  2225   2227     sqlite3 *db = pParse->db;      /* The database connection */
  2226   2228     int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
  2227   2229   
  2228   2230   #ifndef SQLITE_OMIT_AUTHORIZATION
  2229   2231     if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
  2230   2232         db->aDb[iDb].zName ) ){
  2231   2233       return;
................................................................................
  2248   2250     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
  2249   2251                       (char *)pKey, P4_KEYINFO_HANDOFF);
  2250   2252     if( memRootPage>=0 ){
  2251   2253       sqlite3VdbeChangeP5(v, 1);
  2252   2254     }
  2253   2255     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2254   2256     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
  2255         -  sqlite3GenerateIndexKey(v, pIndex, iTab);
         2257  +  regRecord = sqlite3GetTempReg(pParse);
         2258  +  regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord);
  2256   2259     if( pIndex->onError!=OE_None ){
  2257         -    int curaddr = sqlite3VdbeCurrentAddr(v);
  2258         -    int addr2 = curaddr+4;
  2259         -    sqlite3VdbeChangeP2(v, curaddr-1, addr2);
  2260         -    sqlite3VdbeAddOp1(v, OP_Rowid, iTab);
  2261         -    sqlite3VdbeAddOp2(v, OP_AddImm, 0, 1);
  2262         -    sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, addr2, 0, 0, P4_INT32);
         2260  +    int j1, j2;
         2261  +    int regRowid;
         2262  +
         2263  +    regRowid = regIdxKey + pIndex->nColumn;
         2264  +    j1 = sqlite3VdbeAddOp3(v, OP_IsNull, regIdxKey, 0, pIndex->nColumn);
         2265  +    j2 = sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx,
         2266  +                           0, regRowid, (char*)regRecord, P4_INT32);
  2263   2267       sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, OE_Abort, 0,
  2264   2268                       "indexed columns are not unique", P4_STATIC);
  2265         -    assert( db->mallocFailed || addr2==sqlite3VdbeCurrentAddr(v) );
         2269  +    sqlite3VdbeJumpHere(v, j1);
         2270  +    sqlite3VdbeJumpHere(v, j2);
  2266   2271     }
  2267         -  sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, 0);
         2272  +  sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
         2273  +  sqlite3ReleaseTempReg(pParse, regRecord);
  2268   2274     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
  2269   2275     sqlite3VdbeJumpHere(v, addr1);
  2270   2276     sqlite3VdbeAddOp1(v, OP_Close, iTab);
  2271   2277     sqlite3VdbeAddOp1(v, OP_Close, iIdx);
  2272   2278   }
  2273   2279   
  2274   2280   /*

Changes to src/delete.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** in order to generate code for DELETE FROM statements.
    14     14   **
    15         -** $Id: delete.c,v 1.155 2008/01/09 23:04:12 drh Exp $
           15  +** $Id: delete.c,v 1.156 2008/01/10 23:50:11 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Look up every table that is named in pSrc.  If any table is not found,
    21     21   ** add an error message to pParse->zErrMsg and return NULL.  If all tables
    22     22   ** are found, return a pointer to the last table.
................................................................................
   386    386         if( IsVirtual(pTab) ){
   387    387           const char *pVtab = (const char *)pTab->pVtab;
   388    388           pParse->pVirtualLock = pTab;
   389    389           sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVtab, P4_VTAB);
   390    390         }else
   391    391   #endif
   392    392         {
   393         -        sqlite3GenerateRowDelete(db, v, pTab, iCur, iRowid, pParse->nested==0);
          393  +        sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, pParse->nested==0);
   394    394         }
   395    395       }
   396    396   
   397    397       /* If there are row triggers, close all cursors then invoke
   398    398       ** the AFTER triggers
   399    399       */
   400    400       if( triggers_exist ){
................................................................................
   451    451   **       memory cell iRowid.
   452    452   **
   453    453   ** This routine pops the top of the stack to remove the record number
   454    454   ** and then generates code to remove both the table record and all index
   455    455   ** entries that point to that record.
   456    456   */
   457    457   void sqlite3GenerateRowDelete(
   458         -  sqlite3 *db,       /* The database containing the index */
   459         -  Vdbe *v,           /* Generate code into this VDBE */
          458  +  Parse *pParse,     /* Parsing context */
   460    459     Table *pTab,       /* Table containing the row to be deleted */
   461    460     int iCur,          /* Cursor number for the table */
   462    461     int iRowid,        /* Memory cell that contains the rowid to delete */
   463    462     int count          /* Increment the row change counter */
   464    463   ){
   465    464     int addr;
          465  +  Vdbe *v;
          466  +
          467  +  v = pParse->pVdbe;
   466    468     addr = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowid);
   467         -  sqlite3GenerateRowIndexDelete(v, pTab, iCur, 0);
          469  +  sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
   468    470     sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
   469    471     if( count ){
   470    472       sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
   471    473     }
   472    474     sqlite3VdbeJumpHere(v, addr);
   473    475   }
   474    476   
................................................................................
   485    487   **   2.  Read/write cursors for all indices of pTab must be open as
   486    488   **       cursor number iCur+i for the i-th index.
   487    489   **
   488    490   **   3.  The "iCur" cursor must be pointing to the row that is to be
   489    491   **       deleted.
   490    492   */
   491    493   void sqlite3GenerateRowIndexDelete(
   492         -  Vdbe *v,           /* Generate code into this VDBE */
          494  +  Parse *pParse,     /* Parsing and code generating context */
   493    495     Table *pTab,       /* Table containing the row to be deleted */
   494    496     int iCur,          /* Cursor number for the table */
   495    497     int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
   496    498   ){
   497    499     int i;
   498    500     Index *pIdx;
   499    501   
   500    502     for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
   501    503       if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue;
   502         -    sqlite3GenerateIndexKey(v, pIdx, iCur);
   503         -    sqlite3VdbeAddOp1(v, OP_IdxDelete, iCur+i);
          504  +    sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0);
          505  +    sqlite3VdbeAddOp2(pParse->pVdbe, OP_IdxDelete, iCur+i, 0);
   504    506     }
   505    507   }
   506    508   
   507    509   /*
   508    510   ** Generate code that will assemble an index key and put it on the top
   509    511   ** of the tack.  The key with be for index pIdx which is an index on pTab.
   510    512   ** iCur is the index of a cursor open on the pTab table and pointing to
   511    513   ** the entry that needs indexing.
          514  +**
          515  +** Return a register number which is the first in a block of
          516  +** registers that holds the elements of the index key.  The
          517  +** block of registers has already been deallocated by the time
          518  +** this routine returns.
   512    519   */
   513         -void sqlite3GenerateIndexKey(
   514         -  Vdbe *v,           /* Generate code into this VDBE */
          520  +int sqlite3GenerateIndexKey(
          521  +  Parse *pParse,     /* Parsing context */
   515    522     Index *pIdx,       /* The index for which to generate a key */
   516         -  int iCur           /* Cursor number for the pIdx->pTable table */
          523  +  int iCur,          /* Cursor number for the pIdx->pTable table */
          524  +  int regOut         /* Write the new index key to this register */
   517    525   ){
          526  +  Vdbe *v = pParse->pVdbe;
   518    527     int j;
   519    528     Table *pTab = pIdx->pTable;
          529  +  int regBase;
          530  +  int nCol;
   520    531   
   521         -  sqlite3VdbeAddOp1(v, OP_Rowid, iCur);
   522         -  for(j=0; j<pIdx->nColumn; j++){
          532  +  nCol = pIdx->nColumn;
          533  +  regBase = sqlite3GetTempRange(pParse, nCol+1);
          534  +  sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol);
          535  +  for(j=0; j<nCol; j++){
   523    536       int idx = pIdx->aiColumn[j];
   524    537       if( idx==pTab->iPKey ){
   525         -      sqlite3VdbeAddOp1(v, OP_Copy, -j);
          538  +      sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);
   526    539       }else{
   527         -      sqlite3VdbeAddOp2(v, OP_Column, iCur, idx);
          540  +      sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
   528    541         sqlite3ColumnDefault(v, pTab, idx);
   529    542       }
   530    543     }
   531         -  sqlite3VdbeAddOp1(v, OP_MakeIdxRec, pIdx->nColumn);
          544  +  sqlite3VdbeAddOp3(v, OP_RegMakeRec, regBase, nCol+1, regOut);
   532    545     sqlite3IndexAffinityStr(v, pIdx);
          546  +  sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
          547  +  return regBase;
   533    548   }

Changes to src/expr.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used for analyzing expressions and
    13     13   ** for generating VDBE code that evaluates expressions in SQLite.
    14     14   **
    15         -** $Id: expr.c,v 1.341 2008/01/10 03:46:36 drh Exp $
           15  +** $Id: expr.c,v 1.342 2008/01/10 23:50:11 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /*
    21     21   ** Return the 'affinity' of the expression pExpr if any.
    22     22   **
................................................................................
  1546   1546   ** its members, skipping duplicates.
  1547   1547   **
  1548   1548   ** The cursor opened on the structure (database table, database index 
  1549   1549   ** or ephermal table) is stored in pX->iTable before this function returns.
  1550   1550   ** The returned value indicates the structure type, as follows:
  1551   1551   **
  1552   1552   **   IN_INDEX_ROWID - The cursor was opened on a database table.
  1553         -**   IN_INDEX_INDEX - The cursor was opened on a database indec.
         1553  +**   IN_INDEX_INDEX - The cursor was opened on a database index.
  1554   1554   **   IN_INDEX_EPH -   The cursor was opened on a specially created and
  1555   1555   **                    populated epheremal table.
  1556   1556   **
  1557   1557   ** An existing structure may only be used if the SELECT is of the simple
  1558   1558   ** form:
  1559   1559   **
  1560   1560   **     SELECT <column> FROM <table>
................................................................................
  1761   1761           ** store it in the temporary table. If <expr> is a column, then use
  1762   1762           ** that columns affinity when building index keys. If <expr> is not
  1763   1763           ** a column, use numeric affinity.
  1764   1764           */
  1765   1765           int i;
  1766   1766           ExprList *pList = pExpr->pList;
  1767   1767           struct ExprList_item *pItem;
         1768  +        int r1, r2;
  1768   1769   
  1769   1770           if( !affinity ){
  1770   1771             affinity = SQLITE_AFF_NONE;
  1771   1772           }
  1772   1773           keyInfo.aColl[0] = pExpr->pLeft->pColl;
  1773   1774   
  1774   1775           /* Loop through each expression in <exprlist>. */
         1776  +        r1 = sqlite3GetTempReg(pParse);
         1777  +        r2 = sqlite3GetTempReg(pParse);
  1775   1778           for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
  1776   1779             Expr *pE2 = pItem->pExpr;
  1777   1780   
  1778   1781             /* If the expression is not constant then we will need to
  1779   1782             ** disable the test that was generated above that makes sure
  1780   1783             ** this code only executes once.  Because for a non-constant
  1781   1784             ** expression we need to rerun this code each time.
................................................................................
  1782   1785             */
  1783   1786             if( testAddr && !sqlite3ExprIsConstant(pE2) ){
  1784   1787               sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
  1785   1788               testAddr = 0;
  1786   1789             }
  1787   1790   
  1788   1791             /* Evaluate the expression and insert it into the temp table */
  1789         -          sqlite3ExprCode(pParse, pE2, 0);
  1790         -          sqlite3VdbeAddOp4(v, OP_MakeRecord, 1, 0, 0, &affinity, 1);
  1791         -          sqlite3VdbeAddOp1(v, OP_IdxInsert, pExpr->iTable);
         1792  +          sqlite3ExprCode(pParse, pE2, r1);
         1793  +          sqlite3VdbeAddOp4(v, OP_RegMakeRec, r1, 1, r2, &affinity, 1);
         1794  +          sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
  1792   1795           }
         1796  +        sqlite3ReleaseTempReg(pParse, r1);
         1797  +        sqlite3ReleaseTempReg(pParse, r2);
  1793   1798         }
  1794   1799         sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
  1795   1800         break;
  1796   1801       }
  1797   1802   
  1798   1803       case TK_EXISTS:
  1799   1804       case TK_SELECT: {
................................................................................
  2226   2231         inReg = pExpr->iColumn;
  2227   2232         break;
  2228   2233       }
  2229   2234       case TK_IN: {
  2230   2235         int j1, j2, j3, j4, j5;
  2231   2236         char affinity;
  2232   2237         int eType;
         2238  +      int r1, r2, r3;
  2233   2239   
  2234   2240         eType = sqlite3FindInIndex(pParse, pExpr, 0);
  2235   2241   
  2236   2242         /* Figure out the affinity to use to create a key from the results
  2237   2243         ** of the expression. affinityStr stores a static string suitable for
  2238   2244         ** P4 of OP_MakeRecord.
  2239   2245         */
  2240   2246         affinity = comparisonAffinity(pExpr);
  2241   2247   
  2242         -      sqlite3VdbeAddOp1(v, OP_Integer, 1);
         2248  +      if( target ){
         2249  +        r1 = target;
         2250  +      }else{
         2251  +        r1 = sqlite3GetTempReg(pParse);
         2252  +      }
         2253  +      inReg = r1;
         2254  +      sqlite3VdbeAddOp2(v, OP_Integer, 1, r1);
  2243   2255   
  2244   2256         /* Code the <expr> from "<expr> IN (...)". The temporary table
  2245   2257         ** pExpr->iTable contains the values that make up the (...) set.
  2246   2258         */
  2247         -      sqlite3ExprCode(pParse, pExpr->pLeft, 0);
  2248         -      sqlite3VdbeAddOp0(v, OP_SCopy);
  2249         -      j1 = sqlite3VdbeAddOp0(v, OP_NotNull);
  2250         -      sqlite3VdbeAddOp1(v, OP_Pop, 2);
  2251         -      sqlite3VdbeAddOp0(v, OP_Null);
         2259  +      r2 = sqlite3ExprCode(pParse, pExpr->pLeft, -1);
         2260  +      j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r2);
         2261  +      sqlite3VdbeAddOp2(v, OP_Null, 0, r1);
  2252   2262         j2  = sqlite3VdbeAddOp0(v, OP_Goto);
  2253   2263         sqlite3VdbeJumpHere(v, j1);
  2254   2264         if( eType==IN_INDEX_ROWID ){
  2255         -        j3 = sqlite3VdbeAddOp3(v, OP_MustBeInt, 0, 0, 1);
  2256         -        j4 = sqlite3VdbeAddOp1(v, OP_NotExists, pExpr->iTable);
         2265  +        j3 = sqlite3VdbeAddOp3(v, OP_MustBeInt, r2, 0, 1);
         2266  +        j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r2);
  2257   2267           j5 = sqlite3VdbeAddOp0(v, OP_Goto);
  2258   2268           sqlite3VdbeJumpHere(v, j3);
  2259   2269           sqlite3VdbeJumpHere(v, j4);
  2260   2270         }else{
  2261         -        sqlite3VdbeAddOp4(v, OP_MakeRecord, 1, 0, 0, &affinity, 1);
  2262         -        j5 = sqlite3VdbeAddOp1(v, OP_Found, pExpr->iTable);
         2271  +        r3 = sqlite3GetTempReg(pParse);
         2272  +        sqlite3VdbeAddOp4(v, OP_RegMakeRec, r2, 1, r3, &affinity, 1);
         2273  +        j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r3);
         2274  +        sqlite3ReleaseTempReg(pParse, r3);
  2263   2275         }
  2264         -      sqlite3VdbeAddOp2(v, OP_AddImm, 0, -1);
         2276  +      sqlite3VdbeAddOp2(v, OP_AddImm, r1, -1);
  2265   2277         sqlite3VdbeJumpHere(v, j2);
  2266   2278         sqlite3VdbeJumpHere(v, j5);
  2267   2279         break;
  2268   2280       }
  2269   2281   #endif
  2270   2282       case TK_BETWEEN: {
  2271   2283         Expr *pLeft = pExpr->pLeft;
................................................................................
  2380   2392   **
  2381   2393   ** This routine might also cache the result and modify the pExpr tree
  2382   2394   ** so that it will make use of the cached result on subsequent evaluations
  2383   2395   ** rather than evaluate the whole expression again.  Trivial expressions are
  2384   2396   ** not cached.  If the expression is cached, its result is stored in a 
  2385   2397   ** memory location.
  2386   2398   */
  2387         -void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
         2399  +void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
  2388   2400     Vdbe *v = pParse->pVdbe;
  2389   2401     VdbeOp *pOp;
  2390   2402     int iMem;
  2391   2403     int addr1, addr2;
  2392   2404     if( v==0 ) return;
  2393   2405     addr1 = sqlite3VdbeCurrentAddr(v);
  2394         -  sqlite3ExprCode(pParse, pExpr, 0);
         2406  +  sqlite3ExprCode(pParse, pExpr, target);
  2395   2407     addr2 = sqlite3VdbeCurrentAddr(v);
  2396   2408     if( addr2>addr1+1
  2397   2409      || ((pOp = sqlite3VdbeGetOp(v, addr1))!=0 && pOp->opcode==OP_Function) ){
  2398   2410       iMem = pExpr->iTable = ++pParse->nMem;
  2399         -    sqlite3VdbeAddOp2(v, OP_Copy, 0, iMem);
         2411  +    sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
  2400   2412       pExpr->op = TK_REGISTER;
  2401   2413     }
  2402   2414   }
  2403   2415   #endif
  2404   2416   
  2405   2417   /*
  2406   2418   ** Generate code that pushes the value of every element of the given

Changes to src/insert.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle INSERT statements in SQLite.
    14     14   **
    15         -** $Id: insert.c,v 1.221 2008/01/10 03:46:36 drh Exp $
           15  +** $Id: insert.c,v 1.222 2008/01/10 23:50:11 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Set P4 of the most recently inserted opcode to a column affinity
    21     21   ** string for index pIdx. A column affinity string has one character
    22     22   ** for each column in the table, according to the affinity of the column:
................................................................................
    24     24   **  Character      Column affinity
    25     25   **  ------------------------------
    26     26   **  'a'            TEXT
    27     27   **  'b'            NONE
    28     28   **  'c'            NUMERIC
    29     29   **  'd'            INTEGER
    30     30   **  'e'            REAL
           31  +**
           32  +** An extra 'b' is appended to the end of the string to cover the
           33  +** rowid that appears as the last column in every index.
    31     34   */
    32     35   void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
    33     36     if( !pIdx->zColAff ){
    34     37       /* The first time a column affinity string for a particular index is
    35     38       ** required, it is allocated and populated here. It is then stored as
    36     39       ** a member of the Index structure for subsequent use.
    37     40       **
................................................................................
    38     41       ** The column affinity string will eventually be deleted by
    39     42       ** sqliteDeleteIndex() when the Index structure itself is cleaned
    40     43       ** up.
    41     44       */
    42     45       int n;
    43     46       Table *pTab = pIdx->pTable;
    44     47       sqlite3 *db = sqlite3VdbeDb(v);
    45         -    pIdx->zColAff = (char *)sqlite3DbMallocZero(db, pIdx->nColumn+1);
           48  +    pIdx->zColAff = (char *)sqlite3DbMallocZero(db, pIdx->nColumn+2);
    46     49       if( !pIdx->zColAff ){
    47     50         return;
    48     51       }
    49     52       for(n=0; n<pIdx->nColumn; n++){
    50     53         pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
    51     54       }
    52         -    pIdx->zColAff[pIdx->nColumn] = '\0';
           55  +    pIdx->zColAff[n++] = SQLITE_AFF_NONE;
           56  +    pIdx->zColAff[n] = 0;
    53     57     }
    54     58    
    55     59     sqlite3VdbeChangeP4(v, -1, pIdx->zColAff, 0);
    56     60   }
    57     61   
    58     62   /*
    59     63   ** Set P4 of the most recently inserted opcode to a column affinity
................................................................................
   675    679     }
   676    680     regData = regRowid+1;
   677    681   
   678    682     /* Run the BEFORE and INSTEAD OF triggers, if there are any
   679    683     */
   680    684     endOfLoop = sqlite3VdbeMakeLabel(v);
   681    685     if( triggers_exist & TRIGGER_BEFORE ){
          686  +    int regRowid;
          687  +    int regCols;
          688  +    int regRec;
   682    689   
   683    690       /* build the NEW.* reference row.  Note that if there is an INTEGER
   684    691       ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
   685    692       ** translated into a unique ID for the row.  But on a BEFORE trigger,
   686    693       ** we do not know what the unique ID will be (because the insert has
   687    694       ** not happened yet) so we substitute a rowid of -1
   688    695       */
          696  +    regRowid = sqlite3GetTempReg(pParse);
   689    697       if( keyColumn<0 ){
   690         -      sqlite3VdbeAddOp2(v, OP_Integer, -1, 0);
          698  +      sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
   691    699       }else if( useTempTable ){
   692         -      sqlite3VdbeAddOp2(v, OP_Column, srcTab, keyColumn);
          700  +      sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
   693    701       }else{
   694    702         int j1;
   695    703         assert( pSelect==0 );  /* Otherwise useTempTable is true */
   696         -      sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, 0);
   697         -      sqlite3VdbeAddOp0(v, OP_SCopy);
   698         -      j1 = sqlite3VdbeAddOp0(v, OP_NotNull);
   699         -      sqlite3VdbeAddOp1(v, OP_Pop, 1);
   700         -      sqlite3VdbeAddOp1(v, OP_Integer, -1);
          704  +      sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
          705  +      j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
          706  +      sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
   701    707         sqlite3VdbeJumpHere(v, j1);
   702         -      sqlite3VdbeAddOp0(v, OP_MustBeInt);
          708  +      sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
   703    709       }
   704    710   
   705    711       /* Cannot have triggers on a virtual table. If it were possible,
   706    712       ** this block would have to account for hidden column.
   707    713       */
   708    714       assert(!IsVirtual(pTab));
   709    715   
   710    716       /* Create the new column data
   711    717       */
          718  +    regCols = sqlite3GetTempRange(pParse, pTab->nCol);
   712    719       for(i=0; i<pTab->nCol; i++){
   713    720         if( pColumn==0 ){
   714    721           j = i;
   715    722         }else{
   716    723           for(j=0; j<pColumn->nId; j++){
   717    724             if( pColumn->a[j].idx==i ) break;
   718    725           }
   719    726         }
   720    727         if( pColumn && j>=pColumn->nId ){
   721         -        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, 0);
          728  +        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i);
   722    729         }else if( useTempTable ){
   723         -        sqlite3VdbeAddOp2(v, OP_Column, srcTab, j); 
          730  +        sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i); 
   724    731         }else{
   725    732           assert( pSelect==0 ); /* Otherwise useTempTable is true */
   726         -        sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr);
          733  +        sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i);
   727    734         }
   728    735       }
   729         -    sqlite3VdbeAddOp2(v, OP_MakeRecord, pTab->nCol, 0);
          736  +    regRec = sqlite3GetTempReg(pParse);
          737  +    sqlite3VdbeAddOp3(v, OP_RegMakeRec, regCols, pTab->nCol, regRec);
   730    738   
   731    739       /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
   732    740       ** do not attempt any conversions before assembling the record.
   733    741       ** If this is a real table, attempt conversions as required by the
   734    742       ** table column affinities.
   735    743       */
   736    744       if( !isView ){
   737    745         sqlite3TableAffinityStr(v, pTab);
   738    746       }
   739         -    sqlite3CodeInsert(pParse, newIdx, OPFLAG_APPEND);
          747  +    sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRec, regRowid);
          748  +    sqlite3ReleaseTempReg(pParse, regRec);
          749  +    sqlite3ReleaseTempReg(pParse, regRowid);
          750  +    sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol);
   740    751   
   741    752       /* Fire BEFORE or INSTEAD OF triggers */
   742    753       if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab, 
   743    754           newIdx, -1, onError, endOfLoop, 0, 0) ){
   744    755         goto insert_cleanup;
   745    756       }
   746    757     }
................................................................................
  1119   1130         case OE_Abort:
  1120   1131         case OE_Fail: {
  1121   1132           sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0,
  1122   1133                            "PRIMARY KEY must be unique", P4_STATIC);
  1123   1134           break;
  1124   1135         }
  1125   1136         case OE_Replace: {
  1126         -        sqlite3GenerateRowIndexDelete(v, pTab, baseCur, 0);
         1137  +        sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
  1127   1138           if( isUpdate ){
  1128   1139             sqlite3VdbeAddOp3(v, OP_MoveGe, baseCur, 0, regRowid-hasTwoRowids);
  1129   1140           }
  1130   1141           seenReplace = 1;
  1131   1142           break;
  1132   1143         }
  1133   1144         case OE_Ignore: {
................................................................................
  1144   1155     }
  1145   1156   
  1146   1157     /* Test all UNIQUE constraints by creating entries for each UNIQUE
  1147   1158     ** index and making sure that duplicate entries do not already exist.
  1148   1159     ** Add the new records to the indices as we go.
  1149   1160     */
  1150   1161     for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
         1162  +    int regIdx;
         1163  +    int regR;
         1164  +
  1151   1165       if( aRegIdx[iCur]==0 ) continue;  /* Skip unused indices */
  1152   1166   
  1153   1167       /* Create a key for accessing the index entry */
  1154         -    sqlite3VdbeAddOp1(v, OP_SCopy, regRowid);
         1168  +    regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
  1155   1169       for(i=0; i<pIdx->nColumn; i++){
  1156   1170         int idx = pIdx->aiColumn[i];
  1157   1171         if( idx==pTab->iPKey ){
  1158         -        sqlite3VdbeAddOp1(v, OP_SCopy, regRowid);
         1172  +        sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
  1159   1173         }else{
  1160         -        sqlite3VdbeAddOp1(v, OP_SCopy, regData+idx);
         1174  +        sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
  1161   1175         }
  1162   1176       }
  1163         -    j2 = sqlite3VdbeAddOp3(v, OP_MakeIdxRec, pIdx->nColumn, 0, aRegIdx[iCur]);
         1177  +    sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
         1178  +    sqlite3VdbeAddOp3(v, OP_RegMakeRec, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
  1164   1179       sqlite3IndexAffinityStr(v, pIdx);
         1180  +    sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
  1165   1181   
  1166   1182       /* Find out what action to take in case there is an indexing conflict */
  1167   1183       onError = pIdx->onError;
  1168   1184       if( onError==OE_None ) continue;  /* pIdx is not a UNIQUE index */
  1169   1185       if( overrideError!=OE_Default ){
  1170   1186         onError = overrideError;
  1171   1187       }else if( onError==OE_Default ){
................................................................................
  1174   1190       if( seenReplace ){
  1175   1191         if( onError==OE_Ignore ) onError = OE_Replace;
  1176   1192         else if( onError==OE_Fail ) onError = OE_Abort;
  1177   1193       }
  1178   1194       
  1179   1195   
  1180   1196       /* Check to see if the new index entry will be unique */
  1181         -    sqlite3VdbeAddOp1(v, OP_SCopy, aRegIdx[iCur]);
  1182         -    sqlite3VdbeAddOp1(v, OP_SCopy, regRowid-hasTwoRowids);
  1183         -    j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0, 0, 0, P4_INT32);
         1197  +    j2 = sqlite3VdbeAddOp3(v, OP_IsNull, regIdx, 0, pIdx->nColumn);
         1198  +    regR = sqlite3GetTempReg(pParse);
         1199  +    sqlite3VdbeAddOp2(v, OP_SCopy, regRowid-hasTwoRowids, regR);
         1200  +    j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
         1201  +                           regR, (char*)aRegIdx[iCur], P4_INT32);
  1184   1202   
  1185   1203       /* Generate code that executes if the new index entry is not unique */
  1186   1204       assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
  1187   1205           || onError==OE_Ignore || onError==OE_Replace );
  1188   1206       switch( onError ){
  1189   1207         case OE_Rollback:
  1190   1208         case OE_Abort:
................................................................................
  1213   1231           sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], 
  1214   1232               pIdx->nColumn>1 ? " are not unique" : " is not unique");
  1215   1233           sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, zErrMsg,0);
  1216   1234           break;
  1217   1235         }
  1218   1236         case OE_Ignore: {
  1219   1237           assert( seenReplace==0 );
  1220         -        sqlite3VdbeAddOp1(v, OP_Pop, 2+hasTwoRowids);
  1221   1238           sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
  1222   1239           break;
  1223   1240         }
  1224   1241         case OE_Replace: {
  1225         -        int iRowid = sqlite3StackToReg(pParse, 1);
  1226         -        sqlite3GenerateRowDelete(pParse->db, v, pTab, baseCur, iRowid, 0);
         1242  +        sqlite3GenerateRowDelete(pParse, pTab, baseCur, regR, 0);
  1227   1243           if( isUpdate ){
  1228         -          sqlite3VdbeAddOp1(v, OP_SCopy, regRowid-hasTwoRowids);
  1229         -          sqlite3VdbeAddOp2(v, OP_MoveGe, baseCur, 0);
         1244  +          sqlite3VdbeAddOp3(v, OP_MoveGe, baseCur, 0, regRowid-hasTwoRowids);
  1230   1245           }
  1231   1246           seenReplace = 1;
  1232   1247           break;
  1233   1248         }
  1234   1249       }
         1250  +    sqlite3VdbeJumpHere(v, j2);
  1235   1251       sqlite3VdbeJumpHere(v, j3);
  1236         -    sqlite3VdbeAddOp1(v, OP_Pop, 1);
  1237         -#if NULL_DISTINCT_FOR_UNIQUE
  1238         -    sqlite3VdbeJumpHere(v, j2);
  1239         -#endif
         1252  +    sqlite3ReleaseTempReg(pParse, regR);
  1240   1253     }
  1241   1254   }
  1242   1255   
  1243   1256   /*
  1244   1257   ** This routine generates code to finish the INSERT or UPDATE operation
  1245   1258   ** that was started by a prior call to sqlite3GenerateConstraintChecks.
  1246   1259   ** A consecutive range of registers starting at regRowid contains the

Changes to src/pragma.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the PRAGMA command.
    13     13   **
    14         -** $Id: pragma.c,v 1.164 2008/01/09 23:04:12 drh Exp $
           14  +** $Id: pragma.c,v 1.165 2008/01/10 23:50:11 drh Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   #include <ctype.h>
    18     18   
    19     19   /* Ignore this whole file if pragmas are disabled
    20     20   */
    21     21   #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
................................................................................
   248    248     const char *zDb = 0;   /* The database name */
   249    249     Token *pId;            /* Pointer to <id> token */
   250    250     int iDb;               /* Database index for <database> */
   251    251     sqlite3 *db = pParse->db;
   252    252     Db *pDb;
   253    253     Vdbe *v = sqlite3GetVdbe(pParse);
   254    254     if( v==0 ) return;
          255  +  pParse->nMem = 1;
   255    256   
   256    257     /* Interpret the [database.] part of the pragma statement. iDb is the
   257    258     ** index of the database this pragma is being applied to in db.aDb[]. */
   258    259     iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
   259    260     if( iDb<0 ) return;
   260    261     pDb = &db->aDb[iDb];
   261    262   
................................................................................
   415    416   
   416    417       assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
   417    418       if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
   418    419         zRet = "exclusive";
   419    420       }
   420    421       sqlite3VdbeSetNumCols(v, 1);
   421    422       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", P4_STATIC);
   422         -    sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, zRet, 0);
   423         -    sqlite3VdbeAddOp2(v, OP_Callback, 1, 0);
          423  +    sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
          424  +    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   424    425     }else
   425    426   #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
   426    427   
   427    428     /*
   428    429     **  PRAGMA [database.]auto_vacuum
   429    430     **  PRAGMA [database.]auto_vacuum=N
   430    431     **
................................................................................
   491    492       }
   492    493       if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
   493    494         iLimit = 0x7fffffff;
   494    495       }
   495    496       sqlite3BeginWriteOperation(pParse, 0, iDb);
   496    497       sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
   497    498       addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
   498         -    sqlite3VdbeAddOp0(v, OP_Callback);
          499  +    sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
   499    500       sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
   500    501       sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
   501    502       sqlite3VdbeJumpHere(v, addr);
   502    503     }else
   503    504   #endif
   504    505   
   505    506   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
................................................................................
   560    561     */
   561    562     if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
   562    563       if( !zRight ){
   563    564         if( sqlite3_temp_directory ){
   564    565           sqlite3VdbeSetNumCols(v, 1);
   565    566           sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
   566    567               "temp_store_directory", P4_STATIC);
   567         -        sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, sqlite3_temp_directory, 0);
   568         -        sqlite3VdbeAddOp2(v, OP_Callback, 1, 0);
          568  +        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
          569  +        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   569    570         }
   570    571       }else{
   571    572         if( zRight[0] 
   572    573          && !sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE) 
   573    574         ){
   574    575           sqlite3ErrorMsg(pParse, "not a writable directory");
   575    576           goto pragma_out;
................................................................................
   639    640       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   640    641       pTab = sqlite3FindTable(db, zRight, zDb);
   641    642       if( pTab ){
   642    643         int i;
   643    644         int nHidden = 0;
   644    645         Column *pCol;
   645    646         sqlite3VdbeSetNumCols(v, 6);
          647  +      pParse->nMem = 6;
   646    648         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P4_STATIC);
   647    649         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
   648    650         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P4_STATIC);
   649    651         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P4_STATIC);
   650    652         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P4_STATIC);
   651    653         sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P4_STATIC);
   652    654         sqlite3ViewGetColumnNames(pParse, pTab);
   653    655         for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
   654    656           const Token *pDflt;
   655    657           if( IsHiddenColumn(pCol) ){
   656    658             nHidden++;
   657    659             continue;
   658    660           }
   659         -        sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 0);
   660         -        sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, pCol->zName, 0);
   661         -        sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0,
          661  +        sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
          662  +        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
          663  +        sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
   662    664              pCol->zType ? pCol->zType : "", 0);
   663         -        sqlite3VdbeAddOp2(v, OP_Integer, pCol->notNull, 0);
          665  +        sqlite3VdbeAddOp2(v, OP_Integer, pCol->notNull, 4);
   664    666           if( pCol->pDflt && (pDflt = &pCol->pDflt->span)->z ){
   665         -          sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, (char*)pDflt->z, pDflt->n);
          667  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pDflt->z, pDflt->n);
   666    668           }else{
   667         -          sqlite3VdbeAddOp2(v, OP_Null, 0, 0);
          669  +          sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
   668    670           }
   669         -        sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 0);
   670         -        sqlite3VdbeAddOp2(v, OP_Callback, 6, 0);
          671  +        sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
          672  +        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
   671    673         }
   672    674       }
   673    675     }else
   674    676   
   675    677     if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
   676    678       Index *pIdx;
   677    679       Table *pTab;
   678    680       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   679    681       pIdx = sqlite3FindIndex(db, zRight, zDb);
   680    682       if( pIdx ){
   681    683         int i;
   682    684         pTab = pIdx->pTable;
   683    685         sqlite3VdbeSetNumCols(v, 3);
          686  +      pParse->nMem = 3;
   684    687         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P4_STATIC);
   685    688         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P4_STATIC);
   686    689         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P4_STATIC);
   687    690         for(i=0; i<pIdx->nColumn; i++){
   688    691           int cnum = pIdx->aiColumn[i];
   689         -        sqlite3VdbeAddOp2(v, OP_Integer, i, 0);
   690         -        sqlite3VdbeAddOp2(v, OP_Integer, cnum, 0);
          692  +        sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
          693  +        sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
   691    694           assert( pTab->nCol>cnum );
   692         -        sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, pTab->aCol[cnum].zName, 0);
   693         -        sqlite3VdbeAddOp2(v, OP_Callback, 3, 0);
          695  +        sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
          696  +        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
   694    697         }
   695    698       }
   696    699     }else
   697    700   
   698    701     if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
   699    702       Index *pIdx;
   700    703       Table *pTab;
................................................................................
   702    705       pTab = sqlite3FindTable(db, zRight, zDb);
   703    706       if( pTab ){
   704    707         v = sqlite3GetVdbe(pParse);
   705    708         pIdx = pTab->pIndex;
   706    709         if( pIdx ){
   707    710           int i = 0; 
   708    711           sqlite3VdbeSetNumCols(v, 3);
          712  +        pParse->nMem = 3;
   709    713           sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
   710    714           sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
   711    715           sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P4_STATIC);
   712    716           while(pIdx){
   713         -          sqlite3VdbeAddOp2(v, OP_Integer, i, 0);
   714         -          sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, pIdx->zName, 0);
   715         -          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 0);
   716         -          sqlite3VdbeAddOp2(v, OP_Callback, 3, 0);
          717  +          sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
          718  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
          719  +          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
          720  +          sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
   717    721             ++i;
   718    722             pIdx = pIdx->pNext;
   719    723           }
   720    724         }
   721    725       }
   722    726     }else
   723    727   
   724    728     if( sqlite3StrICmp(zLeft, "database_list")==0 ){
   725    729       int i;
   726    730       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   727    731       sqlite3VdbeSetNumCols(v, 3);
          732  +    pParse->nMem = 3;
   728    733       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
   729    734       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
   730    735       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P4_STATIC);
   731    736       for(i=0; i<db->nDb; i++){
   732    737         if( db->aDb[i].pBt==0 ) continue;
   733    738         assert( db->aDb[i].zName!=0 );
   734         -      sqlite3VdbeAddOp2(v, OP_Integer, i, 0);
   735         -      sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, db->aDb[i].zName, 0);
   736         -      sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0,
          739  +      sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
          740  +      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
          741  +      sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
   737    742              sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
   738         -      sqlite3VdbeAddOp2(v, OP_Callback, 3, 0);
          743  +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
   739    744       }
   740    745     }else
   741    746   
   742    747     if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
   743    748       int i = 0;
   744    749       HashElem *p;
   745    750       sqlite3VdbeSetNumCols(v, 2);
          751  +    pParse->nMem = 2;
   746    752       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
   747    753       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
   748    754       for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
   749    755         CollSeq *pColl = (CollSeq *)sqliteHashData(p);
   750         -      sqlite3VdbeAddOp2(v, OP_Integer, i++, 0);
   751         -      sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, pColl->zName, 0);
   752         -      sqlite3VdbeAddOp2(v, OP_Callback, 2, 0);
          756  +      sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
          757  +      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
          758  +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
   753    759       }
   754    760     }else
   755    761   #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
   756    762   
   757    763   #ifndef SQLITE_OMIT_FOREIGN_KEY
   758    764     if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
   759    765       FKey *pFK;
................................................................................
   762    768       pTab = sqlite3FindTable(db, zRight, zDb);
   763    769       if( pTab ){
   764    770         v = sqlite3GetVdbe(pParse);
   765    771         pFK = pTab->pFKey;
   766    772         if( pFK ){
   767    773           int i = 0; 
   768    774           sqlite3VdbeSetNumCols(v, 5);
          775  +        pParse->nMem = 5;
   769    776           sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P4_STATIC);
   770    777           sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P4_STATIC);
   771    778           sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P4_STATIC);
   772    779           sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P4_STATIC);
   773    780           sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P4_STATIC);
   774    781           while(pFK){
   775    782             int j;
   776    783             for(j=0; j<pFK->nCol; j++){
   777    784               char *zCol = pFK->aCol[j].zCol;
   778         -            sqlite3VdbeAddOp2(v, OP_Integer, i, 0);
   779         -            sqlite3VdbeAddOp2(v, OP_Integer, j, 0);
   780         -            sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, pFK->zTo, 0);
   781         -            sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0,
          785  +            sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
          786  +            sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
          787  +            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
          788  +            sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
   782    789                                 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
   783         -            sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 0, 0, zCol, 0);
   784         -            sqlite3VdbeAddOp2(v, OP_Callback, 5, 0);
          790  +            sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
          791  +            sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
   785    792             }
   786    793             ++i;
   787    794             pFK = pFK->pNextFrom;
   788    795           }
   789    796         }
   790    797       }
   791    798     }else
................................................................................
   827    834       int i, j, addr, mxErr;
   828    835   
   829    836       /* Code that appears at the end of the integrity check.  If no error
   830    837       ** messages have been generated, output OK.  Otherwise output the
   831    838       ** error message
   832    839       */
   833    840       static const VdbeOpList endCode[] = {
   834         -      { OP_SCopy,       1, 0,        0},
   835         -      { OP_Integer,     0, 0,        0},
   836         -      { OP_Ne,          0, 0,        0},    /* 2 */
   837         -      { OP_String8,     0, 0,        0},    /* 3 */
   838         -      { OP_Callback,    1, 0,        0},
          841  +      { OP_AddImm,      1, 0,        0},    /* 0 */
          842  +      { OP_IfNeg,       1, 0,        0},    /* 1 */
          843  +      { OP_String8,     0, 3,        0},    /* 2 */
          844  +      { OP_ResultRow,   3, 1,        0},
   839    845       };
   840    846   
   841    847       int isQuick = (zLeft[0]=='q');
   842    848   
   843    849       /* Initialize the VDBE program */
   844    850       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
          851  +    pParse->nMem = 6;
   845    852       sqlite3VdbeSetNumCols(v, 1);
   846    853       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P4_STATIC);
   847    854   
   848    855       /* Set the maximum error count */
   849    856       mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
   850    857       if( zRight ){
   851    858         mxErr = atoi(zRight);
   852    859         if( mxErr<=0 ){
   853    860           mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
   854    861         }
   855    862       }
   856         -    sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);
   857         -    pParse->nMem = 1;
          863  +    sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
   858    864   
   859    865       /* Do an integrity check on each database file */
   860    866       for(i=0; i<db->nDb; i++){
   861    867         HashElem *x;
   862    868         Hash *pTbls;
   863    869         int cnt = 0;
   864    870   
   865    871         if( OMIT_TEMPDB && i==1 ) continue;
   866    872   
   867    873         sqlite3CodeVerifySchema(pParse, i);
   868         -      addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
          874  +      addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
   869    875         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
   870    876         sqlite3VdbeJumpHere(v, addr);
   871    877   
   872    878         /* Do an integrity check of the B-Tree
          879  +      **
          880  +      ** Begin by filling registers 2, 3, ... with the root pages numbers
          881  +      ** for all tables and indices in the database.
   873    882         */
   874    883         pTbls = &db->aDb[i].pSchema->tblHash;
   875    884         for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
   876    885           Table *pTab = sqliteHashData(x);
   877    886           Index *pIdx;
   878    887           sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
   879    888           cnt++;
   880    889           for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   881    890             sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
   882    891             cnt++;
   883    892           }
   884    893         }
   885    894         if( cnt==0 ) continue;
          895  +
          896  +      /* Make sure sufficient number of registers have been allocated */
   886    897         if( pParse->nMem < cnt+3 ){
   887    898           pParse->nMem = cnt+3;
   888    899         }
          900  +
          901  +      /* Do the b-tree integrity checks */
   889    902         sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
   890    903         sqlite3VdbeChangeP5(v, i);
   891    904         addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
   892    905         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
   893    906            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
   894    907            P4_DYNAMIC);
   895    908         sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 2);
................................................................................
   900    913         */
   901    914         for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
   902    915           Table *pTab = sqliteHashData(x);
   903    916           Index *pIdx;
   904    917           int loopTop;
   905    918   
   906    919           if( pTab->pIndex==0 ) continue;
   907         -        addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
          920  +        addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
   908    921           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
   909    922           sqlite3VdbeJumpHere(v, addr);
   910    923           sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
   911         -        sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);
          924  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
   912    925           loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
   913         -        sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);
          926  +        sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
   914    927           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
   915    928             int jmp2;
   916    929             static const VdbeOpList idxErr[] = {
   917    930               { OP_AddImm,      1, -1,  0},
   918         -            { OP_String8,     0,  0,  0},    /* 1 */
   919         -            { OP_Rowid,       1,  0,  0},
   920         -            { OP_String8,     0,  0,  0},    /* 3 */
   921         -            { OP_String8,     0,  0,  0},    /* 4 */
   922         -            { OP_Concat,      0,  0,  0},
   923         -            { OP_Concat,      0,  0,  0},
   924         -            { OP_Concat,      0,  0,  0},
   925         -            { OP_Callback,    1,  0,  0},
          931  +            { OP_String8,     0,  3,  0},    /* 1 */
          932  +            { OP_Rowid,       1,  4,  0},
          933  +            { OP_String8,     0,  5,  0},    /* 3 */
          934  +            { OP_String8,     0,  6,  0},    /* 4 */
          935  +            { OP_Concat,      4,  3,  3},
          936  +            { OP_Concat,      5,  3,  3},
          937  +            { OP_Concat,      6,  3,  3},
          938  +            { OP_ResultRow,   3,  1,  0},
   926    939             };
   927         -          sqlite3GenerateIndexKey(v, pIdx, 1);
   928         -          jmp2 = sqlite3VdbeAddOp2(v, OP_Found, j+2, 0);
          940  +          sqlite3GenerateIndexKey(pParse, pIdx, 1, 3);
          941  +          jmp2 = sqlite3VdbeAddOp3(v, OP_Found, j+2, 0, 3);
   929    942             addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
   930    943             sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
   931    944             sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
   932    945             sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
   933    946             sqlite3VdbeJumpHere(v, jmp2);
   934    947           }
   935    948           sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
................................................................................
   936    949           sqlite3VdbeJumpHere(v, loopTop);
   937    950           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
   938    951             static const VdbeOpList cntIdx[] = {
   939    952                { OP_Integer,      0,  3,  0},
   940    953                { OP_Rewind,       0,  0,  0},  /* 1 */
   941    954                { OP_AddImm,       3,  1,  0},
   942    955                { OP_Next,         0,  0,  0},  /* 3 */
   943         -             { OP_SCopy,        2,  0,  0},
   944         -             { OP_SCopy,        3,  0,  0},
   945         -             { OP_Eq,           0,  0,  0},  /* 6 */
          956  +             { OP_Eq,           2,  0,  3},  /* 4 */
   946    957                { OP_AddImm,       1, -1,  0},
   947         -             { OP_String8,      0,  0,  0},  /* 8 */
   948         -             { OP_String8,      0,  0,  0},  /* 9 */
   949         -             { OP_Concat,       0,  0,  0},
   950         -             { OP_Callback,     1,  0,  0},
          958  +             { OP_String8,      0,  2,  0},  /* 6 */
          959  +             { OP_String8,      0,  3,  0},  /* 7 */
          960  +             { OP_Concat,       3,  2,  2},
          961  +             { OP_ResultRow,    2,  1,  0},
   951    962             };
   952    963             if( pIdx->tnum==0 ) continue;
   953    964             addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
   954    965             sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
   955    966             sqlite3VdbeJumpHere(v, addr);
   956    967             addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
   957    968             sqlite3VdbeChangeP1(v, addr+1, j+2);
   958    969             sqlite3VdbeChangeP2(v, addr+1, addr+4);
   959    970             sqlite3VdbeChangeP1(v, addr+3, j+2);
   960    971             sqlite3VdbeChangeP2(v, addr+3, addr+2);
   961         -          sqlite3VdbeJumpHere(v, addr+6);
   962         -          sqlite3VdbeChangeP4(v, addr+8, 
          972  +          sqlite3VdbeJumpHere(v, addr+4);
          973  +          sqlite3VdbeChangeP4(v, addr+6, 
   963    974                        "wrong # of entries in index ", P4_STATIC);
   964         -          sqlite3VdbeChangeP4(v, addr+9, pIdx->zName, P4_STATIC);
          975  +          sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
   965    976           }
   966    977         } 
   967    978       }
   968    979       addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
   969         -    sqlite3VdbeChangeP1(v, addr+1, mxErr);
   970         -    sqlite3VdbeChangeP4(v, addr+3, "ok", P4_STATIC);
   971         -    sqlite3VdbeJumpHere(v, addr+2);
          980  +    sqlite3VdbeChangeP2(v, addr, -mxErr);
          981  +    sqlite3VdbeJumpHere(v, addr+1);
          982  +    sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
   972    983     }else
   973    984   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
   974    985   
   975    986   #ifndef SQLITE_OMIT_UTF16
   976    987     /*
   977    988     **   PRAGMA encoding
   978    989     **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
................................................................................
  1011   1022         { 0, 0 }
  1012   1023       };
  1013   1024       const struct EncName *pEnc;
  1014   1025       if( !zRight ){    /* "PRAGMA encoding" */
  1015   1026         if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  1016   1027         sqlite3VdbeSetNumCols(v, 1);
  1017   1028         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P4_STATIC);
  1018         -      sqlite3VdbeAddOp2(v, OP_String8, 0, 0);
         1029  +      sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
  1019   1030         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
  1020   1031           if( pEnc->enc==ENC(pParse->db) ){
  1021   1032             sqlite3VdbeChangeP4(v, -1, pEnc->zName, P4_STATIC);
  1022   1033             break;
  1023   1034           }
  1024   1035         }
  1025         -      sqlite3VdbeAddOp2(v, OP_Callback, 1, 0);
         1036  +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
  1026   1037       }else{                        /* "PRAGMA encoding = XXX" */
  1027   1038         /* Only change the value of sqlite.enc if the database handle is not
  1028   1039         ** initialized. If the main database exists, the new sqlite.enc value
  1029   1040         ** will be overwritten when the schema is next loaded. If it does not
  1030   1041         ** already exists, it will be created to use the new encoding value.
  1031   1042         */
  1032   1043         if( 
................................................................................
  1105   1116         sqlite3VdbeChangeP1(v, addr, iDb);
  1106   1117         sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
  1107   1118         sqlite3VdbeChangeP1(v, addr+2, iDb);
  1108   1119         sqlite3VdbeChangeP2(v, addr+2, iCookie);
  1109   1120       }else{
  1110   1121         /* Read the specified cookie value */
  1111   1122         static const VdbeOpList readCookie[] = {
  1112         -        { OP_ReadCookie,      0,  0,  0},    /* 0 */
  1113         -        { OP_Callback,        1,  0,  0}
         1123  +        { OP_ReadCookie,      0,  1,  0},    /* 0 */
         1124  +        { OP_ResultRow,       1,  1,  0}
  1114   1125         };
  1115   1126         int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
  1116   1127         sqlite3VdbeChangeP1(v, addr, iDb);
  1117   1128         sqlite3VdbeChangeP3(v, addr, iCookie);
  1118   1129         sqlite3VdbeSetNumCols(v, 1);
  1119   1130         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, P4_TRANSIENT);
  1120   1131       }
................................................................................
  1128   1139     if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
  1129   1140       static const char *const azLockName[] = {
  1130   1141         "unlocked", "shared", "reserved", "pending", "exclusive"
  1131   1142       };
  1132   1143       int i;
  1133   1144       Vdbe *v = sqlite3GetVdbe(pParse);
  1134   1145       sqlite3VdbeSetNumCols(v, 2);
         1146  +    pParse->nMem = 2;
  1135   1147       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P4_STATIC);
  1136   1148       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P4_STATIC);
  1137   1149       for(i=0; i<db->nDb; i++){
  1138   1150         Btree *pBt;
  1139   1151         Pager *pPager;
  1140   1152         const char *zState = "unknown";
  1141   1153         int j;
  1142   1154         if( db->aDb[i].zName==0 ) continue;
  1143         -      sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, db->aDb[i].zName, P4_STATIC);
         1155  +      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
  1144   1156         pBt = db->aDb[i].pBt;
  1145   1157         if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
  1146   1158           zState = "closed";
  1147   1159         }else if( sqlite3_file_control(db, db->aDb[i].zName, 
  1148   1160                                        SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
  1149   1161            zState = azLockName[j];
  1150   1162         }
  1151         -      sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0, zState, P4_STATIC);
  1152         -      sqlite3VdbeAddOp2(v, OP_Callback, 2, 0);
         1163  +      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
         1164  +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
  1153   1165       }
  1154   1166     }else
  1155   1167   #endif
  1156   1168   
  1157   1169   #ifdef SQLITE_SSE
  1158   1170     /*
  1159   1171     ** Check to see if the sqlite_statements table exists.  Create it

Changes to src/select.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.396 2008/01/10 03:46:36 drh Exp $
           15  +** $Id: select.c,v 1.397 2008/01/10 23:50:11 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /*
    21     21   ** Delete all the content of a Select structure but do not deallocate
    22     22   ** the select structure itself.
................................................................................
   765    765     int iTab;
   766    766     int pseudoTab = 0;
   767    767     ExprList *pOrderBy = p->pOrderBy;
   768    768   
   769    769     int eDest = pDest->eDest;
   770    770     int iParm = pDest->iParm;
   771    771   
          772  +  int regRow;
          773  +  int regRowid;
          774  +
   772    775     iTab = pOrderBy->iECursor;
   773    776     if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
   774    777       pseudoTab = pParse->nTab++;
   775    778       sqlite3VdbeAddOp2(v, OP_OpenPseudo, pseudoTab, 0);
   776    779       sqlite3VdbeAddOp2(v, OP_SetNumColumns, pseudoTab, nColumn);
   777    780     }
   778    781     addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, brk);
   779    782     codeOffset(v, p, cont, 0);
   780         -  if( eDest==SRT_Callback || eDest==SRT_Subroutine ){
   781         -    sqlite3VdbeAddOp2(v, OP_Integer, 1, 0);
   782         -  }
   783         -  sqlite3VdbeAddOp2(v, OP_Column, iTab, pOrderBy->nExpr + 1);
          783  +  regRow = sqlite3GetTempReg(pParse);
          784  +  regRowid = sqlite3GetTempReg(pParse);
          785  +  sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow);
   784    786     switch( eDest ){
   785    787       case SRT_Table:
   786    788       case SRT_EphemTab: {
   787         -      sqlite3VdbeAddOp1(v, OP_NewRowid, iParm);
   788         -      sqlite3VdbeAddOp2(v, OP_Pull, 1, 0);
   789         -      sqlite3CodeInsert(pParse, iParm, OPFLAG_APPEND);
          789  +      sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
          790  +      sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
          791  +      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   790    792         break;
   791    793       }
   792    794   #ifndef SQLITE_OMIT_SUBQUERY
   793    795       case SRT_Set: {
   794         -      int j1, j2;
          796  +      int j1;
   795    797         assert( nColumn==1 );
   796         -      sqlite3VdbeAddOp0(v, OP_SCopy);
   797         -      j1 = sqlite3VdbeAddOp0(v, OP_NotNull);
   798         -      sqlite3VdbeAddOp1(v, OP_Pop, 1);
   799         -      j2 = sqlite3VdbeAddOp0(v, OP_Goto);
          798  +      j1 = sqlite3VdbeAddOp1(v, OP_IsNull, regRow);
          799  +      sqlite3VdbeAddOp4(v, OP_RegMakeRec, regRow, 1, regRow, &p->affinity, 1);
          800  +      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRow);
   800    801         sqlite3VdbeJumpHere(v, j1);
   801         -      sqlite3VdbeAddOp4(v, OP_MakeRecord, 1, 0, 0, &p->affinity, 1);
   802         -      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, 0);
   803         -      sqlite3VdbeJumpHere(v, j2);
   804    802         break;
   805    803       }
   806    804       case SRT_Mem: {
   807    805         assert( nColumn==1 );
   808         -      sqlite3VdbeAddOp2(v, OP_Move, 0, iParm);
          806  +      sqlite3VdbeAddOp2(v, OP_Move, regRow, iParm);
   809    807         /* The LIMIT clause will terminate the loop for us */
   810    808         break;
   811    809       }
   812    810   #endif
   813    811       case SRT_Callback:
   814    812       case SRT_Subroutine: {
   815    813         int i;
   816         -      sqlite3CodeInsert(pParse, pseudoTab, 0);
          814  +      sqlite3VdbeAddOp2(v, OP_Integer, 1, regRowid);
          815  +      sqlite3VdbeAddOp3(v, OP_Insert, pseudoTab, regRow, regRowid);
   817    816         for(i=0; i<nColumn; i++){
   818    817           sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
   819    818         }
   820    819         if( eDest==SRT_Callback ){
   821    820           sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
   822    821         }else{
   823    822           sqlite3VdbeAddOp2(v, OP_Gosub, 0, iParm);
................................................................................
   825    824         break;
   826    825       }
   827    826       default: {
   828    827         /* Do nothing */
   829    828         break;
   830    829       }
   831    830     }
          831  +  sqlite3ReleaseTempReg(pParse, regRow);
          832  +  sqlite3ReleaseTempReg(pParse, regRowid);
   832    833   
   833    834     /* Jump to the end of the loop when the LIMIT is reached
   834    835     */
   835    836     if( p->iLimit>=0 ){
   836    837       sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
   837    838       sqlite3VdbeAddOp2(v, OP_IfZero, p->iLimit, brk);
   838    839     }
................................................................................
  2647   2648       return ORDERBY_MIN;
  2648   2649     }else if( sqlite3StrNICmp((char*)pExpr->token.z,"max",3)==0 ){
  2649   2650       return ORDERBY_MAX;
  2650   2651     }
  2651   2652     return ORDERBY_NORMAL;
  2652   2653   }
  2653   2654   
  2654         -/*
  2655         -** Analyze the SELECT statement passed in as an argument to see if it
  2656         -** is a simple min() or max() query.  If it is and this query can be
  2657         -** satisfied using a single seek to the beginning or end of an index,
  2658         -** then generate the code for this SELECT and return 1.  If this is not a 
  2659         -** simple min() or max() query, then return 0;
  2660         -**
  2661         -** A simply min() or max() query looks like this:
  2662         -**
  2663         -**    SELECT min(a) FROM table;
  2664         -**    SELECT max(a) FROM table;
  2665         -**
  2666         -** The query may have only a single table in its FROM argument.  There
  2667         -** can be no GROUP BY or HAVING or WHERE clauses.  The result set must
  2668         -** be the min() or max() of a single column of the table.  The column
  2669         -** in the min() or max() function must be indexed.
  2670         -**
  2671         -** The parameters to this routine are the same as for sqlite3Select().
  2672         -** See the header comment on that routine for additional information.
  2673         -*/
  2674         -#if 0
  2675         -static int simpleMinMaxQuery(Parse *pParse, Select *p, SelectDest *pDest){
  2676         -  Expr *pExpr;
  2677         -  int iCol;
  2678         -  Table *pTab;
  2679         -  Index *pIdx;
  2680         -  int base;
  2681         -  Vdbe *v;
  2682         -  int seekOp;
  2683         -  ExprList *pEList, *pList, eList;
  2684         -  struct ExprList_item eListItem;
  2685         -  SrcList *pSrc;
  2686         -  int brk;
  2687         -  int iDb;
  2688         -
  2689         -  /* Check to see if this query is a simple min() or max() query.  Return
  2690         -  ** zero if it is  not.
  2691         -  */
  2692         -  if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
  2693         -  pSrc = p->pSrc;
  2694         -  if( pSrc->nSrc!=1 ) return 0;
  2695         -  pEList = p->pEList;
  2696         -  if( pEList->nExpr!=1 ) return 0;
  2697         -  pExpr = pEList->a[0].pExpr;
  2698         -  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
  2699         -  pList = pExpr->pList;
  2700         -  if( pList==0 || pList->nExpr!=1 ) return 0;
  2701         -  if( pExpr->token.n!=3 ) return 0;
  2702         -  if( sqlite3StrNICmp((char*)pExpr->token.z,"min",3)==0 ){
  2703         -    seekOp = OP_Rewind;
  2704         -  }else if( sqlite3StrNICmp((char*)pExpr->token.z,"max",3)==0 ){
  2705         -    seekOp = OP_Last;
  2706         -  }else{
  2707         -    return 0;
  2708         -  }
  2709         -  pExpr = pList->a[0].pExpr;
  2710         -  if( pExpr->op!=TK_COLUMN ) return 0;
  2711         -  iCol = pExpr->iColumn;
  2712         -  pTab = pSrc->a[0].pTab;
  2713         -
  2714         -  /* This optimization cannot be used with virtual tables. */
  2715         -  if( IsVirtual(pTab) ) return 0;
  2716         -
  2717         -  /* If we get to here, it means the query is of the correct form.
  2718         -  ** Check to make sure we have an index and make pIdx point to the
  2719         -  ** appropriate index.  If the min() or max() is on an INTEGER PRIMARY
  2720         -  ** key column, no index is necessary so set pIdx to NULL.  If no
  2721         -  ** usable index is found, return 0.
  2722         -  */
  2723         -  if( iCol<0 ){
  2724         -    pIdx = 0;
  2725         -  }else{
  2726         -    CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
  2727         -    if( pColl==0 ) return 0;
  2728         -    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  2729         -      assert( pIdx->nColumn>=1 );
  2730         -      if( pIdx->aiColumn[0]==iCol && 
  2731         -          0==sqlite3StrICmp(pIdx->azColl[0], pColl->zName) ){
  2732         -        break;
  2733         -      }
  2734         -    }
  2735         -    if( pIdx==0 ) return 0;
  2736         -  }
  2737         -
  2738         -  /* Identify column types if we will be using the callback.  This
  2739         -  ** step is skipped if the output is going to a table or a memory cell.
  2740         -  ** The column names have already been generated in the calling function.
  2741         -  */
  2742         -  v = sqlite3GetVdbe(pParse);
  2743         -  if( v==0 ) return 0;
  2744         -
  2745         -  /* If the output is destined for a temporary table, open that table.
  2746         -  */
  2747         -  if( pDest->eDest==SRT_EphemTab ){
  2748         -    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, 1);
  2749         -  }
  2750         -
  2751         -  /* Generating code to find the min or the max.  Basically all we have
  2752         -  ** to do is find the first or the last entry in the chosen index.  If
  2753         -  ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
  2754         -  ** or last entry in the main table.
  2755         -  */
  2756         -  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  2757         -  assert( iDb>=0 || pTab->isEphem );
  2758         -  sqlite3CodeVerifySchema(pParse, iDb);
  2759         -  sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  2760         -  base = pSrc->a[0].iCursor;
  2761         -  brk = sqlite3VdbeMakeLabel(v);
  2762         -  computeLimitRegisters(pParse, p, brk);
  2763         -  if( pSrc->a[0].pSelect==0 ){
  2764         -    sqlite3OpenTable(pParse, base, iDb, pTab, OP_OpenRead);
  2765         -  }
  2766         -  if( pIdx==0 ){
  2767         -    sqlite3VdbeAddOp2(v, seekOp, base, 0);
  2768         -  }else{
  2769         -    /* Even though the cursor used to open the index here is closed
  2770         -    ** as soon as a single value has been read from it, allocate it
  2771         -    ** using (pParse->nTab++) to prevent the cursor id from being 
  2772         -    ** reused. This is important for statements of the form 
  2773         -    ** "INSERT INTO x SELECT max() FROM x".
  2774         -    */
  2775         -    int iIdx;
  2776         -    KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
  2777         -    iIdx = pParse->nTab++;
  2778         -    assert( pIdx->pSchema==pTab->pSchema );
  2779         -    sqlite3VdbeAddOp4(v, OP_OpenRead, iIdx, pIdx->tnum, iDb,
  2780         -        (char*)pKey, P4_KEYINFO_HANDOFF);
  2781         -    if( seekOp==OP_Rewind ){
  2782         -      sqlite3VdbeAddOp2(v, OP_Null, 0, 0);
  2783         -      sqlite3VdbeAddOp2(v, OP_MakeRecord, 1, 0);
  2784         -      seekOp = OP_MoveGt;
  2785         -    }
  2786         -    if( pIdx->aSortOrder[0]==SQLITE_SO_DESC ){
  2787         -      /* Ticket #2514: invert the seek operator if we are using
  2788         -      ** a descending index. */
  2789         -      if( seekOp==OP_Last ){
  2790         -        seekOp = OP_Rewind;
  2791         -      }else{
  2792         -        assert( seekOp==OP_MoveGt );
  2793         -        seekOp = OP_MoveLt;
  2794         -      }
  2795         -    }
  2796         -    sqlite3VdbeAddOp2(v, seekOp, iIdx, 0);
  2797         -    sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdx, 0);
  2798         -    sqlite3VdbeAddOp2(v, OP_Close, iIdx, 0);
  2799         -    sqlite3VdbeAddOp2(v, OP_MoveGe, base, 0);
  2800         -  }
  2801         -  eList.nExpr = 1;
  2802         -  memset(&eListItem, 0, sizeof(eListItem));
  2803         -  eList.a = &eListItem;
  2804         -  eList.a[0].pExpr = pExpr;
  2805         -  selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, pDest, brk, brk, 0);
  2806         -  sqlite3VdbeResolveLabel(v, brk);
  2807         -  sqlite3VdbeAddOp2(v, OP_Close, base, 0);
  2808         -  
  2809         -  return 1;
  2810         -}
  2811         -#endif 
  2812         -
  2813   2655   /*
  2814   2656   ** This routine resolves any names used in the result set of the
  2815   2657   ** supplied SELECT statement. If the SELECT statement being resolved
  2816   2658   ** is a sub-select, then pOuterNC is a pointer to the NameContext 
  2817   2659   ** of the parent SELECT.
  2818   2660   */
  2819   2661   int sqlite3SelectResolve(

Changes to src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.646 2008/01/10 03:46:36 drh Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.647 2008/01/10 23:50:11 drh Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** The macro unlikely() is a hint that surrounds a boolean
    21     21   ** expression that is usually false.  Macro likely() surrounds
................................................................................
  1762   1762   void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
  1763   1763   void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
  1764   1764   void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
  1765   1765   WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**, u8);
  1766   1766   void sqlite3WhereEnd(WhereInfo*);
  1767   1767   void sqlite3ExprCodeGetColumn(Vdbe*, Table*, int, int, int);
  1768   1768   int sqlite3ExprCode(Parse*, Expr*, int);
  1769         -void sqlite3ExprCodeAndCache(Parse*, Expr*);
         1769  +void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
  1770   1770   int sqlite3ExprCodeExprList(Parse*, ExprList*, int);
  1771   1771   void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
  1772   1772   void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
  1773   1773   Table *sqlite3FindTable(sqlite3*,const char*, const char*);
  1774   1774   Table *sqlite3LocateTable(Parse*,const char*, const char*);
  1775   1775   Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
  1776   1776   void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
................................................................................
  1791   1791   void sqlite3CommitTransaction(Parse*);
  1792   1792   void sqlite3RollbackTransaction(Parse*);
  1793   1793   int sqlite3ExprIsConstant(Expr*);
  1794   1794   int sqlite3ExprIsConstantNotJoin(Expr*);
  1795   1795   int sqlite3ExprIsConstantOrFunction(Expr*);
  1796   1796   int sqlite3ExprIsInteger(Expr*, int*);
  1797   1797   int sqlite3IsRowid(const char*);
  1798         -void sqlite3GenerateRowDelete(sqlite3*, Vdbe*, Table*, int, int, int);
  1799         -void sqlite3GenerateRowIndexDelete(Vdbe*, Table*, int, int*);
  1800         -void sqlite3GenerateIndexKey(Vdbe*, Index*, int);
         1798  +void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int);
         1799  +void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
         1800  +int sqlite3GenerateIndexKey(Parse*, Index*, int, int);
  1801   1801   void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
  1802   1802                                        int*,int,int,int,int);
  1803   1803   void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*,int,int,int,int);
  1804   1804   int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
  1805   1805   void sqlite3BeginWriteOperation(Parse*, int, int);
  1806   1806   Expr *sqlite3ExprDup(sqlite3*,Expr*);
  1807   1807   void sqlite3TokenCopy(sqlite3*,Token*, Token*);

Changes to src/trigger.c.

   223    223       goto triggerfinish_cleanup;
   224    224     }
   225    225   
   226    226     /* if we are not initializing, and this trigger is not on a TEMP table, 
   227    227     ** build the sqlite_master entry
   228    228     */
   229    229     if( !db->init.busy ){
   230         -    static const VdbeOpList insertTrig[] = {
   231         -      { OP_NewRowid,   0, 0,  0          },
   232         -      { OP_String8,    0, 0,  0          },  /* 1: "trigger" */
   233         -      { OP_String8,    0, 0,  0          },  /* 2: trigger name */
   234         -      { OP_String8,    0, 0,  0          },  /* 3: table name */
   235         -      { OP_Integer,    0, 0,  0          },
   236         -      { OP_String8,    0, 0,  0          },  /* 5: "CREATE TRIGGER " */
   237         -      { OP_String8,    0, 0,  0          },  /* 6: SQL */
   238         -      { OP_Concat,     0, 0,  0          }, 
   239         -      { OP_MakeRecord, 5, 0,  0          },  /* 8: "aaada" */
   240         -      { OP_Move,       0, 0,  0          },  /* 9: Store data */
   241         -      { OP_Move,       0, 0,  0          },  /* 10: Store key */
   242         -      { OP_Insert,     0, 0,  0          },
   243         -    };
   244         -    int addr;
   245    230       Vdbe *v;
   246         -    int iKey = ++pParse->nMem;
   247         -    int iData = ++pParse->nMem;
          231  +    char *z;
   248    232   
   249    233       /* Make an entry in the sqlite_master table */
   250    234       v = sqlite3GetVdbe(pParse);
   251    235       if( v==0 ) goto triggerfinish_cleanup;
   252    236       sqlite3BeginWriteOperation(pParse, 0, iDb);
   253         -    sqlite3OpenMasterTable(pParse, iDb);
   254         -    addr = sqlite3VdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
   255         -    sqlite3VdbeChangeP4(v, addr+1, "trigger", P4_STATIC);
   256         -    sqlite3VdbeChangeP4(v, addr+2, pTrig->name, 0); 
   257         -    sqlite3VdbeChangeP4(v, addr+3, pTrig->table, 0);
   258         -    sqlite3VdbeChangeP4(v, addr+5, "CREATE TRIGGER ", P4_STATIC); 
   259         -    sqlite3VdbeChangeP4(v, addr+6, (char*)pAll->z, pAll->n);
   260         -    sqlite3VdbeChangeP4(v, addr+8, "aaada", P4_STATIC);
   261         -    sqlite3VdbeChangeP2(v, addr+9, iData);
   262         -    sqlite3VdbeChangeP2(v, addr+10, iKey);
   263         -    sqlite3VdbeChangeP2(v, addr+11, iData);
   264         -    sqlite3VdbeChangeP3(v, addr+11, iKey);
          237  +    z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
          238  +    sqlite3NestedParse(pParse,
          239  +       "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
          240  +       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrig->name,
          241  +       pTrig->table, z);
          242  +    sqlite3_free(z);
   265    243       sqlite3ChangeCookie(db, v, iDb);
   266         -    sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
   267    244       sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
   268    245           db, "type='trigger' AND name='%q'", pTrig->name), P4_DYNAMIC
   269    246       );
   270    247     }
   271    248   
   272    249     if( db->init.busy ){
   273    250       int n;

Changes to src/update.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle UPDATE statements.
    14     14   **
    15         -** $Id: update.c,v 1.166 2008/01/09 23:04:12 drh Exp $
           15  +** $Id: update.c,v 1.167 2008/01/10 23:50:11 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   #ifndef SQLITE_OMIT_VIRTUALTABLE
    20     20   /* Forward declaration */
    21     21   static void updateVirtualTable(
    22     22     Parse *pParse,       /* The parsing context */
................................................................................
   400    400     }
   401    401   
   402    402     /* Top of the update loop */
   403    403     addr = sqlite3VdbeAddOp2(v, OP_FifoRead, regOldRowid, 0);
   404    404     sqlite3VdbeAddOp2(v, OP_StackDepth, -1, 0);
   405    405   
   406    406     if( triggers_exist ){
          407  +    int regRowid;
          408  +    int regRow;
          409  +    int regCols;
          410  +
   407    411       /* Make cursor iCur point to the record that is being updated.
   408    412       */
   409    413       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
   410    414   
   411    415       /* Generate the OLD table
   412    416       */
   413         -    sqlite3VdbeAddOp2(v, OP_Rowid, iCur, 0);
          417  +    regRowid = sqlite3GetTempReg(pParse);
          418  +    regRow = sqlite3GetTempReg(pParse);
          419  +    sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid);
   414    420       if( !old_col_mask ){
   415         -      sqlite3VdbeAddOp2(v, OP_Null, 0, 0);
          421  +      sqlite3VdbeAddOp2(v, OP_Null, 0, regRow);
   416    422       }else{
   417         -      sqlite3VdbeAddOp1(v, OP_RowData, iCur);
          423  +      sqlite3VdbeAddOp2(v, OP_RowData, iCur, regRow);
   418    424       }
   419         -    sqlite3CodeInsert(pParse, oldIdx, 0);
          425  +    sqlite3VdbeAddOp3(v, OP_Insert, oldIdx, regRow, regRowid);
   420    426   
   421    427       /* Generate the NEW table
   422    428       */
   423    429       if( chngRowid ){
   424         -      sqlite3ExprCodeAndCache(pParse, pRowidExpr);
          430  +      sqlite3ExprCodeAndCache(pParse, pRowidExpr, regRowid);
   425    431       }else{
   426         -      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, 0);
          432  +      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid);
   427    433       }
          434  +    regCols = sqlite3GetTempRange(pParse, pTab->nCol);
   428    435       for(i=0; i<pTab->nCol; i++){
   429    436         if( i==pTab->iPKey ){
   430         -        sqlite3VdbeAddOp2(v, OP_Null, 0, 0);
          437  +        sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i);
   431    438           continue;
   432    439         }
   433    440         j = aXRef[i];
   434    441         if( new_col_mask&((u32)1<<i) || new_col_mask==0xffffffff ){
   435    442           if( j<0 ){
   436         -          sqlite3VdbeAddOp2(v, OP_Column, iCur, i);
          443  +          sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regCols+i);
   437    444             sqlite3ColumnDefault(v, pTab, i);
   438    445           }else{
   439         -          sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr);
          446  +          sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr, regCols+i);
   440    447           }
   441    448         }else{
   442         -        sqlite3VdbeAddOp2(v, OP_Null, 0, 0);
          449  +        sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i);
   443    450         }
   444    451       }
   445         -    sqlite3VdbeAddOp2(v, OP_MakeRecord, pTab->nCol, 0);
          452  +    sqlite3VdbeAddOp3(v, OP_RegMakeRec, regCols, pTab->nCol, regRow);
   446    453       if( !isView ){
   447    454         sqlite3TableAffinityStr(v, pTab);
   448    455       }
          456  +    sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol);
   449    457       if( pParse->nErr ) goto update_cleanup;
   450         -    sqlite3CodeInsert(pParse, newIdx, 0);
          458  +    sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRow, regRowid);
          459  +    sqlite3ReleaseTempReg(pParse, regRowid);
          460  +    sqlite3ReleaseTempReg(pParse, regRow);
   451    461   
   452    462       sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger);
   453    463       sqlite3VdbeJumpHere(v, iEndBeforeTrigger);
   454    464     }
   455    465   
   456    466     if( !isView && !IsVirtual(pTab) ){
   457    467       /* Loop over every record that needs updating.  We have to load
................................................................................
   491    501       */
   492    502       sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
   493    503                                       aRegIdx, chngRowid, 1,
   494    504                                       onError, addr);
   495    505   
   496    506       /* Delete the old indices for the current record.
   497    507       */
   498         -    sqlite3GenerateRowIndexDelete(v, pTab, iCur, aRegIdx);
          508  +    sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
   499    509   
   500    510       /* If changing the record number, delete the old record.
   501    511       */
   502    512       if( chngRowid ){
   503    513         sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
   504    514       }
   505    515   

Changes to src/vdbe.c.

    39     39   **
    40     40   ** Various scripts scan this source file in order to generate HTML
    41     41   ** documentation, headers files, or other derived files.  The formatting
    42     42   ** of the code in this file is, therefore, important.  See other comments
    43     43   ** in this file for details.  If in doubt, do not deviate from existing
    44     44   ** commenting and indentation practices when changing or adding code.
    45     45   **
    46         -** $Id: vdbe.c,v 1.691 2008/01/09 23:04:12 drh Exp $
           46  +** $Id: vdbe.c,v 1.692 2008/01/10 23:50:11 drh Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #include <ctype.h>
    50     50   #include "vdbeInt.h"
    51     51   
    52     52   /*
    53     53   ** The following global variable is incremented every time a cursor
................................................................................
  1117   1117     if( pOp->p2==0 ){
  1118   1118       pOut = ++pTos;
  1119   1119       pOut->flags = MEM_Null;
  1120   1120     }else{
  1121   1121       assert( pOp->p2<=p->nMem );
  1122   1122       pOut = &p->aMem[pOp->p2];
  1123   1123     }
         1124  +  assert( pOut!=pIn1 );
  1124   1125     if( pOp->opcode==OP_Move ){
  1125   1126       rc = sqlite3VdbeMemMove(pOut, pIn1);
  1126   1127       if( pOp->p1==0 ) pTos--;
  1127   1128     }else{
         1129  +    Release(pOut);
  1128   1130       sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
  1129   1131       if( pOp->opcode==OP_Copy ){
  1130   1132         Deephemeralize(pOut);
  1131   1133       }
  1132   1134     }
  1133   1135     REGISTER_TRACE(pOp->p2, pOut);
  1134   1136     break;
................................................................................
  1164   1166       assert( pTos->flags & (MEM_Str|MEM_Blob) );
  1165   1167       assert( pTos->z==pTos[-pOp->p1].zShort );
  1166   1168       pTos->z = pTos->zShort;
  1167   1169     }
  1168   1170     break;
  1169   1171   }
  1170   1172   
  1171         -/* Opcode: Callback P1 * *
  1172         -**
  1173         -** The top P1 values on the stack represent a single result row from
  1174         -** a query.  This opcode causes the sqlite3_step() call to terminate
  1175         -** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
  1176         -** structure to provide access to the top P1 values as the result
  1177         -** row.  When the sqlite3_step() function is run again, the top P1
  1178         -** values will be automatically popped from the stack before the next
  1179         -** instruction executes.
  1180         -*/
  1181         -case OP_Callback: {            /* no-push */
  1182         -  Mem *pMem;
  1183         -  Mem *pFirstColumn;
  1184         -  assert( p->nResColumn==pOp->p1 );
  1185         -
  1186         -  /* Data in the pager might be moved or changed out from under us
  1187         -  ** in between the return from this sqlite3_step() call and the
  1188         -  ** next call to sqlite3_step().  So deephermeralize everything on 
  1189         -  ** the stack.  Note that ephemeral data is never stored in memory 
  1190         -  ** cells so we do not have to worry about them.
  1191         -  */
  1192         -  pFirstColumn = &pTos[1-pOp->p1];
  1193         -  for(pMem = p->aStack; pMem<pFirstColumn; pMem++){
  1194         -    Deephemeralize(pMem);
  1195         -  }
  1196         -
  1197         -  /* Invalidate all ephemeral cursor row caches */
  1198         -  p->cacheCtr = (p->cacheCtr + 2)|1;
  1199         -
  1200         -  /* Make sure the results of the current row are \000 terminated
  1201         -  ** and have an assigned type.  The results are deephemeralized as
  1202         -  ** as side effect.
  1203         -  */
  1204         -  for(; pMem<=pTos; pMem++ ){
  1205         -    sqlite3VdbeMemNulTerminate(pMem);
  1206         -    storeTypeInfo(pMem, encoding);
  1207         -  }
  1208         -
  1209         -  /* Set up the statement structure so that it will pop the current
  1210         -  ** results from the stack when the statement returns.
  1211         -  */
  1212         -  p->pResultSet = pFirstColumn;
  1213         -  p->nCallback++;
  1214         -  p->popStack = pOp->p1;
  1215         -  p->pc = pc + 1;
  1216         -  p->pTos = pTos;
  1217         -  rc = SQLITE_ROW;
  1218         -  goto vdbe_return;
  1219         -}
  1220         -
  1221   1173   /* Opcode: ResultRow P1 P2 *
  1222   1174   **
  1223   1175   ** The registers P1 throught P1+P2-1 contain a single row of
  1224   1176   ** results. This opcode causes the sqlite3_step() call to terminate
  1225   1177   ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
  1226   1178   ** structure to provide access to the top P1 values as the result
  1227   1179   ** row.  When the sqlite3_step() function is run again, the top P1
................................................................................
  1984   1936   ** with its complement.  If the value in register P1 is NULL its value
  1985   1937   ** is unchanged.
  1986   1938   */
  1987   1939   case OP_Not: {                /* same as TK_NOT, no-push, in1 */
  1988   1940     nPop = 0;
  1989   1941     if( pIn1->flags & MEM_Null ) break;  /* Do nothing to NULLs */
  1990   1942     sqlite3VdbeMemIntegerify(pIn1);
  1991         -  assert( (pIn1->flags & MEM_Dyn)==0 );
  1992   1943     pIn1->u.i = !pIn1->u.i;
  1993         -  pIn1->flags = MEM_Int;
         1944  +  assert( pIn1->flags==MEM_Int );
  1994   1945     break;
  1995   1946   }
  1996   1947   
  1997   1948   /* Opcode: BitNot P1 * * * *
  1998   1949   **
  1999   1950   ** Interpret the content of register P1 as an integer.  Replace it
  2000   1951   ** with its ones-complement.  If the value is originally NULL, leave
  2001   1952   ** it unchanged.
  2002   1953   */
  2003   1954   case OP_BitNot: {             /* same as TK_BITNOT, no-push, in1 */
  2004   1955     nPop = 0;
  2005   1956     if( pIn1->flags & MEM_Null ) break;  /* Do nothing to NULLs */
  2006   1957     sqlite3VdbeMemIntegerify(pIn1);
  2007         -  assert( (pIn1->flags & MEM_Dyn)==0 );
  2008   1958     pIn1->u.i = ~pIn1->u.i;
  2009         -  pIn1->flags = MEM_Int;
         1959  +  assert( pIn1->flags==MEM_Int );
  2010   1960     break;
  2011   1961   }
  2012   1962   
  2013   1963   /* Opcode: Noop * * * * *
  2014   1964   **
  2015   1965   ** Do nothing.  This instruction is often useful as a jump
  2016   1966   ** destination.
................................................................................
  2074   2024     }
  2075   2025     if( pOp->p1>0 ){
  2076   2026       popStack(&pTos, pOp->p1);
  2077   2027     }
  2078   2028     break;
  2079   2029   }
  2080   2030   
  2081         -/* Opcode: IsNull P1 P2 * * *
         2031  +/* Opcode: IsNull P1 P2 P3 * *
  2082   2032   **
  2083         -** Jump to P2 if the value in register P1 is NULL.
         2033  +** Jump to P2 if the value in register P1 is NULL.  If P3 is greater
         2034  +** than zero, then check all values reg(P1), reg(P1+1), 
         2035  +** reg(P1+2), ..., reg(P1+P3-1).
  2084   2036   **
  2085   2037   ** If P1 is 0 then use the top of the stack instead of a register
  2086   2038   ** and pop the stack regardless of whether or not the jump is taken.
  2087   2039   */
  2088   2040   case OP_IsNull: {            /* same as TK_ISNULL, no-push, jump, in1 */
  2089         -  if( (pIn1->flags & MEM_Null)!=0 ){
  2090         -    pc = pOp->p2 - 1;
  2091         -  }
         2041  +  int n = pOp->p3;
         2042  +  assert( pOp->p3==0 || pOp->p1>0 );
         2043  +  do{
         2044  +    if( (pIn1->flags & MEM_Null)!=0 ){
         2045  +      pc = pOp->p2 - 1;
         2046  +      break;
         2047  +    }
         2048  +    pIn1++;
         2049  +  }while( --n > 0 );
  2092   2050     break;
  2093   2051   }
  2094   2052   
  2095   2053   /* Opcode: NotNull P1 P2 * * *
  2096   2054   **
  2097   2055   ** Jump to P2 if the value in register P1 is not NULL.  
  2098   2056   **
................................................................................
  2405   2363   ** field of the index key (i.e. the first character of P4 corresponds to the
  2406   2364   ** lowest element on the stack).
  2407   2365   **
  2408   2366   ** The mapping from character to affinity is given by the SQLITE_AFF_
  2409   2367   ** macros defined in sqliteInt.h.
  2410   2368   **
  2411   2369   ** If P4 is NULL then all index fields have the affinity NONE.
  2412         -**
  2413         -** See also OP_MakeIdxRec
  2414         -*/
  2415         -/* Opcode: MakeIdxRec P1 P2 P4
  2416         -**
  2417         -** This opcode works just OP_MakeRecord except that it reads an extra
  2418         -** integer from the stack (thus reading a total of abs(P1+1) entries)
  2419         -** and appends that extra integer to the end of the record as a varint.
  2420         -** This results in an index key.
  2421   2370   */
  2422   2371   /*
  2423   2372   ** Opcode: RegMakeRec P1 P2 P3 P4 *
  2424   2373   **
  2425   2374   ** Builds a record like OP_MakeRecord.  But the data is taken from
  2426   2375   ** P2 registers beginning with P1:  P1, P1+1, P1+2, ..., P1+P2-1.
  2427   2376   ** The result is written into P3 or pushed onto the stack if P3 is zero.
  2428   2377   ** There is no jump on NULL - that can be done with a separate
  2429   2378   ** OP_AnyNull opcode.
  2430   2379   */
  2431         -/*
  2432         -** Opcode: RegMakeIRec P1 P2 P4
  2433         -**
  2434         -** Works like OP_MakeIdxRec except data is taken from registers
  2435         -** rather than from the stack.  The P1 register is an integer which
  2436         -** is the number of register to use in building the new record.
  2437         -** Data is taken from P1+1, P1+2, ..., P1+mem[P1].
  2438         -*/
  2439   2380   case OP_RegMakeRec:
  2440         -case OP_RegMakeIRec:
  2441         -case OP_MakeIdxRec:          /* jump */
  2442   2381   case OP_MakeRecord: {        /* jump */
  2443   2382     /* Assuming the record contains N fields, the record format looks
  2444   2383     ** like this:
  2445   2384     **
  2446   2385     ** ------------------------------------------------------------------------
  2447   2386     ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | 
  2448   2387     ** ------------------------------------------------------------------------
................................................................................
  2453   2392     ** Each type field is a varint representing the serial type of the 
  2454   2393     ** corresponding data element (see sqlite3VdbeSerialType()). The
  2455   2394     ** hdr-size field is also a varint which is the offset from the beginning
  2456   2395     ** of the record to data0.
  2457   2396     */
  2458   2397     u8 *zNewRecord;        /* A buffer to hold the data for the new record */
  2459   2398     Mem *pRec;             /* The new record */
  2460         -  Mem *pRowid = 0;       /* Rowid appended to the new record */
  2461   2399     u64 nData = 0;         /* Number of bytes of data space */
  2462   2400     int nHdr = 0;          /* Number of bytes of header space */
  2463   2401     u64 nByte = 0;         /* Data space required for this record */
  2464   2402     int nZero = 0;         /* Number of zero bytes at the end of the record */
  2465   2403     int nVarint;           /* Number of bytes in a varint */
  2466   2404     u32 serial_type;       /* Type field */
  2467   2405     int containsNull = 0;  /* True if any of the data fields are NULL */
  2468   2406     Mem *pData0;           /* Bottom of the stack */
  2469   2407     Mem *pLast;            /* Top of the stack */
  2470   2408     int leaveOnStack;      /* If true, leave the entries on the stack */
  2471   2409     int nField;            /* Number of fields in the record */
  2472   2410     int jumpIfNull;        /* Jump here if non-zero and any entries are NULL. */
  2473         -  int addRowid;          /* True to append a rowid column at the end */
  2474   2411     char *zAffinity;       /* The affinity string for the record */
  2475   2412     int file_format;       /* File format to use for encoding */
  2476   2413     int i;                 /* Space used in zNewRecord[] */
  2477   2414     char zTemp[NBFS];      /* Space to hold small records */
  2478   2415   
  2479   2416     if( pOp->p1<0 ){
  2480         -    assert( pOp->opcode==OP_MakeRecord || pOp->opcode==OP_MakeIdxRec );
         2417  +    assert( pOp->opcode==OP_MakeRecord );
  2481   2418       leaveOnStack = 1;
  2482   2419       nField = -pOp->p1;
  2483   2420     }else{
  2484   2421       leaveOnStack = 0;
  2485   2422       nField = pOp->p1;
  2486   2423     }
  2487         -  addRowid = pOp->opcode==OP_MakeIdxRec || pOp->opcode==OP_RegMakeIRec;
  2488   2424     zAffinity = pOp->p4.z;
  2489   2425   
  2490         -  if( pOp->opcode==OP_RegMakeRec || pOp->opcode==OP_RegMakeIRec ){
         2426  +  if( pOp->opcode==OP_RegMakeRec ){
  2491   2427       assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem );
  2492   2428       pData0 = &p->aMem[nField];
  2493   2429       nField = pOp->p2;
  2494   2430       leaveOnStack = 1;
  2495   2431       jumpIfNull = 0;
  2496   2432       pLast = &pData0[nField-1];
  2497   2433     }else{
................................................................................
  2526   2462         ** We do not allow blobs with a prefix and a zero-filled tail. */
  2527   2463         nZero += pRec->u.i;
  2528   2464       }else if( len ){
  2529   2465         nZero = 0;
  2530   2466       }
  2531   2467     }
  2532   2468   
  2533         -  /* If we have to append a varint rowid to this record, set pRowid
  2534         -  ** to the value of the rowid and increase nByte by the amount of space
  2535         -  ** required to store it.
  2536         -  */
  2537         -  if( addRowid ){
  2538         -    pRowid = &pData0[-1];
  2539         -    assert( pRowid>=p->aStack );
  2540         -    sqlite3VdbeMemIntegerify(pRowid);
  2541         -    serial_type = sqlite3VdbeSerialType(pRowid, 0);
  2542         -    nData += sqlite3VdbeSerialTypeLen(serial_type);
  2543         -    nHdr += sqlite3VarintLen(serial_type);
  2544         -    nZero = 0;
  2545         -  }
  2546         -
  2547   2469     /* Add the initial header varint and total the size */
  2548   2470     nHdr += nVarint = sqlite3VarintLen(nHdr);
  2549   2471     if( nVarint<sqlite3VarintLen(nHdr) ){
  2550   2472       nHdr++;
  2551   2473     }
  2552   2474     nByte = nHdr+nData-nZero;
  2553   2475     if( nByte>SQLITE_MAX_LENGTH ){
................................................................................
  2566   2488   
  2567   2489     /* Write the record */
  2568   2490     i = sqlite3PutVarint(zNewRecord, nHdr);
  2569   2491     for(pRec=pData0; pRec<=pLast; pRec++){
  2570   2492       serial_type = sqlite3VdbeSerialType(pRec, file_format);
  2571   2493       i += sqlite3PutVarint(&zNewRecord[i], serial_type);      /* serial type */
  2572   2494     }
  2573         -  if( addRowid ){
  2574         -    i += sqlite3PutVarint(&zNewRecord[i], sqlite3VdbeSerialType(pRowid, 0));
  2575         -  }
  2576   2495     for(pRec=pData0; pRec<=pLast; pRec++){  /* serial data */
  2577   2496       i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRec, file_format);
  2578   2497     }
  2579         -  if( addRowid ){
  2580         -    i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRowid, 0);
  2581         -  }
  2582   2498     assert( i==nByte );
  2583   2499   
  2584   2500     /* Pop entries off the stack if required. Push the new record on. */
  2585   2501     if( !leaveOnStack ){
  2586         -    popStack(&pTos, nField+addRowid);
         2502  +    popStack(&pTos, nField);
  2587   2503     }
  2588   2504     if( pOp->p3==0 ){
  2589   2505       pOut = ++pTos;
  2590   2506     }else{
  2591   2507       pOut = &p->aMem[pOp->p3];
  2592   2508       Release(pOut);
  2593   2509     }
................................................................................
  3430   3346   
  3431   3347       /* The final varint of the key is different from R.  Push it onto
  3432   3348       ** the stack.  (The record number of an entry that violates a UNIQUE
  3433   3349       ** constraint.)
  3434   3350       */
  3435   3351       nPop = 0;
  3436   3352       pIn3->u.i = v;
  3437         -    pIn3->flags = MEM_Int;
         3353  +    assert( pIn3->flags==MEM_Int );
  3438   3354     }
  3439   3355     break;
  3440   3356   }
  3441   3357   
  3442   3358   /* Opcode: NotExists P1 P2 P3
  3443   3359   **
  3444   3360   ** Use the top of the stack as a integer key. Or, if P3 is non-zero,

Changes to src/vdbeaux.c.

   391    391   }
   392    392   
   393    393   /*
   394    394   ** Change the value of the P2 operand for a specific instruction.
   395    395   ** This routine is useful for setting a jump destination.
   396    396   */
   397    397   void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
   398         -  assert( val>=0 );
   399    398     assert( p==0 || p->magic==VDBE_MAGIC_INIT );
   400    399     if( p && addr>=0 && p->nOp>addr && p->aOp ){
   401    400       p->aOp[addr].p2 = val;
   402    401     }
   403    402   }
   404    403   
   405    404   /*

Changes to src/vdbeblob.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains code used to implement incremental BLOB I/O.
    14     14   **
    15         -** $Id: vdbeblob.c,v 1.17 2008/01/03 07:54:24 danielk1977 Exp $
           15  +** $Id: vdbeblob.c,v 1.18 2008/01/10 23:50:11 drh Exp $
    16     16   */
    17     17   
    18     18   #include "sqliteInt.h"
    19     19   #include "vdbeInt.h"
    20     20   
    21     21   #ifndef SQLITE_OMIT_INCRBLOB
    22     22   
................................................................................
    50     50   
    51     51     /* This VDBE program seeks a btree cursor to the identified 
    52     52     ** db/table/row entry. The reason for using a vdbe program instead
    53     53     ** of writing code to use the b-tree layer directly is that the
    54     54     ** vdbe program will take advantage of the various transaction,
    55     55     ** locking and error handling infrastructure built into the vdbe.
    56     56     **
    57         -  ** After seeking the cursor, the vdbe executes an OP_Callback.
           57  +  ** After seeking the cursor, the vdbe executes an OP_ResultRow.
    58     58     ** Code external to the Vdbe then "borrows" the b-tree cursor and
    59     59     ** uses it to implement the blob_read(), blob_write() and 
    60     60     ** blob_bytes() functions.
    61     61     **
    62     62     ** The sqlite3_blob_close() function finalizes the vdbe program,
    63     63     ** which closes the b-tree cursor and (possibly) commits the 
    64     64     ** transaction.
................................................................................
    70     70       /* One of the following two instructions is replaced by an
    71     71       ** OP_Noop before exection.
    72     72       */
    73     73       {OP_OpenRead, 0, 0, 0},        /* 2: Open cursor 0 for reading */
    74     74       {OP_OpenWrite, 0, 0, 0},       /* 3: Open cursor 0 for read/write */
    75     75       {OP_SetNumColumns, 0, 0, 0},   /* 4: Num cols for cursor */
    76     76   
    77         -    {OP_Variable, 1, 0, 0},        /* 5: Push the rowid to the stack */
    78         -    {OP_NotExists, 0, 10, 0},      /* 6: Seek the cursor */
    79         -    {OP_Column, 0, 0, 0},          /* 7  */
    80         -    {OP_Callback, 0, 0, 0},        /* 8  */
           77  +    {OP_Variable, 1, 1, 0},        /* 5: Push the rowid to the stack */
           78  +    {OP_NotExists, 0, 10, 1},      /* 6: Seek the cursor */
           79  +    {OP_Column, 0, 0, 1},          /* 7  */
           80  +    {OP_ResultRow, 1, 0, 0},       /* 8  */
    81     81       {OP_Close, 0, 0, 0},           /* 9  */
    82     82       {OP_Halt, 0, 0, 0},            /* 10 */
    83     83     };
    84     84   
    85     85     Vdbe *v = 0;
    86     86     int rc = SQLITE_OK;
    87     87     char zErr[128];
................................................................................
   177    177         ** does. An OP_Column to retrieve this imaginary column will
   178    178         ** always return an SQL NULL. This is useful because it means
   179    179         ** we can invoke OP_Column to fill in the vdbe cursors type 
   180    180         ** and offset cache without causing any IO.
   181    181         */
   182    182         sqlite3VdbeChangeP2(v, 4, pTab->nCol+1);
   183    183         if( !db->mallocFailed ){
   184         -        sqlite3VdbeMakeReady(v, 1, 0, 1, 0);
          184  +        sqlite3VdbeMakeReady(v, 1, 1, 1, 0);
   185    185         }
   186    186       }
   187    187      
   188    188       sqlite3BtreeLeaveAll(db);
   189    189       rc = sqlite3SafetyOff(db);
   190    190       if( rc!=SQLITE_OK || db->mallocFailed ){
   191    191         goto blob_open_out;