SQLite Android Bindings
Check-in [9fa38e007e]
Not logged in

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

Overview
Comment:Update this project to version 3.21.0
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9fa38e007e87d3dc4bd2c027e0058b208e650a95
User & Date: dan 2017-10-24 19:33:52
Context
2017-11-23
14:27
Update gradle version to 3.0.0. check-in: 89b788860c user: dan tags: trunk
2017-11-13
18:48
Upgrade to gradle 3.0. Remove ApplicationTest class (not required?). check-in: df18f25465 user: dan tags: experimental
2017-10-24
19:33
Update this project to version 3.21.0 check-in: 9fa38e007e user: dan tags: trunk
2017-09-11
15:39
Add test case to detect problems with zipvfs deployments. check-in: 61fae530b5 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to build.gradle.

     1      1   // Top-level build file where you can add configuration options common to all sub-projects/modules.
     2      2   
     3      3   buildscript {
     4      4       repositories {
     5      5           jcenter()
     6      6       }
     7      7       dependencies {
     8         -        classpath 'com.android.tools.build:gradle:2.3.1'
            8  +        classpath 'com.android.tools.build:gradle:2.3.3'
     9      9   
    10     10           // NOTE: Do not place your application dependencies here; they belong
    11     11           // in the individual module build.gradle files
    12     12       }
    13     13   }
    14     14   
    15     15   allprojects {

Changes to sqlite3/src/main/jni/sqlite/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.20.1.  By combining all the individual C code files into this
            3  +** version 3.21.0.  By combining all the individual C code files into this
     4      4   ** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
   204    204     "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
   205    205   #endif
   206    206   #if SQLITE_ENABLE_API_ARMOR
   207    207     "ENABLE_API_ARMOR",
   208    208   #endif
   209    209   #if SQLITE_ENABLE_ATOMIC_WRITE
   210    210     "ENABLE_ATOMIC_WRITE",
          211  +#endif
          212  +#if SQLITE_ENABLE_BATCH_ATOMIC_WRITE
          213  +  "ENABLE_BATCH_ATOMIC_WRITE",
   211    214   #endif
   212    215   #if SQLITE_ENABLE_CEROD
   213    216     "ENABLE_CEROD",
   214    217   #endif
   215    218   #if SQLITE_ENABLE_COLUMN_METADATA
   216    219     "ENABLE_COLUMN_METADATA",
   217    220   #endif
................................................................................
   823    826   ** Make sure the Tcl calling convention macro is defined.  This macro is
   824    827   ** only used by test code and Tcl integration code.
   825    828   */
   826    829   #ifndef SQLITE_TCLAPI
   827    830   #  define SQLITE_TCLAPI
   828    831   #endif
   829    832   
   830         -/*
   831         -** Make sure that rand_s() is available on Windows systems with MSVC 2005
   832         -** or higher.
   833         -*/
   834         -#if defined(_MSC_VER) && _MSC_VER>=1400
   835         -#  define _CRT_RAND_S
   836         -#endif
   837         -
   838    833   /*
   839    834   ** Include the header file used to customize the compiler options for MSVC.
   840    835   ** This should be done first so that it can successfully prevent spurious
   841    836   ** compiler warnings due to subsequent content in this file and other files
   842    837   ** that are included by this file.
   843    838   */
   844    839   /************** Include msvc.h in the middle of sqliteInt.h ******************/
................................................................................
  1140   1135   ** Since [version 3.6.18] ([dateof:3.6.18]), 
  1141   1136   ** SQLite source code has been stored in the
  1142   1137   ** <a href="http://www.fossil-scm.org/">Fossil configuration management
  1143   1138   ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
  1144   1139   ** a string which identifies a particular check-in of SQLite
  1145   1140   ** within its configuration management system.  ^The SQLITE_SOURCE_ID
  1146   1141   ** string contains the date and time of the check-in (UTC) and a SHA1
  1147         -** or SHA3-256 hash of the entire source tree.
         1142  +** or SHA3-256 hash of the entire source tree.  If the source code has
         1143  +** been edited in any way since it was last checked in, then the last
         1144  +** four hexadecimal digits of the hash may be modified.
  1148   1145   **
  1149   1146   ** See also: [sqlite3_libversion()],
  1150   1147   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  1151   1148   ** [sqlite_version()] and [sqlite_source_id()].
  1152   1149   */
  1153         -#define SQLITE_VERSION        "3.20.1"
  1154         -#define SQLITE_VERSION_NUMBER 3020001
  1155         -#define SQLITE_SOURCE_ID      "2017-08-24 16:21:36 8d3a7ea6c5690d6b7c3767558f4f01b511c55463e3f9e64506801fe9b74dce34"
         1150  +#define SQLITE_VERSION        "3.21.0"
         1151  +#define SQLITE_VERSION_NUMBER 3021000
         1152  +#define SQLITE_SOURCE_ID      "2017-10-24 18:55:49 1a584e499906b5c87ec7d43d4abce641fdf017c42125b083109bc77c4de48827"
  1156   1153   
  1157   1154   /*
  1158   1155   ** CAPI3REF: Run-Time Library Version Numbers
  1159   1156   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1160   1157   **
  1161   1158   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1162   1159   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1164   1161   ** programmers might include assert() statements in their application to
  1165   1162   ** verify that values returned by these interfaces match the macros in
  1166   1163   ** the header, and thus ensure that the application is
  1167   1164   ** compiled with matching library and header files.
  1168   1165   **
  1169   1166   ** <blockquote><pre>
  1170   1167   ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
  1171         -** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
         1168  +** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
  1172   1169   ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
  1173   1170   ** </pre></blockquote>)^
  1174   1171   **
  1175   1172   ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
  1176   1173   ** macro.  ^The sqlite3_libversion() function returns a pointer to the
  1177   1174   ** to the sqlite3_version[] string constant.  The sqlite3_libversion()
  1178   1175   ** function is provided for use in DLLs since DLL users usually do not have
  1179   1176   ** direct access to string constants within the DLL.  ^The
  1180   1177   ** sqlite3_libversion_number() function returns an integer equal to
  1181         -** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns 
         1178  +** [SQLITE_VERSION_NUMBER].  ^(The sqlite3_sourceid() function returns 
  1182   1179   ** a pointer to a string constant whose value is the same as the 
  1183         -** [SQLITE_SOURCE_ID] C preprocessor macro.
         1180  +** [SQLITE_SOURCE_ID] C preprocessor macro.  Except if SQLite is built
         1181  +** using an edited copy of [the amalgamation], then the last four characters
         1182  +** of the hash might be different from [SQLITE_SOURCE_ID].)^
  1184   1183   **
  1185   1184   ** See also: [sqlite_version()] and [sqlite_source_id()].
  1186   1185   */
  1187   1186   SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
  1188   1187   SQLITE_API const char *sqlite3_libversion(void);
  1189   1188   SQLITE_API const char *sqlite3_sourceid(void);
  1190   1189   SQLITE_API int sqlite3_libversion_number(void);
................................................................................
  1457   1456   #define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
  1458   1457   #define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
  1459   1458   #define SQLITE_CORRUPT     11   /* The database disk image is malformed */
  1460   1459   #define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
  1461   1460   #define SQLITE_FULL        13   /* Insertion failed because database is full */
  1462   1461   #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
  1463   1462   #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
  1464         -#define SQLITE_EMPTY       16   /* Not used */
         1463  +#define SQLITE_EMPTY       16   /* Internal use only */
  1465   1464   #define SQLITE_SCHEMA      17   /* The database schema changed */
  1466   1465   #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
  1467   1466   #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
  1468   1467   #define SQLITE_MISMATCH    20   /* Data type mismatch */
  1469   1468   #define SQLITE_MISUSE      21   /* Library used incorrectly */
  1470   1469   #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
  1471   1470   #define SQLITE_AUTH        23   /* Authorization denied */
................................................................................
  1519   1518   #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
  1520   1519   #define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
  1521   1520   #define SQLITE_IOERR_MMAP              (SQLITE_IOERR | (24<<8))
  1522   1521   #define SQLITE_IOERR_GETTEMPPATH       (SQLITE_IOERR | (25<<8))
  1523   1522   #define SQLITE_IOERR_CONVPATH          (SQLITE_IOERR | (26<<8))
  1524   1523   #define SQLITE_IOERR_VNODE             (SQLITE_IOERR | (27<<8))
  1525   1524   #define SQLITE_IOERR_AUTH              (SQLITE_IOERR | (28<<8))
         1525  +#define SQLITE_IOERR_BEGIN_ATOMIC      (SQLITE_IOERR | (29<<8))
         1526  +#define SQLITE_IOERR_COMMIT_ATOMIC     (SQLITE_IOERR | (30<<8))
         1527  +#define SQLITE_IOERR_ROLLBACK_ATOMIC   (SQLITE_IOERR | (31<<8))
  1526   1528   #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
  1527   1529   #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
  1528   1530   #define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
  1529   1531   #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
  1530   1532   #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
  1531   1533   #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
  1532   1534   #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
................................................................................
  1605   1607   ** file that were written at the application level might have changed
  1606   1608   ** and that adjacent bytes, even bytes within the same sector are
  1607   1609   ** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
  1608   1610   ** flag indicates that a file cannot be deleted when open.  The
  1609   1611   ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
  1610   1612   ** read-only media and cannot be changed even by processes with
  1611   1613   ** elevated privileges.
         1614  +**
         1615  +** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
         1616  +** filesystem supports doing multiple write operations atomically when those
         1617  +** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
         1618  +** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
  1612   1619   */
  1613   1620   #define SQLITE_IOCAP_ATOMIC                 0x00000001
  1614   1621   #define SQLITE_IOCAP_ATOMIC512              0x00000002
  1615   1622   #define SQLITE_IOCAP_ATOMIC1K               0x00000004
  1616   1623   #define SQLITE_IOCAP_ATOMIC2K               0x00000008
  1617   1624   #define SQLITE_IOCAP_ATOMIC4K               0x00000010
  1618   1625   #define SQLITE_IOCAP_ATOMIC8K               0x00000020
................................................................................
  1620   1627   #define SQLITE_IOCAP_ATOMIC32K              0x00000080
  1621   1628   #define SQLITE_IOCAP_ATOMIC64K              0x00000100
  1622   1629   #define SQLITE_IOCAP_SAFE_APPEND            0x00000200
  1623   1630   #define SQLITE_IOCAP_SEQUENTIAL             0x00000400
  1624   1631   #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
  1625   1632   #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
  1626   1633   #define SQLITE_IOCAP_IMMUTABLE              0x00002000
         1634  +#define SQLITE_IOCAP_BATCH_ATOMIC           0x00004000
  1627   1635   
  1628   1636   /*
  1629   1637   ** CAPI3REF: File Locking Levels
  1630   1638   **
  1631   1639   ** SQLite uses one of these integer values as the second
  1632   1640   ** argument to calls it makes to the xLock() and xUnlock() methods
  1633   1641   ** of an [sqlite3_io_methods] object.
................................................................................
  1754   1762   ** <li> [SQLITE_IOCAP_ATOMIC32K]
  1755   1763   ** <li> [SQLITE_IOCAP_ATOMIC64K]
  1756   1764   ** <li> [SQLITE_IOCAP_SAFE_APPEND]
  1757   1765   ** <li> [SQLITE_IOCAP_SEQUENTIAL]
  1758   1766   ** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
  1759   1767   ** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
  1760   1768   ** <li> [SQLITE_IOCAP_IMMUTABLE]
         1769  +** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
  1761   1770   ** </ul>
  1762   1771   **
  1763   1772   ** The SQLITE_IOCAP_ATOMIC property means that all writes of
  1764   1773   ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
  1765   1774   ** mean that writes of blocks that are nnn bytes in size and
  1766   1775   ** are aligned to an address which is an integer multiple of
  1767   1776   ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
................................................................................
  2037   2046   ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
  2038   2047   ** VFS should return SQLITE_NOTFOUND for this opcode.
  2039   2048   **
  2040   2049   ** <li>[[SQLITE_FCNTL_RBU]]
  2041   2050   ** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
  2042   2051   ** the RBU extension only.  All other VFS should return SQLITE_NOTFOUND for
  2043   2052   ** this opcode.  
         2053  +**
         2054  +** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
         2055  +** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
         2056  +** the file descriptor is placed in "batch write mode", which
         2057  +** means all subsequent write operations will be deferred and done
         2058  +** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].  Systems
         2059  +** that do not support batch atomic writes will return SQLITE_NOTFOUND.
         2060  +** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
         2061  +** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
         2062  +** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
         2063  +** no VFS interface calls on the same [sqlite3_file] file descriptor
         2064  +** except for calls to the xWrite method and the xFileControl method
         2065  +** with [SQLITE_FCNTL_SIZE_HINT].
         2066  +**
         2067  +** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
         2068  +** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
         2069  +** operations since the previous successful call to 
         2070  +** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
         2071  +** This file control returns [SQLITE_OK] if and only if the writes were
         2072  +** all performed successfully and have been committed to persistent storage.
         2073  +** ^Regardless of whether or not it is successful, this file control takes
         2074  +** the file descriptor out of batch write mode so that all subsequent
         2075  +** write operations are independent.
         2076  +** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
         2077  +** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
         2078  +**
         2079  +** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
         2080  +** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
         2081  +** operations since the previous successful call to 
         2082  +** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
         2083  +** ^This file control takes the file descriptor out of batch write mode
         2084  +** so that all subsequent write operations are independent.
         2085  +** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
         2086  +** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
  2044   2087   ** </ul>
  2045   2088   */
  2046   2089   #define SQLITE_FCNTL_LOCKSTATE               1
  2047   2090   #define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
  2048   2091   #define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
  2049   2092   #define SQLITE_FCNTL_LAST_ERRNO              4
  2050   2093   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
  2068   2111   #define SQLITE_FCNTL_WAL_BLOCK              24
  2069   2112   #define SQLITE_FCNTL_ZIPVFS                 25
  2070   2113   #define SQLITE_FCNTL_RBU                    26
  2071   2114   #define SQLITE_FCNTL_VFS_POINTER            27
  2072   2115   #define SQLITE_FCNTL_JOURNAL_POINTER        28
  2073   2116   #define SQLITE_FCNTL_WIN32_GET_HANDLE       29
  2074   2117   #define SQLITE_FCNTL_PDB                    30
         2118  +#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
         2119  +#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
         2120  +#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
  2075   2121   
  2076   2122   /* deprecated names */
  2077   2123   #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
  2078   2124   #define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
  2079   2125   #define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
  2080   2126   
  2081   2127   
................................................................................
  2637   2683   ** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
  2638   2684   ** is a pointer to an instance of the [sqlite3_mem_methods] structure.
  2639   2685   ** The [sqlite3_mem_methods]
  2640   2686   ** structure is filled with the currently defined memory allocation routines.)^
  2641   2687   ** This option can be used to overload the default memory allocation
  2642   2688   ** routines with a wrapper that simulations memory allocation failure or
  2643   2689   ** tracks memory usage, for example. </dd>
         2690  +**
         2691  +** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
         2692  +** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
         2693  +** type int, interpreted as a boolean, which if true provides a hint to
         2694  +** SQLite that it should avoid large memory allocations if possible.
         2695  +** SQLite will run faster if it is free to make large memory allocations,
         2696  +** but some application might prefer to run slower in exchange for
         2697  +** guarantees about memory fragmentation that are possible if large
         2698  +** allocations are avoided.  This hint is normally off.
         2699  +** </dd>
  2644   2700   **
  2645   2701   ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
  2646   2702   ** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
  2647   2703   ** interpreted as a boolean, which enables or disables the collection of
  2648   2704   ** memory allocation statistics. ^(When memory allocation statistics are
  2649   2705   ** disabled, the following SQLite interfaces become non-operational:
  2650   2706   **   <ul>
................................................................................
  2655   2711   **   </ul>)^
  2656   2712   ** ^Memory allocation statistics are enabled by default unless SQLite is
  2657   2713   ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
  2658   2714   ** allocation statistics are disabled by default.
  2659   2715   ** </dd>
  2660   2716   **
  2661   2717   ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
  2662         -** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer
  2663         -** that SQLite can use for scratch memory.  ^(There are three arguments
  2664         -** to SQLITE_CONFIG_SCRATCH:  A pointer an 8-byte
  2665         -** aligned memory buffer from which the scratch allocations will be
  2666         -** drawn, the size of each scratch allocation (sz),
  2667         -** and the maximum number of scratch allocations (N).)^
  2668         -** The first argument must be a pointer to an 8-byte aligned buffer
  2669         -** of at least sz*N bytes of memory.
  2670         -** ^SQLite will not use more than one scratch buffers per thread.
  2671         -** ^SQLite will never request a scratch buffer that is more than 6
  2672         -** times the database page size.
  2673         -** ^If SQLite needs needs additional
  2674         -** scratch memory beyond what is provided by this configuration option, then 
  2675         -** [sqlite3_malloc()] will be used to obtain the memory needed.<p>
  2676         -** ^When the application provides any amount of scratch memory using
  2677         -** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large
  2678         -** [sqlite3_malloc|heap allocations].
  2679         -** This can help [Robson proof|prevent memory allocation failures] due to heap
  2680         -** fragmentation in low-memory embedded systems.
         2718  +** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
  2681   2719   ** </dd>
  2682   2720   **
  2683   2721   ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
  2684   2722   ** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
  2685   2723   ** that SQLite can use for the database page cache with the default page
  2686   2724   ** cache implementation.  
  2687   2725   ** This configuration option is a no-op if an application-define page
................................................................................
  2709   2747   ** page cache memory is needed beyond what is provided by the initial
  2710   2748   ** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
  2711   2749   ** additional cache line. </dd>
  2712   2750   **
  2713   2751   ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
  2714   2752   ** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer 
  2715   2753   ** that SQLite will use for all of its dynamic memory allocation needs
  2716         -** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and
  2717         -** [SQLITE_CONFIG_PAGECACHE].
         2754  +** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
  2718   2755   ** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
  2719   2756   ** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
  2720   2757   ** [SQLITE_ERROR] if invoked otherwise.
  2721   2758   ** ^There are three arguments to SQLITE_CONFIG_HEAP:
  2722   2759   ** An 8-byte aligned pointer to the memory,
  2723   2760   ** the number of bytes in the memory buffer, and the minimum allocation size.
  2724   2761   ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
................................................................................
  2903   2940   ** </dl>
  2904   2941   */
  2905   2942   #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
  2906   2943   #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
  2907   2944   #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
  2908   2945   #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
  2909   2946   #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
  2910         -#define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
         2947  +#define SQLITE_CONFIG_SCRATCH       6  /* No longer used */
  2911   2948   #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
  2912   2949   #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
  2913   2950   #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
  2914   2951   #define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
  2915   2952   #define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
  2916   2953   /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ 
  2917   2954   #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
................................................................................
  2924   2961   #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
  2925   2962   #define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
  2926   2963   #define SQLITE_CONFIG_MMAP_SIZE    22  /* sqlite3_int64, sqlite3_int64 */
  2927   2964   #define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
  2928   2965   #define SQLITE_CONFIG_PCACHE_HDRSZ        24  /* int *psz */
  2929   2966   #define SQLITE_CONFIG_PMASZ               25  /* unsigned int szPma */
  2930   2967   #define SQLITE_CONFIG_STMTJRNL_SPILL      26  /* int nByte */
         2968  +#define SQLITE_CONFIG_SMALL_MALLOC        27  /* boolean */
  2931   2969   
  2932   2970   /*
  2933   2971   ** CAPI3REF: Database Connection Configuration Options
  2934   2972   **
  2935   2973   ** These constants are the available integer configuration options that
  2936   2974   ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  2937   2975   **
................................................................................
  4124   4162   ** automatically deleted as soon as the database connection is closed.
  4125   4163   **
  4126   4164   ** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
  4127   4165   **
  4128   4166   ** ^If [URI filename] interpretation is enabled, and the filename argument
  4129   4167   ** begins with "file:", then the filename is interpreted as a URI. ^URI
  4130   4168   ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
  4131         -** set in the fourth argument to sqlite3_open_v2(), or if it has
         4169  +** set in the third argument to sqlite3_open_v2(), or if it has
  4132   4170   ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
  4133   4171   ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
  4134         -** As of SQLite version 3.7.7, URI filename interpretation is turned off
         4172  +** URI filename interpretation is turned off
  4135   4173   ** by default, but future releases of SQLite might enable URI filename
  4136   4174   ** interpretation by default.  See "[URI filenames]" for additional
  4137   4175   ** information.
  4138   4176   **
  4139   4177   ** URI filenames are parsed according to RFC 3986. ^If the URI contains an
  4140   4178   ** authority, then it must be either an empty string or the string 
  4141   4179   ** "localhost". ^If the authority is not an empty string or "localhost", an 
................................................................................
  4801   4839   ** still make the distinction between protected and unprotected
  4802   4840   ** sqlite3_value objects even when not strictly required.
  4803   4841   **
  4804   4842   ** ^The sqlite3_value objects that are passed as parameters into the
  4805   4843   ** implementation of [application-defined SQL functions] are protected.
  4806   4844   ** ^The sqlite3_value object returned by
  4807   4845   ** [sqlite3_column_value()] is unprotected.
  4808         -** Unprotected sqlite3_value objects may only be used with
  4809         -** [sqlite3_result_value()] and [sqlite3_bind_value()].
         4846  +** Unprotected sqlite3_value objects may only be used as arguments
         4847  +** to [sqlite3_result_value()], [sqlite3_bind_value()], and
         4848  +** [sqlite3_value_dup()].
  4810   4849   ** The [sqlite3_value_blob | sqlite3_value_type()] family of
  4811   4850   ** interfaces require protected sqlite3_value objects.
  4812   4851   */
  4813   4852   typedef struct sqlite3_value sqlite3_value;
  4814   4853   
  4815   4854   /*
  4816   4855   ** CAPI3REF: SQL Function Context Object
................................................................................
  5224   5263   ** more threads at the same moment in time.
  5225   5264   **
  5226   5265   ** For all versions of SQLite up to and including 3.6.23.1, a call to
  5227   5266   ** [sqlite3_reset()] was required after sqlite3_step() returned anything
  5228   5267   ** other than [SQLITE_ROW] before any subsequent invocation of
  5229   5268   ** sqlite3_step().  Failure to reset the prepared statement using 
  5230   5269   ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
  5231         -** sqlite3_step().  But after [version 3.6.23.1] ([dateof:3.6.23.1]),
         5270  +** sqlite3_step().  But after [version 3.6.23.1] ([dateof:3.6.23.1],
  5232   5271   ** sqlite3_step() began
  5233   5272   ** calling [sqlite3_reset()] automatically in this circumstance rather
  5234   5273   ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
  5235   5274   ** break because any application that ever receives an SQLITE_MISUSE error
  5236   5275   ** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
  5237   5276   ** can be used to restore the legacy behavior.
  5238   5277   **
................................................................................
  7228   7267   ** CAPI3REF: Virtual Table Constraint Operator Codes
  7229   7268   **
  7230   7269   ** These macros defined the allowed values for the
  7231   7270   ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
  7232   7271   ** an operator that is part of a constraint term in the wHERE clause of
  7233   7272   ** a query that uses a [virtual table].
  7234   7273   */
  7235         -#define SQLITE_INDEX_CONSTRAINT_EQ      2
  7236         -#define SQLITE_INDEX_CONSTRAINT_GT      4
  7237         -#define SQLITE_INDEX_CONSTRAINT_LE      8
  7238         -#define SQLITE_INDEX_CONSTRAINT_LT     16
  7239         -#define SQLITE_INDEX_CONSTRAINT_GE     32
  7240         -#define SQLITE_INDEX_CONSTRAINT_MATCH  64
  7241         -#define SQLITE_INDEX_CONSTRAINT_LIKE   65
  7242         -#define SQLITE_INDEX_CONSTRAINT_GLOB   66
  7243         -#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
         7274  +#define SQLITE_INDEX_CONSTRAINT_EQ         2
         7275  +#define SQLITE_INDEX_CONSTRAINT_GT         4
         7276  +#define SQLITE_INDEX_CONSTRAINT_LE         8
         7277  +#define SQLITE_INDEX_CONSTRAINT_LT        16
         7278  +#define SQLITE_INDEX_CONSTRAINT_GE        32
         7279  +#define SQLITE_INDEX_CONSTRAINT_MATCH     64
         7280  +#define SQLITE_INDEX_CONSTRAINT_LIKE      65
         7281  +#define SQLITE_INDEX_CONSTRAINT_GLOB      66
         7282  +#define SQLITE_INDEX_CONSTRAINT_REGEXP    67
         7283  +#define SQLITE_INDEX_CONSTRAINT_NE        68
         7284  +#define SQLITE_INDEX_CONSTRAINT_ISNOT     69
         7285  +#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
         7286  +#define SQLITE_INDEX_CONSTRAINT_ISNULL    71
         7287  +#define SQLITE_INDEX_CONSTRAINT_IS        72
  7244   7288   
  7245   7289   /*
  7246   7290   ** CAPI3REF: Register A Virtual Table Implementation
  7247   7291   ** METHOD: sqlite3
  7248   7292   **
  7249   7293   ** ^These routines are used to register a new [virtual table module] name.
  7250   7294   ** ^Module names must be registered before
................................................................................
  7988   8032   #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
  7989   8033   #define SQLITE_TESTCTRL_PENDING_BYTE            11
  7990   8034   #define SQLITE_TESTCTRL_ASSERT                  12
  7991   8035   #define SQLITE_TESTCTRL_ALWAYS                  13
  7992   8036   #define SQLITE_TESTCTRL_RESERVE                 14
  7993   8037   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  7994   8038   #define SQLITE_TESTCTRL_ISKEYWORD               16
  7995         -#define SQLITE_TESTCTRL_SCRATCHMALLOC           17
         8039  +#define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
  7996   8040   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  7997   8041   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
  7998   8042   #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  7999   8043   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  8000   8044   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  8001   8045   #define SQLITE_TESTCTRL_BYTEORDER               22
  8002   8046   #define SQLITE_TESTCTRL_ISINIT                  23
................................................................................
  8047   8091   ** that can be returned by [sqlite3_status()].
  8048   8092   **
  8049   8093   ** <dl>
  8050   8094   ** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
  8051   8095   ** <dd>This parameter is the current amount of memory checked out
  8052   8096   ** using [sqlite3_malloc()], either directly or indirectly.  The
  8053   8097   ** figure includes calls made to [sqlite3_malloc()] by the application
  8054         -** and internal memory usage by the SQLite library.  Scratch memory
  8055         -** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
         8098  +** and internal memory usage by the SQLite library.  Auxiliary page-cache
  8056   8099   ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
  8057   8100   ** this parameter.  The amount returned is the sum of the allocation
  8058   8101   ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
  8059   8102   **
  8060   8103   ** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
  8061   8104   ** <dd>This parameter records the largest memory allocation request
  8062   8105   ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
................................................................................
  8086   8129   **
  8087   8130   ** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
  8088   8131   ** <dd>This parameter records the largest memory allocation request
  8089   8132   ** handed to [pagecache memory allocator].  Only the value returned in the
  8090   8133   ** *pHighwater parameter to [sqlite3_status()] is of interest.  
  8091   8134   ** The value written into the *pCurrent parameter is undefined.</dd>)^
  8092   8135   **
  8093         -** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
  8094         -** <dd>This parameter returns the number of allocations used out of the
  8095         -** [scratch memory allocator] configured using
  8096         -** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
  8097         -** in bytes.  Since a single thread may only have one scratch allocation
  8098         -** outstanding at time, this parameter also reports the number of threads
  8099         -** using scratch memory at the same time.</dd>)^
         8136  +** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
         8137  +** <dd>No longer used.</dd>
  8100   8138   **
  8101   8139   ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
  8102         -** <dd>This parameter returns the number of bytes of scratch memory
  8103         -** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
  8104         -** buffer and where forced to overflow to [sqlite3_malloc()].  The values
  8105         -** returned include overflows because the requested allocation was too
  8106         -** larger (that is, because the requested allocation was larger than the
  8107         -** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
  8108         -** slots were available.
  8109         -** </dd>)^
         8140  +** <dd>No longer used.</dd>
  8110   8141   **
  8111         -** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
  8112         -** <dd>This parameter records the largest memory allocation request
  8113         -** handed to [scratch memory allocator].  Only the value returned in the
  8114         -** *pHighwater parameter to [sqlite3_status()] is of interest.  
  8115         -** The value written into the *pCurrent parameter is undefined.</dd>)^
         8142  +** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
         8143  +** <dd>No longer used.</dd>
  8116   8144   **
  8117   8145   ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
  8118   8146   ** <dd>The *pHighwater parameter records the deepest parser stack. 
  8119   8147   ** The *pCurrent value is undefined.  The *pHighwater value is only
  8120   8148   ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
  8121   8149   ** </dl>
  8122   8150   **
  8123   8151   ** New status parameters may be added from time to time.
  8124   8152   */
  8125   8153   #define SQLITE_STATUS_MEMORY_USED          0
  8126   8154   #define SQLITE_STATUS_PAGECACHE_USED       1
  8127   8155   #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
  8128         -#define SQLITE_STATUS_SCRATCH_USED         3
  8129         -#define SQLITE_STATUS_SCRATCH_OVERFLOW     4
         8156  +#define SQLITE_STATUS_SCRATCH_USED         3  /* NOT USED */
         8157  +#define SQLITE_STATUS_SCRATCH_OVERFLOW     4  /* NOT USED */
  8130   8158   #define SQLITE_STATUS_MALLOC_SIZE          5
  8131   8159   #define SQLITE_STATUS_PARSER_STACK         6
  8132   8160   #define SQLITE_STATUS_PAGECACHE_SIZE       7
  8133         -#define SQLITE_STATUS_SCRATCH_SIZE         8
         8161  +#define SQLITE_STATUS_SCRATCH_SIZE         8  /* NOT USED */
  8134   8162   #define SQLITE_STATUS_MALLOC_COUNT         9
  8135   8163   
  8136   8164   /*
  8137   8165   ** CAPI3REF: Database Connection Status
  8138   8166   ** METHOD: sqlite3
  8139   8167   **
  8140   8168   ** ^This interface is used to retrieve runtime status information 
................................................................................
 10223  10251   ** Changes within a patchset are ordered in the same way as for changesets
 10224  10252   ** generated by the sqlite3session_changeset() function (i.e. all changes for
 10225  10253   ** a single table are grouped together, tables appear in the order in which
 10226  10254   ** they were attached to the session object).
 10227  10255   */
 10228  10256   SQLITE_API int sqlite3session_patchset(
 10229  10257     sqlite3_session *pSession,      /* Session object */
 10230         -  int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
 10231         -  void **ppPatchset               /* OUT: Buffer containing changeset */
        10258  +  int *pnPatchset,                /* OUT: Size of buffer at *ppPatchset */
        10259  +  void **ppPatchset               /* OUT: Buffer containing patchset */
 10232  10260   );
 10233  10261   
 10234  10262   /*
 10235  10263   ** CAPI3REF: Test if a changeset has recorded any changes.
 10236  10264   **
 10237  10265   ** Return non-zero if no changes to attached tables have been recorded by 
 10238  10266   ** the session object passed as the first argument. Otherwise, if one or 
................................................................................
 10991  11019   ** CAPI3REF: Streaming Versions of API functions.
 10992  11020   **
 10993  11021   ** The six streaming API xxx_strm() functions serve similar purposes to the 
 10994  11022   ** corresponding non-streaming API functions:
 10995  11023   **
 10996  11024   ** <table border=1 style="margin-left:8ex;margin-right:8ex">
 10997  11025   **   <tr><th>Streaming function<th>Non-streaming equivalent</th>
 10998         -**   <tr><td>sqlite3changeset_apply_str<td>[sqlite3changeset_apply] 
 10999         -**   <tr><td>sqlite3changeset_concat_str<td>[sqlite3changeset_concat] 
 11000         -**   <tr><td>sqlite3changeset_invert_str<td>[sqlite3changeset_invert] 
 11001         -**   <tr><td>sqlite3changeset_start_str<td>[sqlite3changeset_start] 
 11002         -**   <tr><td>sqlite3session_changeset_str<td>[sqlite3session_changeset] 
 11003         -**   <tr><td>sqlite3session_patchset_str<td>[sqlite3session_patchset] 
        11026  +**   <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply] 
        11027  +**   <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat] 
        11028  +**   <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert] 
        11029  +**   <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start] 
        11030  +**   <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset] 
        11031  +**   <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset] 
 11004  11032   ** </table>
 11005  11033   **
 11006  11034   ** Non-streaming functions that accept changesets (or patchsets) as input
 11007  11035   ** require that the entire changeset be stored in a single buffer in memory. 
 11008  11036   ** Similarly, those that return a changeset or patchset do so by returning 
 11009  11037   ** a pointer to a single large buffer allocated using sqlite3_malloc(). 
 11010  11038   ** Normally this is convenient. However, if an application running in a 
................................................................................
 12216  12244   # define ALWAYS(X)      ((X)?1:(assert(0),0))
 12217  12245   # define NEVER(X)       ((X)?(assert(0),1):0)
 12218  12246   #else
 12219  12247   # define ALWAYS(X)      (X)
 12220  12248   # define NEVER(X)       (X)
 12221  12249   #endif
 12222  12250   
        12251  +/*
        12252  +** Some conditionals are optimizations only.  In other words, if the
        12253  +** conditionals are replaced with a constant 1 (true) or 0 (false) then
        12254  +** the correct answer is still obtained, though perhaps not as quickly.
        12255  +**
        12256  +** The following macros mark these optimizations conditionals.
        12257  +*/
        12258  +#if defined(SQLITE_MUTATION_TEST)
        12259  +# define OK_IF_ALWAYS_TRUE(X)  (1)
        12260  +# define OK_IF_ALWAYS_FALSE(X) (0)
        12261  +#else
        12262  +# define OK_IF_ALWAYS_TRUE(X)  (X)
        12263  +# define OK_IF_ALWAYS_FALSE(X) (X)
        12264  +#endif
        12265  +
 12223  12266   /*
 12224  12267   ** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
 12225  12268   ** defined.  We need to defend against those failures when testing with
 12226  12269   ** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
 12227  12270   ** during a normal build.  The following macro can be used to disable tests
 12228  12271   ** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
 12229  12272   */
................................................................................
 12410  12453   #define TK_EXISTS                          20
 12411  12454   #define TK_TEMP                            21
 12412  12455   #define TK_LP                              22
 12413  12456   #define TK_RP                              23
 12414  12457   #define TK_AS                              24
 12415  12458   #define TK_WITHOUT                         25
 12416  12459   #define TK_COMMA                           26
 12417         -#define TK_ID                              27
 12418         -#define TK_ABORT                           28
 12419         -#define TK_ACTION                          29
 12420         -#define TK_AFTER                           30
 12421         -#define TK_ANALYZE                         31
 12422         -#define TK_ASC                             32
 12423         -#define TK_ATTACH                          33
 12424         -#define TK_BEFORE                          34
 12425         -#define TK_BY                              35
 12426         -#define TK_CASCADE                         36
 12427         -#define TK_CAST                            37
 12428         -#define TK_COLUMNKW                        38
 12429         -#define TK_CONFLICT                        39
 12430         -#define TK_DATABASE                        40
 12431         -#define TK_DESC                            41
 12432         -#define TK_DETACH                          42
 12433         -#define TK_EACH                            43
 12434         -#define TK_FAIL                            44
 12435         -#define TK_FOR                             45
 12436         -#define TK_IGNORE                          46
 12437         -#define TK_INITIALLY                       47
 12438         -#define TK_INSTEAD                         48
 12439         -#define TK_LIKE_KW                         49
 12440         -#define TK_MATCH                           50
 12441         -#define TK_NO                              51
 12442         -#define TK_KEY                             52
 12443         -#define TK_OF                              53
 12444         -#define TK_OFFSET                          54
 12445         -#define TK_PRAGMA                          55
 12446         -#define TK_RAISE                           56
 12447         -#define TK_RECURSIVE                       57
 12448         -#define TK_REPLACE                         58
 12449         -#define TK_RESTRICT                        59
 12450         -#define TK_ROW                             60
 12451         -#define TK_TRIGGER                         61
 12452         -#define TK_VACUUM                          62
 12453         -#define TK_VIEW                            63
 12454         -#define TK_VIRTUAL                         64
 12455         -#define TK_WITH                            65
 12456         -#define TK_REINDEX                         66
 12457         -#define TK_RENAME                          67
 12458         -#define TK_CTIME_KW                        68
 12459         -#define TK_ANY                             69
 12460         -#define TK_OR                              70
 12461         -#define TK_AND                             71
 12462         -#define TK_IS                              72
 12463         -#define TK_BETWEEN                         73
 12464         -#define TK_IN                              74
 12465         -#define TK_ISNULL                          75
 12466         -#define TK_NOTNULL                         76
 12467         -#define TK_NE                              77
 12468         -#define TK_EQ                              78
 12469         -#define TK_GT                              79
 12470         -#define TK_LE                              80
 12471         -#define TK_LT                              81
 12472         -#define TK_GE                              82
 12473         -#define TK_ESCAPE                          83
        12460  +#define TK_ABORT                           27
        12461  +#define TK_ACTION                          28
        12462  +#define TK_AFTER                           29
        12463  +#define TK_ANALYZE                         30
        12464  +#define TK_ASC                             31
        12465  +#define TK_ATTACH                          32
        12466  +#define TK_BEFORE                          33
        12467  +#define TK_BY                              34
        12468  +#define TK_CASCADE                         35
        12469  +#define TK_CAST                            36
        12470  +#define TK_CONFLICT                        37
        12471  +#define TK_DATABASE                        38
        12472  +#define TK_DESC                            39
        12473  +#define TK_DETACH                          40
        12474  +#define TK_EACH                            41
        12475  +#define TK_FAIL                            42
        12476  +#define TK_OR                              43
        12477  +#define TK_AND                             44
        12478  +#define TK_IS                              45
        12479  +#define TK_MATCH                           46
        12480  +#define TK_LIKE_KW                         47
        12481  +#define TK_BETWEEN                         48
        12482  +#define TK_IN                              49
        12483  +#define TK_ISNULL                          50
        12484  +#define TK_NOTNULL                         51
        12485  +#define TK_NE                              52
        12486  +#define TK_EQ                              53
        12487  +#define TK_GT                              54
        12488  +#define TK_LE                              55
        12489  +#define TK_LT                              56
        12490  +#define TK_GE                              57
        12491  +#define TK_ESCAPE                          58
        12492  +#define TK_ID                              59
        12493  +#define TK_COLUMNKW                        60
        12494  +#define TK_FOR                             61
        12495  +#define TK_IGNORE                          62
        12496  +#define TK_INITIALLY                       63
        12497  +#define TK_INSTEAD                         64
        12498  +#define TK_NO                              65
        12499  +#define TK_KEY                             66
        12500  +#define TK_OF                              67
        12501  +#define TK_OFFSET                          68
        12502  +#define TK_PRAGMA                          69
        12503  +#define TK_RAISE                           70
        12504  +#define TK_RECURSIVE                       71
        12505  +#define TK_REPLACE                         72
        12506  +#define TK_RESTRICT                        73
        12507  +#define TK_ROW                             74
        12508  +#define TK_TRIGGER                         75
        12509  +#define TK_VACUUM                          76
        12510  +#define TK_VIEW                            77
        12511  +#define TK_VIRTUAL                         78
        12512  +#define TK_WITH                            79
        12513  +#define TK_REINDEX                         80
        12514  +#define TK_RENAME                          81
        12515  +#define TK_CTIME_KW                        82
        12516  +#define TK_ANY                             83
 12474  12517   #define TK_BITAND                          84
 12475  12518   #define TK_BITOR                           85
 12476  12519   #define TK_LSHIFT                          86
 12477  12520   #define TK_RSHIFT                          87
 12478  12521   #define TK_PLUS                            88
 12479  12522   #define TK_MINUS                           89
 12480  12523   #define TK_STAR                            90
................................................................................
 12526  12569   #define TK_CASE                           136
 12527  12570   #define TK_WHEN                           137
 12528  12571   #define TK_THEN                           138
 12529  12572   #define TK_ELSE                           139
 12530  12573   #define TK_INDEX                          140
 12531  12574   #define TK_ALTER                          141
 12532  12575   #define TK_ADD                            142
 12533         -#define TK_TO_TEXT                        143
 12534         -#define TK_TO_BLOB                        144
 12535         -#define TK_TO_NUMERIC                     145
 12536         -#define TK_TO_INT                         146
 12537         -#define TK_TO_REAL                        147
 12538         -#define TK_ISNOT                          148
 12539         -#define TK_END_OF_FILE                    149
 12540         -#define TK_UNCLOSED_STRING                150
 12541         -#define TK_FUNCTION                       151
 12542         -#define TK_COLUMN                         152
 12543         -#define TK_AGG_FUNCTION                   153
 12544         -#define TK_AGG_COLUMN                     154
 12545         -#define TK_UMINUS                         155
 12546         -#define TK_UPLUS                          156
 12547         -#define TK_REGISTER                       157
 12548         -#define TK_VECTOR                         158
 12549         -#define TK_SELECT_COLUMN                  159
 12550         -#define TK_IF_NULL_ROW                    160
 12551         -#define TK_ASTERISK                       161
 12552         -#define TK_SPAN                           162
 12553         -#define TK_SPACE                          163
 12554         -#define TK_ILLEGAL                        164
        12576  +#define TK_ISNOT                          143
        12577  +#define TK_FUNCTION                       144
        12578  +#define TK_COLUMN                         145
        12579  +#define TK_AGG_FUNCTION                   146
        12580  +#define TK_AGG_COLUMN                     147
        12581  +#define TK_UMINUS                         148
        12582  +#define TK_UPLUS                          149
        12583  +#define TK_REGISTER                       150
        12584  +#define TK_VECTOR                         151
        12585  +#define TK_SELECT_COLUMN                  152
        12586  +#define TK_IF_NULL_ROW                    153
        12587  +#define TK_ASTERISK                       154
        12588  +#define TK_SPAN                           155
        12589  +#define TK_END_OF_FILE                    156
        12590  +#define TK_UNCLOSED_STRING                157
        12591  +#define TK_SPACE                          158
        12592  +#define TK_ILLEGAL                        159
 12555  12593   
 12556  12594   /* The token codes above must all fit in 8 bits */
 12557  12595   #define TKFLG_MASK           0xff  
 12558  12596   
 12559  12597   /* Flags that can be added to a token code when it is not
 12560  12598   ** being stored in a u8: */
 12561  12599   #define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
................................................................................
 12667  12705   ** The default value of "20" was choosen to minimize the run-time of the
 12668  12706   ** speedtest1 test program with options: --shrink-memory --reprepare
 12669  12707   */
 12670  12708   #ifndef SQLITE_DEFAULT_PCACHE_INITSZ
 12671  12709   # define SQLITE_DEFAULT_PCACHE_INITSZ 20
 12672  12710   #endif
 12673  12711   
        12712  +/*
        12713  +** The compile-time options SQLITE_MMAP_READWRITE and 
        12714  +** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
        12715  +** You must choose one or the other (or neither) but not both.
        12716  +*/
        12717  +#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
        12718  +#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
        12719  +#endif
        12720  +
 12674  12721   /*
 12675  12722   ** GCC does not define the offsetof() macro so we'll have to do it
 12676  12723   ** ourselves.
 12677  12724   */
 12678  12725   #ifndef offsetof
 12679  12726   #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
 12680  12727   #endif
................................................................................
 12965  13012   # undef SQLITE_ENABLE_STAT3_OR_STAT4
 12966  13013   #endif
 12967  13014   
 12968  13015   /*
 12969  13016   ** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
 12970  13017   ** the Select query generator tracing logic is turned on.
 12971  13018   */
 12972         -#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_SELECTTRACE)
        13019  +#if defined(SQLITE_ENABLE_SELECTTRACE)
 12973  13020   # define SELECTTRACE_ENABLED 1
 12974  13021   #else
 12975  13022   # define SELECTTRACE_ENABLED 0
 12976  13023   #endif
 12977  13024   
 12978  13025   /*
 12979  13026   ** An instance of the following structure is used to store the busy-handler
................................................................................
 13369  13416   SQLITE_PRIVATE int sqlite3BtreeCursor(
 13370  13417     Btree*,                              /* BTree containing table to open */
 13371  13418     int iTable,                          /* Index of root page */
 13372  13419     int wrFlag,                          /* 1 for writing.  0 for read-only */
 13373  13420     struct KeyInfo*,                     /* First argument to compare function */
 13374  13421     BtCursor *pCursor                    /* Space to write cursor structure */
 13375  13422   );
        13423  +SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void);
 13376  13424   SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
 13377  13425   SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
 13378  13426   SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
 13379  13427   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 13380  13428   SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor*, int, ...);
 13381  13429   #endif
 13382  13430   
................................................................................
 13687  13735   /************** Include opcodes.h in the middle of vdbe.h ********************/
 13688  13736   /************** Begin file opcodes.h *****************************************/
 13689  13737   /* Automatically generated.  Do not edit */
 13690  13738   /* See the tool/mkopcodeh.tcl script for details */
 13691  13739   #define OP_Savepoint       0
 13692  13740   #define OP_AutoCommit      1
 13693  13741   #define OP_Transaction     2
 13694         -#define OP_SorterNext      3
 13695         -#define OP_PrevIfOpen      4
 13696         -#define OP_NextIfOpen      5
 13697         -#define OP_Prev            6
 13698         -#define OP_Next            7
        13742  +#define OP_SorterNext      3 /* jump                                       */
        13743  +#define OP_PrevIfOpen      4 /* jump                                       */
        13744  +#define OP_NextIfOpen      5 /* jump                                       */
        13745  +#define OP_Prev            6 /* jump                                       */
        13746  +#define OP_Next            7 /* jump                                       */
 13699  13747   #define OP_Checkpoint      8
 13700  13748   #define OP_JournalMode     9
 13701  13749   #define OP_Vacuum         10
 13702         -#define OP_VFilter        11 /* synopsis: iplan=r[P3] zplan='P4'           */
        13750  +#define OP_VFilter        11 /* jump, synopsis: iplan=r[P3] zplan='P4'     */
 13703  13751   #define OP_VUpdate        12 /* synopsis: data=r[P3@P2]                    */
 13704         -#define OP_Goto           13
 13705         -#define OP_Gosub          14
 13706         -#define OP_InitCoroutine  15
 13707         -#define OP_Yield          16
 13708         -#define OP_MustBeInt      17
 13709         -#define OP_Jump           18
        13752  +#define OP_Goto           13 /* jump                                       */
        13753  +#define OP_Gosub          14 /* jump                                       */
        13754  +#define OP_InitCoroutine  15 /* jump                                       */
        13755  +#define OP_Yield          16 /* jump                                       */
        13756  +#define OP_MustBeInt      17 /* jump                                       */
        13757  +#define OP_Jump           18 /* jump                                       */
 13710  13758   #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
 13711         -#define OP_Once           20
 13712         -#define OP_If             21
 13713         -#define OP_IfNot          22
 13714         -#define OP_IfNullRow      23 /* synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
 13715         -#define OP_SeekLT         24 /* synopsis: key=r[P3@P4]                     */
 13716         -#define OP_SeekLE         25 /* synopsis: key=r[P3@P4]                     */
 13717         -#define OP_SeekGE         26 /* synopsis: key=r[P3@P4]                     */
 13718         -#define OP_SeekGT         27 /* synopsis: key=r[P3@P4]                     */
 13719         -#define OP_NoConflict     28 /* synopsis: key=r[P3@P4]                     */
 13720         -#define OP_NotFound       29 /* synopsis: key=r[P3@P4]                     */
 13721         -#define OP_Found          30 /* synopsis: key=r[P3@P4]                     */
 13722         -#define OP_SeekRowid      31 /* synopsis: intkey=r[P3]                     */
 13723         -#define OP_NotExists      32 /* synopsis: intkey=r[P3]                     */
 13724         -#define OP_Last           33
 13725         -#define OP_IfSmaller      34
 13726         -#define OP_SorterSort     35
 13727         -#define OP_Sort           36
 13728         -#define OP_Rewind         37
 13729         -#define OP_IdxLE          38 /* synopsis: key=r[P3@P4]                     */
 13730         -#define OP_IdxGT          39 /* synopsis: key=r[P3@P4]                     */
 13731         -#define OP_IdxLT          40 /* synopsis: key=r[P3@P4]                     */
 13732         -#define OP_IdxGE          41 /* synopsis: key=r[P3@P4]                     */
 13733         -#define OP_RowSetRead     42 /* synopsis: r[P3]=rowset(P1)                 */
 13734         -#define OP_RowSetTest     43 /* synopsis: if r[P3] in rowset(P1) goto P2   */
 13735         -#define OP_Program        44
 13736         -#define OP_FkIfZero       45 /* synopsis: if fkctr[P1]==0 goto P2          */
 13737         -#define OP_IfPos          46 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
 13738         -#define OP_IfNotZero      47 /* synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
 13739         -#define OP_DecrJumpZero   48 /* synopsis: if (--r[P1])==0 goto P2          */
 13740         -#define OP_IncrVacuum     49
 13741         -#define OP_VNext          50
 13742         -#define OP_Init           51 /* synopsis: Start at P2                      */
 13743         -#define OP_Return         52
 13744         -#define OP_EndCoroutine   53
 13745         -#define OP_HaltIfNull     54 /* synopsis: if r[P3]=null halt               */
 13746         -#define OP_Halt           55
 13747         -#define OP_Integer        56 /* synopsis: r[P2]=P1                         */
 13748         -#define OP_Int64          57 /* synopsis: r[P2]=P4                         */
 13749         -#define OP_String         58 /* synopsis: r[P2]='P4' (len=P1)              */
 13750         -#define OP_Null           59 /* synopsis: r[P2..P3]=NULL                   */
 13751         -#define OP_SoftNull       60 /* synopsis: r[P1]=NULL                       */
 13752         -#define OP_Blob           61 /* synopsis: r[P2]=P4 (len=P1)                */
 13753         -#define OP_Variable       62 /* synopsis: r[P2]=parameter(P1,P4)           */
 13754         -#define OP_Move           63 /* synopsis: r[P2@P3]=r[P1@P3]                */
 13755         -#define OP_Copy           64 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
 13756         -#define OP_SCopy          65 /* synopsis: r[P2]=r[P1]                      */
 13757         -#define OP_IntCopy        66 /* synopsis: r[P2]=r[P1]                      */
 13758         -#define OP_ResultRow      67 /* synopsis: output=r[P1@P2]                  */
 13759         -#define OP_CollSeq        68
 13760         -#define OP_AddImm         69 /* synopsis: r[P1]=r[P1]+P2                   */
 13761         -#define OP_Or             70 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
 13762         -#define OP_And            71 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
 13763         -#define OP_RealAffinity   72
 13764         -#define OP_Cast           73 /* synopsis: affinity(r[P1])                  */
 13765         -#define OP_Permutation    74
 13766         -#define OP_IsNull         75 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
 13767         -#define OP_NotNull        76 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
 13768         -#define OP_Ne             77 /* same as TK_NE, synopsis: IF r[P3]!=r[P1]   */
 13769         -#define OP_Eq             78 /* same as TK_EQ, synopsis: IF r[P3]==r[P1]   */
 13770         -#define OP_Gt             79 /* same as TK_GT, synopsis: IF r[P3]>r[P1]    */
 13771         -#define OP_Le             80 /* same as TK_LE, synopsis: IF r[P3]<=r[P1]   */
 13772         -#define OP_Lt             81 /* same as TK_LT, synopsis: IF r[P3]<r[P1]    */
 13773         -#define OP_Ge             82 /* same as TK_GE, synopsis: IF r[P3]>=r[P1]   */
 13774         -#define OP_ElseNotEq      83 /* same as TK_ESCAPE                          */
        13759  +#define OP_Once           20 /* jump                                       */
        13760  +#define OP_If             21 /* jump                                       */
        13761  +#define OP_IfNot          22 /* jump                                       */
        13762  +#define OP_IfNullRow      23 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
        13763  +#define OP_SeekLT         24 /* jump, synopsis: key=r[P3@P4]               */
        13764  +#define OP_SeekLE         25 /* jump, synopsis: key=r[P3@P4]               */
        13765  +#define OP_SeekGE         26 /* jump, synopsis: key=r[P3@P4]               */
        13766  +#define OP_SeekGT         27 /* jump, synopsis: key=r[P3@P4]               */
        13767  +#define OP_NoConflict     28 /* jump, synopsis: key=r[P3@P4]               */
        13768  +#define OP_NotFound       29 /* jump, synopsis: key=r[P3@P4]               */
        13769  +#define OP_Found          30 /* jump, synopsis: key=r[P3@P4]               */
        13770  +#define OP_SeekRowid      31 /* jump, synopsis: intkey=r[P3]               */
        13771  +#define OP_NotExists      32 /* jump, synopsis: intkey=r[P3]               */
        13772  +#define OP_Last           33 /* jump                                       */
        13773  +#define OP_IfSmaller      34 /* jump                                       */
        13774  +#define OP_SorterSort     35 /* jump                                       */
        13775  +#define OP_Sort           36 /* jump                                       */
        13776  +#define OP_Rewind         37 /* jump                                       */
        13777  +#define OP_IdxLE          38 /* jump, synopsis: key=r[P3@P4]               */
        13778  +#define OP_IdxGT          39 /* jump, synopsis: key=r[P3@P4]               */
        13779  +#define OP_IdxLT          40 /* jump, synopsis: key=r[P3@P4]               */
        13780  +#define OP_IdxGE          41 /* jump, synopsis: key=r[P3@P4]               */
        13781  +#define OP_RowSetRead     42 /* jump, synopsis: r[P3]=rowset(P1)           */
        13782  +#define OP_Or             43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
        13783  +#define OP_And            44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
        13784  +#define OP_RowSetTest     45 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
        13785  +#define OP_Program        46 /* jump                                       */
        13786  +#define OP_FkIfZero       47 /* jump, synopsis: if fkctr[P1]==0 goto P2    */
        13787  +#define OP_IfPos          48 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
        13788  +#define OP_IfNotZero      49 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
        13789  +#define OP_IsNull         50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
        13790  +#define OP_NotNull        51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
        13791  +#define OP_Ne             52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
        13792  +#define OP_Eq             53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
        13793  +#define OP_Gt             54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
        13794  +#define OP_Le             55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
        13795  +#define OP_Lt             56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
        13796  +#define OP_Ge             57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
        13797  +#define OP_ElseNotEq      58 /* jump, same as TK_ESCAPE                    */
        13798  +#define OP_DecrJumpZero   59 /* jump, synopsis: if (--r[P1])==0 goto P2    */
        13799  +#define OP_IncrVacuum     60 /* jump                                       */
        13800  +#define OP_VNext          61 /* jump                                       */
        13801  +#define OP_Init           62 /* jump, synopsis: Start at P2                */
        13802  +#define OP_Return         63
        13803  +#define OP_EndCoroutine   64
        13804  +#define OP_HaltIfNull     65 /* synopsis: if r[P3]=null halt               */
        13805  +#define OP_Halt           66
        13806  +#define OP_Integer        67 /* synopsis: r[P2]=P1                         */
        13807  +#define OP_Int64          68 /* synopsis: r[P2]=P4                         */
        13808  +#define OP_String         69 /* synopsis: r[P2]='P4' (len=P1)              */
        13809  +#define OP_Null           70 /* synopsis: r[P2..P3]=NULL                   */
        13810  +#define OP_SoftNull       71 /* synopsis: r[P1]=NULL                       */
        13811  +#define OP_Blob           72 /* synopsis: r[P2]=P4 (len=P1)                */
        13812  +#define OP_Variable       73 /* synopsis: r[P2]=parameter(P1,P4)           */
        13813  +#define OP_Move           74 /* synopsis: r[P2@P3]=r[P1@P3]                */
        13814  +#define OP_Copy           75 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
        13815  +#define OP_SCopy          76 /* synopsis: r[P2]=r[P1]                      */
        13816  +#define OP_IntCopy        77 /* synopsis: r[P2]=r[P1]                      */
        13817  +#define OP_ResultRow      78 /* synopsis: output=r[P1@P2]                  */
        13818  +#define OP_CollSeq        79
        13819  +#define OP_AddImm         80 /* synopsis: r[P1]=r[P1]+P2                   */
        13820  +#define OP_RealAffinity   81
        13821  +#define OP_Cast           82 /* synopsis: affinity(r[P1])                  */
        13822  +#define OP_Permutation    83
 13775  13823   #define OP_BitAnd         84 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
 13776  13824   #define OP_BitOr          85 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
 13777  13825   #define OP_ShiftLeft      86 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
 13778  13826   #define OP_ShiftRight     87 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
 13779  13827   #define OP_Add            88 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
 13780  13828   #define OP_Subtract       89 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 13781  13829   #define OP_Multiply       90 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
................................................................................
 13809  13857   #define OP_Delete        118
 13810  13858   #define OP_ResetCount    119
 13811  13859   #define OP_SorterCompare 120 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 13812  13860   #define OP_SorterData    121 /* synopsis: r[P2]=data                       */
 13813  13861   #define OP_RowData       122 /* synopsis: r[P2]=data                       */
 13814  13862   #define OP_Rowid         123 /* synopsis: r[P2]=rowid                      */
 13815  13863   #define OP_NullRow       124
 13816         -#define OP_SorterInsert  125 /* synopsis: key=r[P2]                        */
 13817         -#define OP_IdxInsert     126 /* synopsis: key=r[P2]                        */
 13818         -#define OP_IdxDelete     127 /* synopsis: key=r[P2@P3]                     */
 13819         -#define OP_DeferredSeek  128 /* synopsis: Move P3 to P1.rowid if needed    */
 13820         -#define OP_IdxRowid      129 /* synopsis: r[P2]=rowid                      */
 13821         -#define OP_Destroy       130
 13822         -#define OP_Clear         131
        13864  +#define OP_SeekEnd       125
        13865  +#define OP_SorterInsert  126 /* synopsis: key=r[P2]                        */
        13866  +#define OP_IdxInsert     127 /* synopsis: key=r[P2]                        */
        13867  +#define OP_IdxDelete     128 /* synopsis: key=r[P2@P3]                     */
        13868  +#define OP_DeferredSeek  129 /* synopsis: Move P3 to P1.rowid if needed    */
        13869  +#define OP_IdxRowid      130 /* synopsis: r[P2]=rowid                      */
        13870  +#define OP_Destroy       131
 13823  13871   #define OP_Real          132 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 13824         -#define OP_ResetSorter   133
 13825         -#define OP_CreateIndex   134 /* synopsis: r[P2]=root iDb=P1                */
 13826         -#define OP_CreateTable   135 /* synopsis: r[P2]=root iDb=P1                */
        13872  +#define OP_Clear         133
        13873  +#define OP_ResetSorter   134
        13874  +#define OP_CreateBtree   135 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 13827  13875   #define OP_SqlExec       136
 13828  13876   #define OP_ParseSchema   137
 13829  13877   #define OP_LoadAnalysis  138
 13830  13878   #define OP_DropTable     139
 13831  13879   #define OP_DropIndex     140
 13832  13880   #define OP_DropTrigger   141
 13833  13881   #define OP_IntegrityCk   142
................................................................................
 13869  13917   #define OPFLG_OUT3        0x20  /* out3:  P3 is an output */
 13870  13918   #define OPFLG_INITIALIZER {\
 13871  13919   /*   0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,\
 13872  13920   /*   8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01,\
 13873  13921   /*  16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x03, 0x03, 0x01,\
 13874  13922   /*  24 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
 13875  13923   /*  32 */ 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
 13876         -/*  40 */ 0x01, 0x01, 0x23, 0x0b, 0x01, 0x01, 0x03, 0x03,\
 13877         -/*  48 */ 0x03, 0x01, 0x01, 0x01, 0x02, 0x02, 0x08, 0x00,\
 13878         -/*  56 */ 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00,\
 13879         -/*  64 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x02, 0x26, 0x26,\
 13880         -/*  72 */ 0x02, 0x02, 0x00, 0x03, 0x03, 0x0b, 0x0b, 0x0b,\
 13881         -/*  80 */ 0x0b, 0x0b, 0x0b, 0x01, 0x26, 0x26, 0x26, 0x26,\
        13924  +/*  40 */ 0x01, 0x01, 0x23, 0x26, 0x26, 0x0b, 0x01, 0x01,\
        13925  +/*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
        13926  +/*  56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x02,\
        13927  +/*  64 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
        13928  +/*  72 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
        13929  +/*  80 */ 0x02, 0x02, 0x02, 0x00, 0x26, 0x26, 0x26, 0x26,\
 13882  13930   /*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 13883  13931   /*  96 */ 0x00, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 13884  13932   /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 13885  13933   /* 112 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
 13886         -/* 120 */ 0x00, 0x00, 0x00, 0x10, 0x00, 0x04, 0x04, 0x00,\
 13887         -/* 128 */ 0x00, 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10,\
        13934  +/* 120 */ 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x04, 0x04,\
        13935  +/* 128 */ 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00, 0x10,\
 13888  13936   /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,\
 13889  13937   /* 144 */ 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00,\
 13890  13938   /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
 13891  13939   /* 160 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 13892  13940   }
 13893  13941   
 13894  13942   /* The sqlite3P2Values() routine is able to run faster if it knows
 13895  13943   ** the value of the largest JUMP opcode.  The smaller the maximum
 13896  13944   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 13897  13945   ** generated this include file strives to group all JUMP opcodes
 13898  13946   ** together near the beginning of the list.
 13899  13947   */
 13900         -#define SQLITE_MX_JUMP_OPCODE  83  /* Maximum JUMP opcode */
        13948  +#define SQLITE_MX_JUMP_OPCODE  62  /* Maximum JUMP opcode */
 13901  13949   
 13902  13950   /************** End of opcodes.h *********************************************/
 13903  13951   /************** Continuing where we left off in vdbe.h ***********************/
 13904  13952   
 13905  13953   /*
 13906  13954   ** Additional non-public SQLITE_PREPARE_* flags
 13907  13955   */
................................................................................
 14211  14259   
 14212  14260   /* Functions used to obtain and release page references. */ 
 14213  14261   SQLITE_PRIVATE int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
 14214  14262   SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
 14215  14263   SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
 14216  14264   SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
 14217  14265   SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
        14266  +SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage*);
 14218  14267   
 14219  14268   /* Operations on page references. */
 14220  14269   SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
 14221  14270   SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
 14222  14271   SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
 14223  14272   SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
 14224  14273   SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *); 
................................................................................
 14347  14396     ** Elements above, except pCache, are public.  All that follow are 
 14348  14397     ** private to pcache.c and should not be accessed by other modules.
 14349  14398     ** pCache is grouped with the public elements for efficiency.
 14350  14399     */
 14351  14400     i16 nRef;                      /* Number of users of this page */
 14352  14401     PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
 14353  14402     PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
        14403  +                          /* NB: pDirtyNext and pDirtyPrev are undefined if the
        14404  +                          ** PgHdr object is not dirty */
 14354  14405   };
 14355  14406   
 14356  14407   /* Bit values for PgHdr.flags */
 14357  14408   #define PGHDR_CLEAN           0x001  /* Page not on the PCache.pDirty list */
 14358  14409   #define PGHDR_DIRTY           0x002  /* Page is on the PCache.pDirty list */
 14359  14410   #define PGHDR_WRITEABLE       0x004  /* Journaled and ready to modify */
 14360  14411   #define PGHDR_NEED_SYNC       0x008  /* Fsync the rollback journal before
................................................................................
 14728  14779   SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
 14729  14780   SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
 14730  14781   SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
 14731  14782   SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
 14732  14783   #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
 14733  14784   SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
 14734  14785   SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
        14786  +#ifndef SQLITE_OMIT_WAL
 14735  14787   SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
 14736  14788   SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
 14737  14789   SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
 14738  14790   SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
        14791  +#endif /* SQLITE_OMIT_WAL */
 14739  14792   SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
 14740  14793   SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
 14741  14794   
 14742  14795   
 14743  14796   /* 
 14744  14797   ** Functions for accessing sqlite3_vfs methods 
 14745  14798   */
................................................................................
 14940  14993   ** DB_UnresetViews means that one or more views have column names that
 14941  14994   ** have been filled out.  If the schema changes, these column names might
 14942  14995   ** changes and so the view will need to be reset.
 14943  14996   */
 14944  14997   #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
 14945  14998   #define DB_UnresetViews    0x0002  /* Some views have defined column names */
 14946  14999   #define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
        15000  +#define DB_ResetWanted     0x0008  /* Reset the schema when nSchemaLock==0 */
 14947  15001   
 14948  15002   /*
 14949  15003   ** The number of different kinds of things that can be limited
 14950  15004   ** using the sqlite3_limit() interface.
 14951  15005   */
 14952  15006   #define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1)
 14953  15007   
................................................................................
 14971  15025   ** schema information, the Lookaside.bEnabled flag is cleared so that
 14972  15026   ** lookaside allocations are not used to construct the schema objects.
 14973  15027   */
 14974  15028   struct Lookaside {
 14975  15029     u32 bDisable;           /* Only operate the lookaside when zero */
 14976  15030     u16 sz;                 /* Size of each buffer in bytes */
 14977  15031     u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
 14978         -  int nOut;               /* Number of buffers currently checked out */
 14979         -  int mxOut;              /* Highwater mark for nOut */
 14980         -  int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
        15032  +  u32 nSlot;              /* Number of lookaside slots allocated */
        15033  +  u32 anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
        15034  +  LookasideSlot *pInit;   /* List of buffers not previously used */
 14981  15035     LookasideSlot *pFree;   /* List of available buffers */
 14982  15036     void *pStart;           /* First byte of available memory space */
 14983  15037     void *pEnd;             /* First byte past end of available space */
 14984  15038   };
 14985  15039   struct LookasideSlot {
 14986  15040     LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
 14987  15041   };
................................................................................
 15052  15106   struct sqlite3 {
 15053  15107     sqlite3_vfs *pVfs;            /* OS Interface */
 15054  15108     struct Vdbe *pVdbe;           /* List of active virtual machines */
 15055  15109     CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
 15056  15110     sqlite3_mutex *mutex;         /* Connection mutex */
 15057  15111     Db *aDb;                      /* All backends */
 15058  15112     int nDb;                      /* Number of backends currently in use */
 15059         -  int flags;                    /* Miscellaneous flags. See below */
        15113  +  u32 mDbFlags;                 /* flags recording internal state */
        15114  +  u32 flags;                    /* flags settable by pragmas. See below */
 15060  15115     i64 lastRowid;                /* ROWID of most recent insert (see above) */
 15061  15116     i64 szMmap;                   /* Default mmap_size setting */
        15117  +  u32 nSchemaLock;              /* Do not reset the schema when non-zero */
 15062  15118     unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
 15063  15119     int errCode;                  /* Most recent error code (SQLITE_*) */
 15064  15120     int errMask;                  /* & result codes with this before returning */
 15065  15121     int iSysErrno;                /* Errno value from last system error */
 15066  15122     u16 dbOptFlags;               /* Flags to enable/disable optimizations */
 15067  15123     u8 enc;                       /* Text encoding */
 15068  15124     u8 autoCommit;                /* The auto-commit flag. */
................................................................................
 15206  15262   #define SQLITE_ReadUncommit   0x00000400  /* READ UNCOMMITTED in shared-cache */
 15207  15263   #define SQLITE_NoCkptOnClose  0x00000800  /* No checkpoint on close()/DETACH */
 15208  15264   #define SQLITE_ReverseOrder   0x00001000  /* Reverse unordered SELECTs */
 15209  15265   #define SQLITE_RecTriggers    0x00002000  /* Enable recursive triggers */
 15210  15266   #define SQLITE_ForeignKeys    0x00004000  /* Enforce foreign key constraints  */
 15211  15267   #define SQLITE_AutoIndex      0x00008000  /* Enable automatic indexes */
 15212  15268   #define SQLITE_LoadExtension  0x00010000  /* Enable load_extension */
 15213         -#define SQLITE_EnableTrigger  0x00020000  /* True to enable triggers */
 15214         -#define SQLITE_DeferFKs       0x00040000  /* Defer all FK constraints */
 15215         -#define SQLITE_QueryOnly      0x00080000  /* Disable database changes */
 15216         -#define SQLITE_CellSizeCk     0x00100000  /* Check btree cell sizes on load */
 15217         -#define SQLITE_Fts3Tokenizer  0x00200000  /* Enable fts3_tokenizer(2) */
 15218         -#define SQLITE_EnableQPSG     0x00400000  /* Query Planner Stability Guarantee */
 15219         -/* The next four values are not used by PRAGMAs or by sqlite3_dbconfig() and
 15220         -** could be factored out into a separate bit vector of the sqlite3 object. */
 15221         -#define SQLITE_InternChanges  0x00800000  /* Uncommitted Hash table changes */
 15222         -#define SQLITE_LoadExtFunc    0x01000000  /* Enable load_extension() SQL func */
 15223         -#define SQLITE_PreferBuiltin  0x02000000  /* Preference to built-in funcs */
 15224         -#define SQLITE_Vacuum         0x04000000  /* Currently in a VACUUM */
        15269  +#define SQLITE_LoadExtFunc    0x00020000  /* Enable load_extension() SQL func */
        15270  +#define SQLITE_EnableTrigger  0x00040000  /* True to enable triggers */
        15271  +#define SQLITE_DeferFKs       0x00080000  /* Defer all FK constraints */
        15272  +#define SQLITE_QueryOnly      0x00100000  /* Disable database changes */
        15273  +#define SQLITE_CellSizeCk     0x00200000  /* Check btree cell sizes on load */
        15274  +#define SQLITE_Fts3Tokenizer  0x00400000  /* Enable fts3_tokenizer(2) */
        15275  +#define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee */
 15225  15276   /* Flags used only if debugging */
 15226  15277   #ifdef SQLITE_DEBUG
 15227  15278   #define SQLITE_SqlTrace       0x08000000  /* Debug print SQL as it executes */
 15228  15279   #define SQLITE_VdbeListing    0x10000000  /* Debug listings of VDBE programs */
 15229  15280   #define SQLITE_VdbeTrace      0x20000000  /* True to trace VDBE execution */
 15230  15281   #define SQLITE_VdbeAddopTrace 0x40000000  /* Trace sqlite3VdbeAddOp() calls */
 15231  15282   #define SQLITE_VdbeEQP        0x80000000  /* Debug EXPLAIN QUERY PLAN */
 15232  15283   #endif
 15233  15284   
        15285  +/*
        15286  +** Allowed values for sqlite3.mDbFlags
        15287  +*/
        15288  +#define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
        15289  +#define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
        15290  +#define DBFLAG_Vacuum         0x0004  /* Currently in a VACUUM */
 15234  15291   
 15235  15292   /*
 15236  15293   ** Bits of the sqlite3.dbOptFlags field that are used by the
 15237  15294   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
 15238  15295   ** selectively disable various optimizations.
 15239  15296   */
 15240  15297   #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
 15241  15298   #define SQLITE_ColumnCache    0x0002   /* Column cache */
 15242  15299   #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
 15243  15300   #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
 15244         -/*                not used    0x0010   // Was: SQLITE_IdxRealAsInt */
 15245         -#define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
 15246         -#define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
 15247         -#define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
 15248         -#define SQLITE_SubqCoroutine  0x0100   /* Evaluate subqueries as coroutines */
 15249         -#define SQLITE_Transitive     0x0200   /* Transitive constraints */
 15250         -#define SQLITE_OmitNoopJoin   0x0400   /* Omit unused tables in joins */
        15301  +#define SQLITE_DistinctOpt    0x0010   /* DISTINCT using indexes */
        15302  +#define SQLITE_CoverIdxScan   0x0020   /* Covering index scans */
        15303  +#define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
        15304  +#define SQLITE_Transitive     0x0080   /* Transitive constraints */
        15305  +#define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
        15306  +#define SQLITE_CountOfView    0x0200   /* The count-of-view optimization */
        15307  +#define SQLITE_CursorHints    0x0400   /* Add OP_CursorHint opcodes */
 15251  15308   #define SQLITE_Stat34         0x0800   /* Use STAT3 or STAT4 data */
 15252         -#define SQLITE_CountOfView    0x1000   /* The count-of-view optimization */
 15253         -#define SQLITE_CursorHints    0x2000   /* Add OP_CursorHint opcodes */
        15309  +   /* TH3 expects the Stat34  ^^^^^^ value to be 0x0800.  Don't change it */
 15254  15310   #define SQLITE_AllOpts        0xffff   /* All optimizations */
 15255  15311   
 15256  15312   /*
 15257  15313   ** Macros for testing whether or not optimizations are enabled or disabled.
 15258  15314   */
 15259  15315   #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
 15260  15316   #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
................................................................................
 15777  15833   ** Note that aSortOrder[] and aColl[] have nField+1 slots.  There
 15778  15834   ** are nField slots for the columns of an index then one extra slot
 15779  15835   ** for the rowid at the end.
 15780  15836   */
 15781  15837   struct KeyInfo {
 15782  15838     u32 nRef;           /* Number of references to this KeyInfo object */
 15783  15839     u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
 15784         -  u16 nField;         /* Number of key columns in the index */
 15785         -  u16 nXField;        /* Number of columns beyond the key columns */
        15840  +  u16 nKeyField;      /* Number of key columns in the index */
        15841  +  u16 nAllField;      /* Total columns, including key plus others */
 15786  15842     sqlite3 *db;        /* The database connection */
 15787  15843     u8 *aSortOrder;     /* Sort order for each column. */
 15788  15844     CollSeq *aColl[1];  /* Collating sequence for each term of the key */
 15789  15845   };
 15790  15846   
 15791  15847   /*
 15792  15848   ** This object holds a record which has been parsed out into individual
................................................................................
 15825  15881   */
 15826  15882   struct UnpackedRecord {
 15827  15883     KeyInfo *pKeyInfo;  /* Collation and sort-order information */
 15828  15884     Mem *aMem;          /* Values */
 15829  15885     u16 nField;         /* Number of entries in apMem[] */
 15830  15886     i8 default_rc;      /* Comparison result if keys are equal */
 15831  15887     u8 errCode;         /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
 15832         -  i8 r1;              /* Value to return if (lhs > rhs) */
 15833         -  i8 r2;              /* Value to return if (rhs < lhs) */
        15888  +  i8 r1;              /* Value to return if (lhs < rhs) */
        15889  +  i8 r2;              /* Value to return if (lhs > rhs) */
 15834  15890     u8 eqSeen;          /* True if an equality comparison has been seen */
 15835  15891   };
 15836  15892   
 15837  15893   
 15838  15894   /*
 15839  15895   ** Each SQL index is represented in memory by an
 15840  15896   ** instance of the following structure.
................................................................................
 16110  16166                            ** TK_SELECT_COLUMN: column of the result vector */
 16111  16167     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
 16112  16168     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
 16113  16169     u8 op2;                /* TK_REGISTER: original value of Expr.op
 16114  16170                            ** TK_COLUMN: the value of p5 for OP_Column
 16115  16171                            ** TK_AGG_FUNCTION: nesting depth */
 16116  16172     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
 16117         -  Table *pTab;           /* Table for TK_COLUMN expressions. */
        16173  +  Table *pTab;           /* Table for TK_COLUMN expressions.  Can be NULL
        16174  +                         ** for a column of an index on an expression */
 16118  16175   };
 16119  16176   
 16120  16177   /*
 16121  16178   ** The following are the meanings of bits in the Expr.flags field.
 16122  16179   */
 16123  16180   #define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
 16124  16181   #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
................................................................................
 16198  16255   ** column expression as it exists in a SELECT statement.  However, if
 16199  16256   ** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
 16200  16257   ** of the result column in the form: DATABASE.TABLE.COLUMN.  This later
 16201  16258   ** form is used for name resolution with nested FROM clauses.
 16202  16259   */
 16203  16260   struct ExprList {
 16204  16261     int nExpr;             /* Number of expressions on the list */
 16205         -  int nAlloc;            /* Number of a[] slots allocated */
 16206  16262     struct ExprList_item { /* For each expression in the list */
 16207  16263       Expr *pExpr;            /* The parse tree for this expression */
 16208  16264       char *zName;            /* Token associated with this expression */
 16209  16265       char *zSpan;            /* Original text of the expression */
 16210  16266       u8 sortOrder;           /* 1 for DESC or 0 for ASC */
 16211  16267       unsigned done :1;       /* A flag to indicate when processing is finished */
 16212  16268       unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
................................................................................
 16723  16779   #ifndef SQLITE_OMIT_SHARED_CACHE
 16724  16780     int nTableLock;        /* Number of locks in aTableLock */
 16725  16781     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 16726  16782   #endif
 16727  16783     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 16728  16784     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
 16729  16785     Table *pTriggerTab;  /* Table triggers are being coded for */
 16730         -  int addrCrTab;       /* Address of OP_CreateTable opcode on CREATE TABLE */
        16786  +  int addrCrTab;       /* Address of OP_CreateBtree opcode on CREATE TABLE */
 16731  16787     u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
 16732  16788     u32 oldmask;         /* Mask of old.* columns referenced */
 16733  16789     u32 newmask;         /* Mask of new.* columns referenced */
 16734  16790     u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
 16735  16791     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
 16736  16792     u8 disableTriggers;  /* True to disable triggers */
 16737  16793   
................................................................................
 16952  17008   
 16953  17009   /*
 16954  17010   ** An objected used to accumulate the text of a string where we
 16955  17011   ** do not necessarily know how big the string will be in the end.
 16956  17012   */
 16957  17013   struct StrAccum {
 16958  17014     sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
 16959         -  char *zBase;         /* A base allocation.  Not from malloc. */
 16960  17015     char *zText;         /* The string collected so far */
 16961         -  u32  nChar;          /* Length of the string so far */
 16962  17016     u32  nAlloc;         /* Amount of space allocated in zText */
 16963  17017     u32  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
        17018  +  u32  nChar;          /* Length of the string so far */
 16964  17019     u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
 16965  17020     u8   printfFlags;    /* SQLITE_PRINTF flags below */
 16966  17021   };
 16967  17022   #define STRACCUM_NOMEM   1
 16968  17023   #define STRACCUM_TOOBIG  2
 16969  17024   #define SQLITE_PRINTF_INTERNAL 0x01  /* Internal-use-only converters allowed */
 16970  17025   #define SQLITE_PRINTF_SQLFUNC  0x02  /* SQL function arguments to VXPrintf */
................................................................................
 16991  17046   */
 16992  17047   struct Sqlite3Config {
 16993  17048     int bMemstat;                     /* True to enable memory status */
 16994  17049     int bCoreMutex;                   /* True to enable core mutexing */
 16995  17050     int bFullMutex;                   /* True to enable full mutexing */
 16996  17051     int bOpenUri;                     /* True to interpret filenames as URIs */
 16997  17052     int bUseCis;                      /* Use covering indices for full-scans */
        17053  +  int bSmallMalloc;                 /* Avoid large memory allocations if true */
 16998  17054     int mxStrlen;                     /* Maximum string length */
 16999  17055     int neverCorrupt;                 /* Database is always well-formed */
 17000  17056     int szLookaside;                  /* Default lookaside buffer size */
 17001  17057     int nLookaside;                   /* Default lookaside buffer count */
 17002  17058     int nStmtSpill;                   /* Stmt-journal spill-to-disk threshold */
 17003  17059     sqlite3_mem_methods m;            /* Low-level memory allocation interface */
 17004  17060     sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
 17005  17061     sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
 17006  17062     void *pHeap;                      /* Heap storage space */
 17007  17063     int nHeap;                        /* Size of pHeap[] */
 17008  17064     int mnReq, mxReq;                 /* Min and max heap requests sizes */
 17009  17065     sqlite3_int64 szMmap;             /* mmap() space per open file */
 17010  17066     sqlite3_int64 mxMmap;             /* Maximum value for szMmap */
 17011         -  void *pScratch;                   /* Scratch memory */
 17012         -  int szScratch;                    /* Size of each scratch buffer */
 17013         -  int nScratch;                     /* Number of scratch buffers */
 17014  17067     void *pPage;                      /* Page cache memory */
 17015  17068     int szPage;                       /* Size of each page in pPage[] */
 17016  17069     int nPage;                        /* Number of pages in pPage[] */
 17017  17070     int mxParserStack;                /* maximum depth of the parser stack */
 17018  17071     int sharedCacheEnabled;           /* true if shared-cache mode enabled */
 17019  17072     u32 szPma;                        /* Maximum Sorter PMA size */
 17020  17073     /* The above might be initialized to non-zero.  The following need to always
................................................................................
 17093  17146   SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
 17094  17147   SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
 17095  17148   SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
 17096  17149   SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
 17097  17150   SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
 17098  17151   SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*);
 17099  17152   SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*);
        17153  +SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*);
 17100  17154   #ifdef SQLITE_DEBUG
 17101  17155   SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*);
 17102  17156   #endif
 17103  17157   
 17104  17158   /*
 17105  17159   ** Return code from the parse-tree walking primitives and their
 17106  17160   ** callbacks.
................................................................................
 17245  17299   SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
 17246  17300   SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
 17247  17301   SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
 17248  17302   SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
 17249  17303   SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
 17250  17304   SQLITE_PRIVATE int sqlite3MallocSize(void*);
 17251  17305   SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
 17252         -SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
 17253         -SQLITE_PRIVATE void sqlite3ScratchFree(void*);
 17254  17306   SQLITE_PRIVATE void *sqlite3PageMalloc(int);
 17255  17307   SQLITE_PRIVATE void sqlite3PageFree(void*);
 17256  17308   SQLITE_PRIVATE void sqlite3MemSetDefault(void);
 17257  17309   #ifndef SQLITE_UNTESTABLE
 17258  17310   SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
 17259  17311   #endif
 17260  17312   SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
................................................................................
 17302  17354   # define sqlite3MemoryBarrier()
 17303  17355   #endif
 17304  17356   
 17305  17357   SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
 17306  17358   SQLITE_PRIVATE void sqlite3StatusUp(int, int);
 17307  17359   SQLITE_PRIVATE void sqlite3StatusDown(int, int);
 17308  17360   SQLITE_PRIVATE void sqlite3StatusHighwater(int, int);
        17361  +SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*);
 17309  17362   
 17310  17363   /* Access to mutexes used by sqlite3_status() */
 17311  17364   SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
 17312  17365   SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
 17313  17366   
 17314  17367   #ifndef SQLITE_OMIT_FLOATING_POINT
 17315  17368   SQLITE_PRIVATE   int sqlite3IsNaN(double);
................................................................................
 17738  17791   #endif
 17739  17792   
 17740  17793   SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 17741  17794   SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 17742  17795   SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
 17743  17796   SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
 17744  17797   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
        17798  +SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
        17799  +SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
 17745  17800   SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
 17746  17801   SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
 17747  17802   SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
 17748  17803   SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
 17749  17804   SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
 17750  17805   SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
 17751  17806   SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
................................................................................
 18021  18076   #define IN_INDEX_NOOP_OK     0x0001  /* OK to return IN_INDEX_NOOP */
 18022  18077   #define IN_INDEX_MEMBERSHIP  0x0002  /* IN operator used for membership test */
 18023  18078   #define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
 18024  18079   SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*);
 18025  18080   
 18026  18081   SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
 18027  18082   SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
 18028         -#ifdef SQLITE_ENABLE_ATOMIC_WRITE
        18083  +#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
        18084  + || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
 18029  18085   SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
 18030  18086   #endif
 18031  18087   
 18032  18088   SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p);
 18033  18089   SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
 18034  18090   
 18035  18091   SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
................................................................................
 18107  18163   #else
 18108  18164   # define sqlite3MemdebugSetType(X,Y)  /* no-op */
 18109  18165   # define sqlite3MemdebugHasType(X,Y)  1
 18110  18166   # define sqlite3MemdebugNoType(X,Y)   1
 18111  18167   #endif
 18112  18168   #define MEMTYPE_HEAP       0x01  /* General heap allocations */
 18113  18169   #define MEMTYPE_LOOKASIDE  0x02  /* Heap that might have been lookaside */
 18114         -#define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
 18115         -#define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
        18170  +#define MEMTYPE_PCACHE     0x04  /* Page cache allocations */
 18116  18171   
 18117  18172   /*
 18118  18173   ** Threading interface
 18119  18174   */
 18120  18175   #if SQLITE_MAX_WORKER_THREADS>0
 18121  18176   SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
 18122  18177   SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
 18123  18178   #endif
 18124  18179   
        18180  +#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
        18181  +SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3*);
        18182  +#endif
 18125  18183   #if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
 18126  18184   SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
 18127  18185   #endif
 18128  18186   
 18129  18187   SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr);
 18130  18188   SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr);
 18131  18189   SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int);
................................................................................
 18337  18395   */
 18338  18396   SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
 18339  18397      SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
 18340  18398      1,                         /* bCoreMutex */
 18341  18399      SQLITE_THREADSAFE==1,      /* bFullMutex */
 18342  18400      SQLITE_USE_URI,            /* bOpenUri */
 18343  18401      SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
        18402  +   0,                         /* bSmallMalloc */
 18344  18403      0x7ffffffe,                /* mxStrlen */
 18345  18404      0,                         /* neverCorrupt */
 18346  18405      SQLITE_DEFAULT_LOOKASIDE,  /* szLookaside, nLookaside */
 18347  18406      SQLITE_STMTJRNL_SPILL,     /* nStmtSpill */
 18348  18407      {0,0,0,0,0,0,0,0},         /* m */
 18349  18408      {0,0,0,0,0,0,0,0,0},       /* mutex */
 18350  18409      {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
 18351  18410      (void*)0,                  /* pHeap */
 18352  18411      0,                         /* nHeap */
 18353  18412      0, 0,                      /* mnHeap, mxHeap */
 18354  18413      SQLITE_DEFAULT_MMAP_SIZE,  /* szMmap */
 18355  18414      SQLITE_MAX_MMAP_SIZE,      /* mxMmap */
 18356         -   (void*)0,                  /* pScratch */
 18357         -   0,                         /* szScratch */
 18358         -   0,                         /* nScratch */
 18359  18415      (void*)0,                  /* pPage */
 18360  18416      0,                         /* szPage */
 18361  18417      SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */
 18362  18418      0,                         /* mxParserStack */
 18363  18419      0,                         /* sharedCacheEnabled */
 18364  18420      SQLITE_SORTER_PMASZ,       /* szPma */
 18365  18421      /* All the rest should always be initialized to zero */
................................................................................
 18553  18609     /* Cached OP_Column parse information is only valid if cacheStatus matches
 18554  18610     ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
 18555  18611     ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
 18556  18612     ** the cache is out of date. */
 18557  18613     u32 cacheStatus;        /* Cache is valid if this matches Vdbe.cacheCtr */
 18558  18614     int seekResult;         /* Result of previous sqlite3BtreeMoveto() or 0
 18559  18615                             ** if there have been no prior seeks on the cursor. */
 18560         -  /* NB: seekResult does not distinguish between "no seeks have ever occurred
 18561         -  ** on this cursor" and "the most recent seek was an exact match". */
        18616  +  /* seekResult does not distinguish between "no seeks have ever occurred
        18617  +  ** on this cursor" and "the most recent seek was an exact match".
        18618  +  ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
 18562  18619   
 18563  18620     /* When a new VdbeCursor is allocated, only the fields above are zeroed.
 18564  18621     ** The fields that follow are uninitialized, and must be individually
 18565  18622     ** initialized prior to first use. */
 18566  18623     VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
 18567  18624     union {
 18568         -    BtCursor *pCursor;          /* CURTYPE_BTREE.  Btree cursor */
 18569         -    sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB.   Vtab cursor */
 18570         -    int pseudoTableReg;         /* CURTYPE_PSEUDO. Reg holding content. */
 18571         -    VdbeSorter *pSorter;        /* CURTYPE_SORTER. Sorter object */
        18625  +    BtCursor *pCursor;          /* CURTYPE_BTREE or _PSEUDO.  Btree cursor */
        18626  +    sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB.              Vtab cursor */
        18627  +    VdbeSorter *pSorter;        /* CURTYPE_SORTER.            Sorter object */
 18572  18628     } uc;
 18573  18629     KeyInfo *pKeyInfo;      /* Info about index keys needed by index cursors */
 18574  18630     u32 iHdrOffset;         /* Offset to next unparsed byte of the header */
 18575  18631     Pgno pgnoRoot;          /* Root page of the open btree cursor */
 18576  18632     i16 nField;             /* Number of fields in the header */
 18577  18633     u16 nHdrParsed;         /* Number of header fields parsed so far */
 18578  18634     i64 movetoTarget;       /* Argument to the deferred sqlite3BtreeMoveto() */
................................................................................
 19122  19178     newValue = (sqlite3StatValueType)X;
 19123  19179     assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 19124  19180     assert( op>=0 && op<ArraySize(statMutex) );
 19125  19181     assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
 19126  19182                                              : sqlite3MallocMutex()) );
 19127  19183     assert( op==SQLITE_STATUS_MALLOC_SIZE
 19128  19184             || op==SQLITE_STATUS_PAGECACHE_SIZE
 19129         -          || op==SQLITE_STATUS_SCRATCH_SIZE
 19130  19185             || op==SQLITE_STATUS_PARSER_STACK );
 19131  19186     if( newValue>wsdStat.mxValue[op] ){
 19132  19187       wsdStat.mxValue[op] = newValue;
 19133  19188     }
 19134  19189   }
 19135  19190   
 19136  19191   /*
................................................................................
 19170  19225     rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
 19171  19226     if( rc==0 ){
 19172  19227       *pCurrent = (int)iCur;
 19173  19228       *pHighwater = (int)iHwtr;
 19174  19229     }
 19175  19230     return rc;
 19176  19231   }
        19232  +
        19233  +/*
        19234  +** Return the number of LookasideSlot elements on the linked list
        19235  +*/
        19236  +static u32 countLookasideSlots(LookasideSlot *p){
        19237  +  u32 cnt = 0;
        19238  +  while( p ){
        19239  +    p = p->pNext;
        19240  +    cnt++;
        19241  +  }
        19242  +  return cnt;
        19243  +}
        19244  +
        19245  +/*
        19246  +** Count the number of slots of lookaside memory that are outstanding
        19247  +*/
        19248  +SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
        19249  +  u32 nInit = countLookasideSlots(db->lookaside.pInit);
        19250  +  u32 nFree = countLookasideSlots(db->lookaside.pFree);
        19251  +  if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
        19252  +  return db->lookaside.nSlot - (nInit+nFree);
        19253  +}
 19177  19254   
 19178  19255   /*
 19179  19256   ** Query status information for a single database connection
 19180  19257   */
 19181  19258   SQLITE_API int sqlite3_db_status(
 19182  19259     sqlite3 *db,          /* The database connection whose status is desired */
 19183  19260     int op,               /* Status verb */
................................................................................
 19190  19267     if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
 19191  19268       return SQLITE_MISUSE_BKPT;
 19192  19269     }
 19193  19270   #endif
 19194  19271     sqlite3_mutex_enter(db->mutex);
 19195  19272     switch( op ){
 19196  19273       case SQLITE_DBSTATUS_LOOKASIDE_USED: {
 19197         -      *pCurrent = db->lookaside.nOut;
 19198         -      *pHighwater = db->lookaside.mxOut;
        19274  +      *pCurrent = sqlite3LookasideUsed(db, pHighwater);
 19199  19275         if( resetFlag ){
 19200         -        db->lookaside.mxOut = db->lookaside.nOut;
        19276  +        LookasideSlot *p = db->lookaside.pFree;
        19277  +        if( p ){
        19278  +          while( p->pNext ) p = p->pNext;
        19279  +          p->pNext = db->lookaside.pInit;
        19280  +          db->lookaside.pInit = db->lookaside.pFree;
        19281  +          db->lookaside.pFree = 0;
        19282  +        }
 19201  19283         }
 19202  19284         break;
 19203  19285       }
 19204  19286   
 19205  19287       case SQLITE_DBSTATUS_LOOKASIDE_HIT:
 19206  19288       case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
 19207  19289       case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
................................................................................
 20704  20786     return id->pMethods->xWrite(id, pBuf, amt, offset);
 20705  20787   }
 20706  20788   SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
 20707  20789     return id->pMethods->xTruncate(id, size);
 20708  20790   }
 20709  20791   SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
 20710  20792     DO_OS_MALLOC_TEST(id);
 20711         -  return id->pMethods->xSync(id, flags);
        20793  +  return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
 20712  20794   }
 20713  20795   SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
 20714  20796     DO_OS_MALLOC_TEST(id);
 20715  20797     return id->pMethods->xFileSize(id, pSize);
 20716  20798   }
 20717  20799   SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
 20718  20800     DO_OS_MALLOC_TEST(id);
................................................................................
 20759  20841   SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
 20760  20842     int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
 20761  20843     return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
 20762  20844   }
 20763  20845   SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
 20764  20846     return id->pMethods->xDeviceCharacteristics(id);
 20765  20847   }
        20848  +#ifndef SQLITE_OMIT_WAL
 20766  20849   SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
 20767  20850     return id->pMethods->xShmLock(id, offset, n, flags);
 20768  20851   }
 20769  20852   SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
 20770  20853     id->pMethods->xShmBarrier(id);
 20771  20854   }
 20772  20855   SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
................................................................................
 20778  20861     int pgsz,
 20779  20862     int bExtend,                    /* True to extend file if necessary */
 20780  20863     void volatile **pp              /* OUT: Pointer to mapping */
 20781  20864   ){
 20782  20865     DO_OS_MALLOC_TEST(id);
 20783  20866     return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
 20784  20867   }
        20868  +#endif /* SQLITE_OMIT_WAL */
 20785  20869   
 20786  20870   #if SQLITE_MAX_MMAP_SIZE>0
 20787  20871   /* The real implementation of xFetch and xUnfetch */
 20788  20872   SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
 20789  20873     DO_OS_MALLOC_TEST(id);
 20790  20874     return id->pMethods->xFetch(id, iOff, iAmt, pp);
 20791  20875   }
................................................................................
 24774  24858     ** is a no-op returning zero if SQLite is not compiled with
 24775  24859     ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
 24776  24860     UNUSED_PARAMETER(n);
 24777  24861     return 0;
 24778  24862   #endif
 24779  24863   }
 24780  24864   
 24781         -/*
 24782         -** An instance of the following object records the location of
 24783         -** each unused scratch buffer.
 24784         -*/
 24785         -typedef struct ScratchFreeslot {
 24786         -  struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
 24787         -} ScratchFreeslot;
 24788         -
 24789  24865   /*
 24790  24866   ** State information local to the memory allocation subsystem.
 24791  24867   */
 24792  24868   static SQLITE_WSD struct Mem0Global {
 24793  24869     sqlite3_mutex *mutex;         /* Mutex to serialize access */
 24794  24870     sqlite3_int64 alarmThreshold; /* The soft heap limit */
 24795  24871   
 24796         -  /*
 24797         -  ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
 24798         -  ** (so that a range test can be used to determine if an allocation
 24799         -  ** being freed came from pScratch) and a pointer to the list of
 24800         -  ** unused scratch allocations.
 24801         -  */
 24802         -  void *pScratchEnd;
 24803         -  ScratchFreeslot *pScratchFree;
 24804         -  u32 nScratchFree;
 24805         -
 24806  24872     /*
 24807  24873     ** True if heap is nearly "full" where "full" is defined by the
 24808  24874     ** sqlite3_soft_heap_limit() setting.
 24809  24875     */
 24810  24876     int nearlyFull;
 24811         -} mem0 = { 0, 0, 0, 0, 0, 0 };
        24877  +} mem0 = { 0, 0, 0 };
 24812  24878   
 24813  24879   #define mem0 GLOBAL(struct Mem0Global, mem0)
 24814  24880   
 24815  24881   /*
 24816  24882   ** Return the memory allocator mutex. sqlite3_status() needs it.
 24817  24883   */
 24818  24884   SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
................................................................................
 24874  24940   SQLITE_PRIVATE int sqlite3MallocInit(void){
 24875  24941     int rc;
 24876  24942     if( sqlite3GlobalConfig.m.xMalloc==0 ){
 24877  24943       sqlite3MemSetDefault();
 24878  24944     }
 24879  24945     memset(&mem0, 0, sizeof(mem0));
 24880  24946     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 24881         -  if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
 24882         -      && sqlite3GlobalConfig.nScratch>0 ){
 24883         -    int i, n, sz;
 24884         -    ScratchFreeslot *pSlot;
 24885         -    sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
 24886         -    sqlite3GlobalConfig.szScratch = sz;
 24887         -    pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
 24888         -    n = sqlite3GlobalConfig.nScratch;
 24889         -    mem0.pScratchFree = pSlot;
 24890         -    mem0.nScratchFree = n;
 24891         -    for(i=0; i<n-1; i++){
 24892         -      pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
 24893         -      pSlot = pSlot->pNext;
 24894         -    }
 24895         -    pSlot->pNext = 0;
 24896         -    mem0.pScratchEnd = (void*)&pSlot[1];
 24897         -  }else{
 24898         -    mem0.pScratchEnd = 0;
 24899         -    sqlite3GlobalConfig.pScratch = 0;
 24900         -    sqlite3GlobalConfig.szScratch = 0;
 24901         -    sqlite3GlobalConfig.nScratch = 0;
 24902         -  }
 24903  24947     if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
 24904  24948         || sqlite3GlobalConfig.nPage<=0 ){
 24905  24949       sqlite3GlobalConfig.pPage = 0;
 24906  24950       sqlite3GlobalConfig.szPage = 0;
 24907  24951     }
 24908  24952     rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
 24909  24953     if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
................................................................................
 25046  25090   SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
 25047  25091   #ifndef SQLITE_OMIT_AUTOINIT
 25048  25092     if( sqlite3_initialize() ) return 0;
 25049  25093   #endif
 25050  25094     return sqlite3Malloc(n);
 25051  25095   }
 25052  25096   
 25053         -/*
 25054         -** Each thread may only have a single outstanding allocation from
 25055         -** xScratchMalloc().  We verify this constraint in the single-threaded
 25056         -** case by setting scratchAllocOut to 1 when an allocation
 25057         -** is outstanding clearing it when the allocation is freed.
 25058         -*/
 25059         -#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 25060         -static int scratchAllocOut = 0;
 25061         -#endif
 25062         -
 25063         -
 25064         -/*
 25065         -** Allocate memory that is to be used and released right away.
 25066         -** This routine is similar to alloca() in that it is not intended
 25067         -** for situations where the memory might be held long-term.  This
 25068         -** routine is intended to get memory to old large transient data
 25069         -** structures that would not normally fit on the stack of an
 25070         -** embedded processor.
 25071         -*/
 25072         -SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
 25073         -  void *p;
 25074         -  assert( n>0 );
 25075         -
 25076         -  sqlite3_mutex_enter(mem0.mutex);
 25077         -  sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n);
 25078         -  if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
 25079         -    p = mem0.pScratchFree;
 25080         -    mem0.pScratchFree = mem0.pScratchFree->pNext;
 25081         -    mem0.nScratchFree--;
 25082         -    sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
 25083         -    sqlite3_mutex_leave(mem0.mutex);
 25084         -  }else{
 25085         -    sqlite3_mutex_leave(mem0.mutex);
 25086         -    p = sqlite3Malloc(n);
 25087         -    if( sqlite3GlobalConfig.bMemstat && p ){
 25088         -      sqlite3_mutex_enter(mem0.mutex);
 25089         -      sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
 25090         -      sqlite3_mutex_leave(mem0.mutex);
 25091         -    }
 25092         -    sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
 25093         -  }
 25094         -  assert( sqlite3_mutex_notheld(mem0.mutex) );
 25095         -
 25096         -
 25097         -#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 25098         -  /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
 25099         -  ** buffers per thread.
 25100         -  **
 25101         -  ** This can only be checked in single-threaded mode.
 25102         -  */
 25103         -  assert( scratchAllocOut==0 );
 25104         -  if( p ) scratchAllocOut++;
 25105         -#endif
 25106         -
 25107         -  return p;
 25108         -}
 25109         -SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
 25110         -  if( p ){
 25111         -
 25112         -#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 25113         -    /* Verify that no more than two scratch allocation per thread
 25114         -    ** is outstanding at one time.  (This is only checked in the
 25115         -    ** single-threaded case since checking in the multi-threaded case
 25116         -    ** would be much more complicated.) */
 25117         -    assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
 25118         -    scratchAllocOut--;
 25119         -#endif
 25120         -
 25121         -    if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){
 25122         -      /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
 25123         -      ScratchFreeslot *pSlot;
 25124         -      pSlot = (ScratchFreeslot*)p;
 25125         -      sqlite3_mutex_enter(mem0.mutex);
 25126         -      pSlot->pNext = mem0.pScratchFree;
 25127         -      mem0.pScratchFree = pSlot;
 25128         -      mem0.nScratchFree++;
 25129         -      assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
 25130         -      sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
 25131         -      sqlite3_mutex_leave(mem0.mutex);
 25132         -    }else{
 25133         -      /* Release memory back to the heap */
 25134         -      assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
 25135         -      assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
 25136         -      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 25137         -      if( sqlite3GlobalConfig.bMemstat ){
 25138         -        int iSize = sqlite3MallocSize(p);
 25139         -        sqlite3_mutex_enter(mem0.mutex);
 25140         -        sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
 25141         -        sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
 25142         -        sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
 25143         -        sqlite3GlobalConfig.m.xFree(p);
 25144         -        sqlite3_mutex_leave(mem0.mutex);
 25145         -      }else{
 25146         -        sqlite3GlobalConfig.m.xFree(p);
 25147         -      }
 25148         -    }
 25149         -  }
 25150         -}
 25151         -
 25152  25097   /*
 25153  25098   ** TRUE if p is a lookaside memory allocation from db
 25154  25099   */
 25155  25100   #ifndef SQLITE_OMIT_LOOKASIDE
 25156  25101   static int isLookaside(sqlite3 *db, void *p){
 25157  25102     return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
 25158  25103   }
................................................................................
 25235  25180         LookasideSlot *pBuf = (LookasideSlot*)p;
 25236  25181   #ifdef SQLITE_DEBUG
 25237  25182         /* Trash all content in the buffer being freed */
 25238  25183         memset(p, 0xaa, db->lookaside.sz);
 25239  25184   #endif
 25240  25185         pBuf->pNext = db->lookaside.pFree;
 25241  25186         db->lookaside.pFree = pBuf;
 25242         -      db->lookaside.nOut--;
 25243  25187         return;
 25244  25188       }
 25245  25189     }
 25246  25190     assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
 25247  25191     assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
 25248  25192     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
 25249  25193     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
................................................................................
 25396  25340     assert( db!=0 );
 25397  25341     assert( sqlite3_mutex_held(db->mutex) );
 25398  25342     assert( db->pnBytesFreed==0 );
 25399  25343     if( db->lookaside.bDisable==0 ){
 25400  25344       assert( db->mallocFailed==0 );
 25401  25345       if( n>db->lookaside.sz ){
 25402  25346         db->lookaside.anStat[1]++;
 25403         -    }else if( (pBuf = db->lookaside.pFree)==0 ){
 25404         -      db->lookaside.anStat[2]++;
 25405         -    }else{
        25347  +    }else if( (pBuf = db->lookaside.pFree)!=0 ){
 25406  25348         db->lookaside.pFree = pBuf->pNext;
 25407         -      db->lookaside.nOut++;
        25349  +      db->lookaside.anStat[0]++;
        25350  +      return (void*)pBuf;
        25351  +    }else if( (pBuf = db->lookaside.pInit)!=0 ){
        25352  +      db->lookaside.pInit = pBuf->pNext;
 25408  25353         db->lookaside.anStat[0]++;
 25409         -      if( db->lookaside.nOut>db->lookaside.mxOut ){
 25410         -        db->lookaside.mxOut = db->lookaside.nOut;
 25411         -      }
 25412  25354         return (void*)pBuf;
        25355  +    }else{
        25356  +      db->lookaside.anStat[2]++;
 25413  25357       }
 25414  25358     }else if( db->mallocFailed ){
 25415  25359       return 0;
 25416  25360     }
 25417  25361   #else
 25418  25362     assert( db!=0 );
 25419  25363     assert( sqlite3_mutex_held(db->mutex) );
................................................................................
 26243  26187             bufpt = "";
 26244  26188           }else if( xtype==etDYNSTRING ){
 26245  26189             zExtra = bufpt;
 26246  26190           }
 26247  26191           if( precision>=0 ){
 26248  26192             for(length=0; length<precision && bufpt[length]; length++){}
 26249  26193           }else{
 26250         -          length = sqlite3Strlen30(bufpt);
        26194  +          length = 0x7fffffff & (int)strlen(bufpt);
 26251  26195           }
 26252  26196           break;
 26253  26197         case etSQLESCAPE:           /* Escape ' characters */
 26254  26198         case etSQLESCAPE2:          /* Escape ' and enclose in '...' */
 26255  26199         case etSQLESCAPE3: {        /* Escape " characters */
 26256  26200           int i, j, k, n, isnull;
 26257  26201           int needQuote;
................................................................................
 26369  26313     if( p->mxAlloc==0 ){
 26370  26314       N = p->nAlloc - p->nChar - 1;
 26371  26315       setStrAccumError(p, STRACCUM_TOOBIG);
 26372  26316       return N;
 26373  26317     }else{
 26374  26318       char *zOld = isMalloced(p) ? p->zText : 0;
 26375  26319       i64 szNew = p->nChar;
 26376         -    assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
 26377  26320       szNew += N + 1;
 26378  26321       if( szNew+p->nChar<=p->mxAlloc ){
 26379  26322         /* Force exponential buffer size growth as long as it does not overflow,
 26380  26323         ** to avoid having to call this routine too often */
 26381  26324         szNew += p->nChar;
 26382  26325       }
 26383  26326       if( szNew > p->mxAlloc ){
................................................................................
 26411  26354   ** Append N copies of character c to the given string buffer.
 26412  26355   */
 26413  26356   SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
 26414  26357     testcase( p->nChar + (i64)N > 0x7fffffff );
 26415  26358     if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
 26416  26359       return;
 26417  26360     }
 26418         -  assert( (p->zText==p->zBase)==!isMalloced(p) );
 26419  26361     while( (N--)>0 ) p->zText[p->nChar++] = c;
 26420  26362   }
 26421  26363   
 26422  26364   /*
 26423  26365   ** The StrAccum "p" is not large enough to accept N new bytes of z[].
 26424  26366   ** So enlarge if first, then do the append.
 26425  26367   **
................................................................................
 26429  26371   */
 26430  26372   static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
 26431  26373     N = sqlite3StrAccumEnlarge(p, N);
 26432  26374     if( N>0 ){
 26433  26375       memcpy(&p->zText[p->nChar], z, N);
 26434  26376       p->nChar += N;
 26435  26377     }
 26436         -  assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
 26437  26378   }
 26438  26379   
 26439  26380   /*
 26440  26381   ** Append N bytes of text from z to the StrAccum object.  Increase the
 26441  26382   ** size of the memory allocation for StrAccum if necessary.
 26442  26383   */
 26443  26384   SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
................................................................................
 26464  26405   
 26465  26406   /*
 26466  26407   ** Finish off a string by making sure it is zero-terminated.
 26467  26408   ** Return a pointer to the resulting string.  Return a NULL
 26468  26409   ** pointer if any kind of error was encountered.
 26469  26410   */
 26470  26411   static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
        26412  +  char *zText;
 26471  26413     assert( p->mxAlloc>0 && !isMalloced(p) );
 26472         -  p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
 26473         -  if( p->zText ){
 26474         -    memcpy(p->zText, p->zBase, p->nChar+1);
        26414  +  zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
        26415  +  if( zText ){
        26416  +    memcpy(zText, p->zText, p->nChar+1);
 26475  26417       p->printfFlags |= SQLITE_PRINTF_MALLOCED;
 26476  26418     }else{
 26477  26419       setStrAccumError(p, STRACCUM_NOMEM);
 26478  26420     }
 26479         -  return p->zText;
        26421  +  p->zText = zText;
        26422  +  return zText;
 26480  26423   }
 26481  26424   SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
 26482  26425     if( p->zText ){
 26483         -    assert( (p->zText==p->zBase)==!isMalloced(p) );
 26484  26426       p->zText[p->nChar] = 0;
 26485  26427       if( p->mxAlloc>0 && !isMalloced(p) ){
 26486  26428         return strAccumFinishRealloc(p);
 26487  26429       }
 26488  26430     }
 26489  26431     return p->zText;
 26490  26432   }
 26491  26433   
 26492  26434   /*
 26493  26435   ** Reset an StrAccum string.  Reclaim all malloced memory.
 26494  26436   */
 26495  26437   SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
 26496         -  assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
 26497  26438     if( isMalloced(p) ){
 26498  26439       sqlite3DbFree(p->db, p->zText);
 26499  26440       p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
 26500  26441     }
 26501  26442     p->zText = 0;
 26502  26443   }
 26503  26444   
................................................................................
 26512  26453   **        is malloced.
 26513  26454   ** n:     Size of zBase in bytes.  If total space requirements never exceed
 26514  26455   **        n then no memory allocations ever occur.
 26515  26456   ** mx:    Maximum number of bytes to accumulate.  If mx==0 then no memory
 26516  26457   **        allocations will ever occur.
 26517  26458   */
 26518  26459   SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
 26519         -  p->zText = p->zBase = zBase;
        26460  +  p->zText = zBase;
 26520  26461     p->db = db;
 26521         -  p->nChar = 0;
 26522  26462     p->nAlloc = n;
 26523  26463     p->mxAlloc = mx;
        26464  +  p->nChar = 0;
 26524  26465     p->accError = 0;
 26525  26466     p->printfFlags = 0;
 26526  26467   }
 26527  26468   
 26528  26469   /*
 26529  26470   ** Print into memory obtained from sqliteMalloc().  Use the internal
 26530  26471   ** %-conversion extensions.
................................................................................
 28677  28618               result = s * scale;
 28678  28619               result *= 1.0e+308;
 28679  28620             }
 28680  28621           }else{ assert( e>=342 );
 28681  28622             if( esign<0 ){
 28682  28623               result = 0.0*s;
 28683  28624             }else{
        28625  +#ifdef INFINITY
        28626  +            result = INFINITY*s;
        28627  +#else
 28684  28628               result = 1e308*1e308*s;  /* Infinity */
        28629  +#endif
 28685  28630             }
 28686  28631           }
 28687  28632         }else{
 28688  28633           /* 1.0e+22 is the largest power of 10 than can be 
 28689  28634           ** represented exactly. */
 28690  28635           while( e%22 ) { scale *= 1.0e+1; e -= 1; }
 28691  28636           while( e>0 ) { scale *= 1.0e+22; e -= 22; }
................................................................................
 28739  28684     return c;
 28740  28685   }
 28741  28686   
 28742  28687   /*
 28743  28688   ** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
 28744  28689   ** routine does *not* accept hexadecimal notation.
 28745  28690   **
 28746         -** If the zNum value is representable as a 64-bit twos-complement 
 28747         -** integer, then write that value into *pNum and return 0.
        28691  +** Returns:
 28748  28692   **
 28749         -** If zNum is exactly 9223372036854775808, return 2.  This special
 28750         -** case is broken out because while 9223372036854775808 cannot be a 
 28751         -** signed 64-bit integer, its negative -9223372036854775808 can be.
 28752         -**
 28753         -** If zNum is too big for a 64-bit integer and is not
 28754         -** 9223372036854775808  or if zNum contains any non-numeric text,
 28755         -** then return 1.
        28693  +**     0    Successful transformation.  Fits in a 64-bit signed integer.
        28694  +**     1    Excess text after the integer value
        28695  +**     2    Integer too large for a 64-bit signed integer or is malformed
        28696  +**     3    Special case of 9223372036854775808
 28756  28697   **
 28757  28698   ** length is the number of bytes in the string (bytes, not characters).
 28758  28699   ** The string is not necessarily zero-terminated.  The encoding is
 28759  28700   ** given by enc.
 28760  28701   */
 28761  28702   SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
 28762  28703     int incr;
 28763  28704     u64 u = 0;
 28764  28705     int neg = 0; /* assume positive */
 28765  28706     int i;
 28766  28707     int c = 0;
 28767  28708     int nonNum = 0;  /* True if input contains UTF16 with high byte non-zero */
        28709  +  int rc;          /* Baseline return code */
 28768  28710     const char *zStart;
 28769  28711     const char *zEnd = zNum + length;
 28770  28712     assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
 28771  28713     if( enc==SQLITE_UTF8 ){
 28772  28714       incr = 1;
 28773  28715     }else{
 28774  28716       incr = 2;
................................................................................
 28800  28742       *pNum = (i64)u;
 28801  28743     }
 28802  28744     testcase( i==18 );
 28803  28745     testcase( i==19 );
 28804  28746     testcase( i==20 );
 28805  28747     if( &zNum[i]<zEnd              /* Extra bytes at the end */
 28806  28748      || (i==0 && zStart==zNum)     /* No digits */
 28807         -   || i>19*incr                  /* Too many digits */
 28808  28749      || nonNum                     /* UTF16 with high-order bytes non-zero */
 28809  28750     ){
        28751  +    rc = 1;
        28752  +  }else{
        28753  +    rc = 0;
        28754  +  }
        28755  +  if( i>19*incr ){                /* Too many digits */
 28810  28756       /* zNum is empty or contains non-numeric text or is longer
 28811  28757       ** than 19 digits (thus guaranteeing that it is too large) */
 28812         -    return 1;
        28758  +    return 2;
 28813  28759     }else if( i<19*incr ){
 28814  28760       /* Less than 19 digits, so we know that it fits in 64 bits */
 28815  28761       assert( u<=LARGEST_INT64 );
 28816         -    return 0;
        28762  +    return rc;
 28817  28763     }else{
 28818  28764       /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
 28819  28765       c = compare2pow63(zNum, incr);
 28820  28766       if( c<0 ){
 28821  28767         /* zNum is less than 9223372036854775808 so it fits */
 28822  28768         assert( u<=LARGEST_INT64 );
 28823         -      return 0;
        28769  +      return rc;
 28824  28770       }else if( c>0 ){
 28825  28771         /* zNum is greater than 9223372036854775808 so it overflows */
 28826         -      return 1;
        28772  +      return 2;
 28827  28773       }else{
 28828  28774         /* zNum is exactly 9223372036854775808.  Fits if negative.  The
 28829  28775         ** special case 2 overflow if positive */
 28830  28776         assert( u-1==LARGEST_INT64 );
 28831         -      return neg ? 0 : 2;
        28777  +      return neg ? rc : 3;
 28832  28778       }
 28833  28779     }
 28834  28780   }
 28835  28781   
 28836  28782   /*
 28837  28783   ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
 28838  28784   ** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
 28839  28785   ** whereas sqlite3Atoi64() does not.
 28840  28786   **
 28841  28787   ** Returns:
 28842  28788   **
 28843  28789   **     0    Successful transformation.  Fits in a 64-bit signed integer.
 28844         -**     1    Integer too large for a 64-bit signed integer or is malformed
 28845         -**     2    Special case of 9223372036854775808
        28790  +**     1    Excess text after the integer value
        28791  +**     2    Integer too large for a 64-bit signed integer or is malformed
        28792  +**     3    Special case of 9223372036854775808
 28846  28793   */
 28847  28794   SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
 28848  28795   #ifndef SQLITE_OMIT_HEX_INTEGER
 28849  28796     if( z[0]=='0'
 28850  28797      && (z[1]=='x' || z[1]=='X')
 28851  28798     ){
 28852  28799       u64 u = 0;
 28853  28800       int i, k;
 28854  28801       for(i=2; z[i]=='0'; i++){}
 28855  28802       for(k=i; sqlite3Isxdigit(z[k]); k++){
 28856  28803         u = u*16 + sqlite3HexToInt(z[k]);
 28857  28804       }
 28858  28805       memcpy(pOut, &u, 8);
 28859         -    return (z[k]==0 && k-i<=16) ? 0 : 1;
        28806  +    return (z[k]==0 && k-i<=16) ? 0 : 2;
 28860  28807     }else
 28861  28808   #endif /* SQLITE_OMIT_HEX_INTEGER */
 28862  28809     {
 28863  28810       return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
 28864  28811     }
 28865  28812   }
 28866  28813   
................................................................................
 29462  29409   /*
 29463  29410   ** Attempt to add, substract, or multiply the 64-bit signed value iB against
 29464  29411   ** the other 64-bit signed integer at *pA and store the result in *pA.
 29465  29412   ** Return 0 on success.  Or if the operation would have resulted in an
 29466  29413   ** overflow, leave *pA unchanged and return 1.
 29467  29414   */
 29468  29415   SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
 29469         -#if GCC_VERSION>=5004000
        29416  +#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
 29470  29417     return __builtin_add_overflow(*pA, iB, pA);
 29471  29418   #else
 29472  29419     i64 iA = *pA;
 29473  29420     testcase( iA==0 ); testcase( iA==1 );
 29474  29421     testcase( iB==-1 ); testcase( iB==0 );
 29475  29422     if( iB>=0 ){
 29476  29423       testcase( iA>0 && LARGEST_INT64 - iA == iB );
................................................................................
 29482  29429       if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
 29483  29430     }
 29484  29431     *pA += iB;
 29485  29432     return 0; 
 29486  29433   #endif
 29487  29434   }
 29488  29435   SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
 29489         -#if GCC_VERSION>=5004000
        29436  +#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
 29490  29437     return __builtin_sub_overflow(*pA, iB, pA);
 29491  29438   #else
 29492  29439     testcase( iB==SMALLEST_INT64+1 );
 29493  29440     if( iB==SMALLEST_INT64 ){
 29494  29441       testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
 29495  29442       if( (*pA)>=0 ) return 1;
 29496  29443       *pA -= iB;
................................................................................
 29497  29444       return 0;
 29498  29445     }else{
 29499  29446       return sqlite3AddInt64(pA, -iB);
 29500  29447     }
 29501  29448   #endif
 29502  29449   }
 29503  29450   SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
 29504         -#if GCC_VERSION>=5004000
        29451  +#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
 29505  29452     return __builtin_mul_overflow(*pA, iB, pA);
 29506  29453   #else
 29507  29454     i64 iA = *pA;
 29508  29455     if( iB>0 ){
 29509  29456       if( iA>LARGEST_INT64/iB ) return 1;
 29510  29457       if( iA<SMALLEST_INT64/iB ) return 1;
 29511  29458     }else if( iB<0 ){
................................................................................
 29599  29546   SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
 29600  29547     static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
 29601  29548     LogEst y = 40;
 29602  29549     if( x<8 ){
 29603  29550       if( x<2 ) return 0;
 29604  29551       while( x<8 ){  y -= 10; x <<= 1; }
 29605  29552     }else{
        29553  +#if GCC_VERSION>=5004000
        29554  +    int i = 60 - __builtin_clzll(x);
        29555  +    y += i*10;
        29556  +    x >>= i;
        29557  +#else
 29606  29558       while( x>255 ){ y += 40; x >>= 4; }  /*OPTIMIZATION-IF-TRUE*/
 29607  29559       while( x>15 ){  y += 10; x >>= 1; }
        29560  +#endif
 29608  29561     }
 29609  29562     return a[x&7] + y - 10;
 29610  29563   }
 29611  29564   
 29612  29565   #ifndef SQLITE_OMIT_VIRTUALTABLE
 29613  29566   /*
 29614  29567   ** Convert a double into a LogEst
................................................................................
 30081  30034       /*  36 */ "Sort"             OpHelp(""),
 30082  30035       /*  37 */ "Rewind"           OpHelp(""),
 30083  30036       /*  38 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
 30084  30037       /*  39 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
 30085  30038       /*  40 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
 30086  30039       /*  41 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
 30087  30040       /*  42 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
 30088         -    /*  43 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
 30089         -    /*  44 */ "Program"          OpHelp(""),
 30090         -    /*  45 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
 30091         -    /*  46 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
 30092         -    /*  47 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
 30093         -    /*  48 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
 30094         -    /*  49 */ "IncrVacuum"       OpHelp(""),
 30095         -    /*  50 */ "VNext"            OpHelp(""),
 30096         -    /*  51 */ "Init"             OpHelp("Start at P2"),
 30097         -    /*  52 */ "Return"           OpHelp(""),
 30098         -    /*  53 */ "EndCoroutine"     OpHelp(""),
 30099         -    /*  54 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
 30100         -    /*  55 */ "Halt"             OpHelp(""),
 30101         -    /*  56 */ "Integer"          OpHelp("r[P2]=P1"),
 30102         -    /*  57 */ "Int64"            OpHelp("r[P2]=P4"),
 30103         -    /*  58 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
 30104         -    /*  59 */ "Null"             OpHelp("r[P2..P3]=NULL"),
 30105         -    /*  60 */ "SoftNull"         OpHelp("r[P1]=NULL"),
 30106         -    /*  61 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
 30107         -    /*  62 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
 30108         -    /*  63 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
 30109         -    /*  64 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
 30110         -    /*  65 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
 30111         -    /*  66 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
 30112         -    /*  67 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
 30113         -    /*  68 */ "CollSeq"          OpHelp(""),
 30114         -    /*  69 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
 30115         -    /*  70 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
 30116         -    /*  71 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
 30117         -    /*  72 */ "RealAffinity"     OpHelp(""),
 30118         -    /*  73 */ "Cast"             OpHelp("affinity(r[P1])"),
 30119         -    /*  74 */ "Permutation"      OpHelp(""),
 30120         -    /*  75 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
 30121         -    /*  76 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
 30122         -    /*  77 */ "Ne"               OpHelp("IF r[P3]!=r[P1]"),
 30123         -    /*  78 */ "Eq"               OpHelp("IF r[P3]==r[P1]"),
 30124         -    /*  79 */ "Gt"               OpHelp("IF r[P3]>r[P1]"),
 30125         -    /*  80 */ "Le"               OpHelp("IF r[P3]<=r[P1]"),
 30126         -    /*  81 */ "Lt"               OpHelp("IF r[P3]<r[P1]"),
 30127         -    /*  82 */ "Ge"               OpHelp("IF r[P3]>=r[P1]"),
 30128         -    /*  83 */ "ElseNotEq"        OpHelp(""),
        30041  +    /*  43 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
        30042  +    /*  44 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
        30043  +    /*  45 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
        30044  +    /*  46 */ "Program"          OpHelp(""),
        30045  +    /*  47 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
        30046  +    /*  48 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
        30047  +    /*  49 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
        30048  +    /*  50 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
        30049  +    /*  51 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
        30050  +    /*  52 */ "Ne"               OpHelp("IF r[P3]!=r[P1]"),
        30051  +    /*  53 */ "Eq"               OpHelp("IF r[P3]==r[P1]"),
        30052  +    /*  54 */ "Gt"               OpHelp("IF r[P3]>r[P1]"),
        30053  +    /*  55 */ "Le"               OpHelp("IF r[P3]<=r[P1]"),
        30054  +    /*  56 */ "Lt"               OpHelp("IF r[P3]<r[P1]"),
        30055  +    /*  57 */ "Ge"               OpHelp("IF r[P3]>=r[P1]"),
        30056  +    /*  58 */ "ElseNotEq"        OpHelp(""),
        30057  +    /*  59 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
        30058  +    /*  60 */ "IncrVacuum"       OpHelp(""),
        30059  +    /*  61 */ "VNext"            OpHelp(""),
        30060  +    /*  62 */ "Init"             OpHelp("Start at P2"),
        30061  +    /*  63 */ "Return"           OpHelp(""),
        30062  +    /*  64 */ "EndCoroutine"     OpHelp(""),
        30063  +    /*  65 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
        30064  +    /*  66 */ "Halt"             OpHelp(""),
        30065  +    /*  67 */ "Integer"          OpHelp("r[P2]=P1"),
        30066  +    /*  68 */ "Int64"            OpHelp("r[P2]=P4"),
        30067  +    /*  69 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
        30068  +    /*  70 */ "Null"             OpHelp("r[P2..P3]=NULL"),
        30069  +    /*  71 */ "SoftNull"         OpHelp("r[P1]=NULL"),
        30070  +    /*  72 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
        30071  +    /*  73 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
        30072  +    /*  74 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
        30073  +    /*  75 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
        30074  +    /*  76 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
        30075  +    /*  77 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
        30076  +    /*  78 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
        30077  +    /*  79 */ "CollSeq"          OpHelp(""),
        30078  +    /*  80 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
        30079  +    /*  81 */ "RealAffinity"     OpHelp(""),
        30080  +    /*  82 */ "Cast"             OpHelp("affinity(r[P1])"),
        30081  +    /*  83 */ "Permutation"      OpHelp(""),
 30129  30082       /*  84 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
 30130  30083       /*  85 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
 30131  30084       /*  86 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
 30132  30085       /*  87 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
 30133  30086       /*  88 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
 30134  30087       /*  89 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 30135  30088       /*  90 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
................................................................................
 30163  30116       /* 118 */ "Delete"           OpHelp(""),
 30164  30117       /* 119 */ "ResetCount"       OpHelp(""),
 30165  30118       /* 120 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 30166  30119       /* 121 */ "SorterData"       OpHelp("r[P2]=data"),
 30167  30120       /* 122 */ "RowData"          OpHelp("r[P2]=data"),
 30168  30121       /* 123 */ "Rowid"            OpHelp("r[P2]=rowid"),
 30169  30122       /* 124 */ "NullRow"          OpHelp(""),
 30170         -    /* 125 */ "SorterInsert"     OpHelp("key=r[P2]"),
 30171         -    /* 126 */ "IdxInsert"        OpHelp("key=r[P2]"),
 30172         -    /* 127 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 30173         -    /* 128 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 30174         -    /* 129 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 30175         -    /* 130 */ "Destroy"          OpHelp(""),
 30176         -    /* 131 */ "Clear"            OpHelp(""),
        30123  +    /* 125 */ "SeekEnd"          OpHelp(""),
        30124  +    /* 126 */ "SorterInsert"     OpHelp("key=r[P2]"),
        30125  +    /* 127 */ "IdxInsert"        OpHelp("key=r[P2]"),
        30126  +    /* 128 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        30127  +    /* 129 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
        30128  +    /* 130 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        30129  +    /* 131 */ "Destroy"          OpHelp(""),
 30177  30130       /* 132 */ "Real"             OpHelp("r[P2]=P4"),
 30178         -    /* 133 */ "ResetSorter"      OpHelp(""),
 30179         -    /* 134 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
 30180         -    /* 135 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
        30131  +    /* 133 */ "Clear"            OpHelp(""),
        30132  +    /* 134 */ "ResetSorter"      OpHelp(""),
        30133  +    /* 135 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
 30181  30134       /* 136 */ "SqlExec"          OpHelp(""),
 30182  30135       /* 137 */ "ParseSchema"      OpHelp(""),
 30183  30136       /* 138 */ "LoadAnalysis"     OpHelp(""),
 30184  30137       /* 139 */ "DropTable"        OpHelp(""),
 30185  30138       /* 140 */ "DropIndex"        OpHelp(""),
 30186  30139       /* 141 */ "DropTrigger"      OpHelp(""),
 30187  30140       /* 142 */ "IntegrityCk"      OpHelp(""),
................................................................................
 30305  30258   
 30306  30259   /*
 30307  30260   ** standard include files.
 30308  30261   */
 30309  30262   #include <sys/types.h>
 30310  30263   #include <sys/stat.h>
 30311  30264   #include <fcntl.h>
        30265  +#include <sys/ioctl.h>
 30312  30266   #include <unistd.h>
 30313  30267   /* #include <time.h> */
 30314  30268   #include <sys/time.h>
 30315  30269   #include <errno.h>
 30316  30270   #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
 30317  30271   # include <sys/mman.h>
 30318  30272   #endif
 30319  30273   
 30320  30274   #if SQLITE_ENABLE_LOCKING_STYLE
 30321         -# include <sys/ioctl.h>
        30275  +/* # include <sys/ioctl.h> */
 30322  30276   # include <sys/file.h>
 30323  30277   # include <sys/param.h>
 30324  30278   #endif /* SQLITE_ENABLE_LOCKING_STYLE */
 30325  30279   
 30326  30280   #if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
 30327  30281                              (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
 30328  30282   #  if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
................................................................................
 30424  30378     sqlite3_vfs *pVfs;                  /* The VFS that created this unixFile */
 30425  30379     unixInodeInfo *pInode;              /* Info about locks on this inode */
 30426  30380     int h;                              /* The file descriptor */
 30427  30381     unsigned char eFileLock;            /* The type of lock held on this fd */
 30428  30382     unsigned short int ctrlFlags;       /* Behavioral bits.  UNIXFILE_* flags */
 30429  30383     int lastErrno;                      /* The unix errno from last I/O error */
 30430  30384     void *lockingContext;               /* Locking style specific state */
 30431         -  UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
        30385  +  UnixUnusedFd *pPreallocatedUnused;  /* Pre-allocated UnixUnusedFd */
 30432  30386     const char *zPath;                  /* Name of the file */
 30433  30387     unixShm *pShm;                      /* Shared memory segment information */
 30434  30388     int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
 30435  30389   #if SQLITE_MAX_MMAP_SIZE>0
 30436  30390     int nFetchOut;                      /* Number of outstanding xFetch refs */
 30437  30391     sqlite3_int64 mmapSize;             /* Usable size of mapping at pMapRegion */
 30438  30392     sqlite3_int64 mmapSizeActual;       /* Actual size of mapping at pMapRegion */
 30439  30393     sqlite3_int64 mmapSizeMax;          /* Configured FCNTL_MMAP_SIZE value */
 30440  30394     void *pMapRegion;                   /* Memory mapped region */
 30441  30395   #endif
 30442         -#ifdef __QNXNTO__
 30443  30396     int sectorSize;                     /* Device sector size */
 30444  30397     int deviceCharacteristics;          /* Precomputed device characteristics */
 30445         -#endif
 30446  30398   #if SQLITE_ENABLE_LOCKING_STYLE
 30447  30399     int openFlags;                      /* The flags specified at open() */
 30448  30400   #endif
 30449  30401   #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
 30450  30402     unsigned fsFlags;                   /* cached details from statfs() */
 30451  30403   #endif
 30452  30404   #if OS_VXWORKS
................................................................................
 30740  30692   /*
 30741  30693   ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
 30742  30694   ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
 30743  30695   */
 30744  30696   #ifdef __ANDROID__
 30745  30697   # define lseek lseek64
 30746  30698   #endif
        30699  +
        30700  +#ifdef __linux__
        30701  +/*
        30702  +** Linux-specific IOCTL magic numbers used for controlling F2FS
        30703  +*/
        30704  +#define F2FS_IOCTL_MAGIC        0xf5
        30705  +#define F2FS_IOC_START_ATOMIC_WRITE     _IO(F2FS_IOCTL_MAGIC, 1)
        30706  +#define F2FS_IOC_COMMIT_ATOMIC_WRITE    _IO(F2FS_IOCTL_MAGIC, 2)
        30707  +#define F2FS_IOC_START_VOLATILE_WRITE   _IO(F2FS_IOCTL_MAGIC, 3)
        30708  +#define F2FS_IOC_ABORT_VOLATILE_WRITE   _IO(F2FS_IOCTL_MAGIC, 5)
        30709  +#define F2FS_IOC_GET_FEATURES           _IOR(F2FS_IOCTL_MAGIC, 12, u32)
        30710  +#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
        30711  +#endif /* __linux__ */
        30712  +
 30747  30713   
 30748  30714   /*
 30749  30715   ** Different Unix systems declare open() in different ways.  Same use
 30750  30716   ** open(const char*,int,mode_t).  Others use open(const char*,int,...).
 30751  30717   ** The difference is important when using a pointer to the function.
 30752  30718   **
 30753  30719   ** The safest way to deal with the problem is to always use this wrapper
................................................................................
 30912  30878   
 30913  30879   #if defined(HAVE_LSTAT)
 30914  30880     { "lstat",         (sqlite3_syscall_ptr)lstat,          0 },
 30915  30881   #else
 30916  30882     { "lstat",         (sqlite3_syscall_ptr)0,              0 },
 30917  30883   #endif
 30918  30884   #define osLstat      ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
        30885  +
        30886  +  { "ioctl",         (sqlite3_syscall_ptr)ioctl,          0 },
        30887  +#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
 30919  30888   
 30920  30889   }; /* End of the overrideable system calls */
 30921  30890   
 30922  30891   
 30923  30892   /*
 30924  30893   ** On some systems, calls to fchown() will trigger a message in a security
 30925  30894   ** log if they come from non-root processes.  So avoid calling fchown() if
................................................................................
 31517  31486     char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
 31518  31487   #endif
 31519  31488   };
 31520  31489   
 31521  31490   /*
 31522  31491   ** A lists of all unixInodeInfo objects.
 31523  31492   */
 31524         -static unixInodeInfo *inodeList = 0;
        31493  +static unixInodeInfo *inodeList = 0;  /* All unixInodeInfo objects */
        31494  +static unsigned int nUnusedFd = 0;    /* Total unused file descriptors */
 31525  31495   
 31526  31496   /*
 31527  31497   **
 31528  31498   ** This function - unixLogErrorAtLine(), is only ever called via the macro
 31529  31499   ** unixLogError().
 31530  31500   **
 31531  31501   ** It is invoked after an error occurs in an OS function and errno has been
................................................................................
 31627  31597     unixInodeInfo *pInode = pFile->pInode;
 31628  31598     UnixUnusedFd *p;
 31629  31599     UnixUnusedFd *pNext;
 31630  31600     for(p=pInode->pUnused; p; p=pNext){
 31631  31601       pNext = p->pNext;
 31632  31602       robust_close(pFile, p->fd, __LINE__);
 31633  31603       sqlite3_free(p);
        31604  +    nUnusedFd--;
 31634  31605     }
 31635  31606     pInode->pUnused = 0;
 31636  31607   }
 31637  31608   
 31638  31609   /*
 31639  31610   ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
 31640  31611   **
................................................................................
 31659  31630         if( pInode->pNext ){
 31660  31631           assert( pInode->pNext->pPrev==pInode );
 31661  31632           pInode->pNext->pPrev = pInode->pPrev;
 31662  31633         }
 31663  31634         sqlite3_free(pInode);
 31664  31635       }
 31665  31636     }
        31637  +  assert( inodeList!=0 || nUnusedFd==0 );
 31666  31638   }
 31667  31639   
 31668  31640   /*
 31669  31641   ** Given a file descriptor, locate the unixInodeInfo object that
 31670  31642   ** describes that file descriptor.  Create a new one if necessary.  The
 31671  31643   ** return value might be uninitialized if an error occurs.
 31672  31644   **
................................................................................
 31728  31700     memset(&fileId, 0, sizeof(fileId));
 31729  31701     fileId.dev = statbuf.st_dev;
 31730  31702   #if OS_VXWORKS
 31731  31703     fileId.pId = pFile->pId;
 31732  31704   #else
 31733  31705     fileId.ino = (u64)statbuf.st_ino;
 31734  31706   #endif
        31707  +  assert( inodeList!=0 || nUnusedFd==0 );
 31735  31708     pInode = inodeList;
 31736  31709     while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
 31737  31710       pInode = pInode->pNext;
 31738  31711     }
 31739  31712     if( pInode==0 ){
 31740  31713       pInode = sqlite3_malloc64( sizeof(*pInode) );
 31741  31714       if( pInode==0 ){
................................................................................
 32147  32120   
 32148  32121   /*
 32149  32122   ** Add the file descriptor used by file handle pFile to the corresponding
 32150  32123   ** pUnused list.
 32151  32124   */
 32152  32125   static void setPendingFd(unixFile *pFile){
 32153  32126     unixInodeInfo *pInode = pFile->pInode;
 32154         -  UnixUnusedFd *p = pFile->pUnused;
        32127  +  UnixUnusedFd *p = pFile->pPreallocatedUnused;
 32155  32128     p->pNext = pInode->pUnused;
 32156  32129     pInode->pUnused = p;
 32157  32130     pFile->h = -1;
 32158         -  pFile->pUnused = 0;
        32131  +  pFile->pPreallocatedUnused = 0;
        32132  +  nUnusedFd++;
 32159  32133   }
 32160  32134   
 32161  32135   /*
 32162  32136   ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 32163  32137   ** must be either NO_LOCK or SHARED_LOCK.
 32164  32138   **
 32165  32139   ** If the locking level of the file descriptor is already at or below
................................................................................
 32376  32350       osUnlink(pFile->zPath);
 32377  32351       sqlite3_free(*(char**)&pFile->zPath);
 32378  32352       pFile->zPath = 0;
 32379  32353     }
 32380  32354   #endif
 32381  32355     OSTRACE(("CLOSE   %-3d\n", pFile->h));
 32382  32356     OpenCounter(-1);
 32383         -  sqlite3_free(pFile->pUnused);
        32357  +  sqlite3_free(pFile->pPreallocatedUnused);
 32384  32358     memset(pFile, 0, sizeof(unixFile));
 32385  32359     return SQLITE_OK;
 32386  32360   }
 32387  32361   
 32388  32362   /*
 32389  32363   ** Close a file.
 32390  32364   */
................................................................................
 32713  32687           rc = lrc;
 32714  32688         }
 32715  32689       }
 32716  32690     }
 32717  32691     OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
 32718  32692   
 32719  32693   #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 32720         -  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
        32694  +  if( (rc & 0xff) == SQLITE_IOERR ){
 32721  32695       rc = SQLITE_OK;
 32722  32696       reserved=1;
 32723  32697     }
 32724  32698   #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 32725  32699     *pResOut = reserved;
 32726  32700     return rc;
 32727  32701   }
................................................................................
 32780  32754     } else {
 32781  32755       /* got it, set the type and return ok */
 32782  32756       pFile->eFileLock = eFileLock;
 32783  32757     }
 32784  32758     OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), 
 32785  32759              rc==SQLITE_OK ? "ok" : "failed"));
 32786  32760   #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 32787         -  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
        32761  +  if( (rc & 0xff) == SQLITE_IOERR ){
 32788  32762       rc = SQLITE_BUSY;
 32789  32763     }
 32790  32764   #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 32791  32765     return rc;
 32792  32766   }
 32793  32767   
 32794  32768   
................................................................................
 33317  33291           failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, 
 33318  33292                                  SHARED_SIZE, 1);
 33319  33293           if( failed && (failed2 = afpSetLock(context->dbPath, pFile, 
 33320  33294                          SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
 33321  33295             /* Can't reestablish the shared lock.  Sqlite can't deal, this is
 33322  33296             ** a critical I/O error
 33323  33297             */
 33324         -          rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : 
        33298  +          rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 : 
 33325  33299                  SQLITE_IOERR_LOCK;
 33326  33300             goto afp_end_lock;
 33327  33301           } 
 33328  33302         }else{
 33329  33303           rc = failed; 
 33330  33304         }
 33331  33305       }
................................................................................
 33597  33571     assert( id );
 33598  33572     assert( offset>=0 );
 33599  33573     assert( amt>0 );
 33600  33574   
 33601  33575     /* If this is a database file (not a journal, master-journal or temp
 33602  33576     ** file), the bytes in the locking range should never be read or written. */
 33603  33577   #if 0
 33604         -  assert( pFile->pUnused==0
        33578  +  assert( pFile->pPreallocatedUnused==0
 33605  33579          || offset>=PENDING_BYTE+512
 33606  33580          || offset+amt<=PENDING_BYTE 
 33607  33581     );
 33608  33582   #endif
 33609  33583   
 33610  33584   #if SQLITE_MAX_MMAP_SIZE>0
 33611  33585     /* Deal with as much of this read request as possible by transfering
................................................................................
 33710  33684     int wrote = 0;
 33711  33685     assert( id );
 33712  33686     assert( amt>0 );
 33713  33687   
 33714  33688     /* If this is a database file (not a journal, master-journal or temp
 33715  33689     ** file), the bytes in the locking range should never be read or written. */
 33716  33690   #if 0
 33717         -  assert( pFile->pUnused==0
        33691  +  assert( pFile->pPreallocatedUnused==0
 33718  33692          || offset>=PENDING_BYTE+512
 33719  33693          || offset+amt<=PENDING_BYTE 
 33720  33694     );
 33721  33695   #endif
 33722  33696   
 33723  33697   #ifdef SQLITE_DEBUG
 33724  33698     /* If we are doing a normal write to a database file (as opposed to
................................................................................
 34190  34164   
 34191  34165   /*
 34192  34166   ** Information and control of an open file handle.
 34193  34167   */
 34194  34168   static int unixFileControl(sqlite3_file *id, int op, void *pArg){
 34195  34169     unixFile *pFile = (unixFile*)id;
 34196  34170     switch( op ){
        34171  +#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
        34172  +    case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: {
        34173  +      int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
        34174  +      return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK;
        34175  +    }
        34176  +    case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: {
        34177  +      int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
        34178  +      return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK;
        34179  +    }
        34180  +    case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: {
        34181  +      int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
        34182  +      return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK;
        34183  +    }
        34184  +#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
        34185  +
 34197  34186       case SQLITE_FCNTL_LOCKSTATE: {
 34198  34187         *(int*)pArg = pFile->eFileLock;
 34199  34188         return SQLITE_OK;
 34200  34189       }
 34201  34190       case SQLITE_FCNTL_LAST_ERRNO: {
 34202  34191         *(int*)pArg = pFile->lastErrno;
 34203  34192         return SQLITE_OK;
................................................................................
 34240  34229   #if SQLITE_MAX_MMAP_SIZE>0
 34241  34230       case SQLITE_FCNTL_MMAP_SIZE: {
 34242  34231         i64 newLimit = *(i64*)pArg;
 34243  34232         int rc = SQLITE_OK;
 34244  34233         if( newLimit>sqlite3GlobalConfig.mxMmap ){
 34245  34234           newLimit = sqlite3GlobalConfig.mxMmap;
 34246  34235         }
        34236  +
        34237  +      /* The value of newLimit may be eventually cast to (size_t) and passed
        34238  +      ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
        34239  +      ** 64-bit type. */
        34240  +      if( newLimit>0 && sizeof(size_t)<8 ){
        34241  +        newLimit = (newLimit & 0x7FFFFFFF);
        34242  +      }
        34243  +
 34247  34244         *(i64*)pArg = pFile->mmapSizeMax;
 34248  34245         if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
 34249  34246           pFile->mmapSizeMax = newLimit;
 34250  34247           if( pFile->mmapSize>0 ){
 34251  34248             unixUnmapfile(pFile);
 34252  34249             rc = unixMapfile(pFile, -1);
 34253  34250           }
................................................................................
 34273  34270       }
 34274  34271   #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
 34275  34272     }
 34276  34273     return SQLITE_NOTFOUND;
 34277  34274   }
 34278  34275   
 34279  34276   /*
 34280         -** Return the sector size in bytes of the underlying block device for
 34281         -** the specified file. This is almost always 512 bytes, but may be
 34282         -** larger for some devices.
 34283         -**
 34284         -** SQLite code assumes this function cannot fail. It also assumes that
 34285         -** if two files are created in the same file-system directory (i.e.
 34286         -** a database and its journal file) that the sector size will be the
 34287         -** same for both.
 34288         -*/
 34289         -#ifndef __QNXNTO__ 
 34290         -static int unixSectorSize(sqlite3_file *NotUsed){
 34291         -  UNUSED_PARAMETER(NotUsed);
 34292         -  return SQLITE_DEFAULT_SECTOR_SIZE;
 34293         -}
 34294         -#endif
 34295         -
 34296         -/*
 34297         -** The following version of unixSectorSize() is optimized for QNX.
 34298         -*/
 34299         -#ifdef __QNXNTO__
        34277  +** If pFd->sectorSize is non-zero when this function is called, it is a
        34278  +** no-op. Otherwise, the values of pFd->sectorSize and 
        34279  +** pFd->deviceCharacteristics are set according to the file-system 
        34280  +** characteristics. 
        34281  +**
        34282  +** There are two versions of this function. One for QNX and one for all
        34283  +** other systems.
        34284  +*/
        34285  +#ifndef __QNXNTO__
        34286  +static void setDeviceCharacteristics(unixFile *pFd){
        34287  +  assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
        34288  +  if( pFd->sectorSize==0 ){
        34289  +#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
        34290  +    int res;
        34291  +    u32 f = 0;
        34292  +
        34293  +    /* Check for support for F2FS atomic batch writes. */
        34294  +    res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
        34295  +    if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){
        34296  +      pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
        34297  +    }
        34298  +#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
        34299  +
        34300  +    /* Set the POWERSAFE_OVERWRITE flag if requested. */
        34301  +    if( pFd->ctrlFlags & UNIXFILE_PSOW ){
        34302  +      pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
        34303  +    }
        34304  +
        34305  +    pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
        34306  +  }
        34307  +}
        34308  +#else
 34300  34309   #include <sys/dcmd_blk.h>
 34301  34310   #include <sys/statvfs.h>
 34302         -static int unixSectorSize(sqlite3_file *id){
 34303         -  unixFile *pFile = (unixFile*)id;
        34311  +static void setDeviceCharacteristics(unixFile *pFile){
 34304  34312     if( pFile->sectorSize == 0 ){
 34305  34313       struct statvfs fsInfo;
 34306  34314          
 34307  34315       /* Set defaults for non-supported filesystems */
 34308  34316       pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
 34309  34317       pFile->deviceCharacteristics = 0;
 34310  34318       if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
................................................................................
 34365  34373     }
 34366  34374     /* Last chance verification.  If the sector size isn't a multiple of 512
 34367  34375     ** then it isn't valid.*/
 34368  34376     if( pFile->sectorSize % 512 != 0 ){
 34369  34377       pFile->deviceCharacteristics = 0;
 34370  34378       pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
 34371  34379     }
 34372         -  return pFile->sectorSize;
 34373  34380   }
 34374         -#endif /* __QNXNTO__ */
        34381  +#endif
        34382  +
        34383  +/*
        34384  +** Return the sector size in bytes of the underlying block device for
        34385  +** the specified file. This is almost always 512 bytes, but may be
        34386  +** larger for some devices.
        34387  +**
        34388  +** SQLite code assumes this function cannot fail. It also assumes that
        34389  +** if two files are created in the same file-system directory (i.e.
        34390  +** a database and its journal file) that the sector size will be the
        34391  +** same for both.
        34392  +*/
        34393  +static int unixSectorSize(sqlite3_file *id){
        34394  +  unixFile *pFd = (unixFile*)id;
        34395  +  setDeviceCharacteristics(pFd);
        34396  +  return pFd->sectorSize;
        34397  +}
 34375  34398   
 34376  34399   /*
 34377  34400   ** Return the device characteristics for the file.
 34378  34401   **
 34379  34402   ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
 34380  34403   ** However, that choice is controversial since technically the underlying
 34381  34404   ** file system does not always provide powersafe overwrites.  (In other
................................................................................
 34383  34406   ** written might end up being altered.)  However, non-PSOW behavior is very,
 34384  34407   ** very rare.  And asserting PSOW makes a large reduction in the amount
 34385  34408   ** of required I/O for journaling, since a lot of padding is eliminated.
 34386  34409   **  Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
 34387  34410   ** available to turn it off and URI query parameter available to turn it off.
 34388  34411   */
 34389  34412   static int unixDeviceCharacteristics(sqlite3_file *id){
 34390         -  unixFile *p = (unixFile*)id;
 34391         -  int rc = 0;
 34392         -#ifdef __QNXNTO__
 34393         -  if( p->sectorSize==0 ) unixSectorSize(id);
 34394         -  rc = p->deviceCharacteristics;
 34395         -#endif
 34396         -  if( p->ctrlFlags & UNIXFILE_PSOW ){
 34397         -    rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
 34398         -  }
 34399         -  return rc;
        34413  +  unixFile *pFd = (unixFile*)id;
        34414  +  setDeviceCharacteristics(pFd);
        34415  +  return pFd->deviceCharacteristics;
 34400  34416   }
 34401  34417   
 34402  34418   #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
 34403  34419   
 34404  34420   /*
 34405  34421   ** Return the system page size.
 34406  34422   **
................................................................................
 35650  35666   ){
 35651  35667     const sqlite3_io_methods *pLockingStyle;
 35652  35668     unixFile *pNew = (unixFile *)pId;
 35653  35669     int rc = SQLITE_OK;
 35654  35670   
 35655  35671     assert( pNew->pInode==NULL );
 35656  35672   
 35657         -  /* Usually the path zFilename should not be a relative pathname. The
 35658         -  ** exception is when opening the proxy "conch" file in builds that
 35659         -  ** include the special Apple locking styles.
 35660         -  */
 35661         -#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 35662         -  assert( zFilename==0 || zFilename[0]=='/' 
 35663         -    || pVfs->pAppData==(void*)&autolockIoFinder );
 35664         -#else
 35665         -  assert( zFilename==0 || zFilename[0]=='/' );
 35666         -#endif
 35667         -
 35668  35673     /* No locking occurs in temporary files */
 35669  35674     assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
 35670  35675   
 35671  35676     OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
 35672  35677     pNew->h = h;
 35673  35678     pNew->pVfs = pVfs;
 35674  35679     pNew->zPath = zFilename;
................................................................................
 35918  35923     /* Do not search for an unused file descriptor on vxworks. Not because
 35919  35924     ** vxworks would not benefit from the change (it might, we're not sure),
 35920  35925     ** but because no way to test it is currently available. It is better 
 35921  35926     ** not to risk breaking vxworks support for the sake of such an obscure 
 35922  35927     ** feature.  */
 35923  35928   #if !OS_VXWORKS
 35924  35929     struct stat sStat;                   /* Results of stat() call */
        35930  +
        35931  +  unixEnterMutex();
 35925  35932   
 35926  35933     /* A stat() call may fail for various reasons. If this happens, it is
 35927  35934     ** almost certain that an open() call on the same path will also fail.
 35928  35935     ** For this reason, if an error occurs in the stat() call here, it is
 35929  35936     ** ignored and -1 is returned. The caller will try to open a new file
 35930  35937     ** descriptor on the same path, fail, and return an error to SQLite.
 35931  35938     **
 35932  35939     ** Even if a subsequent open() call does succeed, the consequences of
 35933  35940     ** not searching for a reusable file descriptor are not dire.  */
 35934         -  if( 0==osStat(zPath, &sStat) ){
        35941  +  if( nUnusedFd>0 && 0==osStat(zPath, &sStat) ){
 35935  35942       unixInodeInfo *pInode;
 35936  35943   
 35937         -    unixEnterMutex();
 35938  35944       pInode = inodeList;
 35939  35945       while( pInode && (pInode->fileId.dev!=sStat.st_dev
 35940  35946                        || pInode->fileId.ino!=(u64)sStat.st_ino) ){
 35941  35947          pInode = pInode->pNext;
 35942  35948       }
 35943  35949       if( pInode ){
 35944  35950         UnixUnusedFd **pp;
 35945  35951         for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
 35946  35952         pUnused = *pp;
 35947  35953         if( pUnused ){
        35954  +        nUnusedFd--;
 35948  35955           *pp = pUnused->pNext;
 35949  35956         }
 35950  35957       }
 35951         -    unixLeaveMutex();
 35952  35958     }
        35959  +  unixLeaveMutex();
 35953  35960   #endif    /* if !OS_VXWORKS */
 35954  35961     return pUnused;
 35955  35962   }
 35956  35963   
 35957  35964   /*
 35958  35965   ** Find the mode, uid and gid of file zFile. 
 35959  35966   */
................................................................................
 36021  36028       **   "<path to db>-walNN"
 36022  36029       **
 36023  36030       ** where NN is a decimal number. The NN naming schemes are 
 36024  36031       ** used by the test_multiplex.c module.
 36025  36032       */
 36026  36033       nDb = sqlite3Strlen30(zPath) - 1; 
 36027  36034       while( zPath[nDb]!='-' ){
 36028         -#ifndef SQLITE_ENABLE_8_3_NAMES
 36029         -      /* In the normal case (8+3 filenames disabled) the journal filename
 36030         -      ** is guaranteed to contain a '-' character. */
 36031         -      assert( nDb>0 );
 36032         -      assert( sqlite3Isalnum(zPath[nDb]) );
 36033         -#else
 36034         -      /* If 8+3 names are possible, then the journal file might not contain
 36035         -      ** a '-' character.  So check for that case and return early. */
        36035  +      /* In normal operation, the journal file name will always contain
        36036  +      ** a '-' character.  However in 8+3 filename mode, or if a corrupt
        36037  +      ** rollback journal specifies a master journal with a goofy name, then
        36038  +      ** the '-' might be missing. */
 36036  36039         if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
 36037         -#endif
 36038  36040         nDb--;
 36039  36041       }
 36040  36042       memcpy(zDb, zPath, nDb);
 36041  36043       zDb[nDb] = '\0';
 36042  36044   
 36043  36045       rc = getFileMode(zDb, pMode, pUid, pGid);
 36044  36046     }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
................................................................................
 36166  36168         fd = pUnused->fd;
 36167  36169       }else{
 36168  36170         pUnused = sqlite3_malloc64(sizeof(*pUnused));
 36169  36171         if( !pUnused ){
 36170  36172           return SQLITE_NOMEM_BKPT;
 36171  36173         }
 36172  36174       }
 36173         -    p->pUnused = pUnused;
        36175  +    p->pPreallocatedUnused = pUnused;
 36174  36176   
 36175  36177       /* Database filenames are double-zero terminated if they are not
 36176  36178       ** URIs with parameters.  Hence, they can always be passed into
 36177  36179       ** sqlite3_uri_parameter(). */
 36178  36180       assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
 36179  36181   
 36180  36182     }else if( !zName ){
................................................................................
 36203  36205   
 36204  36206     if( fd<0 ){
 36205  36207       mode_t openMode;              /* Permissions to create file with */
 36206  36208       uid_t uid;                    /* Userid for the file */
 36207  36209       gid_t gid;                    /* Groupid for the file */
 36208  36210       rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
 36209  36211       if( rc!=SQLITE_OK ){
 36210         -      assert( !p->pUnused );
        36212  +      assert( !p->pPreallocatedUnused );
 36211  36213         assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
 36212  36214         return rc;
 36213  36215       }
 36214  36216       fd = robust_open(zName, openFlags, openMode);
 36215  36217       OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
 36216  36218       assert( !isExclusive || (openFlags & O_CREAT)!=0 );
 36217  36219       if( fd<0 && errno!=EISDIR && isReadWrite ){
................................................................................
 36237  36239       }
 36238  36240     }
 36239  36241     assert( fd>=0 );
 36240  36242     if( pOutFlags ){
 36241  36243       *pOutFlags = flags;
 36242  36244     }
 36243  36245   
 36244         -  if( p->pUnused ){
 36245         -    p->pUnused->fd = fd;
 36246         -    p->pUnused->flags = flags;
        36246  +  if( p->pPreallocatedUnused ){
        36247  +    p->pPreallocatedUnused->fd = fd;
        36248  +    p->pPreallocatedUnused->flags = flags;
 36247  36249     }
 36248  36250   
 36249  36251     if( isDelete ){
 36250  36252   #if OS_VXWORKS
 36251  36253       zPath = zName;
 36252  36254   #elif defined(SQLITE_UNLINK_AFTER_CLOSE)
 36253  36255       zPath = sqlite3_mprintf("%s", zName);
................................................................................
 36316  36318           }
 36317  36319         }
 36318  36320         goto open_finished;
 36319  36321       }
 36320  36322     }
 36321  36323   #endif
 36322  36324     
        36325  +  assert( zPath==0 || zPath[0]=='/' 
        36326  +      || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL 
        36327  +  );
 36323  36328     rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
 36324  36329   
 36325  36330   open_finished:
 36326  36331     if( rc!=SQLITE_OK ){
 36327         -    sqlite3_free(p->pUnused);
        36332  +    sqlite3_free(p->pPreallocatedUnused);
 36328  36333     }
 36329  36334     return rc;
 36330  36335   }
 36331  36336   
 36332  36337   
 36333  36338   /*
 36334  36339   ** Delete the file at zPath. If the dirSync argument is true, fsync()
................................................................................
 37061  37066     memset(pNew, 0, sizeof(unixFile));
 37062  37067     pNew->openFlags = openFlags;
 37063  37068     memset(&dummyVfs, 0, sizeof(dummyVfs));
 37064  37069     dummyVfs.pAppData = (void*)&autolockIoFinder;
 37065  37070     dummyVfs.zName = "dummy";
 37066  37071     pUnused->fd = fd;
 37067  37072     pUnused->flags = openFlags;
 37068         -  pNew->pUnused = pUnused;
        37073  +  pNew->pPreallocatedUnused = pUnused;
 37069  37074     
 37070  37075     rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
 37071  37076     if( rc==SQLITE_OK ){
 37072  37077       *ppFile = pNew;
 37073  37078       return SQLITE_OK;
 37074  37079     }
 37075  37080   end_create_proxy:    
................................................................................
 38011  38016       UNIXVFS("unix-proxy",    proxyIoFinder ),
 38012  38017   #endif
 38013  38018     };
 38014  38019     unsigned int i;          /* Loop counter */
 38015  38020   
 38016  38021     /* Double-check that the aSyscall[] array has been constructed
 38017  38022     ** correctly.  See ticket [bb3a86e890c8e96ab] */
 38018         -  assert( ArraySize(aSyscall)==28 );
        38023  +  assert( ArraySize(aSyscall)==29 );
 38019  38024   
 38020  38025     /* Register all VFSes defined in the aVfs[] array */
 38021  38026     for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
 38022  38027       sqlite3_vfs_register(&aVfs[i], i==0);
 38023  38028     }
 38024  38029     return SQLITE_OK; 
 38025  38030   }
................................................................................
 41794  41799   #if SQLITE_MAX_MMAP_SIZE>0
 41795  41800       case SQLITE_FCNTL_MMAP_SIZE: {
 41796  41801         i64 newLimit = *(i64*)pArg;
 41797  41802         int rc = SQLITE_OK;
 41798  41803         if( newLimit>sqlite3GlobalConfig.mxMmap ){
 41799  41804           newLimit = sqlite3GlobalConfig.mxMmap;
 41800  41805         }
        41806  +
        41807  +      /* The value of newLimit may be eventually cast to (SIZE_T) and passed
        41808  +      ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
        41809  +      ** least a 64-bit type. */
        41810  +      if( newLimit>0 && sizeof(SIZE_T)<8 ){
        41811  +        newLimit = (newLimit & 0x7FFFFFFF);
        41812  +      }
        41813  +
 41801  41814         *(i64*)pArg = pFile->mmapSizeMax;
 41802  41815         if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
 41803  41816           pFile->mmapSizeMax = newLimit;
 41804  41817           if( pFile->mmapSize>0 ){
 41805  41818             winUnmapfile(pFile);
 41806  41819             rc = winMapfile(pFile, -1);
 41807  41820           }
................................................................................
 43106  43119     }else{
 43107  43120       attr = osGetFileAttributesA((char*)zConverted);
 43108  43121   #endif
 43109  43122     }
 43110  43123     return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
 43111  43124   }
 43112  43125   
        43126  +/* forward reference */
        43127  +static int winAccess(
        43128  +  sqlite3_vfs *pVfs,         /* Not used on win32 */
        43129  +  const char *zFilename,     /* Name of file to check */
        43130  +  int flags,                 /* Type of test to make on this file */
        43131  +  int *pResOut               /* OUT: Result */
        43132  +);
        43133  +
 43113  43134   /*
 43114  43135   ** Open a file.
 43115  43136   */
 43116  43137   static int winOpen(
 43117  43138     sqlite3_vfs *pVfs,        /* Used to get maximum path length and AppData */
 43118  43139     const char *zName,        /* Name of the file (UTF-8) */
 43119  43140     sqlite3_file *id,         /* Write the SQLite file handle here */
................................................................................
 43282  43303       extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
 43283  43304       extendedParameters.dwFileAttributes =
 43284  43305               dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
 43285  43306       extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
 43286  43307       extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
 43287  43308       extendedParameters.lpSecurityAttributes = NULL;
 43288  43309       extendedParameters.hTemplateFile = NULL;
 43289         -    while( (h = osCreateFile2((LPCWSTR)zConverted,
 43290         -                              dwDesiredAccess,
 43291         -                              dwShareMode,
 43292         -                              dwCreationDisposition,
 43293         -                              &extendedParameters))==INVALID_HANDLE_VALUE &&
 43294         -                              winRetryIoerr(&cnt, &lastErrno) ){
 43295         -               /* Noop */
 43296         -    }
 43297         -#else
 43298         -    while( (h = osCreateFileW((LPCWSTR)zConverted,
 43299         -                              dwDesiredAccess,
 43300         -                              dwShareMode, NULL,
 43301         -                              dwCreationDisposition,
 43302         -                              dwFlagsAndAttributes,
 43303         -                              NULL))==INVALID_HANDLE_VALUE &&
 43304         -                              winRetryIoerr(&cnt, &lastErrno) ){
 43305         -               /* Noop */
 43306         -    }
        43310  +    do{
        43311  +      h = osCreateFile2((LPCWSTR)zConverted,
        43312  +                        dwDesiredAccess,
        43313  +                        dwShareMode,
        43314  +                        dwCreationDisposition,
        43315  +                        &extendedParameters);
        43316  +      if( h!=INVALID_HANDLE_VALUE ) break;
        43317  +      if( isReadWrite ){
        43318  +        int isRO = 0;
        43319  +        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        43320  +        if( rc2==SQLITE_OK && isRO ) break;
        43321  +      }
        43322  +    }while( winRetryIoerr(&cnt, &lastErrno) );
        43323  +#else
        43324  +    do{
        43325  +      h = osCreateFileW((LPCWSTR)zConverted,
        43326  +                        dwDesiredAccess,
        43327  +                        dwShareMode, NULL,
        43328  +                        dwCreationDisposition,
        43329  +                        dwFlagsAndAttributes,
        43330  +                        NULL);
        43331  +      if( h!=INVALID_HANDLE_VALUE ) break;
        43332  +      if( isReadWrite ){
        43333  +        int isRO = 0;
        43334  +        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        43335  +        if( rc2==SQLITE_OK && isRO ) break;
        43336  +      }
        43337  +    }while( winRetryIoerr(&cnt, &lastErrno) );
 43307  43338   #endif
 43308  43339     }
 43309  43340   #ifdef SQLITE_WIN32_HAS_ANSI
 43310  43341     else{
 43311         -    while( (h = osCreateFileA((LPCSTR)zConverted,
 43312         -                              dwDesiredAccess,
 43313         -                              dwShareMode, NULL,
 43314         -                              dwCreationDisposition,
 43315         -                              dwFlagsAndAttributes,
 43316         -                              NULL))==INVALID_HANDLE_VALUE &&
 43317         -                              winRetryIoerr(&cnt, &lastErrno) ){
 43318         -               /* Noop */
 43319         -    }
        43342  +    do{
        43343  +      h = osCreateFileA((LPCSTR)zConverted,
        43344  +                        dwDesiredAccess,
        43345  +                        dwShareMode, NULL,
        43346  +                        dwCreationDisposition,
        43347  +                        dwFlagsAndAttributes,
        43348  +                        NULL);
        43349  +      if( h!=INVALID_HANDLE_VALUE ) break;
        43350  +      if( isReadWrite ){
        43351  +        int isRO = 0;
        43352  +        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        43353  +        if( rc2==SQLITE_OK && isRO ) break;
        43354  +      }
        43355  +    }while( winRetryIoerr(&cnt, &lastErrno) );
 43320  43356     }
 43321  43357   #endif
 43322  43358     winLogIoerr(cnt, __LINE__);
 43323  43359   
 43324  43360     OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
 43325  43361              dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
 43326  43362   
 43327  43363     if( h==INVALID_HANDLE_VALUE ){
 43328         -    pFile->lastErrno = lastErrno;
 43329         -    winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
 43330  43364       sqlite3_free(zConverted);
 43331  43365       sqlite3_free(zTmpname);
 43332  43366       if( isReadWrite && !isExclusive ){
 43333  43367         return winOpen(pVfs, zName, id,
 43334  43368            ((flags|SQLITE_OPEN_READONLY) &
 43335  43369                        ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
 43336  43370            pOutFlags);
 43337  43371       }else{
        43372  +      pFile->lastErrno = lastErrno;
        43373  +      winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
 43338  43374         return SQLITE_CANTOPEN_BKPT;
 43339  43375       }
 43340  43376     }
 43341  43377   
 43342  43378     if( pOutFlags ){
 43343  43379       if( isReadWrite ){
 43344  43380         *pOutFlags = SQLITE_OPEN_READWRITE;
................................................................................
 43923  43959     UNUSED_PARAMETER(pVfs);
 43924  43960     memset(zBuf, 0, nBuf);
 43925  43961     return nBuf;
 43926  43962   #else
 43927  43963     EntropyGatherer e;
 43928  43964     UNUSED_PARAMETER(pVfs);
 43929  43965     memset(zBuf, 0, nBuf);
 43930         -#if defined(_MSC_VER) && _MSC_VER>=1400 && !SQLITE_OS_WINCE
 43931         -  rand_s((unsigned int*)zBuf); /* rand_s() is not available with MinGW */
 43932         -#endif /* defined(_MSC_VER) && _MSC_VER>=1400 */
 43933  43966     e.a = (unsigned char*)zBuf;
 43934  43967     e.na = nBuf;
 43935  43968     e.nXor = 0;
 43936  43969     e.i = 0;
 43937  43970     {
 43938  43971       SYSTEMTIME x;
 43939  43972       osGetSystemTime(&x);
................................................................................
 44844  44877         p->pDirty = pPage->pDirtyNext;
 44845  44878         assert( p->bPurgeable || p->eCreate==2 );
 44846  44879         if( p->pDirty==0 ){         /*OPTIMIZATION-IF-TRUE*/
 44847  44880           assert( p->bPurgeable==0 || p->eCreate==1 );
 44848  44881           p->eCreate = 2;
 44849  44882         }
 44850  44883       }
 44851         -    pPage->pDirtyNext = 0;
 44852         -    pPage->pDirtyPrev = 0;
 44853  44884     }
 44854  44885     if( addRemove & PCACHE_DIRTYLIST_ADD ){
 44855         -    assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
 44856         -  
        44886  +    pPage->pDirtyPrev = 0;
 44857  44887       pPage->pDirtyNext = p->pDirty;
 44858  44888       if( pPage->pDirtyNext ){
 44859  44889         assert( pPage->pDirtyNext->pDirtyPrev==0 );
 44860  44890         pPage->pDirtyNext->pDirtyPrev = pPage;
 44861  44891       }else{
 44862  44892         p->pDirtyTail = pPage;
 44863  44893         if( p->bPurgeable ){
................................................................................
 45166  45196   */
 45167  45197   SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
 45168  45198     assert( p->nRef>0 );
 45169  45199     p->pCache->nRefSum--;
 45170  45200     if( (--p->nRef)==0 ){
 45171  45201       if( p->flags&PGHDR_CLEAN ){
 45172  45202         pcacheUnpin(p);
 45173         -    }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/
 45174         -      /* Move the page to the head of the dirty list. If p->pDirtyPrev==0,
 45175         -      ** then page p is already at the head of the dirty list and the
 45176         -      ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE
 45177         -      ** tag above.  */
        45203  +    }else{
 45178  45204         pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
 45179  45205       }
 45180  45206     }
 45181  45207   }
 45182  45208   
 45183  45209   /*
 45184  45210   ** Increase the reference count of a supplied page by 1.
................................................................................
 45631  45657   ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
 45632  45658   ** PgHdr1.pCache->szPage bytes is allocated directly before this structure 
 45633  45659   ** in memory.
 45634  45660   */
 45635  45661   struct PgHdr1 {
 45636  45662     sqlite3_pcache_page page;      /* Base class. Must be first. pBuf & pExtra */
 45637  45663     unsigned int iKey;             /* Key value (page number) */
 45638         -  u8 isPinned;                   /* Page in use, not on the LRU list */
 45639  45664     u8 isBulkLocal;                /* This page from bulk local storage */
 45640  45665     u8 isAnchor;                   /* This is the PGroup.lru element */
 45641  45666     PgHdr1 *pNext;                 /* Next in hash table chain */
 45642  45667     PCache1 *pCache;               /* Cache that currently owns this page */
 45643  45668     PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
 45644  45669     PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
 45645  45670   };
 45646  45671   
        45672  +/*
        45673  +** A page is pinned if it is no on the LRU list
        45674  +*/
        45675  +#define PAGE_IS_PINNED(p)    ((p)->pLruNext==0)
        45676  +#define PAGE_IS_UNPINNED(p)  ((p)->pLruNext!=0)
        45677  +
 45647  45678   /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
 45648  45679   ** of one or more PCaches that are able to recycle each other's unpinned
 45649  45680   ** pages when they are under memory pressure.  A PGroup is an instance of
 45650  45681   ** the following object.
 45651  45682   **
 45652  45683   ** This page cache implementation works in one of two modes:
 45653  45684   **
................................................................................
 45667  45698   ** SQLITE_MUTEX_STATIC_LRU.
 45668  45699   */
 45669  45700   struct PGroup {
 45670  45701     sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
 45671  45702     unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
 45672  45703     unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
 45673  45704     unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
 45674         -  unsigned int nCurrentPage;     /* Number of purgeable pages allocated */
        45705  +  unsigned int nPurgeable;       /* Number of purgeable pages allocated */
 45675  45706     PgHdr1 lru;                    /* The beginning and end of the LRU list */
 45676  45707   };
 45677  45708   
 45678  45709   /* Each page cache is an instance of the following object.  Every
 45679  45710   ** open database file (including each in-memory database and each
 45680  45711   ** temporary or transient database) has a single page cache which
 45681  45712   ** is an instance of this object.
 45682  45713   **
 45683  45714   ** Pointers to structures of this type are cast and returned as 
 45684  45715   ** opaque sqlite3_pcache* handles.
 45685  45716   */
 45686  45717   struct PCache1 {
 45687  45718     /* Cache configuration parameters. Page size (szPage) and the purgeable
 45688         -  ** flag (bPurgeable) are set when the cache is created. nMax may be 
        45719  +  ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
        45720  +  ** cache is created and are never changed thereafter. nMax may be 
 45689  45721     ** modified at any time by a call to the pcache1Cachesize() method.
 45690  45722     ** The PGroup mutex must be held when accessing nMax.
 45691  45723     */
 45692  45724     PGroup *pGroup;                     /* PGroup this cache belongs to */
        45725  +  unsigned int *pnPurgeable;          /* Pointer to pGroup->nPurgeable */
 45693  45726     int szPage;                         /* Size of database content section */
 45694  45727     int szExtra;                        /* sizeof(MemPage)+sizeof(PgHdr) */
 45695  45728     int szAlloc;                        /* Total size of one pcache line */
 45696  45729     int bPurgeable;                     /* True if cache is purgeable */
 45697  45730     unsigned int nMin;                  /* Minimum number of pages reserved */
 45698  45731     unsigned int nMax;                  /* Configured "cache_size" value */
 45699  45732     unsigned int n90pct;                /* nMax*9/10 */
................................................................................
 45780  45813   ** This routine is called from sqlite3_initialize() and so it is guaranteed
 45781  45814   ** to be serialized already.  There is no need for further mutexing.
 45782  45815   */
 45783  45816   SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
 45784  45817     if( pcache1.isInit ){
 45785  45818       PgFreeslot *p;
 45786  45819       if( pBuf==0 ) sz = n = 0;
        45820  +    if( n==0 ) sz = 0;
 45787  45821       sz = ROUNDDOWN8(sz);
 45788  45822       pcache1.szSlot = sz;
 45789  45823       pcache1.nSlot = pcache1.nFreeSlot = n;
 45790  45824       pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
 45791  45825       pcache1.pStart = pBuf;
 45792  45826       pcache1.pFree = 0;
 45793  45827       pcache1.bUnderPressure = 0;
................................................................................
 45972  46006   #endif
 45973  46007       if( pPg==0 ) return 0;
 45974  46008       p->page.pBuf = pPg;
 45975  46009       p->page.pExtra = &p[1];
 45976  46010       p->isBulkLocal = 0;
 45977  46011       p->isAnchor = 0;
 45978  46012     }
 45979         -  if( pCache->bPurgeable ){
 45980         -    pCache->pGroup->nCurrentPage++;
 45981         -  }
        46013  +  (*pCache->pnPurgeable)++;
 45982  46014     return p;
 45983  46015   }
 45984  46016   
 45985  46017   /*
 45986  46018   ** Free a page object allocated by pcache1AllocPage().
 45987  46019   */
 45988  46020   static void pcache1FreePage(PgHdr1 *p){
................................................................................
 45995  46027       pCache->pFree = p;
 45996  46028     }else{
 45997  46029       pcache1Free(p->page.pBuf);
 45998  46030   #ifdef SQLITE_PCACHE_SEPARATE_HEADER
 45999  46031       sqlite3_free(p);
 46000  46032   #endif
 46001  46033     }
 46002         -  if( pCache->bPurgeable ){
 46003         -    pCache->pGroup->nCurrentPage--;
 46004         -  }
        46034  +  (*pCache->pnPurgeable)--;
 46005  46035   }
 46006  46036   
 46007  46037   /*
 46008  46038   ** Malloc function used by SQLite to obtain space from the buffer configured
 46009  46039   ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
 46010  46040   ** exists, this function falls back to sqlite3Malloc().
 46011  46041   */
................................................................................
 46092  46122   ** This function is used internally to remove the page pPage from the 
 46093  46123   ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
 46094  46124   ** LRU list, then this function is a no-op.
 46095  46125   **
 46096  46126   ** The PGroup mutex must be held when this function is called.
 46097  46127   */
 46098  46128   static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
 46099         -  PCache1 *pCache;
 46100         -
 46101  46129     assert( pPage!=0 );
 46102         -  assert( pPage->isPinned==0 );
 46103         -  pCache = pPage->pCache;
        46130  +  assert( PAGE_IS_UNPINNED(pPage) );
 46104  46131     assert( pPage->pLruNext );
 46105  46132     assert( pPage->pLruPrev );
 46106         -  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
        46133  +  assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
 46107  46134     pPage->pLruPrev->pLruNext = pPage->pLruNext;
 46108  46135     pPage->pLruNext->pLruPrev = pPage->pLruPrev;
 46109  46136     pPage->pLruNext = 0;
 46110  46137     pPage->pLruPrev = 0;
 46111         -  pPage->isPinned = 1;
 46112  46138     assert( pPage->isAnchor==0 );
 46113         -  assert( pCache->pGroup->lru.isAnchor==1 );
 46114         -  pCache->nRecyclable--;
        46139  +  assert( pPage->pCache->pGroup->lru.isAnchor==1 );
        46140  +  pPage->pCache->nRecyclable--;
 46115  46141     return pPage;
 46116  46142   }
 46117  46143   
 46118  46144   
 46119  46145   /*
 46120  46146   ** Remove the page supplied as an argument from the hash table 
 46121  46147   ** (PCache1.apHash structure) that it is currently stored in.
................................................................................
 46141  46167   ** If there are currently more than nMaxPage pages allocated, try
 46142  46168   ** to recycle pages to reduce the number allocated to nMaxPage.
 46143  46169   */
 46144  46170   static void pcache1EnforceMaxPage(PCache1 *pCache){
 46145  46171     PGroup *pGroup = pCache->pGroup;
 46146  46172     PgHdr1 *p;
 46147  46173     assert( sqlite3_mutex_held(pGroup->mutex) );
 46148         -  while( pGroup->nCurrentPage>pGroup->nMaxPage
        46174  +  while( pGroup->nPurgeable>pGroup->nMaxPage
 46149  46175         && (p=pGroup->lru.pLruPrev)->isAnchor==0
 46150  46176     ){
 46151  46177       assert( p->pCache->pGroup==pGroup );
 46152         -    assert( p->isPinned==0 );
        46178  +    assert( PAGE_IS_UNPINNED(p) );
 46153  46179       pcache1PinPage(p);
 46154  46180       pcache1RemoveFromHash(p, 1);
 46155  46181     }
 46156  46182     if( pCache->nPage==0 && pCache->pBulk ){
 46157  46183       sqlite3_free(pCache->pBulk);
 46158  46184       pCache->pBulk = pCache->pFree = 0;
 46159  46185     }
................................................................................
 46194  46220       PgHdr1 *pPage;
 46195  46221       assert( h<pCache->nHash );
 46196  46222       pp = &pCache->apHash[h]; 
 46197  46223       while( (pPage = *pp)!=0 ){
 46198  46224         if( pPage->iKey>=iLimit ){
 46199  46225           pCache->nPage--;
 46200  46226           *pp = pPage->pNext;
 46201         -        if( !pPage->isPinned ) pcache1PinPage(pPage);
        46227  +        if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
 46202  46228           pcache1FreePage(pPage);
 46203  46229         }else{
 46204  46230           pp = &pPage->pNext;
 46205  46231           TESTONLY( if( nPage>=0 ) nPage++; )
 46206  46232         }
 46207  46233       }
 46208  46234       if( h==iStop ) break;
................................................................................
 46312  46338       pCache->bPurgeable = (bPurgeable ? 1 : 0);
 46313  46339       pcache1EnterMutex(pGroup);
 46314  46340       pcache1ResizeHash(pCache);
 46315  46341       if( bPurgeable ){
 46316  46342         pCache->nMin = 10;
 46317  46343         pGroup->nMinPage += pCache->nMin;
 46318  46344         pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
        46345  +      pCache->pnPurgeable = &pGroup->nPurgeable;
        46346  +    }else{
        46347  +      static unsigned int dummyCurrentPage;
        46348  +      pCache->pnPurgeable = &dummyCurrentPage;
 46319  46349       }
 46320  46350       pcache1LeaveMutex(pGroup);
 46321  46351       if( pCache->nHash==0 ){
 46322  46352         pcache1Destroy((sqlite3_pcache*)pCache);
 46323  46353         pCache = 0;
 46324  46354       }
 46325  46355     }
................................................................................
 46413  46443     /* Step 4. Try to recycle a page. */
 46414  46444     if( pCache->bPurgeable
 46415  46445      && !pGroup->lru.pLruPrev->isAnchor
 46416  46446      && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
 46417  46447     ){
 46418  46448       PCache1 *pOther;
 46419  46449       pPage = pGroup->lru.pLruPrev;
 46420         -    assert( pPage->isPinned==0 );
        46450  +    assert( PAGE_IS_UNPINNED(pPage) );
 46421  46451       pcache1RemoveFromHash(pPage, 0);
 46422  46452       pcache1PinPage(pPage);
 46423  46453       pOther = pPage->pCache;
 46424  46454       if( pOther->szAlloc != pCache->szAlloc ){
 46425  46455         pcache1FreePage(pPage);
 46426  46456         pPage = 0;
 46427  46457       }else{
 46428         -      pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
        46458  +      pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
 46429  46459       }
 46430  46460     }
 46431  46461   
 46432  46462     /* Step 5. If a usable page buffer has still not been found, 
 46433  46463     ** attempt to allocate a new one. 
 46434  46464     */
 46435  46465     if( !pPage ){
................................................................................
 46440  46470       unsigned int h = iKey % pCache->nHash;
 46441  46471       pCache->nPage++;
 46442  46472       pPage->iKey = iKey;
 46443  46473       pPage->pNext = pCache->apHash[h];
 46444  46474       pPage->pCache = pCache;
 46445  46475       pPage->pLruPrev = 0;
 46446  46476       pPage->pLruNext = 0;
 46447         -    pPage->isPinned = 1;
 46448  46477       *(void **)pPage->page.pExtra = 0;
 46449  46478       pCache->apHash[h] = pPage;
 46450  46479       if( iKey>pCache->iMaxKey ){
 46451  46480         pCache->iMaxKey = iKey;
 46452  46481       }
 46453  46482     }
 46454  46483     return pPage;
................................................................................
 46526  46555     while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
 46527  46556   
 46528  46557     /* Step 2: If the page was found in the hash table, then return it.
 46529  46558     ** If the page was not in the hash table and createFlag is 0, abort.
 46530  46559     ** Otherwise (page not in hash and createFlag!=0) continue with
 46531  46560     ** subsequent steps to try to create the page. */
 46532  46561     if( pPage ){
 46533         -    if( !pPage->isPinned ){
        46562  +    if( PAGE_IS_UNPINNED(pPage) ){
 46534  46563         return pcache1PinPage(pPage);
 46535  46564       }else{
 46536  46565         return pPage;
 46537  46566       }
 46538  46567     }else if( createFlag ){
 46539  46568       /* Steps 3, 4, and 5 implemented by this subroutine */
 46540  46569       return pcache1FetchStage2(pCache, iKey, createFlag);
................................................................................
 46601  46630     assert( pPage->pCache==pCache );
 46602  46631     pcache1EnterMutex(pGroup);
 46603  46632   
 46604  46633     /* It is an error to call this function if the page is already 
 46605  46634     ** part of the PGroup LRU list.
 46606  46635     */
 46607  46636     assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
 46608         -  assert( pPage->isPinned==1 );
        46637  +  assert( PAGE_IS_PINNED(pPage) );
 46609  46638   
 46610         -  if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
        46639  +  if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
 46611  46640       pcache1RemoveFromHash(pPage, 1);
 46612  46641     }else{
 46613  46642       /* Add the page to the PGroup LRU list. */
 46614  46643       PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
 46615  46644       pPage->pLruPrev = &pGroup->lru;
 46616  46645       (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
 46617  46646       *ppFirst = pPage;
 46618  46647       pCache->nRecyclable++;
 46619         -    pPage->isPinned = 0;
 46620  46648     }
 46621  46649   
 46622  46650     pcache1LeaveMutex(pCache->pGroup);
 46623  46651   }
 46624  46652   
 46625  46653   /*
 46626  46654   ** Implementation of the sqlite3_pcache.xRekey method. 
................................................................................
 46756  46784          &&  (p=pcache1.grp.lru.pLruPrev)!=0
 46757  46785          &&  p->isAnchor==0
 46758  46786       ){
 46759  46787         nFree += pcache1MemSize(p->page.pBuf);
 46760  46788   #ifdef SQLITE_PCACHE_SEPARATE_HEADER
 46761  46789         nFree += sqlite3MemSize(p);
 46762  46790   #endif
 46763         -      assert( p->isPinned==0 );
        46791  +      assert( PAGE_IS_UNPINNED(p) );
 46764  46792         pcache1PinPage(p);
 46765  46793         pcache1RemoveFromHash(p, 1);
 46766  46794       }
 46767  46795       pcache1LeaveMutex(&pcache1.grp);
 46768  46796     }
 46769  46797     return nFree;
 46770  46798   }
................................................................................
 46780  46808     int *pnMax,          /* OUT: Global maximum cache size */
 46781  46809     int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
 46782  46810     int *pnRecyclable    /* OUT: Total number of pages available for recycling */
 46783  46811   ){
 46784  46812     PgHdr1 *p;
 46785  46813     int nRecyclable = 0;
 46786  46814     for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
 46787         -    assert( p->isPinned==0 );
        46815  +    assert( PAGE_IS_UNPINNED(p) );
 46788  46816       nRecyclable++;
 46789  46817     }
 46790         -  *pnCurrent = pcache1.grp.nCurrentPage;
        46818  +  *pnCurrent = pcache1.grp.nPurgeable;
 46791  46819     *pnMax = (int)pcache1.grp.nMaxPage;
 46792  46820     *pnMin = (int)pcache1.grp.nMinPage;
 46793  46821     *pnRecyclable = nRecyclable;
 46794  46822   }
 46795  46823   #endif
 46796  46824   
 46797  46825   /************** End of pcache1.c *********************************************/
................................................................................
 47338  47366   */
 47339  47367   
 47340  47368   #ifndef SQLITE_WAL_H
 47341  47369   #define SQLITE_WAL_H
 47342  47370   
 47343  47371   /* #include "sqliteInt.h" */
 47344  47372   
 47345         -/* Additional values that can be added to the sync_flags argument of
 47346         -** sqlite3WalFrames():
        47373  +/* Macros for extracting appropriate sync flags for either transaction
        47374  +** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
 47347  47375   */
 47348         -#define WAL_SYNC_TRANSACTIONS  0x20   /* Sync at the end of each transaction */
 47349         -#define SQLITE_SYNC_MASK       0x13   /* Mask off the SQLITE_SYNC_* values */
        47376  +#define WAL_SYNC_FLAGS(X)   ((X)&0x03)
        47377  +#define CKPT_SYNC_FLAGS(X)  (((X)>>2)&0x03)
 47350  47378   
 47351  47379   #ifdef SQLITE_OMIT_WAL
 47352  47380   # define sqlite3WalOpen(x,y,z)                   0
 47353  47381   # define sqlite3WalLimit(x,y)
 47354  47382   # define sqlite3WalClose(v,w,x,y,z)              0
 47355  47383   # define sqlite3WalBeginReadTransaction(y,z)     0
 47356  47384   # define sqlite3WalEndReadTransaction(z)
................................................................................
 47575  47603   ** The following two macros are used within the PAGERTRACE() macros above
 47576  47604   ** to print out file-descriptors. 
 47577  47605   **
 47578  47606   ** PAGERID() takes a pointer to a Pager struct as its argument. The
 47579  47607   ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
 47580  47608   ** struct as its argument.
 47581  47609   */
 47582         -#define PAGERID(p) ((int)(p->fd))
 47583         -#define FILEHANDLEID(fd) ((int)fd)
        47610  +#define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
        47611  +#define FILEHANDLEID(fd) (SQLITE_PTR_TO_INT(fd))
 47584  47612   
 47585  47613   /*
 47586  47614   ** The Pager.eState variable stores the current 'state' of a pager. A
 47587  47615   ** pager may be in any one of the seven states shown in the following
 47588  47616   ** state diagram.
 47589  47617   **
 47590  47618   **                            OPEN <------+------+
................................................................................
 48063  48091   **
 48064  48092   ** errCode
 48065  48093   **
 48066  48094   **   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
 48067  48095   **   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode 
 48068  48096   **   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX 
 48069  48097   **   sub-codes.
        48098  +**
        48099  +** syncFlags, walSyncFlags
        48100  +**
        48101  +**   syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
        48102  +**   syncFlags is used for rollback mode.  walSyncFlags is used for WAL mode
        48103  +**   and contains the flags used to sync the checkpoint operations in the
        48104  +**   lower two bits, and sync flags used for transaction commits in the WAL
        48105  +**   file in bits 0x04 and 0x08.  In other words, to get the correct sync flags
        48106  +**   for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
        48107  +**   sync flags for transaction commit, use ((walSyncFlags>>2)&0x03).  Note
        48108  +**   that with synchronous=NORMAL in WAL mode, transaction commit is not synced
        48109  +**   meaning that the 0x04 and 0x08 bits are both zero.
 48070  48110   */
 48071  48111   struct Pager {
 48072  48112     sqlite3_vfs *pVfs;          /* OS functions to use for IO */
 48073  48113     u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
 48074  48114     u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
 48075  48115     u8 useJournal;              /* Use a rollback journal on this file */
 48076  48116     u8 noSync;                  /* Do not sync the journal if true */
 48077  48117     u8 fullSync;                /* Do extra syncs of the journal for robustness */
 48078  48118     u8 extraSync;               /* sync directory after journal delete */
 48079         -  u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
 48080         -  u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
 48081  48119     u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
        48120  +  u8 walSyncFlags;            /* See description above */
 48082  48121     u8 tempFile;                /* zFilename is a temporary or immutable file */
 48083  48122     u8 noLock;                  /* Do not lock (except in WAL mode) */
 48084  48123     u8 readOnly;                /* True for a read-only database */
 48085  48124     u8 memDb;                   /* True to inhibit all file I/O */
 48086  48125   
 48087  48126     /**************************************************************************
 48088  48127     ** The following block contains those class members that change during
................................................................................
 48394  48433         assert( p->eLock==EXCLUSIVE_LOCK );
 48395  48434         assert( pPager->errCode==SQLITE_OK );
 48396  48435         assert( !pagerUseWal(pPager) );
 48397  48436         assert( p->eLock>=EXCLUSIVE_LOCK );
 48398  48437         assert( isOpen(p->jfd) 
 48399  48438              || p->journalMode==PAGER_JOURNALMODE_OFF 
 48400  48439              || p->journalMode==PAGER_JOURNALMODE_WAL 
        48440  +           || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
 48401  48441         );
 48402  48442         assert( pPager->dbOrigSize<=pPager->dbHintSize );
 48403  48443         break;
 48404  48444   
 48405  48445       case PAGER_WRITER_FINISHED:
 48406  48446         assert( p->eLock==EXCLUSIVE_LOCK );
 48407  48447         assert( pPager->errCode==SQLITE_OK );
 48408  48448         assert( !pagerUseWal(pPager) );
 48409  48449         assert( isOpen(p->jfd) 
 48410  48450              || p->journalMode==PAGER_JOURNALMODE_OFF 
 48411  48451              || p->journalMode==PAGER_JOURNALMODE_WAL 
        48452  +           || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
 48412  48453         );
 48413  48454         break;
 48414  48455   
 48415  48456       case PAGER_ERROR:
 48416  48457         /* There must be at least one outstanding reference to the pager if
 48417  48458         ** in ERROR state. Otherwise the pager should have already dropped
 48418  48459         ** back to OPEN state.
................................................................................
 48615  48656         IOTRACE(("LOCK %p %d\n", pPager, eLock))
 48616  48657       }
 48617  48658     }
 48618  48659     return rc;
 48619  48660   }
 48620  48661   
 48621  48662   /*
 48622         -** This function determines whether or not the atomic-write optimization
 48623         -** can be used with this pager. The optimization can be used if:
        48663  +** This function determines whether or not the atomic-write or
        48664  +** atomic-batch-write optimizations can be used with this pager. The
        48665  +** atomic-write optimization can be used if:
 48624  48666   **
 48625  48667   **  (a) the value returned by OsDeviceCharacteristics() indicates that
 48626  48668   **      a database page may be written atomically, and
 48627  48669   **  (b) the value returned by OsSectorSize() is less than or equal
 48628  48670   **      to the page size.
 48629  48671   **
 48630         -** The optimization is also always enabled for temporary files. It is
 48631         -** an error to call this function if pPager is opened on an in-memory
 48632         -** database.
        48672  +** If it can be used, then the value returned is the size of the journal 
        48673  +** file when it contains rollback data for exactly one page.
 48633  48674   **
 48634         -** If the optimization cannot be used, 0 is returned. If it can be used,
 48635         -** then the value returned is the size of the journal file when it
 48636         -** contains rollback data for exactly one page.
        48675  +** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
        48676  +** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
        48677  +** returned in this case.
        48678  +**
        48679  +** If neither optimization can be used, 0 is returned.
 48637  48680   */
 48638         -#ifdef SQLITE_ENABLE_ATOMIC_WRITE
 48639  48681   static int jrnlBufferSize(Pager *pPager){
 48640  48682     assert( !MEMDB );
 48641         -  if( !pPager->tempFile ){
 48642         -    int dc;                           /* Device characteristics */
 48643         -    int nSector;                      /* Sector size */
 48644         -    int szPage;                       /* Page size */
 48645  48683   
 48646         -    assert( isOpen(pPager->fd) );
 48647         -    dc = sqlite3OsDeviceCharacteristics(pPager->fd);
 48648         -    nSector = pPager->sectorSize;
 48649         -    szPage = pPager->pageSize;
        48684  +#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
        48685  + || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
        48686  +  int dc;                           /* Device characteristics */
        48687  +
        48688  +  assert( isOpen(pPager->fd) );
        48689  +  dc = sqlite3OsDeviceCharacteristics(pPager->fd);
        48690  +#else
        48691  +  UNUSED_PARAMETER(pPager);
        48692  +#endif
        48693  +
        48694  +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
        48695  +  if( dc&SQLITE_IOCAP_BATCH_ATOMIC ){
        48696  +    return -1;
        48697  +  }
        48698  +#endif
        48699  +
        48700  +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
        48701  +  {
        48702  +    int nSector = pPager->sectorSize;
        48703  +    int szPage = pPager->pageSize;
 48650  48704   
 48651  48705       assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
 48652  48706       assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
 48653  48707       if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
 48654  48708         return 0;
 48655  48709       }
 48656  48710     }
 48657  48711   
 48658  48712     return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
        48713  +#endif
        48714  +
        48715  +  return 0;
 48659  48716   }
 48660         -#else
 48661         -# define jrnlBufferSize(x) 0
 48662         -#endif
 48663  48717   
 48664  48718   /*
 48665  48719   ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
 48666  48720   ** on the cache using a hash function.  This is used for testing
 48667  48721   ** and debugging only.
 48668  48722   */
 48669  48723   #ifdef SQLITE_CHECK_PAGES
................................................................................
 48738  48792     unsigned char aMagic[8];   /* A buffer to hold the magic header */
 48739  48793     zMaster[0] = '\0';
 48740  48794   
 48741  48795     if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
 48742  48796      || szJ<16
 48743  48797      || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
 48744  48798      || len>=nMaster 
        48799  +   || len>szJ-16
 48745  48800      || len==0 
 48746  48801      || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
 48747  48802      || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
 48748  48803      || memcmp(aMagic, aJournalMagic, 8)
 48749  48804      || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
 48750  48805     ){
 48751  48806       return rc;
................................................................................
 49459  49514     assert( assert_pager_state(pPager) );
 49460  49515     assert( pPager->eState!=PAGER_ERROR );
 49461  49516     if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
 49462  49517       return SQLITE_OK;
 49463  49518     }
 49464  49519   
 49465  49520     releaseAllSavepoints(pPager);
 49466         -  assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
        49521  +  assert( isOpen(pPager->jfd) || pPager->pInJournal==0 
        49522  +      || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
        49523  +  );
 49467  49524     if( isOpen(pPager->jfd) ){
 49468  49525       assert( !pagerUseWal(pPager) );
 49469  49526   
 49470  49527       /* Finalize the journal file. */
 49471  49528       if( sqlite3JournalIsInMemory(pPager->jfd) ){
 49472  49529         /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
 49473  49530         sqlite3OsClose(pPager->jfd);
................................................................................
 50227  50284     u32 u;                   /* Unsigned loop counter */
 50228  50285     Pgno mxPg = 0;           /* Size of the original file in pages */
 50229  50286     int rc;                  /* Result code of a subroutine */
 50230  50287     int res = 1;             /* Value returned by sqlite3OsAccess() */
 50231  50288     char *zMaster = 0;       /* Name of master journal file if any */
 50232  50289     int needPagerReset;      /* True to reset page prior to first page rollback */
 50233  50290     int nPlayback = 0;       /* Total number of pages restored from journal */
        50291  +  u32 savedPageSize = pPager->pageSize;
 50234  50292   
 50235  50293     /* Figure out how many records are in the journal.  Abort early if
 50236  50294     ** the journal is empty.
 50237  50295     */
 50238  50296     assert( isOpen(pPager->jfd) );
 50239  50297     rc = sqlite3OsFileSize(pPager->jfd, &szJ);
 50240  50298     if( rc!=SQLITE_OK ){
................................................................................
 50356  50414         }
 50357  50415       }
 50358  50416     }
 50359  50417     /*NOTREACHED*/
 50360  50418     assert( 0 );
 50361  50419   
 50362  50420   end_playback:
        50421  +  if( rc==SQLITE_OK ){
        50422  +    rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
        50423  +  }
 50363  50424     /* Following a rollback, the database file should be back in its original
 50364  50425     ** state prior to the start of the transaction, so invoke the
 50365  50426     ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
 50366  50427     ** assertion that the transaction counter was modified.
 50367  50428     */
 50368  50429   #ifdef SQLITE_DEBUG
 50369  50430     if( pPager->fd->pMethods ){
................................................................................
 50414  50475     */
 50415  50476     setSectorSize(pPager);
 50416  50477     return rc;
 50417  50478   }
 50418  50479   
 50419  50480   
 50420  50481   /*
 50421         -** Read the content for page pPg out of the database file and into 
        50482  +** Read the content for page pPg out of the database file (or out of
        50483  +** the WAL if that is where the most recent copy if found) into 
 50422  50484   ** pPg->pData. A shared lock or greater must be held on the database
 50423  50485   ** file before this function is called.
 50424  50486   **
 50425  50487   ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
 50426  50488   ** the value read from the database file.
 50427  50489   **
 50428  50490   ** If an IO error occurs, then the IO error is returned to the caller.
 50429  50491   ** Otherwise, SQLITE_OK is returned.
 50430  50492   */
 50431         -static int readDbPage(PgHdr *pPg, u32 iFrame){
        50493  +static int readDbPage(PgHdr *pPg){
 50432  50494     Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
 50433         -  Pgno pgno = pPg->pgno;       /* Page number to read */
 50434  50495     int rc = SQLITE_OK;          /* Return code */
 50435         -  int pgsz = pPager->pageSize; /* Number of bytes to read */
        50496  +
        50497  +#ifndef SQLITE_OMIT_WAL
        50498  +  u32 iFrame = 0;              /* Frame of WAL containing pgno */
 50436  50499   
 50437  50500     assert( pPager->eState>=PAGER_READER && !MEMDB );
 50438  50501     assert( isOpen(pPager->fd) );
 50439  50502   
 50440         -#ifndef SQLITE_OMIT_WAL
        50503  +  if( pagerUseWal(pPager) ){
        50504  +    rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
        50505  +    if( rc ) return rc;
        50506  +  }
 50441  50507     if( iFrame ){
 50442         -    /* Try to pull the page from the write-ahead log. */
 50443         -    rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
        50508  +    rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
 50444  50509     }else
 50445  50510   #endif
 50446  50511     {
 50447         -    i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
 50448         -    rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
        50512  +    i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
        50513  +    rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
 50449  50514       if( rc==SQLITE_IOERR_SHORT_READ ){
 50450  50515         rc = SQLITE_OK;
 50451  50516       }
 50452  50517     }
 50453  50518   
 50454         -  if( pgno==1 ){
        50519  +  if( pPg->pgno==1 ){
 50455  50520       if( rc ){
 50456  50521         /* If the read is unsuccessful, set the dbFileVers[] to something
 50457  50522         ** that will never be a valid file version.  dbFileVers[] is a copy
 50458  50523         ** of bytes 24..39 of the database.  Bytes 28..31 should always be
 50459  50524         ** zero or the size of the database in page. Bytes 32..35 and 35..39
 50460  50525         ** should be page numbers which are never 0xffffffff.  So filling
 50461  50526         ** pPager->dbFileVers[] with all 0xff bytes should suffice.
................................................................................
 50467  50532         */
 50468  50533         memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
 50469  50534       }else{
 50470  50535         u8 *dbFileVers = &((u8*)pPg->pData)[24];
 50471  50536         memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
 50472  50537       }
 50473  50538     }
 50474         -  CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM_BKPT);
        50539  +  CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM_BKPT);
 50475  50540   
 50476  50541     PAGER_INCR(sqlite3_pager_readdb_count);
 50477  50542     PAGER_INCR(pPager->nRead);
 50478         -  IOTRACE(("PGIN %p %d\n", pPager, pgno));
        50543  +  IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
 50479  50544     PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
 50480         -               PAGERID(pPager), pgno, pager_pagehash(pPg)));
        50545  +               PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
 50481  50546   
 50482  50547     return rc;
 50483  50548   }
 50484  50549   
 50485  50550   /*
 50486  50551   ** Update the value of the change-counter at offsets 24 and 92 in
 50487  50552   ** the header and the sqlite version number at offset 96.
................................................................................
 50524  50589   
 50525  50590     assert( pagerUseWal(pPager) );
 50526  50591     pPg = sqlite3PagerLookup(pPager, iPg);
 50527  50592     if( pPg ){
 50528  50593       if( sqlite3PcachePageRefcount(pPg)==1 ){
 50529  50594         sqlite3PcacheDrop(pPg);
 50530  50595       }else{
 50531         -      u32 iFrame = 0;
 50532         -      rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
 50533         -      if( rc==SQLITE_OK ){
 50534         -        rc = readDbPage(pPg, iFrame);
 50535         -      }
        50596  +      rc = readDbPage(pPg);
 50536  50597         if( rc==SQLITE_OK ){
 50537  50598           pPager->xReiniter(pPg);
 50538  50599         }
 50539  50600         sqlite3PagerUnrefNotNull(pPg);
 50540  50601       }
 50541  50602     }
 50542  50603   
................................................................................
 51034  51095     }else{
 51035  51096       pPager->noSync =  level==PAGER_SYNCHRONOUS_OFF ?1:0;
 51036  51097       pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
 51037  51098       pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
 51038  51099     }
 51039  51100     if( pPager->noSync ){
 51040  51101       pPager->syncFlags = 0;
 51041         -    pPager->ckptSyncFlags = 0;
 51042  51102     }else if( pgFlags & PAGER_FULLFSYNC ){
 51043  51103       pPager->syncFlags = SQLITE_SYNC_FULL;
 51044         -    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
 51045         -  }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){
 51046         -    pPager->syncFlags = SQLITE_SYNC_NORMAL;
 51047         -    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
 51048  51104     }else{
 51049  51105       pPager->syncFlags = SQLITE_SYNC_NORMAL;
 51050         -    pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
 51051  51106     }
 51052         -  pPager->walSyncFlags = pPager->syncFlags;
        51107  +  pPager->walSyncFlags = (pPager->syncFlags<<2);
 51053  51108     if( pPager->fullSync ){
 51054         -    pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
        51109  +    pPager->walSyncFlags |= pPager->syncFlags;
        51110  +  }
        51111  +  if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
        51112  +    pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
 51055  51113     }
 51056  51114     if( pgFlags & PAGER_CACHESPILL ){
 51057  51115       pPager->doNotSpill &= ~SPILLFLAG_OFF;
 51058  51116     }else{
 51059  51117       pPager->doNotSpill |= SPILLFLAG_OFF;
 51060  51118     }
 51061  51119   }
................................................................................
 51546  51604     disable_simulated_io_errors();
 51547  51605     sqlite3BeginBenignMalloc();
 51548  51606     pagerFreeMapHdrs(pPager);
 51549  51607     /* pPager->errCode = 0; */
 51550  51608     pPager->exclusiveMode = 0;
 51551  51609   #ifndef SQLITE_OMIT_WAL
 51552  51610     assert( db || pPager->pWal==0 );
 51553         -  sqlite3WalClose(pPager->pWal, db, pPager->ckptSyncFlags, pPager->pageSize,
        51611  +  sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,
 51554  51612         (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp)
 51555  51613     );
 51556  51614     pPager->pWal = 0;
 51557  51615   #endif
 51558  51616     pager_reset(pPager);
 51559  51617     if( MEMDB ){
 51560  51618       pager_unlock(pPager);
................................................................................
 52014  52072     if( pagerUseWal(pPager) ){
 52015  52073       /* Write a single frame for this page to the log. */
 52016  52074       rc = subjournalPageIfRequired(pPg); 
 52017  52075       if( rc==SQLITE_OK ){
 52018  52076         rc = pagerWalFrames(pPager, pPg, 0, 0);
 52019  52077       }
 52020  52078     }else{
        52079  +    
        52080  +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
        52081  +    if( pPager->tempFile==0 ){
        52082  +      rc = sqlite3JournalCreate(pPager->jfd);
        52083  +      if( rc!=SQLITE_OK ) return pager_error(pPager, rc);
        52084  +    }
        52085  +#endif
 52021  52086     
 52022  52087       /* Sync the journal file if required. */
 52023  52088       if( pPg->flags&PGHDR_NEED_SYNC 
 52024  52089        || pPager->eState==PAGER_WRITER_CACHEMOD
 52025  52090       ){
 52026  52091         rc = syncJournal(pPager, 1);
 52027  52092       }
................................................................................
 52347  52412     assert( useJournal || pPager->tempFile );
 52348  52413     pPager->noSync = pPager->tempFile;
 52349  52414     if( pPager->noSync ){
 52350  52415       assert( pPager->fullSync==0 );
 52351  52416       assert( pPager->extraSync==0 );
 52352  52417       assert( pPager->syncFlags==0 );
 52353  52418       assert( pPager->walSyncFlags==0 );
 52354         -    assert( pPager->ckptSyncFlags==0 );
 52355  52419     }else{
 52356  52420       pPager->fullSync = 1;
 52357  52421       pPager->extraSync = 0;
 52358  52422       pPager->syncFlags = SQLITE_SYNC_NORMAL;
 52359         -    pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
 52360         -    pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
        52423  +    pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
 52361  52424     }
 52362  52425     /* pPager->pFirst = 0; */
 52363  52426     /* pPager->pFirstSynced = 0; */
 52364  52427     /* pPager->pLast = 0; */
 52365  52428     pPager->nExtra = (u16)nExtra;
 52366  52429     pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
 52367  52430     assert( isOpen(pPager->fd) || tempFile );
................................................................................
 52773  52836   ** transaction and unlock the pager.
 52774  52837   **
 52775  52838   ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
 52776  52839   ** the rollback journal, the unlock is not performed and there is
 52777  52840   ** nothing to rollback, so this routine is a no-op.
 52778  52841   */ 
 52779  52842   static void pagerUnlockIfUnused(Pager *pPager){
 52780         -  if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
        52843  +  if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
        52844  +    assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
 52781  52845       pagerUnlockAndRollback(pPager);
 52782  52846     }
 52783  52847   }
 52784  52848   
 52785  52849   /*
 52786  52850   ** The page getter methods each try to acquire a reference to a
 52787  52851   ** page with page number pgno. If the requested reference is 
................................................................................
 52914  52978           TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
 52915  52979           testcase( rc==SQLITE_NOMEM );
 52916  52980           sqlite3EndBenignMalloc();
 52917  52981         }
 52918  52982         memset(pPg->pData, 0, pPager->pageSize);
 52919  52983         IOTRACE(("ZERO %p %d\n", pPager, pgno));
 52920  52984       }else{
 52921         -      u32 iFrame = 0;                 /* Frame to read from WAL file */
 52922         -      if( pagerUseWal(pPager) ){
 52923         -        rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
 52924         -        if( rc!=SQLITE_OK ) goto pager_acquire_err;
 52925         -      }
 52926  52985         assert( pPg->pPager==pPager );
 52927  52986         pPager->aStat[PAGER_STAT_MISS]++;
 52928         -      rc = readDbPage(pPg, iFrame);
        52987  +      rc = readDbPage(pPg);
 52929  52988         if( rc!=SQLITE_OK ){
 52930  52989           goto pager_acquire_err;
 52931  52990         }
 52932  52991       }
 52933  52992       pager_set_pagehash(pPg);
 52934  52993     }
 52935  52994     return SQLITE_OK;
................................................................................
 53064  53123     if( pPage==0 ) return 0;
 53065  53124     return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
 53066  53125   }
 53067  53126   
 53068  53127   /*
 53069  53128   ** Release a page reference.
 53070  53129   **
 53071         -** If the number of references to the page drop to zero, then the
 53072         -** page is added to the LRU list.  When all references to all pages
 53073         -** are released, a rollback occurs and the lock on the database is
 53074         -** removed.
        53130  +** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be
        53131  +** used if we know that the page being released is not the last page.
        53132  +** The btree layer always holds page1 open until the end, so these first
        53133  +** to routines can be used to release any page other than BtShared.pPage1.
        53134  +**
        53135  +** Use sqlite3PagerUnrefPageOne() to release page1.  This latter routine
        53136  +** checks the total number of outstanding pages and if the number of
        53137  +** pages reaches zero it drops the database lock.
 53075  53138   */
 53076  53139   SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
 53077         -  Pager *pPager;
        53140  +  TESTONLY( Pager *pPager = pPg->pPager; )
 53078  53141     assert( pPg!=0 );
 53079         -  pPager = pPg->pPager;
 53080  53142     if( pPg->flags & PGHDR_MMAP ){
        53143  +    assert( pPg->pgno!=1 );  /* Page1 is never memory mapped */
 53081  53144       pagerReleaseMapPage(pPg);
 53082  53145     }else{
 53083  53146       sqlite3PcacheRelease(pPg);
 53084  53147     }
 53085         -  pagerUnlockIfUnused(pPager);
        53148  +  /* Do not use this routine to release the last reference to page1 */
        53149  +  assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
 53086  53150   }
 53087  53151   SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
 53088  53152     if( pPg ) sqlite3PagerUnrefNotNull(pPg);
        53153  +}
        53154  +SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
        53155  +  Pager *pPager;
        53156  +  assert( pPg!=0 );
        53157  +  assert( pPg->pgno==1 );
        53158  +  assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
        53159  +  pPager = pPg->pPager;
        53160  +  sqlite3PcacheRelease(pPg);
        53161  +  pagerUnlockIfUnused(pPager);
 53089  53162   }
 53090  53163   
 53091  53164   /*
 53092  53165   ** This function is called at the start of every write transaction.
 53093  53166   ** There must already be a RESERVED or EXCLUSIVE lock on the database 
 53094  53167   ** file when this routine is called.
 53095  53168   **
................................................................................
 53794  53867           rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
 53795  53868         }
 53796  53869         sqlite3PagerUnref(pPageOne);
 53797  53870         if( rc==SQLITE_OK ){
 53798  53871           sqlite3PcacheCleanAll(pPager->pPCache);
 53799  53872         }
 53800  53873       }else{
        53874  +      /* The bBatch boolean is true if the batch-atomic-write commit method
        53875  +      ** should be used.  No rollback journal is created if batch-atomic-write
        53876  +      ** is enabled.
        53877  +      */
        53878  +      sqlite3_file *fd = pPager->fd;
        53879  +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
        53880  +      const int bBatch = zMaster==0    /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
        53881  +        && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC)
        53882  +        && !pPager->noSync
        53883  +        && sqlite3JournalIsInMemory(pPager->jfd);
        53884  +#else
        53885  +# define bBatch 0
        53886  +#endif
        53887  +
        53888  +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
 53801  53889         /* The following block updates the change-counter. Exactly how it
 53802  53890         ** does this depends on whether or not the atomic-update optimization
 53803  53891         ** was enabled at compile time, and if this transaction meets the 
 53804  53892         ** runtime criteria to use the operation: 
 53805  53893         **
 53806  53894         **    * The file-system supports the atomic-write property for
 53807  53895         **      blocks of size page-size, and 
................................................................................
 53817  53905         ** mode. 
 53818  53906         **
 53819  53907         ** Otherwise, if the optimization is both enabled and applicable,
 53820  53908         ** then call pager_incr_changecounter() to update the change-counter
 53821  53909         ** in 'direct' mode. In this case the journal file will never be
 53822  53910         ** created for this transaction.
 53823  53911         */
 53824         -  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 53825         -      PgHdr *pPg;
 53826         -      assert( isOpen(pPager->jfd) 
 53827         -           || pPager->journalMode==PAGER_JOURNALMODE_OFF 
 53828         -           || pPager->journalMode==PAGER_JOURNALMODE_WAL 
 53829         -      );
 53830         -      if( !zMaster && isOpen(pPager->jfd) 
 53831         -       && pPager->journalOff==jrnlBufferSize(pPager) 
 53832         -       && pPager->dbSize>=pPager->dbOrigSize
 53833         -       && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
 53834         -      ){
 53835         -        /* Update the db file change counter via the direct-write method. The 
 53836         -        ** following call will modify the in-memory representation of page 1 
 53837         -        ** to include the updated change counter and then write page 1 
 53838         -        ** directly to the database file. Because of the atomic-write 
 53839         -        ** property of the host file-system, this is safe.
 53840         -        */
 53841         -        rc = pager_incr_changecounter(pPager, 1);
 53842         -      }else{
        53912  +      if( bBatch==0 ){
        53913  +        PgHdr *pPg;
        53914  +        assert( isOpen(pPager->jfd) 
        53915  +            || pPager->journalMode==PAGER_JOURNALMODE_OFF 
        53916  +            || pPager->journalMode==PAGER_JOURNALMODE_WAL 
        53917  +            );
        53918  +        if( !zMaster && isOpen(pPager->jfd) 
        53919  +         && pPager->journalOff==jrnlBufferSize(pPager) 
        53920  +         && pPager->dbSize>=pPager->dbOrigSize
        53921  +         && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
        53922  +        ){
        53923  +          /* Update the db file change counter via the direct-write method. The 
        53924  +          ** following call will modify the in-memory representation of page 1 
        53925  +          ** to include the updated change counter and then write page 1 
        53926  +          ** directly to the database file. Because of the atomic-write 
        53927  +          ** property of the host file-system, this is safe.
        53928  +          */
        53929  +          rc = pager_incr_changecounter(pPager, 1);
        53930  +        }else{
        53931  +          rc = sqlite3JournalCreate(pPager->jfd);
        53932  +          if( rc==SQLITE_OK ){
        53933  +            rc = pager_incr_changecounter(pPager, 0);
        53934  +          }
        53935  +        }
        53936  +      }
        53937  +#else 
        53938  +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
        53939  +      if( zMaster ){
 53843  53940           rc = sqlite3JournalCreate(pPager->jfd);
 53844         -        if( rc==SQLITE_OK ){
 53845         -          rc = pager_incr_changecounter(pPager, 0);
 53846         -        }
        53941  +        if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 53847  53942         }
 53848         -  #else
        53943  +#endif
 53849  53944         rc = pager_incr_changecounter(pPager, 0);
 53850         -  #endif
        53945  +#endif
 53851  53946         if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 53852  53947     
 53853  53948         /* Write the master journal name into the journal file. If a master 
 53854  53949         ** journal file name has already been written to the journal file, 
 53855  53950         ** or if zMaster is NULL (no master journal), then this call is a no-op.
 53856  53951         */
 53857  53952         rc = writeMasterJournal(pPager, zMaster);
................................................................................
 53866  53961         ** journal requires a sync here. However, in locking_mode=exclusive
 53867  53962         ** on a system under memory pressure it is just possible that this is 
 53868  53963         ** not the case. In this case it is likely enough that the redundant
 53869  53964         ** xSync() call will be changed to a no-op by the OS anyhow. 
 53870  53965         */
 53871  53966         rc = syncJournal(pPager, 0);
 53872  53967         if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 53873         -  
        53968  +
        53969  +      if( bBatch ){
        53970  +        /* The pager is now in DBMOD state. But regardless of what happens
        53971  +        ** next, attempting to play the journal back into the database would
        53972  +        ** be unsafe. Close it now to make sure that does not happen.  */
        53973  +        sqlite3OsClose(pPager->jfd);
        53974  +        rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
        53975  +        if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
        53976  +      }
 53874  53977         rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
        53978  +      if( bBatch ){
        53979  +        if( rc==SQLITE_OK ){
        53980  +          rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
        53981  +        }else{
        53982  +          sqlite3OsFileControl(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
        53983  +        }
        53984  +      }
        53985  +
 53875  53986         if( rc!=SQLITE_OK ){
 53876  53987           assert( rc!=SQLITE_IOERR_BLOCKED );
 53877  53988           goto commit_phase_one_exit;
 53878  53989         }
 53879  53990         sqlite3PcacheCleanAll(pPager->pPCache);
 53880  53991   
 53881  53992         /* If the file on disk is smaller than the database image, use 
................................................................................
 54768  54879     int *pnCkpt                     /* OUT: Final number of checkpointed frames */
 54769  54880   ){
 54770  54881     int rc = SQLITE_OK;
 54771  54882     if( pPager->pWal ){
 54772  54883       rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
 54773  54884           (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
 54774  54885           pPager->pBusyHandlerArg,
 54775         -        pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
        54886  +        pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
 54776  54887           pnLog, pnCkpt
 54777  54888       );
 54778  54889     }
 54779  54890     return rc;
 54780  54891   }
 54781  54892   
 54782  54893   SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
................................................................................
 54925  55036       
 54926  55037     /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
 54927  55038     ** the database file, the log and log-summary files will be deleted.
 54928  55039     */
 54929  55040     if( rc==SQLITE_OK && pPager->pWal ){
 54930  55041       rc = pagerExclusiveLock(pPager);
 54931  55042       if( rc==SQLITE_OK ){
 54932         -      rc = sqlite3WalClose(pPager->pWal, db, pPager->ckptSyncFlags,
        55043  +      rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
 54933  55044                              pPager->pageSize, (u8*)pPager->pTmpSpace);
 54934  55045         pPager->pWal = 0;
 54935  55046         pagerFixMaplimit(pPager);
 54936  55047         if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
 54937  55048       }
 54938  55049     }
 54939  55050     return rc;
................................................................................
 56797  56908       ){
 56798  56909         i64 nSize;                    /* Current size of database file */
 56799  56910         u32 nBackfill = pInfo->nBackfill;
 56800  56911   
 56801  56912         pInfo->nBackfillAttempted = mxSafeFrame;
 56802  56913   
 56803  56914         /* Sync the WAL to disk */
 56804         -      if( sync_flags ){
 56805         -        rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
 56806         -      }
        56915  +      rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
 56807  56916   
 56808  56917         /* If the database may grow as a result of this checkpoint, hint
 56809  56918         ** about the eventual size of the db file to the VFS layer.
 56810  56919         */
 56811  56920         if( rc==SQLITE_OK ){
 56812  56921           i64 nReq = ((i64)mxPage * szPage);
 56813  56922           rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
................................................................................
 56840  56949   
 56841  56950         /* If work was actually accomplished... */
 56842  56951         if( rc==SQLITE_OK ){
 56843  56952           if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
 56844  56953             i64 szDb = pWal->hdr.nPage*(i64)szPage;
 56845  56954             testcase( IS_BIG_INT(szDb) );
 56846  56955             rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
 56847         -          if( rc==SQLITE_OK && sync_flags ){
 56848         -            rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
        56956  +          if( rc==SQLITE_OK ){
        56957  +            rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
 56849  56958             }
 56850  56959           }
 56851  56960           if( rc==SQLITE_OK ){
 56852  56961             pInfo->nBackfill = mxSafeFrame;
 56853  56962           }
 56854  56963         }
 56855  56964   
................................................................................
 57947  58056     if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
 57948  58057       int iFirstAmt = (int)(p->iSyncPoint - iOffset);
 57949  58058       rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
 57950  58059       if( rc ) return rc;
 57951  58060       iOffset += iFirstAmt;
 57952  58061       iAmt -= iFirstAmt;
 57953  58062       pContent = (void*)(iFirstAmt + (char*)pContent);
 57954         -    assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
 57955         -    rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK);
        58063  +    assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
        58064  +    rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
 57956  58065       if( iAmt==0 || rc ) return rc;
 57957  58066     }
 57958  58067     rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
 57959  58068     return rc;
 57960  58069   }
 57961  58070   
 57962  58071   /*
................................................................................
 58118  58227       }
 58119  58228   
 58120  58229       /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
 58121  58230       ** all syncing is turned off by PRAGMA synchronous=OFF).  Otherwise
 58122  58231       ** an out-of-order write following a WAL restart could result in
 58123  58232       ** database corruption.  See the ticket:
 58124  58233       **
 58125         -    **     http://localhost:591/sqlite/info/ff5be73dee
        58234  +    **     https://sqlite.org/src/info/ff5be73dee
 58126  58235       */
 58127         -    if( pWal->syncHeader && sync_flags ){
 58128         -      rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
        58236  +    if( pWal->syncHeader ){
        58237  +      rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
 58129  58238         if( rc ) return rc;
 58130  58239       }
 58131  58240     }
 58132  58241     assert( (int)pWal->szPage==szPage );
 58133  58242   
 58134  58243     /* Setup information needed to write frames into the WAL */
 58135  58244     w.pWal = pWal;
................................................................................
 58196  58305     ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
 58197  58306     ** needed and only the sync is done.  If padding is needed, then the
 58198  58307     ** final frame is repeated (with its commit mark) until the next sector
 58199  58308     ** boundary is crossed.  Only the part of the WAL prior to the last
 58200  58309     ** sector boundary is synced; the part of the last frame that extends
 58201  58310     ** past the sector boundary is written after the sync.
 58202  58311     */
 58203         -  if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
        58312  +  if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){
 58204  58313       int bSync = 1;
 58205  58314       if( pWal->padToSectorBoundary ){
 58206  58315         int sectorSize = sqlite3SectorSize(pWal->pWalFd);
 58207  58316         w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
 58208  58317         bSync = (w.iSyncPoint==iOffset);
 58209  58318         testcase( bSync );
 58210  58319         while( iOffset<w.iSyncPoint ){
................................................................................
 58212  58321           if( rc ) return rc;
 58213  58322           iOffset += szFrame;
 58214  58323           nExtra++;
 58215  58324         }
 58216  58325       }
 58217  58326       if( bSync ){
 58218  58327         assert( rc==SQLITE_OK );
 58219         -      rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
        58328  +      rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags));
 58220  58329       }
 58221  58330     }
 58222  58331   
 58223  58332     /* If this frame set completes the first transaction in the WAL and
 58224  58333     ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
 58225  58334     ** journal size limit, if possible.
 58226  58335     */
................................................................................
 59059  59168   **
 59060  59169   ** skipNext meaning:
 59061  59170   **    eState==SKIPNEXT && skipNext>0:  Next sqlite3BtreeNext() is no-op.
 59062  59171   **    eState==SKIPNEXT && skipNext<0:  Next sqlite3BtreePrevious() is no-op.
 59063  59172   **    eState==FAULT:                   Cursor fault with skipNext as error code.
 59064  59173   */
 59065  59174   struct BtCursor {
        59175  +  u8 eState;                /* One of the CURSOR_XXX constants (see below) */
        59176  +  u8 curFlags;              /* zero or more BTCF_* flags defined below */
        59177  +  u8 curPagerFlags;         /* Flags to send to sqlite3PagerGet() */
        59178  +  u8 hints;                 /* As configured by CursorSetHints() */
        59179  +  int nOvflAlloc;           /* Allocated size of aOverflow[] array */
 59066  59180     Btree *pBtree;            /* The Btree to which this cursor belongs */
 59067  59181     BtShared *pBt;            /* The BtShared this cursor points to */
 59068  59182     BtCursor *pNext;          /* Forms a linked list of all cursors */
 59069  59183     Pgno *aOverflow;          /* Cache of overflow page locations */
 59070  59184     CellInfo info;            /* A parse of the cell we are pointing at */
 59071  59185     i64 nKey;                 /* Size of pKey, or last integer key */
 59072  59186     void *pKey;               /* Saved key that was cursor last known position */
 59073  59187     Pgno pgnoRoot;            /* The root page of this tree */
 59074         -  int nOvflAlloc;           /* Allocated size of aOverflow[] array */
 59075  59188     int skipNext;    /* Prev() is noop if negative. Next() is noop if positive.
 59076  59189                      ** Error code if eState==CURSOR_FAULT */
 59077         -  u8 curFlags;              /* zero or more BTCF_* flags defined below */
 59078         -  u8 curPagerFlags;         /* Flags to send to sqlite3PagerGet() */
 59079         -  u8 eState;                /* One of the CURSOR_XXX constants (see below) */
 59080         -  u8 hints;                 /* As configured by CursorSetHints() */
 59081  59190     /* All fields above are zeroed when the cursor is allocated.  See
 59082  59191     ** sqlite3BtreeCursorZero().  Fields that follow must be manually
 59083  59192     ** initialized. */
 59084  59193     i8 iPage;                 /* Index of current page in apPage */
 59085  59194     u8 curIntKey;             /* Value of apPage[0]->intKey */
 59086  59195     u16 ix;                   /* Current index for apPage[iPage] */
 59087  59196     u16 aiIdx[BTCURSOR_MAX_DEPTH-1];     /* Current index in apPage[i] */
 59088  59197     struct KeyInfo *pKeyInfo;            /* Arg passed to comparison function */
 59089         -  MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */
        59198  +  MemPage *pPage;                        /* Current page */
        59199  +  MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
 59090  59200   };
 59091  59201   
 59092  59202   /*
 59093  59203   ** Legal values for BtCursor.curFlags
 59094  59204   */
 59095  59205   #define BTCF_WriteFlag    0x01   /* True if a write cursor */
 59096  59206   #define BTCF_ValidNKey    0x02   /* True if info.nKey is valid */
................................................................................
 59998  60108         pLock->eLock = READ_LOCK;
 59999  60109       }
 60000  60110     }
 60001  60111   }
 60002  60112   
 60003  60113   #endif /* SQLITE_OMIT_SHARED_CACHE */
 60004  60114   
 60005         -static void releasePage(MemPage *pPage);  /* Forward reference */
        60115  +static void releasePage(MemPage *pPage);         /* Forward reference */
        60116  +static void releasePageOne(MemPage *pPage);      /* Forward reference */
        60117  +static void releasePageNotNull(MemPage *pPage);  /* Forward reference */
 60006  60118   
 60007  60119   /*
 60008  60120   ***** This routine is used inside of assert() only ****
 60009  60121   **
 60010  60122   ** Verify that the cursor holds the mutex on its BtShared
 60011  60123   */
 60012  60124   #ifdef SQLITE_DEBUG
................................................................................
 60157  60269   }
 60158  60270   
 60159  60271   /*
 60160  60272   ** Release all of the apPage[] pages for a cursor.
 60161  60273   */
 60162  60274   static void btreeReleaseAllCursorPages(BtCursor *pCur){
 60163  60275     int i;
 60164         -  for(i=0; i<=pCur->iPage; i++){
 60165         -    releasePage(pCur->apPage[i]);
 60166         -    pCur->apPage[i] = 0;
        60276  +  if( pCur->iPage>=0 ){
        60277  +    for(i=0; i<pCur->iPage; i++){
        60278  +      releasePageNotNull(pCur->apPage[i]);
        60279  +    }
        60280  +    releasePageNotNull(pCur->pPage);
        60281  +    pCur->iPage = -1;
 60167  60282     }
 60168         -  pCur->iPage = -1;
 60169  60283   }
 60170  60284   
 60171  60285   /*
 60172  60286   ** The cursor passed as the only argument must point to a valid entry
 60173  60287   ** when this function is called (i.e. have eState==CURSOR_VALID). This
 60174  60288   ** function saves the current cursor key in variables pCur->nKey and
 60175  60289   ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error 
................................................................................
 60290  60404       if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
 60291  60405         if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
 60292  60406           int rc = saveCursorPosition(p);
 60293  60407           if( SQLITE_OK!=rc ){
 60294  60408             return rc;
 60295  60409           }
 60296  60410         }else{
 60297         -        testcase( p->iPage>0 );
        60411  +        testcase( p->iPage>=0 );
 60298  60412           btreeReleaseAllCursorPages(p);
 60299  60413         }
 60300  60414       }
 60301  60415       p = p->pNext;
 60302  60416     }while( p );
 60303  60417     return SQLITE_OK;
 60304  60418   }
................................................................................
 60330  60444   
 60331  60445     if( pKey ){
 60332  60446       assert( nKey==(i64)(int)nKey );
 60333  60447       pIdxKey = sqlite3VdbeAllocUnpackedRecord(pCur->pKeyInfo);
 60334  60448       if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
 60335  60449       sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
 60336  60450       if( pIdxKey->nField==0 ){
 60337         -      rc = SQLITE_CORRUPT_PGNO(pCur->apPage[pCur->iPage]->pgno);
        60451  +      rc = SQLITE_CORRUPT_BKPT;
 60338  60452         goto moveto_done;
 60339  60453       }
 60340  60454     }else{
 60341  60455       pIdxKey = 0;
 60342  60456     }
 60343  60457     rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
 60344  60458   moveto_done:
................................................................................
 60393  60507   **
 60394  60508   ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
 60395  60509   ** back to where it ought to be if this routine returns true.
 60396  60510   */
 60397  60511   SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
 60398  60512     return pCur->eState!=CURSOR_VALID;
 60399  60513   }
        60514  +
        60515  +/*
        60516  +** Return a pointer to a fake BtCursor object that will always answer
        60517  +** false to the sqlite3BtreeCursorHasMoved() routine above.  The fake
        60518  +** cursor returned must not be used with any other Btree interface.
        60519  +*/
        60520  +SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void){
        60521  +  static u8 fakeCursor = CURSOR_VALID;
        60522  +  assert( offsetof(BtCursor, eState)==0 );
        60523  +  return (BtCursor*)&fakeCursor;
        60524  +}
 60400  60525   
 60401  60526   /*
 60402  60527   ** This routine restores a cursor back to its original position after it
 60403  60528   ** has been moved by some outside activity (such as a btree rebalance or
 60404  60529   ** a row having been deleted out from under the cursor).  
 60405  60530   **
 60406  60531   ** On success, the *pDifferentRow parameter is false if the cursor is left
................................................................................
 60943  61068   
 60944  61069         if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
 60945  61070           u8 *pEnd = &data[cellOffset + nCell*2];
 60946  61071           u8 *pAddr;
 60947  61072           int sz2 = 0;
 60948  61073           int sz = get2byte(&data[iFree+2]);
 60949  61074           int top = get2byte(&data[hdr+5]);
        61075  +        if( top>=iFree ){
        61076  +          return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61077  +        }
 60950  61078           if( iFree2 ){
 60951         -          if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61079  +          assert( iFree+sz<=iFree2 ); /* Verified by pageFindSlot() */
 60952  61080             sz2 = get2byte(&data[iFree2+2]);
 60953  61081             assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
 60954  61082             memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
 60955  61083             sz += sz2;
 60956  61084           }
 60957  61085           cbrk = top+sz;
 60958  61086           assert( cbrk+(iFree-top) <= usableSize );
................................................................................
 61033  61161   static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
 61034  61162     const int hdr = pPg->hdrOffset;
 61035  61163     u8 * const aData = pPg->aData;
 61036  61164     int iAddr = hdr + 1;
 61037  61165     int pc = get2byte(&aData[iAddr]);
 61038  61166     int x;
 61039  61167     int usableSize = pPg->pBt->usableSize;
        61168  +  int size;            /* Size of the free slot */
 61040  61169   
 61041  61170     assert( pc>0 );
 61042         -  do{
 61043         -    int size;            /* Size of the free slot */
 61044         -    /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
 61045         -    ** increasing offset. */
 61046         -    if( pc>usableSize-4 || pc<iAddr+4 ){
 61047         -      *pRc = SQLITE_CORRUPT_PGNO(pPg->pgno);
 61048         -      return 0;
 61049         -    }
        61171  +  while( pc<=usableSize-4 ){
 61050  61172       /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
 61051  61173       ** freeblock form a big-endian integer which is the size of the freeblock
 61052  61174       ** in bytes, including the 4-byte header. */
 61053  61175       size = get2byte(&aData[pc+2]);
 61054  61176       if( (x = size - nByte)>=0 ){
 61055  61177         testcase( x==4 );
 61056  61178         testcase( x==3 );
 61057         -      if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
        61179  +      if( size+pc > usableSize ){
 61058  61180           *pRc = SQLITE_CORRUPT_PGNO(pPg->pgno);
 61059  61181           return 0;
 61060  61182         }else if( x<4 ){
 61061  61183           /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
 61062  61184           ** number of bytes in fragments may not exceed 60. */
 61063  61185           if( aData[hdr+7]>57 ) return 0;
 61064  61186   
................................................................................
 61071  61193            ** for the portion used by the new allocation. */
 61072  61194           put2byte(&aData[pc+2], x);
 61073  61195         }
 61074  61196         return &aData[pc + x];
 61075  61197       }
 61076  61198       iAddr = pc;
 61077  61199       pc = get2byte(&aData[pc]);
 61078         -  }while( pc );
        61200  +    if( pc<iAddr+size ) break;
        61201  +  }
        61202  +  if( pc ){
        61203  +    *pRc = SQLITE_CORRUPT_PGNO(pPg->pgno);
        61204  +  }
 61079  61205   
 61080  61206     return 0;
 61081  61207   }
 61082  61208   
 61083  61209   /*
 61084  61210   ** Allocate nByte bytes of space from within the B-Tree page passed
 61085  61211   ** as the first argument. Write into *pIdx the index into pPage->aData[]
................................................................................
 61185  61311   */
 61186  61312   static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
 61187  61313     u16 iPtr;                             /* Address of ptr to next freeblock */
 61188  61314     u16 iFreeBlk;                         /* Address of the next freeblock */
 61189  61315     u8 hdr;                               /* Page header size.  0 or 100 */
 61190  61316     u8 nFrag = 0;                         /* Reduction in fragmentation */
 61191  61317     u16 iOrigSize = iSize;                /* Original value of iSize */
 61192         -  u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */
        61318  +  u16 x;                                /* Offset to cell content area */
 61193  61319     u32 iEnd = iStart + iSize;            /* First byte past the iStart buffer */
 61194  61320     unsigned char *data = pPage->aData;   /* Page content */
 61195  61321   
 61196  61322     assert( pPage->pBt!=0 );
 61197  61323     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 61198  61324     assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
 61199  61325     assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
 61200  61326     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 61201  61327     assert( iSize>=4 );   /* Minimum cell size is 4 */
 61202         -  assert( iStart<=iLast );
 61203         -
 61204         -  /* Overwrite deleted information with zeros when the secure_delete
 61205         -  ** option is enabled */
 61206         -  if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
 61207         -    memset(&data[iStart], 0, iSize);
 61208         -  }
        61328  +  assert( iStart<=pPage->pBt->usableSize-4 );
 61209  61329   
 61210  61330     /* The list of freeblocks must be in ascending order.  Find the 
 61211  61331     ** spot on the list where iStart should be inserted.
 61212  61332     */
 61213  61333     hdr = pPage->hdrOffset;
 61214  61334     iPtr = hdr + 1;
 61215  61335     if( data[iPtr+1]==0 && data[iPtr]==0 ){
................................................................................
 61218  61338       while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
 61219  61339         if( iFreeBlk<iPtr+4 ){
 61220  61340           if( iFreeBlk==0 ) break;
 61221  61341           return SQLITE_CORRUPT_PGNO(pPage->pgno);
 61222  61342         }
 61223  61343         iPtr = iFreeBlk;
 61224  61344       }
 61225         -    if( iFreeBlk>iLast ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61345  +    if( iFreeBlk>pPage->pBt->usableSize-4 ){
        61346  +      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61347  +    }
 61226  61348       assert( iFreeBlk>iPtr || iFreeBlk==0 );
 61227  61349     
 61228  61350       /* At this point:
 61229  61351       **    iFreeBlk:   First freeblock after iStart, or zero if none
 61230  61352       **    iPtr:       The address of a pointer to iFreeBlk
 61231  61353       **
 61232  61354       ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
................................................................................
 61254  61376           iSize = iEnd - iPtr;
 61255  61377           iStart = iPtr;
 61256  61378         }
 61257  61379       }
 61258  61380       if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
 61259  61381       data[hdr+7] -= nFrag;
 61260  61382     }
 61261         -  if( iStart==get2byte(&data[hdr+5]) ){
        61383  +  x = get2byte(&data[hdr+5]);
        61384  +  if( iStart<=x ){
 61262  61385       /* The new freeblock is at the beginning of the cell content area,
 61263  61386       ** so just extend the cell content area rather than create another
 61264  61387       ** freelist entry */
 61265         -    if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61388  +    if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
 61266  61389       put2byte(&data[hdr+1], iFreeBlk);
 61267  61390       put2byte(&data[hdr+5], iEnd);
 61268  61391     }else{
 61269  61392       /* Insert the new freeblock into the freelist */
 61270  61393       put2byte(&data[iPtr], iStart);
 61271         -    put2byte(&data[iStart], iFreeBlk);
 61272         -    put2byte(&data[iStart+2], iSize);
 61273  61394     }
        61395  +  if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
        61396  +    /* Overwrite deleted information with zeros when the secure_delete
        61397  +    ** option is enabled */
        61398  +    memset(&data[iStart], 0, iSize);
        61399  +  }
        61400  +  put2byte(&data[iStart], iFreeBlk);
        61401  +  put2byte(&data[iStart+2], iSize);
 61274  61402     pPage->nFree += iOrigSize;
 61275  61403     return SQLITE_OK;
 61276  61404   }
 61277  61405   
 61278  61406   /*
 61279  61407   ** Decode the flags byte (the first byte of the header) for a page
 61280  61408   ** and initialize fields of the MemPage structure accordingly.
................................................................................
 61581  61709   ** error, return ((unsigned int)-1).
 61582  61710   */
 61583  61711   static Pgno btreePagecount(BtShared *pBt){
 61584  61712     return pBt->nPage;
 61585  61713   }
 61586  61714   SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
 61587  61715     assert( sqlite3BtreeHoldsMutex(p) );
 61588         -  assert( ((p->pBt->nPage)&0x8000000)==0 );
        61716  +  assert( ((p->pBt->nPage)&0x80000000)==0 );
 61589  61717     return btreePagecount(p->pBt);
 61590  61718   }
 61591  61719   
 61592  61720   /*
 61593  61721   ** Get a page from the pager and initialize it.
 61594  61722   **
 61595  61723   ** If pCur!=0 then the page is being fetched as part of a moveToChild()
................................................................................
 61608  61736     MemPage **ppPage,               /* Write the page pointer here */
 61609  61737     BtCursor *pCur,                 /* Cursor to receive the page, or NULL */
 61610  61738     int bReadOnly                   /* True for a read-only page */
 61611  61739   ){
 61612  61740     int rc;
 61613  61741     DbPage *pDbPage;
 61614  61742     assert( sqlite3_mutex_held(pBt->mutex) );
 61615         -  assert( pCur==0 || ppPage==&pCur->apPage[pCur->iPage] );
        61743  +  assert( pCur==0 || ppPage==&pCur->pPage );
 61616  61744     assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
 61617  61745     assert( pCur==0 || pCur->iPage>0 );
 61618  61746   
 61619  61747     if( pgno>btreePagecount(pBt) ){
 61620  61748       rc = SQLITE_CORRUPT_BKPT;
 61621  61749       goto getAndInitPage_error;
 61622  61750     }
................................................................................
 61642  61770       rc = SQLITE_CORRUPT_PGNO(pgno);
 61643  61771       releasePage(*ppPage);
 61644  61772       goto getAndInitPage_error;
 61645  61773     }
 61646  61774     return SQLITE_OK;
 61647  61775   
 61648  61776   getAndInitPage_error:
 61649         -  if( pCur ) pCur->iPage--;
        61777  +  if( pCur ){
        61778  +    pCur->iPage--;
        61779  +    pCur->pPage = pCur->apPage[pCur->iPage];
        61780  +  }
 61650  61781     testcase( pgno==0 );
 61651  61782     assert( pgno!=0 || rc==SQLITE_CORRUPT );
 61652  61783     return rc;
 61653  61784   }
 61654  61785   
 61655  61786   /*
 61656  61787   ** Release a MemPage.  This should be called once for each prior
 61657  61788   ** call to btreeGetPage.
        61789  +**
        61790  +** Page1 is a special case and must be released using releasePageOne().
 61658  61791   */
 61659  61792   static void releasePageNotNull(MemPage *pPage){
 61660  61793     assert( pPage->aData );
 61661  61794     assert( pPage->pBt );
 61662  61795     assert( pPage->pDbPage!=0 );
 61663  61796     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
 61664  61797     assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
 61665  61798     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 61666  61799     sqlite3PagerUnrefNotNull(pPage->pDbPage);
 61667  61800   }
 61668  61801   static void releasePage(MemPage *pPage){
 61669  61802     if( pPage ) releasePageNotNull(pPage);
        61803  +}
        61804  +static void releasePageOne(MemPage *pPage){
        61805  +  assert( pPage!=0 );
        61806  +  assert( pPage->aData );
        61807  +  assert( pPage->pBt );
        61808  +  assert( pPage->pDbPage!=0 );
        61809  +  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
        61810  +  assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
        61811  +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
        61812  +  sqlite3PagerUnrefPageOne(pPage->pDbPage);
 61670  61813   }
 61671  61814   
 61672  61815   /*
 61673  61816   ** Get an unused page.
 61674  61817   **
 61675  61818   ** This works just like btreeGetPage() with the addition:
 61676  61819   **
................................................................................
 62448  62591   
 62449  62592   /*
 62450  62593   ** If the user has not set the safety-level for this database connection
 62451  62594   ** using "PRAGMA synchronous", and if the safety-level is not already
 62452  62595   ** set to the value passed to this function as the second parameter,
 62453  62596   ** set it so.
 62454  62597   */
 62455         -#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS
        62598  +#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
        62599  +    && !defined(SQLITE_OMIT_WAL)
 62456  62600   static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
 62457  62601     sqlite3 *db;
 62458  62602     Db *pDb;
 62459  62603     if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
 62460  62604       while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
 62461  62605       if( pDb->bSyncSet==0 
 62462  62606        && pDb->safety_level!=safety_level 
................................................................................
 62542  62686         int isOpen = 0;
 62543  62687         rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
 62544  62688         if( rc!=SQLITE_OK ){
 62545  62689           goto page1_init_failed;
 62546  62690         }else{
 62547  62691           setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
 62548  62692           if( isOpen==0 ){
 62549         -          releasePage(pPage1);
        62693  +          releasePageOne(pPage1);
 62550  62694             return SQLITE_OK;
 62551  62695           }
 62552  62696         }
 62553  62697         rc = SQLITE_NOTADB;
 62554  62698       }else{
 62555  62699         setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
 62556  62700       }
................................................................................
 62589  62733       if( (u32)pageSize!=pBt->pageSize ){
 62590  62734         /* After reading the first page of the database assuming a page size
 62591  62735         ** of BtShared.pageSize, we have discovered that the page-size is
 62592  62736         ** actually pageSize. Unlock the database, leave pBt->pPage1 at
 62593  62737         ** zero and return SQLITE_OK. The caller will call this function
 62594  62738         ** again with the correct page-size.
 62595  62739         */
 62596         -      releasePage(pPage1);
        62740  +      releasePageOne(pPage1);
 62597  62741         pBt->usableSize = usableSize;
 62598  62742         pBt->pageSize = pageSize;
 62599  62743         freeTempSpace(pBt);
 62600  62744         rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
 62601  62745                                      pageSize-usableSize);
 62602  62746         return rc;
 62603  62747       }
................................................................................
 62643  62787     }
 62644  62788     assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
 62645  62789     pBt->pPage1 = pPage1;
 62646  62790     pBt->nPage = nPage;
 62647  62791     return SQLITE_OK;
 62648  62792   
 62649  62793   page1_init_failed:
 62650         -  releasePage(pPage1);
        62794  +  releasePageOne(pPage1);
 62651  62795     pBt->pPage1 = 0;
 62652  62796     return rc;
 62653  62797   }
 62654  62798   
 62655  62799   #ifndef NDEBUG
 62656  62800   /*
 62657  62801   ** Return the number of cursors open on pBt. This is for use
................................................................................
 62688  62832     assert( sqlite3_mutex_held(pBt->mutex) );
 62689  62833     assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
 62690  62834     if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
 62691  62835       MemPage *pPage1 = pBt->pPage1;
 62692  62836       assert( pPage1->aData );
 62693  62837       assert( sqlite3PagerRefcount(pBt->pPager)==1 );
 62694  62838       pBt->pPage1 = 0;
 62695         -    releasePageNotNull(pPage1);
        62839  +    releasePageOne(pPage1);
 62696  62840     }
 62697  62841   }
 62698  62842   
 62699  62843   /*
 62700  62844   ** If pBt points to an empty file then convert that empty file
 62701  62845   ** into a new empty database by initializing the first page of
 62702  62846   ** the database.
................................................................................
 63540  63684     BtCursor *p;
 63541  63685     int rc = SQLITE_OK;
 63542  63686   
 63543  63687     assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
 63544  63688     if( pBtree ){
 63545  63689       sqlite3BtreeEnter(pBtree);
 63546  63690       for(p=pBtree->pBt->pCursor; p; p=p->pNext){
 63547         -      int i;
 63548  63691         if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
 63549  63692           if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
 63550  63693             rc = saveCursorPosition(p);
 63551  63694             if( rc!=SQLITE_OK ){
 63552  63695               (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
 63553  63696               break;
 63554  63697             }
 63555  63698           }
 63556  63699         }else{
 63557  63700           sqlite3BtreeClearCursor(p);
 63558  63701           p->eState = CURSOR_FAULT;
 63559  63702           p->skipNext = errCode;
 63560  63703         }
 63561         -      for(i=0; i<=p->iPage; i++){
 63562         -        releasePage(p->apPage[i]);
 63563         -        p->apPage[i] = 0;
 63564         -      }
        63704  +      btreeReleaseAllCursorPages(p);
 63565  63705       }
 63566  63706       sqlite3BtreeLeave(pBtree);
 63567  63707     }
 63568  63708     return rc;
 63569  63709   }
 63570  63710   
 63571  63711   /*
................................................................................
 63614  63754       ** sure pPage1->aData is set correctly. */
 63615  63755       if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
 63616  63756         int nPage = get4byte(28+(u8*)pPage1->aData);
 63617  63757         testcase( nPage==0 );
 63618  63758         if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
 63619  63759         testcase( pBt->nPage!=nPage );
 63620  63760         pBt->nPage = nPage;
 63621         -      releasePage(pPage1);
        63761  +      releasePageOne(pPage1);
 63622  63762       }
 63623  63763       assert( countValidCursors(pBt, 1)==0 );
 63624  63764       pBt->inTransaction = TRANS_READ;
 63625  63765       btreeClearHasContent(pBt);
 63626  63766     }
 63627  63767   
 63628  63768     btreeEndTransaction(p);
................................................................................
 63856  63996   /*
 63857  63997   ** Close a cursor.  The read lock on the database file is released
 63858  63998   ** when the last cursor is closed.
 63859  63999   */
 63860  64000   SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
 63861  64001     Btree *pBtree = pCur->pBtree;
 63862  64002     if( pBtree ){
 63863         -    int i;
 63864  64003       BtShared *pBt = pCur->pBt;
 63865  64004       sqlite3BtreeEnter(pBtree);
 63866         -    sqlite3BtreeClearCursor(pCur);
 63867  64005       assert( pBt->pCursor!=0 );
 63868  64006       if( pBt->pCursor==pCur ){
 63869  64007         pBt->pCursor = pCur->pNext;
 63870  64008       }else{
 63871  64009         BtCursor *pPrev = pBt->pCursor;
 63872  64010         do{
 63873  64011           if( pPrev->pNext==pCur ){
 63874  64012             pPrev->pNext = pCur->pNext;
 63875  64013             break;
 63876  64014           }
 63877  64015           pPrev = pPrev->pNext;
 63878  64016         }while( ALWAYS(pPrev) );
 63879  64017       }
 63880         -    for(i=0; i<=pCur->iPage; i++){
 63881         -      releasePage(pCur->apPage[i]);
 63882         -    }
        64018  +    btreeReleaseAllCursorPages(pCur);
 63883  64019       unlockBtreeIfUnused(pBt);
 63884  64020       sqlite3_free(pCur->aOverflow);
 63885         -    /* sqlite3_free(pCur); */
        64021  +    sqlite3_free(pCur->pKey);
 63886  64022       sqlite3BtreeLeave(pBtree);
 63887  64023     }
 63888  64024     return SQLITE_OK;
 63889  64025   }
 63890  64026   
 63891  64027   /*
 63892  64028   ** Make sure the BtCursor* given in the argument has a valid
................................................................................
 63895  64031   **
 63896  64032   ** BtCursor.info is a cache of the information in the current cell.
 63897  64033   ** Using this cache reduces the number of calls to btreeParseCell().
 63898  64034   */
 63899  64035   #ifndef NDEBUG
 63900  64036     static void assertCellInfo(BtCursor *pCur){
 63901  64037       CellInfo info;
 63902         -    int iPage = pCur->iPage;
 63903  64038       memset(&info, 0, sizeof(info));
 63904         -    btreeParseCell(pCur->apPage[iPage], pCur->ix, &info);
        64039  +    btreeParseCell(pCur->pPage, pCur->ix, &info);
 63905  64040       assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
 63906  64041     }
 63907  64042   #else
 63908  64043     #define assertCellInfo(x)
 63909  64044   #endif
 63910  64045   static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
 63911  64046     if( pCur->info.nSize==0 ){
 63912         -    int iPage = pCur->iPage;
 63913  64047       pCur->curFlags |= BTCF_ValidNKey;
 63914         -    btreeParseCell(pCur->apPage[iPage],pCur->ix,&pCur->info);
        64048  +    btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
 63915  64049     }else{
 63916  64050       assertCellInfo(pCur);
 63917  64051     }
 63918  64052   }
 63919  64053   
 63920  64054   #ifndef NDEBUG  /* The next routine used only within assert() statements */
 63921  64055   /*
................................................................................
 64105  64239     u32 amt,             /* Read this many bytes */
 64106  64240     unsigned char *pBuf, /* Write the bytes into this buffer */ 
 64107  64241     int eOp              /* zero to read. non-zero to write. */
 64108  64242   ){
 64109  64243     unsigned char *aPayload;
 64110  64244     int rc = SQLITE_OK;
 64111  64245     int iIdx = 0;
 64112         -  MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
        64246  +  MemPage *pPage = pCur->pPage;               /* Btree page of current entry */
 64113  64247     BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
 64114  64248   #ifdef SQLITE_DIRECT_OVERFLOW_READ
 64115  64249     unsigned char * const pBufStart = pBuf;     /* Start of original out buffer */
 64116  64250   #endif
 64117  64251   
 64118  64252     assert( pPage );
 64119  64253     assert( eOp==0 || eOp==1 );
................................................................................
 64301  64435   ** Return SQLITE_OK on success or an error code if anything goes
 64302  64436   ** wrong.  An error is returned if "offset+amt" is larger than
 64303  64437   ** the available payload.
 64304  64438   */
 64305  64439   SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
 64306  64440     assert( cursorHoldsMutex(pCur) );
 64307  64441     assert( pCur->eState==CURSOR_VALID );
 64308         -  assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
 64309         -  assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
        64442  +  assert( pCur->iPage>=0 && pCur->pPage );
        64443  +  assert( pCur->ix<pCur->pPage->nCell );
 64310  64444     return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
 64311  64445   }
 64312  64446   
 64313  64447   /*
 64314  64448   ** This variant of sqlite3BtreePayload() works even if the cursor has not
 64315  64449   ** in the CURSOR_VALID state.  It is only used by the sqlite3_blob_read()
 64316  64450   ** interface.
................................................................................
 64359  64493   ** page of the database.  The data might change or move the next time
 64360  64494   ** any btree routine is called.
 64361  64495   */
 64362  64496   static const void *fetchPayload(
 64363  64497     BtCursor *pCur,      /* Cursor pointing to entry to read from */
 64364  64498     u32 *pAmt            /* Write the number of available bytes here */
 64365  64499   ){
 64366         -  u32 amt;
 64367         -  assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
        64500  +  int amt;
        64501  +  assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
 64368  64502     assert( pCur->eState==CURSOR_VALID );
 64369  64503     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 64370  64504     assert( cursorOwnsBtShared(pCur) );
 64371         -  assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
        64505  +  assert( pCur->ix<pCur->pPage->nCell );
 64372  64506     assert( pCur->info.nSize>0 );
 64373         -  assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB );
 64374         -  assert( pCur->info.pPayload<pCur->apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB);
 64375         -  amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload);
 64376         -  if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal;
 64377         -  *pAmt = amt;
        64507  +  assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
        64508  +  assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
        64509  +  amt = pCur->info.nLocal;
        64510  +  if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
        64511  +    /* There is too little space on the page for the expected amount
        64512  +    ** of local content. Database must be corrupt. */
        64513  +    assert( CORRUPT_DB );
        64514  +    amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
        64515  +  }
        64516  +  *pAmt = (u32)amt;
 64378  64517     return (void*)pCur->info.pPayload;
 64379  64518   }
 64380  64519   
 64381  64520   
 64382  64521   /*
 64383  64522   ** For the entry that cursor pCur is point to, return as
 64384  64523   ** many bytes of the key or data as are available on the local
................................................................................
 64415  64554     assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
 64416  64555     assert( pCur->iPage>=0 );
 64417  64556     if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
 64418  64557       return SQLITE_CORRUPT_BKPT;
 64419  64558     }
 64420  64559     pCur->info.nSize = 0;
 64421  64560     pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
 64422         -  pCur->aiIdx[pCur->iPage++] = pCur->ix;
        64561  +  pCur->aiIdx[pCur->iPage] = pCur->ix;
        64562  +  pCur->apPage[pCur->iPage] = pCur->pPage;
 64423  64563     pCur->ix = 0;
 64424         -  return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage],
 64425         -                        pCur, pCur->curPagerFlags);
        64564  +  pCur->iPage++;
        64565  +  return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
 64426  64566   }
 64427  64567   
 64428  64568   #ifdef SQLITE_DEBUG
 64429  64569   /*
 64430  64570   ** Page pParent is an internal (non-leaf) tree page. This function 
 64431  64571   ** asserts that page number iChild is the left-child if the iIdx'th
 64432  64572   ** cell in page pParent. Or, if iIdx is equal to the total number of
................................................................................
 64452  64592   **
 64453  64593   ** pCur->idx is set to the cell index that contains the pointer
 64454  64594   ** to the page we are coming from.  If we are coming from the
 64455  64595   ** right-most child page then pCur->idx is set to one more than
 64456  64596   ** the largest cell index.
 64457  64597   */
 64458  64598   static void moveToParent(BtCursor *pCur){
        64599  +  MemPage *pLeaf;
 64459  64600     assert( cursorOwnsBtShared(pCur) );
 64460  64601     assert( pCur->eState==CURSOR_VALID );
 64461  64602     assert( pCur->iPage>0 );
 64462         -  assert( pCur->apPage[pCur->iPage] );
        64603  +  assert( pCur->pPage );
 64463  64604     assertParentIndex(
 64464  64605       pCur->apPage[pCur->iPage-1], 
 64465  64606       pCur->aiIdx[pCur->iPage-1], 
 64466         -    pCur->apPage[pCur->iPage]->pgno
        64607  +    pCur->pPage->pgno
 64467  64608     );
 64468  64609     testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
 64469  64610     pCur->info.nSize = 0;
 64470  64611     pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
 64471  64612     pCur->ix = pCur->aiIdx[pCur->iPage-1];
 64472         -  releasePageNotNull(pCur->apPage[pCur->iPage--]);
        64613  +  pLeaf = pCur->pPage;
        64614  +  pCur->pPage = pCur->apPage[--pCur->iPage];
        64615  +  releasePageNotNull(pLeaf);
 64473  64616   }
 64474  64617   
 64475  64618   /*
 64476  64619   ** Move the cursor to point to the root page of its b-tree structure.
 64477  64620   **
 64478  64621   ** If the table has a virtual root page, then the cursor is moved to point
 64479  64622   ** to the virtual root page instead of the actual root page. A table has a
 64480  64623   ** virtual root page when the actual root page contains no cells and a 
 64481  64624   ** single child page. This can only happen with the table rooted at page 1.
 64482  64625   **
 64483  64626   ** If the b-tree structure is empty, the cursor state is set to 
 64484         -** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
 64485         -** cell located on the root (or virtual root) page and the cursor state
 64486         -** is set to CURSOR_VALID.
        64627  +** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
        64628  +** the cursor is set to point to the first cell located on the root
        64629  +** (or virtual root) page and the cursor state is set to CURSOR_VALID.
 64487  64630   **
 64488  64631   ** If this function returns successfully, it may be assumed that the
 64489  64632   ** page-header flags indicate that the [virtual] root-page is the expected 
 64490  64633   ** kind of b-tree page (i.e. if when opening the cursor the caller did not
 64491  64634   ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
 64492  64635   ** indicating a table b-tree, or if the caller did specify a KeyInfo 
 64493  64636   ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
................................................................................
 64497  64640     MemPage *pRoot;
 64498  64641     int rc = SQLITE_OK;
 64499  64642   
 64500  64643     assert( cursorOwnsBtShared(pCur) );
 64501  64644     assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
 64502  64645     assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
 64503  64646     assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
 64504         -  if( pCur->eState>=CURSOR_REQUIRESEEK ){
 64505         -    if( pCur->eState==CURSOR_FAULT ){
 64506         -      assert( pCur->skipNext!=SQLITE_OK );
 64507         -      return pCur->skipNext;
 64508         -    }
 64509         -    sqlite3BtreeClearCursor(pCur);
 64510         -  }
        64647  +  assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
        64648  +  assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
 64511  64649   
 64512  64650     if( pCur->iPage>=0 ){
 64513  64651       if( pCur->iPage ){
 64514         -      do{
 64515         -        assert( pCur->apPage[pCur->iPage]!=0 );
 64516         -        releasePageNotNull(pCur->apPage[pCur->iPage--]);
 64517         -      }while( pCur->iPage);
        64652  +      releasePageNotNull(pCur->pPage);
        64653  +      while( --pCur->iPage ){
        64654  +        releasePageNotNull(pCur->apPage[pCur->iPage]);
        64655  +      }
        64656  +      pCur->pPage = pCur->apPage[0];
 64518  64657         goto skip_init;
 64519  64658       }
 64520  64659     }else if( pCur->pgnoRoot==0 ){
 64521  64660       pCur->eState = CURSOR_INVALID;
 64522         -    return SQLITE_OK;
        64661  +    return SQLITE_EMPTY;
 64523  64662     }else{
 64524  64663       assert( pCur->iPage==(-1) );
 64525         -    rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
        64664  +    if( pCur->eState>=CURSOR_REQUIRESEEK ){
        64665  +      if( pCur->eState==CURSOR_FAULT ){
        64666  +        assert( pCur->skipNext!=SQLITE_OK );
        64667  +        return pCur->skipNext;
        64668  +      }
        64669  +      sqlite3BtreeClearCursor(pCur);
        64670  +    }
        64671  +    rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
 64526  64672                           0, pCur->curPagerFlags);
 64527  64673       if( rc!=SQLITE_OK ){
 64528  64674         pCur->eState = CURSOR_INVALID;
 64529         -       return rc;
        64675  +      return rc;
 64530  64676       }
 64531  64677       pCur->iPage = 0;
 64532         -    pCur->curIntKey = pCur->apPage[0]->intKey;
        64678  +    pCur->curIntKey = pCur->pPage->intKey;
 64533  64679     }
 64534         -  pRoot = pCur->apPage[0];
        64680  +  pRoot = pCur->pPage;
 64535  64681     assert( pRoot->pgno==pCur->pgnoRoot );
 64536  64682   
 64537  64683     /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
 64538  64684     ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
 64539  64685     ** NULL, the caller expects a table b-tree. If this is not the case,
 64540  64686     ** return an SQLITE_CORRUPT error. 
 64541  64687     **
................................................................................
 64542  64688     ** Earlier versions of SQLite assumed that this test could not fail
 64543  64689     ** if the root page was already loaded when this function was called (i.e.
 64544  64690     ** if pCur->iPage>=0). But this is not so if the database is corrupted 
 64545  64691     ** in such a way that page pRoot is linked into a second b-tree table 
 64546  64692     ** (or the freelist).  */
 64547  64693     assert( pRoot->intKey==1 || pRoot->intKey==0 );
 64548  64694     if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
 64549         -    return SQLITE_CORRUPT_PGNO(pCur->apPage[pCur->iPage]->pgno);
        64695  +    return SQLITE_CORRUPT_PGNO(pCur->pPage->pgno);
 64550  64696     }
 64551  64697   
 64552  64698   skip_init:  
 64553  64699     pCur->ix = 0;
 64554  64700     pCur->info.nSize = 0;
 64555  64701     pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
 64556  64702   
 64557         -  pRoot = pCur->apPage[0];
        64703  +  pRoot = pCur->pPage;
 64558  64704     if( pRoot->nCell>0 ){
 64559  64705       pCur->eState = CURSOR_VALID;
 64560  64706     }else if( !pRoot->leaf ){
 64561  64707       Pgno subpage;
 64562  64708       if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
 64563  64709       subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
 64564  64710       pCur->eState = CURSOR_VALID;
 64565  64711       rc = moveToChild(pCur, subpage);
 64566  64712     }else{
 64567  64713       pCur->eState = CURSOR_INVALID;
        64714  +    rc = SQLITE_EMPTY;
 64568  64715     }
 64569  64716     return rc;
 64570  64717   }
 64571  64718   
 64572  64719   /*
 64573  64720   ** Move the cursor down to the left-most leaf entry beneath the
 64574  64721   ** entry to which it is currently pointing.
................................................................................
 64579  64726   static int moveToLeftmost(BtCursor *pCur){
 64580  64727     Pgno pgno;
 64581  64728     int rc = SQLITE_OK;
 64582  64729     MemPage *pPage;
 64583  64730   
 64584  64731     assert( cursorOwnsBtShared(pCur) );
 64585  64732     assert( pCur->eState==CURSOR_VALID );
 64586         -  while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
        64733  +  while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
 64587  64734       assert( pCur->ix<pPage->nCell );
 64588  64735       pgno = get4byte(findCell(pPage, pCur->ix));
 64589  64736       rc = moveToChild(pCur, pgno);
 64590  64737     }
 64591  64738     return rc;
 64592  64739   }
 64593  64740   
................................................................................
 64604  64751   static int moveToRightmost(BtCursor *pCur){
 64605  64752     Pgno pgno;
 64606  64753     int rc = SQLITE_OK;
 64607  64754     MemPage *pPage = 0;
 64608  64755   
 64609  64756     assert( cursorOwnsBtShared(pCur) );
 64610  64757     assert( pCur->eState==CURSOR_VALID );
 64611         -  while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){
        64758  +  while( !(pPage = pCur->pPage)->leaf ){
 64612  64759       pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 64613  64760       pCur->ix = pPage->nCell;
 64614  64761       rc = moveToChild(pCur, pgno);
 64615  64762       if( rc ) return rc;
 64616  64763     }
 64617  64764     pCur->ix = pPage->nCell-1;
 64618  64765     assert( pCur->info.nSize==0 );
................................................................................
 64627  64774   SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
 64628  64775     int rc;
 64629  64776   
 64630  64777     assert( cursorOwnsBtShared(pCur) );
 64631  64778     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 64632  64779     rc = moveToRoot(pCur);
 64633  64780     if( rc==SQLITE_OK ){
 64634         -    if( pCur->eState==CURSOR_INVALID ){
 64635         -      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 64636         -      *pRes = 1;
 64637         -    }else{
 64638         -      assert( pCur->apPage[pCur->iPage]->nCell>0 );
 64639         -      *pRes = 0;
 64640         -      rc = moveToLeftmost(pCur);
 64641         -    }
        64781  +    assert( pCur->pPage->nCell>0 );
        64782  +    *pRes = 0;
        64783  +    rc = moveToLeftmost(pCur);
        64784  +  }else if( rc==SQLITE_EMPTY ){
        64785  +    assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
        64786  +    *pRes = 1;
        64787  +    rc = SQLITE_OK;
 64642  64788     }
 64643  64789     return rc;
 64644  64790   }
 64645  64791   
 64646  64792   /* Move the cursor to the last entry in the table.  Return SQLITE_OK
 64647  64793   ** on success.  Set *pRes to 0 if the cursor actually points to something
 64648  64794   ** or set *pRes to 1 if the table is empty.
................................................................................
 64658  64804   #ifdef SQLITE_DEBUG
 64659  64805       /* This block serves to assert() that the cursor really does point 
 64660  64806       ** to the last entry in the b-tree. */
 64661  64807       int ii;
 64662  64808       for(ii=0; ii<pCur->iPage; ii++){
 64663  64809         assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
 64664  64810       }
 64665         -    assert( pCur->ix==pCur->apPage[pCur->iPage]->nCell-1 );
 64666         -    assert( pCur->apPage[pCur->iPage]->leaf );
        64811  +    assert( pCur->ix==pCur->pPage->nCell-1 );
        64812  +    assert( pCur->pPage->leaf );
 64667  64813   #endif
 64668  64814       return SQLITE_OK;
 64669  64815     }
 64670  64816   
 64671  64817     rc = moveToRoot(pCur);
 64672  64818     if( rc==SQLITE_OK ){
 64673         -    if( CURSOR_INVALID==pCur->eState ){
 64674         -      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 64675         -      *pRes = 1;
        64819  +    assert( pCur->eState==CURSOR_VALID );
        64820  +    *pRes = 0;
        64821  +    rc = moveToRightmost(pCur);
        64822  +    if( rc==SQLITE_OK ){
        64823  +      pCur->curFlags |= BTCF_AtLast;
 64676  64824       }else{
 64677         -      assert( pCur->eState==CURSOR_VALID );
 64678         -      *pRes = 0;
 64679         -      rc = moveToRightmost(pCur);
 64680         -      if( rc==SQLITE_OK ){
 64681         -        pCur->curFlags |= BTCF_AtLast;
 64682         -      }else{
 64683         -        pCur->curFlags &= ~BTCF_AtLast;
 64684         -      }
 64685         -   
        64825  +      pCur->curFlags &= ~BTCF_AtLast;
 64686  64826       }
        64827  +  }else if( rc==SQLITE_EMPTY ){
        64828  +    assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
        64829  +    *pRes = 1;
        64830  +    rc = SQLITE_OK;
 64687  64831     }
 64688  64832     return rc;
 64689  64833   }
 64690  64834   
 64691  64835   /* Move the cursor so that it points to an entry near the key 
 64692  64836   ** specified by pIdxKey or intKey.   Return a success code.
 64693  64837   **
................................................................................
 64778  64922       );
 64779  64923     }else{
 64780  64924       xRecordCompare = 0; /* All keys are integers */
 64781  64925     }
 64782  64926   
 64783  64927     rc = moveToRoot(pCur);
 64784  64928     if( rc ){
        64929  +    if( rc==SQLITE_EMPTY ){
        64930  +      assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
        64931  +      *pRes = -1;
        64932  +      return SQLITE_OK;
        64933  +    }
 64785  64934       return rc;
 64786  64935     }
 64787         -  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
 64788         -  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
 64789         -  assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
 64790         -  if( pCur->eState==CURSOR_INVALID ){
 64791         -    *pRes = -1;
 64792         -    assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 64793         -    return SQLITE_OK;
 64794         -  }
 64795         -  assert( pCur->apPage[0]->intKey==pCur->curIntKey );
        64936  +  assert( pCur->pPage );
        64937  +  assert( pCur->pPage->isInit );
        64938  +  assert( pCur->eState==CURSOR_VALID );
        64939  +  assert( pCur->pPage->nCell > 0 );
        64940  +  assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
 64796  64941     assert( pCur->curIntKey || pIdxKey );
 64797  64942     for(;;){
 64798  64943       int lwr, upr, idx, c;
 64799  64944       Pgno chldPg;
 64800         -    MemPage *pPage = pCur->apPage[pCur->iPage];
        64945  +    MemPage *pPage = pCur->pPage;
 64801  64946       u8 *pCell;                          /* Pointer to current cell in pPage */
 64802  64947   
 64803  64948       /* pPage->nCell must be greater than zero. If this is the root-page
 64804  64949       ** the cursor would have been INVALID above and this for(;;) loop
 64805  64950       ** not run. If this is not the root-page, then the moveToChild() routine
 64806  64951       ** would have already detected db corruption. Similarly, pPage must
 64807  64952       ** be the right kind (index or table) of b-tree page. Otherwise
................................................................................
 64921  65066           }else if( c>0 ){
 64922  65067             upr = idx-1;
 64923  65068           }else{
 64924  65069             assert( c==0 );
 64925  65070             *pRes = 0;
 64926  65071             rc = SQLITE_OK;
 64927  65072             pCur->ix = (u16)idx;
 64928         -          if( pIdxKey->errCode ) rc = SQLITE_CORRUPT;
        65073  +          if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
 64929  65074             goto moveto_finish;
 64930  65075           }
 64931  65076           if( lwr>upr ) break;
 64932  65077           assert( lwr+upr>=0 );
 64933  65078           idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2 */
 64934  65079         }
 64935  65080       }
 64936  65081       assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
 64937  65082       assert( pPage->isInit );
 64938  65083       if( pPage->leaf ){
 64939         -      assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
        65084  +      assert( pCur->ix<pCur->pPage->nCell );
 64940  65085         pCur->ix = (u16)idx;
 64941  65086         *pRes = c;
 64942  65087         rc = SQLITE_OK;
 64943  65088         goto moveto_finish;
 64944  65089       }
 64945  65090   moveto_next_layer:
 64946  65091       if( lwr>=pPage->nCell ){
................................................................................
 64986  65131     assert( cursorOwnsBtShared(pCur) );
 64987  65132     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 64988  65133   
 64989  65134     /* Currently this interface is only called by the OP_IfSmaller
 64990  65135     ** opcode, and it that case the cursor will always be valid and
 64991  65136     ** will always point to a leaf node. */
 64992  65137     if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
 64993         -  if( NEVER(pCur->apPage[pCur->iPage]->leaf==0) ) return -1;
        65138  +  if( NEVER(pCur->pPage->leaf==0) ) return -1;
 64994  65139   
 64995         -  for(n=1, i=0; i<=pCur->iPage; i++){
        65140  +  n = pCur->pPage->nCell;
        65141  +  for(i=0; i<pCur->iPage; i++){
 64996  65142       n *= pCur->apPage[i]->nCell;
 64997  65143     }
 64998  65144     return n;
 64999  65145   }
 65000  65146   
 65001  65147   /*
 65002  65148   ** Advance the cursor to the next entry in the database. 
................................................................................
 65041  65187           pCur->skipNext = 0;
 65042  65188           return SQLITE_OK;
 65043  65189         }
 65044  65190         pCur->skipNext = 0;
 65045  65191       }
 65046  65192     }
 65047  65193   
 65048         -  pPage = pCur->apPage[pCur->iPage];
        65194  +  pPage = pCur->pPage;
 65049  65195     idx = ++pCur->ix;
 65050  65196     assert( pPage->isInit );
 65051  65197   
 65052  65198     /* If the database file is corrupt, it is possible for the value of idx 
 65053  65199     ** to be invalid here. This can only occur if a second cursor modifies
 65054  65200     ** the page while cursor pCur is holding a reference to it. Which can
 65055  65201     ** only happen if the database is corrupt in such a way as to link the
................................................................................
 65064  65210       }
 65065  65211       do{
 65066  65212         if( pCur->iPage==0 ){
 65067  65213           pCur->eState = CURSOR_INVALID;
 65068  65214           return SQLITE_DONE;
 65069  65215         }
 65070  65216         moveToParent(pCur);
 65071         -      pPage = pCur->apPage[pCur->iPage];
        65217  +      pPage = pCur->pPage;
 65072  65218       }while( pCur->ix>=pPage->nCell );
 65073  65219       if( pPage->intKey ){
 65074  65220         return sqlite3BtreeNext(pCur, 0);
 65075  65221       }else{
 65076  65222         return SQLITE_OK;
 65077  65223       }
 65078  65224     }
................................................................................
 65087  65233     UNUSED_PARAMETER( flags );  /* Used in COMDB2 but not native SQLite */
 65088  65234     assert( cursorOwnsBtShared(pCur) );
 65089  65235     assert( flags==0 || flags==1 );
 65090  65236     assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
 65091  65237     pCur->info.nSize = 0;
 65092  65238     pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
 65093  65239     if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
 65094         -  pPage = pCur->apPage[pCur->iPage];
        65240  +  pPage = pCur->pPage;
 65095  65241     if( (++pCur->ix)>=pPage->nCell ){
 65096  65242       pCur->ix--;
 65097  65243       return btreeNext(pCur);
 65098  65244     }
 65099  65245     if( pPage->leaf ){
 65100  65246       return SQLITE_OK;
 65101  65247     }else{
................................................................................
 65146  65292           pCur->skipNext = 0;
 65147  65293           return SQLITE_OK;
 65148  65294         }
 65149  65295         pCur->skipNext = 0;
 65150  65296       }
 65151  65297     }
 65152  65298   
 65153         -  pPage = pCur->apPage[pCur->iPage];
        65299  +  pPage = pCur->pPage;
 65154  65300     assert( pPage->isInit );
 65155  65301     if( !pPage->leaf ){
 65156  65302       int idx = pCur->ix;
 65157  65303       rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
 65158  65304       if( rc ) return rc;
 65159  65305       rc = moveToRightmost(pCur);
 65160  65306     }else{
................................................................................
 65165  65311         }
 65166  65312         moveToParent(pCur);
 65167  65313       }
 65168  65314       assert( pCur->info.nSize==0 );
 65169  65315       assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
 65170  65316   
 65171  65317       pCur->ix--;
 65172         -    pPage = pCur->apPage[pCur->iPage];
        65318  +    pPage = pCur->pPage;
 65173  65319       if( pPage->intKey && !pPage->leaf ){
 65174  65320         rc = sqlite3BtreePrevious(pCur, 0);
 65175  65321       }else{
 65176  65322         rc = SQLITE_OK;
 65177  65323       }
 65178  65324     }
 65179  65325     return rc;
................................................................................
 65183  65329     assert( flags==0 || flags==1 );
 65184  65330     assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
 65185  65331     UNUSED_PARAMETER( flags );  /* Used in COMDB2 but not native SQLite */
 65186  65332     pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
 65187  65333     pCur->info.nSize = 0;
 65188  65334     if( pCur->eState!=CURSOR_VALID
 65189  65335      || pCur->ix==0
 65190         -   || pCur->apPage[pCur->iPage]->leaf==0
        65336  +   || pCur->pPage->leaf==0
 65191  65337     ){
 65192  65338       return btreePrevious(pCur);
 65193  65339     }
 65194  65340     pCur->ix--;
 65195  65341     return SQLITE_OK;
 65196  65342   }
 65197  65343   
................................................................................
 65679  65825   ** overflow) into *pnSize.
 65680  65826   */
 65681  65827   static int clearCell(
 65682  65828     MemPage *pPage,          /* The page that contains the Cell */
 65683  65829     unsigned char *pCell,    /* First byte of the Cell */
 65684  65830     CellInfo *pInfo          /* Size information about the cell */
 65685  65831   ){
 65686         -  BtShared *pBt = pPage->pBt;
        65832  +  BtShared *pBt;
 65687  65833     Pgno ovflPgno;
 65688  65834     int rc;
 65689  65835     int nOvfl;
 65690  65836     u32 ovflPageSize;
 65691  65837   
 65692  65838     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 65693  65839     pPage->xParseCell(pPage, pCell, pInfo);
................................................................................
 65695  65841       return SQLITE_OK;  /* No overflow pages. Return without doing anything */
 65696  65842     }
 65697  65843     if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
 65698  65844       /* Cell extends past end of page */
 65699  65845       return SQLITE_CORRUPT_PGNO(pPage->pgno);
 65700  65846     }
 65701  65847     ovflPgno = get4byte(pCell + pInfo->nSize - 4);
        65848  +  pBt = pPage->pBt;
 65702  65849     assert( pBt->usableSize > 4 );
 65703  65850     ovflPageSize = pBt->usableSize - 4;
 65704  65851     nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
 65705  65852     assert( nOvfl>0 || 
 65706  65853       (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
 65707  65854     );
 65708  65855     while( nOvfl-- ){
................................................................................
 65762  65909     MemPage *pPage,                /* The page that contains the cell */
 65763  65910     unsigned char *pCell,          /* Complete text of the cell */
 65764  65911     const BtreePayload *pX,        /* Payload with which to construct the cell */
 65765  65912     int *pnSize                    /* Write cell size here */
 65766  65913   ){
 65767  65914     int nPayload;
 65768  65915     const u8 *pSrc;
 65769         -  int nSrc, n, rc;
        65916  +  int nSrc, n, rc, mn;
 65770  65917     int spaceLeft;
 65771         -  MemPage *pOvfl = 0;
 65772         -  MemPage *pToRelease = 0;
        65918  +  MemPage *pToRelease;
 65773  65919     unsigned char *pPrior;
 65774  65920     unsigned char *pPayload;
 65775         -  BtShared *pBt = pPage->pBt;
 65776         -  Pgno pgnoOvfl = 0;
        65921  +  BtShared *pBt;
        65922  +  Pgno pgnoOvfl;
 65777  65923     int nHeader;
 65778  65924   
 65779  65925     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 65780  65926   
 65781  65927     /* pPage is not necessarily writeable since pCell might be auxiliary
 65782  65928     ** buffer space that is separate from the pPage buffer area */
 65783         -  assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
        65929  +  assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
 65784  65930               || sqlite3PagerIswriteable(pPage->pDbPage) );
 65785  65931   
 65786  65932     /* Fill in the header. */
 65787  65933     nHeader = pPage->childPtrSize;
 65788  65934     if( pPage->intKey ){
 65789  65935       nPayload = pX->nData + pX->nZero;
 65790  65936       pSrc = pX->pData;
................................................................................
 65796  65942       assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
 65797  65943       nSrc = nPayload = (int)pX->nKey;
 65798  65944       pSrc = pX->pKey;
 65799  65945       nHeader += putVarint32(&pCell[nHeader], nPayload);
 65800  65946     }
 65801  65947     
 65802  65948     /* Fill in the payload */
        65949  +  pPayload = &pCell[nHeader];
 65803  65950     if( nPayload<=pPage->maxLocal ){
        65951  +    /* This is the common case where everything fits on the btree page
        65952  +    ** and no overflow pages are required. */
 65804  65953       n = nHeader + nPayload;
 65805  65954       testcase( n==3 );
 65806  65955       testcase( n==4 );
 65807  65956       if( n<4 ) n = 4;
 65808  65957       *pnSize = n;
 65809         -    spaceLeft = nPayload;
 65810         -    pPrior = pCell;
 65811         -  }else{
 65812         -    int mn = pPage->minLocal;
 65813         -    n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
 65814         -    testcase( n==pPage->maxLocal );
 65815         -    testcase( n==pPage->maxLocal+1 );
 65816         -    if( n > pPage->maxLocal ) n = mn;
 65817         -    spaceLeft = n;
 65818         -    *pnSize = n + nHeader + 4;
 65819         -    pPrior = &pCell[nHeader+n];
        65958  +    assert( nSrc<=nPayload );
        65959  +    testcase( nSrc<nPayload );
        65960  +    memcpy(pPayload, pSrc, nSrc);
        65961  +    memset(pPayload+nSrc, 0, nPayload-nSrc);
        65962  +    return SQLITE_OK;
 65820  65963     }
 65821         -  pPayload = &pCell[nHeader];
        65964  +
        65965  +  /* If we reach this point, it means that some of the content will need
        65966  +  ** to spill onto overflow pages.
        65967  +  */
        65968  +  mn = pPage->minLocal;
        65969  +  n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
        65970  +  testcase( n==pPage->maxLocal );
        65971  +  testcase( n==pPage->maxLocal+1 );
        65972  +  if( n > pPage->maxLocal ) n = mn;
        65973  +  spaceLeft = n;
        65974  +  *pnSize = n + nHeader + 4;
        65975  +  pPrior = &pCell[nHeader+n];
        65976  +  pToRelease = 0;
        65977  +  pgnoOvfl = 0;
        65978  +  pBt = pPage->pBt;
 65822  65979   
 65823  65980     /* At this point variables should be set as follows:
 65824  65981     **
 65825  65982     **   nPayload           Total payload size in bytes
 65826  65983     **   pPayload           Begin writing payload here
 65827  65984     **   spaceLeft          Space available at pPayload.  If nPayload>spaceLeft,
 65828  65985     **                      that means content must spill into overflow pages.
................................................................................
 65840  65997       assert( info.nKey==pX->nKey );
 65841  65998       assert( *pnSize == info.nSize );
 65842  65999       assert( spaceLeft == info.nLocal );
 65843  66000     }
 65844  66001   #endif
 65845  66002   
 65846  66003     /* Write the payload into the local Cell and any extra into overflow pages */
 65847         -  while( nPayload>0 ){
        66004  +  while( 1 ){
        66005  +    n = nPayload;
        66006  +    if( n>spaceLeft ) n = spaceLeft;
        66007  +
        66008  +    /* If pToRelease is not zero than pPayload points into the data area
        66009  +    ** of pToRelease.  Make sure pToRelease is still writeable. */
        66010  +    assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
        66011  +
        66012  +    /* If pPayload is part of the data area of pPage, then make sure pPage
        66013  +    ** is still writeable */
        66014  +    assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
        66015  +            || sqlite3PagerIswriteable(pPage->pDbPage) );
        66016  +
        66017  +    if( nSrc>=n ){
        66018  +      memcpy(pPayload, pSrc, n);
        66019  +    }else if( nSrc>0 ){
        66020  +      n = nSrc;
        66021  +      memcpy(pPayload, pSrc, n);
        66022  +    }else{
        66023  +      memset(pPayload, 0, n);
        66024  +    }
        66025  +    nPayload -= n;
        66026  +    if( nPayload<=0 ) break;
        66027  +    pPayload += n;
        66028  +    pSrc += n;
        66029  +    nSrc -= n;
        66030  +    spaceLeft -= n;
 65848  66031       if( spaceLeft==0 ){
        66032  +      MemPage *pOvfl = 0;
 65849  66033   #ifndef SQLITE_OMIT_AUTOVACUUM
 65850  66034         Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
 65851  66035         if( pBt->autoVacuum ){
 65852  66036           do{
 65853  66037             pgnoOvfl++;
 65854  66038           } while( 
 65855  66039             PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) 
................................................................................
 65894  66078         releasePage(pToRelease);
 65895  66079         pToRelease = pOvfl;
 65896  66080         pPrior = pOvfl->aData;
 65897  66081         put4byte(pPrior, 0);
 65898  66082         pPayload = &pOvfl->aData[4];
 65899  66083         spaceLeft = pBt->usableSize - 4;
 65900  66084       }
 65901         -    n = nPayload;
 65902         -    if( n>spaceLeft ) n = spaceLeft;
 65903         -
 65904         -    /* If pToRelease is not zero than pPayload points into the data area
 65905         -    ** of pToRelease.  Make sure pToRelease is still writeable. */
 65906         -    assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
 65907         -
 65908         -    /* If pPayload is part of the data area of pPage, then make sure pPage
 65909         -    ** is still writeable */
 65910         -    assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
 65911         -            || sqlite3PagerIswriteable(pPage->pDbPage) );
 65912         -
 65913         -    if( nSrc>0 ){
 65914         -      if( n>nSrc ) n = nSrc;
 65915         -      assert( pSrc );
 65916         -      memcpy(pPayload, pSrc, n);
 65917         -    }else{
 65918         -      memset(pPayload, 0, n);
 65919         -    }
 65920         -    nPayload -= n;
 65921         -    pPayload += n;
 65922         -    pSrc += n;
 65923         -    nSrc -= n;
 65924         -    spaceLeft -= n;
 65925  66085     }
 65926  66086     releasePage(pToRelease);
 65927  66087     return SQLITE_OK;
 65928  66088   }
 65929  66089   
 65930  66090   /*
 65931  66091   ** Remove the i-th cell from pPage.  This routine effects pPage only.
................................................................................
 65949  66109     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 65950  66110     data = pPage->aData;
 65951  66111     ptr = &pPage->aCellIdx[2*idx];
 65952  66112     pc = get2byte(ptr);
 65953  66113     hdr = pPage->hdrOffset;
 65954  66114     testcase( pc==get2byte(&data[hdr+5]) );
 65955  66115     testcase( pc+sz==pPage->pBt->usableSize );
 65956         -  if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
        66116  +  if( pc+sz > pPage->pBt->usableSize ){
 65957  66117       *pRC = SQLITE_CORRUPT_BKPT;
 65958  66118       return;
 65959  66119     }
 65960  66120     rc = freeSpace(pPage, pc, sz);
 65961  66121     if( rc ){
 65962  66122       *pRC = rc;
 65963  66123       return;
................................................................................
 66816  66976     ** Allocate space for memory structures
 66817  66977     */
 66818  66978     szScratch =
 66819  66979          nMaxCells*sizeof(u8*)                       /* b.apCell */
 66820  66980        + nMaxCells*sizeof(u16)                       /* b.szCell */
 66821  66981        + pBt->pageSize;                              /* aSpace1 */
 66822  66982   
 66823         -  /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
 66824         -  ** that is more than 6 times the database page size. */
 66825  66983     assert( szScratch<=6*(int)pBt->pageSize );
 66826         -  b.apCell = sqlite3ScratchMalloc( szScratch ); 
        66984  +  b.apCell = sqlite3StackAllocRaw(0, szScratch );
 66827  66985     if( b.apCell==0 ){
 66828  66986       rc = SQLITE_NOMEM_BKPT;
 66829  66987       goto balance_cleanup;
 66830  66988     }
 66831  66989     b.szCell = (u16*)&b.apCell[nMaxCells];
 66832  66990     aSpace1 = (u8*)&b.szCell[nMaxCells];
 66833  66991     assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
................................................................................
 67397  67555     }
 67398  67556   #endif
 67399  67557   
 67400  67558     /*
 67401  67559     ** Cleanup before returning.
 67402  67560     */
 67403  67561   balance_cleanup:
 67404         -  sqlite3ScratchFree(b.apCell);
        67562  +  sqlite3StackFree(0, b.apCell);
 67405  67563     for(i=0; i<nOld; i++){
 67406  67564       releasePage(apOld[i]);
 67407  67565     }
 67408  67566     for(i=0; i<nNew; i++){
 67409  67567       releasePage(apNew[i]);
 67410  67568     }
 67411  67569   
................................................................................
 67496  67654     u8 *pFree = 0;
 67497  67655   
 67498  67656     VVA_ONLY( int balance_quick_called = 0 );
 67499  67657     VVA_ONLY( int balance_deeper_called = 0 );
 67500  67658   
 67501  67659     do {
 67502  67660       int iPage = pCur->iPage;
 67503         -    MemPage *pPage = pCur->apPage[iPage];
        67661  +    MemPage *pPage = pCur->pPage;
 67504  67662   
 67505  67663       if( iPage==0 ){
 67506  67664         if( pPage->nOverflow ){
 67507  67665           /* The root page of the b-tree is overfull. In this case call the
 67508  67666           ** balance_deeper() function to create a new child for the root-page
 67509  67667           ** and copy the current contents of the root-page to it. The
 67510  67668           ** next iteration of the do-loop will balance the child page.
................................................................................
 67512  67670           assert( balance_deeper_called==0 );
 67513  67671           VVA_ONLY( balance_deeper_called++ );
 67514  67672           rc = balance_deeper(pPage, &pCur->apPage[1]);
 67515  67673           if( rc==SQLITE_OK ){
 67516  67674             pCur->iPage = 1;
 67517  67675             pCur->ix = 0;
 67518  67676             pCur->aiIdx[0] = 0;
 67519         -          assert( pCur->apPage[1]->nOverflow );
        67677  +          pCur->apPage[0] = pPage;
        67678  +          pCur->pPage = pCur->apPage[1];
        67679  +          assert( pCur->pPage->nOverflow );
 67520  67680           }
 67521  67681         }else{
 67522  67682           break;
 67523  67683         }
 67524  67684       }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
 67525  67685         break;
 67526  67686       }else{
................................................................................
 67592  67752   
 67593  67753         pPage->nOverflow = 0;
 67594  67754   
 67595  67755         /* The next iteration of the do-loop balances the parent page. */
 67596  67756         releasePage(pPage);
 67597  67757         pCur->iPage--;
 67598  67758         assert( pCur->iPage>=0 );
        67759  +      pCur->pPage = pCur->apPage[pCur->iPage];
 67599  67760       }
 67600  67761     }while( rc==SQLITE_OK );
 67601  67762   
 67602  67763     if( pFree ){
 67603  67764       sqlite3PageFree(pFree);
 67604  67765     }
 67605  67766     return rc;
................................................................................
 67723  67884       }else{
 67724  67885         rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
 67725  67886       }
 67726  67887       if( rc ) return rc;
 67727  67888     }
 67728  67889     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 67729  67890   
 67730         -  pPage = pCur->apPage[pCur->iPage];
        67891  +  pPage = pCur->pPage;
 67731  67892     assert( pPage->intKey || pX->nKey>=0 );
 67732  67893     assert( pPage->leaf || !pPage->intKey );
 67733  67894   
 67734  67895     TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
 67735  67896             pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
 67736  67897             loc==0 ? "overwrite" : "new entry"));
 67737  67898     assert( pPage->isInit );
................................................................................
 67810  67971       pCur->curFlags &= ~(BTCF_ValidNKey);
 67811  67972       rc = balance(pCur);
 67812  67973   
 67813  67974       /* Must make sure nOverflow is reset to zero even if the balance()
 67814  67975       ** fails. Internal data structure corruption will result otherwise. 
 67815  67976       ** Also, set the cursor state to invalid. This stops saveCursorPosition()
 67816  67977       ** from trying to save the current position of the cursor.  */
 67817         -    pCur->apPage[pCur->iPage]->nOverflow = 0;
        67978  +    pCur->pPage->nOverflow = 0;
 67818  67979       pCur->eState = CURSOR_INVALID;
 67819  67980       if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
 67820         -      rc = moveToRoot(pCur);
        67981  +      btreeReleaseAllCursorPages(pCur);
 67821  67982         if( pCur->pKeyInfo ){
 67822  67983           assert( pCur->pKey==0 );
 67823  67984           pCur->pKey = sqlite3Malloc( pX->nKey );
 67824  67985           if( pCur->pKey==0 ){
 67825  67986             rc = SQLITE_NOMEM;
 67826  67987           }else{
 67827  67988             memcpy(pCur->pKey, pX->pKey, pX->nKey);
 67828  67989           }
 67829  67990         }
 67830  67991         pCur->eState = CURSOR_REQUIRESEEK;
 67831  67992         pCur->nKey = pX->nKey;
 67832  67993       }
 67833  67994     }
 67834         -  assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
        67995  +  assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
 67835  67996   
 67836  67997   end_insert:
 67837  67998     return rc;
 67838  67999   }
 67839  68000   
 67840  68001   /*
 67841  68002   ** Delete the entry that the cursor is pointing to. 
................................................................................
 67868  68029   
 67869  68030     assert( cursorOwnsBtShared(pCur) );
 67870  68031     assert( pBt->inTransaction==TRANS_WRITE );
 67871  68032     assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 67872  68033     assert( pCur->curFlags & BTCF_WriteFlag );
 67873  68034     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 67874  68035     assert( !hasReadConflicts(p, pCur->pgnoRoot) );
 67875         -  assert( pCur->ix<pCur->apPage[pCur->iPage]->nCell );
        68036  +  assert( pCur->ix<pCur->pPage->nCell );
 67876  68037     assert( pCur->eState==CURSOR_VALID );
 67877  68038     assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
 67878  68039   
 67879  68040     iCellDepth = pCur->iPage;
 67880  68041     iCellIdx = pCur->ix;
 67881         -  pPage = pCur->apPage[iCellDepth];
        68042  +  pPage = pCur->pPage;
 67882  68043     pCell = findCell(pPage, iCellIdx);
 67883  68044   
 67884  68045     /* If the bPreserve flag is set to true, then the cursor position must
 67885  68046     ** be preserved following this delete operation. If the current delete
 67886  68047     ** will cause a b-tree rebalance, then this is done by saving the cursor
 67887  68048     ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
 67888  68049     ** returning. 
................................................................................
 67940  68101   
 67941  68102     /* If the cell deleted was not located on a leaf page, then the cursor
 67942  68103     ** is currently pointing to the largest entry in the sub-tree headed
 67943  68104     ** by the child-page of the cell that was just deleted from an internal
 67944  68105     ** node. The cell from the leaf node needs to be moved to the internal
 67945  68106     ** node to replace the deleted cell.  */
 67946  68107     if( !pPage->leaf ){
 67947         -    MemPage *pLeaf = pCur->apPage[pCur->iPage];
        68108  +    MemPage *pLeaf = pCur->pPage;
 67948  68109       int nCell;
 67949         -    Pgno n = pCur->apPage[iCellDepth+1]->pgno;
        68110  +    Pgno n;
 67950  68111       unsigned char *pTmp;
 67951  68112   
        68113  +    if( iCellDepth<pCur->iPage-1 ){
        68114  +      n = pCur->apPage[iCellDepth+1]->pgno;
        68115  +    }else{
        68116  +      n = pCur->pPage->pgno;
        68117  +    }
 67952  68118       pCell = findCell(pLeaf, pLeaf->nCell-1);
 67953  68119       if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
 67954  68120       nCell = pLeaf->xCellSize(pLeaf, pCell);
 67955  68121       assert( MX_CELL_SIZE(pBt) >= nCell );
 67956  68122       pTmp = pBt->pTmpSpace;
 67957  68123       assert( pTmp!=0 );
 67958  68124       rc = sqlite3PagerWrite(pLeaf->pDbPage);
................................................................................
 67976  68142     ** on the leaf node first. If the balance proceeds far enough up the
 67977  68143     ** tree that we can be sure that any problem in the internal node has
 67978  68144     ** been corrected, so be it. Otherwise, after balancing the leaf node,
 67979  68145     ** walk the cursor up the tree to the internal node and balance it as 
 67980  68146     ** well.  */
 67981  68147     rc = balance(pCur);
 67982  68148     if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
        68149  +    releasePageNotNull(pCur->pPage);
        68150  +    pCur->iPage--;
 67983  68151       while( pCur->iPage>iCellDepth ){
 67984  68152         releasePage(pCur->apPage[pCur->iPage--]);
 67985  68153       }
        68154  +    pCur->pPage = pCur->apPage[pCur->iPage];
 67986  68155       rc = balance(pCur);
 67987  68156     }
 67988  68157   
 67989  68158     if( rc==SQLITE_OK ){
 67990  68159       if( bSkipnext ){
 67991  68160         assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
 67992         -      assert( pPage==pCur->apPage[pCur->iPage] || CORRUPT_DB );
        68161  +      assert( pPage==pCur->pPage || CORRUPT_DB );
 67993  68162         assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
 67994  68163         pCur->eState = CURSOR_SKIPNEXT;
 67995  68164         if( iCellIdx>=pPage->nCell ){
 67996  68165           pCur->skipNext = -1;
 67997  68166           pCur->ix = pPage->nCell-1;
 67998  68167         }else{
 67999  68168           pCur->skipNext = 1;
 68000  68169         }
 68001  68170       }else{
 68002  68171         rc = moveToRoot(pCur);
 68003  68172         if( bPreserve ){
        68173  +        btreeReleaseAllCursorPages(pCur);
 68004  68174           pCur->eState = CURSOR_REQUIRESEEK;
 68005  68175         }
        68176  +      if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
 68006  68177       }
 68007  68178     }
 68008  68179     return rc;
 68009  68180   }
 68010  68181   
 68011  68182   /*
 68012  68183   ** Create a new BTree table.  Write into *piTable the page
................................................................................
 68463  68634   ** Otherwise, if an error is encountered (i.e. an IO error or database
 68464  68635   ** corruption) an SQLite error code is returned.
 68465  68636   */
 68466  68637   SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
 68467  68638     i64 nEntry = 0;                      /* Value to return in *pnEntry */
 68468  68639     int rc;                              /* Return code */
 68469  68640   
 68470         -  if( pCur->pgnoRoot==0 ){
        68641  +  rc = moveToRoot(pCur);
        68642  +  if( rc==SQLITE_EMPTY ){
 68471  68643       *pnEntry = 0;
 68472  68644       return SQLITE_OK;
 68473  68645     }
 68474         -  rc = moveToRoot(pCur);
 68475  68646   
 68476  68647     /* Unless an error occurs, the following loop runs one iteration for each
 68477  68648     ** page in the B-Tree structure (not including overflow pages). 
 68478  68649     */
 68479  68650     while( rc==SQLITE_OK ){
 68480  68651       int iIdx;                          /* Index of child node in parent */
 68481  68652       MemPage *pPage;                    /* Current page of the b-tree */
 68482  68653   
 68483  68654       /* If this is a leaf page or the tree is not an int-key tree, then 
 68484  68655       ** this page contains countable entries. Increment the entry counter
 68485  68656       ** accordingly.
 68486  68657       */
 68487         -    pPage = pCur->apPage[pCur->iPage];
        68658  +    pPage = pCur->pPage;
 68488  68659       if( pPage->leaf || !pPage->intKey ){
 68489  68660         nEntry += pPage->nCell;
 68490  68661       }
 68491  68662   
 68492  68663       /* pPage is a leaf node. This loop navigates the cursor so that it 
 68493  68664       ** points to the first interior cell that it points to the parent of
 68494  68665       ** the next page in the tree that has not yet been visited. The
................................................................................
 68503  68674         do {
 68504  68675           if( pCur->iPage==0 ){
 68505  68676             /* All pages of the b-tree have been visited. Return successfully. */
 68506  68677             *pnEntry = nEntry;
 68507  68678             return moveToRoot(pCur);
 68508  68679           }
 68509  68680           moveToParent(pCur);
 68510         -      }while ( pCur->ix>=pCur->apPage[pCur->iPage]->nCell );
        68681  +      }while ( pCur->ix>=pCur->pPage->nCell );
 68511  68682   
 68512  68683         pCur->ix++;
 68513         -      pPage = pCur->apPage[pCur->iPage];
        68684  +      pPage = pCur->pPage;
 68514  68685       }
 68515  68686   
 68516  68687       /* Descend to the child node of the cell that the cursor currently 
 68517  68688       ** points at. This is the right-child if (iIdx==pPage->nCell).
 68518  68689       */
 68519  68690       iIdx = pCur->ix;
 68520  68691       if( iIdx==pPage->nCell ){
................................................................................
 69347  69518     if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
 69348  69519       return SQLITE_READONLY;
 69349  69520     }
 69350  69521     assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
 69351  69522                 && pCsr->pBt->inTransaction==TRANS_WRITE );
 69352  69523     assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
 69353  69524     assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
 69354         -  assert( pCsr->apPage[pCsr->iPage]->intKey );
        69525  +  assert( pCsr->pPage->intKey );
 69355  69526   
 69356  69527     return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
 69357  69528   }
 69358  69529   
 69359  69530   /* 
 69360  69531   ** Mark this cursor as an incremental blob cursor.
 69361  69532   */
................................................................................
 70398  70569     ** contain a valid string or blob value.  */
 70399  70570     assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
 70400  70571     testcase( bPreserve && pMem->z==0 );
 70401  70572   
 70402  70573     assert( pMem->szMalloc==0
 70403  70574          || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
 70404  70575     if( n<32 ) n = 32;
 70405         -  if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
        70576  +  if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
 70406  70577       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
 70407  70578       bPreserve = 0;
 70408  70579     }else{
 70409  70580       if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
 70410  70581       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
 70411  70582     }
 70412  70583     if( pMem->zMalloc==0 ){
................................................................................
 70414  70585       pMem->z = 0;
 70415  70586       pMem->szMalloc = 0;
 70416  70587       return SQLITE_NOMEM_BKPT;
 70417  70588     }else{
 70418  70589       pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
 70419  70590     }
 70420  70591   
 70421         -  if( bPreserve && pMem->z && ALWAYS(pMem->z!=pMem->zMalloc) ){
        70592  +  if( bPreserve && pMem->z ){
        70593  +    assert( pMem->z!=pMem->zMalloc );
 70422  70594       memcpy(pMem->zMalloc, pMem->z, pMem->n);
 70423  70595     }
 70424  70596     if( (pMem->flags&MEM_Dyn)!=0 ){
 70425  70597       assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
 70426  70598       pMem->xDel((void *)(pMem->z));
 70427  70599     }
 70428  70600   
................................................................................
 70451  70623       return sqlite3VdbeMemGrow(pMem, szNew, 0);
 70452  70624     }
 70453  70625     assert( (pMem->flags & MEM_Dyn)==0 );
 70454  70626     pMem->z = pMem->zMalloc;
 70455  70627     pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
 70456  70628     return SQLITE_OK;
 70457  70629   }
        70630  +
        70631  +/*
        70632  +** It is already known that pMem contains an unterminated string.
        70633  +** Add the zero terminator.
        70634  +*/
        70635  +static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
        70636  +  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
        70637  +    return SQLITE_NOMEM_BKPT;
        70638  +  }
        70639  +  pMem->z[pMem->n] = 0;
        70640  +  pMem->z[pMem->n+1] = 0;
        70641  +  pMem->flags |= MEM_Term;
        70642  +  return SQLITE_OK;
        70643  +}
 70458  70644   
 70459  70645   /*
 70460  70646   ** Change pMem so that its MEM_Str or MEM_Blob value is stored in
 70461  70647   ** MEM.zMalloc, where it can be safely written.
 70462  70648   **
 70463  70649   ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
 70464  70650   */
 70465  70651   SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
 70466  70652     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 70467  70653     assert( (pMem->flags&MEM_RowSet)==0 );
 70468  70654     if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
 70469  70655       if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
 70470  70656       if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
 70471         -      if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
 70472         -        return SQLITE_NOMEM_BKPT;
 70473         -      }
 70474         -      pMem->z[pMem->n] = 0;
 70475         -      pMem->z[pMem->n+1] = 0;
 70476         -      pMem->flags |= MEM_Term;
        70657  +      int rc = vdbeMemAddTerminator(pMem);
        70658  +      if( rc ) return rc;
 70477  70659       }
 70478  70660     }
 70479  70661     pMem->flags &= ~MEM_Ephem;
 70480  70662   #ifdef SQLITE_DEBUG
 70481  70663     pMem->pScopyFrom = 0;
 70482  70664   #endif
 70483  70665   
................................................................................
 70508  70690     memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
 70509  70691     pMem->n += pMem->u.nZero;
 70510  70692     pMem->flags &= ~(MEM_Zero|MEM_Term);
 70511  70693     return SQLITE_OK;
 70512  70694   }
 70513  70695   #endif
 70514  70696   
 70515         -/*
 70516         -** It is already known that pMem contains an unterminated string.
 70517         -** Add the zero terminator.
 70518         -*/
 70519         -static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
 70520         -  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
 70521         -    return SQLITE_NOMEM_BKPT;
 70522         -  }
 70523         -  pMem->z[pMem->n] = 0;
 70524         -  pMem->z[pMem->n+1] = 0;
 70525         -  pMem->flags |= MEM_Term;
 70526         -  return SQLITE_OK;
 70527         -}
 70528         -
 70529  70697   /*
 70530  70698   ** Make sure the given Mem is \u0000 terminated.
 70531  70699   */
 70532  70700   SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
 70533  70701     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 70534  70702     testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
 70535  70703     testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
................................................................................
 70840  71008   **
 70841  71009   ** Every effort is made to force the conversion, even if the input
 70842  71010   ** is a string that does not look completely like a number.  Convert
 70843  71011   ** as much of the string as we can and ignore the rest.
 70844  71012   */
 70845  71013   SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
 70846  71014     if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
        71015  +    int rc;
 70847  71016       assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
 70848  71017       assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 70849         -    if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
        71018  +    rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc);
        71019  +    if( rc==0 ){
 70850  71020         MemSetTypeFlag(pMem, MEM_Int);
 70851  71021       }else{
 70852         -      pMem->u.r = sqlite3VdbeRealValue(pMem);
 70853         -      MemSetTypeFlag(pMem, MEM_Real);
 70854         -      sqlite3VdbeIntegerAffinity(pMem);
        71022  +      i64 i = pMem->u.i;
        71023  +      sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
        71024  +      if( rc==1 && pMem->u.r==(double)i ){
        71025  +        pMem->u.i = i;
        71026  +        MemSetTypeFlag(pMem, MEM_Int);
        71027  +      }else{
        71028  +        MemSetTypeFlag(pMem, MEM_Real);
        71029  +      }
 70855  71030       }
 70856  71031     }
 70857  71032     assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
 70858  71033     pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
 70859  71034     return SQLITE_OK;
 70860  71035   }
 70861  71036   
................................................................................
 71174  71349     }else{
 71175  71350       iLimit = SQLITE_MAX_LENGTH;
 71176  71351     }
 71177  71352     flags = (enc==0?MEM_Blob:MEM_Str);
 71178  71353     if( nByte<0 ){
 71179  71354       assert( enc!=0 );
 71180  71355       if( enc==SQLITE_UTF8 ){
 71181         -      nByte = sqlite3Strlen30(z);
        71356  +      nByte = 0x7fffffff & (int)strlen(z);
 71182  71357         if( nByte>iLimit ) nByte = iLimit+1;
 71183  71358       }else{
 71184  71359         for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
 71185  71360       }
 71186  71361       flags |= MEM_Term;
 71187  71362     }
 71188  71363   
................................................................................
 71252  71427     BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
 71253  71428     u32 offset,       /* Offset from the start of data to return bytes from. */
 71254  71429     u32 amt,          /* Number of bytes to return. */
 71255  71430     Mem *pMem         /* OUT: Return data in this Mem structure. */
 71256  71431   ){
 71257  71432     int rc;
 71258  71433     pMem->flags = MEM_Null;
 71259         -  if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
        71434  +  if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
 71260  71435       rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
 71261  71436       if( rc==SQLITE_OK ){
 71262         -      pMem->z[amt] = 0;
 71263         -      pMem->z[amt+1] = 0;
 71264         -      pMem->flags = MEM_Blob|MEM_Term;
        71437  +      pMem->z[amt] = 0;   /* Overrun area used when reading malformed records */
        71438  +      pMem->flags = MEM_Blob;
 71265  71439         pMem->n = (int)amt;
 71266  71440       }else{
 71267  71441         sqlite3VdbeMemRelease(pMem);
 71268  71442       }
 71269  71443     }
 71270  71444     return rc;
 71271  71445   }
................................................................................
 71406  71580         int nCol = pIdx->nColumn;   /* Number of index columns including rowid */
 71407  71581     
 71408  71582         nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
 71409  71583         pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
 71410  71584         if( pRec ){
 71411  71585           pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
 71412  71586           if( pRec->pKeyInfo ){
 71413         -          assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
        71587  +          assert( pRec->pKeyInfo->nAllField==nCol );
 71414  71588             assert( pRec->pKeyInfo->enc==ENC(db) );
 71415  71589             pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
 71416  71590             for(i=0; i<nCol; i++){
 71417  71591               pRec->aMem[i].flags = MEM_Null;
 71418  71592               pRec->aMem[i].db = db;
 71419  71593             }
 71420  71594           }else{
................................................................................
 71942  72116   ** Unless it is NULL, the argument must be an UnpackedRecord object returned
 71943  72117   ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
 71944  72118   ** the object.
 71945  72119   */
 71946  72120   SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
 71947  72121     if( pRec ){
 71948  72122       int i;
 71949         -    int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
        72123  +    int nCol = pRec->pKeyInfo->nAllField;
 71950  72124       Mem *aMem = pRec->aMem;
 71951  72125       sqlite3 *db = aMem[0].db;
 71952  72126       for(i=0; i<nCol; i++){
 71953  72127         sqlite3VdbeMemRelease(&aMem[i]);
 71954  72128       }
 71955  72129       sqlite3KeyInfoUnref(pRec->pKeyInfo);
 71956  72130       sqlite3DbFreeNN(db, pRec);
................................................................................
 72038  72212       db->pVdbe->pPrev = p;
 72039  72213     }
 72040  72214     p->pNext = db->pVdbe;
 72041  72215     p->pPrev = 0;
 72042  72216     db->pVdbe = p;
 72043  72217     p->magic = VDBE_MAGIC_INIT;
 72044  72218     p->pParse = pParse;
        72219  +  pParse->pVdbe = p;
 72045  72220     assert( pParse->aLabel==0 );
 72046  72221     assert( pParse->nLabel==0 );
 72047  72222     assert( pParse->nOpAlloc==0 );
 72048  72223     assert( pParse->szOpAlloc==0 );
        72224  +  sqlite3VdbeAddOp2(p, OP_Init, 0, 1);
 72049  72225     return p;
 72050  72226   }
 72051  72227   
 72052  72228   /*
 72053  72229   ** Change the error string stored in Vdbe.zErrMsg
 72054  72230   */
 72055  72231   SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
................................................................................
 72495  72671   **
 72496  72672   **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 72497  72673   **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 72498  72674   **   *  OP_Destroy
 72499  72675   **   *  OP_VUpdate
 72500  72676   **   *  OP_VRename
 72501  72677   **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
 72502         -**   *  OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...)
        72678  +**   *  OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine 
        72679  +**      (for CREATE TABLE AS SELECT ...)
 72503  72680   **
 72504  72681   ** Then check that the value of Parse.mayAbort is true if an
 72505  72682   ** ABORT may be thrown, or false otherwise. Return true if it does
 72506  72683   ** match, or false otherwise. This function is intended to be used as
 72507  72684   ** part of an assert statement in the compiler. Similar to:
 72508  72685   **
 72509  72686   **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
................................................................................
 72523  72700       if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
 72524  72701        || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
 72525  72702         && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
 72526  72703       ){
 72527  72704         hasAbort = 1;
 72528  72705         break;
 72529  72706       }
 72530         -    if( opcode==OP_CreateTable ) hasCreateTable = 1;
        72707  +    if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
 72531  72708       if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
 72532  72709   #ifndef SQLITE_OMIT_FOREIGN_KEY
 72533  72710       if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
 72534  72711         hasFkCounter = 1;
 72535  72712       }
 72536  72713   #endif
 72537  72714     }
................................................................................
 72601  72778           case OP_Checkpoint:
 72602  72779   #endif
 72603  72780           case OP_Vacuum:
 72604  72781           case OP_JournalMode: {
 72605  72782             p->readOnly = 0;
 72606  72783             p->bIsReader = 1;
 72607  72784             break;
        72785  +        }
        72786  +        case OP_Next:
        72787  +        case OP_NextIfOpen:
        72788  +        case OP_SorterNext: {
        72789  +          pOp->p4.xAdvance = sqlite3BtreeNext;
        72790  +          pOp->p4type = P4_ADVANCE;
        72791  +          /* The code generator never codes any of these opcodes as a jump
        72792  +          ** to a label.  They are always coded as a jump backwards to a 
        72793  +          ** known address */
        72794  +          assert( pOp->p2>=0 );
        72795  +          break;
        72796  +        }
        72797  +        case OP_Prev:
        72798  +        case OP_PrevIfOpen: {
        72799  +          pOp->p4.xAdvance = sqlite3BtreePrevious;
        72800  +          pOp->p4type = P4_ADVANCE;
        72801  +          /* The code generator never codes any of these opcodes as a jump
        72802  +          ** to a label.  They are always coded as a jump backwards to a 
        72803  +          ** known address */
        72804  +          assert( pOp->p2>=0 );
        72805  +          break;
 72608  72806           }
 72609  72807   #ifndef SQLITE_OMIT_VIRTUALTABLE
 72610  72808           case OP_VUpdate: {
 72611  72809             if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
 72612  72810             break;
 72613  72811           }
 72614  72812           case OP_VFilter: {
 72615  72813             int n;
 72616  72814             assert( (pOp - p->aOp) >= 3 );
 72617  72815             assert( pOp[-1].opcode==OP_Integer );
 72618  72816             n = pOp[-1].p1;
 72619  72817             if( n>nMaxArgs ) nMaxArgs = n;
 72620         -          break;
        72818  +          /* Fall through into the default case */
 72621  72819           }
 72622  72820   #endif
 72623         -        case OP_Next:
 72624         -        case OP_NextIfOpen:
 72625         -        case OP_SorterNext: {
 72626         -          pOp->p4.xAdvance = sqlite3BtreeNext;
 72627         -          pOp->p4type = P4_ADVANCE;
 72628         -          break;
 72629         -        }
 72630         -        case OP_Prev:
 72631         -        case OP_PrevIfOpen: {
 72632         -          pOp->p4.xAdvance = sqlite3BtreePrevious;
 72633         -          pOp->p4type = P4_ADVANCE;
        72821  +        default: {
        72822  +          if( pOp->p2<0 ){
        72823  +            /* The mkopcodeh.tcl script has so arranged things that the only
        72824  +            ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
        72825  +            ** have non-negative values for P2. */
        72826  +            assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
        72827  +            assert( ADDR(pOp->p2)<pParse->nLabel );
        72828  +            pOp->p2 = aLabel[ADDR(pOp->p2)];
        72829  +          }
 72634  72830             break;
 72635  72831           }
 72636  72832         }
 72637         -      if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 && pOp->p2<0 ){
 72638         -        assert( ADDR(pOp->p2)<pParse->nLabel );
 72639         -        pOp->p2 = aLabel[ADDR(pOp->p2)];
 72640         -      }
        72833  +      /* The mkopcodeh.tcl script has so arranged things that the only
        72834  +      ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
        72835  +      ** have non-negative values for P2. */
        72836  +      assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
 72641  72837       }
 72642  72838       if( pOp==p->aOp ) break;
 72643  72839       pOp--;
 72644  72840     }
 72645  72841     sqlite3DbFree(p->db, pParse->aLabel);
 72646  72842     pParse->aLabel = 0;
 72647  72843     pParse->nLabel = 0;
................................................................................
 73306  73502     assert( nTemp>=20 );
 73307  73503     sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
 73308  73504     switch( pOp->p4type ){
 73309  73505       case P4_KEYINFO: {
 73310  73506         int j;
 73311  73507         KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
 73312  73508         assert( pKeyInfo->aSortOrder!=0 );
 73313         -      sqlite3XPrintf(&x, "k(%d", pKeyInfo->nField);
 73314         -      for(j=0; j<pKeyInfo->nField; j++){
        73509  +      sqlite3XPrintf(&x, "k(%d", pKeyInfo->nKeyField);
        73510  +      for(j=0; j<pKeyInfo->nKeyField; j++){
 73315  73511           CollSeq *pColl = pKeyInfo->aColl[j];
 73316  73512           const char *zColl = pColl ? pColl->zName : "";
 73317  73513           if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
 73318  73514           sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
 73319  73515         }
 73320  73516         sqlite3StrAccumAppend(&x, ")", 1);
 73321  73517         break;
................................................................................
 73379  73575       }
 73380  73576   #endif
 73381  73577       case P4_INTARRAY: {
 73382  73578         int i;
 73383  73579         int *ai = pOp->p4.ai;
 73384  73580         int n = ai[0];   /* The first element of an INTARRAY is always the
 73385  73581                          ** count of the number of elements to follow */
 73386         -      for(i=1; i<n; i++){
        73582  +      for(i=1; i<=n; i++){
 73387  73583           sqlite3XPrintf(&x, ",%d", ai[i]);
 73388  73584         }
 73389  73585         zTemp[0] = '[';
 73390  73586         sqlite3StrAccumAppend(&x, "]", 1);
 73391  73587         break;
 73392  73588       }
 73393  73589       case P4_SUBPROGRAM: {
................................................................................
 74143  74339     }
 74144  74340   
 74145  74341     /* Delete any auxdata allocations made by the VM */
 74146  74342     if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
 74147  74343     assert( p->pAuxData==0 );
 74148  74344   }
 74149  74345   
 74150         -/*
 74151         -** Clean up the VM after a single run.
 74152         -*/
 74153         -static void Cleanup(Vdbe *p){
 74154         -  sqlite3 *db = p->db;
 74155         -
 74156         -#ifdef SQLITE_DEBUG
 74157         -  /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
 74158         -  ** Vdbe.aMem[] arrays have already been cleaned up.  */
 74159         -  int i;
 74160         -  if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
 74161         -  if( p->aMem ){
 74162         -    for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
 74163         -  }
 74164         -#endif
 74165         -
 74166         -  sqlite3DbFree(db, p->zErrMsg);
 74167         -  p->zErrMsg = 0;
 74168         -  p->pResultSet = 0;
 74169         -}
 74170         -
 74171  74346   /*
 74172  74347   ** Set the number of result columns that will be returned by this SQL
 74173  74348   ** statement. This is now set at compile time, rather than during
 74174  74349   ** execution of the vdbe program so that sqlite3_column_count() can
 74175  74350   ** be called on an SQL statement before sqlite3_step().
 74176  74351   */
 74177  74352   SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
................................................................................
 74872  75047   ** again.
 74873  75048   **
 74874  75049   ** To look at it another way, this routine resets the state of the
 74875  75050   ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
 74876  75051   ** VDBE_MAGIC_INIT.
 74877  75052   */
 74878  75053   SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
        75054  +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
        75055  +  int i;
        75056  +#endif
        75057  +
 74879  75058     sqlite3 *db;
 74880  75059     db = p->db;
 74881  75060   
 74882  75061     /* If the VM did not run to completion or if it encountered an
 74883  75062     ** error, then it might not have been halted properly.  So halt
 74884  75063     ** it now.
 74885  75064     */
................................................................................
 74889  75068     ** and error message from the VDBE into the main database structure.  But
 74890  75069     ** if the VDBE has just been set to run but has not actually executed any
 74891  75070     ** instructions yet, leave the main database error information unchanged.
 74892  75071     */
 74893  75072     if( p->pc>=0 ){
 74894  75073       vdbeInvokeSqllog(p);
 74895  75074       sqlite3VdbeTransferError(p);
 74896         -    sqlite3DbFree(db, p->zErrMsg);
 74897         -    p->zErrMsg = 0;
 74898  75075       if( p->runOnlyOnce ) p->expired = 1;
 74899  75076     }else if( p->rc && p->expired ){
 74900  75077       /* The expired flag was set on the VDBE before the first call
 74901  75078       ** to sqlite3_step(). For consistency (since sqlite3_step() was
 74902  75079       ** called), set the database error in this case as well.
 74903  75080       */
 74904  75081       sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
 74905         -    sqlite3DbFree(db, p->zErrMsg);
 74906         -    p->zErrMsg = 0;
 74907  75082     }
 74908  75083   
 74909         -  /* Reclaim all memory used by the VDBE
        75084  +  /* Reset register contents and reclaim error message memory.
 74910  75085     */
 74911         -  Cleanup(p);
        75086  +#ifdef SQLITE_DEBUG
        75087  +  /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
        75088  +  ** Vdbe.aMem[] arrays have already been cleaned up.  */
        75089  +  if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
        75090  +  if( p->aMem ){
        75091  +    for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
        75092  +  }
        75093  +#endif
        75094  +  sqlite3DbFree(db, p->zErrMsg);
        75095  +  p->zErrMsg = 0;
        75096  +  p->pResultSet = 0;
 74912  75097   
 74913  75098     /* Save profiling information from this VDBE run.
 74914  75099     */
 74915  75100   #ifdef VDBE_PROFILE
 74916  75101     {
 74917  75102       FILE *out = fopen("vdbe_profile.out", "a");
 74918  75103       if( out ){
 74919         -      int i;
 74920  75104         fprintf(out, "---- ");
 74921  75105         for(i=0; i<p->nOp; i++){
 74922  75106           fprintf(out, "%02x", p->aOp[i].opcode);
 74923  75107         }
 74924  75108         fprintf(out, "\n");
 74925  75109         if( p->zSql ){
 74926  75110           char c, pc = 0;
................................................................................
 75125  75309   ** a NULL row.
 75126  75310   **
 75127  75311   ** If the cursor is already pointing to the correct row and that row has
 75128  75312   ** not been deleted out from under the cursor, then this routine is a no-op.
 75129  75313   */
 75130  75314   SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){
 75131  75315     VdbeCursor *p = *pp;
 75132         -  if( p->eCurType==CURTYPE_BTREE ){
 75133         -    if( p->deferredMoveto ){
 75134         -      int iMap;
 75135         -      if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
 75136         -        *pp = p->pAltCursor;
 75137         -        *piCol = iMap - 1;
 75138         -        return SQLITE_OK;
 75139         -      }
 75140         -      return handleDeferredMoveto(p);
        75316  +  assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO );
        75317  +  if( p->deferredMoveto ){
        75318  +    int iMap;
        75319  +    if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
        75320  +      *pp = p->pAltCursor;
        75321  +      *piCol = iMap - 1;
        75322  +      return SQLITE_OK;
 75141  75323       }
 75142         -    if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
 75143         -      return handleMovedCursor(p);
 75144         -    }
        75324  +    return handleDeferredMoveto(p);
        75325  +  }
        75326  +  if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
        75327  +    return handleMovedCursor(p);
 75145  75328     }
 75146  75329     return SQLITE_OK;
 75147  75330   }
 75148  75331   
 75149  75332   /*
 75150  75333   ** The following functions:
 75151  75334   **
................................................................................
 75533  75716   ** If an OOM error occurs, NULL is returned.
 75534  75717   */
 75535  75718   SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
 75536  75719     KeyInfo *pKeyInfo               /* Description of the record */
 75537  75720   ){
 75538  75721     UnpackedRecord *p;              /* Unpacked record to return */
 75539  75722     int nByte;                      /* Number of bytes required for *p */
 75540         -  nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
        75723  +  nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
 75541  75724     p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
 75542  75725     if( !p ) return 0;
 75543  75726     p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
 75544  75727     assert( pKeyInfo->aSortOrder!=0 );
 75545  75728     p->pKeyInfo = pKeyInfo;
 75546         -  p->nField = pKeyInfo->nField + 1;
        75729  +  p->nField = pKeyInfo->nKeyField + 1;
 75547  75730     return p;
 75548  75731   }
 75549  75732   
 75550  75733   /*
 75551  75734   ** Given the nKey-byte encoding of a record in pKey[], populate the 
 75552  75735   ** UnpackedRecord structure indicated by the fourth argument with the
 75553  75736   ** contents of the decoded record.
................................................................................
 75579  75762       /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
 75580  75763       pMem->szMalloc = 0;
 75581  75764       pMem->z = 0;
 75582  75765       d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
 75583  75766       pMem++;
 75584  75767       if( (++u)>=p->nField ) break;
 75585  75768     }
 75586         -  assert( u<=pKeyInfo->nField + 1 );
        75769  +  assert( u<=pKeyInfo->nKeyField + 1 );
 75587  75770     p->nField = u;
 75588  75771   }
 75589  75772   
 75590  75773   #ifdef SQLITE_DEBUG
 75591  75774   /*
 75592  75775   ** This function compares two index or table record keys in the same way
 75593  75776   ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
................................................................................
 75628  75811     ** to ignore the compiler warnings and leave this variable uninitialized.
 75629  75812     */
 75630  75813     /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
 75631  75814     
 75632  75815     idx1 = getVarint32(aKey1, szHdr1);
 75633  75816     if( szHdr1>98307 ) return SQLITE_CORRUPT;
 75634  75817     d1 = szHdr1;
 75635         -  assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
        75818  +  assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
 75636  75819     assert( pKeyInfo->aSortOrder!=0 );
 75637         -  assert( pKeyInfo->nField>0 );
        75820  +  assert( pKeyInfo->nKeyField>0 );
 75638  75821     assert( idx1<=szHdr1 || CORRUPT_DB );
 75639  75822     do{
 75640  75823       u32 serial_type1;
 75641  75824   
 75642  75825       /* Read the serial types for the next element in each key. */
 75643  75826       idx1 += getVarint32( aKey1+idx1, serial_type1 );
 75644  75827   
................................................................................
 75692  75875   }
 75693  75876   #endif
 75694  75877   
 75695  75878   #ifdef SQLITE_DEBUG
 75696  75879   /*
 75697  75880   ** Count the number of fields (a.k.a. columns) in the record given by
 75698  75881   ** pKey,nKey.  The verify that this count is less than or equal to the
 75699         -** limit given by pKeyInfo->nField + pKeyInfo->nXField.
        75882  +** limit given by pKeyInfo->nAllField.
 75700  75883   **
 75701  75884   ** If this constraint is not satisfied, it means that the high-speed
 75702  75885   ** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
 75703  75886   ** not work correctly.  If this assert() ever fires, it probably means
 75704         -** that the KeyInfo.nField or KeyInfo.nXField values were computed
        75887  +** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
 75705  75888   ** incorrectly.
 75706  75889   */
 75707  75890   static void vdbeAssertFieldCountWithinLimits(
 75708  75891     int nKey, const void *pKey,   /* The record to verify */ 
 75709  75892     const KeyInfo *pKeyInfo       /* Compare size with this KeyInfo */
 75710  75893   ){
 75711  75894     int nField = 0;
................................................................................
 75718  75901     idx = getVarint32(aKey, szHdr);
 75719  75902     assert( nKey>=0 );
 75720  75903     assert( szHdr<=(u32)nKey );
 75721  75904     while( idx<szHdr ){
 75722  75905       idx += getVarint32(aKey+idx, notUsed);
 75723  75906       nField++;
 75724  75907     }
 75725         -  assert( nField <= pKeyInfo->nField+pKeyInfo->nXField );
        75908  +  assert( nField <= pKeyInfo->nAllField );
 75726  75909   }
 75727  75910   #else
 75728  75911   # define vdbeAssertFieldCountWithinLimits(A,B,C)
 75729  75912   #endif
 75730  75913   
 75731  75914   /*
 75732  75915   ** Both *pMem1 and *pMem2 contain string values. Compare the two values
................................................................................
 76023  76206         pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
 76024  76207         return 0;  /* Corruption */
 76025  76208       }
 76026  76209       i = 0;
 76027  76210     }
 76028  76211   
 76029  76212     VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
 76030         -  assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField 
        76213  +  assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField 
 76031  76214          || CORRUPT_DB );
 76032  76215     assert( pPKey2->pKeyInfo->aSortOrder!=0 );
 76033         -  assert( pPKey2->pKeyInfo->nField>0 );
        76216  +  assert( pPKey2->pKeyInfo->nKeyField>0 );
 76034  76217     assert( idx1<=szHdr1 || CORRUPT_DB );
 76035  76218     do{
 76036  76219       u32 serial_type;
 76037  76220   
 76038  76221       /* RHS is an integer */
 76039  76222       if( pRhs->flags & MEM_Int ){
 76040  76223         serial_type = aKey1[idx1];
................................................................................
 76359  76542     ** buffer passed to varintRecordCompareInt() this makes it convenient to
 76360  76543     ** limit the size of the header to 64 bytes in cases where the first field
 76361  76544     ** is an integer.
 76362  76545     **
 76363  76546     ** The easiest way to enforce this limit is to consider only records with
 76364  76547     ** 13 fields or less. If the first field is an integer, the maximum legal
 76365  76548     ** header size is (12*5 + 1 + 1) bytes.  */
 76366         -  if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
        76549  +  if( p->pKeyInfo->nAllField<=13 ){
 76367  76550       int flags = p->aMem[0].flags;
 76368  76551       if( p->pKeyInfo->aSortOrder[0] ){
 76369  76552         p->r1 = 1;
 76370  76553         p->r2 = -1;
 76371  76554       }else{
 76372  76555         p->r1 = -1;
 76373  76556         p->r2 = 1;
................................................................................
 76694  76877   
 76695  76878     preupdate.v = v;
 76696  76879     preupdate.pCsr = pCsr;
 76697  76880     preupdate.op = op;
 76698  76881     preupdate.iNewReg = iReg;
 76699  76882     preupdate.keyinfo.db = db;
 76700  76883     preupdate.keyinfo.enc = ENC(db);
 76701         -  preupdate.keyinfo.nField = pTab->nCol;
        76884  +  preupdate.keyinfo.nKeyField = pTab->nCol;
 76702  76885     preupdate.keyinfo.aSortOrder = (u8*)&fakeSortOrder;
 76703  76886     preupdate.iKey1 = iKey1;
 76704  76887     preupdate.iKey2 = iKey2;
 76705  76888     preupdate.pTab = pTab;
 76706  76889   
 76707  76890     db->pPreUpdate = &preupdate;
 76708  76891     db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
 76709  76892     db->pPreUpdate = 0;
 76710  76893     sqlite3DbFree(db, preupdate.aRecord);
 76711         -  vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pUnpacked);
 76712         -  vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pNewUnpacked);
        76894  +  vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
        76895  +  vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
 76713  76896     if( preupdate.aNew ){
 76714  76897       int i;
 76715  76898       for(i=0; i<pCsr->nField; i++){
 76716  76899         sqlite3VdbeMemRelease(&preupdate.aNew[i]);
 76717  76900       }
 76718  76901       sqlite3DbFreeNN(db, preupdate.aNew);
 76719  76902     }
................................................................................
 77244  77427     for(i=0; i<db->nDb; i++){
 77245  77428       Btree *pBt = db->aDb[i].pBt;
 77246  77429       if( pBt ){
 77247  77430         int nEntry;
 77248  77431         sqlite3BtreeEnter(pBt);
 77249  77432         nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
 77250  77433         sqlite3BtreeLeave(pBt);
 77251         -      if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
        77434  +      if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
 77252  77435           rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
 77253  77436         }
 77254  77437       }
 77255  77438     }
 77256  77439   #endif
 77257  77440     return rc;
 77258  77441   }
................................................................................
 77354  77537     }
 77355  77538   
 77356  77539   #ifndef SQLITE_OMIT_TRACE
 77357  77540     /* If the statement completed successfully, invoke the profile callback */
 77358  77541     if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
 77359  77542   #endif
 77360  77543   
 77361         -  if( rc==SQLITE_DONE ){
        77544  +  if( rc==SQLITE_DONE && db->autoCommit ){
 77362  77545       assert( p->rc==SQLITE_OK );
 77363  77546       p->rc = doWalCallbacks(db);
 77364  77547       if( p->rc!=SQLITE_OK ){
 77365  77548         rc = SQLITE_ERROR;
 77366  77549       }
 77367  77550     }
 77368  77551   
................................................................................
 77398  77581   /*
 77399  77582   ** This is the top-level implementation of sqlite3_step().  Call
 77400  77583   ** sqlite3Step() to do most of the work.  If a schema error occurs,
 77401  77584   ** call sqlite3Reprepare() and try again.
 77402  77585   */
 77403  77586   SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
 77404  77587     int rc = SQLITE_OK;      /* Result from sqlite3Step() */
 77405         -  int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
 77406  77588     Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
 77407  77589     int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
 77408  77590     sqlite3 *db;             /* The database connection */
 77409  77591   
 77410  77592     if( vdbeSafetyNotNull(v) ){
 77411  77593       return SQLITE_MISUSE_BKPT;
 77412  77594     }
 77413  77595     db = v->db;
 77414  77596     sqlite3_mutex_enter(db->mutex);
 77415  77597     v->doingRerun = 0;
 77416  77598     while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
 77417  77599            && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
 77418  77600       int savedPc = v->pc;
 77419         -    rc2 = rc = sqlite3Reprepare(v);
 77420         -    if( rc!=SQLITE_OK) break;
        77601  +    rc = sqlite3Reprepare(v);
        77602  +    if( rc!=SQLITE_OK ){
        77603  +      /* This case occurs after failing to recompile an sql statement. 
        77604  +      ** The error message from the SQL compiler has already been loaded 
        77605  +      ** into the database handle. This block copies the error message 
        77606  +      ** from the database handle into the statement and sets the statement
        77607  +      ** program counter to 0 to ensure that when the statement is 
        77608  +      ** finalized or reset the parser error message is available via
        77609  +      ** sqlite3_errmsg() and sqlite3_errcode().
        77610  +      */
        77611  +      const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
        77612  +      sqlite3DbFree(db, v->zErrMsg);
        77613  +      if( !db->mallocFailed ){
        77614  +        v->zErrMsg = sqlite3DbStrDup(db, zErr);
        77615  +        v->rc = rc = sqlite3ApiExit(db, rc);
        77616  +      } else {
        77617  +        v->zErrMsg = 0;
        77618  +        v->rc = rc = SQLITE_NOMEM_BKPT;
        77619  +      }
        77620  +      break;
        77621  +    }
 77421  77622       sqlite3_reset(pStmt);
 77422  77623       if( savedPc>=0 ) v->doingRerun = 1;
 77423  77624       assert( v->expired==0 );
 77424  77625     }
 77425         -  if( rc2!=SQLITE_OK ){
 77426         -    /* This case occurs after failing to recompile an sql statement. 
 77427         -    ** The error message from the SQL compiler has already been loaded 
 77428         -    ** into the database handle. This block copies the error message 
 77429         -    ** from the database handle into the statement and sets the statement
 77430         -    ** program counter to 0 to ensure that when the statement is 
 77431         -    ** finalized or reset the parser error message is available via
 77432         -    ** sqlite3_errmsg() and sqlite3_errcode().
 77433         -    */
 77434         -    const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
 77435         -    sqlite3DbFree(db, v->zErrMsg);
 77436         -    if( !db->mallocFailed ){
 77437         -      v->zErrMsg = sqlite3DbStrDup(db, zErr);
 77438         -      v->rc = rc2;
 77439         -    } else {
 77440         -      v->zErrMsg = 0;
 77441         -      v->rc = rc = SQLITE_NOMEM_BKPT;
 77442         -    }
 77443         -  }
 77444         -  rc = sqlite3ApiExit(db, rc);
 77445  77626     sqlite3_mutex_leave(db->mutex);
 77446  77627     return rc;
 77447  77628   }
 77448  77629   
 77449  77630   
 77450  77631   /*
 77451  77632   ** Extract the user data from a sqlite3_context structure and return a
................................................................................
 78443  78624     int nKey, 
 78444  78625     const void *pKey
 78445  78626   ){
 78446  78627     UnpackedRecord *pRet;           /* Return value */
 78447  78628   
 78448  78629     pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
 78449  78630     if( pRet ){
 78450         -    memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1));
        78631  +    memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
 78451  78632       sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
 78452  78633     }
 78453  78634     return pRet;
 78454  78635   }
 78455  78636   
 78456  78637   /*
 78457  78638   ** This function is called from within a pre-update callback to retrieve
................................................................................
 78516  78697   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 78517  78698   /*
 78518  78699   ** This function is called from within a pre-update callback to retrieve
 78519  78700   ** the number of columns in the row being updated, deleted or inserted.
 78520  78701   */
 78521  78702   SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
 78522  78703     PreUpdate *p = db->pPreUpdate;
 78523         -  return (p ? p->keyinfo.nField : 0);
        78704  +  return (p ? p->keyinfo.nKeyField : 0);
 78524  78705   }
 78525  78706   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
 78526  78707   
 78527  78708   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 78528  78709   /*
 78529  78710   ** This function is designed to be called from within a pre-update callback
 78530  78711   ** only. It returns zero if the change that caused the callback was made
................................................................................
 78769  78950     int nextIndex = 1;       /* Index of next ? host parameter */
 78770  78951     int n;                   /* Length of a token prefix */
 78771  78952     int nToken;              /* Length of the parameter token */
 78772  78953     int i;                   /* Loop counter */
 78773  78954     Mem *pVar;               /* Value of a host parameter */
 78774  78955     StrAccum out;            /* Accumulate the output here */
 78775  78956   #ifndef SQLITE_OMIT_UTF16
 78776         -  Mem utf8;                /* Used to convert UTF16 parameters into UTF8 for display */
        78957  +  Mem utf8;                /* Used to convert UTF16 into UTF8 for display */
 78777  78958   #endif
 78778  78959     char zBase[100];         /* Initial working space */
 78779  78960   
 78780  78961     db = p->db;
 78781  78962     sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase), 
 78782  78963                         db->aLimit[SQLITE_LIMIT_LENGTH]);
 78783  78964     if( db->nVdbeExec>1 ){
................................................................................
 79238  79419   */
 79239  79420   static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
 79240  79421     assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
 79241  79422     assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
 79242  79423     if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
 79243  79424       return 0;
 79244  79425     }
 79245         -  if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
        79426  +  if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
 79246  79427       return MEM_Int;
 79247  79428     }
 79248  79429     return MEM_Real;
 79249  79430   }
 79250  79431   
 79251  79432   /*
 79252  79433   ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
................................................................................
 80928  81109           flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
 80929  81110         }
 80930  81111       }
 80931  81112       assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
 80932  81113       res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
 80933  81114     }
 80934  81115   compare_op:
 80935         -  switch( pOp->opcode ){
 80936         -    case OP_Eq:    res2 = res==0;     break;
 80937         -    case OP_Ne:    res2 = res;        break;
 80938         -    case OP_Lt:    res2 = res<0;      break;
 80939         -    case OP_Le:    res2 = res<=0;     break;
 80940         -    case OP_Gt:    res2 = res>0;      break;
 80941         -    default:       res2 = res>=0;     break;
        81116  +  /* At this point, res is negative, zero, or positive if reg[P1] is
        81117  +  ** less than, equal to, or greater than reg[P3], respectively.  Compute
        81118  +  ** the answer to this operator in res2, depending on what the comparison
        81119  +  ** operator actually is.  The next block of code depends on the fact
        81120  +  ** that the 6 comparison operators are consecutive integers in this
        81121  +  ** order:  NE, EQ, GT, LE, LT, GE */
        81122  +  assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
        81123  +  assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
        81124  +  if( res<0 ){                        /* ne, eq, gt, le, lt, ge */
        81125  +    static const unsigned char aLTb[] = { 1,  0,  0,  1,  1,  0 };
        81126  +    res2 = aLTb[pOp->opcode - OP_Ne];
        81127  +  }else if( res==0 ){
        81128  +    static const unsigned char aEQb[] = { 0,  1,  0,  1,  0,  1 };
        81129  +    res2 = aEQb[pOp->opcode - OP_Ne];
        81130  +  }else{
        81131  +    static const unsigned char aGTb[] = { 1,  0,  1,  0,  0,  1 };
        81132  +    res2 = aGTb[pOp->opcode - OP_Ne];
 80942  81133     }
 80943  81134   
 80944  81135     /* Undo any changes made by applyAffinity() to the input registers. */
 80945  81136     assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
 80946  81137     pIn1->flags = flags1;
 80947  81138     assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
 80948  81139     pIn3->flags = flags3;
 80949  81140   
 80950  81141     if( pOp->p5 & SQLITE_STOREP2 ){
 80951  81142       pOut = &aMem[pOp->p2];
 80952  81143       iCompare = res;
 80953         -    res2 = res2!=0;  /* For this path res2 must be exactly 0 or 1 */
 80954  81144       if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
 80955  81145         /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
 80956  81146         ** and prevents OP_Ne from overwriting NULL with 0.  This flag
 80957  81147         ** is only used in contexts where either:
 80958  81148         **   (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
 80959  81149         **   (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
 80960  81150         ** Therefore it is not necessary to check the content of r[P2] for
................................................................................
 81077  81267   #endif /* SQLITE_DEBUG */
 81078  81268     for(i=0; i<n; i++){
 81079  81269       idx = aPermute ? aPermute[i] : i;
 81080  81270       assert( memIsValid(&aMem[p1+idx]) );
 81081  81271       assert( memIsValid(&aMem[p2+idx]) );
 81082  81272       REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
 81083  81273       REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
 81084         -    assert( i<pKeyInfo->nField );
        81274  +    assert( i<pKeyInfo->nKeyField );
 81085  81275       pColl = pKeyInfo->aColl[i];
 81086  81276       bRev = pKeyInfo->aSortOrder[i];
 81087  81277       iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
 81088  81278       if( iCompare ){
 81089  81279         if( bRev ) iCompare = -iCompare;
 81090  81280         break;
 81091  81281       }
................................................................................
 81350  81540     int len;           /* The length of the serialized data for the column */
 81351  81541     int i;             /* Loop counter */
 81352  81542     Mem *pDest;        /* Where to write the extracted value */
 81353  81543     Mem sMem;          /* For storing the record being decoded */
 81354  81544     const u8 *zData;   /* Part of the record being decoded */
 81355  81545     const u8 *zHdr;    /* Next unparsed byte of the header */
 81356  81546     const u8 *zEndHdr; /* Pointer to first byte after the header */
 81357         -  u32 offset;        /* Offset into the data */
 81358  81547     u64 offset64;      /* 64-bit offset */
 81359         -  u32 avail;         /* Number of bytes of available data */
 81360  81548     u32 t;             /* A type code from the record header */
 81361  81549     Mem *pReg;         /* PseudoTable input register */
 81362  81550   
 81363  81551     pC = p->apCsr[pOp->p1];
 81364  81552     p2 = pOp->p2;
 81365  81553   
 81366  81554     /* If the cursor cache is stale (meaning it is not currently point at
................................................................................
 81379  81567     assert( pC->eCurType!=CURTYPE_VTAB );
 81380  81568     assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
 81381  81569     assert( pC->eCurType!=CURTYPE_SORTER );
 81382  81570   
 81383  81571     if( pC->cacheStatus!=p->cacheCtr ){                /*OPTIMIZATION-IF-FALSE*/
 81384  81572       if( pC->nullRow ){
 81385  81573         if( pC->eCurType==CURTYPE_PSEUDO ){
 81386         -        assert( pC->uc.pseudoTableReg>0 );
 81387         -        pReg = &aMem[pC->uc.pseudoTableReg];
        81574  +        /* For the special case of as pseudo-cursor, the seekResult field
        81575  +        ** identifies the register that holds the record */
        81576  +        assert( pC->seekResult>0 );
        81577  +        pReg = &aMem[pC->seekResult];
 81388  81578           assert( pReg->flags & MEM_Blob );
 81389  81579           assert( memIsValid(pReg) );
 81390         -        pC->payloadSize = pC->szRow = avail = pReg->n;
        81580  +        pC->payloadSize = pC->szRow = pReg->n;
 81391  81581           pC->aRow = (u8*)pReg->z;
 81392  81582         }else{
 81393  81583           sqlite3VdbeMemSetNull(pDest);
 81394  81584           goto op_column_out;
 81395  81585         }
 81396  81586       }else{
 81397  81587         pCrsr = pC->uc.pCursor;
 81398  81588         assert( pC->eCurType==CURTYPE_BTREE );
 81399  81589         assert( pCrsr );
 81400  81590         assert( sqlite3BtreeCursorIsValid(pCrsr) );
 81401  81591         pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
 81402         -      pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &avail);
 81403         -      assert( avail<=65536 );  /* Maximum page size is 64KiB */
 81404         -      if( pC->payloadSize <= (u32)avail ){
 81405         -        pC->szRow = pC->payloadSize;
 81406         -      }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
        81592  +      pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
        81593  +      assert( pC->szRow<=pC->payloadSize );
        81594  +      assert( pC->szRow<=65536 );  /* Maximum page size is 64KiB */
        81595  +      if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 81407  81596           goto too_big;
 81408         -      }else{
 81409         -        pC->szRow = avail;
 81410  81597         }
 81411  81598       }
 81412  81599       pC->cacheStatus = p->cacheCtr;
 81413         -    pC->iHdrOffset = getVarint32(pC->aRow, offset);
        81600  +    pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
 81414  81601       pC->nHdrParsed = 0;
 81415         -    aOffset[0] = offset;
 81416  81602   
 81417  81603   
 81418         -    if( avail<offset ){      /*OPTIMIZATION-IF-FALSE*/
        81604  +    if( pC->szRow<aOffset[0] ){      /*OPTIMIZATION-IF-FALSE*/
 81419  81605         /* pC->aRow does not have to hold the entire row, but it does at least
 81420  81606         ** need to cover the header of the record.  If pC->aRow does not contain
 81421  81607         ** the complete header, then set it to zero, forcing the header to be
 81422  81608         ** dynamically allocated. */
 81423  81609         pC->aRow = 0;
 81424  81610         pC->szRow = 0;
 81425  81611   
................................................................................
 81428  81614         **
 81429  81615         ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
 81430  81616         ** types use so much data space that there can only be 4096 and 32 of
 81431  81617         ** them, respectively.  So the maximum header length results from a
 81432  81618         ** 3-byte type for each of the maximum of 32768 columns plus three
 81433  81619         ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
 81434  81620         */
 81435         -      if( offset > 98307 || offset > pC->payloadSize ){
 81436         -        rc = SQLITE_CORRUPT_BKPT;
 81437         -        goto abort_due_to_error;
        81621  +      if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
        81622  +        goto op_column_corrupt;
 81438  81623         }
 81439         -    }else if( offset>0 ){ /*OPTIMIZATION-IF-TRUE*/
 81440         -      /* The following goto is an optimization.  It can be omitted and
 81441         -      ** everything will still work.  But OP_Column is measurably faster
 81442         -      ** by skipping the subsequent conditional, which is always true.
        81624  +    }else{
        81625  +      /* This is an optimization.  By skipping over the first few tests
        81626  +      ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
        81627  +      ** measurable performance gain.
        81628  +      **
        81629  +      ** This branch is taken even if aOffset[0]==0.  Such a record is never
        81630  +      ** generated by SQLite, and could be considered corruption, but we
        81631  +      ** accept it for historical reasons.  When aOffset[0]==0, the code this
        81632  +      ** branch jumps to reads past the end of the record, but never more
        81633  +      ** than a few bytes.  Even if the record occurs at the end of the page
        81634  +      ** content area, the "page header" comes after the page content and so
        81635  +      ** this overread is harmless.  Similar overreads can occur for a corrupt
        81636  +      ** database file.
 81443  81637         */
 81444  81638         zData = pC->aRow;
 81445  81639         assert( pC->nHdrParsed<=p2 );         /* Conditional skipped */
        81640  +      testcase( aOffset[0]==0 );
 81446  81641         goto op_column_read_header;
 81447  81642       }
 81448  81643     }
 81449  81644   
 81450  81645     /* Make sure at least the first p2+1 entries of the header have been
 81451  81646     ** parsed and valid information is in aOffset[] and pC->aType[].
 81452  81647     */
................................................................................
 81467  81662     
 81468  81663         /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
 81469  81664       op_column_read_header:
 81470  81665         i = pC->nHdrParsed;
 81471  81666         offset64 = aOffset[i];
 81472  81667         zHdr = zData + pC->iHdrOffset;
 81473  81668         zEndHdr = zData + aOffset[0];
        81669  +      testcase( zHdr>=zEndHdr );
 81474  81670         do{
 81475  81671           if( (t = zHdr[0])<0x80 ){
 81476  81672             zHdr++;
 81477  81673             offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
 81478  81674           }else{
 81479  81675             zHdr += sqlite3GetVarint32(zHdr, &t);
 81480  81676             offset64 += sqlite3VdbeSerialTypeLen(t);
................................................................................
 81487  81683         ** (1) the bytes of the header extend past the declared header size
 81488  81684         ** (2) the entire header was used but not all data was used
 81489  81685         ** (3) the end of the data extends beyond the end of the record.
 81490  81686         */
 81491  81687         if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
 81492  81688          || (offset64 > pC->payloadSize)
 81493  81689         ){
 81494         -        if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
 81495         -        rc = SQLITE_CORRUPT_BKPT;
 81496         -        goto abort_due_to_error;
        81690  +        if( aOffset[0]==0 ){
        81691  +          i = 0;
        81692  +          zHdr = zEndHdr;
        81693  +        }else{
        81694  +          if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
        81695  +          goto op_column_corrupt;
        81696  +        }
 81497  81697         }
 81498  81698   
 81499  81699         pC->nHdrParsed = i;
 81500  81700         pC->iHdrOffset = (u32)(zHdr - zData);
 81501  81701         if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
 81502  81702       }else{
 81503  81703         t = 0;
................................................................................
 81583  81783       }
 81584  81784     }
 81585  81785   
 81586  81786   op_column_out:
 81587  81787     UPDATE_MAX_BLOBSIZE(pDest);
 81588  81788     REGISTER_TRACE(pOp->p3, pDest);
 81589  81789     break;
        81790  +
        81791  +op_column_corrupt:
        81792  +  if( aOp[0].p3>0 ){
        81793  +    pOp = &aOp[aOp[0].p3-1];
        81794  +    break;
        81795  +  }else{
        81796  +    rc = SQLITE_CORRUPT_BKPT;
        81797  +    goto abort_due_to_error;
        81798  +  }
 81590  81799   }
 81591  81800   
 81592  81801   /* Opcode: Affinity P1 P2 * P4 *
 81593  81802   ** Synopsis: affinity(r[P1@P2])
 81594  81803   **
 81595  81804   ** Apply affinities to a range of P2 registers starting with P1.
 81596  81805   **
................................................................................
 81923  82132           }
 81924  82133           db->isTransactionSavepoint = 0;
 81925  82134           rc = p->rc;
 81926  82135         }else{
 81927  82136           int isSchemaChange;
 81928  82137           iSavepoint = db->nSavepoint - iSavepoint - 1;
 81929  82138           if( p1==SAVEPOINT_ROLLBACK ){
 81930         -          isSchemaChange = (db->flags & SQLITE_InternChanges)!=0;
        82139  +          isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
 81931  82140             for(ii=0; ii<db->nDb; ii++){
 81932  82141               rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
 81933  82142                                          SQLITE_ABORT_ROLLBACK,
 81934  82143                                          isSchemaChange==0);
 81935  82144               if( rc!=SQLITE_OK ) goto abort_due_to_error;
 81936  82145             }
 81937  82146           }else{
................................................................................
 81942  82151             if( rc!=SQLITE_OK ){
 81943  82152               goto abort_due_to_error;
 81944  82153             }
 81945  82154           }
 81946  82155           if( isSchemaChange ){
 81947  82156             sqlite3ExpirePreparedStatements(db);
 81948  82157             sqlite3ResetAllSchemasOfConnection(db);
 81949         -          db->flags = (db->flags | SQLITE_InternChanges);
        82158  +          db->mDbFlags |= DBFLAG_SchemaChange;
 81950  82159           }
 81951  82160         }
 81952  82161     
 81953  82162         /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all 
 81954  82163         ** savepoints nested inside of the savepoint being operated on. */
 81955  82164         while( db->pSavepoint!=pSavepoint ){
 81956  82165           pTmp = db->pSavepoint;
................................................................................
 82222  82431     assert( pDb->pBt!=0 );
 82223  82432     assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
 82224  82433     /* See note about index shifting on OP_ReadCookie */
 82225  82434     rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
 82226  82435     if( pOp->p2==BTREE_SCHEMA_VERSION ){
 82227  82436       /* When the schema cookie changes, record the new cookie internally */
 82228  82437       pDb->pSchema->schema_cookie = pOp->p3;
 82229         -    db->flags |= SQLITE_InternChanges;
        82438  +    db->mDbFlags |= DBFLAG_SchemaChange;
 82230  82439     }else if( pOp->p2==BTREE_FILE_FORMAT ){
 82231  82440       /* Record changes in the file format */
 82232  82441       pDb->pSchema->file_format = pOp->p3;
 82233  82442     }
 82234  82443     if( pOp->p1==1 ){
 82235  82444       /* Invalidate all prepared statements whenever the TEMP database
 82236  82445       ** schema is changed.  Ticket #1644 */
................................................................................
 82361  82570       assert( p2>0 );
 82362  82571       assert( p2<=(p->nMem+1 - p->nCursor) );
 82363  82572       pIn2 = &aMem[p2];
 82364  82573       assert( memIsValid(pIn2) );
 82365  82574       assert( (pIn2->flags & MEM_Int)!=0 );
 82366  82575       sqlite3VdbeMemIntegerify(pIn2);
 82367  82576       p2 = (int)pIn2->u.i;
 82368         -    /* The p2 value always comes from a prior OP_CreateTable opcode and
        82577  +    /* The p2 value always comes from a prior OP_CreateBtree opcode and
 82369  82578       ** that opcode will always set the p2 value to 2 or more or else fail.
 82370  82579       ** If there were a failure, the prepared statement would have halted
 82371  82580       ** before reaching this instruction. */
 82372  82581       assert( p2>=2 );
 82373  82582     }
 82374  82583     if( pOp->p4type==P4_KEYINFO ){
 82375  82584       pKeyInfo = pOp->p4.pKeyInfo;
 82376  82585       assert( pKeyInfo->enc==ENC(db) );
 82377  82586       assert( pKeyInfo->db==db );
 82378         -    nField = pKeyInfo->nField+pKeyInfo->nXField;
        82587  +    nField = pKeyInfo->nAllField;
 82379  82588     }else if( pOp->p4type==P4_INT32 ){
 82380  82589       nField = pOp->p4.i;
 82381  82590     }
 82382  82591     assert( pOp->p1>=0 );
 82383  82592     assert( nField>=0 );
 82384  82593     testcase( nField==0 );  /* Table with INTEGER PRIMARY KEY and nothing else */
 82385  82594     pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
................................................................................
 82582  82791     VdbeCursor *pCx;
 82583  82792   
 82584  82793     assert( pOp->p1>=0 );
 82585  82794     assert( pOp->p3>=0 );
 82586  82795     pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
 82587  82796     if( pCx==0 ) goto no_mem;
 82588  82797     pCx->nullRow = 1;
 82589         -  pCx->uc.pseudoTableReg = pOp->p2;
        82798  +  pCx->seekResult = pOp->p2;
 82590  82799     pCx->isTable = 1;
        82800  +  /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
        82801  +  ** can be safely passed to sqlite3VdbeCursorMoveto().  This avoids a test
        82802  +  ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
        82803  +  ** which is a performance optimization */
        82804  +  pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
 82591  82805     assert( pOp->p5==0 );
 82592  82806     break;
 82593  82807   }
 82594  82808   
 82595  82809   /* Opcode: Close P1 * * * *
 82596  82810   **
 82597  82811   ** Close a cursor previously opened as P1.  If P1 is not
................................................................................
 83375  83589       sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
 83376  83590     }
 83377  83591     if( pOp->p5 & OPFLAG_ISNOOP ) break;
 83378  83592   #endif
 83379  83593   
 83380  83594     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 83381  83595     if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
 83382         -  if( pData->flags & MEM_Null ){
 83383         -    x.pData = 0;
 83384         -    x.nData = 0;
 83385         -  }else{
 83386         -    assert( pData->flags & (MEM_Blob|MEM_Str) );
 83387         -    x.pData = pData->z;
 83388         -    x.nData = pData->n;
 83389         -  }
        83596  +  assert( pData->flags & (MEM_Blob|MEM_Str) );
        83597  +  x.pData = pData->z;
        83598  +  x.nData = pData->n;
 83390  83599     seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
 83391  83600     if( pData->flags & MEM_Zero ){
 83392  83601       x.nZero = pData->u.nZero;
 83393  83602     }else{
 83394  83603       x.nZero = 0;
 83395  83604     }
 83396  83605     x.pKey = 0;
................................................................................
 83749  83958     if( pC->eCurType==CURTYPE_BTREE ){
 83750  83959       assert( pC->uc.pCursor!=0 );
 83751  83960       sqlite3BtreeClearCursor(pC->uc.pCursor);
 83752  83961     }
 83753  83962     break;
 83754  83963   }
 83755  83964   
 83756         -/* Opcode: Last P1 P2 P3 * *
        83965  +/* Opcode: SeekEnd P1 * * * *
        83966  +**
        83967  +** Position cursor P1 at the end of the btree for the purpose of
        83968  +** appending a new entry onto the btree.
        83969  +**
        83970  +** It is assumed that the cursor is used only for appending and so
        83971  +** if the cursor is valid, then the cursor must already be pointing
        83972  +** at the end of the btree and so no changes are made to
        83973  +** the cursor.
        83974  +*/
        83975  +/* Opcode: Last P1 P2 * * *
 83757  83976   **
 83758  83977   ** The next use of the Rowid or Column or Prev instruction for P1 
 83759  83978   ** will refer to the last entry in the database table or index.
 83760  83979   ** If the table or index is empty and P2>0, then jump immediately to P2.
 83761  83980   ** If P2 is 0 or if the table or index is not empty, fall through
 83762  83981   ** to the following instruction.
 83763  83982   **
 83764  83983   ** This opcode leaves the cursor configured to move in reverse order,
 83765  83984   ** from the end toward the beginning.  In other words, the cursor is
 83766  83985   ** configured to use Prev, not Next.
 83767         -**
 83768         -** If P3 is -1, then the cursor is positioned at the end of the btree
 83769         -** for the purpose of appending a new entry onto the btree.  In that
 83770         -** case P2 must be 0.  It is assumed that the cursor is used only for
 83771         -** appending and so if the cursor is valid, then the cursor must already
 83772         -** be pointing at the end of the btree and so no changes are made to
 83773         -** the cursor.
 83774  83986   */
        83987  +case OP_SeekEnd:
 83775  83988   case OP_Last: {        /* jump */
 83776  83989     VdbeCursor *pC;
 83777  83990     BtCursor *pCrsr;
 83778  83991     int res;
 83779  83992   
 83780  83993     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 83781  83994     pC = p->apCsr[pOp->p1];
 83782  83995     assert( pC!=0 );
 83783  83996     assert( pC->eCurType==CURTYPE_BTREE );
 83784  83997     pCrsr = pC->uc.pCursor;
 83785  83998     res = 0;
 83786  83999     assert( pCrsr!=0 );
 83787         -  pC->seekResult = pOp->p3;
 83788  84000   #ifdef SQLITE_DEBUG
 83789         -  pC->seekOp = OP_Last;
        84001  +  pC->seekOp = pOp->opcode;
 83790  84002   #endif
 83791         -  if( pOp->p3==0 || !sqlite3BtreeCursorIsValidNN(pCrsr) ){
 83792         -    rc = sqlite3BtreeLast(pCrsr, &res);
 83793         -    pC->nullRow = (u8)res;
 83794         -    pC->deferredMoveto = 0;
 83795         -    pC->cacheStatus = CACHE_STALE;
 83796         -    if( rc ) goto abort_due_to_error;
 83797         -    if( pOp->p2>0 ){
 83798         -      VdbeBranchTaken(res!=0,2);
 83799         -      if( res ) goto jump_to_p2;
 83800         -    }
 83801         -  }else{
        84003  +  if( pOp->opcode==OP_SeekEnd ){
 83802  84004       assert( pOp->p2==0 );
        84005  +    pC->seekResult = -1;
        84006  +    if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
        84007  +      break;
        84008  +    }
        84009  +  }
        84010  +  rc = sqlite3BtreeLast(pCrsr, &res);
        84011  +  pC->nullRow = (u8)res;
        84012  +  pC->deferredMoveto = 0;
        84013  +  pC->cacheStatus = CACHE_STALE;
        84014  +  if( rc ) goto abort_due_to_error;
        84015  +  if( pOp->p2>0 ){
        84016  +    VdbeBranchTaken(res!=0,2);
        84017  +    if( res ) goto jump_to_p2;
 83803  84018     }
 83804  84019     break;
 83805  84020   }
 83806  84021   
 83807  84022   /* Opcode: IfSmaller P1 P2 P3 * *
 83808  84023   **
 83809  84024   ** Estimate the number of rows in the table P1.  Jump to P2 if that
................................................................................
 84424  84639       assert( pC->isEphemeral );
 84425  84640       rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
 84426  84641       if( rc ) goto abort_due_to_error;
 84427  84642     }
 84428  84643     break;
 84429  84644   }
 84430  84645   
 84431         -/* Opcode: CreateTable P1 P2 * * *
 84432         -** Synopsis: r[P2]=root iDb=P1
        84646  +/* Opcode: CreateBtree P1 P2 P3 * *
        84647  +** Synopsis: r[P2]=root iDb=P1 flags=P3
 84433  84648   **
 84434         -** Allocate a new table in the main database file if P1==0 or in the
 84435         -** auxiliary database file if P1==1 or in an attached database if
 84436         -** P1>1.  Write the root page number of the new table into
 84437         -** register P2
 84438         -**
 84439         -** The difference between a table and an index is this:  A table must
 84440         -** have a 4-byte integer key and can have arbitrary data.  An index
 84441         -** has an arbitrary key but no data.
 84442         -**
 84443         -** See also: CreateIndex
        84649  +** Allocate a new b-tree in the main database file if P1==0 or in the
        84650  +** TEMP database file if P1==1 or in an attached database if
        84651  +** P1>1.  The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
        84652  +** it must be 2 (BTREE_BLOBKEY) for a index or WITHOUT ROWID table.
        84653  +** The root page number of the new b-tree is stored in register P2.
 84444  84654   */
 84445         -/* Opcode: CreateIndex P1 P2 * * *
 84446         -** Synopsis: r[P2]=root iDb=P1
 84447         -**
 84448         -** Allocate a new index in the main database file if P1==0 or in the
 84449         -** auxiliary database file if P1==1 or in an attached database if
 84450         -** P1>1.  Write the root page number of the new table into
 84451         -** register P2.
 84452         -**
 84453         -** See documentation on OP_CreateTable for additional information.
 84454         -*/
 84455         -case OP_CreateIndex:            /* out2 */
 84456         -case OP_CreateTable: {          /* out2 */
        84655  +case OP_CreateBtree: {          /* out2 */
 84457  84656     int pgno;
 84458         -  int flags;
 84459  84657     Db *pDb;
 84460  84658   
 84461  84659     pOut = out2Prerelease(p, pOp);
 84462  84660     pgno = 0;
        84661  +  assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
 84463  84662     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 84464  84663     assert( DbMaskTest(p->btreeMask, pOp->p1) );
 84465  84664     assert( p->readOnly==0 );
 84466  84665     pDb = &db->aDb[pOp->p1];
 84467  84666     assert( pDb->pBt!=0 );
 84468         -  if( pOp->opcode==OP_CreateTable ){
 84469         -    /* flags = BTREE_INTKEY; */
 84470         -    flags = BTREE_INTKEY;
 84471         -  }else{
 84472         -    flags = BTREE_BLOBKEY;
 84473         -  }
 84474         -  rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
        84667  +  rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
 84475  84668     if( rc ) goto abort_due_to_error;
 84476  84669     pOut->u.i = pgno;
 84477  84670     break;
 84478  84671   }
 84479  84672   
 84480  84673   /* Opcode: SqlExec * * * P4 *
 84481  84674   **
................................................................................
 84629  84822     char *z;        /* Text of the error report */
 84630  84823     Mem *pnErr;     /* Register keeping track of errors remaining */
 84631  84824   
 84632  84825     assert( p->bIsReader );
 84633  84826     nRoot = pOp->p2;
 84634  84827     aRoot = pOp->p4.ai;
 84635  84828     assert( nRoot>0 );
 84636         -  assert( aRoot[nRoot]==0 );
        84829  +  assert( aRoot[0]==nRoot );
 84637  84830     assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
 84638  84831     pnErr = &aMem[pOp->p3];
 84639  84832     assert( (pnErr->flags & MEM_Int)!=0 );
 84640  84833     assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
 84641  84834     pIn1 = &aMem[pOp->p1];
 84642  84835     assert( pOp->p5<db->nDb );
 84643  84836     assert( DbMaskTest(p->btreeMask, pOp->p5) );
 84644         -  z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
        84837  +  z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
 84645  84838                                    (int)pnErr->u.i+1, &nErr);
 84646  84839     sqlite3VdbeMemSetNull(pIn1);
 84647  84840     if( nErr==0 ){
 84648  84841       assert( z==0 );
 84649  84842     }else if( z==0 ){
 84650  84843       goto no_mem;
 84651  84844     }else{
................................................................................
 85978  86171   
 85979  86172     REGISTER_TRACE(pOp->p3, pOut);
 85980  86173     UPDATE_MAX_BLOBSIZE(pOut);
 85981  86174     break;
 85982  86175   }
 85983  86176   
 85984  86177   
 85985         -/* Opcode: Init P1 P2 * P4 *
        86178  +/* Opcode: Init P1 P2 P3 P4 *
 85986  86179   ** Synopsis: Start at P2
 85987  86180   **
 85988  86181   ** Programs contain a single instance of this opcode as the very first
 85989  86182   ** opcode.
 85990  86183   **
 85991  86184   ** If tracing is enabled (by the sqlite3_trace()) interface, then
 85992  86185   ** the UTF-8 string contained in P4 is emitted on the trace callback.
 85993  86186   ** Or if P4 is blank, use the string returned by sqlite3_sql().
 85994  86187   **
 85995  86188   ** If P2 is not zero, jump to instruction P2.
 85996  86189   **
 85997  86190   ** Increment the value of P1 so that OP_Once opcodes will jump the
 85998  86191   ** first time they are evaluated for this run.
        86192  +**
        86193  +** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
        86194  +** error is encountered.
 85999  86195   */
 86000  86196   case OP_Init: {          /* jump */
 86001  86197     char *zTrace;
 86002  86198     int i;
 86003  86199   
 86004  86200     /* If the P4 argument is not NULL, then it must be an SQL comment string.
 86005  86201     ** The "--" string is broken up to prevent false-positives with srcck1.c.
................................................................................
 86266  86462     /* Set the value of register r[1] in the SQL statement to integer iRow. 
 86267  86463     ** This is done directly as a performance optimization
 86268  86464     */
 86269  86465     v->aMem[1].flags = MEM_Int;
 86270  86466     v->aMem[1].u.i = iRow;
 86271  86467   
 86272  86468     /* If the statement has been run before (and is paused at the OP_ResultRow)
 86273         -  ** then back it up to the point where it does the OP_SeekRowid.  This could
        86469  +  ** then back it up to the point where it does the OP_NotExists.  This could
 86274  86470     ** have been down with an extra OP_Goto, but simply setting the program
 86275  86471     ** counter is faster. */
 86276         -  if( v->pc>3 ){
 86277         -    v->pc = 3;
        86472  +  if( v->pc>4 ){
        86473  +    v->pc = 4;
        86474  +    assert( v->aOp[v->pc].opcode==OP_NotExists );
 86278  86475       rc = sqlite3VdbeExec(v);
 86279  86476     }else{
 86280  86477       rc = sqlite3_step(p->pStmt);
 86281  86478     }
 86282  86479     if( rc==SQLITE_ROW ){
 86283  86480       VdbeCursor *pC = v->apCsr[0];
 86284  86481       u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
................................................................................
 86332  86529     sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
 86333  86530   ){
 86334  86531     int nAttempt = 0;
 86335  86532     int iCol;               /* Index of zColumn in row-record */
 86336  86533     int rc = SQLITE_OK;
 86337  86534     char *zErr = 0;
 86338  86535     Table *pTab;
 86339         -  Parse *pParse = 0;
 86340  86536     Incrblob *pBlob = 0;
        86537  +  Parse sParse;
 86341  86538   
 86342  86539   #ifdef SQLITE_ENABLE_API_ARMOR
 86343  86540     if( ppBlob==0 ){
 86344  86541       return SQLITE_MISUSE_BKPT;
 86345  86542     }
 86346  86543   #endif
 86347  86544     *ppBlob = 0;
................................................................................
 86351  86548     }
 86352  86549   #endif
 86353  86550     wrFlag = !!wrFlag;                /* wrFlag = (wrFlag ? 1 : 0); */
 86354  86551   
 86355  86552     sqlite3_mutex_enter(db->mutex);
 86356  86553   
 86357  86554     pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
 86358         -  if( !pBlob ) goto blob_open_out;
 86359         -  pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
 86360         -  if( !pParse ) goto blob_open_out;
 86361         -
 86362  86555     do {
 86363         -    memset(pParse, 0, sizeof(Parse));
 86364         -    pParse->db = db;
        86556  +    memset(&sParse, 0, sizeof(Parse));
        86557  +    if( !pBlob ) goto blob_open_out;
        86558  +    sParse.db = db;
 86365  86559       sqlite3DbFree(db, zErr);
 86366  86560       zErr = 0;
 86367  86561   
 86368  86562       sqlite3BtreeEnterAll(db);
 86369         -    pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
        86563  +    pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
 86370  86564       if( pTab && IsVirtual(pTab) ){
 86371  86565         pTab = 0;
 86372         -      sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
        86566  +      sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
 86373  86567       }
 86374  86568       if( pTab && !HasRowid(pTab) ){
 86375  86569         pTab = 0;
 86376         -      sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable);
        86570  +      sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
 86377  86571       }
 86378  86572   #ifndef SQLITE_OMIT_VIEW
 86379  86573       if( pTab && pTab->pSelect ){
 86380  86574         pTab = 0;
 86381         -      sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
        86575  +      sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
 86382  86576       }
 86383  86577   #endif
 86384  86578       if( !pTab ){
 86385         -      if( pParse->zErrMsg ){
        86579  +      if( sParse.zErrMsg ){
 86386  86580           sqlite3DbFree(db, zErr);
 86387         -        zErr = pParse->zErrMsg;
 86388         -        pParse->zErrMsg = 0;
        86581  +        zErr = sParse.zErrMsg;
        86582  +        sParse.zErrMsg = 0;
 86389  86583         }
 86390  86584         rc = SQLITE_ERROR;
 86391  86585         sqlite3BtreeLeaveAll(db);
 86392  86586         goto blob_open_out;
 86393  86587       }
 86394  86588       pBlob->pTab = pTab;
 86395  86589       pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
................................................................................
 86445  86639           zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
 86446  86640           rc = SQLITE_ERROR;
 86447  86641           sqlite3BtreeLeaveAll(db);
 86448  86642           goto blob_open_out;
 86449  86643         }
 86450  86644       }
 86451  86645   
 86452         -    pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
        86646  +    pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
 86453  86647       assert( pBlob->pStmt || db->mallocFailed );
 86454  86648       if( pBlob->pStmt ){
 86455  86649         
 86456  86650         /* This VDBE program seeks a btree cursor to the identified 
 86457  86651         ** db/table/row entry. The reason for using a vdbe program instead
 86458  86652         ** of writing code to use the b-tree layer directly is that the
 86459  86653         ** vdbe program will take advantage of the various transaction,
................................................................................
 86481  86675         Vdbe *v = (Vdbe *)pBlob->pStmt;
 86482  86676         int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 86483  86677         VdbeOp *aOp;
 86484  86678   
 86485  86679         sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag, 
 86486  86680                              pTab->pSchema->schema_cookie,
 86487  86681                              pTab->pSchema->iGeneration);
 86488         -      sqlite3VdbeChangeP5(v, 1);     
        86682  +      sqlite3VdbeChangeP5(v, 1);
        86683  +      assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
 86489  86684         aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
 86490  86685   
 86491  86686         /* Make sure a mutex is held on the table to be accessed */
 86492  86687         sqlite3VdbeUsesBtree(v, iDb); 
 86493  86688   
 86494  86689         if( db->mallocFailed==0 ){
 86495  86690           assert( aOp!=0 );
................................................................................
 86496  86691           /* Configure the OP_TableLock instruction */
 86497  86692   #ifdef SQLITE_OMIT_SHARED_CACHE
 86498  86693           aOp[0].opcode = OP_Noop;
 86499  86694   #else
 86500  86695           aOp[0].p1 = iDb;
 86501  86696           aOp[0].p2 = pTab->tnum;
 86502  86697           aOp[0].p3 = wrFlag;
 86503         -        sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
        86698  +        sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
 86504  86699         }
 86505  86700         if( db->mallocFailed==0 ){
 86506  86701   #endif
 86507  86702   
 86508  86703           /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
 86509  86704           ** parameter of the other to pTab->tnum.  */
 86510  86705           if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
................................................................................
 86518  86713           ** we can invoke OP_Column to fill in the vdbe cursors type 
 86519  86714           ** and offset cache without causing any IO.
 86520  86715           */
 86521  86716           aOp[1].p4type = P4_INT32;
 86522  86717           aOp[1].p4.i = pTab->nCol+1;
 86523  86718           aOp[3].p2 = pTab->nCol;
 86524  86719   
 86525         -        pParse->nVar = 0;
 86526         -        pParse->nMem = 1;
 86527         -        pParse->nTab = 1;
 86528         -        sqlite3VdbeMakeReady(v, pParse);
        86720  +        sParse.nVar = 0;
        86721  +        sParse.nMem = 1;
        86722  +        sParse.nTab = 1;
        86723  +        sqlite3VdbeMakeReady(v, &sParse);
 86529  86724         }
 86530  86725       }
 86531  86726      
 86532  86727       pBlob->iCol = iCol;
 86533  86728       pBlob->db = db;
 86534  86729       sqlite3BtreeLeaveAll(db);
 86535  86730       if( db->mallocFailed ){
................................................................................
 86543  86738       *ppBlob = (sqlite3_blob *)pBlob;
 86544  86739     }else{
 86545  86740       if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
 86546  86741       sqlite3DbFree(db, pBlob);
 86547  86742     }
 86548  86743     sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
 86549  86744     sqlite3DbFree(db, zErr);
 86550         -  sqlite3ParserReset(pParse);
 86551         -  sqlite3StackFree(db, pParse);
        86745  +  sqlite3ParserReset(&sParse);
 86552  86746     rc = sqlite3ApiExit(db, rc);
 86553  86747     sqlite3_mutex_leave(db->mutex);
 86554  86748     return rc;
 86555  86749   }
 86556  86750   
 86557  86751   /*
 86558  86752   ** Close a blob handle that was previously created using
................................................................................
 87538  87732     getVarint32(&p2[1], n2);
 87539  87733     res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
 87540  87734     if( res==0 ){
 87541  87735       res = n1 - n2;
 87542  87736     }
 87543  87737   
 87544  87738     if( res==0 ){
 87545         -    if( pTask->pSorter->pKeyInfo->nField>1 ){
        87739  +    if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
 87546  87740         res = vdbeSorterCompareTail(
 87547  87741             pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
 87548  87742         );
 87549  87743       }
 87550  87744     }else{
 87551  87745       if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
 87552  87746         res = res * -1;
................................................................................
 87607  87801         if( *v1 & 0x80 ) res = -1;
 87608  87802       }else{
 87609  87803         if( *v2 & 0x80 ) res = +1;
 87610  87804       }
 87611  87805     }
 87612  87806   
 87613  87807     if( res==0 ){
 87614         -    if( pTask->pSorter->pKeyInfo->nField>1 ){
        87808  +    if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
 87615  87809         res = vdbeSorterCompareTail(
 87616  87810             pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
 87617  87811         );
 87618  87812       }
 87619  87813     }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
 87620  87814       res = res * -1;
 87621  87815     }
................................................................................
 87622  87816   
 87623  87817     return res;
 87624  87818   }
 87625  87819   
 87626  87820   /*
 87627  87821   ** Initialize the temporary index cursor just opened as a sorter cursor.
 87628  87822   **
 87629         -** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField)
        87823  +** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
 87630  87824   ** to determine the number of fields that should be compared from the
 87631  87825   ** records being sorted. However, if the value passed as argument nField
 87632  87826   ** is non-zero and the sorter is able to guarantee a stable sort, nField
 87633  87827   ** is used instead. This is used when sorting records for a CREATE INDEX
 87634  87828   ** statement. In this case, keys are always delivered to the sorter in
 87635  87829   ** order of the primary key, which happens to be make up the final part 
 87636  87830   ** of the records being sorted. So if the sort is stable, there is never
................................................................................
 87675  87869     if( nWorker>=SORTER_MAX_MERGE_COUNT ){
 87676  87870       nWorker = SORTER_MAX_MERGE_COUNT-1;
 87677  87871     }
 87678  87872   #endif
 87679  87873   
 87680  87874     assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
 87681  87875     assert( pCsr->eCurType==CURTYPE_SORTER );
 87682         -  szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*);
        87876  +  szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
 87683  87877     sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
 87684  87878   
 87685  87879     pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
 87686  87880     pCsr->uc.pSorter = pSorter;
 87687  87881     if( pSorter==0 ){
 87688  87882       rc = SQLITE_NOMEM_BKPT;
 87689  87883     }else{
 87690  87884       pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
 87691  87885       memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
 87692  87886       pKeyInfo->db = 0;
 87693  87887       if( nField && nWorker==0 ){
 87694         -      pKeyInfo->nXField += (pKeyInfo->nField - nField);
 87695         -      pKeyInfo->nField = nField;
        87888  +      pKeyInfo->nKeyField = nField;
 87696  87889       }
 87697  87890       pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 87698  87891       pSorter->nTask = nWorker + 1;
 87699  87892       pSorter->iPrev = (u8)(nWorker - 1);
 87700  87893       pSorter->bUseThreads = (pSorter->nTask>1);
 87701  87894       pSorter->db = db;
 87702  87895       for(i=0; i<pSorter->nTask; i++){
................................................................................
 87716  87909           mxCache = mxCache * -1024;
 87717  87910         }else{
 87718  87911           mxCache = mxCache * pgsz;
 87719  87912         }
 87720  87913         mxCache = MIN(mxCache, SQLITE_MAX_PMASZ);
 87721  87914         pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
 87722  87915   
 87723         -      /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of
 87724         -      ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary
 87725         -      ** large heap allocations.
 87726         -      */
 87727         -      if( sqlite3GlobalConfig.pScratch==0 ){
        87916  +      /* Avoid large memory allocations if the application has requested
        87917  +      ** SQLITE_CONFIG_SMALL_MALLOC. */
        87918  +      if( sqlite3GlobalConfig.bSmallMalloc==0 ){
 87728  87919           assert( pSorter->iMemory==0 );
 87729  87920           pSorter->nMemory = pgsz;
 87730  87921           pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
 87731  87922           if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
 87732  87923         }
 87733  87924       }
 87734  87925   
 87735         -    if( (pKeyInfo->nField+pKeyInfo->nXField)<13 
        87926  +    if( pKeyInfo->nAllField<13 
 87736  87927        && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
 87737  87928       ){
 87738  87929         pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
 87739  87930       }
 87740  87931     }
 87741  87932   
 87742  87933     return rc;
................................................................................
 88043  88234   ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or 
 88044  88235   ** if no allocation was required), or SQLITE_NOMEM otherwise.
 88045  88236   */
 88046  88237   static int vdbeSortAllocUnpacked(SortSubtask *pTask){
 88047  88238     if( pTask->pUnpacked==0 ){
 88048  88239       pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
 88049  88240       if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
 88050         -    pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nField;
        88241  +    pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
 88051  88242       pTask->pUnpacked->errCode = 0;
 88052  88243     }
 88053  88244     return SQLITE_OK;
 88054  88245   }
 88055  88246   
 88056  88247   
 88057  88248   /*
................................................................................
 89567  89758   ){
 89568  89759     MemJournal *p = (MemJournal *)pJfd;
 89569  89760     u8 *zOut = zBuf;
 89570  89761     int nRead = iAmt;
 89571  89762     int iChunkOffset;
 89572  89763     FileChunk *pChunk;
 89573  89764   
 89574         -#ifdef SQLITE_ENABLE_ATOMIC_WRITE
        89765  +#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
        89766  + || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
 89575  89767     if( (iAmt+iOfst)>p->endpoint.iOffset ){
 89576  89768       return SQLITE_IOERR_SHORT_READ;
 89577  89769     }
 89578  89770   #endif
 89579  89771   
 89580  89772     assert( (iAmt+iOfst)<=p->endpoint.iOffset );
 89581  89773     assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
................................................................................
 89686  89878     else{
 89687  89879       /* An in-memory journal file should only ever be appended to. Random
 89688  89880       ** access writes are not required. The only exception to this is when
 89689  89881       ** the in-memory journal is being used by a connection using the
 89690  89882       ** atomic-write optimization. In this case the first 28 bytes of the
 89691  89883       ** journal file may be written as part of committing the transaction. */ 
 89692  89884       assert( iOfst==p->endpoint.iOffset || iOfst==0 );
 89693         -#ifdef SQLITE_ENABLE_ATOMIC_WRITE
        89885  +#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
        89886  + || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
 89694  89887       if( iOfst==0 && p->pFirst ){
 89695  89888         assert( p->nChunkSize>iAmt );
 89696  89889         memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
 89697  89890       }else
 89698  89891   #else
 89699  89892       assert( iOfst>0 || p->pFirst==0 );
 89700  89893   #endif
................................................................................
 89855  90048   /*
 89856  90049   ** Open an in-memory journal file.
 89857  90050   */
 89858  90051   SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
 89859  90052     sqlite3JournalOpen(0, 0, pJfd, 0, -1);
 89860  90053   }
 89861  90054   
 89862         -#ifdef SQLITE_ENABLE_ATOMIC_WRITE
        90055  +#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
        90056  + || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
 89863  90057   /*
 89864  90058   ** If the argument p points to a MemJournal structure that is not an 
 89865  90059   ** in-memory-only journal file (i.e. is one that was opened with a +ve
 89866         -** nSpill parameter), and the underlying file has not yet been created, 
 89867         -** create it now.
        90060  +** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying 
        90061  +** file has not yet been created, create it now.
 89868  90062   */
 89869         -SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
        90063  +SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *pJfd){
 89870  90064     int rc = SQLITE_OK;
 89871         -  if( p->pMethods==&MemJournalMethods && ((MemJournal*)p)->nSpill>0 ){
 89872         -    rc = memjrnlCreateFile((MemJournal*)p);
        90065  +  MemJournal *p = (MemJournal*)pJfd;
        90066  +  if( p->pMethod==&MemJournalMethods && (
        90067  +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
        90068  +     p->nSpill>0
        90069  +#else
        90070  +     /* While this appears to not be possible without ATOMIC_WRITE, the
        90071  +     ** paths are complex, so it seems prudent to leave the test in as
        90072  +     ** a NEVER(), in case our analysis is subtly flawed. */
        90073  +     NEVER(p->nSpill>0)
        90074  +#endif
        90075  +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
        90076  +     || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
        90077  +#endif
        90078  +  )){
        90079  +    rc = memjrnlCreateFile(p);
 89873  90080     }
 89874  90081     return rc;
 89875  90082   }
 89876  90083   #endif
 89877  90084   
 89878  90085   /*
 89879  90086   ** The file-handle passed as the only argument is open on a journal file.
................................................................................
 89932  90139   ** The return value from this routine is WRC_Abort to abandon the tree walk
 89933  90140   ** and WRC_Continue to continue.
 89934  90141   */
 89935  90142   static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
 89936  90143     int rc;
 89937  90144     testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
 89938  90145     testcase( ExprHasProperty(pExpr, EP_Reduced) );
 89939         -  rc = pWalker->xExprCallback(pWalker, pExpr);
 89940         -  if( rc ) return rc & WRC_Abort;
 89941         -  if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
 89942         -    if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
 89943         -    assert( pExpr->x.pList==0 || pExpr->pRight==0 );
 89944         -    if( pExpr->pRight ){
 89945         -      if( walkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
 89946         -    }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 89947         -      if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
 89948         -    }else if( pExpr->x.pList ){
 89949         -      if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
        90146  +  while(1){
        90147  +    rc = pWalker->xExprCallback(pWalker, pExpr);
        90148  +    if( rc ) return rc & WRC_Abort;
        90149  +    if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
        90150  +      if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
        90151  +       assert( pExpr->x.pList==0 || pExpr->pRight==0 );
        90152  +      if( pExpr->pRight ){
        90153  +        pExpr = pExpr->pRight;
        90154  +        continue;
        90155  +      }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
        90156  +        if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
        90157  +      }else if( pExpr->x.pList ){
        90158  +        if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
        90159  +      }
 89950  90160       }
        90161  +    break;
 89951  90162     }
 89952  90163     return WRC_Continue;
 89953  90164   }
 89954  90165   SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
 89955  90166     return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
 89956  90167   }
 89957  90168   
................................................................................
 91011  91222     ExprList *pEList;
 91012  91223     sqlite3 *db;
 91013  91224     int moreToDo = 1;
 91014  91225   
 91015  91226     pOrderBy = pSelect->pOrderBy;
 91016  91227     if( pOrderBy==0 ) return 0;
 91017  91228     db = pParse->db;
 91018         -#if SQLITE_MAX_COLUMN
 91019  91229     if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 91020  91230       sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
 91021  91231       return 1;
 91022  91232     }
 91023         -#endif
 91024  91233     for(i=0; i<pOrderBy->nExpr; i++){
 91025  91234       pOrderBy->a[i].done = 0;
 91026  91235     }
 91027  91236     pSelect->pNext = 0;
 91028  91237     while( pSelect->pPrior ){
 91029  91238       pSelect->pPrior->pNext = pSelect;
 91030  91239       pSelect = pSelect->pPrior;
................................................................................
 91108  91317   ){
 91109  91318     int i;
 91110  91319     sqlite3 *db = pParse->db;
 91111  91320     ExprList *pEList;
 91112  91321     struct ExprList_item *pItem;
 91113  91322   
 91114  91323     if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
 91115         -#if SQLITE_MAX_COLUMN
 91116  91324     if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 91117  91325       sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
 91118  91326       return 1;
 91119  91327     }
 91120         -#endif
 91121  91328     pEList = pSelect->pEList;
 91122  91329     assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
 91123  91330     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
 91124  91331       if( pItem->u.x.iOrderByCol ){
 91125  91332         if( pItem->u.x.iOrderByCol>pEList->nExpr ){
 91126  91333           resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
 91127  91334           return 1;
................................................................................
 91714  91921     return pExpr;
 91715  91922   }
 91716  91923   
 91717  91924   /*
 91718  91925   ** Return the collation sequence for the expression pExpr. If
 91719  91926   ** there is no defined collating sequence, return NULL.
 91720  91927   **
        91928  +** See also: sqlite3ExprNNCollSeq()
        91929  +**
        91930  +** The sqlite3ExprNNCollSeq() works the same exact that it returns the
        91931  +** default collation if pExpr has no defined collation.
        91932  +**
 91721  91933   ** The collating sequence might be determined by a COLLATE operator
 91722  91934   ** or by the presence of a column with a defined collating sequence.
 91723  91935   ** COLLATE operators take first precedence.  Left operands take
 91724  91936   ** precedence over right operands.
 91725  91937   */
 91726  91938   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
 91727  91939     sqlite3 *db = pParse->db;
................................................................................
 91777  91989       }
 91778  91990     }
 91779  91991     if( sqlite3CheckCollSeq(pParse, pColl) ){ 
 91780  91992       pColl = 0;
 91781  91993     }
 91782  91994     return pColl;
 91783  91995   }
        91996  +
        91997  +/*
        91998  +** Return the collation sequence for the expression pExpr. If
        91999  +** there is no defined collating sequence, return a pointer to the
        92000  +** defautl collation sequence.
        92001  +**
        92002  +** See also: sqlite3ExprCollSeq()
        92003  +**
        92004  +** The sqlite3ExprCollSeq() routine works the same except that it
        92005  +** returns NULL if there is no defined collation.
        92006  +*/
        92007  +SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr){
        92008  +  CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
        92009  +  if( p==0 ) p = pParse->db->pDfltColl;
        92010  +  assert( p!=0 );
        92011  +  return p;
        92012  +}
        92013  +
        92014  +/*
        92015  +** Return TRUE if the two expressions have equivalent collating sequences.
        92016  +*/
        92017  +SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, Expr *pE1, Expr *pE2){
        92018  +  CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
        92019  +  CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
        92020  +  return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
        92021  +}
 91784  92022   
 91785  92023   /*
 91786  92024   ** pExpr is an operand of a comparison operator.  aff2 is the
 91787  92025   ** type affinity of the other operand.  This routine returns the
 91788  92026   ** type affinity that should be used for the comparison operator.
 91789  92027   */
 91790  92028   SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
................................................................................
 92365  92603   SQLITE_PRIVATE Expr *sqlite3Expr(
 92366  92604     sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
 92367  92605     int op,                 /* Expression opcode */
 92368  92606     const char *zToken      /* Token argument.  Might be NULL */
 92369  92607   ){
 92370  92608     Token x;
 92371  92609     x.z = zToken;
 92372         -  x.n = zToken ? sqlite3Strlen30(zToken) : 0;
        92610  +  x.n = sqlite3Strlen30(zToken);
 92373  92611     return sqlite3ExprAlloc(db, op, &x, 0);
 92374  92612   }
 92375  92613   
 92376  92614   /*
 92377  92615   ** Attach subtrees pLeft and pRight to the Expr node pRoot.
 92378  92616   **
 92379  92617   ** If pRoot==NULL that means that a memory allocation error has occurred.
................................................................................
 92892  93130   SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
 92893  93131     ExprList *pNew;
 92894  93132     struct ExprList_item *pItem, *pOldItem;
 92895  93133     int i;
 92896  93134     Expr *pPriorSelectCol = 0;
 92897  93135     assert( db!=0 );
 92898  93136     if( p==0 ) return 0;
 92899         -  pNew = sqlite3DbMallocRawNN(db, 
 92900         -             sizeof(*pNew)+sizeof(pNew->a[0])*(p->nExpr-1) );
        93137  +  pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
 92901  93138     if( pNew==0 ) return 0;
 92902         -  pNew->nAlloc = pNew->nExpr = p->nExpr;
        93139  +  pNew->nExpr = p->nExpr;
 92903  93140     pItem = pNew->a;
 92904  93141     pOldItem = p->a;
 92905  93142     for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
 92906  93143       Expr *pOldExpr = pOldItem->pExpr;
 92907  93144       Expr *pNewExpr;
 92908  93145       pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
 92909  93146       if( pOldExpr 
................................................................................
 93049  93286   #endif
 93050  93287   
 93051  93288   
 93052  93289   /*
 93053  93290   ** Add a new element to the end of an expression list.  If pList is
 93054  93291   ** initially NULL, then create a new expression list.
 93055  93292   **
        93293  +** The pList argument must be either NULL or a pointer to an ExprList
        93294  +** obtained from a prior call to sqlite3ExprListAppend().  This routine
        93295  +** may not be used with an ExprList obtained from sqlite3ExprListDup().
        93296  +** Reason:  This routine assumes that the number of slots in pList->a[]
        93297  +** is a power of two.  That is true for sqlite3ExprListAppend() returns
        93298  +** but is not necessarily true from the return value of sqlite3ExprListDup().
        93299  +**
 93056  93300   ** If a memory allocation error occurs, the entire list is freed and
 93057  93301   ** NULL is returned.  If non-NULL is returned, then it is guaranteed
 93058  93302   ** that the new entry was successfully appended.
 93059  93303   */
 93060  93304   SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
 93061  93305     Parse *pParse,          /* Parsing context */
 93062  93306     ExprList *pList,        /* List to which to append. Might be NULL */
................................................................................
 93067  93311     assert( db!=0 );
 93068  93312     if( pList==0 ){
 93069  93313       pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
 93070  93314       if( pList==0 ){
 93071  93315         goto no_mem;
 93072  93316       }
 93073  93317       pList->nExpr = 0;
 93074         -    pList->nAlloc = 1;
 93075         -  }else if( pList->nExpr==pList->nAlloc ){
        93318  +  }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
 93076  93319       ExprList *pNew;
 93077  93320       pNew = sqlite3DbRealloc(db, pList, 
 93078         -             sizeof(*pList)+(2*pList->nAlloc - 1)*sizeof(pList->a[0]));
        93321  +             sizeof(*pList)+(2*pList->nExpr - 1)*sizeof(pList->a[0]));
 93079  93322       if( pNew==0 ){
 93080  93323         goto no_mem;
 93081  93324       }
 93082  93325       pList = pNew;
 93083         -    pList->nAlloc *= 2;
 93084  93326     }
 93085  93327     pItem = &pList->a[pList->nExpr++];
 93086  93328     assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) );
 93087  93329     assert( offsetof(struct ExprList_item,pExpr)==0 );
 93088  93330     memset(&pItem->zName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zName));
 93089  93331     pItem->pExpr = pExpr;
 93090  93332     return pList;
................................................................................
 93267  93509   /*
 93268  93510   ** Return the bitwise-OR of all Expr.flags fields in the given
 93269  93511   ** ExprList.
 93270  93512   */
 93271  93513   SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
 93272  93514     int i;
 93273  93515     u32 m = 0;
 93274         -  if( pList ){
 93275         -    for(i=0; i<pList->nExpr; i++){
 93276         -       Expr *pExpr = pList->a[i].pExpr;
 93277         -       assert( pExpr!=0 );
 93278         -       m |= pExpr->flags;
 93279         -    }
        93516  +  assert( pList!=0 );
        93517  +  for(i=0; i<pList->nExpr; i++){
        93518  +     Expr *pExpr = pList->a[i].pExpr;
        93519  +     assert( pExpr!=0 );
        93520  +     m |= pExpr->flags;
 93280  93521     }
 93281  93522     return m;
 93282  93523   }
        93524  +
        93525  +/*
        93526  +** This is a SELECT-node callback for the expression walker that
        93527  +** always "fails".  By "fail" in this case, we mean set
        93528  +** pWalker->eCode to zero and abort.
        93529  +**
        93530  +** This callback is used by multiple expression walkers.
        93531  +*/
        93532  +SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
        93533  +  UNUSED_PARAMETER(NotUsed);
        93534  +  pWalker->eCode = 0;
        93535  +  return WRC_Abort;
        93536  +}
 93283  93537   
 93284  93538   /*
 93285  93539   ** These routines are Walker callbacks used to check expressions to
 93286  93540   ** see if they are "constant" for some definition of constant.  The
 93287  93541   ** Walker.eCode value determines the type of "constant" we are looking
 93288  93542   ** for.
 93289  93543   **
................................................................................
 93353  93607           /* A bound parameter in a CREATE statement that originates from
 93354  93608           ** sqlite3_prepare() causes an error */
 93355  93609           pWalker->eCode = 0;
 93356  93610           return WRC_Abort;
 93357  93611         }
 93358  93612         /* Fall through */
 93359  93613       default:
 93360         -      testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
 93361         -      testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
        93614  +      testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail will disallow */
        93615  +      testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail will disallow */
 93362  93616         return WRC_Continue;
 93363  93617     }
 93364  93618   }
 93365         -static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
 93366         -  UNUSED_PARAMETER(NotUsed);
 93367         -  pWalker->eCode = 0;
 93368         -  return WRC_Abort;
 93369         -}
 93370  93619   static int exprIsConst(Expr *p, int initFlag, int iCur){
 93371  93620     Walker w;
 93372  93621     w.eCode = initFlag;
 93373  93622     w.xExprCallback = exprNodeIsConstant;
 93374         -  w.xSelectCallback = selectNodeIsConstant;
        93623  +  w.xSelectCallback = sqlite3SelectWalkFail;
 93375  93624   #ifdef SQLITE_DEBUG
 93376  93625     w.xSelectCallback2 = sqlite3SelectWalkAssert2;
 93377  93626   #endif
 93378  93627     w.u.iCur = iCur;
 93379  93628     sqlite3WalkExpr(&w, p);
 93380  93629     return w.eCode;
 93381  93630   }
................................................................................
 93421  93670     int i;
 93422  93671   
 93423  93672     /* Check if pExpr is identical to any GROUP BY term. If so, consider
 93424  93673     ** it constant.  */
 93425  93674     for(i=0; i<pGroupBy->nExpr; i++){
 93426  93675       Expr *p = pGroupBy->a[i].pExpr;
 93427  93676       if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
 93428         -      CollSeq *pColl = sqlite3ExprCollSeq(pWalker->pParse, p);
 93429         -      if( pColl==0 || sqlite3_stricmp("BINARY", pColl->zName)==0 ){
        93677  +      CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
        93678  +      if( sqlite3_stricmp("BINARY", pColl->zName)==0 ){
 93430  93679           return WRC_Prune;
 93431  93680         }
 93432  93681       }
 93433  93682     }
 93434  93683   
 93435  93684     /* Check if pExpr is a sub-select. If so, consider it variable. */
 93436  93685     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
................................................................................
 93490  93739   ** Walk an expression tree.  Return 1 if the expression contains a
 93491  93740   ** subquery of some kind.  Return 0 if there are no subqueries.
 93492  93741   */
 93493  93742   SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){
 93494  93743     Walker w;
 93495  93744     w.eCode = 1;
 93496  93745     w.xExprCallback = sqlite3ExprWalkNoop;
 93497         -  w.xSelectCallback = selectNodeIsConstant;
        93746  +  w.xSelectCallback = sqlite3SelectWalkFail;
 93498  93747   #ifdef SQLITE_DEBUG
 93499  93748     w.xSelectCallback2 = sqlite3SelectWalkAssert2;
 93500  93749   #endif
 93501  93750     sqlite3WalkExpr(&w, p);
 93502  93751     return w.eCode==0;
 93503  93752   }
 93504  93753   #endif
................................................................................
 93563  93812     switch( op ){
 93564  93813       case TK_INTEGER:
 93565  93814       case TK_STRING:
 93566  93815       case TK_FLOAT:
 93567  93816       case TK_BLOB:
 93568  93817         return 0;
 93569  93818       case TK_COLUMN:
 93570         -      assert( p->pTab!=0 );
 93571  93819         return ExprHasProperty(p, EP_CanBeNull) ||
        93820  +             p->pTab==0 ||  /* Reference to column of index on expression */
 93572  93821                (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
 93573  93822       default:
 93574  93823         return 1;
 93575  93824     }
 93576  93825   }
 93577  93826   
 93578  93827   /*
................................................................................
 94226  94475             assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 94227  94476             pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 94228  94477           }
 94229  94478   
 94230  94479           /* Loop through each expression in <exprlist>. */
 94231  94480           r1 = sqlite3GetTempReg(pParse);
 94232  94481           r2 = sqlite3GetTempReg(pParse);
 94233         -        if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
        94482  +        if( isRowid ) sqlite3VdbeAddOp4(v, OP_Blob, 0, r2, 0, "", P4_STATIC);
 94234  94483           for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
 94235  94484             Expr *pE2 = pItem->pExpr;
 94236  94485             int iValToIns;
 94237  94486   
 94238  94487             /* If the expression is not constant then we will need to
 94239  94488             ** disable the test that was generated above that makes sure
 94240  94489             ** this code only executes once.  Because for a non-constant
................................................................................
 94654  94903       sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
 94655  94904     }else{
 94656  94905       int c;
 94657  94906       i64 value;
 94658  94907       const char *z = pExpr->u.zToken;
 94659  94908       assert( z!=0 );
 94660  94909       c = sqlite3DecOrHexToI64(z, &value);
 94661         -    if( c==1 || (c==2 && !negFlag) || (negFlag && value==SMALLEST_INT64)){
        94910  +    if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){
 94662  94911   #ifdef SQLITE_OMIT_FLOATING_POINT
 94663  94912         sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
 94664  94913   #else
 94665  94914   #ifndef SQLITE_OMIT_HEX_INTEGER
 94666  94915         if( sqlite3_strnicmp(z,"0x",2)==0 ){
 94667  94916           sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
 94668  94917         }else
 94669  94918   #endif
 94670  94919         {
 94671  94920           codeReal(v, z, negFlag, iMem);
 94672  94921         }
 94673  94922   #endif
 94674  94923       }else{
 94675         -      if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
        94924  +      if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
 94676  94925         sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
 94677  94926       }
 94678  94927     }
 94679  94928   }
 94680  94929   
 94681  94930   /*
 94682  94931   ** Erase column-cache entry number i
................................................................................
 95823  96072     exprToRegister(pExpr, iMem);
 95824  96073   }
 95825  96074   
 95826  96075   /*
 95827  96076   ** Generate code that pushes the value of every element of the given
 95828  96077   ** expression list into a sequence of registers beginning at target.
 95829  96078   **
 95830         -** Return the number of elements evaluated.
        96079  +** Return the number of elements evaluated.  The number returned will
        96080  +** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
        96081  +** is defined.
 95831  96082   **
 95832  96083   ** The SQLITE_ECEL_DUP flag prevents the arguments from being
 95833  96084   ** filled using OP_SCopy.  OP_Copy must be used instead.
 95834  96085   **
 95835  96086   ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
 95836  96087   ** factored out into initialization code.
 95837  96088   **
 95838  96089   ** The SQLITE_ECEL_REF flag means that expressions in the list with
 95839  96090   ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
 95840  96091   ** in registers at srcReg, and so the value can be copied from there.
        96092  +** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
        96093  +** are simply omitted rather than being copied from srcReg.
 95841  96094   */
 95842  96095   SQLITE_PRIVATE int sqlite3ExprCodeExprList(
 95843  96096     Parse *pParse,     /* Parsing context */
 95844  96097     ExprList *pList,   /* The expression list to be coded */
 95845  96098     int target,        /* Where to write results */
 95846  96099     int srcReg,        /* Source registers if SQLITE_ECEL_REF */
 95847  96100     u8 flags           /* SQLITE_ECEL_* flags */
................................................................................
 97267  97520     int nTabName;             /* Number of UTF-8 characters in zTabName */
 97268  97521     const char *zTabName;     /* Original name of the table */
 97269  97522     Vdbe *v;
 97270  97523   #ifndef SQLITE_OMIT_TRIGGER
 97271  97524     char *zWhere = 0;         /* Where clause to locate temp triggers */
 97272  97525   #endif
 97273  97526     VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
 97274         -  int savedDbFlags;         /* Saved value of db->flags */
        97527  +  u32 savedDbFlags;         /* Saved value of db->mDbFlags */
 97275  97528   
 97276         -  savedDbFlags = db->flags;  
        97529  +  savedDbFlags = db->mDbFlags;  
 97277  97530     if( NEVER(db->mallocFailed) ) goto exit_rename_table;
 97278  97531     assert( pSrc->nSrc==1 );
 97279  97532     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 97280  97533   
 97281  97534     pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
 97282  97535     if( !pTab ) goto exit_rename_table;
 97283  97536     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 97284  97537     zDb = db->aDb[iDb].zDbSName;
 97285         -  db->flags |= SQLITE_PreferBuiltin;
        97538  +  db->mDbFlags |= DBFLAG_PreferBuiltin;
 97286  97539   
 97287  97540     /* Get a NULL terminated version of the new table name. */
 97288  97541     zName = sqlite3NameFromToken(db, pName);
 97289  97542     if( !zName ) goto exit_rename_table;
 97290  97543   
 97291  97544     /* Check that a table or index named 'zName' does not already exist
 97292  97545     ** in database iDb. If so, this is an error.
................................................................................
 97443  97696   
 97444  97697     /* Drop and reload the internal table schema. */
 97445  97698     reloadTableSchema(pParse, pTab, zName);
 97446  97699   
 97447  97700   exit_rename_table:
 97448  97701     sqlite3SrcListDelete(db, pSrc);
 97449  97702     sqlite3DbFree(db, zName);
 97450         -  db->flags = savedDbFlags;
        97703  +  db->mDbFlags = savedDbFlags;
 97451  97704   }
 97452  97705   
 97453  97706   /*
 97454  97707   ** This function is called after an "ALTER TABLE ... ADD" statement
 97455  97708   ** has been parsed. Argument pColDef contains the text of the new
 97456  97709   ** column definition.
 97457  97710   **
................................................................................
 97544  97797       sqlite3ValueFree(pVal);
 97545  97798     }
 97546  97799   
 97547  97800     /* Modify the CREATE TABLE statement. */
 97548  97801     zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
 97549  97802     if( zCol ){
 97550  97803       char *zEnd = &zCol[pColDef->n-1];
 97551         -    int savedDbFlags = db->flags;
        97804  +    u32 savedDbFlags = db->mDbFlags;
 97552  97805       while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
 97553  97806         *zEnd-- = '\0';
 97554  97807       }
 97555         -    db->flags |= SQLITE_PreferBuiltin;
        97808  +    db->mDbFlags |= DBFLAG_PreferBuiltin;
 97556  97809       sqlite3NestedParse(pParse, 
 97557  97810           "UPDATE \"%w\".%s SET "
 97558  97811             "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
 97559  97812           "WHERE type = 'table' AND name = %Q", 
 97560  97813         zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1,
 97561  97814         zTab
 97562  97815       );
 97563  97816       sqlite3DbFree(db, zCol);
 97564         -    db->flags = savedDbFlags;
        97817  +    db->mDbFlags = savedDbFlags;
 97565  97818     }
 97566  97819   
 97567  97820     /* Make sure the schema version is at least 3.  But do not upgrade
 97568  97821     ** from less than 3 to 4, as that will corrupt any preexisting DESC
 97569  97822     ** index.
 97570  97823     */
 97571  97824     r1 = sqlite3GetTempReg(pParse);
................................................................................
 99682  99935     */
 99683  99936     if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
 99684  99937       zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
 99685  99938         db->aLimit[SQLITE_LIMIT_ATTACHED]
 99686  99939       );
 99687  99940       goto attach_error;
 99688  99941     }
 99689         -  if( !db->autoCommit ){
 99690         -    zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
 99691         -    goto attach_error;
 99692         -  }
 99693  99942     for(i=0; i<db->nDb; i++){
 99694  99943       char *z = db->aDb[i].zDbSName;
 99695  99944       assert( z && zName );
 99696  99945       if( sqlite3StrICmp(z, zName)==0 ){
 99697  99946         zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
 99698  99947         goto attach_error;
 99699  99948       }
................................................................................
 99877 100126       sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
 99878 100127       goto detach_error;
 99879 100128     }
 99880 100129     if( i<2 ){
 99881 100130       sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
 99882 100131       goto detach_error;
 99883 100132     }
 99884         -  if( !db->autoCommit ){
 99885         -    sqlite3_snprintf(sizeof(zErr), zErr,
 99886         -                     "cannot DETACH database within transaction");
 99887         -    goto detach_error;
 99888         -  }
 99889 100133     if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
 99890 100134       sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
 99891 100135       goto detach_error;
 99892 100136     }
 99893 100137   
 99894 100138     sqlite3BtreeClose(pDb->pBt);
 99895 100139     pDb->pBt = 0;
................................................................................
100294 100538     if( db->init.busy ) return SQLITE_OK;
100295 100539     rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
100296 100540   #ifdef SQLITE_USER_AUTHENTICATION
100297 100541                    ,db->auth.zAuthUser
100298 100542   #endif
100299 100543                   );
100300 100544     if( rc==SQLITE_DENY ){
100301         -    if( db->nDb>2 || iDb!=0 ){
100302         -      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
100303         -    }else{
100304         -      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
100305         -    }
       100545  +    char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
       100546  +    if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
       100547  +    sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
100306 100548       pParse->rc = SQLITE_AUTH;
100307 100549     }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
100308 100550       sqliteAuthBadReturnCode(pParse);
100309 100551     }
100310 100552     return rc;
100311 100553   }
100312 100554   
................................................................................
100931 101173         while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
100932 101174         if( ALWAYS(p && p->pNext==pIndex) ){
100933 101175           p->pNext = pIndex->pNext;
100934 101176         }
100935 101177       }
100936 101178       freeIndex(db, pIndex);
100937 101179     }
100938         -  db->flags |= SQLITE_InternChanges;
       101180  +  db->mDbFlags |= DBFLAG_SchemaChange;
100939 101181   }
100940 101182   
100941 101183   /*
100942 101184   ** Look through the list of open database files in db->aDb[] and if
100943 101185   ** any have been closed, remove them from the list.  Reallocate the
100944 101186   ** db->aDb[] structure to a smaller size, if possible.
100945 101187   **
................................................................................
100966 101208       sqlite3DbFree(db, db->aDb);
100967 101209       db->aDb = db->aDbStatic;
100968 101210     }
100969 101211   }
100970 101212   
100971 101213   /*
100972 101214   ** Reset the schema for the database at index iDb.  Also reset the
100973         -** TEMP schema.
       101215  +** TEMP schema.  The reset is deferred if db->nSchemaLock is not zero.
       101216  +** Deferred resets may be run by calling with iDb<0.
100974 101217   */
100975 101218   SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
100976         -  Db *pDb;
       101219  +  int i;
100977 101220     assert( iDb<db->nDb );
100978 101221   
100979         -  /* Case 1:  Reset the single schema identified by iDb */
100980         -  pDb = &db->aDb[iDb];
100981         -  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
100982         -  assert( pDb->pSchema!=0 );
100983         -  sqlite3SchemaClear(pDb->pSchema);
       101222  +  if( iDb>=0 ){
       101223  +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
       101224  +    DbSetProperty(db, iDb, DB_ResetWanted);
       101225  +    DbSetProperty(db, 1, DB_ResetWanted);
       101226  +  }
100984 101227   
100985         -  /* If any database other than TEMP is reset, then also reset TEMP
100986         -  ** since TEMP might be holding triggers that reference tables in the
100987         -  ** other database.
100988         -  */
100989         -  if( iDb!=1 ){
100990         -    pDb = &db->aDb[1];
100991         -    assert( pDb->pSchema!=0 );
100992         -    sqlite3SchemaClear(pDb->pSchema);
       101228  +  if( db->nSchemaLock==0 ){
       101229  +    for(i=0; i<db->nDb; i++){
       101230  +      if( DbHasProperty(db, i, DB_ResetWanted) ){
       101231  +        sqlite3SchemaClear(db->aDb[i].pSchema);
       101232  +      }
       101233  +    }
100993 101234     }
100994         -  return;
100995 101235   }
100996 101236   
100997 101237   /*
100998 101238   ** Erase all schema information from all attached databases (including
100999 101239   ** "main" and "temp") for a single database connection.
101000 101240   */
101001 101241   SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
101002 101242     int i;
101003 101243     sqlite3BtreeEnterAll(db);
       101244  +  assert( db->nSchemaLock==0 );
101004 101245     for(i=0; i<db->nDb; i++){
101005 101246       Db *pDb = &db->aDb[i];
101006 101247       if( pDb->pSchema ){
101007 101248         sqlite3SchemaClear(pDb->pSchema);
101008 101249       }
101009 101250     }
101010         -  db->flags &= ~SQLITE_InternChanges;
       101251  +  db->mDbFlags &= ~DBFLAG_SchemaChange;
101011 101252     sqlite3VtabUnlockList(db);
101012 101253     sqlite3BtreeLeaveAll(db);
101013 101254     sqlite3CollapseDatabaseArray(db);
101014 101255   }
101015 101256   
101016 101257   /*
101017 101258   ** This routine is called when a commit occurs.
101018 101259   */
101019 101260   SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
101020         -  db->flags &= ~SQLITE_InternChanges;
       101261  +  db->mDbFlags &= ~DBFLAG_SchemaChange;
101021 101262   }
101022 101263   
101023 101264   /*
101024 101265   ** Delete memory allocated for the column names of a table or view (the
101025 101266   ** Table.aCol[] array).
101026 101267   */
101027 101268   SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
................................................................................
101051 101292   ** contains lookaside memory.  (Table objects in the schema do not use
101052 101293   ** lookaside memory, but some ephemeral Table objects do.)  Or the
101053 101294   ** db parameter can be used with db->pnBytesFreed to measure the memory
101054 101295   ** used by the Table object.
101055 101296   */
101056 101297   static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
101057 101298     Index *pIndex, *pNext;
101058         -  TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
101059 101299   
       101300  +#ifdef SQLITE_DEBUG
101060 101301     /* Record the number of outstanding lookaside allocations in schema Tables
101061 101302     ** prior to doing any free() operations.  Since schema Tables do not use
101062 101303     ** lookaside, this number should not change. */
101063         -  TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
101064         -                         db->lookaside.nOut : 0 );
       101304  +  int nLookaside = 0;
       101305  +  if( db && (pTable->tabFlags & TF_Ephemeral)==0 ){
       101306  +    nLookaside = sqlite3LookasideUsed(db, 0);
       101307  +  }
       101308  +#endif
101065 101309   
101066 101310     /* Delete all indices associated with this table. */
101067 101311     for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
101068 101312       pNext = pIndex->pNext;
101069 101313       assert( pIndex->pSchema==pTable->pSchema
101070 101314            || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
101071 101315       if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
................................................................................
101091 101335     sqlite3ExprListDelete(db, pTable->pCheck);
101092 101336   #ifndef SQLITE_OMIT_VIRTUALTABLE
101093 101337     sqlite3VtabClear(db, pTable);
101094 101338   #endif
101095 101339     sqlite3DbFree(db, pTable);
101096 101340   
101097 101341     /* Verify that no lookaside memory was used by schema tables */
101098         -  assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
       101342  +  assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
101099 101343   }
101100 101344   SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
101101 101345     /* Do not delete the table until the reference count reaches zero. */
101102 101346     if( !pTable ) return;
101103 101347     if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
101104 101348     deleteTable(db, pTable);
101105 101349   }
................................................................................
101117 101361     assert( iDb>=0 && iDb<db->nDb );
101118 101362     assert( zTabName );
101119 101363     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
101120 101364     testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
101121 101365     pDb = &db->aDb[iDb];
101122 101366     p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
101123 101367     sqlite3DeleteTable(db, p);
101124         -  db->flags |= SQLITE_InternChanges;
       101368  +  db->mDbFlags |= DBFLAG_SchemaChange;
101125 101369   }
101126 101370   
101127 101371   /*
101128 101372   ** Given a token, return a string that consists of the text of that
101129 101373   ** token.  Space to hold the returned string
101130 101374   ** is obtained from sqliteMalloc() and must be freed by the calling
101131 101375   ** function.
................................................................................
101230 101474       *pUnqual = pName2;
101231 101475       iDb = sqlite3FindDb(db, pName1);
101232 101476       if( iDb<0 ){
101233 101477         sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
101234 101478         return -1;
101235 101479       }
101236 101480     }else{
101237         -    assert( db->init.iDb==0 || db->init.busy || (db->flags & SQLITE_Vacuum)!=0);
       101481  +    assert( db->init.iDb==0 || db->init.busy
       101482  +             || (db->mDbFlags & DBFLAG_Vacuum)!=0);
101238 101483       iDb = db->init.iDb;
101239 101484       *pUnqual = pName1;
101240 101485     }
101241 101486     return iDb;
101242 101487   }
101243 101488   
101244 101489   /*
................................................................................
101462 101707       */
101463 101708   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
101464 101709       if( isView || isVirtual ){
101465 101710         sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
101466 101711       }else
101467 101712   #endif
101468 101713       {
101469         -      pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
       101714  +      pParse->addrCrTab =
       101715  +         sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY);
101470 101716       }
101471 101717       sqlite3OpenMasterTable(pParse, iDb);
101472 101718       sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
101473 101719       sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC);
101474 101720       sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
101475 101721       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
101476 101722       sqlite3VdbeAddOp0(v, OP_Close);
................................................................................
101511 101757     Table *p;
101512 101758     int i;
101513 101759     char *z;
101514 101760     char *zType;
101515 101761     Column *pCol;
101516 101762     sqlite3 *db = pParse->db;
101517 101763     if( (p = pParse->pNewTable)==0 ) return;
101518         -#if SQLITE_MAX_COLUMN
101519 101764     if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
101520 101765       sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
101521 101766       return;
101522 101767     }
101523         -#endif
101524 101768     z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
101525 101769     if( z==0 ) return;
101526 101770     memcpy(z, pName->z, pName->n);
101527 101771     z[pName->n] = 0;
101528 101772     sqlite3Dequote(z);
101529 101773     for(i=0; i<p->nCol; i++){
101530 101774       if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
................................................................................
102122 102366   ** This routine runs at the end of parsing a CREATE TABLE statement that
102123 102367   ** has a WITHOUT ROWID clause.  The job of this routine is to convert both
102124 102368   ** internal schema data structures and the generated VDBE code so that they
102125 102369   ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
102126 102370   ** Changes include:
102127 102371   **
102128 102372   **     (1)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
102129         -**     (2)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
102130         -**          no rowid btree for a WITHOUT ROWID.  Instead, the canonical
102131         -**          data storage is a covering index btree.
       102373  +**     (2)  Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY 
       102374  +**          into BTREE_BLOBKEY.
102132 102375   **     (3)  Bypass the creation of the sqlite_master table entry
102133 102376   **          for the PRIMARY KEY as the primary key index is now
102134 102377   **          identified by the sqlite_master table entry of the table itself.
102135 102378   **     (4)  Set the Index.tnum of the PRIMARY KEY Index object in the
102136 102379   **          schema to the rootpage from the main table.
102137 102380   **     (5)  Add all table columns to the PRIMARY KEY Index object
102138 102381   **          so that the PRIMARY KEY is a covering index.  The surplus
102139         -**          columns are part of KeyInfo.nXField and are not used for
       102382  +**          columns are part of KeyInfo.nAllField and are not used for
102140 102383   **          sorting or lookup or uniqueness checks.
102141 102384   **     (6)  Replace the rowid tail on all automatically generated UNIQUE
102142 102385   **          indices with the PRIMARY KEY columns.
102143 102386   **
102144 102387   ** For virtual tables, only (1) is performed.
102145 102388   */
102146 102389   static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
................................................................................
102161 102404       }
102162 102405     }
102163 102406   
102164 102407     /* The remaining transformations only apply to b-tree tables, not to
102165 102408     ** virtual tables */
102166 102409     if( IN_DECLARE_VTAB ) return;
102167 102410   
102168         -  /* Convert the OP_CreateTable opcode that would normally create the
102169         -  ** root-page for the table into an OP_CreateIndex opcode.  The index
102170         -  ** created will become the PRIMARY KEY index.
       102411  +  /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
       102412  +  ** into BTREE_BLOBKEY.
102171 102413     */
102172 102414     if( pParse->addrCrTab ){
102173 102415       assert( v );
102174         -    sqlite3VdbeChangeOpcode(v, pParse->addrCrTab, OP_CreateIndex);
       102416  +    sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY);
102175 102417     }
102176 102418   
102177 102419     /* Locate the PRIMARY KEY index.  Or, if this table was originally
102178 102420     ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. 
102179 102421     */
102180 102422     if( pTab->iPKey>=0 ){
102181 102423       ExprList *pList;
................................................................................
102507 102749       pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
102508 102750       if( pOld ){
102509 102751         assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
102510 102752         sqlite3OomFault(db);
102511 102753         return;
102512 102754       }
102513 102755       pParse->pNewTable = 0;
102514         -    db->flags |= SQLITE_InternChanges;
       102756  +    db->mDbFlags |= DBFLAG_SchemaChange;
102515 102757   
102516 102758   #ifndef SQLITE_OMIT_ALTERTABLE
102517 102759       if( !p->pSelect ){
102518 102760         const char *zName = (const char *)pParse->sNameToken.z;
102519 102761         int nName;
102520 102762         assert( !pSelect && pCons && pEnd );
102521 102763         if( pCons->z==0 ){
................................................................................
102606 102848   */
102607 102849   SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
102608 102850     Table *pSelTab;   /* A fake table from which we get the result set */
102609 102851     Select *pSel;     /* Copy of the SELECT that implements the view */
102610 102852     int nErr = 0;     /* Number of errors encountered */
102611 102853     int n;            /* Temporarily holds the number of cursors assigned */
102612 102854     sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
       102855  +#ifndef SQLITE_OMIT_VIRTUALTABLE	
       102856  +  int rc;
       102857  +#endif
102613 102858   #ifndef SQLITE_OMIT_AUTHORIZATION
102614 102859     sqlite3_xauth xAuth;       /* Saved xAuth pointer */
102615 102860   #endif
102616 102861   
102617 102862     assert( pTable );
102618 102863   
102619 102864   #ifndef SQLITE_OMIT_VIRTUALTABLE
102620         -  if( sqlite3VtabCallConnect(pParse, pTable) ){
102621         -    return SQLITE_ERROR;
       102865  +  db->nSchemaLock++;
       102866  +  rc = sqlite3VtabCallConnect(pParse, pTable);
       102867  +  db->nSchemaLock--;
       102868  +  if( rc ){
       102869  +    return 1;
102622 102870     }
102623 102871     if( IsVirtual(pTable) ) return 0;
102624 102872   #endif
102625 102873   
102626 102874   #ifndef SQLITE_OMIT_VIEW
102627 102875     /* A positive nCol means the columns names for this view are
102628 102876     ** already known.
................................................................................
102810 103058   /*
102811 103059   ** Write VDBE code to erase table pTab and all associated indices on disk.
102812 103060   ** Code to update the sqlite_master tables and internal schema definitions
102813 103061   ** in case a root-page belonging to another table is moved by the btree layer
102814 103062   ** is also added (this can happen with an auto-vacuum database).
102815 103063   */
102816 103064   static void destroyTable(Parse *pParse, Table *pTab){
102817         -#ifdef SQLITE_OMIT_AUTOVACUUM
102818         -  Index *pIdx;
102819         -  int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
102820         -  destroyRootPage(pParse, pTab->tnum, iDb);
102821         -  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
102822         -    destroyRootPage(pParse, pIdx->tnum, iDb);
102823         -  }
102824         -#else
102825 103065     /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
102826 103066     ** is not defined), then it is important to call OP_Destroy on the
102827 103067     ** table and index root-pages in order, starting with the numerically 
102828 103068     ** largest root-page number. This guarantees that none of the root-pages
102829 103069     ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
102830 103070     ** following were coded:
102831 103071     **
................................................................................
102860 103100       }else{
102861 103101         int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
102862 103102         assert( iDb>=0 && iDb<pParse->db->nDb );
102863 103103         destroyRootPage(pParse, iLargest, iDb);
102864 103104         iDestroyed = iLargest;
102865 103105       }
102866 103106     }
102867         -#endif
102868 103107   }
102869 103108   
102870 103109   /*
102871 103110   ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
102872 103111   ** after a DROP INDEX or DROP TABLE command.
102873 103112   */
102874 103113   static void sqlite3ClearStatTables(
................................................................................
103287 103526       sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
103288 103527                            pIndex->nKeyCol); VdbeCoverage(v);
103289 103528       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
103290 103529     }else{
103291 103530       addr2 = sqlite3VdbeCurrentAddr(v);
103292 103531     }
103293 103532     sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
103294         -  sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1);
       103533  +  sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
103295 103534     sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
103296 103535     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
103297 103536     sqlite3ReleaseTempReg(pParse, regRecord);
103298 103537     sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
103299 103538     sqlite3VdbeJumpHere(v, addr1);
103300 103539   
103301 103540     sqlite3VdbeAddOp1(v, OP_Close, iTab);
................................................................................
103776 104015       p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
103777 104016                             pIndex->zName, pIndex);
103778 104017       if( p ){
103779 104018         assert( p==pIndex );  /* Malloc must have failed */
103780 104019         sqlite3OomFault(db);
103781 104020         goto exit_create_index;
103782 104021       }
103783         -    db->flags |= SQLITE_InternChanges;
       104022  +    db->mDbFlags |= DBFLAG_SchemaChange;
103784 104023       if( pTblName!=0 ){
103785 104024         pIndex->tnum = db->init.newTnum;
103786 104025       }
103787 104026     }
103788 104027   
103789 104028     /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
103790 104029     ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
................................................................................
103812 104051       /* Create the rootpage for the index using CreateIndex. But before
103813 104052       ** doing so, code a Noop instruction and store its address in 
103814 104053       ** Index.tnum. This is required in case this index is actually a 
103815 104054       ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In 
103816 104055       ** that case the convertToWithoutRowidTable() routine will replace
103817 104056       ** the Noop with a Goto to jump over the VDBE code generated below. */
103818 104057       pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
103819         -    sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
       104058  +    sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
103820 104059   
103821 104060       /* Gather the complete text of the CREATE INDEX statement into
103822 104061       ** the zStmt variable
103823 104062       */
103824 104063       if( pStart ){
103825 104064         int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
103826 104065         if( pName->z[n-1]==';' ) n--;
................................................................................
104334 104573   
104335 104574   /*
104336 104575   ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 
104337 104576   ** element of the source-list passed as the second argument.
104338 104577   */
104339 104578   SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
104340 104579     assert( pIndexedBy!=0 );
104341         -  if( p && ALWAYS(p->nSrc>0) ){
104342         -    struct SrcList_item *pItem = &p->a[p->nSrc-1];
       104580  +  if( p && pIndexedBy->n>0 ){
       104581  +    struct SrcList_item *pItem;
       104582  +    assert( p->nSrc>0 );
       104583  +    pItem = &p->a[p->nSrc-1];
104343 104584       assert( pItem->fg.notIndexed==0 );
104344 104585       assert( pItem->fg.isIndexedBy==0 );
104345 104586       assert( pItem->fg.isTabFunc==0 );
104346 104587       if( pIndexedBy->n==1 && !pIndexedBy->z ){
104347 104588         /* A "NOT INDEXED" clause was supplied. See parse.y 
104348 104589         ** construct "indexed_opt" for details. */
104349 104590         pItem->fg.notIndexed = 1;
104350 104591       }else{
104351 104592         pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
104352         -      pItem->fg.isIndexedBy = (pItem->u1.zIndexedBy!=0);
       104593  +      pItem->fg.isIndexedBy = 1;
104353 104594       }
104354 104595     }
104355 104596   }
104356 104597   
104357 104598   /*
104358 104599   ** Add the list of function arguments to the SrcList entry for a
104359 104600   ** table-valued-function.
................................................................................
105270 105511         bestScore = score;
105271 105512       }
105272 105513       p = p->pNext;
105273 105514     }
105274 105515   
105275 105516     /* If no match is found, search the built-in functions.
105276 105517     **
105277         -  ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
       105518  +  ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
105278 105519     ** functions even if a prior app-defined function was found.  And give
105279 105520     ** priority to built-in functions.
105280 105521     **
105281 105522     ** Except, if createFlag is true, that means that we are trying to
105282 105523     ** install a new function.  Whatever FuncDef structure is returned it will
105283 105524     ** have fields overwritten with new information appropriate for the
105284 105525     ** new function.  But the FuncDefs for built-in functions are read-only.
105285 105526     ** So we must not search for built-ins when creating a new function.
105286 105527     */ 
105287         -  if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
       105528  +  if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
105288 105529       bestScore = 0;
105289 105530       h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ;
105290 105531       p = functionSearch(h, zName);
105291 105532       while( p ){
105292 105533         int score = matchQuality(p, nArg, enc);
105293 105534         if( score>bestScore ){
105294 105535           pBest = p;
................................................................................
105353 105594       sqlite3DeleteTable(0, pTab);
105354 105595     }
105355 105596     sqlite3HashClear(&temp1);
105356 105597     sqlite3HashClear(&pSchema->fkeyHash);
105357 105598     pSchema->pSeqTab = 0;
105358 105599     if( pSchema->schemaFlags & DB_SchemaLoaded ){
105359 105600       pSchema->iGeneration++;
105360         -    pSchema->schemaFlags &= ~DB_SchemaLoaded;
105361 105601     }
       105602  +  pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
105362 105603   }
105363 105604   
105364 105605   /*
105365 105606   ** Find and return the schema associated with a BTree.  Create
105366 105607   ** a new one if necessary.
105367 105608   */
105368 105609   SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
................................................................................
105886 106127         if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
105887 106128           assert( pPk!=0 || pTab->pSelect!=0 );
105888 106129           sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
105889 106130           VdbeCoverage(v);
105890 106131         }
105891 106132       }else if( pPk ){
105892 106133         addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
105893         -      sqlite3VdbeAddOp2(v, OP_RowData, iEphCur, iKey);
       106134  +      if( IsVirtual(pTab) ){
       106135  +        sqlite3VdbeAddOp3(v, OP_Column, iEphCur, 0, iKey);
       106136  +      }else{
       106137  +        sqlite3VdbeAddOp2(v, OP_RowData, iEphCur, iKey);
       106138  +      }
105894 106139         assert( nKey==0 );  /* OP_Found will use a composite key */
105895 106140       }else{
105896 106141         addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
105897 106142         VdbeCoverage(v);
105898 106143         assert( nKey==1 );
105899 106144       }  
105900 106145     
................................................................................
107157 107402     }else{
107158 107403       escape = pInfo->matchSet;
107159 107404     }
107160 107405     if( zA && zB ){
107161 107406   #ifdef SQLITE_TEST
107162 107407       sqlite3_like_count++;
107163 107408   #endif
107164         -    sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH);
       107409  +    sqlite3_result_int(context,
       107410  +                      patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH);
107165 107411     }
107166 107412   }
107167 107413   
107168 107414   /*
107169 107415   ** Implementation of the NULLIF(x,y) function.  The result is the first
107170 107416   ** argument if the arguments are different.  The result is NULL if the
107171 107417   ** arguments are equal to each other.
................................................................................
107998 108244     setLikeOptFlag(db, "like", 
107999 108245         caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
108000 108246   }
108001 108247   
108002 108248   /*
108003 108249   ** pExpr points to an expression which implements a function.  If
108004 108250   ** it is appropriate to apply the LIKE optimization to that function
108005         -** then set aWc[0] through aWc[2] to the wildcard characters and
108006         -** return TRUE.  If the function is not a LIKE-style function then
108007         -** return FALSE.
       108251  +** then set aWc[0] through aWc[2] to the wildcard characters and the
       108252  +** escape character and then return TRUE.  If the function is not a 
       108253  +** LIKE-style function then return FALSE.
       108254  +**
       108255  +** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
       108256  +** operator if c is a string literal that is exactly one byte in length.
       108257  +** That one byte is stored in aWc[3].  aWc[3] is set to zero if there is
       108258  +** no ESCAPE clause.
108008 108259   **
108009 108260   ** *pIsNocase is set to true if uppercase and lowercase are equivalent for
108010 108261   ** the function (default for LIKE).  If the function makes the distinction
108011 108262   ** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
108012 108263   ** false.
108013 108264   */
108014 108265   SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
108015 108266     FuncDef *pDef;
108016         -  if( pExpr->op!=TK_FUNCTION 
108017         -   || !pExpr->x.pList 
108018         -   || pExpr->x.pList->nExpr!=2
108019         -  ){
       108267  +  int nExpr;
       108268  +  if( pExpr->op!=TK_FUNCTION || !pExpr->x.pList ){
108020 108269       return 0;
108021 108270     }
108022 108271     assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
108023         -  pDef = sqlite3FindFunction(db, pExpr->u.zToken, 2, SQLITE_UTF8, 0);
       108272  +  nExpr = pExpr->x.pList->nExpr;
       108273  +  pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
108024 108274     if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
108025 108275       return 0;
108026 108276     }
       108277  +  if( nExpr<3 ){
       108278  +    aWc[3] = 0;
       108279  +  }else{
       108280  +    Expr *pEscape = pExpr->x.pList->a[2].pExpr;
       108281  +    char *zEscape;
       108282  +    if( pEscape->op!=TK_STRING ) return 0;
       108283  +    zEscape = pEscape->u.zToken;
       108284  +    if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
       108285  +    aWc[3] = zEscape[0];
       108286  +  }
108027 108287   
108028 108288     /* The memcpy() statement assumes that the wildcard characters are
108029 108289     ** the first three statements in the compareInfo structure.  The
108030 108290     ** asserts() that follow verify that assumption
108031 108291     */
108032 108292     memcpy(aWc, pDef->pUserData, 3);
108033 108293     assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
................................................................................
109819 110079   static int autoIncBegin(
109820 110080     Parse *pParse,      /* Parsing context */
109821 110081     int iDb,            /* Index of the database holding pTab */
109822 110082     Table *pTab         /* The table we are writing to */
109823 110083   ){
109824 110084     int memId = 0;      /* Register holding maximum rowid */
109825 110085     if( (pTab->tabFlags & TF_Autoincrement)!=0
109826         -   && (pParse->db->flags & SQLITE_Vacuum)==0
       110086  +   && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
109827 110087     ){
109828 110088       Parse *pToplevel = sqlite3ParseToplevel(pParse);
109829 110089       AutoincInfo *pInfo;
109830 110090   
109831 110091       pInfo = pToplevel->pAinc;
109832 110092       while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
109833 110093       if( pInfo==0 ){
................................................................................
110077 110337     SrcList *pTabList,    /* Name of table into which we are inserting */
110078 110338     Select *pSelect,      /* A SELECT statement to use as the data source */
110079 110339     IdList *pColumn,      /* Column names corresponding to IDLIST. */
110080 110340     int onError           /* How to handle constraint errors */
110081 110341   ){
110082 110342     sqlite3 *db;          /* The main database structure */
110083 110343     Table *pTab;          /* The table to insert into.  aka TABLE */
110084         -  char *zTab;           /* Name of the table into which we are inserting */
110085 110344     int i, j;             /* Loop counters */
110086 110345     Vdbe *v;              /* Generate code into this virtual machine */
110087 110346     Index *pIdx;          /* For looping over indices of the table */
110088 110347     int nColumn;          /* Number of columns in the data */
110089 110348     int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
110090 110349     int iDataCur = 0;     /* VDBE cursor that is the main data repository */
110091 110350     int iIdxCur = 0;      /* First index cursor */
................................................................................
110133 110392       sqlite3SelectDelete(db, pSelect);
110134 110393       pSelect = 0;
110135 110394     }
110136 110395   
110137 110396     /* Locate the table into which we will be inserting new information.
110138 110397     */
110139 110398     assert( pTabList->nSrc==1 );
110140         -  zTab = pTabList->a[0].zName;
110141         -  if( NEVER(zTab==0) ) goto insert_cleanup;
110142 110399     pTab = sqlite3SrcListLookup(pParse, pTabList);
110143 110400     if( pTab==0 ){
110144 110401       goto insert_cleanup;
110145 110402     }
110146 110403     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
110147 110404     assert( iDb<db->nDb );
110148 110405     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
................................................................................
111652 111909     if( pDest->iPKey!=pSrc->iPKey ){
111653 111910       return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
111654 111911     }
111655 111912     for(i=0; i<pDest->nCol; i++){
111656 111913       Column *pDestCol = &pDest->aCol[i];
111657 111914       Column *pSrcCol = &pSrc->aCol[i];
111658 111915   #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
111659         -    if( (db->flags & SQLITE_Vacuum)==0 
       111916  +    if( (db->mDbFlags & DBFLAG_Vacuum)==0 
111660 111917        && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN 
111661 111918       ){
111662 111919         return 0;    /* Neither table may have __hidden__ columns */
111663 111920       }
111664 111921   #endif
111665 111922       if( pDestCol->affinity!=pSrcCol->affinity ){
111666 111923         return 0;    /* Affinity must be the same on all columns */
................................................................................
111728 111985     iSrc = pParse->nTab++;
111729 111986     iDest = pParse->nTab++;
111730 111987     regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
111731 111988     regData = sqlite3GetTempReg(pParse);
111732 111989     regRowid = sqlite3GetTempReg(pParse);
111733 111990     sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
111734 111991     assert( HasRowid(pDest) || destHasUniqueIdx );
111735         -  if( (db->flags & SQLITE_Vacuum)==0 && (
       111992  +  if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
111736 111993         (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
111737 111994      || destHasUniqueIdx                              /* (2) */
111738 111995      || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
111739 111996     )){
111740 111997       /* In some circumstances, we are able to run the xfer optimization
111741 111998       ** only if the destination table is initially empty. Unless the
111742         -    ** SQLITE_Vacuum flag is set, this block generates code to make
111743         -    ** that determination. If SQLITE_Vacuum is set, then the destination
       111999  +    ** DBFLAG_Vacuum flag is set, this block generates code to make
       112000  +    ** that determination. If DBFLAG_Vacuum is set, then the destination
111744 112001       ** table is always empty.
111745 112002       **
111746 112003       ** Conditions under which the destination must be empty:
111747 112004       **
111748 112005       ** (1) There is no INTEGER PRIMARY KEY but there are indices.
111749 112006       **     (If the destination is not initially empty, the rowid fields
111750 112007       **     of index entries might need to change.)
................................................................................
111772 112029       }else if( pDest->pIndex==0 ){
111773 112030         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
111774 112031       }else{
111775 112032         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
111776 112033         assert( (pDest->tabFlags & TF_Autoincrement)==0 );
111777 112034       }
111778 112035       sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
111779         -    if( db->flags & SQLITE_Vacuum ){
111780         -      sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
       112036  +    if( db->mDbFlags & DBFLAG_Vacuum ){
       112037  +      sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
111781 112038         insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
111782 112039                              OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
111783 112040       }else{
111784 112041         insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND;
111785 112042       }
111786 112043       sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid,
111787 112044                         (char*)pDest, P4_TABLE);
................................................................................
111804 112061       VdbeComment((v, "%s", pSrcIdx->zName));
111805 112062       sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
111806 112063       sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
111807 112064       sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
111808 112065       VdbeComment((v, "%s", pDestIdx->zName));
111809 112066       addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
111810 112067       sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
111811         -    if( db->flags & SQLITE_Vacuum ){
       112068  +    if( db->mDbFlags & DBFLAG_Vacuum ){
111812 112069         /* This INSERT command is part of a VACUUM operation, which guarantees
111813 112070         ** that the destination table is empty. If all indexed columns use
111814 112071         ** collation sequence BINARY, then it can also be assumed that the
111815 112072         ** index will be populated by inserting keys in strictly sorted 
111816 112073         ** order. In this case, instead of seeking within the b-tree as part
111817         -      ** of every OP_IdxInsert opcode, an OP_Last is added before the
       112074  +      ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
111818 112075         ** OP_IdxInsert to seek to the point within the b-tree where each key 
111819 112076         ** should be inserted. This is faster.
111820 112077         **
111821 112078         ** If any of the indexed columns use a collation sequence other than
111822 112079         ** BINARY, this optimization is disabled. This is because the user 
111823 112080         ** might change the definition of a collation sequence and then run
111824 112081         ** a VACUUM command. In that case keys may not be written in strictly
................................................................................
111825 112082         ** sorted order.  */
111826 112083         for(i=0; i<pSrcIdx->nColumn; i++){
111827 112084           const char *zColl = pSrcIdx->azColl[i];
111828 112085           if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
111829 112086         }
111830 112087         if( i==pSrcIdx->nColumn ){
111831 112088           idxInsFlags = OPFLAG_USESEEKRESULT;
111832         -        sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
       112089  +        sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
111833 112090         }
111834 112091       }
111835 112092       if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
111836 112093         idxInsFlags |= OPFLAG_NCHANGE;
111837 112094       }
111838 112095       sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
111839 112096       sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
................................................................................
112156 112413     void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
112157 112414     void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
112158 112415     void  (*result_value)(sqlite3_context*,sqlite3_value*);
112159 112416     void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
112160 112417     int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
112161 112418                            const char*,const char*),void*);
112162 112419     void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
112163         -  char * (*snprintf)(int,char*,const char*,...);
       112420  +  char * (*xsnprintf)(int,char*,const char*,...);
112164 112421     int  (*step)(sqlite3_stmt*);
112165 112422     int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
112166 112423                                   char const**,char const**,int*,int*,int*);
112167 112424     void  (*thread_cleanup)(void);
112168 112425     int  (*total_changes)(sqlite3*);
112169 112426     void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
112170 112427     int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
................................................................................
112268 112525     const char *(*errstr)(int);
112269 112526     int (*stmt_busy)(sqlite3_stmt*);
112270 112527     int (*stmt_readonly)(sqlite3_stmt*);
112271 112528     int (*stricmp)(const char*,const char*);
112272 112529     int (*uri_boolean)(const char*,const char*,int);
112273 112530     sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
112274 112531     const char *(*uri_parameter)(const char*,const char*);
112275         -  char *(*vsnprintf)(int,char*,const char*,va_list);
       112532  +  char *(*xvsnprintf)(int,char*,const char*,va_list);
112276 112533     int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
112277 112534     /* Version 3.8.7 and later */
112278 112535     int (*auto_extension)(void(*)(void));
112279 112536     int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
112280 112537                        void(*)(void*));
112281 112538     int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
112282 112539                         void(*)(void*),unsigned char);
................................................................................
112440 112697   #define sqlite3_result_text16          sqlite3_api->result_text16
112441 112698   #define sqlite3_result_text16be        sqlite3_api->result_text16be
112442 112699   #define sqlite3_result_text16le        sqlite3_api->result_text16le
112443 112700   #define sqlite3_result_value           sqlite3_api->result_value
112444 112701   #define sqlite3_rollback_hook          sqlite3_api->rollback_hook
112445 112702   #define sqlite3_set_authorizer         sqlite3_api->set_authorizer
112446 112703   #define sqlite3_set_auxdata            sqlite3_api->set_auxdata
112447         -#define sqlite3_snprintf               sqlite3_api->snprintf
       112704  +#define sqlite3_snprintf               sqlite3_api->xsnprintf
112448 112705   #define sqlite3_step                   sqlite3_api->step
112449 112706   #define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
112450 112707   #define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
112451 112708   #define sqlite3_total_changes          sqlite3_api->total_changes
112452 112709   #define sqlite3_trace                  sqlite3_api->trace
112453 112710   #ifndef SQLITE_OMIT_DEPRECATED
112454 112711   #define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
................................................................................
112464 112721   #define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
112465 112722   #define sqlite3_value_text             sqlite3_api->value_text
112466 112723   #define sqlite3_value_text16           sqlite3_api->value_text16
112467 112724   #define sqlite3_value_text16be         sqlite3_api->value_text16be
112468 112725   #define sqlite3_value_text16le         sqlite3_api->value_text16le
112469 112726   #define sqlite3_value_type             sqlite3_api->value_type
112470 112727   #define sqlite3_vmprintf               sqlite3_api->vmprintf
112471         -#define sqlite3_vsnprintf              sqlite3_api->vsnprintf
       112728  +#define sqlite3_vsnprintf              sqlite3_api->xvsnprintf
112472 112729   #define sqlite3_overload_function      sqlite3_api->overload_function
112473 112730   #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
112474 112731   #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
112475 112732   #define sqlite3_clear_bindings         sqlite3_api->clear_bindings
112476 112733   #define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
112477 112734   #define sqlite3_blob_bytes             sqlite3_api->blob_bytes
112478 112735   #define sqlite3_blob_close             sqlite3_api->blob_close
................................................................................
112540 112797   #define sqlite3_errstr                 sqlite3_api->errstr
112541 112798   #define sqlite3_stmt_busy              sqlite3_api->stmt_busy
112542 112799   #define sqlite3_stmt_readonly          sqlite3_api->stmt_readonly
112543 112800   #define sqlite3_stricmp                sqlite3_api->stricmp
112544 112801   #define sqlite3_uri_boolean            sqlite3_api->uri_boolean
112545 112802   #define sqlite3_uri_int64              sqlite3_api->uri_int64
112546 112803   #define sqlite3_uri_parameter          sqlite3_api->uri_parameter
112547         -#define sqlite3_uri_vsnprintf          sqlite3_api->vsnprintf
       112804  +#define sqlite3_uri_vsnprintf          sqlite3_api->xvsnprintf
112548 112805   #define sqlite3_wal_checkpoint_v2      sqlite3_api->wal_checkpoint_v2
112549 112806   /* Version 3.8.7 and later */
112550 112807   #define sqlite3_auto_extension         sqlite3_api->auto_extension
112551 112808   #define sqlite3_bind_blob64            sqlite3_api->bind_blob64
112552 112809   #define sqlite3_bind_text64            sqlite3_api->bind_text64
112553 112810   #define sqlite3_cancel_auto_extension  sqlite3_api->cancel_auto_extension
112554 112811   #define sqlite3_load_extension         sqlite3_api->load_extension
................................................................................
114335 114592     }
114336 114593     return lwr>upr ? 0 : &aPragmaName[mid];
114337 114594   }
114338 114595   
114339 114596   /*
114340 114597   ** Helper subroutine for PRAGMA integrity_check:
114341 114598   **
114342         -** Generate code to output a single-column result row with the result
114343         -** held in register regResult.  Decrement the result count and halt if
114344         -** the maximum number of result rows have been issued.
       114599  +** Generate code to output a single-column result row with a value of the
       114600  +** string held in register 3.  Decrement the result count in register 1
       114601  +** and halt if the maximum number of result rows have been issued.
114345 114602   */
114346         -static int integrityCheckResultRow(Vdbe *v, int regResult){
       114603  +static int integrityCheckResultRow(Vdbe *v){
114347 114604     int addr;
114348         -  sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 1);
       114605  +  sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
114349 114606     addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
114350 114607     VdbeCoverage(v);
114351         -  sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
       114608  +  sqlite3VdbeAddOp0(v, OP_Halt);
114352 114609     return addr;
114353 114610   }
114354 114611   
114355 114612   /*
114356 114613   ** Process a pragma statement.  
114357 114614   **
114358 114615   ** Pragmas are of this form:
................................................................................
115271 115528       int i;
115272 115529       HashElem *j;
115273 115530       FuncDef *p;
115274 115531       pParse->nMem = 2;
115275 115532       for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
115276 115533         for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
115277 115534           sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1);
115278         -        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
115279 115535         }
115280 115536       }
115281 115537       for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
115282 115538         p = (FuncDef*)sqliteHashData(j);
115283 115539         sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 0);
115284         -      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
115285 115540       }
115286 115541     }
115287 115542     break;
115288 115543   
115289 115544   #ifndef SQLITE_OMIT_VIRTUALTABLE
115290 115545     case PragTyp_MODULE_LIST: {
115291 115546       HashElem *j;
115292 115547       pParse->nMem = 1;
115293 115548       for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
115294 115549         Module *pMod = (Module*)sqliteHashData(j);
115295 115550         sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
115296         -      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
115297 115551       }
115298 115552     }
115299 115553     break;
115300 115554   #endif /* SQLITE_OMIT_VIRTUALTABLE */
115301 115555   
115302 115556     case PragTyp_PRAGMA_LIST: {
115303 115557       int i;
115304 115558       for(i=0; i<ArraySize(aPragmaName); i++){
115305 115559         sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
115306         -      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
115307 115560       }
115308 115561     }
115309 115562     break;
115310 115563   #endif /* SQLITE_INTROSPECTION_PRAGMAS */
115311 115564   
115312 115565   #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
115313 115566   
................................................................................
115525 115778           mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
115526 115779         }
115527 115780       }
115528 115781       sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
115529 115782   
115530 115783       /* Do an integrity check on each database file */
115531 115784       for(i=0; i<db->nDb; i++){
115532         -      HashElem *x;
115533         -      Hash *pTbls;
115534         -      int *aRoot;
115535         -      int cnt = 0;
115536         -      int mxIdx = 0;
115537         -      int nIdx;
       115785  +      HashElem *x;     /* For looping over tables in the schema */
       115786  +      Hash *pTbls;     /* Set of all tables in the schema */
       115787  +      int *aRoot;      /* Array of root page numbers of all btrees */
       115788  +      int cnt = 0;     /* Number of entries in aRoot[] */
       115789  +      int mxIdx = 0;   /* Maximum number of indexes for any table */
115538 115790   
115539 115791         if( OMIT_TEMPDB && i==1 ) continue;
115540 115792         if( iDb>=0 && i!=iDb ) continue;
115541 115793   
115542 115794         sqlite3CodeVerifySchema(pParse, i);
115543 115795   
115544 115796         /* Do an integrity check of the B-Tree
................................................................................
115545 115797         **
115546 115798         ** Begin by finding the root pages numbers
115547 115799         ** for all tables and indices in the database.
115548 115800         */
115549 115801         assert( sqlite3SchemaMutexHeld(db, i, 0) );
115550 115802         pTbls = &db->aDb[i].pSchema->tblHash;
115551 115803         for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
115552         -        Table *pTab = sqliteHashData(x);
115553         -        Index *pIdx;
       115804  +        Table *pTab = sqliteHashData(x);  /* Current table */
       115805  +        Index *pIdx;                      /* An index on pTab */
       115806  +        int nIdx;                         /* Number of indexes on pTab */
115554 115807           if( HasRowid(pTab) ) cnt++;
115555 115808           for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
115556 115809           if( nIdx>mxIdx ) mxIdx = nIdx;
115557 115810         }
115558 115811         aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
115559 115812         if( aRoot==0 ) break;
115560 115813         for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
115561 115814           Table *pTab = sqliteHashData(x);
115562 115815           Index *pIdx;
115563         -        if( HasRowid(pTab) ) aRoot[cnt++] = pTab->tnum;
       115816  +        if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
115564 115817           for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
115565         -          aRoot[cnt++] = pIdx->tnum;
       115818  +          aRoot[++cnt] = pIdx->tnum;
115566 115819           }
115567 115820         }
115568         -      aRoot[cnt] = 0;
       115821  +      aRoot[0] = cnt;
115569 115822   
115570 115823         /* Make sure sufficient number of registers have been allocated */
115571 115824         pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
115572 115825         sqlite3ClearTempRegCache(pParse);
115573 115826   
115574 115827         /* Do the b-tree integrity checks */
115575 115828         sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
115576 115829         sqlite3VdbeChangeP5(v, (u8)i);
115577 115830         addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
115578 115831         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
115579 115832            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
115580 115833            P4_DYNAMIC);
115581         -      sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
115582         -      sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
115583         -      integrityCheckResultRow(v, 2);
       115834  +      sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
       115835  +      integrityCheckResultRow(v);
115584 115836         sqlite3VdbeJumpHere(v, addr);
115585 115837   
115586 115838         /* Make sure all the indices are constructed correctly.
115587 115839         */
115588 115840         for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
115589 115841           Table *pTab = sqliteHashData(x);
115590 115842           Index *pIdx, *pPk;
115591 115843           Index *pPrior = 0;
115592 115844           int loopTop;
115593 115845           int iDataCur, iIdxCur;
115594 115846           int r1 = -1;
115595 115847   
115596 115848           if( pTab->tnum<1 ) continue;  /* Skip VIEWs or VIRTUAL TABLEs */
115597         -        if( pTab->pCheck==0
115598         -         && (pTab->tabFlags & TF_HasNotNull)==0
115599         -         && (pTab->pIndex==0 || isQuick)
115600         -        ){
115601         -          continue;  /* No additional checks needed for this table */
115602         -        }
115603 115849           pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
115604 115850           sqlite3ExprCacheClear(pParse);
115605 115851           sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
115606 115852                                      1, 0, &iDataCur, &iIdxCur);
       115853  +        /* reg[7] counts the number of entries in the table.
       115854  +        ** reg[8+i] counts the number of entries in the i-th index 
       115855  +        */
115607 115856           sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
115608 115857           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
115609 115858             sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
115610 115859           }
115611 115860           assert( pParse->nMem>=8+j );
115612 115861           assert( sqlite3NoTempsInRange(pParse,1,7+j) );
115613 115862           sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
................................................................................
115620 115869             if( pTab->aCol[j].notNull==0 ) continue;
115621 115870             sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
115622 115871             sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
115623 115872             jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
115624 115873             zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
115625 115874                                 pTab->aCol[j].zName);
115626 115875             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
115627         -          integrityCheckResultRow(v, 3);
       115876  +          integrityCheckResultRow(v);
115628 115877             sqlite3VdbeJumpHere(v, jmp2);
115629 115878           }
115630 115879           /* Verify CHECK constraints */
115631 115880           if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
115632 115881             ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
115633 115882             if( db->mallocFailed==0 ){
115634 115883               int addrCkFault = sqlite3VdbeMakeLabel(v);
................................................................................
115643 115892               sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk, 
115644 115893                   SQLITE_JUMPIFNULL);
115645 115894               sqlite3VdbeResolveLabel(v, addrCkFault);
115646 115895               pParse->iSelfTab = 0;
115647 115896               zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
115648 115897                   pTab->zName);
115649 115898               sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
115650         -            integrityCheckResultRow(v, 3);
       115899  +            integrityCheckResultRow(v);
115651 115900               sqlite3VdbeResolveLabel(v, addrCkOk);
115652 115901               sqlite3ExprCachePop(pParse);
115653 115902             }
115654 115903             sqlite3ExprListDelete(db, pCheck);
115655 115904           }
115656         -        /* Validate index entries for the current row */
115657         -        for(j=0, pIdx=pTab->pIndex; pIdx && !isQuick; pIdx=pIdx->pNext, j++){
115658         -          int jmp2, jmp3, jmp4, jmp5;
115659         -          int ckUniq = sqlite3VdbeMakeLabel(v);
115660         -          if( pPk==pIdx ) continue;
115661         -          r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
115662         -                                       pPrior, r1);
115663         -          pPrior = pIdx;
115664         -          sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
115665         -          /* Verify that an index entry exists for the current table row */
115666         -          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
115667         -                                      pIdx->nColumn); VdbeCoverage(v);
115668         -          sqlite3VdbeLoadString(v, 3, "row ");
115669         -          sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
115670         -          sqlite3VdbeLoadString(v, 4, " missing from index ");
115671         -          sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
115672         -          jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
115673         -          sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
115674         -          jmp4 = integrityCheckResultRow(v, 3);
115675         -          sqlite3VdbeJumpHere(v, jmp2);
115676         -          /* For UNIQUE indexes, verify that only one entry exists with the
115677         -          ** current key.  The entry is unique if (1) any column is NULL
115678         -          ** or (2) the next entry has a different key */
115679         -          if( IsUniqueIndex(pIdx) ){
115680         -            int uniqOk = sqlite3VdbeMakeLabel(v);
115681         -            int jmp6;
115682         -            int kk;
115683         -            for(kk=0; kk<pIdx->nKeyCol; kk++){
115684         -              int iCol = pIdx->aiColumn[kk];
115685         -              assert( iCol!=XN_ROWID && iCol<pTab->nCol );
115686         -              if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
115687         -              sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
115688         -              VdbeCoverage(v);
115689         -            }
115690         -            jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
115691         -            sqlite3VdbeGoto(v, uniqOk);
115692         -            sqlite3VdbeJumpHere(v, jmp6);
115693         -            sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
115694         -                                 pIdx->nKeyCol); VdbeCoverage(v);
115695         -            sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
115696         -            sqlite3VdbeGoto(v, jmp5);
115697         -            sqlite3VdbeResolveLabel(v, uniqOk);
115698         -          }
115699         -          sqlite3VdbeJumpHere(v, jmp4);
115700         -          sqlite3ResolvePartIdxLabel(pParse, jmp3);
       115905  +        if( !isQuick ){ /* Omit the remaining tests for quick_check */
       115906  +          /* Sanity check on record header decoding */
       115907  +          sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nCol-1, 3);
       115908  +          sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
       115909  +          /* Validate index entries for the current row */
       115910  +          for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
       115911  +            int jmp2, jmp3, jmp4, jmp5;
       115912  +            int ckUniq = sqlite3VdbeMakeLabel(v);
       115913  +            if( pPk==pIdx ) continue;
       115914  +            r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
       115915  +                                         pPrior, r1);
       115916  +            pPrior = pIdx;
       115917  +            sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
       115918  +            /* Verify that an index entry exists for the current table row */
       115919  +            jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
       115920  +                                        pIdx->nColumn); VdbeCoverage(v);
       115921  +            sqlite3VdbeLoadString(v, 3, "row ");
       115922  +            sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
       115923  +            sqlite3VdbeLoadString(v, 4, " missing from index ");
       115924  +            sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
       115925  +            jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
       115926  +            sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
       115927  +            jmp4 = integrityCheckResultRow(v);
       115928  +            sqlite3VdbeJumpHere(v, jmp2);
       115929  +            /* For UNIQUE indexes, verify that only one entry exists with the
       115930  +            ** current key.  The entry is unique if (1) any column is NULL
       115931  +            ** or (2) the next entry has a different key */
       115932  +            if( IsUniqueIndex(pIdx) ){
       115933  +              int uniqOk = sqlite3VdbeMakeLabel(v);
       115934  +              int jmp6;
       115935  +              int kk;
       115936  +              for(kk=0; kk<pIdx->nKeyCol; kk++){
       115937  +                int iCol = pIdx->aiColumn[kk];
       115938  +                assert( iCol!=XN_ROWID && iCol<pTab->nCol );
       115939  +                if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
       115940  +                sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
       115941  +                VdbeCoverage(v);
       115942  +              }
       115943  +              jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
       115944  +              sqlite3VdbeGoto(v, uniqOk);
       115945  +              sqlite3VdbeJumpHere(v, jmp6);
       115946  +              sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
       115947  +                                   pIdx->nKeyCol); VdbeCoverage(v);
       115948  +              sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
       115949  +              sqlite3VdbeGoto(v, jmp5);
       115950  +              sqlite3VdbeResolveLabel(v, uniqOk);
       115951  +            }
       115952  +            sqlite3VdbeJumpHere(v, jmp4);
       115953  +            sqlite3ResolvePartIdxLabel(pParse, jmp3);
       115954  +          }
115701 115955           }
115702 115956           sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
115703 115957           sqlite3VdbeJumpHere(v, loopTop-1);
115704 115958   #ifndef SQLITE_OMIT_BTREECOUNT
115705 115959           if( !isQuick ){
115706 115960             sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
115707 115961             for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
115708 115962               if( pPk==pIdx ) continue;
115709 115963               sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
115710 115964               addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
115711 115965               sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
115712         -            sqlite3VdbeLoadString(v, 3, pIdx->zName);
115713         -            sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
115714         -            integrityCheckResultRow(v, 7);
       115966  +            sqlite3VdbeLoadString(v, 4, pIdx->zName);
       115967  +            sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
       115968  +            integrityCheckResultRow(v);
115715 115969               sqlite3VdbeJumpHere(v, addr);
115716 115970             }
115717 115971           }
115718 115972   #endif /* SQLITE_OMIT_BTREECOUNT */
115719 115973         } 
115720 115974       }
115721 115975       {
115722 115976         static const int iLn = VDBE_OFFSET_LINENO(2);
115723 115977         static const VdbeOpList endCode[] = {
115724 115978           { OP_AddImm,      1, 0,        0},    /* 0 */
115725 115979           { OP_IfNotZero,   1, 4,        0},    /* 1 */
115726 115980           { OP_String8,     0, 3,        0},    /* 2 */
115727 115981           { OP_ResultRow,   3, 1,        0},    /* 3 */
       115982  +        { OP_Halt,        0, 0,        0},    /* 4 */
       115983  +        { OP_String8,     0, 3,        0},    /* 5 */
       115984  +        { OP_Goto,        0, 3,        0},    /* 6 */
115728 115985         };
115729 115986         VdbeOp *aOp;
115730 115987   
115731 115988         aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
115732 115989         if( aOp ){
115733 115990           aOp[0].p2 = 1-mxErr;
115734 115991           aOp[2].p4type = P4_STATIC;
115735 115992           aOp[2].p4.z = "ok";
       115993  +        aOp[5].p4type = P4_STATIC;
       115994  +        aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
115736 115995         }
       115996  +      sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
115737 115997       }
115738 115998     }
115739 115999     break;
115740 116000   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
115741 116001   
115742 116002   #ifndef SQLITE_OMIT_UTF16
115743 116003     /*
................................................................................
116609 116869       db->init.iDb = iDb;
116610 116870       db->init.newTnum = sqlite3Atoi(argv[1]);
116611 116871       db->init.orphanTrigger = 0;
116612 116872       TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
116613 116873       rc = db->errCode;
116614 116874       assert( (rc&0xFF)==(rcp&0xFF) );
116615 116875       db->init.iDb = saved_iDb;
116616         -    assert( saved_iDb==0 || (db->flags & SQLITE_Vacuum)!=0 );
       116876  +    assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 );
116617 116877       if( SQLITE_OK!=rc ){
116618 116878         if( db->init.orphanTrigger ){
116619 116879           assert( iDb==1 );
116620 116880         }else{
116621 116881           pData->rc = rc;
116622 116882           if( rc==SQLITE_NOMEM ){
116623 116883             sqlite3OomFault(db);
................................................................................
116673 116933     const char *zMasterName;
116674 116934     int openedTransaction = 0;
116675 116935   
116676 116936     assert( iDb>=0 && iDb<db->nDb );
116677 116937     assert( db->aDb[iDb].pSchema );
116678 116938     assert( sqlite3_mutex_held(db->mutex) );
116679 116939     assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
       116940  +
       116941  +  db->init.busy = 1;
116680 116942   
116681 116943     /* Construct the in-memory representation schema tables (sqlite_master or
116682 116944     ** sqlite_temp_master) by invoking the parser directly.  The appropriate
116683 116945     ** table name will be inserted automatically by the parser so we can just
116684 116946     ** use the abbreviation "x" here.  The parser will also automatically tag
116685 116947     ** the schema table as read-only. */
116686 116948     azArg[0] = zMasterName = SCHEMA_TABLE(iDb);
116687 116949     azArg[1] = "1";
116688 116950     azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
116689         -                            "rootpage integer,sql text)";
       116951  +                            "rootpage int,sql text)";
116690 116952     azArg[3] = 0;
116691 116953     initData.db = db;
116692 116954     initData.iDb = iDb;
116693 116955     initData.rc = SQLITE_OK;
116694 116956     initData.pzErrMsg = pzErrMsg;
116695 116957     sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
116696 116958     if( initData.rc ){
................................................................................
116698 116960       goto error_out;
116699 116961     }
116700 116962   
116701 116963     /* Create a cursor to hold the database open
116702 116964     */
116703 116965     pDb = &db->aDb[iDb];
116704 116966     if( pDb->pBt==0 ){
116705         -    if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
116706         -      DbSetProperty(db, 1, DB_SchemaLoaded);
116707         -    }
116708         -    return SQLITE_OK;
       116967  +    assert( iDb==1 );
       116968  +    DbSetProperty(db, 1, DB_SchemaLoaded);
       116969  +    rc = SQLITE_OK;
       116970  +    goto error_out;
116709 116971     }
116710 116972   
116711 116973     /* If there is not already a read-only (or read-write) transaction opened
116712 116974     ** on the b-tree database, open one now. If a transaction is opened, it 
116713 116975     ** will be closed before this function returns.  */
116714 116976     sqlite3BtreeEnter(pDb->pBt);
116715 116977     if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
................................................................................
116860 117122   initone_error_out:
116861 117123     if( openedTransaction ){
116862 117124       sqlite3BtreeCommit(pDb->pBt);
116863 117125     }
116864 117126     sqlite3BtreeLeave(pDb->pBt);
116865 117127   
116866 117128   error_out:
116867         -  if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
116868         -    sqlite3OomFault(db);
       117129  +  if( rc ){
       117130  +    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
       117131  +      sqlite3OomFault(db);
       117132  +    }
       117133  +    sqlite3ResetOneSchema(db, iDb);
116869 117134     }
       117135  +  db->init.busy = 0;
116870 117136     return rc;
116871 117137   }
116872 117138   
116873 117139   /*
116874 117140   ** Initialize all database files - the main database file, the file
116875 117141   ** used to store temporary tables, and any additional database files
116876 117142   ** created using ATTACH statements.  Return a success code.  If an
................................................................................
116878 117144   **
116879 117145   ** After a database is initialized, the DB_SchemaLoaded bit is set
116880 117146   ** bit is set in the flags field of the Db structure. If the database
116881 117147   ** file was of zero-length, then the DB_Empty flag is also set.
116882 117148   */
116883 117149   SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
116884 117150     int i, rc;
116885         -  int commit_internal = !(db->flags&SQLITE_InternChanges);
       117151  +  int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
116886 117152     
116887 117153     assert( sqlite3_mutex_held(db->mutex) );
116888 117154     assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
116889 117155     assert( db->init.busy==0 );
116890         -  rc = SQLITE_OK;
116891         -  db->init.busy = 1;
116892 117156     ENC(db) = SCHEMA_ENC(db);
116893         -  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
116894         -    if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
116895         -    rc = sqlite3InitOne(db, i, pzErrMsg);
116896         -    if( rc ){
116897         -      sqlite3ResetOneSchema(db, i);
       117157  +  assert( db->nDb>0 );
       117158  +  /* Do the main schema first */
       117159  +  if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
       117160  +    rc = sqlite3InitOne(db, 0, pzErrMsg);
       117161  +    if( rc ) return rc;
       117162  +  }
       117163  +  /* All other schemas after the main schema. The "temp" schema must be last */
       117164  +  for(i=db->nDb-1; i>0; i--){
       117165  +    if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
       117166  +      rc = sqlite3InitOne(db, i, pzErrMsg);
       117167  +      if( rc ) return rc;
116898 117168       }
116899 117169     }
116900         -
116901         -  /* Once all the other databases have been initialized, load the schema
116902         -  ** for the TEMP database. This is loaded last, as the TEMP database
116903         -  ** schema may contain references to objects in other databases.
116904         -  */
116905         -#ifndef SQLITE_OMIT_TEMPDB
116906         -  assert( db->nDb>1 );
116907         -  if( rc==SQLITE_OK && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
116908         -    rc = sqlite3InitOne(db, 1, pzErrMsg);
116909         -    if( rc ){
116910         -      sqlite3ResetOneSchema(db, 1);
116911         -    }
116912         -  }
116913         -#endif
116914         -
116915         -  db->init.busy = 0;
116916         -  if( rc==SQLITE_OK && commit_internal ){
       117170  +  if( commit_internal ){
116917 117171       sqlite3CommitInternalChanges(db);
116918 117172     }
116919         -
116920         -  return rc; 
       117173  +  return SQLITE_OK;
116921 117174   }
116922 117175   
116923 117176   /*
116924 117177   ** This routine is a no-op if the database schema is already initialized.
116925 117178   ** Otherwise, the schema is loaded. An error code is returned.
116926 117179   */
116927 117180   SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
................................................................................
117018 117271     return i;
117019 117272   }
117020 117273   
117021 117274   /*
117022 117275   ** Free all memory allocations in the pParse object
117023 117276   */
117024 117277   SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
117025         -  if( pParse ){
117026         -    sqlite3 *db = pParse->db;
117027         -    sqlite3DbFree(db, pParse->aLabel);
117028         -    sqlite3ExprListDelete(db, pParse->pConstExpr);
117029         -    if( db ){
117030         -      assert( db->lookaside.bDisable >= pParse->disableLookaside );
117031         -      db->lookaside.bDisable -= pParse->disableLookaside;
117032         -    }
117033         -    pParse->disableLookaside = 0;
       117278  +  sqlite3 *db = pParse->db;
       117279  +  sqlite3DbFree(db, pParse->aLabel);
       117280  +  sqlite3ExprListDelete(db, pParse->pConstExpr);
       117281  +  if( db ){
       117282  +    assert( db->lookaside.bDisable >= pParse->disableLookaside );
       117283  +    db->lookaside.bDisable -= pParse->disableLookaside;
117034 117284     }
       117285  +  pParse->disableLookaside = 0;
117035 117286   }
117036 117287   
117037 117288   /*
117038 117289   ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
117039 117290   */
117040 117291   static int sqlite3Prepare(
117041 117292     sqlite3 *db,              /* Database handle. */
................................................................................
117213 117464     if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
117214 117465       return SQLITE_MISUSE_BKPT;
117215 117466     }
117216 117467     sqlite3_mutex_enter(db->mutex);
117217 117468     sqlite3BtreeEnterAll(db);
117218 117469     rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
117219 117470     if( rc==SQLITE_SCHEMA ){
       117471  +    sqlite3ResetOneSchema(db, -1);
117220 117472       sqlite3_finalize(*ppStmt);
117221 117473       rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
117222 117474     }
117223 117475     sqlite3BtreeLeaveAll(db);
117224 117476     sqlite3_mutex_leave(db->mutex);
117225 117477     assert( rc==SQLITE_OK || *ppStmt==0 );
117226 117478     return rc;
................................................................................
117506 117758       sqlite3SrcListDelete(db, p->pSrc);
117507 117759       sqlite3ExprDelete(db, p->pWhere);
117508 117760       sqlite3ExprListDelete(db, p->pGroupBy);
117509 117761       sqlite3ExprDelete(db, p->pHaving);
117510 117762       sqlite3ExprListDelete(db, p->pOrderBy);
117511 117763       sqlite3ExprDelete(db, p->pLimit);
117512 117764       sqlite3ExprDelete(db, p->pOffset);
117513         -    if( p->pWith ) sqlite3WithDelete(db, p->pWith);
       117765  +    if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
117514 117766       if( bFree ) sqlite3DbFreeNN(db, p);
117515 117767       p = pPrior;
117516 117768       bFree = 1;
117517 117769     }
117518 117770   }
117519 117771   
117520 117772   /*
................................................................................
117549 117801     Select standin;
117550 117802     pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
117551 117803     if( pNew==0 ){
117552 117804       assert( pParse->db->mallocFailed );
117553 117805       pNew = &standin;
117554 117806     }
117555 117807     if( pEList==0 ){
117556         -    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(pParse->db,TK_ASTERISK,0));
       117808  +    pEList = sqlite3ExprListAppend(pParse, 0,
       117809  +                                   sqlite3Expr(pParse->db,TK_ASTERISK,0));
117557 117810     }
117558 117811     pNew->pEList = pEList;
117559 117812     pNew->op = TK_SELECT;
117560 117813     pNew->selFlags = selFlags;
117561 117814     pNew->iLimit = 0;
117562 117815     pNew->iOffset = 0;
117563 117816   #if SELECTTRACE_ENABLED
................................................................................
117573 117826     pNew->pHaving = pHaving;
117574 117827     pNew->pOrderBy = pOrderBy;
117575 117828     pNew->pPrior = 0;
117576 117829     pNew->pNext = 0;
117577 117830     pNew->pLimit = pLimit;
117578 117831     pNew->pOffset = pOffset;
117579 117832     pNew->pWith = 0;
117580         -  assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || pParse->db->mallocFailed!=0 );
       117833  +  assert( pOffset==0 || pLimit!=0 || pParse->nErr>0
       117834  +                     || pParse->db->mallocFailed!=0 );
117581 117835     if( pParse->db->mallocFailed ) {
117582 117836       clearSelect(pParse->db, pNew, pNew!=&standin);
117583 117837       pNew = 0;
117584 117838     }else{
117585 117839       assert( pNew->pSrc!=0 || pParse->nErr>0 );
117586 117840     }
117587 117841     assert( pNew!=&standin );
................................................................................
117600 117854   #endif
117601 117855   
117602 117856   
117603 117857   /*
117604 117858   ** Delete the given Select structure and all of its substructures.
117605 117859   */
117606 117860   SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
117607         -  if( p ) clearSelect(db, p, 1);
       117861  +  if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1);
117608 117862   }
117609 117863   
117610 117864   /*
117611 117865   ** Return a pointer to the right-most SELECT statement in a compound.
117612 117866   */
117613 117867   static Select *findRightmost(Select *p){
117614 117868     while( p->pNext ) p = p->pNext;
................................................................................
117841 118095     struct SrcList_item *pLeft;     /* Left table being joined */
117842 118096     struct SrcList_item *pRight;    /* Right table being joined */
117843 118097   
117844 118098     pSrc = p->pSrc;
117845 118099     pLeft = &pSrc->a[0];
117846 118100     pRight = &pLeft[1];
117847 118101     for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
117848         -    Table *pLeftTab = pLeft->pTab;
117849 118102       Table *pRightTab = pRight->pTab;
117850 118103       int isOuter;
117851 118104   
117852         -    if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
       118105  +    if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
117853 118106       isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
117854 118107   
117855 118108       /* When the NATURAL keyword is present, add WHERE clause terms for
117856 118109       ** every column that the two tables have in common.
117857 118110       */
117858 118111       if( pRight->fg.jointype & JT_NATURAL ){
117859 118112         if( pRight->pOn || pRight->pUsing ){
................................................................................
117993 118246       }
117994 118247       VdbeCoverage(v);
117995 118248       sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
117996 118249       pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
117997 118250       if( pParse->db->mallocFailed ) return;
117998 118251       pOp->p2 = nKey + nData;
117999 118252       pKI = pOp->p4.pKeyInfo;
118000         -    memset(pKI->aSortOrder, 0, pKI->nField); /* Makes OP_Jump below testable */
       118253  +    memset(pKI->aSortOrder, 0, pKI->nKeyField); /* Makes OP_Jump testable */
118001 118254       sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
118002         -    testcase( pKI->nXField>2 );
       118255  +    testcase( pKI->nAllField > pKI->nKeyField+2 );
118003 118256       pOp->p4.pKeyInfo = keyInfoFromExprList(pParse, pSort->pOrderBy, nOBSat,
118004         -                                           pKI->nXField-1);
       118257  +                                           pKI->nAllField-pKI->nKeyField-1);
118005 118258       addrJmp = sqlite3VdbeCurrentAddr(v);
118006 118259       sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
118007 118260       pSort->labelBkOut = sqlite3VdbeMakeLabel(v);
118008 118261       pSort->regReturn = ++pParse->nMem;
118009 118262       sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
118010 118263       sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
118011 118264       if( iLimit ){
................................................................................
118095 118348     sqlite3ReleaseTempReg(pParse, r1);
118096 118349   }
118097 118350   
118098 118351   /*
118099 118352   ** This routine generates the code for the inside of the inner loop
118100 118353   ** of a SELECT.
118101 118354   **
118102         -** If srcTab is negative, then the pEList expressions
       118355  +** If srcTab is negative, then the p->pEList expressions
118103 118356   ** are evaluated in order to get the data for this row.  If srcTab is
118104         -** zero or more, then data is pulled from srcTab and pEList is used only 
       118357  +** zero or more, then data is pulled from srcTab and p->pEList is used only 
118105 118358   ** to get the number of columns and the collation sequence for each column.
118106 118359   */
118107 118360   static void selectInnerLoop(
118108 118361     Parse *pParse,          /* The parser context */
118109 118362     Select *p,              /* The complete select statement being coded */
118110         -  ExprList *pEList,       /* List of values being extracted */
118111         -  int srcTab,             /* Pull data from this table */
       118363  +  int srcTab,             /* Pull data from this table if non-negative */
118112 118364     SortCtx *pSort,         /* If not NULL, info on how to process ORDER BY */
118113 118365     DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
118114 118366     SelectDest *pDest,      /* How to dispose of the results */
118115 118367     int iContinue,          /* Jump here to continue with next row */
118116 118368     int iBreak              /* Jump here to break out of the inner loop */
118117 118369   ){
118118 118370     Vdbe *v = pParse->pVdbe;
................................................................................
118128 118380     ** same value. However, if the results are being sent to the sorter, the
118129 118381     ** values for any expressions that are also part of the sort-key are omitted
118130 118382     ** from this array. In this case regOrig is set to zero.  */
118131 118383     int regResult;              /* Start of memory holding current results */
118132 118384     int regOrig;                /* Start of memory holding full result (or 0) */
118133 118385   
118134 118386     assert( v );
118135         -  assert( pEList!=0 );
       118387  +  assert( p->pEList!=0 );
118136 118388     hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
118137 118389     if( pSort && pSort->pOrderBy==0 ) pSort = 0;
118138 118390     if( pSort==0 && !hasDistinct ){
118139 118391       assert( iContinue!=0 );
118140 118392       codeOffset(v, p->iOffset, iContinue);
118141 118393     }
118142 118394   
118143 118395     /* Pull the requested columns.
118144 118396     */
118145         -  nResultCol = pEList->nExpr;
       118397  +  nResultCol = p->pEList->nExpr;
118146 118398   
118147 118399     if( pDest->iSdst==0 ){
118148 118400       if( pSort ){
118149 118401         nPrefixReg = pSort->pOrderBy->nExpr;
118150 118402         if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
118151 118403         pParse->nMem += nPrefixReg;
118152 118404       }
................................................................................
118161 118413       pParse->nMem += nResultCol;
118162 118414     }
118163 118415     pDest->nSdst = nResultCol;
118164 118416     regOrig = regResult = pDest->iSdst;
118165 118417     if( srcTab>=0 ){
118166 118418       for(i=0; i<nResultCol; i++){
118167 118419         sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
118168         -      VdbeComment((v, "%s", pEList->a[i].zName));
       118420  +      VdbeComment((v, "%s", p->pEList->a[i].zName));
118169 118421       }
118170 118422     }else if( eDest!=SRT_Exists ){
118171 118423       /* If the destination is an EXISTS(...) expression, the actual
118172 118424       ** values returned by the SELECT are not required.
118173 118425       */
118174 118426       u8 ecelFlags;
118175 118427       if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
118176 118428         ecelFlags = SQLITE_ECEL_DUP;
118177 118429       }else{
118178 118430         ecelFlags = 0;
118179 118431       }
118180 118432       if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
118181         -      /* For each expression in pEList that is a copy of an expression in
       118433  +      /* For each expression in p->pEList that is a copy of an expression in
118182 118434         ** the ORDER BY clause (pSort->pOrderBy), set the associated 
118183 118435         ** iOrderByCol value to one more than the index of the ORDER BY 
118184 118436         ** expression within the sort-key that pushOntoSorter() will generate.
118185         -      ** This allows the pEList field to be omitted from the sorted record,
       118437  +      ** This allows the p->pEList field to be omitted from the sorted record,
118186 118438         ** saving space and CPU cycles.  */
118187 118439         ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
118188 118440         for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
118189 118441           int j;
118190 118442           if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
118191         -          pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
       118443  +          p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
118192 118444           }
118193 118445         }
118194 118446         regOrig = 0;
118195 118447         assert( eDest==SRT_Set || eDest==SRT_Mem 
118196 118448              || eDest==SRT_Coroutine || eDest==SRT_Output );
118197 118449       }
118198         -    nResultCol = sqlite3ExprCodeExprList(pParse,pEList,regResult,0,ecelFlags);
       118450  +    nResultCol = sqlite3ExprCodeExprList(pParse,p->pEList,regResult,
       118451  +                                         0,ecelFlags);
118199 118452     }
118200 118453   
118201 118454     /* If the DISTINCT keyword was present on the SELECT statement
118202 118455     ** and this row has been seen before, then do not make this row
118203 118456     ** part of the result.
118204 118457     */
118205 118458     if( hasDistinct ){
................................................................................
118223 118476           pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
118224 118477           pOp->opcode = OP_Null;
118225 118478           pOp->p1 = 1;
118226 118479           pOp->p2 = regPrev;
118227 118480   
118228 118481           iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
118229 118482           for(i=0; i<nResultCol; i++){
118230         -          CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
       118483  +          CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
118231 118484             if( i<nResultCol-1 ){
118232 118485               sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
118233 118486               VdbeCoverage(v);
118234 118487             }else{
118235 118488               sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
118236 118489               VdbeCoverage(v);
118237 118490              }
................................................................................
118466 118719   ** X extra columns.
118467 118720   */
118468 118721   SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
118469 118722     int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
118470 118723     KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
118471 118724     if( p ){
118472 118725       p->aSortOrder = (u8*)&p->aColl[N+X];
118473         -    p->nField = (u16)N;
118474         -    p->nXField = (u16)X;
       118726  +    p->nKeyField = (u16)N;
       118727  +    p->nAllField = (u16)(N+X);
118475 118728       p->enc = ENC(db);
118476 118729       p->db = db;
118477 118730       p->nRef = 1;
118478 118731       memset(&p[1], 0, nExtra);
118479 118732     }else{
118480 118733       sqlite3OomFault(db);
118481 118734     }
................................................................................
118541 118794     int i;
118542 118795   
118543 118796     nExpr = pList->nExpr;
118544 118797     pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
118545 118798     if( pInfo ){
118546 118799       assert( sqlite3KeyInfoIsWriteable(pInfo) );
118547 118800       for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
118548         -      CollSeq *pColl;
118549         -      pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
118550         -      if( !pColl ) pColl = db->pDfltColl;
118551         -      pInfo->aColl[i-iStart] = pColl;
       118801  +      pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
118552 118802         pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
118553 118803       }
118554 118804     }
118555 118805     return pInfo;
118556 118806   }
118557 118807   
118558 118808   /*
................................................................................
118794 119044   ** 
118795 119045   ** The declaration type for any expression other than a column is NULL.
118796 119046   **
118797 119047   ** This routine has either 3 or 6 parameters depending on whether or not
118798 119048   ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
118799 119049   */
118800 119050   #ifdef SQLITE_ENABLE_COLUMN_METADATA
118801         -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
       119051  +# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
118802 119052   #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
118803         -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
       119053  +# define columnType(A,B,C,D,E) columnTypeImpl(A,B)
118804 119054   #endif
118805 119055   static const char *columnTypeImpl(
118806 119056     NameContext *pNC, 
       119057  +#ifndef SQLITE_ENABLE_COLUMN_METADATA
       119058  +  Expr *pExpr
       119059  +#else
118807 119060     Expr *pExpr,
118808         -#ifdef SQLITE_ENABLE_COLUMN_METADATA
118809 119061     const char **pzOrigDb,
118810 119062     const char **pzOrigTab,
118811         -  const char **pzOrigCol,
       119063  +  const char **pzOrigCol
118812 119064   #endif
118813         -  u8 *pEstWidth
118814 119065   ){
118815 119066     char const *zType = 0;
118816 119067     int j;
118817         -  u8 estWidth = 1;
118818 119068   #ifdef SQLITE_ENABLE_COLUMN_METADATA
118819 119069     char const *zOrigDb = 0;
118820 119070     char const *zOrigTab = 0;
118821 119071     char const *zOrigCol = 0;
118822 119072   #endif
118823 119073   
118824 119074     assert( pExpr!=0 );
................................................................................
118879 119129             ** test case misc2.2.2) - it always evaluates to NULL.
118880 119130             */
118881 119131             NameContext sNC;
118882 119132             Expr *p = pS->pEList->a[iCol].pExpr;
118883 119133             sNC.pSrcList = pS->pSrc;
118884 119134             sNC.pNext = pNC;
118885 119135             sNC.pParse = pNC->pParse;
118886         -          zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth); 
       119136  +          zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol); 
118887 119137           }
118888         -      }else if( pTab->pSchema ){
118889         -        /* A real table */
       119138  +      }else{
       119139  +        /* A real table or a CTE table */
118890 119140           assert( !pS );
       119141  +#ifdef SQLITE_ENABLE_COLUMN_METADATA
118891 119142           if( iCol<0 ) iCol = pTab->iPKey;
118892         -        assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
118893         -#ifdef SQLITE_ENABLE_COLUMN_METADATA
       119143  +        assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
118894 119144           if( iCol<0 ){
118895 119145             zType = "INTEGER";
118896 119146             zOrigCol = "rowid";
118897 119147           }else{
118898 119148             zOrigCol = pTab->aCol[iCol].zName;
118899 119149             zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
118900         -          estWidth = pTab->aCol[iCol].szEst;
118901 119150           }
118902 119151           zOrigTab = pTab->zName;
118903         -        if( pNC->pParse ){
       119152  +        if( pNC->pParse && pTab->pSchema ){
118904 119153             int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
118905 119154             zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
118906 119155           }
118907 119156   #else
       119157  +        assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
118908 119158           if( iCol<0 ){
118909 119159             zType = "INTEGER";
118910 119160           }else{
118911 119161             zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
118912         -          estWidth = pTab->aCol[iCol].szEst;
118913 119162           }
118914 119163   #endif
118915 119164         }
118916 119165         break;
118917 119166       }
118918 119167   #ifndef SQLITE_OMIT_SUBQUERY
118919 119168       case TK_SELECT: {
................................................................................
118924 119173         NameContext sNC;
118925 119174         Select *pS = pExpr->x.pSelect;
118926 119175         Expr *p = pS->pEList->a[0].pExpr;
118927 119176         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
118928 119177         sNC.pSrcList = pS->pSrc;
118929 119178         sNC.pNext = pNC;
118930 119179         sNC.pParse = pNC->pParse;
118931         -      zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth); 
       119180  +      zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); 
118932 119181         break;
118933 119182       }
118934 119183   #endif
118935 119184     }
118936 119185   
118937 119186   #ifdef SQLITE_ENABLE_COLUMN_METADATA  
118938 119187     if( pzOrigDb ){
118939 119188       assert( pzOrigTab && pzOrigCol );
118940 119189       *pzOrigDb = zOrigDb;
118941 119190       *pzOrigTab = zOrigTab;
118942 119191       *pzOrigCol = zOrigCol;
118943 119192     }
118944 119193   #endif
118945         -  if( pEstWidth ) *pEstWidth = estWidth;
118946 119194     return zType;
118947 119195   }
118948 119196   
118949 119197   /*
118950 119198   ** Generate code that will tell the VDBE the declaration types of columns
118951 119199   ** in the result set.
118952 119200   */
................................................................................
118965 119213     for(i=0; i<pEList->nExpr; i++){
118966 119214       Expr *p = pEList->a[i].pExpr;
118967 119215       const char *zType;
118968 119216   #ifdef SQLITE_ENABLE_COLUMN_METADATA
118969 119217       const char *zOrigDb = 0;
118970 119218       const char *zOrigTab = 0;
118971 119219       const char *zOrigCol = 0;
118972         -    zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0);
       119220  +    zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
118973 119221   
118974 119222       /* The vdbe must make its own copy of the column-type and other 
118975 119223       ** column specific strings, in case the schema is reset before this
118976 119224       ** virtual machine is deleted.
118977 119225       */
118978 119226       sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
118979 119227       sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
118980 119228       sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
118981 119229   #else
118982         -    zType = columnType(&sNC, p, 0, 0, 0, 0);
       119230  +    zType = columnType(&sNC, p, 0, 0, 0);
118983 119231   #endif
118984 119232       sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
118985 119233     }
118986 119234   #endif /* !defined(SQLITE_OMIT_DECLTYPE) */
118987 119235   }
118988 119236   
118989 119237   
................................................................................
119005 119253   ** other modes for legacy:
119006 119254   **
119007 119255   **    short=OFF, full=OFF:      Column name is the text of the expression has it
119008 119256   **                              originally appears in the SELECT statement.  In
119009 119257   **                              other words, the zSpan of the result expression.
119010 119258   **
119011 119259   **    short=ON, full=OFF:       (This is the default setting).  If the result
119012         -**                              refers directly to a table column, then the result
119013         -**                              column name is just the table column name: COLUMN. 
119014         -**                              Otherwise use zSpan.
       119260  +**                              refers directly to a table column, then the
       119261  +**                              result column name is just the table column
       119262  +**                              name: COLUMN.  Otherwise use zSpan.
119015 119263   **
119016 119264   **    full=ON, short=ANY:       If the result refers directly to a table column,
119017 119265   **                              then the result column name with the table name
119018 119266   **                              prefix, ex: TABLE.COLUMN.  Otherwise use zSpan.
119019 119267   */
119020 119268   static void generateColumnNames(
119021 119269     Parse *pParse,      /* Parser context */
................................................................................
119048 119296     fullName = (db->flags & SQLITE_FullColNames)!=0;
119049 119297     srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
119050 119298     sqlite3VdbeSetNumCols(v, pEList->nExpr);
119051 119299     for(i=0; i<pEList->nExpr; i++){
119052 119300       Expr *p = pEList->a[i].pExpr;
119053 119301   
119054 119302       assert( p!=0 );
       119303  +    assert( p->op!=TK_AGG_COLUMN );  /* Agg processing has not run yet */
       119304  +    assert( p->op!=TK_COLUMN || p->pTab!=0 ); /* Covering idx not yet coded */
119055 119305       if( pEList->a[i].zName ){
119056 119306         /* An AS clause always takes first priority */
119057 119307         char *zName = pEList->a[i].zName;
119058 119308         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
119059 119309       }else if( srcName && p->op==TK_COLUMN ){
119060 119310         char *zCol;
119061 119311         int iCol = p->iColumn;
................................................................................
119122 119372     Hash ht;                    /* Hash table of column names */
119123 119373   
119124 119374     sqlite3HashInit(&ht);
119125 119375     if( pEList ){
119126 119376       nCol = pEList->nExpr;
119127 119377       aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
119128 119378       testcase( aCol==0 );
       119379  +    if( nCol>32767 ) nCol = 32767;
119129 119380     }else{
119130 119381       nCol = 0;
119131 119382       aCol = 0;
119132 119383     }
119133 119384     assert( nCol==(i16)nCol );
119134 119385     *pnCol = nCol;
119135 119386     *paCol = aCol;
................................................................................
119141 119392         /* If the column contains an "AS <name>" phrase, use <name> as the name */
119142 119393       }else{
119143 119394         Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
119144 119395         while( pColExpr->op==TK_DOT ){
119145 119396           pColExpr = pColExpr->pRight;
119146 119397           assert( pColExpr!=0 );
119147 119398         }
119148         -      if( pColExpr->op==TK_COLUMN && pColExpr->pTab!=0 ){
       119399  +      if( (pColExpr->op==TK_COLUMN || pColExpr->op==TK_AGG_COLUMN)
       119400  +       && pColExpr->pTab!=0 
       119401  +      ){
119149 119402           /* For columns use the column name name */
119150 119403           int iCol = pColExpr->iColumn;
119151 119404           Table *pTab = pColExpr->pTab;
119152 119405           if( iCol<0 ) iCol = pTab->iPKey;
119153 119406           zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
119154 119407         }else if( pColExpr->op==TK_ID ){
119155 119408           assert( !ExprHasProperty(pColExpr, EP_IntValue) );
................................................................................
119216 119469     sqlite3 *db = pParse->db;
119217 119470     NameContext sNC;
119218 119471     Column *pCol;
119219 119472     CollSeq *pColl;
119220 119473     int i;
119221 119474     Expr *p;
119222 119475     struct ExprList_item *a;
119223         -  u64 szAll = 0;
119224 119476   
119225 119477     assert( pSelect!=0 );
119226 119478     assert( (pSelect->selFlags & SF_Resolved)!=0 );
119227 119479     assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
119228 119480     if( db->mallocFailed ) return;
119229 119481     memset(&sNC, 0, sizeof(sNC));
119230 119482     sNC.pSrcList = pSelect->pSrc;
119231 119483     a = pSelect->pEList->a;
119232 119484     for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
119233 119485       const char *zType;
119234 119486       int n, m;
119235 119487       p = a[i].pExpr;
119236         -    zType = columnType(&sNC, p, 0, 0, 0, &pCol->szEst);
119237         -    szAll += pCol->szEst;
       119488  +    zType = columnType(&sNC, p, 0, 0, 0);
       119489  +    /* pCol->szEst = ... // Column size est for SELECT tables never used */
119238 119490       pCol->affinity = sqlite3ExprAffinity(p);
119239         -    if( zType && (m = sqlite3Strlen30(zType))>0 ){
       119491  +    if( zType ){
       119492  +      m = sqlite3Strlen30(zType);
119240 119493         n = sqlite3Strlen30(pCol->zName);
119241 119494         pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
119242 119495         if( pCol->zName ){
119243 119496           memcpy(&pCol->zName[n+1], zType, m+1);
119244 119497           pCol->colFlags |= COLFLAG_HASTYPE;
119245 119498         }
119246 119499       }
119247 119500       if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
119248 119501       pColl = sqlite3ExprCollSeq(pParse, p);
119249 119502       if( pColl && pCol->zColl==0 ){
119250 119503         pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
119251 119504       }
119252 119505     }
119253         -  pTab->szTabRow = sqlite3LogEst(szAll*4);
       119506  +  pTab->szTabRow = 1; /* Any non-zero value works */
119254 119507   }
119255 119508   
119256 119509   /*
119257 119510   ** Given a SELECT statement, generate a Table structure that describes
119258 119511   ** the result set of that SELECT.
119259 119512   */
119260 119513   SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
................................................................................
119289 119542     return pTab;
119290 119543   }
119291 119544   
119292 119545   /*
119293 119546   ** Get a VDBE for the given parser context.  Create a new one if necessary.
119294 119547   ** If an error occurs, return NULL and leave a message in pParse.
119295 119548   */
119296         -static SQLITE_NOINLINE Vdbe *allocVdbe(Parse *pParse){
119297         -  Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
119298         -  if( v ) sqlite3VdbeAddOp2(v, OP_Init, 0, 1);
       119549  +SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
       119550  +  if( pParse->pVdbe ){
       119551  +    return pParse->pVdbe;
       119552  +  }
119299 119553     if( pParse->pToplevel==0
119300 119554      && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
119301 119555     ){
119302 119556       pParse->okConstFactor = 1;
119303 119557     }
119304         -  return v;
119305         -}
119306         -SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
119307         -  Vdbe *v = pParse->pVdbe;
119308         -  return v ? v : allocVdbe(pParse);
       119558  +  return sqlite3VdbeCreate(pParse);
119309 119559   }
119310 119560   
119311 119561   
119312 119562   /*
119313 119563   ** Compute the iLimit and iOffset fields of the SELECT based on the
119314 119564   ** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
119315 119565   ** that appear in the original SQL statement after the LIMIT and OFFSET
................................................................................
119574 119824       sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
119575 119825     }
119576 119826     sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
119577 119827   
119578 119828     /* Output the single row in Current */
119579 119829     addrCont = sqlite3VdbeMakeLabel(v);
119580 119830     codeOffset(v, regOffset, addrCont);
119581         -  selectInnerLoop(pParse, p, p->pEList, iCurrent,
       119831  +  selectInnerLoop(pParse, p, iCurrent,
119582 119832         0, 0, pDest, addrCont, addrBreak);
119583 119833     if( regLimit ){
119584 119834       sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
119585 119835       VdbeCoverage(v);
119586 119836     }
119587 119837     sqlite3VdbeResolveLabel(v, addrCont);
119588 119838   
................................................................................
119712 119962     ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
119713 119963     */
119714 119964     assert( p && p->pPrior );  /* Calling function guarantees this much */
119715 119965     assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
119716 119966     db = pParse->db;
119717 119967     pPrior = p->pPrior;
119718 119968     dest = *pDest;
119719         -  if( pPrior->pOrderBy ){
119720         -    sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
119721         -      selectOpName(p->op));
119722         -    rc = 1;
119723         -    goto multi_select_end;
119724         -  }
119725         -  if( pPrior->pLimit ){
119726         -    sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
119727         -      selectOpName(p->op));
       119969  +  if( pPrior->pOrderBy || pPrior->pLimit ){
       119970  +    sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
       119971  +      pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
119728 119972       rc = 1;
119729 119973       goto multi_select_end;
119730 119974     }
119731 119975   
119732 119976     v = sqlite3GetVdbe(pParse);
119733 119977     assert( v!=0 );  /* The VDBE already created by calling function */
119734 119978   
................................................................................
119893 120137           int iCont, iBreak, iStart;
119894 120138           assert( p->pEList );
119895 120139           iBreak = sqlite3VdbeMakeLabel(v);
119896 120140           iCont = sqlite3VdbeMakeLabel(v);
119897 120141           computeLimitRegisters(pParse, p, iBreak);
119898 120142           sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
119899 120143           iStart = sqlite3VdbeCurrentAddr(v);
119900         -        selectInnerLoop(pParse, p, p->pEList, unionTab,
       120144  +        selectInnerLoop(pParse, p, unionTab,
119901 120145                           0, 0, &dest, iCont, iBreak);
119902 120146           sqlite3VdbeResolveLabel(v, iCont);
119903 120147           sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
119904 120148           sqlite3VdbeResolveLabel(v, iBreak);
119905 120149           sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
119906 120150         }
119907 120151         break;
................................................................................
119966 120210         iCont = sqlite3VdbeMakeLabel(v);
119967 120211         computeLimitRegisters(pParse, p, iBreak);
119968 120212         sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
119969 120213         r1 = sqlite3GetTempReg(pParse);
119970 120214         iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
119971 120215         sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
119972 120216         sqlite3ReleaseTempReg(pParse, r1);
119973         -      selectInnerLoop(pParse, p, p->pEList, tab1,
       120217  +      selectInnerLoop(pParse, p, tab1,
119974 120218                         0, 0, &dest, iCont, iBreak);
119975 120219         sqlite3VdbeResolveLabel(v, iCont);
119976 120220         sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
119977 120221         sqlite3VdbeResolveLabel(v, iBreak);
119978 120222         sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
119979 120223         sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
119980 120224         break;
................................................................................
120618 120862   ** of the subquery rather the result set of the subquery.
120619 120863   */
120620 120864   static Expr *substExpr(
120621 120865     SubstContext *pSubst,  /* Description of the substitution */
120622 120866     Expr *pExpr            /* Expr in which substitution occurs */
120623 120867   ){
120624 120868     if( pExpr==0 ) return 0;
120625         -  if( ExprHasProperty(pExpr, EP_FromJoin) && pExpr->iRightJoinTable==pSubst->iTable ){
       120869  +  if( ExprHasProperty(pExpr, EP_FromJoin)
       120870  +   && pExpr->iRightJoinTable==pSubst->iTable
       120871  +  ){
120626 120872       pExpr->iRightJoinTable = pSubst->iNewTable;
120627 120873     }
120628 120874     if( pExpr->op==TK_COLUMN && pExpr->iTable==pSubst->iTable ){
120629 120875       if( pExpr->iColumn<0 ){
120630 120876         pExpr->op = TK_NULL;
120631 120877       }else{
120632 120878         Expr *pNew;
................................................................................
120731 120977   **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
120732 120978   **
120733 120979   ** The code generated for this simplification gives the same result
120734 120980   ** but only has to scan the data once.  And because indices might 
120735 120981   ** exist on the table t1, a complete scan of the data might be
120736 120982   ** avoided.
120737 120983   **
120738         -** Flattening is only attempted if all of the following are true:
       120984  +** Flattening is subject to the following constraints:
120739 120985   **
120740         -**   (1)  The subquery and the outer query do not both use aggregates.
       120986  +**  (**)  We no longer attempt to flatten aggregate subqueries. Was:
       120987  +**        The subquery and the outer query cannot both be aggregates.
120741 120988   **
120742         -**   (2)  The subquery is not an aggregate or (2a) the outer query is not a join
120743         -**        and (2b) the outer query does not use subqueries other than the one
120744         -**        FROM-clause subquery that is a candidate for flattening.  (2b is
120745         -**        due to ticket [2f7170d73bf9abf80] from 2015-02-09.)
       120989  +**  (**)  We no longer attempt to flatten aggregate subqueries. Was:
       120990  +**        (2) If the subquery is an aggregate then
       120991  +**        (2a) the outer query must not be a join and
       120992  +**        (2b) the outer query must not use subqueries
       120993  +**             other than the one FROM-clause subquery that is a candidate
       120994  +**             for flattening.  (This is due to ticket [2f7170d73bf9abf80]
       120995  +**             from 2015-02-09.)
120746 120996   **
120747         -**   (3)  The subquery is not the right operand of a LEFT JOIN
120748         -**        or (a) the subquery is not itself a join and (b) the FROM clause
120749         -**        of the subquery does not contain a virtual table and (c) the 
120750         -**        outer query is not an aggregate.
       120997  +**   (3)  If the subquery is the right operand of a LEFT JOIN then
       120998  +**        (3a) the subquery may not be a join and
       120999  +**        (3b) the FROM clause of the subquery may not contain a virtual
       121000  +**             table and
       121001  +**        (3c) the outer query may not be an aggregate.
120751 121002   **
120752         -**   (4)  The subquery is not DISTINCT.
       121003  +**   (4)  The subquery can not be DISTINCT.
120753 121004   **
120754 121005   **  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
120755 121006   **        sub-queries that were excluded from this optimization. Restriction 
120756 121007   **        (4) has since been expanded to exclude all DISTINCT subqueries.
120757 121008   **
120758         -**   (6)  The subquery does not use aggregates or the outer query is not
120759         -**        DISTINCT.
       121009  +**  (**)  We no longer attempt to flatten aggregate subqueries.  Was:
       121010  +**        If the subquery is aggregate, the outer query may not be DISTINCT.
120760 121011   **
120761         -**   (7)  The subquery has a FROM clause.  TODO:  For subqueries without
       121012  +**   (7)  The subquery must have a FROM clause.  TODO:  For subqueries without
120762 121013   **        A FROM clause, consider adding a FROM clause with the special
120763 121014   **        table sqlite_once that consists of a single row containing a
120764 121015   **        single NULL.
120765 121016   **
120766         -**   (8)  The subquery does not use LIMIT or the outer query is not a join.
       121017  +**   (8)  If the subquery uses LIMIT then the outer query may not be a join.
120767 121018   **
120768         -**   (9)  The subquery does not use LIMIT or the outer query does not use
120769         -**        aggregates.
       121019  +**   (9)  If the subquery uses LIMIT then the outer query may not be aggregate.
120770 121020   **
120771 121021   **  (**)  Restriction (10) was removed from the code on 2005-02-05 but we
120772 121022   **        accidently carried the comment forward until 2014-09-15.  Original
120773         -**        text: "The subquery does not use aggregates or the outer query 
120774         -**        does not use LIMIT."
       121023  +**        constraint: "If the subquery is aggregate then the outer query 
       121024  +**        may not use LIMIT."
120775 121025   **
120776         -**  (11)  The subquery and the outer query do not both have ORDER BY clauses.
       121026  +**  (11)  The subquery and the outer query may not both have ORDER BY clauses.
120777 121027   **
120778 121028   **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
120779 121029   **        a separate restriction deriving from ticket #350.
120780 121030   **
120781         -**  (13)  The subquery and outer query do not both use LIMIT.
       121031  +**  (13)  The subquery and outer query may not both use LIMIT.
120782 121032   **
120783         -**  (14)  The subquery does not use OFFSET.
       121033  +**  (14)  The subquery may not use OFFSET.
120784 121034   **
120785         -**  (15)  The outer query is not part of a compound select or the
120786         -**        subquery does not have a LIMIT clause.
       121035  +**  (15)  If the outer query is part of a compound select, then the
       121036  +**        subquery may not use LIMIT.
120787 121037   **        (See ticket #2339 and ticket [02a8e81d44]).
120788 121038   **
120789         -**  (16)