/ Check-in [8f1ef090]
Login

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

Overview
Comment:Merge latest trunk changes with this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | schemalint
Files: files | file ages | folders
SHA1: 8f1ef0904d055b5510ec9043810ebf22a8c5e253
User & Date: dan 2015-11-23 17:14:03
Context
2015-11-23
18:28
In the CREATE INDEX statements output by schemalint.tcl, avoid declaring an explicit collation sequence that is the same as the column's default. check-in: d3aa067c user: dan tags: schemalint
17:14
Merge latest trunk changes with this branch. check-in: 8f1ef090 user: dan tags: schemalint
17:10
Fix ORDER BY handling in the schemalint.tcl script. Add internal self-tests to the same script. check-in: b8f277c9 user: dan tags: schemalint
2015-11-21
19:43
Fix an obscure memory leak found by libfuzzer that may occur under some circumstances if expanding a "*" expression causes a SELECT to return more than 32767 columns. check-in: 60de5f23 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to autoconf/Makefile.am.

     2      2   AM_CFLAGS = @THREADSAFE_FLAGS@ @DYNAMIC_EXTENSION_FLAGS@ @FTS5_FLAGS@ @JSON1_FLAGS@ -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE
     3      3   
     4      4   lib_LTLIBRARIES = libsqlite3.la
     5      5   libsqlite3_la_SOURCES = sqlite3.c
     6      6   libsqlite3_la_LDFLAGS = -no-undefined -version-info 8:6:8
     7      7   
     8      8   bin_PROGRAMS = sqlite3
     9         -sqlite3_SOURCES = shell.c sqlite3.c sqlite3.h
    10         -sqlite3_LDADD = @READLINE_LIBS@
            9  +sqlite3_SOURCES = shell.c sqlite3.h
           10  +EXTRA_sqlite3_SOURCES = sqlite3.c
           11  +sqlite3_LDADD = @EXTRA_SHELL_OBJ@ @READLINE_LIBS@
           12  +sqlite3_DEPENDENCIES = @EXTRA_SHELL_OBJ@
    11     13   sqlite3_CFLAGS = $(AM_CFLAGS)
    12     14   
    13     15   include_HEADERS = sqlite3.h sqlite3ext.h
    14     16   
    15     17   EXTRA_DIST = sqlite3.1 tea
    16     18   pkgconfigdir = ${libdir}/pkgconfig
    17     19   pkgconfig_DATA = sqlite3.pc
    18     20   
    19     21   man_MANS = sqlite3.1

Changes to autoconf/configure.ac.

     1      1   
     2      2   #-----------------------------------------------------------------------
     3      3   # Supports the following non-standard switches.
     4      4   #
     5      5   #   --enable-threadsafe
     6      6   #   --enable-readline
            7  +#   --enable-editline
            8  +#   --enable-static-shell
     7      9   #   --enable-dynamic-extensions
     8     10   #
     9     11   
    10     12   AC_PREREQ(2.61)
    11     13   AC_INIT(sqlite, 3.7.5, http://www.sqlite.org)
    12     14   AC_CONFIG_SRCDIR([sqlite3.c])
    13     15   
................................................................................
   112    114     [--enable-json1], [include json1 support [default=no]])], 
   113    115     [], [enable_json1=no])
   114    116   if test x"$enable_json1" == "xyes"; then
   115    117     JSON1_FLAGS=-DSQLITE_ENABLE_JSON1
   116    118   fi
   117    119   AC_SUBST(JSON1_FLAGS)
   118    120   #-----------------------------------------------------------------------
          121  +
          122  +#-----------------------------------------------------------------------
          123  +#   --enable-static-shell
          124  +#
          125  +AC_ARG_ENABLE(static-shell, [AS_HELP_STRING(
          126  +  [--enable-static-shell], 
          127  +  [statically link libsqlite3 into shell tool [default=yes]])], 
          128  +  [], [enable_static_shell=yes])
          129  +if test x"$enable_static_shell" == "xyes"; then
          130  +  EXTRA_SHELL_OBJ=sqlite3.$OBJEXT
          131  +else
          132  +  EXTRA_SHELL_OBJ=libsqlite3.la
          133  +fi
          134  +AC_SUBST(EXTRA_SHELL_OBJ)
          135  +#-----------------------------------------------------------------------
   119    136   
   120    137   AC_CHECK_FUNCS(posix_fallocate)
   121    138   
   122    139   #-----------------------------------------------------------------------
   123    140   # UPDATE: Maybe it's better if users just set CFLAGS before invoking
   124    141   # configure. This option doesn't really add much...
   125    142   #

Changes to src/bitvec.c.

    37     37   #include "sqliteInt.h"
    38     38   
    39     39   /* Size of the Bitvec structure in bytes. */
    40     40   #define BITVEC_SZ        512
    41     41   
    42     42   /* Round the union size down to the nearest pointer boundary, since that's how 
    43     43   ** it will be aligned within the Bitvec struct. */
    44         -#define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
           44  +#define BITVEC_USIZE \
           45  +    (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
    45     46   
    46     47   /* Type of the array "element" for the bitmap representation. 
    47     48   ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. 
    48     49   ** Setting this to the "natural word" size of your CPU may improve
    49     50   ** performance. */
    50     51   #define BITVEC_TELEM     u8
    51     52   /* Size, in bits, of the bitmap element. */

Changes to src/btree.c.

  2552   2552   #endif
  2553   2553   
  2554   2554     sqlite3_free(p);
  2555   2555     return SQLITE_OK;
  2556   2556   }
  2557   2557   
  2558   2558   /*
  2559         -** Change the limit on the number of pages allowed in the cache.
  2560         -**
  2561         -** The maximum number of cache pages is set to the absolute
  2562         -** value of mxPage.  If mxPage is negative, the pager will
  2563         -** operate asynchronously - it will not stop to do fsync()s
  2564         -** to insure data is written to the disk surface before
  2565         -** continuing.  Transactions still work if synchronous is off,
  2566         -** and the database cannot be corrupted if this program
  2567         -** crashes.  But if the operating system crashes or there is
  2568         -** an abrupt power failure when synchronous is off, the database
  2569         -** could be left in an inconsistent and unrecoverable state.
  2570         -** Synchronous is on by default so database corruption is not
  2571         -** normally a worry.
         2559  +** Change the "soft" limit on the number of pages in the cache.
         2560  +** Unused and unmodified pages will be recycled when the number of
         2561  +** pages in the cache exceeds this soft limit.  But the size of the
         2562  +** cache is allowed to grow larger than this limit if it contains
         2563  +** dirty pages or pages still in active use.
  2572   2564   */
  2573   2565   int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
  2574   2566     BtShared *pBt = p->pBt;
  2575   2567     assert( sqlite3_mutex_held(p->db->mutex) );
  2576   2568     sqlite3BtreeEnter(p);
  2577   2569     sqlite3PagerSetCachesize(pBt->pPager, mxPage);
  2578   2570     sqlite3BtreeLeave(p);
  2579   2571     return SQLITE_OK;
  2580   2572   }
         2573  +
         2574  +/*
         2575  +** Change the "spill" limit on the number of pages in the cache.
         2576  +** If the number of pages exceeds this limit during a write transaction,
         2577  +** the pager might attempt to "spill" pages to the journal early in
         2578  +** order to free up memory.
         2579  +**
         2580  +** The value returned is the current spill size.  If zero is passed
         2581  +** as an argument, no changes are made to the spill size setting, so
         2582  +** using mxPage of 0 is a way to query the current spill size.
         2583  +*/
         2584  +int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
         2585  +  BtShared *pBt = p->pBt;
         2586  +  int res;
         2587  +  assert( sqlite3_mutex_held(p->db->mutex) );
         2588  +  sqlite3BtreeEnter(p);
         2589  +  res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
         2590  +  sqlite3BtreeLeave(p);
         2591  +  return res;
         2592  +}
  2581   2593   
  2582   2594   #if SQLITE_MAX_MMAP_SIZE>0
  2583   2595   /*
  2584   2596   ** Change the limit on the amount of the database file that may be
  2585   2597   ** memory mapped.
  2586   2598   */
  2587   2599   int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){

Changes to src/btree.h.

    59     59   #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
    60     60   #define BTREE_MEMORY        2  /* This is an in-memory DB */
    61     61   #define BTREE_SINGLE        4  /* The file contains at most 1 b-tree */
    62     62   #define BTREE_UNORDERED     8  /* Use of a hash implementation is OK */
    63     63   
    64     64   int sqlite3BtreeClose(Btree*);
    65     65   int sqlite3BtreeSetCacheSize(Btree*,int);
           66  +int sqlite3BtreeSetSpillSize(Btree*,int);
    66     67   #if SQLITE_MAX_MMAP_SIZE>0
    67     68     int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
    68     69   #endif
    69     70   int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
    70     71   int sqlite3BtreeSyncDisabled(Btree*);
    71     72   int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
    72     73   int sqlite3BtreeGetPageSize(Btree*);

Changes to src/build.c.

  1045   1045   
  1046   1046     /* If an error occurs, we jump here */
  1047   1047   begin_table_error:
  1048   1048     sqlite3DbFree(db, zName);
  1049   1049     return;
  1050   1050   }
  1051   1051   
  1052         -/*
  1053         -** This macro is used to compare two strings in a case-insensitive manner.
  1054         -** It is slightly faster than calling sqlite3StrICmp() directly, but
  1055         -** produces larger code.
  1056         -**
  1057         -** WARNING: This macro is not compatible with the strcmp() family. It
  1058         -** returns true if the two strings are equal, otherwise false.
         1052  +/* Set properties of a table column based on the (magical)
         1053  +** name of the column.
  1059   1054   */
  1060         -#define STRICMP(x, y) (\
  1061         -sqlite3UpperToLower[*(unsigned char *)(x)]==   \
  1062         -sqlite3UpperToLower[*(unsigned char *)(y)]     \
  1063         -&& sqlite3StrICmp((x)+1,(y)+1)==0 )
         1055  +void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
         1056  +#if SQLITE_ENABLE_HIDDEN_COLUMNS
         1057  +  if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
         1058  +    pCol->colFlags |= COLFLAG_HIDDEN;
         1059  +  }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
         1060  +    pTab->tabFlags |= TF_OOOHidden;
         1061  +  }
         1062  +#endif
         1063  +}
         1064  +
  1064   1065   
  1065   1066   /*
  1066   1067   ** Add a new column to the table currently being constructed.
  1067   1068   **
  1068   1069   ** The parser calls this routine once for each column declaration
  1069   1070   ** in a CREATE TABLE statement.  sqlite3StartTable() gets called
  1070   1071   ** first to get things going.  Then this routine is called for each
................................................................................
  1082   1083       sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
  1083   1084       return;
  1084   1085     }
  1085   1086   #endif
  1086   1087     z = sqlite3NameFromToken(db, pName);
  1087   1088     if( z==0 ) return;
  1088   1089     for(i=0; i<p->nCol; i++){
  1089         -    if( STRICMP(z, p->aCol[i].zName) ){
         1090  +    if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
  1090   1091         sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
  1091   1092         sqlite3DbFree(db, z);
  1092   1093         return;
  1093   1094       }
  1094   1095     }
  1095   1096     if( (p->nCol & 0x7)==0 ){
  1096   1097       Column *aNew;
................................................................................
  1100   1101         return;
  1101   1102       }
  1102   1103       p->aCol = aNew;
  1103   1104     }
  1104   1105     pCol = &p->aCol[p->nCol];
  1105   1106     memset(pCol, 0, sizeof(p->aCol[0]));
  1106   1107     pCol->zName = z;
         1108  +  sqlite3ColumnPropertiesFromName(p, pCol);
  1107   1109    
  1108   1110     /* If there is no type specified, columns have the default affinity
  1109   1111     ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will
  1110   1112     ** be called next to set pCol->affinity correctly.
  1111   1113     */
  1112   1114     pCol->affinity = SQLITE_AFF_BLOB;
  1113   1115     pCol->szEst = 1;
................................................................................
  3870   3872   }
  3871   3873   
  3872   3874   /*
  3873   3875   ** Add the list of function arguments to the SrcList entry for a
  3874   3876   ** table-valued-function.
  3875   3877   */
  3876   3878   void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
  3877         -  if( p && pList ){
         3879  +  if( p ){
  3878   3880       struct SrcList_item *pItem = &p->a[p->nSrc-1];
  3879   3881       assert( pItem->fg.notIndexed==0 );
  3880   3882       assert( pItem->fg.isIndexedBy==0 );
  3881   3883       assert( pItem->fg.isTabFunc==0 );
  3882   3884       pItem->u1.pFuncArg = pList;
  3883   3885       pItem->fg.isTabFunc = 1;
  3884   3886     }else{

Changes to src/delete.c.

   102    102     if( pFrom ){
   103    103       assert( pFrom->nSrc==1 );
   104    104       pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
   105    105       pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
   106    106       assert( pFrom->a[0].pOn==0 );
   107    107       assert( pFrom->a[0].pUsing==0 );
   108    108     }
   109         -  pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
          109  +  pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 
          110  +                          SF_IncludeHidden, 0, 0);
   110    111     sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
   111    112     sqlite3Select(pParse, pSel, &dest);
   112    113     sqlite3SelectDelete(db, pSel);
   113    114   }
   114    115   #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
   115    116   
   116    117   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)

Changes to src/expr.c.

  1885   1885     */
  1886   1886     if( !ExprHasProperty(pExpr, EP_VarSelect) ){
  1887   1887       jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
  1888   1888     }
  1889   1889   
  1890   1890   #ifndef SQLITE_OMIT_EXPLAIN
  1891   1891     if( pParse->explain==2 ){
  1892         -    char *zMsg = sqlite3MPrintf(
  1893         -        pParse->db, "EXECUTE %s%s SUBQUERY %d", jmpIfDynamic>=0?"":"CORRELATED ",
  1894         -        pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
         1892  +    char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d",
         1893  +        jmpIfDynamic>=0?"":"CORRELATED ",
         1894  +        pExpr->op==TK_IN?"LIST":"SCALAR",
         1895  +        pParse->iNextSelectId
  1895   1896       );
  1896   1897       sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
  1897   1898     }
  1898   1899   #endif
  1899   1900   
  1900   1901     switch( pExpr->op ){
  1901   1902       case TK_IN: {

Changes to src/insert.c.

   732    732     if( pColumn==0 && nColumn>0 ){
   733    733       ipkColumn = pTab->iPKey;
   734    734     }
   735    735   
   736    736     /* Make sure the number of columns in the source data matches the number
   737    737     ** of columns to be inserted into the table.
   738    738     */
   739         -  if( IsVirtual(pTab) ){
   740         -    for(i=0; i<pTab->nCol; i++){
   741         -      nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
   742         -    }
          739  +  for(i=0; i<pTab->nCol; i++){
          740  +    nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
   743    741     }
   744    742     if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
   745    743       sqlite3ErrorMsg(pParse, 
   746    744          "table %S has %d columns but %d values were supplied",
   747    745          pTabList, 0, pTab->nCol-nHidden, nColumn);
   748    746       goto insert_cleanup;
   749    747     }
................................................................................
   831    829       /* Cannot have triggers on a virtual table. If it were possible,
   832    830       ** this block would have to account for hidden column.
   833    831       */
   834    832       assert( !IsVirtual(pTab) );
   835    833   
   836    834       /* Create the new column data
   837    835       */
   838         -    for(i=0; i<pTab->nCol; i++){
   839         -      if( pColumn==0 ){
   840         -        j = i;
   841         -      }else{
          836  +    for(i=j=0; i<pTab->nCol; i++){
          837  +      if( pColumn ){
   842    838           for(j=0; j<pColumn->nId; j++){
   843    839             if( pColumn->a[j].idx==i ) break;
   844    840           }
   845    841         }
   846         -      if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
          842  +      if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId)
          843  +            || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){
   847    844           sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
   848    845         }else if( useTempTable ){
   849    846           sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); 
   850    847         }else{
   851    848           assert( pSelect==0 ); /* Otherwise useTempTable is true */
   852    849           sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
   853    850         }
          851  +      if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++;
   854    852       }
   855    853   
   856    854       /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
   857    855       ** do not attempt any conversions before assembling the record.
   858    856       ** If this is a real table, attempt conversions as required by the
   859    857       ** table column affinities.
   860    858       */
................................................................................
   930    928           ** taking up data space with information that will never be used.
   931    929           ** As there may be shallow copies of this value, make it a soft-NULL */
   932    930           sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
   933    931           continue;
   934    932         }
   935    933         if( pColumn==0 ){
   936    934           if( IsHiddenColumn(&pTab->aCol[i]) ){
   937         -          assert( IsVirtual(pTab) );
   938    935             j = -1;
   939    936             nHidden++;
   940    937           }else{
   941    938             j = i - nHidden;
   942    939           }
   943    940         }else{
   944    941           for(j=0; j<pColumn->nId; j++){
................................................................................
  1872   1869     }
  1873   1870     pEList = pSelect->pEList;
  1874   1871     assert( pEList!=0 );
  1875   1872     if( pEList->nExpr!=1 ){
  1876   1873       return 0;   /* The result set must have exactly one column */
  1877   1874     }
  1878   1875     assert( pEList->a[0].pExpr );
  1879         -  if( pEList->a[0].pExpr->op!=TK_ALL ){
         1876  +  if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
  1880   1877       return 0;   /* The result set must be the special operator "*" */
  1881   1878     }
  1882   1879   
  1883   1880     /* At this point we have established that the statement is of the
  1884   1881     ** correct syntactic form to participate in this optimization.  Now
  1885   1882     ** we have to check the semantics.
  1886   1883     */
................................................................................
  1908   1905     }
  1909   1906     if( pDest->iPKey!=pSrc->iPKey ){
  1910   1907       return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
  1911   1908     }
  1912   1909     for(i=0; i<pDest->nCol; i++){
  1913   1910       Column *pDestCol = &pDest->aCol[i];
  1914   1911       Column *pSrcCol = &pSrc->aCol[i];
         1912  +#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
         1913  +    if( (db->flags & SQLITE_Vacuum)==0 
         1914  +     && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN 
         1915  +    ){
         1916  +      return 0;    /* Neither table may have __hidden__ columns */
         1917  +    }
         1918  +#endif
  1915   1919       if( pDestCol->affinity!=pSrcCol->affinity ){
  1916   1920         return 0;    /* Affinity must be the same on all columns */
  1917   1921       }
  1918   1922       if( !xferCompatibleCollation(pDestCol->zColl, pSrcCol->zColl) ){
  1919   1923         return 0;    /* Collating sequence must be the same on all columns */
  1920   1924       }
  1921   1925       if( pDestCol->notNull && !pSrcCol->notNull ){

Changes to src/main.c.

   216    216     ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
   217    217     ** call to sqlite3PcacheInitialize().
   218    218     */
   219    219     sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
   220    220     if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
   221    221       FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
   222    222       sqlite3GlobalConfig.inProgress = 1;
          223  +#ifdef SQLITE_ENABLE_SQLLOG
          224  +    {
          225  +      extern void sqlite3_init_sqllog(void);
          226  +      sqlite3_init_sqllog();
          227  +    }
          228  +#endif
   223    229       memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
   224    230       sqlite3RegisterGlobalFunctions();
   225    231       if( sqlite3GlobalConfig.isPCacheInit==0 ){
   226    232         rc = sqlite3PcacheInitialize();
   227    233       }
   228    234       if( rc==SQLITE_OK ){
   229    235         sqlite3GlobalConfig.isPCacheInit = 1;

Changes to src/mem5.c.

   237    237     ** power of two that we can represent using 32-bit signed integers.
   238    238     */
   239    239     if( nByte > 0x40000000 ){
   240    240       return 0;
   241    241     }
   242    242   
   243    243     /* Round nByte up to the next valid power of two */
   244         -  for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
          244  +  for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
   245    245   
   246    246     /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
   247    247     ** block.  If not, then split a block of the next larger power of
   248    248     ** two in order to create a new free block of size iLogsize.
   249    249     */
   250    250     for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
   251    251     if( iBin>LOGMAX ){

Changes to src/mutex_unix.c.

    46     46   #if SQLITE_MUTEX_NREF
    47     47     volatile int nRef;         /* Number of entrances */
    48     48     volatile pthread_t owner;  /* Thread that is within this mutex */
    49     49     int trace;                 /* True to trace changes */
    50     50   #endif
    51     51   };
    52     52   #if SQLITE_MUTEX_NREF
    53         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
           53  +#define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0}
    54     54   #else
    55     55   #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
    56     56   #endif
    57     57   
    58     58   /*
    59     59   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
    60     60   ** intended for use only inside assert() statements.  On some platforms,

Changes to src/os_unix.c.

  3345   3345         if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
  3346   3346           pFile->transCntrChng = 1;  /* The transaction counter has changed */
  3347   3347         }
  3348   3348       }
  3349   3349     }
  3350   3350   #endif
  3351   3351   
  3352         -#if SQLITE_MAX_MMAP_SIZE>0
         3352  +#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
  3353   3353     /* Deal with as much of this write request as possible by transfering
  3354   3354     ** data from the memory mapping using memcpy().  */
  3355   3355     if( offset<pFile->mmapSize ){
  3356   3356       if( offset+amt <= pFile->mmapSize ){
  3357   3357         memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
  3358   3358         return SQLITE_OK;
  3359   3359       }else{
................................................................................
  4770   4770     assert( pFd->nFetchOut==0 );
  4771   4771     assert( nNew>pFd->mmapSize );
  4772   4772     assert( nNew<=pFd->mmapSizeMax );
  4773   4773     assert( nNew>0 );
  4774   4774     assert( pFd->mmapSizeActual>=pFd->mmapSize );
  4775   4775     assert( MAP_FAILED!=0 );
  4776   4776   
         4777  +#ifdef SQLITE_MMAP_READWRITE
  4777   4778     if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
         4779  +#endif
  4778   4780   
  4779   4781     if( pOrig ){
  4780   4782   #if HAVE_MREMAP
  4781   4783       i64 nReuse = pFd->mmapSize;
  4782   4784   #else
  4783   4785       const int szSyspage = osGetpagesize();
  4784   4786       i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
................................................................................
  6549   6551             }
  6550   6552           }
  6551   6553         }
  6552   6554         start=i+1;
  6553   6555       }
  6554   6556       buf[i] = lockPath[i];
  6555   6557     }
  6556         -  OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, osGetpid(0)));
         6558  +  OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
  6557   6559     return 0;
  6558   6560   }
  6559   6561   
  6560   6562   /*
  6561   6563   ** Create a new VFS file descriptor (stored in memory obtained from
  6562   6564   ** sqlite3_malloc) and open the file named "path" in the file descriptor.
  6563   6565   **

Changes to src/os_win.c.

  2598   2598     SimulateIOError(return SQLITE_IOERR_WRITE);
  2599   2599     SimulateDiskfullError(return SQLITE_FULL);
  2600   2600   
  2601   2601     OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
  2602   2602              "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
  2603   2603              pFile->h, pBuf, amt, offset, pFile->locktype));
  2604   2604   
  2605         -#if SQLITE_MAX_MMAP_SIZE>0
         2605  +#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
  2606   2606     /* Deal with as much of this write request as possible by transfering
  2607   2607     ** data from the memory mapping using memcpy().  */
  2608   2608     if( offset<pFile->mmapSize ){
  2609   2609       if( offset+amt <= pFile->mmapSize ){
  2610   2610         memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
  2611   2611         OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
  2612   2612                  osGetCurrentProcessId(), pFile, pFile->h));
................................................................................
  3146   3146     OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
  3147   3147   
  3148   3148     assert( id!=0 );
  3149   3149     if( pFile->locktype>=RESERVED_LOCK ){
  3150   3150       res = 1;
  3151   3151       OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
  3152   3152     }else{
  3153         -    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
         3153  +    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
  3154   3154       if( res ){
  3155   3155         winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
  3156   3156       }
  3157   3157       res = !res;
  3158   3158       OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
  3159   3159     }
  3160   3160     *pResOut = res;
................................................................................
  4092   4092     }
  4093   4093     if( nMap!=pFd->mmapSize ){
  4094   4094       void *pNew = 0;
  4095   4095       DWORD protect = PAGE_READONLY;
  4096   4096       DWORD flags = FILE_MAP_READ;
  4097   4097   
  4098   4098       winUnmapfile(pFd);
         4099  +#ifdef SQLITE_MMAP_READWRITE
  4099   4100       if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
  4100   4101         protect = PAGE_READWRITE;
  4101   4102         flags |= FILE_MAP_WRITE;
  4102   4103       }
         4104  +#endif
  4103   4105   #if SQLITE_OS_WINRT
  4104   4106       pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
  4105   4107   #elif defined(SQLITE_WIN32_HAS_WIDE)
  4106   4108       pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
  4107   4109                                   (DWORD)((nMap>>32) & 0xffffffff),
  4108   4110                                   (DWORD)(nMap & 0xffffffff), NULL);
  4109   4111   #elif defined(SQLITE_WIN32_HAS_ANSI)

Changes to src/pager.c.

  3388   3388       pPager->journalOff = szJ;
  3389   3389     }
  3390   3390   
  3391   3391     return rc;
  3392   3392   }
  3393   3393   
  3394   3394   /*
  3395         -** Change the maximum number of in-memory pages that are allowed.
         3395  +** Change the maximum number of in-memory pages that are allowed
         3396  +** before attempting to recycle clean and unused pages.
  3396   3397   */
  3397   3398   void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
  3398   3399     sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
  3399   3400   }
         3401  +
         3402  +/*
         3403  +** Change the maximum number of in-memory pages that are allowed
         3404  +** before attempting to spill pages to journal.
         3405  +*/
         3406  +int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
         3407  +  return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
         3408  +}
  3400   3409   
  3401   3410   /*
  3402   3411   ** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
  3403   3412   */
  3404   3413   static void pagerFixMaplimit(Pager *pPager){
  3405   3414   #if SQLITE_MAX_MMAP_SIZE>0
  3406   3415     sqlite3_file *fd = pPager->fd;

Changes to src/pager.h.

   119    119   void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
   120    120   int sqlite3PagerSetPagesize(Pager*, u32*, int);
   121    121   #ifdef SQLITE_HAS_CODEC
   122    122   void sqlite3PagerAlignReserve(Pager*,Pager*);
   123    123   #endif
   124    124   int sqlite3PagerMaxPageCount(Pager*, int);
   125    125   void sqlite3PagerSetCachesize(Pager*, int);
          126  +int sqlite3PagerSetSpillsize(Pager*, int);
   126    127   void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
   127    128   void sqlite3PagerShrink(Pager*);
   128    129   void sqlite3PagerSetFlags(Pager*,unsigned);
   129    130   int sqlite3PagerLockingMode(Pager *, int);
   130    131   int sqlite3PagerSetJournalMode(Pager *, int);
   131    132   int sqlite3PagerGetJournalMode(Pager*);
   132    133   int sqlite3PagerOkToChangeJournalMode(Pager*);

Changes to src/parse.y.

    60     60   */
    61     61   #define yytestcase(X) testcase(X)
    62     62   
    63     63   /*
    64     64   ** Indicate that sqlite3ParserFree() will never be called with a null
    65     65   ** pointer.
    66     66   */
    67         -#define YYPARSEFREENOTNULL 1
           67  +#define YYPARSEFREENEVERNULL 1
    68     68   
    69     69   /*
    70     70   ** Alternative datatype for the argument to the malloc() routine passed
    71     71   ** into sqlite3ParserAlloc().  The default is size_t.
    72     72   */
    73     73   #define YYMALLOCARGTYPE  u64
    74     74   
................................................................................
   542    542   distinct(A) ::= DISTINCT.   {A = SF_Distinct;}
   543    543   distinct(A) ::= ALL.        {A = SF_All;}
   544    544   distinct(A) ::= .           {A = 0;}
   545    545   
   546    546   // selcollist is a list of expressions that are to become the return
   547    547   // values of the SELECT statement.  The "*" in statements like
   548    548   // "SELECT * FROM ..." is encoded as a special expression with an
   549         -// opcode of TK_ALL.
          549  +// opcode of TK_ASTERISK.
   550    550   //
   551    551   %type selcollist {ExprList*}
   552    552   %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
   553    553   %type sclp {ExprList*}
   554    554   %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
   555    555   sclp(A) ::= selcollist(X) COMMA.             {A = X;}
   556    556   sclp(A) ::= .                                {A = 0;}
   557    557   selcollist(A) ::= sclp(P) expr(X) as(Y).     {
   558    558      A = sqlite3ExprListAppend(pParse, P, X.pExpr);
   559    559      if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
   560    560      sqlite3ExprListSetSpan(pParse,A,&X);
   561    561   }
   562    562   selcollist(A) ::= sclp(P) STAR. {
   563         -  Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
          563  +  Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
   564    564     A = sqlite3ExprListAppend(pParse, P, p);
   565    565   }
   566    566   selcollist(A) ::= sclp(P) nm(X) DOT STAR(Y). {
   567         -  Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &Y);
          567  +  Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0, &Y);
   568    568     Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
   569    569     Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
   570    570     A = sqlite3ExprListAppend(pParse,P, pDot);
   571    571   }
   572    572   
   573    573   // An option "AS <id>" phrase that can follow one of the expressions that
   574    574   // define the result set, or one of the tables in the FROM clause.

