/ Check-in [567dd843]
Login

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

Overview
Comment:Merge the latest trunk changes into the fts3-prefix-search branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | fts3-prefix-search
Files: files | file ages | folders
SHA1: 567dd84359218245d4e6887547e2a48881f2c8e0
User & Date: drh 2011-06-06 18:14:25
Context
2011-06-07
18:35
Have NEAR queries use incremental merging. Fix issues surrounding the deferred token optimization. check-in: 9d10a684 user: dan tags: fts3-prefix-search
2011-06-06
18:14
Merge the latest trunk changes into the fts3-prefix-search branch. check-in: 567dd843 user: drh tags: fts3-prefix-search
14:51
Modify fts3rnd.test to run tests for both "ORDER BY docid ASC" and "ORDER BY docid DESC" with both order=ASC and order=DESC FTS tables. Fixes for some bugs found. check-in: 89f2f482 user: dan tags: fts3-prefix-search
13:38
Add assert() statements to verify that u16 pointers associated with the enhancement in [897f56a158] are always 2-byte aligned. check-in: 98ccfa93 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/fts3/fts3.c.

  3045   3045       p->zDb, p->zName, zName
  3046   3046     );
  3047   3047     return rc;
  3048   3048   }
  3049   3049   
  3050   3050   static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
  3051   3051     Fts3Table *p = (Fts3Table*)pVtab;
         3052  +  UNUSED_PARAMETER(iSavepoint);
  3052   3053     assert( p->inTransaction );
  3053   3054     assert( p->mxSavepoint < iSavepoint );
  3054   3055     TESTONLY( p->mxSavepoint = iSavepoint );
  3055   3056     return fts3SyncMethod(pVtab);
  3056   3057   }
  3057   3058   static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
  3058   3059     TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
         3060  +  UNUSED_PARAMETER(iSavepoint);
         3061  +  UNUSED_PARAMETER(pVtab);
  3059   3062     assert( p->inTransaction );
  3060   3063     assert( p->mxSavepoint >= iSavepoint );
  3061   3064     TESTONLY( p->mxSavepoint = iSavepoint-1 );
  3062   3065     return SQLITE_OK;
  3063   3066   }
  3064   3067   static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
  3065   3068     Fts3Table *p = (Fts3Table*)pVtab;
         3069  +  UNUSED_PARAMETER(iSavepoint);
  3066   3070     assert( p->inTransaction );
  3067   3071     assert( p->mxSavepoint >= iSavepoint );
  3068   3072     TESTONLY( p->mxSavepoint = iSavepoint );
  3069   3073     sqlite3Fts3PendingTermsClear(p);
  3070   3074     return SQLITE_OK;
  3071   3075   }
  3072   3076   

Changes to ext/fts3/fts3_aux.c.

   339    339   ){
   340    340     Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
   341    341     Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
   342    342     int rc;
   343    343     int isScan;
   344    344   
   345    345     UNUSED_PARAMETER(nVal);
          346  +  UNUSED_PARAMETER(idxStr);
   346    347   
   347    348     assert( idxStr==0 );
   348    349     assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
   349    350          || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
   350    351          || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
   351    352     );
   352    353     isScan = (idxNum!=FTS4AUX_EQ_CONSTRAINT);
................................................................................
   456    457        fts3auxRowidMethod,          /* xRowid        */
   457    458        0,                           /* xUpdate       */
   458    459        0,                           /* xBegin        */
   459    460        0,                           /* xSync         */
   460    461        0,                           /* xCommit       */
   461    462        0,                           /* xRollback     */
   462    463        0,                           /* xFindFunction */
   463         -     0                            /* xRename       */
          464  +     0,                           /* xRename       */
          465  +     0,                           /* xSavepoint    */
          466  +     0,                           /* xRelease      */
          467  +     0                            /* xRollbackTo   */
   464    468     };
   465    469     int rc;                         /* Return code */
   466    470   
   467    471     rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
   468    472     return rc;
   469    473   }
   470    474   
   471    475   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */

Changes to ext/rtree/rtree.c.

  2845   2845       rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
  2846   2846       sqlite3_free(zSql);
  2847   2847     }
  2848   2848     return rc;
  2849   2849   }
  2850   2850   
  2851   2851   static sqlite3_module rtreeModule = {
  2852         -  0,                         /* iVersion */
         2852  +  0,                          /* iVersion */
  2853   2853     rtreeCreate,                /* xCreate - create a table */
  2854   2854     rtreeConnect,               /* xConnect - connect to an existing table */
  2855   2855     rtreeBestIndex,             /* xBestIndex - Determine search strategy */
  2856   2856     rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
  2857   2857     rtreeDestroy,               /* xDestroy - Drop a table */
  2858   2858     rtreeOpen,                  /* xOpen - open a cursor */
  2859   2859     rtreeClose,                 /* xClose - close a cursor */
................................................................................
  2864   2864     rtreeRowid,                 /* xRowid - read data */
  2865   2865     rtreeUpdate,                /* xUpdate - write data */
  2866   2866     0,                          /* xBegin - begin transaction */
  2867   2867     0,                          /* xSync - sync transaction */
  2868   2868     0,                          /* xCommit - commit transaction */
  2869   2869     0,                          /* xRollback - rollback transaction */
  2870   2870     0,                          /* xFindFunction - function overloading */
  2871         -  rtreeRename                 /* xRename - rename the table */
         2871  +  rtreeRename,                /* xRename - rename the table */
         2872  +  0,                          /* xSavepoint */
         2873  +  0,                          /* xRelease */
         2874  +  0                           /* xRollbackTo */
  2872   2875   };
  2873   2876   
  2874   2877   static int rtreeSqlInit(
  2875   2878     Rtree *pRtree, 
  2876   2879     sqlite3 *db, 
  2877   2880     const char *zDb, 
  2878   2881     const char *zPrefix, 

Changes to src/alter.c.

   354    354   
   355    355     /* Drop the table and index from the internal schema.  */
   356    356     sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
   357    357   
   358    358     /* Reload the table, index and permanent trigger schemas. */
   359    359     zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
   360    360     if( !zWhere ) return;
   361         -  sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
          361  +  sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
   362    362   
   363    363   #ifndef SQLITE_OMIT_TRIGGER
   364    364     /* Now, if the table is not stored in the temp database, reload any temp 
   365    365     ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
   366    366     */
   367    367     if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
   368         -    sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC);
          368  +    sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
   369    369     }
   370    370   #endif
   371    371   }
   372    372   
   373    373   /*
   374    374   ** Parameter zName is the name of a table that is about to be altered
   375    375   ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).

Changes to src/btree.c.

   853    853   ** the page, 1 means the second cell, and so forth) return a pointer
   854    854   ** to the cell content.
   855    855   **
   856    856   ** This routine works only for pages that do not contain overflow cells.
   857    857   */
   858    858   #define findCell(P,I) \
   859    859     ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)])))
          860  +#define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
          861  +
   860    862   
   861    863   /*
   862    864   ** This a more complex version of findCell() that works for
   863    865   ** pages that do contain overflow cells.
   864    866   */
   865    867   static u8 *findOverflowCell(MemPage *pPage, int iCell){
   866    868     int i;
................................................................................
  4447   4449     if( pCur->eState==CURSOR_INVALID ){
  4448   4450       *pRes = -1;
  4449   4451       assert( pCur->apPage[pCur->iPage]->nCell==0 );
  4450   4452       return SQLITE_OK;
  4451   4453     }
  4452   4454     assert( pCur->apPage[0]->intKey || pIdxKey );
  4453   4455     for(;;){
  4454         -    int lwr, upr;
         4456  +    int lwr, upr, idx;
  4455   4457       Pgno chldPg;
  4456   4458       MemPage *pPage = pCur->apPage[pCur->iPage];
  4457   4459       int c;
  4458   4460   
  4459   4461       /* pPage->nCell must be greater than zero. If this is the root-page
  4460   4462       ** the cursor would have been INVALID above and this for(;;) loop
  4461   4463       ** not run. If this is not the root-page, then the moveToChild() routine
................................................................................
  4463   4465       ** be the right kind (index or table) of b-tree page. Otherwise
  4464   4466       ** a moveToChild() or moveToRoot() call would have detected corruption.  */
  4465   4467       assert( pPage->nCell>0 );
  4466   4468       assert( pPage->intKey==(pIdxKey==0) );
  4467   4469       lwr = 0;
  4468   4470       upr = pPage->nCell-1;
  4469   4471       if( biasRight ){
  4470         -      pCur->aiIdx[pCur->iPage] = (u16)upr;
         4472  +      pCur->aiIdx[pCur->iPage] = (u16)(idx = upr);
  4471   4473       }else{
  4472         -      pCur->aiIdx[pCur->iPage] = (u16)((upr+lwr)/2);
         4474  +      pCur->aiIdx[pCur->iPage] = (u16)(idx = (upr+lwr)/2);
  4473   4475       }
  4474   4476       for(;;){
  4475         -      int idx = pCur->aiIdx[pCur->iPage]; /* Index of current cell in pPage */
  4476   4477         u8 *pCell;                          /* Pointer to current cell in pPage */
  4477   4478   
         4479  +      assert( idx==pCur->aiIdx[pCur->iPage] );
  4478   4480         pCur->info.nSize = 0;
  4479   4481         pCell = findCell(pPage, idx) + pPage->childPtrSize;
  4480   4482         if( pPage->intKey ){
  4481   4483           i64 nCellKey;
  4482   4484           if( pPage->hasData ){
  4483   4485             u32 dummy;
  4484   4486             pCell += getVarint32(pCell, dummy);
................................................................................
  4553   4555           lwr = idx+1;
  4554   4556         }else{
  4555   4557           upr = idx-1;
  4556   4558         }
  4557   4559         if( lwr>upr ){
  4558   4560           break;
  4559   4561         }
  4560         -      pCur->aiIdx[pCur->iPage] = (u16)((lwr+upr)/2);
         4562  +      pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
  4561   4563       }
  4562   4564       assert( lwr==upr+1 );
  4563   4565       assert( pPage->isInit );
  4564   4566       if( pPage->leaf ){
  4565   4567         chldPg = 0;
  4566   4568       }else if( lwr>=pPage->nCell ){
  4567   4569         chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
................................................................................
  5386   5388   ** The cell content is not freed or deallocated.  It is assumed that
  5387   5389   ** the cell content has been copied someplace else.  This routine just
  5388   5390   ** removes the reference to the cell from pPage.
  5389   5391   **
  5390   5392   ** "sz" must be the number of bytes in the cell.
  5391   5393   */
  5392   5394   static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
  5393         -  int i;          /* Loop counter */
  5394   5395     u32 pc;         /* Offset to cell content of cell being deleted */
  5395   5396     u8 *data;       /* pPage->aData */
  5396   5397     u8 *ptr;        /* Used to move bytes around within data[] */
         5398  +  u8 *endPtr;     /* End of loop */
  5397   5399     int rc;         /* The return code */
  5398   5400     int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
  5399   5401   
  5400   5402     if( *pRC ) return;
  5401   5403   
  5402   5404     assert( idx>=0 && idx<pPage->nCell );
  5403   5405     assert( sz==cellSize(pPage, idx) );
................................................................................
  5414   5416       return;
  5415   5417     }
  5416   5418     rc = freeSpace(pPage, pc, sz);
  5417   5419     if( rc ){
  5418   5420       *pRC = rc;
  5419   5421       return;
  5420   5422     }
  5421         -  for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
  5422         -    ptr[0] = ptr[2];
  5423         -    ptr[1] = ptr[3];
         5423  +  endPtr = &data[pPage->cellOffset + 2*pPage->nCell - 2];
         5424  +  assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
         5425  +  while( ptr<endPtr ){
         5426  +    *(u16*)ptr = *(u16*)&ptr[2];
         5427  +    ptr += 2;
  5424   5428     }
  5425   5429     pPage->nCell--;
  5426   5430     put2byte(&data[hdr+3], pPage->nCell);
  5427   5431     pPage->nFree += 2;
  5428   5432   }
  5429   5433   
  5430   5434   /*
................................................................................
  5456   5460     int idx = 0;      /* Where to write new cell content in data[] */
  5457   5461     int j;            /* Loop counter */
  5458   5462     int end;          /* First byte past the last cell pointer in data[] */
  5459   5463     int ins;          /* Index in data[] where new cell pointer is inserted */
  5460   5464     int cellOffset;   /* Address of first cell pointer in data[] */
  5461   5465     u8 *data;         /* The content of the whole page */
  5462   5466     u8 *ptr;          /* Used for moving information around in data[] */
         5467  +  u8 *endPtr;       /* End of the loop */
  5463   5468   
  5464   5469     int nSkip = (iChild ? 4 : 0);
  5465   5470   
  5466   5471     if( *pRC ) return;
  5467   5472   
  5468   5473     assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
  5469   5474     assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
................................................................................
  5506   5511       assert( idx+sz <= (int)pPage->pBt->usableSize );
  5507   5512       pPage->nCell++;
  5508   5513       pPage->nFree -= (u16)(2 + sz);
  5509   5514       memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
  5510   5515       if( iChild ){
  5511   5516         put4byte(&data[idx], iChild);
  5512   5517       }
  5513         -    for(j=end, ptr=&data[j]; j>ins; j-=2, ptr-=2){
  5514         -      ptr[0] = ptr[-2];
  5515         -      ptr[1] = ptr[-1];
         5518  +    ptr = &data[end];
         5519  +    endPtr = &data[ins];
         5520  +    assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
         5521  +    while( ptr>endPtr ){
         5522  +      *(u16*)ptr = *(u16*)&ptr[-2];
         5523  +      ptr -= 2;
  5516   5524       }
  5517   5525       put2byte(&data[ins], idx);
  5518   5526       put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
  5519   5527   #ifndef SQLITE_OMIT_AUTOVACUUM
  5520   5528       if( pPage->pBt->autoVacuum ){
  5521   5529         /* The cell may contain a pointer to an overflow page. If so, write
  5522   5530         ** the entry for the overflow page into the pointer map.
................................................................................
  5553   5561     /* Check that the page has just been zeroed by zeroPage() */
  5554   5562     assert( pPage->nCell==0 );
  5555   5563     assert( get2byteNotZero(&data[hdr+5])==nUsable );
  5556   5564   
  5557   5565     pCellptr = &data[pPage->cellOffset + nCell*2];
  5558   5566     cellbody = nUsable;
  5559   5567     for(i=nCell-1; i>=0; i--){
         5568  +    u16 sz = aSize[i];
  5560   5569       pCellptr -= 2;
  5561         -    cellbody -= aSize[i];
         5570  +    cellbody -= sz;
  5562   5571       put2byte(pCellptr, cellbody);
  5563         -    memcpy(&data[cellbody], apCell[i], aSize[i]);
         5572  +    memcpy(&data[cellbody], apCell[i], sz);
  5564   5573     }
  5565   5574     put2byte(&data[hdr+3], nCell);
  5566   5575     put2byte(&data[hdr+5], cellbody);
  5567   5576     pPage->nFree -= (nCell*2 + nUsable - cellbody);
  5568   5577     pPage->nCell = (u16)nCell;
  5569   5578   }
  5570   5579   
................................................................................
  6010   6019       ** process of being overwritten.  */
  6011   6020       MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
  6012   6021       memcpy(pOld, apOld[i], sizeof(MemPage));
  6013   6022       pOld->aData = (void*)&pOld[1];
  6014   6023       memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
  6015   6024   
  6016   6025       limit = pOld->nCell+pOld->nOverflow;
  6017         -    for(j=0; j<limit; j++){
  6018         -      assert( nCell<nMaxCells );
  6019         -      apCell[nCell] = findOverflowCell(pOld, j);
  6020         -      szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
  6021         -      nCell++;
  6022         -    }
         6026  +    if( pOld->nOverflow>0 ){
         6027  +      for(j=0; j<limit; j++){
         6028  +        assert( nCell<nMaxCells );
         6029  +        apCell[nCell] = findOverflowCell(pOld, j);
         6030  +        szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
         6031  +        nCell++;
         6032  +      }
         6033  +    }else{
         6034  +      u8 *aData = pOld->aData;
         6035  +      u16 maskPage = pOld->maskPage;
         6036  +      u16 cellOffset = pOld->cellOffset;
         6037  +      for(j=0; j<limit; j++){
         6038  +        assert( nCell<nMaxCells );
         6039  +        apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
         6040  +        szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
         6041  +        nCell++;
         6042  +      }
         6043  +    }       
  6023   6044       if( i<nOld-1 && !leafData){
  6024   6045         u16 sz = (u16)szNew[i];
  6025   6046         u8 *pTemp;
  6026   6047         assert( nCell<nMaxCells );
  6027   6048         szCell[nCell] = sz;
  6028   6049         pTemp = &aSpace1[iSpace1];
  6029   6050         iSpace1 += sz;

Changes to src/build.c.

   196    196       FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
   197    197       sqlite3VdbeTrace(v, trace);
   198    198   #endif
   199    199       assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
   200    200       /* A minimum of one cursor is required if autoincrement is used
   201    201       *  See ticket [a696379c1f08866] */
   202    202       if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
   203         -    sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem,
   204         -                         pParse->nTab, pParse->nMaxArg, pParse->explain,
   205         -                         pParse->isMultiWrite && pParse->mayAbort);
          203  +    sqlite3VdbeMakeReady(v, pParse);
   206    204       pParse->rc = SQLITE_DONE;
   207    205       pParse->colNamesSet = 0;
   208    206     }else{
   209    207       pParse->rc = SQLITE_ERROR;
   210    208     }
   211    209     pParse->nTab = 0;
   212    210     pParse->nMem = 0;
................................................................................
  1617   1615             pDb->zName
  1618   1616           );
  1619   1617         }
  1620   1618       }
  1621   1619   #endif
  1622   1620   
  1623   1621       /* Reparse everything to update our internal data structures */
  1624         -    sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0,
  1625         -        sqlite3MPrintf(db, "tbl_name='%q'",p->zName), P4_DYNAMIC);
         1622  +    sqlite3VdbeAddParseSchemaOp(v, iDb,
         1623  +               sqlite3MPrintf(db, "tbl_name='%q'", p->zName));
  1626   1624     }
  1627   1625   
  1628   1626   
  1629   1627     /* Add the table to the in-memory representation of the database.
  1630   1628     */
  1631   1629     if( db->init.busy ){
  1632   1630       Table *pOld;
................................................................................
  2815   2813   
  2816   2814       /* Fill the index with data and reparse the schema. Code an OP_Expire
  2817   2815       ** to invalidate all pre-compiled statements.
  2818   2816       */
  2819   2817       if( pTblName ){
  2820   2818         sqlite3RefillIndex(pParse, pIndex, iMem);
  2821   2819         sqlite3ChangeCookie(pParse, iDb);
  2822         -      sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0,
  2823         -         sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 
  2824         -         P4_DYNAMIC);
         2820  +      sqlite3VdbeAddParseSchemaOp(v, iDb,
         2821  +         sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
  2825   2822         sqlite3VdbeAddOp1(v, OP_Expire, 0);
  2826   2823       }
  2827   2824     }
  2828   2825   
  2829   2826     /* When adding an index to the list of indices for a table, make
  2830   2827     ** sure all indices labeled OE_Replace come after all those labeled
  2831   2828     ** OE_Ignore.  This is necessary for the correct constraint check

Changes to src/delete.c.

   397    397   
   398    398       /* Delete the row */
   399    399   #ifndef SQLITE_OMIT_VIRTUALTABLE
   400    400       if( IsVirtual(pTab) ){
   401    401         const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
   402    402         sqlite3VtabMakeWritable(pParse, pTab);
   403    403         sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
          404  +      sqlite3VdbeChangeP5(v, OE_Abort);
   404    405         sqlite3MayAbort(pParse);
   405    406       }else
   406    407   #endif
   407    408       {
   408    409         int count = (pParse->nested==0);    /* True to count changes */
   409    410         sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default);
   410    411       }

