/ Check-in [ce5f95de]
Login

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

Overview
Comment:Merge the latest trunk changes, including the read-only shared memory enhancement, into the apple-osx branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: ce5f95dee6989f17a6a7af89699a02f8c5c6f787
User & Date: drh 2011-06-02 13:07:43
Context
2011-06-20
11:57
Merge the latest trunk changes into the apple-osx branch. check-in: 4c69e827 user: drh tags: apple-osx
2011-06-02
13:07
Merge the latest trunk changes, including the read-only shared memory enhancement, into the apple-osx branch. check-in: ce5f95de user: drh tags: apple-osx
13:04
Merge the read-only shared memory branch into trunk. After this merge, an unprivileged process can open WAL-mode databases owned by another user as long as a database connection with write permission exists on the database file and if the readonly_shm=1 URI query parameter is supplied. check-in: 19084a66 user: drh tags: trunk
2011-05-19
01:51
Pull all the latest trunk changes, and especially the fix for WAL cache spills causing transactions to disappear, into the apple-osx branch. check-in: 8d1a6bb0 user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/fts3/fts3.c.

   973    973     p->nPendingData = 0;
   974    974     p->azColumn = (char **)&p[1];
   975    975     p->pTokenizer = pTokenizer;
   976    976     p->nNodeSize = 1000;
   977    977     p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
   978    978     p->bHasDocsize = (isFts4 && bNoDocsize==0);
   979    979     p->bHasStat = isFts4;
          980  +  TESTONLY( p->inTransaction = -1 );
          981  +  TESTONLY( p->mxSavepoint = -1 );
   980    982     fts3HashInit(&p->pendingTerms, FTS3_HASH_STRING, 1);
   981    983   
   982    984     /* Fill in the zName and zDb fields of the vtab structure. */
   983    985     zCsr = (char *)&p->azColumn[nCol];
   984    986     p->zName = zCsr;
   985    987     memcpy(zCsr, argv[2], nName);
   986    988     zCsr += nName;
................................................................................
  3270   3272   }
  3271   3273   
  3272   3274   /*
  3273   3275   ** Implementation of xBegin() method. This is a no-op.
  3274   3276   */
  3275   3277   static int fts3BeginMethod(sqlite3_vtab *pVtab){
  3276   3278     UNUSED_PARAMETER(pVtab);
  3277         -  assert( ((Fts3Table *)pVtab)->nPendingData==0 );
         3279  +  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
         3280  +  assert( p->nPendingData==0 );
         3281  +  assert( p->inTransaction!=1 );
         3282  +  TESTONLY( p->inTransaction = 1 );
         3283  +  TESTONLY( p->mxSavepoint = -1; );
  3278   3284     return SQLITE_OK;
  3279   3285   }
  3280   3286   
  3281   3287   /*
  3282   3288   ** Implementation of xCommit() method. This is a no-op. The contents of
  3283   3289   ** the pending-terms hash-table have already been flushed into the database
  3284   3290   ** by fts3SyncMethod().
  3285   3291   */
  3286   3292   static int fts3CommitMethod(sqlite3_vtab *pVtab){
  3287   3293     UNUSED_PARAMETER(pVtab);
  3288         -  assert( ((Fts3Table *)pVtab)->nPendingData==0 );
         3294  +  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
         3295  +  assert( p->nPendingData==0 );
         3296  +  assert( p->inTransaction!=0 );
         3297  +  TESTONLY( p->inTransaction = 0 );
         3298  +  TESTONLY( p->mxSavepoint = -1; );
  3289   3299     return SQLITE_OK;
  3290   3300   }
  3291   3301   
  3292   3302   /*
  3293   3303   ** Implementation of xRollback(). Discard the contents of the pending-terms
  3294   3304   ** hash-table. Any changes made to the database are reverted by SQLite.
  3295   3305   */
  3296   3306   static int fts3RollbackMethod(sqlite3_vtab *pVtab){
  3297         -  sqlite3Fts3PendingTermsClear((Fts3Table *)pVtab);
         3307  +  Fts3Table *p = (Fts3Table*)pVtab;
         3308  +  sqlite3Fts3PendingTermsClear(p);
         3309  +  assert( p->inTransaction!=0 );
         3310  +  TESTONLY( p->inTransaction = 0 );
         3311  +  TESTONLY( p->mxSavepoint = -1; );
  3298   3312     return SQLITE_OK;
  3299   3313   }
  3300   3314   
  3301   3315   /*
  3302   3316   ** Load the doclist associated with expression pExpr to pExpr->aDoclist.
  3303   3317   ** The loaded doclist contains positions as well as the document ids.
  3304   3318   ** This is used by the matchinfo(), snippet() and offsets() auxillary
................................................................................
  3646   3660       "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
  3647   3661       p->zDb, p->zName, zName
  3648   3662     );
  3649   3663     return rc;
  3650   3664   }
  3651   3665   
  3652   3666   static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
  3653         -  return sqlite3Fts3PendingTermsFlush((Fts3Table *)pVtab);
         3667  +  Fts3Table *p = (Fts3Table*)pVtab;
         3668  +  UNUSED_PARAMETER(iSavepoint);
         3669  +  assert( p->inTransaction );
         3670  +  assert( p->mxSavepoint < iSavepoint );
         3671  +  TESTONLY( p->mxSavepoint = iSavepoint );
         3672  +  return sqlite3Fts3PendingTermsFlush(p);
  3654   3673   }
  3655   3674   static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
         3675  +  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
         3676  +  UNUSED_PARAMETER(iSavepoint);
         3677  +  UNUSED_PARAMETER(pVtab);
         3678  +  assert( p->inTransaction );
         3679  +  assert( p->mxSavepoint >= iSavepoint );
         3680  +  TESTONLY( p->mxSavepoint = iSavepoint-1 );
  3656   3681     return SQLITE_OK;
  3657   3682   }
  3658   3683   static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
  3659         -  sqlite3Fts3PendingTermsClear((Fts3Table *)pVtab);
         3684  +  Fts3Table *p = (Fts3Table*)pVtab;
         3685  +  UNUSED_PARAMETER(iSavepoint);
         3686  +  assert( p->inTransaction );
         3687  +  assert( p->mxSavepoint >= iSavepoint );
         3688  +  TESTONLY( p->mxSavepoint = iSavepoint );
         3689  +  sqlite3Fts3PendingTermsClear(p);
  3660   3690     return SQLITE_OK;
  3661   3691   }
  3662   3692   
  3663   3693   static const sqlite3_module fts3Module = {
  3664   3694     /* iVersion      */ 2,
  3665   3695     /* xCreate       */ fts3CreateMethod,
  3666   3696     /* xConnect      */ fts3ConnectMethod,

Changes to ext/fts3/fts3Int.h.

    88     88   /*
    89     89   ** Internal types used by SQLite.
    90     90   */
    91     91   typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
    92     92   typedef short int i16;            /* 2-byte (or larger) signed integer */
    93     93   typedef unsigned int u32;         /* 4-byte unsigned integer */
    94     94   typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
           95  +
    95     96   /*
    96     97   ** Macro used to suppress compiler warnings for unused parameters.
    97     98   */
    98     99   #define UNUSED_PARAMETER(x) (void)(x)
          100  +
          101  +/*
          102  +** Activate assert() only if SQLITE_TEST is enabled.
          103  +*/
          104  +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
          105  +# define NDEBUG 1
          106  +#endif
          107  +
          108  +/*
          109  +** The TESTONLY macro is used to enclose variable declarations or
          110  +** other bits of code that are needed to support the arguments
          111  +** within testcase() and assert() macros.
          112  +*/
          113  +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
          114  +# define TESTONLY(X)  X
          115  +#else
          116  +# define TESTONLY(X)
    99    117   #endif
          118  +
          119  +#endif /* SQLITE_AMALGAMATION */
   100    120   
   101    121   typedef struct Fts3Table Fts3Table;
   102    122   typedef struct Fts3Cursor Fts3Cursor;
   103    123   typedef struct Fts3Expr Fts3Expr;
   104    124   typedef struct Fts3Phrase Fts3Phrase;
   105    125   typedef struct Fts3PhraseToken Fts3PhraseToken;
   106    126   
