Fossil

Check-in [0d120a61]
Login

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

Overview
Comment:Update the built-in SQLite to a newer version that fixes C89-isms in FTS5.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:0d120a6102c662b469f4ac44c199bdfbfb531ec1
User & Date: drh 2016-01-19 01:37:24
Context
2016-01-19
18:48
Get configure and make, via autosetup, working on MinGW. check-in: a0061bb2 user: mistachkin tags: trunk
01:37
Update the built-in SQLite to a newer version that fixes C89-isms in FTS5. check-in: 0d120a61 user: drh tags: trunk
2016-01-18
23:46
Fix to the test-content-erase command so that it correctly undeltafies. check-in: d0cabcb6 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/sqlite3.c.

   325    325   **
   326    326   ** See also: [sqlite3_libversion()],
   327    327   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   328    328   ** [sqlite_version()] and [sqlite_source_id()].
   329    329   */
   330    330   #define SQLITE_VERSION        "3.11.0"
   331    331   #define SQLITE_VERSION_NUMBER 3011000
   332         -#define SQLITE_SOURCE_ID      "2016-01-14 14:19:50 d17bc2c92f4d086280e49a3cc72993be7fee2da7"
          332  +#define SQLITE_SOURCE_ID      "2016-01-18 17:48:28 acaf426449bf6fd3140fd63141750ff69d1119a5"
   333    333   
   334    334   /*
   335    335   ** CAPI3REF: Run-Time Library Version Numbers
   336    336   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   337    337   **
   338    338   ** These interfaces provide the same information as the [SQLITE_VERSION],
   339    339   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  9404   9404   # define ALWAYS(X)      ((X)?1:(assert(0),0))
  9405   9405   # define NEVER(X)       ((X)?(assert(0),1):0)
  9406   9406   #else
  9407   9407   # define ALWAYS(X)      (X)
  9408   9408   # define NEVER(X)       (X)
  9409   9409   #endif
  9410   9410   
         9411  +/*
         9412  +** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
         9413  +** defined.  We need to defend against those failures when testing with
         9414  +** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
         9415  +** during a normal build.  The following macro can be used to disable tests
         9416  +** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
         9417  +*/
         9418  +#if defined(SQLITE_TEST_REALLOC_STRESS)
         9419  +# define ONLY_IF_REALLOC_STRESS(X)  (X)
         9420  +#elif !defined(NDEBUG)
         9421  +# define ONLY_IF_REALLOC_STRESS(X)  ((X)?(assert(0),1):0)
         9422  +#else
         9423  +# define ONLY_IF_REALLOC_STRESS(X)  (0)
         9424  +#endif
         9425  +
  9411   9426   /*
  9412   9427   ** Declarations used for tracing the operating system interfaces.
  9413   9428   */
  9414   9429   #if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
  9415   9430       (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
  9416   9431     extern int sqlite3OSTrace;
  9417   9432   # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
................................................................................
 10959  10974   SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe*,int);
 10960  10975   SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe*,int,const char*);
 10961  10976   SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
 10962  10977   SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
 10963  10978   SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
 10964  10979   SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
 10965  10980   SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
 10966         -SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
        10981  +#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
        10982  +SQLITE_PRIVATE   void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
        10983  +#else
        10984  +# define sqlite3VdbeVerifyNoMallocRequired(A,B)
        10985  +#endif
        10986  +SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
 10967  10987   SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
 10968  10988   SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
 10969  10989   SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
 10970  10990   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
 10971  10991   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
 10972  10992   SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
 10973  10993   SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
 10974         -SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
        10994  +SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
 10975  10995   SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
 10976  10996   SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
 10977  10997   SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
 10978  10998   SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
 10979  10999   SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
 10980  11000   SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
 10981  11001   SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
................................................................................
 12225  12245   ** points to a linked list of these structures.
 12226  12246   */
 12227  12247   struct FuncDef {
 12228  12248     i16 nArg;            /* Number of arguments.  -1 means unlimited */
 12229  12249     u16 funcFlags;       /* Some combination of SQLITE_FUNC_* */
 12230  12250     void *pUserData;     /* User data parameter */
 12231  12251     FuncDef *pNext;      /* Next function with same name */
 12232         -  void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
 12233         -  void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
 12234         -  void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
        12252  +  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
        12253  +  void (*xFinalize)(sqlite3_context*);                  /* Agg finalizer */
 12235  12254     char *zName;         /* SQL name of the function. */
 12236  12255     FuncDef *pHash;      /* Next with a different name but the same hash */
 12237  12256     FuncDestructor *pDestructor;   /* Reference counted destructor function */
 12238  12257   };
 12239  12258   
 12240  12259   /*
 12241  12260   ** This structure encapsulates a user-function destructor callback (as
................................................................................
 12310  12329   **     function likeFunc. Argument pArg is cast to a (void *) and made
 12311  12330   **     available as the function user-data (sqlite3_user_data()). The
 12312  12331   **     FuncDef.flags variable is set to the value passed as the flags
 12313  12332   **     parameter.
 12314  12333   */
 12315  12334   #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
 12316  12335     {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
 12317         -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
        12336  +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
 12318  12337   #define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
 12319  12338     {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
 12320         -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
        12339  +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
 12321  12340   #define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
 12322  12341     {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
 12323         -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
        12342  +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
 12324  12343   #define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
 12325  12344     {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
 12326         -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
        12345  +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
 12327  12346   #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
 12328  12347     {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
 12329         -   pArg, 0, xFunc, 0, 0, #zName, 0, 0}
        12348  +   pArg, 0, xFunc, 0, #zName, 0, 0}
 12330  12349   #define LIKEFUNC(zName, nArg, arg, flags) \
 12331  12350     {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
 12332         -   (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
        12351  +   (void *)arg, 0, likeFunc, 0, #zName, 0, 0}
 12333  12352   #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
 12334  12353     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
 12335         -   SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
        12354  +   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName,0,0}
 12336  12355   #define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \
 12337  12356     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
 12338         -   SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
        12357  +   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName,0,0}
 12339  12358   
 12340  12359   /*
 12341  12360   ** All current savepoints are stored in a linked list starting at
 12342  12361   ** sqlite3.pSavepoint. The first element in the list is the most recently
 12343  12362   ** opened savepoint. Savepoints are added to the list by the vdbe
 12344  12363   ** OP_Savepoint instruction.
 12345  12364   */
................................................................................
 16574  16593     char validJD;      /* True (1) if iJD is valid */
 16575  16594     char validTZ;      /* True (1) if tz is valid */
 16576  16595     char tzSet;        /* Timezone was set explicitly */
 16577  16596   };
 16578  16597   
 16579  16598   
 16580  16599   /*
 16581         -** Convert zDate into one or more integers.  Additional arguments
 16582         -** come in groups of 5 as follows:
        16600  +** Convert zDate into one or more integers according to the conversion
        16601  +** specifier zFormat.
 16583  16602   **
 16584         -**       N       number of digits in the integer
 16585         -**       min     minimum allowed value of the integer
 16586         -**       max     maximum allowed value of the integer
 16587         -**       nextC   first character after the integer
 16588         -**       pVal    where to write the integers value.
        16603  +** zFormat[] contains 4 characters for each integer converted, except for
        16604  +** the last integer which is specified by three characters.  The meaning
        16605  +** of a four-character format specifiers ABCD is:
 16589  16606   **
 16590         -** Conversions continue until one with nextC==0 is encountered.
        16607  +**    A:   number of digits to convert.  Always "2" or "4".
        16608  +**    B:   minimum value.  Always "0" or "1".
        16609  +**    C:   maximum value, decoded as:
        16610  +**           a:  12
        16611  +**           b:  14
        16612  +**           c:  24
        16613  +**           d:  31
        16614  +**           e:  59
        16615  +**           f:  9999
        16616  +**    D:   the separator character, or \000 to indicate this is the
        16617  +**         last number to convert.
        16618  +**
        16619  +** Example:  To translate an ISO-8601 date YYYY-MM-DD, the format would
        16620  +** be "40f-21a-20c".  The "40f-" indicates the 4-digit year followed by "-".
        16621  +** The "21a-" indicates the 2-digit month followed by "-".  The "20c" indicates
        16622  +** the 2-digit day which is the last integer in the set.
        16623  +**
 16591  16624   ** The function returns the number of successful conversions.
 16592  16625   */
 16593         -static int getDigits(const char *zDate, ...){
        16626  +static int getDigits(const char *zDate, const char *zFormat, ...){
        16627  +  /* The aMx[] array translates the 3rd character of each format
        16628  +  ** spec into a max size:    a   b   c   d   e     f */
        16629  +  static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 };
 16594  16630     va_list ap;
 16595         -  int val;
 16596         -  int N;
 16597         -  int min;
 16598         -  int max;
 16599         -  int nextC;
 16600         -  int *pVal;
 16601  16631     int cnt = 0;
 16602         -  va_start(ap, zDate);
        16632  +  char nextC;
        16633  +  va_start(ap, zFormat);
 16603  16634     do{
 16604         -    N = va_arg(ap, int);
 16605         -    min = va_arg(ap, int);
 16606         -    max = va_arg(ap, int);
 16607         -    nextC = va_arg(ap, int);
 16608         -    pVal = va_arg(ap, int*);
        16635  +    char N = zFormat[0] - '0';
        16636  +    char min = zFormat[1] - '0';
        16637  +    int val = 0;
        16638  +    u16 max;
        16639  +
        16640  +    assert( zFormat[2]>='a' && zFormat[2]<='f' );
        16641  +    max = aMx[zFormat[2] - 'a'];
        16642  +    nextC = zFormat[3];
 16609  16643       val = 0;
 16610  16644       while( N-- ){
 16611  16645         if( !sqlite3Isdigit(*zDate) ){
 16612  16646           goto end_getDigits;
 16613  16647         }
 16614  16648         val = val*10 + *zDate - '0';
 16615  16649         zDate++;
 16616  16650       }
 16617         -    if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
        16651  +    if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
 16618  16652         goto end_getDigits;
 16619  16653       }
 16620         -    *pVal = val;
        16654  +    *va_arg(ap,int*) = val;
 16621  16655       zDate++;
 16622  16656       cnt++;
        16657  +    zFormat += 4;
 16623  16658     }while( nextC );
 16624  16659   end_getDigits:
 16625  16660     va_end(ap);
 16626  16661     return cnt;
 16627  16662   }
 16628  16663   
 16629  16664   /*
................................................................................
 16656  16691     }else if( c=='Z' || c=='z' ){
 16657  16692       zDate++;
 16658  16693       goto zulu_time;
 16659  16694     }else{
 16660  16695       return c!=0;
 16661  16696     }
 16662  16697     zDate++;
 16663         -  if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
        16698  +  if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
 16664  16699       return 1;
 16665  16700     }
 16666  16701     zDate += 5;
 16667  16702     p->tz = sgn*(nMn + nHr*60);
 16668  16703   zulu_time:
 16669  16704     while( sqlite3Isspace(*zDate) ){ zDate++; }
 16670  16705     p->tzSet = 1;
................................................................................
 16677  16712   ** fractional seconds FFFF can be one or more digits.
 16678  16713   **
 16679  16714   ** Return 1 if there is a parsing error and 0 on success.
 16680  16715   */
 16681  16716   static int parseHhMmSs(const char *zDate, DateTime *p){
 16682  16717     int h, m, s;
 16683  16718     double ms = 0.0;
 16684         -  if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
        16719  +  if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){
 16685  16720       return 1;
 16686  16721     }
 16687  16722     zDate += 5;
 16688  16723     if( *zDate==':' ){
 16689  16724       zDate++;
 16690         -    if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
        16725  +    if( getDigits(zDate, "20e", &s)!=1 ){
 16691  16726         return 1;
 16692  16727       }
 16693  16728       zDate += 2;
 16694  16729       if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
 16695  16730         double rScale = 1.0;
 16696  16731         zDate++;
 16697  16732         while( sqlite3Isdigit(*zDate) ){
................................................................................
 16771  16806   
 16772  16807     if( zDate[0]=='-' ){
 16773  16808       zDate++;
 16774  16809       neg = 1;
 16775  16810     }else{
 16776  16811       neg = 0;
 16777  16812     }
 16778         -  if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
        16813  +  if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
 16779  16814       return 1;
 16780  16815     }
 16781  16816     zDate += 10;
 16782  16817     while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
 16783  16818     if( parseHhMmSs(zDate, p)==0 ){
 16784  16819       /* We got the time */
 16785  16820     }else if( *zDate==0 ){
................................................................................
 63841  63876     **
 63842  63877     ** This error is caught long before control reaches this point.
 63843  63878     */
 63844  63879     if( NEVER(pBt->pCursor) ){
 63845  63880       sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
 63846  63881       return SQLITE_LOCKED_SHAREDCACHE;
 63847  63882     }
        63883  +
        63884  +  /*
        63885  +  ** It is illegal to drop the sqlite_master table on page 1.  But again,
        63886  +  ** this error is caught long before reaching this point.
        63887  +  */
        63888  +  if( NEVER(iTable<2) ){
        63889  +    return SQLITE_CORRUPT_BKPT;
        63890  +  }
 63848  63891   
 63849  63892     rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
 63850  63893     if( rc ) return rc;
 63851  63894     rc = sqlite3BtreeClearTable(p, iTable, 0);
 63852  63895     if( rc ){
 63853  63896       releasePage(pPage);
 63854  63897       return rc;
 63855  63898     }
 63856  63899   
 63857  63900     *piMoved = 0;
 63858  63901   
 63859         -  if( iTable>1 ){
 63860  63902   #ifdef SQLITE_OMIT_AUTOVACUUM
 63861         -    freePage(pPage, &rc);
 63862         -    releasePage(pPage);
 63863         -#else
 63864         -    if( pBt->autoVacuum ){
 63865         -      Pgno maxRootPgno;
 63866         -      sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
 63867         -
 63868         -      if( iTable==maxRootPgno ){
 63869         -        /* If the table being dropped is the table with the largest root-page
 63870         -        ** number in the database, put the root page on the free list. 
 63871         -        */
 63872         -        freePage(pPage, &rc);
 63873         -        releasePage(pPage);
 63874         -        if( rc!=SQLITE_OK ){
 63875         -          return rc;
 63876         -        }
 63877         -      }else{
 63878         -        /* The table being dropped does not have the largest root-page
 63879         -        ** number in the database. So move the page that does into the 
 63880         -        ** gap left by the deleted root-page.
 63881         -        */
 63882         -        MemPage *pMove;
 63883         -        releasePage(pPage);
 63884         -        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
 63885         -        if( rc!=SQLITE_OK ){
 63886         -          return rc;
 63887         -        }
 63888         -        rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
 63889         -        releasePage(pMove);
 63890         -        if( rc!=SQLITE_OK ){
 63891         -          return rc;
 63892         -        }
 63893         -        pMove = 0;
 63894         -        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
 63895         -        freePage(pMove, &rc);
 63896         -        releasePage(pMove);
 63897         -        if( rc!=SQLITE_OK ){
 63898         -          return rc;
 63899         -        }
 63900         -        *piMoved = maxRootPgno;
 63901         -      }
 63902         -
 63903         -      /* Set the new 'max-root-page' value in the database header. This
 63904         -      ** is the old value less one, less one more if that happens to
 63905         -      ** be a root-page number, less one again if that is the
 63906         -      ** PENDING_BYTE_PAGE.
 63907         -      */
 63908         -      maxRootPgno--;
 63909         -      while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
 63910         -             || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
 63911         -        maxRootPgno--;
 63912         -      }
 63913         -      assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
 63914         -
 63915         -      rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
 63916         -    }else{
        63903  +  freePage(pPage, &rc);
        63904  +  releasePage(pPage);
        63905  +#else
        63906  +  if( pBt->autoVacuum ){
        63907  +    Pgno maxRootPgno;
        63908  +    sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
        63909  +
        63910  +    if( iTable==maxRootPgno ){
        63911  +      /* If the table being dropped is the table with the largest root-page
        63912  +      ** number in the database, put the root page on the free list. 
        63913  +      */
 63917  63914         freePage(pPage, &rc);
 63918  63915         releasePage(pPage);
        63916  +      if( rc!=SQLITE_OK ){
        63917  +        return rc;
        63918  +      }
        63919  +    }else{
        63920  +      /* The table being dropped does not have the largest root-page
        63921  +      ** number in the database. So move the page that does into the 
        63922  +      ** gap left by the deleted root-page.
        63923  +      */
        63924  +      MemPage *pMove;
        63925  +      releasePage(pPage);
        63926  +      rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
        63927  +      if( rc!=SQLITE_OK ){
        63928  +        return rc;
        63929  +      }
        63930  +      rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
        63931  +      releasePage(pMove);
        63932  +      if( rc!=SQLITE_OK ){
        63933  +        return rc;
        63934  +      }
        63935  +      pMove = 0;
        63936  +      rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
        63937  +      freePage(pMove, &rc);
        63938  +      releasePage(pMove);
        63939  +      if( rc!=SQLITE_OK ){
        63940  +        return rc;
        63941  +      }
        63942  +      *piMoved = maxRootPgno;
 63919  63943       }
 63920         -#endif
        63944  +
        63945  +    /* Set the new 'max-root-page' value in the database header. This
        63946  +    ** is the old value less one, less one more if that happens to
        63947  +    ** be a root-page number, less one again if that is the
        63948  +    ** PENDING_BYTE_PAGE.
        63949  +    */
        63950  +    maxRootPgno--;
        63951  +    while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
        63952  +           || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
        63953  +      maxRootPgno--;
        63954  +    }
        63955  +    assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
        63956  +
        63957  +    rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
 63921  63958     }else{
 63922         -    /* If sqlite3BtreeDropTable was called on page 1.
 63923         -    ** This really never should happen except in a corrupt
 63924         -    ** database. 
 63925         -    */
 63926         -    zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
        63959  +    freePage(pPage, &rc);
 63927  63960       releasePage(pPage);
 63928  63961     }
        63962  +#endif
 63929  63963     return rc;  
 63930  63964   }
 63931  63965   SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
 63932  63966     int rc;
 63933  63967     sqlite3BtreeEnter(p);
 63934  63968     rc = btreeDropTable(p, iTable, piMoved);
 63935  63969     sqlite3BtreeLeave(p);
................................................................................
 67008  67042       goto value_from_function_out;
 67009  67043     }
 67010  67044   
 67011  67045     assert( pCtx->pParse->rc==SQLITE_OK );
 67012  67046     memset(&ctx, 0, sizeof(ctx));
 67013  67047     ctx.pOut = pVal;
 67014  67048     ctx.pFunc = pFunc;
 67015         -  pFunc->xFunc(&ctx, nVal, apVal);
        67049  +  pFunc->xSFunc(&ctx, nVal, apVal);
 67016  67050     if( ctx.isError ){
 67017  67051       rc = ctx.isError;
 67018  67052       sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
 67019  67053     }else{
 67020  67054       sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
 67021  67055       assert( rc==SQLITE_OK );
 67022  67056       rc = sqlite3VdbeChangeEncoding(pVal, enc);
................................................................................
 67755  67789     va_list ap;
 67756  67790     int i;
 67757  67791     char c;
 67758  67792     va_start(ap, zTypes);
 67759  67793     for(i=0; (c = zTypes[i])!=0; i++){
 67760  67794       if( c=='s' ){
 67761  67795         const char *z = va_arg(ap, const char*);
 67762         -      int addr = sqlite3VdbeAddOp2(p, z==0 ? OP_Null : OP_String8, 0, iDest++);
 67763         -      if( z ) sqlite3VdbeChangeP4(p, addr, z, 0);
        67796  +      sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest++, 0, z, 0);
 67764  67797       }else{
 67765  67798         assert( c=='i' );
 67766  67799         sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest++);
 67767  67800       }
 67768  67801     }
 67769  67802     va_end(ap);
 67770  67803   }
................................................................................
 68111  68144   ** Return the address of the next instruction to be inserted.
 68112  68145   */
 68113  68146   SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
 68114  68147     assert( p->magic==VDBE_MAGIC_INIT );
 68115  68148     return p->nOp;
 68116  68149   }
 68117  68150   
        68151  +/*
        68152  +** Verify that at least N opcode slots are available in p without
        68153  +** having to malloc for more space (except when compiled using
        68154  +** SQLITE_TEST_REALLOC_STRESS).  This interface is used during testing
        68155  +** to verify that certain calls to sqlite3VdbeAddOpList() can never
        68156  +** fail due to a OOM fault and hence that the return value from
        68157  +** sqlite3VdbeAddOpList() will always be non-NULL.
        68158  +*/
        68159  +#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
        68160  +SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
        68161  +  assert( p->nOp + N <= p->pParse->nOpAlloc );
        68162  +}
        68163  +#endif
        68164  +
 68118  68165   /*
 68119  68166   ** This function returns a pointer to the array of opcodes associated with
 68120  68167   ** the Vdbe passed as the first argument. It is the callers responsibility
 68121  68168   ** to arrange for the returned array to be eventually freed using the 
 68122  68169   ** vdbeFreeOpArray() function.
 68123  68170   **
 68124  68171   ** Before returning, *pnOp is set to the number of entries in the returned
................................................................................
 68136  68183     resolveP2Values(p, pnMaxArg);
 68137  68184     *pnOp = p->nOp;
 68138  68185     p->aOp = 0;
 68139  68186     return aOp;
 68140  68187   }
 68141  68188   
 68142  68189   /*
 68143         -** Add a whole list of operations to the operation stack.  Return the
 68144         -** address of the first operation added.
        68190  +** Add a whole list of operations to the operation stack.  Return a
        68191  +** pointer to the first operation inserted.
 68145  68192   */
 68146         -SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
 68147         -  int addr, i;
 68148         -  VdbeOp *pOut;
        68193  +SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
        68194  +  Vdbe *p,                     /* Add opcodes to the prepared statement */
        68195  +  int nOp,                     /* Number of opcodes to add */
        68196  +  VdbeOpList const *aOp,       /* The opcodes to be added */
        68197  +  int iLineno                  /* Source-file line number of first opcode */
        68198  +){
        68199  +  int i;
        68200  +  VdbeOp *pOut, *pFirst;
 68149  68201     assert( nOp>0 );
 68150  68202     assert( p->magic==VDBE_MAGIC_INIT );
 68151  68203     if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
 68152  68204       return 0;
 68153  68205     }
 68154         -  addr = p->nOp;
 68155         -  pOut = &p->aOp[addr];
        68206  +  pFirst = pOut = &p->aOp[p->nOp];
 68156  68207     for(i=0; i<nOp; i++, aOp++, pOut++){
 68157  68208       pOut->opcode = aOp->opcode;
 68158  68209       pOut->p1 = aOp->p1;
 68159  68210       pOut->p2 = aOp->p2;
 68160  68211       assert( aOp->p2>=0 );
 68161  68212       pOut->p3 = aOp->p3;
 68162  68213       pOut->p4type = P4_NOTUSED;
................................................................................
 68168  68219   #ifdef SQLITE_VDBE_COVERAGE
 68169  68220       pOut->iSrcLine = iLineno+i;
 68170  68221   #else
 68171  68222       (void)iLineno;
 68172  68223   #endif
 68173  68224   #ifdef SQLITE_DEBUG
 68174  68225       if( p->db->flags & SQLITE_VdbeAddopTrace ){
 68175         -      sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
        68226  +      sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
 68176  68227       }
 68177  68228   #endif
 68178  68229     }
 68179  68230     p->nOp += nOp;
 68180         -  return addr;
        68231  +  return pFirst;
 68181  68232   }
 68182  68233   
 68183  68234   #if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
 68184  68235   /*
 68185  68236   ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
 68186  68237   */
 68187  68238   SQLITE_PRIVATE void sqlite3VdbeScanStatus(
................................................................................
 68221  68272   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
 68222  68273     sqlite3VdbeGetOp(p,addr)->p2 = val;
 68223  68274   }
 68224  68275   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
 68225  68276     sqlite3VdbeGetOp(p,addr)->p3 = val;
 68226  68277   }
 68227  68278   SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){
 68228         -  sqlite3VdbeGetOp(p,-1)->p5 = p5;
        68279  +  if( !p->db->mallocFailed ) p->aOp[p->nOp-1].p5 = p5;
 68229  68280   }
 68230  68281   
 68231  68282   /*
 68232  68283   ** Change the P2 operand of instruction addr so that it points to
 68233  68284   ** the address of the next instruction to be coded.
 68234  68285   */
 68235  68286   SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