Changes to src/expr.c.

   551    551     z = pExpr->u.zToken;
   552    552     assert( z!=0 );
   553    553     assert( z[0]!=0 );
   554    554     if( z[1]==0 ){
   555    555       /* Wildcard of the form "?".  Assign the next variable number */
   556    556       assert( z[0]=='?' );
   557    557       pExpr->iColumn = (ynVar)(++pParse->nVar);
   558         -  }else if( z[0]=='?' ){
   559         -    /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
   560         -    ** use it as the variable number */
   561         -    i64 i;
   562         -    int bOk = 0==sqlite3Atoi64(&z[1], &i, sqlite3Strlen30(&z[1]), SQLITE_UTF8);
   563         -    pExpr->iColumn = (ynVar)i;
   564         -    testcase( i==0 );
   565         -    testcase( i==1 );
   566         -    testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
   567         -    testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
   568         -    if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
   569         -      sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
   570         -          db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
   571         -    }
   572         -    if( i>pParse->nVar ){
   573         -      pParse->nVar = (int)i;
   574         -    }
   575         -  }else{
   576         -    /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
   577         -    ** number as the prior appearance of the same name, or if the name
   578         -    ** has never appeared before, reuse the same variable number
   579         -    */
   580         -    int i;
   581         -    u32 n;
   582         -    n = sqlite3Strlen30(z);
   583         -    for(i=0; i<pParse->nVarExpr; i++){
   584         -      Expr *pE = pParse->apVarExpr[i];
   585         -      assert( pE!=0 );
   586         -      if( memcmp(pE->u.zToken, z, n)==0 && pE->u.zToken[n]==0 ){
   587         -        pExpr->iColumn = pE->iColumn;
   588         -        break;
   589         -      }
   590         -    }
   591         -    if( i>=pParse->nVarExpr ){
   592         -      pExpr->iColumn = (ynVar)(++pParse->nVar);
   593         -      if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
   594         -        pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
   595         -        pParse->apVarExpr =
   596         -            sqlite3DbReallocOrFree(
   597         -              db,
   598         -              pParse->apVarExpr,
   599         -              pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
   600         -            );
   601         -      }
   602         -      if( !db->mallocFailed ){
   603         -        assert( pParse->apVarExpr!=0 );
   604         -        pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
          558  +  }else{
          559  +    ynVar x = 0;
          560  +    u32 n = sqlite3Strlen30(z);
          561  +    if( z[0]=='?' ){
          562  +      /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
          563  +      ** use it as the variable number */
          564  +      i64 i;
          565  +      int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
          566  +      pExpr->iColumn = x = (ynVar)i;
          567  +      testcase( i==0 );
          568  +      testcase( i==1 );
          569  +      testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
          570  +      testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
          571  +      if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
          572  +        sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
          573  +            db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
          574  +        x = 0;
          575  +      }
          576  +      if( i>pParse->nVar ){
          577  +        pParse->nVar = (int)i;
          578  +      }
          579  +    }else{
          580  +      /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
          581  +      ** number as the prior appearance of the same name, or if the name
          582  +      ** has never appeared before, reuse the same variable number
          583  +      */
          584  +      ynVar i;
          585  +      for(i=0; i<pParse->nzVar; i++){
          586  +        if( pParse->azVar[i] && memcmp(pParse->azVar[i],z,n+1)==0 ){
          587  +          pExpr->iColumn = x = (ynVar)i+1;
          588  +          break;
          589  +        }
          590  +      }
          591  +      if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
          592  +    }
          593  +    if( x>0 ){
          594  +      if( x>pParse->nzVar ){
          595  +        char **a;
          596  +        a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
          597  +        if( a==0 ) return;  /* Error reported through db->mallocFailed */
          598  +        pParse->azVar = a;
          599  +        memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
          600  +        pParse->nzVar = x;
          601  +      }
          602  +      if( z[0]!='?' || pParse->azVar[x-1]==0 ){
          603  +        sqlite3DbFree(db, pParse->azVar[x-1]);
          604  +        pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
   605    605         }
   606    606       }
   607    607     } 
   608    608     if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
   609    609       sqlite3ErrorMsg(pParse, "too many SQL variables");
   610    610     }
   611    611   }
................................................................................
  2341   2341   #endif
  2342   2342       case TK_VARIABLE: {
  2343   2343         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  2344   2344         assert( pExpr->u.zToken!=0 );
  2345   2345         assert( pExpr->u.zToken[0]!=0 );
  2346   2346         sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
  2347   2347         if( pExpr->u.zToken[1]!=0 ){
  2348         -        sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, P4_TRANSIENT);
         2348  +        assert( pExpr->u.zToken[0]=='?' 
         2349  +             || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
         2350  +        sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
  2349   2351         }
  2350   2352         break;
  2351   2353       }
  2352   2354       case TK_REGISTER: {
  2353   2355         inReg = pExpr->iTable;
  2354   2356         break;
  2355   2357       }