................................................................................
   147    167     ** automatically. Variable iPrevDocid is the docid of the most recently
   148    168     ** inserted record.
   149    169     */
   150    170     int nMaxPendingData;
   151    171     int nPendingData;
   152    172     sqlite_int64 iPrevDocid;
   153    173     Fts3Hash pendingTerms;
          174  +
          175  +#if defined(SQLITE_DEBUG)
          176  +  /* State variables used for validating that the transaction control
          177  +  ** methods of the virtual table are called at appropriate times.  These
          178  +  ** values do not contribution to the FTS computation; they are used for
          179  +  ** verifying the SQLite core.
          180  +  */
          181  +  int inTransaction;     /* True after xBegin but before xCommit/xRollback */
          182  +  int mxSavepoint;       /* Largest valid xSavepoint integer */
          183  +#endif
   154    184   };
   155    185   
   156    186   /*
   157    187   ** When the core wants to read from the virtual table, it creates a
   158    188   ** virtual table cursor (an instance of the following structure) using
   159    189   ** the xOpen method. Cursors are destroyed using the xClose method.
   160    190   */

Changes to ext/fts3/fts3_aux.c.

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

Changes to ext/rtree/rtree.c.

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

Changes to src/btree.c.

  5386   5386   ** The cell content is not freed or deallocated.  It is assumed that
  5387   5387   ** the cell content has been copied someplace else.  This routine just
  5388   5388   ** removes the reference to the cell from pPage.
  5389   5389   **
  5390   5390   ** "sz" must be the number of bytes in the cell.
  5391   5391   */
  5392   5392   static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
  5393         -  int i;          /* Loop counter */
  5394   5393     u32 pc;         /* Offset to cell content of cell being deleted */
  5395   5394     u8 *data;       /* pPage->aData */
  5396   5395     u8 *ptr;        /* Used to move bytes around within data[] */
         5396  +  u8 *endPtr;     /* End of loop */
  5397   5397     int rc;         /* The return code */
  5398   5398     int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
  5399   5399   
  5400   5400     if( *pRC ) return;
  5401   5401   
  5402   5402     assert( idx>=0 && idx<pPage->nCell );
  5403   5403     assert( sz==cellSize(pPage, idx) );
................................................................................
  5414   5414       return;
  5415   5415     }
  5416   5416     rc = freeSpace(pPage, pc, sz);
  5417   5417     if( rc ){
  5418   5418       *pRC = rc;
  5419   5419       return;
  5420   5420     }
  5421         -  for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
         5421  +  endPtr = &data[pPage->cellOffset + 2*pPage->nCell - 2];
         5422  +  while( ptr<endPtr ){
  5422   5423       ptr[0] = ptr[2];
  5423   5424       ptr[1] = ptr[3];
         5425  +    ptr += 2;
  5424   5426     }
  5425   5427     pPage->nCell--;
  5426   5428     put2byte(&data[hdr+3], pPage->nCell);
  5427   5429     pPage->nFree += 2;
  5428   5430   }
  5429   5431   
  5430   5432   /*

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;

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/os_unix.c.

  3767   3767   */
  3768   3768   struct unixShmNode {
  3769   3769     unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
  3770   3770     sqlite3_mutex *mutex;      /* Mutex to access this object */
  3771   3771     char *zFilename;           /* Name of the mmapped file */
  3772   3772     int h;                     /* Open file descriptor */
  3773   3773     int szRegion;              /* Size of shared-memory regions */
  3774         -  int nRegion;               /* Size of array apRegion */
         3774  +  u16 nRegion;               /* Size of array apRegion */
         3775  +  u8 isReadonly;             /* True if read-only */
  3775   3776     char **apRegion;           /* Array of mapped shared-memory regions */
  3776   3777     int nRef;                  /* Number of unixShm objects pointing to this */
  3777   3778     unixShm *pFirst;           /* All unixShm objects pointing to this */
  3778   3779   #ifdef SQLITE_DEBUG
  3779   3780     u8 exclMask;               /* Mask of exclusive locks held */
  3780   3781     u8 sharedMask;             /* Mask of shared locks held */
  3781   3782     u8 nextShmId;              /* Next available unixShm.id value */
................................................................................
  4031   4032         goto shm_open_err;
  4032   4033       }
  4033   4034   
  4034   4035       if( pInode->bProcessLock==0 ){
  4035   4036         pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT,
  4036   4037                                  (sStat.st_mode & 0777));
  4037   4038         if( pShmNode->h<0 ){
  4038         -        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
  4039         -        goto shm_open_err;
         4039  +        const char *zRO;
         4040  +        zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
         4041  +        if( zRO && sqlite3GetBoolean(zRO) ){
         4042  +          pShmNode->h = robust_open(zShmFilename, O_RDONLY,
         4043  +                                    (sStat.st_mode & 0777));
         4044  +          pShmNode->isReadonly = 1;
         4045  +        }
         4046  +        if( pShmNode->h<0 ){
         4047  +          rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
         4048  +          goto shm_open_err;
         4049  +        }
  4040   4050         }
  4041   4051     
  4042   4052         /* Check to see if another process is holding the dead-man switch.
  4043   4053         ** If not, truncate the file to zero length. 
  4044   4054         */
  4045   4055         rc = SQLITE_OK;
  4046   4056         if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