................................................................................
 68331  68382     p->pNext = pVdbe->pProgram;
 68332  68383     pVdbe->pProgram = p;
 68333  68384   }
 68334  68385   
 68335  68386   /*
 68336  68387   ** Change the opcode at addr into OP_Noop
 68337  68388   */
 68338         -SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
 68339         -  if( addr<p->nOp ){
 68340         -    VdbeOp *pOp = &p->aOp[addr];
 68341         -    sqlite3 *db = p->db;
 68342         -    freeP4(db, pOp->p4type, pOp->p4.p);
 68343         -    memset(pOp, 0, sizeof(pOp[0]));
 68344         -    pOp->opcode = OP_Noop;
 68345         -  }
        68389  +SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
        68390  +  VdbeOp *pOp;
        68391  +  if( p->db->mallocFailed ) return 0;
        68392  +  assert( addr>=0 && addr<p->nOp );
        68393  +  pOp = &p->aOp[addr];
        68394  +  freeP4(p->db, pOp->p4type, pOp->p4.p);
        68395  +  memset(pOp, 0, sizeof(pOp[0]));
        68396  +  pOp->opcode = OP_Noop;
        68397  +  return 1;
 68346  68398   }
 68347  68399   
 68348  68400   /*
 68349  68401   ** If the last opcode is "op" and it is not a jump destination,
 68350  68402   ** then remove it.  Return true if and only if an opcode was removed.
 68351  68403   */
 68352  68404   SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
 68353  68405     if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
 68354         -    sqlite3VdbeChangeToNoop(p, p->nOp-1);
 68355         -    return 1;
        68406  +    return sqlite3VdbeChangeToNoop(p, p->nOp-1);
 68356  68407     }else{
 68357  68408       return 0;
 68358  68409     }
 68359  68410   }
 68360  68411   
 68361  68412   /*
 68362  68413   ** Change the value of the P4 operand for a specific instruction.
................................................................................
 72724  72775   
 72725  72776   /*
 72726  72777   ** Allocate or return the aggregate context for a user function.  A new
 72727  72778   ** context is allocated on the first call.  Subsequent calls return the
 72728  72779   ** same context that was returned on prior calls.
 72729  72780   */
 72730  72781   SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context *p, int nByte){
 72731         -  assert( p && p->pFunc && p->pFunc->xStep );
        72782  +  assert( p && p->pFunc && p->pFunc->xFinalize );
 72732  72783     assert( sqlite3_mutex_held(p->pOut->db->mutex) );
 72733  72784     testcase( nByte<0 );
 72734  72785     if( (p->pMem->flags & MEM_Agg)==0 ){
 72735  72786       return createAggContext(p, nByte);
 72736  72787     }else{
 72737  72788       return (void*)p->pMem->z;
 72738  72789     }
................................................................................
 72815  72866   **
 72816  72867   ** This function is deprecated.  Do not use it for new code.  It is
 72817  72868   ** provide only to avoid breaking legacy code.  New aggregate function
 72818  72869   ** implementations should keep their own counts within their aggregate
 72819  72870   ** context.
 72820  72871   */
 72821  72872   SQLITE_API int SQLITE_STDCALL sqlite3_aggregate_count(sqlite3_context *p){
 72822         -  assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
        72873  +  assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
 72823  72874     return p->pMem->n;
 72824  72875   }
 72825  72876   #endif
 72826  72877   
 72827  72878   /*
 72828  72879   ** Return the number of columns in the result set for the statement pStmt.
 72829  72880   */
................................................................................
 75558  75609       assert( memIsValid(pCtx->argv[i]) );
 75559  75610       REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
 75560  75611     }
 75561  75612   #endif
 75562  75613     MemSetTypeFlag(pCtx->pOut, MEM_Null);
 75563  75614     pCtx->fErrorOrAux = 0;
 75564  75615     db->lastRowid = lastRowid;
 75565         -  (*pCtx->pFunc->xFunc)(pCtx, pCtx->argc, pCtx->argv); /* IMP: R-24505-23230 */
 75566         -  lastRowid = db->lastRowid;  /* Remember rowid changes made by xFunc */
        75616  +  (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
        75617  +  lastRowid = db->lastRowid;  /* Remember rowid changes made by xSFunc */
 75567  75618   
 75568  75619     /* If the function returned an error, throw an exception */
 75569  75620     if( pCtx->fErrorOrAux ){
 75570  75621       if( pCtx->isError ){
 75571  75622         sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
 75572  75623         rc = pCtx->isError;
 75573  75624       }
................................................................................
 78995  79046   ** See also: Clear
 78996  79047   */
 78997  79048   case OP_Destroy: {     /* out2 */
 78998  79049     int iMoved;
 78999  79050     int iDb;
 79000  79051   
 79001  79052     assert( p->readOnly==0 );
        79053  +  assert( pOp->p1>1 );
 79002  79054     pOut = out2Prerelease(p, pOp);
 79003  79055     pOut->flags = MEM_Null;
 79004  79056     if( db->nVdbeRead > db->nVDestroy+1 ){
 79005  79057       rc = SQLITE_LOCKED;
 79006  79058       p->errorAction = OE_Abort;
 79007  79059     }else{
 79008  79060       iDb = pOp->p3;
................................................................................
 79799  79851   #endif
 79800  79852   
 79801  79853     pMem->n++;
 79802  79854     sqlite3VdbeMemInit(&t, db, MEM_Null);
 79803  79855     pCtx->pOut = &t;
 79804  79856     pCtx->fErrorOrAux = 0;
 79805  79857     pCtx->skipFlag = 0;
 79806         -  (pCtx->pFunc->xStep)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
        79858  +  (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
 79807  79859     if( pCtx->fErrorOrAux ){
 79808  79860       if( pCtx->isError ){
 79809  79861         sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
 79810  79862         rc = pCtx->isError;
 79811  79863       }
 79812  79864       sqlite3VdbeMemRelease(&t);
 79813  79865     }else{
................................................................................
 80797  80849     const char *zColumn,    /* The column containing the blob */
 80798  80850     sqlite_int64 iRow,      /* The row containing the glob */
 80799  80851     int flags,              /* True -> read/write access, false -> read-only */
 80800  80852     sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
 80801  80853   ){
 80802  80854     int nAttempt = 0;
 80803  80855     int iCol;               /* Index of zColumn in row-record */
 80804         -
 80805         -  /* This VDBE program seeks a btree cursor to the identified 
 80806         -  ** db/table/row entry. The reason for using a vdbe program instead
 80807         -  ** of writing code to use the b-tree layer directly is that the
 80808         -  ** vdbe program will take advantage of the various transaction,
 80809         -  ** locking and error handling infrastructure built into the vdbe.
 80810         -  **
 80811         -  ** After seeking the cursor, the vdbe executes an OP_ResultRow.
 80812         -  ** Code external to the Vdbe then "borrows" the b-tree cursor and
 80813         -  ** uses it to implement the blob_read(), blob_write() and 
 80814         -  ** blob_bytes() functions.
 80815         -  **
 80816         -  ** The sqlite3_blob_close() function finalizes the vdbe program,
 80817         -  ** which closes the b-tree cursor and (possibly) commits the 
 80818         -  ** transaction.
 80819         -  */
 80820         -  static const int iLn = VDBE_OFFSET_LINENO(4);
 80821         -  static const VdbeOpList openBlob[] = {
 80822         -    /* {OP_Transaction, 0, 0, 0},  // 0: Inserted separately */
 80823         -    {OP_TableLock, 0, 0, 0},       /* 1: Acquire a read or write lock */
 80824         -    /* One of the following two instructions is replaced by an OP_Noop. */
 80825         -    {OP_OpenRead, 0, 0, 0},        /* 2: Open cursor 0 for reading */
 80826         -    {OP_OpenWrite, 0, 0, 0},       /* 3: Open cursor 0 for read/write */
 80827         -    {OP_Variable, 1, 1, 1},        /* 4: Push the rowid to the stack */
 80828         -    {OP_NotExists, 0, 10, 1},      /* 5: Seek the cursor */
 80829         -    {OP_Column, 0, 0, 1},          /* 6  */
 80830         -    {OP_ResultRow, 1, 0, 0},       /* 7  */
 80831         -    {OP_Goto, 0, 4, 0},            /* 8  */
 80832         -    {OP_Close, 0, 0, 0},           /* 9  */
 80833         -    {OP_Halt, 0, 0, 0},            /* 10 */
 80834         -  };
 80835         -
 80836  80856     int rc = SQLITE_OK;
 80837  80857     char *zErr = 0;
 80838  80858     Table *pTab;
 80839  80859     Parse *pParse = 0;
 80840  80860     Incrblob *pBlob = 0;
 80841  80861   
 80842  80862   #ifdef SQLITE_ENABLE_API_ARMOR
................................................................................
 80947  80967           goto blob_open_out;
 80948  80968         }
 80949  80969       }
 80950  80970   
 80951  80971       pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
 80952  80972       assert( pBlob->pStmt || db->mallocFailed );
 80953  80973       if( pBlob->pStmt ){
        80974  +      
        80975  +      /* This VDBE program seeks a btree cursor to the identified 
        80976  +      ** db/table/row entry. The reason for using a vdbe program instead
        80977  +      ** of writing code to use the b-tree layer directly is that the
        80978  +      ** vdbe program will take advantage of the various transaction,
        80979  +      ** locking and error handling infrastructure built into the vdbe.
        80980  +      **
        80981  +      ** After seeking the cursor, the vdbe executes an OP_ResultRow.
        80982  +      ** Code external to the Vdbe then "borrows" the b-tree cursor and
        80983  +      ** uses it to implement the blob_read(), blob_write() and 
        80984  +      ** blob_bytes() functions.
        80985  +      **
        80986  +      ** The sqlite3_blob_close() function finalizes the vdbe program,
        80987  +      ** which closes the b-tree cursor and (possibly) commits the 
        80988  +      ** transaction.
        80989  +      */
        80990  +      static const int iLn = VDBE_OFFSET_LINENO(4);
        80991  +      static const VdbeOpList openBlob[] = {
        80992  +                                    /* addr/ofst */
        80993  +        /* {OP_Transaction, 0, 0, 0},  // 0/   inserted separately */
        80994  +        {OP_TableLock, 0, 0, 0},       /* 1/0: Acquire a read or write lock */
        80995  +        {OP_OpenRead, 0, 0, 0},        /* 2/1: Open a cursor */
        80996  +        {OP_Variable, 1, 1, 0},        /* 3/2: Move ?1 into reg[1] */
        80997  +        {OP_NotExists, 0, 8, 1},       /* 4/3: Seek the cursor */
        80998  +        {OP_Column, 0, 0, 1},          /* 5/4  */
        80999  +        {OP_ResultRow, 1, 0, 0},       /* 6/5  */
        81000  +        {OP_Goto, 0, 3, 0},            /* 7/6  */
        81001  +        {OP_Close, 0, 0, 0},           /* 8/7  */
        81002  +        {OP_Halt, 0, 0, 0},            /* 9/8  */
        81003  +      };
 80954  81004         Vdbe *v = (Vdbe *)pBlob->pStmt;
 80955  81005         int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 80956         -
        81006  +      VdbeOp *aOp;
 80957  81007   
 80958  81008         sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, 
 80959  81009                              pTab->pSchema->schema_cookie,
 80960  81010                              pTab->pSchema->iGeneration);
 80961  81011         sqlite3VdbeChangeP5(v, 1);     
 80962         -      sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
        81012  +      aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
 80963  81013   
 80964  81014         /* Make sure a mutex is held on the table to be accessed */
 80965  81015         sqlite3VdbeUsesBtree(v, iDb); 
 80966  81016   
 80967         -      /* Configure the OP_TableLock instruction */
        81017  +      if( db->mallocFailed==0 ){
        81018  +        assert( aOp!=0 );
        81019  +        /* Configure the OP_TableLock instruction */
 80968  81020   #ifdef SQLITE_OMIT_SHARED_CACHE
 80969         -      sqlite3VdbeChangeToNoop(v, 1);
 80970         -#else
 80971         -      sqlite3VdbeChangeP1(v, 1, iDb);
 80972         -      sqlite3VdbeChangeP2(v, 1, pTab->tnum);
 80973         -      sqlite3VdbeChangeP3(v, 1, flags);
 80974         -      sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
 80975         -#endif
 80976         -
 80977         -      /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
 80978         -      ** parameter of the other to pTab->tnum.  */
 80979         -      sqlite3VdbeChangeToNoop(v, 3 - flags);
 80980         -      sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum);
 80981         -      sqlite3VdbeChangeP3(v, 2 + flags, iDb);
 80982         -
 80983         -      /* Configure the number of columns. Configure the cursor to
 80984         -      ** think that the table has one more column than it really
 80985         -      ** does. An OP_Column to retrieve this imaginary column will
 80986         -      ** always return an SQL NULL. This is useful because it means
 80987         -      ** we can invoke OP_Column to fill in the vdbe cursors type 
 80988         -      ** and offset cache without causing any IO.
 80989         -      */
 80990         -      sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
 80991         -      sqlite3VdbeChangeP2(v, 6, pTab->nCol);
 80992         -      if( !db->mallocFailed ){
        81021  +        aOp[0].opcode = OP_Noop;
        81022  +#else
        81023  +        aOp[0].p1 = iDb;
        81024  +        aOp[0].p2 = pTab->tnum;
        81025  +        aOp[0].p3 = flags;
        81026  +        sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
        81027  +      }
        81028  +      if( db->mallocFailed==0 ){
        81029  +#endif
        81030  +
        81031  +        /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
        81032  +        ** parameter of the other to pTab->tnum.  */
        81033  +        if( flags ) aOp[1].opcode = OP_OpenWrite;
        81034  +        aOp[1].p2 = pTab->tnum;
        81035  +        aOp[1].p3 = iDb;   
        81036  +
        81037  +        /* Configure the number of columns. Configure the cursor to
        81038  +        ** think that the table has one more column than it really
        81039  +        ** does. An OP_Column to retrieve this imaginary column will
        81040  +        ** always return an SQL NULL. This is useful because it means
        81041  +        ** we can invoke OP_Column to fill in the vdbe cursors type 
        81042  +        ** and offset cache without causing any IO.
        81043  +        */
        81044  +        aOp[1].p4type = P4_INT32;
        81045  +        aOp[1].p4.i = pTab->nCol+1;
        81046  +        aOp[4].p2 = pTab->nCol;
        81047  +
 80993  81048           pParse->nVar = 1;
 80994  81049           pParse->nMem = 1;
 80995  81050           pParse->nTab = 1;
 80996  81051           sqlite3VdbeMakeReady(v, pParse);
 80997  81052         }
 80998  81053       }
 80999  81054      
................................................................................
 85249  85304           pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
 85250  85305           if( pDef==0 ){
 85251  85306             no_such_func = 1;
 85252  85307           }else{
 85253  85308             wrong_num_args = 1;
 85254  85309           }
 85255  85310         }else{
 85256         -        is_agg = pDef->xFunc==0;
        85311  +        is_agg = pDef->xFinalize!=0;
 85257  85312           if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
 85258  85313             ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
 85259  85314             if( n==2 ){
 85260  85315               pExpr->iTable = exprProbability(pList->a[1].pExpr);
 85261  85316               if( pExpr->iTable<0 ){
 85262  85317                 sqlite3ErrorMsg(pParse,
 85263  85318                   "second argument to likelihood() must be a "
................................................................................
 87216  87271   SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
 87217  87272     Parse *pParse,          /* Parsing context */
 87218  87273     ExprList *pList,        /* List to which to append. Might be NULL */
 87219  87274     Expr *pExpr             /* Expression to be appended. Might be NULL */
 87220  87275   ){
 87221  87276     sqlite3 *db = pParse->db;
 87222  87277     if( pList==0 ){
 87223         -    pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
        87278  +    pList = sqlite3DbMallocRaw(db, sizeof(ExprList) );
 87224  87279       if( pList==0 ){
 87225  87280         goto no_mem;
 87226  87281       }
        87282  +    pList->nExpr = 0;
 87227  87283       pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
 87228  87284       if( pList->a==0 ) goto no_mem;
 87229  87285     }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
 87230  87286       struct ExprList_item *a;
 87231  87287       assert( pList->nExpr>0 );
 87232  87288       a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
 87233  87289       if( a==0 ){
................................................................................
 88977  89033           pFarg = pExpr->x.pList;
 88978  89034         }
 88979  89035         nFarg = pFarg ? pFarg->nExpr : 0;
 88980  89036         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 88981  89037         zId = pExpr->u.zToken;
 88982  89038         nId = sqlite3Strlen30(zId);
 88983  89039         pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
 88984         -      if( pDef==0 || pDef->xFunc==0 ){
        89040  +      if( pDef==0 || pDef->xFinalize!=0 ){
 88985  89041           sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
 88986  89042           break;
 88987  89043         }
 88988  89044   
 88989  89045         /* Attempt a direct implementation of the built-in COALESCE() and
 88990  89046         ** IFNULL() functions.  This avoids unnecessary evaluation of
 88991  89047         ** arguments past the first non-NULL argument.
................................................................................
 91649  91705     sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor);
 91650  91706   }
 91651  91707   static const FuncDef statInitFuncdef = {
 91652  91708     2+IsStat34,      /* nArg */
 91653  91709     SQLITE_UTF8,     /* funcFlags */
 91654  91710     0,               /* pUserData */
 91655  91711     0,               /* pNext */
 91656         -  statInit,        /* xFunc */
 91657         -  0,               /* xStep */
        91712  +  statInit,        /* xSFunc */
 91658  91713     0,               /* xFinalize */
 91659  91714     "stat_init",     /* zName */
 91660  91715     0,               /* pHash */
 91661  91716     0                /* pDestructor */
 91662  91717   };
 91663  91718   
 91664  91719   #ifdef SQLITE_ENABLE_STAT4
................................................................................
 91950  92005   #endif
 91951  92006   }
 91952  92007   static const FuncDef statPushFuncdef = {
 91953  92008     2+IsStat34,      /* nArg */
 91954  92009     SQLITE_UTF8,     /* funcFlags */
 91955  92010     0,               /* pUserData */
 91956  92011     0,               /* pNext */
 91957         -  statPush,        /* xFunc */
 91958         -  0,               /* xStep */
        92012  +  statPush,        /* xSFunc */
 91959  92013     0,               /* xFinalize */
 91960  92014     "stat_push",     /* zName */
 91961  92015     0,               /* pHash */
 91962  92016     0                /* pDestructor */
 91963  92017   };
 91964  92018   
 91965  92019   #define STAT_GET_STAT1 0          /* "stat" column of stat1 table */
................................................................................
 92097  92151   #endif
 92098  92152   }
 92099  92153   static const FuncDef statGetFuncdef = {
 92100  92154     1+IsStat34,      /* nArg */
 92101  92155     SQLITE_UTF8,     /* funcFlags */
 92102  92156     0,               /* pUserData */
 92103  92157     0,               /* pNext */
 92104         -  statGet,         /* xFunc */
 92105         -  0,               /* xStep */
        92158  +  statGet,         /* xSFunc */
 92106  92159     0,               /* xFinalize */
 92107  92160     "stat_get",      /* zName */
 92108  92161     0,               /* pHash */
 92109  92162     0                /* pDestructor */
 92110  92163   };
 92111  92164   
 92112  92165   static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
................................................................................
 92114  92167   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 92115  92168     sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
 92116  92169   #elif SQLITE_DEBUG
 92117  92170     assert( iParam==STAT_GET_STAT1 );
 92118  92171   #else
 92119  92172     UNUSED_PARAMETER( iParam );
 92120  92173   #endif
 92121         -  sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4, regOut);
 92122         -  sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
        92174  +  sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4, regOut,
        92175  +                    (char*)&statGetFuncdef, P4_FUNCDEF);
 92123  92176     sqlite3VdbeChangeP5(v, 1 + IsStat34);
 92124  92177   }
 92125  92178   
 92126  92179   /*
 92127  92180   ** Generate code to do an analysis of all indices associated with
 92128  92181   ** a single table.
 92129  92182   */
................................................................................
 92269  92322       ** The third argument is only used for STAT3 and STAT4
 92270  92323       */
 92271  92324   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 92272  92325       sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
 92273  92326   #endif
 92274  92327       sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
 92275  92328       sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
 92276         -    sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4+1, regStat4);
 92277         -    sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
        92329  +    sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4+1, regStat4,
        92330  +                     (char*)&statInitFuncdef, P4_FUNCDEF);
 92278  92331       sqlite3VdbeChangeP5(v, 2+IsStat34);
 92279  92332   
 92280  92333       /* Implementation of the following:
 92281  92334       **
 92282  92335       **   Rewind csr
 92283  92336       **   if eof(csr) goto end_of_scan;
 92284  92337       **   regChng = 0
................................................................................
 92366  92419           VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
 92367  92420         }
 92368  92421         sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
 92369  92422         sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
 92370  92423       }
 92371  92424   #endif
 92372  92425       assert( regChng==(regStat4+1) );
 92373         -    sqlite3VdbeAddOp3(v, OP_Function0, 1, regStat4, regTemp);
 92374         -    sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
        92426  +    sqlite3VdbeAddOp4(v, OP_Function0, 1, regStat4, regTemp,
        92427  +                     (char*)&statPushFuncdef, P4_FUNCDEF);
 92375  92428       sqlite3VdbeChangeP5(v, 2+IsStat34);
 92376  92429       sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
 92377  92430   
 92378  92431       /* Add the entry to the stat1 table. */
 92379  92432       callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
 92380  92433       assert( "BBB"[0]==SQLITE_AFF_TEXT );
 92381  92434       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