Changes to src/main.c.

  1934   1934         if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
  1935   1935           zVfs = zVal;
  1936   1936         }else{
  1937   1937           struct OpenMode {
  1938   1938             const char *z;
  1939   1939             int mode;
  1940   1940           } *aMode = 0;
  1941         -        char *zModeType;
  1942         -        int mask;
  1943         -        int limit;
         1941  +        char *zModeType = 0;
         1942  +        int mask = 0;
         1943  +        int limit = 0;
  1944   1944   
  1945   1945           if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
  1946   1946             static struct OpenMode aCacheMode[] = {
  1947   1947               { "shared",  SQLITE_OPEN_SHAREDCACHE },
  1948   1948               { "private", SQLITE_OPEN_PRIVATECACHE },
  1949   1949               { 0, 0 }
  1950   1950             };

Changes to src/os_unix.c.

  3533   3533   */
  3534   3534   struct unixShmNode {
  3535   3535     unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
  3536   3536     sqlite3_mutex *mutex;      /* Mutex to access this object */
  3537   3537     char *zFilename;           /* Name of the mmapped file */
  3538   3538     int h;                     /* Open file descriptor */
  3539   3539     int szRegion;              /* Size of shared-memory regions */
  3540         -  int nRegion;               /* Size of array apRegion */
         3540  +  u16 nRegion;               /* Size of array apRegion */
         3541  +  u8 isReadonly;             /* True if read-only */
  3541   3542     char **apRegion;           /* Array of mapped shared-memory regions */
  3542   3543     int nRef;                  /* Number of unixShm objects pointing to this */
  3543   3544     unixShm *pFirst;           /* All unixShm objects pointing to this */
  3544   3545   #ifdef SQLITE_DEBUG
  3545   3546     u8 exclMask;               /* Mask of exclusive locks held */
  3546   3547     u8 sharedMask;             /* Mask of shared locks held */
  3547   3548     u8 nextShmId;              /* Next available unixShm.id value */
................................................................................
  3780   3781         goto shm_open_err;
  3781   3782       }
  3782   3783   
  3783   3784       if( pInode->bProcessLock==0 ){
  3784   3785         pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT,
  3785   3786                                  (sStat.st_mode & 0777));
  3786   3787         if( pShmNode->h<0 ){
  3787         -        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
  3788         -        goto shm_open_err;
         3788  +        const char *zRO;
         3789  +        zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
         3790  +        if( zRO && sqlite3GetBoolean(zRO) ){
         3791  +          pShmNode->h = robust_open(zShmFilename, O_RDONLY,
         3792  +                                    (sStat.st_mode & 0777));
         3793  +          pShmNode->isReadonly = 1;
         3794  +        }
         3795  +        if( pShmNode->h<0 ){
         3796  +          rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
         3797  +          goto shm_open_err;
         3798  +        }
  3789   3799         }
  3790   3800     
  3791   3801         /* Check to see if another process is holding the dead-man switch.
  3792   3802         ** If not, truncate the file to zero length. 
  3793   3803         */
  3794   3804         rc = SQLITE_OK;
  3795   3805         if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
................................................................................
  3920   3930         rc = SQLITE_IOERR_NOMEM;
  3921   3931         goto shmpage_out;
  3922   3932       }
  3923   3933       pShmNode->apRegion = apNew;
  3924   3934       while(pShmNode->nRegion<=iRegion){
  3925   3935         void *pMem;
  3926   3936         if( pShmNode->h>=0 ){
  3927         -        pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, 
         3937  +        pMem = mmap(0, szRegion,
         3938  +            pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
  3928   3939               MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
  3929   3940           );
  3930   3941           if( pMem==MAP_FAILED ){
  3931   3942             rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
  3932   3943             goto shmpage_out;
  3933   3944           }
  3934   3945         }else{
................................................................................
  3946   3957   
  3947   3958   shmpage_out:
  3948   3959     if( pShmNode->nRegion>iRegion ){
  3949   3960       *pp = pShmNode->apRegion[iRegion];
  3950   3961     }else{
  3951   3962       *pp = 0;
  3952   3963     }
         3964  +  if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
  3953   3965     sqlite3_mutex_leave(pShmNode->mutex);
  3954   3966     return rc;
  3955   3967   }
  3956   3968   
  3957   3969   /*
  3958   3970   ** Change the lock state for a shared-memory segment.
  3959   3971   **

Changes to src/pcache.c.

   249    249       );
   250    250       pCache->pSynced = pPg;
   251    251       if( !pPg ){
   252    252         for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
   253    253       }
   254    254       if( pPg ){
   255    255         int rc;
          256  +#ifdef SQLITE_LOG_CACHE_SPILL
          257  +      sqlite3_log(SQLITE_FULL, 
          258  +                  "spill page %d making room for %d - cache used: %d/%d",
          259  +                  pPg->pgno, pgno,
          260  +                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
          261  +                  pCache->nMax);
          262  +#endif
   256    263         rc = pCache->xStress(pCache->pStress, pPg);
   257    264         if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
   258    265           return rc;
   259    266         }
   260    267       }
   261    268   
   262    269       pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);

Changes to src/pragma.c.

     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     14   #include "sqliteInt.h"
    15     15   
    16         -/* Ignore this whole file if pragmas are disabled
    17         -*/
    18         -#if !defined(SQLITE_OMIT_PRAGMA)
    19         -
    20     16   /*
    21     17   ** Interpret the given string as a safety level.  Return 0 for OFF,
    22     18   ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or 
    23     19   ** unrecognized string argument.
    24     20   **
    25     21   ** Note that the values returned are one less that the values that
    26     22   ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
................................................................................
    48     44   
    49     45   /*
    50     46   ** Interpret the given string as a boolean value.
    51     47   */
    52     48   u8 sqlite3GetBoolean(const char *z){
    53     49     return getSafetyLevel(z)&1;
    54     50   }
           51  +
           52  +/* The sqlite3GetBoolean() function is used by other modules but the
           53  +** remainder of this file is specific to PRAGMA processing.  So omit
           54  +** the rest of the file if PRAGMAs are omitted from the build.
           55  +*/
           56  +#if !defined(SQLITE_OMIT_PRAGMA)
    55     57   
    56     58   /*
    57     59   ** Interpret the given string as a locking mode value.
    58     60   */
    59     61   static int getLockingMode(const char *z){
    60     62     if( z ){
    61     63       if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;

Changes to src/shell.c.

  2298   2298       
  2299   2299     if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0
  2300   2300      && nArg==2
  2301   2301     ){
  2302   2302       enableTimer = booleanValue(azArg[1]);
  2303   2303     }else
  2304   2304     
         2305  +  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
         2306  +    printf("SQLite %s %s\n",
         2307  +        sqlite3_libversion(), sqlite3_sourceid());
         2308  +  }else
         2309  +
  2305   2310     if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
  2306   2311       int j;
  2307   2312       assert( nArg<=ArraySize(azArg) );
  2308   2313       for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
  2309   2314         p->colWidth[j-1] = atoi(azArg[j]);
  2310   2315       }
  2311   2316     }else