................................................................................
  4171   4181         rc = SQLITE_IOERR_NOMEM;
  4172   4182         goto shmpage_out;
  4173   4183       }
  4174   4184       pShmNode->apRegion = apNew;
  4175   4185       while(pShmNode->nRegion<=iRegion){
  4176   4186         void *pMem;
  4177   4187         if( pShmNode->h>=0 ){
  4178         -        pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, 
         4188  +        pMem = mmap(0, szRegion,
         4189  +            pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
  4179   4190               MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
  4180   4191           );
  4181   4192           if( pMem==MAP_FAILED ){
  4182   4193             rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
  4183   4194             goto shmpage_out;
  4184   4195           }
  4185   4196         }else{
................................................................................
  4197   4208   
  4198   4209   shmpage_out:
  4199   4210     if( pShmNode->nRegion>iRegion ){
  4200   4211       *pp = pShmNode->apRegion[iRegion];
  4201   4212     }else{
  4202   4213       *pp = 0;
  4203   4214     }
         4215  +  if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
  4204   4216     sqlite3_mutex_leave(pShmNode->mutex);
  4205   4217     return rc;
  4206   4218   }
  4207   4219   
  4208   4220   /*
  4209   4221   ** Change the lock state for a shared-memory segment.
  4210   4222   **

Changes to src/pager.c.

  5751   5751         if( pList==0 ){
  5752   5752           /* Must have at least one page for the WAL commit flag.
  5753   5753           ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
  5754   5754           rc = sqlite3PagerGet(pPager, 1, &pPageOne);
  5755   5755           pList = pPageOne;
  5756   5756           pList->pDirty = 0;
  5757   5757         }
  5758         -      assert( pList!=0 || rc!=SQLITE_OK );
  5759         -      if( pList ){
         5758  +      assert( rc==SQLITE_OK );
         5759  +      if( ALWAYS(pList) ){
  5760   5760           rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1, 
  5761   5761               (pPager->fullSync ? pPager->syncFlags : 0)
  5762   5762           );
  5763   5763         }
  5764   5764         sqlite3PagerUnref(pPageOne);
  5765   5765         if( rc==SQLITE_OK ){
  5766   5766           sqlite3PcacheCleanAll(pPager->pPCache);

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/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.
................................................................................
   755    757   **
   756    758   ** Mutexes are created using [sqlite3_mutex_alloc()].
   757    759   */
   758    760   typedef struct sqlite3_mutex sqlite3_mutex;
   759    761   
   760    762   /*
   761    763   ** CAPI3REF: OS Interface Object
   762         -** KEYWORDS: VFS VFSes
   763    764   **
   764    765   ** An instance of the sqlite3_vfs object defines the interface between
   765    766   ** the SQLite core and the underlying operating system.  The "vfs"
   766         -** in the name of the object stands for "virtual file system".
          767  +** in the name of the object stands for "virtual file system".  See
          768  +** the [VFS | VFS documentation] for further information.
   767    769   **
   768    770   ** The value of the iVersion field is initially 1 but may be larger in
   769    771   ** future versions of SQLite.  Additional fields may be appended to this
   770    772   ** object when the iVersion value is increased.  Note that the structure
   771    773   ** of the sqlite3_vfs object changes in the transaction between
   772    774   ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
   773    775   ** modified.

Changes to src/sqliteInt.h.

  1231   1231   */
  1232   1232   struct VTable {
  1233   1233     sqlite3 *db;              /* Database connection associated with this table */
  1234   1234     Module *pMod;             /* Pointer to module implementation */
  1235   1235     sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
  1236   1236     int nRef;                 /* Number of pointers to this structure */
  1237   1237     u8 bConstraint;           /* True if constraints are supported */
         1238  +  int iSavepoint;           /* Depth of the SAVEPOINT stack */
  1238   1239     VTable *pNext;            /* Next in linked list (see above) */
  1239   1240   };
  1240   1241   
  1241   1242   /*
  1242   1243   ** Each SQL table is represented in memory by an instance of the
  1243   1244   ** following structure.
  1244   1245   **
................................................................................
  2225   2226     u8 disableTriggers;  /* True to disable triggers */
  2226   2227     double nQueryLoop;   /* Estimated number of iterations of a query */
  2227   2228   
  2228   2229     /* Above is constant between recursions.  Below is reset before and after
  2229   2230     ** each recursion */
  2230   2231   
  2231   2232     int nVar;            /* Number of '?' variables seen in the SQL so far */
  2232         -  int nVarExpr;        /* Number of used slots in apVarExpr[] */
  2233         -  int nVarExprAlloc;   /* Number of allocated slots in apVarExpr[] */
  2234         -  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 */
  2235   2235     Vdbe *pReprepare;    /* VM being reprepared (sqlite3Reprepare()) */
  2236   2236     int nAlias;          /* Number of aliased result set columns */
  2237   2237     int nAliasAlloc;     /* Number of allocated slots for aAlias[] */
  2238   2238     int *aAlias;         /* Register used to hold aliased result */
  2239   2239     u8 explain;          /* True if the EXPLAIN flag is found on the query */
  2240   2240     Token sNameToken;    /* Token with unqualified schema object name */
  2241   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/test_demovfs.c.

     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13         -** An example of a simple VFS implementation that omits complex features
    14         -** often not required or not possible on embedded platforms. Also includes
    15         -** code to buffer writes to the journal file, which can be a significant
    16         -** performance improvement on some embedded platforms.
           13  +** This file implements an example of a simple VFS implementation that 
           14  +** omits complex features often not required or not possible on embedded
           15  +** platforms.  Code is included to buffer writes to the journal file, 
           16  +** which can be a significant performance improvement on some embedded
           17  +** platforms.
    17     18   **
    18         -*/
    19         -
    20         -/*
    21     19   ** OVERVIEW
    22     20   **
    23     21   **   The code in this file implements a minimal SQLite VFS that can be 
    24     22   **   used on Linux and other posix-like operating systems. The following 
    25     23   **   system calls are used:
    26     24   **
    27     25   **    File-system: access(), unlink(), getcwd()

Changes to src/test_journal.c.

    10     10   **
    11     11   ******************************************************************************
    12     12   **
    13     13   ** This file contains code for a VFS layer that acts as a wrapper around
    14     14   ** an existing VFS. The code in this file attempts to verify that SQLite
    15     15   ** correctly populates and syncs a journal file before writing to a
    16     16   ** corresponding database file.
    17         -*/
    18         -#if SQLITE_TEST          /* This file is used for testing only */
    19         -
    20         -#include "sqlite3.h"
    21         -#include "sqliteInt.h"
    22         -
    23         -/*
           17  +**
    24     18   ** INTERFACE
    25     19   **
    26     20   **   The public interface to this wrapper VFS is two functions:
    27     21   **
    28     22   **     jt_register()
    29     23   **     jt_unregister()
    30     24   **
................................................................................
    95     89   **     a) A block of zeroes (or anything else that is not a valid 
    96     90   **        journal-header) is written to the start of the journal file.
    97     91   **
    98     92   **     b) A journal file is truncated to zero bytes in size using xTruncate.
    99     93   **
   100     94   **     c) The journal file is deleted using xDelete.
   101     95   */
           96  +#if SQLITE_TEST          /* This file is used for testing only */
           97  +
           98  +#include "sqlite3.h"
           99  +#include "sqliteInt.h"
   102    100   
   103    101   /*
   104    102   ** Maximum pathname length supported by the jt backend.
   105    103   */
   106    104   #define JT_MAX_PATHNAME 512
   107    105   
   108    106   /*

Changes to src/test_multiplex.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains a VFS "shim" - a layer that sits in between the
    14         -** pager and the real VFS.
           14  +** pager and the real VFS - that breaks up a very large database file
           15  +** into two or more smaller files on disk.  This is useful, for example,
           16  +** in order to support large, multi-gigabyte databases on older filesystems
           17  +** that limit the maximum file size to 2 GiB.
           18  +**
           19  +** USAGE:
           20  +**
           21  +** Compile this source file and link it with your application.  Then
           22  +** at start-time, invoke the following procedure:
           23  +**
           24  +**   int sqlite3_multiplex_initialize(
           25  +**      const char *zOrigVfsName,    // The underlying real VFS
           26  +**      int makeDefault              // True to make multiplex the default VFS
           27  +**   );
           28  +**
           29  +** The procedure call above will create and register a new VFS shim named
           30  +** "multiplex".  The multiplex VFS will use the VFS named by zOrigVfsName to
           31  +** do the actual disk I/O.  (The zOrigVfsName parameter may be NULL, in 
           32  +** which case the default VFS at the moment sqlite3_multiplex_initialize()
           33  +** is called will be used as the underlying real VFS.)  
    15     34   **
    16         -** This particular shim enforces a multiplex system on DB files.  
    17         -** This shim shards/partitions a single DB file into smaller 
    18         -** "chunks" such that the total DB file size may exceed the maximum
    19         -** file size of the underlying file system.
           35  +** If the makeDefault parameter is TRUE then multiplex becomes the new
           36  +** default VFS.  Otherwise, you can use the multiplex VFS by specifying
           37  +** "multiplex" as the 4th parameter to sqlite3_open_v2() or by employing
           38  +** URI filenames and adding "vfs=multiplex" as a parameter to the filename
           39  +** URI.
    20     40   **
           41  +** The multiplex VFS allows databases up to 32 GiB in size.  But it splits
           42  +** the files up into 1 GiB pieces, so that they will work even on filesystems
           43  +** that do not support large files.
    21     44   */
    22     45   #include "sqlite3.h"
    23     46   #include <string.h>
    24     47   #include <assert.h>
    25     48   #include "test_multiplex.h"
    26     49   
    27     50   #ifndef SQLITE_CORE

Changes to src/test_vfs.c.

     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   ******************************************************************************
    12     12   **
    13         -*/
    14         -#if SQLITE_TEST          /* This file is used for testing only */
    15         -
    16         -/*
    17     13   ** This file contains the implementation of the Tcl [testvfs] command,
    18     14   ** used to create SQLite VFS implementations with various properties and
    19     15   ** instrumentation to support testing SQLite.
    20     16   **
    21     17   **   testvfs VFSNAME ?OPTIONS?
    22     18   **
    23     19   ** Available options are:
................................................................................
    24     20   **
    25     21   **   -noshm      BOOLEAN        (True to omit shm methods. Default false)
    26     22   **   -default    BOOLEAN        (True to make the vfs default. Default false)
    27     23   **   -szosfile   INTEGER        (Value for sqlite3_vfs.szOsFile)
    28     24   **   -mxpathname INTEGER        (Value for sqlite3_vfs.mxPathname)
    29     25   **   -iversion   INTEGER        (Value for sqlite3_vfs.iVersion)
    30     26   */
           27  +#if SQLITE_TEST          /* This file is used for testing only */
    31     28   
    32     29   #include "sqlite3.h"
    33     30   #include "sqliteInt.h"
    34     31   
    35     32   typedef struct Testvfs Testvfs;
    36     33   typedef struct TestvfsShm TestvfsShm;
    37     34   typedef struct TestvfsBuffer TestvfsBuffer;

Changes to src/test_vfstrace.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   ******************************************************************************
    12     12   **
    13     13   ** This file contains code implements a VFS shim that writes diagnostic
    14     14   ** output for each VFS call, similar to "strace".
           15  +**
           16  +** USAGE:
           17  +**
           18  +** This source file exports a single symbol which is the name of a
           19  +** function:
           20  +**
           21  +**   int vfstrace_register(
           22  +**     const char *zTraceName,         // Name of the newly constructed VFS
           23  +**     const char *zOldVfsName,        // Name of the underlying VFS
           24  +**     int (*xOut)(const char*,void*), // Output routine.  ex: fputs
           25  +**     void *pOutArg,                  // 2nd argument to xOut.  ex: stderr
           26  +**     int makeDefault                 // Make the new VFS the default
           27  +**   );
           28  +**
           29  +** Applications that want to trace their VFS usage must provide a callback
           30  +** function with this prototype:
           31  +**
           32  +**   int traceOutput(const char *zMessage, void *pAppData);
           33  +**
           34  +** This function will "output" the trace messages, where "output" can
           35  +** mean different things to different applications.  The traceOutput function
           36  +** for the command-line shell (see shell.c) is "fputs" from the standard
           37  +** library, which means that all trace output is written on the stream
           38  +** specified by the second argument.  In the case of the command-line shell
           39  +** the second argument is stderr.  Other applications might choose to output
           40  +** trace information to a file, over a socket, or write it into a buffer.
           41  +**
           42  +** The vfstrace_register() function creates a new "shim" VFS named by
           43  +** the zTraceName parameter.  A "shim" VFS is an SQLite backend that does
           44  +** not really perform the duties of a true backend, but simply filters or
           45  +** interprets VFS calls before passing them off to another VFS which does
           46  +** the actual work.  In this case the other VFS - the one that does the
           47  +** real work - is identified by the second parameter, zOldVfsName.  If
           48  +** the the 2nd parameter is NULL then the default VFS is used.  The common
           49  +** case is for the 2nd parameter to be NULL.
           50  +**
           51  +** The third and fourth parameters are the pointer to the output function
           52  +** and the second argument to the output function.  For the SQLite
           53  +** command-line shell, when the -vfstrace option is used, these parameters
           54  +** are fputs and stderr, respectively.
           55  +**
           56  +** The fifth argument is true (non-zero) to cause the newly created VFS
           57  +** to become the default VFS.  The common case is for the fifth parameter
           58  +** to be true.
           59  +**
           60  +** The call to vfstrace_register() simply creates the shim VFS that does
           61  +** tracing.  The application must also arrange to use the new VFS for
           62  +** all database connections that are created and for which tracing is 
           63  +** desired.  This can be done by specifying the trace VFS using URI filename
           64  +** notation, or by specifying the trace VFS as the 4th parameter to
           65  +** sqlite3_open_v2() or by making the trace VFS be the default (by setting
           66  +** the 5th parameter of vfstrace_register() to 1).
           67  +**
           68  +**
           69  +** ENABLING VFSTRACE IN A COMMAND-LINE SHELL
           70  +**
           71  +** The SQLite command line shell implemented by the shell.c source file
           72  +** can be used with this module.  To compile in -vfstrace support, first
           73  +** gather this file (test_vfstrace.c), the shell source file (shell.c),
           74  +** and the SQLite amalgamation source files (sqlite3.c, sqlite3.h) into
           75  +** the working directory.  Then compile using a command like the following:
           76  +**
           77  +**    gcc -o sqlite3 -Os -I. -DSQLITE_ENABLE_VFSTRACE \
           78  +**        -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE \
           79  +**        -DHAVE_READLINE -DHAVE_USLEEP=1 \
           80  +**        shell.c test_vfstrace.c sqlite3.c -ldl -lreadline -lncurses
           81  +**
           82  +** The gcc command above works on Linux and provides (in addition to the
           83  +** -vfstrace option) support for FTS3 and FTS4, RTREE, and command-line
           84  +** editing using the readline library.  The command-line shell does not
           85  +** use threads so we added -DSQLITE_THREADSAFE=0 just to make the code
           86  +** run a little faster.   For compiling on a Mac, you'll probably need
           87  +** to omit the -DHAVE_READLINE, the -lreadline, and the -lncurses options.
           88  +** The compilation could be simplified to just this:
           89  +**
           90  +**    gcc -DSQLITE_ENABLE_VFSTRACE \
           91  +**         shell.c test_vfstrace.c sqlite3.c -ldl -lpthread
           92  +**
           93  +** In this second example, all unnecessary options have been removed
           94  +** Note that since the code is now threadsafe, we had to add the -lpthread
           95  +** option to pull in the pthreads library.
           96  +**
           97  +** To cross-compile for windows using MinGW, a command like this might
           98  +** work:
           99  +**
          100  +**    /opt/mingw/bin/i386-mingw32msvc-gcc -o sqlite3.exe -Os -I \
          101  +**         -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_VFSTRACE \
          102  +**         shell.c test_vfstrace.c sqlite3.c
          103  +**
          104  +** Similar compiler commands will work on different systems.  The key
          105  +** invariants are (1) you must have -DSQLITE_ENABLE_VFSTRACE so that
          106  +** the shell.c source file will know to include the -vfstrace command-line
          107  +** option and (2) you must compile and link the three source files
          108  +** shell,c, test_vfstrace.c, and sqlite3.c.  
    15    109   */
    16    110   #include <stdlib.h>
    17    111   #include <string.h>
    18    112   #include "sqlite3.h"
    19    113   
    20    114   /*
    21    115   ** An instance of this structure is attached to the each trace VFS to

Changes to src/tokenize.c.

   408    408     if( pEngine==0 ){
   409    409       db->mallocFailed = 1;
   410    410       return SQLITE_NOMEM;
   411    411     }
   412    412     assert( pParse->pNewTable==0 );
   413    413     assert( pParse->pNewTrigger==0 );
   414    414     assert( pParse->nVar==0 );
   415         -  assert( pParse->nVarExpr==0 );
   416         -  assert( pParse->nVarExprAlloc==0 );
   417         -  assert( pParse->apVarExpr==0 );
          415  +  assert( pParse->nzVar==0 );
          416  +  assert( pParse->azVar==0 );
   418    417     enableLookaside = db->lookaside.bEnabled;
   419    418     if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
   420    419     while( !db->mallocFailed && zSql[i]!=0 ){
   421    420       assert( i>=0 );
   422    421       pParse->sLastToken.z = &zSql[i];
   423    422       pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
   424    423       i += pParse->sLastToken.n;
................................................................................
   504    503       ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
   505    504       ** will take responsibility for freeing the Table structure.
   506    505       */
   507    506       sqlite3DeleteTable(db, pParse->pNewTable);
   508    507     }
   509    508   
   510    509     sqlite3DeleteTrigger(db, pParse->pNewTrigger);
   511         -  sqlite3DbFree(db, pParse->apVarExpr);
          510  +  for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
          511  +  sqlite3DbFree(db, pParse->azVar);
   512    512     sqlite3DbFree(db, pParse->aAlias);
   513    513     while( pParse->pAinc ){
   514    514       AutoincInfo *p = pParse->pAinc;
   515    515       pParse->pAinc = p->pNext;
   516    516       sqlite3DbFree(db, p);
   517    517     }
   518    518     while( pParse->pZombieTab ){

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_VIRTUAL_TABLE
  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         -      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement);
         2594  +      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
         2595  +                                db->nStatement+db->nSavepoint);
  2593   2596         if( rc!=SQLITE_OK ) goto abort_due_to_error;
         2597  +#endif
  2594   2598   
  2595   2599         /* Create a new savepoint structure. */
  2596   2600         pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
  2597   2601         if( pNew ){
  2598   2602           pNew->zName = (char *)&pNew[1];
  2599   2603           memcpy(pNew->zName, zName, nName+1);
  2600   2604       
................................................................................
  2835   2839         assert( sqlite3BtreeIsInTrans(pBt) );
  2836   2840         if( p->iStatement==0 ){
  2837   2841           assert( db->nStatement>=0 && db->nSavepoint>=0 );
  2838   2842           db->nStatement++; 
  2839   2843           p->iStatement = db->nSavepoint + db->nStatement;
  2840   2844         }
  2841   2845   
  2842         -      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement);
         2846  +      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
  2843   2847         if( rc==SQLITE_OK ){
  2844   2848           rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
  2845   2849         }
  2846   2850   
  2847   2851         /* Store the current value of the database handles deferred constraint
  2848   2852         ** counter. If the statement transaction needs to be rolled back,
  2849   2853         ** the value of this counter needs to be restored too.  */