................................................................................
 93424  93477     regArgs = sqlite3GetTempRange(pParse, 4);
 93425  93478     sqlite3ExprCode(pParse, pFilename, regArgs);
 93426  93479     sqlite3ExprCode(pParse, pDbname, regArgs+1);
 93427  93480     sqlite3ExprCode(pParse, pKey, regArgs+2);
 93428  93481   
 93429  93482     assert( v || db->mallocFailed );
 93430  93483     if( v ){
 93431         -    sqlite3VdbeAddOp3(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3);
        93484  +    sqlite3VdbeAddOp4(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3,
        93485  +                      (char *)pFunc, P4_FUNCDEF);
 93432  93486       assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
 93433  93487       sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
 93434         -    sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
 93435         -
        93488  + 
 93436  93489       /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
 93437  93490       ** statement only). For DETACH, set it to false (expire all existing
 93438  93491       ** statements).
 93439  93492       */
 93440  93493       sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
 93441  93494     }
 93442  93495     
................................................................................
 93453  93506   */
 93454  93507   SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
 93455  93508     static const FuncDef detach_func = {
 93456  93509       1,                /* nArg */
 93457  93510       SQLITE_UTF8,      /* funcFlags */
 93458  93511       0,                /* pUserData */
 93459  93512       0,                /* pNext */
 93460         -    detachFunc,       /* xFunc */
 93461         -    0,                /* xStep */
        93513  +    detachFunc,       /* xSFunc */
 93462  93514       0,                /* xFinalize */
 93463  93515       "sqlite_detach",  /* zName */
 93464  93516       0,                /* pHash */
 93465  93517       0                 /* pDestructor */
 93466  93518     };
 93467  93519     codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
 93468  93520   }