................................................................................
  2832   2837       }else if( strcmp(z,"-echo")==0 ){
  2833   2838         data.echoOn = 1;
  2834   2839       }else if( strcmp(z,"-stats")==0 ){
  2835   2840         data.statsOn = 1;
  2836   2841       }else if( strcmp(z,"-bail")==0 ){
  2837   2842         bail_on_error = 1;
  2838   2843       }else if( strcmp(z,"-version")==0 ){
  2839         -      printf("%s\n", sqlite3_libversion());
         2844  +      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
  2840   2845         return 0;
  2841   2846       }else if( strcmp(z,"-interactive")==0 ){
  2842   2847         stdin_is_interactive = 1;
  2843   2848       }else if( strcmp(z,"-batch")==0 ){
  2844   2849         stdin_is_interactive = 0;
  2845   2850       }else if( strcmp(z,"-heap")==0 ){
  2846   2851         i++;
................................................................................
  2877   2882       /* Run commands received from standard input
  2878   2883       */
  2879   2884       if( stdin_is_interactive ){
  2880   2885         char *zHome;
  2881   2886         char *zHistory = 0;
  2882   2887         int nHistory;
  2883   2888         printf(
  2884         -        "SQLite version %s\n"
         2889  +        "SQLite version %s %.19s\n"
  2885   2890           "Enter \".help\" for instructions\n"
  2886   2891           "Enter SQL statements terminated with a \";\"\n",
  2887         -        sqlite3_libversion()
         2892  +        sqlite3_libversion(), sqlite3_sourceid()
  2888   2893         );
  2889   2894         zHome = find_home_dir();
  2890   2895         if( zHome ){
  2891   2896           nHistory = strlen30(zHome) + 20;
  2892   2897           if( (zHistory = malloc(nHistory))!=0 ){
  2893   2898             sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
  2894   2899           }

Changes to src/sqlite.h.in.

   450    450   #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
   451    451   #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
   452    452   #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
   453    453   #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
   454    454   #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
   455    455   #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
   456    456   #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
          457  +#define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
          458  +#define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
   457    459   
   458    460   /*
   459    461   ** CAPI3REF: Flags For File Open Operations
   460    462   **
   461    463   ** These bit values are intended for use in the
   462    464   ** 3rd parameter to the [sqlite3_open_v2()] interface and
   463    465   ** in the 4th parameter to the [sqlite3_vfs.xOpen] method.

Changes to src/sqliteInt.h.

  2226   2226     u8 disableTriggers;  /* True to disable triggers */
  2227   2227     double nQueryLoop;   /* Estimated number of iterations of a query */
  2228   2228   
  2229   2229     /* Above is constant between recursions.  Below is reset before and after
  2230   2230     ** each recursion */
  2231   2231   
  2232   2232     int nVar;            /* Number of '?' variables seen in the SQL so far */
  2233         -  int nVarExpr;        /* Number of used slots in apVarExpr[] */
  2234         -  int nVarExprAlloc;   /* Number of allocated slots in apVarExpr[] */
  2235         -  Expr **apVarExpr;    /* Pointers to :aaa and $aaaa wildcard expressions */
         2233  +  int nzVar;           /* Number of available slots in azVar[] */
         2234  +  char **azVar;        /* Pointers to names of parameters */
  2236   2235     Vdbe *pReprepare;    /* VM being reprepared (sqlite3Reprepare()) */
  2237   2236     int nAlias;          /* Number of aliased result set columns */
  2238   2237     int nAliasAlloc;     /* Number of allocated slots for aAlias[] */
  2239   2238     int *aAlias;         /* Register used to hold aliased result */
  2240   2239     u8 explain;          /* True if the EXPLAIN flag is found on the query */
  2241   2240     Token sNameToken;    /* Token with unqualified schema object name */
  2242   2241     Token sLastToken;    /* The last token parsed */

Changes to src/test1.c.

   160    160       case SQLITE_IOERR_BLOCKED:       zName = "SQLITE_IOERR_BLOCKED";     break;
   161    161       case SQLITE_IOERR_NOMEM:         zName = "SQLITE_IOERR_NOMEM";       break;
   162    162       case SQLITE_IOERR_ACCESS:        zName = "SQLITE_IOERR_ACCESS";      break;
   163    163       case SQLITE_IOERR_CHECKRESERVEDLOCK:
   164    164                                  zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
   165    165       case SQLITE_IOERR_LOCK:          zName = "SQLITE_IOERR_LOCK";        break;
   166    166       case SQLITE_CORRUPT_VTAB:        zName = "SQLITE_CORRUPT_VTAB";      break;
   167         -                               zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
          167  +    case SQLITE_READONLY_RECOVERY:   zName = "SQLITE_READONLY_RECOVERY"; break;
          168  +    case SQLITE_READONLY_CANTLOCK:   zName = "SQLITE_READONLY_CANTLOCK"; break;
   168    169       default:                         zName = "SQLITE_Unknown";           break;
   169    170     }
   170    171     return zName;
   171    172   }
   172    173   #define t1ErrorName sqlite3TestErrorName
   173    174   
   174    175   /*

Changes to src/tokenize.c.

   349    349         return i;
   350    350       }
   351    351   #ifndef SQLITE_OMIT_BLOB_LITERAL
   352    352       case 'x': case 'X': {
   353    353         testcase( z[0]=='x' ); testcase( z[0]=='X' );
   354    354         if( z[1]=='\'' ){
   355    355           *tokenType = TK_BLOB;
   356         -        for(i=2; (c=z[i])!=0 && c!='\''; i++){
   357         -          if( !sqlite3Isxdigit(c) ){
   358         -            *tokenType = TK_ILLEGAL;
   359         -          }
          356  +        for(i=2; sqlite3Isxdigit(z[i]); i++){}
          357  +        if( z[i]!='\'' || i%2 ){
          358  +          *tokenType = TK_ILLEGAL;
          359  +          while( z[i] && z[i]!='\'' ){ i++; }
   360    360           }
   361         -        if( i%2 || !c ) *tokenType = TK_ILLEGAL;
   362         -        if( c ) i++;
          361  +        if( z[i] ) i++;
   363    362           return i;
   364    363         }
   365    364         /* Otherwise fall through to the next case */
   366    365       }
   367    366   #endif
   368    367       default: {
   369    368         if( !IdChar(*z) ){
................................................................................
   408    407     if( pEngine==0 ){
   409    408       db->mallocFailed = 1;
   410    409       return SQLITE_NOMEM;
   411    410     }
   412    411     assert( pParse->pNewTable==0 );
   413    412     assert( pParse->pNewTrigger==0 );
   414    413     assert( pParse->nVar==0 );
   415         -  assert( pParse->nVarExpr==0 );
   416         -  assert( pParse->nVarExprAlloc==0 );
   417         -  assert( pParse->apVarExpr==0 );
          414  +  assert( pParse->nzVar==0 );
          415  +  assert( pParse->azVar==0 );
   418    416     enableLookaside = db->lookaside.bEnabled;
   419    417     if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
   420    418     while( !db->mallocFailed && zSql[i]!=0 ){
   421    419       assert( i>=0 );
   422    420       pParse->sLastToken.z = &zSql[i];
   423    421       pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
   424    422       i += pParse->sLastToken.n;
................................................................................
   504    502       ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
   505    503       ** will take responsibility for freeing the Table structure.
   506    504       */
   507    505       sqlite3DeleteTable(db, pParse->pNewTable);
   508    506     }
   509    507   
   510    508     sqlite3DeleteTrigger(db, pParse->pNewTrigger);
   511         -  sqlite3DbFree(db, pParse->apVarExpr);
          509  +  for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
          510  +  sqlite3DbFree(db, pParse->azVar);
   512    511     sqlite3DbFree(db, pParse->aAlias);
   513    512     while( pParse->pAinc ){
   514    513       AutoincInfo *p = pParse->pAinc;
   515    514       pParse->pAinc = p->pNext;
   516    515       sqlite3DbFree(db, p);
   517    516     }
   518    517     while( pParse->pZombieTab ){

Changes to src/trigger.c.

   297    297       z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
   298    298       sqlite3NestedParse(pParse,
   299    299          "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
   300    300          db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
   301    301          pTrig->table, z);
   302    302       sqlite3DbFree(db, z);
   303    303       sqlite3ChangeCookie(pParse, iDb);
   304         -    sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf(
   305         -        db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC
   306         -    );
          304  +    sqlite3VdbeAddParseSchemaOp(v, iDb,
          305  +        sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
   307    306     }
   308    307   
   309    308     if( db->init.busy ){
   310    309       Trigger *pLink = pTrig;
   311    310       Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
   312    311       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   313    312       pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);

Changes to src/vdbe.c.

   984    984   ** If the parameter is named, then its name appears in P4 and P3==1.
   985    985   ** The P4 value is used by sqlite3_bind_parameter_name().
   986    986   */
   987    987   case OP_Variable: {            /* out2-prerelease */
   988    988     Mem *pVar;       /* Value being transferred */
   989    989   
   990    990     assert( pOp->p1>0 && pOp->p1<=p->nVar );
          991  +  assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
   991    992     pVar = &p->aVar[pOp->p1 - 1];
   992    993     if( sqlite3VdbeMemTooBig(pVar) ){
   993    994       goto too_big;
   994    995     }
   995    996     sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
   996    997     UPDATE_MAX_BLOBSIZE(pOut);
   997    998     break;
................................................................................
  1762   1763     u16 flags1;         /* Copy of initial value of pIn1->flags */
  1763   1764     u16 flags3;         /* Copy of initial value of pIn3->flags */
  1764   1765   
  1765   1766     pIn1 = &aMem[pOp->p1];
  1766   1767     pIn3 = &aMem[pOp->p3];
  1767   1768     flags1 = pIn1->flags;
  1768   1769     flags3 = pIn3->flags;
  1769         -  if( (pIn1->flags | pIn3->flags)&MEM_Null ){
         1770  +  if( (flags1 | flags3)&MEM_Null ){
  1770   1771       /* One or both operands are NULL */
  1771   1772       if( pOp->p5 & SQLITE_NULLEQ ){
  1772   1773         /* If SQLITE_NULLEQ is set (which will only happen if the operator is
  1773   1774         ** OP_Eq or OP_Ne) then take the jump or not depending on whether
  1774   1775         ** or not both operands are null.
  1775   1776         */
  1776   1777         assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
  1777         -      res = (pIn1->flags & pIn3->flags & MEM_Null)==0;
         1778  +      res = (flags1 & flags3 & MEM_Null)==0;
  1778   1779       }else{
  1779   1780         /* SQLITE_NULLEQ is clear and at least one operand is NULL,
  1780   1781         ** then the result is always NULL.
  1781   1782         ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
  1782   1783         */
  1783   1784         if( pOp->p5 & SQLITE_STOREP2 ){
  1784   1785           pOut = &aMem[pOp->p2];
................................................................................
  2580   2581         */
  2581   2582         sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
  2582   2583           "SQL statements in progress");
  2583   2584         rc = SQLITE_BUSY;
  2584   2585       }else{
  2585   2586         nName = sqlite3Strlen30(zName);
  2586   2587   
         2588  +#ifndef SQLITE_OMIT_VIRTUALTABLE
  2587   2589         /* This call is Ok even if this savepoint is actually a transaction
  2588   2590         ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
  2589   2591         ** If this is a transaction savepoint being opened, it is guaranteed
  2590   2592         ** that the db->aVTrans[] array is empty.  */
  2591   2593         assert( db->autoCommit==0 || db->nVTrans==0 );
  2592   2594         rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
  2593   2595                                   db->nStatement+db->nSavepoint);
  2594   2596         if( rc!=SQLITE_OK ) goto abort_due_to_error;
         2597  +#endif
  2595   2598   
  2596   2599         /* Create a new savepoint structure. */
  2597   2600         pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
  2598   2601         if( pNew ){
  2599   2602           pNew->zName = (char *)&pNew[1];
  2600   2603           memcpy(pNew->zName, zName, nName+1);
  2601   2604       
................................................................................
  5876   5879   /* Opcode: Trace * * * P4 *
  5877   5880   **
  5878   5881   ** If tracing is enabled (by the sqlite3_trace()) interface, then
  5879   5882   ** the UTF-8 string contained in P4 is emitted on the trace callback.
  5880   5883   */
  5881   5884   case OP_Trace: {
  5882   5885     char *zTrace;
         5886  +  char *z;
  5883   5887   
  5884         -  zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
  5885         -  if( zTrace ){
  5886         -    if( db->xTrace ){
  5887         -      char *z = sqlite3VdbeExpandSql(p, zTrace);
  5888         -      db->xTrace(db->pTraceArg, z);
  5889         -      sqlite3DbFree(db, z);
  5890         -    }
         5888  +  if( db->xTrace && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
         5889  +    z = sqlite3VdbeExpandSql(p, zTrace);
         5890  +    db->xTrace(db->pTraceArg, z);
         5891  +    sqlite3DbFree(db, z);
         5892  +  }
  5891   5893   #ifdef SQLITE_DEBUG
  5892         -    if( (db->flags & SQLITE_SqlTrace)!=0 ){
  5893         -      sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
  5894         -    }
  5895         -#endif /* SQLITE_DEBUG */
         5894  +  if( (db->flags & SQLITE_SqlTrace)!=0
         5895  +   && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
         5896  +  ){
         5897  +    sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
  5896   5898     }
         5899  +#endif /* SQLITE_DEBUG */
  5897   5900     break;
  5898   5901   }
  5899   5902   #endif
  5900   5903   
  5901   5904   
  5902   5905   /* Opcode: Noop * * * * *
  5903   5906   **

Changes to src/vdbe.h.

   168    168   int sqlite3VdbeAddOp0(Vdbe*,int);
   169    169   int sqlite3VdbeAddOp1(Vdbe*,int,int);
   170    170   int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
   171    171   int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
   172    172   int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
   173    173   int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
   174    174   int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
          175  +void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
   175    176   void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
   176    177   void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
   177    178   void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
   178    179   void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
   179    180   void sqlite3VdbeJumpHere(Vdbe*, int addr);
   180    181   void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
   181    182   void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
   182    183   void sqlite3VdbeUsesBtree(Vdbe*, int);
   183    184   VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
   184    185   int sqlite3VdbeMakeLabel(Vdbe*);
   185    186   void sqlite3VdbeRunOnlyOnce(Vdbe*);
   186    187   void sqlite3VdbeDelete(Vdbe*);
   187    188   void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
   188         -void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int,int,int);
          189  +void sqlite3VdbeMakeReady(Vdbe*,Parse*);
   189    190   int sqlite3VdbeFinalize(Vdbe*);
   190    191   void sqlite3VdbeResolveLabel(Vdbe*, int);
   191    192   int sqlite3VdbeCurrentAddr(Vdbe*);
   192    193   #ifdef SQLITE_DEBUG
   193    194     int sqlite3VdbeAssertMayAbort(Vdbe *, int);
   194    195     void sqlite3VdbeTrace(Vdbe*,FILE*);
   195    196   #endif
   196    197   void sqlite3VdbeResetStepResult(Vdbe*);
          198  +void sqlite3VdbeRewind(Vdbe*);
   197    199   int sqlite3VdbeReset(Vdbe*);
   198    200   void sqlite3VdbeSetNumCols(Vdbe*,int);
   199    201   int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
   200    202   void sqlite3VdbeCountChanges(Vdbe*);
   201    203   sqlite3 *sqlite3VdbeDb(Vdbe*);
   202    204   void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
   203    205   void sqlite3VdbeSwap(Vdbe*,Vdbe*);

Changes to src/vdbeInt.h.

   283    283     u32 magic;              /* Magic number for sanity checking */
   284    284     char *zErrMsg;          /* Error message written here */
   285    285     Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
   286    286     VdbeCursor **apCsr;     /* One element of this array for each open cursor */
   287    287     Mem *aVar;              /* Values for the OP_Variable opcode. */
   288    288     char **azVar;           /* Name of variables */
   289    289     ynVar nVar;             /* Number of entries in aVar[] */
          290  +  ynVar nzVar;            /* Number of entries in azVar[] */
   290    291     u32 cacheCtr;           /* VdbeCursor row cache generation counter */
   291    292     int pc;                 /* The program counter */
   292    293     int rc;                 /* Value to return */
   293    294     u8 errorAction;         /* Recovery action to do in case of an error */
   294         -  u8 okVar;               /* True if azVar[] has been initialized */
   295    295     u8 explain;             /* True if EXPLAIN present on SQL command */
   296    296     u8 changeCntOn;         /* True to update the change-counter */
   297    297     u8 expired;             /* True if the VM needs to be recompiled */
   298    298     u8 runOnlyOnce;         /* Automatically expire on reset */
   299    299     u8 minWriteFileFormat;  /* Minimum file format for writable database files */
   300    300     u8 inVtabMethod;        /* See comments above */
   301    301     u8 usesStmtJournal;     /* True if uses a statement journal */

Changes to src/vdbeapi.c.

    98     98     int rc;
    99     99     if( pStmt==0 ){
   100    100       rc = SQLITE_OK;
   101    101     }else{
   102    102       Vdbe *v = (Vdbe*)pStmt;
   103    103       sqlite3_mutex_enter(v->db->mutex);
   104    104       rc = sqlite3VdbeReset(v);
   105         -    sqlite3VdbeMakeReady(v, -1, 0, 0, 0, 0, 0);
          105  +    sqlite3VdbeRewind(v);
   106    106       assert( (rc & (v->db->errMask))==rc );
   107    107       rc = sqlite3ApiExit(v->db, rc);
   108    108       sqlite3_mutex_leave(v->db->mutex);
   109    109     }
   110    110     return rc;
   111    111   }
   112    112   
................................................................................
  1163   1163   ** This routine is added to support DBD::SQLite.  
  1164   1164   */
  1165   1165   int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
  1166   1166     Vdbe *p = (Vdbe*)pStmt;
  1167   1167     return p ? p->nVar : 0;
  1168   1168   }
  1169   1169   
  1170         -/*
  1171         -** Create a mapping from variable numbers to variable names
  1172         -** in the Vdbe.azVar[] array, if such a mapping does not already
  1173         -** exist.
  1174         -*/
  1175         -static void createVarMap(Vdbe *p){
  1176         -  if( !p->okVar ){
  1177         -    int j;
  1178         -    Op *pOp;
  1179         -    sqlite3_mutex_enter(p->db->mutex);
  1180         -    /* The race condition here is harmless.  If two threads call this
  1181         -    ** routine on the same Vdbe at the same time, they both might end
  1182         -    ** up initializing the Vdbe.azVar[] array.  That is a little extra
  1183         -    ** work but it results in the same answer.
  1184         -    */
  1185         -    for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
  1186         -      if( pOp->opcode==OP_Variable ){
  1187         -        assert( pOp->p1>0 && pOp->p1<=p->nVar );
  1188         -        p->azVar[pOp->p1-1] = pOp->p4.z;
  1189         -      }
  1190         -    }
  1191         -    p->okVar = 1;
  1192         -    sqlite3_mutex_leave(p->db->mutex);
  1193         -  }
  1194         -}
  1195         -
  1196   1170   /*
  1197   1171   ** Return the name of a wildcard parameter.  Return NULL if the index
  1198   1172   ** is out of range or if the wildcard is unnamed.
  1199   1173   **
  1200   1174   ** The result is always UTF-8.
  1201   1175   */
  1202   1176   const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
  1203   1177     Vdbe *p = (Vdbe*)pStmt;
  1204         -  if( p==0 || i<1 || i>p->nVar ){
         1178  +  if( p==0 || i<1 || i>p->nzVar ){
  1205   1179       return 0;
  1206   1180     }
  1207         -  createVarMap(p);
  1208   1181     return p->azVar[i-1];
  1209   1182   }
  1210   1183   
  1211   1184   /*
  1212   1185   ** Given a wildcard parameter name, return the index of the variable
  1213   1186   ** with that name.  If there is no variable with the given name,
  1214   1187   ** return 0.
  1215   1188   */
  1216   1189   int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
  1217   1190     int i;
  1218   1191     if( p==0 ){
  1219   1192       return 0;
  1220   1193     }
  1221         -  createVarMap(p); 
  1222   1194     if( zName ){
  1223         -    for(i=0; i<p->nVar; i++){
         1195  +    for(i=0; i<p->nzVar; i++){
  1224   1196         const char *z = p->azVar[i];
  1225   1197         if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){
  1226   1198           return i+1;
  1227   1199         }
  1228   1200       }
  1229   1201     }
  1230   1202     return 0;

Changes to src/vdbeaux.c.

   152    152     pOp->opcode = (u8)op;
   153    153     pOp->p5 = 0;
   154    154     pOp->p1 = p1;
   155    155     pOp->p2 = p2;
   156    156     pOp->p3 = p3;
   157    157     pOp->p4.p = 0;
   158    158     pOp->p4type = P4_NOTUSED;
   159         -  p->expired = 0;
   160         -  if( op==OP_ParseSchema ){
   161         -    /* Any program that uses the OP_ParseSchema opcode needs to lock
   162         -    ** all btrees. */
   163         -    int j;
   164         -    for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
   165         -  }
   166    159   #ifdef SQLITE_DEBUG
   167    160     pOp->zComment = 0;
   168    161     if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
   169    162   #endif
   170    163   #ifdef VDBE_PROFILE
   171    164     pOp->cycles = 0;
   172    165     pOp->cnt = 0;
................................................................................
   196    189     const char *zP4,    /* The P4 operand */
   197    190     int p4type          /* P4 operand type */
   198    191   ){
   199    192     int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
   200    193     sqlite3VdbeChangeP4(p, addr, zP4, p4type);
   201    194     return addr;
   202    195   }
          196  +
          197  +/*
          198  +** Add an OP_ParseSchema opcode.  This routine is broken out from
          199  +** sqlite3VdbeAddOp4() since it needs to also local all btrees.
          200  +**
          201  +** The zWhere string must have been obtained from sqlite3_malloc().
          202  +** This routine will take ownership of the allocated memory.
          203  +*/
          204  +void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
          205  +  int j;
          206  +  int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
          207  +  sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
          208  +  for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
          209  +}
   203    210   
   204    211   /*
   205    212   ** Add an opcode that includes the p4 value as an integer.
   206    213   */
   207    214   int sqlite3VdbeAddOp4Int(
   208    215     Vdbe *p,            /* Add the opcode to this VM */
   209    216     int op,             /* The new opcode */
................................................................................
  1387   1394     }else{
  1388   1395       *pnByte += nByte;
  1389   1396     }
  1390   1397     return pBuf;
  1391   1398   }
  1392   1399   
  1393   1400   /*
  1394         -** Prepare a virtual machine for execution.  This involves things such
  1395         -** as allocating stack space and initializing the program counter.
  1396         -** After the VDBE has be prepped, it can be executed by one or more
  1397         -** calls to sqlite3VdbeExec().  
  1398         -**
  1399         -** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
  1400         -** VDBE_MAGIC_RUN.
  1401         -**
  1402         -** This function may be called more than once on a single virtual machine.
  1403         -** The first call is made while compiling the SQL statement. Subsequent
  1404         -** calls are made as part of the process of resetting a statement to be
  1405         -** re-executed (from a call to sqlite3_reset()). The nVar, nMem, nCursor 
  1406         -** and isExplain parameters are only passed correct values the first time
  1407         -** the function is called. On subsequent calls, from sqlite3_reset(), nVar
  1408         -** is passed -1 and nMem, nCursor and isExplain are all passed zero.
         1401  +** Rewind the VDBE back to the beginning in preparation for
         1402  +** running it.
  1409   1403   */
  1410         -void sqlite3VdbeMakeReady(
  1411         -  Vdbe *p,                       /* The VDBE */
  1412         -  int nVar,                      /* Number of '?' see in the SQL statement */
  1413         -  int nMem,                      /* Number of memory cells to allocate */
  1414         -  int nCursor,                   /* Number of cursors to allocate */
  1415         -  int nArg,                      /* Maximum number of args in SubPrograms */
  1416         -  int isExplain,                 /* True if the EXPLAIN keywords is present */
  1417         -  int usesStmtJournal            /* True to set Vdbe.usesStmtJournal */
  1418         -){
  1419         -  int n;
  1420         -  sqlite3 *db = p->db;
  1421         -
         1404  +void sqlite3VdbeRewind(Vdbe *p){
         1405  +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
         1406  +  int i;
         1407  +#endif
  1422   1408     assert( p!=0 );
  1423   1409     assert( p->magic==VDBE_MAGIC_INIT );
  1424   1410   
  1425   1411     /* There should be at least one opcode.
  1426   1412     */
  1427   1413     assert( p->nOp>0 );
  1428   1414   
  1429   1415     /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
  1430   1416     p->magic = VDBE_MAGIC_RUN;
  1431   1417   
         1418  +#ifdef SQLITE_DEBUG
         1419  +  for(i=1; i<p->nMem; i++){
         1420  +    assert( p->aMem[i].db==p->db );
         1421  +  }
         1422  +#endif
         1423  +  p->pc = -1;
         1424  +  p->rc = SQLITE_OK;
         1425  +  p->errorAction = OE_Abort;
         1426  +  p->magic = VDBE_MAGIC_RUN;
         1427  +  p->nChange = 0;
         1428  +  p->cacheCtr = 1;
         1429  +  p->minWriteFileFormat = 255;
         1430  +  p->iStatement = 0;
         1431  +  p->nFkConstraint = 0;
         1432  +#ifdef VDBE_PROFILE
         1433  +  for(i=0; i<p->nOp; i++){
         1434  +    p->aOp[i].cnt = 0;
         1435  +    p->aOp[i].cycles = 0;
         1436  +  }
         1437  +#endif
         1438  +}
         1439  +
         1440  +/*
         1441  +** Prepare a virtual machine for execution for the first time after
         1442  +** creating the virtual machine.  This involves things such
         1443  +** as allocating stack space and initializing the program counter.
         1444  +** After the VDBE has be prepped, it can be executed by one or more
         1445  +** calls to sqlite3VdbeExec().  
         1446  +**
         1447  +** This function may be called exact once on a each virtual machine.
         1448  +** After this routine is called the VM has been "packaged" and is ready
         1449  +** to run.  After this routine is called, futher calls to 
         1450  +** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
         1451  +** the Vdbe from the Parse object that helped generate it so that the
         1452  +** the Vdbe becomes an independent entity and the Parse object can be
         1453  +** destroyed.
         1454  +**
         1455  +** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
         1456  +** to its initial state after it has been run.
         1457  +*/
         1458  +void sqlite3VdbeMakeReady(
         1459  +  Vdbe *p,                       /* The VDBE */
         1460  +  Parse *pParse                  /* Parsing context */
         1461  +){
         1462  +  sqlite3 *db;                   /* The database connection */
         1463  +  int nVar;                      /* Number of parameters */
         1464  +  int nMem;                      /* Number of VM memory registers */
         1465  +  int nCursor;                   /* Number of cursors required */
         1466  +  int nArg;                      /* Number of arguments in subprograms */
         1467  +  int n;                         /* Loop counter */
         1468  +  u8 *zCsr;                      /* Memory available for allocation */
         1469  +  u8 *zEnd;                      /* First byte past allocated memory */
         1470  +  int nByte;                     /* How much extra memory is needed */
         1471  +
         1472  +  assert( p!=0 );
         1473  +  assert( p->nOp>0 );
         1474  +  assert( pParse!=0 );
         1475  +  assert( p->magic==VDBE_MAGIC_INIT );
         1476  +  db = p->db;
         1477  +  assert( db->mallocFailed==0 );
         1478  +  nVar = pParse->nVar;
         1479  +  nMem = pParse->nMem;
         1480  +  nCursor = pParse->nTab;
         1481  +  nArg = pParse->nMaxArg;
         1482  +  
  1432   1483     /* For each cursor required, also allocate a memory cell. Memory
  1433   1484     ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
  1434   1485     ** the vdbe program. Instead they are used to allocate space for
  1435   1486     ** VdbeCursor/BtCursor structures. The blob of memory associated with 
  1436   1487     ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
  1437   1488     ** stores the blob of memory associated with cursor 1, etc.
  1438   1489     **
  1439   1490     ** See also: allocateCursor().
  1440   1491     */
  1441   1492     nMem += nCursor;
  1442   1493   
  1443   1494     /* Allocate space for memory registers, SQL variables, VDBE cursors and 
  1444         -  ** an array to marshal SQL function arguments in. This is only done the
  1445         -  ** first time this function is called for a given VDBE, not when it is
  1446         -  ** being called from sqlite3_reset() to reset the virtual machine.
  1447         -  */
  1448         -  if( nVar>=0 && ALWAYS(db->mallocFailed==0) ){
  1449         -    u8 *zCsr = (u8 *)&p->aOp[p->nOp];       /* Memory avaliable for alloation */
  1450         -    u8 *zEnd = (u8 *)&p->aOp[p->nOpAlloc];  /* First byte past available mem */
  1451         -    int nByte;                              /* How much extra memory needed */
  1452         -
  1453         -    resolveP2Values(p, &nArg);
  1454         -    p->usesStmtJournal = (u8)usesStmtJournal;
  1455         -    if( isExplain && nMem<10 ){
  1456         -      nMem = 10;
  1457         -    }
  1458         -    memset(zCsr, 0, zEnd-zCsr);
  1459         -    zCsr += (zCsr - (u8*)0)&7;
  1460         -    assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
  1461         -
  1462         -    /* Memory for registers, parameters, cursor, etc, is allocated in two
  1463         -    ** passes.  On the first pass, we try to reuse unused space at the 
  1464         -    ** end of the opcode array.  If we are unable to satisfy all memory
  1465         -    ** requirements by reusing the opcode array tail, then the second
  1466         -    ** pass will fill in the rest using a fresh allocation.  
  1467         -    **
  1468         -    ** This two-pass approach that reuses as much memory as possible from
  1469         -    ** the leftover space at the end of the opcode array can significantly
  1470         -    ** reduce the amount of memory held by a prepared statement.
  1471         -    */
  1472         -    do {
  1473         -      nByte = 0;
  1474         -      p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
  1475         -      p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
  1476         -      p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
  1477         -      p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
  1478         -      p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
  1479         -                            &zCsr, zEnd, &nByte);
  1480         -      if( nByte ){
  1481         -        p->pFree = sqlite3DbMallocZero(db, nByte);
  1482         -      }
  1483         -      zCsr = p->pFree;
  1484         -      zEnd = &zCsr[nByte];
  1485         -    }while( nByte && !db->mallocFailed );
  1486         -
  1487         -    p->nCursor = (u16)nCursor;
  1488         -    if( p->aVar ){
  1489         -      p->nVar = (ynVar)nVar;
  1490         -      for(n=0; n<nVar; n++){
  1491         -        p->aVar[n].flags = MEM_Null;
  1492         -        p->aVar[n].db = db;
  1493         -      }
  1494         -    }
  1495         -    if( p->aMem ){
  1496         -      p->aMem--;                      /* aMem[] goes from 1..nMem */
  1497         -      p->nMem = nMem;                 /*       not from 0..nMem-1 */
  1498         -      for(n=1; n<=nMem; n++){
  1499         -        p->aMem[n].flags = MEM_Null;
  1500         -        p->aMem[n].db = db;
  1501         -      }
  1502         -    }
  1503         -  }
  1504         -#ifdef SQLITE_DEBUG
  1505         -  for(n=1; n<p->nMem; n++){
  1506         -    assert( p->aMem[n].db==db );
  1507         -  }
  1508         -#endif
  1509         -
  1510         -  p->pc = -1;
  1511         -  p->rc = SQLITE_OK;
  1512         -  p->errorAction = OE_Abort;
  1513         -  p->explain |= isExplain;
  1514         -  p->magic = VDBE_MAGIC_RUN;
  1515         -  p->nChange = 0;
  1516         -  p->cacheCtr = 1;
  1517         -  p->minWriteFileFormat = 255;
  1518         -  p->iStatement = 0;
  1519         -  p->nFkConstraint = 0;
  1520         -#ifdef VDBE_PROFILE
  1521         -  {
  1522         -    int i;
  1523         -    for(i=0; i<p->nOp; i++){
  1524         -      p->aOp[i].cnt = 0;
  1525         -      p->aOp[i].cycles = 0;
  1526         -    }
  1527         -  }
  1528         -#endif
         1495  +  ** an array to marshal SQL function arguments in.
         1496  +  */
         1497  +  zCsr = (u8*)&p->aOp[p->nOp];       /* Memory avaliable for allocation */
         1498  +  zEnd = (u8*)&p->aOp[p->nOpAlloc];  /* First byte past end of zCsr[] */
         1499  +
         1500  +  resolveP2Values(p, &nArg);
         1501  +  p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
         1502  +  if( pParse->explain && nMem<10 ){
         1503  +    nMem = 10;
         1504  +  }
         1505  +  memset(zCsr, 0, zEnd-zCsr);
         1506  +  zCsr += (zCsr - (u8*)0)&7;
         1507  +  assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
         1508  +
         1509  +  /* Memory for registers, parameters, cursor, etc, is allocated in two
         1510  +  ** passes.  On the first pass, we try to reuse unused space at the 
         1511  +  ** end of the opcode array.  If we are unable to satisfy all memory
         1512  +  ** requirements by reusing the opcode array tail, then the second
         1513  +  ** pass will fill in the rest using a fresh allocation.  
         1514  +  **
         1515  +  ** This two-pass approach that reuses as much memory as possible from
         1516  +  ** the leftover space at the end of the opcode array can significantly
         1517  +  ** reduce the amount of memory held by a prepared statement.
         1518  +  */
         1519  +  do {
         1520  +    nByte = 0;
         1521  +    p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
         1522  +    p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
         1523  +    p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
         1524  +    p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
         1525  +    p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
         1526  +                          &zCsr, zEnd, &nByte);
         1527  +    if( nByte ){
         1528  +      p->pFree = sqlite3DbMallocZero(db, nByte);
         1529  +    }
         1530  +    zCsr = p->pFree;
         1531  +    zEnd = &zCsr[nByte];
         1532  +  }while( nByte && !db->mallocFailed );
         1533  +
         1534  +  p->nCursor = (u16)nCursor;
         1535  +  if( p->aVar ){
         1536  +    p->nVar = (ynVar)nVar;
         1537  +    for(n=0; n<nVar; n++){
         1538  +      p->aVar[n].flags = MEM_Null;
         1539  +      p->aVar[n].db = db;
         1540  +    }
         1541  +  }
         1542  +  if( p->azVar ){
         1543  +    p->nzVar = pParse->nzVar;
         1544  +    memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
         1545  +    memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
         1546  +  }
         1547  +  if( p->aMem ){
         1548  +    p->aMem--;                      /* aMem[] goes from 1..nMem */
         1549  +    p->nMem = nMem;                 /*       not from 0..nMem-1 */
         1550  +    for(n=1; n<=nMem; n++){
         1551  +      p->aMem[n].flags = MEM_Null;
         1552  +      p->aMem[n].db = db;
         1553  +    }
         1554  +  }
         1555  +  p->explain = pParse->explain;
         1556  +  sqlite3VdbeRewind(p);
  1529   1557   }
  1530   1558   
  1531   1559   /*
  1532   1560   ** Close a VDBE cursor and release all the resources that cursor 
  1533   1561   ** happens to hold.
  1534   1562   */
  1535   1563   void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
................................................................................
  2395   2423   ** Free all memory associated with the Vdbe passed as the second argument.
  2396   2424   ** The difference between this function and sqlite3VdbeDelete() is that
  2397   2425   ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
  2398   2426   ** the database connection.
  2399   2427   */
  2400   2428   void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
  2401   2429     SubProgram *pSub, *pNext;
         2430  +  int i;
  2402   2431     assert( p->db==0 || p->db==db );
  2403   2432     releaseMemArray(p->aVar, p->nVar);
  2404   2433     releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
  2405   2434     for(pSub=p->pProgram; pSub; pSub=pNext){
  2406   2435       pNext = pSub->pNext;
  2407   2436       vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
  2408   2437       sqlite3DbFree(db, pSub);
  2409   2438     }
         2439  +  for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
  2410   2440     vdbeFreeOpArray(db, p->aOp, p->nOp);
  2411   2441     sqlite3DbFree(db, p->aLabel);
  2412   2442     sqlite3DbFree(db, p->aColName);
  2413   2443     sqlite3DbFree(db, p->zSql);
  2414   2444     sqlite3DbFree(db, p->pFree);
  2415   2445     sqlite3DbFree(db, p);
  2416   2446   }