................................................................................
  5875   5879   /* Opcode: Trace * * * P4 *
  5876   5880   **
  5877   5881   ** If tracing is enabled (by the sqlite3_trace()) interface, then
  5878   5882   ** the UTF-8 string contained in P4 is emitted on the trace callback.
  5879   5883   */
  5880   5884   case OP_Trace: {
  5881   5885     char *zTrace;
         5886  +  char *z;
  5882   5887   
  5883         -  zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
  5884         -  if( zTrace ){
  5885         -    if( db->xTrace ){
  5886         -      char *z = sqlite3VdbeExpandSql(p, zTrace);
  5887         -      db->xTrace(db->pTraceArg, z);
  5888         -      sqlite3DbFree(db, z);
  5889         -    }
         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  +  }
  5890   5893   #ifdef SQLITE_DEBUG
  5891         -    if( (db->flags & SQLITE_SqlTrace)!=0 ){
  5892         -      sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
  5893         -    }
  5894         -#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);
  5895   5898     }
         5899  +#endif /* SQLITE_DEBUG */
  5896   5900     break;
  5897   5901   }
  5898   5902   #endif
  5899   5903   
  5900   5904   
  5901   5905   /* Opcode: Noop * * * * *
  5902   5906   **

Changes to src/vdbe.h.

   181    181   void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
   182    182   void sqlite3VdbeUsesBtree(Vdbe*, int);
   183    183   VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
   184    184   int sqlite3VdbeMakeLabel(Vdbe*);
   185    185   void sqlite3VdbeRunOnlyOnce(Vdbe*);
   186    186   void sqlite3VdbeDelete(Vdbe*);
   187    187   void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
   188         -void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int,int,int);
          188  +void sqlite3VdbeMakeReady(Vdbe*,Parse*);
   189    189   int sqlite3VdbeFinalize(Vdbe*);
   190    190   void sqlite3VdbeResolveLabel(Vdbe*, int);
   191    191   int sqlite3VdbeCurrentAddr(Vdbe*);
   192    192   #ifdef SQLITE_DEBUG
   193    193     int sqlite3VdbeAssertMayAbort(Vdbe *, int);
   194    194     void sqlite3VdbeTrace(Vdbe*,FILE*);
   195    195   #endif
   196    196   void sqlite3VdbeResetStepResult(Vdbe*);
          197  +void sqlite3VdbeRewind(Vdbe*);
   197    198   int sqlite3VdbeReset(Vdbe*);
   198    199   void sqlite3VdbeSetNumCols(Vdbe*,int);
   199    200   int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
   200    201   void sqlite3VdbeCountChanges(Vdbe*);
   201    202   sqlite3 *sqlite3VdbeDb(Vdbe*);
   202    203   void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
   203    204   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.

   108    108     }else{
   109    109       Vdbe *v = (Vdbe*)pStmt;
   110    110   #ifdef SQLITE_ENABLE_SQLRR
   111    111       SRRecReset(pStmt);
   112    112   #endif
   113    113       sqlite3_mutex_enter(v->db->mutex);
   114    114       rc = sqlite3VdbeReset(v);
   115         -    sqlite3VdbeMakeReady(v, -1, 0, 0, 0, 0, 0);
          115  +    sqlite3VdbeRewind(v);
   116    116       assert( (rc & (v->db->errMask))==rc );
   117    117       rc = sqlite3ApiExit(v->db, rc);
   118    118       sqlite3_mutex_leave(v->db->mutex);
   119    119     }
   120    120     return rc;
   121    121   }
   122    122   
................................................................................
  1207   1207   ** This routine is added to support DBD::SQLite.  
  1208   1208   */
  1209   1209   int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
  1210   1210     Vdbe *p = (Vdbe*)pStmt;
  1211   1211     return p ? p->nVar : 0;
  1212   1212   }
  1213   1213   
  1214         -/*
  1215         -** Create a mapping from variable numbers to variable names
  1216         -** in the Vdbe.azVar[] array, if such a mapping does not already
  1217         -** exist.
  1218         -*/
  1219         -static void createVarMap(Vdbe *p){
  1220         -  if( !p->okVar ){
  1221         -    int j;
  1222         -    Op *pOp;
  1223         -    sqlite3_mutex_enter(p->db->mutex);
  1224         -    /* The race condition here is harmless.  If two threads call this
  1225         -    ** routine on the same Vdbe at the same time, they both might end
  1226         -    ** up initializing the Vdbe.azVar[] array.  That is a little extra
  1227         -    ** work but it results in the same answer.
  1228         -    */
  1229         -    for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
  1230         -      if( pOp->opcode==OP_Variable ){
  1231         -        assert( pOp->p1>0 && pOp->p1<=p->nVar );
  1232         -        p->azVar[pOp->p1-1] = pOp->p4.z;
  1233         -      }
  1234         -    }
  1235         -    p->okVar = 1;
  1236         -    sqlite3_mutex_leave(p->db->mutex);
  1237         -  }
  1238         -}
  1239         -
  1240   1214   /*
  1241   1215   ** Return the name of a wildcard parameter.  Return NULL if the index
  1242   1216   ** is out of range or if the wildcard is unnamed.
  1243   1217   **
  1244   1218   ** The result is always UTF-8.
  1245   1219   */
  1246   1220   const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
  1247   1221     Vdbe *p = (Vdbe*)pStmt;
  1248         -  if( p==0 || i<1 || i>p->nVar ){
         1222  +  if( p==0 || i<1 || i>p->nzVar ){
  1249   1223       return 0;
  1250   1224     }
  1251         -  createVarMap(p);
  1252   1225     return p->azVar[i-1];
  1253   1226   }
  1254   1227   
  1255   1228   /*
  1256   1229   ** Given a wildcard parameter name, return the index of the variable
  1257   1230   ** with that name.  If there is no variable with the given name,
  1258   1231   ** return 0.
  1259   1232   */
  1260   1233   int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
  1261   1234     int i;
  1262   1235     if( p==0 ){
  1263   1236       return 0;
  1264   1237     }
  1265         -  createVarMap(p); 
  1266   1238     if( zName ){
  1267         -    for(i=0; i<p->nVar; i++){
         1239  +    for(i=0; i<p->nzVar; i++){
  1268   1240         const char *z = p->azVar[i];
  1269   1241         if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){
  1270   1242           return i+1;
  1271   1243         }
  1272   1244       }
  1273   1245     }
  1274   1246     return 0;