................................................................................
 93474  93526   */
 93475  93527   SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
 93476  93528     static const FuncDef attach_func = {
 93477  93529       3,                /* nArg */
 93478  93530       SQLITE_UTF8,      /* funcFlags */
 93479  93531       0,                /* pUserData */
 93480  93532       0,                /* pNext */
 93481         -    attachFunc,       /* xFunc */
 93482         -    0,                /* xStep */
        93533  +    attachFunc,       /* xSFunc */
 93483  93534       0,                /* xFinalize */
 93484  93535       "sqlite_attach",  /* zName */
 93485  93536       0,                /* pHash */
 93486  93537       0                 /* pDestructor */
 93487  93538     };
 93488  93539     codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
 93489  93540   }
................................................................................
 94143  94194     if( v && pParse->nErr==0 && !db->mallocFailed ){
 94144  94195       assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
 94145  94196       /* A minimum of one cursor is required if autoincrement is used
 94146  94197       *  See ticket [a696379c1f08866] */
 94147  94198       if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
 94148  94199       sqlite3VdbeMakeReady(v, pParse);
 94149  94200       pParse->rc = SQLITE_DONE;
 94150         -    pParse->colNamesSet = 0;
 94151  94201     }else{
 94152  94202       pParse->rc = SQLITE_ERROR;
 94153  94203     }
        94204  +
        94205  +  /* We are done with this Parse object. There is no need to de-initialize it */
        94206  +#if 0
        94207  +  pParse->colNamesSet = 0;
 94154  94208     pParse->nTab = 0;
 94155  94209     pParse->nMem = 0;
 94156  94210     pParse->nSet = 0;
 94157  94211     pParse->nVar = 0;
 94158  94212     DbMaskZero(pParse->cookieMask);
        94213  +#endif
 94159  94214   }
 94160  94215   
 94161  94216   /*
 94162  94217   ** Run the parser and code generator recursively in order to generate
 94163  94218   ** code for the SQL statement given onto the end of the pParse context
 94164  94219   ** currently under construction.  When the parser is run recursively
 94165  94220   ** this way, the final OP_Halt is not appended and other initialization
................................................................................
 94410  94465         continue;
 94411  94466       }
 94412  94467       if( j<i ){
 94413  94468         db->aDb[j] = db->aDb[i];
 94414  94469       }
 94415  94470       j++;
 94416  94471     }
 94417         -  memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
 94418  94472     db->nDb = j;
 94419  94473     if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
 94420  94474       memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
 94421  94475       sqlite3DbFree(db, db->aDb);
 94422  94476       db->aDb = db->aDbStatic;
 94423  94477     }
 94424  94478   }
................................................................................
 94673  94727     Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
 94674  94728     Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
 94675  94729     Token **pUnqual     /* Write the unqualified object name here */
 94676  94730   ){
 94677  94731     int iDb;                    /* Database holding the object */
 94678  94732     sqlite3 *db = pParse->db;
 94679  94733   
 94680         -  if( ALWAYS(pName2!=0) && pName2->n>0 ){
        94734  +  assert( pName2!=0 );
        94735  +  if( pName2->n>0 ){
 94681  94736       if( db->init.busy ) {
 94682  94737         sqlite3ErrorMsg(pParse, "corrupt database");
 94683  94738         return -1;
 94684  94739       }
 94685  94740       *pUnqual = pName2;
 94686  94741       iDb = sqlite3FindDb(db, pName1);
 94687  94742       if( iDb<0 ){
................................................................................
 94762  94817     Table *pTable;
 94763  94818     char *zName = 0; /* The name of the new table */
 94764  94819     sqlite3 *db = pParse->db;
 94765  94820     Vdbe *v;
 94766  94821     int iDb;         /* Database number to create the table in */
 94767  94822     Token *pName;    /* Unqualified name of the table to create */
 94768  94823   
 94769         -  /* The table or view name to create is passed to this routine via tokens
 94770         -  ** pName1 and pName2. If the table name was fully qualified, for example:
 94771         -  **
 94772         -  ** CREATE TABLE xxx.yyy (...);
 94773         -  ** 
 94774         -  ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
 94775         -  ** the table name is not fully qualified, i.e.:
 94776         -  **
 94777         -  ** CREATE TABLE yyy(...);
 94778         -  **
 94779         -  ** Then pName1 is set to "yyy" and pName2 is "".
 94780         -  **
 94781         -  ** The call below sets the pName pointer to point at the token (pName1 or
 94782         -  ** pName2) that stores the unqualified table name. The variable iDb is
 94783         -  ** set to the index of the database that the table or view is to be
 94784         -  ** created in.
 94785         -  */
 94786         -  iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 94787         -  if( iDb<0 ) return;
 94788         -  if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
 94789         -    /* If creating a temp table, the name may not be qualified. Unless 
 94790         -    ** the database name is "temp" anyway.  */
 94791         -    sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
 94792         -    return;
 94793         -  }
 94794         -  if( !OMIT_TEMPDB && isTemp ) iDb = 1;
 94795         -
 94796         -  pParse->sNameToken = *pName;
 94797         -  zName = sqlite3NameFromToken(db, pName);
        94824  +  if( db->init.busy && db->init.newTnum==1 ){
        94825  +    /* Special case:  Parsing the sqlite_master or sqlite_temp_master schema */
        94826  +    iDb = db->init.iDb;
        94827  +    zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb));
        94828  +    pName = pName1;
        94829  +  }else{
        94830  +    /* The common case */
        94831  +    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
        94832  +    if( iDb<0 ) return;
        94833  +    if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
        94834  +      /* If creating a temp table, the name may not be qualified. Unless 
        94835  +      ** the database name is "temp" anyway.  */
        94836  +      sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
        94837  +      return;
        94838  +    }
        94839  +    if( !OMIT_TEMPDB && isTemp ) iDb = 1;
        94840  +    zName = sqlite3NameFromToken(db, pName);
        94841  +  }
        94842  +  pParse->sNameToken = *pName;
 94798  94843     if( zName==0 ) return;
 94799  94844     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 94800  94845       goto begin_table_error;
 94801  94846     }
 94802  94847     if( db->init.iDb==1 ) isTemp = 1;
 94803  94848   #ifndef SQLITE_OMIT_AUTHORIZATION
 94804         -  assert( (isTemp & 1)==isTemp );
        94849  +  assert( isTemp==0 || isTemp==1 );
        94850  +  assert( isView==0 || isView==1 );
 94805  94851     {
 94806         -    int code;
        94852  +    static const u8 aCode[] = {
        94853  +       SQLITE_CREATE_TABLE,
        94854  +       SQLITE_CREATE_TEMP_TABLE,
        94855  +       SQLITE_CREATE_VIEW,
        94856  +       SQLITE_CREATE_TEMP_VIEW
        94857  +    };
 94807  94858       char *zDb = db->aDb[iDb].zName;
 94808  94859       if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
 94809  94860         goto begin_table_error;
 94810  94861       }
 94811         -    if( isView ){
 94812         -      if( !OMIT_TEMPDB && isTemp ){
 94813         -        code = SQLITE_CREATE_TEMP_VIEW;
 94814         -      }else{
 94815         -        code = SQLITE_CREATE_VIEW;
 94816         -      }
 94817         -    }else{
 94818         -      if( !OMIT_TEMPDB && isTemp ){
 94819         -        code = SQLITE_CREATE_TEMP_TABLE;
 94820         -      }else{
 94821         -        code = SQLITE_CREATE_TABLE;
 94822         -      }
 94823         -    }
 94824         -    if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
        94862  +    if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
        94863  +                                       zName, 0, zDb) ){
 94825  94864         goto begin_table_error;
 94826  94865       }
 94827  94866     }
 94828  94867   #endif
 94829  94868   
 94830  94869     /* Make sure the new table name does not collide with an existing
 94831  94870     ** index or table name in the same database.  Issue an error message if
................................................................................
 95779  95818     assert( !db->init.busy || !pSelect );
 95780  95819   
 95781  95820     /* If the db->init.busy is 1 it means we are reading the SQL off the
 95782  95821     ** "sqlite_master" or "sqlite_temp_master" table on the disk.
 95783  95822     ** So do not write to the disk again.  Extract the root page number
 95784  95823     ** for the table from the db->init.newTnum field.  (The page number
 95785  95824     ** should have been put there by the sqliteOpenCb routine.)
        95825  +  **
        95826  +  ** If the root page number is 1, that means this is the sqlite_master
        95827  +  ** table itself.  So mark it read-only.
 95786  95828     */
 95787  95829     if( db->init.busy ){
 95788  95830       p->tnum = db->init.newTnum;
        95831  +    if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
 95789  95832     }
 95790  95833   
 95791  95834     /* Special processing for WITHOUT ROWID Tables */
 95792  95835     if( tabOpts & TF_WithoutRowid ){
 95793  95836       if( (p->tabFlags & TF_Autoincrement) ){
 95794  95837         sqlite3ErrorMsg(pParse,
 95795  95838             "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
................................................................................
 96234  96277   ** Also write code to modify the sqlite_master table and internal schema
 96235  96278   ** if a root-page of another table is moved by the btree-layer whilst
 96236  96279   ** erasing iTable (this can happen with an auto-vacuum database).
 96237  96280   */ 
 96238  96281   static void destroyRootPage(Parse *pParse, int iTable, int iDb){
 96239  96282     Vdbe *v = sqlite3GetVdbe(pParse);
 96240  96283     int r1 = sqlite3GetTempReg(pParse);
        96284  +  assert( iTable>1 );
 96241  96285     sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
 96242  96286     sqlite3MayAbort(pParse);
 96243  96287   #ifndef SQLITE_OMIT_AUTOVACUUM
 96244  96288     /* OP_Destroy stores an in integer r1. If this integer
 96245  96289     ** is non-zero, then it is the root page number of a table moved to
 96246  96290     ** location iTable. The following code modifies the sqlite_master table to
 96247  96291     ** reflect this.
................................................................................
 97632  97676     SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
 97633  97677     Token *pTable,      /* Table to append */
 97634  97678     Token *pDatabase    /* Database of the table */
 97635  97679   ){
 97636  97680     struct SrcList_item *pItem;
 97637  97681     assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
 97638  97682     if( pList==0 ){
 97639         -    pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
        97683  +    pList = sqlite3DbMallocRaw(db, sizeof(SrcList) );
 97640  97684       if( pList==0 ) return 0;
 97641  97685       pList->nAlloc = 1;
        97686  +    pList->nSrc = 0;
 97642  97687     }
 97643  97688     pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
 97644  97689     if( db->mallocFailed ){
 97645  97690       sqlite3SrcListDelete(db, pList);
 97646  97691       return 0;
 97647  97692     }
 97648  97693     pItem = &pList->a[pList->nSrc-1];
................................................................................
 98037  98082   ){
 98038  98083     Vdbe *v = sqlite3GetVdbe(pParse);
 98039  98084     assert( (errCode&0xff)==SQLITE_CONSTRAINT );
 98040  98085     if( onError==OE_Abort ){
 98041  98086       sqlite3MayAbort(pParse);
 98042  98087     }
 98043  98088     sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
 98044         -  if( p5Errmsg ) sqlite3VdbeChangeP5(v, p5Errmsg);
        98089  +  sqlite3VdbeChangeP5(v, p5Errmsg);
 98045  98090   }
 98046  98091   
 98047  98092   /*
 98048  98093   ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
 98049  98094   */
 98050  98095   SQLITE_PRIVATE void sqlite3UniqueConstraint(
 98051  98096     Parse *pParse,    /* Parsing context */
................................................................................
 98578  98623   ** 1: UTF8/16 conversion required and function takes any number of arguments.
 98579  98624   ** 2: UTF16 byte order change required and function takes any number of args.
 98580  98625   ** 3: encoding matches and function takes any number of arguments
 98581  98626   ** 4: UTF8/16 conversion required - argument count matches exactly
 98582  98627   ** 5: UTF16 byte order conversion required - argument count matches exactly
 98583  98628   ** 6: Perfect match:  encoding and argument count match exactly.
 98584  98629   **
 98585         -** If nArg==(-2) then any function with a non-null xStep or xFunc is
 98586         -** a perfect match and any function with both xStep and xFunc NULL is
        98630  +** If nArg==(-2) then any function with a non-null xSFunc is
        98631  +** a perfect match and any function with xSFunc NULL is
 98587  98632   ** a non-match.
 98588  98633   */
 98589  98634   #define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
 98590  98635   static int matchQuality(
 98591  98636     FuncDef *p,     /* The function we are evaluating for match quality */
 98592  98637     int nArg,       /* Desired number of arguments.  (-1)==any */
 98593  98638     u8 enc          /* Desired text encoding */
 98594  98639   ){
 98595  98640     int match;
 98596  98641   
 98597  98642     /* nArg of -2 is a special case */
 98598         -  if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
        98643  +  if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
 98599  98644   
 98600  98645     /* Wrong number of arguments means "no match" */
 98601  98646     if( p->nArg!=nArg && p->nArg>=0 ) return 0;
 98602  98647   
 98603  98648     /* Give a better score to a function with a specific number of arguments
 98604  98649     ** than to function that accepts any number of arguments. */
 98605  98650     if( p->nArg==nArg ){
................................................................................
 98669  98714   ** NULL if the function does not exist.
 98670  98715   **
 98671  98716   ** If the createFlag argument is true, then a new (blank) FuncDef
 98672  98717   ** structure is created and liked into the "db" structure if a
 98673  98718   ** no matching function previously existed.
 98674  98719   **
 98675  98720   ** If nArg is -2, then the first valid function found is returned.  A
 98676         -** function is valid if either xFunc or xStep is non-zero.  The nArg==(-2)
        98721  +** function is valid if xSFunc is non-zero.  The nArg==(-2)
 98677  98722   ** case is used to see if zName is a valid function name for some number
 98678  98723   ** of arguments.  If nArg is -2, then createFlag must be 0.
 98679  98724   **
 98680  98725   ** If createFlag is false, then a function with the required name and
 98681  98726   ** number of arguments may be returned even if the eTextRep flag does not
 98682  98727   ** match that requested.
 98683  98728   */
................................................................................
 98746  98791       pBest->nArg = (u16)nArg;
 98747  98792       pBest->funcFlags = enc;
 98748  98793       memcpy(pBest->zName, zName, nName);
 98749  98794       pBest->zName[nName] = 0;
 98750  98795       sqlite3FuncDefInsert(&db->aFunc, pBest);
 98751  98796     }
 98752  98797   
 98753         -  if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
        98798  +  if( pBest && (pBest->xSFunc || createFlag) ){
 98754  98799       return pBest;
 98755  98800     }
 98756  98801     return 0;
 98757  98802   }
 98758  98803   
 98759  98804   /*
 98760  98805   ** Free all resources held by the schema structure. The void* argument points
................................................................................
104528 104573       sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
104529 104574       pik_flags = 0;
104530 104575       if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
104531 104576       if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
104532 104577         assert( pParse->nested==0 );
104533 104578         pik_flags |= OPFLAG_NCHANGE;
104534 104579       }
104535         -    if( pik_flags )  sqlite3VdbeChangeP5(v, pik_flags);
       104580  +    sqlite3VdbeChangeP5(v, pik_flags);
104536 104581     }
104537 104582     if( !HasRowid(pTab) ) return;
104538 104583     regData = regNewData + 1;
104539 104584     regRec = sqlite3GetTempReg(pParse);
104540 104585     sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
104541 104586     if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0);
104542 104587     sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
................................................................................
104944 104989       }else if( pDest->pIndex==0 ){
104945 104990         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
104946 104991       }else{
104947 104992         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
104948 104993         assert( (pDest->tabFlags & TF_Autoincrement)==0 );
104949 104994       }
104950 104995       sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
104951         -    sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
       104996  +    sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid,
       104997  +                      pDest->zName, 0);
104952 104998       sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
104953         -    sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
104954 104999       sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
104955 105000       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
104956 105001       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
104957 105002     }else{
104958 105003       sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
104959 105004       sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
104960 105005     }
................................................................................
107399 107444         { OP_Integer,     0, 2,        0},
107400 107445         { OP_Subtract,    1, 2,        1},
107401 107446         { OP_IfPos,       1, 8,        0},
107402 107447         { OP_Integer,     0, 1,        0},                         /* 6 */
107403 107448         { OP_Noop,        0, 0,        0},
107404 107449         { OP_ResultRow,   1, 1,        0},
107405 107450       };
107406         -    int addr;
       107451  +    VdbeOp *aOp;
107407 107452       sqlite3VdbeUsesBtree(v, iDb);
107408 107453       if( !zRight ){
107409 107454         setOneColumnName(v, "cache_size");
107410 107455         pParse->nMem += 2;
107411         -      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
107412         -      sqlite3VdbeChangeP1(v, addr, iDb);
107413         -      sqlite3VdbeChangeP1(v, addr+1, iDb);
107414         -      sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
       107456  +      sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
       107457  +      aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
       107458  +      if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
       107459  +      aOp[0].p1 = iDb;
       107460  +      aOp[1].p1 = iDb;
       107461  +      aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
107415 107462       }else{
107416 107463         int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
107417 107464         sqlite3BeginWriteOperation(pParse, 0, iDb);
107418 107465         sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
107419 107466         sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
107420 107467         assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
107421 107468         pDb->pSchema->cache_size = size;
................................................................................
107653 107700             { OP_Transaction,    0,         1,                 0},    /* 0 */
107654 107701             { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
107655 107702             { OP_If,             1,         0,                 0},    /* 2 */
107656 107703             { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
107657 107704             { OP_Integer,        0,         1,                 0},    /* 4 */
107658 107705             { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
107659 107706           };
107660         -        int iAddr;
107661         -        iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
107662         -        sqlite3VdbeChangeP1(v, iAddr, iDb);
107663         -        sqlite3VdbeChangeP1(v, iAddr+1, iDb);
107664         -        sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
107665         -        sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
107666         -        sqlite3VdbeChangeP1(v, iAddr+5, iDb);
       107707  +        VdbeOp *aOp;
       107708  +        int iAddr = sqlite3VdbeCurrentAddr(v);
       107709  +        sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
       107710  +        aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
       107711  +        if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
       107712  +        aOp[0].p1 = iDb;
       107713  +        aOp[1].p1 = iDb;
       107714  +        aOp[2].p2 = iAddr+4;
       107715  +        aOp[4].p1 = eAuto - 1;
       107716  +        aOp[5].p1 = iDb;
107667 107717           sqlite3VdbeUsesBtree(v, iDb);
107668 107718         }
107669 107719       }
107670 107720       break;
107671 107721     }
107672 107722   #endif
107673 107723   
................................................................................
108365 108415     /* Pragma "quick_check" is reduced version of 
108366 108416     ** integrity_check designed to detect most database corruption
108367 108417     ** without most of the overhead of a full integrity-check.
108368 108418     */
108369 108419     case PragTyp_INTEGRITY_CHECK: {
108370 108420       int i, j, addr, mxErr;
108371 108421   
108372         -    /* Code that appears at the end of the integrity check.  If no error
108373         -    ** messages have been generated, output OK.  Otherwise output the
108374         -    ** error message
108375         -    */
108376         -    static const int iLn = VDBE_OFFSET_LINENO(2);
108377         -    static const VdbeOpList endCode[] = {
108378         -      { OP_AddImm,      1, 0,        0},    /* 0 */
108379         -      { OP_If,          1, 0,        0},    /* 1 */
108380         -      { OP_String8,     0, 3,        0},    /* 2 */
108381         -      { OP_ResultRow,   3, 1,        0},
108382         -    };
108383         -
108384 108422       int isQuick = (sqlite3Tolower(zLeft[0])=='q');
108385 108423   
108386 108424       /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
108387 108425       ** then iDb is set to the index of the database identified by <db>.
108388 108426       ** In this case, the integrity of database iDb only is verified by
108389 108427       ** the VDBE created below.
108390 108428       **
................................................................................
108573 108611             sqlite3VdbeLoadString(v, 3, pIdx->zName);
108574 108612             sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
108575 108613             sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
108576 108614           }
108577 108615   #endif /* SQLITE_OMIT_BTREECOUNT */
108578 108616         } 
108579 108617       }
108580         -    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
108581         -    sqlite3VdbeChangeP2(v, addr, -mxErr);
108582         -    sqlite3VdbeJumpHere(v, addr+1);
108583         -    sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
       108618  +    {
       108619  +      static const int iLn = VDBE_OFFSET_LINENO(2);
       108620  +      static const VdbeOpList endCode[] = {
       108621  +        { OP_AddImm,      1, 0,        0},    /* 0 */
       108622  +        { OP_If,          1, 0,        0},    /* 1 */
       108623  +        { OP_String8,     0, 3,        0},    /* 2 */
       108624  +        { OP_ResultRow,   3, 1,        0},
       108625  +      };
       108626  +      VdbeOp *aOp;
       108627  +
       108628  +      aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
       108629  +      if( aOp ){
       108630  +        aOp[0].p2 = -mxErr;
       108631  +        aOp[1].p2 = sqlite3VdbeCurrentAddr(v);
       108632  +        aOp[2].p4type = P4_STATIC;
       108633  +        aOp[2].p4.z = "ok";
       108634  +      }
       108635  +    }
108584 108636     }
108585 108637     break;
108586 108638   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
108587 108639   
108588 108640   #ifndef SQLITE_OMIT_UTF16
108589 108641     /*
108590 108642     **   PRAGMA encoding
................................................................................
108693 108745       if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
108694 108746         /* Write the specified cookie value */
108695 108747         static const VdbeOpList setCookie[] = {
108696 108748           { OP_Transaction,    0,  1,  0},    /* 0 */
108697 108749           { OP_Integer,        0,  1,  0},    /* 1 */
108698 108750           { OP_SetCookie,      0,  0,  1},    /* 2 */
108699 108751         };
108700         -      int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
108701         -      sqlite3VdbeChangeP1(v, addr, iDb);
108702         -      sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
108703         -      sqlite3VdbeChangeP1(v, addr+2, iDb);
108704         -      sqlite3VdbeChangeP2(v, addr+2, iCookie);
       108752  +      VdbeOp *aOp;
       108753  +      sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
       108754  +      aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
       108755  +      if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
       108756  +      aOp[0].p1 = iDb;
       108757  +      aOp[1].p1 = sqlite3Atoi(zRight);
       108758  +      aOp[2].p1 = iDb;
       108759  +      aOp[2].p2 = iCookie;