................................................................................
  2848   2878     u = 0;
  2849   2879     while( idx<szHdr && u<p->nField && d<=nKey ){
  2850   2880       u32 serial_type;
  2851   2881   
  2852   2882       idx += getVarint32(&aKey[idx], serial_type);
  2853   2883       pMem->enc = pKeyInfo->enc;
  2854   2884       pMem->db = pKeyInfo->db;
  2855         -    pMem->flags = 0;
         2885  +    /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
  2856   2886       pMem->zMalloc = 0;
  2857   2887       d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
  2858   2888       pMem++;
  2859   2889       u++;
  2860   2890     }
  2861   2891     assert( u<=pKeyInfo->nField + 1 );
  2862   2892     p->nField = u;
................................................................................
  2863   2893     return (void*)p;
  2864   2894   }
  2865   2895   
  2866   2896   /*
  2867   2897   ** This routine destroys a UnpackedRecord object.
  2868   2898   */
  2869   2899   void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
         2900  +#ifdef SQLITE_DEBUG
  2870   2901     int i;
  2871   2902     Mem *pMem;
  2872   2903   
  2873   2904     assert( p!=0 );
  2874   2905     assert( p->flags & UNPACKED_NEED_DESTROY );
  2875   2906     for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
  2876   2907       /* The unpacked record is always constructed by the
  2877   2908       ** sqlite3VdbeUnpackRecord() function above, which makes all
  2878   2909       ** strings and blobs static.  And none of the elements are
  2879   2910       ** ever transformed, so there is never anything to delete.
  2880   2911       */
  2881   2912       if( NEVER(pMem->zMalloc) ) sqlite3VdbeMemRelease(pMem);
  2882   2913     }
         2914  +#endif
  2883   2915     if( p->flags & UNPACKED_NEED_FREE ){
  2884   2916       sqlite3DbFree(p->pKeyInfo->db, p);
  2885   2917     }
  2886   2918   }
  2887   2919   
  2888   2920   /*
  2889   2921   ** This function compares the two table rows or index records

Changes to src/vdbeblob.c.

   293    293         ** always return an SQL NULL. This is useful because it means
   294    294         ** we can invoke OP_Column to fill in the vdbe cursors type 
   295    295         ** and offset cache without causing any IO.
   296    296         */
   297    297         sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
   298    298         sqlite3VdbeChangeP2(v, 7, pTab->nCol);
   299    299         if( !db->mallocFailed ){
   300         -        sqlite3VdbeMakeReady(v, 1, 1, 1, 0, 0, 0);
          300  +        pParse->nVar = 1;
          301  +        pParse->nMem = 1;
          302  +        pParse->nTab = 1;
          303  +        sqlite3VdbeMakeReady(v, pParse);
   301    304         }
   302    305       }
   303    306      
   304    307       pBlob->flags = flags;
   305    308       pBlob->iCol = iCol;
   306    309       pBlob->db = db;
   307    310       sqlite3BtreeLeaveAll(db);