Changes to src/pcache.c.

    17     17   ** A complete page cache is an instance of this structure.
    18     18   */
    19     19   struct PCache {
    20     20     PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
    21     21     PgHdr *pSynced;                     /* Last synced page in dirty page list */
    22     22     int nRefSum;                        /* Sum of ref counts over all pages */
    23     23     int szCache;                        /* Configured cache size */
           24  +  int szSpill;                        /* Size before spilling occurs */
    24     25     int szPage;                         /* Size of every page in this cache */
    25     26     int szExtra;                        /* Size of extra space for each page */
    26     27     u8 bPurgeable;                      /* True if pages are on backing store */
    27     28     u8 eCreate;                         /* eCreate value for for xFetch() */
    28     29     int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
    29     30     void *pStress;                      /* Argument to xStress */
    30     31     sqlite3_pcache *pCache;             /* Pluggable cache module */
................................................................................
   106    107   static void pcacheUnpin(PgHdr *p){
   107    108     if( p->pCache->bPurgeable ){
   108    109       sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
   109    110     }
   110    111   }
   111    112   
   112    113   /*
   113         -** Compute the number of pages of cache requested.  p->szCache is the
          114  +** Compute the number of pages of cache requested.   p->szCache is the
   114    115   ** cache size requested by the "PRAGMA cache_size" statement.
   115         -**
   116         -**
   117    116   */
   118    117   static int numberOfCachePages(PCache *p){
   119    118     if( p->szCache>=0 ){
   120    119       /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
   121    120       ** suggested cache size is set to N. */
   122    121       return p->szCache;
   123    122     }else{
................................................................................
   172    171     p->szPage = 1;
   173    172     p->szExtra = szExtra;
   174    173     p->bPurgeable = bPurgeable;
   175    174     p->eCreate = 2;
   176    175     p->xStress = xStress;
   177    176     p->pStress = pStress;
   178    177     p->szCache = 100;
          178  +  p->szSpill = 1;
   179    179     return sqlite3PcacheSetPageSize(p, szPage);
   180    180   }
   181    181   
   182    182   /*
   183    183   ** Change the page size for PCache object. The caller must ensure that there
   184    184   ** are no outstanding page references when this function is called.
   185    185   */
................................................................................
   267    267     PCache *pCache,                 /* Obtain the page from this cache */
   268    268     Pgno pgno,                      /* Page number to obtain */
   269    269     sqlite3_pcache_page **ppPage    /* Write result here */
   270    270   ){
   271    271     PgHdr *pPg;
   272    272     if( pCache->eCreate==2 ) return 0;
   273    273   
   274         -
   275         -  /* Find a dirty page to write-out and recycle. First try to find a 
   276         -  ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
   277         -  ** cleared), but if that is not possible settle for any other 
   278         -  ** unreferenced dirty page.
   279         -  */
   280         -  for(pPg=pCache->pSynced; 
   281         -      pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
   282         -      pPg=pPg->pDirtyPrev
   283         -  );
   284         -  pCache->pSynced = pPg;
   285         -  if( !pPg ){
   286         -    for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
   287         -  }
   288         -  if( pPg ){
   289         -    int rc;
          274  +  if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
          275  +    /* Find a dirty page to write-out and recycle. First try to find a 
          276  +    ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
          277  +    ** cleared), but if that is not possible settle for any other 
          278  +    ** unreferenced dirty page.
          279  +    */
          280  +    for(pPg=pCache->pSynced; 
          281  +        pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
          282  +        pPg=pPg->pDirtyPrev
          283  +    );
          284  +    pCache->pSynced = pPg;
          285  +    if( !pPg ){
          286  +      for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
          287  +    }
          288  +    if( pPg ){
          289  +      int rc;
   290    290   #ifdef SQLITE_LOG_CACHE_SPILL
   291         -    sqlite3_log(SQLITE_FULL, 
   292         -                "spill page %d making room for %d - cache used: %d/%d",
   293         -                pPg->pgno, pgno,
   294         -                sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
          291  +      sqlite3_log(SQLITE_FULL, 
          292  +                  "spill page %d making room for %d - cache used: %d/%d",
          293  +                  pPg->pgno, pgno,
          294  +                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
   295    295                   numberOfCachePages(pCache));
   296    296   #endif
   297         -    rc = pCache->xStress(pCache->pStress, pPg);
   298         -    if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
   299         -      return rc;
          297  +      rc = pCache->xStress(pCache->pStress, pPg);
          298  +      if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
          299  +        return rc;
          300  +      }
   300    301       }
   301    302     }
   302    303     *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
   303    304     return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK;
   304    305   }
   305    306   
   306    307   /*
................................................................................
   636    637   */
   637    638   void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
   638    639     assert( pCache->pCache!=0 );
   639    640     pCache->szCache = mxPage;
   640    641     sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
   641    642                                            numberOfCachePages(pCache));
   642    643   }
          644  +
          645  +/*
          646  +** Set the suggested cache-spill value.  Make no changes if if the
          647  +** argument is zero.  Return the effective cache-spill size, which will
          648  +** be the larger of the szSpill and szCache.
          649  +*/
          650  +int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
          651  +  int res;
          652  +  assert( p->pCache!=0 );
          653  +  if( mxPage ){
          654  +    if( mxPage<0 ){
          655  +      mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
          656  +    }
          657  +    p->szSpill = mxPage;
          658  +  }
          659  +  res = numberOfCachePages(p);
          660  +  if( res<p->szSpill ) res = p->szSpill; 
          661  +  return res;
          662  +}
   643    663   
   644    664   /*
   645    665   ** Free up as much memory as possible from the page cache.
   646    666   */
   647    667   void sqlite3PcacheShrink(PCache *pCache){
   648    668     assert( pCache->pCache!=0 );
   649    669     sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);

Changes to src/pcache.h.

   141    141   ** the total number of pages cached by purgeable pager-caches to the sum
   142    142   ** of the suggested cache-sizes.
   143    143   */
   144    144   void sqlite3PcacheSetCachesize(PCache *, int);
   145    145   #ifdef SQLITE_TEST
   146    146   int sqlite3PcacheGetCachesize(PCache *);
   147    147   #endif
          148  +
          149  +/* Set or get the suggested spill-size for the specified pager-cache.
          150  +**
          151  +** The spill-size is the minimum number of pages in cache before the cache
          152  +** will attempt to spill dirty pages by calling xStress.
          153  +*/
          154  +int sqlite3PcacheSetSpillsize(PCache *, int);
   148    155   
   149    156   /* Free up as much memory as possible from the page cache */
   150    157   void sqlite3PcacheShrink(PCache*);
   151    158   
   152    159   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   153    160   /* Try to return memory used by the pcache module to the main memory heap */
   154    161   int sqlite3PcacheReleaseMemory(int);

Changes to src/pragma.c.

   275    275   }
   276    276   
   277    277   /*
   278    278   ** Process a pragma statement.  
   279    279   **
   280    280   ** Pragmas are of this form:
   281    281   **
   282         -**      PRAGMA [database.]id [= value]
          282  +**      PRAGMA [schema.]id [= value]
   283    283   **
   284    284   ** The identifier might also be a string.  The value is a string, and
   285    285   ** identifier, or a number.  If minusFlag is true, then the value is
   286    286   ** a number that was preceded by a minus sign.
   287    287   **
   288    288   ** If the left side is "database.id" then pId1 is the database name
   289    289   ** and pId2 is the id.  If the left side is just "id" then pId1 is the
   290    290   ** id and pId2 is any empty string.
   291    291   */
   292    292   void sqlite3Pragma(
   293    293     Parse *pParse, 
   294         -  Token *pId1,        /* First part of [database.]id field */
   295         -  Token *pId2,        /* Second part of [database.]id field, or NULL */
          294  +  Token *pId1,        /* First part of [schema.]id field */
          295  +  Token *pId2,        /* Second part of [schema.]id field, or NULL */
   296    296     Token *pValue,      /* Token for <value>, or NULL */
   297    297     int minusFlag       /* True if a '-' sign preceded <value> */
   298    298   ){
   299    299     char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
   300    300     char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
   301    301     const char *zDb = 0;   /* The database name */
   302    302     Token *pId;            /* Pointer to <id> token */
................................................................................
   309    309     Vdbe *v = sqlite3GetVdbe(pParse);  /* Prepared statement */
   310    310     const struct sPragmaNames *pPragma;
   311    311   
   312    312     if( v==0 ) return;
   313    313     sqlite3VdbeRunOnlyOnce(v);
   314    314     pParse->nMem = 2;
   315    315   
   316         -  /* Interpret the [database.] part of the pragma statement. iDb is the
          316  +  /* Interpret the [schema.] part of the pragma statement. iDb is the
   317    317     ** index of the database this pragma is being applied to in db.aDb[]. */
   318    318     iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
   319    319     if( iDb<0 ) return;
   320    320     pDb = &db->aDb[iDb];
   321    321   
   322    322     /* If the temp database has been explicitly named as part of the 
   323    323     ** pragma, make sure it is open. 
................................................................................
   398    398     }
   399    399   
   400    400     /* Jump to the appropriate pragma handler */
   401    401     switch( pPragma->ePragTyp ){
   402    402     
   403    403   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
   404    404     /*
   405         -  **  PRAGMA [database.]default_cache_size
   406         -  **  PRAGMA [database.]default_cache_size=N
          405  +  **  PRAGMA [schema.]default_cache_size
          406  +  **  PRAGMA [schema.]default_cache_size=N
   407    407     **
   408    408     ** The first form reports the current persistent setting for the
   409    409     ** page cache size.  The value returned is the maximum number of
   410    410     ** pages in the page cache.  The second form sets both the current
   411    411     ** page cache size value and the persistent page cache size value
   412    412     ** stored in the database file.
   413    413     **
................................................................................
   450    450       }
   451    451       break;
   452    452     }
   453    453   #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
   454    454   
   455    455   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
   456    456     /*
   457         -  **  PRAGMA [database.]page_size
   458         -  **  PRAGMA [database.]page_size=N
          457  +  **  PRAGMA [schema.]page_size
          458  +  **  PRAGMA [schema.]page_size=N
   459    459     **
   460    460     ** The first form reports the current setting for the
   461    461     ** database page size in bytes.  The second form sets the
   462    462     ** database page size value.  The value can only be set if
   463    463     ** the database has not yet been created.
   464    464     */
   465    465     case PragTyp_PAGE_SIZE: {
................................................................................
   477    477           db->mallocFailed = 1;
   478    478         }
   479    479       }
   480    480       break;
   481    481     }
   482    482   
   483    483     /*
   484         -  **  PRAGMA [database.]secure_delete
   485         -  **  PRAGMA [database.]secure_delete=ON/OFF
          484  +  **  PRAGMA [schema.]secure_delete
          485  +  **  PRAGMA [schema.]secure_delete=ON/OFF
   486    486     **
   487    487     ** The first form reports the current setting for the
   488    488     ** secure_delete flag.  The second form changes the secure_delete
   489    489     ** flag setting and reports thenew value.
   490    490     */
   491    491     case PragTyp_SECURE_DELETE: {
   492    492       Btree *pBt = pDb->pBt;
................................................................................
   503    503       }
   504    504       b = sqlite3BtreeSecureDelete(pBt, b);
   505    505       returnSingleInt(v, "secure_delete", b);
   506    506       break;
   507    507     }
   508    508   
   509    509     /*
   510         -  **  PRAGMA [database.]max_page_count
   511         -  **  PRAGMA [database.]max_page_count=N
          510  +  **  PRAGMA [schema.]max_page_count
          511  +  **  PRAGMA [schema.]max_page_count=N
   512    512     **
   513    513     ** The first form reports the current setting for the
   514    514     ** maximum number of pages in the database file.  The 
   515    515     ** second form attempts to change this setting.  Both
   516    516     ** forms return the current setting.
   517    517     **
   518    518     ** The absolute value of N is used.  This is undocumented and might
   519    519     ** change.  The only purpose is to provide an easy way to test
   520    520     ** the sqlite3AbsInt32() function.
   521    521     **
   522         -  **  PRAGMA [database.]page_count
          522  +  **  PRAGMA [schema.]page_count
   523    523     **
   524    524     ** Return the number of pages in the specified database.
   525    525     */
   526    526     case PragTyp_PAGE_COUNT: {
   527    527       int iReg;
   528    528       sqlite3CodeVerifySchema(pParse, iDb);
   529    529       iReg = ++pParse->nMem;
................................................................................
   536    536       sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
   537    537       sqlite3VdbeSetNumCols(v, 1);
   538    538       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
   539    539       break;
   540    540     }
   541    541   
   542    542     /*
   543         -  **  PRAGMA [database.]locking_mode
   544         -  **  PRAGMA [database.]locking_mode = (normal|exclusive)
          543  +  **  PRAGMA [schema.]locking_mode
          544  +  **  PRAGMA [schema.]locking_mode = (normal|exclusive)
   545    545     */
   546    546     case PragTyp_LOCKING_MODE: {
   547    547       const char *zRet = "normal";
   548    548       int eMode = getLockingMode(zRight);
   549    549   
   550    550       if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
   551    551         /* Simple "PRAGMA locking_mode;" statement. This is a query for
................................................................................
   582    582         zRet = "exclusive";
   583    583       }
   584    584       returnSingleText(v, "locking_mode", zRet);
   585    585       break;
   586    586     }
   587    587   
   588    588     /*
   589         -  **  PRAGMA [database.]journal_mode
   590         -  **  PRAGMA [database.]journal_mode =
          589  +  **  PRAGMA [schema.]journal_mode
          590  +  **  PRAGMA [schema.]journal_mode =
   591    591     **                      (delete|persist|off|truncate|memory|wal|off)
   592    592     */
   593    593     case PragTyp_JOURNAL_MODE: {
   594    594       int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
   595    595       int ii;           /* Loop counter */
   596    596   
   597    597       setOneColumnName(v, "journal_mode");
................................................................................
   623    623         }
   624    624       }
   625    625       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   626    626       break;
   627    627     }
   628    628   
   629    629     /*
   630         -  **  PRAGMA [database.]journal_size_limit
   631         -  **  PRAGMA [database.]journal_size_limit=N
          630  +  **  PRAGMA [schema.]journal_size_limit
          631  +  **  PRAGMA [schema.]journal_size_limit=N
   632    632     **
   633    633     ** Get or set the size limit on rollback journal files.
   634    634     */
   635    635     case PragTyp_JOURNAL_SIZE_LIMIT: {
   636    636       Pager *pPager = sqlite3BtreePager(pDb->pBt);
   637    637       i64 iLimit = -2;
   638    638       if( zRight ){
................................................................................
   643    643       returnSingleInt(v, "journal_size_limit", iLimit);
   644    644       break;
   645    645     }
   646    646   
   647    647   #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
   648    648   
   649    649     /*
   650         -  **  PRAGMA [database.]auto_vacuum
   651         -  **  PRAGMA [database.]auto_vacuum=N
          650  +  **  PRAGMA [schema.]auto_vacuum
          651  +  **  PRAGMA [schema.]auto_vacuum=N
   652    652     **
   653    653     ** Get or set the value of the database 'auto-vacuum' parameter.
   654    654     ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
   655    655     */
   656    656   #ifndef SQLITE_OMIT_AUTOVACUUM
   657    657     case PragTyp_AUTO_VACUUM: {
   658    658       Btree *pBt = pDb->pBt;
................................................................................
   695    695         }
   696    696       }
   697    697       break;
   698    698     }
   699    699   #endif
   700    700   
   701    701     /*
   702         -  **  PRAGMA [database.]incremental_vacuum(N)
          702  +  **  PRAGMA [schema.]incremental_vacuum(N)
   703    703     **
   704    704     ** Do N steps of incremental vacuuming on a database.
   705    705     */
   706    706   #ifndef SQLITE_OMIT_AUTOVACUUM
   707    707     case PragTyp_INCREMENTAL_VACUUM: {
   708    708       int iLimit, addr;
   709    709       if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
................................................................................
   718    718       sqlite3VdbeJumpHere(v, addr);
   719    719       break;
   720    720     }
   721    721   #endif
   722    722   
   723    723   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   724    724     /*
   725         -  **  PRAGMA [database.]cache_size
   726         -  **  PRAGMA [database.]cache_size=N
          725  +  **  PRAGMA [schema.]cache_size
          726  +  **  PRAGMA [schema.]cache_size=N
   727    727     **
   728    728     ** The first form reports the current local setting for the
   729    729     ** page cache size. The second form sets the local
   730    730     ** page cache size value.  If N is positive then that is the
   731    731     ** number of pages in the cache.  If N is negative, then the
   732    732     ** number of pages is adjusted so that the cache uses -N kibibytes
   733    733     ** of memory.
   734    734     */
   735    735     case PragTyp_CACHE_SIZE: {
   736    736       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   737    737       if( !zRight ){
   738         -      if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   739    738         returnSingleInt(v, "cache_size", pDb->pSchema->cache_size);
   740    739       }else{
   741    740         int size = sqlite3Atoi(zRight);
   742    741         pDb->pSchema->cache_size = size;
   743    742         sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
   744         -      if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   745    743       }
   746    744       break;
   747    745     }
   748    746   
   749    747     /*
   750         -  **  PRAGMA [database.]mmap_size(N)
          748  +  **  PRAGMA [schema.]cache_spill
          749  +  **  PRAGMA cache_spill=BOOLEAN
          750  +  **  PRAGMA [schema.]cache_spill=N
          751  +  **
          752  +  ** The first form reports the current local setting for the
          753  +  ** page cache spill size. The second form turns cache spill on
          754  +  ** or off.  When turnning cache spill on, the size is set to the
          755  +  ** current cache_size.  The third form sets a spill size that
          756  +  ** may be different form the cache size.
          757  +  ** If N is positive then that is the
          758  +  ** number of pages in the cache.  If N is negative, then the
          759  +  ** number of pages is adjusted so that the cache uses -N kibibytes
          760  +  ** of memory.
          761  +  **
          762  +  ** If the number of cache_spill pages is less then the number of
          763  +  ** cache_size pages, no spilling occurs until the page count exceeds
          764  +  ** the number of cache_size pages.
          765  +  **
          766  +  ** The cache_spill=BOOLEAN setting applies to all attached schemas,
          767  +  ** not just the schema specified.
          768  +  */
          769  +  case PragTyp_CACHE_SPILL: {
          770  +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
          771  +    if( !zRight ){
          772  +      returnSingleInt(v, "cache_spill", 
          773  +         (db->flags & SQLITE_CacheSpill)==0 ? 0 : 
          774  +            sqlite3BtreeSetSpillSize(pDb->pBt,0));
          775  +    }else{
          776  +      int size = 1;
          777  +      if( sqlite3GetInt32(zRight, &size) ){
          778  +        sqlite3BtreeSetSpillSize(pDb->pBt, size);
          779  +      }
          780  +      if( sqlite3GetBoolean(zRight, size!=0) ){
          781  +        db->flags |= SQLITE_CacheSpill;
          782  +      }else{
          783  +        db->flags &= ~SQLITE_CacheSpill;
          784  +      }
          785  +      setAllPagerFlags(db);
          786  +    }
          787  +    break;
          788  +  }
          789  +
          790  +  /*
          791  +  **  PRAGMA [schema.]mmap_size(N)
   751    792     **
   752    793     ** Used to set mapping size limit. The mapping size limit is
   753    794     ** used to limit the aggregate size of all memory mapped regions of the
   754    795     ** database file. If this parameter is set to zero, then memory mapping
   755    796     ** is not used at all.  If N is negative, then the default memory map
   756    797     ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
   757    798     ** The parameter N is measured in bytes.
................................................................................
   887    928       }
   888    929       break;
   889    930     }
   890    931   #endif
   891    932   
   892    933   #if SQLITE_ENABLE_LOCKING_STYLE
   893    934     /*
   894         -  **   PRAGMA [database.]lock_proxy_file
   895         -  **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
          935  +  **   PRAGMA [schema.]lock_proxy_file
          936  +  **   PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
   896    937     **
   897    938     ** Return or set the value of the lock_proxy_file flag.  Changing
   898    939     ** the value sets a specific file to be used for database access locks.
   899    940     **
   900    941     */
   901    942     case PragTyp_LOCK_PROXY_FILE: {
   902    943       if( !zRight ){
................................................................................
   923    964         }
   924    965       }
   925    966       break;
   926    967     }
   927    968   #endif /* SQLITE_ENABLE_LOCKING_STYLE */      
   928    969       
   929    970     /*
   930         -  **   PRAGMA [database.]synchronous
   931         -  **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
          971  +  **   PRAGMA [schema.]synchronous
          972  +  **   PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL
   932    973     **
   933    974     ** Return or set the local value of the synchronous flag.  Changing
   934    975     ** the local value does not make changes to the disk file and the
   935    976     ** default value will be restored the next time the database is
   936    977     ** opened.
   937    978     */
   938    979     case PragTyp_SYNCHRONOUS: {
................................................................................
  1640   1681       }
  1641   1682     }
  1642   1683     break;
  1643   1684   #endif /* SQLITE_OMIT_UTF16 */
  1644   1685   
  1645   1686   #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
  1646   1687     /*
  1647         -  **   PRAGMA [database.]schema_version
  1648         -  **   PRAGMA [database.]schema_version = <integer>
         1688  +  **   PRAGMA [schema.]schema_version
         1689  +  **   PRAGMA [schema.]schema_version = <integer>
         1690  +  **
         1691  +  **   PRAGMA [schema.]user_version
         1692  +  **   PRAGMA [schema.]user_version = <integer>
  1649   1693     **
  1650         -  **   PRAGMA [database.]user_version
  1651         -  **   PRAGMA [database.]user_version = <integer>
         1694  +  **   PRAGMA [schema.]freelist_count = <integer>
  1652   1695     **
  1653         -  **   PRAGMA [database.]freelist_count = <integer>
  1654         -  **
  1655         -  **   PRAGMA [database.]application_id
  1656         -  **   PRAGMA [database.]application_id = <integer>
         1696  +  **   PRAGMA [schema.]application_id
         1697  +  **   PRAGMA [schema.]application_id = <integer>
  1657   1698     **
  1658   1699     ** The pragma's schema_version and user_version are used to set or get
  1659   1700     ** the value of the schema-version and user-version, respectively. Both
  1660   1701     ** the schema-version and the user-version are 32-bit signed integers
  1661   1702     ** stored in the database header.
  1662   1703     **
  1663   1704     ** The schema-cookie is usually only manipulated internally by SQLite. It
................................................................................
  1724   1765       }
  1725   1766     }
  1726   1767     break;
  1727   1768   #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
  1728   1769   
  1729   1770   #ifndef SQLITE_OMIT_WAL
  1730   1771     /*
  1731         -  **   PRAGMA [database.]wal_checkpoint = passive|full|restart|truncate
         1772  +  **   PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
  1732   1773     **
  1733   1774     ** Checkpoint the database.
  1734   1775     */
  1735   1776     case PragTyp_WAL_CHECKPOINT: {
  1736   1777       static const char *azCol[] = { "busy", "log", "checkpointed" };
  1737   1778       int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
  1738   1779       int eMode = SQLITE_CHECKPOINT_PASSIVE;

Changes to src/pragma.h.

     4      4   ** that script and rerun it.
     5      5   */
     6      6   #define PragTyp_HEADER_VALUE                   0
     7      7   #define PragTyp_AUTO_VACUUM                    1
     8      8   #define PragTyp_FLAG                           2
     9      9   #define PragTyp_BUSY_TIMEOUT                   3
    10     10   #define PragTyp_CACHE_SIZE                     4
    11         -#define PragTyp_CASE_SENSITIVE_LIKE            5
    12         -#define PragTyp_COLLATION_LIST                 6
    13         -#define PragTyp_COMPILE_OPTIONS                7
    14         -#define PragTyp_DATA_STORE_DIRECTORY           8
    15         -#define PragTyp_DATABASE_LIST                  9
    16         -#define PragTyp_DEFAULT_CACHE_SIZE            10
    17         -#define PragTyp_ENCODING                      11
    18         -#define PragTyp_FOREIGN_KEY_CHECK             12
    19         -#define PragTyp_FOREIGN_KEY_LIST              13
    20         -#define PragTyp_INCREMENTAL_VACUUM            14
    21         -#define PragTyp_INDEX_INFO                    15
    22         -#define PragTyp_INDEX_LIST                    16
    23         -#define PragTyp_INTEGRITY_CHECK               17
    24         -#define PragTyp_JOURNAL_MODE                  18
    25         -#define PragTyp_JOURNAL_SIZE_LIMIT            19
    26         -#define PragTyp_LOCK_PROXY_FILE               20
    27         -#define PragTyp_LOCKING_MODE                  21
    28         -#define PragTyp_PAGE_COUNT                    22
    29         -#define PragTyp_MMAP_SIZE                     23
    30         -#define PragTyp_PAGE_SIZE                     24
    31         -#define PragTyp_SECURE_DELETE                 25
    32         -#define PragTyp_SHRINK_MEMORY                 26
    33         -#define PragTyp_SOFT_HEAP_LIMIT               27
    34         -#define PragTyp_STATS                         28
    35         -#define PragTyp_SYNCHRONOUS                   29
    36         -#define PragTyp_TABLE_INFO                    30
    37         -#define PragTyp_TEMP_STORE                    31
    38         -#define PragTyp_TEMP_STORE_DIRECTORY          32
    39         -#define PragTyp_THREADS                       33
    40         -#define PragTyp_WAL_AUTOCHECKPOINT            34
    41         -#define PragTyp_WAL_CHECKPOINT                35
    42         -#define PragTyp_ACTIVATE_EXTENSIONS           36
    43         -#define PragTyp_HEXKEY                        37
    44         -#define PragTyp_KEY                           38
    45         -#define PragTyp_REKEY                         39
    46         -#define PragTyp_LOCK_STATUS                   40
    47         -#define PragTyp_PARSER_TRACE                  41
           11  +#define PragTyp_CACHE_SPILL                    5
           12  +#define PragTyp_CASE_SENSITIVE_LIKE            6
           13  +#define PragTyp_COLLATION_LIST                 7
           14  +#define PragTyp_COMPILE_OPTIONS                8
           15  +#define PragTyp_DATA_STORE_DIRECTORY           9
           16  +#define PragTyp_DATABASE_LIST                 10
           17  +#define PragTyp_DEFAULT_CACHE_SIZE            11
           18  +#define PragTyp_ENCODING                      12
           19  +#define PragTyp_FOREIGN_KEY_CHECK             13
           20  +#define PragTyp_FOREIGN_KEY_LIST              14
           21  +#define PragTyp_INCREMENTAL_VACUUM            15
           22  +#define PragTyp_INDEX_INFO                    16
           23  +#define PragTyp_INDEX_LIST                    17
           24  +#define PragTyp_INTEGRITY_CHECK               18
           25  +#define PragTyp_JOURNAL_MODE                  19
           26  +#define PragTyp_JOURNAL_SIZE_LIMIT            20
           27  +#define PragTyp_LOCK_PROXY_FILE               21
           28  +#define PragTyp_LOCKING_MODE                  22
           29  +#define PragTyp_PAGE_COUNT                    23
           30  +#define PragTyp_MMAP_SIZE                     24
           31  +#define PragTyp_PAGE_SIZE                     25
           32  +#define PragTyp_SECURE_DELETE                 26
           33  +#define PragTyp_SHRINK_MEMORY                 27
           34  +#define PragTyp_SOFT_HEAP_LIMIT               28
           35  +#define PragTyp_STATS                         29
           36  +#define PragTyp_SYNCHRONOUS                   30
           37  +#define PragTyp_TABLE_INFO                    31
           38  +#define PragTyp_TEMP_STORE                    32
           39  +#define PragTyp_TEMP_STORE_DIRECTORY          33
           40  +#define PragTyp_THREADS                       34
           41  +#define PragTyp_WAL_AUTOCHECKPOINT            35
           42  +#define PragTyp_WAL_CHECKPOINT                36
           43  +#define PragTyp_ACTIVATE_EXTENSIONS           37
           44  +#define PragTyp_HEXKEY                        38
           45  +#define PragTyp_KEY                           39
           46  +#define PragTyp_REKEY                         40
           47  +#define PragTyp_LOCK_STATUS                   41
           48  +#define PragTyp_PARSER_TRACE                  42
    48     49   #define PragFlag_NeedSchema           0x01
    49     50   #define PragFlag_ReadOnly             0x02
    50     51   static const struct sPragmaNames {
    51     52     const char *const zName;  /* Name of pragma */
    52     53     u8 ePragTyp;              /* PragTyp_XXX value */
    53     54     u8 mPragFlag;             /* Zero or more PragFlag_XXX values */
    54     55     u32 iArg;                 /* Extra argument */
................................................................................
    82     83     { /* zName:     */ "busy_timeout",
    83     84       /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
    84     85       /* ePragFlag: */ 0,
    85     86       /* iArg:      */ 0 },
    86     87   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
    87     88     { /* zName:     */ "cache_size",
    88     89       /* ePragTyp:  */ PragTyp_CACHE_SIZE,
    89         -    /* ePragFlag: */ 0,
           90  +    /* ePragFlag: */ PragFlag_NeedSchema,
    90     91       /* iArg:      */ 0 },
    91     92   #endif
    92     93   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
    93     94     { /* zName:     */ "cache_spill",
    94         -    /* ePragTyp:  */ PragTyp_FLAG,
           95  +    /* ePragTyp:  */ PragTyp_CACHE_SPILL,
    95     96       /* ePragFlag: */ 0,
    96         -    /* iArg:      */ SQLITE_CacheSpill },
           97  +    /* iArg:      */ 0 },
    97     98   #endif
    98     99     { /* zName:     */ "case_sensitive_like",
    99    100       /* ePragTyp:  */ PragTyp_CASE_SENSITIVE_LIKE,
   100    101       /* ePragFlag: */ 0,
   101    102       /* iArg:      */ 0 },
   102    103     { /* zName:     */ "cell_size_check",
   103    104       /* ePragTyp:  */ PragTyp_FLAG,

Changes to src/printf.c.

   722    722       */
   723    723       width -= length;
   724    724       if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
   725    725       sqlite3StrAccumAppend(pAccum, bufpt, length);
   726    726       if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
   727    727   
   728    728       if( zExtra ){
   729         -      sqlite3_free(zExtra);
          729  +      sqlite3DbFree(pAccum->db, zExtra);
   730    730         zExtra = 0;
   731    731       }
   732    732     }/* End for loop over the format string */
   733    733   } /* End of function */
   734    734   
   735    735   /*
   736    736   ** Enlarge the memory allocation on a StrAccum object so that it is

Changes to src/resolve.c.

  1419   1419   ** list rather than a single expression.
  1420   1420   */
  1421   1421   int sqlite3ResolveExprListNames( 
  1422   1422     NameContext *pNC,       /* Namespace to resolve expressions in. */
  1423   1423     ExprList *pList         /* The expression list to be analyzed. */
  1424   1424   ){
  1425   1425     int i;
  1426         -  assert( pList!=0 );
  1427         -  for(i=0; i<pList->nExpr; i++){
  1428         -    if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
         1426  +  if( pList ){
         1427  +    for(i=0; i<pList->nExpr; i++){
         1428  +      if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
         1429  +    }
  1429   1430     }
  1430   1431     return WRC_Continue;
  1431   1432   }
  1432   1433   
  1433   1434   /*
  1434   1435   ** Resolve all names in all expressions of a SELECT and in all
  1435   1436   ** decendents of the SELECT, including compounds off of p->pPrior,

Changes to src/select.c.

   114    114     pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
   115    115     if( pNew==0 ){
   116    116       assert( db->mallocFailed );
   117    117       pNew = &standin;
   118    118       memset(pNew, 0, sizeof(*pNew));
   119    119     }
   120    120     if( pEList==0 ){
   121         -    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
          121  +    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ASTERISK,0));
   122    122     }
   123    123     pNew->pEList = pEList;
   124    124     if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc));
   125    125     pNew->pSrc = pSrc;
   126    126     pNew->pWhere = pWhere;
   127    127     pNew->pGroupBy = pGroupBy;
   128    128     pNew->pHaving = pHaving;
................................................................................
  1592   1592     Parse *pParse,          /* Parsing context */
  1593   1593     ExprList *pEList,       /* Expr list from which to derive column names */
  1594   1594     i16 *pnCol,             /* Write the number of columns here */
  1595   1595     Column **paCol          /* Write the new column list here */
  1596   1596   ){
  1597   1597     sqlite3 *db = pParse->db;   /* Database connection */
  1598   1598     int i, j;                   /* Loop counters */
  1599         -  int cnt;                    /* Index added to make the name unique */
         1599  +  u32 cnt;                    /* Index added to make the name unique */
  1600   1600     Column *aCol, *pCol;        /* For looping over result columns */
  1601   1601     int nCol;                   /* Number of columns in the result set */
  1602   1602     Expr *p;                    /* Expression for a single result column */
  1603   1603     char *zName;                /* Column name */
  1604   1604     int nName;                  /* Size of name in zName[] */
         1605  +  Hash ht;                    /* Hash table of column names */
  1605   1606   
         1607  +  sqlite3HashInit(&ht);
  1606   1608     if( pEList ){
  1607   1609       nCol = pEList->nExpr;
  1608   1610       aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
  1609   1611       testcase( aCol==0 );
  1610   1612     }else{
  1611   1613       nCol = 0;
  1612   1614       aCol = 0;
  1613   1615     }
         1616  +  assert( nCol==(i16)nCol );
  1614   1617     *pnCol = nCol;
  1615   1618     *paCol = aCol;
  1616   1619   
  1617         -  for(i=0, pCol=aCol; i<nCol; i++, pCol++){
         1620  +  for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
  1618   1621       /* Get an appropriate name for the column
  1619   1622       */
  1620   1623       p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
  1621   1624       if( (zName = pEList->a[i].zName)!=0 ){
  1622   1625         /* If the column contains an "AS <name>" phrase, use <name> as the name */
  1623         -      zName = sqlite3DbStrDup(db, zName);
  1624   1626       }else{
  1625   1627         Expr *pColExpr = p;  /* The expression that is the result column name */
  1626   1628         Table *pTab;         /* Table associated with this expression */
  1627   1629         while( pColExpr->op==TK_DOT ){
  1628   1630           pColExpr = pColExpr->pRight;
  1629   1631           assert( pColExpr!=0 );
  1630   1632         }
  1631   1633         if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
  1632   1634           /* For columns use the column name name */
  1633   1635           int iCol = pColExpr->iColumn;
  1634   1636           pTab = pColExpr->pTab;
  1635   1637           if( iCol<0 ) iCol = pTab->iPKey;
  1636         -        zName = sqlite3MPrintf(db, "%s",
  1637         -                 iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
         1638  +        zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
  1638   1639         }else if( pColExpr->op==TK_ID ){
  1639   1640           assert( !ExprHasProperty(pColExpr, EP_IntValue) );
  1640         -        zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
         1641  +        zName = pColExpr->u.zToken;
  1641   1642         }else{
  1642   1643           /* Use the original text of the column expression as its name */
  1643         -        zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
         1644  +        zName = pEList->a[i].zSpan;
  1644   1645         }
  1645   1646       }
  1646         -    if( db->mallocFailed ){
  1647         -      sqlite3DbFree(db, zName);
  1648         -      break;
  1649         -    }
         1647  +    zName = sqlite3MPrintf(db, "%s", zName);
  1650   1648   
  1651   1649       /* Make sure the column name is unique.  If the name is not unique,
  1652   1650       ** append an integer to the name so that it becomes unique.
  1653   1651       */
  1654         -    nName = sqlite3Strlen30(zName);
  1655         -    for(j=cnt=0; j<i; j++){
  1656         -      if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
  1657         -        char *zNewName;
  1658         -        int k;
  1659         -        for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){}
  1660         -        if( k>=0 && zName[k]==':' ) nName = k;
  1661         -        zName[nName] = 0;
  1662         -        zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
  1663         -        sqlite3DbFree(db, zName);
  1664         -        zName = zNewName;
  1665         -        j = -1;
  1666         -        if( zName==0 ) break;
         1652  +    cnt = 0;
         1653  +    while( zName && sqlite3HashFind(&ht, zName)!=0 ){
         1654  +      nName = sqlite3Strlen30(zName);
         1655  +      if( nName>0 ){
         1656  +        for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
         1657  +        if( zName[j]==':' ) nName = j;
  1667   1658         }
         1659  +      zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
         1660  +      if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
  1668   1661       }
  1669   1662       pCol->zName = zName;
         1663  +    sqlite3ColumnPropertiesFromName(0, pCol);
         1664  +    if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
         1665  +      db->mallocFailed = 1;
         1666  +    }
  1670   1667     }
         1668  +  sqlite3HashClear(&ht);
  1671   1669     if( db->mallocFailed ){
  1672   1670       for(j=0; j<i; j++){
  1673   1671         sqlite3DbFree(db, aCol[j].zName);
  1674   1672       }
  1675   1673       sqlite3DbFree(db, aCol);
  1676   1674       *paCol = 0;
  1677   1675       *pnCol = 0;
................................................................................
  3622   3620       }
  3623   3621   
  3624   3622       /* Transfer the FROM clause terms from the subquery into the
  3625   3623       ** outer query.
  3626   3624       */
  3627   3625       for(i=0; i<nSubSrc; i++){
  3628   3626         sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
         3627  +      assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
  3629   3628         pSrc->a[i+iFrom] = pSubSrc->a[i];
  3630   3629         memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
  3631   3630       }
  3632   3631       pSrc->a[iFrom].fg.jointype = jointype;
  3633   3632     
  3634   3633       /* Now begin substituting subquery result set expressions for 
  3635   3634       ** references to the iParent in the outer query.
................................................................................
  3937   3936     pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
  3938   3937     if( pNew==0 ) return WRC_Abort;
  3939   3938     memset(&dummy, 0, sizeof(dummy));
  3940   3939     pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
  3941   3940     if( pNewSrc==0 ) return WRC_Abort;
  3942   3941     *pNew = *p;
  3943   3942     p->pSrc = pNewSrc;
  3944         -  p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ALL, 0));
         3943  +  p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
  3945   3944     p->op = TK_SELECT;
  3946   3945     p->pWhere = 0;
  3947   3946     pNew->pGroupBy = 0;
  3948   3947     pNew->pHaving = 0;
  3949   3948     pNew->pOrderBy = 0;
  3950   3949     p->pPrior = 0;
  3951   3950     p->pNext = 0;
................................................................................
  3955   3954     p->selFlags |= SF_Converted;
  3956   3955     assert( pNew->pPrior!=0 );
  3957   3956     pNew->pPrior->pNext = pNew;
  3958   3957     pNew->pLimit = 0;
  3959   3958     pNew->pOffset = 0;
  3960   3959     return WRC_Continue;
  3961   3960   }
         3961  +
         3962  +/*
         3963  +** Check to see if the FROM clause term pFrom has table-valued function
         3964  +** arguments.  If it does, leave an error message in pParse and return
         3965  +** non-zero, since pFrom is not allowed to be a table-valued function.
         3966  +*/
         3967  +static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){
         3968  +  if( pFrom->fg.isTabFunc ){
         3969  +    sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
         3970  +    return 1;
         3971  +  }
         3972  +  return 0;
         3973  +}
  3962   3974   
  3963   3975   #ifndef SQLITE_OMIT_CTE
  3964   3976   /*
  3965   3977   ** Argument pWith (which may be NULL) points to a linked list of nested 
  3966   3978   ** WITH contexts, from inner to outermost. If the table identified by 
  3967   3979   ** FROM clause element pItem is really a common-table-expression (CTE) 
  3968   3980   ** then return a pointer to the CTE definition for that table. Otherwise
................................................................................
  4051   4063       ** recursive reference to CTE pCte. Leave an error in pParse and return
  4052   4064       ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
  4053   4065       ** In this case, proceed.  */
  4054   4066       if( pCte->zCteErr ){
  4055   4067         sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
  4056   4068         return SQLITE_ERROR;
  4057   4069       }
         4070  +    if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR;
  4058   4071   
  4059   4072       assert( pFrom->pTab==0 );
  4060   4073       pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
  4061   4074       if( pTab==0 ) return WRC_Abort;
  4062   4075       pTab->nRef = 1;
  4063   4076       pTab->zName = sqlite3DbStrDup(db, pCte->zName);
  4064   4077       pTab->iPKey = -1;