108705 108760       }else{
108706 108761         /* Read the specified cookie value */
108707 108762         static const VdbeOpList readCookie[] = {
108708 108763           { OP_Transaction,     0,  0,  0},    /* 0 */
108709 108764           { OP_ReadCookie,      0,  1,  0},    /* 1 */
108710 108765           { OP_ResultRow,       1,  1,  0}
108711 108766         };
108712         -      int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
108713         -      sqlite3VdbeChangeP1(v, addr, iDb);
108714         -      sqlite3VdbeChangeP1(v, addr+1, iDb);
108715         -      sqlite3VdbeChangeP3(v, addr+1, iCookie);
       108767  +      VdbeOp *aOp;
       108768  +      sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
       108769  +      aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
       108770  +      if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
       108771  +      aOp[0].p1 = iDb;
       108772  +      aOp[1].p1 = iDb;
       108773  +      aOp[1].p3 = iCookie;
108716 108774         sqlite3VdbeSetNumCols(v, 1);
108717 108775         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
108718 108776       }
108719 108777     }
108720 108778     break;
108721 108779   #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
108722 108780   
................................................................................
109075 109133   */
109076 109134   static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
109077 109135     int rc;
109078 109136     int i;
109079 109137   #ifndef SQLITE_OMIT_DEPRECATED
109080 109138     int size;
109081 109139   #endif
109082         -  Table *pTab;
109083 109140     Db *pDb;
109084 109141     char const *azArg[4];
109085 109142     int meta[5];
109086 109143     InitData initData;
109087         -  char const *zMasterSchema;
109088         -  char const *zMasterName;
       109144  +  const char *zMasterName;
109089 109145     int openedTransaction = 0;
109090 109146   
109091         -  /*
109092         -  ** The master database table has a structure like this
109093         -  */
109094         -  static const char master_schema[] = 
109095         -     "CREATE TABLE sqlite_master(\n"
109096         -     "  type text,\n"
109097         -     "  name text,\n"
109098         -     "  tbl_name text,\n"
109099         -     "  rootpage integer,\n"
109100         -     "  sql text\n"
109101         -     ")"
109102         -  ;
109103         -#ifndef SQLITE_OMIT_TEMPDB
109104         -  static const char temp_master_schema[] = 
109105         -     "CREATE TEMP TABLE sqlite_temp_master(\n"
109106         -     "  type text,\n"
109107         -     "  name text,\n"
109108         -     "  tbl_name text,\n"
109109         -     "  rootpage integer,\n"
109110         -     "  sql text\n"
109111         -     ")"
109112         -  ;
109113         -#else
109114         -  #define temp_master_schema 0
109115         -#endif
109116         -
109117 109147     assert( iDb>=0 && iDb<db->nDb );
109118 109148     assert( db->aDb[iDb].pSchema );
109119 109149     assert( sqlite3_mutex_held(db->mutex) );
109120 109150     assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
109121 109151   
109122         -  /* zMasterSchema and zInitScript are set to point at the master schema
109123         -  ** and initialisation script appropriate for the database being
109124         -  ** initialized. zMasterName is the name of the master table.
109125         -  */
109126         -  if( !OMIT_TEMPDB && iDb==1 ){
109127         -    zMasterSchema = temp_master_schema;
109128         -  }else{
109129         -    zMasterSchema = master_schema;
109130         -  }
109131         -  zMasterName = SCHEMA_TABLE(iDb);
109132         -
109133         -  /* Construct the schema tables.  */
109134         -  azArg[0] = zMasterName;
       109152  +  /* Construct the in-memory representation schema tables (sqlite_master or
       109153  +  ** sqlite_temp_master) by invoking the parser directly.  The appropriate
       109154  +  ** table name will be inserted automatically by the parser so we can just
       109155  +  ** use the abbreviation "x" here.  The parser will also automatically tag
       109156  +  ** the schema table as read-only. */
       109157  +  azArg[0] = zMasterName = SCHEMA_TABLE(iDb);
109135 109158     azArg[1] = "1";
109136         -  azArg[2] = zMasterSchema;
       109159  +  azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
       109160  +                            "rootpage integer,sql text)";
109137 109161     azArg[3] = 0;
109138 109162     initData.db = db;
109139 109163     initData.iDb = iDb;
109140 109164     initData.rc = SQLITE_OK;
109141 109165     initData.pzErrMsg = pzErrMsg;
109142 109166     sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
109143 109167     if( initData.rc ){
109144 109168       rc = initData.rc;
109145 109169       goto error_out;
109146 109170     }
109147         -  pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
109148         -  if( ALWAYS(pTab) ){
109149         -    pTab->tabFlags |= TF_Readonly;
109150         -  }
109151 109171   
109152 109172     /* Create a cursor to hold the database open
109153 109173     */
109154 109174     pDb = &db->aDb[iDb];
109155 109175     if( pDb->pBt==0 ){
109156 109176       if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
109157 109177         DbSetProperty(db, 1, DB_SchemaLoaded);
................................................................................
109262 109282   
109263 109283     /* Read the schema information out of the schema tables
109264 109284     */
109265 109285     assert( db->init.busy );
109266 109286     {
109267 109287       char *zSql;
109268 109288       zSql = sqlite3MPrintf(db, 
109269         -        "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
       109289  +        "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid",
109270 109290           db->aDb[iDb].zName, zMasterName);
109271 109291   #ifndef SQLITE_OMIT_AUTHORIZATION
109272 109292       {
109273 109293         sqlite3_xauth xAuth;
109274 109294         xAuth = db->xAuth;
109275 109295         db->xAuth = 0;
109276 109296   #endif
................................................................................
110839 110859   }
110840 110860   
110841 110861   /*
110842 110862   ** Allocate a KeyInfo object sufficient for an index of N key columns and
110843 110863   ** X extra columns.
110844 110864   */
110845 110865   SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
110846         -  KeyInfo *p = sqlite3DbMallocZero(0, 
110847         -                   sizeof(KeyInfo) + (N+X)*(sizeof(CollSeq*)+1));
       110866  +  int nExtra = (N+X)*(sizeof(CollSeq*)+1);
       110867  +  KeyInfo *p = sqlite3Malloc(sizeof(KeyInfo) + nExtra);
110848 110868     if( p ){
110849 110869       p->aSortOrder = (u8*)&p->aColl[N+X];
110850 110870       p->nField = (u16)N;
110851 110871       p->nXField = (u16)X;
110852 110872       p->enc = ENC(db);
110853 110873       p->db = db;
110854 110874       p->nRef = 1;
       110875  +    memset(&p[1], 0, nExtra);
110855 110876     }else{
110856 110877       db->mallocFailed = 1;
110857 110878     }
110858 110879     return p;
110859 110880   }
110860 110881   
110861 110882   /*
................................................................................
116636 116657     assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
116637 116658   
116638 116659     /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program 
116639 116660     ** is a pointer to the sub-vdbe containing the trigger program.  */
116640 116661     if( pPrg ){
116641 116662       int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
116642 116663   
116643         -    sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
116644         -    sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
       116664  +    sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
       116665  +                      (const char *)pPrg->pProgram, P4_SUBPROGRAM);
116645 116666       VdbeComment(
116646 116667           (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
116647 116668   
116648 116669       /* Set the P5 operand of the OP_Program instruction to non-zero if
116649 116670       ** recursive invocation of this trigger program is disallowed. Recursive
116650 116671       ** invocation is disallowed if (a) the sub-program is really a trigger,
116651 116672       ** not a foreign key action, and (b) the flag to enable recursive triggers
................................................................................
118991 119012     FuncDef *pDef,  /* Function to possibly overload */
118992 119013     int nArg,       /* Number of arguments to the function */
118993 119014     Expr *pExpr     /* First argument to the function */
118994 119015   ){
118995 119016     Table *pTab;
118996 119017     sqlite3_vtab *pVtab;
118997 119018     sqlite3_module *pMod;
118998         -  void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
       119019  +  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
118999 119020     void *pArg = 0;
119000 119021     FuncDef *pNew;
119001 119022     int rc = 0;
119002 119023     char *zLowerName;
119003 119024     unsigned char *z;
119004 119025   
119005 119026   
................................................................................
119019 119040     ** to see if the implementation wants to overload this function 
119020 119041     */
119021 119042     zLowerName = sqlite3DbStrDup(db, pDef->zName);
119022 119043     if( zLowerName ){
119023 119044       for(z=(unsigned char*)zLowerName; *z; z++){
119024 119045         *z = sqlite3UpperToLower[*z];
119025 119046       }
119026         -    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
       119047  +    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg);
119027 119048       sqlite3DbFree(db, zLowerName);
119028 119049     }
119029 119050     if( rc==0 ){
119030 119051       return pDef;
119031 119052     }
119032 119053   
119033 119054     /* Create a new ephemeral function definition for the overloaded
................................................................................
119036 119057                                + sqlite3Strlen30(pDef->zName) + 1);
119037 119058     if( pNew==0 ){
119038 119059       return pDef;
119039 119060     }
119040 119061     *pNew = *pDef;
119041 119062     pNew->zName = (char *)&pNew[1];
119042 119063     memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
119043         -  pNew->xFunc = xFunc;
       119064  +  pNew->xSFunc = xSFunc;
119044 119065     pNew->pUserData = pArg;
119045 119066     pNew->funcFlags |= SQLITE_FUNC_EPHEM;
119046 119067     return pNew;
119047 119068   }
119048 119069   
119049 119070   /*
119050 119071   ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
................................................................................
120056 120077     }
120057 120078     while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
120058 120079       n--;
120059 120080     }
120060 120081   
120061 120082     /* Code the OP_Affinity opcode if there is anything left to do. */
120062 120083     if( n>0 ){
120063         -    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
120064         -    sqlite3VdbeChangeP4(v, -1, zAff, n);
       120084  +    sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n);
120065 120085       sqlite3ExprCacheAffinityChange(pParse, base, n);
120066 120086     }
120067 120087   }
120068 120088   
120069 120089   
120070 120090   /*
120071 120091   ** Generate code for a single equality term of the WHERE clause.  An equality
................................................................................
133809 133829   */
133810 133830   SQLITE_PRIVATE int sqlite3CreateFunc(
133811 133831     sqlite3 *db,
133812 133832     const char *zFunctionName,
133813 133833     int nArg,
133814 133834     int enc,
133815 133835     void *pUserData,
133816         -  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
       133836  +  void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
133817 133837     void (*xStep)(sqlite3_context*,int,sqlite3_value **),
133818 133838     void (*xFinal)(sqlite3_context*),
133819 133839     FuncDestructor *pDestructor
133820 133840   ){
133821 133841     FuncDef *p;
133822 133842     int nName;
133823 133843     int extraFlags;
133824 133844   
133825 133845     assert( sqlite3_mutex_held(db->mutex) );
133826 133846     if( zFunctionName==0 ||
133827         -      (xFunc && (xFinal || xStep)) || 
133828         -      (!xFunc && (xFinal && !xStep)) ||
133829         -      (!xFunc && (!xFinal && xStep)) ||
       133847  +      (xSFunc && (xFinal || xStep)) || 
       133848  +      (!xSFunc && (xFinal && !xStep)) ||
       133849  +      (!xSFunc && (!xFinal && xStep)) ||
133830 133850         (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
133831 133851         (255<(nName = sqlite3Strlen30( zFunctionName))) ){
133832 133852       return SQLITE_MISUSE_BKPT;
133833 133853     }
133834 133854   
133835 133855     assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
133836 133856     extraFlags = enc &  SQLITE_DETERMINISTIC;
................................................................................
133845 133865     ** to the hash table.
133846 133866     */
133847 133867     if( enc==SQLITE_UTF16 ){
133848 133868       enc = SQLITE_UTF16NATIVE;
133849 133869     }else if( enc==SQLITE_ANY ){
133850 133870       int rc;
133851 133871       rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags,
133852         -         pUserData, xFunc, xStep, xFinal, pDestructor);
       133872  +         pUserData, xSFunc, xStep, xFinal, pDestructor);
133853 133873       if( rc==SQLITE_OK ){
133854 133874         rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags,
133855         -          pUserData, xFunc, xStep, xFinal, pDestructor);
       133875  +          pUserData, xSFunc, xStep, xFinal, pDestructor);