Changes to src/vtab.c.

   379    379       );
   380    380       sqlite3DbFree(db, zStmt);
   381    381       v = sqlite3GetVdbe(pParse);
   382    382       sqlite3ChangeCookie(pParse, iDb);
   383    383   
   384    384       sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
   385    385       zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
   386         -    sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
          386  +    sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
   387    387       sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, 
   388    388                            pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
   389    389     }
   390    390   
   391    391     /* If we are rereading the sqlite_master table create the in-memory
   392    392     ** record of the table. The xConnect() method is not called until
   393    393     ** the first time the virtual table is used in an SQL statement. This

Changes to src/wal.c.

   416    416     int nWiData;               /* Size of array apWiData */
   417    417     volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
   418    418     u32 szPage;                /* Database page size */
   419    419     i16 readLock;              /* Which read lock is being held.  -1 for none */
   420    420     u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
   421    421     u8 writeLock;              /* True if in a write transaction */
   422    422     u8 ckptLock;               /* True if holding a checkpoint lock */
   423         -  u8 readOnly;               /* True if the WAL file is open read-only */
          423  +  u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
   424    424     WalIndexHdr hdr;           /* Wal-index header for current transaction */
   425    425     const char *zWalName;      /* Name of WAL file */
   426    426     u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
   427    427   #ifdef SQLITE_DEBUG
   428    428     u8 lockError;              /* True if a locking error has occurred */
   429    429   #endif
   430    430   };