Changes to src/vdbeaux.c.

  1387   1387     }else{
  1388   1388       *pnByte += nByte;
  1389   1389     }
  1390   1390     return pBuf;
  1391   1391   }
  1392   1392   
  1393   1393   /*
  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.
         1394  +** Rewind the VDBE back to the beginning in preparation for
         1395  +** running it.
  1409   1396   */
  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         -
         1397  +void sqlite3VdbeRewind(Vdbe *p){
         1398  +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
         1399  +  int i;
         1400  +#endif
  1422   1401     assert( p!=0 );
  1423   1402     assert( p->magic==VDBE_MAGIC_INIT );
  1424   1403   
  1425   1404     /* There should be at least one opcode.
  1426   1405     */
  1427   1406     assert( p->nOp>0 );
  1428   1407   
  1429   1408     /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
  1430   1409     p->magic = VDBE_MAGIC_RUN;
  1431   1410   
         1411  +#ifdef SQLITE_DEBUG
         1412  +  for(i=1; i<p->nMem; i++){
         1413  +    assert( p->aMem[i].db==p->db );
         1414  +  }
         1415  +#endif
         1416  +  p->pc = -1;
         1417  +  p->rc = SQLITE_OK;
         1418  +  p->errorAction = OE_Abort;
         1419  +  p->magic = VDBE_MAGIC_RUN;
         1420  +  p->nChange = 0;
         1421  +  p->cacheCtr = 1;
         1422  +  p->minWriteFileFormat = 255;
         1423  +  p->iStatement = 0;
         1424  +  p->nFkConstraint = 0;
         1425  +#ifdef VDBE_PROFILE
         1426  +  for(i=0; i<p->nOp; i++){
         1427  +    p->aOp[i].cnt = 0;
         1428  +    p->aOp[i].cycles = 0;
         1429  +  }
         1430  +#endif
         1431  +}
         1432  +
         1433  +/*
         1434  +** Prepare a virtual machine for execution for the first time after
         1435  +** creating the virtual machine.  This involves things such
         1436  +** as allocating stack space and initializing the program counter.
         1437  +** After the VDBE has be prepped, it can be executed by one or more
         1438  +** calls to sqlite3VdbeExec().  
         1439  +**
         1440  +** This function may be called exact once on a each virtual machine.
         1441  +** After this routine is called the VM has been "packaged" and is ready
         1442  +** to run.  After this routine is called, futher calls to 
         1443  +** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
         1444  +** the Vdbe from the Parse object that helped generate it so that the
         1445  +** the Vdbe becomes an independent entity and the Parse object can be
         1446  +** destroyed.
         1447  +**
         1448  +** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
         1449  +** to its initial state after it has been run.
         1450  +*/
         1451  +void sqlite3VdbeMakeReady(
         1452  +  Vdbe *p,                       /* The VDBE */
         1453  +  Parse *pParse                  /* Parsing context */
         1454  +){
         1455  +  sqlite3 *db;                   /* The database connection */
         1456  +  int nVar;                      /* Number of parameters */
         1457  +  int nMem;                      /* Number of VM memory registers */
         1458  +  int nCursor;                   /* Number of cursors required */
         1459  +  int nArg;                      /* Number of arguments in subprograms */
         1460  +  int n;                         /* Loop counter */
         1461  +  u8 *zCsr;                      /* Memory available for allocation */
         1462  +  u8 *zEnd;                      /* First byte past allocated memory */
         1463  +  int nByte;                     /* How much extra memory is needed */
         1464  +
         1465  +  assert( p!=0 );
         1466  +  assert( p->nOp>0 );
         1467  +  assert( pParse!=0 );
         1468  +  assert( p->magic==VDBE_MAGIC_INIT );
         1469  +  db = p->db;
         1470  +  assert( db->mallocFailed==0 );
         1471  +  nVar = pParse->nVar;
         1472  +  nMem = pParse->nMem;
         1473  +  nCursor = pParse->nTab;
         1474  +  nArg = pParse->nMaxArg;
         1475  +  
  1432   1476     /* For each cursor required, also allocate a memory cell. Memory
  1433   1477     ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
  1434   1478     ** the vdbe program. Instead they are used to allocate space for
  1435   1479     ** VdbeCursor/BtCursor structures. The blob of memory associated with 
  1436   1480     ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
  1437   1481     ** stores the blob of memory associated with cursor 1, etc.
  1438   1482     **
  1439   1483     ** See also: allocateCursor().
  1440   1484     */
  1441   1485     nMem += nCursor;
  1442   1486   
  1443   1487     /* 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
         1488  +  ** an array to marshal SQL function arguments in.
         1489  +  */
         1490  +  zCsr = (u8*)&p->aOp[p->nOp];       /* Memory avaliable for allocation */
         1491  +  zEnd = (u8*)&p->aOp[p->nOpAlloc];  /* First byte past end of zCsr[] */
         1492  +
         1493  +  resolveP2Values(p, &nArg);
         1494  +  p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
         1495  +  if( pParse->explain && nMem<10 ){
         1496  +    nMem = 10;
         1497  +  }
         1498  +  memset(zCsr, 0, zEnd-zCsr);
         1499  +  zCsr += (zCsr - (u8*)0)&7;
         1500  +  assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
         1501  +
         1502  +  /* Memory for registers, parameters, cursor, etc, is allocated in two
         1503  +  ** passes.  On the first pass, we try to reuse unused space at the 
         1504  +  ** end of the opcode array.  If we are unable to satisfy all memory
         1505  +  ** requirements by reusing the opcode array tail, then the second
         1506  +  ** pass will fill in the rest using a fresh allocation.  
         1507  +  **
         1508  +  ** This two-pass approach that reuses as much memory as possible from
         1509  +  ** the leftover space at the end of the opcode array can significantly
         1510  +  ** reduce the amount of memory held by a prepared statement.
         1511  +  */
         1512  +  do {
         1513  +    nByte = 0;
         1514  +    p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
         1515  +    p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
         1516  +    p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
         1517  +    p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
         1518  +    p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
         1519  +                          &zCsr, zEnd, &nByte);
         1520  +    if( nByte ){
         1521  +      p->pFree = sqlite3DbMallocZero(db, nByte);
         1522  +    }
         1523  +    zCsr = p->pFree;
         1524  +    zEnd = &zCsr[nByte];
         1525  +  }while( nByte && !db->mallocFailed );
         1526  +
         1527  +  p->nCursor = (u16)nCursor;
         1528  +  if( p->aVar ){
         1529  +    p->nVar = (ynVar)nVar;
         1530  +    for(n=0; n<nVar; n++){
         1531  +      p->aVar[n].flags = MEM_Null;
         1532  +      p->aVar[n].db = db;
         1533  +    }
         1534  +  }
         1535  +  if( p->azVar ){
         1536  +    p->nzVar = pParse->nzVar;
         1537  +    memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
         1538  +    memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
         1539  +  }
         1540  +  if( p->aMem ){
         1541  +    p->aMem--;                      /* aMem[] goes from 1..nMem */
         1542  +    p->nMem = nMem;                 /*       not from 0..nMem-1 */
         1543  +    for(n=1; n<=nMem; n++){
         1544  +      p->aMem[n].flags = MEM_Null;
         1545  +      p->aMem[n].db = db;
         1546  +    }
         1547  +  }
         1548  +  p->explain = pParse->explain;
         1549  +  sqlite3VdbeRewind(p);
  1529   1550   }
  1530   1551   
  1531   1552   /*
  1532   1553   ** Close a VDBE cursor and release all the resources that cursor 
  1533   1554   ** happens to hold.
  1534   1555   */
  1535   1556   void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