133856 133876       }
133857 133877       if( rc!=SQLITE_OK ){
133858 133878         return rc;
133859 133879       }
133860 133880       enc = SQLITE_UTF16BE;
133861 133881     }
133862 133882   #else
................................................................................
133892 133912   
133893 133913     if( pDestructor ){
133894 133914       pDestructor->nRef++;
133895 133915     }
133896 133916     p->pDestructor = pDestructor;
133897 133917     p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
133898 133918     testcase( p->funcFlags & SQLITE_DETERMINISTIC );
133899         -  p->xFunc = xFunc;
133900         -  p->xStep = xStep;
       133919  +  p->xSFunc = xSFunc ? xSFunc : xStep;
133901 133920     p->xFinalize = xFinal;
133902 133921     p->pUserData = pUserData;
133903 133922     p->nArg = (u16)nArg;
133904 133923     return SQLITE_OK;
133905 133924   }
133906 133925   
133907 133926   /*
................................................................................
133909 133928   */
133910 133929   SQLITE_API int SQLITE_STDCALL sqlite3_create_function(
133911 133930     sqlite3 *db,
133912 133931     const char *zFunc,
133913 133932     int nArg,
133914 133933     int enc,
133915 133934     void *p,
133916         -  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
       133935  +  void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
133917 133936     void (*xStep)(sqlite3_context*,int,sqlite3_value **),
133918 133937     void (*xFinal)(sqlite3_context*)
133919 133938   ){
133920         -  return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
       133939  +  return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xSFunc, xStep,
133921 133940                                       xFinal, 0);
133922 133941   }
133923 133942   
133924 133943   SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
133925 133944     sqlite3 *db,
133926 133945     const char *zFunc,
133927 133946     int nArg,
133928 133947     int enc,
133929 133948     void *p,
133930         -  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
       133949  +  void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
133931 133950     void (*xStep)(sqlite3_context*,int,sqlite3_value **),
133932 133951     void (*xFinal)(sqlite3_context*),
133933 133952     void (*xDestroy)(void *)
133934 133953   ){
133935 133954     int rc = SQLITE_ERROR;
133936 133955     FuncDestructor *pArg = 0;
133937 133956   
................................................................................
133946 133965       if( !pArg ){
133947 133966         xDestroy(p);
133948 133967         goto out;
133949 133968       }
133950 133969       pArg->xDestroy = xDestroy;
133951 133970       pArg->pUserData = p;
133952 133971     }
133953         -  rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
       133972  +  rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, pArg);
133954 133973     if( pArg && pArg->nRef==0 ){
133955 133974       assert( rc!=SQLITE_OK );
133956 133975       xDestroy(p);
133957 133976       sqlite3DbFree(db, pArg);
133958 133977     }
133959 133978   
133960 133979    out:
................................................................................
133966 133985   #ifndef SQLITE_OMIT_UTF16
133967 133986   SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
133968 133987     sqlite3 *db,
133969 133988     const void *zFunctionName,
133970 133989     int nArg,
133971 133990     int eTextRep,
133972 133991     void *p,
133973         -  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
       133992  +  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
133974 133993     void (*xStep)(sqlite3_context*,int,sqlite3_value**),
133975 133994     void (*xFinal)(sqlite3_context*)
133976 133995   ){
133977 133996     int rc;
133978 133997     char *zFunc8;
133979 133998   
133980 133999   #ifdef SQLITE_ENABLE_API_ARMOR
133981 134000     if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
133982 134001   #endif
133983 134002     sqlite3_mutex_enter(db->mutex);
133984 134003     assert( !db->mallocFailed );
133985 134004     zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
133986         -  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
       134005  +  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0);
133987 134006     sqlite3DbFree(db, zFunc8);
133988 134007     rc = sqlite3ApiExit(db, rc);
133989 134008     sqlite3_mutex_leave(db->mutex);
133990 134009     return rc;
133991 134010   }
133992 134011   #endif
133993 134012   
................................................................................
135204 135223     /* Enable the lookaside-malloc subsystem */
135205 135224     setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
135206 135225                           sqlite3GlobalConfig.nLookaside);
135207 135226   
135208 135227     sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
135209 135228   
135210 135229   opendb_out:
135211         -  sqlite3_free(zOpen);
135212 135230     if( db ){
135213 135231       assert( db->mutex!=0 || isThreadsafe==0
135214 135232              || sqlite3GlobalConfig.bFullMutex==0 );
135215 135233       sqlite3_mutex_leave(db->mutex);
135216 135234     }
135217 135235     rc = sqlite3_errcode(db);
135218 135236     assert( db!=0 || rc==SQLITE_NOMEM );
................................................................................
135241 135259           iByte = (iByte<<4) + sqlite3HexToInt(zHexKey[i]);
135242 135260           if( (i&1)!=0 ) zKey[i/2] = iByte;
135243 135261         }
135244 135262         sqlite3_key_v2(db, 0, zKey, i/2);
135245 135263       }
135246 135264     }
135247 135265   #endif
       135266  +  sqlite3_free(zOpen);
135248 135267     return rc & 0xff;
135249 135268   }
135250 135269   
135251 135270   /*
135252 135271   ** Open a new database handle.
135253 135272   */
135254 135273   SQLITE_API int SQLITE_STDCALL sqlite3_open(
................................................................................
168106 168125   
168107 168126   /*
168108 168127   ** Buffer object for the incremental building of string data.
168109 168128   */
168110 168129   typedef struct Fts5Buffer Fts5Buffer;
168111 168130   struct Fts5Buffer {
168112 168131     u8 *p;
168113         -  int n;
168114         -  int nSpace;
       168132  +  u32 n;
       168133  +  u32 nSpace;
168115 168134   };
168116 168135   
168117         -static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, int);
       168136  +static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
168118 168137   static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
168119         -static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, int, const u8*);
       168138  +static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
168120 168139   static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
168121 168140   static void sqlite3Fts5BufferFree(Fts5Buffer*);
168122 168141   static void sqlite3Fts5BufferZero(Fts5Buffer*);
168123 168142   static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
168124 168143   static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
168125 168144   
168126 168145   static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
................................................................................
168219 168238   **   sqlite3Fts5IndexQuery(p, "token", 5, 0, 0, &pIter);
168220 168239   **   0==sqlite3Fts5IterEof(pIter);
168221 168240   **   sqlite3Fts5IterNext(pIter)
168222 168241   ** ){
168223 168242   **   i64 iRowid = sqlite3Fts5IterRowid(pIter);
168224 168243   ** }
168225 168244   */
       168245  +
       168246  +/*
       168247  +** Return a simple checksum value based on the arguments.
       168248  +*/
       168249  +static u64 sqlite3Fts5IndexEntryCksum(
       168250  +  i64 iRowid, 
       168251  +  int iCol, 
       168252  +  int iPos, 
       168253  +  int iIdx,
       168254  +  const char *pTerm,
       168255  +  int nTerm
       168256  +);
       168257  +
       168258  +/*
       168259  +** Argument p points to a buffer containing utf-8 text that is n bytes in 
       168260  +** size. Return the number of bytes in the nChar character prefix of the
       168261  +** buffer, or 0 if there are less than nChar characters in total.
       168262  +*/
       168263  +static int sqlite3Fts5IndexCharlenToBytelen(
       168264  +  const char *p, 
       168265  +  int nByte, 
       168266  +  int nChar
       168267  +);
168226 168268   
168227 168269   /*
168228 168270   ** Open a new iterator to iterate though all rowids that match the 
168229 168271   ** specified token or token prefix.
168230 168272   */
168231 168273   static int sqlite3Fts5IndexQuery(
168232 168274     Fts5Index *p,                   /* FTS index to query */
................................................................................
168444 168486   static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
168445 168487   static int sqlite3Fts5StorageClose(Fts5Storage *p);
168446 168488   static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
168447 168489   
168448 168490   static int sqlite3Fts5DropAll(Fts5Config*);
168449 168491   static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
168450 168492   
168451         -static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64);
       168493  +static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**);