................................................................................
   432    432   /*
   433    433   ** Candidate values for Wal.exclusiveMode.
   434    434   */
   435    435   #define WAL_NORMAL_MODE     0
   436    436   #define WAL_EXCLUSIVE_MODE  1     
   437    437   #define WAL_HEAPMEMORY_MODE 2
   438    438   
          439  +/*
          440  +** Possible values for WAL.readOnly
          441  +*/
          442  +#define WAL_RDWR        0    /* Normal read/write connection */
          443  +#define WAL_RDONLY      1    /* The WAL file is readonly */
          444  +#define WAL_SHM_RDONLY  2    /* The SHM file is readonly */
          445  +
   439    446   /*
   440    447   ** Each page of the wal-index mapping contains a hash-table made up of
   441    448   ** an array of HASHTABLE_NSLOT elements of the following type.
   442    449   */
   443    450   typedef u16 ht_slot;
   444    451   
   445    452   /*
................................................................................
   525    532       if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
   526    533         pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
   527    534         if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
   528    535       }else{
   529    536         rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
   530    537             pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
   531    538         );
          539  +      if( rc==SQLITE_READONLY ){
          540  +        pWal->readOnly |= WAL_SHM_RDONLY;
          541  +        rc = SQLITE_OK;
          542  +      }
   532    543       }
   533    544     }
   534    545   
   535    546     *ppPage = pWal->apWiData[iPage];
   536    547     assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
   537    548     return rc;
   538    549   }
................................................................................
  1272   1283     pRet->zWalName = zWalName;
  1273   1284     pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
  1274   1285   
  1275   1286     /* Open file handle on the write-ahead log file. */
  1276   1287     flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
  1277   1288     rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
  1278   1289     if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
  1279         -    pRet->readOnly = 1;
         1290  +    pRet->readOnly = WAL_RDONLY;
  1280   1291     }
  1281   1292   
  1282   1293     if( rc!=SQLITE_OK ){
  1283   1294       walIndexClose(pRet, 0);
  1284   1295       sqlite3OsClose(pRet->pWalFd);
  1285   1296       sqlite3_free(pRet);
  1286   1297     }else{
................................................................................
  1913   1924     */
  1914   1925     badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
  1915   1926   
  1916   1927     /* If the first attempt failed, it might have been due to a race
  1917   1928     ** with a writer.  So get a WRITE lock and try again.
  1918   1929     */
  1919   1930     assert( badHdr==0 || pWal->writeLock==0 );
  1920         -  if( badHdr && SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
  1921         -    pWal->writeLock = 1;
  1922         -    if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
  1923         -      badHdr = walIndexTryHdr(pWal, pChanged);
  1924         -      if( badHdr ){
  1925         -        /* If the wal-index header is still malformed even while holding
  1926         -        ** a WRITE lock, it can only mean that the header is corrupted and
  1927         -        ** needs to be reconstructed.  So run recovery to do exactly that.
  1928         -        */
  1929         -        rc = walIndexRecover(pWal);
  1930         -        *pChanged = 1;
         1931  +  if( badHdr ){
         1932  +    if( pWal->readOnly & WAL_SHM_RDONLY ){
         1933  +      if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
         1934  +        walUnlockShared(pWal, WAL_WRITE_LOCK);
         1935  +        rc = SQLITE_READONLY_RECOVERY;
         1936  +      }
         1937  +    }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
         1938  +      pWal->writeLock = 1;
         1939  +      if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
         1940  +        badHdr = walIndexTryHdr(pWal, pChanged);
         1941  +        if( badHdr ){
         1942  +          /* If the wal-index header is still malformed even while holding
         1943  +          ** a WRITE lock, it can only mean that the header is corrupted and
         1944  +          ** needs to be reconstructed.  So run recovery to do exactly that.
         1945  +          */
         1946  +          rc = walIndexRecover(pWal);
         1947  +          *pChanged = 1;
         1948  +        }
  1931   1949         }
         1950  +      pWal->writeLock = 0;
         1951  +      walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
  1932   1952       }
  1933         -    pWal->writeLock = 0;
  1934         -    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
  1935   1953     }
  1936   1954   
  1937   1955     /* If the header is read successfully, check the version number to make
  1938   1956     ** sure the wal-index was not constructed with some future format that
  1939   1957     ** this version of SQLite cannot understand.
  1940   1958     */
  1941   1959     if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