................................................................................
  2198   2219       }
  2199   2220     
  2200   2221       /* If eStatementOp is non-zero, then a statement transaction needs to
  2201   2222       ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
  2202   2223       ** do so. If this operation returns an error, and the current statement
  2203   2224       ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
  2204   2225       ** current statement error code.
  2205         -    **
  2206         -    ** Note that sqlite3VdbeCloseStatement() can only fail if eStatementOp
  2207         -    ** is SAVEPOINT_ROLLBACK.  But if p->rc==SQLITE_OK then eStatementOp
  2208         -    ** must be SAVEPOINT_RELEASE.  Hence the NEVER(p->rc==SQLITE_OK) in 
  2209         -    ** the following code.
  2210   2226       */
  2211   2227       if( eStatementOp ){
  2212   2228         rc = sqlite3VdbeCloseStatement(p, eStatementOp);
  2213   2229         if( rc ){
  2214         -        assert( eStatementOp==SAVEPOINT_ROLLBACK );
  2215         -        if( NEVER(p->rc==SQLITE_OK) || p->rc==SQLITE_CONSTRAINT ){
         2230  +        if( p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ){
  2216   2231             p->rc = rc;
  2217   2232             sqlite3DbFree(db, p->zErrMsg);
  2218   2233             p->zErrMsg = 0;
  2219   2234           }
  2220   2235           invalidateCursorsOnModifiedBtrees(db);
  2221   2236           sqlite3RollbackAll(db);
  2222   2237           sqlite3CloseSavepoints(db);
................................................................................
  2401   2416   ** Free all memory associated with the Vdbe passed as the second argument.
  2402   2417   ** The difference between this function and sqlite3VdbeDelete() is that
  2403   2418   ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
  2404   2419   ** the database connection.
  2405   2420   */
  2406   2421   void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
  2407   2422     SubProgram *pSub, *pNext;
         2423  +  int i;
  2408   2424     assert( p->db==0 || p->db==db );
  2409   2425     releaseMemArray(p->aVar, p->nVar);
  2410   2426     releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
  2411   2427     for(pSub=p->pProgram; pSub; pSub=pNext){
  2412   2428       pNext = pSub->pNext;
  2413   2429       vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
  2414   2430       sqlite3DbFree(db, pSub);
  2415   2431     }
         2432  +  for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
  2416   2433     vdbeFreeOpArray(db, p->aOp, p->nOp);
  2417   2434     sqlite3DbFree(db, p->aLabel);
  2418   2435     sqlite3DbFree(db, p->aColName);
  2419   2436     sqlite3DbFree(db, p->zSql);
  2420   2437     sqlite3DbFree(db, p->pFree);
  2421   2438     sqlite3DbFree(db, p);
  2422   2439   }
................................................................................
  2854   2871     u = 0;
  2855   2872     while( idx<szHdr && u<p->nField && d<=nKey ){
  2856   2873       u32 serial_type;
  2857   2874   
  2858   2875       idx += getVarint32(&aKey[idx], serial_type);
  2859   2876       pMem->enc = pKeyInfo->enc;
  2860   2877       pMem->db = pKeyInfo->db;
  2861         -    pMem->flags = 0;
         2878  +    /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
  2862   2879       pMem->zMalloc = 0;
  2863   2880       d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
  2864   2881       pMem++;
  2865   2882       u++;
  2866   2883     }
  2867   2884     assert( u<=pKeyInfo->nField + 1 );
  2868   2885     p->nField = u;
................................................................................
  2869   2886     return (void*)p;
  2870   2887   }
  2871   2888   
  2872   2889   /*
  2873   2890   ** This routine destroys a UnpackedRecord object.
  2874   2891   */
  2875   2892   void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
         2893  +#ifdef SQLITE_DEBUG
  2876   2894     int i;
  2877   2895     Mem *pMem;
  2878   2896   
  2879   2897     assert( p!=0 );
  2880   2898     assert( p->flags & UNPACKED_NEED_DESTROY );
  2881   2899     for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
  2882   2900       /* The unpacked record is always constructed by the
  2883   2901       ** sqlite3VdbeUnpackRecord() function above, which makes all
  2884   2902       ** strings and blobs static.  And none of the elements are
  2885   2903       ** ever transformed, so there is never anything to delete.
  2886   2904       */
  2887   2905       if( NEVER(pMem->zMalloc) ) sqlite3VdbeMemRelease(pMem);
  2888   2906     }
         2907  +#endif
  2889   2908     if( p->flags & UNPACKED_NEED_FREE ){
  2890   2909       sqlite3DbFree(p->pKeyInfo->db, p);
  2891   2910     }
  2892   2911   }
  2893   2912   
  2894   2913   /*
  2895   2914   ** 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.

   573    573         sqlite3ErrorMsg(pParse, "%s", zErr);
   574    574       }
   575    575       sqlite3DbFree(db, zErr);
   576    576     }
   577    577   
   578    578     return rc;
   579    579   }
   580         -
   581    580   /*
   582         -** Add the virtual table pVTab to the array sqlite3.aVTrans[].
          581  +** Grow the db->aVTrans[] array so that there is room for at least one
          582  +** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
   583    583   */
   584         -static int addToVTrans(sqlite3 *db, VTable *pVTab){
          584  +static int growVTrans(sqlite3 *db){
   585    585     const int ARRAY_INCR = 5;
   586    586   
   587    587     /* Grow the sqlite3.aVTrans array if required */
   588    588     if( (db->nVTrans%ARRAY_INCR)==0 ){
   589    589       VTable **aVTrans;
   590    590       int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
   591    591       aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
................................................................................
   592    592       if( !aVTrans ){
   593    593         return SQLITE_NOMEM;
   594    594       }
   595    595       memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
   596    596       db->aVTrans = aVTrans;
   597    597     }
   598    598   
          599  +  return SQLITE_OK;
          600  +}
          601  +
          602  +/*
          603  +** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
          604  +** have already been reserved using growVTrans().
          605  +*/
          606  +static void addToVTrans(sqlite3 *db, VTable *pVTab){
   599    607     /* Add pVtab to the end of sqlite3.aVTrans */
   600    608     db->aVTrans[db->nVTrans++] = pVTab;
   601    609     sqlite3VtabLock(pVTab);
   602         -  return SQLITE_OK;
   603    610   }
   604    611   
   605    612   /*
   606    613   ** This function is invoked by the vdbe to call the xCreate method
   607    614   ** of the virtual table named zTab in database iDb. 
   608    615   **
   609    616   ** If an error occurs, *pzErr is set to point an an English language
................................................................................
   633    640     }else{
   634    641       rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
   635    642     }
   636    643   
   637    644     /* Justification of ALWAYS():  The xConstructor method is required to
   638    645     ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
   639    646     if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
   640         -      rc = addToVTrans(db, sqlite3GetVTable(db, pTab));
          647  +    rc = growVTrans(db);
          648  +    if( rc==SQLITE_OK ){
          649  +      addToVTrans(db, sqlite3GetVTable(db, pTab));
          650  +    }
   641    651     }
   642    652   
   643    653     return rc;
   644    654   }
   645    655   
   646    656   /*
   647    657   ** This function is used to set the schema of a virtual table.  It is only
................................................................................
   749    759         VTable *pVTab = db->aVTrans[i];
   750    760         sqlite3_vtab *p = pVTab->pVtab;
   751    761         if( p ){
   752    762           int (*x)(sqlite3_vtab *);
   753    763           x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
   754    764           if( x ) x(p);
   755    765         }
          766  +      pVTab->iSavepoint = 0;
   756    767         sqlite3VtabUnlock(pVTab);
   757    768       }
   758    769       sqlite3DbFree(db, db->aVTrans);
   759    770       db->nVTrans = 0;
   760    771       db->aVTrans = 0;
   761    772     }
   762    773   }
................................................................................
   838    849       /* If pVtab is already in the aVTrans array, return early */
   839    850       for(i=0; i<db->nVTrans; i++){
   840    851         if( db->aVTrans[i]==pVTab ){
   841    852           return SQLITE_OK;
   842    853         }
   843    854       }
   844    855   
   845         -    /* Invoke the xBegin method */
   846         -    rc = pModule->xBegin(pVTab->pVtab);
          856  +    /* Invoke the xBegin method. If successful, add the vtab to the 
          857  +    ** sqlite3.aVTrans[] array. */
          858  +    rc = growVTrans(db);
   847    859       if( rc==SQLITE_OK ){
   848         -      rc = addToVTrans(db, pVTab);
          860  +      rc = pModule->xBegin(pVTab->pVtab);
          861  +      if( rc==SQLITE_OK ){
          862  +        addToVTrans(db, pVTab);
          863  +      }
   849    864       }
   850    865     }
   851    866     return rc;
   852    867   }
   853    868   
   854    869   /*
   855    870   ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
................................................................................
   866    881   ** function immediately. If all calls to virtual table methods are successful,
   867    882   ** SQLITE_OK is returned.
   868    883   */
   869    884   int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
   870    885     int rc = SQLITE_OK;
   871    886   
   872    887     assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
          888  +  assert( iSavepoint>=0 );
   873    889     if( db->aVTrans ){
   874    890       int i;
   875    891       for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
   876         -      const sqlite3_module *pMod = db->aVTrans[i]->pMod->pModule;
          892  +      VTable *pVTab = db->aVTrans[i];
          893  +      const sqlite3_module *pMod = pVTab->pMod->pModule;
   877    894         if( pMod->iVersion>=2 ){
   878    895           int (*xMethod)(sqlite3_vtab *, int);
   879    896           switch( op ){
   880    897             case SAVEPOINT_BEGIN:
   881    898               xMethod = pMod->xSavepoint;
          899  +            pVTab->iSavepoint = iSavepoint+1;
   882    900               break;
   883    901             case SAVEPOINT_ROLLBACK:
   884    902               xMethod = pMod->xRollbackTo;
   885    903               break;
   886    904             default:
   887    905               xMethod = pMod->xRelease;
   888    906               break;
   889    907           }
   890         -        if( xMethod ) rc = xMethod(db->aVTrans[i]->pVtab, iSavepoint);
          908  +        if( xMethod && pVTab->iSavepoint>iSavepoint ){
          909  +          rc = xMethod(db->aVTrans[i]->pVtab, iSavepoint);
          910  +        }
   891    911         }
   892    912       }
   893    913     }
   894    914     return rc;
   895    915   }
   896    916   
   897    917   /*

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   2155         assert( rc==SQLITE_BUSY );
  2136         -      return WAL_RETRY;
         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.

    13     13   # been resolved.
    14     14   #
    15     15   # 
    16     16   #
    17     17   
    18     18   set testdir [file dirname $argv0]
    19     19   source $testdir/tester.tcl
           20  +set testprefix tkt-2d1a5c67d
    20     21   
    21         -ifcapable !wal {finish_test; return}
           22  +ifcapable {!wal || !vtab} {finish_test; return}
    22     23   
    23     24   for {set ii 1} {$ii<=10} {incr ii} {
    24     25     do_test tkt-2d1a5c67d.1.$ii {
    25     26       db close
    26     27       forcedelete test.db test.db-wal
    27     28       sqlite3 db test.db
    28     29       db eval "PRAGMA cache_size=$::ii"
................................................................................
    65     66         INSERT INTO t1(a,b) VALUES(1,2);
    66     67         SELECT sum(length(y)) FROM t2;
    67     68         COMMIT;
    68     69         SELECT * FROM t1;
    69     70       }
    70     71     } {1000000 1 2}
    71     72   }
           73  +
           74  +db close
           75  +sqlite3 db test.db
           76  +
           77  +
           78  +do_execsql_test 3.1 {
           79  +  PRAGMA cache_size = 10;
           80  +  CREATE TABLE t3(a INTEGER PRIMARY KEY, b);
           81  +  CREATE TABLE t4(a);
           82  +}
           83  +
           84  +do_execsql_test 3.2 {
           85  +  INSERT INTO t3 VALUES(NULL, randomblob(500));
           86  +  INSERT INTO t3 SELECT NULL, b||b FROM t3;     -- 2
           87  +  INSERT INTO t3 SELECT NULL, b||b FROM t3;     -- 4
           88  +  INSERT INTO t3 SELECT NULL, b||b FROM t3;     -- 8
           89  +  INSERT INTO t3 SELECT NULL, b||b FROM t3;     -- 16
           90  +  INSERT INTO t3 SELECT NULL, b||b FROM t3;     -- 32
           91  +  INSERT INTO t3 SELECT NULL, b||b FROM t3;     -- 64
           92  +  INSERT INTO t3 SELECT NULL, b||b FROM t3;     -- 128
           93  +}
           94  +
           95  +do_execsql_test 3.3 {
           96  +  BEGIN;
           97  +    INSERT INTO t4 VALUES('xyz');
           98  +}
           99  +
          100  +do_test 3.4 {
          101  +  set blobs [list]
          102  +  for {set i 1} {$i<100} {incr i} {
          103  +    set b [db incrblob -readonly t3 b $i]
          104  +    read $b
          105  +    lappend blobs $b
          106  +  }
          107  +
          108  +  execsql COMMIT
          109  +  execsql { SELECT * FROM t4 WHERE a = 'xyz' }
          110  +} {xyz}
          111  +
          112  +do_test 3.5 {
          113  +  foreach b $blobs { close $b }
          114  +  execsql { SELECT * FROM t4 WHERE a = 'xyz' }
          115  +} {xyz}
          116  +
          117  +# Check that recovery works on the WAL file.
          118  +#
          119  +forcedelete test.db2-wal test.db2
          120  +do_test 3.6 {
          121  +  file copy test.db-wal test.db2-wal
          122  +  file copy test.db test.db2
          123  +  sqlite3 db2 test.db2
          124  +  execsql { SELECT * FROM t4 WHERE a = 'xyz' } db2
          125  +} {xyz}
    72    126   
    73    127   finish_test

Changes to test/uri.test.

    42     42     7      file:test.db?mork=1#boris            test.db
    43     43     8      file:test.db#boris                   test.db
    44     44     9      test.db#boris                        test.db#boris
    45     45     10     file:test%2Edb                       test.db
    46     46     11     file                                 file
    47     47     12     http:test.db                         http:test.db
    48     48     13     file:test.db%00extra                 test.db
    49         -  14     file:test%00.db%00extra              test
           49  +  14     file:testdb%00.db%00extra            testdb
    50     50   
    51     51     15     test.db?mork=1#boris                 test.db?mork=1#boris
    52     52     16     file://localhostPWD/test.db%3Fhello  test.db?hello
    53     53   } {
    54     54     
    55     55     if {$tcl_platform(platform)=="windows"} {
    56     56       if {$tn>14} break
    57     57       set uri  [string map [list PWD /[pwd]] $uri]
    58     58     } else {
    59     59       set uri  [string map [list PWD [pwd]] $uri]
    60     60     }
    61     61   
           62  +  if {[file isdir $file]} {error "$file is a directory"}
    62     63     forcedelete $file
    63     64     do_test 1.$tn.1 { file exists $file } 0
    64     65     set DB [sqlite3_open $uri]
    65     66     do_test 1.$tn.2 { file exists $file } 1
    66     67     sqlite3_close $DB
    67     68     forcedelete $file
    68     69   
................................................................................
    86     87   }
    87     88   foreach {tn uri kvlist} {
    88     89     1      file:test.db?hello=world                     {hello world}
    89     90     2      file:test.db?hello&world                     {hello {} world {}}
    90     91     3      file:test.db?hello=1&world=2&vfs=tvfs        {hello 1 world 2 vfs tvfs}
    91     92     4      file:test.db?hello=1&world=2&vfs=tvfs2        {}
    92     93     5      file:test.db?%68%65%6C%6C%6F=%77%6F%72%6C%64 {hello world}
    93         -  6      file:test%00.db?hello%00extra=world%00ex     {hello world}
    94         -  7      file:test%00.db?hello%00=world%00            {hello world}
    95         -  8      file:test%00.db?=world&xyz=abc               {xyz abc}
           94  +  6      file:testdb%00.db?hello%00extra=world%00ex   {hello world}
           95  +  7      file:testdb%00.db?hello%00=world%00          {hello world}
           96  +  8      file:testdb%00.db?=world&xyz=abc             {xyz abc}
    96     97     9      file:test.db?%00hello=world&xyz=abc          {xyz abc}
    97     98     10     file:test.db?hello=%00world&xyz=             {hello {} xyz {}}
    98     99     11     file:test.db?=#ravada                        {}
    99    100     12     file:test.db?&&&&&&&&hello=world&&&&&&&      {hello world}
   100    101   
   101    102     13     test.db?&&&&&&&&hello=world&&&&&&&           {}
   102    103     14     http:test.db?hello&world                     {}

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