168452 168494   static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, sqlite3_value**, i64*);
168453 168495   static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
168454 168496   
168455 168497   static int sqlite3Fts5StorageIntegrity(Fts5Storage *p);
168456 168498   
168457 168499   static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
168458 168500   static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
................................................................................
168464 168506   static int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit);
168465 168507   static int sqlite3Fts5StorageRollback(Fts5Storage *p);
168466 168508   
168467 168509   static int sqlite3Fts5StorageConfigValue(
168468 168510       Fts5Storage *p, const char*, sqlite3_value*, int
168469 168511   );
168470 168512   
168471         -static int sqlite3Fts5StorageSpecialDelete(Fts5Storage *p, i64 iDel, sqlite3_value**);
168472         -
168473 168513   static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
168474 168514   static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
168475 168515   static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
168476 168516   static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
168477 168517   
168478 168518   /*
168479 168519   ** End of interface to code in fts5_storage.c.
................................................................................
170412 170452   ******************************************************************************
170413 170453   */
170414 170454   
170415 170455   
170416 170456   
170417 170457   /* #include "fts5Int.h" */
170418 170458   
170419         -static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, int nByte){
170420         -  int nNew = pBuf->nSpace ? pBuf->nSpace*2 : 64;
       170459  +static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
       170460  +  u32 nNew = pBuf->nSpace ? pBuf->nSpace*2 : 64;
170421 170461     u8 *pNew;
170422 170462     while( nNew<nByte ){
170423 170463       nNew = nNew * 2;
170424 170464     }
170425 170465     pNew = sqlite3_realloc(pBuf->p, nNew);
170426 170466     if( pNew==0 ){
170427 170467       *pRc = SQLITE_NOMEM;
................................................................................
170458 170498   ** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set 
170459 170499   ** the error code in p. If an error has already occurred when this function
170460 170500   ** is called, it is a no-op.
170461 170501   */
170462 170502   static void sqlite3Fts5BufferAppendBlob(
170463 170503     int *pRc,
170464 170504     Fts5Buffer *pBuf, 
170465         -  int nData, 
       170505  +  u32 nData, 
170466 170506     const u8 *pData
170467 170507   ){
170468         -  assert( *pRc || nData>=0 );
       170508  +  assert_nc( *pRc || nData>=0 );
170469 170509     if( fts5BufferGrow(pRc, pBuf, nData) ) return;
170470 170510     memcpy(&pBuf->p[pBuf->n], pData, nData);
170471 170511     pBuf->n += nData;
170472 170512   }
170473 170513   
170474 170514   /*
170475 170515   ** Append the nul-terminated string zStr to the buffer pBuf. This function
................................................................................
170719 170759     const char *pTerm, int nTerm, 
170720 170760     int *pbPresent
170721 170761   ){
170722 170762     int rc = SQLITE_OK;
170723 170763     *pbPresent = 0;
170724 170764     if( p ){
170725 170765       int i;
170726         -    int hash;
       170766  +    int hash = 13;
170727 170767       Fts5TermsetEntry *pEntry;
170728 170768   
170729         -    /* Calculate a hash value for this term */
170730         -    hash = 104 + iIdx;
170731         -    for(i=0; i<nTerm; i++){
170732         -      hash += (hash << 3) + (int)pTerm[i];
       170769  +    /* Calculate a hash value for this term. This is the same hash checksum
       170770  +    ** used by the fts5_hash.c module. This is not important for correct
       170771  +    ** operation of the module, but is necessary to ensure that some tests
       170772  +    ** designed to produce hash table collisions really do work.  */
       170773  +    for(i=nTerm-1; i>=0; i--){
       170774  +      hash = (hash << 3) ^ hash ^ pTerm[i];
170733 170775       }
       170776  +    hash = (hash << 3) ^ hash ^ iIdx;
170734 170777       hash = hash % ArraySize(p->apHash);
170735 170778   
170736 170779       for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
170737 170780         if( pEntry->iIdx==iIdx 
170738 170781             && pEntry->nTerm==nTerm 
170739 170782             && memcmp(pEntry->pTerm, pTerm, nTerm)==0 
170740 170783           ){
................................................................................
171053 171096         }
171054 171097   
171055 171098         while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
171056 171099           nPre = nPre*10 + (p[0] - '0');
171057 171100           p++;
171058 171101         }
171059 171102   
171060         -      if( rc==SQLITE_OK && (nPre<=0 || nPre>=1000) ){
       171103  +      if( nPre<=0 || nPre>=1000 ){
171061 171104           *pzErr = sqlite3_mprintf("prefix length out of range (max 999)");
171062 171105           rc = SQLITE_ERROR;
171063 171106           break;
171064 171107         }
171065 171108   
171066 171109         pConfig->aPrefix[pConfig->nPrefix] = nPre;
171067 171110         pConfig->nPrefix++;
................................................................................
172556 172599     int rc;
172557 172600   
172558 172601     assert( pNode->eType==FTS5_TERM );
172559 172602     assert( pNear->nPhrase==1 && pPhrase->nTerm==1 );
172560 172603     assert( pPhrase->aTerm[0].pSynonym==0 );
172561 172604   
172562 172605     rc = sqlite3Fts5IterPoslist(pIter, pColset, 
172563         -      (const u8**)&pPhrase->poslist.p, &pPhrase->poslist.n, &pNode->iRowid
       172606  +      (const u8**)&pPhrase->poslist.p, (int*)&pPhrase->poslist.n, &pNode->iRowid
172564 172607     );
172565 172608     pNode->bNomatch = (pPhrase->poslist.n==0);
172566 172609     return rc;
172567 172610   }
172568 172611   
172569 172612   /*
172570 172613   ** All individual term iterators in pNear are guaranteed to be valid when
................................................................................
174118 174161         aPopulator[i].bOk = 0;
174119 174162       }else{
174120 174163         aPopulator[i].bOk = 1;
174121 174164       }
174122 174165     }
174123 174166   
174124 174167     return sqlite3Fts5Tokenize(pConfig, 
174125         -      FTS5_TOKENIZE_AUX, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
       174168  +      FTS5_TOKENIZE_DOCUMENT, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
174126 174169     );
174127 174170   }
174128 174171   
174129 174172   static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
174130 174173     if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
174131 174174       pNode->pNear->apPhrase[0]->poslist.n = 0;
174132 174175     }else{
................................................................................
174134 174177       for(i=0; i<pNode->nChild; i++){
174135 174178         fts5ExprClearPoslists(pNode->apChild[i]);
174136 174179       }
174137 174180     }
174138 174181   }
174139 174182   
174140 174183   static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
174141         -  if( pNode ){
174142         -    pNode->iRowid = iRowid;
174143         -    pNode->bEof = 0;
174144         -    switch( pNode->eType ){
174145         -      case FTS5_TERM:
174146         -      case FTS5_STRING:
174147         -        return (pNode->pNear->apPhrase[0]->poslist.n>0);
174148         -
174149         -      case FTS5_AND: {
174150         -        int i;
174151         -        for(i=0; i<pNode->nChild; i++){
174152         -          if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
174153         -            fts5ExprClearPoslists(pNode);
174154         -            return 0;
174155         -          }
174156         -        }
174157         -        break;
174158         -      }
174159         -
174160         -      case FTS5_OR: {
174161         -        int i;
174162         -        int bRet = 0;
174163         -        for(i=0; i<pNode->nChild; i++){
174164         -          if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
174165         -            bRet = 1;
174166         -          }
174167         -        }
174168         -        if( bRet==0 ){
174169         -          fts5ExprClearPoslists(pNode);
174170         -        }
174171         -        return bRet;
174172         -      }
174173         -
174174         -      default: {
174175         -        assert( pNode->eType==FTS5_NOT );
174176         -        if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
174177         -         || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
174178         -        ){
       174184  +  pNode->iRowid = iRowid;
       174185  +  pNode->bEof = 0;
       174186  +  switch( pNode->eType ){
       174187  +    case FTS5_TERM:
       174188  +    case FTS5_STRING:
       174189  +      return (pNode->pNear->apPhrase[0]->poslist.n>0);
       174190  +
       174191  +    case FTS5_AND: {
       174192  +      int i;
       174193  +      for(i=0; i<pNode->nChild; i++){
       174194  +        if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
174179 174195             fts5ExprClearPoslists(pNode);
174180 174196             return 0;
174181 174197           }
174182         -        break;
174183 174198         }
       174199  +      break;
       174200  +    }
       174201  +
       174202  +    case FTS5_OR: {
       174203  +      int i;
       174204  +      int bRet = 0;
       174205  +      for(i=0; i<pNode->nChild; i++){
       174206  +        if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
       174207  +          bRet = 1;
       174208  +        }
       174209  +      }
       174210  +      return bRet;
       174211  +    }
       174212  +
       174213  +    default: {
       174214  +      assert( pNode->eType==FTS5_NOT );
       174215  +      if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
       174216  +          || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
       174217  +        ){
       174218  +        fts5ExprClearPoslists(pNode);
       174219  +        return 0;
       174220  +      }
       174221  +      break;
174184 174222       }
174185 174223     }
174186 174224     return 1;
174187 174225   }
174188 174226   
174189 174227   static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
174190 174228     fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
................................................................................
176645 176683     Fts5SegIter *pIter,             /* Iterator to advance */
176646 176684     int *pbNewTerm                  /* OUT: Set for new term */
176647 176685   ){
176648 176686     Fts5Data *pLeaf = pIter->pLeaf;
176649 176687     int iOff;
176650 176688     int bNewTerm = 0;
176651 176689     int nKeep = 0;
       176690  +  u8 *a;
       176691  +  int n;
176652 176692   
176653 176693     assert( pbNewTerm==0 || *pbNewTerm==0 );
176654 176694     assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
176655 176695   
176656 176696     /* Search for the end of the position list within the current page. */
176657         -  u8 *a = pLeaf->p;
176658         -  int n = pLeaf->szLeaf;
       176697  +  a = pLeaf->p;
       176698  +  n = pLeaf->szLeaf;
176659 176699   
176660 176700     ASSERT_SZLEAF_OK(pLeaf);
176661 176701     iOff = pIter->iLeafOffset + pIter->nPos;
176662 176702   
176663 176703     if( iOff<n ){
176664 176704       /* The next entry is on the current page. */
176665 176705       assert_nc( iOff<=pIter->iEndofDoclist );
................................................................................
180671 180711         iDocid += iDelta;
180672 180712         sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
180673 180713       }
180674 180714     }
180675 180715   
180676 180716     return iOff;
180677 180717   }
       180718  +
       180719  +/*
       180720  +** This function is part of the fts5_decode() debugging function. It is 
       180721  +** only ever used with detail=none tables.
       180722  +**
       180723  +** Buffer (pData/nData) contains a doclist in the format used by detail=none
       180724  +** tables. This function appends a human-readable version of that list to
       180725  +** buffer pBuf.
       180726  +**
       180727  +** If *pRc is other than SQLITE_OK when this function is called, it is a
       180728  +** no-op. If an OOM or other error occurs within this function, *pRc is
       180729  +** set to an SQLite error code before returning. The final state of buffer
       180730  +** pBuf is undefined in this case.
       180731  +*/
       180732  +static void fts5DecodeRowidList(
       180733  +  int *pRc,                       /* IN/OUT: Error code */
       180734  +  Fts5Buffer *pBuf,               /* Buffer to append text to */
       180735  +  const u8 *pData, int nData      /* Data to decode list-of-rowids from */
       180736  +){
       180737  +  int i = 0;
       180738  +  i64 iRowid = 0;
       180739  +
       180740  +  while( i<nData ){
       180741  +    const char *zApp = "";
       180742  +    u64 iVal;
       180743  +    i += sqlite3Fts5GetVarint(&pData[i], &iVal);
       180744  +    iRowid += iVal;
       180745  +
       180746  +    if( i<nData && pData[i]==0x00 ){
       180747  +      i++;
       180748  +      if( i<nData && pData[i]==0x00 ){
       180749  +        i++;
       180750  +        zApp = "+";
       180751  +      }else{
       180752  +        zApp = "*";
       180753  +      }
       180754  +    }
       180755  +
       180756  +    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
       180757  +  }
       180758  +}
180678 180759   
180679 180760   /*
180680 180761   ** The implementation of user-defined scalar function fts5_decode().
180681 180762   */
180682 180763   static void fts5DecodeFunction(
180683 180764     sqlite3_context *pCtx,          /* Function call context */
180684 180765     int nArg,                       /* Number of args (always 2) */
................................................................................
180687 180768     i64 iRowid;                     /* Rowid for record being decoded */
180688 180769     int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
180689 180770     const u8 *aBlob; int n;         /* Record to decode */
180690 180771     u8 *a = 0;
180691 180772     Fts5Buffer s;                   /* Build up text to return here */
180692 180773     int rc = SQLITE_OK;             /* Return code */
180693 180774     int nSpace = 0;
       180775  +  int eDetailNone = (sqlite3_user_data(pCtx)!=0);
180694 180776   
180695 180777     assert( nArg==2 );
180696 180778     memset(&s, 0, sizeof(Fts5Buffer));
180697 180779     iRowid = sqlite3_value_int64(apVal[0]);
180698 180780   
180699 180781     /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
180700 180782     ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
................................................................................
180728 180810       }
180729 180811     }else if( iSegid==0 ){
180730 180812       if( iRowid==FTS5_AVERAGES_ROWID ){
180731 180813         fts5DecodeAverages(&rc, &s, a, n);
180732 180814       }else{
180733 180815         fts5DecodeStructure(&rc, &s, a, n);
180734 180816       }
       180817  +  }else if( eDetailNone ){
       180818  +    Fts5Buffer term;              /* Current term read from page */
       180819  +    int szLeaf;
       180820  +    int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
       180821  +    int iTermOff;
       180822  +    int nKeep = 0;
       180823  +    int iOff;
       180824  +
       180825  +    memset(&term, 0, sizeof(Fts5Buffer));
       180826  +
       180827  +    /* Decode any entries that occur before the first term. */
       180828  +    if( szLeaf<n ){
       180829  +      iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff);
       180830  +    }else{
       180831  +      iTermOff = szLeaf;
       180832  +    }
       180833  +    fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
       180834  +
       180835  +    iOff = iTermOff;
       180836  +    while( iOff<szLeaf ){
       180837  +      int nAppend;
       180838  +
       180839  +      /* Read the term data for the next term*/
       180840  +      iOff += fts5GetVarint32(&a[iOff], nAppend);
       180841  +      term.n = nKeep;
       180842  +      fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff]);
       180843  +      sqlite3Fts5BufferAppendPrintf(
       180844  +          &rc, &s, " term=%.*s", term.n, (const char*)term.p
       180845  +      );
       180846  +      iOff += nAppend;
       180847  +
       180848  +      /* Figure out where the doclist for this term ends */
       180849  +      if( iPgidxOff<n ){
       180850  +        int nIncr;
       180851  +        iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr);
       180852  +        iTermOff += nIncr;
       180853  +      }else{
       180854  +        iTermOff = szLeaf;
       180855  +      }
       180856  +
       180857  +      fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
       180858  +      iOff = iTermOff;
       180859  +      if( iOff<szLeaf ){
       180860  +        iOff += fts5GetVarint32(&a[iOff], nKeep);
       180861  +      }
       180862  +    }
       180863  +
       180864  +    fts5BufferFree(&term);
180735 180865     }else{
180736 180866       Fts5Buffer term;              /* Current term read from page */
180737 180867       int szLeaf;                   /* Offset of pgidx in a[] */
180738 180868       int iPgidxOff;
180739 180869       int iPgidxPrev = 0;           /* Previous value read from pgidx */
180740 180870       int iTermOff = 0;
180741 180871       int iRowidOff = 0;
................................................................................
180855 180985   ** If successful, SQLITE_OK is returned. If an error occurs, some other
180856 180986   ** SQLite error code is returned instead.
180857 180987   */
180858 180988   static int sqlite3Fts5IndexInit(sqlite3 *db){
180859 180989     int rc = sqlite3_create_function(
180860 180990         db, "fts5_decode", 2, SQLITE_UTF8, 0, fts5DecodeFunction, 0, 0
180861 180991     );
       180992  +
       180993  +  if( rc==SQLITE_OK ){
       180994  +    rc = sqlite3_create_function(
       180995  +        db, "fts5_decode_none", 2, 
       180996  +        SQLITE_UTF8, (void*)db, fts5DecodeFunction, 0, 0
       180997  +    );
       180998  +  }
       180999  +
180862 181000     if( rc==SQLITE_OK ){
180863 181001       rc = sqlite3_create_function(
180864 181002           db, "fts5_rowid", -1, SQLITE_UTF8, 0, fts5RowidFunction, 0, 0
180865 181003       );
180866 181004     }
180867 181005     return rc;
180868 181006   }
180869         -
180870 181007   
180871 181008   /*
180872 181009   ** 2014 Jun 09
180873 181010   **
180874 181011   ** The author disclaims copyright to this source code.  In place of
180875 181012   ** a legal notice, here is a blessing:
180876 181013   **
................................................................................
181710 181847       }
181711 181848     }
181712 181849     
181713 181850     return rc;
181714 181851   }
181715 181852   
181716 181853   
181717         -static sqlite3_stmt *fts5PrepareStatement(
181718         -  int *pRc,
       181854  +static int fts5PrepareStatement(
       181855  +  sqlite3_stmt **ppStmt,
181719 181856     Fts5Config *pConfig, 
181720 181857     const char *zFmt,
181721 181858     ...
181722 181859   ){
181723 181860     sqlite3_stmt *pRet = 0;
       181861  +  int rc;
       181862  +  char *zSql;
181724 181863     va_list ap;
       181864  +
181725 181865     va_start(ap, zFmt);
181726         -
181727         -  if( *pRc==SQLITE_OK ){
181728         -    int rc;
181729         -    char *zSql = sqlite3_vmprintf(zFmt, ap);
181730         -    if( zSql==0 ){
181731         -      rc = SQLITE_NOMEM; 
181732         -    }else{
181733         -      rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pRet, 0);
181734         -      if( rc!=SQLITE_OK ){
181735         -        *pConfig->pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(pConfig->db));
181736         -      }
181737         -      sqlite3_free(zSql);
       181866  +  zSql = sqlite3_vmprintf(zFmt, ap);
       181867  +  if( zSql==0 ){
       181868  +    rc = SQLITE_NOMEM; 
       181869  +  }else{
       181870  +    rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pRet, 0);
       181871  +    if( rc!=SQLITE_OK ){
       181872  +      *pConfig->pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(pConfig->db));
181738 181873       }
181739         -    *pRc = rc;
       181874  +    sqlite3_free(zSql);
181740 181875     }
181741 181876   
181742 181877     va_end(ap);
181743         -  return pRet;
       181878  +  *ppStmt = pRet;
       181879  +  return rc;
181744 181880   } 
181745 181881   
181746 181882   static int fts5CursorFirstSorted(Fts5Table *pTab, Fts5Cursor *pCsr, int bDesc){
181747 181883     Fts5Config *pConfig = pTab->pConfig;
181748 181884     Fts5Sorter *pSorter;
181749 181885     int nPhrase;
181750 181886     int nByte;
181751         -  int rc = SQLITE_OK;
       181887  +  int rc;
181752 181888     const char *zRank = pCsr->zRank;
181753 181889     const char *zRankArgs = pCsr->zRankArgs;
181754 181890     
181755 181891     nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
181756 181892     nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1);
181757 181893     pSorter = (Fts5Sorter*)sqlite3_malloc(nByte);
181758 181894     if( pSorter==0 ) return SQLITE_NOMEM;
................................................................................
181762 181898     /* TODO: It would be better to have some system for reusing statement
181763 181899     ** handles here, rather than preparing a new one for each query. But that
181764 181900     ** is not possible as SQLite reference counts the virtual table objects.
181765 181901     ** And since the statement required here reads from this very virtual 
181766 181902     ** table, saving it creates a circular reference.
181767 181903     **
181768 181904     ** If SQLite a built-in statement cache, this wouldn't be a problem. */
181769         -  pSorter->pStmt = fts5PrepareStatement(&rc, pConfig,
       181905  +  rc = fts5PrepareStatement(&pSorter->pStmt, pConfig,
181770 181906         "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(%s%s%s) %s",
181771 181907         pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
181772 181908         (zRankArgs ? ", " : ""),
181773 181909         (zRankArgs ? zRankArgs : ""),
181774 181910         bDesc ? "DESC" : "ASC"
181775 181911     );
181776 181912   
................................................................................
182271 182407     sqlite3_value **apVal, 
182272 182408     sqlite3_int64 *piRowid
182273 182409   ){
182274 182410     int rc = SQLITE_OK;
182275 182411     int eType1 = sqlite3_value_type(apVal[1]);
182276 182412     if( eType1==SQLITE_INTEGER ){
182277 182413       sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
182278         -    rc = sqlite3Fts5StorageSpecialDelete(pTab->pStorage, iDel, &apVal[2]);
       182414  +    rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2]);
182279 182415     }
182280 182416     return rc;
182281 182417   }
182282 182418   
182283 182419   static void fts5StorageInsert(
182284 182420     int *pRc, 
182285 182421     Fts5Table *pTab, 
................................................................................
182378 182514         );
182379 182515         rc = SQLITE_ERROR;
182380 182516       }
182381 182517   
182382 182518       /* Case 1: DELETE */
182383 182519       else if( nArg==1 ){
182384 182520         i64 iDel = sqlite3_value_int64(apVal[0]);  /* Rowid to delete */
182385         -      rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel);
       182521  +      rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0);