................................................................................
  4244   4257         if( pTab->nRef==0xffff ){
  4245   4258           sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
  4246   4259              pTab->zName);
  4247   4260           pFrom->pTab = 0;
  4248   4261           return WRC_Abort;
  4249   4262         }
  4250   4263         pTab->nRef++;
         4264  +      if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
         4265  +        return WRC_Abort;
         4266  +      }
  4251   4267   #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
  4252         -      if( pTab->pSelect || IsVirtual(pTab) ){
         4268  +      if( IsVirtual(pTab) || pTab->pSelect ){
  4253   4269           i16 nCol;
  4254   4270           if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
  4255   4271           assert( pFrom->pSelect==0 );
  4256         -        if( pFrom->fg.isTabFunc && !IsVirtual(pTab) ){
  4257         -          sqlite3ErrorMsg(pParse, "'%s' is not a function", pTab->zName);
  4258         -          return WRC_Abort;
  4259         -        }
  4260   4272           pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
  4261   4273           sqlite3SelectSetName(pFrom->pSelect, pTab->zName);
  4262   4274           nCol = pTab->nCol;
  4263   4275           pTab->nCol = -1;
  4264   4276           sqlite3WalkSelect(pWalker, pFrom->pSelect);
  4265   4277           pTab->nCol = nCol;
  4266   4278         }
................................................................................
  4278   4290     if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
  4279   4291       return WRC_Abort;
  4280   4292     }
  4281   4293   
  4282   4294     /* For every "*" that occurs in the column list, insert the names of
  4283   4295     ** all columns in all tables.  And for every TABLE.* insert the names
  4284   4296     ** of all columns in TABLE.  The parser inserted a special expression
  4285         -  ** with the TK_ALL operator for each "*" that it found in the column list.
  4286         -  ** The following code just has to locate the TK_ALL expressions and expand
  4287         -  ** each one to the list of all columns in all tables.
         4297  +  ** with the TK_ASTERISK operator for each "*" that it found in the column
         4298  +  ** list.  The following code just has to locate the TK_ASTERISK
         4299  +  ** expressions and expand each one to the list of all columns in
         4300  +  ** all tables.
  4288   4301     **
  4289   4302     ** The first loop just checks to see if there are any "*" operators
  4290   4303     ** that need expanding.
  4291   4304     */
  4292   4305     for(k=0; k<pEList->nExpr; k++){
  4293   4306       pE = pEList->a[k].pExpr;
  4294         -    if( pE->op==TK_ALL ) break;
         4307  +    if( pE->op==TK_ASTERISK ) break;
  4295   4308       assert( pE->op!=TK_DOT || pE->pRight!=0 );
  4296   4309       assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
  4297         -    if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
         4310  +    if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
  4298   4311     }
  4299   4312     if( k<pEList->nExpr ){
  4300   4313       /*
  4301   4314       ** If we get here it means the result set contains one or more "*"
  4302   4315       ** operators that need to be expanded.  Loop through each expression
  4303   4316       ** in the result set and expand them one by one.
  4304   4317       */
................................................................................
  4308   4321       int longNames = (flags & SQLITE_FullColNames)!=0
  4309   4322                         && (flags & SQLITE_ShortColNames)==0;
  4310   4323   
  4311   4324       for(k=0; k<pEList->nExpr; k++){
  4312   4325         pE = a[k].pExpr;
  4313   4326         pRight = pE->pRight;
  4314   4327         assert( pE->op!=TK_DOT || pRight!=0 );
  4315         -      if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){
         4328  +      if( pE->op!=TK_ASTERISK
         4329  +       && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
         4330  +      ){
  4316   4331           /* This particular expression does not need to be expanded.
  4317   4332           */
  4318   4333           pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
  4319   4334           if( pNew ){
  4320   4335             pNew->a[pNew->nExpr-1].zName = a[k].zName;
  4321   4336             pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
  4322   4337             a[k].zName = 0;
................................................................................
  4360   4375               assert( zName );
  4361   4376               if( zTName && pSub
  4362   4377                && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
  4363   4378               ){
  4364   4379                 continue;
  4365   4380               }
  4366   4381   
  4367         -            /* If a column is marked as 'hidden' (currently only possible
  4368         -            ** for virtual tables), do not include it in the expanded
  4369         -            ** result-set list.
         4382  +            /* If a column is marked as 'hidden', omit it from the expanded
         4383  +            ** result-set list unless the SELECT has the SF_IncludeHidden
         4384  +            ** bit set.
  4370   4385               */
  4371         -            if( IsHiddenColumn(&pTab->aCol[j]) ){
  4372         -              assert(IsVirtual(pTab));
         4386  +            if( (p->selFlags & SF_IncludeHidden)==0
         4387  +             && IsHiddenColumn(&pTab->aCol[j]) 
         4388  +            ){
  4373   4389                 continue;
  4374   4390               }
  4375   4391               tableSeen = 1;
  4376   4392   
  4377   4393               if( i>0 && zTName==0 ){
  4378   4394                 if( (pFrom->fg.jointype & JT_NATURAL)!=0
  4379   4395                   && tableAndColumnIndex(pTabList, i, zName, 0, 0)
................................................................................
  4436   4452       }
  4437   4453       sqlite3ExprListDelete(db, pEList);
  4438   4454       p->pEList = pNew;
  4439   4455     }
  4440   4456   #if SQLITE_MAX_COLUMN
  4441   4457     if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
  4442   4458       sqlite3ErrorMsg(pParse, "too many columns in result set");
         4459  +    return WRC_Abort;
  4443   4460     }
  4444   4461   #endif
  4445   4462     return WRC_Continue;
  4446   4463   }
  4447   4464   
  4448   4465   /*
  4449   4466   ** No-op routine for the parse-tree walker.

Changes to src/shell.c.

   161    161   static int enableTimer = 0;
   162    162   
   163    163   /* Return the current wall-clock time */
   164    164   static sqlite3_int64 timeOfDay(void){
   165    165     static sqlite3_vfs *clockVfs = 0;
   166    166     sqlite3_int64 t;
   167    167     if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
   168         -  if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
          168  +  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
   169    169       clockVfs->xCurrentTimeInt64(clockVfs, &t);
   170    170     }else{
   171    171       double r;
   172    172       clockVfs->xCurrentTime(clockVfs, &r);
   173    173       t = (sqlite3_int64)(r*86400000.0);
   174    174     }
   175    175     return t;
................................................................................
  2556   2556        { "number of triggers:",
  2557   2557          "SELECT count(*) FROM %s WHERE type='trigger'" },
  2558   2558        { "number of views:",
  2559   2559          "SELECT count(*) FROM %s WHERE type='view'" },
  2560   2560        { "schema size:",
  2561   2561          "SELECT total(length(sql)) FROM %s" },
  2562   2562     };
  2563         -  sqlite3_file *pFile;
         2563  +  sqlite3_file *pFile = 0;
  2564   2564     int i;
  2565   2565     char *zSchemaTab;
  2566   2566     char *zDb = nArg>=2 ? azArg[1] : "main";
  2567   2567     unsigned char aHdr[100];
  2568   2568     open_db(p, 0);
  2569   2569     if( p->db==0 ) return 1;
  2570   2570     sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile);

Changes to src/sqlite.h.in.

  1595   1595   ** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large
  1596   1596   ** [sqlite3_malloc|heap allocations].
  1597   1597   ** This can help [Robson proof|prevent memory allocation failures] due to heap
  1598   1598   ** fragmentation in low-memory embedded systems.
  1599   1599   ** </dd>
  1600   1600   **
  1601   1601   ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
  1602         -** <dd> ^The SQLITE_CONFIG_PAGECACHE option a memory pool
         1602  +** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
  1603   1603   ** that SQLite can use for the database page cache with the default page
  1604   1604   ** cache implementation.  
  1605   1605   ** This configuration option is a no-op if an application-define page
  1606   1606   ** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
  1607   1607   ** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
  1608   1608   ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
  1609   1609   ** and the number of cache lines (N).

Changes to src/sqliteInt.h.

   523    523   ** number of pages.  A negative number N translations means that a buffer
   524    524   ** of -1024*N bytes is allocated and used for as many pages as it will hold.
   525    525   */
   526    526   #ifndef SQLITE_DEFAULT_PCACHE_INITSZ
   527    527   # define SQLITE_DEFAULT_PCACHE_INITSZ 100
   528    528   #endif
   529    529   
   530         -
   531    530   /*
   532    531   ** GCC does not define the offsetof() macro so we'll have to do it
   533    532   ** ourselves.
   534    533   */
   535    534   #ifndef offsetof
   536    535   #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
   537    536   #endif
................................................................................
  1379   1378     int nRef;
  1380   1379     void (*xDestroy)(void *);
  1381   1380     void *pUserData;
  1382   1381   };
  1383   1382   
  1384   1383   /*
  1385   1384   ** Possible values for FuncDef.flags.  Note that the _LENGTH and _TYPEOF
  1386         -** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  There
         1385  +** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  And
         1386  +** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC.  There
  1387   1387   ** are assert() statements in the code to verify this.
  1388   1388   */
  1389   1389   #define SQLITE_FUNC_ENCMASK  0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
  1390   1390   #define SQLITE_FUNC_LIKE     0x0004 /* Candidate for the LIKE optimization */
  1391   1391   #define SQLITE_FUNC_CASE     0x0008 /* Case-sensitive LIKE-type function */
  1392   1392   #define SQLITE_FUNC_EPHEM    0x0010 /* Ephemeral.  Delete with VDBE */
  1393   1393   #define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
................................................................................
  1670   1670     Schema *pSchema;     /* Schema that contains this table */
  1671   1671     Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
  1672   1672   };
  1673   1673   
  1674   1674   /*
  1675   1675   ** Allowed values for Table.tabFlags.
  1676   1676   **
  1677         -** TF_OOOHidden applies to virtual tables that have hidden columns that are
         1677  +** TF_OOOHidden applies to tables or view that have hidden columns that are
  1678   1678   ** followed by non-hidden columns.  Example:  "CREATE VIRTUAL TABLE x USING
  1679   1679   ** vtab1(a HIDDEN, b);".  Since "b" is a non-hidden column but "a" is hidden,
  1680   1680   ** the TF_OOOHidden attribute would apply in this case.  Such tables require
  1681   1681   ** special handling during INSERT processing.
  1682   1682   */
  1683   1683   #define TF_Readonly        0x01    /* Read-only system table */
  1684   1684   #define TF_Ephemeral       0x02    /* An ephemeral table */
................................................................................
  1693   1693   /*
  1694   1694   ** Test to see whether or not a table is a virtual table.  This is
  1695   1695   ** done as a macro so that it will be optimized out when virtual
  1696   1696   ** table support is omitted from the build.
  1697   1697   */
  1698   1698   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1699   1699   #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
  1700         -#  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
  1701   1700   #else
  1702   1701   #  define IsVirtual(X)      0
  1703         -#  define IsHiddenColumn(X) 0
  1704   1702   #endif
         1703  +
         1704  +/*
         1705  +** Macros to determine if a column is hidden.  IsOrdinaryHiddenColumn()
         1706  +** only works for non-virtual tables (ordinary tables and views) and is
         1707  +** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined.  The
         1708  +** IsHiddenColumn() macro is general purpose.
         1709  +*/
         1710  +#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
         1711  +#  define IsHiddenColumn(X)         (((X)->colFlags & COLFLAG_HIDDEN)!=0)
         1712  +#  define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
         1713  +#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
         1714  +#  define IsHiddenColumn(X)         (((X)->colFlags & COLFLAG_HIDDEN)!=0)
         1715  +#  define IsOrdinaryHiddenColumn(X) 0
         1716  +#else
         1717  +#  define IsHiddenColumn(X)         0
         1718  +#  define IsOrdinaryHiddenColumn(X) 0
         1719  +#endif
         1720  +
  1705   1721   
  1706   1722   /* Does the table have a rowid */
  1707   1723   #define HasRowid(X)     (((X)->tabFlags & TF_WithoutRowid)==0)
  1708   1724   #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
  1709   1725   
  1710   1726   /*
  1711   1727   ** Each foreign key constraint is an instance of the following structure.
................................................................................
  2492   2508   #define SF_Values          0x0100  /* Synthesized from VALUES clause */
  2493   2509   #define SF_MultiValue      0x0200  /* Single VALUES term with multiple rows */
  2494   2510   #define SF_NestedFrom      0x0400  /* Part of a parenthesized FROM clause */
  2495   2511   #define SF_MaybeConvert    0x0800  /* Need convertCompoundSelectToSubquery() */
  2496   2512   #define SF_MinMaxAgg       0x1000  /* Aggregate containing min() or max() */
  2497   2513   #define SF_Recursive       0x2000  /* The recursive part of a recursive CTE */
  2498   2514   #define SF_Converted       0x4000  /* By convertCompoundSelectToSubquery() */
         2515  +#define SF_IncludeHidden   0x8000  /* Include hidden columns in output */
  2499   2516   
  2500   2517   
  2501   2518   /*
  2502   2519   ** The results of a SELECT can be distributed in several ways, as defined
  2503   2520   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  2504   2521   ** Type".
  2505   2522   **
................................................................................
  3311   3328   void sqlite3DeleteColumnNames(sqlite3*,Table*);
  3312   3329   int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
  3313   3330   Table *sqlite3ResultSetOfSelect(Parse*,Select*);
  3314   3331   void sqlite3OpenMasterTable(Parse *, int);
  3315   3332   Index *sqlite3PrimaryKeyIndex(Table*);
  3316   3333   i16 sqlite3ColumnOfIndex(Index*, i16);
  3317   3334   void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
         3335  +void sqlite3ColumnPropertiesFromName(Table*, Column*);
  3318   3336   void sqlite3AddColumn(Parse*,Token*);
  3319   3337   void sqlite3AddNotNull(Parse*, int);
  3320   3338   void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
  3321   3339   void sqlite3AddCheckConstraint(Parse*, Expr*);
  3322   3340   void sqlite3AddColumnType(Parse*,Token*);
  3323   3341   void sqlite3AddDefaultValue(Parse*,ExprSpan*);
  3324   3342   void sqlite3AddCollateType(Parse*, Token*);

Changes to src/test1.c.

  2221   2221       return TCL_ERROR;
  2222   2222     }
  2223   2223     if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2224   2224     sqlite3_stmt_scanstatus_reset(pStmt);
  2225   2225     return TCL_OK;
  2226   2226   }
  2227   2227   #endif
         2228  +
         2229  +#ifdef SQLITE_ENABLE_SQLLOG
         2230  +/*
         2231  +** Usage:  sqlite3_config_sqllog
         2232  +**
         2233  +** Zero the SQLITE_CONFIG_SQLLOG configuration
         2234  +*/
         2235  +static int test_config_sqllog(
         2236  +  void * clientData,
         2237  +  Tcl_Interp *interp,
         2238  +  int objc,
         2239  +  Tcl_Obj *CONST objv[]
         2240  +){
         2241  +  if( objc!=1 ){
         2242  +    Tcl_WrongNumArgs(interp, 1, objv, "");
         2243  +    return TCL_ERROR;
         2244  +  }
         2245  +  sqlite3_config(SQLITE_CONFIG_SQLLOG, 0, 0);
         2246  +  return TCL_OK;
         2247  +}
         2248  +#endif
         2249  +
         2250  +/*
         2251  +** Usage: vfs_current_time_int64
         2252  +**
         2253  +** Return the value returned by the default VFS's xCurrentTimeInt64 method.
         2254  +*/
         2255  +static int vfsCurrentTimeInt64(
         2256  +  void * clientData,
         2257  +  Tcl_Interp *interp,
         2258  +  int objc,
         2259  +  Tcl_Obj *CONST objv[]
         2260  +){
         2261  +  i64 t;
         2262  +  sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
         2263  +  if( objc!=1 ){
         2264  +    Tcl_WrongNumArgs(interp, 1, objv, "");
         2265  +    return TCL_ERROR;
         2266  +  }
         2267  +  pVfs->xCurrentTimeInt64(pVfs, &t);
         2268  +  Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t));
         2269  +  return TCL_OK;
         2270  +}
  2228   2271   
  2229   2272   /*
  2230   2273   ** Usage:  sqlite3_next_stmt  DB  STMT
  2231   2274   **
  2232   2275   ** Return the next statment in sequence after STMT.
  2233   2276   */
  2234   2277   static int test_next_stmt(
................................................................................
  7032   7075        { "sqlite3_user_change",       test_user_change,       0 },
  7033   7076        { "sqlite3_user_delete",       test_user_delete,       0 },
  7034   7077   #endif
  7035   7078   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
  7036   7079        { "sqlite3_stmt_scanstatus",       test_stmt_scanstatus,   0 },
  7037   7080        { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset,   0 },
  7038   7081   #endif
  7039         -
         7082  +#ifdef SQLITE_ENABLE_SQLLOG
         7083  +     { "sqlite3_config_sqllog",         test_config_sqllog,   0 },
         7084  +#endif
         7085  +     { "vfs_current_time_int64",           vfsCurrentTimeInt64,   0 },
  7040   7086     };
  7041   7087     static int bitmask_size = sizeof(Bitmask)*8;
  7042   7088     static int longdouble_size = sizeof(LONGDOUBLE_TYPE);
  7043   7089     int i;
  7044   7090     extern int sqlite3_sync_count, sqlite3_fullsync_count;
  7045   7091     extern int sqlite3_opentemp_count;
  7046   7092     extern int sqlite3_like_count;

Changes to src/test_config.c.

   120    120   #endif
   121    121   
   122    122   #ifdef SQLITE_ENABLE_CURSOR_HINTS
   123    123     Tcl_SetVar2(interp, "sqlite_options", "cursorhints", "1", TCL_GLOBAL_ONLY);
   124    124   #else
   125    125     Tcl_SetVar2(interp, "sqlite_options", "cursorhints", "0", TCL_GLOBAL_ONLY);
   126    126   #endif
          127  +
          128  +#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
          129  +  Tcl_SetVar2(interp, "sqlite_options", "hiddencolumns", "1", TCL_GLOBAL_ONLY);
          130  +#else
          131  +  Tcl_SetVar2(interp, "sqlite_options", "hiddencolumns", "0", TCL_GLOBAL_ONLY);
          132  +#endif
   127    133   
   128    134   #ifdef SQLITE_ENABLE_MEMSYS3
   129    135     Tcl_SetVar2(interp, "sqlite_options", "mem3", "1", TCL_GLOBAL_ONLY);
   130    136   #else
   131    137     Tcl_SetVar2(interp, "sqlite_options", "mem3", "0", TCL_GLOBAL_ONLY);
   132    138   #endif
   133    139   
................................................................................
   652    658   #endif
   653    659   
   654    660   #ifdef YYTRACKMAXSTACKDEPTH
   655    661     Tcl_SetVar2(interp, "sqlite_options", "yytrackmaxstackdepth", "1", TCL_GLOBAL_ONLY);
   656    662   #else
   657    663     Tcl_SetVar2(interp, "sqlite_options", "yytrackmaxstackdepth", "0", TCL_GLOBAL_ONLY);
   658    664   #endif
          665  +
          666  +#ifdef SQLITE_ENABLE_SQLLOG
          667  +  Tcl_SetVar2(interp, "sqlite_options", "sqllog", "1", TCL_GLOBAL_ONLY);
          668  +#else
          669  +  Tcl_SetVar2(interp, "sqlite_options", "sqllog", "0", TCL_GLOBAL_ONLY);
          670  +#endif
   659    671   
   660    672   #define LINKVAR(x) { \
   661    673       static const int cv_ ## x = SQLITE_ ## x; \
   662    674       Tcl_LinkVar(interp, "SQLITE_" #x, (char *)&(cv_ ## x), \
   663    675                   TCL_LINK_INT | TCL_LINK_READ_ONLY); }
   664    676   
   665    677     LINKVAR( MAX_LENGTH );

Changes to src/test_sqllog.c.

    42     42   **
    43     43   **   Usually, if the application opens the same database file more than once
    44     44   **   (either by attaching it or by using more than one database handle), only
    45     45   **   a single copy is made. This behavior may be overridden (so that a 
    46     46   **   separate copy is taken each time the database file is opened or attached)
    47     47   **   by setting the environment variable SQLITE_SQLLOG_REUSE_FILES to 0.
    48     48   **
           49  +**   If the environment variable SQLITE_SQLLOG_CONDITIONAL is defined, then
           50  +**   logging is only done for database connections if a file named
           51  +**   "<database>-sqllog" exists in the same directly as the main database
           52  +**   file when it is first opened ("<database>" is replaced by the actual 
           53  +**   name of the main database file).
           54  +**
    49     55   ** OUTPUT:
    50     56   **
    51     57   **   The SQLITE_SQLLOG_DIR is populated with three types of files:
    52     58   **
    53     59   **      sqllog_N.db   - Copies of database files. N may be any integer.
    54     60   **
    55     61   **      sqllog_N.sql  - A list of SQL statements executed by a single
................................................................................
    84     90     return (int)getpid();
    85     91   #endif
    86     92   }
    87     93   
    88     94   /* Names of environment variables to be used */
    89     95   #define ENVIRONMENT_VARIABLE1_NAME "SQLITE_SQLLOG_DIR"
    90     96   #define ENVIRONMENT_VARIABLE2_NAME "SQLITE_SQLLOG_REUSE_FILES"
           97  +#define ENVIRONMENT_VARIABLE3_NAME "SQLITE_SQLLOG_CONDITIONAL"
    91     98   
    92     99   /* Assume that all database and database file names are shorted than this. */
    93    100   #define SQLLOG_NAMESZ 512
    94    101   
    95    102   /* Maximum number of simultaneous database connections the process may
    96    103   ** open (if any more are opened an error is logged using sqlite3_log()
    97    104   ** and processing is halted).
................................................................................
   112    119   */
   113    120   static struct SLGlobal {
   114    121     /* Protected by MUTEX_STATIC_MASTER */
   115    122     sqlite3_mutex *mutex;           /* Recursive mutex */
   116    123     int nConn;                      /* Size of aConn[] array */
   117    124   
   118    125     /* Protected by SLGlobal.mutex */
          126  +  int bConditional;               /* Only trace if *-sqllog file is present */
   119    127     int bReuse;                     /* True to avoid extra copies of db files */
   120    128     char zPrefix[SQLLOG_NAMESZ];    /* Prefix for all created files */
   121    129     char zIdx[SQLLOG_NAMESZ];       /* Full path to *.idx file */
   122    130     int iNextLog;                   /* Used to allocate file names */
   123    131     int iNextDb;                    /* Used to allocate database file names */
   124    132     int bRec;                       /* True if testSqllog() is called rec. */
   125    133     int iClock;                     /* Clock value */
................................................................................
   211    219     }
   212    220   
   213    221     fclose(fd);
   214    222     return zRet;
   215    223   }
   216    224   
   217    225   static int sqllogFindAttached(
   218         -  struct SLConn *p,               /* Database connection */
          226  +  sqlite3 *db,                    /* Database connection */
   219    227     const char *zSearch,            /* Name to search for (or NULL) */
   220    228     char *zName,                    /* OUT: Name of attached database */
   221    229     char *zFile                     /* OUT: Name of attached file */
   222    230   ){
   223    231     sqlite3_stmt *pStmt;
   224    232     int rc;
   225    233   
   226    234     /* The "PRAGMA database_list" command returns a list of databases in the
   227    235     ** order that they were attached. So a newly attached database is 
   228    236     ** described by the last row returned.  */
   229    237     assert( sqllogglobal.bRec==0 );
   230    238     sqllogglobal.bRec = 1;
   231         -  rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
          239  +  rc = sqlite3_prepare_v2(db, "PRAGMA database_list", -1, &pStmt, 0);
   232    240     if( rc==SQLITE_OK ){
   233    241       while( SQLITE_ROW==sqlite3_step(pStmt) ){
   234    242         const char *zVal1; int nVal1;
   235    243         const char *zVal2; int nVal2;
   236    244   
   237    245         zVal1 = (const char*)sqlite3_column_text(pStmt, 1);
   238    246         nVal1 = sqlite3_column_bytes(pStmt, 1);
   239         -      memcpy(zName, zVal1, nVal1+1);
          247  +      if( zName ){
          248  +        memcpy(zName, zVal1, nVal1+1);
          249  +      }
   240    250   
   241    251         zVal2 = (const char*)sqlite3_column_text(pStmt, 2);
   242    252         nVal2 = sqlite3_column_bytes(pStmt, 2);
   243    253         memcpy(zFile, zVal2, nVal2+1);
   244    254   
   245    255         if( zSearch && strlen(zSearch)==nVal1 
   246    256          && 0==sqlite3_strnicmp(zSearch, zVal1, nVal1)
................................................................................
   281    291   static void sqllogCopydb(struct SLConn *p, const char *zSearch, int bLog){
   282    292     char zName[SQLLOG_NAMESZ];      /* Attached database name */
   283    293     char zFile[SQLLOG_NAMESZ];      /* Database file name */
   284    294     char *zFree;
   285    295     char *zInit = 0;
   286    296     int rc;
   287    297   
   288         -  rc = sqllogFindAttached(p, zSearch, zName, zFile);
          298  +  rc = sqllogFindAttached(p->db, zSearch, zName, zFile);
   289    299     if( rc!=SQLITE_OK ) return;
   290    300   
   291    301     if( zFile[0]=='\0' ){
   292    302       zInit = sqlite3_mprintf("");
   293    303     }else{
   294    304       if( sqllogglobal.bReuse ){
   295    305         zInit = sqllogFindFile(zFile);
................................................................................
   400    410       /* Not an ATTACH statement. Write this directly to the log. */
   401    411       fprintf(p->fd, "%s; -- clock=%d\n", zSql, sqllogglobal.iClock++);
   402    412     }else{
   403    413       /* This is an ATTACH statement. Copy the database. */
   404    414       sqllogCopydb(p, 0, 1);
   405    415     }
   406    416   }
          417  +
          418  +/*
          419  +** The database handle passed as the only argument has just been opened.
          420  +** Return true if this module should log initial databases and SQL 
          421  +** statements for this connection, or false otherwise.
          422  +**
          423  +** If an error occurs, sqlite3_log() is invoked to report it to the user
          424  +** and zero returned.
          425  +*/
          426  +static int sqllogTraceDb(sqlite3 *db){
          427  +  int bRet = 1;
          428  +  if( sqllogglobal.bConditional ){
          429  +    char zFile[SQLLOG_NAMESZ];      /* Attached database name */
          430  +    int rc = sqllogFindAttached(db, "main", 0, zFile);
          431  +    if( rc==SQLITE_OK ){
          432  +      int nFile = strlen(zFile);
          433  +      if( (SQLLOG_NAMESZ-nFile)<8 ){
          434  +        sqlite3_log(SQLITE_IOERR, 
          435  +            "sqllogTraceDb(): database name too long (%d bytes)", nFile
          436  +        );
          437  +        bRet = 0;
          438  +      }else{
          439  +        memcpy(&zFile[nFile], "-sqllog", 8);
          440  +        bRet = !access(zFile, F_OK);
          441  +      }
          442  +    }
          443  +  }
          444  +  return bRet;
          445  +}
   407    446   
   408    447   /*
   409    448   ** The SQLITE_CONFIG_SQLLOG callback registered by sqlite3_init_sqllog().
   410    449   **
   411    450   ** The eType parameter has the following values:
   412    451   **
   413    452   **    0:  Opening a new database connection.  zSql is the name of the
................................................................................
   435    474   
   436    475     /* This is a database open command. */
   437    476     if( eType==0 ){
   438    477       sqlite3_mutex_enter(master);
   439    478       if( sqllogglobal.mutex==0 ){
   440    479         sqllogglobal.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
   441    480       }
   442         -    p = &sqllogglobal.aConn[sqllogglobal.nConn++];
   443         -    p->fd = 0;
   444         -    p->db = db;
   445         -    p->iLog = sqllogglobal.iNextLog++;
   446    481       sqlite3_mutex_leave(master);
   447    482   
   448         -    /* Open the log and take a copy of the main database file */
   449    483       sqlite3_mutex_enter(sqllogglobal.mutex);
   450         -    if( sqllogglobal.bRec==0 ){
          484  +    if( sqllogglobal.bRec==0 && sqllogTraceDb(db) ){
          485  +
          486  +      sqlite3_mutex_enter(master);
          487  +      p = &sqllogglobal.aConn[sqllogglobal.nConn++];
          488  +      p->fd = 0;
          489  +      p->db = db;
          490  +      p->iLog = sqllogglobal.iNextLog++;
          491  +      sqlite3_mutex_leave(master);
          492  +
          493  +      /* Open the log and take a copy of the main database file */
   451    494         sqllogOpenlog(p);
   452    495         if( p->fd ) sqllogCopydb(p, "main", 0);
   453    496       }
   454    497       sqlite3_mutex_leave(sqllogglobal.mutex);
   455    498     }
   456    499   
   457    500     else{
   458    501   
   459    502       int i;
   460    503       for(i=0; i<sqllogglobal.nConn; i++){
   461    504         p = &sqllogglobal.aConn[i];
   462    505         if( p->db==db ) break;
   463    506       }
   464         -    if( i==sqllogglobal.nConn ) return;
   465    507   
   466    508       /* A database handle close command */
   467    509       if( eType==2 ){
   468    510         sqlite3_mutex_enter(master);
   469         -      if( p->fd ) fclose(p->fd);
   470         -      p->db = 0;
   471         -      p->fd = 0;
          511  +      if( i<sqllogglobal.nConn ){
          512  +        if( p->fd ) fclose(p->fd);
          513  +        p->db = 0;
          514  +        p->fd = 0;
          515  +        sqllogglobal.nConn--;
          516  +      }
   472    517   
   473         -      sqllogglobal.nConn--;
   474    518         if( sqllogglobal.nConn==0 ){
   475    519           sqlite3_mutex_free(sqllogglobal.mutex);
   476    520           sqllogglobal.mutex = 0;
   477         -      }else{
          521  +      }else if( i<sqllogglobal.nConn ){
   478    522           int nShift = &sqllogglobal.aConn[sqllogglobal.nConn] - p;
   479    523           if( nShift>0 ){
   480    524             memmove(p, &p[1], nShift*sizeof(struct SLConn));
   481    525           }
   482    526         }
   483    527         sqlite3_mutex_leave(master);
   484    528   
   485    529       /* An ordinary SQL command. */
   486         -    }else if( p->fd ){
          530  +    }else if( i<sqllogglobal.nConn && p->fd ){
   487    531         sqlite3_mutex_enter(sqllogglobal.mutex);
   488    532         if( sqllogglobal.bRec==0 ){
   489    533           testSqllogStmt(p, zSql);
   490    534         }
   491    535         sqlite3_mutex_leave(sqllogglobal.mutex);
   492    536       }
   493    537     }
................................................................................
   500    544   ** database activity.
   501    545   */
   502    546   void sqlite3_init_sqllog(void){
   503    547     if( getenv(ENVIRONMENT_VARIABLE1_NAME) ){
   504    548       if( SQLITE_OK==sqlite3_config(SQLITE_CONFIG_SQLLOG, testSqllog, 0) ){
   505    549         memset(&sqllogglobal, 0, sizeof(sqllogglobal));
   506    550         sqllogglobal.bReuse = 1;
          551  +      if( getenv(ENVIRONMENT_VARIABLE3_NAME) ){
          552  +        sqllogglobal.bConditional = 1;
          553  +      }
   507    554       }
   508    555     }
   509    556   }

Changes to src/util.c.

   551    551       *pNum = -(i64)u;
   552    552     }else{
   553    553       *pNum = (i64)u;
   554    554     }
   555    555     testcase( i==18 );
   556    556     testcase( i==19 );
   557    557     testcase( i==20 );
   558         -  if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum ){
          558  +  if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum)
          559  +       || i>19*incr || nonNum ){
   559    560       /* zNum is empty or contains non-numeric text or is longer
   560    561       ** than 19 digits (thus guaranteeing that it is too large) */
   561    562       return 1;
   562    563     }else if( i<19*incr ){
   563    564       /* Less than 19 digits, so we know that it fits in 64 bits */
   564    565       assert( u<=LARGEST_INT64 );
   565    566       return 0;
................................................................................
   840    841   
   841    842     p++;
   842    843     a = a<<14;
   843    844     a |= *p;
   844    845     /* a: p0<<28 | p2<<14 | p4 (unmasked) */
   845    846     if (!(a&0x80))
   846    847     {
   847         -    /* we can skip these cause they were (effectively) done above in calc'ing s */
          848  +    /* we can skip these cause they were (effectively) done above
          849  +    ** while calculating s */
   848    850       /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
   849    851       /* b &= (0x7f<<14)|(0x7f); */
   850    852       b = b<<7;
   851    853       a |= b;
   852    854       s = s>>18;
   853    855       *v = ((u64)s)<<32 | a;
   854    856       return 5;

Changes to src/vdbe.c.

   161    161   ** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
   162    162   */
   163    163   #define Deephemeralize(P) \
   164    164      if( ((P)->flags&MEM_Ephem)!=0 \
   165    165          && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
   166    166   
   167    167   /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
   168         -#define isSorter(x) ((x)->pSorter!=0)
          168  +#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
   169    169   
   170    170   /*
   171    171   ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
   172    172   ** if we run out of memory.
   173    173   */
   174    174   static VdbeCursor *allocateCursor(
   175    175     Vdbe *p,              /* The virtual machine */
   176    176     int iCur,             /* Index of the new VdbeCursor */
   177    177     int nField,           /* Number of fields in the table or index */
   178    178     int iDb,              /* Database the cursor belongs to, or -1 */
   179         -  int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
          179  +  u8 eCurType           /* Type of the new cursor */
   180    180   ){
   181    181     /* Find the memory cell that will be used to store the blob of memory
   182    182     ** required for this VdbeCursor structure. It is convenient to use a 
   183    183     ** vdbe memory cell to manage the memory allocation required for a
   184    184     ** VdbeCursor structure for the following reasons:
   185    185     **
   186    186     **   * Sometimes cursor numbers are used for a couple of different
................................................................................
   198    198     */
   199    199     Mem *pMem = &p->aMem[p->nMem-iCur];
   200    200   
   201    201     int nByte;
   202    202     VdbeCursor *pCx = 0;
   203    203     nByte = 
   204    204         ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + 
   205         -      (isBtreeCursor?sqlite3BtreeCursorSize():0);
          205  +      (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
   206    206   
   207    207     assert( iCur<p->nCursor );
   208    208     if( p->apCsr[iCur] ){
   209    209       sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
   210    210       p->apCsr[iCur] = 0;
   211    211     }
   212    212     if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
   213    213       p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
   214    214       memset(pCx, 0, sizeof(VdbeCursor));
          215  +    pCx->eCurType = eCurType;
   215    216       pCx->iDb = iDb;
   216    217       pCx->nField = nField;
   217    218       pCx->aOffset = &pCx->aType[nField];
   218         -    if( isBtreeCursor ){
   219         -      pCx->pCursor = (BtCursor*)
          219  +    if( eCurType==CURTYPE_BTREE ){
          220  +      pCx->uc.pCursor = (BtCursor*)
   220    221             &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
   221         -      sqlite3BtreeCursorZero(pCx->pCursor);
          222  +      sqlite3BtreeCursorZero(pCx->uc.pCursor);
   222    223       }
   223    224     }
   224    225     return pCx;
   225    226   }
   226    227   
   227    228   /*
   228    229   ** Try to convert a value into a numeric representation if we can
................................................................................
  2379   2380     pDest = &aMem[pOp->p3];
  2380   2381     memAboutToChange(p, pDest);
  2381   2382     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  2382   2383     pC = p->apCsr[pOp->p1];
  2383   2384     assert( pC!=0 );
  2384   2385     assert( p2<pC->nField );
  2385   2386     aOffset = pC->aOffset;
  2386         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  2387         -  assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
  2388         -#endif
  2389         -  pCrsr = pC->pCursor;
  2390         -  assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
  2391         -  assert( pCrsr!=0 || pC->nullRow );          /* pC->nullRow on PseudoTables */
         2387  +  assert( pC->eCurType!=CURTYPE_VTAB );
         2388  +  assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
         2389  +  assert( pC->eCurType!=CURTYPE_SORTER );
         2390  +  pCrsr = pC->uc.pCursor;
  2392   2391   
  2393   2392     /* If the cursor cache is stale, bring it up-to-date */
  2394   2393     rc = sqlite3VdbeCursorMoveto(pC);
  2395   2394     if( rc ) goto abort_due_to_error;
  2396   2395     if( pC->cacheStatus!=p->cacheCtr ){
  2397   2396       if( pC->nullRow ){
  2398         -      if( pCrsr==0 ){
  2399         -        assert( pC->pseudoTableReg>0 );
  2400         -        pReg = &aMem[pC->pseudoTableReg];
         2397  +      if( pC->eCurType==CURTYPE_PSEUDO ){
         2398  +        assert( pC->uc.pseudoTableReg>0 );
         2399  +        pReg = &aMem[pC->uc.pseudoTableReg];
  2401   2400           assert( pReg->flags & MEM_Blob );
  2402   2401           assert( memIsValid(pReg) );
  2403   2402           pC->payloadSize = pC->szRow = avail = pReg->n;
  2404   2403           pC->aRow = (u8*)pReg->z;
  2405   2404         }else{
  2406   2405           sqlite3VdbeMemSetNull(pDest);
  2407   2406           goto op_column_out;
  2408   2407         }
  2409   2408       }else{
         2409  +      assert( pC->eCurType==CURTYPE_BTREE );
  2410   2410         assert( pCrsr );
  2411   2411         if( pC->isTable==0 ){
  2412   2412           assert( sqlite3BtreeCursorIsValid(pCrsr) );
  2413   2413           VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
  2414   2414           assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
  2415   2415           /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
  2416   2416           ** payload size, so it is impossible for payloadSize64 to be
................................................................................
  2423   2423           VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
  2424   2424           assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
  2425   2425           pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
  2426   2426         }
  2427   2427         assert( avail<=65536 );  /* Maximum page size is 64KiB */
  2428   2428         if( pC->payloadSize <= (u32)avail ){
  2429   2429           pC->szRow = pC->payloadSize;
         2430  +      }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
         2431  +        goto too_big;
  2430   2432         }else{
  2431   2433           pC->szRow = avail;
  2432   2434         }
  2433         -      if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  2434         -        goto too_big;
  2435         -      }
  2436   2435       }
  2437   2436       pC->cacheStatus = p->cacheCtr;
  2438   2437       pC->iHdrOffset = getVarint32(pC->aRow, offset);
  2439   2438       pC->nHdrParsed = 0;
  2440   2439       aOffset[0] = offset;
  2441   2440   
  2442   2441   
................................................................................
  2792   2791   ** opened by cursor P1 in register P2
  2793   2792   */
  2794   2793   #ifndef SQLITE_OMIT_BTREECOUNT
  2795   2794   case OP_Count: {         /* out2 */
  2796   2795     i64 nEntry;
  2797   2796     BtCursor *pCrsr;
  2798   2797   
  2799         -  pCrsr = p->apCsr[pOp->p1]->pCursor;
         2798  +  assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
         2799  +  pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
  2800   2800     assert( pCrsr );
  2801   2801     nEntry = 0;  /* Not needed.  Only used to silence a warning. */
  2802   2802     rc = sqlite3BtreeCount(pCrsr, &nEntry);
  2803   2803     pOut = out2Prerelease(p, pOp);
  2804   2804     pOut->u.i = nEntry;
  2805   2805     break;
  2806   2806   }
................................................................................
  3377   3377       nField = pKeyInfo->nField+pKeyInfo->nXField;
  3378   3378     }else if( pOp->p4type==P4_INT32 ){
  3379   3379       nField = pOp->p4.i;
  3380   3380     }
  3381   3381     assert( pOp->p1>=0 );
  3382   3382     assert( nField>=0 );
  3383   3383     testcase( nField==0 );  /* Table with INTEGER PRIMARY KEY and nothing else */
  3384         -  pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
         3384  +  pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
  3385   3385     if( pCur==0 ) goto no_mem;
  3386   3386     pCur->nullRow = 1;
  3387   3387     pCur->isOrdered = 1;
  3388   3388     pCur->pgnoRoot = p2;
  3389         -  rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
         3389  +  rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
  3390   3390     pCur->pKeyInfo = pKeyInfo;
  3391   3391     /* Set the VdbeCursor.isTable variable. Previous versions of
  3392   3392     ** SQLite used to check if the root-page flags were sane at this point
  3393   3393     ** and report database corruption if they were not, but this check has
  3394   3394     ** since moved into the btree layer.  */  
  3395   3395     pCur->isTable = pOp->p4type!=P4_KEYINFO;
  3396   3396   
................................................................................
  3397   3397   open_cursor_set_hints:
  3398   3398     assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
  3399   3399     assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
  3400   3400     testcase( pOp->p5 & OPFLAG_BULKCSR );
  3401   3401   #ifdef SQLITE_ENABLE_CURSOR_HINT
  3402   3402     testcase( pOp->p2 & OPFLAG_SEEKEQ );
  3403   3403   #endif
  3404         -  sqlite3BtreeCursorHintFlags(pCur->pCursor,
         3404  +  sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
  3405   3405                                  (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
  3406   3406     break;
  3407   3407   }
  3408   3408   
  3409   3409   /* Opcode: OpenEphemeral P1 P2 * P4 P5
  3410   3410   ** Synopsis: nColumn=P2
  3411   3411   **
................................................................................
  3441   3441         SQLITE_OPEN_READWRITE |
  3442   3442         SQLITE_OPEN_CREATE |
  3443   3443         SQLITE_OPEN_EXCLUSIVE |
  3444   3444         SQLITE_OPEN_DELETEONCLOSE |
  3445   3445         SQLITE_OPEN_TRANSIENT_DB;
  3446   3446     assert( pOp->p1>=0 );
  3447   3447     assert( pOp->p2>=0 );
  3448         -  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
         3448  +  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
  3449   3449     if( pCx==0 ) goto no_mem;
  3450   3450     pCx->nullRow = 1;
  3451   3451     pCx->isEphemeral = 1;
  3452   3452     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, 
  3453   3453                           BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
  3454   3454     if( rc==SQLITE_OK ){
  3455   3455       rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
................................................................................
  3465   3465         assert( pOp->p4type==P4_KEYINFO );
  3466   3466         rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5); 
  3467   3467         if( rc==SQLITE_OK ){
  3468   3468           assert( pgno==MASTER_ROOT+1 );
  3469   3469           assert( pKeyInfo->db==db );
  3470   3470           assert( pKeyInfo->enc==ENC(db) );
  3471   3471           pCx->pKeyInfo = pKeyInfo;
  3472         -        rc = sqlite3BtreeCursor(pCx->pBt, pgno, BTREE_WRCSR, pKeyInfo, pCx->pCursor);
         3472  +        rc = sqlite3BtreeCursor(pCx->pBt, pgno, BTREE_WRCSR,
         3473  +                                pKeyInfo, pCx->uc.pCursor);
  3473   3474         }
  3474   3475         pCx->isTable = 0;
  3475   3476       }else{
  3476         -      rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, BTREE_WRCSR, 0, pCx->pCursor);
         3477  +      rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, BTREE_WRCSR,
         3478  +                              0, pCx->uc.pCursor);
  3477   3479         pCx->isTable = 1;
  3478   3480       }
  3479   3481     }
  3480   3482     pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
  3481   3483     break;
  3482   3484   }
  3483   3485   
................................................................................
  3492   3494   ** key is sufficient to produce the required results.
  3493   3495   */
  3494   3496   case OP_SorterOpen: {
  3495   3497     VdbeCursor *pCx;
  3496   3498   
  3497   3499     assert( pOp->p1>=0 );
  3498   3500     assert( pOp->p2>=0 );
  3499         -  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
         3501  +  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
  3500   3502     if( pCx==0 ) goto no_mem;
  3501   3503     pCx->pKeyInfo = pOp->p4.pKeyInfo;
  3502   3504     assert( pCx->pKeyInfo->db==db );
  3503   3505     assert( pCx->pKeyInfo->enc==ENC(db) );
  3504   3506     rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
  3505   3507     break;
  3506   3508   }
................................................................................
  3512   3514   ** to P2. Regardless of whether or not the jump is taken, increment the
  3513   3515   ** the sequence value.
  3514   3516   */
  3515   3517   case OP_SequenceTest: {
  3516   3518     VdbeCursor *pC;
  3517   3519     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3518   3520     pC = p->apCsr[pOp->p1];
  3519         -  assert( pC->pSorter );
         3521  +  assert( isSorter(pC) );
  3520   3522     if( (pC->seqCount++)==0 ){
  3521   3523       goto jump_to_p2;
  3522   3524     }
  3523   3525     break;
  3524   3526   }
  3525   3527   
  3526   3528   /* Opcode: OpenPseudo P1 P2 P3 * *
................................................................................
  3540   3542   ** the pseudo-table.
  3541   3543   */
  3542   3544   case OP_OpenPseudo: {
  3543   3545     VdbeCursor *pCx;
  3544   3546   
  3545   3547     assert( pOp->p1>=0 );
  3546   3548     assert( pOp->p3>=0 );
  3547         -  pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
         3549  +  pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
  3548   3550     if( pCx==0 ) goto no_mem;
  3549   3551     pCx->nullRow = 1;
  3550         -  pCx->pseudoTableReg = pOp->p2;
         3552  +  pCx->uc.pseudoTableReg = pOp->p2;
  3551   3553     pCx->isTable = 1;
  3552   3554     assert( pOp->p5==0 );
  3553   3555     break;
  3554   3556   }
  3555   3557   
  3556   3558   /* Opcode: Close P1 * * * *
  3557   3559   **
................................................................................
  3575   3577   ** first 63 columns of the table or index that are actually used
  3576   3578   ** by the cursor.  The high-order bit is set if any column after
  3577   3579   ** the 64th is used.
  3578   3580   */
  3579   3581   case OP_ColumnsUsed: {
  3580   3582     VdbeCursor *pC;
  3581   3583     pC = p->apCsr[pOp->p1];
  3582         -  assert( pC->pCursor );
         3584  +  assert( pC->eCurType==CURTYPE_BTREE );
  3583   3585     pC->maskUsed = *(u64*)pOp->p4.pI64;
  3584   3586     break;
  3585   3587   }
  3586   3588   #endif
  3587   3589   
  3588   3590   /* Opcode: SeekGE P1 P2 P3 P4 *
  3589   3591   ** Synopsis: key=r[P3@P4]
................................................................................
  3683   3685     i64 iKey;          /* The rowid we are to seek to */
  3684   3686     int eqOnly;        /* Only interested in == results */
  3685   3687   
  3686   3688     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3687   3689     assert( pOp->p2!=0 );
  3688   3690     pC = p->apCsr[pOp->p1];
  3689   3691     assert( pC!=0 );
  3690         -  assert( pC->pseudoTableReg==0 );
         3692  +  assert( pC->eCurType==CURTYPE_BTREE );
  3691   3693     assert( OP_SeekLE == OP_SeekLT+1 );
  3692   3694     assert( OP_SeekGE == OP_SeekLT+2 );
  3693   3695     assert( OP_SeekGT == OP_SeekLT+3 );
  3694   3696     assert( pC->isOrdered );
  3695         -  assert( pC->pCursor!=0 );
         3697  +  assert( pC->uc.pCursor!=0 );
  3696   3698     oc = pOp->opcode;
  3697   3699     eqOnly = 0;
  3698   3700     pC->nullRow = 0;
  3699   3701   #ifdef SQLITE_DEBUG
  3700   3702     pC->seekOp = pOp->opcode;
  3701   3703   #endif
  3702   3704   
  3703   3705     if( pC->isTable ){
  3704   3706       /* The BTREE_SEEK_EQ flag is only set on index cursors */
  3705         -    assert( sqlite3BtreeCursorHasHint(pC->pCursor, BTREE_SEEK_EQ)==0 );
         3707  +    assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 );
  3706   3708   
  3707   3709       /* The input value in P3 might be of any type: integer, real, string,
  3708   3710       ** blob, or NULL.  But it needs to be an integer before we can do
  3709   3711       ** the seek, so convert it. */
  3710   3712       pIn3 = &aMem[pOp->p3];
  3711   3713       if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
  3712   3714         applyNumericAffinity(pIn3, 0);
................................................................................
  3742   3744         else if( pIn3->u.r>(double)iKey ){
  3743   3745           assert( OP_SeekLE==(OP_SeekLT+1) );
  3744   3746           assert( OP_SeekGT==(OP_SeekGE+1) );
  3745   3747           assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
  3746   3748           if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
  3747   3749         }
  3748   3750       } 
  3749         -    rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
         3751  +    rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
  3750   3752       pC->movetoTarget = iKey;  /* Used by OP_Delete */
  3751   3753       if( rc!=SQLITE_OK ){
  3752   3754         goto abort_due_to_error;
  3753   3755       }
  3754   3756     }else{
  3755   3757       /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
  3756   3758       ** OP_SeekLE opcodes are allowed, and these must be immediately followed
  3757   3759       ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
  3758   3760       */
  3759         -    if( sqlite3BtreeCursorHasHint(pC->pCursor, BTREE_SEEK_EQ) ){
         3761  +    if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
  3760   3762         eqOnly = 1;
  3761   3763         assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
  3762   3764         assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
  3763   3765         assert( pOp[1].p1==pOp[0].p1 );
  3764   3766         assert( pOp[1].p2==pOp[0].p2 );
  3765   3767         assert( pOp[1].p3==pOp[0].p3 );
  3766   3768         assert( pOp[1].p4.i==pOp[0].p4.i );
................................................................................
  3787   3789   
  3788   3790       r.aMem = &aMem[pOp->p3];
  3789   3791   #ifdef SQLITE_DEBUG
  3790   3792       { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  3791   3793   #endif
  3792   3794       ExpandBlob(r.aMem);
  3793   3795       r.eqSeen = 0;
  3794         -    rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
         3796  +    rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
  3795   3797       if( rc!=SQLITE_OK ){
  3796   3798         goto abort_due_to_error;
  3797   3799       }
  3798   3800       if( eqOnly && r.eqSeen==0 ){
  3799   3801         assert( res!=0 );
  3800   3802         goto seek_not_found;
  3801   3803       }
................................................................................
  3804   3806     pC->cacheStatus = CACHE_STALE;
  3805   3807   #ifdef SQLITE_TEST
  3806   3808     sqlite3_search_count++;
  3807   3809   #endif
  3808   3810     if( oc>=OP_SeekGE ){  assert( oc==OP_SeekGE || oc==OP_SeekGT );
  3809   3811       if( res<0 || (res==0 && oc==OP_SeekGT) ){
  3810   3812         res = 0;
  3811         -      rc = sqlite3BtreeNext(pC->pCursor, &res);
         3813  +      rc = sqlite3BtreeNext(pC->uc.pCursor, &res);
  3812   3814         if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3813   3815       }else{
  3814   3816         res = 0;
  3815   3817       }
  3816   3818     }else{
  3817   3819       assert( oc==OP_SeekLT || oc==OP_SeekLE );
  3818   3820       if( res>0 || (res==0 && oc==OP_SeekLT) ){
  3819   3821         res = 0;
  3820         -      rc = sqlite3BtreePrevious(pC->pCursor, &res);
         3822  +      rc = sqlite3BtreePrevious(pC->uc.pCursor, &res);
  3821   3823         if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3822   3824       }else{
  3823   3825         /* res might be negative because the table is empty.  Check to
  3824   3826         ** see if this is the case.
  3825   3827         */
  3826         -      res = sqlite3BtreeEof(pC->pCursor);
         3828  +      res = sqlite3BtreeEof(pC->uc.pCursor);
  3827   3829       }
  3828   3830     }
  3829   3831   seek_not_found:
  3830   3832     assert( pOp->p2>0 );
  3831   3833     VdbeBranchTaken(res!=0,2);
  3832   3834     if( res ){
  3833   3835       goto jump_to_p2;
................................................................................
  3850   3852   */
  3851   3853   case OP_Seek: {    /* in2 */
  3852   3854     VdbeCursor *pC;
  3853   3855   
  3854   3856     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3855   3857     pC = p->apCsr[pOp->p1];
  3856   3858     assert( pC!=0 );
  3857         -  assert( pC->pCursor!=0 );
         3859  +  assert( pC->eCurType==CURTYPE_BTREE );
         3860  +  assert( pC->uc.pCursor!=0 );
  3858   3861     assert( pC->isTable );
  3859   3862     pC->nullRow = 0;
  3860   3863     pIn2 = &aMem[pOp->p2];
  3861   3864     pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
  3862   3865     pC->deferredMoveto = 1;
  3863   3866     break;
  3864   3867   }
................................................................................
  3944   3947     assert( pOp->p4type==P4_INT32 );
  3945   3948     pC = p->apCsr[pOp->p1];
  3946   3949     assert( pC!=0 );
  3947   3950   #ifdef SQLITE_DEBUG
  3948   3951     pC->seekOp = pOp->opcode;
  3949   3952   #endif
  3950   3953     pIn3 = &aMem[pOp->p3];
  3951         -  assert( pC->pCursor!=0 );
         3954  +  assert( pC->eCurType==CURTYPE_BTREE );
         3955  +  assert( pC->uc.pCursor!=0 );
  3952   3956     assert( pC->isTable==0 );
  3953   3957     pFree = 0;
  3954   3958     if( pOp->p4.i>0 ){
  3955   3959       r.pKeyInfo = pC->pKeyInfo;
  3956   3960       r.nField = (u16)pOp->p4.i;
  3957   3961       r.aMem = pIn3;
  3958   3962       for(ii=0; ii<r.nField; ii++){
................................................................................
  3981   3985       for(ii=0; ii<pIdxKey->nField; ii++){
  3982   3986         if( pIdxKey->aMem[ii].flags & MEM_Null ){
  3983   3987           takeJump = 1;
  3984   3988           break;
  3985   3989         }
  3986   3990       }
  3987   3991     }
  3988         -  rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
         3992  +  rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
  3989   3993     sqlite3DbFree(db, pFree);
  3990   3994     if( rc!=SQLITE_OK ){
  3991   3995       break;
  3992   3996     }
  3993   3997     pC->seekResult = res;
  3994   3998     alreadyExists = (res==0);
  3995   3999     pC->nullRow = 1-alreadyExists;
................................................................................
  4035   4039     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4036   4040     pC = p->apCsr[pOp->p1];
  4037   4041     assert( pC!=0 );
  4038   4042   #ifdef SQLITE_DEBUG
  4039   4043     pC->seekOp = 0;
  4040   4044   #endif
  4041   4045     assert( pC->isTable );
  4042         -  assert( pC->pseudoTableReg==0 );
  4043         -  pCrsr = pC->pCursor;
         4046  +  assert( pC->eCurType==CURTYPE_BTREE );
         4047  +  pCrsr = pC->uc.pCursor;
  4044   4048     assert( pCrsr!=0 );
  4045   4049     res = 0;
  4046   4050     iKey = pIn3->u.i;
  4047   4051     rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
  4048   4052     assert( rc==SQLITE_OK || res==0 );
  4049   4053     pC->movetoTarget = iKey;  /* Used by OP_Delete */
  4050   4054     pC->nullRow = 0;
................................................................................
  4070   4074   ** Write the sequence number into register P2.
  4071   4075   ** The sequence number on the cursor is incremented after this
  4072   4076   ** instruction.  
  4073   4077   */
  4074   4078   case OP_Sequence: {           /* out2 */
  4075   4079     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4076   4080     assert( p->apCsr[pOp->p1]!=0 );
         4081  +  assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
  4077   4082     pOut = out2Prerelease(p, pOp);
  4078   4083     pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
  4079   4084     break;
  4080   4085   }
  4081   4086   
  4082   4087   
  4083   4088   /* Opcode: NewRowid P1 P2 P3 * *
................................................................................
  4105   4110   
  4106   4111     v = 0;
  4107   4112     res = 0;
  4108   4113     pOut = out2Prerelease(p, pOp);
  4109   4114     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4110   4115     pC = p->apCsr[pOp->p1];
  4111   4116     assert( pC!=0 );
  4112         -  assert( pC->pCursor!=0 );
         4117  +  assert( pC->eCurType==CURTYPE_BTREE );
         4118  +  assert( pC->uc.pCursor!=0 );
  4113   4119     {
  4114   4120       /* The next rowid or record number (different terms for the same
  4115   4121       ** thing) is obtained in a two-step algorithm.
  4116   4122       **
  4117   4123       ** First we attempt to find the largest existing rowid and add one
  4118   4124       ** to that.  But if the largest existing rowid is already the maximum
  4119   4125       ** positive integer, we have to fall through to the second
................................................................................
  4133   4139       ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
  4134   4140       ** to provide the constant while making all compilers happy.
  4135   4141       */
  4136   4142   #   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
  4137   4143   #endif
  4138   4144   
  4139   4145       if( !pC->useRandomRowid ){
  4140         -      rc = sqlite3BtreeLast(pC->pCursor, &res);
         4146  +      rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
  4141   4147         if( rc!=SQLITE_OK ){
  4142   4148           goto abort_due_to_error;
  4143   4149         }
  4144   4150         if( res ){
  4145   4151           v = 1;   /* IMP: R-61914-48074 */
  4146   4152         }else{
  4147         -        assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
  4148         -        rc = sqlite3BtreeKeySize(pC->pCursor, &v);
         4153  +        assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
         4154  +        rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v);
  4149   4155           assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
  4150   4156           if( v>=MAX_ROWID ){
  4151   4157             pC->useRandomRowid = 1;
  4152   4158           }else{
  4153   4159             v++;   /* IMP: R-29538-34987 */
  4154   4160           }
  4155   4161         }
................................................................................
  4192   4198         ** it finds one that is not previously used. */
  4193   4199         assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
  4194   4200                                ** an AUTOINCREMENT table. */
  4195   4201         cnt = 0;
  4196   4202         do{
  4197   4203           sqlite3_randomness(sizeof(v), &v);
  4198   4204           v &= (MAX_ROWID>>1); v++;  /* Ensure that v is greater than zero */
  4199         -      }while(  ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
         4205  +      }while(  ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
  4200   4206                                                    0, &res))==SQLITE_OK)
  4201   4207               && (res==0)
  4202   4208               && (++cnt<100));
  4203   4209         if( rc==SQLITE_OK && res==0 ){
  4204   4210           rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
  4205   4211           goto abort_due_to_error;
  4206   4212         }
................................................................................
  4272   4278     int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
  4273   4279   
  4274   4280     pData = &aMem[pOp->p2];
  4275   4281     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4276   4282     assert( memIsValid(pData) );
  4277   4283     pC = p->apCsr[pOp->p1];
  4278   4284     assert( pC!=0 );
  4279         -  assert( pC->pCursor!=0 );
  4280         -  assert( pC->pseudoTableReg==0 );
         4285  +  assert( pC->eCurType==CURTYPE_BTREE );
         4286  +  assert( pC->uc.pCursor!=0 );
  4281   4287     assert( pC->isTable );
  4282   4288     REGISTER_TRACE(pOp->p2, pData);
  4283   4289   
  4284   4290     if( pOp->opcode==OP_Insert ){
  4285   4291       pKey = &aMem[pOp->p3];
  4286   4292       assert( pKey->flags & MEM_Int );
  4287   4293       assert( memIsValid(pKey) );
................................................................................
  4302   4308     }
  4303   4309     seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
  4304   4310     if( pData->flags & MEM_Zero ){
  4305   4311       nZero = pData->u.nZero;
  4306   4312     }else{
  4307   4313       nZero = 0;
  4308   4314     }
  4309         -  rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
         4315  +  rc = sqlite3BtreeInsert(pC->uc.pCursor, 0, iKey,
  4310   4316                             pData->z, pData->n, nZero,
  4311   4317                             (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
  4312   4318     );
  4313   4319     pC->deferredMoveto = 0;
  4314   4320     pC->cacheStatus = CACHE_STALE;
  4315   4321   
  4316   4322     /* Invoke the update-hook if required. */
................................................................................
  4349   4355   case OP_Delete: {
  4350   4356     VdbeCursor *pC;
  4351   4357     u8 hasUpdateCallback;
  4352   4358   
  4353   4359     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4354   4360     pC = p->apCsr[pOp->p1];
  4355   4361     assert( pC!=0 );
  4356         -  assert( pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
         4362  +  assert( pC->eCurType==CURTYPE_BTREE );
         4363  +  assert( pC->uc.pCursor!=0 );
  4357   4364     assert( pC->deferredMoveto==0 );
  4358   4365   
  4359   4366     hasUpdateCallback = db->xUpdateCallback && pOp->p4.z && pC->isTable;
  4360   4367     if( pOp->p5 && hasUpdateCallback ){
  4361         -    sqlite3BtreeKeySize(pC->pCursor, &pC->movetoTarget);
         4368  +    sqlite3BtreeKeySize(pC->uc.pCursor, &pC->movetoTarget);
  4362   4369     }
  4363   4370   
  4364   4371   #ifdef SQLITE_DEBUG
  4365   4372     /* The seek operation that positioned the cursor prior to OP_Delete will
  4366   4373     ** have also set the pC->movetoTarget field to the rowid of the row that
  4367   4374     ** is being deleted */
  4368   4375     if( pOp->p4.z && pC->isTable && pOp->p5==0 ){
  4369   4376       i64 iKey = 0;
  4370         -    sqlite3BtreeKeySize(pC->pCursor, &iKey);
         4377  +    sqlite3BtreeKeySize(pC->uc.pCursor, &iKey);
  4371   4378       assert( pC->movetoTarget==iKey ); 
  4372   4379     }
  4373   4380   #endif
  4374   4381    
  4375         -  rc = sqlite3BtreeDelete(pC->pCursor, pOp->p5);
         4382  +  rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
  4376   4383     pC->cacheStatus = CACHE_STALE;
  4377   4384   
  4378   4385     /* Invoke the update-hook if required. */
  4379   4386     if( rc==SQLITE_OK && hasUpdateCallback ){
  4380   4387       db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
  4381   4388                           db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget);
  4382   4389       assert( pC->iDb>=0 );
................................................................................
  4485   4492   
  4486   4493     pOut = &aMem[pOp->p2];
  4487   4494     memAboutToChange(p, pOut);
  4488   4495   
  4489   4496     /* Note that RowKey and RowData are really exactly the same instruction */
  4490   4497     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4491   4498     pC = p->apCsr[pOp->p1];
         4499  +  assert( pC!=0 );
         4500  +  assert( pC->eCurType==CURTYPE_BTREE );
  4492   4501     assert( isSorter(pC)==0 );
  4493   4502     assert( pC->isTable || pOp->opcode!=OP_RowData );
  4494   4503     assert( pC->isTable==0 || pOp->opcode==OP_RowData );
  4495         -  assert( pC!=0 );
  4496   4504     assert( pC->nullRow==0 );
  4497         -  assert( pC->pseudoTableReg==0 );
  4498         -  assert( pC->pCursor!=0 );
  4499         -  pCrsr = pC->pCursor;
         4505  +  assert( pC->uc.pCursor!=0 );
         4506  +  pCrsr = pC->uc.pCursor;
  4500   4507   
  4501   4508     /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
  4502   4509     ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
  4503   4510     ** the cursor.  If this where not the case, on of the following assert()s
  4504   4511     ** would fail.  Should this ever change (because of changes in the code
  4505   4512     ** generator) then the fix would be to insert a call to
  4506   4513     ** sqlite3VdbeCursorMoveto().
................................................................................
  4560   4567     sqlite3_vtab *pVtab;
  4561   4568     const sqlite3_module *pModule;
  4562   4569   
  4563   4570     pOut = out2Prerelease(p, pOp);
  4564   4571     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4565   4572     pC = p->apCsr[pOp->p1];
  4566   4573     assert( pC!=0 );
  4567         -  assert( pC->pseudoTableReg==0 || pC->nullRow );
         4574  +  assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
  4568   4575     if( pC->nullRow ){
  4569   4576       pOut->flags = MEM_Null;
  4570   4577       break;
  4571   4578     }else if( pC->deferredMoveto ){
  4572   4579       v = pC->movetoTarget;
  4573   4580   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4574         -  }else if( pC->pVtabCursor ){
  4575         -    pVtab = pC->pVtabCursor->pVtab;
         4581  +  }else if( pC->eCurType==CURTYPE_VTAB ){
         4582  +    assert( pC->uc.pVCur!=0 );
         4583  +    pVtab = pC->uc.pVCur->pVtab;
  4576   4584       pModule = pVtab->pModule;
  4577   4585       assert( pModule->xRowid );
  4578         -    rc = pModule->xRowid(pC->pVtabCursor, &v);
         4586  +    rc = pModule->xRowid(pC->uc.pVCur, &v);
  4579   4587       sqlite3VtabImportErrmsg(p, pVtab);
  4580   4588   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  4581   4589     }else{
  4582         -    assert( pC->pCursor!=0 );
         4590  +    assert( pC->eCurType==CURTYPE_BTREE );
         4591  +    assert( pC->uc.pCursor!=0 );
  4583   4592       rc = sqlite3VdbeCursorRestore(pC);
  4584   4593       if( rc ) goto abort_due_to_error;
  4585   4594       if( pC->nullRow ){
  4586   4595         pOut->flags = MEM_Null;
  4587   4596         break;
  4588   4597       }
  4589         -    rc = sqlite3BtreeKeySize(pC->pCursor, &v);
         4598  +    rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v);
  4590   4599       assert( rc==SQLITE_OK );  /* Always so because of CursorRestore() above */
  4591   4600     }
  4592   4601     pOut->u.i = v;
  4593   4602     break;
  4594   4603   }
  4595   4604   
  4596   4605   /* Opcode: NullRow P1 * * * *
................................................................................
  4603   4612     VdbeCursor *pC;
  4604   4613   
  4605   4614     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4606   4615     pC = p->apCsr[pOp->p1];
  4607   4616     assert( pC!=0 );
  4608   4617     pC->nullRow = 1;
  4609   4618     pC->cacheStatus = CACHE_STALE;
  4610         -  if( pC->pCursor ){
  4611         -    sqlite3BtreeClearCursor(pC->pCursor);
         4619  +  if( pC->eCurType==CURTYPE_BTREE ){
         4620  +    assert( pC->uc.pCursor!=0 );
         4621  +    sqlite3BtreeClearCursor(pC->uc.pCursor);
  4612   4622     }
  4613   4623     break;
  4614   4624   }
  4615   4625   
  4616   4626   /* Opcode: Last P1 P2 P3 * *
  4617   4627   **
  4618   4628   ** The next use of the Rowid or Column or Prev instruction for P1 
................................................................................
  4629   4639     VdbeCursor *pC;
  4630   4640     BtCursor *pCrsr;
  4631   4641     int res;
  4632   4642   
  4633   4643     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4634   4644     pC = p->apCsr[pOp->p1];
  4635   4645     assert( pC!=0 );
  4636         -  pCrsr = pC->pCursor;
         4646  +  assert( pC->eCurType==CURTYPE_BTREE );
         4647  +  pCrsr = pC->uc.pCursor;
  4637   4648     res = 0;
  4638   4649     assert( pCrsr!=0 );
  4639   4650     rc = sqlite3BtreeLast(pCrsr, &res);
  4640   4651     pC->nullRow = (u8)res;
  4641   4652     pC->deferredMoveto = 0;
  4642   4653     pC->cacheStatus = CACHE_STALE;
  4643   4654     pC->seekResult = pOp->p3;
................................................................................
  4697   4708     res = 1;
  4698   4709   #ifdef SQLITE_DEBUG
  4699   4710     pC->seekOp = OP_Rewind;
  4700   4711   #endif
  4701   4712     if( isSorter(pC) ){
  4702   4713       rc = sqlite3VdbeSorterRewind(pC, &res);
  4703   4714     }else{
  4704         -    pCrsr = pC->pCursor;
         4715  +    assert( pC->eCurType==CURTYPE_BTREE );
         4716  +    pCrsr = pC->uc.pCursor;
  4705   4717       assert( pCrsr );
  4706   4718       rc = sqlite3BtreeFirst(pCrsr, &res);
  4707   4719       pC->deferredMoveto = 0;
  4708   4720       pC->cacheStatus = CACHE_STALE;
  4709   4721     }
  4710   4722     pC->nullRow = (u8)res;
  4711   4723     assert( pOp->p2>0 && pOp->p2<p->nOp );
................................................................................
  4794   4806   case OP_Next:          /* jump */
  4795   4807     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4796   4808     assert( pOp->p5<ArraySize(p->aCounter) );
  4797   4809     pC = p->apCsr[pOp->p1];
  4798   4810     res = pOp->p3;
  4799   4811     assert( pC!=0 );
  4800   4812     assert( pC->deferredMoveto==0 );
  4801         -  assert( pC->pCursor );
         4813  +  assert( pC->eCurType==CURTYPE_BTREE );
  4802   4814     assert( res==0 || (res==1 && pC->isTable==0) );
  4803   4815     testcase( res==1 );
  4804   4816     assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
  4805   4817     assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
  4806   4818     assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
  4807   4819     assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
  4808   4820   
................................................................................
  4811   4823     assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
  4812   4824          || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
  4813   4825          || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
  4814   4826     assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
  4815   4827          || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
  4816   4828          || pC->seekOp==OP_Last );
  4817   4829   
  4818         -  rc = pOp->p4.xAdvance(pC->pCursor, &res);
         4830  +  rc = pOp->p4.xAdvance(pC->uc.pCursor, &res);
  4819   4831   next_tail:
  4820   4832     pC->cacheStatus = CACHE_STALE;
  4821   4833     VdbeBranchTaken(res==0,2);
  4822   4834     if( res==0 ){
  4823   4835       pC->nullRow = 0;
  4824   4836       p->aCounter[pOp->p5]++;
  4825   4837   #ifdef SQLITE_TEST
................................................................................
  4862   4874     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4863   4875     pC = p->apCsr[pOp->p1];
  4864   4876     assert( pC!=0 );
  4865   4877     assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
  4866   4878     pIn2 = &aMem[pOp->p2];
  4867   4879     assert( pIn2->flags & MEM_Blob );
  4868   4880     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
  4869         -  assert( pC->pCursor!=0 );
         4881  +  assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
  4870   4882     assert( pC->isTable==0 );
  4871   4883     rc = ExpandBlob(pIn2);
  4872   4884     if( rc==SQLITE_OK ){
  4873   4885       if( pOp->opcode==OP_SorterInsert ){
  4874   4886         rc = sqlite3VdbeSorterWrite(pC, pIn2);
  4875   4887       }else{
  4876   4888         nKey = pIn2->n;
  4877   4889         zKey = pIn2->z;
  4878         -      rc = sqlite3BtreeInsert(pC->pCursor, zKey, nKey, "", 0, 0, pOp->p3, 
         4890  +      rc = sqlite3BtreeInsert(pC->uc.pCursor, zKey, nKey, "", 0, 0, pOp->p3, 
  4879   4891             ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
  4880   4892             );
  4881   4893         assert( pC->deferredMoveto==0 );
  4882   4894         pC->cacheStatus = CACHE_STALE;
  4883   4895       }
  4884   4896     }
  4885   4897     break;
................................................................................
  4899   4911     UnpackedRecord r;
  4900   4912   
  4901   4913     assert( pOp->p3>0 );
  4902   4914     assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
  4903   4915     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4904   4916     pC = p->apCsr[pOp->p1];
  4905   4917     assert( pC!=0 );
  4906         -  pCrsr = pC->pCursor;
         4918  +  assert( pC->eCurType==CURTYPE_BTREE );
         4919  +  pCrsr = pC->uc.pCursor;
  4907   4920     assert( pCrsr!=0 );
  4908   4921     assert( pOp->p5==0 );
  4909   4922     r.pKeyInfo = pC->pKeyInfo;
  4910   4923     r.nField = (u16)pOp->p3;
  4911   4924     r.default_rc = 0;
  4912   4925     r.aMem = &aMem[pOp->p2];
  4913   4926   #ifdef SQLITE_DEBUG
................................................................................
  4936   4949     VdbeCursor *pC;
  4937   4950     i64 rowid;
  4938   4951   
  4939   4952     pOut = out2Prerelease(p, pOp);
  4940   4953     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4941   4954     pC = p->apCsr[pOp->p1];
  4942   4955     assert( pC!=0 );
  4943         -  pCrsr = pC->pCursor;
         4956  +  assert( pC->eCurType==CURTYPE_BTREE );
         4957  +  pCrsr = pC->uc.pCursor;
  4944   4958     assert( pCrsr!=0 );
  4945   4959     pOut->flags = MEM_Null;
  4946   4960     assert( pC->isTable==0 );
  4947   4961     assert( pC->deferredMoveto==0 );
  4948   4962   
  4949   4963     /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
  4950   4964     ** out from under the cursor.  That will never happend for an IdxRowid
................................................................................
  5017   5031     int res;
  5018   5032     UnpackedRecord r;
  5019   5033   
  5020   5034     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  5021   5035     pC = p->apCsr[pOp->p1];
  5022   5036     assert( pC!=0 );
  5023   5037     assert( pC->isOrdered );
  5024         -  assert( pC->pCursor!=0);
         5038  +  assert( pC->eCurType==CURTYPE_BTREE );
         5039  +  assert( pC->uc.pCursor!=0);
  5025   5040     assert( pC->deferredMoveto==0 );
  5026   5041     assert( pOp->p5==0 || pOp->p5==1 );
  5027   5042     assert( pOp->p4type==P4_INT32 );
  5028   5043     r.pKeyInfo = pC->pKeyInfo;
  5029   5044     r.nField = (u16)pOp->p4.i;
  5030   5045     if( pOp->opcode<OP_IdxLT ){
  5031   5046       assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
................................................................................
  5150   5165   */
  5151   5166   case OP_ResetSorter: {
  5152   5167     VdbeCursor *pC;
  5153   5168    
  5154   5169     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  5155   5170     pC = p->apCsr[pOp->p1];
  5156   5171     assert( pC!=0 );
  5157         -  if( pC->pSorter ){
  5158         -    sqlite3VdbeSorterReset(db, pC->pSorter);
         5172  +  if( isSorter(pC) ){
         5173  +    sqlite3VdbeSorterReset(db, pC->uc.pSorter);
  5159   5174     }else{
         5175  +    assert( pC->eCurType==CURTYPE_BTREE );
  5160   5176       assert( pC->isEphemeral );
  5161         -    rc = sqlite3BtreeClearTableOfCursor(pC->pCursor);
         5177  +    rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
  5162   5178     }
  5163   5179     break;
  5164   5180   }
  5165   5181   
  5166   5182   /* Opcode: CreateTable P1 P2 * * *
  5167   5183   ** Synopsis: r[P2]=root iDb=P1
  5168   5184   **
................................................................................
  6228   6244   **
  6229   6245   ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
  6230   6246   ** P1 is a cursor number.  This opcode opens a cursor to the virtual
  6231   6247   ** table and stores that cursor in P1.
  6232   6248   */
  6233   6249   case OP_VOpen: {
  6234   6250     VdbeCursor *pCur;
  6235         -  sqlite3_vtab_cursor *pVtabCursor;
         6251  +  sqlite3_vtab_cursor *pVCur;
  6236   6252     sqlite3_vtab *pVtab;
  6237   6253     const sqlite3_module *pModule;
  6238   6254   
  6239   6255     assert( p->bIsReader );
  6240   6256     pCur = 0;
  6241         -  pVtabCursor = 0;
         6257  +  pVCur = 0;
  6242   6258     pVtab = pOp->p4.pVtab->pVtab;
  6243   6259     if( pVtab==0 || NEVER(pVtab->pModule==0) ){
  6244   6260       rc = SQLITE_LOCKED;
  6245   6261       break;
  6246   6262     }
  6247   6263     pModule = pVtab->pModule;
  6248         -  rc = pModule->xOpen(pVtab, &pVtabCursor);
         6264  +  rc = pModule->xOpen(pVtab, &pVCur);
  6249   6265     sqlite3VtabImportErrmsg(p, pVtab);
  6250   6266     if( SQLITE_OK==rc ){
  6251   6267       /* Initialize sqlite3_vtab_cursor base class */
  6252         -    pVtabCursor->pVtab = pVtab;
         6268  +    pVCur->pVtab = pVtab;
  6253   6269   
  6254   6270       /* Initialize vdbe cursor object */
  6255         -    pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
         6271  +    pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
  6256   6272       if( pCur ){
  6257         -      pCur->pVtabCursor = pVtabCursor;
         6273  +      pCur->uc.pVCur = pVCur;
  6258   6274         pVtab->nRef++;
  6259   6275       }else{
  6260   6276         assert( db->mallocFailed );
  6261         -      pModule->xClose(pVtabCursor);
         6277  +      pModule->xClose(pVCur);
  6262   6278         goto no_mem;
  6263   6279       }
  6264   6280     }
  6265   6281     break;
  6266   6282   }
  6267   6283   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  6268   6284   
................................................................................
  6288   6304   */
  6289   6305   case OP_VFilter: {   /* jump */
  6290   6306     int nArg;
  6291   6307     int iQuery;
  6292   6308     const sqlite3_module *pModule;
  6293   6309     Mem *pQuery;
  6294   6310     Mem *pArgc;
  6295         -  sqlite3_vtab_cursor *pVtabCursor;
         6311  +  sqlite3_vtab_cursor *pVCur;
  6296   6312     sqlite3_vtab *pVtab;
  6297   6313     VdbeCursor *pCur;
  6298   6314     int res;
  6299   6315     int i;
  6300   6316     Mem **apArg;
  6301   6317   
  6302   6318     pQuery = &aMem[pOp->p3];
  6303   6319     pArgc = &pQuery[1];
  6304   6320     pCur = p->apCsr[pOp->p1];
  6305   6321     assert( memIsValid(pQuery) );
  6306   6322     REGISTER_TRACE(pOp->p3, pQuery);
  6307         -  assert( pCur->pVtabCursor );
  6308         -  pVtabCursor = pCur->pVtabCursor;
  6309         -  pVtab = pVtabCursor->pVtab;
         6323  +  assert( pCur->eCurType==CURTYPE_VTAB );
         6324  +  pVCur = pCur->uc.pVCur;
         6325  +  pVtab = pVCur->pVtab;
  6310   6326     pModule = pVtab->pModule;
  6311   6327   
  6312   6328     /* Grab the index number and argc parameters */
  6313   6329     assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
  6314   6330     nArg = (int)pArgc->u.i;
  6315   6331     iQuery = (int)pQuery->u.i;
  6316   6332   
  6317   6333     /* Invoke the xFilter method */
  6318   6334     res = 0;
  6319   6335     apArg = p->apArg;
  6320   6336     for(i = 0; i<nArg; i++){
  6321   6337       apArg[i] = &pArgc[i+1];
  6322   6338     }
  6323         -  rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
         6339  +  rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
  6324   6340     sqlite3VtabImportErrmsg(p, pVtab);
  6325   6341     if( rc==SQLITE_OK ){
  6326         -    res = pModule->xEof(pVtabCursor);
         6342  +    res = pModule->xEof(pVCur);
  6327   6343     }
  6328   6344     pCur->nullRow = 0;
  6329   6345     VdbeBranchTaken(res!=0,2);
  6330   6346     if( res ) goto jump_to_p2;
  6331   6347     break;
  6332   6348   }
  6333   6349   #endif /* SQLITE_OMIT_VIRTUALTABLE */
................................................................................
  6343   6359   case OP_VColumn: {
  6344   6360     sqlite3_vtab *pVtab;
  6345   6361     const sqlite3_module *pModule;
  6346   6362     Mem *pDest;
  6347   6363     sqlite3_context sContext;
  6348   6364   
  6349   6365     VdbeCursor *pCur = p->apCsr[pOp->p1];
  6350         -  assert( pCur->pVtabCursor );
         6366  +  assert( pCur->eCurType==CURTYPE_VTAB );
  6351   6367     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  6352   6368     pDest = &aMem[pOp->p3];
  6353   6369     memAboutToChange(p, pDest);
  6354   6370     if( pCur->nullRow ){
  6355   6371       sqlite3VdbeMemSetNull(pDest);
  6356   6372       break;
  6357   6373     }
  6358         -  pVtab = pCur->pVtabCursor->pVtab;
         6374  +  pVtab = pCur->uc.pVCur->pVtab;
  6359   6375     pModule = pVtab->pModule;
  6360   6376     assert( pModule->xColumn );
  6361   6377     memset(&sContext, 0, sizeof(sContext));
  6362   6378     sContext.pOut = pDest;
  6363   6379     MemSetTypeFlag(pDest, MEM_Null);
  6364         -  rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
         6380  +  rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
  6365   6381     sqlite3VtabImportErrmsg(p, pVtab);
  6366   6382     if( sContext.isError ){
  6367   6383       rc = sContext.isError;
  6368   6384     }
  6369   6385     sqlite3VdbeChangeEncoding(pDest, encoding);
  6370   6386     REGISTER_TRACE(pOp->p3, pDest);
  6371   6387     UPDATE_MAX_BLOBSIZE(pDest);
................................................................................
  6388   6404     sqlite3_vtab *pVtab;
  6389   6405     const sqlite3_module *pModule;
  6390   6406     int res;
  6391   6407     VdbeCursor *pCur;
  6392   6408   
  6393   6409     res = 0;
  6394   6410     pCur = p->apCsr[pOp->p1];
  6395         -  assert( pCur->pVtabCursor );
         6411  +  assert( pCur->eCurType==CURTYPE_VTAB );
  6396   6412     if( pCur->nullRow ){
  6397   6413       break;
  6398   6414     }
  6399         -  pVtab = pCur->pVtabCursor->pVtab;
         6415  +  pVtab = pCur->uc.pVCur->pVtab;
  6400   6416     pModule = pVtab->pModule;
  6401   6417     assert( pModule->xNext );
  6402   6418   
  6403   6419     /* Invoke the xNext() method of the module. There is no way for the
  6404   6420     ** underlying implementation to return an error if one occurs during
  6405   6421     ** xNext(). Instead, if an error occurs, true is returned (indicating that 
  6406   6422     ** data is available) and the error code returned when xColumn or
  6407   6423     ** some other method is next invoked on the save virtual table cursor.
  6408   6424     */
  6409         -  rc = pModule->xNext(pCur->pVtabCursor);
         6425  +  rc = pModule->xNext(pCur->uc.pVCur);
  6410   6426     sqlite3VtabImportErrmsg(p, pVtab);
  6411   6427     if( rc==SQLITE_OK ){
  6412         -    res = pModule->xEof(pCur->pVtabCursor);
         6428  +    res = pModule->xEof(pCur->uc.pVCur);
  6413   6429     }
  6414   6430     VdbeBranchTaken(!res,2);
  6415   6431     if( !res ){
  6416   6432       /* If there is data, jump to P2 */
  6417   6433       goto jump_to_p2_and_check_for_interrupt;
  6418   6434     }
  6419   6435     goto check_for_interrupt;
................................................................................
  6629   6645   case OP_CursorHint: {
  6630   6646     VdbeCursor *pC;
  6631   6647   
  6632   6648     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  6633   6649     assert( pOp->p4type==P4_EXPR );
  6634   6650     pC = p->apCsr[pOp->p1];
  6635   6651     if( pC ){
  6636         -    sqlite3BtreeCursorHint(pC->pCursor, BTREE_HINT_RANGE, pOp->p4.pExpr, aMem);
         6652  +    assert( pC->eCurType==CURTYPE_BTREE );
         6653  +    sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
         6654  +                           pOp->p4.pExpr, aMem);
  6637   6655     }
  6638   6656     break;
  6639   6657   }
  6640   6658   #endif /* SQLITE_ENABLE_CURSOR_HINTS */
  6641   6659   
  6642   6660   /* Opcode: Noop * * * * *
  6643   6661   **

Changes to src/vdbeInt.h.

    54     54   
    55     55   /* Opaque type used by the explainer */
    56     56   typedef struct Explain Explain;
    57     57   
    58     58   /* Elements of the linked list at Vdbe.pAuxData */
    59     59   typedef struct AuxData AuxData;
    60     60   
           61  +/* Types of VDBE cursors */
           62  +#define CURTYPE_BTREE       0
           63  +#define CURTYPE_SORTER      1
           64  +#define CURTYPE_VTAB        2
           65  +#define CURTYPE_PSEUDO      3
           66  +
    61     67   /*
    62         -** A cursor is a pointer into a single BTree within a database file.
    63         -** The cursor can seek to a BTree entry with a particular key, or
    64         -** loop over all entries of the Btree.  You can also insert new BTree
    65         -** entries or retrieve the key or data from the entry that the cursor
    66         -** is currently pointing to.
           68  +** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
    67     69   **
    68         -** Cursors can also point to virtual tables, sorters, or "pseudo-tables".
    69         -** A pseudo-table is a single-row table implemented by registers.
    70         -** 
    71         -** Every cursor that the virtual machine has open is represented by an
    72         -** instance of the following structure.
           70  +**      * A b-tree cursor
           71  +**          -  In the main database or in an ephemeral database
           72  +**          -  On either an index or a table
           73  +**      * A sorter
           74  +**      * A virtual table
           75  +**      * A one-row "pseudotable" stored in a single register
    73     76   */
    74     77   struct VdbeCursor {
    75         -  BtCursor *pCursor;    /* The cursor structure of the backend */
    76         -  Btree *pBt;           /* Separate file holding temporary table */
    77         -  KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
    78         -  int seekResult;       /* Result of previous sqlite3BtreeMoveto() */
    79         -  int pseudoTableReg;   /* Register holding pseudotable content. */
    80         -  i16 nField;           /* Number of fields in the header */
    81         -  u16 nHdrParsed;       /* Number of header fields parsed so far */
           78  +  u8 eCurType;          /* One of the CURTYPE_* values above */
           79  +  i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
           80  +  u8 nullRow;           /* True if pointing to a row with no data */
           81  +  u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
           82  +  u8 isTable;           /* True for rowid tables.  False for indexes */
    82     83   #ifdef SQLITE_DEBUG
    83     84     u8 seekOp;            /* Most recent seek operation on this cursor */
    84     85   #endif
    85         -  i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
    86         -  u8 nullRow;           /* True if pointing to a row with no data */
    87         -  u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
    88     86     Bool isEphemeral:1;   /* True for an ephemeral table */
    89     87     Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
    90         -  Bool isTable:1;       /* True if a table requiring integer keys */
    91     88     Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
    92     89     Pgno pgnoRoot;        /* Root page of the open btree cursor */
    93         -  sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
           90  +  i16 nField;           /* Number of fields in the header */
           91  +  u16 nHdrParsed;       /* Number of header fields parsed so far */
           92  +  union {
           93  +    BtCursor *pCursor;          /* CURTYPE_BTREE.  Btree cursor */
           94  +    sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB.   Vtab cursor */
           95  +    int pseudoTableReg;         /* CURTYPE_PSEUDO. Reg holding content. */
           96  +    VdbeSorter *pSorter;        /* CURTYPE_SORTER. Sorter object */
           97  +  } uc;
           98  +  Btree *pBt;           /* Separate file holding temporary table */
           99  +  KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
          100  +  int seekResult;       /* Result of previous sqlite3BtreeMoveto() */
    94    101     i64 seqCount;         /* Sequence counter */
    95    102     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
    96         -  VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
    97    103   #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
    98    104     u64 maskUsed;         /* Mask of columns used by this cursor */
    99    105   #endif
   100    106   
   101    107     /* Cached information about the header for the data record that the
   102    108     ** cursor is currently pointing to.  Only valid if cacheStatus matches
   103    109     ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of

Changes to src/vdbeaux.c.

  1926   1926   ** Close a VDBE cursor and release all the resources that cursor 
  1927   1927   ** happens to hold.
  1928   1928   */
  1929   1929   void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
  1930   1930     if( pCx==0 ){
  1931   1931       return;
  1932   1932     }
  1933         -  sqlite3VdbeSorterClose(p->db, pCx);
  1934         -  if( pCx->pBt ){
  1935         -    sqlite3BtreeClose(pCx->pBt);
  1936         -    /* The pCx->pCursor will be close automatically, if it exists, by
  1937         -    ** the call above. */
  1938         -  }else if( pCx->pCursor ){
  1939         -    sqlite3BtreeCloseCursor(pCx->pCursor);
  1940         -  }
         1933  +  assert( pCx->pBt==0 || pCx->eCurType==CURTYPE_BTREE );
         1934  +  switch( pCx->eCurType ){
         1935  +    case CURTYPE_SORTER: {
         1936  +      sqlite3VdbeSorterClose(p->db, pCx);
         1937  +      break;
         1938  +    }
         1939  +    case CURTYPE_BTREE: {
         1940  +      if( pCx->pBt ){
         1941  +        sqlite3BtreeClose(pCx->pBt);
         1942  +        /* The pCx->pCursor will be close automatically, if it exists, by
         1943  +        ** the call above. */
         1944  +      }else{
         1945  +        assert( pCx->uc.pCursor!=0 );
         1946  +        sqlite3BtreeCloseCursor(pCx->uc.pCursor);
         1947  +      }
         1948  +      break;
         1949  +    }
  1941   1950   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1942         -  else if( pCx->pVtabCursor ){
  1943         -    sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
  1944         -    const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
  1945         -    assert( pVtabCursor->pVtab->nRef>0 );
  1946         -    pVtabCursor->pVtab->nRef--;
  1947         -    pModule->xClose(pVtabCursor);
         1951  +    case CURTYPE_VTAB: {
         1952  +      sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
         1953  +      const sqlite3_module *pModule = pVCur->pVtab->pModule;
         1954  +      assert( pVCur->pVtab->nRef>0 );
         1955  +      pVCur->pVtab->nRef--;
         1956  +      pModule->xClose(pVCur);
         1957  +      break;
         1958  +    }
         1959  +#endif
  1948   1960     }
  1949         -#endif
  1950   1961   }
  1951   1962   
  1952   1963   /*
  1953   1964   ** Close all cursors in the current frame.
  1954   1965   */
  1955   1966   static void closeCursorsInFrame(Vdbe *p){
  1956   1967     if( p->apCsr ){
................................................................................
  2929   2940   static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
  2930   2941     int res, rc;
  2931   2942   #ifdef SQLITE_TEST
  2932   2943     extern int sqlite3_search_count;
  2933   2944   #endif
  2934   2945     assert( p->deferredMoveto );
  2935   2946     assert( p->isTable );
  2936         -  rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
         2947  +  assert( p->eCurType==CURTYPE_BTREE );
         2948  +  rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
  2937   2949     if( rc ) return rc;
  2938   2950     if( res!=0 ) return SQLITE_CORRUPT_BKPT;
  2939   2951   #ifdef SQLITE_TEST
  2940   2952     sqlite3_search_count++;
  2941   2953   #endif
  2942   2954     p->deferredMoveto = 0;
  2943   2955     p->cacheStatus = CACHE_STALE;
................................................................................
  2949   2961   ** pointed to was deleted out from under it.  Or maybe the btree was
  2950   2962   ** rebalanced.  Whatever the cause, try to restore "p" to the place it
  2951   2963   ** is supposed to be pointing.  If the row was deleted out from under the
  2952   2964   ** cursor, set the cursor to point to a NULL row.
  2953   2965   */
  2954   2966   static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
  2955   2967     int isDifferentRow, rc;
  2956         -  assert( p->pCursor!=0 );
  2957         -  assert( sqlite3BtreeCursorHasMoved(p->pCursor) );
  2958         -  rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow);
         2968  +  assert( p->eCurType==CURTYPE_BTREE );
         2969  +  assert( p->uc.pCursor!=0 );
         2970  +  assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
         2971  +  rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
  2959   2972     p->cacheStatus = CACHE_STALE;
  2960   2973     if( isDifferentRow ) p->nullRow = 1;
  2961   2974     return rc;
  2962   2975   }
  2963   2976   
  2964   2977   /*
  2965   2978   ** Check to ensure that the cursor is valid.  Restore the cursor
  2966   2979   ** if need be.  Return any I/O error from the restore operation.
  2967   2980   */
  2968   2981   int sqlite3VdbeCursorRestore(VdbeCursor *p){
  2969         -  if( sqlite3BtreeCursorHasMoved(p->pCursor) ){
         2982  +  assert( p->eCurType==CURTYPE_BTREE );
         2983  +  if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
  2970   2984       return handleMovedCursor(p);
  2971   2985     }
  2972   2986     return SQLITE_OK;
  2973   2987   }
  2974   2988   
  2975   2989   /*
  2976   2990   ** Make sure the cursor p is ready to read or write the row to which it
................................................................................
  2982   2996   ** deleted out from under the cursor and if it has, mark the row as
  2983   2997   ** a NULL row.
  2984   2998   **
  2985   2999   ** If the cursor is already pointing to the correct row and that row has
  2986   3000   ** not been deleted out from under the cursor, then this routine is a no-op.
  2987   3001   */
  2988   3002   int sqlite3VdbeCursorMoveto(VdbeCursor *p){
  2989         -  if( p->deferredMoveto ){
  2990         -    return handleDeferredMoveto(p);
  2991         -  }
  2992         -  if( p->pCursor && sqlite3BtreeCursorHasMoved(p->pCursor) ){
  2993         -    return handleMovedCursor(p);
         3003  +  if( p->eCurType==CURTYPE_BTREE ){
         3004  +    if( p->deferredMoveto ){
         3005  +      return handleDeferredMoveto(p);
         3006  +    }
         3007  +    if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
         3008  +      return handleMovedCursor(p);
         3009  +    }
  2994   3010     }
  2995   3011     return SQLITE_OK;
  2996   3012   }
  2997   3013   
  2998   3014   /*
  2999   3015   ** The following functions:
  3000   3016   **
................................................................................
  4299   4315     sqlite3 *db,                     /* Database connection */
  4300   4316     VdbeCursor *pC,                  /* The cursor to compare against */
  4301   4317     UnpackedRecord *pUnpacked,       /* Unpacked version of key */
  4302   4318     int *res                         /* Write the comparison result here */
  4303   4319   ){
  4304   4320     i64 nCellKey = 0;
  4305   4321     int rc;
  4306         -  BtCursor *pCur = pC->pCursor;
         4322  +  BtCursor *pCur;
  4307   4323     Mem m;
  4308   4324   
         4325  +  assert( pC->eCurType==CURTYPE_BTREE );
         4326  +  pCur = pC->uc.pCursor;
  4309   4327     assert( sqlite3BtreeCursorIsValid(pCur) );
  4310   4328     VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
  4311   4329     assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
  4312   4330     /* nCellKey will always be between 0 and 0xffffffff because of the way
  4313   4331     ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
  4314   4332     if( nCellKey<=0 || nCellKey>0x7fffffff ){
  4315   4333       *res = 0;
  4316   4334       return SQLITE_CORRUPT_BKPT;
  4317   4335     }
  4318   4336     sqlite3VdbeMemInit(&m, db, 0);
  4319         -  rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
         4337  +  rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
  4320   4338     if( rc ){
  4321   4339       return rc;
  4322   4340     }
  4323   4341     *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
  4324   4342     sqlite3VdbeMemRelease(&m);
  4325   4343     return SQLITE_OK;
  4326   4344   }

Changes to src/vdbeblob.c.

    72     72         );
    73     73         rc = SQLITE_ERROR;
    74     74         sqlite3_finalize(p->pStmt);
    75     75         p->pStmt = 0;
    76     76       }else{
    77     77         p->iOffset = pC->aType[p->iCol + pC->nField];
    78     78         p->nByte = sqlite3VdbeSerialTypeLen(type);
    79         -      p->pCsr =  pC->pCursor;
           79  +      p->pCsr =  pC->uc.pCursor;
    80     80         sqlite3BtreeIncrblobCursor(p->pCsr);
    81     81       }
    82     82     }
    83     83   
    84     84     if( rc==SQLITE_ROW ){
    85     85       rc = SQLITE_OK;
    86     86     }else if( p->pStmt ){

Changes to src/vdbesort.c.

   957    957   #if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
   958    958     if( nWorker>=SORTER_MAX_MERGE_COUNT ){
   959    959       nWorker = SORTER_MAX_MERGE_COUNT-1;
   960    960     }
   961    961   #endif
   962    962   
   963    963     assert( pCsr->pKeyInfo && pCsr->pBt==0 );
          964  +  assert( pCsr->eCurType==CURTYPE_SORTER );
   964    965     szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*);
   965    966     sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
   966    967   
   967    968     pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
   968         -  pCsr->pSorter = pSorter;
          969  +  pCsr->uc.pSorter = pSorter;
   969    970     if( pSorter==0 ){
   970    971       rc = SQLITE_NOMEM;
   971    972     }else{
   972    973       pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
   973    974       memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
   974    975       pKeyInfo->db = 0;
   975    976       if( nField && nWorker==0 ){
................................................................................
  1245   1246     pSorter->pUnpacked = 0;
  1246   1247   }
  1247   1248   
  1248   1249   /*
  1249   1250   ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
  1250   1251   */
  1251   1252   void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
  1252         -  VdbeSorter *pSorter = pCsr->pSorter;
         1253  +  VdbeSorter *pSorter;
         1254  +  assert( pCsr->eCurType==CURTYPE_SORTER );
         1255  +  pSorter = pCsr->uc.pSorter;
  1253   1256     if( pSorter ){
  1254   1257       sqlite3VdbeSorterReset(db, pSorter);
  1255   1258       sqlite3_free(pSorter->list.aMemory);
  1256   1259       sqlite3DbFree(db, pSorter);
  1257         -    pCsr->pSorter = 0;
         1260  +    pCsr->uc.pSorter = 0;
  1258   1261     }
  1259   1262   }
  1260   1263   
  1261   1264   #if SQLITE_MAX_MMAP_SIZE>0
  1262   1265   /*
  1263   1266   ** The first argument is a file-handle open on a temporary file. The file
  1264   1267   ** is guaranteed to be nByte bytes or smaller in size. This function
................................................................................
  1748   1751   /*
  1749   1752   ** Add a record to the sorter.
  1750   1753   */
  1751   1754   int sqlite3VdbeSorterWrite(
  1752   1755     const VdbeCursor *pCsr,         /* Sorter cursor */
  1753   1756     Mem *pVal                       /* Memory cell containing record */
  1754   1757   ){
  1755         -  VdbeSorter *pSorter = pCsr->pSorter;
         1758  +  VdbeSorter *pSorter;
  1756   1759     int rc = SQLITE_OK;             /* Return Code */
  1757   1760     SorterRecord *pNew;             /* New list element */
  1758         -
  1759   1761     int bFlush;                     /* True to flush contents of memory to PMA */
  1760   1762     int nReq;                       /* Bytes of memory required */
  1761   1763     int nPMA;                       /* Bytes of PMA space required */
  1762   1764     int t;                          /* serial type of first record field */
  1763   1765   
         1766  +  assert( pCsr->eCurType==CURTYPE_SORTER );
         1767  +  pSorter = pCsr->uc.pSorter;
  1764   1768     getVarint32((const u8*)&pVal->z[1], t);
  1765   1769     if( t>0 && t<10 && t!=7 ){
  1766   1770       pSorter->typeMask &= SORTER_TYPE_INTEGER;
  1767   1771     }else if( t>10 && (t & 0x01) ){
  1768   1772       pSorter->typeMask &= SORTER_TYPE_TEXT;
  1769   1773     }else{
  1770   1774       pSorter->typeMask = 0;
................................................................................
  2548   2552   
  2549   2553   /*
  2550   2554   ** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
  2551   2555   ** this function is called to prepare for iterating through the records
  2552   2556   ** in sorted order.
  2553   2557   */
  2554   2558   int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
  2555         -  VdbeSorter *pSorter = pCsr->pSorter;
         2559  +  VdbeSorter *pSorter;
  2556   2560     int rc = SQLITE_OK;             /* Return code */
  2557   2561   
         2562  +  assert( pCsr->eCurType==CURTYPE_SORTER );
         2563  +  pSorter = pCsr->uc.pSorter;
  2558   2564     assert( pSorter );
  2559   2565   
  2560   2566     /* If no data has been written to disk, then do not do so now. Instead,
  2561   2567     ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
  2562   2568     ** from the in-memory list.  */
  2563   2569     if( pSorter->bUsePMA==0 ){
  2564   2570       if( pSorter->list.pList ){
................................................................................
  2594   2600     return rc;
  2595   2601   }
  2596   2602   
  2597   2603   /*
  2598   2604   ** Advance to the next element in the sorter.
  2599   2605   */
  2600   2606   int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
  2601         -  VdbeSorter *pSorter = pCsr->pSorter;
         2607  +  VdbeSorter *pSorter;
  2602   2608     int rc;                         /* Return code */
  2603   2609   
         2610  +  assert( pCsr->eCurType==CURTYPE_SORTER );
         2611  +  pSorter = pCsr->uc.pSorter;
  2604   2612     assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
  2605   2613     if( pSorter->bUsePMA ){
  2606   2614       assert( pSorter->pReader==0 || pSorter->pMerger==0 );
  2607   2615       assert( pSorter->bUseThreads==0 || pSorter->pReader );
  2608   2616       assert( pSorter->bUseThreads==1 || pSorter->pMerger );
  2609   2617   #if SQLITE_MAX_WORKER_THREADS>0
  2610   2618       if( pSorter->bUseThreads ){
................................................................................
  2656   2664     return pKey;
  2657   2665   }
  2658   2666   
  2659   2667   /*
  2660   2668   ** Copy the current sorter key into the memory cell pOut.
  2661   2669   */
  2662   2670   int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
  2663         -  VdbeSorter *pSorter = pCsr->pSorter;
         2671  +  VdbeSorter *pSorter;
  2664   2672     void *pKey; int nKey;           /* Sorter key to copy into pOut */
  2665   2673   
         2674  +  assert( pCsr->eCurType==CURTYPE_SORTER );
         2675  +  pSorter = pCsr->uc.pSorter;
  2666   2676     pKey = vdbeSorterRowkey(pSorter, &nKey);
  2667   2677     if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
  2668   2678       return SQLITE_NOMEM;
  2669   2679     }
  2670   2680     pOut->n = nKey;
  2671   2681     MemSetTypeFlag(pOut, MEM_Blob);
  2672   2682     memcpy(pOut->z, pKey, nKey);
................................................................................
  2692   2702   */
  2693   2703   int sqlite3VdbeSorterCompare(
  2694   2704     const VdbeCursor *pCsr,         /* Sorter cursor */
  2695   2705     Mem *pVal,                      /* Value to compare to current sorter key */
  2696   2706     int nKeyCol,                    /* Compare this many columns */
  2697   2707     int *pRes                       /* OUT: Result of comparison */
  2698   2708   ){
  2699         -  VdbeSorter *pSorter = pCsr->pSorter;
  2700         -  UnpackedRecord *r2 = pSorter->pUnpacked;
  2701         -  KeyInfo *pKeyInfo = pCsr->pKeyInfo;
         2709  +  VdbeSorter *pSorter;
         2710  +  UnpackedRecord *r2;
         2711  +  KeyInfo *pKeyInfo;
  2702   2712     int i;
  2703   2713     void *pKey; int nKey;           /* Sorter key to compare pVal with */
  2704   2714   
         2715  +  assert( pCsr->eCurType==CURTYPE_SORTER );
         2716  +  pSorter = pCsr->uc.pSorter;
         2717  +  r2 = pSorter->pUnpacked;
         2718  +  pKeyInfo = pCsr->pKeyInfo;
  2705   2719     if( r2==0 ){
  2706   2720       char *p;
  2707   2721       r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo,0,0,&p);
  2708   2722       assert( pSorter->pUnpacked==(UnpackedRecord*)p );
  2709   2723       if( r2==0 ) return SQLITE_NOMEM;
  2710   2724       r2->nField = nKeyCol;
  2711   2725     }

Changes to src/whereexpr.c.

  1218   1218     pWC->nTerm = 0;
  1219   1219     pWC->nSlot = ArraySize(pWC->aStatic);
  1220   1220     pWC->a = pWC->aStatic;
  1221   1221   }
  1222   1222   
  1223   1223   /*
  1224   1224   ** Deallocate a WhereClause structure.  The WhereClause structure
  1225         -** itself is not freed.  This routine is the inverse of sqlite3WhereClauseInit().
         1225  +** itself is not freed.  This routine is the inverse of
         1226  +** sqlite3WhereClauseInit().
  1226   1227   */
  1227   1228   void sqlite3WhereClauseClear(WhereClause *pWC){
  1228   1229     int i;
  1229   1230     WhereTerm *a;
  1230   1231     sqlite3 *db = pWC->pWInfo->pParse->db;
  1231   1232     for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
  1232   1233       if( a->wtFlags & TERM_DYNAMIC ){
................................................................................
  1312   1313     ExprList *pArgs;
  1313   1314     Expr *pColRef;
  1314   1315     Expr *pTerm;
  1315   1316     if( pItem->fg.isTabFunc==0 ) return;
  1316   1317     pTab = pItem->pTab;
  1317   1318     assert( pTab!=0 );
  1318   1319     pArgs = pItem->u1.pFuncArg;
  1319         -  assert( pArgs!=0 );
         1320  +  if( pArgs==0 ) return;
  1320   1321     for(j=k=0; j<pArgs->nExpr; j++){
  1321         -    while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){ k++; }
         1322  +    while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
  1322   1323       if( k>=pTab->nCol ){
  1323   1324         sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
  1324   1325                         pTab->zName, j);
  1325   1326         return;
  1326   1327       }
  1327   1328       pColRef = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
  1328   1329       if( pColRef==0 ) return;

Changes to test/fuzzcheck.c.

   834    834     const char *zFailCode = 0;   /* Value of the TEST_FAILURE environment variable */
   835    835     int cellSzCkFlag = 0;        /* --cell-size-check */
   836    836     int sqlFuzz = 0;             /* True for SQL fuzz testing. False for DB fuzz */
   837    837     int iTimeout = 120;          /* Default 120-second timeout */
   838    838     int nMem = 0;                /* Memory limit */
   839    839     char *zExpDb = 0;            /* Write Databases to files in this directory */
   840    840     char *zExpSql = 0;           /* Write SQL to files in this directory */
          841  +  void *pHeap = 0;             /* Heap for use by SQLite */
   841    842   
   842    843     iBegin = timeOfDay();
   843    844   #ifdef __unix__
   844    845     signal(SIGALRM, timeoutHandler);
   845    846   #endif
   846    847     g.zArgv0 = argv[0];
   847    848     zFailCode = getenv("TEST_FAILURE");
................................................................................
  1081   1082       sqlite3_close(db);
  1082   1083       if( sqlite3_memory_used()>0 ){
  1083   1084         fatalError("SQLite has memory in use before the start of testing");
  1084   1085       }
  1085   1086   
  1086   1087       /* Limit available memory, if requested */
  1087   1088       if( nMem>0 ){
  1088         -      void *pHeap;
  1089   1089         sqlite3_shutdown();
  1090   1090         pHeap = malloc(nMem);
  1091   1091         if( pHeap==0 ){
  1092   1092           fatalError("failed to allocate %d bytes of heap memory", nMem);
  1093   1093         }
  1094   1094         sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMem, 128);
  1095   1095       }
................................................................................
  1180   1180       sqlite3_int64 iElapse = timeOfDay() - iBegin;
  1181   1181       printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n"
  1182   1182              "SQLite %s %s\n",
  1183   1183              nTest, (int)(iElapse/1000), (int)(iElapse%1000),
  1184   1184              sqlite3_libversion(), sqlite3_sourceid());
  1185   1185     }
  1186   1186     free(azSrcDb);
         1187  +  free(pHeap);
  1187   1188     return 0;
  1188   1189   }

Added test/hidden.test.

            1  +# 2015 November 18
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# Test the __hidden__ hack.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set testprefix hidden
           18  +
           19  +ifcapable !hiddencolumns {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +do_execsql_test 1.1 {
           25  +  CREATE TABLE t1(__hidden__a, b);
           26  +  INSERT INTO t1 VALUES('1');
           27  +  INSERT INTO t1(__hidden__a, b) VALUES('x', 'y');
           28  +} {}
           29  +
           30  +do_execsql_test 1.2 {
           31  +  SELECT * FROM t1;
           32  +} {1 y}
           33  +
           34  +do_execsql_test 1.3 {
           35  +  SELECT __hidden__a, * FROM t1;
           36  +} {{} 1 x y}
           37  +
           38  +foreach {tn view} {
           39  +  1 { CREATE VIEW v1(a, b, __hidden__c) AS SELECT a, b, c FROM x1 }
           40  +  2 { CREATE VIEW v1 AS SELECT a, b, c AS __hidden__c FROM x1 }
           41  +} {
           42  +  do_execsql_test 2.$tn.1 {
           43  +    DROP TABLE IF EXISTS x1;
           44  +    CREATE TABLE x1(a, b, c);
           45  +    INSERT INTO x1 VALUES(1, 2, 3);
           46  +  }
           47  +
           48  +  catchsql { DROP VIEW v1 }
           49  +  execsql $view
           50  +
           51  +  do_execsql_test 2.$tn.2 {
           52  +    SELECT a, b, __hidden__c FROM v1;
           53  +  } {1 2 3}
           54  +  
           55  +  do_execsql_test 2.$tn.3 {
           56  +    SELECT * FROM v1;
           57  +  } {1 2}
           58  +  
           59  +  do_execsql_test 2.$tn.4 {
           60  +    CREATE TRIGGER tr1 INSTEAD OF INSERT ON v1 BEGIN
           61  +      INSERT INTO x1 VALUES(new.a, new.b, new.__hidden__c);
           62  +    END;
           63  +  
           64  +    INSERT INTO v1 VALUES(4, 5);
           65  +    SELECT * FROM x1;
           66  +  } {1 2 3 4 5 {}}
           67  +  
           68  +  do_execsql_test 2.$tn.5 {
           69  +    INSERT INTO v1(a, b, __hidden__c) VALUES(7, 8, 9);
           70  +    SELECT * FROM x1;
           71  +  } {1 2 3 4 5 {} 7 8 9}
           72  +}
           73  +
           74  +#-------------------------------------------------------------------------
           75  +# Test INSERT INTO ... SELECT ... statements that write to tables with
           76  +# hidden columns.
           77  +#
           78  +do_execsql_test 3.1 {
           79  +  CREATE TABLE t4(a, __hidden__b, c);
           80  +  INSERT INTO t4 SELECT 1, 2;
           81  +  SELECT a, __hidden__b, c FROM t4;
           82  +} {1 {} 2}
           83  +
           84  +do_execsql_test 3.2.1 {
           85  +  CREATE TABLE t5(__hidden__a, b, c);
           86  +  CREATE TABLE t6(__hidden__a, b, c);
           87  +  INSERT INTO t6(__hidden__a, b, c) VALUES(1, 2, 3);
           88  +  INSERT INTO t6(__hidden__a, b, c) VALUES(4, 5, 6);
           89  +  INSERT INTO t6(__hidden__a, b, c) VALUES(7, 8, 9);
           90  +}
           91  +
           92  +do_execsql_test 3.2.2 {
           93  +  INSERT INTO t5 SELECT * FROM t6;
           94  +  SELECT * FROM t5;
           95  +} {2 3   5 6   8 9}
           96  +
           97  +do_execsql_test 3.2.3 {
           98  +  SELECT __hidden__a FROM t5;
           99  +} {{} {} {}}
          100  +
          101  +
          102  +do_execsql_test 3.3.1 {
          103  +  CREATE TABLE t5a(a, b, __hidden__c);
          104  +  CREATE TABLE t6a(a, b, __hidden__c);
          105  +  INSERT INTO t6a(a, b, __hidden__c) VALUES(1, 2, 3);
          106  +  INSERT INTO t6a(a, b, __hidden__c) VALUES(4, 5, 6);
          107  +  INSERT INTO t6a(a, b, __hidden__c) VALUES(7, 8, 9);
          108  +}
          109  +
          110  +do_execsql_test 3.3.2 {
          111  +  INSERT INTO t5a SELECT * FROM t6a;
          112  +  SELECT * FROM t5a;
          113  +} {1 2   4 5   7 8}
          114  +
          115  +do_execsql_test 3.3.3 {
          116  +  SELECT __hidden__c FROM t5a;
          117  +} {{} {} {}}
          118  +
          119  +do_execsql_test 3.4.1 {
          120  +  CREATE TABLE t5b(a, __hidden__b, c);
          121  +  CREATE TABLE t6b(a, b, __hidden__c);
          122  +  INSERT INTO t6b(a, b, __hidden__c) VALUES(1, 2, 3);
          123  +  INSERT INTO t6b(a, b, __hidden__c) VALUES(4, 5, 6);
          124  +  INSERT INTO t6b(a, b, __hidden__c) VALUES(7, 8, 9);
          125  +}
          126  +
          127  +do_execsql_test 3.4.2 {
          128  +  INSERT INTO t5b SELECT * FROM t6b;
          129  +  SELECT * FROM t5b;
          130  +} {1 2   4 5   7 8}
          131  +
          132  +do_execsql_test 3.4.3 {
          133  +  SELECT __hidden__b FROM t5b;
          134  +} {{} {} {}}
          135  +
          136  +#-------------------------------------------------------------------------
          137  +# Test VACUUM
          138  +#
          139  +reset_db
          140  +do_execsql_test 4.1 {
          141  +  CREATE TABLE t1(a, __hidden__b, c UNIQUE);
          142  +  INSERT INTO t1(a, __hidden__b, c) VALUES(1, 2, 3);
          143  +  INSERT INTO t1(a, __hidden__b, c) VALUES(4, 5, 6);
          144  +  INSERT INTO t1(a, __hidden__b, c) VALUES(7, 8, 9);
          145  +  DELETE FROM t1 WHERE __hidden__b = 5;
          146  +  SELECT rowid, a, __hidden__b, c FROM t1;
          147  +} {1 1 2 3   3 7 8 9}
          148  +do_execsql_test 4.2 {
          149  +  VACUUM;
          150  +  SELECT rowid, a, __hidden__b, c FROM t1;
          151  +} {1 1 2 3   3 7 8 9}
          152  +
          153  +finish_test

Changes to test/misc1.test.

   696    696   
   697    697   # At one point, running this would read one byte passed the end of a 
   698    698   # buffer, upsetting valgrind.
   699    699   #
   700    700   do_test misc1-24.0 {
   701    701     list [catch { sqlite3_prepare_v2 db ! -1 dummy } msg] $msg
   702    702   } {1 {(1) unrecognized token: "!}}
          703  +
          704  +# The following query (provided by Kostya Serebryany) used to take 25
          705  +# minutes to prepare.  This has been speeded up to about 250 milliseconds.
          706  +#
          707  +do_catchsql_test misc1-25.0 {
          708  +SELECT-1 UNION  SELECT 5 UNION SELECT 0 UNION SElECT*from(SELECT-5) UNION SELECT*from(SELECT-0) UNION  SELECT:SELECT-0 UNION SELECT-1 UNION SELECT 1 UNION SELECT 1 ORDER BY S  in(WITH K AS(WITH K AS(select'CREINDERcharREADEVIRTUL5TABLECONFLICT !1 USIN'' MFtOR(b38q,eWITH K AS(selectCREATe TABLE t0(a,b,c,d,e, PRIMARY KEY(a,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,b,c,d,c,a,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d'CEIl,k'',ab, g, a,b,o11b, i'nEX/charREDE IVT LR!VABLt5SG',N  ,N in rement,l_vacuum,M&U,'te3(''5l' a,bB,b,l*e)SELECT:SELECT, *,*,*from(( SELECT
          709  +$group,:conc ap0,1)fro,(select"",:PBAG,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,c,d,c,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d, foreign_keysc,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,c,d,c,a,b,d,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,bb,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,c,d,c,a,b,d,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,MAato_aecSELEC,+?b," "O,"i","a",""b  ,5 ))KEY)SELECT*FROM((k()reaC,k,K) eA,k '' )t ,K  M);
          710  +} {1 {'k' is not a function}}
          711  +
   703    712   
   704    713   finish_test

Changes to test/pragma2.test.

   131    131   # EVIDENCE-OF: R-23955-02765 Cache_spill is enabled by default
   132    132   #
   133    133   db close
   134    134   delete_file test.db test.db-journal
   135    135   delete_file test2.db test2.db-journal
   136    136   sqlite3 db test.db
   137    137   do_execsql_test pragma2-4.1 {
          138  +  PRAGMA main.cache_size=2000;
          139  +  PRAGMA temp.cache_size=2000;
   138    140     PRAGMA cache_spill;
   139    141     PRAGMA main.cache_spill;
   140    142     PRAGMA temp.cache_spill;
   141         -} {1 1 1}
          143  +} {2000 2000 2000}
   142    144   do_execsql_test pragma2-4.2 {
   143    145     PRAGMA cache_spill=OFF;
   144    146     PRAGMA cache_spill;
   145    147     PRAGMA main.cache_spill;
   146    148     PRAGMA temp.cache_spill;
   147    149   } {0 0 0}
   148    150   do_execsql_test pragma2-4.3 {
................................................................................
   174    176   do_test pragma2-4.4 {
   175    177     db eval {
   176    178       BEGIN;
   177    179       UPDATE t1 SET c=c+1;
   178    180       PRAGMA lock_status;
   179    181     }
   180    182   } {main exclusive temp unknown}  ;# EXCLUSIVE lock due to cache spill
   181         -do_test pragma2-4.5 {
          183  +do_test pragma2-4.5.1 {
   182    184     db eval {
   183         -    COMMIT;
          185  +    ROLLBACK;
   184    186       PRAGMA cache_spill=OFF;
          187  +    PRAGMA Cache_Spill;
          188  +    BEGIN;
          189  +    UPDATE t1 SET c=c+1;
          190  +    PRAGMA lock_status;
          191  +  }
          192  +} {0 main reserved temp unknown}   ;# No cache spill, so no exclusive lock
          193  +do_test pragma2-4.5.2 {
          194  +  db eval {
          195  +    ROLLBACK;
          196  +    PRAGMA cache_spill=100000;
          197  +    PRAGMA cache_spill;
   185    198       BEGIN;
   186         -    UPDATE t1 SET c=c-1;
          199  +    UPDATE t1 SET c=c+1;
   187    200       PRAGMA lock_status;
   188    201     }
   189         -} {main reserved temp unknown}   ;# No cache spill, so no exclusive lock
          202  +} {100000 main reserved temp unknown}   ;# Big spill threshold -> no excl lock
          203  +ifcapable !memorymanage {
          204  +  do_test pragma2-4.5.3 {
          205  +    db eval {
          206  +      ROLLBACK;
          207  +      PRAGMA cache_spill=25;
          208  +      PRAGMA main.cache_spill;
          209  +      BEGIN;
          210  +      UPDATE t1 SET c=c+1;
          211  +      PRAGMA lock_status;
          212  +    }
          213  +  } {50 main exclusive temp unknown}   ;# Small cache spill -> exclusive lock
          214  +  do_test pragma2-4.5.4 {
          215  +    db eval {
          216  +      ROLLBACK;
          217  +      PRAGMA cache_spill(-25);
          218  +      PRAGMA main.cache_spill;
          219  +      BEGIN;
          220  +      UPDATE t1 SET c=c+1;
          221  +      PRAGMA lock_status;
          222  +    }
          223  +  } {50 main exclusive temp unknown}   ;# Small cache spill -> exclusive lock
          224  +}
          225  +
   190    226   
   191    227   # Verify that newly attached databases inherit the cache_spill=OFF
   192    228   # setting.
   193    229   #
   194    230   do_execsql_test pragma2-4.6 {
   195         -  COMMIT;
          231  +  ROLLBACK;
          232  +  PRAGMA cache_spill=OFF;
   196    233     ATTACH 'test2.db' AS aux1;
   197    234     PRAGMA aux1.cache_size=50;
   198    235     BEGIN;
   199    236     UPDATE t2 SET c=c+1;
   200    237     PRAGMA lock_status;
   201    238   } {main unlocked temp unknown aux1 reserved}
   202    239   do_execsql_test pragma2-4.7 {
................................................................................
   205    242   sqlite3_release_memory
   206    243   do_execsql_test pragma2-4.8 {
   207    244     PRAGMA cache_spill=ON; -- Applies to all databases
   208    245     BEGIN;
   209    246     UPDATE t2 SET c=c-1;
   210    247     PRAGMA lock_status;
   211    248   } {main unlocked temp unknown aux1 exclusive}
          249  +db close
          250  +forcedelete test.db
          251  +sqlite3 db test.db
          252  +
          253  +breakpoint
          254  +do_execsql_test pragma2-5.1 {
          255  +  PRAGMA page_size=16384;
          256  +  CREATE TABLE t1(x);
          257  +  PRAGMA cache_size=2;
          258  +  PRAGMA cache_spill=YES;
          259  +  PRAGMA cache_spill;
          260  +} {2}
          261  +do_execsql_test pragma2-5.2 {
          262  +  PRAGMA cache_spill=NO;
          263  +  PRAGMA cache_spill;
          264  +} {0}
          265  +do_execsql_test pragma2-5.3 {
          266  +  PRAGMA cache_spill(-51);
          267  +  PRAGMA cache_spill;
          268  +} {3}
   212    269      
   213    270   test_restore_config_pagecache
   214    271   finish_test

Changes to test/releasetest.tcl.

   115    115       -DSQLITE_TCL_DEFAULT_FULLMUTEX=1
   116    116       -DSQLITE_ENABLE_FTS3=1
   117    117       -DSQLITE_ENABLE_RTREE=1
   118    118       -DSQLITE_ENABLE_MEMSYS5=1
   119    119       -DSQLITE_ENABLE_MEMSYS3=1
   120    120       -DSQLITE_ENABLE_COLUMN_METADATA=1
   121    121       -DSQLITE_ENABLE_STAT4
          122  +    -DSQLITE_ENABLE_HIDDEN_COLUMNS
   122    123       -DSQLITE_MAX_ATTACHED=125
   123    124     }
   124    125     "Fast-One" {
   125    126       -O6
   126    127       -DSQLITE_ENABLE_FTS4=1
   127    128       -DSQLITE_ENABLE_RTREE=1
   128    129       -DSQLITE_ENABLE_STAT4
................................................................................
   141    142       -DSQLITE_ENABLE_ATOMIC_WRITE=1
   142    143       -DSQLITE_ENABLE_IOTRACE=1
   143    144       -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1
   144    145       -DSQLITE_MAX_PAGE_SIZE=4096
   145    146       -DSQLITE_OMIT_LOAD_EXTENSION=1
   146    147       -DSQLITE_OMIT_PROGRESS_CALLBACK=1
   147    148       -DSQLITE_OMIT_VIRTUALTABLE=1
          149  +    -DSQLITE_ENABLE_HIDDEN_COLUMNS
   148    150       -DSQLITE_TEMP_STORE=3
   149    151       --enable-json1
   150    152     }
   151    153     "Device-Two" {
   152    154       -DSQLITE_4_BYTE_ALIGNED_MALLOC=1
   153    155       -DSQLITE_DEFAULT_AUTOVACUUM=1
   154    156       -DSQLITE_DEFAULT_CACHE_SIZE=1000
................................................................................
   209    211       -DSQLITE_OMIT_LOOKASIDE=1
   210    212       -DHAVE_USLEEP=1
   211    213     }
   212    214     "Valgrind" {
   213    215       -DSQLITE_ENABLE_STAT4
   214    216       -DSQLITE_ENABLE_FTS4
   215    217       -DSQLITE_ENABLE_RTREE
          218  +    -DSQLITE_ENABLE_HIDDEN_COLUMNS
   216    219       --enable-json1
   217    220     }
   218    221   
   219    222     # The next group of configurations are used only by the
   220    223     # Failure-Detection platform.  They are all the same, but we need
   221    224     # different names for them all so that they results appear in separate
   222    225     # subdirectories.
................................................................................
   302    305       if {0==[info exists ::Configs($v)]} {
   303    306         puts stderr "No such configuration: \"$v\""
   304    307         exit -1
   305    308       }
   306    309     }
   307    310   }
   308    311   
   309         -# Output log
   310         -#
   311         -set LOG [open releasetest-out.txt w]
   312         -proc PUTS {args} {
   313         -  if {[llength $args]==2} {
   314         -    puts [lindex $args 0] [lindex $args 1]
   315         -    puts $::LOG [lindex $args 1]
   316         -  } else {
   317         -    puts [lindex $args 0]
   318         -    puts $::LOG [lindex $args 0]
   319         -  }
   320         -  flush $::LOG
   321         -}
   322         -puts $LOG "$argv0 $argv"
   323         -set tm0 [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S} -gmt 1]
   324         -puts $LOG "start-time: $tm0 UTC"
          312  +# Output log.   Disabled for slave interpreters.
          313  +#
          314  +if {[lindex $argv end]!="--slave"} {
          315  +  set LOG [open releasetest-out.txt w]
          316  +  proc PUTS {txt} {
          317  +    puts $txt
          318  +    puts $::LOG $txt
          319  +    flush $::LOG
          320  +  }
          321  +  proc PUTSNNL {txt} {
          322  +    puts -nonewline $txt
          323  +    puts -nonewline $::LOG $txt
          324  +    flush $::LOG
          325  +  }
          326  +  proc PUTSERR {txt} {
          327  +    puts stderr $txt
          328  +    puts $::LOG $txt
          329  +    flush $::LOG
          330  +  }
          331  +  puts $LOG "$argv0 $argv"
          332  +  set tm0 [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S} -gmt 1]
          333  +  puts $LOG "start-time: $tm0 UTC"
          334  +} else {
          335  +  proc PUTS {txt} {
          336  +    puts $txt
          337  +  }
          338  +  proc PUTSNNL {txt} {
          339  +    puts -nonewline $txt
          340  +  }
          341  +  proc PUTSERR {txt} {
          342  +    puts stderr $txt
          343  +  }
          344  +}
   325    345   
   326    346   # Open the file $logfile and look for a report on the number of errors
   327    347   # and the number of test cases run.  Add these values to the global
   328    348   # $::NERRCASE and $::NTESTCASE variables.
   329    349   #
   330    350   # If any errors occur, then write into $errmsgVar the text of an appropriate
   331    351   # one-line error message to show on the output.
................................................................................
   418    438     foreach {::TRACE ::MSVC ::DRYRUN} $V {}
   419    439   
   420    440     # Read the test-suite configuration from stdin.
   421    441     set T [gets stdin]
   422    442     foreach {title dir configOpts testtarget makeOpts cflags opts} $T {}
   423    443   
   424    444     # Create and switch to the test directory.
          445  +  set ::env(SQLITE_TMPDIR) [file normalize $dir]
   425    446     trace_cmd file mkdir $dir
   426    447     trace_cmd cd $dir
   427    448     catch {file delete core}
   428    449     catch {file delete test.log}
   429    450   
   430    451     # Run the "./configure && make" commands.
   431    452     set rc 0
................................................................................
   805    826         -enable-* -
   806    827         -disable-* -
   807    828         *=* {
   808    829           lappend ::EXTRACONFIG [lindex $argv $i]
   809    830         }
   810    831   
   811    832         default {
   812         -        PUTS stderr ""
   813         -        PUTS stderr [string trim $::USAGE_MESSAGE]
          833  +        PUTSERR stderr ""
          834  +        PUTSERR stderr [string trim $::USAGE_MESSAGE]
   814    835           exit -1
   815    836         }
   816    837       }
   817    838     }
   818    839   
   819    840     if {0==[info exists ::Platforms($platform)]} {
   820    841       PUTS "Unknown platform: $platform"
   821         -    PUTS -nonewline "Set the -platform option to "
          842  +    PUTSNNL "Set the -platform option to "
   822    843       set print [list]
   823    844       foreach p [array names ::Platforms] {
   824    845         lappend print "\"$p\""
   825    846       }
   826    847       lset print end "or [lindex $print end]"
   827    848       PUTS "[join $print {, }]."
   828    849       exit
................................................................................
   840    861         }
   841    862       } else {
   842    863         set ::CONFIGLIST $::Platforms($platform)
   843    864       }
   844    865     }
   845    866     PUTS "Running the following test configurations for $platform:"
   846    867     PUTS "    [string trim $::CONFIGLIST]"
   847         -  PUTS -nonewline "Flags:"
   848         -  if {$::PROGRESS_MSGS} {PUTS -nonewline " --progress"}
   849         -  if {$::DRYRUN} {PUTS -nonewline " --dryrun"}
   850         -  if {$::BUILDONLY} {PUTS -nonewline " --buildonly"}
   851         -  if {$::MSVC} {PUTS -nonewline " --msvc"}
          868  +  PUTSNNL "Flags:"
          869  +  if {$::PROGRESS_MSGS} {PUTSNNL " --progress"}
          870  +  if {$::DRYRUN} {PUTSNNL " --dryrun"}
          871  +  if {$::BUILDONLY} {PUTSNNL " --buildonly"}
          872  +  if {$::MSVC} {PUTSNNL " --msvc"}
   852    873     switch -- $::QUICK {
   853         -     1 {PUTS -nonewline " --quick"}
   854         -     2 {PUTS -nonewline " --veryquick"}
          874  +     1 {PUTSNNL " --quick"}
          875  +     2 {PUTSNNL " --veryquick"}
   855    876     }
   856         -  if {$::JOBS>1} {PUTS -nonewline " --jobs $::JOBS"}
          877  +  if {$::JOBS>1} {PUTSNNL " --jobs $::JOBS"}
   857    878     PUTS ""
   858    879   }
   859    880   
   860    881   # Main routine.
   861    882   #
   862    883   proc main {argv} {
   863    884   

Changes to test/sqllimits1.test.

   870    870   do_test sqllimits1-16.2 {
   871    871     set ::format "[string repeat A 60][string repeat "%J" $::N]"
   872    872     catchsql {
   873    873       SELECT strftime($::format, 1);
   874    874     }
   875    875   } {1 {string or blob too big}}
   876    876   
          877  +do_catchsql_test sqllimits1.17.0 {
          878  +  SELECT *,*,*,*,*,*,*,* FROM (
          879  +  SELECT *,*,*,*,*,*,*,* FROM (
          880  +  SELECT *,*,*,*,*,*,*,* FROM (
          881  +  SELECT *,*,*,*,*,*,*,* FROM (
          882  +  SELECT *,*,*,*,*,*,*,* FROM (
          883  +    SELECT 1,2,3,4,5,6,7,8,9,10
          884  +  )
          885  +  ))))
          886  +} "1 {too many columns in result set}"
          887  +
   877    888   
   878    889   foreach {key value} [array get saved] {
   879    890     catch {set $key $value}
   880    891   }
   881    892   finish_test

Added test/sqllog.test.

            1  +# 2015 November 13
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.  The
           12  +# focus of this file is testing the test_sqllog.c module.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set testprefix sqllog
           18  +
           19  +ifcapable !sqllog {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +proc readfile {f} {
           25  +  set fd [open $f]
           26  +  set txt [read $fd]
           27  +  close $fd
           28  +  set txt
           29  +}
           30  +
           31  +proc delete_all_sqllog_files {} {
           32  +  forcedelete {*}[glob -nocomplain sqllog_*.sql]
           33  +  forcedelete {*}[glob -nocomplain sqllog_*.db]
           34  +  forcedelete {*}[glob -nocomplain sqllog_*.idx]
           35  +}
           36  +
           37  +proc touch {f} {
           38  +  set fd [open $f w+]
           39  +  close $fd
           40  +}
           41  +
           42  +db close
           43  +sqlite3_shutdown
           44  +set ::env(SQLITE_SQLLOG_DIR) [pwd]
           45  +
           46  +delete_all_sqllog_files
           47  +
           48  +sqlite3 db test.db
           49  +set a a
           50  +set b b
           51  +do_execsql_test 1.0 {
           52  +  CREATE TABLE t1(x, y);
           53  +  INSERT INTO t1 VALUES(1, 2);
           54  +  INSERT INTO t1 VALUES($a, $b);
           55  +  SELECT * FROM t1;
           56  +} {1 2 a b}
           57  +db close
           58  +
           59  +do_test 1.1 {
           60  +  readfile [lindex [glob sqllog_*.sql] 0]
           61  +} [string trimleft {
           62  +/-- Main database is '.*/sqllog_.*_0.db'
           63  +CREATE TABLE t1\(x, y\);; -- clock=0
           64  +INSERT INTO t1 VALUES\(1, 2\);; -- clock=1
           65  +INSERT INTO t1 VALUES\('a', 'b'\);; -- clock=2
           66  +SELECT . FROM t1;; -- clock=3
           67  +/}]
           68  +
           69  +do_test 1.2 {
           70  +  file size [lindex [glob sqllog_*_0.db] 0]
           71  +} 1024
           72  +
           73  +#-------------------------------------------------------------------------
           74  +catch { db close }
           75  +sqlite3_shutdown
           76  +delete_all_sqllog_files
           77  +forcedelete test.db-sqllog
           78  +
           79  +set ::env(SQLITE_SQLLOG_CONDITIONAL) 1
           80  +sqlite3 db test.db
           81  +do_execsql_test 2.1 {
           82  +  INSERT INTO t1 VALUES(4, 5);
           83  +  SELECT * FROM t1;
           84  +} {1 2 a b 4 5}
           85  +
           86  +do_test 2.2 {
           87  +  glob -nocomplain sqllog_*
           88  +} {}
           89  +
           90  +db close
           91  +touch test.db-sqllog
           92  +sqlite3 db test.db
           93  +do_execsql_test 2.3 {
           94  +  INSERT INTO t1 VALUES(6, 7);
           95  +  SELECT * FROM t1;
           96  +} {1 2 a b 4 5 6 7}
           97  +db close
           98  +
           99  +do_test 2.4 {
          100  +  readfile [lindex [glob sqllog_*.sql] 0]
          101  +} [string trimleft {
          102  +/-- Main database is '.*/sqllog_.*_0.db'
          103  +INSERT INTO t1 VALUES\(6, 7\);; -- clock=0
          104  +SELECT . FROM t1;; -- clock=1
          105  +/}]
          106  +
          107  +catch { db close }
          108  +sqlite3_shutdown
          109  +unset ::env(SQLITE_SQLLOG_DIR)
          110  +unset ::env(SQLITE_SQLLOG_CONDITIONAL)
          111  +sqlite3_config_sqllog
          112  +sqlite3_initialize
          113  +breakpoint
          114  +finish_test
          115  +
          116  +

Changes to test/tabfunc01.test.

    55     55     SELECT rowid, * FROM generate_series(0,32,5) ORDER BY +value DESC;
    56     56   } {7 30 6 25 5 20 4 15 3 10 2 5 1 0}
    57     57   
    58     58   do_execsql_test tabfunc01-1.20 {
    59     59     CREATE VIEW v1(a,b) AS VALUES(1,2),(3,4);
    60     60     SELECT * FROM v1;
    61     61   } {1 2 3 4}
    62         -do_catchsql_test tabfunc01-1.21 {
           62  +do_catchsql_test tabfunc01-1.21.1 {
    63     63     SELECT * FROM v1(55);
           64  +} {1 {'v1' is not a function}}
           65  +do_catchsql_test tabfunc01-1.21.2 {
           66  +  SELECT * FROM v1();
    64     67   } {1 {'v1' is not a function}}
    65     68   do_execsql_test tabfunc01-1.22 {
    66     69     CREATE VIEW v2(x) AS SELECT value FROM generate_series(1,5);
    67     70     SELECT * FROM v2;
    68     71   } {1 2 3 4 5}
    69         -do_catchsql_test tabfunc01-1.23 {
           72  +do_catchsql_test tabfunc01-1.23.1 {
    70     73     SELECT * FROM v2(55);
    71     74   } {1 {'v2' is not a function}}
           75  +do_catchsql_test tabfunc01-1.23.2 {
           76  +  SELECT * FROM v2();
           77  +} {1 {'v2' is not a function}}
           78  +do_execsql_test tabfunc01-1.24 {
           79  +  CREATE TABLE t0(x);
           80  +  INSERT INTO t0(x) VALUES(123),(456),(789);
           81  +  SELECT * FROM t0 ORDER BY x;
           82  +} {123 456 789}
           83  +do_catchsql_test tabfunc01-1.25 {
           84  +  SELECT * FROM t0(55) ORDER BY x;
           85  +} {1 {'t0' is not a function}}
           86  +do_catchsql_test tabfunc01-1.26 {
           87  +  WITH w0 AS (SELECT * FROM t0)
           88  +  INSERT INTO t0(x) SELECT * FROM w0()
           89  +} {1 {'w0' is not a function}}
    72     90   
    73     91   do_execsql_test tabfunc01-2.1 {
    74     92     CREATE TABLE t1(x);
    75     93     INSERT INTO t1(x) VALUES(2),(3);
    76     94     SELECT *, '|' FROM t1, generate_series(1,x) ORDER BY 1, 2
    77     95   } {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |}
    78     96   do_execsql_test tabfunc01-2.2 {

Changes to test/tester.tcl.

   742    742         } else {
   743    743           set ok [expr {[string compare $result $expected]==0}]
   744    744         }
   745    745         if {!$ok} {
   746    746           # if {![info exists ::testprefix] || $::testprefix eq ""} {
   747    747           #   error "no test prefix"
   748    748           # }
   749         -        output2_if_no_verbose -nonewline $name...
   750         -        output2 "\nExpected: \[$expected\]\n     Got: \[$result\]"
          749  +        output1 ""
          750  +        output2 "! $name expected: \[$expected\]\n! $name got:      \[$result\]"
   751    751           fail_test $name
   752    752         } else {
   753    753           output1 " Ok"
   754    754         }
   755    755       }
   756    756     } else {
   757    757       output1 " Omitted"
................................................................................
  1027   1027     if {$nKnown>0} {
  1028   1028       output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
  1029   1029            out of $nTest tests"
  1030   1030     } else {
  1031   1031       output2 "$nErr errors out of $nTest tests"
  1032   1032     }
  1033   1033     if {$nErr>$nKnown} {
  1034         -    output2 -nonewline "Failures on these tests:"
         1034  +    output2 -nonewline "!Failures on these tests:"
  1035   1035       foreach x [set_test_counter fail_list] {
  1036   1036         if {![info exists known_error($x)]} {output2 -nonewline " $x"}
  1037   1037       }
  1038   1038       output2 ""
  1039   1039     }
  1040   1040     foreach warning [set_test_counter warn_list] {
  1041   1041       output2 "Warning: $warning"
................................................................................
  1043   1043     run_thread_tests 1
  1044   1044     if {[llength $omitList]>0} {
  1045   1045       output2 "Omitted test cases:"
  1046   1046       set prec {}
  1047   1047       foreach {rec} [lsort $omitList] {
  1048   1048         if {$rec==$prec} continue
  1049   1049         set prec $rec
  1050         -      output2 [format {  %-12s %s} [lindex $rec 0] [lindex $rec 1]]
         1050  +      output2 [format {.  %-12s %s} [lindex $rec 0] [lindex $rec 1]]
  1051   1051       }
  1052   1052     }
  1053   1053     if {$nErr>0 && ![working_64bit_int]} {
  1054   1054       output2 "******************************************************************"
  1055   1055       output2 "N.B.:  The version of TCL that you used to build this test harness"
  1056   1056       output2 "is defective in that it does not support 64-bit integers.  Some or"
  1057   1057       output2 "all of the test failures above might be a result from this defect"

Changes to test/trigger7.test.

    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   ifcapable {!trigger} {
    20     20     finish_test
    21     21     return
    22     22   }
    23         -
    24     23   
    25     24   # Error messages resulting from qualified trigger names.
    26     25   #
    27     26   do_test trigger7-1.1 {
    28     27     execsql {
    29     28       CREATE TABLE t1(x, y);
    30     29     }

Changes to test/wal3.test.

   271    271   } [list {0 1 lock exclusive} {1 7 lock exclusive}      \
   272    272           {1 7 unlock exclusive} {0 1 unlock exclusive}  \
   273    273   ]
   274    274   proc lock_callback {method filename handle lock} {
   275    275     if {$lock == "1 7 lock exclusive"} { return SQLITE_BUSY }
   276    276     return SQLITE_OK
   277    277   }
   278         -puts "  Warning: This next test case causes SQLite to call xSleep(1) 100 times."
   279         -puts "  Normally this equates to a 100ms delay, but if SQLite is built on unix"
   280         -puts "  without HAVE_USLEEP defined, it may be 100 seconds."
          278  +puts "# Warning: This next test case causes SQLite to call xSleep(1) 100 times."
          279  +puts "# Normally this equates to a 100ms delay, but if SQLite is built on unix"
          280  +puts "# without HAVE_USLEEP defined, it may be 100 seconds."
   281    281   do_test wal3-4.3 {
   282    282     db close
   283    283     set ::locks [list]
   284    284     sqlite3 db test.db -vfs T
   285    285     catchsql { SELECT * FROM x }
   286    286   } {1 {locking protocol}}
   287    287   
   288         -puts "  Warning: Same again!"
          288  +puts "# Warning: Same again!"
   289    289   proc lock_callback {method filename handle lock} {
   290    290     if {$lock == "0 1 lock exclusive"} { return SQLITE_BUSY }
   291    291     return SQLITE_OK
   292    292   }
   293    293   do_test wal3-4.4 {
   294    294     db close
   295    295     set ::locks [list]

Changes to tool/addopcodes.tcl.

    33     33     FUNCTION
    34     34     COLUMN
    35     35     AGG_FUNCTION
    36     36     AGG_COLUMN
    37     37     UMINUS
    38     38     UPLUS
    39     39     REGISTER
           40  +  ASTERISK
    40     41     SPACE
    41     42     ILLEGAL
    42     43   }
    43     44   if {[lrange $extras end-1 end]!="SPACE ILLEGAL"} {
    44     45     error "SPACE and ILLEGAL must be the last two token codes and they\
    45     46            must be in that order"
    46     47   }

Changes to tool/mkkeywordhash.c.

   324    324   }
   325    325   
   326    326   /*
   327    327   ** This routine does the work.  The generated code is printed on standard
   328    328   ** output.
   329    329   */
   330    330   int main(int argc, char **argv){
   331         -  int i, j, k, h, m;
          331  +  int i, j, k, h;
   332    332     int bestSize, bestCount;
   333    333     int count;
   334    334     int nChar;
   335    335     int totalLen = 0;
   336    336     int aHash[1000];  /* 1000 is much bigger than nKeyword */
   337    337     char zText[2000];
   338    338   

Changes to tool/mkpragmatab.tcl.

    43     43   
    44     44     NAME: checkpoint_fullfsync
    45     45     TYPE: FLAG
    46     46     ARG:  SQLITE_CkptFullFSync
    47     47     IF:   !defined(SQLITE_OMIT_FLAG_PRAGMAS)
    48     48   
    49     49     NAME: cache_spill
    50         -  TYPE: FLAG
    51         -  ARG:  SQLITE_CacheSpill
    52     50     IF:   !defined(SQLITE_OMIT_FLAG_PRAGMAS)
    53     51   
    54     52     NAME: reverse_unordered_selects
    55     53     TYPE: FLAG
    56     54     ARG:  SQLITE_ReverseOrder
    57     55     IF:   !defined(SQLITE_OMIT_FLAG_PRAGMAS)
    58     56   
................................................................................
   166    164     FLAG: NeedSchema
   167    165     IF:   !defined(SQLITE_OMIT_PAGER_PRAGMAS)
   168    166   
   169    167     NAME: journal_size_limit
   170    168     IF:   !defined(SQLITE_OMIT_PAGER_PRAGMAS)
   171    169   
   172    170     NAME: cache_size
          171  +  FLAG: NeedSchema
   173    172     IF:   !defined(SQLITE_OMIT_PAGER_PRAGMAS)
   174    173   
   175    174     NAME: mmap_size
   176    175     IF:   !defined(SQLITE_OMIT_PAGER_PRAGMAS)
   177    176   
   178    177     NAME: auto_vacuum
   179    178     FLAG: NeedSchema

Changes to tool/sqldiff.c.

  1737   1737   "  --changeset FILE      Write a CHANGESET into FILE\n"
  1738   1738   "  -L|--lib LIBRARY      Load an SQLite extension library\n"
  1739   1739   "  --primarykey          Use schema-defined PRIMARY KEYs\n"
  1740   1740   "  --rbu                 Output SQL to create/populate RBU table(s)\n"
  1741   1741   "  --schema              Show only differences in the schema\n"
  1742   1742   "  --summary             Show only a summary of the differences\n"
  1743   1743   "  --table TAB           Show only differences in table TAB\n"
         1744  +"  --transaction         Show SQL output inside a transaction\n"
  1744   1745     );
  1745   1746   }
  1746   1747   
  1747   1748   int main(int argc, char **argv){
  1748   1749     const char *zDb1 = 0;
  1749   1750     const char *zDb2 = 0;
  1750   1751     int i;
................................................................................
  1753   1754     char *zSql;
  1754   1755     sqlite3_stmt *pStmt;
  1755   1756     char *zTab = 0;
  1756   1757     FILE *out = stdout;
  1757   1758     void (*xDiff)(const char*,FILE*) = diff_one_table;
  1758   1759     int nExt = 0;
  1759   1760     char **azExt = 0;
         1761  +  int useTransaction = 0;
         1762  +  int neverUseTransaction = 0;
  1760   1763   
  1761   1764     g.zArgv0 = argv[0];
  1762   1765     sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
  1763   1766     for(i=1; i<argc; i++){
  1764   1767       const char *z = argv[i];
  1765   1768       if( z[0]=='-' ){
  1766   1769         z++;
  1767   1770         if( z[0]=='-' ) z++;
  1768   1771         if( strcmp(z,"changeset")==0 ){
  1769   1772           if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
  1770   1773           out = fopen(argv[++i], "wb");
  1771   1774           if( out==0 ) cmdlineError("cannot open: %s", argv[i]);
  1772   1775           xDiff = changeset_one_table;
         1776  +        neverUseTransaction = 1;
  1773   1777         }else
  1774   1778         if( strcmp(z,"debug")==0 ){
  1775   1779           if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
  1776   1780           g.fDebug = strtol(argv[++i], 0, 0);
  1777   1781         }else
  1778   1782         if( strcmp(z,"help")==0 ){
  1779   1783           showHelp();
................................................................................
  1798   1802         }else
  1799   1803         if( strcmp(z,"summary")==0 ){
  1800   1804           xDiff = summarize_one_table;
  1801   1805         }else
  1802   1806         if( strcmp(z,"table")==0 ){
  1803   1807           if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
  1804   1808           zTab = argv[++i];
         1809  +      }else
         1810  +      if( strcmp(z,"transaction")==0 ){
         1811  +        useTransaction = 1;
  1805   1812         }else
  1806   1813         {
  1807   1814           cmdlineError("unknown option: %s", argv[i]);
  1808   1815         }
  1809   1816       }else if( zDb1==0 ){
  1810   1817         zDb1 = argv[i];
  1811   1818       }else if( zDb2==0 ){
................................................................................
  1841   1848       cmdlineError("cannot attach database \"%s\"", zDb2);
  1842   1849     }
  1843   1850     rc = sqlite3_exec(g.db, "SELECT * FROM aux.sqlite_master", 0, 0, &zErrMsg);
  1844   1851     if( rc || zErrMsg ){
  1845   1852       cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb2);
  1846   1853     }
  1847   1854   
         1855  +  if( neverUseTransaction ) useTransaction = 0;
         1856  +  if( useTransaction ) printf("BEGIN TRANSACTION;\n");
  1848   1857     if( zTab ){
  1849   1858       xDiff(zTab, out);
  1850   1859     }else{
  1851   1860       /* Handle tables one by one */
  1852   1861       pStmt = db_prepare(
  1853   1862         "SELECT name FROM main.sqlite_master\n"
  1854   1863         " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
................................................................................
  1858   1867         " ORDER BY name"
  1859   1868       );
  1860   1869       while( SQLITE_ROW==sqlite3_step(pStmt) ){
  1861   1870         xDiff((const char*)sqlite3_column_text(pStmt,0), out);
  1862   1871       }
  1863   1872       sqlite3_finalize(pStmt);
  1864   1873     }
         1874  +  if( useTransaction ) printf("COMMIT;\n");
  1865   1875   
  1866   1876     /* TBD: Handle trigger differences */
  1867   1877     /* TBD: Handle view differences */
  1868   1878     sqlite3_close(g.db);
  1869   1879     return 0;
  1870   1880   }