................................................................................
  2114   2132         assert( thisMark!=READMARK_NOT_USED );
  2115   2133         mxReadMark = thisMark;
  2116   2134         mxI = i;
  2117   2135       }
  2118   2136     }
  2119   2137     /* There was once an "if" here. The extra "{" is to preserve indentation. */
  2120   2138     {
  2121         -    if( mxReadMark < pWal->hdr.mxFrame || mxI==0 ){
         2139  +    if( (pWal->readOnly & WAL_SHM_RDONLY)==0
         2140  +     && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
         2141  +    ){
  2122   2142         for(i=1; i<WAL_NREADER; i++){
  2123   2143           rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
  2124   2144           if( rc==SQLITE_OK ){
  2125   2145             mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
  2126   2146             mxI = i;
  2127   2147             walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
  2128   2148             break;
  2129   2149           }else if( rc!=SQLITE_BUSY ){
  2130   2150             return rc;
  2131   2151           }
  2132   2152         }
  2133   2153       }
  2134   2154       if( mxI==0 ){
  2135         -      assert( rc==SQLITE_BUSY );
  2136         -      return WAL_RETRY;
         2155  +      assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
         2156  +      return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
  2137   2157       }
  2138   2158   
  2139   2159       rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
  2140   2160       if( rc ){
  2141   2161         return rc==SQLITE_BUSY ? WAL_RETRY : rc;
  2142   2162       }
  2143   2163       /* Now that the read-lock has been obtained, check that neither the
................................................................................
  2535   2555   
  2536   2556           /* Limit the size of WAL file if the journal_size_limit PRAGMA is
  2537   2557           ** set to a non-negative value.  Log errors encountered
  2538   2558           ** during the truncation attempt. */
  2539   2559           if( pWal->mxWalSize>=0 ){
  2540   2560             i64 sz;
  2541   2561             int rx;
         2562  +          sqlite3BeginBenignMalloc();
  2542   2563             rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
  2543   2564             if( rx==SQLITE_OK && (sz > pWal->mxWalSize) ){
  2544   2565               rx = sqlite3OsTruncate(pWal->pWalFd, pWal->mxWalSize);
  2545   2566             }
         2567  +          sqlite3EndBenignMalloc();
  2546   2568             if( rx ){
  2547   2569               sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
  2548   2570             }
  2549   2571           }
  2550   2572   
  2551   2573           pWal->nCkpt++;
  2552   2574           pWal->hdr.mxFrame = 0;
................................................................................
  2769   2791     int rc;                         /* Return code */
  2770   2792     int isChanged = 0;              /* True if a new wal-index header is loaded */
  2771   2793     int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
  2772   2794   
  2773   2795     assert( pWal->ckptLock==0 );
  2774   2796     assert( pWal->writeLock==0 );
  2775   2797   
         2798  +  if( pWal->readOnly ) return SQLITE_READONLY;
  2776   2799     WALTRACE(("WAL%p: checkpoint begins\n", pWal));
  2777   2800     rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
  2778   2801     if( rc ){
  2779   2802       /* Usually this is SQLITE_BUSY meaning that another thread or process
  2780   2803       ** is already running a checkpoint, or maybe a recovery.  But it might
  2781   2804       ** also be SQLITE_IOERR. */
  2782   2805       return rc;

Changes to test/e_uri.test.

    44     44     set e
    45     45   }
    46     46   
    47     47   # EVIDENCE-OF: R-35840-33204 If URI filename interpretation is enabled,
    48     48   # and the filename argument begins with "file:", then the filename is
    49     49   # interpreted as a URI.
    50     50   #
    51         -# EVIDENCE-OF: R-00067-59538 URI filename interpretation is enabled if
           51  +# EVIDENCE-OF: R-32637-34037 URI filename interpretation is enabled if
    52     52   # the SQLITE_OPEN_URI flag is is set in the fourth argument to
    53     53   # sqlite3_open_v2(), or if it has been enabled globally using the
    54         -# SQLITE_CONFIG_URI option with the sqlite3_config() method.
           54  +# SQLITE_CONFIG_URI option with the sqlite3_config() method or by the
           55  +# SQLITE_USE_URI compile-time option.
    55     56   #
    56     57   if {$tcl_platform(platform) == "unix"} {
    57     58     set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE]
    58     59   
    59     60     # Tests with SQLITE_CONFIG_URI configured to false. URI intepretation is
    60     61     # only enabled if the SQLITE_OPEN_URI flag is specified.
    61     62     sqlite3_shutdown
................................................................................
   136    137         set e [sqlite3_errmsg $DB]
   137    138         sqlite3_close $DB
   138    139         set e
   139    140       } $error
   140    141     }
   141    142   }
   142    143   
   143         -# EVIDENCE-OF: R-43804-65312 The 'fragment' component of a URI, if
   144         -# present, is always ignored.
          144  +# EVIDENCE-OF: R-45981-25528 The fragment component of a URI, if
          145  +# present, is ignored.
   145    146   #
   146    147   #   It is difficult to test that something is ignore correctly. So these tests
   147    148   #   just show that adding a fragment does not interfere with the pathname or
   148    149   #   parameters passed through to the VFS xOpen() methods.
   149    150   #
   150    151   if {$tcl_platform(platform) == "unix"} {
   151    152     foreach {tn uri parse} "
................................................................................
   153    154       2    {file:test.db?a=b#abc} {[pwd]/test.db {a b}}
   154    155       3    {file:test.db?a=b#?c=d} {[pwd]/test.db {a b}}
   155    156     " {
   156    157       do_test 3.$tn { parse_uri $uri } $parse
   157    158     }
   158    159   }
   159    160   
   160         -# EVIDENCE-OF: R-00273-20588 SQLite uses the 'path' component of the URI
   161         -# as the path to the database file to open.
          161  +# EVIDENCE-OF: R-62557-09390 SQLite uses the path component of the URI
          162  +# as the name of the disk file which contains the database.
   162    163   #
   163    164   # EVIDENCE-OF: R-28659-11035 If the path begins with a '/' character,
   164    165   # then it is interpreted as an absolute path.
   165    166   #
   166         -# EVIDENCE-OF: R-39349-47203 If it does not begin with a '/', it is
   167         -# interpreted as a relative path.
          167  +# EVIDENCE-OF: R-46234-61323 If the path does not begin with a '/'
          168  +# (meaning that the authority section is omitted from the URI) then the
          169  +# path is interpreted as a relative path.
   168    170   #
   169    171   if {$tcl_platform(platform) == "unix"} {
   170    172     foreach {tn uri parse} "
   171    173       1    {file:test.db}             {[pwd]/test.db {}}
   172    174       2    {file:/test.db}            {/test.db {}}
   173    175       3    {file:///test.db}          {/test.db {}}
   174    176       4    {file://localhost/test.db} {/test.db {}}

Changes to test/lock_common.tcl.

    51     51       proc csql1 {sql} { list [catch { sql1 $sql } msg] $msg }
    52     52       proc csql2 {sql} { list [catch { sql2 $sql } msg] $msg }
    53     53       proc csql3 {sql} { list [catch { sql3 $sql } msg] $msg }
    54     54   
    55     55       uplevel set $varname $tn
    56     56       uplevel $script
    57     57   
    58         -    code2 { db2 close }
    59         -    code3 { db3 close }
           58  +    catch { code2 { db2 close } }
           59  +    catch { code3 { db3 close } }
    60     60       catch { close $::code2_chan }
    61     61       catch { close $::code3_chan }
    62     62       catch { db close }
    63     63     }
    64     64   }
    65     65   
    66     66   # Launch another testfixture process to be controlled by this one. A

Changes to test/tkt-2d1a5c67d.test.

    15     15   # 
    16     16   #
    17     17   
    18     18   set testdir [file dirname $argv0]
    19     19   source $testdir/tester.tcl
    20     20   set testprefix tkt-2d1a5c67d
    21     21   
    22         -ifcapable !wal {finish_test; return}
           22  +ifcapable {!wal || !vtab} {finish_test; return}
    23     23   
    24     24   for {set ii 1} {$ii<=10} {incr ii} {
    25     25     do_test tkt-2d1a5c67d.1.$ii {
    26     26       db close
    27     27       forcedelete test.db test.db-wal
    28     28       sqlite3 db test.db
    29     29       db eval "PRAGMA cache_size=$::ii"

Added test/walro.test.

            1  +# 2011 May 09
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# This file contains tests for using WAL databases in read-only mode.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +source $testdir/lock_common.tcl
           18  +set ::testprefix walro
           19  +
           20  +
           21  +do_multiclient_test tn {
           22  +  # These tests are only going to work on unix.
           23  +  #
           24  +  if {$tcl_platform(platform) != "unix"} continue
           25  +
           26  +  # Do not run tests with the connections in the same process.
           27  +  #
           28  +  if {$tn==2} continue
           29  +  
           30  +  # Close all connections and delete the database.
           31  +  #
           32  +  code1 { db close  }
           33  +  code2 { db2 close }
           34  +  code3 { db3 close }
           35  +  forcedelete test.db
           36  +  forcedelete walro
           37  +
           38  +  foreach c {code1 code2 code3} {
           39  +    $c {
           40  +      sqlite3_shutdown
           41  +      sqlite3_config_uri 1
           42  +    }
           43  +  }
           44  +
           45  +  file mkdir walro
           46  +
           47  +  do_test 1.1.1 {
           48  +    code2 { sqlite3 db2 test.db }
           49  +    sql2 { 
           50  +      PRAGMA journal_mode = WAL;
           51  +      CREATE TABLE t1(x, y);
           52  +      INSERT INTO t1 VALUES('a', 'b');
           53  +    }
           54  +    file exists test.db-shm
           55  +  } {1}
           56  +
           57  +  do_test 1.1.2 {
           58  +    file attributes test.db-shm -permissions r--r--r--
           59  +    code1 { sqlite3 db file:test.db?readonly_shm=1 }
           60  +  } {}
           61  +
           62  +  do_test 1.1.3 { sql1 "SELECT * FROM t1" }                {a b}
           63  +  do_test 1.1.4 { sql2 "INSERT INTO t1 VALUES('c', 'd')" } {}
           64  +  do_test 1.1.5 { sql1 "SELECT * FROM t1" }                {a b c d}
           65  +
           66  +  # Check that the read-only connection cannot write or checkpoint the db.
           67  +  #
           68  +  do_test 1.1.6 { 
           69  +    csql1 "INSERT INTO t1 VALUES('e', 'f')" 
           70  +  } {1 {attempt to write a readonly database}}
           71  +  do_test 1.1.7 { 
           72  +    csql1 "PRAGMA wal_checkpoint"
           73  +  } {1 {attempt to write a readonly database}}
           74  +
           75  +  do_test 1.1.9  { sql2 "INSERT INTO t1 VALUES('e', 'f')" } {}
           76  +  do_test 1.1.10 { sql1 "SELECT * FROM t1" }                {a b c d e f}
           77  +
           78  +  do_test 1.1.11 { 
           79  +    sql2 {
           80  +      INSERT INTO t1 VALUES('g', 'h');
           81  +      PRAGMA wal_checkpoint;
           82  +    }
           83  +    set {} {}
           84  +  } {}
           85  +  do_test 1.1.12 { sql1 "SELECT * FROM t1" }                {a b c d e f g h}
           86  +  do_test 1.1.13  { sql2 "INSERT INTO t1 VALUES('i', 'j')" } {}
           87  +
           88  +  do_test 1.2.1 {
           89  +    code2 { db2 close }
           90  +    code1 { db close }
           91  +    list [file exists test.db-wal] [file exists test.db-shm]
           92  +  } {1 1}
           93  +  do_test 1.2.2 {
           94  +    code1 { sqlite3 db file:test.db?readonly_shm=1 }
           95  +    sql1 { SELECT * FROM t1 }
           96  +  } {a b c d e f g h i j}
           97  +
           98  +  do_test 1.2.3 {
           99  +    code1 { db close }
          100  +    file attributes test.db-shm -permissions rw-r--r--
          101  +    hexio_write test.db-shm 0 01020304 
          102  +    file attributes test.db-shm -permissions r--r--r--
          103  +    code1 { sqlite3 db file:test.db?readonly_shm=1 }
          104  +    csql1 { SELECT * FROM t1 }
          105  +  } {1 {attempt to write a readonly database}}
          106  +  do_test 1.2.4 {
          107  +    code1 { sqlite3_extended_errcode db } 
          108  +  } {SQLITE_READONLY_RECOVERY}
          109  +
          110  +  do_test 1.2.5 {
          111  +    file attributes test.db-shm -permissions rw-r--r--
          112  +    code2 { sqlite3 db2 test.db }
          113  +    sql2 "SELECT * FROM t1" 
          114  +  } {a b c d e f g h i j}
          115  +  file attributes test.db-shm -permissions r--r--r--
          116  +  do_test 1.2.6 { sql1 "SELECT * FROM t1" } {a b c d e f g h i j}
          117  +
          118  +  do_test 1.2.7 { 
          119  +    sql2 {
          120  +      PRAGMA wal_checkpoint;
          121  +      INSERT INTO t1 VALUES('k', 'l');
          122  +    }
          123  +    set {} {}
          124  +  } {}
          125  +  do_test 1.2.8 { sql1 "SELECT * FROM t1" } {a b c d e f g h i j k l}
          126  +
          127  +  # Now check that if the readonly_shm option is not supplied, or if it
          128  +  # is set to zero, it is not possible to connect to the database without
          129  +  # read-write access to the shm.
          130  +  do_test 1.3.1 {
          131  +    code1 { db close }
          132  +    code1 { sqlite3 db test.db }
          133  +    csql1 { SELECT * FROM t1 }
          134  +  } {1 {unable to open database file}}
          135  +
          136  +  # Also test that if the -shm file can be opened for read/write access,
          137  +  # it is, even if readonly_shm=1 is present in the URI.
          138  +  do_test 1.3.2.1 {
          139  +    code1 { db close }
          140  +    code2 { db2 close }
          141  +    file exists test.db-shm
          142  +  } {0}
          143  +  do_test 1.3.2.2 {
          144  +    code1 { sqlite3 db file:test.db?readonly_shm=1 }
          145  +    sql1 { SELECT * FROM t1 }
          146  +  } {a b c d e f g h i j k l}
          147  +  do_test 1.3.2.3 {
          148  +    code1 { db close }
          149  +    close [open test.db-shm w]
          150  +    file attributes test.db-shm -permissions r--r--r--
          151  +    code1 { sqlite3 db file:test.db?readonly_shm=1 }
          152  +    csql1 { SELECT * FROM t1 }
          153  +  } {1 {attempt to write a readonly database}}
          154  +  do_test 1.3.2.4 {
          155  +    code1 { sqlite3_extended_errcode db } 
          156  +  } {SQLITE_READONLY_RECOVERY}
          157  +}
          158  +
          159  +finish_test

Added tool/build-shell.sh.

            1  +#!/bin/sh
            2  +#
            3  +# This script demonstrates how to do a full-featured build of the sqlite3
            4  +# command-line shell on Linux.
            5  +#
            6  +# SQLite source code should be in a sibling directory named "sqlite".  For
            7  +# example, put SQLite sources in ~/sqlite/sqlite and run this script from
            8  +# ~/sqlite/bld.  There should be an appropriate Makefile in the current
            9  +# directory as well.
           10  +#
           11  +make sqlite3.c
           12  +gcc -o sqlite3 -g -Os -I. \
           13  +   -DSQLITE_THREADSAFE=0 \
           14  +   -DSQLITE_ENABLE_VFSTRACE \
           15  +   -DSQLITE_ENABLE_STAT2 \
           16  +   -DSQLITE_ENABLE_FTS3 \
           17  +   -DSQLITE_ENABLE_RTREE \
           18  +   -DHAVE_READLINE \
           19  +   -DHAVE_USLEEP=1 \
           20  +   ../sqlite/src/shell.c ../sqlite/src/test_vfstrace.c \
           21  +   sqlite3.c -ldl -lreadline -lncurses

Changes to tool/lemon.c.

  3430   3430     int i,j;                  /* Loop counters */
  3431   3431     int hash;                 /* For hashing the name of a type */
  3432   3432     const char *name;         /* Name of the parser */
  3433   3433   
  3434   3434     /* Allocate and initialize types[] and allocate stddt[] */
  3435   3435     arraysize = lemp->nsymbol * 2;
  3436   3436     types = (char**)calloc( arraysize, sizeof(char*) );
         3437  +  if( types==0 ){
         3438  +    fprintf(stderr,"Out of memory.\n");
         3439  +    exit(1);
         3440  +  }
  3437   3441     for(i=0; i<arraysize; i++) types[i] = 0;
  3438   3442     maxdtlength = 0;
  3439   3443     if( lemp->vartype ){
  3440   3444       maxdtlength = lemonStrlen(lemp->vartype);
  3441   3445     }
  3442   3446     for(i=0; i<lemp->nsymbol; i++){
  3443   3447       int len;
  3444   3448       struct symbol *sp = lemp->symbols[i];
  3445   3449       if( sp->datatype==0 ) continue;
  3446   3450       len = lemonStrlen(sp->datatype);
  3447   3451       if( len>maxdtlength ) maxdtlength = len;
  3448   3452     }
  3449   3453     stddt = (char*)malloc( maxdtlength*2 + 1 );
  3450         -  if( types==0 || stddt==0 ){
         3454  +  if( stddt==0 ){
  3451   3455       fprintf(stderr,"Out of memory.\n");
  3452   3456       exit(1);
  3453   3457     }
  3454   3458   
  3455   3459     /* Build a hash table of datatypes. The ".dtnum" field of each symbol
  3456   3460     ** is filled in with the hash index plus 1.  A ".dtnum" value of 0 is
  3457   3461     ** used for terminal symbols.  If there is no %default_type defined then

Changes to tool/shell1.test.

   195    195     set rc [lindex $res 0]
   196    196     list $rc \
   197    197          [regexp {Error: missing argument for option: -nullvalue} $res]
   198    198   } {1 1}
   199    199   
   200    200   # -version             show SQLite version
   201    201   do_test shell1-1.16.1 {
   202         -  catchcmd "-version test.db" "" 
   203         -} {0 3.7.7}
          202  +  set x [catchcmd "-version test.db" ""]
          203  +  regexp {0 \{3.\d.\d+ 20\d\d-[01]\d-\d\d \d\d:\d\d:\d\d [0-9a-f]+\}} $x 
          204  +} 1
   204    205   
   205    206   #----------------------------------------------------------------------------
   206    207   # Test cases shell1-2.*: Basic "dot" command token parsing.
   207    208   #
   208    209   
   209    210   # check first token handling
   210    211   do_test shell1-2.1.1 {

Added tool/symbols.sh.

            1  +#!/bin/sh
            2  +#
            3  +# Run this script in a directory that contains a valid SQLite makefile in
            4  +# order to verify that unintentionally exported symbols.
            5  +#
            6  +make sqlite3.c
            7  +
            8  +echo '****** Exported symbols from a build including RTREE, FTS4 & ICU ******'
            9  +gcc -c -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE \
           10  +  -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_STAT2 \
           11  +  -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_ENABLE_UNLOCK_NOTIFY \
           12  +  -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_ATOMIC_WRITE \
           13  +  -DSQLITE_ENABLE_ICU \
           14  +  sqlite3.c
           15  +nm sqlite3.o | grep ' T ' | sort -k 3
           16  +
           17  +echo '****** Surplus symbols from a build including RTREE, FTS4 & ICU ******'
           18  +nm sqlite3.o | grep ' T ' | grep -v ' sqlite3_'
           19  +
           20  +echo '****** Dependencies of the core. No extensions. No OS interface *******'
           21  +gcc -c -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_STAT2 \
           22  +  -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_ENABLE_UNLOCK_NOTIFY \
           23  +  -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_ATOMIC_WRITE \
           24  +  -DSQLITE_OS_OTHER -DSQLITE_THREADSAFE=0 \
           25  +  sqlite3.c
           26  +nm sqlite3.o | grep ' U ' | sort -k 3
           27  +
           28  +echo '****** Dependencies including RTREE & FTS4 *******'
           29  +gcc -c -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE \
           30  +  -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_STAT2 \
           31  +  -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_ENABLE_UNLOCK_NOTIFY \
           32  +  -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_ATOMIC_WRITE \
           33  +  sqlite3.c
           34  +nm sqlite3.o | grep ' U ' | sort -k 3

Added tool/warnings.sh.

            1  +#/bin/sh
            2  +#
            3  +# Run this script in a directory with a working makefile to check for 
            4  +# compiler warnings in SQLite.
            5  +#
            6  +make sqlite3.c
            7  +echo '********** No optimizations.  Includes FTS4 and RTREE *********'
            8  +gcc -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \
            9  +      -ansi -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \
           10  +      sqlite3.c
           11  +echo '********** Optimized -O3.  Includes FTS4 and RTREE *********'
           12  +gcc -O3 -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \
           13  +      -ansi -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \
           14  +      sqlite3.c