182386 182522       }
182387 182523   
182388 182524       /* Case 2: INSERT */
182389 182525       else if( eType0!=SQLITE_INTEGER ){     
182390 182526         /* If this is a REPLACE, first remove the current entry (if any) */
182391 182527         if( eConflict==SQLITE_REPLACE 
182392 182528          && sqlite3_value_type(apVal[1])==SQLITE_INTEGER 
182393 182529         ){
182394 182530           i64 iNew = sqlite3_value_int64(apVal[1]);  /* Rowid to delete */
182395         -        rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew);
       182531  +        rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
182396 182532         }
182397 182533         fts5StorageInsert(&rc, pTab, apVal, pRowid);
182398 182534       }
182399 182535   
182400 182536       /* Case 2: UPDATE */
182401 182537       else{
182402 182538         i64 iOld = sqlite3_value_int64(apVal[0]);  /* Old rowid */
182403 182539         i64 iNew = sqlite3_value_int64(apVal[1]);  /* New rowid */
182404 182540         if( iOld!=iNew ){
182405 182541           if( eConflict==SQLITE_REPLACE ){
182406         -          rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld);
       182542  +          rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
182407 182543             if( rc==SQLITE_OK ){
182408         -            rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew);
       182544  +            rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
182409 182545             }
182410 182546             fts5StorageInsert(&rc, pTab, apVal, pRowid);
182411 182547           }else{
182412 182548             rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, pRowid);
182413 182549             if( rc==SQLITE_OK ){
182414         -            rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld);
       182550  +            rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
182415 182551             }
182416 182552             if( rc==SQLITE_OK ){
182417 182553               rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *pRowid);
182418 182554             }
182419 182555           }
182420 182556         }else{
182421         -        rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld);
       182557  +        rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
182422 182558           fts5StorageInsert(&rc, pTab, apVal, pRowid);
182423 182559         }
182424 182560       }
182425 182561     }
182426 182562   
182427 182563     pTab->pConfig->pzErrmsg = 0;
182428 182564     return rc;
................................................................................
182613 182749       int i;
182614 182750   
182615 182751       /* Initialize all iterators */
182616 182752       for(i=0; i<nIter && rc==SQLITE_OK; i++){
182617 182753         const u8 *a;
182618 182754         int n; 
182619 182755         rc = fts5CsrPoslist(pCsr, i, &a, &n);
182620         -      sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
       182756  +      if( rc==SQLITE_OK ){
       182757  +        sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
       182758  +      }
182621 182759       }
182622 182760   
182623 182761       if( rc==SQLITE_OK ){
182624 182762         while( 1 ){
182625 182763           int *aInst;
182626 182764           int iBest = -1;
182627 182765           for(i=0; i<nIter; i++){
................................................................................
182903 183041     int *piCol
182904 183042   ){
182905 183043     int rc = SQLITE_OK;
182906 183044     Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
182907 183045     Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
182908 183046   
182909 183047     if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
       183048  +    Fts5Sorter *pSorter = pCsr->pSorter;
182910 183049       int n;
182911         -    rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n);
       183050  +    if( pSorter ){
       183051  +      int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
       183052  +      n = pSorter->aIdx[iPhrase] - i1;
       183053  +      pIter->a = &pSorter->aPoslist[i1];
       183054  +    }else{
       183055  +      rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n);
       183056  +    }
182912 183057       if( rc==SQLITE_OK ){
182913 183058         pIter->b = &pIter->a[n];
182914 183059         *piCol = 0;
182915 183060         fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
182916 183061       }
182917 183062     }else{
182918 183063       int n;
................................................................................
183449 183594   */
183450 183595   static void fts5SourceIdFunc(
183451 183596     sqlite3_context *pCtx,          /* Function call context */
183452 183597     int nArg,                       /* Number of args */
183453 183598     sqlite3_value **apVal           /* Function arguments */
183454 183599   ){
183455 183600     assert( nArg==0 );
183456         -  sqlite3_result_text(pCtx, "fts5: 2016-01-14 14:19:50 d17bc2c92f4d086280e49a3cc72993be7fee2da7", -1, SQLITE_TRANSIENT);
       183601  +  sqlite3_result_text(pCtx, "fts5: 2016-01-18 17:48:28 acaf426449bf6fd3140fd63141750ff69d1119a5", -1, SQLITE_TRANSIENT);
183457 183602   }
183458 183603   
183459 183604   static int fts5Init(sqlite3 *db){
183460 183605     static const sqlite3_module fts5Mod = {
183461 183606       /* iVersion      */ 2,
183462 183607       /* xCreate       */ fts5CreateMethod,
183463 183608       /* xConnect      */ fts5ConnectMethod,
................................................................................
183934 184079   }
183935 184080   
183936 184081   /*
183937 184082   ** If a row with rowid iDel is present in the %_content table, add the
183938 184083   ** delete-markers to the FTS index necessary to delete it. Do not actually
183939 184084   ** remove the %_content row at this time though.
183940 184085   */
183941         -static int fts5StorageDeleteFromIndex(Fts5Storage *p, i64 iDel){
       184086  +static int fts5StorageDeleteFromIndex(
       184087  +  Fts5Storage *p, 
       184088  +  i64 iDel, 
       184089  +  sqlite3_value **apVal
       184090  +){
183942 184091     Fts5Config *pConfig = p->pConfig;
183943         -  sqlite3_stmt *pSeek;            /* SELECT to read row iDel from %_data */
       184092  +  sqlite3_stmt *pSeek = 0;        /* SELECT to read row iDel from %_data */
183944 184093     int rc;                         /* Return code */
       184094  +  int rc2;                        /* sqlite3_reset() return code */
       184095  +  int iCol;
       184096  +  Fts5InsertCtx ctx;
183945 184097   
183946         -  rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP, &pSeek, 0);
183947         -  if( rc==SQLITE_OK ){
183948         -    int rc2;
       184098  +  if( apVal==0 ){
       184099  +    rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP, &pSeek, 0);
       184100  +    if( rc!=SQLITE_OK ) return rc;
183949 184101       sqlite3_bind_int64(pSeek, 1, iDel);
183950         -    if( sqlite3_step(pSeek)==SQLITE_ROW ){
183951         -      int iCol;
183952         -      Fts5InsertCtx ctx;
183953         -      ctx.pStorage = p;
183954         -      ctx.iCol = -1;
183955         -      rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
183956         -      for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){
183957         -        if( pConfig->abUnindexed[iCol-1] ) continue;
183958         -        ctx.szCol = 0;
183959         -        rc = sqlite3Fts5Tokenize(pConfig, 
183960         -            FTS5_TOKENIZE_DOCUMENT,
183961         -            (const char*)sqlite3_column_text(pSeek, iCol),
183962         -            sqlite3_column_bytes(pSeek, iCol),
183963         -            (void*)&ctx,
183964         -            fts5StorageInsertCallback
183965         -        );
183966         -        p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
183967         -      }
183968         -      p->nTotalRow--;
183969         -    }
183970         -    rc2 = sqlite3_reset(pSeek);
183971         -    if( rc==SQLITE_OK ) rc = rc2;
183972         -  }
183973         -
       184102  +    if( sqlite3_step(pSeek)!=SQLITE_ROW ){
       184103  +      return sqlite3_reset(pSeek);
       184104  +    }
       184105  +  }
       184106  +
       184107  +  ctx.pStorage = p;
       184108  +  ctx.iCol = -1;
       184109  +  rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
       184110  +  for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){
       184111  +    if( pConfig->abUnindexed[iCol-1]==0 ){
       184112  +      const char *zText;
       184113  +      int nText;
       184114  +      if( pSeek ){
       184115  +        zText = (const char*)sqlite3_column_text(pSeek, iCol);
       184116  +        nText = sqlite3_column_bytes(pSeek, iCol);
       184117  +      }else{
       184118  +        zText = (const char*)sqlite3_value_text(apVal[iCol-1]);
       184119  +        nText = sqlite3_value_bytes(apVal[iCol-1]);
       184120  +      }
       184121  +      ctx.szCol = 0;
       184122  +      rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT, 
       184123  +          zText, nText, (void*)&ctx, fts5StorageInsertCallback
       184124  +      );
       184125  +      p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
       184126  +    }
       184127  +  }
       184128  +  p->nTotalRow--;
       184129  +
       184130  +  rc2 = sqlite3_reset(pSeek);
       184131  +  if( rc==SQLITE_OK ) rc = rc2;
183974 184132     return rc;
183975 184133   }
183976 184134   
183977 184135   
183978 184136   /*
183979 184137   ** Insert a record into the %_docsize table. Specifically, do:
183980 184138   **
................................................................................
184046 184204   
184047 184205     return rc;
184048 184206   }
184049 184207   
184050 184208   /*
184051 184209   ** Remove a row from the FTS table.
184052 184210   */
184053         -static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel){
       184211  +static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){
184054 184212     Fts5Config *pConfig = p->pConfig;
184055 184213     int rc;
184056 184214     sqlite3_stmt *pDel = 0;
184057 184215   
       184216  +  assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 );
184058 184217     rc = fts5StorageLoadTotals(p, 1);
184059 184218   
184060 184219     /* Delete the index records */
184061 184220     if( rc==SQLITE_OK ){
184062         -    rc = fts5StorageDeleteFromIndex(p, iDel);
       184221  +    rc = fts5StorageDeleteFromIndex(p, iDel, apVal);
184063 184222     }
184064 184223   
184065 184224     /* Delete the %_docsize record */
184066 184225     if( rc==SQLITE_OK && pConfig->bColumnsize ){
184067 184226       rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, &pDel, 0);
184068 184227       if( rc==SQLITE_OK ){
184069 184228         sqlite3_bind_int64(pDel, 1, iDel);
................................................................................
184073 184232     }
184074 184233   
184075 184234     /* Delete the %_content record */
184076 184235     if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
184077 184236       if( rc==SQLITE_OK ){
184078 184237         rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, &pDel, 0);
184079 184238       }
184080         -    if( rc==SQLITE_OK ){
184081         -      sqlite3_bind_int64(pDel, 1, iDel);
184082         -      sqlite3_step(pDel);
184083         -      rc = sqlite3_reset(pDel);
184084         -    }
184085         -  }
184086         -
184087         -  /* Write the averages record */
184088         -  if( rc==SQLITE_OK ){
184089         -    rc = fts5StorageSaveTotals(p);
184090         -  }
184091         -
184092         -  return rc;
184093         -}
184094         -
184095         -static int sqlite3Fts5StorageSpecialDelete(
184096         -  Fts5Storage *p, 
184097         -  i64 iDel, 
184098         -  sqlite3_value **apVal
184099         -){
184100         -  Fts5Config *pConfig = p->pConfig;
184101         -  int rc;
184102         -  sqlite3_stmt *pDel = 0;
184103         -
184104         -  assert( pConfig->eContent!=FTS5_CONTENT_NORMAL );
184105         -  rc = fts5StorageLoadTotals(p, 1);
184106         -
184107         -  /* Delete the index records */
184108         -  if( rc==SQLITE_OK ){
184109         -    int iCol;
184110         -    Fts5InsertCtx ctx;
184111         -    ctx.pStorage = p;
184112         -    ctx.iCol = -1;
184113         -
184114         -    rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
184115         -    for(iCol=0; rc==SQLITE_OK && iCol<pConfig->nCol; iCol++){
184116         -      if( pConfig->abUnindexed[iCol] ) continue;
184117         -      ctx.szCol = 0;
184118         -      rc = sqlite3Fts5Tokenize(pConfig, 
184119         -        FTS5_TOKENIZE_DOCUMENT,
184120         -        (const char*)sqlite3_value_text(apVal[iCol]),
184121         -        sqlite3_value_bytes(apVal[iCol]),
184122         -        (void*)&ctx,
184123         -        fts5StorageInsertCallback
184124         -      );
184125         -      p->aTotalSize[iCol] -= (i64)ctx.szCol;
184126         -    }
184127         -    p->nTotalRow--;
184128         -  }
184129         -
184130         -  /* Delete the %_docsize record */
184131         -  if( pConfig->bColumnsize ){
184132         -    if( rc==SQLITE_OK ){
184133         -      rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, &pDel, 0);
184134         -    }
184135 184239       if( rc==SQLITE_OK ){
184136 184240         sqlite3_bind_int64(pDel, 1, iDel);
184137 184241         sqlite3_step(pDel);
184138 184242         rc = sqlite3_reset(pDel);
184139 184243       }
184140 184244     }
184141 184245   
................................................................................
187087 187191                   pCsr->aDoc[0]++;
187088 187192                 }else{
187089 187193                   int iCol = -1;
187090 187194                   while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
187091 187195                     int ii = FTS5_POS2COLUMN(iPos);
187092 187196                     pCsr->aCnt[ii]++;
187093 187197                     if( iCol!=ii ){
       187198  +                    if( ii>=nCol ){
       187199  +                      rc = FTS5_CORRUPT;
       187200  +                      break;
       187201  +                    }
187094 187202                       pCsr->aDoc[ii]++;
187095 187203                       iCol = ii;
187096 187204                     }
187097 187205                   }
187098 187206                 }
187099 187207               }
187100 187208               break;
................................................................................
187104 187212                 pCsr->aDoc[0]++;
187105 187213               }else{
187106 187214                 Fts5Buffer buf = {0, 0, 0};
187107 187215                 rc = sqlite3Fts5IterPoslistBuffer(pCsr->pIter, &buf);
187108 187216                 if( rc==SQLITE_OK ){
187109 187217                   while( 0==sqlite3Fts5PoslistNext64(buf.p, buf.n, &iOff,&iPos) ){
187110 187218                     assert_nc( iPos>=0 && iPos<nCol );
187111         -                  if( iPos<nCol ) pCsr->aDoc[iPos]++;
       187219  +                  if( iPos>=nCol ){
       187220  +                    rc = FTS5_CORRUPT;
       187221  +                    break;
       187222  +                  }
       187223  +                  pCsr->aDoc[iPos]++;
187112 187224                   }
187113 187225                 }
187114 187226                 sqlite3Fts5BufferFree(&buf);
187115 187227               }
187116 187228               break;
187117 187229   
187118 187230             default: 
................................................................................
187132 187244             }
187133 187245             if( sqlite3Fts5IterEof(pCsr->pIter) ) break;
187134 187246           }
187135 187247         }
187136 187248       }
187137 187249     }
187138 187250   
187139         -  if( pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){
       187251  +  if( rc==SQLITE_OK && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){
187140 187252       while( pCsr->aDoc[pCsr->iCol]==0 ) pCsr->iCol++;
187141 187253       assert( pCsr->iCol<pCsr->pConfig->nCol );
187142 187254     }
187143 187255     return rc;
187144 187256   }
187145 187257   
187146 187258   /*

Changes to src/sqlite3.h.

   109    109   **
   110    110   ** See also: [sqlite3_libversion()],
   111    111   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   112    112   ** [sqlite_version()] and [sqlite_source_id()].
   113    113   */
   114    114   #define SQLITE_VERSION        "3.11.0"
   115    115   #define SQLITE_VERSION_NUMBER 3011000
   116         -#define SQLITE_SOURCE_ID      "2016-01-14 14:19:50 d17bc2c92f4d086280e49a3cc72993be7fee2da7"
          116  +#define SQLITE_SOURCE_ID      "2016-01-18 17:48:28 acaf426449bf6fd3140fd63141750ff69d1119a5"
   117    117   
   118    118   /*
   119    119   ** CAPI3REF: Run-Time Library Version Numbers
   120    120   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   121    121   **
   122    122   ** These interfaces provide the same information as the [SQLITE_VERSION],
   123    123   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros