src/sqlite3.c
changeset 26 843e629627bf
parent 0 16f27efbef98
     1.1 --- a/src/sqlite3.c	Mon Sep 01 17:50:28 2014 +0200
     1.2 +++ b/src/sqlite3.c	Mon Sep 01 18:36:42 2014 +0200
     1.3 @@ -1,6 +1,6 @@
     1.4  /******************************************************************************
     1.5  ** This file is an amalgamation of many separate C source files from SQLite
     1.6 -** version 3.8.2.  By combining all the individual C code files into this 
     1.7 +** version 3.8.6.  By combining all the individual C code files into this 
     1.8  ** single large file, the entire code can be compiled as a single translation
     1.9  ** unit.  This allows many compilers to do optimizations that would not be
    1.10  ** possible if the files were compiled separately.  Performance improvements
    1.11 @@ -25,6 +25,93 @@
    1.12  #ifndef SQLITE_API
    1.13  # define SQLITE_API
    1.14  #endif
    1.15 +/************** Begin file sqliteInt.h ***************************************/
    1.16 +/*
    1.17 +** 2001 September 15
    1.18 +**
    1.19 +** The author disclaims copyright to this source code.  In place of
    1.20 +** a legal notice, here is a blessing:
    1.21 +**
    1.22 +**    May you do good and not evil.
    1.23 +**    May you find forgiveness for yourself and forgive others.
    1.24 +**    May you share freely, never taking more than you give.
    1.25 +**
    1.26 +*************************************************************************
    1.27 +** Internal interface definitions for SQLite.
    1.28 +**
    1.29 +*/
    1.30 +#ifndef _SQLITEINT_H_
    1.31 +#define _SQLITEINT_H_
    1.32 +
    1.33 +/*
    1.34 +** These #defines should enable >2GB file support on POSIX if the
    1.35 +** underlying operating system supports it.  If the OS lacks
    1.36 +** large file support, or if the OS is windows, these should be no-ops.
    1.37 +**
    1.38 +** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
    1.39 +** system #includes.  Hence, this block of code must be the very first
    1.40 +** code in all source files.
    1.41 +**
    1.42 +** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
    1.43 +** on the compiler command line.  This is necessary if you are compiling
    1.44 +** on a recent machine (ex: Red Hat 7.2) but you want your code to work
    1.45 +** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
    1.46 +** without this option, LFS is enable.  But LFS does not exist in the kernel
    1.47 +** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
    1.48 +** portability you should omit LFS.
    1.49 +**
    1.50 +** The previous paragraph was written in 2005.  (This paragraph is written
    1.51 +** on 2008-11-28.) These days, all Linux kernels support large files, so
    1.52 +** you should probably leave LFS enabled.  But some embedded platforms might
    1.53 +** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
    1.54 +**
    1.55 +** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
    1.56 +*/
    1.57 +#ifndef SQLITE_DISABLE_LFS
    1.58 +# define _LARGE_FILE       1
    1.59 +# ifndef _FILE_OFFSET_BITS
    1.60 +#   define _FILE_OFFSET_BITS 64
    1.61 +# endif
    1.62 +# define _LARGEFILE_SOURCE 1
    1.63 +#endif
    1.64 +
    1.65 +/*
    1.66 +** For MinGW, check to see if we can include the header file containing its
    1.67 +** version information, among other things.  Normally, this internal MinGW
    1.68 +** header file would [only] be included automatically by other MinGW header
    1.69 +** files; however, the contained version information is now required by this
    1.70 +** header file to work around binary compatibility issues (see below) and
    1.71 +** this is the only known way to reliably obtain it.  This entire #if block
    1.72 +** would be completely unnecessary if there was any other way of detecting
    1.73 +** MinGW via their preprocessor (e.g. if they customized their GCC to define
    1.74 +** some MinGW-specific macros).  When compiling for MinGW, either the
    1.75 +** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
    1.76 +** defined; otherwise, detection of conditions specific to MinGW will be
    1.77 +** disabled.
    1.78 +*/
    1.79 +#if defined(_HAVE_MINGW_H)
    1.80 +# include "mingw.h"
    1.81 +#elif defined(_HAVE__MINGW_H)
    1.82 +# include "_mingw.h"
    1.83 +#endif
    1.84 +
    1.85 +/*
    1.86 +** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
    1.87 +** define is required to maintain binary compatibility with the MSVC runtime
    1.88 +** library in use (e.g. for Windows XP).
    1.89 +*/
    1.90 +#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
    1.91 +    defined(_WIN32) && !defined(_WIN64) && \
    1.92 +    defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
    1.93 +    defined(__MSVCRT__)
    1.94 +# define _USE_32BIT_TIME_T
    1.95 +#endif
    1.96 +
    1.97 +/* The public SQLite interface.  The _FILE_OFFSET_BITS macro must appear
    1.98 +** first in QNX.  Also, the _USE_32BIT_TIME_T macro must appear first for
    1.99 +** MinGW.
   1.100 +*/
   1.101 +/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
   1.102  /************** Begin file sqlite3.h *****************************************/
   1.103  /*
   1.104  ** 2001 September 15
   1.105 @@ -135,9 +222,9 @@
   1.106  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   1.107  ** [sqlite_version()] and [sqlite_source_id()].
   1.108  */
   1.109 -#define SQLITE_VERSION        "3.8.2"
   1.110 -#define SQLITE_VERSION_NUMBER 3008002
   1.111 -#define SQLITE_SOURCE_ID      "2013-12-06 14:53:30 27392118af4c38c5203a04b8013e1afdb1cebd0d"
   1.112 +#define SQLITE_VERSION        "3.8.6"
   1.113 +#define SQLITE_VERSION_NUMBER 3008006
   1.114 +#define SQLITE_SOURCE_ID      "2014-08-15 11:46:33 9491ba7d738528f168657adb43a198238abde19e"
   1.115  
   1.116  /*
   1.117  ** CAPI3REF: Run-Time Library Version Numbers
   1.118 @@ -297,7 +384,7 @@
   1.119  **
   1.120  ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
   1.121  ** for the [sqlite3] object.
   1.122 -** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
   1.123 +** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
   1.124  ** the [sqlite3] object is successfully destroyed and all associated
   1.125  ** resources are deallocated.
   1.126  **
   1.127 @@ -305,7 +392,7 @@
   1.128  ** statements or unfinished sqlite3_backup objects then sqlite3_close()
   1.129  ** will leave the database connection open and return [SQLITE_BUSY].
   1.130  ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
   1.131 -** and unfinished sqlite3_backups, then the database connection becomes
   1.132 +** and/or unfinished sqlite3_backups, then the database connection becomes
   1.133  ** an unusable "zombie" which will automatically be deallocated when the
   1.134  ** last prepared statement is finalized or the last sqlite3_backup is
   1.135  ** finished.  The sqlite3_close_v2() interface is intended for use with
   1.136 @@ -318,7 +405,7 @@
   1.137  ** with the [sqlite3] object prior to attempting to close the object.  ^If
   1.138  ** sqlite3_close_v2() is called on a [database connection] that still has
   1.139  ** outstanding [prepared statements], [BLOB handles], and/or
   1.140 -** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
   1.141 +** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
   1.142  ** of resources is deferred until all [prepared statements], [BLOB handles],
   1.143  ** and [sqlite3_backup] objects are also destroyed.
   1.144  **
   1.145 @@ -414,16 +501,14 @@
   1.146  
   1.147  /*
   1.148  ** CAPI3REF: Result Codes
   1.149 -** KEYWORDS: SQLITE_OK {error code} {error codes}
   1.150 -** KEYWORDS: {result code} {result codes}
   1.151 +** KEYWORDS: {result code definitions}
   1.152  **
   1.153  ** Many SQLite functions return an integer result code from the set shown
   1.154  ** here in order to indicate success or failure.
   1.155  **
   1.156  ** New error codes may be added in future versions of SQLite.
   1.157  **
   1.158 -** See also: [SQLITE_IOERR_READ | extended result codes],
   1.159 -** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
   1.160 +** See also: [extended result code definitions]
   1.161  */
   1.162  #define SQLITE_OK           0   /* Successful result */
   1.163  /* beginning-of-error-codes */
   1.164 @@ -461,26 +546,19 @@
   1.165  
   1.166  /*
   1.167  ** CAPI3REF: Extended Result Codes
   1.168 -** KEYWORDS: {extended error code} {extended error codes}
   1.169 -** KEYWORDS: {extended result code} {extended result codes}
   1.170 -**
   1.171 -** In its default configuration, SQLite API routines return one of 26 integer
   1.172 -** [SQLITE_OK | result codes].  However, experience has shown that many of
   1.173 +** KEYWORDS: {extended result code definitions}
   1.174 +**
   1.175 +** In its default configuration, SQLite API routines return one of 30 integer
   1.176 +** [result codes].  However, experience has shown that many of
   1.177  ** these result codes are too coarse-grained.  They do not provide as
   1.178  ** much information about problems as programmers might like.  In an effort to
   1.179  ** address this, newer versions of SQLite (version 3.3.8 and later) include
   1.180  ** support for additional result codes that provide more detailed information
   1.181 -** about errors. The extended result codes are enabled or disabled
   1.182 +** about errors. These [extended result codes] are enabled or disabled
   1.183  ** on a per database connection basis using the
   1.184 -** [sqlite3_extended_result_codes()] API.
   1.185 -**
   1.186 -** Some of the available extended result codes are listed here.
   1.187 -** One may expect the number of extended result codes will increase
   1.188 -** over time.  Software that uses extended result codes should expect
   1.189 -** to see new result codes in future releases of SQLite.
   1.190 -**
   1.191 -** The SQLITE_OK result code will never be extended.  It will always
   1.192 -** be exactly zero.
   1.193 +** [sqlite3_extended_result_codes()] API.  Or, the extended code for
   1.194 +** the most recent error can be obtained using
   1.195 +** [sqlite3_extended_errcode()].
   1.196  */
   1.197  #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
   1.198  #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
   1.199 @@ -519,6 +597,7 @@
   1.200  #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
   1.201  #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
   1.202  #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
   1.203 +#define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
   1.204  #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
   1.205  #define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
   1.206  #define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
   1.207 @@ -586,7 +665,11 @@
   1.208  ** after reboot following a crash or power loss, the only bytes in a
   1.209  ** file that were written at the application level might have changed
   1.210  ** and that adjacent bytes, even bytes within the same sector are
   1.211 -** guaranteed to be unchanged.
   1.212 +** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
   1.213 +** flag indicate that a file cannot be deleted when open.  The
   1.214 +** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
   1.215 +** read-only media and cannot be changed even by processes with
   1.216 +** elevated privileges.
   1.217  */
   1.218  #define SQLITE_IOCAP_ATOMIC                 0x00000001
   1.219  #define SQLITE_IOCAP_ATOMIC512              0x00000002
   1.220 @@ -601,6 +684,7 @@
   1.221  #define SQLITE_IOCAP_SEQUENTIAL             0x00000400
   1.222  #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
   1.223  #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
   1.224 +#define SQLITE_IOCAP_IMMUTABLE              0x00002000
   1.225  
   1.226  /*
   1.227  ** CAPI3REF: File Locking Levels
   1.228 @@ -707,7 +791,7 @@
   1.229  ** locking strategy (for example to use dot-file locks), to inquire
   1.230  ** about the status of a lock, or to break stale locks.  The SQLite
   1.231  ** core reserves all opcodes less than 100 for its own use.
   1.232 -** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
   1.233 +** A [file control opcodes | list of opcodes] less than 100 is available.
   1.234  ** Applications that define a custom xFileControl method should use opcodes
   1.235  ** greater than 100 to avoid conflicts.  VFS implementations should
   1.236  ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
   1.237 @@ -780,6 +864,7 @@
   1.238  
   1.239  /*
   1.240  ** CAPI3REF: Standard File Control Opcodes
   1.241 +** KEYWORDS: {file control opcodes} {file control opcode}
   1.242  **
   1.243  ** These integer constants are opcodes for the xFileControl method
   1.244  ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
   1.245 @@ -817,15 +902,29 @@
   1.246  ** additional information.
   1.247  **
   1.248  ** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
   1.249 -** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
   1.250 -** SQLite and sent to all VFSes in place of a call to the xSync method
   1.251 -** when the database connection has [PRAGMA synchronous] set to OFF.)^
   1.252 -** Some specialized VFSes need this signal in order to operate correctly
   1.253 -** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most 
   1.254 -** VFSes do not need this signal and should silently ignore this opcode.
   1.255 -** Applications should not call [sqlite3_file_control()] with this
   1.256 -** opcode as doing so may disrupt the operation of the specialized VFSes
   1.257 -** that do require it.  
   1.258 +** No longer in use.
   1.259 +**
   1.260 +** <li>[[SQLITE_FCNTL_SYNC]]
   1.261 +** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
   1.262 +** sent to the VFS immediately before the xSync method is invoked on a
   1.263 +** database file descriptor. Or, if the xSync method is not invoked 
   1.264 +** because the user has configured SQLite with 
   1.265 +** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place 
   1.266 +** of the xSync method. In most cases, the pointer argument passed with
   1.267 +** this file-control is NULL. However, if the database file is being synced
   1.268 +** as part of a multi-database commit, the argument points to a nul-terminated
   1.269 +** string containing the transactions master-journal file name. VFSes that 
   1.270 +** do not need this signal should silently ignore this opcode. Applications 
   1.271 +** should not call [sqlite3_file_control()] with this opcode as doing so may 
   1.272 +** disrupt the operation of the specialized VFSes that do require it.  
   1.273 +**
   1.274 +** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
   1.275 +** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
   1.276 +** and sent to the VFS after a transaction has been committed immediately
   1.277 +** but before the database is unlocked. VFSes that do not need this signal
   1.278 +** should silently ignore this opcode. Applications should not call
   1.279 +** [sqlite3_file_control()] with this opcode as doing so may disrupt the 
   1.280 +** operation of the specialized VFSes that do require it.  
   1.281  **
   1.282  ** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
   1.283  ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
   1.284 @@ -949,6 +1048,18 @@
   1.285  ** SQLite stack may generate instances of this file control if
   1.286  ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
   1.287  **
   1.288 +** <li>[[SQLITE_FCNTL_HAS_MOVED]]
   1.289 +** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
   1.290 +** pointer to an integer and it writes a boolean into that integer depending
   1.291 +** on whether or not the file has been renamed, moved, or deleted since it
   1.292 +** was first opened.
   1.293 +**
   1.294 +** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
   1.295 +** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging.  This
   1.296 +** opcode causes the xFileControl method to swap the file handle with the one
   1.297 +** pointed to by the pArg argument.  This capability is used during testing
   1.298 +** and only needs to be supported when SQLITE_TEST is defined.
   1.299 +**
   1.300  ** </ul>
   1.301  */
   1.302  #define SQLITE_FCNTL_LOCKSTATE               1
   1.303 @@ -969,6 +1080,10 @@
   1.304  #define SQLITE_FCNTL_TEMPFILENAME           16
   1.305  #define SQLITE_FCNTL_MMAP_SIZE              18
   1.306  #define SQLITE_FCNTL_TRACE                  19
   1.307 +#define SQLITE_FCNTL_HAS_MOVED              20
   1.308 +#define SQLITE_FCNTL_SYNC                   21
   1.309 +#define SQLITE_FCNTL_COMMIT_PHASETWO        22
   1.310 +#define SQLITE_FCNTL_WIN32_SET_HANDLE       23
   1.311  
   1.312  /*
   1.313  ** CAPI3REF: Mutex Handle
   1.314 @@ -2029,27 +2144,33 @@
   1.315  /*
   1.316  ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
   1.317  **
   1.318 -** ^This routine sets a callback function that might be invoked whenever
   1.319 -** an attempt is made to open a database table that another thread
   1.320 -** or process has locked.
   1.321 -**
   1.322 -** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
   1.323 +** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
   1.324 +** that might be invoked with argument P whenever
   1.325 +** an attempt is made to access a database table associated with
   1.326 +** [database connection] D when another thread
   1.327 +** or process has the table locked.
   1.328 +** The sqlite3_busy_handler() interface is used to implement
   1.329 +** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
   1.330 +**
   1.331 +** ^If the busy callback is NULL, then [SQLITE_BUSY]
   1.332  ** is returned immediately upon encountering the lock.  ^If the busy callback
   1.333  ** is not NULL, then the callback might be invoked with two arguments.
   1.334  **
   1.335  ** ^The first argument to the busy handler is a copy of the void* pointer which
   1.336  ** is the third argument to sqlite3_busy_handler().  ^The second argument to
   1.337  ** the busy handler callback is the number of times that the busy handler has
   1.338 -** been invoked for this locking event.  ^If the
   1.339 +** been invoked for the same locking event.  ^If the
   1.340  ** busy callback returns 0, then no additional attempts are made to
   1.341 -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
   1.342 +** access the database and [SQLITE_BUSY] is returned
   1.343 +** to the application.
   1.344  ** ^If the callback returns non-zero, then another attempt
   1.345 -** is made to open the database for reading and the cycle repeats.
   1.346 +** is made to access the database and the cycle repeats.
   1.347  **
   1.348  ** The presence of a busy handler does not guarantee that it will be invoked
   1.349  ** when there is lock contention. ^If SQLite determines that invoking the busy
   1.350  ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
   1.351 -** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
   1.352 +** to the application instead of invoking the 
   1.353 +** busy handler.
   1.354  ** Consider a scenario where one process is holding a read lock that
   1.355  ** it is trying to promote to a reserved lock and
   1.356  ** a second process is holding a reserved lock that it is trying
   1.357 @@ -2063,28 +2184,15 @@
   1.358  **
   1.359  ** ^The default busy callback is NULL.
   1.360  **
   1.361 -** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
   1.362 -** when SQLite is in the middle of a large transaction where all the
   1.363 -** changes will not fit into the in-memory cache.  SQLite will
   1.364 -** already hold a RESERVED lock on the database file, but it needs
   1.365 -** to promote this lock to EXCLUSIVE so that it can spill cache
   1.366 -** pages into the database file without harm to concurrent
   1.367 -** readers.  ^If it is unable to promote the lock, then the in-memory
   1.368 -** cache will be left in an inconsistent state and so the error
   1.369 -** code is promoted from the relatively benign [SQLITE_BUSY] to
   1.370 -** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
   1.371 -** forces an automatic rollback of the changes.  See the
   1.372 -** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
   1.373 -** CorruptionFollowingBusyError</a> wiki page for a discussion of why
   1.374 -** this is important.
   1.375 -**
   1.376  ** ^(There can only be a single busy handler defined for each
   1.377  ** [database connection].  Setting a new busy handler clears any
   1.378  ** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
   1.379 -** will also set or clear the busy handler.
   1.380 +** or evaluating [PRAGMA busy_timeout=N] will change the
   1.381 +** busy handler and thus clear any previously set busy handler.
   1.382  **
   1.383  ** The busy callback should not take any actions which modify the
   1.384 -** database connection that invoked the busy handler.  Any such actions
   1.385 +** database connection that invoked the busy handler.  In other words,
   1.386 +** the busy handler is not reentrant.  Any such actions
   1.387  ** result in undefined behavior.
   1.388  ** 
   1.389  ** A busy handler must not close the database connection
   1.390 @@ -2100,7 +2208,7 @@
   1.391  ** will sleep multiple times until at least "ms" milliseconds of sleeping
   1.392  ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
   1.393  ** the handler returns 0 which causes [sqlite3_step()] to return
   1.394 -** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
   1.395 +** [SQLITE_BUSY].
   1.396  **
   1.397  ** ^Calling this routine with an argument less than or equal to zero
   1.398  ** turns off all busy handlers.
   1.399 @@ -2109,6 +2217,8 @@
   1.400  ** [database connection] any any given moment.  If another busy handler
   1.401  ** was defined  (using [sqlite3_busy_handler()]) prior to calling
   1.402  ** this routine, that other busy handler is cleared.)^
   1.403 +**
   1.404 +** See also:  [PRAGMA busy_timeout]
   1.405  */
   1.406  SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
   1.407  
   1.408 @@ -2403,11 +2513,13 @@
   1.409  ** applications to access the same PRNG for other purposes.
   1.410  **
   1.411  ** ^A call to this routine stores N bytes of randomness into buffer P.
   1.412 -**
   1.413 -** ^The first time this routine is invoked (either internally or by
   1.414 -** the application) the PRNG is seeded using randomness obtained
   1.415 -** from the xRandomness method of the default [sqlite3_vfs] object.
   1.416 -** ^On all subsequent invocations, the pseudo-randomness is generated
   1.417 +** ^If N is less than one, then P can be a NULL pointer.
   1.418 +**
   1.419 +** ^If this routine has not been previously called or if the previous
   1.420 +** call had N less than one, then the PRNG is seeded using randomness
   1.421 +** obtained from the xRandomness method of the default [sqlite3_vfs] object.
   1.422 +** ^If the previous call to this routine had an N of 1 or more then
   1.423 +** the pseudo-randomness is generated
   1.424  ** internally and without recourse to the [sqlite3_vfs] xRandomness
   1.425  ** method.
   1.426  */
   1.427 @@ -2508,8 +2620,8 @@
   1.428  ** [sqlite3_set_authorizer | authorizer documentation] for additional
   1.429  ** information.
   1.430  **
   1.431 -** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
   1.432 -** from the [sqlite3_vtab_on_conflict()] interface.
   1.433 +** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
   1.434 +** returned from the [sqlite3_vtab_on_conflict()] interface.
   1.435  */
   1.436  #define SQLITE_DENY   1   /* Abort the SQL statement with an error */
   1.437  #define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
   1.438 @@ -2567,6 +2679,7 @@
   1.439  #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
   1.440  #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
   1.441  #define SQLITE_COPY                  0   /* No longer used */
   1.442 +#define SQLITE_RECURSIVE            33   /* NULL            NULL            */
   1.443  
   1.444  /*
   1.445  ** CAPI3REF: Tracing And Profiling Functions
   1.446 @@ -2779,6 +2892,30 @@
   1.447  **     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
   1.448  **     a URI filename, its value overrides any behavior requested by setting
   1.449  **     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
   1.450 +**
   1.451 +**  <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or
   1.452 +**     "1") or "false" (or "off" or "no" or "0") to indicate that the
   1.453 +**     [powersafe overwrite] property does or does not apply to the
   1.454 +**     storage media on which the database file resides.  ^The psow query
   1.455 +**     parameter only works for the built-in unix and Windows VFSes.
   1.456 +**
   1.457 +**  <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
   1.458 +**     which if set disables file locking in rollback journal modes.  This
   1.459 +**     is useful for accessing a database on a filesystem that does not
   1.460 +**     support locking.  Caution:  Database corruption might result if two
   1.461 +**     or more processes write to the same database and any one of those
   1.462 +**     processes uses nolock=1.
   1.463 +**
   1.464 +**  <li> <b>immutable</b>: ^The immutable parameter is a boolean query
   1.465 +**     parameter that indicates that the database file is stored on
   1.466 +**     read-only media.  ^When immutable is set, SQLite assumes that the
   1.467 +**     database file cannot be changed, even by a process with higher
   1.468 +**     privilege, and so the database is opened read-only and all locking
   1.469 +**     and change detection is disabled.  Caution: Setting the immutable
   1.470 +**     property on a database file that does in fact change can result
   1.471 +**     in incorrect query results and/or [SQLITE_CORRUPT] errors.
   1.472 +**     See also: [SQLITE_IOCAP_IMMUTABLE].
   1.473 +**       
   1.474  ** </ul>
   1.475  **
   1.476  ** ^Specifying an unknown parameter in the query component of a URI is not an
   1.477 @@ -2808,8 +2945,9 @@
   1.478  **          Open file "data.db" in the current directory for read-only access.
   1.479  **          Regardless of whether or not shared-cache mode is enabled by
   1.480  **          default, use a private cache.
   1.481 -** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
   1.482 -**          Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
   1.483 +** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
   1.484 +**          Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
   1.485 +**          that uses dot-files in place of posix advisory locking.
   1.486  ** <tr><td> file:data.db?mode=readonly <td> 
   1.487  **          An error. "readonly" is not a valid option for the "mode" parameter.
   1.488  ** </table>
   1.489 @@ -3985,15 +4123,24 @@
   1.490  **
   1.491  ** ^The fourth parameter, eTextRep, specifies what
   1.492  ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
   1.493 -** its parameters.  Every SQL function implementation must be able to work
   1.494 -** with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
   1.495 -** more efficient with one encoding than another.  ^An application may
   1.496 -** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
   1.497 -** times with the same function but with different values of eTextRep.
   1.498 +** its parameters.  The application should set this parameter to
   1.499 +** [SQLITE_UTF16LE] if the function implementation invokes 
   1.500 +** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
   1.501 +** implementation invokes [sqlite3_value_text16be()] on an input, or
   1.502 +** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
   1.503 +** otherwise.  ^The same SQL function may be registered multiple times using
   1.504 +** different preferred text encodings, with different implementations for
   1.505 +** each encoding.
   1.506  ** ^When multiple implementations of the same function are available, SQLite
   1.507  ** will pick the one that involves the least amount of data conversion.
   1.508 -** If there is only a single implementation which does not care what text
   1.509 -** encoding is used, then the fourth argument should be [SQLITE_ANY].
   1.510 +**
   1.511 +** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
   1.512 +** to signal that the function will always return the same result given
   1.513 +** the same inputs within a single SQL statement.  Most SQL functions are
   1.514 +** deterministic.  The built-in [random()] SQL function is an example of a
   1.515 +** function that is not deterministic.  The SQLite query planner is able to
   1.516 +** perform additional optimizations on deterministic functions, so use
   1.517 +** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
   1.518  **
   1.519  ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
   1.520  ** function can gain access to this pointer using [sqlite3_user_data()].)^
   1.521 @@ -4079,10 +4226,20 @@
   1.522  #define SQLITE_UTF16LE        2
   1.523  #define SQLITE_UTF16BE        3
   1.524  #define SQLITE_UTF16          4    /* Use native byte order */
   1.525 -#define SQLITE_ANY            5    /* sqlite3_create_function only */
   1.526 +#define SQLITE_ANY            5    /* Deprecated */
   1.527  #define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
   1.528  
   1.529  /*
   1.530 +** CAPI3REF: Function Flags
   1.531 +**
   1.532 +** These constants may be ORed together with the 
   1.533 +** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
   1.534 +** to [sqlite3_create_function()], [sqlite3_create_function16()], or
   1.535 +** [sqlite3_create_function_v2()].
   1.536 +*/
   1.537 +#define SQLITE_DETERMINISTIC    0x800
   1.538 +
   1.539 +/*
   1.540  ** CAPI3REF: Deprecated Functions
   1.541  ** DEPRECATED
   1.542  **
   1.543 @@ -4651,6 +4808,13 @@
   1.544  ** is a NULL pointer, then SQLite performs a search for an appropriate
   1.545  ** temporary file directory.
   1.546  **
   1.547 +** Applications are strongly discouraged from using this global variable.
   1.548 +** It is required to set a temporary folder on Windows Runtime (WinRT).
   1.549 +** But for all other platforms, it is highly recommended that applications
   1.550 +** neither read nor write this variable.  This global variable is a relic
   1.551 +** that exists for backwards compatibility of legacy applications and should
   1.552 +** be avoided in new projects.
   1.553 +**
   1.554  ** It is not safe to read or modify this variable in more than one
   1.555  ** thread at a time.  It is not safe to read or modify this variable
   1.556  ** if a [database connection] is being used at the same time in a separate
   1.557 @@ -4669,6 +4833,11 @@
   1.558  ** Hence, if this variable is modified directly, either it should be
   1.559  ** made NULL or made to point to memory obtained from [sqlite3_malloc]
   1.560  ** or else the use of the [temp_store_directory pragma] should be avoided.
   1.561 +** Except when requested by the [temp_store_directory pragma], SQLite
   1.562 +** does not free the memory that sqlite3_temp_directory points to.  If
   1.563 +** the application wants that memory to be freed, it must do
   1.564 +** so itself, taking care to only do so after all [database connection]
   1.565 +** objects have been destroyed.
   1.566  **
   1.567  ** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
   1.568  ** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
   1.569 @@ -5803,10 +5972,12 @@
   1.570  ** <li>  SQLITE_MUTEX_RECURSIVE
   1.571  ** <li>  SQLITE_MUTEX_STATIC_MASTER
   1.572  ** <li>  SQLITE_MUTEX_STATIC_MEM
   1.573 -** <li>  SQLITE_MUTEX_STATIC_MEM2
   1.574 +** <li>  SQLITE_MUTEX_STATIC_OPEN
   1.575  ** <li>  SQLITE_MUTEX_STATIC_PRNG
   1.576  ** <li>  SQLITE_MUTEX_STATIC_LRU
   1.577 -** <li>  SQLITE_MUTEX_STATIC_LRU2
   1.578 +** <li>  SQLITE_MUTEX_STATIC_PMEM
   1.579 +** <li>  SQLITE_MUTEX_STATIC_APP1
   1.580 +** <li>  SQLITE_MUTEX_STATIC_APP2
   1.581  ** </ul>)^
   1.582  **
   1.583  ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
   1.584 @@ -6010,6 +6181,9 @@
   1.585  #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
   1.586  #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
   1.587  #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
   1.588 +#define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
   1.589 +#define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
   1.590 +#define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
   1.591  
   1.592  /*
   1.593  ** CAPI3REF: Retrieve the mutex for a database connection
   1.594 @@ -6103,7 +6277,10 @@
   1.595  #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
   1.596  #define SQLITE_TESTCTRL_EXPLAIN_STMT            19
   1.597  #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
   1.598 -#define SQLITE_TESTCTRL_LAST                    20
   1.599 +#define SQLITE_TESTCTRL_VDBE_COVERAGE           21
   1.600 +#define SQLITE_TESTCTRL_BYTEORDER               22
   1.601 +#define SQLITE_TESTCTRL_ISINIT                  23
   1.602 +#define SQLITE_TESTCTRL_LAST                    23
   1.603  
   1.604  /*
   1.605  ** CAPI3REF: SQLite Runtime Status
   1.606 @@ -7087,6 +7264,9 @@
   1.607  ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
   1.608  ** from SQL.
   1.609  **
   1.610 +** ^Checkpoints initiated by this mechanism are
   1.611 +** [sqlite3_wal_checkpoint_v2|PASSIVE].
   1.612 +**
   1.613  ** ^Every new [database connection] defaults to having the auto-checkpoint
   1.614  ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
   1.615  ** pages.  The use of this interface
   1.616 @@ -7103,6 +7283,10 @@
   1.617  ** empty string, then a checkpoint is run on all databases of
   1.618  ** connection D.  ^If the database connection D is not in
   1.619  ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
   1.620 +** ^The [sqlite3_wal_checkpoint(D,X)] interface initiates a
   1.621 +** [sqlite3_wal_checkpoint_v2|PASSIVE] checkpoint.
   1.622 +** Use the [sqlite3_wal_checkpoint_v2()] interface to get a FULL
   1.623 +** or RESET checkpoint.
   1.624  **
   1.625  ** ^The [wal_checkpoint pragma] can be used to invoke this interface
   1.626  ** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
   1.627 @@ -7125,10 +7309,12 @@
   1.628  **   Checkpoint as many frames as possible without waiting for any database 
   1.629  **   readers or writers to finish. Sync the db file if all frames in the log
   1.630  **   are checkpointed. This mode is the same as calling 
   1.631 -**   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
   1.632 +**   sqlite3_wal_checkpoint(). The [sqlite3_busy_handler|busy-handler callback]
   1.633 +**   is never invoked.
   1.634  **
   1.635  ** <dt>SQLITE_CHECKPOINT_FULL<dd>
   1.636 -**   This mode blocks (calls the busy-handler callback) until there is no
   1.637 +**   This mode blocks (it invokes the
   1.638 +**   [sqlite3_busy_handler|busy-handler callback]) until there is no
   1.639  **   database writer and all readers are reading from the most recent database
   1.640  **   snapshot. It then checkpoints all frames in the log file and syncs the
   1.641  **   database file. This call blocks database writers while it is running,
   1.642 @@ -7136,7 +7322,8 @@
   1.643  **
   1.644  ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
   1.645  **   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after 
   1.646 -**   checkpointing the log file it blocks (calls the busy-handler callback)
   1.647 +**   checkpointing the log file it blocks (calls the 
   1.648 +**   [sqlite3_busy_handler|busy-handler callback])
   1.649  **   until all readers are reading from the database file only. This ensures 
   1.650  **   that the next client to write to the database file restarts the log file 
   1.651  **   from the beginning. This call blocks database writers while it is running,
   1.652 @@ -7274,6 +7461,7 @@
   1.653  
   1.654  /*
   1.655  ** CAPI3REF: Conflict resolution modes
   1.656 +** KEYWORDS: {conflict resolution mode}
   1.657  **
   1.658  ** These constants are returned by [sqlite3_vtab_on_conflict()] to
   1.659  ** inform a [virtual table] implementation what the [ON CONFLICT] mode
   1.660 @@ -7326,6 +7514,16 @@
   1.661  #endif
   1.662  
   1.663  typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
   1.664 +typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
   1.665 +
   1.666 +/* The double-precision datatype used by RTree depends on the
   1.667 +** SQLITE_RTREE_INT_ONLY compile-time option.
   1.668 +*/
   1.669 +#ifdef SQLITE_RTREE_INT_ONLY
   1.670 +  typedef sqlite3_int64 sqlite3_rtree_dbl;
   1.671 +#else
   1.672 +  typedef double sqlite3_rtree_dbl;
   1.673 +#endif
   1.674  
   1.675  /*
   1.676  ** Register a geometry callback named zGeom that can be used as part of an
   1.677 @@ -7336,11 +7534,7 @@
   1.678  SQLITE_API int sqlite3_rtree_geometry_callback(
   1.679    sqlite3 *db,
   1.680    const char *zGeom,
   1.681 -#ifdef SQLITE_RTREE_INT_ONLY
   1.682 -  int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
   1.683 -#else
   1.684 -  int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
   1.685 -#endif
   1.686 +  int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
   1.687    void *pContext
   1.688  );
   1.689  
   1.690 @@ -7352,11 +7546,60 @@
   1.691  struct sqlite3_rtree_geometry {
   1.692    void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
   1.693    int nParam;                     /* Size of array aParam[] */
   1.694 -  double *aParam;                 /* Parameters passed to SQL geom function */
   1.695 +  sqlite3_rtree_dbl *aParam;      /* Parameters passed to SQL geom function */
   1.696    void *pUser;                    /* Callback implementation user data */
   1.697    void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
   1.698  };
   1.699  
   1.700 +/*
   1.701 +** Register a 2nd-generation geometry callback named zScore that can be 
   1.702 +** used as part of an R-Tree geometry query as follows:
   1.703 +**
   1.704 +**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
   1.705 +*/
   1.706 +SQLITE_API int sqlite3_rtree_query_callback(
   1.707 +  sqlite3 *db,
   1.708 +  const char *zQueryFunc,
   1.709 +  int (*xQueryFunc)(sqlite3_rtree_query_info*),
   1.710 +  void *pContext,
   1.711 +  void (*xDestructor)(void*)
   1.712 +);
   1.713 +
   1.714 +
   1.715 +/*
   1.716 +** A pointer to a structure of the following type is passed as the 
   1.717 +** argument to scored geometry callback registered using
   1.718 +** sqlite3_rtree_query_callback().
   1.719 +**
   1.720 +** Note that the first 5 fields of this structure are identical to
   1.721 +** sqlite3_rtree_geometry.  This structure is a subclass of
   1.722 +** sqlite3_rtree_geometry.
   1.723 +*/
   1.724 +struct sqlite3_rtree_query_info {
   1.725 +  void *pContext;                   /* pContext from when function registered */
   1.726 +  int nParam;                       /* Number of function parameters */
   1.727 +  sqlite3_rtree_dbl *aParam;        /* value of function parameters */
   1.728 +  void *pUser;                      /* callback can use this, if desired */
   1.729 +  void (*xDelUser)(void*);          /* function to free pUser */
   1.730 +  sqlite3_rtree_dbl *aCoord;        /* Coordinates of node or entry to check */
   1.731 +  unsigned int *anQueue;            /* Number of pending entries in the queue */
   1.732 +  int nCoord;                       /* Number of coordinates */
   1.733 +  int iLevel;                       /* Level of current node or entry */
   1.734 +  int mxLevel;                      /* The largest iLevel value in the tree */
   1.735 +  sqlite3_int64 iRowid;             /* Rowid for current entry */
   1.736 +  sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
   1.737 +  int eParentWithin;                /* Visibility of parent node */
   1.738 +  int eWithin;                      /* OUT: Visiblity */
   1.739 +  sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
   1.740 +};
   1.741 +
   1.742 +/*
   1.743 +** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
   1.744 +*/
   1.745 +#define NOT_WITHIN       0   /* Object completely outside of query region */
   1.746 +#define PARTLY_WITHIN    1   /* Object partially overlaps query region */
   1.747 +#define FULLY_WITHIN     2   /* Object fully contained within query region */
   1.748 +
   1.749  
   1.750  #if 0
   1.751  }  /* end of the 'extern "C"' block */
   1.752 @@ -7366,50 +7609,7 @@
   1.753  
   1.754  
   1.755  /************** End of sqlite3.h *********************************************/
   1.756 -/************** Begin file sqliteInt.h ***************************************/
   1.757 -/*
   1.758 -** 2001 September 15
   1.759 -**
   1.760 -** The author disclaims copyright to this source code.  In place of
   1.761 -** a legal notice, here is a blessing:
   1.762 -**
   1.763 -**    May you do good and not evil.
   1.764 -**    May you find forgiveness for yourself and forgive others.
   1.765 -**    May you share freely, never taking more than you give.
   1.766 -**
   1.767 -*************************************************************************
   1.768 -** Internal interface definitions for SQLite.
   1.769 -**
   1.770 -*/
   1.771 -#ifndef _SQLITEINT_H_
   1.772 -#define _SQLITEINT_H_
   1.773 -
   1.774 -/*
   1.775 -** These #defines should enable >2GB file support on POSIX if the
   1.776 -** underlying operating system supports it.  If the OS lacks
   1.777 -** large file support, or if the OS is windows, these should be no-ops.
   1.778 -**
   1.779 -** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
   1.780 -** system #includes.  Hence, this block of code must be the very first
   1.781 -** code in all source files.
   1.782 -**
   1.783 -** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
   1.784 -** on the compiler command line.  This is necessary if you are compiling
   1.785 -** on a recent machine (ex: Red Hat 7.2) but you want your code to work
   1.786 -** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
   1.787 -** without this option, LFS is enable.  But LFS does not exist in the kernel
   1.788 -** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
   1.789 -** portability you should omit LFS.
   1.790 -**
   1.791 -** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
   1.792 -*/
   1.793 -#ifndef SQLITE_DISABLE_LFS
   1.794 -# define _LARGE_FILE       1
   1.795 -# ifndef _FILE_OFFSET_BITS
   1.796 -#   define _FILE_OFFSET_BITS 64
   1.797 -# endif
   1.798 -# define _LARGEFILE_SOURCE 1
   1.799 -#endif
   1.800 +/************** Continuing where we left off in sqliteInt.h ******************/
   1.801  
   1.802  /*
   1.803  ** Include the configuration header output by 'configure' if we're using the
   1.804 @@ -7996,163 +8196,165 @@
   1.805  /************** Continuing where we left off in sqliteInt.h ******************/
   1.806  /************** Include parse.h in the middle of sqliteInt.h *****************/
   1.807  /************** Begin file parse.h *******************************************/
   1.808 -#define TK_SEMI                            1
   1.809 -#define TK_EXPLAIN                         2
   1.810 -#define TK_QUERY                           3
   1.811 -#define TK_PLAN                            4
   1.812 -#define TK_BEGIN                           5
   1.813 -#define TK_TRANSACTION                     6
   1.814 -#define TK_DEFERRED                        7
   1.815 -#define TK_IMMEDIATE                       8
   1.816 -#define TK_EXCLUSIVE                       9
   1.817 -#define TK_COMMIT                         10
   1.818 -#define TK_END                            11
   1.819 -#define TK_ROLLBACK                       12
   1.820 -#define TK_SAVEPOINT                      13
   1.821 -#define TK_RELEASE                        14
   1.822 -#define TK_TO                             15
   1.823 -#define TK_TABLE                          16
   1.824 -#define TK_CREATE                         17
   1.825 -#define TK_IF                             18
   1.826 -#define TK_NOT                            19
   1.827 -#define TK_EXISTS                         20
   1.828 -#define TK_TEMP                           21
   1.829 -#define TK_LP                             22
   1.830 -#define TK_RP                             23
   1.831 -#define TK_AS                             24
   1.832 -#define TK_WITHOUT                        25
   1.833 -#define TK_COMMA                          26
   1.834 -#define TK_ID                             27
   1.835 -#define TK_INDEXED                        28
   1.836 -#define TK_ABORT                          29
   1.837 -#define TK_ACTION                         30
   1.838 -#define TK_AFTER                          31
   1.839 -#define TK_ANALYZE                        32
   1.840 -#define TK_ASC                            33
   1.841 -#define TK_ATTACH                         34
   1.842 -#define TK_BEFORE                         35
   1.843 -#define TK_BY                             36
   1.844 -#define TK_CASCADE                        37
   1.845 -#define TK_CAST                           38
   1.846 -#define TK_COLUMNKW                       39
   1.847 -#define TK_CONFLICT                       40
   1.848 -#define TK_DATABASE                       41
   1.849 -#define TK_DESC                           42
   1.850 -#define TK_DETACH                         43
   1.851 -#define TK_EACH                           44
   1.852 -#define TK_FAIL                           45
   1.853 -#define TK_FOR                            46
   1.854 -#define TK_IGNORE                         47
   1.855 -#define TK_INITIALLY                      48
   1.856 -#define TK_INSTEAD                        49
   1.857 -#define TK_LIKE_KW                        50
   1.858 -#define TK_MATCH                          51
   1.859 -#define TK_NO                             52
   1.860 -#define TK_KEY                            53
   1.861 -#define TK_OF                             54
   1.862 -#define TK_OFFSET                         55
   1.863 -#define TK_PRAGMA                         56
   1.864 -#define TK_RAISE                          57
   1.865 -#define TK_REPLACE                        58
   1.866 -#define TK_RESTRICT                       59
   1.867 -#define TK_ROW                            60
   1.868 -#define TK_TRIGGER                        61
   1.869 -#define TK_VACUUM                         62
   1.870 -#define TK_VIEW                           63
   1.871 -#define TK_VIRTUAL                        64
   1.872 -#define TK_REINDEX                        65
   1.873 -#define TK_RENAME                         66
   1.874 -#define TK_CTIME_KW                       67
   1.875 -#define TK_ANY                            68
   1.876 -#define TK_OR                             69
   1.877 -#define TK_AND                            70
   1.878 -#define TK_IS                             71
   1.879 -#define TK_BETWEEN                        72
   1.880 -#define TK_IN                             73
   1.881 -#define TK_ISNULL                         74
   1.882 -#define TK_NOTNULL                        75
   1.883 -#define TK_NE                             76
   1.884 -#define TK_EQ                             77
   1.885 -#define TK_GT                             78
   1.886 -#define TK_LE                             79
   1.887 -#define TK_LT                             80
   1.888 -#define TK_GE                             81
   1.889 -#define TK_ESCAPE                         82
   1.890 -#define TK_BITAND                         83
   1.891 -#define TK_BITOR                          84
   1.892 -#define TK_LSHIFT                         85
   1.893 -#define TK_RSHIFT                         86
   1.894 -#define TK_PLUS                           87
   1.895 -#define TK_MINUS                          88
   1.896 -#define TK_STAR                           89
   1.897 -#define TK_SLASH                          90
   1.898 -#define TK_REM                            91
   1.899 -#define TK_CONCAT                         92
   1.900 -#define TK_COLLATE                        93
   1.901 -#define TK_BITNOT                         94
   1.902 -#define TK_STRING                         95
   1.903 -#define TK_JOIN_KW                        96
   1.904 -#define TK_CONSTRAINT                     97
   1.905 -#define TK_DEFAULT                        98
   1.906 -#define TK_NULL                           99
   1.907 -#define TK_PRIMARY                        100
   1.908 -#define TK_UNIQUE                         101
   1.909 -#define TK_CHECK                          102
   1.910 -#define TK_REFERENCES                     103
   1.911 -#define TK_AUTOINCR                       104
   1.912 -#define TK_ON                             105
   1.913 -#define TK_INSERT                         106
   1.914 -#define TK_DELETE                         107
   1.915 -#define TK_UPDATE                         108
   1.916 -#define TK_SET                            109
   1.917 -#define TK_DEFERRABLE                     110
   1.918 -#define TK_FOREIGN                        111
   1.919 -#define TK_DROP                           112
   1.920 -#define TK_UNION                          113
   1.921 -#define TK_ALL                            114
   1.922 -#define TK_EXCEPT                         115
   1.923 -#define TK_INTERSECT                      116
   1.924 -#define TK_SELECT                         117
   1.925 -#define TK_DISTINCT                       118
   1.926 -#define TK_DOT                            119
   1.927 -#define TK_FROM                           120
   1.928 -#define TK_JOIN                           121
   1.929 -#define TK_USING                          122
   1.930 -#define TK_ORDER                          123
   1.931 -#define TK_GROUP                          124
   1.932 -#define TK_HAVING                         125
   1.933 -#define TK_LIMIT                          126
   1.934 -#define TK_WHERE                          127
   1.935 -#define TK_INTO                           128
   1.936 -#define TK_VALUES                         129
   1.937 -#define TK_INTEGER                        130
   1.938 -#define TK_FLOAT                          131
   1.939 -#define TK_BLOB                           132
   1.940 -#define TK_REGISTER                       133
   1.941 -#define TK_VARIABLE                       134
   1.942 -#define TK_CASE                           135
   1.943 -#define TK_WHEN                           136
   1.944 -#define TK_THEN                           137
   1.945 -#define TK_ELSE                           138
   1.946 -#define TK_INDEX                          139
   1.947 -#define TK_ALTER                          140
   1.948 -#define TK_ADD                            141
   1.949 -#define TK_TO_TEXT                        142
   1.950 -#define TK_TO_BLOB                        143
   1.951 -#define TK_TO_NUMERIC                     144
   1.952 -#define TK_TO_INT                         145
   1.953 -#define TK_TO_REAL                        146
   1.954 -#define TK_ISNOT                          147
   1.955 -#define TK_END_OF_FILE                    148
   1.956 -#define TK_ILLEGAL                        149
   1.957 -#define TK_SPACE                          150
   1.958 -#define TK_UNCLOSED_STRING                151
   1.959 -#define TK_FUNCTION                       152
   1.960 -#define TK_COLUMN                         153
   1.961 -#define TK_AGG_FUNCTION                   154
   1.962 -#define TK_AGG_COLUMN                     155
   1.963 -#define TK_UMINUS                         156
   1.964 -#define TK_UPLUS                          157
   1.965 +#define TK_SEMI                             1
   1.966 +#define TK_EXPLAIN                          2
   1.967 +#define TK_QUERY                            3
   1.968 +#define TK_PLAN                             4
   1.969 +#define TK_BEGIN                            5
   1.970 +#define TK_TRANSACTION                      6
   1.971 +#define TK_DEFERRED                         7
   1.972 +#define TK_IMMEDIATE                        8
   1.973 +#define TK_EXCLUSIVE                        9
   1.974 +#define TK_COMMIT                          10
   1.975 +#define TK_END                             11
   1.976 +#define TK_ROLLBACK                        12
   1.977 +#define TK_SAVEPOINT                       13
   1.978 +#define TK_RELEASE                         14
   1.979 +#define TK_TO                              15
   1.980 +#define TK_TABLE                           16
   1.981 +#define TK_CREATE                          17
   1.982 +#define TK_IF                              18
   1.983 +#define TK_NOT                             19
   1.984 +#define TK_EXISTS                          20
   1.985 +#define TK_TEMP                            21
   1.986 +#define TK_LP                              22
   1.987 +#define TK_RP                              23
   1.988 +#define TK_AS                              24
   1.989 +#define TK_WITHOUT                         25
   1.990 +#define TK_COMMA                           26
   1.991 +#define TK_ID                              27
   1.992 +#define TK_INDEXED                         28
   1.993 +#define TK_ABORT                           29
   1.994 +#define TK_ACTION                          30
   1.995 +#define TK_AFTER                           31
   1.996 +#define TK_ANALYZE                         32
   1.997 +#define TK_ASC                             33
   1.998 +#define TK_ATTACH                          34
   1.999 +#define TK_BEFORE                          35
  1.1000 +#define TK_BY                              36
  1.1001 +#define TK_CASCADE                         37
  1.1002 +#define TK_CAST                            38
  1.1003 +#define TK_COLUMNKW                        39
  1.1004 +#define TK_CONFLICT                        40
  1.1005 +#define TK_DATABASE                        41
  1.1006 +#define TK_DESC                            42
  1.1007 +#define TK_DETACH                          43
  1.1008 +#define TK_EACH                            44
  1.1009 +#define TK_FAIL                            45
  1.1010 +#define TK_FOR                             46
  1.1011 +#define TK_IGNORE                          47
  1.1012 +#define TK_INITIALLY                       48
  1.1013 +#define TK_INSTEAD                         49
  1.1014 +#define TK_LIKE_KW                         50
  1.1015 +#define TK_MATCH                           51
  1.1016 +#define TK_NO                              52
  1.1017 +#define TK_KEY                             53
  1.1018 +#define TK_OF                              54
  1.1019 +#define TK_OFFSET                          55
  1.1020 +#define TK_PRAGMA                          56
  1.1021 +#define TK_RAISE                           57
  1.1022 +#define TK_RECURSIVE                       58
  1.1023 +#define TK_REPLACE                         59
  1.1024 +#define TK_RESTRICT                        60
  1.1025 +#define TK_ROW                             61
  1.1026 +#define TK_TRIGGER                         62
  1.1027 +#define TK_VACUUM                          63
  1.1028 +#define TK_VIEW                            64
  1.1029 +#define TK_VIRTUAL                         65
  1.1030 +#define TK_WITH                            66
  1.1031 +#define TK_REINDEX                         67
  1.1032 +#define TK_RENAME                          68
  1.1033 +#define TK_CTIME_KW                        69
  1.1034 +#define TK_ANY                             70
  1.1035 +#define TK_OR                              71
  1.1036 +#define TK_AND                             72
  1.1037 +#define TK_IS                              73
  1.1038 +#define TK_BETWEEN                         74
  1.1039 +#define TK_IN                              75
  1.1040 +#define TK_ISNULL                          76
  1.1041 +#define TK_NOTNULL                         77
  1.1042 +#define TK_NE                              78
  1.1043 +#define TK_EQ                              79
  1.1044 +#define TK_GT                              80
  1.1045 +#define TK_LE                              81
  1.1046 +#define TK_LT                              82
  1.1047 +#define TK_GE                              83
  1.1048 +#define TK_ESCAPE                          84
  1.1049 +#define TK_BITAND                          85
  1.1050 +#define TK_BITOR                           86
  1.1051 +#define TK_LSHIFT                          87
  1.1052 +#define TK_RSHIFT                          88
  1.1053 +#define TK_PLUS                            89
  1.1054 +#define TK_MINUS                           90
  1.1055 +#define TK_STAR                            91
  1.1056 +#define TK_SLASH                           92
  1.1057 +#define TK_REM                             93
  1.1058 +#define TK_CONCAT                          94
  1.1059 +#define TK_COLLATE                         95
  1.1060 +#define TK_BITNOT                          96
  1.1061 +#define TK_STRING                          97
  1.1062 +#define TK_JOIN_KW                         98
  1.1063 +#define TK_CONSTRAINT                      99
  1.1064 +#define TK_DEFAULT                        100
  1.1065 +#define TK_NULL                           101
  1.1066 +#define TK_PRIMARY                        102
  1.1067 +#define TK_UNIQUE                         103
  1.1068 +#define TK_CHECK                          104
  1.1069 +#define TK_REFERENCES                     105
  1.1070 +#define TK_AUTOINCR                       106
  1.1071 +#define TK_ON                             107
  1.1072 +#define TK_INSERT                         108
  1.1073 +#define TK_DELETE                         109
  1.1074 +#define TK_UPDATE                         110
  1.1075 +#define TK_SET                            111
  1.1076 +#define TK_DEFERRABLE                     112
  1.1077 +#define TK_FOREIGN                        113
  1.1078 +#define TK_DROP                           114
  1.1079 +#define TK_UNION                          115
  1.1080 +#define TK_ALL                            116
  1.1081 +#define TK_EXCEPT                         117
  1.1082 +#define TK_INTERSECT                      118
  1.1083 +#define TK_SELECT                         119
  1.1084 +#define TK_VALUES                         120
  1.1085 +#define TK_DISTINCT                       121
  1.1086 +#define TK_DOT                            122
  1.1087 +#define TK_FROM                           123
  1.1088 +#define TK_JOIN                           124
  1.1089 +#define TK_USING                          125
  1.1090 +#define TK_ORDER                          126
  1.1091 +#define TK_GROUP                          127
  1.1092 +#define TK_HAVING                         128
  1.1093 +#define TK_LIMIT                          129
  1.1094 +#define TK_WHERE                          130
  1.1095 +#define TK_INTO                           131
  1.1096 +#define TK_INTEGER                        132
  1.1097 +#define TK_FLOAT                          133
  1.1098 +#define TK_BLOB                           134
  1.1099 +#define TK_VARIABLE                       135
  1.1100 +#define TK_CASE                           136
  1.1101 +#define TK_WHEN                           137
  1.1102 +#define TK_THEN                           138
  1.1103 +#define TK_ELSE                           139
  1.1104 +#define TK_INDEX                          140
  1.1105 +#define TK_ALTER                          141
  1.1106 +#define TK_ADD                            142
  1.1107 +#define TK_TO_TEXT                        143
  1.1108 +#define TK_TO_BLOB                        144
  1.1109 +#define TK_TO_NUMERIC                     145
  1.1110 +#define TK_TO_INT                         146
  1.1111 +#define TK_TO_REAL                        147
  1.1112 +#define TK_ISNOT                          148
  1.1113 +#define TK_END_OF_FILE                    149
  1.1114 +#define TK_ILLEGAL                        150
  1.1115 +#define TK_SPACE                          151
  1.1116 +#define TK_UNCLOSED_STRING                152
  1.1117 +#define TK_FUNCTION                       153
  1.1118 +#define TK_COLUMN                         154
  1.1119 +#define TK_AGG_FUNCTION                   155
  1.1120 +#define TK_AGG_COLUMN                     156
  1.1121 +#define TK_UMINUS                         157
  1.1122 +#define TK_UPLUS                          158
  1.1123 +#define TK_REGISTER                       159
  1.1124  
  1.1125  /************** End of parse.h ***********************************************/
  1.1126  /************** Continuing where we left off in sqliteInt.h ******************/
  1.1127 @@ -8324,10 +8526,10 @@
  1.1128  ** gives a possible range of values of approximately 1.0e986 to 1e-986.
  1.1129  ** But the allowed values are "grainy".  Not every value is representable.
  1.1130  ** For example, quantities 16 and 17 are both represented by a LogEst
  1.1131 -** of 40.  However, since LogEst quantatites are suppose to be estimates,
  1.1132 +** of 40.  However, since LogEst quantaties are suppose to be estimates,
  1.1133  ** not exact values, this imprecision is not a problem.
  1.1134  **
  1.1135 -** "LogEst" is short for "Logarithimic Estimate".
  1.1136 +** "LogEst" is short for "Logarithmic Estimate".
  1.1137  **
  1.1138  ** Examples:
  1.1139  **      1 -> 0              20 -> 43          10000 -> 132
  1.1140 @@ -8345,22 +8547,39 @@
  1.1141  
  1.1142  /*
  1.1143  ** Macros to determine whether the machine is big or little endian,
  1.1144 -** evaluated at runtime.
  1.1145 +** and whether or not that determination is run-time or compile-time.
  1.1146 +**
  1.1147 +** For best performance, an attempt is made to guess at the byte-order
  1.1148 +** using C-preprocessor macros.  If that is unsuccessful, or if
  1.1149 +** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
  1.1150 +** at run-time.
  1.1151  */
  1.1152  #ifdef SQLITE_AMALGAMATION
  1.1153  SQLITE_PRIVATE const int sqlite3one = 1;
  1.1154  #else
  1.1155  SQLITE_PRIVATE const int sqlite3one;
  1.1156  #endif
  1.1157 -#if defined(i386) || defined(__i386__) || defined(_M_IX86)\
  1.1158 -                             || defined(__x86_64) || defined(__x86_64__)
  1.1159 +#if (defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
  1.1160 +     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
  1.1161 +     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
  1.1162 +     defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
  1.1163 +# define SQLITE_BYTEORDER    1234
  1.1164  # define SQLITE_BIGENDIAN    0
  1.1165  # define SQLITE_LITTLEENDIAN 1
  1.1166  # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
  1.1167 -#else
  1.1168 +#endif
  1.1169 +#if (defined(sparc)    || defined(__ppc__))  \
  1.1170 +    && !defined(SQLITE_RUNTIME_BYTEORDER)
  1.1171 +# define SQLITE_BYTEORDER    4321
  1.1172 +# define SQLITE_BIGENDIAN    1
  1.1173 +# define SQLITE_LITTLEENDIAN 0
  1.1174 +# define SQLITE_UTF16NATIVE  SQLITE_UTF16BE
  1.1175 +#endif
  1.1176 +#if !defined(SQLITE_BYTEORDER)
  1.1177 +# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
  1.1178  # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
  1.1179  # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
  1.1180 -# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
  1.1181 +# define SQLITE_UTF16NATIVE  (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
  1.1182  #endif
  1.1183  
  1.1184  /*
  1.1185 @@ -8583,6 +8802,7 @@
  1.1186  typedef struct Module Module;
  1.1187  typedef struct NameContext NameContext;
  1.1188  typedef struct Parse Parse;
  1.1189 +typedef struct PrintfArguments PrintfArguments;
  1.1190  typedef struct RowSet RowSet;
  1.1191  typedef struct Savepoint Savepoint;
  1.1192  typedef struct Select Select;
  1.1193 @@ -8600,6 +8820,7 @@
  1.1194  typedef struct VtabCtx VtabCtx;
  1.1195  typedef struct Walker Walker;
  1.1196  typedef struct WhereInfo WhereInfo;
  1.1197 +typedef struct With With;
  1.1198  
  1.1199  /*
  1.1200  ** Defer sourcing vdbe.h and btree.h until after the "u8" and 
  1.1201 @@ -8673,7 +8894,9 @@
  1.1202  
  1.1203  SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
  1.1204  SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
  1.1205 -SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
  1.1206 +#if SQLITE_MAX_MMAP_SIZE>0
  1.1207 +SQLITE_PRIVATE   int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
  1.1208 +#endif
  1.1209  SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
  1.1210  SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
  1.1211  SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  1.1212 @@ -8723,6 +8946,7 @@
  1.1213  
  1.1214  SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
  1.1215  SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
  1.1216 +SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
  1.1217  SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
  1.1218  
  1.1219  SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
  1.1220 @@ -8792,17 +9016,16 @@
  1.1221  SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
  1.1222  SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
  1.1223  SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
  1.1224 -SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
  1.1225 -SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
  1.1226  
  1.1227  SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
  1.1228  SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
  1.1229  
  1.1230  SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
  1.1231 -SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
  1.1232 +SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
  1.1233  SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
  1.1234  SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
  1.1235  SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
  1.1236 +SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
  1.1237  
  1.1238  #ifndef NDEBUG
  1.1239  SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
  1.1240 @@ -8933,9 +9156,12 @@
  1.1241    char *zComment;          /* Comment to improve readability */
  1.1242  #endif
  1.1243  #ifdef VDBE_PROFILE
  1.1244 -  int cnt;                 /* Number of times this instruction was executed */
  1.1245 +  u32 cnt;                 /* Number of times this instruction was executed */
  1.1246    u64 cycles;              /* Total time spent executing this instruction */
  1.1247  #endif
  1.1248 +#ifdef SQLITE_VDBE_COVERAGE
  1.1249 +  int iSrcLine;            /* Source-code line that generated this opcode */
  1.1250 +#endif
  1.1251  };
  1.1252  typedef struct VdbeOp VdbeOp;
  1.1253  
  1.1254 @@ -9039,145 +9265,151 @@
  1.1255  #define OP_Checkpoint     11
  1.1256  #define OP_JournalMode    12
  1.1257  #define OP_Vacuum         13
  1.1258 -#define OP_VFilter        14 /* synopsis: iPlan=r[P3] zPlan='P4'           */
  1.1259 +#define OP_VFilter        14 /* synopsis: iplan=r[P3] zplan='P4'           */
  1.1260  #define OP_VUpdate        15 /* synopsis: data=r[P3@P2]                    */
  1.1261  #define OP_Goto           16
  1.1262  #define OP_Gosub          17
  1.1263  #define OP_Return         18
  1.1264  #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
  1.1265 -#define OP_Yield          20
  1.1266 -#define OP_HaltIfNull     21 /* synopsis: if r[P3] null then halt          */
  1.1267 -#define OP_Halt           22
  1.1268 -#define OP_Integer        23 /* synopsis: r[P2]=P1                         */
  1.1269 -#define OP_Int64          24 /* synopsis: r[P2]=P4                         */
  1.1270 -#define OP_String         25 /* synopsis: r[P2]='P4' (len=P1)              */
  1.1271 -#define OP_Null           26 /* synopsis: r[P2..P3]=NULL                   */
  1.1272 -#define OP_Blob           27 /* synopsis: r[P2]=P4 (len=P1)                */
  1.1273 -#define OP_Variable       28 /* synopsis: r[P2]=parameter(P1,P4)           */
  1.1274 -#define OP_Move           29 /* synopsis: r[P2@P3]=r[P1@P3]                */
  1.1275 -#define OP_Copy           30 /* synopsis: r[P2@P3]=r[P1@P3]                */
  1.1276 -#define OP_SCopy          31 /* synopsis: r[P2]=r[P1]                      */
  1.1277 -#define OP_ResultRow      32 /* synopsis: output=r[P1@P2]                  */
  1.1278 -#define OP_CollSeq        33
  1.1279 -#define OP_AddImm         34 /* synopsis: r[P1]=r[P1]+P2                   */
  1.1280 -#define OP_MustBeInt      35
  1.1281 -#define OP_RealAffinity   36
  1.1282 -#define OP_Permutation    37
  1.1283 -#define OP_Compare        38
  1.1284 -#define OP_Jump           39
  1.1285 -#define OP_Once           40
  1.1286 -#define OP_If             41
  1.1287 -#define OP_IfNot          42
  1.1288 -#define OP_Column         43 /* synopsis: r[P3]=PX                         */
  1.1289 -#define OP_Affinity       44 /* synopsis: affinity(r[P1@P2])               */
  1.1290 -#define OP_MakeRecord     45 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
  1.1291 -#define OP_Count          46 /* synopsis: r[P2]=count()                    */
  1.1292 -#define OP_ReadCookie     47
  1.1293 -#define OP_SetCookie      48
  1.1294 -#define OP_VerifyCookie   49
  1.1295 -#define OP_OpenRead       50 /* synopsis: root=P2 iDb=P3                   */
  1.1296 -#define OP_OpenWrite      51 /* synopsis: root=P2 iDb=P3                   */
  1.1297 -#define OP_OpenAutoindex  52 /* synopsis: nColumn=P2                       */
  1.1298 -#define OP_OpenEphemeral  53 /* synopsis: nColumn=P2                       */
  1.1299 -#define OP_SorterOpen     54
  1.1300 -#define OP_OpenPseudo     55 /* synopsis: content in r[P2@P3]              */
  1.1301 -#define OP_Close          56
  1.1302 -#define OP_SeekLt         57 /* synopsis: key=r[P3@P4]                     */
  1.1303 -#define OP_SeekLe         58 /* synopsis: key=r[P3@P4]                     */
  1.1304 -#define OP_SeekGe         59 /* synopsis: key=r[P3@P4]                     */
  1.1305 -#define OP_SeekGt         60 /* synopsis: key=r[P3@P4]                     */
  1.1306 -#define OP_Seek           61 /* synopsis: intkey=r[P2]                     */
  1.1307 -#define OP_NoConflict     62 /* synopsis: key=r[P3@P4]                     */
  1.1308 -#define OP_NotFound       63 /* synopsis: key=r[P3@P4]                     */
  1.1309 -#define OP_Found          64 /* synopsis: key=r[P3@P4]                     */
  1.1310 -#define OP_NotExists      65 /* synopsis: intkey=r[P3]                     */
  1.1311 -#define OP_Sequence       66 /* synopsis: r[P2]=rowid                      */
  1.1312 -#define OP_NewRowid       67 /* synopsis: r[P2]=rowid                      */
  1.1313 -#define OP_Insert         68 /* synopsis: intkey=r[P3] data=r[P2]          */
  1.1314 -#define OP_Or             69 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  1.1315 -#define OP_And            70 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  1.1316 -#define OP_InsertInt      71 /* synopsis: intkey=P3 data=r[P2]             */
  1.1317 -#define OP_Delete         72
  1.1318 -#define OP_ResetCount     73
  1.1319 -#define OP_IsNull         74 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
  1.1320 -#define OP_NotNull        75 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
  1.1321 -#define OP_Ne             76 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
  1.1322 -#define OP_Eq             77 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
  1.1323 -#define OP_Gt             78 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
  1.1324 -#define OP_Le             79 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
  1.1325 -#define OP_Lt             80 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
  1.1326 -#define OP_Ge             81 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
  1.1327 -#define OP_SorterCompare  82 /* synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2 */
  1.1328 -#define OP_BitAnd         83 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
  1.1329 -#define OP_BitOr          84 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
  1.1330 -#define OP_ShiftLeft      85 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
  1.1331 -#define OP_ShiftRight     86 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
  1.1332 -#define OP_Add            87 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
  1.1333 -#define OP_Subtract       88 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
  1.1334 -#define OP_Multiply       89 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
  1.1335 -#define OP_Divide         90 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
  1.1336 -#define OP_Remainder      91 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
  1.1337 -#define OP_Concat         92 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
  1.1338 -#define OP_SorterData     93 /* synopsis: r[P2]=data                       */
  1.1339 -#define OP_BitNot         94 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
  1.1340 -#define OP_String8        95 /* same as TK_STRING, synopsis: r[P2]='P4'    */
  1.1341 -#define OP_RowKey         96 /* synopsis: r[P2]=key                        */
  1.1342 -#define OP_RowData        97 /* synopsis: r[P2]=data                       */
  1.1343 -#define OP_Rowid          98 /* synopsis: r[P2]=rowid                      */
  1.1344 -#define OP_NullRow        99
  1.1345 -#define OP_Last          100
  1.1346 -#define OP_SorterSort    101
  1.1347 -#define OP_Sort          102
  1.1348 -#define OP_Rewind        103
  1.1349 -#define OP_SorterInsert  104
  1.1350 -#define OP_IdxInsert     105 /* synopsis: key=r[P2]                        */
  1.1351 -#define OP_IdxDelete     106 /* synopsis: key=r[P2@P3]                     */
  1.1352 -#define OP_IdxRowid      107 /* synopsis: r[P2]=rowid                      */
  1.1353 -#define OP_IdxLT         108 /* synopsis: key=r[P3@P4]                     */
  1.1354 -#define OP_IdxGE         109 /* synopsis: key=r[P3@P4]                     */
  1.1355 -#define OP_Destroy       110
  1.1356 -#define OP_Clear         111
  1.1357 -#define OP_CreateIndex   112 /* synopsis: r[P2]=root iDb=P1                */
  1.1358 -#define OP_CreateTable   113 /* synopsis: r[P2]=root iDb=P1                */
  1.1359 -#define OP_ParseSchema   114
  1.1360 -#define OP_LoadAnalysis  115
  1.1361 -#define OP_DropTable     116
  1.1362 -#define OP_DropIndex     117
  1.1363 -#define OP_DropTrigger   118
  1.1364 -#define OP_IntegrityCk   119
  1.1365 -#define OP_RowSetAdd     120 /* synopsis: rowset(P1)=r[P2]                 */
  1.1366 -#define OP_RowSetRead    121 /* synopsis: r[P3]=rowset(P1)                 */
  1.1367 -#define OP_RowSetTest    122 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  1.1368 -#define OP_Program       123
  1.1369 -#define OP_Param         124
  1.1370 -#define OP_FkCounter     125 /* synopsis: fkctr[P1]+=P2                    */
  1.1371 -#define OP_FkIfZero      126 /* synopsis: if fkctr[P1]==0 goto P2          */
  1.1372 -#define OP_MemMax        127 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  1.1373 -#define OP_IfPos         128 /* synopsis: if r[P1]>0 goto P2               */
  1.1374 -#define OP_IfNeg         129 /* synopsis: if r[P1]<0 goto P2               */
  1.1375 -#define OP_IfZero        130 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2   */
  1.1376 -#define OP_Real          131 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
  1.1377 -#define OP_AggFinal      132 /* synopsis: accum=r[P1] N=P2                 */
  1.1378 -#define OP_IncrVacuum    133
  1.1379 -#define OP_Expire        134
  1.1380 -#define OP_TableLock     135 /* synopsis: iDb=P1 root=P2 write=P3          */
  1.1381 -#define OP_VBegin        136
  1.1382 -#define OP_VCreate       137
  1.1383 -#define OP_VDestroy      138
  1.1384 -#define OP_VOpen         139
  1.1385 -#define OP_VColumn       140 /* synopsis: r[P3]=vcolumn(P2)                */
  1.1386 -#define OP_VNext         141
  1.1387 -#define OP_ToText        142 /* same as TK_TO_TEXT                         */
  1.1388 -#define OP_ToBlob        143 /* same as TK_TO_BLOB                         */
  1.1389 -#define OP_ToNumeric     144 /* same as TK_TO_NUMERIC                      */
  1.1390 -#define OP_ToInt         145 /* same as TK_TO_INT                          */
  1.1391 -#define OP_ToReal        146 /* same as TK_TO_REAL                         */
  1.1392 -#define OP_VRename       147
  1.1393 -#define OP_Pagecount     148
  1.1394 -#define OP_MaxPgcnt      149
  1.1395 -#define OP_Trace         150
  1.1396 -#define OP_Noop          151
  1.1397 -#define OP_Explain       152
  1.1398 +#define OP_InitCoroutine  20
  1.1399 +#define OP_EndCoroutine   21
  1.1400 +#define OP_Yield          22
  1.1401 +#define OP_HaltIfNull     23 /* synopsis: if r[P3]=null halt               */
  1.1402 +#define OP_Halt           24
  1.1403 +#define OP_Integer        25 /* synopsis: r[P2]=P1                         */
  1.1404 +#define OP_Int64          26 /* synopsis: r[P2]=P4                         */
  1.1405 +#define OP_String         27 /* synopsis: r[P2]='P4' (len=P1)              */
  1.1406 +#define OP_Null           28 /* synopsis: r[P2..P3]=NULL                   */
  1.1407 +#define OP_SoftNull       29 /* synopsis: r[P1]=NULL                       */
  1.1408 +#define OP_Blob           30 /* synopsis: r[P2]=P4 (len=P1)                */
  1.1409 +#define OP_Variable       31 /* synopsis: r[P2]=parameter(P1,P4)           */
  1.1410 +#define OP_Move           32 /* synopsis: r[P2@P3]=r[P1@P3]                */
  1.1411 +#define OP_Copy           33 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
  1.1412 +#define OP_SCopy          34 /* synopsis: r[P2]=r[P1]                      */
  1.1413 +#define OP_ResultRow      35 /* synopsis: output=r[P1@P2]                  */
  1.1414 +#define OP_CollSeq        36
  1.1415 +#define OP_AddImm         37 /* synopsis: r[P1]=r[P1]+P2                   */
  1.1416 +#define OP_MustBeInt      38
  1.1417 +#define OP_RealAffinity   39
  1.1418 +#define OP_Permutation    40
  1.1419 +#define OP_Compare        41 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
  1.1420 +#define OP_Jump           42
  1.1421 +#define OP_Once           43
  1.1422 +#define OP_If             44
  1.1423 +#define OP_IfNot          45
  1.1424 +#define OP_Column         46 /* synopsis: r[P3]=PX                         */
  1.1425 +#define OP_Affinity       47 /* synopsis: affinity(r[P1@P2])               */
  1.1426 +#define OP_MakeRecord     48 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
  1.1427 +#define OP_Count          49 /* synopsis: r[P2]=count()                    */
  1.1428 +#define OP_ReadCookie     50
  1.1429 +#define OP_SetCookie      51
  1.1430 +#define OP_ReopenIdx      52 /* synopsis: root=P2 iDb=P3                   */
  1.1431 +#define OP_OpenRead       53 /* synopsis: root=P2 iDb=P3                   */
  1.1432 +#define OP_OpenWrite      54 /* synopsis: root=P2 iDb=P3                   */
  1.1433 +#define OP_OpenAutoindex  55 /* synopsis: nColumn=P2                       */
  1.1434 +#define OP_OpenEphemeral  56 /* synopsis: nColumn=P2                       */
  1.1435 +#define OP_SorterOpen     57
  1.1436 +#define OP_OpenPseudo     58 /* synopsis: P3 columns in r[P2]              */
  1.1437 +#define OP_Close          59
  1.1438 +#define OP_SeekLT         60 /* synopsis: key=r[P3@P4]                     */
  1.1439 +#define OP_SeekLE         61 /* synopsis: key=r[P3@P4]                     */
  1.1440 +#define OP_SeekGE         62 /* synopsis: key=r[P3@P4]                     */
  1.1441 +#define OP_SeekGT         63 /* synopsis: key=r[P3@P4]                     */
  1.1442 +#define OP_Seek           64 /* synopsis: intkey=r[P2]                     */
  1.1443 +#define OP_NoConflict     65 /* synopsis: key=r[P3@P4]                     */
  1.1444 +#define OP_NotFound       66 /* synopsis: key=r[P3@P4]                     */
  1.1445 +#define OP_Found          67 /* synopsis: key=r[P3@P4]                     */
  1.1446 +#define OP_NotExists      68 /* synopsis: intkey=r[P3]                     */
  1.1447 +#define OP_Sequence       69 /* synopsis: r[P2]=cursor[P1].ctr++           */
  1.1448 +#define OP_NewRowid       70 /* synopsis: r[P2]=rowid                      */
  1.1449 +#define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  1.1450 +#define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  1.1451 +#define OP_Insert         73 /* synopsis: intkey=r[P3] data=r[P2]          */
  1.1452 +#define OP_InsertInt      74 /* synopsis: intkey=P3 data=r[P2]             */
  1.1453 +#define OP_Delete         75
  1.1454 +#define OP_IsNull         76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
  1.1455 +#define OP_NotNull        77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
  1.1456 +#define OP_Ne             78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
  1.1457 +#define OP_Eq             79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
  1.1458 +#define OP_Gt             80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
  1.1459 +#define OP_Le             81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
  1.1460 +#define OP_Lt             82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
  1.1461 +#define OP_Ge             83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
  1.1462 +#define OP_ResetCount     84
  1.1463 +#define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
  1.1464 +#define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
  1.1465 +#define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
  1.1466 +#define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
  1.1467 +#define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
  1.1468 +#define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
  1.1469 +#define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
  1.1470 +#define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
  1.1471 +#define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
  1.1472 +#define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
  1.1473 +#define OP_SorterCompare  95 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
  1.1474 +#define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
  1.1475 +#define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
  1.1476 +#define OP_SorterData     98 /* synopsis: r[P2]=data                       */
  1.1477 +#define OP_RowKey         99 /* synopsis: r[P2]=key                        */
  1.1478 +#define OP_RowData       100 /* synopsis: r[P2]=data                       */
  1.1479 +#define OP_Rowid         101 /* synopsis: r[P2]=rowid                      */
  1.1480 +#define OP_NullRow       102
  1.1481 +#define OP_Last          103
  1.1482 +#define OP_SorterSort    104
  1.1483 +#define OP_Sort          105
  1.1484 +#define OP_Rewind        106
  1.1485 +#define OP_SorterInsert  107
  1.1486 +#define OP_IdxInsert     108 /* synopsis: key=r[P2]                        */
  1.1487 +#define OP_IdxDelete     109 /* synopsis: key=r[P2@P3]                     */
  1.1488 +#define OP_IdxRowid      110 /* synopsis: r[P2]=rowid                      */
  1.1489 +#define OP_IdxLE         111 /* synopsis: key=r[P3@P4]                     */
  1.1490 +#define OP_IdxGT         112 /* synopsis: key=r[P3@P4]                     */
  1.1491 +#define OP_IdxLT         113 /* synopsis: key=r[P3@P4]                     */
  1.1492 +#define OP_IdxGE         114 /* synopsis: key=r[P3@P4]                     */
  1.1493 +#define OP_Destroy       115
  1.1494 +#define OP_Clear         116
  1.1495 +#define OP_ResetSorter   117
  1.1496 +#define OP_CreateIndex   118 /* synopsis: r[P2]=root iDb=P1                */
  1.1497 +#define OP_CreateTable   119 /* synopsis: r[P2]=root iDb=P1                */
  1.1498 +#define OP_ParseSchema   120
  1.1499 +#define OP_LoadAnalysis  121
  1.1500 +#define OP_DropTable     122
  1.1501 +#define OP_DropIndex     123
  1.1502 +#define OP_DropTrigger   124
  1.1503 +#define OP_IntegrityCk   125
  1.1504 +#define OP_RowSetAdd     126 /* synopsis: rowset(P1)=r[P2]                 */
  1.1505 +#define OP_RowSetRead    127 /* synopsis: r[P3]=rowset(P1)                 */
  1.1506 +#define OP_RowSetTest    128 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  1.1507 +#define OP_Program       129
  1.1508 +#define OP_Param         130
  1.1509 +#define OP_FkCounter     131 /* synopsis: fkctr[P1]+=P2                    */
  1.1510 +#define OP_FkIfZero      132 /* synopsis: if fkctr[P1]==0 goto P2          */
  1.1511 +#define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
  1.1512 +#define OP_MemMax        134 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  1.1513 +#define OP_IfPos         135 /* synopsis: if r[P1]>0 goto P2               */
  1.1514 +#define OP_IfNeg         136 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2    */
  1.1515 +#define OP_IfZero        137 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2   */
  1.1516 +#define OP_AggFinal      138 /* synopsis: accum=r[P1] N=P2                 */
  1.1517 +#define OP_IncrVacuum    139
  1.1518 +#define OP_Expire        140
  1.1519 +#define OP_TableLock     141 /* synopsis: iDb=P1 root=P2 write=P3          */
  1.1520 +#define OP_VBegin        142
  1.1521 +#define OP_ToText        143 /* same as TK_TO_TEXT                         */
  1.1522 +#define OP_ToBlob        144 /* same as TK_TO_BLOB                         */
  1.1523 +#define OP_ToNumeric     145 /* same as TK_TO_NUMERIC                      */
  1.1524 +#define OP_ToInt         146 /* same as TK_TO_INT                          */
  1.1525 +#define OP_ToReal        147 /* same as TK_TO_REAL                         */
  1.1526 +#define OP_VCreate       148
  1.1527 +#define OP_VDestroy      149
  1.1528 +#define OP_VOpen         150
  1.1529 +#define OP_VColumn       151 /* synopsis: r[P3]=vcolumn(P2)                */
  1.1530 +#define OP_VNext         152
  1.1531 +#define OP_VRename       153
  1.1532 +#define OP_Pagecount     154
  1.1533 +#define OP_MaxPgcnt      155
  1.1534 +#define OP_Init          156 /* synopsis: Start at P2                      */
  1.1535 +#define OP_Noop          157
  1.1536 +#define OP_Explain       158
  1.1537  
  1.1538  
  1.1539  /* Properties such as "out2" or "jump" that are specified in
  1.1540 @@ -9194,24 +9426,24 @@
  1.1541  #define OPFLG_INITIALIZER {\
  1.1542  /*   0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\
  1.1543  /*   8 */ 0x01, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00,\
  1.1544 -/*  16 */ 0x01, 0x01, 0x04, 0x24, 0x04, 0x10, 0x00, 0x02,\
  1.1545 -/*  24 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x20,\
  1.1546 -/*  32 */ 0x00, 0x00, 0x04, 0x05, 0x04, 0x00, 0x00, 0x01,\
  1.1547 -/*  40 */ 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x02,\
  1.1548 -/*  48 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  1.1549 -/*  56 */ 0x00, 0x11, 0x11, 0x11, 0x11, 0x08, 0x11, 0x11,\
  1.1550 -/*  64 */ 0x11, 0x11, 0x02, 0x02, 0x00, 0x4c, 0x4c, 0x00,\
  1.1551 -/*  72 */ 0x00, 0x00, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15,\
  1.1552 -/*  80 */ 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
  1.1553 -/*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00, 0x24, 0x02,\
  1.1554 -/*  96 */ 0x00, 0x00, 0x02, 0x00, 0x01, 0x01, 0x01, 0x01,\
  1.1555 -/* 104 */ 0x08, 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00,\
  1.1556 -/* 112 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  1.1557 -/* 120 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
  1.1558 -/* 128 */ 0x05, 0x05, 0x05, 0x02, 0x00, 0x01, 0x00, 0x00,\
  1.1559 -/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x04,\
  1.1560 -/* 144 */ 0x04, 0x04, 0x04, 0x00, 0x02, 0x02, 0x00, 0x00,\
  1.1561 -/* 152 */ 0x00,}
  1.1562 +/*  16 */ 0x01, 0x01, 0x04, 0x24, 0x01, 0x04, 0x05, 0x10,\
  1.1563 +/*  24 */ 0x00, 0x02, 0x02, 0x02, 0x02, 0x00, 0x02, 0x02,\
  1.1564 +/*  32 */ 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x05, 0x04,\
  1.1565 +/*  40 */ 0x00, 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00,\
  1.1566 +/*  48 */ 0x00, 0x02, 0x02, 0x10, 0x00, 0x00, 0x00, 0x00,\
  1.1567 +/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
  1.1568 +/*  64 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x4c,\
  1.1569 +/*  72 */ 0x4c, 0x00, 0x00, 0x00, 0x05, 0x05, 0x15, 0x15,\
  1.1570 +/*  80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\
  1.1571 +/*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\
  1.1572 +/*  96 */ 0x24, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
  1.1573 +/* 104 */ 0x01, 0x01, 0x01, 0x08, 0x08, 0x00, 0x02, 0x01,\
  1.1574 +/* 112 */ 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0x02, 0x02,\
  1.1575 +/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x45,\
  1.1576 +/* 128 */ 0x15, 0x01, 0x02, 0x00, 0x01, 0x02, 0x08, 0x05,\
  1.1577 +/* 136 */ 0x05, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04,\
  1.1578 +/* 144 */ 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00,\
  1.1579 +/* 152 */ 0x01, 0x00, 0x02, 0x02, 0x01, 0x00, 0x00,}
  1.1580  
  1.1581  /************** End of opcodes.h *********************************************/
  1.1582  /************** Continuing where we left off in vdbe.h ***********************/
  1.1583 @@ -9220,14 +9452,14 @@
  1.1584  ** Prototypes for the VDBE interface.  See comments on the implementation
  1.1585  ** for a description of what each of these routines does.
  1.1586  */
  1.1587 -SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
  1.1588 +SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
  1.1589  SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
  1.1590  SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
  1.1591  SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
  1.1592  SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
  1.1593  SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
  1.1594  SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
  1.1595 -SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
  1.1596 +SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
  1.1597  SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
  1.1598  SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
  1.1599  SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
  1.1600 @@ -9235,6 +9467,7 @@
  1.1601  SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
  1.1602  SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
  1.1603  SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
  1.1604 +SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
  1.1605  SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
  1.1606  SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
  1.1607  SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
  1.1608 @@ -9265,11 +9498,15 @@
  1.1609  #ifndef SQLITE_OMIT_TRACE
  1.1610  SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  1.1611  #endif
  1.1612 +SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
  1.1613  
  1.1614  SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
  1.1615 -SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
  1.1616 +SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*,int);
  1.1617  SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
  1.1618  
  1.1619 +typedef int (*RecordCompare)(int,const void*,UnpackedRecord*,int);
  1.1620 +SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
  1.1621 +
  1.1622  #ifndef SQLITE_OMIT_TRIGGER
  1.1623  SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
  1.1624  #endif
  1.1625 @@ -9297,6 +9534,43 @@
  1.1626  # define VdbeModuleComment(X)
  1.1627  #endif
  1.1628  
  1.1629 +/*
  1.1630 +** The VdbeCoverage macros are used to set a coverage testing point
  1.1631 +** for VDBE branch instructions.  The coverage testing points are line
  1.1632 +** numbers in the sqlite3.c source file.  VDBE branch coverage testing
  1.1633 +** only works with an amalagmation build.  That's ok since a VDBE branch
  1.1634 +** coverage build designed for testing the test suite only.  No application
  1.1635 +** should ever ship with VDBE branch coverage measuring turned on.
  1.1636 +**
  1.1637 +**    VdbeCoverage(v)                  // Mark the previously coded instruction
  1.1638 +**                                     // as a branch
  1.1639 +**
  1.1640 +**    VdbeCoverageIf(v, conditional)   // Mark previous if conditional true
  1.1641 +**
  1.1642 +**    VdbeCoverageAlwaysTaken(v)       // Previous branch is always taken
  1.1643 +**
  1.1644 +**    VdbeCoverageNeverTaken(v)        // Previous branch is never taken
  1.1645 +**
  1.1646 +** Every VDBE branch operation must be tagged with one of the macros above.
  1.1647 +** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
  1.1648 +** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
  1.1649 +** routine in vdbe.c, alerting the developer to the missed tag.
  1.1650 +*/
  1.1651 +#ifdef SQLITE_VDBE_COVERAGE
  1.1652 +SQLITE_PRIVATE   void sqlite3VdbeSetLineNumber(Vdbe*,int);
  1.1653 +# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
  1.1654 +# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
  1.1655 +# define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2);
  1.1656 +# define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1);
  1.1657 +# define VDBE_OFFSET_LINENO(x) (__LINE__+x)
  1.1658 +#else
  1.1659 +# define VdbeCoverage(v)
  1.1660 +# define VdbeCoverageIf(v,x)
  1.1661 +# define VdbeCoverageAlwaysTaken(v)
  1.1662 +# define VdbeCoverageNeverTaken(v)
  1.1663 +# define VDBE_OFFSET_LINENO(x) 0
  1.1664 +#endif
  1.1665 +
  1.1666  #endif
  1.1667  
  1.1668  /************** End of vdbe.h ************************************************/
  1.1669 @@ -9441,6 +9715,7 @@
  1.1670  SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
  1.1671  SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
  1.1672  SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
  1.1673 +SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
  1.1674  
  1.1675  /* Operations on page references. */
  1.1676  SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
  1.1677 @@ -9455,7 +9730,7 @@
  1.1678  SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
  1.1679  SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
  1.1680  SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
  1.1681 -SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
  1.1682 +SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster);
  1.1683  SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
  1.1684  SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
  1.1685  SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
  1.1686 @@ -9707,83 +9982,71 @@
  1.1687  #define _SQLITE_OS_H_
  1.1688  
  1.1689  /*
  1.1690 -** Figure out if we are dealing with Unix, Windows, or some other
  1.1691 -** operating system.  After the following block of preprocess macros,
  1.1692 -** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER 
  1.1693 -** will defined to either 1 or 0.  One of the four will be 1.  The other 
  1.1694 -** three will be 0.
  1.1695 +** Attempt to automatically detect the operating system and setup the
  1.1696 +** necessary pre-processor macros for it.
  1.1697 +*/
  1.1698 +/************** Include os_setup.h in the middle of os.h *********************/
  1.1699 +/************** Begin file os_setup.h ****************************************/
  1.1700 +/*
  1.1701 +** 2013 November 25
  1.1702 +**
  1.1703 +** The author disclaims copyright to this source code.  In place of
  1.1704 +** a legal notice, here is a blessing:
  1.1705 +**
  1.1706 +**    May you do good and not evil.
  1.1707 +**    May you find forgiveness for yourself and forgive others.
  1.1708 +**    May you share freely, never taking more than you give.
  1.1709 +**
  1.1710 +******************************************************************************
  1.1711 +**
  1.1712 +** This file contains pre-processor directives related to operating system
  1.1713 +** detection and/or setup.
  1.1714 +*/
  1.1715 +#ifndef _OS_SETUP_H_
  1.1716 +#define _OS_SETUP_H_
  1.1717 +
  1.1718 +/*
  1.1719 +** Figure out if we are dealing with Unix, Windows, or some other operating
  1.1720 +** system.
  1.1721 +**
  1.1722 +** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
  1.1723 +** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0.  One of
  1.1724 +** the three will be 1.  The other two will be 0.
  1.1725  */
  1.1726  #if defined(SQLITE_OS_OTHER)
  1.1727 -# if SQLITE_OS_OTHER==1
  1.1728 -#   undef SQLITE_OS_UNIX
  1.1729 -#   define SQLITE_OS_UNIX 0
  1.1730 -#   undef SQLITE_OS_WIN
  1.1731 -#   define SQLITE_OS_WIN 0
  1.1732 -# else
  1.1733 -#   undef SQLITE_OS_OTHER
  1.1734 -# endif
  1.1735 +#  if SQLITE_OS_OTHER==1
  1.1736 +#    undef SQLITE_OS_UNIX
  1.1737 +#    define SQLITE_OS_UNIX 0
  1.1738 +#    undef SQLITE_OS_WIN
  1.1739 +#    define SQLITE_OS_WIN 0
  1.1740 +#  else
  1.1741 +#    undef SQLITE_OS_OTHER
  1.1742 +#  endif
  1.1743  #endif
  1.1744  #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
  1.1745 -# define SQLITE_OS_OTHER 0
  1.1746 -# ifndef SQLITE_OS_WIN
  1.1747 -#   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
  1.1748 -#     define SQLITE_OS_WIN 1
  1.1749 -#     define SQLITE_OS_UNIX 0
  1.1750 -#   else
  1.1751 -#     define SQLITE_OS_WIN 0
  1.1752 -#     define SQLITE_OS_UNIX 1
  1.1753 +#  define SQLITE_OS_OTHER 0
  1.1754 +#  ifndef SQLITE_OS_WIN
  1.1755 +#    if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
  1.1756 +        defined(__MINGW32__) || defined(__BORLANDC__)
  1.1757 +#      define SQLITE_OS_WIN 1
  1.1758 +#      define SQLITE_OS_UNIX 0
  1.1759 +#    else
  1.1760 +#      define SQLITE_OS_WIN 0
  1.1761 +#      define SQLITE_OS_UNIX 1
  1.1762 +#    endif
  1.1763 +#  else
  1.1764 +#    define SQLITE_OS_UNIX 0
  1.1765  #  endif
  1.1766 -# else
  1.1767 -#  define SQLITE_OS_UNIX 0
  1.1768 -# endif
  1.1769 -#else
  1.1770 -# ifndef SQLITE_OS_WIN
  1.1771 -#  define SQLITE_OS_WIN 0
  1.1772 -# endif
  1.1773 -#endif
  1.1774 -
  1.1775 -#if SQLITE_OS_WIN
  1.1776 -# include <windows.h>
  1.1777 -#endif
  1.1778 -
  1.1779 -/*
  1.1780 -** Determine if we are dealing with Windows NT.
  1.1781 -**
  1.1782 -** We ought to be able to determine if we are compiling for win98 or winNT
  1.1783 -** using the _WIN32_WINNT macro as follows:
  1.1784 -**
  1.1785 -** #if defined(_WIN32_WINNT)
  1.1786 -** # define SQLITE_OS_WINNT 1
  1.1787 -** #else
  1.1788 -** # define SQLITE_OS_WINNT 0
  1.1789 -** #endif
  1.1790 -**
  1.1791 -** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
  1.1792 -** so the above test does not work.  We'll just assume that everything is
  1.1793 -** winNT unless the programmer explicitly says otherwise by setting
  1.1794 -** SQLITE_OS_WINNT to 0.
  1.1795 -*/
  1.1796 -#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
  1.1797 -# define SQLITE_OS_WINNT 1
  1.1798 -#endif
  1.1799 -
  1.1800 -/*
  1.1801 -** Determine if we are dealing with WindowsCE - which has a much
  1.1802 -** reduced API.
  1.1803 -*/
  1.1804 -#if defined(_WIN32_WCE)
  1.1805 -# define SQLITE_OS_WINCE 1
  1.1806 -#else
  1.1807 -# define SQLITE_OS_WINCE 0
  1.1808 -#endif
  1.1809 -
  1.1810 -/*
  1.1811 -** Determine if we are dealing with WinRT, which provides only a subset of
  1.1812 -** the full Win32 API.
  1.1813 -*/
  1.1814 -#if !defined(SQLITE_OS_WINRT)
  1.1815 -# define SQLITE_OS_WINRT 0
  1.1816 -#endif
  1.1817 +#else
  1.1818 +#  ifndef SQLITE_OS_WIN
  1.1819 +#    define SQLITE_OS_WIN 0
  1.1820 +#  endif
  1.1821 +#endif
  1.1822 +
  1.1823 +#endif /* _OS_SETUP_H_ */
  1.1824 +
  1.1825 +/************** End of os_setup.h ********************************************/
  1.1826 +/************** Continuing where we left off in os.h *************************/
  1.1827  
  1.1828  /* If the SET_FULLSYNC macro is not defined above, then make it
  1.1829  ** a no-op
  1.1830 @@ -10087,7 +10350,7 @@
  1.1831    Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
  1.1832    u8 file_format;      /* Schema format version for this file */
  1.1833    u8 enc;              /* Text encoding used by this database */
  1.1834 -  u16 flags;           /* Flags associated with this schema */
  1.1835 +  u16 schemaFlags;     /* Flags associated with this schema */
  1.1836    int cache_size;      /* Number of pages to use in the cache */
  1.1837  };
  1.1838  
  1.1839 @@ -10095,10 +10358,10 @@
  1.1840  ** These macros can be used to test, set, or clear bits in the 
  1.1841  ** Db.pSchema->flags field.
  1.1842  */
  1.1843 -#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
  1.1844 -#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
  1.1845 -#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
  1.1846 -#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
  1.1847 +#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
  1.1848 +#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
  1.1849 +#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->schemaFlags|=(P)
  1.1850 +#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->schemaFlags&=~(P)
  1.1851  
  1.1852  /*
  1.1853  ** Allowed values for the DB.pSchema->flags field.
  1.1854 @@ -10327,7 +10590,7 @@
  1.1855  #define SQLITE_ColumnCache    0x0002   /* Column cache */
  1.1856  #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
  1.1857  #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
  1.1858 -#define SQLITE_IdxRealAsInt   0x0010   /* Store REAL as INT in indices */
  1.1859 +/*                not used    0x0010   // Was: SQLITE_IdxRealAsInt */
  1.1860  #define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
  1.1861  #define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
  1.1862  #define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
  1.1863 @@ -10353,8 +10616,7 @@
  1.1864  ** Return true if it OK to factor constant expressions into the initialization
  1.1865  ** code. The argument is a Parse object for the code generator.
  1.1866  */
  1.1867 -#define ConstFactorOk(P) \
  1.1868 -  ((P)->cookieGoto>0 && OptimizationEnabled((P)->db,SQLITE_FactorOutConst))
  1.1869 +#define ConstFactorOk(P) ((P)->okConstFactor)
  1.1870  
  1.1871  /*
  1.1872  ** Possible values for the sqlite.magic field.
  1.1873 @@ -10580,10 +10842,16 @@
  1.1874  /*
  1.1875  ** Additional bit values that can be ORed with an affinity without
  1.1876  ** changing the affinity.
  1.1877 +**
  1.1878 +** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
  1.1879 +** It causes an assert() to fire if either operand to a comparison
  1.1880 +** operator is NULL.  It is added to certain comparison operators to
  1.1881 +** prove that the operands are always NOT NULL.
  1.1882  */
  1.1883  #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
  1.1884  #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
  1.1885  #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
  1.1886 +#define SQLITE_NOTNULL      0x88  /* Assert that operands are never NULL */
  1.1887  
  1.1888  /*
  1.1889  ** An object of this type is created for each virtual table present in
  1.1890 @@ -10677,12 +10945,15 @@
  1.1891  #ifndef SQLITE_OMIT_CHECK
  1.1892    ExprList *pCheck;    /* All CHECK constraints */
  1.1893  #endif
  1.1894 -  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
  1.1895 +  LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
  1.1896    int tnum;            /* Root BTree node for this table (see note above) */
  1.1897    i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
  1.1898    i16 nCol;            /* Number of columns in this table */
  1.1899    u16 nRef;            /* Number of pointers to this Table */
  1.1900    LogEst szTabRow;     /* Estimated size of each table row in bytes */
  1.1901 +#ifdef SQLITE_ENABLE_COSTMULT
  1.1902 +  LogEst costMult;     /* Cost multiplier for using this table */
  1.1903 +#endif
  1.1904    u8 tabFlags;         /* Mask of TF_* values */
  1.1905    u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
  1.1906  #ifndef SQLITE_OMIT_ALTERTABLE
  1.1907 @@ -10699,7 +10970,7 @@
  1.1908  };
  1.1909  
  1.1910  /*
  1.1911 -** Allowed values for Tabe.tabFlags.
  1.1912 +** Allowed values for Table.tabFlags.
  1.1913  */
  1.1914  #define TF_Readonly        0x01    /* Read-only system table */
  1.1915  #define TF_Ephemeral       0x02    /* An ephemeral table */
  1.1916 @@ -10842,19 +11113,20 @@
  1.1917  **
  1.1918  ** This structure holds a record that has already been disassembled
  1.1919  ** into its constituent fields.
  1.1920 +**
  1.1921 +** The r1 and r2 member variables are only used by the optimized comparison
  1.1922 +** functions vdbeRecordCompareInt() and vdbeRecordCompareString().
  1.1923  */
  1.1924  struct UnpackedRecord {
  1.1925    KeyInfo *pKeyInfo;  /* Collation and sort-order information */
  1.1926    u16 nField;         /* Number of entries in apMem[] */
  1.1927 -  u8 flags;           /* Boolean settings.  UNPACKED_... below */
  1.1928 +  i8 default_rc;      /* Comparison result if keys are equal */
  1.1929 +  u8 isCorrupt;       /* Corruption detected by xRecordCompare() */
  1.1930    Mem *aMem;          /* Values */
  1.1931 -};
  1.1932 -
  1.1933 -/*
  1.1934 -** Allowed values of UnpackedRecord.flags
  1.1935 -*/
  1.1936 -#define UNPACKED_INCRKEY       0x01  /* Make this key an epsilon larger */
  1.1937 -#define UNPACKED_PREFIX_MATCH  0x02  /* A prefix match is considered OK */
  1.1938 +  int r1;             /* Value to return if (lhs > rhs) */
  1.1939 +  int r2;             /* Value to return if (rhs < lhs) */
  1.1940 +};
  1.1941 +
  1.1942  
  1.1943  /*
  1.1944  ** Each SQL index is represented in memory by an
  1.1945 @@ -10885,7 +11157,7 @@
  1.1946  struct Index {
  1.1947    char *zName;             /* Name of this index */
  1.1948    i16 *aiColumn;           /* Which columns are used by this index.  1st is 0 */
  1.1949 -  tRowcnt *aiRowEst;       /* From ANALYZE: Est. rows selected by each column */
  1.1950 +  LogEst *aiRowLogEst;     /* From ANALYZE: Est. rows selected by each column */
  1.1951    Table *pTable;           /* The SQL table being indexed */
  1.1952    char *zColAff;           /* String defining the affinity of each column */
  1.1953    Index *pNext;            /* The next index associated with the same table */
  1.1954 @@ -10899,7 +11171,7 @@
  1.1955    u16 nKeyCol;             /* Number of columns forming the key */
  1.1956    u16 nColumn;             /* Number of columns stored in the index */
  1.1957    u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  1.1958 -  unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
  1.1959 +  unsigned idxType:2;      /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
  1.1960    unsigned bUnordered:1;   /* Use this index for == or IN queries only */
  1.1961    unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
  1.1962    unsigned isResized:1;    /* True if resizeIndexObject() has been called */
  1.1963 @@ -10913,6 +11185,19 @@
  1.1964  };
  1.1965  
  1.1966  /*
  1.1967 +** Allowed values for Index.idxType
  1.1968 +*/
  1.1969 +#define SQLITE_IDXTYPE_APPDEF      0   /* Created using CREATE INDEX */
  1.1970 +#define SQLITE_IDXTYPE_UNIQUE      1   /* Implements a UNIQUE constraint */
  1.1971 +#define SQLITE_IDXTYPE_PRIMARYKEY  2   /* Is the PRIMARY KEY for the table */
  1.1972 +
  1.1973 +/* Return true if index X is a PRIMARY KEY index */
  1.1974 +#define IsPrimaryKeyIndex(X)  ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
  1.1975 +
  1.1976 +/* Return true if index X is a UNIQUE index */
  1.1977 +#define IsUniqueIndex(X)      ((X)->onError!=OE_None)
  1.1978 +
  1.1979 +/*
  1.1980  ** Each sample stored in the sqlite_stat3 table is represented in memory 
  1.1981  ** using a structure of this type.  See documentation at the top of the
  1.1982  ** analyze.c source file for additional information.
  1.1983 @@ -10959,6 +11244,7 @@
  1.1984    int sortingIdx;         /* Cursor number of the sorting index */
  1.1985    int sortingIdxPTab;     /* Cursor number of pseudo-table */
  1.1986    int nSortingColumn;     /* Number of columns in the sorting index */
  1.1987 +  int mnReg, mxReg;       /* Range of registers allocated for aCol and aFunc */
  1.1988    ExprList *pGroupBy;     /* The group by clause */
  1.1989    struct AggInfo_col {    /* For each column used in source tables */
  1.1990      Table *pTab;             /* Source table */
  1.1991 @@ -11115,8 +11401,8 @@
  1.1992  #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
  1.1993  #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
  1.1994  #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
  1.1995 -#define EP_Collate   0x000100 /* Tree contains a TK_COLLATE opeartor */
  1.1996 -      /* unused      0x000200 */
  1.1997 +#define EP_Collate   0x000100 /* Tree contains a TK_COLLATE operator */
  1.1998 +#define EP_Generic   0x000200 /* Ignore COLLATE or affinity on this tree */
  1.1999  #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
  1.2000  #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
  1.2001  #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
  1.2002 @@ -11180,7 +11466,6 @@
  1.2003  */
  1.2004  struct ExprList {
  1.2005    int nExpr;             /* Number of expressions on the list */
  1.2006 -  int iECursor;          /* VDBE Cursor associated with this ExprList */
  1.2007    struct ExprList_item { /* For each expression in the list */
  1.2008      Expr *pExpr;            /* The list of expressions */
  1.2009      char *zName;            /* Token associated with this expression */
  1.2010 @@ -11251,6 +11536,7 @@
  1.2011  ** A bit in a Bitmask
  1.2012  */
  1.2013  #define MASKBIT(n)   (((Bitmask)1)<<(n))
  1.2014 +#define MASKBIT32(n) (((unsigned int)1)<<(n))
  1.2015  
  1.2016  /*
  1.2017  ** The following structure describes the FROM clause of a SELECT statement.
  1.2018 @@ -11272,8 +11558,8 @@
  1.2019  ** contains more than 63 columns and the 64-th or later column is used.
  1.2020  */
  1.2021  struct SrcList {
  1.2022 -  u8 nSrc;        /* Number of tables or subqueries in the FROM clause */
  1.2023 -  u8 nAlloc;      /* Number of entries allocated in a[] below */
  1.2024 +  int nSrc;        /* Number of tables or subqueries in the FROM clause */
  1.2025 +  u32 nAlloc;      /* Number of entries allocated in a[] below */
  1.2026    struct SrcList_item {
  1.2027      Schema *pSchema;  /* Schema to which this item is fixed */
  1.2028      char *zDatabase;  /* Name of database holding this table */
  1.2029 @@ -11283,10 +11569,12 @@
  1.2030      Select *pSelect;  /* A SELECT statement used in place of a table name */
  1.2031      int addrFillSub;  /* Address of subroutine to manifest a subquery */
  1.2032      int regReturn;    /* Register holding return address of addrFillSub */
  1.2033 +    int regResult;    /* Registers holding results of a co-routine */
  1.2034      u8 jointype;      /* Type of join between this able and the previous */
  1.2035      unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
  1.2036      unsigned isCorrelated :1;  /* True if sub-query is correlated */
  1.2037      unsigned viaCoroutine :1;  /* Implemented as a co-routine */
  1.2038 +    unsigned isRecursive :1;   /* True for recursive reference in WITH */
  1.2039  #ifndef SQLITE_OMIT_EXPLAIN
  1.2040      u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
  1.2041  #endif
  1.2042 @@ -11327,6 +11615,8 @@
  1.2043  #define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
  1.2044  #define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
  1.2045  #define WHERE_WANT_DISTINCT    0x0400 /* All output needs to be distinct */
  1.2046 +#define WHERE_SORTBYGROUP      0x0800 /* Support sqlite3WhereIsSorted() */
  1.2047 +#define WHERE_REOPEN_IDX       0x1000 /* Try to use OP_ReopenIdx */
  1.2048  
  1.2049  /* Allowed return values from sqlite3WhereIsDistinct()
  1.2050  */
  1.2051 @@ -11401,7 +11691,7 @@
  1.2052    u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
  1.2053    u16 selFlags;          /* Various SF_* values */
  1.2054    int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
  1.2055 -  int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
  1.2056 +  int addrOpenEphm[2];   /* OP_OpenEphem opcodes related to this select */
  1.2057    u64 nSelectRow;        /* Estimated number of result rows */
  1.2058    SrcList *pSrc;         /* The FROM clause */
  1.2059    Expr *pWhere;          /* The WHERE clause */
  1.2060 @@ -11410,9 +11700,9 @@
  1.2061    ExprList *pOrderBy;    /* The ORDER BY clause */
  1.2062    Select *pPrior;        /* Prior select in a compound select statement */
  1.2063    Select *pNext;         /* Next select to the left in a compound */
  1.2064 -  Select *pRightmost;    /* Right-most select in a compound select statement */
  1.2065    Expr *pLimit;          /* LIMIT expression. NULL means not used. */
  1.2066    Expr *pOffset;         /* OFFSET expression. NULL means not used. */
  1.2067 +  With *pWith;           /* WITH clause attached to this select. Or NULL. */
  1.2068  };
  1.2069  
  1.2070  /*
  1.2071 @@ -11425,42 +11715,109 @@
  1.2072  #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
  1.2073  #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
  1.2074  #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
  1.2075 -#define SF_UseSorter       0x0040  /* Sort using a sorter */
  1.2076 +                    /*     0x0040  NOT USED */
  1.2077  #define SF_Values          0x0080  /* Synthesized from VALUES clause */
  1.2078 -#define SF_Materialize     0x0100  /* Force materialization of views */
  1.2079 +                    /*     0x0100  NOT USED */
  1.2080  #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
  1.2081  #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
  1.2082 -
  1.2083 -
  1.2084 -/*
  1.2085 -** The results of a select can be distributed in several ways.  The
  1.2086 -** "SRT" prefix means "SELECT Result Type".
  1.2087 +#define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
  1.2088 +#define SF_Compound        0x1000  /* Part of a compound query */
  1.2089 +
  1.2090 +
  1.2091 +/*
  1.2092 +** The results of a SELECT can be distributed in several ways, as defined
  1.2093 +** by one of the following macros.  The "SRT" prefix means "SELECT Result
  1.2094 +** Type".
  1.2095 +**
  1.2096 +**     SRT_Union       Store results as a key in a temporary index 
  1.2097 +**                     identified by pDest->iSDParm.
  1.2098 +**
  1.2099 +**     SRT_Except      Remove results from the temporary index pDest->iSDParm.
  1.2100 +**
  1.2101 +**     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
  1.2102 +**                     set is not empty.
  1.2103 +**
  1.2104 +**     SRT_Discard     Throw the results away.  This is used by SELECT
  1.2105 +**                     statements within triggers whose only purpose is
  1.2106 +**                     the side-effects of functions.
  1.2107 +**
  1.2108 +** All of the above are free to ignore their ORDER BY clause. Those that
  1.2109 +** follow must honor the ORDER BY clause.
  1.2110 +**
  1.2111 +**     SRT_Output      Generate a row of output (using the OP_ResultRow
  1.2112 +**                     opcode) for each row in the result set.
  1.2113 +**
  1.2114 +**     SRT_Mem         Only valid if the result is a single column.
  1.2115 +**                     Store the first column of the first result row
  1.2116 +**                     in register pDest->iSDParm then abandon the rest
  1.2117 +**                     of the query.  This destination implies "LIMIT 1".
  1.2118 +**
  1.2119 +**     SRT_Set         The result must be a single column.  Store each
  1.2120 +**                     row of result as the key in table pDest->iSDParm. 
  1.2121 +**                     Apply the affinity pDest->affSdst before storing
  1.2122 +**                     results.  Used to implement "IN (SELECT ...)".
  1.2123 +**
  1.2124 +**     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
  1.2125 +**                     the result there. The cursor is left open after
  1.2126 +**                     returning.  This is like SRT_Table except that
  1.2127 +**                     this destination uses OP_OpenEphemeral to create
  1.2128 +**                     the table first.
  1.2129 +**
  1.2130 +**     SRT_Coroutine   Generate a co-routine that returns a new row of
  1.2131 +**                     results each time it is invoked.  The entry point
  1.2132 +**                     of the co-routine is stored in register pDest->iSDParm
  1.2133 +**                     and the result row is stored in pDest->nDest registers
  1.2134 +**                     starting with pDest->iSdst.
  1.2135 +**
  1.2136 +**     SRT_Table       Store results in temporary table pDest->iSDParm.
  1.2137 +**     SRT_Fifo        This is like SRT_EphemTab except that the table
  1.2138 +**                     is assumed to already be open.  SRT_Fifo has
  1.2139 +**                     the additional property of being able to ignore
  1.2140 +**                     the ORDER BY clause.
  1.2141 +**
  1.2142 +**     SRT_DistFifo    Store results in a temporary table pDest->iSDParm.
  1.2143 +**                     But also use temporary table pDest->iSDParm+1 as
  1.2144 +**                     a record of all prior results and ignore any duplicate
  1.2145 +**                     rows.  Name means:  "Distinct Fifo".
  1.2146 +**
  1.2147 +**     SRT_Queue       Store results in priority queue pDest->iSDParm (really
  1.2148 +**                     an index).  Append a sequence number so that all entries
  1.2149 +**                     are distinct.
  1.2150 +**
  1.2151 +**     SRT_DistQueue   Store results in priority queue pDest->iSDParm only if
  1.2152 +**                     the same record has never been stored before.  The
  1.2153 +**                     index at pDest->iSDParm+1 hold all prior stores.
  1.2154  */
  1.2155  #define SRT_Union        1  /* Store result as keys in an index */
  1.2156  #define SRT_Except       2  /* Remove result from a UNION index */
  1.2157  #define SRT_Exists       3  /* Store 1 if the result is not empty */
  1.2158  #define SRT_Discard      4  /* Do not save the results anywhere */
  1.2159 +#define SRT_Fifo         5  /* Store result as data with an automatic rowid */
  1.2160 +#define SRT_DistFifo     6  /* Like SRT_Fifo, but unique results only */
  1.2161 +#define SRT_Queue        7  /* Store result in an queue */
  1.2162 +#define SRT_DistQueue    8  /* Like SRT_Queue, but unique results only */
  1.2163  
  1.2164  /* The ORDER BY clause is ignored for all of the above */
  1.2165 -#define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
  1.2166 -
  1.2167 -#define SRT_Output       5  /* Output each row of result */
  1.2168 -#define SRT_Mem          6  /* Store result in a memory cell */
  1.2169 -#define SRT_Set          7  /* Store results as keys in an index */
  1.2170 -#define SRT_Table        8  /* Store result as data with an automatic rowid */
  1.2171 -#define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
  1.2172 -#define SRT_Coroutine   10  /* Generate a single row of result */
  1.2173 +#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
  1.2174 +
  1.2175 +#define SRT_Output       9  /* Output each row of result */
  1.2176 +#define SRT_Mem         10  /* Store result in a memory cell */
  1.2177 +#define SRT_Set         11  /* Store results as keys in an index */
  1.2178 +#define SRT_EphemTab    12  /* Create transient tab and store like SRT_Table */
  1.2179 +#define SRT_Coroutine   13  /* Generate a single row of result */
  1.2180 +#define SRT_Table       14  /* Store result as data with an automatic rowid */
  1.2181  
  1.2182  /*
  1.2183  ** An instance of this object describes where to put of the results of
  1.2184  ** a SELECT statement.
  1.2185  */
  1.2186  struct SelectDest {
  1.2187 -  u8 eDest;         /* How to dispose of the results.  On of SRT_* above. */
  1.2188 -  char affSdst;     /* Affinity used when eDest==SRT_Set */
  1.2189 -  int iSDParm;      /* A parameter used by the eDest disposal method */
  1.2190 -  int iSdst;        /* Base register where results are written */
  1.2191 -  int nSdst;        /* Number of registers allocated */
  1.2192 +  u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
  1.2193 +  char affSdst;        /* Affinity used when eDest==SRT_Set */
  1.2194 +  int iSDParm;         /* A parameter used by the eDest disposal method */
  1.2195 +  int iSdst;           /* Base register where results are written */
  1.2196 +  int nSdst;           /* Number of registers allocated */
  1.2197 +  ExprList *pOrderBy;  /* Key columns for SRT_Queue and SRT_DistQueue */
  1.2198  };
  1.2199  
  1.2200  /*
  1.2201 @@ -11516,9 +11873,19 @@
  1.2202  ** The yDbMask datatype for the bitmask of all attached databases.
  1.2203  */
  1.2204  #if SQLITE_MAX_ATTACHED>30
  1.2205 -  typedef sqlite3_uint64 yDbMask;
  1.2206 +  typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
  1.2207 +# define DbMaskTest(M,I)    (((M)[(I)/8]&(1<<((I)&7)))!=0)
  1.2208 +# define DbMaskZero(M)      memset((M),0,sizeof(M))
  1.2209 +# define DbMaskSet(M,I)     (M)[(I)/8]|=(1<<((I)&7))
  1.2210 +# define DbMaskAllZero(M)   sqlite3DbMaskAllZero(M)
  1.2211 +# define DbMaskNonZero(M)   (sqlite3DbMaskAllZero(M)==0)
  1.2212  #else
  1.2213    typedef unsigned int yDbMask;
  1.2214 +# define DbMaskTest(M,I)    (((M)&(((yDbMask)1)<<(I)))!=0)
  1.2215 +# define DbMaskZero(M)      (M)=0
  1.2216 +# define DbMaskSet(M,I)     (M)|=(((yDbMask)1)<<(I))
  1.2217 +# define DbMaskAllZero(M)   (M)==0
  1.2218 +# define DbMaskNonZero(M)   (M)!=0
  1.2219  #endif
  1.2220  
  1.2221  /*
  1.2222 @@ -11546,12 +11913,10 @@
  1.2223    u8 checkSchema;      /* Causes schema cookie check after an error */
  1.2224    u8 nested;           /* Number of nested calls to the parser/code generator */
  1.2225    u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
  1.2226 -  u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
  1.2227 -  u8 nColCache;        /* Number of entries in aColCache[] */
  1.2228 -  u8 iColCache;        /* Next entry in aColCache[] to replace */
  1.2229    u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
  1.2230    u8 mayAbort;         /* True if statement may throw an ABORT exception */
  1.2231    u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
  1.2232 +  u8 okConstFactor;    /* OK to factor out constants */
  1.2233    int aTempReg[8];     /* Holding area for temporary registers */
  1.2234    int nRangeReg;       /* Size of the temporary register block */
  1.2235    int iRangeReg;       /* First register in temporary register block */
  1.2236 @@ -11560,27 +11925,30 @@
  1.2237    int nMem;            /* Number of memory cells used so far */
  1.2238    int nSet;            /* Number of sets used so far */
  1.2239    int nOnce;           /* Number of OP_Once instructions so far */
  1.2240 +  int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
  1.2241 +  int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
  1.2242    int ckBase;          /* Base register of data during check constraints */
  1.2243    int iPartIdxTab;     /* Table corresponding to a partial index */
  1.2244    int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
  1.2245    int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
  1.2246 +  int nLabel;          /* Number of labels used */
  1.2247 +  int *aLabel;         /* Space to hold the labels */
  1.2248    struct yColCache {
  1.2249      int iTable;           /* Table cursor number */
  1.2250 -    int iColumn;          /* Table column number */
  1.2251 +    i16 iColumn;          /* Table column number */
  1.2252      u8 tempReg;           /* iReg is a temp register that needs to be freed */
  1.2253      int iLevel;           /* Nesting level */
  1.2254      int iReg;             /* Reg with value of this column. 0 means none. */
  1.2255      int lru;              /* Least recently used entry has the smallest value */
  1.2256    } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
  1.2257    ExprList *pConstExpr;/* Constant expressions */
  1.2258 +  Token constraintName;/* Name of the constraint currently being parsed */
  1.2259    yDbMask writeMask;   /* Start a write transaction on these databases */
  1.2260    yDbMask cookieMask;  /* Bitmask of schema verified databases */
  1.2261 -  int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
  1.2262    int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
  1.2263    int regRowid;        /* Register holding rowid of CREATE TABLE entry */
  1.2264    int regRoot;         /* Register holding root page number for new objects */
  1.2265    int nMaxArg;         /* Max args passed to user function by sub-program */
  1.2266 -  Token constraintName;/* Name of the constraint currently being parsed */
  1.2267  #ifndef SQLITE_OMIT_SHARED_CACHE
  1.2268    int nTableLock;        /* Number of locks in aTableLock */
  1.2269    TableLock *aTableLock; /* Required table locks for shared-cache mode */
  1.2270 @@ -11599,12 +11967,17 @@
  1.2271    u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
  1.2272    u8 disableTriggers;  /* True to disable triggers */
  1.2273  
  1.2274 -  /* Above is constant between recursions.  Below is reset before and after
  1.2275 -  ** each recursion */
  1.2276 +  /************************************************************************
  1.2277 +  ** Above is constant between recursions.  Below is reset before and after
  1.2278 +  ** each recursion.  The boundary between these two regions is determined
  1.2279 +  ** using offsetof(Parse,nVar) so the nVar field must be the first field
  1.2280 +  ** in the recursive region.
  1.2281 +  ************************************************************************/
  1.2282  
  1.2283    int nVar;                 /* Number of '?' variables seen in the SQL so far */
  1.2284    int nzVar;                /* Number of available slots in azVar[] */
  1.2285    u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
  1.2286 +  u8 bFreeWith;             /* True if pWith should be freed with parser */
  1.2287    u8 explain;               /* True if the EXPLAIN flag is found on the query */
  1.2288  #ifndef SQLITE_OMIT_VIRTUALTABLE
  1.2289    u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
  1.2290 @@ -11630,6 +12003,7 @@
  1.2291  #endif
  1.2292    Table *pZombieTab;        /* List of Table objects to delete after code gen */
  1.2293    TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
  1.2294 +  With *pWith;              /* Current WITH clause, or NULL */
  1.2295  };
  1.2296  
  1.2297  /*
  1.2298 @@ -11749,7 +12123,7 @@
  1.2299    Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
  1.2300    Token target;        /* Target table for DELETE, UPDATE, INSERT */
  1.2301    Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
  1.2302 -  ExprList *pExprList; /* SET clause for UPDATE.  VALUES clause for INSERT */
  1.2303 +  ExprList *pExprList; /* SET clause for UPDATE. */
  1.2304    IdList *pIdList;     /* Column names for INSERT */
  1.2305    TriggerStep *pNext;  /* Next in the link-list */
  1.2306    TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
  1.2307 @@ -11836,15 +12210,25 @@
  1.2308    int isMutexInit;                  /* True after mutexes are initialized */
  1.2309    int isMallocInit;                 /* True after malloc is initialized */
  1.2310    int isPCacheInit;                 /* True after malloc is initialized */
  1.2311 +  int nRefInitMutex;                /* Number of users of pInitMutex */
  1.2312    sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
  1.2313 -  int nRefInitMutex;                /* Number of users of pInitMutex */
  1.2314    void (*xLog)(void*,int,const char*); /* Function for logging */
  1.2315    void *pLogArg;                       /* First argument to xLog() */
  1.2316 -  int bLocaltimeFault;              /* True to fail localtime() calls */
  1.2317  #ifdef SQLITE_ENABLE_SQLLOG
  1.2318    void(*xSqllog)(void*,sqlite3*,const char*, int);
  1.2319    void *pSqllogArg;
  1.2320  #endif
  1.2321 +#ifdef SQLITE_VDBE_COVERAGE
  1.2322 +  /* The following callback (if not NULL) is invoked on every VDBE branch
  1.2323 +  ** operation.  Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
  1.2324 +  */
  1.2325 +  void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
  1.2326 +  void *pVdbeBranchArg;                                     /* 1st argument */
  1.2327 +#endif
  1.2328 +#ifndef SQLITE_OMIT_BUILTIN_TEST
  1.2329 +  int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
  1.2330 +#endif
  1.2331 +  int bLocaltimeFault;              /* True to fail localtime() calls */
  1.2332  };
  1.2333  
  1.2334  /*
  1.2335 @@ -11871,9 +12255,9 @@
  1.2336  struct Walker {
  1.2337    int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
  1.2338    int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
  1.2339 +  void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
  1.2340    Parse *pParse;                            /* Parser context.  */
  1.2341    int walkerDepth;                          /* Number of subqueries */
  1.2342 -  u8 bSelectDepthFirst;                     /* Do subqueries first */
  1.2343    union {                                   /* Extra data for callback */
  1.2344      NameContext *pNC;                          /* Naming context */
  1.2345      int i;                                     /* Integer value */
  1.2346 @@ -11898,6 +12282,21 @@
  1.2347  #define WRC_Abort       2   /* Abandon the tree walk */
  1.2348  
  1.2349  /*
  1.2350 +** An instance of this structure represents a set of one or more CTEs
  1.2351 +** (common table expressions) created by a single WITH clause.
  1.2352 +*/
  1.2353 +struct With {
  1.2354 +  int nCte;                       /* Number of CTEs in the WITH clause */
  1.2355 +  With *pOuter;                   /* Containing WITH clause, or NULL */
  1.2356 +  struct Cte {                    /* For each CTE in the WITH clause.... */
  1.2357 +    char *zName;                    /* Name of this CTE */
  1.2358 +    ExprList *pCols;                /* List of explicit column names, or NULL */
  1.2359 +    Select *pSelect;                /* The definition of this CTE */
  1.2360 +    const char *zErr;               /* Error message for circular references */
  1.2361 +  } a[1];
  1.2362 +};
  1.2363 +
  1.2364 +/*
  1.2365  ** Assuming zIn points to the first byte of a UTF-8 character,
  1.2366  ** advance zIn to point to the first byte of the next UTF-8 character.
  1.2367  */
  1.2368 @@ -12036,10 +12435,20 @@
  1.2369  # define sqlite3IsNaN(X)  0
  1.2370  #endif
  1.2371  
  1.2372 -SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
  1.2373 -#ifndef SQLITE_OMIT_TRACE
  1.2374 -SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
  1.2375 -#endif
  1.2376 +/*
  1.2377 +** An instance of the following structure holds information about SQL
  1.2378 +** functions arguments that are the parameters to the printf() function.
  1.2379 +*/
  1.2380 +struct PrintfArguments {
  1.2381 +  int nArg;                /* Total number of arguments */
  1.2382 +  int nUsed;               /* Number of arguments used so far */
  1.2383 +  sqlite3_value **apArg;   /* The argument values */
  1.2384 +};
  1.2385 +
  1.2386 +#define SQLITE_PRINTF_INTERNAL 0x01
  1.2387 +#define SQLITE_PRINTF_SQLFUNC  0x02
  1.2388 +SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
  1.2389 +SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
  1.2390  SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
  1.2391  SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
  1.2392  SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
  1.2393 @@ -12121,6 +12530,12 @@
  1.2394  SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
  1.2395  SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
  1.2396  
  1.2397 +#ifdef SQLITE_OMIT_BUILTIN_TEST
  1.2398 +# define sqlite3FaultSim(X) SQLITE_OK
  1.2399 +#else
  1.2400 +SQLITE_PRIVATE   int sqlite3FaultSim(int);
  1.2401 +#endif
  1.2402 +
  1.2403  SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
  1.2404  SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
  1.2405  SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
  1.2406 @@ -12132,7 +12547,7 @@
  1.2407  SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
  1.2408  SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
  1.2409  SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
  1.2410 -SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
  1.2411 +SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
  1.2412  SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
  1.2413  
  1.2414  SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
  1.2415 @@ -12143,6 +12558,9 @@
  1.2416  # define sqlite3ViewGetColumnNames(A,B) 0
  1.2417  #endif
  1.2418  
  1.2419 +#if SQLITE_MAX_ATTACHED>30
  1.2420 +SQLITE_PRIVATE   int sqlite3DbMaskAllZero(yDbMask);
  1.2421 +#endif
  1.2422  SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
  1.2423  SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
  1.2424  SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
  1.2425 @@ -12153,8 +12571,7 @@
  1.2426  # define sqlite3AutoincrementBegin(X)
  1.2427  # define sqlite3AutoincrementEnd(X)
  1.2428  #endif
  1.2429 -SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
  1.2430 -SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
  1.2431 +SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
  1.2432  SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
  1.2433  SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
  1.2434  SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
  1.2435 @@ -12189,6 +12606,7 @@
  1.2436  SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
  1.2437  SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
  1.2438  SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
  1.2439 +SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
  1.2440  SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
  1.2441  SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
  1.2442  SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
  1.2443 @@ -12197,15 +12615,16 @@
  1.2444  SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
  1.2445  SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
  1.2446  SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
  1.2447 -SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
  1.2448 +SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*);
  1.2449  SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
  1.2450  SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
  1.2451  SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
  1.2452 -SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
  1.2453 +SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
  1.2454 +SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
  1.2455  SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
  1.2456  SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
  1.2457  SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
  1.2458 -SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
  1.2459 +SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
  1.2460  SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
  1.2461  #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
  1.2462  #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
  1.2463 @@ -12229,7 +12648,6 @@
  1.2464  SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
  1.2465  SQLITE_PRIVATE void sqlite3PrngSaveState(void);
  1.2466  SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
  1.2467 -SQLITE_PRIVATE void sqlite3PrngResetState(void);
  1.2468  SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
  1.2469  SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
  1.2470  SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
  1.2471 @@ -12244,12 +12662,12 @@
  1.2472  SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
  1.2473  SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
  1.2474  SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
  1.2475 -SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
  1.2476  SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
  1.2477  SQLITE_PRIVATE int sqlite3IsRowid(const char*);
  1.2478  SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
  1.2479  SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
  1.2480 -SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*);
  1.2481 +SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
  1.2482 +SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
  1.2483  SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
  1.2484                                       u8,u8,int,int*);
  1.2485  SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
  1.2486 @@ -12293,7 +12711,7 @@
  1.2487  SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
  1.2488  SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
  1.2489  SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
  1.2490 -                                        ExprList*,Select*,u8);
  1.2491 +                                        Select*,u8);
  1.2492  SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
  1.2493  SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
  1.2494  SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
  1.2495 @@ -12388,18 +12806,18 @@
  1.2496  
  1.2497  
  1.2498  SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
  1.2499 -SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
  1.2500 +SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
  1.2501  SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
  1.2502  SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
  1.2503  SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
  1.2504  SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
  1.2505 +SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
  1.2506  SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
  1.2507  SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  1.2508  SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
  1.2509  SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  1.2510  
  1.2511 -#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) || \
  1.2512 -    defined(SQLITE_DEBUG_OS_TRACE)
  1.2513 +#if defined(SQLITE_TEST) 
  1.2514  SQLITE_PRIVATE const char *sqlite3ErrName(int);
  1.2515  #endif
  1.2516  
  1.2517 @@ -12408,7 +12826,7 @@
  1.2518  SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
  1.2519  SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
  1.2520  SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
  1.2521 -SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
  1.2522 +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*);
  1.2523  SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
  1.2524  SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
  1.2525  SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
  1.2526 @@ -12423,12 +12841,13 @@
  1.2527  #else
  1.2528  # define sqlite3FileSuffix3(X,Y)
  1.2529  #endif
  1.2530 -SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,int);
  1.2531 +SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
  1.2532  
  1.2533  SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
  1.2534  SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
  1.2535  SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
  1.2536                          void(*)(void*));
  1.2537 +SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
  1.2538  SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
  1.2539  SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
  1.2540  SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
  1.2541 @@ -12494,6 +12913,7 @@
  1.2542  
  1.2543  SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
  1.2544  SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
  1.2545 +SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
  1.2546  SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
  1.2547  SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
  1.2548  SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
  1.2549 @@ -12506,7 +12926,9 @@
  1.2550  #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1.2551  SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
  1.2552  SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
  1.2553 +SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
  1.2554  SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
  1.2555 +SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
  1.2556  #endif
  1.2557  
  1.2558  /*
  1.2559 @@ -12585,6 +13007,14 @@
  1.2560  SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
  1.2561  SQLITE_PRIVATE   int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
  1.2562  #endif
  1.2563 +#ifndef SQLITE_OMIT_CTE
  1.2564 +SQLITE_PRIVATE   With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
  1.2565 +SQLITE_PRIVATE   void sqlite3WithDelete(sqlite3*,With*);
  1.2566 +SQLITE_PRIVATE   void sqlite3WithPush(Parse*, With*, u8);
  1.2567 +#else
  1.2568 +#define sqlite3WithPush(x,y,z)
  1.2569 +#define sqlite3WithDelete(x,y)
  1.2570 +#endif
  1.2571  
  1.2572  /* Declarations for functions in fkey.c. All of these are replaced by
  1.2573  ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
  1.2574 @@ -12635,11 +13065,21 @@
  1.2575    #define sqlite3EndBenignMalloc()
  1.2576  #endif
  1.2577  
  1.2578 -#define IN_INDEX_ROWID           1
  1.2579 -#define IN_INDEX_EPH             2
  1.2580 -#define IN_INDEX_INDEX_ASC       3
  1.2581 -#define IN_INDEX_INDEX_DESC      4
  1.2582 -SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
  1.2583 +/*
  1.2584 +** Allowed return values from sqlite3FindInIndex()
  1.2585 +*/
  1.2586 +#define IN_INDEX_ROWID        1   /* Search the rowid of the table */
  1.2587 +#define IN_INDEX_EPH          2   /* Search an ephemeral b-tree */
  1.2588 +#define IN_INDEX_INDEX_ASC    3   /* Existing index ASCENDING */
  1.2589 +#define IN_INDEX_INDEX_DESC   4   /* Existing index DESCENDING */
  1.2590 +#define IN_INDEX_NOOP         5   /* No table available. Use comparisons */
  1.2591 +/*
  1.2592 +** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
  1.2593 +*/
  1.2594 +#define IN_INDEX_NOOP_OK     0x0001  /* OK to return IN_INDEX_NOOP */
  1.2595 +#define IN_INDEX_MEMBERSHIP  0x0002  /* IN operator used for membership test */
  1.2596 +#define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
  1.2597 +SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
  1.2598  
  1.2599  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  1.2600  SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
  1.2601 @@ -12917,15 +13357,22 @@
  1.2602     0,                         /* isMutexInit */
  1.2603     0,                         /* isMallocInit */
  1.2604     0,                         /* isPCacheInit */
  1.2605 +   0,                         /* nRefInitMutex */
  1.2606     0,                         /* pInitMutex */
  1.2607 -   0,                         /* nRefInitMutex */
  1.2608     0,                         /* xLog */
  1.2609     0,                         /* pLogArg */
  1.2610 -   0,                         /* bLocaltimeFault */
  1.2611  #ifdef SQLITE_ENABLE_SQLLOG
  1.2612     0,                         /* xSqllog */
  1.2613 -   0                          /* pSqllogArg */
  1.2614 -#endif
  1.2615 +   0,                         /* pSqllogArg */
  1.2616 +#endif
  1.2617 +#ifdef SQLITE_VDBE_COVERAGE
  1.2618 +   0,                         /* xVdbeBranch */
  1.2619 +   0,                         /* pVbeBranchArg */
  1.2620 +#endif
  1.2621 +#ifndef SQLITE_OMIT_BUILTIN_TEST
  1.2622 +   0,                         /* xTestCallback */
  1.2623 +#endif
  1.2624 +   0                          /* bLocaltimeFault */
  1.2625  };
  1.2626  
  1.2627  /*
  1.2628 @@ -13192,6 +13639,9 @@
  1.2629  #ifdef SQLITE_OMIT_COMPOUND_SELECT
  1.2630    "OMIT_COMPOUND_SELECT",
  1.2631  #endif
  1.2632 +#ifdef SQLITE_OMIT_CTE
  1.2633 +  "OMIT_CTE",
  1.2634 +#endif
  1.2635  #ifdef SQLITE_OMIT_DATETIME_FUNCS
  1.2636    "OMIT_DATETIME_FUNCS",
  1.2637  #endif
  1.2638 @@ -13476,14 +13926,18 @@
  1.2639    int pseudoTableReg;   /* Register holding pseudotable content. */
  1.2640    i16 nField;           /* Number of fields in the header */
  1.2641    u16 nHdrParsed;       /* Number of header fields parsed so far */
  1.2642 +#ifdef SQLITE_DEBUG
  1.2643 +  u8 seekOp;            /* Most recent seek operation on this cursor */
  1.2644 +#endif
  1.2645    i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
  1.2646    u8 nullRow;           /* True if pointing to a row with no data */
  1.2647    u8 rowidIsValid;      /* True if lastRowid is valid */
  1.2648    u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
  1.2649 +  Bool isEphemeral:1;   /* True for an ephemeral table */
  1.2650    Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
  1.2651    Bool isTable:1;       /* True if a table requiring integer keys */
  1.2652    Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
  1.2653 -  Bool multiPseudo:1;   /* Multi-register pseudo-cursor */
  1.2654 +  Pgno pgnoRoot;        /* Root page of the open btree cursor */
  1.2655    sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
  1.2656    i64 seqCount;         /* Sequence counter */
  1.2657    i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
  1.2658 @@ -13577,7 +14031,6 @@
  1.2659    } u;
  1.2660    int n;              /* Number of characters in string value, excluding '\0' */
  1.2661    u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
  1.2662 -  u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
  1.2663    u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
  1.2664  #ifdef SQLITE_DEBUG
  1.2665    Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
  1.2666 @@ -13604,9 +14057,10 @@
  1.2667  #define MEM_Int       0x0004   /* Value is an integer */
  1.2668  #define MEM_Real      0x0008   /* Value is a real number */
  1.2669  #define MEM_Blob      0x0010   /* Value is a BLOB */
  1.2670 +#define MEM_AffMask   0x001f   /* Mask of affinity bits */
  1.2671  #define MEM_RowSet    0x0020   /* Value is a RowSet object */
  1.2672  #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
  1.2673 -#define MEM_Invalid   0x0080   /* Value is undefined */
  1.2674 +#define MEM_Undefined 0x0080   /* Value is undefined */
  1.2675  #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
  1.2676  #define MEM_TypeMask  0x01ff   /* Mask of type bits */
  1.2677  
  1.2678 @@ -13617,7 +14071,7 @@
  1.2679  ** string is \000 or \u0000 terminated
  1.2680  */
  1.2681  #define MEM_Term      0x0200   /* String rep is nul terminated */
  1.2682 -#define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
  1.2683 +#define MEM_Dyn       0x0400   /* Need to call Mem.xDel() on Mem.z */
  1.2684  #define MEM_Static    0x0800   /* Mem.z points to a static string */
  1.2685  #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
  1.2686  #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
  1.2687 @@ -13638,7 +14092,7 @@
  1.2688  ** is for use inside assert() statements only.
  1.2689  */
  1.2690  #ifdef SQLITE_DEBUG
  1.2691 -#define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
  1.2692 +#define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
  1.2693  #endif
  1.2694  
  1.2695  /*
  1.2696 @@ -13720,12 +14174,9 @@
  1.2697    Mem **apArg;            /* Arguments to currently executing user function */
  1.2698    Mem *aColName;          /* Column names to return */
  1.2699    Mem *pResultSet;        /* Pointer to an array of results */
  1.2700 +  Parse *pParse;          /* Parsing context used to create this Vdbe */
  1.2701    int nMem;               /* Number of memory locations currently allocated */
  1.2702    int nOp;                /* Number of instructions in the program */
  1.2703 -  int nOpAlloc;           /* Number of slots allocated for aOp[] */
  1.2704 -  int nLabel;             /* Number of labels used */
  1.2705 -  int *aLabel;            /* Space to hold the labels */
  1.2706 -  u16 nResColumn;         /* Number of columns in one row of the result set */
  1.2707    int nCursor;            /* Number of slots in apCsr[] */
  1.2708    u32 magic;              /* Magic number for sanity checking */
  1.2709    char *zErrMsg;          /* Error message written here */
  1.2710 @@ -13738,6 +14189,7 @@
  1.2711    u32 cacheCtr;           /* VdbeCursor row cache generation counter */
  1.2712    int pc;                 /* The program counter */
  1.2713    int rc;                 /* Value to return */
  1.2714 +  u16 nResColumn;         /* Number of columns in one row of the result set */
  1.2715    u8 errorAction;         /* Recovery action to do in case of an error */
  1.2716    u8 minWriteFileFormat;  /* Minimum file format for writable database files */
  1.2717    bft explain:2;          /* True if EXPLAIN present on SQL command */
  1.2718 @@ -13797,14 +14249,13 @@
  1.2719  #endif
  1.2720  SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
  1.2721  SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
  1.2722 -SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
  1.2723 +SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
  1.2724  SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
  1.2725  SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
  1.2726  
  1.2727  int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
  1.2728  SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
  1.2729  SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
  1.2730 -SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
  1.2731  SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
  1.2732  SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
  1.2733  SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
  1.2734 @@ -13835,19 +14286,20 @@
  1.2735  SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
  1.2736  SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
  1.2737  SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
  1.2738 +#define VdbeMemDynamic(X)  \
  1.2739 +  (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
  1.2740  #define VdbeMemRelease(X)  \
  1.2741 -  if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
  1.2742 -    sqlite3VdbeMemReleaseExternal(X);
  1.2743 +  if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
  1.2744  SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
  1.2745  SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
  1.2746  SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
  1.2747  SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
  1.2748  SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
  1.2749  SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
  1.2750 -SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
  1.2751  SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
  1.2752  
  1.2753  SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
  1.2754 +SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
  1.2755  SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
  1.2756  SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
  1.2757  SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
  1.2758 @@ -13865,6 +14317,7 @@
  1.2759  
  1.2760  #ifdef SQLITE_DEBUG
  1.2761  SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
  1.2762 +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
  1.2763  #endif
  1.2764  
  1.2765  #ifndef SQLITE_OMIT_FOREIGN_KEY
  1.2766 @@ -15387,7 +15840,21 @@
  1.2767  ** routine has no return value since the return value would be meaningless.
  1.2768  */
  1.2769  SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
  1.2770 -  DO_OS_MALLOC_TEST(id);
  1.2771 +#ifdef SQLITE_TEST
  1.2772 +  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
  1.2773 +    /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
  1.2774 +    ** is using a regular VFS, it is called after the corresponding 
  1.2775 +    ** transaction has been committed. Injecting a fault at this point 
  1.2776 +    ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
  1.2777 +    ** but the transaction is committed anyway.
  1.2778 +    **
  1.2779 +    ** The core must call OsFileControl() though, not OsFileControlHint(),
  1.2780 +    ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
  1.2781 +    ** means the commit really has failed and an error should be returned
  1.2782 +    ** to the user.  */
  1.2783 +    DO_OS_MALLOC_TEST(id);
  1.2784 +  }
  1.2785 +#endif
  1.2786    return id->pMethods->xFileControl(id, op, pArg);
  1.2787  }
  1.2788  SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
  1.2789 @@ -17572,7 +18039,7 @@
  1.2790    ** block.  If not, then split a block of the next larger power of
  1.2791    ** two in order to create a new free block of size iLogsize.
  1.2792    */
  1.2793 -  for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
  1.2794 +  for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
  1.2795    if( iBin>LOGMAX ){
  1.2796      testcase( sqlite3GlobalConfig.xLog!=0 );
  1.2797      sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
  1.2798 @@ -17599,6 +18066,12 @@
  1.2799    if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
  1.2800    if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
  1.2801  
  1.2802 +#ifdef SQLITE_DEBUG
  1.2803 +  /* Make sure the allocated memory does not assume that it is set to zero
  1.2804 +  ** or retains a value from a previous allocation */
  1.2805 +  memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
  1.2806 +#endif
  1.2807 +
  1.2808    /* Return a pointer to the allocated memory. */
  1.2809    return (void*)&mem5.zPool[i*mem5.szAtom];
  1.2810  }
  1.2811 @@ -17656,6 +18129,13 @@
  1.2812      }
  1.2813      size *= 2;
  1.2814    }
  1.2815 +
  1.2816 +#ifdef SQLITE_DEBUG
  1.2817 +  /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
  1.2818 +  ** not used after being freed */
  1.2819 +  memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
  1.2820 +#endif
  1.2821 +
  1.2822    memsys5Link(iBlock, iLogsize);
  1.2823  }
  1.2824  
  1.2825 @@ -17970,7 +18450,7 @@
  1.2826  */
  1.2827  SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
  1.2828  #ifndef SQLITE_OMIT_AUTOINIT
  1.2829 -  if( sqlite3_initialize() ) return 0;
  1.2830 +  if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
  1.2831  #endif
  1.2832    return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
  1.2833  }
  1.2834 @@ -18151,7 +18631,7 @@
  1.2835  ** that means that a mutex could not be allocated. 
  1.2836  */
  1.2837  static sqlite3_mutex *debugMutexAlloc(int id){
  1.2838 -  static sqlite3_debug_mutex aStatic[6];
  1.2839 +  static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_APP3 - 1];
  1.2840    sqlite3_debug_mutex *pNew = 0;
  1.2841    switch( id ){
  1.2842      case SQLITE_MUTEX_FAST:
  1.2843 @@ -18348,10 +18828,13 @@
  1.2844  ** <li>  SQLITE_MUTEX_RECURSIVE
  1.2845  ** <li>  SQLITE_MUTEX_STATIC_MASTER
  1.2846  ** <li>  SQLITE_MUTEX_STATIC_MEM
  1.2847 -** <li>  SQLITE_MUTEX_STATIC_MEM2
  1.2848 +** <li>  SQLITE_MUTEX_STATIC_OPEN
  1.2849  ** <li>  SQLITE_MUTEX_STATIC_PRNG
  1.2850  ** <li>  SQLITE_MUTEX_STATIC_LRU
  1.2851  ** <li>  SQLITE_MUTEX_STATIC_PMEM
  1.2852 +** <li>  SQLITE_MUTEX_STATIC_APP1
  1.2853 +** <li>  SQLITE_MUTEX_STATIC_APP2
  1.2854 +** <li>  SQLITE_MUTEX_STATIC_APP3
  1.2855  ** </ul>
  1.2856  **
  1.2857  ** The first two constants cause sqlite3_mutex_alloc() to create
  1.2858 @@ -18385,6 +18868,9 @@
  1.2859      SQLITE3_MUTEX_INITIALIZER,
  1.2860      SQLITE3_MUTEX_INITIALIZER,
  1.2861      SQLITE3_MUTEX_INITIALIZER,
  1.2862 +    SQLITE3_MUTEX_INITIALIZER,
  1.2863 +    SQLITE3_MUTEX_INITIALIZER,
  1.2864 +    SQLITE3_MUTEX_INITIALIZER,
  1.2865      SQLITE3_MUTEX_INITIALIZER
  1.2866    };
  1.2867    sqlite3_mutex *p;
  1.2868 @@ -18615,12 +19101,303 @@
  1.2869  **    May you share freely, never taking more than you give.
  1.2870  **
  1.2871  *************************************************************************
  1.2872 -** This file contains the C functions that implement mutexes for win32
  1.2873 -*/
  1.2874 +** This file contains the C functions that implement mutexes for Win32.
  1.2875 +*/
  1.2876 +
  1.2877 +#if SQLITE_OS_WIN
  1.2878 +/*
  1.2879 +** Include code that is common to all os_*.c files
  1.2880 +*/
  1.2881 +/************** Include os_common.h in the middle of mutex_w32.c *************/
  1.2882 +/************** Begin file os_common.h ***************************************/
  1.2883 +/*
  1.2884 +** 2004 May 22
  1.2885 +**
  1.2886 +** The author disclaims copyright to this source code.  In place of
  1.2887 +** a legal notice, here is a blessing:
  1.2888 +**
  1.2889 +**    May you do good and not evil.
  1.2890 +**    May you find forgiveness for yourself and forgive others.
  1.2891 +**    May you share freely, never taking more than you give.
  1.2892 +**
  1.2893 +******************************************************************************
  1.2894 +**
  1.2895 +** This file contains macros and a little bit of code that is common to
  1.2896 +** all of the platform-specific files (os_*.c) and is #included into those
  1.2897 +** files.
  1.2898 +**
  1.2899 +** This file should be #included by the os_*.c files only.  It is not a
  1.2900 +** general purpose header file.
  1.2901 +*/
  1.2902 +#ifndef _OS_COMMON_H_
  1.2903 +#define _OS_COMMON_H_
  1.2904 +
  1.2905 +/*
  1.2906 +** At least two bugs have slipped in because we changed the MEMORY_DEBUG
  1.2907 +** macro to SQLITE_DEBUG and some older makefiles have not yet made the
  1.2908 +** switch.  The following code should catch this problem at compile-time.
  1.2909 +*/
  1.2910 +#ifdef MEMORY_DEBUG
  1.2911 +# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
  1.2912 +#endif
  1.2913 +
  1.2914 +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
  1.2915 +# ifndef SQLITE_DEBUG_OS_TRACE
  1.2916 +#   define SQLITE_DEBUG_OS_TRACE 0
  1.2917 +# endif
  1.2918 +  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
  1.2919 +# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
  1.2920 +#else
  1.2921 +# define OSTRACE(X)
  1.2922 +#endif
  1.2923 +
  1.2924 +/*
  1.2925 +** Macros for performance tracing.  Normally turned off.  Only works
  1.2926 +** on i486 hardware.
  1.2927 +*/
  1.2928 +#ifdef SQLITE_PERFORMANCE_TRACE
  1.2929 +
  1.2930 +/* 
  1.2931 +** hwtime.h contains inline assembler code for implementing 
  1.2932 +** high-performance timing routines.
  1.2933 +*/
  1.2934 +/************** Include hwtime.h in the middle of os_common.h ****************/
  1.2935 +/************** Begin file hwtime.h ******************************************/
  1.2936 +/*
  1.2937 +** 2008 May 27
  1.2938 +**
  1.2939 +** The author disclaims copyright to this source code.  In place of
  1.2940 +** a legal notice, here is a blessing:
  1.2941 +**
  1.2942 +**    May you do good and not evil.
  1.2943 +**    May you find forgiveness for yourself and forgive others.
  1.2944 +**    May you share freely, never taking more than you give.
  1.2945 +**
  1.2946 +******************************************************************************
  1.2947 +**
  1.2948 +** This file contains inline asm code for retrieving "high-performance"
  1.2949 +** counters for x86 class CPUs.
  1.2950 +*/
  1.2951 +#ifndef _HWTIME_H_
  1.2952 +#define _HWTIME_H_
  1.2953 +
  1.2954 +/*
  1.2955 +** The following routine only works on pentium-class (or newer) processors.
  1.2956 +** It uses the RDTSC opcode to read the cycle count value out of the
  1.2957 +** processor and returns that value.  This can be used for high-res
  1.2958 +** profiling.
  1.2959 +*/
  1.2960 +#if (defined(__GNUC__) || defined(_MSC_VER)) && \
  1.2961 +      (defined(i386) || defined(__i386__) || defined(_M_IX86))
  1.2962 +
  1.2963 +  #if defined(__GNUC__)
  1.2964 +
  1.2965 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
  1.2966 +     unsigned int lo, hi;
  1.2967 +     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
  1.2968 +     return (sqlite_uint64)hi << 32 | lo;
  1.2969 +  }
  1.2970 +
  1.2971 +  #elif defined(_MSC_VER)
  1.2972 +
  1.2973 +  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
  1.2974 +     __asm {
  1.2975 +        rdtsc
  1.2976 +        ret       ; return value at EDX:EAX
  1.2977 +     }
  1.2978 +  }
  1.2979 +
  1.2980 +  #endif
  1.2981 +
  1.2982 +#elif (defined(__GNUC__) && defined(__x86_64__))
  1.2983 +
  1.2984 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
  1.2985 +      unsigned long val;
  1.2986 +      __asm__ __volatile__ ("rdtsc" : "=A" (val));
  1.2987 +      return val;
  1.2988 +  }
  1.2989 + 
  1.2990 +#elif (defined(__GNUC__) && defined(__ppc__))
  1.2991 +
  1.2992 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
  1.2993 +      unsigned long long retval;
  1.2994 +      unsigned long junk;
  1.2995 +      __asm__ __volatile__ ("\n\
  1.2996 +          1:      mftbu   %1\n\
  1.2997 +                  mftb    %L0\n\
  1.2998 +                  mftbu   %0\n\
  1.2999 +                  cmpw    %0,%1\n\
  1.3000 +                  bne     1b"
  1.3001 +                  : "=r" (retval), "=r" (junk));
  1.3002 +      return retval;
  1.3003 +  }
  1.3004 +
  1.3005 +#else
  1.3006 +
  1.3007 +  #error Need implementation of sqlite3Hwtime() for your platform.
  1.3008 +
  1.3009 +  /*
  1.3010 +  ** To compile without implementing sqlite3Hwtime() for your platform,
  1.3011 +  ** you can remove the above #error and use the following
  1.3012 +  ** stub function.  You will lose timing support for many
  1.3013 +  ** of the debugging and testing utilities, but it should at
  1.3014 +  ** least compile and run.
  1.3015 +  */
  1.3016 +SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
  1.3017 +
  1.3018 +#endif
  1.3019 +
  1.3020 +#endif /* !defined(_HWTIME_H_) */
  1.3021 +
  1.3022 +/************** End of hwtime.h **********************************************/
  1.3023 +/************** Continuing where we left off in os_common.h ******************/
  1.3024 +
  1.3025 +static sqlite_uint64 g_start;
  1.3026 +static sqlite_uint64 g_elapsed;
  1.3027 +#define TIMER_START       g_start=sqlite3Hwtime()
  1.3028 +#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
  1.3029 +#define TIMER_ELAPSED     g_elapsed
  1.3030 +#else
  1.3031 +#define TIMER_START
  1.3032 +#define TIMER_END
  1.3033 +#define TIMER_ELAPSED     ((sqlite_uint64)0)
  1.3034 +#endif
  1.3035 +
  1.3036 +/*
  1.3037 +** If we compile with the SQLITE_TEST macro set, then the following block
  1.3038 +** of code will give us the ability to simulate a disk I/O error.  This
  1.3039 +** is used for testing the I/O recovery logic.
  1.3040 +*/
  1.3041 +#ifdef SQLITE_TEST
  1.3042 +SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
  1.3043 +SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
  1.3044 +SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
  1.3045 +SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
  1.3046 +SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
  1.3047 +SQLITE_API int sqlite3_diskfull_pending = 0;
  1.3048 +SQLITE_API int sqlite3_diskfull = 0;
  1.3049 +#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
  1.3050 +#define SimulateIOError(CODE)  \
  1.3051 +  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
  1.3052 +       || sqlite3_io_error_pending-- == 1 )  \
  1.3053 +              { local_ioerr(); CODE; }
  1.3054 +static void local_ioerr(){
  1.3055 +  IOTRACE(("IOERR\n"));
  1.3056 +  sqlite3_io_error_hit++;
  1.3057 +  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
  1.3058 +}
  1.3059 +#define SimulateDiskfullError(CODE) \
  1.3060 +   if( sqlite3_diskfull_pending ){ \
  1.3061 +     if( sqlite3_diskfull_pending == 1 ){ \
  1.3062 +       local_ioerr(); \
  1.3063 +       sqlite3_diskfull = 1; \
  1.3064 +       sqlite3_io_error_hit = 1; \
  1.3065 +       CODE; \
  1.3066 +     }else{ \
  1.3067 +       sqlite3_diskfull_pending--; \
  1.3068 +     } \
  1.3069 +   }
  1.3070 +#else
  1.3071 +#define SimulateIOErrorBenign(X)
  1.3072 +#define SimulateIOError(A)
  1.3073 +#define SimulateDiskfullError(A)
  1.3074 +#endif
  1.3075 +
  1.3076 +/*
  1.3077 +** When testing, keep a count of the number of open files.
  1.3078 +*/
  1.3079 +#ifdef SQLITE_TEST
  1.3080 +SQLITE_API int sqlite3_open_file_count = 0;
  1.3081 +#define OpenCounter(X)  sqlite3_open_file_count+=(X)
  1.3082 +#else
  1.3083 +#define OpenCounter(X)
  1.3084 +#endif
  1.3085 +
  1.3086 +#endif /* !defined(_OS_COMMON_H_) */
  1.3087 +
  1.3088 +/************** End of os_common.h *******************************************/
  1.3089 +/************** Continuing where we left off in mutex_w32.c ******************/
  1.3090 +
  1.3091 +/*
  1.3092 +** Include the header file for the Windows VFS.
  1.3093 +*/
  1.3094 +/************** Include os_win.h in the middle of mutex_w32.c ****************/
  1.3095 +/************** Begin file os_win.h ******************************************/
  1.3096 +/*
  1.3097 +** 2013 November 25
  1.3098 +**
  1.3099 +** The author disclaims copyright to this source code.  In place of
  1.3100 +** a legal notice, here is a blessing:
  1.3101 +**
  1.3102 +**    May you do good and not evil.
  1.3103 +**    May you find forgiveness for yourself and forgive others.
  1.3104 +**    May you share freely, never taking more than you give.
  1.3105 +**
  1.3106 +******************************************************************************
  1.3107 +**
  1.3108 +** This file contains code that is specific to Windows.
  1.3109 +*/
  1.3110 +#ifndef _OS_WIN_H_
  1.3111 +#define _OS_WIN_H_
  1.3112 +
  1.3113 +/*
  1.3114 +** Include the primary Windows SDK header file.
  1.3115 +*/
  1.3116 +#include "windows.h"
  1.3117 +
  1.3118 +#ifdef __CYGWIN__
  1.3119 +# include <sys/cygwin.h>
  1.3120 +# include <errno.h> /* amalgamator: dontcache */
  1.3121 +#endif
  1.3122 +
  1.3123 +/*
  1.3124 +** Determine if we are dealing with Windows NT.
  1.3125 +**
  1.3126 +** We ought to be able to determine if we are compiling for Windows 9x or
  1.3127 +** Windows NT using the _WIN32_WINNT macro as follows:
  1.3128 +**
  1.3129 +** #if defined(_WIN32_WINNT)
  1.3130 +** # define SQLITE_OS_WINNT 1
  1.3131 +** #else
  1.3132 +** # define SQLITE_OS_WINNT 0
  1.3133 +** #endif
  1.3134 +**
  1.3135 +** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
  1.3136 +** it ought to, so the above test does not work.  We'll just assume that
  1.3137 +** everything is Windows NT unless the programmer explicitly says otherwise
  1.3138 +** by setting SQLITE_OS_WINNT to 0.
  1.3139 +*/
  1.3140 +#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
  1.3141 +# define SQLITE_OS_WINNT 1
  1.3142 +#endif
  1.3143 +
  1.3144 +/*
  1.3145 +** Determine if we are dealing with Windows CE - which has a much reduced
  1.3146 +** API.
  1.3147 +*/
  1.3148 +#if defined(_WIN32_WCE)
  1.3149 +# define SQLITE_OS_WINCE 1
  1.3150 +#else
  1.3151 +# define SQLITE_OS_WINCE 0
  1.3152 +#endif
  1.3153 +
  1.3154 +/*
  1.3155 +** Determine if we are dealing with WinRT, which provides only a subset of
  1.3156 +** the full Win32 API.
  1.3157 +*/
  1.3158 +#if !defined(SQLITE_OS_WINRT)
  1.3159 +# define SQLITE_OS_WINRT 0
  1.3160 +#endif
  1.3161 +
  1.3162 +#endif /* _OS_WIN_H_ */
  1.3163 +
  1.3164 +/************** End of os_win.h **********************************************/
  1.3165 +/************** Continuing where we left off in mutex_w32.c ******************/
  1.3166 +#endif
  1.3167  
  1.3168  /*
  1.3169  ** The code in this file is only used if we are compiling multithreaded
  1.3170 -** on a win32 system.
  1.3171 +** on a Win32 system.
  1.3172  */
  1.3173  #ifdef SQLITE_MUTEX_W32
  1.3174  
  1.3175 @@ -18633,50 +19410,24 @@
  1.3176  #ifdef SQLITE_DEBUG
  1.3177    volatile int nRef;         /* Number of enterances */
  1.3178    volatile DWORD owner;      /* Thread holding this mutex */
  1.3179 -  int trace;                 /* True to trace changes */
  1.3180 -#endif
  1.3181 -};
  1.3182 +  volatile int trace;        /* True to trace changes */
  1.3183 +#endif
  1.3184 +};
  1.3185 +
  1.3186 +/*
  1.3187 +** These are the initializer values used when declaring a "static" mutex
  1.3188 +** on Win32.  It should be noted that all mutexes require initialization
  1.3189 +** on the Win32 platform.
  1.3190 +*/
  1.3191  #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
  1.3192 +
  1.3193  #ifdef SQLITE_DEBUG
  1.3194 -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
  1.3195 +#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
  1.3196 +                                    0L, (DWORD)0, 0 }
  1.3197  #else
  1.3198  #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
  1.3199  #endif
  1.3200  
  1.3201 -/*
  1.3202 -** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
  1.3203 -** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
  1.3204 -**
  1.3205 -** Here is an interesting observation:  Win95, Win98, and WinME lack
  1.3206 -** the LockFileEx() API.  But we can still statically link against that
  1.3207 -** API as long as we don't call it win running Win95/98/ME.  A call to
  1.3208 -** this routine is used to determine if the host is Win95/98/ME or
  1.3209 -** WinNT/2K/XP so that we will know whether or not we can safely call
  1.3210 -** the LockFileEx() API.
  1.3211 -**
  1.3212 -** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
  1.3213 -** which is only available if your application was compiled with 
  1.3214 -** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
  1.3215 -** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef 
  1.3216 -** this out as well.
  1.3217 -*/
  1.3218 -#if 0
  1.3219 -#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
  1.3220 -# define mutexIsNT()  (1)
  1.3221 -#else
  1.3222 -  static int mutexIsNT(void){
  1.3223 -    static int osType = 0;
  1.3224 -    if( osType==0 ){
  1.3225 -      OSVERSIONINFO sInfo;
  1.3226 -      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  1.3227 -      GetVersionEx(&sInfo);
  1.3228 -      osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
  1.3229 -    }
  1.3230 -    return osType==2;
  1.3231 -  }
  1.3232 -#endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
  1.3233 -#endif
  1.3234 -
  1.3235  #ifdef SQLITE_DEBUG
  1.3236  /*
  1.3237  ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
  1.3238 @@ -18685,20 +19436,24 @@
  1.3239  static int winMutexHeld(sqlite3_mutex *p){
  1.3240    return p->nRef!=0 && p->owner==GetCurrentThreadId();
  1.3241  }
  1.3242 +
  1.3243  static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
  1.3244    return p->nRef==0 || p->owner!=tid;
  1.3245  }
  1.3246 +
  1.3247  static int winMutexNotheld(sqlite3_mutex *p){
  1.3248 -  DWORD tid = GetCurrentThreadId(); 
  1.3249 +  DWORD tid = GetCurrentThreadId();
  1.3250    return winMutexNotheld2(p, tid);
  1.3251  }
  1.3252  #endif
  1.3253  
  1.3254 -
  1.3255  /*
  1.3256  ** Initialize and deinitialize the mutex subsystem.
  1.3257  */
  1.3258 -static sqlite3_mutex winMutex_staticMutexes[6] = {
  1.3259 +static sqlite3_mutex winMutex_staticMutexes[] = {
  1.3260 +  SQLITE3_MUTEX_INITIALIZER,
  1.3261 +  SQLITE3_MUTEX_INITIALIZER,
  1.3262 +  SQLITE3_MUTEX_INITIALIZER,
  1.3263    SQLITE3_MUTEX_INITIALIZER,
  1.3264    SQLITE3_MUTEX_INITIALIZER,
  1.3265    SQLITE3_MUTEX_INITIALIZER,
  1.3266 @@ -18706,17 +19461,20 @@
  1.3267    SQLITE3_MUTEX_INITIALIZER,
  1.3268    SQLITE3_MUTEX_INITIALIZER
  1.3269  };
  1.3270 +
  1.3271  static int winMutex_isInit = 0;
  1.3272 -/* As winMutexInit() and winMutexEnd() are called as part
  1.3273 -** of the sqlite3_initialize and sqlite3_shutdown()
  1.3274 -** processing, the "interlocked" magic is probably not
  1.3275 -** strictly necessary.
  1.3276 -*/
  1.3277 -static LONG winMutex_lock = 0;
  1.3278 -
  1.3279 +static int winMutex_isNt = -1; /* <0 means "need to query" */
  1.3280 +
  1.3281 +/* As the winMutexInit() and winMutexEnd() functions are called as part
  1.3282 +** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
  1.3283 +** "interlocked" magic used here is probably not strictly necessary.
  1.3284 +*/
  1.3285 +static LONG volatile winMutex_lock = 0;
  1.3286 +
  1.3287 +SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
  1.3288  SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
  1.3289  
  1.3290 -static int winMutexInit(void){ 
  1.3291 +static int winMutexInit(void){
  1.3292    /* The first to increment to 1 does actual initialization */
  1.3293    if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
  1.3294      int i;
  1.3295 @@ -18729,16 +19487,17 @@
  1.3296      }
  1.3297      winMutex_isInit = 1;
  1.3298    }else{
  1.3299 -    /* Someone else is in the process of initing the static mutexes */
  1.3300 +    /* Another thread is (in the process of) initializing the static
  1.3301 +    ** mutexes */
  1.3302      while( !winMutex_isInit ){
  1.3303        sqlite3_win32_sleep(1);
  1.3304      }
  1.3305    }
  1.3306 -  return SQLITE_OK; 
  1.3307 -}
  1.3308 -
  1.3309 -static int winMutexEnd(void){ 
  1.3310 -  /* The first to decrement to 0 does actual shutdown 
  1.3311 +  return SQLITE_OK;
  1.3312 +}
  1.3313 +
  1.3314 +static int winMutexEnd(void){
  1.3315 +  /* The first to decrement to 0 does actual shutdown
  1.3316    ** (which should be the last to shutdown.) */
  1.3317    if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
  1.3318      if( winMutex_isInit==1 ){
  1.3319 @@ -18749,7 +19508,7 @@
  1.3320        winMutex_isInit = 0;
  1.3321      }
  1.3322    }
  1.3323 -  return SQLITE_OK; 
  1.3324 +  return SQLITE_OK;
  1.3325  }
  1.3326  
  1.3327  /*
  1.3328 @@ -18764,10 +19523,13 @@
  1.3329  ** <li>  SQLITE_MUTEX_RECURSIVE
  1.3330  ** <li>  SQLITE_MUTEX_STATIC_MASTER
  1.3331  ** <li>  SQLITE_MUTEX_STATIC_MEM
  1.3332 -** <li>  SQLITE_MUTEX_STATIC_MEM2
  1.3333 +** <li>  SQLITE_MUTEX_STATIC_OPEN
  1.3334  ** <li>  SQLITE_MUTEX_STATIC_PRNG
  1.3335  ** <li>  SQLITE_MUTEX_STATIC_LRU
  1.3336  ** <li>  SQLITE_MUTEX_STATIC_PMEM
  1.3337 +** <li>  SQLITE_MUTEX_STATIC_APP1
  1.3338 +** <li>  SQLITE_MUTEX_STATIC_APP2
  1.3339 +** <li>  SQLITE_MUTEX_STATIC_APP3
  1.3340  ** </ul>
  1.3341  **
  1.3342  ** The first two constants cause sqlite3_mutex_alloc() to create
  1.3343 @@ -18790,7 +19552,7 @@
  1.3344  **
  1.3345  ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  1.3346  ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  1.3347 -** returns a different mutex on every call.  But for the static 
  1.3348 +** returns a different mutex on every call.  But for the static
  1.3349  ** mutex types, the same mutex is returned on every call that has
  1.3350  ** the same type number.
  1.3351  */
  1.3352 @@ -18801,9 +19563,12 @@
  1.3353      case SQLITE_MUTEX_FAST:
  1.3354      case SQLITE_MUTEX_RECURSIVE: {
  1.3355        p = sqlite3MallocZero( sizeof(*p) );
  1.3356 -      if( p ){  
  1.3357 +      if( p ){
  1.3358  #ifdef SQLITE_DEBUG
  1.3359          p->id = iType;
  1.3360 +#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
  1.3361 +        p->trace = 1;
  1.3362 +#endif
  1.3363  #endif
  1.3364  #if SQLITE_OS_WINRT
  1.3365          InitializeCriticalSectionEx(&p->mutex, 0, 0);
  1.3366 @@ -18814,12 +19579,15 @@
  1.3367        break;
  1.3368      }
  1.3369      default: {
  1.3370 -      assert( winMutex_isInit==1 );
  1.3371        assert( iType-2 >= 0 );
  1.3372        assert( iType-2 < ArraySize(winMutex_staticMutexes) );
  1.3373 +      assert( winMutex_isInit==1 );
  1.3374        p = &winMutex_staticMutexes[iType-2];
  1.3375  #ifdef SQLITE_DEBUG
  1.3376        p->id = iType;
  1.3377 +#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
  1.3378 +      p->trace = 1;
  1.3379 +#endif
  1.3380  #endif
  1.3381        break;
  1.3382      }
  1.3383 @@ -18835,8 +19603,11 @@
  1.3384  */
  1.3385  static void winMutexFree(sqlite3_mutex *p){
  1.3386    assert( p );
  1.3387 +#ifdef SQLITE_DEBUG
  1.3388    assert( p->nRef==0 && p->owner==0 );
  1.3389    assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
  1.3390 +#endif
  1.3391 +  assert( winMutex_isInit==1 );
  1.3392    DeleteCriticalSection(&p->mutex);
  1.3393    sqlite3_free(p);
  1.3394  }
  1.3395 @@ -18853,30 +19624,39 @@
  1.3396  ** more than once, the behavior is undefined.
  1.3397  */
  1.3398  static void winMutexEnter(sqlite3_mutex *p){
  1.3399 +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  1.3400 +  DWORD tid = GetCurrentThreadId();
  1.3401 +#endif
  1.3402  #ifdef SQLITE_DEBUG
  1.3403 -  DWORD tid = GetCurrentThreadId(); 
  1.3404 +  assert( p );
  1.3405    assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
  1.3406 -#endif
  1.3407 +#else
  1.3408 +  assert( p );
  1.3409 +#endif
  1.3410 +  assert( winMutex_isInit==1 );
  1.3411    EnterCriticalSection(&p->mutex);
  1.3412  #ifdef SQLITE_DEBUG
  1.3413    assert( p->nRef>0 || p->owner==0 );
  1.3414 -  p->owner = tid; 
  1.3415 +  p->owner = tid;
  1.3416    p->nRef++;
  1.3417    if( p->trace ){
  1.3418 -    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  1.3419 -  }
  1.3420 -#endif
  1.3421 -}
  1.3422 +    OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
  1.3423 +             tid, p, p->trace, p->nRef));
  1.3424 +  }
  1.3425 +#endif
  1.3426 +}
  1.3427 +
  1.3428  static int winMutexTry(sqlite3_mutex *p){
  1.3429 -#ifndef NDEBUG
  1.3430 -  DWORD tid = GetCurrentThreadId(); 
  1.3431 +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  1.3432 +  DWORD tid = GetCurrentThreadId();
  1.3433  #endif
  1.3434    int rc = SQLITE_BUSY;
  1.3435 +  assert( p );
  1.3436    assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
  1.3437    /*
  1.3438    ** The sqlite3_mutex_try() routine is very rarely used, and when it
  1.3439    ** is used it is merely an optimization.  So it is OK for it to always
  1.3440 -  ** fail.  
  1.3441 +  ** fail.
  1.3442    **
  1.3443    ** The TryEnterCriticalSection() interface is only available on WinNT.
  1.3444    ** And some windows compilers complain if you try to use it without
  1.3445 @@ -18884,18 +19664,27 @@
  1.3446    ** For that reason, we will omit this optimization for now.  See
  1.3447    ** ticket #2685.
  1.3448    */
  1.3449 -#if 0
  1.3450 -  if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
  1.3451 +#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
  1.3452 +  assert( winMutex_isInit==1 );
  1.3453 +  assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
  1.3454 +  if( winMutex_isNt<0 ){
  1.3455 +    winMutex_isNt = sqlite3_win32_is_nt();
  1.3456 +  }
  1.3457 +  assert( winMutex_isNt==0 || winMutex_isNt==1 );
  1.3458 +  if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
  1.3459 +#ifdef SQLITE_DEBUG
  1.3460      p->owner = tid;
  1.3461      p->nRef++;
  1.3462 +#endif
  1.3463      rc = SQLITE_OK;
  1.3464    }
  1.3465  #else
  1.3466    UNUSED_PARAMETER(p);
  1.3467  #endif
  1.3468  #ifdef SQLITE_DEBUG
  1.3469 -  if( rc==SQLITE_OK && p->trace ){
  1.3470 -    printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  1.3471 +  if( p->trace ){
  1.3472 +    OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
  1.3473 +             tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
  1.3474    }
  1.3475  #endif
  1.3476    return rc;
  1.3477 @@ -18908,18 +19697,23 @@
  1.3478  ** is not currently allocated.  SQLite will never do either.
  1.3479  */
  1.3480  static void winMutexLeave(sqlite3_mutex *p){
  1.3481 -#ifndef NDEBUG
  1.3482 +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  1.3483    DWORD tid = GetCurrentThreadId();
  1.3484 +#endif
  1.3485 +  assert( p );
  1.3486 +#ifdef SQLITE_DEBUG
  1.3487    assert( p->nRef>0 );
  1.3488    assert( p->owner==tid );
  1.3489    p->nRef--;
  1.3490    if( p->nRef==0 ) p->owner = 0;
  1.3491    assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
  1.3492  #endif
  1.3493 +  assert( winMutex_isInit==1 );
  1.3494    LeaveCriticalSection(&p->mutex);
  1.3495  #ifdef SQLITE_DEBUG
  1.3496    if( p->trace ){
  1.3497 -    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
  1.3498 +    OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
  1.3499 +             tid, p, p->trace, p->nRef));
  1.3500    }
  1.3501  #endif
  1.3502  }
  1.3503 @@ -18941,9 +19735,9 @@
  1.3504      0
  1.3505  #endif
  1.3506    };
  1.3507 -
  1.3508    return &sMutex;
  1.3509  }
  1.3510 +
  1.3511  #endif /* SQLITE_MUTEX_W32 */
  1.3512  
  1.3513  /************** End of mutex_w32.c *******************************************/
  1.3514 @@ -19382,7 +20176,7 @@
  1.3515  */
  1.3516  #ifndef SQLITE_OMIT_LOOKASIDE
  1.3517  static int isLookaside(sqlite3 *db, void *p){
  1.3518 -  return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
  1.3519 +  return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
  1.3520  }
  1.3521  #else
  1.3522  #define isLookaside(A,B) 0
  1.3523 @@ -19398,8 +20192,9 @@
  1.3524    return sqlite3GlobalConfig.m.xSize(p);
  1.3525  }
  1.3526  SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
  1.3527 -  assert( db==0 || sqlite3_mutex_held(db->mutex) );
  1.3528 -  if( db && isLookaside(db, p) ){
  1.3529 +  assert( db!=0 );
  1.3530 +  assert( sqlite3_mutex_held(db->mutex) );
  1.3531 +  if( isLookaside(db, p) ){
  1.3532      return db->lookaside.sz;
  1.3533    }else{
  1.3534      assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
  1.3535 @@ -19869,18 +20664,29 @@
  1.3536  #endif /* SQLITE_OMIT_FLOATING_POINT */
  1.3537  
  1.3538  /*
  1.3539 -** Append N space characters to the given string buffer.
  1.3540 -*/
  1.3541 -SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
  1.3542 -  static const char zSpaces[] = "                             ";
  1.3543 -  while( N>=(int)sizeof(zSpaces)-1 ){
  1.3544 -    sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
  1.3545 -    N -= sizeof(zSpaces)-1;
  1.3546 -  }
  1.3547 -  if( N>0 ){
  1.3548 -    sqlite3StrAccumAppend(pAccum, zSpaces, N);
  1.3549 -  }
  1.3550 -}
  1.3551 +** Set the StrAccum object to an error mode.
  1.3552 +*/
  1.3553 +static void setStrAccumError(StrAccum *p, u8 eError){
  1.3554 +  p->accError = eError;
  1.3555 +  p->nAlloc = 0;
  1.3556 +}
  1.3557 +
  1.3558 +/*
  1.3559 +** Extra argument values from a PrintfArguments object
  1.3560 +*/
  1.3561 +static sqlite3_int64 getIntArg(PrintfArguments *p){
  1.3562 +  if( p->nArg<=p->nUsed ) return 0;
  1.3563 +  return sqlite3_value_int64(p->apArg[p->nUsed++]);
  1.3564 +}
  1.3565 +static double getDoubleArg(PrintfArguments *p){
  1.3566 +  if( p->nArg<=p->nUsed ) return 0.0;
  1.3567 +  return sqlite3_value_double(p->apArg[p->nUsed++]);
  1.3568 +}
  1.3569 +static char *getTextArg(PrintfArguments *p){
  1.3570 +  if( p->nArg<=p->nUsed ) return 0;
  1.3571 +  return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
  1.3572 +}
  1.3573 +
  1.3574  
  1.3575  /*
  1.3576  ** On machines with a small stack size, you can redefine the
  1.3577 @@ -19895,10 +20701,10 @@
  1.3578  ** Render a string given by "fmt" into the StrAccum object.
  1.3579  */
  1.3580  SQLITE_PRIVATE void sqlite3VXPrintf(
  1.3581 -  StrAccum *pAccum,                  /* Accumulate results here */
  1.3582 -  int useExtended,                   /* Allow extended %-conversions */
  1.3583 -  const char *fmt,                   /* Format string */
  1.3584 -  va_list ap                         /* arguments */
  1.3585 +  StrAccum *pAccum,          /* Accumulate results here */
  1.3586 +  u32 bFlags,                /* SQLITE_PRINTF_* flags */
  1.3587 +  const char *fmt,           /* Format string */
  1.3588 +  va_list ap                 /* arguments */
  1.3589  ){
  1.3590    int c;                     /* Next character in the format string */
  1.3591    char *bufpt;               /* Pointer to the conversion buffer */
  1.3592 @@ -19916,6 +20722,8 @@
  1.3593    etByte flag_longlong;      /* True if the "ll" flag is present */
  1.3594    etByte done;               /* Loop termination flag */
  1.3595    etByte xtype = 0;          /* Conversion paradigm */
  1.3596 +  u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
  1.3597 +  u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
  1.3598    char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
  1.3599    sqlite_uint64 longvalue;   /* Value for integer types */
  1.3600    LONGDOUBLE_TYPE realvalue; /* Value for real types */
  1.3601 @@ -19930,16 +20738,23 @@
  1.3602    etByte flag_dp;            /* True if decimal point should be shown */
  1.3603    etByte flag_rtz;           /* True if trailing zeros should be removed */
  1.3604  #endif
  1.3605 +  PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
  1.3606    char buf[etBUFSIZE];       /* Conversion buffer */
  1.3607  
  1.3608    bufpt = 0;
  1.3609 +  if( bFlags ){
  1.3610 +    if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
  1.3611 +      pArgList = va_arg(ap, PrintfArguments*);
  1.3612 +    }
  1.3613 +    useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
  1.3614 +  }else{
  1.3615 +    bArgList = useIntern = 0;
  1.3616 +  }
  1.3617    for(; (c=(*fmt))!=0; ++fmt){
  1.3618      if( c!='%' ){
  1.3619 -      int amt;
  1.3620        bufpt = (char *)fmt;
  1.3621 -      amt = 1;
  1.3622 -      while( (c=(*++fmt))!='%' && c!=0 ) amt++;
  1.3623 -      sqlite3StrAccumAppend(pAccum, bufpt, amt);
  1.3624 +      while( (c=(*++fmt))!='%' && c!=0 ){};
  1.3625 +      sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
  1.3626        if( c==0 ) break;
  1.3627      }
  1.3628      if( (c=(*++fmt))==0 ){
  1.3629 @@ -19964,7 +20779,11 @@
  1.3630      /* Get the field width */
  1.3631      width = 0;
  1.3632      if( c=='*' ){
  1.3633 -      width = va_arg(ap,int);
  1.3634 +      if( bArgList ){
  1.3635 +        width = (int)getIntArg(pArgList);
  1.3636 +      }else{
  1.3637 +        width = va_arg(ap,int);
  1.3638 +      }
  1.3639        if( width<0 ){
  1.3640          flag_leftjustify = 1;
  1.3641          width = -width;
  1.3642 @@ -19981,7 +20800,11 @@
  1.3643        precision = 0;
  1.3644        c = *++fmt;
  1.3645        if( c=='*' ){
  1.3646 -        precision = va_arg(ap,int);
  1.3647 +        if( bArgList ){
  1.3648 +          precision = (int)getIntArg(pArgList);
  1.3649 +        }else{
  1.3650 +          precision = va_arg(ap,int);
  1.3651 +        }
  1.3652          if( precision<0 ) precision = -precision;
  1.3653          c = *++fmt;
  1.3654        }else{
  1.3655 @@ -20012,7 +20835,7 @@
  1.3656      for(idx=0; idx<ArraySize(fmtinfo); idx++){
  1.3657        if( c==fmtinfo[idx].fmttype ){
  1.3658          infop = &fmtinfo[idx];
  1.3659 -        if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
  1.3660 +        if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
  1.3661            xtype = infop->type;
  1.3662          }else{
  1.3663            return;
  1.3664 @@ -20052,7 +20875,9 @@
  1.3665        case etRADIX:
  1.3666          if( infop->flags & FLAG_SIGNED ){
  1.3667            i64 v;
  1.3668 -          if( flag_longlong ){
  1.3669 +          if( bArgList ){
  1.3670 +            v = getIntArg(pArgList);
  1.3671 +          }else if( flag_longlong ){
  1.3672              v = va_arg(ap,i64);
  1.3673            }else if( flag_long ){
  1.3674              v = va_arg(ap,long int);
  1.3675 @@ -20073,7 +20898,9 @@
  1.3676              else                       prefix = 0;
  1.3677            }
  1.3678          }else{
  1.3679 -          if( flag_longlong ){
  1.3680 +          if( bArgList ){
  1.3681 +            longvalue = (u64)getIntArg(pArgList);
  1.3682 +          }else if( flag_longlong ){
  1.3683              longvalue = va_arg(ap,u64);
  1.3684            }else if( flag_long ){
  1.3685              longvalue = va_arg(ap,unsigned long int);
  1.3686 @@ -20093,7 +20920,7 @@
  1.3687            nOut = precision + 10;
  1.3688            zOut = zExtra = sqlite3Malloc( nOut );
  1.3689            if( zOut==0 ){
  1.3690 -            pAccum->accError = STRACCUM_NOMEM;
  1.3691 +            setStrAccumError(pAccum, STRACCUM_NOMEM);
  1.3692              return;
  1.3693            }
  1.3694          }
  1.3695 @@ -20108,10 +20935,8 @@
  1.3696            *(--bufpt) = zOrd[x*2];
  1.3697          }
  1.3698          {
  1.3699 -          register const char *cset;      /* Use registers for speed */
  1.3700 -          register int base;
  1.3701 -          cset = &aDigits[infop->charset];
  1.3702 -          base = infop->base;
  1.3703 +          const char *cset = &aDigits[infop->charset];
  1.3704 +          u8 base = infop->base;
  1.3705            do{                                           /* Convert to ascii */
  1.3706              *(--bufpt) = cset[longvalue%base];
  1.3707              longvalue = longvalue/base;
  1.3708 @@ -20133,7 +20958,11 @@
  1.3709        case etFLOAT:
  1.3710        case etEXP:
  1.3711        case etGENERIC:
  1.3712 -        realvalue = va_arg(ap,double);
  1.3713 +        if( bArgList ){
  1.3714 +          realvalue = getDoubleArg(pArgList);
  1.3715 +        }else{
  1.3716 +          realvalue = va_arg(ap,double);
  1.3717 +        }
  1.3718  #ifdef SQLITE_OMIT_FLOATING_POINT
  1.3719          length = 0;
  1.3720  #else
  1.3721 @@ -20205,7 +21034,7 @@
  1.3722          if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
  1.3723            bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
  1.3724            if( bufpt==0 ){
  1.3725 -            pAccum->accError = STRACCUM_NOMEM;
  1.3726 +            setStrAccumError(pAccum, STRACCUM_NOMEM);
  1.3727              return;
  1.3728            }
  1.3729          }
  1.3730 @@ -20288,7 +21117,9 @@
  1.3731  #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
  1.3732          break;
  1.3733        case etSIZE:
  1.3734 -        *(va_arg(ap,int*)) = pAccum->nChar;
  1.3735 +        if( !bArgList ){
  1.3736 +          *(va_arg(ap,int*)) = pAccum->nChar;
  1.3737 +        }
  1.3738          length = width = 0;
  1.3739          break;
  1.3740        case etPERCENT:
  1.3741 @@ -20297,7 +21128,12 @@
  1.3742          length = 1;
  1.3743          break;
  1.3744        case etCHARX:
  1.3745 -        c = va_arg(ap,int);
  1.3746 +        if( bArgList ){
  1.3747 +          bufpt = getTextArg(pArgList);
  1.3748 +          c = bufpt ? bufpt[0] : 0;
  1.3749 +        }else{
  1.3750 +          c = va_arg(ap,int);
  1.3751 +        }
  1.3752          buf[0] = (char)c;
  1.3753          if( precision>=0 ){
  1.3754            for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
  1.3755 @@ -20309,10 +21145,14 @@
  1.3756          break;
  1.3757        case etSTRING:
  1.3758        case etDYNSTRING:
  1.3759 -        bufpt = va_arg(ap,char*);
  1.3760 +        if( bArgList ){
  1.3761 +          bufpt = getTextArg(pArgList);
  1.3762 +        }else{
  1.3763 +          bufpt = va_arg(ap,char*);
  1.3764 +        }
  1.3765          if( bufpt==0 ){
  1.3766            bufpt = "";
  1.3767 -        }else if( xtype==etDYNSTRING ){
  1.3768 +        }else if( xtype==etDYNSTRING && !bArgList ){
  1.3769            zExtra = bufpt;
  1.3770          }
  1.3771          if( precision>=0 ){
  1.3772 @@ -20328,7 +21168,13 @@
  1.3773          int needQuote;
  1.3774          char ch;
  1.3775          char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
  1.3776 -        char *escarg = va_arg(ap,char*);
  1.3777 +        char *escarg;
  1.3778 +
  1.3779 +        if( bArgList ){
  1.3780 +          escarg = getTextArg(pArgList);
  1.3781 +        }else{
  1.3782 +          escarg = va_arg(ap,char*);
  1.3783 +        }
  1.3784          isnull = escarg==0;
  1.3785          if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
  1.3786          k = precision;
  1.3787 @@ -20340,7 +21186,7 @@
  1.3788          if( n>etBUFSIZE ){
  1.3789            bufpt = zExtra = sqlite3Malloc( n );
  1.3790            if( bufpt==0 ){
  1.3791 -            pAccum->accError = STRACCUM_NOMEM;
  1.3792 +            setStrAccumError(pAccum, STRACCUM_NOMEM);
  1.3793              return;
  1.3794            }
  1.3795          }else{
  1.3796 @@ -20363,7 +21209,8 @@
  1.3797        }
  1.3798        case etTOKEN: {
  1.3799          Token *pToken = va_arg(ap, Token*);
  1.3800 -        if( pToken ){
  1.3801 +        assert( bArgList==0 );
  1.3802 +        if( pToken && pToken->n ){
  1.3803            sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
  1.3804          }
  1.3805          length = width = 0;
  1.3806 @@ -20373,12 +21220,13 @@
  1.3807          SrcList *pSrc = va_arg(ap, SrcList*);
  1.3808          int k = va_arg(ap, int);
  1.3809          struct SrcList_item *pItem = &pSrc->a[k];
  1.3810 +        assert( bArgList==0 );
  1.3811          assert( k>=0 && k<pSrc->nSrc );
  1.3812          if( pItem->zDatabase ){
  1.3813 -          sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
  1.3814 +          sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
  1.3815            sqlite3StrAccumAppend(pAccum, ".", 1);
  1.3816          }
  1.3817 -        sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
  1.3818 +        sqlite3StrAccumAppendAll(pAccum, pItem->zName);
  1.3819          length = width = 0;
  1.3820          break;
  1.3821        }
  1.3822 @@ -20392,82 +21240,114 @@
  1.3823      ** "length" characters long.  The field width is "width".  Do
  1.3824      ** the output.
  1.3825      */
  1.3826 -    if( !flag_leftjustify ){
  1.3827 -      register int nspace;
  1.3828 -      nspace = width-length;
  1.3829 -      if( nspace>0 ){
  1.3830 -        sqlite3AppendSpace(pAccum, nspace);
  1.3831 -      }
  1.3832 -    }
  1.3833 -    if( length>0 ){
  1.3834 -      sqlite3StrAccumAppend(pAccum, bufpt, length);
  1.3835 -    }
  1.3836 -    if( flag_leftjustify ){
  1.3837 -      register int nspace;
  1.3838 -      nspace = width-length;
  1.3839 -      if( nspace>0 ){
  1.3840 -        sqlite3AppendSpace(pAccum, nspace);
  1.3841 -      }
  1.3842 -    }
  1.3843 -    sqlite3_free(zExtra);
  1.3844 +    width -= length;
  1.3845 +    if( width>0 && !flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
  1.3846 +    sqlite3StrAccumAppend(pAccum, bufpt, length);
  1.3847 +    if( width>0 && flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
  1.3848 +
  1.3849 +    if( zExtra ) sqlite3_free(zExtra);
  1.3850    }/* End for loop over the format string */
  1.3851  } /* End of function */
  1.3852  
  1.3853  /*
  1.3854 -** Append N bytes of text from z to the StrAccum object.
  1.3855 -*/
  1.3856 -SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
  1.3857 -  assert( z!=0 || N==0 );
  1.3858 +** Enlarge the memory allocation on a StrAccum object so that it is
  1.3859 +** able to accept at least N more bytes of text.
  1.3860 +**
  1.3861 +** Return the number of bytes of text that StrAccum is able to accept
  1.3862 +** after the attempted enlargement.  The value returned might be zero.
  1.3863 +*/
  1.3864 +static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
  1.3865 +  char *zNew;
  1.3866 +  assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
  1.3867    if( p->accError ){
  1.3868      testcase(p->accError==STRACCUM_TOOBIG);
  1.3869      testcase(p->accError==STRACCUM_NOMEM);
  1.3870 +    return 0;
  1.3871 +  }
  1.3872 +  if( !p->useMalloc ){
  1.3873 +    N = p->nAlloc - p->nChar - 1;
  1.3874 +    setStrAccumError(p, STRACCUM_TOOBIG);
  1.3875 +    return N;
  1.3876 +  }else{
  1.3877 +    char *zOld = (p->zText==p->zBase ? 0 : p->zText);
  1.3878 +    i64 szNew = p->nChar;
  1.3879 +    szNew += N + 1;
  1.3880 +    if( szNew > p->mxAlloc ){
  1.3881 +      sqlite3StrAccumReset(p);
  1.3882 +      setStrAccumError(p, STRACCUM_TOOBIG);
  1.3883 +      return 0;
  1.3884 +    }else{
  1.3885 +      p->nAlloc = (int)szNew;
  1.3886 +    }
  1.3887 +    if( p->useMalloc==1 ){
  1.3888 +      zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
  1.3889 +    }else{
  1.3890 +      zNew = sqlite3_realloc(zOld, p->nAlloc);
  1.3891 +    }
  1.3892 +    if( zNew ){
  1.3893 +      assert( p->zText!=0 || p->nChar==0 );
  1.3894 +      if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
  1.3895 +      p->zText = zNew;
  1.3896 +    }else{
  1.3897 +      sqlite3StrAccumReset(p);
  1.3898 +      setStrAccumError(p, STRACCUM_NOMEM);
  1.3899 +      return 0;
  1.3900 +    }
  1.3901 +  }
  1.3902 +  return N;
  1.3903 +}
  1.3904 +
  1.3905 +/*
  1.3906 +** Append N space characters to the given string buffer.
  1.3907 +*/
  1.3908 +SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *p, int N){
  1.3909 +  if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
  1.3910 +  while( (N--)>0 ) p->zText[p->nChar++] = ' ';
  1.3911 +}
  1.3912 +
  1.3913 +/*
  1.3914 +** The StrAccum "p" is not large enough to accept N new bytes of z[].
  1.3915 +** So enlarge if first, then do the append.
  1.3916 +**
  1.3917 +** This is a helper routine to sqlite3StrAccumAppend() that does special-case
  1.3918 +** work (enlarging the buffer) using tail recursion, so that the
  1.3919 +** sqlite3StrAccumAppend() routine can use fast calling semantics.
  1.3920 +*/
  1.3921 +static void enlargeAndAppend(StrAccum *p, const char *z, int N){
  1.3922 +  N = sqlite3StrAccumEnlarge(p, N);
  1.3923 +  if( N>0 ){
  1.3924 +    memcpy(&p->zText[p->nChar], z, N);
  1.3925 +    p->nChar += N;
  1.3926 +  }
  1.3927 +}
  1.3928 +
  1.3929 +/*
  1.3930 +** Append N bytes of text from z to the StrAccum object.  Increase the
  1.3931 +** size of the memory allocation for StrAccum if necessary.
  1.3932 +*/
  1.3933 +SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
  1.3934 +  assert( z!=0 );
  1.3935 +  assert( p->zText!=0 || p->nChar==0 || p->accError );
  1.3936 +  assert( N>=0 );
  1.3937 +  assert( p->accError==0 || p->nAlloc==0 );
  1.3938 +  if( p->nChar+N >= p->nAlloc ){
  1.3939 +    enlargeAndAppend(p,z,N);
  1.3940      return;
  1.3941    }
  1.3942 -  assert( p->zText!=0 || p->nChar==0 );
  1.3943 -  if( N<=0 ){
  1.3944 -    if( N==0 || z[0]==0 ) return;
  1.3945 -    N = sqlite3Strlen30(z);
  1.3946 -  }
  1.3947 -  if( p->nChar+N >= p->nAlloc ){
  1.3948 -    char *zNew;
  1.3949 -    if( !p->useMalloc ){
  1.3950 -      p->accError = STRACCUM_TOOBIG;
  1.3951 -      N = p->nAlloc - p->nChar - 1;
  1.3952 -      if( N<=0 ){
  1.3953 -        return;
  1.3954 -      }
  1.3955 -    }else{
  1.3956 -      char *zOld = (p->zText==p->zBase ? 0 : p->zText);
  1.3957 -      i64 szNew = p->nChar;
  1.3958 -      szNew += N + 1;
  1.3959 -      if( szNew > p->mxAlloc ){
  1.3960 -        sqlite3StrAccumReset(p);
  1.3961 -        p->accError = STRACCUM_TOOBIG;
  1.3962 -        return;
  1.3963 -      }else{
  1.3964 -        p->nAlloc = (int)szNew;
  1.3965 -      }
  1.3966 -      if( p->useMalloc==1 ){
  1.3967 -        zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
  1.3968 -      }else{
  1.3969 -        zNew = sqlite3_realloc(zOld, p->nAlloc);
  1.3970 -      }
  1.3971 -      if( zNew ){
  1.3972 -        if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
  1.3973 -        p->zText = zNew;
  1.3974 -      }else{
  1.3975 -        p->accError = STRACCUM_NOMEM;
  1.3976 -        sqlite3StrAccumReset(p);
  1.3977 -        return;
  1.3978 -      }
  1.3979 -    }
  1.3980 -  }
  1.3981    assert( p->zText );
  1.3982    memcpy(&p->zText[p->nChar], z, N);
  1.3983    p->nChar += N;
  1.3984  }
  1.3985  
  1.3986  /*
  1.3987 +** Append the complete text of zero-terminated string z[] to the p string.
  1.3988 +*/
  1.3989 +SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
  1.3990 +  sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
  1.3991 +}
  1.3992 +
  1.3993 +
  1.3994 +/*
  1.3995  ** Finish off a string by making sure it is zero-terminated.
  1.3996  ** Return a pointer to the resulting string.  Return a NULL
  1.3997  ** pointer if any kind of error was encountered.
  1.3998 @@ -20484,7 +21364,7 @@
  1.3999        if( p->zText ){
  1.4000          memcpy(p->zText, p->zBase, p->nChar+1);
  1.4001        }else{
  1.4002 -        p->accError = STRACCUM_NOMEM;
  1.4003 +        setStrAccumError(p, STRACCUM_NOMEM);
  1.4004        }
  1.4005      }
  1.4006    }
  1.4007 @@ -20530,7 +21410,7 @@
  1.4008    sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
  1.4009                        db->aLimit[SQLITE_LIMIT_LENGTH]);
  1.4010    acc.db = db;
  1.4011 -  sqlite3VXPrintf(&acc, 1, zFormat, ap);
  1.4012 +  sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
  1.4013    z = sqlite3StrAccumFinish(&acc);
  1.4014    if( acc.accError==STRACCUM_NOMEM ){
  1.4015      db->mallocFailed = 1;
  1.4016 @@ -20686,17 +21566,15 @@
  1.4017  }
  1.4018  #endif
  1.4019  
  1.4020 -#ifndef SQLITE_OMIT_TRACE
  1.4021  /*
  1.4022  ** variable-argument wrapper around sqlite3VXPrintf().
  1.4023  */
  1.4024 -SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
  1.4025 +SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
  1.4026    va_list ap;
  1.4027    va_start(ap,zFormat);
  1.4028 -  sqlite3VXPrintf(p, 1, zFormat, ap);
  1.4029 +  sqlite3VXPrintf(p, bFlags, zFormat, ap);
  1.4030    va_end(ap);
  1.4031  }
  1.4032 -#endif
  1.4033  
  1.4034  /************** End of printf.c **********************************************/
  1.4035  /************** Begin file random.c ******************************************/
  1.4036 @@ -20753,6 +21631,12 @@
  1.4037    sqlite3_mutex_enter(mutex);
  1.4038  #endif
  1.4039  
  1.4040 +  if( N<=0 ){
  1.4041 +    wsdPrng.isInit = 0;
  1.4042 +    sqlite3_mutex_leave(mutex);
  1.4043 +    return;
  1.4044 +  }
  1.4045 +
  1.4046    /* Initialize the state of the random number generator once,
  1.4047    ** the first time this routine is called.  The seed value does
  1.4048    ** not need to contain a lot of randomness since we are not
  1.4049 @@ -20780,7 +21664,8 @@
  1.4050      wsdPrng.isInit = 1;
  1.4051    }
  1.4052  
  1.4053 -  while( N-- ){
  1.4054 +  assert( N>0 );
  1.4055 +  do{
  1.4056      wsdPrng.i++;
  1.4057      t = wsdPrng.s[wsdPrng.i];
  1.4058      wsdPrng.j += t;
  1.4059 @@ -20788,7 +21673,7 @@
  1.4060      wsdPrng.s[wsdPrng.j] = t;
  1.4061      t += wsdPrng.s[wsdPrng.i];
  1.4062      *(zBuf++) = wsdPrng.s[t];
  1.4063 -  }
  1.4064 +  }while( --N );
  1.4065    sqlite3_mutex_leave(mutex);
  1.4066  }
  1.4067  
  1.4068 @@ -20817,9 +21702,6 @@
  1.4069      sizeof(sqlite3Prng)
  1.4070    );
  1.4071  }
  1.4072 -SQLITE_PRIVATE void sqlite3PrngResetState(void){
  1.4073 -  GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
  1.4074 -}
  1.4075  #endif /* SQLITE_OMIT_BUILTIN_TEST */
  1.4076  
  1.4077  /************** End of random.c **********************************************/
  1.4078 @@ -20972,8 +21854,8 @@
  1.4079  **     and rendered as themselves even though they are technically
  1.4080  **     invalid characters.
  1.4081  **
  1.4082 -**  *  This routine accepts an infinite number of different UTF8 encodings
  1.4083 -**     for unicode values 0x80 and greater.  It do not change over-length
  1.4084 +**  *  This routine accepts over-length UTF8 encodings
  1.4085 +**     for unicode values 0x80 and greater.  It does not change over-length
  1.4086  **     encodings to 0xfffd as some systems recommend.
  1.4087  */
  1.4088  #define READ_UTF8(zIn, zTerm, c)                           \
  1.4089 @@ -21141,7 +22023,7 @@
  1.4090    sqlite3VdbeMemRelease(pMem);
  1.4091    pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
  1.4092    pMem->enc = desiredEnc;
  1.4093 -  pMem->flags |= (MEM_Term|MEM_Dyn);
  1.4094 +  pMem->flags |= (MEM_Term);
  1.4095    pMem->z = (char*)zOut;
  1.4096    pMem->zMalloc = pMem->z;
  1.4097  
  1.4098 @@ -21269,7 +22151,6 @@
  1.4099    }
  1.4100    assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
  1.4101    assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
  1.4102 -  assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
  1.4103    assert( m.z || db->mallocFailed );
  1.4104    return m.z;
  1.4105  }
  1.4106 @@ -21387,6 +22268,24 @@
  1.4107  }
  1.4108  #endif
  1.4109  
  1.4110 +/*
  1.4111 +** Give a callback to the test harness that can be used to simulate faults
  1.4112 +** in places where it is difficult or expensive to do so purely by means
  1.4113 +** of inputs.
  1.4114 +**
  1.4115 +** The intent of the integer argument is to let the fault simulator know
  1.4116 +** which of multiple sqlite3FaultSim() calls has been hit.
  1.4117 +**
  1.4118 +** Return whatever integer value the test callback returns, or return
  1.4119 +** SQLITE_OK if no test callback is installed.
  1.4120 +*/
  1.4121 +#ifndef SQLITE_OMIT_BUILTIN_TEST
  1.4122 +SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
  1.4123 +  int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
  1.4124 +  return xCallback ? xCallback(iTest) : SQLITE_OK;
  1.4125 +}
  1.4126 +#endif
  1.4127 +
  1.4128  #ifndef SQLITE_OMIT_FLOATING_POINT
  1.4129  /*
  1.4130  ** Return true if the floating point value is Not a Number (NaN).
  1.4131 @@ -21471,18 +22370,17 @@
  1.4132  ** to NULL.
  1.4133  */
  1.4134  SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
  1.4135 -  if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
  1.4136 -    db->errCode = err_code;
  1.4137 -    if( zFormat ){
  1.4138 -      char *z;
  1.4139 -      va_list ap;
  1.4140 -      va_start(ap, zFormat);
  1.4141 -      z = sqlite3VMPrintf(db, zFormat, ap);
  1.4142 -      va_end(ap);
  1.4143 -      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
  1.4144 -    }else{
  1.4145 -      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
  1.4146 -    }
  1.4147 +  assert( db!=0 );
  1.4148 +  db->errCode = err_code;
  1.4149 +  if( zFormat && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
  1.4150 +    char *z;
  1.4151 +    va_list ap;
  1.4152 +    va_start(ap, zFormat);
  1.4153 +    z = sqlite3VMPrintf(db, zFormat, ap);
  1.4154 +    va_end(ap);
  1.4155 +    sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
  1.4156 +  }else if( db->pErr ){
  1.4157 +    sqlite3ValueSetNull(db->pErr);
  1.4158    }
  1.4159  }
  1.4160  
  1.4161 @@ -21814,9 +22712,9 @@
  1.4162    return c;
  1.4163  }
  1.4164  
  1.4165 -
  1.4166 -/*
  1.4167 -** Convert zNum to a 64-bit signed integer.
  1.4168 +/*
  1.4169 +** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
  1.4170 +** routine does *not* accept hexadecimal notation.
  1.4171  **
  1.4172  ** If the zNum value is representable as a 64-bit twos-complement 
  1.4173  ** integer, then write that value into *pNum and return 0.
  1.4174 @@ -21905,9 +22803,43 @@
  1.4175  }
  1.4176  
  1.4177  /*
  1.4178 +** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
  1.4179 +** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
  1.4180 +** whereas sqlite3Atoi64() does not.
  1.4181 +**
  1.4182 +** Returns:
  1.4183 +**
  1.4184 +**     0    Successful transformation.  Fits in a 64-bit signed integer.
  1.4185 +**     1    Integer too large for a 64-bit signed integer or is malformed
  1.4186 +**     2    Special case of 9223372036854775808
  1.4187 +*/
  1.4188 +SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
  1.4189 +#ifndef SQLITE_OMIT_HEX_INTEGER
  1.4190 +  if( z[0]=='0'
  1.4191 +   && (z[1]=='x' || z[1]=='X')
  1.4192 +   && sqlite3Isxdigit(z[2])
  1.4193 +  ){
  1.4194 +    u64 u = 0;
  1.4195 +    int i, k;
  1.4196 +    for(i=2; z[i]=='0'; i++){}
  1.4197 +    for(k=i; sqlite3Isxdigit(z[k]); k++){
  1.4198 +      u = u*16 + sqlite3HexToInt(z[k]);
  1.4199 +    }
  1.4200 +    memcpy(pOut, &u, 8);
  1.4201 +    return (z[k]==0 && k-i<=16) ? 0 : 1;
  1.4202 +  }else
  1.4203 +#endif /* SQLITE_OMIT_HEX_INTEGER */
  1.4204 +  {
  1.4205 +    return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
  1.4206 +  }
  1.4207 +}
  1.4208 +
  1.4209 +/*
  1.4210  ** If zNum represents an integer that will fit in 32-bits, then set
  1.4211  ** *pValue to that integer and return true.  Otherwise return false.
  1.4212  **
  1.4213 +** This routine accepts both decimal and hexadecimal notation for integers.
  1.4214 +**
  1.4215  ** Any non-numeric characters that following zNum are ignored.
  1.4216  ** This is different from sqlite3Atoi64() which requires the
  1.4217  ** input number to be zero-terminated.
  1.4218 @@ -21922,7 +22854,25 @@
  1.4219    }else if( zNum[0]=='+' ){
  1.4220      zNum++;
  1.4221    }
  1.4222 -  while( zNum[0]=='0' ) zNum++;
  1.4223 +#ifndef SQLITE_OMIT_HEX_INTEGER
  1.4224 +  else if( zNum[0]=='0'
  1.4225 +        && (zNum[1]=='x' || zNum[1]=='X')
  1.4226 +        && sqlite3Isxdigit(zNum[2])
  1.4227 +  ){
  1.4228 +    u32 u = 0;
  1.4229 +    zNum += 2;
  1.4230 +    while( zNum[0]=='0' ) zNum++;
  1.4231 +    for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
  1.4232 +      u = u*16 + sqlite3HexToInt(zNum[i]);
  1.4233 +    }
  1.4234 +    if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
  1.4235 +      memcpy(pValue, &u, 4);
  1.4236 +      return 1;
  1.4237 +    }else{
  1.4238 +      return 0;
  1.4239 +    }
  1.4240 +  }
  1.4241 +#endif
  1.4242    for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
  1.4243      v = v*10 + c;
  1.4244    }
  1.4245 @@ -22358,7 +23308,8 @@
  1.4246  ** Read or write a four-byte big-endian integer value.
  1.4247  */
  1.4248  SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
  1.4249 -  return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  1.4250 +  testcase( p[0]&0x80 );
  1.4251 +  return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  1.4252  }
  1.4253  SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
  1.4254    p[0] = (u8)(v>>24);
  1.4255 @@ -22479,13 +23430,12 @@
  1.4256      testcase( iA>0 && LARGEST_INT64 - iA == iB );
  1.4257      testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
  1.4258      if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
  1.4259 -    *pA += iB;
  1.4260    }else{
  1.4261      testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
  1.4262      testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
  1.4263      if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
  1.4264 -    *pA += iB;
  1.4265 -  }
  1.4266 +  }
  1.4267 +  *pA += iB;
  1.4268    return 0; 
  1.4269  }
  1.4270  SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
  1.4271 @@ -22509,9 +23459,18 @@
  1.4272    iA0 = iA % TWOPOWER32;
  1.4273    iB1 = iB/TWOPOWER32;
  1.4274    iB0 = iB % TWOPOWER32;
  1.4275 -  if( iA1*iB1 != 0 ) return 1;
  1.4276 -  assert( iA1*iB0==0 || iA0*iB1==0 );
  1.4277 -  r = iA1*iB0 + iA0*iB1;
  1.4278 +  if( iA1==0 ){
  1.4279 +    if( iB1==0 ){
  1.4280 +      *pA *= iB;
  1.4281 +      return 0;
  1.4282 +    }
  1.4283 +    r = iA0*iB1;
  1.4284 +  }else if( iB1==0 ){
  1.4285 +    r = iA1*iB0;
  1.4286 +  }else{
  1.4287 +    /* If both iA1 and iB1 are non-zero, overflow will result */
  1.4288 +    return 1;
  1.4289 +  }
  1.4290    testcase( r==(-TWOPOWER31)-1 );
  1.4291    testcase( r==(-TWOPOWER31) );
  1.4292    testcase( r==TWOPOWER31 );
  1.4293 @@ -22594,8 +23553,8 @@
  1.4294  }
  1.4295  
  1.4296  /*
  1.4297 -** Convert an integer into a LogEst.  In other words, compute a
  1.4298 -** good approximatation for 10*log2(x).
  1.4299 +** Convert an integer into a LogEst.  In other words, compute an
  1.4300 +** approximation for 10*log2(x).
  1.4301  */
  1.4302  SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
  1.4303    static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
  1.4304 @@ -22699,7 +23658,7 @@
  1.4305  ** The hashing function.
  1.4306  */
  1.4307  static unsigned int strHash(const char *z, int nKey){
  1.4308 -  int h = 0;
  1.4309 +  unsigned int h = 0;
  1.4310    assert( nKey>=0 );
  1.4311    while( nKey > 0  ){
  1.4312      h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
  1.4313 @@ -22951,145 +23910,151 @@
  1.4314       /*  11 */ "Checkpoint"       OpHelp(""),
  1.4315       /*  12 */ "JournalMode"      OpHelp(""),
  1.4316       /*  13 */ "Vacuum"           OpHelp(""),
  1.4317 -     /*  14 */ "VFilter"          OpHelp("iPlan=r[P3] zPlan='P4'"),
  1.4318 +     /*  14 */ "VFilter"          OpHelp("iplan=r[P3] zplan='P4'"),
  1.4319       /*  15 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
  1.4320       /*  16 */ "Goto"             OpHelp(""),
  1.4321       /*  17 */ "Gosub"            OpHelp(""),
  1.4322       /*  18 */ "Return"           OpHelp(""),
  1.4323       /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
  1.4324 -     /*  20 */ "Yield"            OpHelp(""),
  1.4325 -     /*  21 */ "HaltIfNull"       OpHelp("if r[P3] null then halt"),
  1.4326 -     /*  22 */ "Halt"             OpHelp(""),
  1.4327 -     /*  23 */ "Integer"          OpHelp("r[P2]=P1"),
  1.4328 -     /*  24 */ "Int64"            OpHelp("r[P2]=P4"),
  1.4329 -     /*  25 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
  1.4330 -     /*  26 */ "Null"             OpHelp("r[P2..P3]=NULL"),
  1.4331 -     /*  27 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
  1.4332 -     /*  28 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
  1.4333 -     /*  29 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
  1.4334 -     /*  30 */ "Copy"             OpHelp("r[P2@P3]=r[P1@P3]"),
  1.4335 -     /*  31 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
  1.4336 -     /*  32 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
  1.4337 -     /*  33 */ "CollSeq"          OpHelp(""),
  1.4338 -     /*  34 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
  1.4339 -     /*  35 */ "MustBeInt"        OpHelp(""),
  1.4340 -     /*  36 */ "RealAffinity"     OpHelp(""),
  1.4341 -     /*  37 */ "Permutation"      OpHelp(""),
  1.4342 -     /*  38 */ "Compare"          OpHelp(""),
  1.4343 -     /*  39 */ "Jump"             OpHelp(""),
  1.4344 -     /*  40 */ "Once"             OpHelp(""),
  1.4345 -     /*  41 */ "If"               OpHelp(""),
  1.4346 -     /*  42 */ "IfNot"            OpHelp(""),
  1.4347 -     /*  43 */ "Column"           OpHelp("r[P3]=PX"),
  1.4348 -     /*  44 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
  1.4349 -     /*  45 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
  1.4350 -     /*  46 */ "Count"            OpHelp("r[P2]=count()"),
  1.4351 -     /*  47 */ "ReadCookie"       OpHelp(""),
  1.4352 -     /*  48 */ "SetCookie"        OpHelp(""),
  1.4353 -     /*  49 */ "VerifyCookie"     OpHelp(""),
  1.4354 -     /*  50 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
  1.4355 -     /*  51 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
  1.4356 -     /*  52 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
  1.4357 -     /*  53 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
  1.4358 -     /*  54 */ "SorterOpen"       OpHelp(""),
  1.4359 -     /*  55 */ "OpenPseudo"       OpHelp("content in r[P2@P3]"),
  1.4360 -     /*  56 */ "Close"            OpHelp(""),
  1.4361 -     /*  57 */ "SeekLt"           OpHelp("key=r[P3@P4]"),
  1.4362 -     /*  58 */ "SeekLe"           OpHelp("key=r[P3@P4]"),
  1.4363 -     /*  59 */ "SeekGe"           OpHelp("key=r[P3@P4]"),
  1.4364 -     /*  60 */ "SeekGt"           OpHelp("key=r[P3@P4]"),
  1.4365 -     /*  61 */ "Seek"             OpHelp("intkey=r[P2]"),
  1.4366 -     /*  62 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
  1.4367 -     /*  63 */ "NotFound"         OpHelp("key=r[P3@P4]"),
  1.4368 -     /*  64 */ "Found"            OpHelp("key=r[P3@P4]"),
  1.4369 -     /*  65 */ "NotExists"        OpHelp("intkey=r[P3]"),
  1.4370 -     /*  66 */ "Sequence"         OpHelp("r[P2]=rowid"),
  1.4371 -     /*  67 */ "NewRowid"         OpHelp("r[P2]=rowid"),
  1.4372 -     /*  68 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
  1.4373 -     /*  69 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
  1.4374 -     /*  70 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
  1.4375 -     /*  71 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
  1.4376 -     /*  72 */ "Delete"           OpHelp(""),
  1.4377 -     /*  73 */ "ResetCount"       OpHelp(""),
  1.4378 -     /*  74 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
  1.4379 -     /*  75 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
  1.4380 -     /*  76 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
  1.4381 -     /*  77 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
  1.4382 -     /*  78 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
  1.4383 -     /*  79 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
  1.4384 -     /*  80 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
  1.4385 -     /*  81 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
  1.4386 -     /*  82 */ "SorterCompare"    OpHelp("if key(P1)!=rtrim(r[P3],P4) goto P2"),
  1.4387 -     /*  83 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
  1.4388 -     /*  84 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
  1.4389 -     /*  85 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
  1.4390 -     /*  86 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
  1.4391 -     /*  87 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
  1.4392 -     /*  88 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
  1.4393 -     /*  89 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
  1.4394 -     /*  90 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
  1.4395 -     /*  91 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
  1.4396 -     /*  92 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
  1.4397 -     /*  93 */ "SorterData"       OpHelp("r[P2]=data"),
  1.4398 -     /*  94 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
  1.4399 -     /*  95 */ "String8"          OpHelp("r[P2]='P4'"),
  1.4400 -     /*  96 */ "RowKey"           OpHelp("r[P2]=key"),
  1.4401 -     /*  97 */ "RowData"          OpHelp("r[P2]=data"),
  1.4402 -     /*  98 */ "Rowid"            OpHelp("r[P2]=rowid"),
  1.4403 -     /*  99 */ "NullRow"          OpHelp(""),
  1.4404 -     /* 100 */ "Last"             OpHelp(""),
  1.4405 -     /* 101 */ "SorterSort"       OpHelp(""),
  1.4406 -     /* 102 */ "Sort"             OpHelp(""),
  1.4407 -     /* 103 */ "Rewind"           OpHelp(""),
  1.4408 -     /* 104 */ "SorterInsert"     OpHelp(""),
  1.4409 -     /* 105 */ "IdxInsert"        OpHelp("key=r[P2]"),
  1.4410 -     /* 106 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
  1.4411 -     /* 107 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
  1.4412 -     /* 108 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
  1.4413 -     /* 109 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
  1.4414 -     /* 110 */ "Destroy"          OpHelp(""),
  1.4415 -     /* 111 */ "Clear"            OpHelp(""),
  1.4416 -     /* 112 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
  1.4417 -     /* 113 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
  1.4418 -     /* 114 */ "ParseSchema"      OpHelp(""),
  1.4419 -     /* 115 */ "LoadAnalysis"     OpHelp(""),
  1.4420 -     /* 116 */ "DropTable"        OpHelp(""),
  1.4421 -     /* 117 */ "DropIndex"        OpHelp(""),
  1.4422 -     /* 118 */ "DropTrigger"      OpHelp(""),
  1.4423 -     /* 119 */ "IntegrityCk"      OpHelp(""),
  1.4424 -     /* 120 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
  1.4425 -     /* 121 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
  1.4426 -     /* 122 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
  1.4427 -     /* 123 */ "Program"          OpHelp(""),
  1.4428 -     /* 124 */ "Param"            OpHelp(""),
  1.4429 -     /* 125 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
  1.4430 -     /* 126 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
  1.4431 -     /* 127 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
  1.4432 -     /* 128 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
  1.4433 -     /* 129 */ "IfNeg"            OpHelp("if r[P1]<0 goto P2"),
  1.4434 -     /* 130 */ "IfZero"           OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
  1.4435 -     /* 131 */ "Real"             OpHelp("r[P2]=P4"),
  1.4436 -     /* 132 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
  1.4437 -     /* 133 */ "IncrVacuum"       OpHelp(""),
  1.4438 -     /* 134 */ "Expire"           OpHelp(""),
  1.4439 -     /* 135 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
  1.4440 -     /* 136 */ "VBegin"           OpHelp(""),
  1.4441 -     /* 137 */ "VCreate"          OpHelp(""),
  1.4442 -     /* 138 */ "VDestroy"         OpHelp(""),
  1.4443 -     /* 139 */ "VOpen"            OpHelp(""),
  1.4444 -     /* 140 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
  1.4445 -     /* 141 */ "VNext"            OpHelp(""),
  1.4446 -     /* 142 */ "ToText"           OpHelp(""),
  1.4447 -     /* 143 */ "ToBlob"           OpHelp(""),
  1.4448 -     /* 144 */ "ToNumeric"        OpHelp(""),
  1.4449 -     /* 145 */ "ToInt"            OpHelp(""),
  1.4450 -     /* 146 */ "ToReal"           OpHelp(""),
  1.4451 -     /* 147 */ "VRename"          OpHelp(""),
  1.4452 -     /* 148 */ "Pagecount"        OpHelp(""),
  1.4453 -     /* 149 */ "MaxPgcnt"         OpHelp(""),
  1.4454 -     /* 150 */ "Trace"            OpHelp(""),
  1.4455 -     /* 151 */ "Noop"             OpHelp(""),
  1.4456 -     /* 152 */ "Explain"          OpHelp(""),
  1.4457 +     /*  20 */ "InitCoroutine"    OpHelp(""),
  1.4458 +     /*  21 */ "EndCoroutine"     OpHelp(""),
  1.4459 +     /*  22 */ "Yield"            OpHelp(""),
  1.4460 +     /*  23 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
  1.4461 +     /*  24 */ "Halt"             OpHelp(""),
  1.4462 +     /*  25 */ "Integer"          OpHelp("r[P2]=P1"),
  1.4463 +     /*  26 */ "Int64"            OpHelp("r[P2]=P4"),
  1.4464 +     /*  27 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
  1.4465 +     /*  28 */ "Null"             OpHelp("r[P2..P3]=NULL"),
  1.4466 +     /*  29 */ "SoftNull"         OpHelp("r[P1]=NULL"),
  1.4467 +     /*  30 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
  1.4468 +     /*  31 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
  1.4469 +     /*  32 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
  1.4470 +     /*  33 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
  1.4471 +     /*  34 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
  1.4472 +     /*  35 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
  1.4473 +     /*  36 */ "CollSeq"          OpHelp(""),
  1.4474 +     /*  37 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
  1.4475 +     /*  38 */ "MustBeInt"        OpHelp(""),
  1.4476 +     /*  39 */ "RealAffinity"     OpHelp(""),
  1.4477 +     /*  40 */ "Permutation"      OpHelp(""),
  1.4478 +     /*  41 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
  1.4479 +     /*  42 */ "Jump"             OpHelp(""),
  1.4480 +     /*  43 */ "Once"             OpHelp(""),
  1.4481 +     /*  44 */ "If"               OpHelp(""),
  1.4482 +     /*  45 */ "IfNot"            OpHelp(""),
  1.4483 +     /*  46 */ "Column"           OpHelp("r[P3]=PX"),
  1.4484 +     /*  47 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
  1.4485 +     /*  48 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
  1.4486 +     /*  49 */ "Count"            OpHelp("r[P2]=count()"),
  1.4487 +     /*  50 */ "ReadCookie"       OpHelp(""),
  1.4488 +     /*  51 */ "SetCookie"        OpHelp(""),
  1.4489 +     /*  52 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
  1.4490 +     /*  53 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
  1.4491 +     /*  54 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
  1.4492 +     /*  55 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
  1.4493 +     /*  56 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
  1.4494 +     /*  57 */ "SorterOpen"       OpHelp(""),
  1.4495 +     /*  58 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
  1.4496 +     /*  59 */ "Close"            OpHelp(""),
  1.4497 +     /*  60 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
  1.4498 +     /*  61 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
  1.4499 +     /*  62 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
  1.4500 +     /*  63 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
  1.4501 +     /*  64 */ "Seek"             OpHelp("intkey=r[P2]"),
  1.4502 +     /*  65 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
  1.4503 +     /*  66 */ "NotFound"         OpHelp("key=r[P3@P4]"),
  1.4504 +     /*  67 */ "Found"            OpHelp("key=r[P3@P4]"),
  1.4505 +     /*  68 */ "NotExists"        OpHelp("intkey=r[P3]"),
  1.4506 +     /*  69 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
  1.4507 +     /*  70 */ "NewRowid"         OpHelp("r[P2]=rowid"),
  1.4508 +     /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
  1.4509 +     /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
  1.4510 +     /*  73 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
  1.4511 +     /*  74 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
  1.4512 +     /*  75 */ "Delete"           OpHelp(""),
  1.4513 +     /*  76 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
  1.4514 +     /*  77 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
  1.4515 +     /*  78 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
  1.4516 +     /*  79 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
  1.4517 +     /*  80 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
  1.4518 +     /*  81 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
  1.4519 +     /*  82 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
  1.4520 +     /*  83 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
  1.4521 +     /*  84 */ "ResetCount"       OpHelp(""),
  1.4522 +     /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
  1.4523 +     /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
  1.4524 +     /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
  1.4525 +     /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
  1.4526 +     /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
  1.4527 +     /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
  1.4528 +     /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
  1.4529 +     /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
  1.4530 +     /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
  1.4531 +     /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
  1.4532 +     /*  95 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
  1.4533 +     /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
  1.4534 +     /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
  1.4535 +     /*  98 */ "SorterData"       OpHelp("r[P2]=data"),
  1.4536 +     /*  99 */ "RowKey"           OpHelp("r[P2]=key"),
  1.4537 +     /* 100 */ "RowData"          OpHelp("r[P2]=data"),
  1.4538 +     /* 101 */ "Rowid"            OpHelp("r[P2]=rowid"),
  1.4539 +     /* 102 */ "NullRow"          OpHelp(""),
  1.4540 +     /* 103 */ "Last"             OpHelp(""),
  1.4541 +     /* 104 */ "SorterSort"       OpHelp(""),
  1.4542 +     /* 105 */ "Sort"             OpHelp(""),
  1.4543 +     /* 106 */ "Rewind"           OpHelp(""),
  1.4544 +     /* 107 */ "SorterInsert"     OpHelp(""),
  1.4545 +     /* 108 */ "IdxInsert"        OpHelp("key=r[P2]"),
  1.4546 +     /* 109 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
  1.4547 +     /* 110 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
  1.4548 +     /* 111 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
  1.4549 +     /* 112 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
  1.4550 +     /* 113 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
  1.4551 +     /* 114 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
  1.4552 +     /* 115 */ "Destroy"          OpHelp(""),
  1.4553 +     /* 116 */ "Clear"            OpHelp(""),
  1.4554 +     /* 117 */ "ResetSorter"      OpHelp(""),
  1.4555 +     /* 118 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
  1.4556 +     /* 119 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
  1.4557 +     /* 120 */ "ParseSchema"      OpHelp(""),
  1.4558 +     /* 121 */ "LoadAnalysis"     OpHelp(""),
  1.4559 +     /* 122 */ "DropTable"        OpHelp(""),
  1.4560 +     /* 123 */ "DropIndex"        OpHelp(""),
  1.4561 +     /* 124 */ "DropTrigger"      OpHelp(""),
  1.4562 +     /* 125 */ "IntegrityCk"      OpHelp(""),
  1.4563 +     /* 126 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
  1.4564 +     /* 127 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
  1.4565 +     /* 128 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
  1.4566 +     /* 129 */ "Program"          OpHelp(""),
  1.4567 +     /* 130 */ "Param"            OpHelp(""),
  1.4568 +     /* 131 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
  1.4569 +     /* 132 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
  1.4570 +     /* 133 */ "Real"             OpHelp("r[P2]=P4"),
  1.4571 +     /* 134 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
  1.4572 +     /* 135 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
  1.4573 +     /* 136 */ "IfNeg"            OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
  1.4574 +     /* 137 */ "IfZero"           OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
  1.4575 +     /* 138 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
  1.4576 +     /* 139 */ "IncrVacuum"       OpHelp(""),
  1.4577 +     /* 140 */ "Expire"           OpHelp(""),
  1.4578 +     /* 141 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
  1.4579 +     /* 142 */ "VBegin"           OpHelp(""),
  1.4580 +     /* 143 */ "ToText"           OpHelp(""),
  1.4581 +     /* 144 */ "ToBlob"           OpHelp(""),
  1.4582 +     /* 145 */ "ToNumeric"        OpHelp(""),
  1.4583 +     /* 146 */ "ToInt"            OpHelp(""),
  1.4584 +     /* 147 */ "ToReal"           OpHelp(""),
  1.4585 +     /* 148 */ "VCreate"          OpHelp(""),
  1.4586 +     /* 149 */ "VDestroy"         OpHelp(""),
  1.4587 +     /* 150 */ "VOpen"            OpHelp(""),
  1.4588 +     /* 151 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
  1.4589 +     /* 152 */ "VNext"            OpHelp(""),
  1.4590 +     /* 153 */ "VRename"          OpHelp(""),
  1.4591 +     /* 154 */ "Pagecount"        OpHelp(""),
  1.4592 +     /* 155 */ "MaxPgcnt"         OpHelp(""),
  1.4593 +     /* 156 */ "Init"             OpHelp("Start at P2"),
  1.4594 +     /* 157 */ "Noop"             OpHelp(""),
  1.4595 +     /* 158 */ "Explain"          OpHelp(""),
  1.4596    };
  1.4597    return azName[i];
  1.4598  }
  1.4599 @@ -23182,32 +24147,6 @@
  1.4600  #endif
  1.4601  
  1.4602  /*
  1.4603 -** These #defines should enable >2GB file support on Posix if the
  1.4604 -** underlying operating system supports it.  If the OS lacks
  1.4605 -** large file support, these should be no-ops.
  1.4606 -**
  1.4607 -** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
  1.4608 -** on the compiler command line.  This is necessary if you are compiling
  1.4609 -** on a recent machine (ex: RedHat 7.2) but you want your code to work
  1.4610 -** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
  1.4611 -** without this option, LFS is enable.  But LFS does not exist in the kernel
  1.4612 -** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
  1.4613 -** portability you should omit LFS.
  1.4614 -**
  1.4615 -** The previous paragraph was written in 2005.  (This paragraph is written
  1.4616 -** on 2008-11-28.) These days, all Linux kernels support large files, so
  1.4617 -** you should probably leave LFS enabled.  But some embedded platforms might
  1.4618 -** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
  1.4619 -*/
  1.4620 -#ifndef SQLITE_DISABLE_LFS
  1.4621 -# define _LARGE_FILE       1
  1.4622 -# ifndef _FILE_OFFSET_BITS
  1.4623 -#   define _FILE_OFFSET_BITS 64
  1.4624 -# endif
  1.4625 -# define _LARGEFILE_SOURCE 1
  1.4626 -#endif
  1.4627 -
  1.4628 -/*
  1.4629  ** standard include files.
  1.4630  */
  1.4631  #include <sys/types.h>
  1.4632 @@ -23218,11 +24157,10 @@
  1.4633  #include <sys/time.h>
  1.4634  #include <errno.h>
  1.4635  #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
  1.4636 -#include <sys/mman.h>
  1.4637 -#endif
  1.4638 -
  1.4639 -
  1.4640 -#if SQLITE_ENABLE_LOCKING_STYLE
  1.4641 +# include <sys/mman.h>
  1.4642 +#endif
  1.4643 +
  1.4644 +#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
  1.4645  # include <sys/ioctl.h>
  1.4646  # if OS_VXWORKS
  1.4647  #  include <semaphore.h>
  1.4648 @@ -23358,6 +24296,12 @@
  1.4649  #endif
  1.4650  };
  1.4651  
  1.4652 +/* This variable holds the process id (pid) from when the xRandomness()
  1.4653 +** method was called.  If xOpen() is called from a different process id,
  1.4654 +** indicating that a fork() has occurred, the PRNG will be reset.
  1.4655 +*/
  1.4656 +static int randomnessPid = 0;
  1.4657 +
  1.4658  /*
  1.4659  ** Allowed values for the unixFile.ctrlFlags bitmask:
  1.4660  */
  1.4661 @@ -23644,11 +24588,16 @@
  1.4662  ** we are not running as root.
  1.4663  */
  1.4664  static int posixFchown(int fd, uid_t uid, gid_t gid){
  1.4665 +#if OS_VXWORKS
  1.4666 +  return 0;
  1.4667 +#else
  1.4668    return geteuid() ? 0 : fchown(fd,uid,gid);
  1.4669 +#endif
  1.4670  }
  1.4671  
  1.4672  /* Forward reference */
  1.4673  static int openDirectory(const char*, int*);
  1.4674 +static int unixGetpagesize(void);
  1.4675  
  1.4676  /*
  1.4677  ** Many system calls are accessed through pointer-to-functions so that
  1.4678 @@ -23699,7 +24648,7 @@
  1.4679    { "read",         (sqlite3_syscall_ptr)read,       0  },
  1.4680  #define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
  1.4681  
  1.4682 -#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
  1.4683 +#if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
  1.4684    { "pread",        (sqlite3_syscall_ptr)pread,      0  },
  1.4685  #else
  1.4686    { "pread",        (sqlite3_syscall_ptr)0,          0  },
  1.4687 @@ -23716,7 +24665,7 @@
  1.4688    { "write",        (sqlite3_syscall_ptr)write,      0  },
  1.4689  #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
  1.4690  
  1.4691 -#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
  1.4692 +#if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
  1.4693    { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
  1.4694  #else
  1.4695    { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
  1.4696 @@ -23770,6 +24719,9 @@
  1.4697    { "mremap",       (sqlite3_syscall_ptr)0,               0 },
  1.4698  #endif
  1.4699  #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
  1.4700 +  { "getpagesize",  (sqlite3_syscall_ptr)unixGetpagesize, 0 },
  1.4701 +#define osGetpagesize ((int(*)(void))aSyscall[24].pCurrent)
  1.4702 +
  1.4703  #endif
  1.4704  
  1.4705  }; /* End of the overrideable system calls */
  1.4706 @@ -24083,16 +25035,6 @@
  1.4707    case EPERM: 
  1.4708      return SQLITE_PERM;
  1.4709      
  1.4710 -  /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
  1.4711 -  ** this module never makes such a call. And the code in SQLite itself 
  1.4712 -  ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
  1.4713 -  ** this case is also commented out. If the system does set errno to EDEADLK,
  1.4714 -  ** the default SQLITE_IOERR_XXX code will be returned. */
  1.4715 -#if 0
  1.4716 -  case EDEADLK:
  1.4717 -    return SQLITE_IOERR_BLOCKED;
  1.4718 -#endif
  1.4719 -    
  1.4720  #if EOPNOTSUPP!=ENOTSUP
  1.4721    case EOPNOTSUPP: 
  1.4722      /* something went terribly awry, unless during file system support 
  1.4723 @@ -24621,6 +25563,19 @@
  1.4724    return SQLITE_OK;
  1.4725  }
  1.4726  
  1.4727 +/*
  1.4728 +** Return TRUE if pFile has been renamed or unlinked since it was first opened.
  1.4729 +*/
  1.4730 +static int fileHasMoved(unixFile *pFile){
  1.4731 +#if OS_VXWORKS
  1.4732 +  return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
  1.4733 +#else
  1.4734 +  struct stat buf;
  1.4735 +  return pFile->pInode!=0 &&
  1.4736 +      (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
  1.4737 +#endif
  1.4738 +}
  1.4739 +
  1.4740  
  1.4741  /*
  1.4742  ** Check a unixFile that is a database.  Verify the following:
  1.4743 @@ -24655,10 +25610,7 @@
  1.4744      pFile->ctrlFlags |= UNIXFILE_WARNED;
  1.4745      return;
  1.4746    }
  1.4747 -  if( pFile->pInode!=0
  1.4748 -   && ((rc = osStat(pFile->zPath, &buf))!=0
  1.4749 -       || buf.st_ino!=pFile->pInode->fileId.ino)
  1.4750 -  ){
  1.4751 +  if( fileHasMoved(pFile) ){
  1.4752      sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
  1.4753      pFile->ctrlFlags |= UNIXFILE_WARNED;
  1.4754      return;
  1.4755 @@ -25235,6 +26187,13 @@
  1.4756      pFile->pId = 0;
  1.4757    }
  1.4758  #endif
  1.4759 +#ifdef SQLITE_UNLINK_AFTER_CLOSE
  1.4760 +  if( pFile->ctrlFlags & UNIXFILE_DELETE ){
  1.4761 +    osUnlink(pFile->zPath);
  1.4762 +    sqlite3_free(*(char**)&pFile->zPath);
  1.4763 +    pFile->zPath = 0;
  1.4764 +  }
  1.4765 +#endif
  1.4766    OSTRACE(("CLOSE   %-3d\n", pFile->h));
  1.4767    OpenCounter(-1);
  1.4768    sqlite3_free(pFile->pUnused);
  1.4769 @@ -25757,7 +26716,6 @@
  1.4770    /* Otherwise see if some other process holds it. */
  1.4771    if( !reserved ){
  1.4772      sem_t *pSem = pFile->pInode->pSem;
  1.4773 -    struct stat statBuf;
  1.4774  
  1.4775      if( sem_trywait(pSem)==-1 ){
  1.4776        int tErrno = errno;
  1.4777 @@ -25810,7 +26768,6 @@
  1.4778  */
  1.4779  static int semLock(sqlite3_file *id, int eFileLock) {
  1.4780    unixFile *pFile = (unixFile*)id;
  1.4781 -  int fd;
  1.4782    sem_t *pSem = pFile->pInode->pSem;
  1.4783    int rc = SQLITE_OK;
  1.4784  
  1.4785 @@ -27107,6 +28064,10 @@
  1.4786        }
  1.4787        return SQLITE_OK;
  1.4788      }
  1.4789 +    case SQLITE_FCNTL_HAS_MOVED: {
  1.4790 +      *(int*)pArg = fileHasMoved(pFile);
  1.4791 +      return SQLITE_OK;
  1.4792 +    }
  1.4793  #if SQLITE_MAX_MMAP_SIZE>0
  1.4794      case SQLITE_FCNTL_MMAP_SIZE: {
  1.4795        i64 newLimit = *(i64*)pArg;
  1.4796 @@ -27269,9 +28230,26 @@
  1.4797    return rc;
  1.4798  }
  1.4799  
  1.4800 +#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
  1.4801 +
  1.4802 +/*
  1.4803 +** Return the system page size.
  1.4804 +**
  1.4805 +** This function should not be called directly by other code in this file. 
  1.4806 +** Instead, it should be called via macro osGetpagesize().
  1.4807 +*/
  1.4808 +static int unixGetpagesize(void){
  1.4809 +#if defined(_BSD_SOURCE)
  1.4810 +  return getpagesize();
  1.4811 +#else
  1.4812 +  return (int)sysconf(_SC_PAGESIZE);
  1.4813 +#endif
  1.4814 +}
  1.4815 +
  1.4816 +#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
  1.4817 +
  1.4818  #ifndef SQLITE_OMIT_WAL
  1.4819  
  1.4820 -
  1.4821  /*
  1.4822  ** Object used to represent an shared memory buffer.  
  1.4823  **
  1.4824 @@ -27387,7 +28365,7 @@
  1.4825  #ifdef SQLITE_DEBUG
  1.4826    { u16 mask;
  1.4827    OSTRACE(("SHM-LOCK "));
  1.4828 -  mask = ofst>31 ? 0xffffffff : (1<<(ofst+n)) - (1<<ofst);
  1.4829 +  mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
  1.4830    if( rc==SQLITE_OK ){
  1.4831      if( lockType==F_UNLCK ){
  1.4832        OSTRACE(("unlock %d ok", ofst));
  1.4833 @@ -27421,6 +28399,22 @@
  1.4834    return rc;        
  1.4835  }
  1.4836  
  1.4837 +/*
  1.4838 +** Return the minimum number of 32KB shm regions that should be mapped at
  1.4839 +** a time, assuming that each mapping must be an integer multiple of the
  1.4840 +** current system page-size.
  1.4841 +**
  1.4842 +** Usually, this is 1. The exception seems to be systems that are configured
  1.4843 +** to use 64KB pages - in this case each mapping must cover at least two
  1.4844 +** shm regions.
  1.4845 +*/
  1.4846 +static int unixShmRegionPerMap(void){
  1.4847 +  int shmsz = 32*1024;            /* SHM region size */
  1.4848 +  int pgsz = osGetpagesize();   /* System page size */
  1.4849 +  assert( ((pgsz-1)&pgsz)==0 );   /* Page size must be a power of 2 */
  1.4850 +  if( pgsz<shmsz ) return 1;
  1.4851 +  return pgsz/shmsz;
  1.4852 +}
  1.4853  
  1.4854  /*
  1.4855  ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
  1.4856 @@ -27432,10 +28426,11 @@
  1.4857    unixShmNode *p = pFd->pInode->pShmNode;
  1.4858    assert( unixMutexHeld() );
  1.4859    if( p && p->nRef==0 ){
  1.4860 +    int nShmPerMap = unixShmRegionPerMap();
  1.4861      int i;
  1.4862      assert( p->pInode==pFd->pInode );
  1.4863      sqlite3_mutex_free(p->mutex);
  1.4864 -    for(i=0; i<p->nRegion; i++){
  1.4865 +    for(i=0; i<p->nRegion; i+=nShmPerMap){
  1.4866        if( p->h>=0 ){
  1.4867          osMunmap(p->apRegion[i], p->szRegion);
  1.4868        }else{
  1.4869 @@ -27642,6 +28637,8 @@
  1.4870    unixShm *p;
  1.4871    unixShmNode *pShmNode;
  1.4872    int rc = SQLITE_OK;
  1.4873 +  int nShmPerMap = unixShmRegionPerMap();
  1.4874 +  int nReqRegion;
  1.4875  
  1.4876    /* If the shared-memory file has not yet been opened, open it now. */
  1.4877    if( pDbFd->pShm==0 ){
  1.4878 @@ -27657,9 +28654,12 @@
  1.4879    assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
  1.4880    assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
  1.4881  
  1.4882 -  if( pShmNode->nRegion<=iRegion ){
  1.4883 +  /* Minimum number of regions required to be mapped. */
  1.4884 +  nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
  1.4885 +
  1.4886 +  if( pShmNode->nRegion<nReqRegion ){
  1.4887      char **apNew;                      /* New apRegion[] array */
  1.4888 -    int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
  1.4889 +    int nByte = nReqRegion*szRegion;   /* Minimum required file size */
  1.4890      struct stat sStat;                 /* Used by fstat() */
  1.4891  
  1.4892      pShmNode->szRegion = szRegion;
  1.4893 @@ -27708,17 +28708,19 @@
  1.4894  
  1.4895      /* Map the requested memory region into this processes address space. */
  1.4896      apNew = (char **)sqlite3_realloc(
  1.4897 -        pShmNode->apRegion, (iRegion+1)*sizeof(char *)
  1.4898 +        pShmNode->apRegion, nReqRegion*sizeof(char *)
  1.4899      );
  1.4900      if( !apNew ){
  1.4901        rc = SQLITE_IOERR_NOMEM;
  1.4902        goto shmpage_out;
  1.4903      }
  1.4904      pShmNode->apRegion = apNew;
  1.4905 -    while(pShmNode->nRegion<=iRegion){
  1.4906 +    while( pShmNode->nRegion<nReqRegion ){
  1.4907 +      int nMap = szRegion*nShmPerMap;
  1.4908 +      int i;
  1.4909        void *pMem;
  1.4910        if( pShmNode->h>=0 ){
  1.4911 -        pMem = osMmap(0, szRegion,
  1.4912 +        pMem = osMmap(0, nMap,
  1.4913              pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
  1.4914              MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
  1.4915          );
  1.4916 @@ -27734,8 +28736,11 @@
  1.4917          }
  1.4918          memset(pMem, 0, szRegion);
  1.4919        }
  1.4920 -      pShmNode->apRegion[pShmNode->nRegion] = pMem;
  1.4921 -      pShmNode->nRegion++;
  1.4922 +
  1.4923 +      for(i=0; i<nShmPerMap; i++){
  1.4924 +        pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
  1.4925 +      }
  1.4926 +      pShmNode->nRegion += nShmPerMap;
  1.4927      }
  1.4928    }
  1.4929  
  1.4930 @@ -27950,19 +28955,6 @@
  1.4931  }
  1.4932  
  1.4933  /*
  1.4934 -** Return the system page size.
  1.4935 -*/
  1.4936 -static int unixGetPagesize(void){
  1.4937 -#if HAVE_MREMAP
  1.4938 -  return 512;
  1.4939 -#elif defined(_BSD_SOURCE)
  1.4940 -  return getpagesize();
  1.4941 -#else
  1.4942 -  return (int)sysconf(_SC_PAGESIZE);
  1.4943 -#endif
  1.4944 -}
  1.4945 -
  1.4946 -/*
  1.4947  ** Attempt to set the size of the memory mapping maintained by file 
  1.4948  ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
  1.4949  **
  1.4950 @@ -27998,8 +28990,12 @@
  1.4951    if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
  1.4952  
  1.4953    if( pOrig ){
  1.4954 -    const int szSyspage = unixGetPagesize();
  1.4955 +#if HAVE_MREMAP
  1.4956 +    i64 nReuse = pFd->mmapSize;
  1.4957 +#else
  1.4958 +    const int szSyspage = osGetpagesize();
  1.4959      i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
  1.4960 +#endif
  1.4961      u8 *pReq = &pOrig[nReuse];
  1.4962  
  1.4963      /* Unmap any pages of the existing mapping that cannot be reused. */
  1.4964 @@ -28138,10 +29134,10 @@
  1.4965  ** may now be invalid and should be unmapped.
  1.4966  */
  1.4967  static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
  1.4968 +#if SQLITE_MAX_MMAP_SIZE>0
  1.4969    unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
  1.4970    UNUSED_PARAMETER(iOff);
  1.4971  
  1.4972 -#if SQLITE_MAX_MMAP_SIZE>0
  1.4973    /* If p==0 (unmap the entire file) then there must be no outstanding 
  1.4974    ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
  1.4975    ** then there must be at least one outstanding.  */
  1.4976 @@ -28157,6 +29153,10 @@
  1.4977    }
  1.4978  
  1.4979    assert( pFd->nFetchOut>=0 );
  1.4980 +#else
  1.4981 +  UNUSED_PARAMETER(fd);
  1.4982 +  UNUSED_PARAMETER(p);
  1.4983 +  UNUSED_PARAMETER(iOff);
  1.4984  #endif
  1.4985    return SQLITE_OK;
  1.4986  }
  1.4987 @@ -28947,6 +29947,16 @@
  1.4988         || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
  1.4989    );
  1.4990  
  1.4991 +  /* Detect a pid change and reset the PRNG.  There is a race condition
  1.4992 +  ** here such that two or more threads all trying to open databases at
  1.4993 +  ** the same instant might all reset the PRNG.  But multiple resets
  1.4994 +  ** are harmless.
  1.4995 +  */
  1.4996 +  if( randomnessPid!=getpid() ){
  1.4997 +    randomnessPid = getpid();
  1.4998 +    sqlite3_randomness(0,0);
  1.4999 +  }
  1.5000 +
  1.5001    memset(p, 0, sizeof(unixFile));
  1.5002  
  1.5003    if( eType==SQLITE_OPEN_MAIN_DB ){
  1.5004 @@ -29038,6 +30048,12 @@
  1.5005    if( isDelete ){
  1.5006  #if OS_VXWORKS
  1.5007      zPath = zName;
  1.5008 +#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
  1.5009 +    zPath = sqlite3_mprintf("%s", zName);
  1.5010 +    if( zPath==0 ){
  1.5011 +      robust_close(p, fd, __LINE__);
  1.5012 +      return SQLITE_NOMEM;
  1.5013 +    }
  1.5014  #else
  1.5015      osUnlink(zName);
  1.5016  #endif
  1.5017 @@ -29138,7 +30154,11 @@
  1.5018    UNUSED_PARAMETER(NotUsed);
  1.5019    SimulateIOError(return SQLITE_IOERR_DELETE);
  1.5020    if( osUnlink(zPath)==(-1) ){
  1.5021 -    if( errno==ENOENT ){
  1.5022 +    if( errno==ENOENT
  1.5023 +#if OS_VXWORKS
  1.5024 +        || errno==0x380003
  1.5025 +#endif
  1.5026 +    ){
  1.5027        rc = SQLITE_IOERR_DELETE_NOENT;
  1.5028      }else{
  1.5029        rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
  1.5030 @@ -29334,18 +30354,18 @@
  1.5031    ** tests repeatable.
  1.5032    */
  1.5033    memset(zBuf, 0, nBuf);
  1.5034 +  randomnessPid = getpid();  
  1.5035  #if !defined(SQLITE_TEST)
  1.5036    {
  1.5037 -    int pid, fd, got;
  1.5038 +    int fd, got;
  1.5039      fd = robust_open("/dev/urandom", O_RDONLY, 0);
  1.5040      if( fd<0 ){
  1.5041        time_t t;
  1.5042        time(&t);
  1.5043        memcpy(zBuf, &t, sizeof(t));
  1.5044 -      pid = getpid();
  1.5045 -      memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
  1.5046 -      assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
  1.5047 -      nBuf = sizeof(t) + sizeof(pid);
  1.5048 +      memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
  1.5049 +      assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
  1.5050 +      nBuf = sizeof(t) + sizeof(randomnessPid);
  1.5051      }else{
  1.5052        do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
  1.5053        robust_close(0, fd, __LINE__);
  1.5054 @@ -30731,7 +31751,7 @@
  1.5055  
  1.5056    /* Double-check that the aSyscall[] array has been constructed
  1.5057    ** correctly.  See ticket [bb3a86e890c8e96ab] */
  1.5058 -  assert( ArraySize(aSyscall)==24 );
  1.5059 +  assert( ArraySize(aSyscall)==25 );
  1.5060  
  1.5061    /* Register all VFSes defined in the aVfs[] array */
  1.5062    for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
  1.5063 @@ -30771,11 +31791,6 @@
  1.5064  */
  1.5065  #if SQLITE_OS_WIN               /* This file is used for Windows only */
  1.5066  
  1.5067 -#ifdef __CYGWIN__
  1.5068 -# include <sys/cygwin.h>
  1.5069 -# include <errno.h> /* amalgamator: keep */
  1.5070 -#endif
  1.5071 -
  1.5072  /*
  1.5073  ** Include code that is common to all os_*.c files
  1.5074  */
  1.5075 @@ -30990,6 +32005,10 @@
  1.5076  /************** Continuing where we left off in os_win.c *********************/
  1.5077  
  1.5078  /*
  1.5079 +** Include the header file for the Windows VFS.
  1.5080 +*/
  1.5081 +
  1.5082 +/*
  1.5083  ** Compiling and using WAL mode requires several APIs that are only
  1.5084  ** available in Windows platforms based on the NT kernel.
  1.5085  */
  1.5086 @@ -31036,18 +32055,14 @@
  1.5087  #endif
  1.5088  
  1.5089  /*
  1.5090 -** Check if the GetVersionEx[AW] functions should be considered deprecated
  1.5091 -** and avoid using them in that case.  It should be noted here that if the
  1.5092 -** value of the SQLITE_WIN32_GETVERSIONEX pre-processor macro is zero
  1.5093 -** (whether via this block or via being manually specified), that implies
  1.5094 -** the underlying operating system will always be based on the Windows NT
  1.5095 -** Kernel.
  1.5096 +** Check to see if the GetVersionEx[AW] functions are deprecated on the
  1.5097 +** target system.  GetVersionEx was first deprecated in Win8.1.
  1.5098  */
  1.5099  #ifndef SQLITE_WIN32_GETVERSIONEX
  1.5100  #  if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
  1.5101 -#    define SQLITE_WIN32_GETVERSIONEX   0
  1.5102 +#    define SQLITE_WIN32_GETVERSIONEX   0   /* GetVersionEx() is deprecated */
  1.5103  #  else
  1.5104 -#    define SQLITE_WIN32_GETVERSIONEX   1
  1.5105 +#    define SQLITE_WIN32_GETVERSIONEX   1   /* GetVersionEx() is current */
  1.5106  #  endif
  1.5107  #endif
  1.5108  
  1.5109 @@ -31119,7 +32134,7 @@
  1.5110  ** [sometimes] not used by the code (e.g. via conditional compilation).
  1.5111  */
  1.5112  #ifndef UNUSED_VARIABLE_VALUE
  1.5113 -#  define UNUSED_VARIABLE_VALUE(x) (void)(x)
  1.5114 +#  define UNUSED_VARIABLE_VALUE(x)      (void)(x)
  1.5115  #endif
  1.5116  
  1.5117  /*
  1.5118 @@ -31168,7 +32183,7 @@
  1.5119  ** Some Microsoft compilers lack this definition.
  1.5120  */
  1.5121  #ifndef INVALID_FILE_ATTRIBUTES
  1.5122 -# define INVALID_FILE_ATTRIBUTES ((DWORD)-1) 
  1.5123 +# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
  1.5124  #endif
  1.5125  
  1.5126  #ifndef FILE_FLAG_MASK
  1.5127 @@ -31218,7 +32233,7 @@
  1.5128    int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
  1.5129  #if SQLITE_OS_WINCE
  1.5130    LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
  1.5131 -  HANDLE hMutex;          /* Mutex used to control access to shared lock */  
  1.5132 +  HANDLE hMutex;          /* Mutex used to control access to shared lock */
  1.5133    HANDLE hShared;         /* Shared memory segment used for locking */
  1.5134    winceLock local;        /* Locks obtained by this instance of winFile */
  1.5135    winceLock *shared;      /* Global shared lock memory for the file  */
  1.5136 @@ -31378,10 +32393,9 @@
  1.5137  ** can manually set this value to 1 to emulate Win98 behavior.
  1.5138  */
  1.5139  #ifdef SQLITE_TEST
  1.5140 -SQLITE_API int sqlite3_os_type = 0;
  1.5141 -#elif !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
  1.5142 -      defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_HAS_WIDE)
  1.5143 -static int sqlite3_os_type = 0;
  1.5144 +SQLITE_API LONG volatile sqlite3_os_type = 0;
  1.5145 +#else
  1.5146 +static LONG volatile sqlite3_os_type = 0;
  1.5147  #endif
  1.5148  
  1.5149  #ifndef SYSCALL
  1.5150 @@ -32012,6 +33026,22 @@
  1.5151  #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
  1.5152          LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
  1.5153  
  1.5154 +/*
  1.5155 +** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
  1.5156 +**       is really just a macro that uses a compiler intrinsic (e.g. x64).
  1.5157 +**       So do not try to make this is into a redefinable interface.
  1.5158 +*/
  1.5159 +#if defined(InterlockedCompareExchange)
  1.5160 +  { "InterlockedCompareExchange", (SYSCALL)0,                    0 },
  1.5161 +
  1.5162 +#define osInterlockedCompareExchange InterlockedCompareExchange
  1.5163 +#else
  1.5164 +  { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
  1.5165 +
  1.5166 +#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG volatile*, \
  1.5167 +        LONG,LONG))aSyscall[76].pCurrent)
  1.5168 +#endif /* defined(InterlockedCompareExchange) */
  1.5169 +
  1.5170  }; /* End of the overrideable system calls */
  1.5171  
  1.5172  /*
  1.5173 @@ -32262,22 +33292,38 @@
  1.5174  #elif !defined(SQLITE_WIN32_HAS_WIDE)
  1.5175  # define osIsNT()  (0)
  1.5176  #else
  1.5177 -  static int osIsNT(void){
  1.5178 -    if( sqlite3_os_type==0 ){
  1.5179 -#if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
  1.5180 -      OSVERSIONINFOW sInfo;
  1.5181 -      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  1.5182 -      osGetVersionExW(&sInfo);
  1.5183 -#else
  1.5184 -      OSVERSIONINFOA sInfo;
  1.5185 -      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  1.5186 -      osGetVersionExA(&sInfo);
  1.5187 -#endif
  1.5188 -      sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
  1.5189 -    }
  1.5190 -    return sqlite3_os_type==2;
  1.5191 -  }
  1.5192 -#endif
  1.5193 +# define osIsNT()  ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
  1.5194 +#endif
  1.5195 +
  1.5196 +/*
  1.5197 +** This function determines if the machine is running a version of Windows
  1.5198 +** based on the NT kernel.
  1.5199 +*/
  1.5200 +SQLITE_API int sqlite3_win32_is_nt(void){
  1.5201 +#if defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
  1.5202 +  if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
  1.5203 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
  1.5204 +        defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
  1.5205 +    OSVERSIONINFOW sInfo;
  1.5206 +    sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  1.5207 +    osGetVersionExW(&sInfo);
  1.5208 +    osInterlockedCompareExchange(&sqlite3_os_type,
  1.5209 +        (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
  1.5210 +#elif defined(SQLITE_WIN32_HAS_ANSI)
  1.5211 +    OSVERSIONINFOA sInfo;
  1.5212 +    sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  1.5213 +    osGetVersionExA(&sInfo);
  1.5214 +    osInterlockedCompareExchange(&sqlite3_os_type,
  1.5215 +        (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
  1.5216 +#endif
  1.5217 +  }
  1.5218 +  return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
  1.5219 +#elif SQLITE_TEST
  1.5220 +  return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
  1.5221 +#else
  1.5222 +  return 1;
  1.5223 +#endif
  1.5224 +}
  1.5225  
  1.5226  #ifdef SQLITE_WIN32_MALLOC
  1.5227  /*
  1.5228 @@ -32485,7 +33531,7 @@
  1.5229  #endif /* SQLITE_WIN32_MALLOC */
  1.5230  
  1.5231  /*
  1.5232 -** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). 
  1.5233 +** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
  1.5234  **
  1.5235  ** Space to hold the returned string is obtained from malloc.
  1.5236  */
  1.5237 @@ -32538,7 +33584,7 @@
  1.5238  /*
  1.5239  ** Convert an ANSI string to Microsoft Unicode, based on the
  1.5240  ** current codepage settings for file apis.
  1.5241 -** 
  1.5242 +**
  1.5243  ** Space to hold the returned string is obtained
  1.5244  ** from sqlite3_malloc.
  1.5245  */
  1.5246 @@ -32612,7 +33658,7 @@
  1.5247  }
  1.5248  
  1.5249  /*
  1.5250 -** Convert UTF-8 to multibyte character string.  Space to hold the 
  1.5251 +** Convert UTF-8 to multibyte character string.  Space to hold the
  1.5252  ** returned string is obtained from sqlite3_malloc().
  1.5253  */
  1.5254  SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
  1.5255 @@ -32752,11 +33798,11 @@
  1.5256  **
  1.5257  ** This routine is invoked after an error occurs in an OS function.
  1.5258  ** It logs a message using sqlite3_log() containing the current value of
  1.5259 -** error code and, if possible, the human-readable equivalent from 
  1.5260 +** error code and, if possible, the human-readable equivalent from
  1.5261  ** FormatMessage.
  1.5262  **
  1.5263  ** The first argument passed to the macro should be the error code that
  1.5264 -** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
  1.5265 +** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
  1.5266  ** The two subsequent arguments should be the name of the OS function that
  1.5267  ** failed and the associated file-system path, if any.
  1.5268  */
  1.5269 @@ -32787,7 +33833,7 @@
  1.5270  
  1.5271  /*
  1.5272  ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
  1.5273 -** will be retried following a locking error - probably caused by 
  1.5274 +** will be retried following a locking error - probably caused by
  1.5275  ** antivirus software.  Also the initial delay before the first retry.
  1.5276  ** The delay increases linearly with each retry.
  1.5277  */
  1.5278 @@ -32801,6 +33847,32 @@
  1.5279  static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
  1.5280  
  1.5281  /*
  1.5282 +** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
  1.5283 +** error code obtained via GetLastError() is eligible to be retried.  It
  1.5284 +** must accept the error code DWORD as its only argument and should return
  1.5285 +** non-zero if the error code is transient in nature and the operation
  1.5286 +** responsible for generating the original error might succeed upon being
  1.5287 +** retried.  The argument to this macro should be a variable.
  1.5288 +**
  1.5289 +** Additionally, a macro named "winIoerrCanRetry2" may be defined.  If it
  1.5290 +** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
  1.5291 +** returns zero.  The "winIoerrCanRetry2" macro is completely optional and
  1.5292 +** may be used to include additional error codes in the set that should
  1.5293 +** result in the failing I/O operation being retried by the caller.  If
  1.5294 +** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
  1.5295 +** identical to those of the "winIoerrCanRetry1" macro.
  1.5296 +*/
  1.5297 +#if !defined(winIoerrCanRetry1)
  1.5298 +#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED)        || \
  1.5299 +                              ((a)==ERROR_SHARING_VIOLATION)    || \
  1.5300 +                              ((a)==ERROR_LOCK_VIOLATION)       || \
  1.5301 +                              ((a)==ERROR_DEV_NOT_EXIST)        || \
  1.5302 +                              ((a)==ERROR_NETNAME_DELETED)      || \
  1.5303 +                              ((a)==ERROR_SEM_TIMEOUT)          || \
  1.5304 +                              ((a)==ERROR_NETWORK_UNREACHABLE))
  1.5305 +#endif
  1.5306 +
  1.5307 +/*
  1.5308  ** If a ReadFile() or WriteFile() error occurs, invoke this routine
  1.5309  ** to see if it should be retried.  Return TRUE to retry.  Return FALSE
  1.5310  ** to give up with an error.
  1.5311 @@ -32813,13 +33885,18 @@
  1.5312      }
  1.5313      return 0;
  1.5314    }
  1.5315 -  if( e==ERROR_ACCESS_DENIED ||
  1.5316 -      e==ERROR_LOCK_VIOLATION ||
  1.5317 -      e==ERROR_SHARING_VIOLATION ){
  1.5318 +  if( winIoerrCanRetry1(e) ){
  1.5319      sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
  1.5320      ++*pnRetry;
  1.5321      return 1;
  1.5322    }
  1.5323 +#if defined(winIoerrCanRetry2)
  1.5324 +  else if( winIoerrCanRetry2(e) ){
  1.5325 +    sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
  1.5326 +    ++*pnRetry;
  1.5327 +    return 1;
  1.5328 +  }
  1.5329 +#endif
  1.5330    if( pError ){
  1.5331      *pError = e;
  1.5332    }
  1.5333 @@ -32831,7 +33908,7 @@
  1.5334  */
  1.5335  static void winLogIoerr(int nRetry){
  1.5336    if( nRetry ){
  1.5337 -    sqlite3_log(SQLITE_IOERR, 
  1.5338 +    sqlite3_log(SQLITE_IOERR,
  1.5339        "delayed %dms for lock/sharing conflict",
  1.5340        winIoerrRetryDelay*nRetry*(nRetry+1)/2
  1.5341      );
  1.5342 @@ -32925,17 +34002,17 @@
  1.5343  
  1.5344    /* Acquire the mutex before continuing */
  1.5345    winceMutexAcquire(pFile->hMutex);
  1.5346 -  
  1.5347 -  /* Since the names of named mutexes, semaphores, file mappings etc are 
  1.5348 +
  1.5349 +  /* Since the names of named mutexes, semaphores, file mappings etc are
  1.5350    ** case-sensitive, take advantage of that by uppercasing the mutex name
  1.5351    ** and using that as the shared filemapping name.
  1.5352    */
  1.5353    osCharUpperW(zName);
  1.5354    pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
  1.5355                                          PAGE_READWRITE, 0, sizeof(winceLock),
  1.5356 -                                        zName);  
  1.5357 -
  1.5358 -  /* Set a flag that indicates we're the first to create the memory so it 
  1.5359 +                                        zName);
  1.5360 +
  1.5361 +  /* Set a flag that indicates we're the first to create the memory so it
  1.5362    ** must be zero-initialized */
  1.5363    lastErrno = osGetLastError();
  1.5364    if (lastErrno == ERROR_ALREADY_EXISTS){
  1.5365 @@ -32946,7 +34023,7 @@
  1.5366  
  1.5367    /* If we succeeded in making the shared memory handle, map it. */
  1.5368    if( pFile->hShared ){
  1.5369 -    pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, 
  1.5370 +    pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
  1.5371               FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
  1.5372      /* If mapping failed, close the shared memory handle and erase it */
  1.5373      if( !pFile->shared ){
  1.5374 @@ -32972,7 +34049,7 @@
  1.5375      pFile->hMutex = NULL;
  1.5376      return SQLITE_IOERR;
  1.5377    }
  1.5378 -  
  1.5379 +
  1.5380    /* Initialize the shared memory if we're supposed to */
  1.5381    if( bInit ){
  1.5382      memset(pFile->shared, 0, sizeof(winceLock));
  1.5383 @@ -33010,13 +34087,13 @@
  1.5384      osCloseHandle(pFile->hShared);
  1.5385  
  1.5386      /* Done with the mutex */
  1.5387 -    winceMutexRelease(pFile->hMutex);    
  1.5388 +    winceMutexRelease(pFile->hMutex);
  1.5389      osCloseHandle(pFile->hMutex);
  1.5390      pFile->hMutex = NULL;
  1.5391    }
  1.5392  }
  1.5393  
  1.5394 -/* 
  1.5395 +/*
  1.5396  ** An implementation of the LockFile() API of Windows for CE
  1.5397  */
  1.5398  static BOOL winceLockFile(
  1.5399 @@ -33227,8 +34304,8 @@
  1.5400  #endif
  1.5401  
  1.5402  /*
  1.5403 -** Move the current position of the file handle passed as the first 
  1.5404 -** argument to offset iOffset within the file. If successful, return 0. 
  1.5405 +** Move the current position of the file handle passed as the first
  1.5406 +** argument to offset iOffset within the file. If successful, return 0.
  1.5407  ** Otherwise, set pFile->lastErrno and return non-zero.
  1.5408  */
  1.5409  static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
  1.5410 @@ -33243,11 +34320,11 @@
  1.5411    upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
  1.5412    lowerBits = (LONG)(iOffset & 0xffffffff);
  1.5413  
  1.5414 -  /* API oddity: If successful, SetFilePointer() returns a dword 
  1.5415 +  /* API oddity: If successful, SetFilePointer() returns a dword
  1.5416    ** containing the lower 32-bits of the new file-offset. Or, if it fails,
  1.5417 -  ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, 
  1.5418 -  ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine 
  1.5419 -  ** whether an error has actually occurred, it is also necessary to call 
  1.5420 +  ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
  1.5421 +  ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
  1.5422 +  ** whether an error has actually occurred, it is also necessary to call
  1.5423    ** GetLastError().
  1.5424    */
  1.5425    dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
  1.5426 @@ -33330,7 +34407,7 @@
  1.5427      int cnt = 0;
  1.5428      while(
  1.5429             osDeleteFileW(pFile->zDeleteOnClose)==0
  1.5430 -        && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff 
  1.5431 +        && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
  1.5432          && cnt++ < WINCE_DELETION_ATTEMPTS
  1.5433      ){
  1.5434         sqlite3_win32_sleep(100);  /* Wait a little before trying again */
  1.5435 @@ -33758,7 +34835,7 @@
  1.5436      pFile->lastErrno = osGetLastError();
  1.5437      /* No need to log a failure to lock */
  1.5438    }
  1.5439 -  OSTRACE(("READ-LOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
  1.5440 +  OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
  1.5441    return res;
  1.5442  }
  1.5443  
  1.5444 @@ -33782,7 +34859,7 @@
  1.5445      winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
  1.5446                  "winUnlockReadLock", pFile->zPath);
  1.5447    }
  1.5448 -  OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
  1.5449 +  OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
  1.5450    return res;
  1.5451  }
  1.5452  
  1.5453 @@ -33857,8 +34934,16 @@
  1.5454        ** If you are using this code as a model for alternative VFSes, do not
  1.5455        ** copy this retry logic.  It is a hack intended for Windows only.
  1.5456        */
  1.5457 -      OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, rc=%s\n",
  1.5458 -               pFile->h, cnt, sqlite3ErrName(res)));
  1.5459 +      lastErrno = osGetLastError();
  1.5460 +      OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
  1.5461 +               pFile->h, cnt, res));
  1.5462 +      if( lastErrno==ERROR_INVALID_HANDLE ){
  1.5463 +        pFile->lastErrno = lastErrno;
  1.5464 +        rc = SQLITE_IOERR_LOCK;
  1.5465 +        OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
  1.5466 +                 pFile->h, cnt, sqlite3ErrName(rc)));
  1.5467 +        return rc;
  1.5468 +      }
  1.5469        if( cnt ) sqlite3_win32_sleep(1);
  1.5470      }
  1.5471      gotPendingLock = res;
  1.5472 @@ -33943,7 +35028,7 @@
  1.5473  ** non-zero, otherwise zero.
  1.5474  */
  1.5475  static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
  1.5476 -  int rc;
  1.5477 +  int res;
  1.5478    winFile *pFile = (winFile*)id;
  1.5479  
  1.5480    SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  1.5481 @@ -33951,17 +35036,17 @@
  1.5482  
  1.5483    assert( id!=0 );
  1.5484    if( pFile->locktype>=RESERVED_LOCK ){
  1.5485 -    rc = 1;
  1.5486 -    OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (local)\n", pFile->h, rc));
  1.5487 -  }else{
  1.5488 -    rc = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
  1.5489 -    if( rc ){
  1.5490 +    res = 1;
  1.5491 +    OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
  1.5492 +  }else{
  1.5493 +    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
  1.5494 +    if( res ){
  1.5495        winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
  1.5496      }
  1.5497 -    rc = !rc;
  1.5498 -    OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (remote)\n", pFile->h, rc));
  1.5499 -  }
  1.5500 -  *pResOut = rc;
  1.5501 +    res = !res;
  1.5502 +    OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
  1.5503 +  }
  1.5504 +  *pResOut = res;
  1.5505    OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
  1.5506             pFile->h, pResOut, *pResOut));
  1.5507    return SQLITE_OK;
  1.5508 @@ -34083,7 +35168,7 @@
  1.5509        return SQLITE_OK;
  1.5510      }
  1.5511      case SQLITE_FCNTL_VFSNAME: {
  1.5512 -      *(char**)pArg = sqlite3_mprintf("win32");
  1.5513 +      *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
  1.5514        OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  1.5515        return SQLITE_OK;
  1.5516      }
  1.5517 @@ -34102,6 +35187,17 @@
  1.5518        OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  1.5519        return SQLITE_OK;
  1.5520      }
  1.5521 +#ifdef SQLITE_TEST
  1.5522 +    case SQLITE_FCNTL_WIN32_SET_HANDLE: {
  1.5523 +      LPHANDLE phFile = (LPHANDLE)pArg;
  1.5524 +      HANDLE hOldFile = pFile->h;
  1.5525 +      pFile->h = *phFile;
  1.5526 +      *phFile = hOldFile;
  1.5527 +      OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
  1.5528 +               hOldFile, pFile->h));
  1.5529 +      return SQLITE_OK;
  1.5530 +    }
  1.5531 +#endif
  1.5532      case SQLITE_FCNTL_TEMPFILENAME: {
  1.5533        char *zTFile = 0;
  1.5534        int rc = winGetTempname(pFile->pVfs, &zTFile);
  1.5535 @@ -34159,23 +35255,23 @@
  1.5536           ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
  1.5537  }
  1.5538  
  1.5539 -/* 
  1.5540 +/*
  1.5541  ** Windows will only let you create file view mappings
  1.5542  ** on allocation size granularity boundaries.
  1.5543  ** During sqlite3_os_init() we do a GetSystemInfo()
  1.5544  ** to get the granularity size.
  1.5545  */
  1.5546 -SYSTEM_INFO winSysInfo;
  1.5547 +static SYSTEM_INFO winSysInfo;
  1.5548  
  1.5549  #ifndef SQLITE_OMIT_WAL
  1.5550  
  1.5551  /*
  1.5552  ** Helper functions to obtain and relinquish the global mutex. The
  1.5553 -** global mutex is used to protect the winLockInfo objects used by 
  1.5554 +** global mutex is used to protect the winLockInfo objects used by
  1.5555  ** this file, all of which may be shared by multiple threads.
  1.5556  **
  1.5557 -** Function winShmMutexHeld() is used to assert() that the global mutex 
  1.5558 -** is held when required. This function is only used as part of assert() 
  1.5559 +** Function winShmMutexHeld() is used to assert() that the global mutex
  1.5560 +** is held when required. This function is only used as part of assert()
  1.5561  ** statements. e.g.
  1.5562  **
  1.5563  **   winShmEnterMutex()
  1.5564 @@ -34188,7 +35284,7 @@
  1.5565  static void winShmLeaveMutex(void){
  1.5566    sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  1.5567  }
  1.5568 -#ifdef SQLITE_DEBUG
  1.5569 +#ifndef NDEBUG
  1.5570  static int winShmMutexHeld(void) {
  1.5571    return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  1.5572  }
  1.5573 @@ -34205,10 +35301,10 @@
  1.5574  ** this object or while reading or writing the following fields:
  1.5575  **
  1.5576  **      nRef
  1.5577 -**      pNext 
  1.5578 +**      pNext
  1.5579  **
  1.5580  ** The following fields are read-only after the object is created:
  1.5581 -** 
  1.5582 +**
  1.5583  **      fid
  1.5584  **      zFilename
  1.5585  **
  1.5586 @@ -34304,7 +35400,7 @@
  1.5587      if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
  1.5588      rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
  1.5589    }
  1.5590 -  
  1.5591 +
  1.5592    if( rc!= 0 ){
  1.5593      rc = SQLITE_OK;
  1.5594    }else{
  1.5595 @@ -34400,7 +35496,7 @@
  1.5596    }
  1.5597    pNew->zFilename = (char*)&pNew[1];
  1.5598    sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
  1.5599 -  sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
  1.5600 +  sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
  1.5601  
  1.5602    /* Look to see if there is an existing winShmNode that can be used.
  1.5603    ** If no matching winShmNode currently exists, create a new one.
  1.5604 @@ -34437,7 +35533,7 @@
  1.5605      }
  1.5606  
  1.5607      /* Check to see if another process is holding the dead-man switch.
  1.5608 -    ** If not, truncate the file to zero length. 
  1.5609 +    ** If not, truncate the file to zero length.
  1.5610      */
  1.5611      if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
  1.5612        rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
  1.5613 @@ -34466,7 +35562,7 @@
  1.5614    ** the cover of the winShmEnterMutex() mutex and the pointer from the
  1.5615    ** new (struct winShm) object to the pShmNode has been set. All that is
  1.5616    ** left to do is to link the new object into the linked list starting
  1.5617 -  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
  1.5618 +  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
  1.5619    ** mutex.
  1.5620    */
  1.5621    sqlite3_mutex_enter(pShmNode->mutex);
  1.5622 @@ -34486,7 +35582,7 @@
  1.5623  }
  1.5624  
  1.5625  /*
  1.5626 -** Close a connection to shared-memory.  Delete the underlying 
  1.5627 +** Close a connection to shared-memory.  Delete the underlying
  1.5628  ** storage if deleteFlag is true.
  1.5629  */
  1.5630  static int winShmUnmap(
  1.5631 @@ -34575,7 +35671,7 @@
  1.5632      if( rc==SQLITE_OK ){
  1.5633        p->exclMask &= ~mask;
  1.5634        p->sharedMask &= ~mask;
  1.5635 -    } 
  1.5636 +    }
  1.5637    }else if( flags & SQLITE_SHM_SHARED ){
  1.5638      u16 allShared = 0;  /* Union of locks held by connections other than "p" */
  1.5639  
  1.5640 @@ -34614,7 +35710,7 @@
  1.5641          break;
  1.5642        }
  1.5643      }
  1.5644 -  
  1.5645 +
  1.5646      /* Get the exclusive locks at the system level.  Then if successful
  1.5647      ** also mark the local connection as being locked.
  1.5648      */
  1.5649 @@ -34634,7 +35730,7 @@
  1.5650  }
  1.5651  
  1.5652  /*
  1.5653 -** Implement a memory barrier or memory fence on shared memory.  
  1.5654 +** Implement a memory barrier or memory fence on shared memory.
  1.5655  **
  1.5656  ** All loads and stores begun before the barrier must complete before
  1.5657  ** any load or store begun after the barrier.
  1.5658 @@ -34649,22 +35745,22 @@
  1.5659  }
  1.5660  
  1.5661  /*
  1.5662 -** This function is called to obtain a pointer to region iRegion of the 
  1.5663 -** shared-memory associated with the database file fd. Shared-memory regions 
  1.5664 -** are numbered starting from zero. Each shared-memory region is szRegion 
  1.5665 +** This function is called to obtain a pointer to region iRegion of the
  1.5666 +** shared-memory associated with the database file fd. Shared-memory regions
  1.5667 +** are numbered starting from zero. Each shared-memory region is szRegion
  1.5668  ** bytes in size.
  1.5669  **
  1.5670  ** If an error occurs, an error code is returned and *pp is set to NULL.
  1.5671  **
  1.5672  ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
  1.5673  ** region has not been allocated (by any client, including one running in a
  1.5674 -** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
  1.5675 -** isWrite is non-zero and the requested shared-memory region has not yet 
  1.5676 +** separate process), then *pp is set to NULL and SQLITE_OK returned. If
  1.5677 +** isWrite is non-zero and the requested shared-memory region has not yet
  1.5678  ** been allocated, it is allocated by this function.
  1.5679  **
  1.5680  ** If the shared-memory region has already been allocated or is allocated by
  1.5681 -** this call as described above, then it is mapped into this processes 
  1.5682 -** address space (if it is not already), *pp is set to point to the mapped 
  1.5683 +** this call as described above, then it is mapped into this processes
  1.5684 +** address space (if it is not already), *pp is set to point to the mapped
  1.5685  ** memory and SQLITE_OK returned.
  1.5686  */
  1.5687  static int winShmMap(
  1.5688 @@ -34736,17 +35832,17 @@
  1.5689      while( pShmNode->nRegion<=iRegion ){
  1.5690        HANDLE hMap = NULL;         /* file-mapping handle */
  1.5691        void *pMap = 0;             /* Mapped memory region */
  1.5692 -     
  1.5693 +
  1.5694  #if SQLITE_OS_WINRT
  1.5695        hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
  1.5696            NULL, PAGE_READWRITE, nByte, NULL
  1.5697        );
  1.5698  #elif defined(SQLITE_WIN32_HAS_WIDE)
  1.5699 -      hMap = osCreateFileMappingW(pShmNode->hFile.h, 
  1.5700 +      hMap = osCreateFileMappingW(pShmNode->hFile.h,
  1.5701            NULL, PAGE_READWRITE, 0, nByte, NULL
  1.5702        );
  1.5703  #elif defined(SQLITE_WIN32_HAS_ANSI)
  1.5704 -      hMap = osCreateFileMappingA(pShmNode->hFile.h, 
  1.5705 +      hMap = osCreateFileMappingA(pShmNode->hFile.h,
  1.5706            NULL, PAGE_READWRITE, 0, nByte, NULL
  1.5707        );
  1.5708  #endif
  1.5709 @@ -34843,14 +35939,14 @@
  1.5710  
  1.5711  /*
  1.5712  ** Memory map or remap the file opened by file-descriptor pFd (if the file
  1.5713 -** is already mapped, the existing mapping is replaced by the new). Or, if 
  1.5714 -** there already exists a mapping for this file, and there are still 
  1.5715 +** is already mapped, the existing mapping is replaced by the new). Or, if
  1.5716 +** there already exists a mapping for this file, and there are still
  1.5717  ** outstanding xFetch() references to it, this function is a no-op.
  1.5718  **
  1.5719 -** If parameter nByte is non-negative, then it is the requested size of 
  1.5720 -** the mapping to create. Otherwise, if nByte is less than zero, then the 
  1.5721 +** If parameter nByte is non-negative, then it is the requested size of
  1.5722 +** the mapping to create. Otherwise, if nByte is less than zero, then the
  1.5723  ** requested size is the size of the file on disk. The actual size of the
  1.5724 -** created mapping is either the requested size or the value configured 
  1.5725 +** created mapping is either the requested size or the value configured
  1.5726  ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
  1.5727  **
  1.5728  ** SQLITE_OK is returned if no error occurs (even if the mapping is not
  1.5729 @@ -34879,7 +35975,7 @@
  1.5730      nMap = pFd->mmapSizeMax;
  1.5731    }
  1.5732    nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
  1.5733 - 
  1.5734 +
  1.5735    if( nMap==0 && pFd->mmapSize>0 ){
  1.5736      winUnmapfile(pFd);
  1.5737    }
  1.5738 @@ -34951,7 +36047,7 @@
  1.5739  ** Finally, if an error does occur, return an SQLite error code. The final
  1.5740  ** value of *pp is undefined in this case.
  1.5741  **
  1.5742 -** If this function does return a pointer, the caller must eventually 
  1.5743 +** If this function does return a pointer, the caller must eventually
  1.5744  ** release the reference by calling winUnfetch().
  1.5745  */
  1.5746  static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
  1.5747 @@ -34986,20 +36082,20 @@
  1.5748  }
  1.5749  
  1.5750  /*
  1.5751 -** If the third argument is non-NULL, then this function releases a 
  1.5752 +** If the third argument is non-NULL, then this function releases a
  1.5753  ** reference obtained by an earlier call to winFetch(). The second
  1.5754  ** argument passed to this function must be the same as the corresponding
  1.5755 -** argument that was passed to the winFetch() invocation. 
  1.5756 -**
  1.5757 -** Or, if the third argument is NULL, then this function is being called 
  1.5758 -** to inform the VFS layer that, according to POSIX, any existing mapping 
  1.5759 +** argument that was passed to the winFetch() invocation.
  1.5760 +**
  1.5761 +** Or, if the third argument is NULL, then this function is being called
  1.5762 +** to inform the VFS layer that, according to POSIX, any existing mapping
  1.5763  ** may now be invalid and should be unmapped.
  1.5764  */
  1.5765  static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
  1.5766  #if SQLITE_MAX_MMAP_SIZE>0
  1.5767    winFile *pFd = (winFile*)fd;   /* The underlying database file */
  1.5768  
  1.5769 -  /* If p==0 (unmap the entire file) then there must be no outstanding 
  1.5770 +  /* If p==0 (unmap the entire file) then there must be no outstanding
  1.5771    ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
  1.5772    ** then there must be at least one outstanding.  */
  1.5773    assert( (p==0)==(pFd->nFetchOut==0) );
  1.5774 @@ -35145,7 +36241,7 @@
  1.5775  
  1.5776    /* It's odd to simulate an io-error here, but really this is just
  1.5777    ** using the io-error infrastructure to test that SQLite handles this
  1.5778 -  ** function failing. 
  1.5779 +  ** function failing.
  1.5780    */
  1.5781    SimulateIOError( return SQLITE_IOERR );
  1.5782  
  1.5783 @@ -35327,7 +36423,7 @@
  1.5784    }
  1.5785  
  1.5786    /*
  1.5787 -  ** Check that the output buffer is large enough for the temporary file 
  1.5788 +  ** Check that the output buffer is large enough for the temporary file
  1.5789    ** name in the following format:
  1.5790    **
  1.5791    **   "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
  1.5792 @@ -35430,8 +36526,8 @@
  1.5793  
  1.5794  #ifndef NDEBUG
  1.5795    int isOpenJournal = (isCreate && (
  1.5796 -        eType==SQLITE_OPEN_MASTER_JOURNAL 
  1.5797 -     || eType==SQLITE_OPEN_MAIN_JOURNAL 
  1.5798 +        eType==SQLITE_OPEN_MASTER_JOURNAL
  1.5799 +     || eType==SQLITE_OPEN_MAIN_JOURNAL
  1.5800       || eType==SQLITE_OPEN_WAL
  1.5801    ));
  1.5802  #endif
  1.5803 @@ -35439,9 +36535,9 @@
  1.5804    OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
  1.5805             zUtf8Name, id, flags, pOutFlags));
  1.5806  
  1.5807 -  /* Check the following statements are true: 
  1.5808 -  **
  1.5809 -  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
  1.5810 +  /* Check the following statements are true:
  1.5811 +  **
  1.5812 +  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
  1.5813    **   (b) if CREATE is set, then READWRITE must also be set, and
  1.5814    **   (c) if EXCLUSIVE is set, then CREATE must also be set.
  1.5815    **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
  1.5816 @@ -35451,7 +36547,7 @@
  1.5817    assert(isExclusive==0 || isCreate);
  1.5818    assert(isDelete==0 || isCreate);
  1.5819  
  1.5820 -  /* The main DB, main journal, WAL file and master journal are never 
  1.5821 +  /* The main DB, main journal, WAL file and master journal are never
  1.5822    ** automatically deleted. Nor are they ever temporary files.  */
  1.5823    assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
  1.5824    assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
  1.5825 @@ -35459,9 +36555,9 @@
  1.5826    assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
  1.5827  
  1.5828    /* Assert that the upper layer has set one of the "file-type" flags. */
  1.5829 -  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
  1.5830 -       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
  1.5831 -       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
  1.5832 +  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
  1.5833 +       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
  1.5834 +       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
  1.5835         || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
  1.5836    );
  1.5837  
  1.5838 @@ -35476,8 +36572,8 @@
  1.5839    }
  1.5840  #endif
  1.5841  
  1.5842 -  /* If the second argument to this function is NULL, generate a 
  1.5843 -  ** temporary file name to use 
  1.5844 +  /* If the second argument to this function is NULL, generate a
  1.5845 +  ** temporary file name to use
  1.5846    */
  1.5847    if( !zUtf8Name ){
  1.5848      assert( isDelete && !isOpenJournal );
  1.5849 @@ -35517,8 +36613,8 @@
  1.5850      dwDesiredAccess = GENERIC_READ;
  1.5851    }
  1.5852  
  1.5853 -  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
  1.5854 -  ** created. SQLite doesn't use it to indicate "exclusive access" 
  1.5855 +  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
  1.5856 +  ** created. SQLite doesn't use it to indicate "exclusive access"
  1.5857    ** as it is usually understood.
  1.5858    */
  1.5859    if( isExclusive ){
  1.5860 @@ -35607,7 +36703,7 @@
  1.5861      sqlite3_free(zConverted);
  1.5862      sqlite3_free(zTmpname);
  1.5863      if( isReadWrite && !isExclusive ){
  1.5864 -      return winOpen(pVfs, zName, id, 
  1.5865 +      return winOpen(pVfs, zName, id,
  1.5866           ((flags|SQLITE_OPEN_READONLY) &
  1.5867                       ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
  1.5868           pOutFlags);
  1.5869 @@ -35816,14 +36912,14 @@
  1.5870      WIN32_FILE_ATTRIBUTE_DATA sAttrData;
  1.5871      memset(&sAttrData, 0, sizeof(sAttrData));
  1.5872      while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
  1.5873 -                             GetFileExInfoStandard, 
  1.5874 +                             GetFileExInfoStandard,
  1.5875                               &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
  1.5876      if( rc ){
  1.5877        /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
  1.5878        ** as if it does not exist.
  1.5879        */
  1.5880        if(    flags==SQLITE_ACCESS_EXISTS
  1.5881 -          && sAttrData.nFileSizeHigh==0 
  1.5882 +          && sAttrData.nFileSizeHigh==0
  1.5883            && sAttrData.nFileSizeLow==0 ){
  1.5884          attr = INVALID_FILE_ATTRIBUTES;
  1.5885        }else{
  1.5886 @@ -35922,7 +37018,7 @@
  1.5887    int nFull,                    /* Size of output buffer in bytes */
  1.5888    char *zFull                   /* Output buffer */
  1.5889  ){
  1.5890 -  
  1.5891 +
  1.5892  #if defined(__CYGWIN__)
  1.5893    SimulateIOError( return SQLITE_ERROR );
  1.5894    UNUSED_PARAMETER(nFull);
  1.5895 @@ -36099,15 +37195,29 @@
  1.5896  ** Interfaces for opening a shared library, finding entry points
  1.5897  ** within the shared library, and closing the shared library.
  1.5898  */
  1.5899 -/*
  1.5900 -** Interfaces for opening a shared library, finding entry points
  1.5901 -** within the shared library, and closing the shared library.
  1.5902 -*/
  1.5903  static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
  1.5904    HANDLE h;
  1.5905 +#if defined(__CYGWIN__)
  1.5906 +  int nFull = pVfs->mxPathname+1;
  1.5907 +  char *zFull = sqlite3MallocZero( nFull );
  1.5908 +  void *zConverted = 0;
  1.5909 +  if( zFull==0 ){
  1.5910 +    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
  1.5911 +    return 0;
  1.5912 +  }
  1.5913 +  if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
  1.5914 +    sqlite3_free(zFull);
  1.5915 +    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
  1.5916 +    return 0;
  1.5917 +  }
  1.5918 +  zConverted = winConvertFromUtf8Filename(zFull);
  1.5919 +  sqlite3_free(zFull);
  1.5920 +#else
  1.5921    void *zConverted = winConvertFromUtf8Filename(zFilename);
  1.5922    UNUSED_PARAMETER(pVfs);
  1.5923 +#endif
  1.5924    if( zConverted==0 ){
  1.5925 +    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
  1.5926      return 0;
  1.5927    }
  1.5928    if( osIsNT() ){
  1.5929 @@ -36122,6 +37232,7 @@
  1.5930      h = osLoadLibraryA((char*)zConverted);
  1.5931    }
  1.5932  #endif
  1.5933 +  OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
  1.5934    sqlite3_free(zConverted);
  1.5935    return (void*)h;
  1.5936  }
  1.5937 @@ -36130,12 +37241,17 @@
  1.5938    winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
  1.5939  }
  1.5940  static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
  1.5941 +  FARPROC proc;
  1.5942    UNUSED_PARAMETER(pVfs);
  1.5943 -  return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
  1.5944 +  proc = osGetProcAddressA((HANDLE)pH, zSym);
  1.5945 +  OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
  1.5946 +           (void*)pH, zSym, (void*)proc));
  1.5947 +  return (void(*)(void))proc;
  1.5948  }
  1.5949  static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
  1.5950    UNUSED_PARAMETER(pVfs);
  1.5951    osFreeLibrary((HANDLE)pHandle);
  1.5952 +  OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
  1.5953  }
  1.5954  #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
  1.5955    #define winDlOpen  0
  1.5956 @@ -36215,12 +37331,12 @@
  1.5957  ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
  1.5958  ** proleptic Gregorian calendar.
  1.5959  **
  1.5960 -** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
  1.5961 +** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
  1.5962  ** cannot be found.
  1.5963  */
  1.5964  static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
  1.5965 -  /* FILETIME structure is a 64-bit value representing the number of 
  1.5966 -     100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 
  1.5967 +  /* FILETIME structure is a 64-bit value representing the number of
  1.5968 +     100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
  1.5969    */
  1.5970    FILETIME ft;
  1.5971    static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
  1.5972 @@ -36228,7 +37344,7 @@
  1.5973    static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
  1.5974  #endif
  1.5975    /* 2^32 - to avoid use of LL and warnings in gcc */
  1.5976 -  static const sqlite3_int64 max32BitValue = 
  1.5977 +  static const sqlite3_int64 max32BitValue =
  1.5978        (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
  1.5979        (sqlite3_int64)294967296;
  1.5980  
  1.5981 @@ -36244,7 +37360,7 @@
  1.5982  #endif
  1.5983  
  1.5984    *piNow = winFiletimeEpoch +
  1.5985 -            ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + 
  1.5986 +            ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
  1.5987                 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
  1.5988  
  1.5989  #ifdef SQLITE_TEST
  1.5990 @@ -36363,7 +37479,7 @@
  1.5991  
  1.5992    /* Double-check that the aSyscall[] array has been constructed
  1.5993    ** correctly.  See ticket [bb3a86e890c8e96ab] */
  1.5994 -  assert( ArraySize(aSyscall)==76 );
  1.5995 +  assert( ArraySize(aSyscall)==77 );
  1.5996  
  1.5997    /* get memory map allocation granularity */
  1.5998    memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
  1.5999 @@ -36381,10 +37497,10 @@
  1.6000    sqlite3_vfs_register(&winLongPathVfs, 0);
  1.6001  #endif
  1.6002  
  1.6003 -  return SQLITE_OK; 
  1.6004 -}
  1.6005 -
  1.6006 -SQLITE_API int sqlite3_os_end(void){ 
  1.6007 +  return SQLITE_OK;
  1.6008 +}
  1.6009 +
  1.6010 +SQLITE_API int sqlite3_os_end(void){
  1.6011  #if SQLITE_OS_WINRT
  1.6012    if( sleepObj!=NULL ){
  1.6013      osCloseHandle(sleepObj);
  1.6014 @@ -36831,7 +37947,8 @@
  1.6015    int szCache;                        /* Configured cache size */
  1.6016    int szPage;                         /* Size of every page in this cache */
  1.6017    int szExtra;                        /* Size of extra space for each page */
  1.6018 -  int bPurgeable;                     /* True if pages are on backing store */
  1.6019 +  u8 bPurgeable;                      /* True if pages are on backing store */
  1.6020 +  u8 eCreate;                         /* eCreate value for for xFetch() */
  1.6021    int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
  1.6022    void *pStress;                      /* Argument to xStress */
  1.6023    sqlite3_pcache *pCache;             /* Pluggable cache module */
  1.6024 @@ -36898,6 +38015,10 @@
  1.6025    }else{
  1.6026      assert( pPage==p->pDirty );
  1.6027      p->pDirty = pPage->pDirtyNext;
  1.6028 +    if( p->pDirty==0 && p->bPurgeable ){
  1.6029 +      assert( p->eCreate==1 );
  1.6030 +      p->eCreate = 2;
  1.6031 +    }
  1.6032    }
  1.6033    pPage->pDirtyNext = 0;
  1.6034    pPage->pDirtyPrev = 0;
  1.6035 @@ -36918,6 +38039,9 @@
  1.6036    if( pPage->pDirtyNext ){
  1.6037      assert( pPage->pDirtyNext->pDirtyPrev==0 );
  1.6038      pPage->pDirtyNext->pDirtyPrev = pPage;
  1.6039 +  }else if( p->bPurgeable ){
  1.6040 +    assert( p->eCreate==2 );
  1.6041 +    p->eCreate = 1;
  1.6042    }
  1.6043    p->pDirty = pPage;
  1.6044    if( !p->pDirtyTail ){
  1.6045 @@ -36987,6 +38111,7 @@
  1.6046    p->szPage = szPage;
  1.6047    p->szExtra = szExtra;
  1.6048    p->bPurgeable = bPurgeable;
  1.6049 +  p->eCreate = 2;
  1.6050    p->xStress = xStress;
  1.6051    p->pStress = pStress;
  1.6052    p->szCache = 100;
  1.6053 @@ -37026,7 +38151,7 @@
  1.6054    int createFlag,       /* If true, create page if it does not exist already */
  1.6055    PgHdr **ppPage        /* Write the page here */
  1.6056  ){
  1.6057 -  sqlite3_pcache_page *pPage = 0;
  1.6058 +  sqlite3_pcache_page *pPage;
  1.6059    PgHdr *pPgHdr = 0;
  1.6060    int eCreate;
  1.6061  
  1.6062 @@ -37037,8 +38162,12 @@
  1.6063    /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
  1.6064    ** allocate it now.
  1.6065    */
  1.6066 -  if( !pCache->pCache && createFlag ){
  1.6067 +  if( !pCache->pCache ){
  1.6068      sqlite3_pcache *p;
  1.6069 +    if( !createFlag ){
  1.6070 +      *ppPage = 0;
  1.6071 +      return SQLITE_OK;
  1.6072 +    }
  1.6073      p = sqlite3GlobalConfig.pcache2.xCreate(
  1.6074          pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
  1.6075      );
  1.6076 @@ -37049,11 +38178,16 @@
  1.6077      pCache->pCache = p;
  1.6078    }
  1.6079  
  1.6080 -  eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
  1.6081 -  if( pCache->pCache ){
  1.6082 -    pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
  1.6083 -  }
  1.6084 -
  1.6085 +  /* eCreate defines what to do if the page does not exist.
  1.6086 +  **    0     Do not allocate a new page.  (createFlag==0)
  1.6087 +  **    1     Allocate a new page if doing so is inexpensive.
  1.6088 +  **          (createFlag==1 AND bPurgeable AND pDirty)
  1.6089 +  **    2     Allocate a new page even it doing so is difficult.
  1.6090 +  **          (createFlag==1 AND !(bPurgeable AND pDirty)
  1.6091 +  */
  1.6092 +  eCreate = createFlag==0 ? 0 : pCache->eCreate;
  1.6093 +  assert( (createFlag*(1+(!pCache->bPurgeable||!pCache->pDirty)))==eCreate );
  1.6094 +  pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
  1.6095    if( !pPage && eCreate==1 ){
  1.6096      PgHdr *pPg;
  1.6097  
  1.6098 @@ -37525,6 +38659,7 @@
  1.6099  struct PgHdr1 {
  1.6100    sqlite3_pcache_page page;
  1.6101    unsigned int iKey;             /* Key value (page number) */
  1.6102 +  u8 isPinned;                   /* Page in use, not on the LRU list */
  1.6103    PgHdr1 *pNext;                 /* Next in hash table chain */
  1.6104    PCache1 *pCache;               /* Cache that currently owns this page */
  1.6105    PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
  1.6106 @@ -37853,34 +38988,32 @@
  1.6107  ** LRU list, then this function is a no-op.
  1.6108  **
  1.6109  ** The PGroup mutex must be held when this function is called.
  1.6110 -**
  1.6111 -** If pPage is NULL then this routine is a no-op.
  1.6112  */
  1.6113  static void pcache1PinPage(PgHdr1 *pPage){
  1.6114    PCache1 *pCache;
  1.6115    PGroup *pGroup;
  1.6116  
  1.6117 -  if( pPage==0 ) return;
  1.6118 +  assert( pPage!=0 );
  1.6119 +  assert( pPage->isPinned==0 );
  1.6120    pCache = pPage->pCache;
  1.6121    pGroup = pCache->pGroup;
  1.6122 +  assert( pPage->pLruNext || pPage==pGroup->pLruTail );
  1.6123 +  assert( pPage->pLruPrev || pPage==pGroup->pLruHead );
  1.6124    assert( sqlite3_mutex_held(pGroup->mutex) );
  1.6125 -  if( pPage->pLruNext || pPage==pGroup->pLruTail ){
  1.6126 -    if( pPage->pLruPrev ){
  1.6127 -      pPage->pLruPrev->pLruNext = pPage->pLruNext;
  1.6128 -    }
  1.6129 -    if( pPage->pLruNext ){
  1.6130 -      pPage->pLruNext->pLruPrev = pPage->pLruPrev;
  1.6131 -    }
  1.6132 -    if( pGroup->pLruHead==pPage ){
  1.6133 -      pGroup->pLruHead = pPage->pLruNext;
  1.6134 -    }
  1.6135 -    if( pGroup->pLruTail==pPage ){
  1.6136 -      pGroup->pLruTail = pPage->pLruPrev;
  1.6137 -    }
  1.6138 -    pPage->pLruNext = 0;
  1.6139 -    pPage->pLruPrev = 0;
  1.6140 -    pPage->pCache->nRecyclable--;
  1.6141 -  }
  1.6142 +  if( pPage->pLruPrev ){
  1.6143 +    pPage->pLruPrev->pLruNext = pPage->pLruNext;
  1.6144 +  }else{
  1.6145 +    pGroup->pLruHead = pPage->pLruNext;
  1.6146 +  }
  1.6147 +  if( pPage->pLruNext ){
  1.6148 +    pPage->pLruNext->pLruPrev = pPage->pLruPrev;
  1.6149 +  }else{
  1.6150 +    pGroup->pLruTail = pPage->pLruPrev;
  1.6151 +  }
  1.6152 +  pPage->pLruNext = 0;
  1.6153 +  pPage->pLruPrev = 0;
  1.6154 +  pPage->isPinned = 1;
  1.6155 +  pCache->nRecyclable--;
  1.6156  }
  1.6157  
  1.6158  
  1.6159 @@ -37912,6 +39045,7 @@
  1.6160    while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
  1.6161      PgHdr1 *p = pGroup->pLruTail;
  1.6162      assert( p->pCache->pGroup==pGroup );
  1.6163 +    assert( p->isPinned==0 );
  1.6164      pcache1PinPage(p);
  1.6165      pcache1RemoveFromHash(p);
  1.6166      pcache1FreePage(p);
  1.6167 @@ -37939,7 +39073,7 @@
  1.6168        if( pPage->iKey>=iLimit ){
  1.6169          pCache->nPage--;
  1.6170          *pp = pPage->pNext;
  1.6171 -        pcache1PinPage(pPage);
  1.6172 +        if( !pPage->isPinned ) pcache1PinPage(pPage);
  1.6173          pcache1FreePage(pPage);
  1.6174        }else{
  1.6175          pp = &pPage->pNext;
  1.6176 @@ -38149,6 +39283,7 @@
  1.6177    PGroup *pGroup;
  1.6178    PgHdr1 *pPage = 0;
  1.6179  
  1.6180 +  assert( offsetof(PgHdr1,page)==0 );
  1.6181    assert( pCache->bPurgeable || createFlag!=1 );
  1.6182    assert( pCache->bPurgeable || pCache->nMin==0 );
  1.6183    assert( pCache->bPurgeable==0 || pCache->nMin==10 );
  1.6184 @@ -38162,8 +39297,11 @@
  1.6185    }
  1.6186  
  1.6187    /* Step 2: Abort if no existing page is found and createFlag is 0 */
  1.6188 -  if( pPage || createFlag==0 ){
  1.6189 -    pcache1PinPage(pPage);
  1.6190 +  if( pPage ){
  1.6191 +    if( !pPage->isPinned ) pcache1PinPage(pPage);
  1.6192 +    goto fetch_out;
  1.6193 +  }
  1.6194 +  if( createFlag==0 ){
  1.6195      goto fetch_out;
  1.6196    }
  1.6197  
  1.6198 @@ -38204,6 +39342,7 @@
  1.6199    )){
  1.6200      PCache1 *pOther;
  1.6201      pPage = pGroup->pLruTail;
  1.6202 +    assert( pPage->isPinned==0 );
  1.6203      pcache1RemoveFromHash(pPage);
  1.6204      pcache1PinPage(pPage);
  1.6205      pOther = pPage->pCache;
  1.6206 @@ -38240,6 +39379,7 @@
  1.6207      pPage->pCache = pCache;
  1.6208      pPage->pLruPrev = 0;
  1.6209      pPage->pLruNext = 0;
  1.6210 +    pPage->isPinned = 1;
  1.6211      *(void **)pPage->page.pExtra = 0;
  1.6212      pCache->apHash[h] = pPage;
  1.6213    }
  1.6214 @@ -38249,7 +39389,7 @@
  1.6215      pCache->iMaxKey = iKey;
  1.6216    }
  1.6217    pcache1LeaveMutex(pGroup);
  1.6218 -  return &pPage->page;
  1.6219 +  return (sqlite3_pcache_page*)pPage;
  1.6220  }
  1.6221  
  1.6222  
  1.6223 @@ -38275,6 +39415,7 @@
  1.6224    */
  1.6225    assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
  1.6226    assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
  1.6227 +  assert( pPage->isPinned==1 );
  1.6228  
  1.6229    if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
  1.6230      pcache1RemoveFromHash(pPage);
  1.6231 @@ -38290,6 +39431,7 @@
  1.6232        pGroup->pLruHead = pPage;
  1.6233      }
  1.6234      pCache->nRecyclable++;
  1.6235 +    pPage->isPinned = 0;
  1.6236    }
  1.6237  
  1.6238    pcache1LeaveMutex(pCache->pGroup);
  1.6239 @@ -38416,6 +39558,7 @@
  1.6240  #ifdef SQLITE_PCACHE_SEPARATE_HEADER
  1.6241        nFree += sqlite3MemSize(p);
  1.6242  #endif
  1.6243 +      assert( p->isPinned==0 );
  1.6244        pcache1PinPage(p);
  1.6245        pcache1RemoveFromHash(p);
  1.6246        pcache1FreePage(p);
  1.6247 @@ -38440,6 +39583,7 @@
  1.6248    PgHdr1 *p;
  1.6249    int nRecyclable = 0;
  1.6250    for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
  1.6251 +    assert( p->isPinned==0 );
  1.6252      nRecyclable++;
  1.6253    }
  1.6254    *pnCurrent = pcache1.grp.nCurrentPage;
  1.6255 @@ -38564,8 +39708,8 @@
  1.6256    struct RowSetEntry *pFresh;    /* Source of new entry objects */
  1.6257    struct RowSetEntry *pForest;   /* List of binary trees of entries */
  1.6258    u16 nFresh;                    /* Number of objects on pFresh */
  1.6259 -  u8 rsFlags;                    /* Various flags */
  1.6260 -  u8 iBatch;                     /* Current insert batch */
  1.6261 +  u16 rsFlags;                   /* Various flags */
  1.6262 +  int iBatch;                    /* Current insert batch */
  1.6263  };
  1.6264  
  1.6265  /*
  1.6266 @@ -38899,7 +40043,7 @@
  1.6267  ** on pRowSet->pEntry, then sort those entires into the forest at
  1.6268  ** pRowSet->pForest so that they can be tested.
  1.6269  */
  1.6270 -SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
  1.6271 +SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
  1.6272    struct RowSetEntry *p, *pTree;
  1.6273  
  1.6274    /* This routine is never called after sqlite3RowSetNext() */
  1.6275 @@ -39728,7 +40872,8 @@
  1.6276    u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
  1.6277    u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
  1.6278    u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
  1.6279 -  u8 tempFile;                /* zFilename is a temporary file */
  1.6280 +  u8 tempFile;                /* zFilename is a temporary or immutable file */
  1.6281 +  u8 noLock;                  /* Do not lock (except in WAL mode) */
  1.6282    u8 readOnly;                /* True for a read-only database */
  1.6283    u8 memDb;                   /* True to inhibit all file I/O */
  1.6284  
  1.6285 @@ -40126,15 +41271,12 @@
  1.6286  static int subjRequiresPage(PgHdr *pPg){
  1.6287    Pager *pPager = pPg->pPager;
  1.6288    PagerSavepoint *p;
  1.6289 -  Pgno pgno;
  1.6290 -  int i;
  1.6291 -  if( pPager->nSavepoint ){
  1.6292 -    pgno = pPg->pgno;
  1.6293 -    for(i=0; i<pPager->nSavepoint; i++){
  1.6294 -      p = &pPager->aSavepoint[i];
  1.6295 -      if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
  1.6296 -        return 1;
  1.6297 -      }
  1.6298 +  Pgno pgno = pPg->pgno;
  1.6299 +  int i;
  1.6300 +  for(i=0; i<pPager->nSavepoint; i++){
  1.6301 +    p = &pPager->aSavepoint[i];
  1.6302 +    if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
  1.6303 +      return 1;
  1.6304      }
  1.6305    }
  1.6306    return 0;
  1.6307 @@ -40143,8 +41285,8 @@
  1.6308  /*
  1.6309  ** Return true if the page is already in the journal file.
  1.6310  */
  1.6311 -static int pageInJournal(PgHdr *pPg){
  1.6312 -  return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
  1.6313 +static int pageInJournal(Pager *pPager, PgHdr *pPg){
  1.6314 +  return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
  1.6315  }
  1.6316  
  1.6317  /*
  1.6318 @@ -40196,7 +41338,7 @@
  1.6319    assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
  1.6320    if( isOpen(pPager->fd) ){
  1.6321      assert( pPager->eLock>=eLock );
  1.6322 -    rc = sqlite3OsUnlock(pPager->fd, eLock);
  1.6323 +    rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
  1.6324      if( pPager->eLock!=UNKNOWN_LOCK ){
  1.6325        pPager->eLock = (u8)eLock;
  1.6326      }
  1.6327 @@ -40220,7 +41362,7 @@
  1.6328  
  1.6329    assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
  1.6330    if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
  1.6331 -    rc = sqlite3OsLock(pPager->fd, eLock);
  1.6332 +    rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
  1.6333      if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
  1.6334        pPager->eLock = (u8)eLock;
  1.6335        IOTRACE(("LOCK %p %d\n", pPager, eLock))
  1.6336 @@ -40351,6 +41493,7 @@
  1.6337     || szJ<16
  1.6338     || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
  1.6339     || len>=nMaster 
  1.6340 +   || len==0 
  1.6341     || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
  1.6342     || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
  1.6343     || memcmp(aMagic, aJournalMagic, 8)
  1.6344 @@ -40728,12 +41871,11 @@
  1.6345  
  1.6346    if( !zMaster 
  1.6347     || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
  1.6348 -   || pPager->journalMode==PAGER_JOURNALMODE_OFF 
  1.6349 +   || !isOpen(pPager->jfd)
  1.6350    ){
  1.6351      return SQLITE_OK;
  1.6352    }
  1.6353    pPager->setMaster = 1;
  1.6354 -  assert( isOpen(pPager->jfd) );
  1.6355    assert( pPager->journalHdr <= pPager->journalOff );
  1.6356  
  1.6357    /* Calculate the length in bytes and the checksum of zMaster */
  1.6358 @@ -40787,7 +41929,7 @@
  1.6359  ** already in memory.
  1.6360  */
  1.6361  static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
  1.6362 -  PgHdr *p;                         /* Return value */
  1.6363 +  PgHdr *p = 0;                     /* Return value */
  1.6364  
  1.6365    /* It is not possible for a call to PcacheFetch() with createFlag==0 to
  1.6366    ** fail, since no attempt to allocate dynamic memory will be made.
  1.6367 @@ -41091,7 +42233,7 @@
  1.6368      PgHdr *p = pager_lookup(pPager, 1);
  1.6369      if( p ){
  1.6370        p->pageHash = 0;
  1.6371 -      sqlite3PagerUnref(p);
  1.6372 +      sqlite3PagerUnrefNotNull(p);
  1.6373      }
  1.6374    }
  1.6375  #endif
  1.6376 @@ -41120,6 +42262,11 @@
  1.6377      rc = pager_truncate(pPager, pPager->dbSize);
  1.6378    }
  1.6379  
  1.6380 +  if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
  1.6381 +    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
  1.6382 +    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
  1.6383 +  }
  1.6384 +
  1.6385    if( !pPager->exclusiveMode 
  1.6386     && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
  1.6387    ){
  1.6388 @@ -41933,7 +43080,7 @@
  1.6389    if( rc==SQLITE_OK
  1.6390     && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
  1.6391    ){
  1.6392 -    rc = sqlite3PagerSync(pPager);
  1.6393 +    rc = sqlite3PagerSync(pPager, 0);
  1.6394    }
  1.6395    if( rc==SQLITE_OK ){
  1.6396      rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
  1.6397 @@ -42079,7 +43226,7 @@
  1.6398        if( rc==SQLITE_OK ){
  1.6399          pPager->xReiniter(pPg);
  1.6400        }
  1.6401 -      sqlite3PagerUnref(pPg);
  1.6402 +      sqlite3PagerUnrefNotNull(pPg);
  1.6403      }
  1.6404    }
  1.6405  
  1.6406 @@ -43434,7 +44581,7 @@
  1.6407      assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
  1.6408      assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
  1.6409      assert( pagerUseWal(pPager) 
  1.6410 -         || pageInJournal(pPg) 
  1.6411 +         || pageInJournal(pPager, pPg) 
  1.6412           || pPg->pgno>pPager->dbOrigSize 
  1.6413      );
  1.6414      rc = openSubJournal(pPager);
  1.6415 @@ -43774,30 +44921,38 @@
  1.6416      **    + The value returned by sqlite3OsSectorSize()
  1.6417      **    + The largest page size that can be written atomically.
  1.6418      */
  1.6419 -    if( rc==SQLITE_OK && !readOnly ){
  1.6420 -      setSectorSize(pPager);
  1.6421 -      assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
  1.6422 -      if( szPageDflt<pPager->sectorSize ){
  1.6423 -        if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
  1.6424 -          szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
  1.6425 -        }else{
  1.6426 -          szPageDflt = (u32)pPager->sectorSize;
  1.6427 -        }
  1.6428 -      }
  1.6429 +    if( rc==SQLITE_OK ){
  1.6430 +      int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
  1.6431 +      if( !readOnly ){
  1.6432 +        setSectorSize(pPager);
  1.6433 +        assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
  1.6434 +        if( szPageDflt<pPager->sectorSize ){
  1.6435 +          if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
  1.6436 +            szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
  1.6437 +          }else{
  1.6438 +            szPageDflt = (u32)pPager->sectorSize;
  1.6439 +          }
  1.6440 +        }
  1.6441  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  1.6442 -      {
  1.6443 -        int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
  1.6444 -        int ii;
  1.6445 -        assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
  1.6446 -        assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
  1.6447 -        assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
  1.6448 -        for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
  1.6449 -          if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
  1.6450 -            szPageDflt = ii;
  1.6451 -          }
  1.6452 -        }
  1.6453 -      }
  1.6454 -#endif
  1.6455 +        {
  1.6456 +          int ii;
  1.6457 +          assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
  1.6458 +          assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
  1.6459 +          assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
  1.6460 +          for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
  1.6461 +            if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
  1.6462 +              szPageDflt = ii;
  1.6463 +            }
  1.6464 +          }
  1.6465 +        }
  1.6466 +#endif
  1.6467 +      }
  1.6468 +      pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
  1.6469 +      if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
  1.6470 +       || sqlite3_uri_boolean(zFilename, "immutable", 0) ){
  1.6471 +          vfsFlags |= SQLITE_OPEN_READONLY;
  1.6472 +          goto act_like_temp_file;
  1.6473 +      }
  1.6474      }
  1.6475    }else{
  1.6476      /* If a temporary file is requested, it is not opened immediately.
  1.6477 @@ -43807,10 +44962,14 @@
  1.6478      ** This branch is also run for an in-memory database. An in-memory
  1.6479      ** database is the same as a temp-file that is never written out to
  1.6480      ** disk and uses an in-memory rollback journal.
  1.6481 +    **
  1.6482 +    ** This branch also runs for files marked as immutable.
  1.6483      */ 
  1.6484 +act_like_temp_file:
  1.6485      tempFile = 1;
  1.6486 -    pPager->eState = PAGER_READER;
  1.6487 -    pPager->eLock = EXCLUSIVE_LOCK;
  1.6488 +    pPager->eState = PAGER_READER;     /* Pretend we already have a lock */
  1.6489 +    pPager->eLock = EXCLUSIVE_LOCK;    /* Pretend we are in EXCLUSIVE locking mode */
  1.6490 +    pPager->noLock = 1;                /* Do no locking */
  1.6491      readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
  1.6492    }
  1.6493  
  1.6494 @@ -43851,9 +45010,6 @@
  1.6495    /* pPager->nPage = 0; */
  1.6496    pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
  1.6497    /* pPager->state = PAGER_UNLOCK; */
  1.6498 -#if 0
  1.6499 -  assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
  1.6500 -#endif
  1.6501    /* pPager->errMask = 0; */
  1.6502    pPager->tempFile = (u8)tempFile;
  1.6503    assert( tempFile==PAGER_LOCKINGMODE_NORMAL 
  1.6504 @@ -43899,6 +45055,30 @@
  1.6505  }
  1.6506  
  1.6507  
  1.6508 +/* Verify that the database file has not be deleted or renamed out from
  1.6509 +** under the pager.  Return SQLITE_OK if the database is still were it ought
  1.6510 +** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
  1.6511 +** code from sqlite3OsAccess()) if the database has gone missing.
  1.6512 +*/
  1.6513 +static int databaseIsUnmoved(Pager *pPager){
  1.6514 +  int bHasMoved = 0;
  1.6515 +  int rc;
  1.6516 +
  1.6517 +  if( pPager->tempFile ) return SQLITE_OK;
  1.6518 +  if( pPager->dbSize==0 ) return SQLITE_OK;
  1.6519 +  assert( pPager->zFilename && pPager->zFilename[0] );
  1.6520 +  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
  1.6521 +  if( rc==SQLITE_NOTFOUND ){
  1.6522 +    /* If the HAS_MOVED file-control is unimplemented, assume that the file
  1.6523 +    ** has not been moved.  That is the historical behavior of SQLite: prior to
  1.6524 +    ** version 3.8.3, it never checked */
  1.6525 +    rc = SQLITE_OK;
  1.6526 +  }else if( rc==SQLITE_OK && bHasMoved ){
  1.6527 +    rc = SQLITE_READONLY_DBMOVED;
  1.6528 +  }
  1.6529 +  return rc;
  1.6530 +}
  1.6531 +
  1.6532  
  1.6533  /*
  1.6534  ** This function is called after transitioning from PAGER_UNLOCK to
  1.6535 @@ -43964,15 +45144,17 @@
  1.6536      if( rc==SQLITE_OK && !locked ){
  1.6537        Pgno nPage;                 /* Number of pages in database file */
  1.6538  
  1.6539 -      /* Check the size of the database file. If it consists of 0 pages,
  1.6540 -      ** then delete the journal file. See the header comment above for 
  1.6541 -      ** the reasoning here.  Delete the obsolete journal file under
  1.6542 -      ** a RESERVED lock to avoid race conditions and to avoid violating
  1.6543 -      ** [H33020].
  1.6544 -      */
  1.6545        rc = pagerPagecount(pPager, &nPage);
  1.6546        if( rc==SQLITE_OK ){
  1.6547 -        if( nPage==0 ){
  1.6548 +        /* If the database is zero pages in size, that means that either (1) the
  1.6549 +        ** journal is a remnant from a prior database with the same name where
  1.6550 +        ** the database file but not the journal was deleted, or (2) the initial
  1.6551 +        ** transaction that populates a new database is being rolled back.
  1.6552 +        ** In either case, the journal file can be deleted.  However, take care
  1.6553 +        ** not to delete the journal file if it is already open due to
  1.6554 +        ** journal_mode=PERSIST.
  1.6555 +        */
  1.6556 +        if( nPage==0 && !jrnlOpen ){
  1.6557            sqlite3BeginBenignMalloc();
  1.6558            if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
  1.6559              sqlite3OsDelete(pVfs, pPager->zJournal, 0);
  1.6560 @@ -44370,7 +45552,7 @@
  1.6561        if( rc!=SQLITE_OK ) goto pager_acquire_err;
  1.6562      }
  1.6563  
  1.6564 -    if( iFrame==0 && bMmapOk ){
  1.6565 +    if( bMmapOk && iFrame==0 ){
  1.6566        void *pData = 0;
  1.6567  
  1.6568        rc = sqlite3OsFetch(pPager->fd, 
  1.6569 @@ -44511,16 +45693,19 @@
  1.6570  ** are released, a rollback occurs and the lock on the database is
  1.6571  ** removed.
  1.6572  */
  1.6573 +SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
  1.6574 +  Pager *pPager;
  1.6575 +  assert( pPg!=0 );
  1.6576 +  pPager = pPg->pPager;
  1.6577 +  if( pPg->flags & PGHDR_MMAP ){
  1.6578 +    pagerReleaseMapPage(pPg);
  1.6579 +  }else{
  1.6580 +    sqlite3PcacheRelease(pPg);
  1.6581 +  }
  1.6582 +  pagerUnlockIfUnused(pPager);
  1.6583 +}
  1.6584  SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
  1.6585 -  if( pPg ){
  1.6586 -    Pager *pPager = pPg->pPager;
  1.6587 -    if( pPg->flags & PGHDR_MMAP ){
  1.6588 -      pagerReleaseMapPage(pPg);
  1.6589 -    }else{
  1.6590 -      sqlite3PcacheRelease(pPg);
  1.6591 -    }
  1.6592 -    pagerUnlockIfUnused(pPager);
  1.6593 -  }
  1.6594 +  if( pPg ) sqlite3PagerUnrefNotNull(pPg);
  1.6595  }
  1.6596  
  1.6597  /*
  1.6598 @@ -44575,13 +45760,19 @@
  1.6599              (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
  1.6600              (SQLITE_OPEN_MAIN_JOURNAL)
  1.6601            );
  1.6602 -  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  1.6603 -        rc = sqlite3JournalOpen(
  1.6604 -            pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
  1.6605 -        );
  1.6606 -  #else
  1.6607 -        rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
  1.6608 -  #endif
  1.6609 +
  1.6610 +        /* Verify that the database still has the same name as it did when
  1.6611 +        ** it was originally opened. */
  1.6612 +        rc = databaseIsUnmoved(pPager);
  1.6613 +        if( rc==SQLITE_OK ){
  1.6614 +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
  1.6615 +          rc = sqlite3JournalOpen(
  1.6616 +              pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
  1.6617 +          );
  1.6618 +#else
  1.6619 +          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
  1.6620 +#endif
  1.6621 +        }
  1.6622        }
  1.6623        assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
  1.6624      }
  1.6625 @@ -44702,9 +45893,9 @@
  1.6626  ** of any open savepoints as appropriate.
  1.6627  */
  1.6628  static int pager_write(PgHdr *pPg){
  1.6629 -  void *pData = pPg->pData;
  1.6630    Pager *pPager = pPg->pPager;
  1.6631    int rc = SQLITE_OK;
  1.6632 +  int inJournal;
  1.6633  
  1.6634    /* This routine is not called unless a write-transaction has already 
  1.6635    ** been started. The journal file may or may not be open at this point.
  1.6636 @@ -44715,14 +45906,8 @@
  1.6637         || pPager->eState==PAGER_WRITER_DBMOD
  1.6638    );
  1.6639    assert( assert_pager_state(pPager) );
  1.6640 -
  1.6641 -  /* If an error has been previously detected, report the same error
  1.6642 -  ** again. This should not happen, but the check provides robustness. */
  1.6643 -  if( NEVER(pPager->errCode) )  return pPager->errCode;
  1.6644 -
  1.6645 -  /* Higher-level routines never call this function if database is not
  1.6646 -  ** writable.  But check anyway, just for robustness. */
  1.6647 -  if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
  1.6648 +  assert( pPager->errCode==0 );
  1.6649 +  assert( pPager->readOnly==0 );
  1.6650  
  1.6651    CHECK_PAGE(pPg);
  1.6652  
  1.6653 @@ -44746,7 +45931,8 @@
  1.6654    ** to the journal then we can return right away.
  1.6655    */
  1.6656    sqlite3PcacheMakeDirty(pPg);
  1.6657 -  if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
  1.6658 +  inJournal = pageInJournal(pPager, pPg);
  1.6659 +  if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){
  1.6660      assert( !pagerUseWal(pPager) );
  1.6661    }else{
  1.6662    
  1.6663 @@ -44754,7 +45940,7 @@
  1.6664      ** EXCLUSIVE lock on the main database file.  Write the current page to
  1.6665      ** the transaction journal if it is not there already.
  1.6666      */
  1.6667 -    if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
  1.6668 +    if( !inJournal && !pagerUseWal(pPager) ){
  1.6669        assert( pagerUseWal(pPager)==0 );
  1.6670        if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
  1.6671          u32 cksum;
  1.6672 @@ -44767,7 +45953,7 @@
  1.6673          assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
  1.6674  
  1.6675          assert( pPager->journalHdr<=pPager->journalOff );
  1.6676 -        CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
  1.6677 +        CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
  1.6678          cksum = pager_cksum(pPager, (u8*)pData2);
  1.6679  
  1.6680          /* Even if an IO or diskfull error occurs while journalling the
  1.6681 @@ -44819,7 +46005,7 @@
  1.6682      ** the statement journal format differs from the standard journal format
  1.6683      ** in that it omits the checksums and the header.
  1.6684      */
  1.6685 -    if( subjRequiresPage(pPg) ){
  1.6686 +    if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){
  1.6687        rc = subjournalPage(pPg);
  1.6688      }
  1.6689    }
  1.6690 @@ -44851,19 +46037,19 @@
  1.6691  
  1.6692    PgHdr *pPg = pDbPage;
  1.6693    Pager *pPager = pPg->pPager;
  1.6694 -  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
  1.6695  
  1.6696    assert( (pPg->flags & PGHDR_MMAP)==0 );
  1.6697    assert( pPager->eState>=PAGER_WRITER_LOCKED );
  1.6698    assert( pPager->eState!=PAGER_ERROR );
  1.6699    assert( assert_pager_state(pPager) );
  1.6700  
  1.6701 -  if( nPagePerSector>1 ){
  1.6702 +  if( pPager->sectorSize > (u32)pPager->pageSize ){
  1.6703      Pgno nPageCount;          /* Total number of pages in database file */
  1.6704      Pgno pg1;                 /* First page of the sector pPg is located on. */
  1.6705      int nPage = 0;            /* Number of pages starting at pg1 to journal */
  1.6706      int ii;                   /* Loop counter */
  1.6707      int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
  1.6708 +    Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
  1.6709  
  1.6710      /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
  1.6711      ** a journal header to be written between the pages journaled by
  1.6712 @@ -44902,14 +46088,14 @@
  1.6713              if( pPage->flags&PGHDR_NEED_SYNC ){
  1.6714                needSync = 1;
  1.6715              }
  1.6716 -            sqlite3PagerUnref(pPage);
  1.6717 +            sqlite3PagerUnrefNotNull(pPage);
  1.6718            }
  1.6719          }
  1.6720        }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
  1.6721          if( pPage->flags&PGHDR_NEED_SYNC ){
  1.6722            needSync = 1;
  1.6723          }
  1.6724 -        sqlite3PagerUnref(pPage);
  1.6725 +        sqlite3PagerUnrefNotNull(pPage);
  1.6726        }
  1.6727      }
  1.6728  
  1.6729 @@ -44925,7 +46111,7 @@
  1.6730          PgHdr *pPage = pager_lookup(pPager, pg1+ii);
  1.6731          if( pPage ){
  1.6732            pPage->flags |= PGHDR_NEED_SYNC;
  1.6733 -          sqlite3PagerUnref(pPage);
  1.6734 +          sqlite3PagerUnrefNotNull(pPage);
  1.6735          }
  1.6736        }
  1.6737      }
  1.6738 @@ -45078,17 +46264,17 @@
  1.6739  ** If successful, or if called on a pager for which it is a no-op, this
  1.6740  ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
  1.6741  */
  1.6742 -SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
  1.6743 -  int rc = SQLITE_OK;
  1.6744 -  if( !pPager->noSync ){
  1.6745 +SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
  1.6746 +  int rc = SQLITE_OK;
  1.6747 +
  1.6748 +  if( isOpen(pPager->fd) ){
  1.6749 +    void *pArg = (void*)zMaster;
  1.6750 +    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
  1.6751 +    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
  1.6752 +  }
  1.6753 +  if( rc==SQLITE_OK && !pPager->noSync ){
  1.6754      assert( !MEMDB );
  1.6755      rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
  1.6756 -  }else if( isOpen(pPager->fd) ){
  1.6757 -    assert( !MEMDB );
  1.6758 -    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, 0);
  1.6759 -    if( rc==SQLITE_NOTFOUND ){
  1.6760 -      rc = SQLITE_OK;
  1.6761 -    }
  1.6762    }
  1.6763    return rc;
  1.6764  }
  1.6765 @@ -45287,7 +46473,7 @@
  1.6766    
  1.6767        /* Finally, sync the database file. */
  1.6768        if( !noSync ){
  1.6769 -        rc = sqlite3PagerSync(pPager);
  1.6770 +        rc = sqlite3PagerSync(pPager, zMaster);
  1.6771        }
  1.6772        IOTRACE(("DBSYNC %p\n", pPager))
  1.6773      }
  1.6774 @@ -45416,7 +46602,9 @@
  1.6775  
  1.6776    assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
  1.6777    assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
  1.6778 -          || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
  1.6779 +          || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR 
  1.6780 +          || rc==SQLITE_CANTOPEN
  1.6781 +  );
  1.6782  
  1.6783    /* If an error occurs during a ROLLBACK, we can no longer trust the pager
  1.6784    ** cache. So call pager_error() on the way out to make any error persistent.
  1.6785 @@ -45819,7 +47007,7 @@
  1.6786    if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
  1.6787      needSyncPgno = pPg->pgno;
  1.6788      assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
  1.6789 -            pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
  1.6790 +            pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
  1.6791      assert( pPg->flags&PGHDR_DIRTY );
  1.6792    }
  1.6793  
  1.6794 @@ -45853,7 +47041,7 @@
  1.6795    if( MEMDB ){
  1.6796      assert( pPgOld );
  1.6797      sqlite3PcacheMove(pPgOld, origPgno);
  1.6798 -    sqlite3PagerUnref(pPgOld);
  1.6799 +    sqlite3PagerUnrefNotNull(pPgOld);
  1.6800    }
  1.6801  
  1.6802    if( needSyncPgno ){
  1.6803 @@ -45882,7 +47070,7 @@
  1.6804      }
  1.6805      pPgHdr->flags |= PGHDR_NEED_SYNC;
  1.6806      sqlite3PcacheMakeDirty(pPgHdr);
  1.6807 -    sqlite3PagerUnref(pPgHdr);
  1.6808 +    sqlite3PagerUnrefNotNull(pPgHdr);
  1.6809    }
  1.6810  
  1.6811    return SQLITE_OK;
  1.6812 @@ -47598,7 +48786,7 @@
  1.6813      sqlite3OsClose(pRet->pWalFd);
  1.6814      sqlite3_free(pRet);
  1.6815    }else{
  1.6816 -    int iDC = sqlite3OsDeviceCharacteristics(pRet->pWalFd);
  1.6817 +    int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
  1.6818      if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
  1.6819      if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
  1.6820        pRet->padToSectorBoundary = 0;
  1.6821 @@ -48388,8 +49576,8 @@
  1.6822    ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
  1.6823    ** is more of a scheduler yield than an actual delay.  But on the 10th
  1.6824    ** an subsequent retries, the delays start becoming longer and longer, 
  1.6825 -  ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
  1.6826 -  ** The total delay time before giving up is less than 1 second.
  1.6827 +  ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
  1.6828 +  ** The total delay time before giving up is less than 10 seconds.
  1.6829    */
  1.6830    if( cnt>5 ){
  1.6831      int nDelay = 1;                      /* Pause time in microseconds */
  1.6832 @@ -48397,7 +49585,7 @@
  1.6833        VVA_ONLY( pWal->lockError = 1; )
  1.6834        return SQLITE_PROTOCOL;
  1.6835      }
  1.6836 -    if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
  1.6837 +    if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
  1.6838      sqlite3OsSleep(pWal->pVfs, nDelay);
  1.6839    }
  1.6840  
  1.6841 @@ -48969,7 +50157,7 @@
  1.6842      iAmt -= iFirstAmt;
  1.6843      pContent = (void*)(iFirstAmt + (char*)pContent);
  1.6844      assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
  1.6845 -    rc = sqlite3OsSync(p->pFd, p->syncFlags);
  1.6846 +    rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK);
  1.6847      if( iAmt==0 || rc ) return rc;
  1.6848    }
  1.6849    rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
  1.6850 @@ -49903,22 +51091,15 @@
  1.6851    BtShared *pBt;            /* The BtShared this cursor points to */
  1.6852    BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
  1.6853    struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
  1.6854 -#ifndef SQLITE_OMIT_INCRBLOB
  1.6855    Pgno *aOverflow;          /* Cache of overflow page locations */
  1.6856 -#endif
  1.6857 +  CellInfo info;            /* A parse of the cell we are pointing at */
  1.6858 +  i64 nKey;                 /* Size of pKey, or last integer key */
  1.6859 +  void *pKey;               /* Saved key that was cursor last known position */
  1.6860    Pgno pgnoRoot;            /* The root page of this tree */
  1.6861 -  sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
  1.6862 -  CellInfo info;            /* A parse of the cell we are pointing at */
  1.6863 -  i64 nKey;        /* Size of pKey, or last integer key */
  1.6864 -  void *pKey;      /* Saved key that was cursor's last known position */
  1.6865 +  int nOvflAlloc;           /* Allocated size of aOverflow[] array */
  1.6866    int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
  1.6867 -  u8 wrFlag;                /* True if writable */
  1.6868 -  u8 atLast;                /* Cursor pointing to the last entry */
  1.6869 -  u8 validNKey;             /* True if info.nKey is valid */
  1.6870 +  u8 curFlags;              /* zero or more BTCF_* flags defined below */
  1.6871    u8 eState;                /* One of the CURSOR_XXX constants (see below) */
  1.6872 -#ifndef SQLITE_OMIT_INCRBLOB
  1.6873 -  u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
  1.6874 -#endif
  1.6875    u8 hints;                             /* As configured by CursorSetHints() */
  1.6876    i16 iPage;                            /* Index of current page in apPage */
  1.6877    u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
  1.6878 @@ -49926,6 +51107,15 @@
  1.6879  };
  1.6880  
  1.6881  /*
  1.6882 +** Legal values for BtCursor.curFlags
  1.6883 +*/
  1.6884 +#define BTCF_WriteFlag    0x01   /* True if a write cursor */
  1.6885 +#define BTCF_ValidNKey    0x02   /* True if info.nKey is valid */
  1.6886 +#define BTCF_ValidOvfl    0x04   /* True if aOverflow is valid */
  1.6887 +#define BTCF_AtLast       0x08   /* Cursor is pointing ot the last entry */
  1.6888 +#define BTCF_Incrblob     0x10   /* True if an incremental I/O handle */
  1.6889 +
  1.6890 +/*
  1.6891  ** Potential values for BtCursor.eState.
  1.6892  **
  1.6893  ** CURSOR_INVALID:
  1.6894 @@ -50511,7 +51701,7 @@
  1.6895    ** the correct locks are held.  So do not bother - just return true.
  1.6896    ** This case does not come up very often anyhow.
  1.6897    */
  1.6898 -  if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
  1.6899 +  if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
  1.6900      return 1;
  1.6901    }
  1.6902  
  1.6903 @@ -50795,16 +51985,11 @@
  1.6904  }
  1.6905  #endif
  1.6906  
  1.6907 -
  1.6908 -#ifndef SQLITE_OMIT_INCRBLOB
  1.6909 -/*
  1.6910 -** Invalidate the overflow page-list cache for cursor pCur, if any.
  1.6911 -*/
  1.6912 -static void invalidateOverflowCache(BtCursor *pCur){
  1.6913 -  assert( cursorHoldsMutex(pCur) );
  1.6914 -  sqlite3_free(pCur->aOverflow);
  1.6915 -  pCur->aOverflow = 0;
  1.6916 -}
  1.6917 +/*
  1.6918 +** Invalidate the overflow cache of the cursor passed as the first argument.
  1.6919 +** on the shared btree structure pBt.
  1.6920 +*/
  1.6921 +#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
  1.6922  
  1.6923  /*
  1.6924  ** Invalidate the overflow page-list cache for all cursors opened
  1.6925 @@ -50818,6 +52003,7 @@
  1.6926    }
  1.6927  }
  1.6928  
  1.6929 +#ifndef SQLITE_OMIT_INCRBLOB
  1.6930  /*
  1.6931  ** This function is called before modifying the contents of a table
  1.6932  ** to invalidate any incrblob cursors that are open on the
  1.6933 @@ -50840,16 +52026,14 @@
  1.6934    BtShared *pBt = pBtree->pBt;
  1.6935    assert( sqlite3BtreeHoldsMutex(pBtree) );
  1.6936    for(p=pBt->pCursor; p; p=p->pNext){
  1.6937 -    if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
  1.6938 +    if( (p->curFlags & BTCF_Incrblob)!=0 && (isClearTable || p->info.nKey==iRow) ){
  1.6939        p->eState = CURSOR_INVALID;
  1.6940      }
  1.6941    }
  1.6942  }
  1.6943  
  1.6944  #else
  1.6945 -  /* Stub functions when INCRBLOB is omitted */
  1.6946 -  #define invalidateOverflowCache(x)
  1.6947 -  #define invalidateAllOverflowCache(x)
  1.6948 +  /* Stub function when INCRBLOB is omitted */
  1.6949    #define invalidateIncrblobCursors(x,y,z)
  1.6950  #endif /* SQLITE_OMIT_INCRBLOB */
  1.6951  
  1.6952 @@ -51095,20 +52279,32 @@
  1.6953  ** at is deleted out from under them.
  1.6954  **
  1.6955  ** This routine returns an error code if something goes wrong.  The
  1.6956 -** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
  1.6957 +** integer *pHasMoved is set as follows:
  1.6958 +**
  1.6959 +**    0:   The cursor is unchanged
  1.6960 +**    1:   The cursor is still pointing at the same row, but the pointers
  1.6961 +**         returned by sqlite3BtreeKeyFetch() or sqlite3BtreeDataFetch()
  1.6962 +**         might now be invalid because of a balance() or other change to the
  1.6963 +**         b-tree.
  1.6964 +**    2:   The cursor is no longer pointing to the row.  The row might have
  1.6965 +**         been deleted out from under the cursor.
  1.6966  */
  1.6967  SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
  1.6968    int rc;
  1.6969  
  1.6970 +  if( pCur->eState==CURSOR_VALID ){
  1.6971 +    *pHasMoved = 0;
  1.6972 +    return SQLITE_OK;
  1.6973 +  }
  1.6974    rc = restoreCursorPosition(pCur);
  1.6975    if( rc ){
  1.6976 +    *pHasMoved = 2;
  1.6977 +    return rc;
  1.6978 +  }
  1.6979 +  if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
  1.6980 +    *pHasMoved = 2;
  1.6981 +  }else{
  1.6982      *pHasMoved = 1;
  1.6983 -    return rc;
  1.6984 -  }
  1.6985 -  if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
  1.6986 -    *pHasMoved = 1;
  1.6987 -  }else{
  1.6988 -    *pHasMoved = 0;
  1.6989    }
  1.6990    return SQLITE_OK;
  1.6991  }
  1.6992 @@ -51891,13 +53087,12 @@
  1.6993      memset(&data[hdr], 0, pBt->usableSize - hdr);
  1.6994    }
  1.6995    data[hdr] = (char)flags;
  1.6996 -  first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
  1.6997 +  first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
  1.6998    memset(&data[hdr+1], 0, 4);
  1.6999    data[hdr+7] = 0;
  1.7000    put2byte(&data[hdr+5], pBt->usableSize);
  1.7001    pPage->nFree = (u16)(pBt->usableSize - first);
  1.7002    decodeFlags(pPage, flags);
  1.7003 -  pPage->hdrOffset = hdr;
  1.7004    pPage->cellOffset = first;
  1.7005    pPage->aDataEnd = &data[pBt->usableSize];
  1.7006    pPage->aCellIdx = &data[first];
  1.7007 @@ -51976,7 +53171,7 @@
  1.7008  SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
  1.7009    assert( sqlite3BtreeHoldsMutex(p) );
  1.7010    assert( ((p->pBt->nPage)&0x8000000)==0 );
  1.7011 -  return (int)btreePagecount(p->pBt);
  1.7012 +  return btreePagecount(p->pBt);
  1.7013  }
  1.7014  
  1.7015  /*
  1.7016 @@ -52001,7 +53196,7 @@
  1.7017      rc = SQLITE_CORRUPT_BKPT;
  1.7018    }else{
  1.7019      rc = btreeGetPage(pBt, pgno, ppPage, bReadonly);
  1.7020 -    if( rc==SQLITE_OK ){
  1.7021 +    if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
  1.7022        rc = btreeInitPage(*ppPage);
  1.7023        if( rc!=SQLITE_OK ){
  1.7024          releasePage(*ppPage);
  1.7025 @@ -52022,10 +53217,11 @@
  1.7026    if( pPage ){
  1.7027      assert( pPage->aData );
  1.7028      assert( pPage->pBt );
  1.7029 +    assert( pPage->pDbPage!=0 );
  1.7030      assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
  1.7031      assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
  1.7032      assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1.7033 -    sqlite3PagerUnref(pPage->pDbPage);
  1.7034 +    sqlite3PagerUnrefNotNull(pPage->pDbPage);
  1.7035    }
  1.7036  }
  1.7037  
  1.7038 @@ -52510,6 +53706,7 @@
  1.7039    return SQLITE_OK;
  1.7040  }
  1.7041  
  1.7042 +#if SQLITE_MAX_MMAP_SIZE>0
  1.7043  /*
  1.7044  ** Change the limit on the amount of the database file that may be
  1.7045  ** memory mapped.
  1.7046 @@ -52522,6 +53719,7 @@
  1.7047    sqlite3BtreeLeave(p);
  1.7048    return SQLITE_OK;
  1.7049  }
  1.7050 +#endif /* SQLITE_MAX_MMAP_SIZE>0 */
  1.7051  
  1.7052  /*
  1.7053  ** Change the way data is synced to disk in order to increase or decrease
  1.7054 @@ -52898,7 +54096,8 @@
  1.7055    BtCursor *pCur;
  1.7056    int r = 0;
  1.7057    for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
  1.7058 -    if( (wrOnly==0 || pCur->wrFlag) && pCur->eState!=CURSOR_FAULT ) r++; 
  1.7059 +    if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
  1.7060 +     && pCur->eState!=CURSOR_FAULT ) r++; 
  1.7061    }
  1.7062    return r;
  1.7063  }
  1.7064 @@ -53973,14 +55172,14 @@
  1.7065    pCur->pKeyInfo = pKeyInfo;
  1.7066    pCur->pBtree = p;
  1.7067    pCur->pBt = pBt;
  1.7068 -  pCur->wrFlag = (u8)wrFlag;
  1.7069 +  assert( wrFlag==0 || wrFlag==BTCF_WriteFlag );
  1.7070 +  pCur->curFlags = wrFlag;
  1.7071    pCur->pNext = pBt->pCursor;
  1.7072    if( pCur->pNext ){
  1.7073      pCur->pNext->pPrev = pCur;
  1.7074    }
  1.7075    pBt->pCursor = pCur;
  1.7076    pCur->eState = CURSOR_INVALID;
  1.7077 -  pCur->cachedRowid = 0;
  1.7078    return SQLITE_OK;
  1.7079  }
  1.7080  SQLITE_PRIVATE int sqlite3BtreeCursor(
  1.7081 @@ -54022,36 +55221,6 @@
  1.7082  }
  1.7083  
  1.7084  /*
  1.7085 -** Set the cached rowid value of every cursor in the same database file
  1.7086 -** as pCur and having the same root page number as pCur.  The value is
  1.7087 -** set to iRowid.
  1.7088 -**
  1.7089 -** Only positive rowid values are considered valid for this cache.
  1.7090 -** The cache is initialized to zero, indicating an invalid cache.
  1.7091 -** A btree will work fine with zero or negative rowids.  We just cannot
  1.7092 -** cache zero or negative rowids, which means tables that use zero or
  1.7093 -** negative rowids might run a little slower.  But in practice, zero
  1.7094 -** or negative rowids are very uncommon so this should not be a problem.
  1.7095 -*/
  1.7096 -SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
  1.7097 -  BtCursor *p;
  1.7098 -  for(p=pCur->pBt->pCursor; p; p=p->pNext){
  1.7099 -    if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
  1.7100 -  }
  1.7101 -  assert( pCur->cachedRowid==iRowid );
  1.7102 -}
  1.7103 -
  1.7104 -/*
  1.7105 -** Return the cached rowid for the given cursor.  A negative or zero
  1.7106 -** return value indicates that the rowid cache is invalid and should be
  1.7107 -** ignored.  If the rowid cache has never before been set, then a
  1.7108 -** zero is returned.
  1.7109 -*/
  1.7110 -SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
  1.7111 -  return pCur->cachedRowid;
  1.7112 -}
  1.7113 -
  1.7114 -/*
  1.7115  ** Close a cursor.  The read lock on the database file is released
  1.7116  ** when the last cursor is closed.
  1.7117  */
  1.7118 @@ -54074,7 +55243,7 @@
  1.7119        releasePage(pCur->apPage[i]);
  1.7120      }
  1.7121      unlockBtreeIfUnused(pBt);
  1.7122 -    invalidateOverflowCache(pCur);
  1.7123 +    sqlite3DbFree(pBtree->db, pCur->aOverflow);
  1.7124      /* sqlite3_free(pCur); */
  1.7125      sqlite3BtreeLeave(pBtree);
  1.7126    }
  1.7127 @@ -54102,7 +55271,7 @@
  1.7128      int iPage = pCur->iPage;
  1.7129      memset(&info, 0, sizeof(info));
  1.7130      btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
  1.7131 -    assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
  1.7132 +    assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
  1.7133    }
  1.7134  #else
  1.7135    #define assertCellInfo(x)
  1.7136 @@ -54113,7 +55282,7 @@
  1.7137      if( pCur->info.nSize==0 ){
  1.7138        int iPage = pCur->iPage;
  1.7139        btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
  1.7140 -      pCur->validNKey = 1;
  1.7141 +      pCur->curFlags |= BTCF_ValidNKey;
  1.7142      }else{
  1.7143        assertCellInfo(pCur);
  1.7144      }
  1.7145 @@ -54123,8 +55292,8 @@
  1.7146  #define getCellInfo(pCur)                                                      \
  1.7147    if( pCur->info.nSize==0 ){                                                   \
  1.7148      int iPage = pCur->iPage;                                                   \
  1.7149 -    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
  1.7150 -    pCur->validNKey = 1;                                                       \
  1.7151 +    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);        \
  1.7152 +    pCur->curFlags |= BTCF_ValidNKey;                                          \
  1.7153    }else{                                                                       \
  1.7154      assertCellInfo(pCur);                                                      \
  1.7155    }
  1.7156 @@ -54295,10 +55464,12 @@
  1.7157  
  1.7158  /*
  1.7159  ** This function is used to read or overwrite payload information
  1.7160 -** for the entry that the pCur cursor is pointing to. If the eOp
  1.7161 -** parameter is 0, this is a read operation (data copied into
  1.7162 -** buffer pBuf). If it is non-zero, a write (data copied from
  1.7163 -** buffer pBuf).
  1.7164 +** for the entry that the pCur cursor is pointing to. The eOp
  1.7165 +** argument is interpreted as follows:
  1.7166 +**
  1.7167 +**   0: The operation is a read. Populate the overflow cache.
  1.7168 +**   1: The operation is a write. Populate the overflow cache.
  1.7169 +**   2: The operation is a read. Do not populate the overflow cache.
  1.7170  **
  1.7171  ** A total of "amt" bytes are read or written beginning at "offset".
  1.7172  ** Data is read to or from the buffer pBuf.
  1.7173 @@ -54306,11 +55477,11 @@
  1.7174  ** The content being read or written might appear on the main page
  1.7175  ** or be scattered out on multiple overflow pages.
  1.7176  **
  1.7177 -** If the BtCursor.isIncrblobHandle flag is set, and the current
  1.7178 -** cursor entry uses one or more overflow pages, this function
  1.7179 -** allocates space for and lazily popluates the overflow page-list 
  1.7180 -** cache array (BtCursor.aOverflow). Subsequent calls use this
  1.7181 -** cache to make seeking to the supplied offset more efficient.
  1.7182 +** If the current cursor entry uses one or more overflow pages and the
  1.7183 +** eOp argument is not 2, this function may allocate space for and lazily 
  1.7184 +** popluates the overflow page-list cache array (BtCursor.aOverflow). 
  1.7185 +** Subsequent calls use this cache to make seeking to the supplied offset 
  1.7186 +** more efficient.
  1.7187  **
  1.7188  ** Once an overflow page-list cache has been allocated, it may be
  1.7189  ** invalidated if some other cursor writes to the same table, or if
  1.7190 @@ -54334,15 +55505,22 @@
  1.7191    int iIdx = 0;
  1.7192    MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
  1.7193    BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
  1.7194 +#ifdef SQLITE_DIRECT_OVERFLOW_READ
  1.7195 +  int bEnd;                                   /* True if reading to end of data */
  1.7196 +#endif
  1.7197  
  1.7198    assert( pPage );
  1.7199    assert( pCur->eState==CURSOR_VALID );
  1.7200    assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
  1.7201    assert( cursorHoldsMutex(pCur) );
  1.7202 +  assert( eOp!=2 || offset==0 );      /* Always start from beginning for eOp==2 */
  1.7203  
  1.7204    getCellInfo(pCur);
  1.7205    aPayload = pCur->info.pCell + pCur->info.nHeader;
  1.7206    nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
  1.7207 +#ifdef SQLITE_DIRECT_OVERFLOW_READ
  1.7208 +  bEnd = (offset+amt==nKey+pCur->info.nData);
  1.7209 +#endif
  1.7210  
  1.7211    if( NEVER(offset+amt > nKey+pCur->info.nData) 
  1.7212     || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
  1.7213 @@ -54357,7 +55535,7 @@
  1.7214      if( a+offset>pCur->info.nLocal ){
  1.7215        a = pCur->info.nLocal - offset;
  1.7216      }
  1.7217 -    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
  1.7218 +    rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage);
  1.7219      offset = 0;
  1.7220      pBuf += a;
  1.7221      amt -= a;
  1.7222 @@ -54371,21 +55549,30 @@
  1.7223  
  1.7224      nextPage = get4byte(&aPayload[pCur->info.nLocal]);
  1.7225  
  1.7226 -#ifndef SQLITE_OMIT_INCRBLOB
  1.7227 -    /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
  1.7228 -    ** has not been allocated, allocate it now. The array is sized at
  1.7229 -    ** one entry for each overflow page in the overflow chain. The
  1.7230 -    ** page number of the first overflow page is stored in aOverflow[0],
  1.7231 -    ** etc. A value of 0 in the aOverflow[] array means "not yet known"
  1.7232 -    ** (the cache is lazily populated).
  1.7233 -    */
  1.7234 -    if( pCur->isIncrblobHandle && !pCur->aOverflow ){
  1.7235 +    /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
  1.7236 +    ** Except, do not allocate aOverflow[] for eOp==2.
  1.7237 +    **
  1.7238 +    ** The aOverflow[] array is sized at one entry for each overflow page
  1.7239 +    ** in the overflow chain. The page number of the first overflow page is
  1.7240 +    ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
  1.7241 +    ** means "not yet known" (the cache is lazily populated).
  1.7242 +    */
  1.7243 +    if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
  1.7244        int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
  1.7245 -      pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
  1.7246 -      /* nOvfl is always positive.  If it were zero, fetchPayload would have
  1.7247 -      ** been used instead of this routine. */
  1.7248 -      if( ALWAYS(nOvfl) && !pCur->aOverflow ){
  1.7249 -        rc = SQLITE_NOMEM;
  1.7250 +      if( nOvfl>pCur->nOvflAlloc ){
  1.7251 +        Pgno *aNew = (Pgno*)sqlite3DbRealloc(
  1.7252 +            pCur->pBtree->db, pCur->aOverflow, nOvfl*2*sizeof(Pgno)
  1.7253 +        );
  1.7254 +        if( aNew==0 ){
  1.7255 +          rc = SQLITE_NOMEM;
  1.7256 +        }else{
  1.7257 +          pCur->nOvflAlloc = nOvfl*2;
  1.7258 +          pCur->aOverflow = aNew;
  1.7259 +        }
  1.7260 +      }
  1.7261 +      if( rc==SQLITE_OK ){
  1.7262 +        memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
  1.7263 +        pCur->curFlags |= BTCF_ValidOvfl;
  1.7264        }
  1.7265      }
  1.7266  
  1.7267 @@ -54393,22 +55580,19 @@
  1.7268      ** entry for the first required overflow page is valid, skip
  1.7269      ** directly to it.
  1.7270      */
  1.7271 -    if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
  1.7272 +    if( (pCur->curFlags & BTCF_ValidOvfl)!=0 && pCur->aOverflow[offset/ovflSize] ){
  1.7273        iIdx = (offset/ovflSize);
  1.7274        nextPage = pCur->aOverflow[iIdx];
  1.7275        offset = (offset%ovflSize);
  1.7276      }
  1.7277 -#endif
  1.7278  
  1.7279      for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
  1.7280  
  1.7281 -#ifndef SQLITE_OMIT_INCRBLOB
  1.7282        /* If required, populate the overflow page-list cache. */
  1.7283 -      if( pCur->aOverflow ){
  1.7284 +      if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
  1.7285          assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
  1.7286          pCur->aOverflow[iIdx] = nextPage;
  1.7287        }
  1.7288 -#endif
  1.7289  
  1.7290        if( offset>=ovflSize ){
  1.7291          /* The only reason to read this page is to obtain the page
  1.7292 @@ -54416,13 +55600,17 @@
  1.7293          ** data is not required. So first try to lookup the overflow
  1.7294          ** page-list cache, if any, then fall back to the getOverflowPage()
  1.7295          ** function.
  1.7296 +        **
  1.7297 +        ** Note that the aOverflow[] array must be allocated because eOp!=2
  1.7298 +        ** here.  If eOp==2, then offset==0 and this branch is never taken.
  1.7299          */
  1.7300 -#ifndef SQLITE_OMIT_INCRBLOB
  1.7301 -        if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
  1.7302 +        assert( eOp!=2 );
  1.7303 +        assert( pCur->curFlags & BTCF_ValidOvfl );
  1.7304 +        if( pCur->aOverflow[iIdx+1] ){
  1.7305            nextPage = pCur->aOverflow[iIdx+1];
  1.7306 -        } else 
  1.7307 -#endif
  1.7308 +        }else{
  1.7309            rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
  1.7310 +        }
  1.7311          offset -= ovflSize;
  1.7312        }else{
  1.7313          /* Need to read this page properly. It contains some of the
  1.7314 @@ -54444,13 +55632,15 @@
  1.7315          **   3) the database is file-backed, and
  1.7316          **   4) there is no open write-transaction, and
  1.7317          **   5) the database is not a WAL database,
  1.7318 +        **   6) all data from the page is being read.
  1.7319          **
  1.7320          ** then data can be read directly from the database file into the
  1.7321          ** output buffer, bypassing the page-cache altogether. This speeds
  1.7322          ** up loading large records that span many overflow pages.
  1.7323          */
  1.7324 -        if( eOp==0                                             /* (1) */
  1.7325 +        if( (eOp&0x01)==0                                      /* (1) */
  1.7326           && offset==0                                          /* (2) */
  1.7327 +         && (bEnd || a==ovflSize)                              /* (6) */
  1.7328           && pBt->inTransaction==TRANS_READ                     /* (4) */
  1.7329           && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
  1.7330           && pBt->pPage1->aData[19]==0x01                       /* (5) */
  1.7331 @@ -54467,12 +55657,12 @@
  1.7332          {
  1.7333            DbPage *pDbPage;
  1.7334            rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
  1.7335 -              (eOp==0 ? PAGER_GET_READONLY : 0)
  1.7336 +              ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0)
  1.7337            );
  1.7338            if( rc==SQLITE_OK ){
  1.7339              aPayload = sqlite3PagerGetData(pDbPage);
  1.7340              nextPage = get4byte(aPayload);
  1.7341 -            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
  1.7342 +            rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage);
  1.7343              sqlite3PagerUnref(pDbPage);
  1.7344              offset = 0;
  1.7345            }
  1.7346 @@ -54541,10 +55731,10 @@
  1.7347  /*
  1.7348  ** Return a pointer to payload information from the entry that the 
  1.7349  ** pCur cursor is pointing to.  The pointer is to the beginning of
  1.7350 -** the key if skipKey==0 and it points to the beginning of data if
  1.7351 -** skipKey==1.  The number of bytes of available key/data is written
  1.7352 -** into *pAmt.  If *pAmt==0, then the value returned will not be
  1.7353 -** a valid pointer.
  1.7354 +** the key if index btrees (pPage->intKey==0) and is the data for
  1.7355 +** table btrees (pPage->intKey==1). The number of bytes of available
  1.7356 +** key/data is written into *pAmt.  If *pAmt==0, then the value
  1.7357 +** returned will not be a valid pointer.
  1.7358  **
  1.7359  ** This routine is an optimization.  It is common for the entire key
  1.7360  ** and data to fit on the local page and for there to be no overflow
  1.7361 @@ -54557,41 +55747,18 @@
  1.7362  ** page of the database.  The data might change or move the next time
  1.7363  ** any btree routine is called.
  1.7364  */
  1.7365 -static const unsigned char *fetchPayload(
  1.7366 +static const void *fetchPayload(
  1.7367    BtCursor *pCur,      /* Cursor pointing to entry to read from */
  1.7368 -  u32 *pAmt,           /* Write the number of available bytes here */
  1.7369 -  int skipKey          /* read beginning at data if this is true */
  1.7370 -){
  1.7371 -  unsigned char *aPayload;
  1.7372 -  MemPage *pPage;
  1.7373 -  u32 nKey;
  1.7374 -  u32 nLocal;
  1.7375 -
  1.7376 +  u32 *pAmt            /* Write the number of available bytes here */
  1.7377 +){
  1.7378    assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
  1.7379    assert( pCur->eState==CURSOR_VALID );
  1.7380 +  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  1.7381    assert( cursorHoldsMutex(pCur) );
  1.7382 -  pPage = pCur->apPage[pCur->iPage];
  1.7383 -  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
  1.7384 -  if( pCur->info.nSize==0 ){
  1.7385 -    btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
  1.7386 -                   &pCur->info);
  1.7387 -  }
  1.7388 -  aPayload = pCur->info.pCell;
  1.7389 -  aPayload += pCur->info.nHeader;
  1.7390 -  if( pPage->intKey ){
  1.7391 -    nKey = 0;
  1.7392 -  }else{
  1.7393 -    nKey = (int)pCur->info.nKey;
  1.7394 -  }
  1.7395 -  if( skipKey ){
  1.7396 -    aPayload += nKey;
  1.7397 -    nLocal = pCur->info.nLocal - nKey;
  1.7398 -  }else{
  1.7399 -    nLocal = pCur->info.nLocal;
  1.7400 -    assert( nLocal<=nKey );
  1.7401 -  }
  1.7402 -  *pAmt = nLocal;
  1.7403 -  return aPayload;
  1.7404 +  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
  1.7405 +  assert( pCur->info.nSize>0 );
  1.7406 +  *pAmt = pCur->info.nLocal;
  1.7407 +  return (void*)(pCur->info.pCell + pCur->info.nHeader);
  1.7408  }
  1.7409  
  1.7410  
  1.7411 @@ -54610,22 +55777,10 @@
  1.7412  ** in the common case where no overflow pages are used.
  1.7413  */
  1.7414  SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
  1.7415 -  const void *p = 0;
  1.7416 -  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  1.7417 -  assert( cursorHoldsMutex(pCur) );
  1.7418 -  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
  1.7419 -    p = (const void*)fetchPayload(pCur, pAmt, 0);
  1.7420 -  }
  1.7421 -  return p;
  1.7422 +  return fetchPayload(pCur, pAmt);
  1.7423  }
  1.7424  SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
  1.7425 -  const void *p = 0;
  1.7426 -  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  1.7427 -  assert( cursorHoldsMutex(pCur) );
  1.7428 -  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
  1.7429 -    p = (const void*)fetchPayload(pCur, pAmt, 1);
  1.7430 -  }
  1.7431 -  return p;
  1.7432 +  return fetchPayload(pCur, pAmt);
  1.7433  }
  1.7434  
  1.7435  
  1.7436 @@ -54652,14 +55807,14 @@
  1.7437      return SQLITE_CORRUPT_BKPT;
  1.7438    }
  1.7439    rc = getAndInitPage(pBt, newPgno, &pNewPage,
  1.7440 -               pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
  1.7441 +               (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
  1.7442    if( rc ) return rc;
  1.7443    pCur->apPage[i+1] = pNewPage;
  1.7444    pCur->aiIdx[i+1] = 0;
  1.7445    pCur->iPage++;
  1.7446  
  1.7447    pCur->info.nSize = 0;
  1.7448 -  pCur->validNKey = 0;
  1.7449 +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  1.7450    if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
  1.7451      return SQLITE_CORRUPT_BKPT;
  1.7452    }
  1.7453 @@ -54717,7 +55872,7 @@
  1.7454    releasePage(pCur->apPage[pCur->iPage]);
  1.7455    pCur->iPage--;
  1.7456    pCur->info.nSize = 0;
  1.7457 -  pCur->validNKey = 0;
  1.7458 +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  1.7459  }
  1.7460  
  1.7461  /*
  1.7462 @@ -54744,8 +55899,6 @@
  1.7463  static int moveToRoot(BtCursor *pCur){
  1.7464    MemPage *pRoot;
  1.7465    int rc = SQLITE_OK;
  1.7466 -  Btree *p = pCur->pBtree;
  1.7467 -  BtShared *pBt = p->pBt;
  1.7468  
  1.7469    assert( cursorHoldsMutex(pCur) );
  1.7470    assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
  1.7471 @@ -54760,56 +55913,51 @@
  1.7472    }
  1.7473  
  1.7474    if( pCur->iPage>=0 ){
  1.7475 -    int i;
  1.7476 -    for(i=1; i<=pCur->iPage; i++){
  1.7477 -      releasePage(pCur->apPage[i]);
  1.7478 -    }
  1.7479 -    pCur->iPage = 0;
  1.7480 +    while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]);
  1.7481    }else if( pCur->pgnoRoot==0 ){
  1.7482      pCur->eState = CURSOR_INVALID;
  1.7483      return SQLITE_OK;
  1.7484    }else{
  1.7485 -    rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0],
  1.7486 -                        pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
  1.7487 +    rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
  1.7488 +                 (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
  1.7489      if( rc!=SQLITE_OK ){
  1.7490        pCur->eState = CURSOR_INVALID;
  1.7491        return rc;
  1.7492      }
  1.7493      pCur->iPage = 0;
  1.7494 -
  1.7495 -    /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
  1.7496 -    ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
  1.7497 -    ** NULL, the caller expects a table b-tree. If this is not the case,
  1.7498 -    ** return an SQLITE_CORRUPT error.  */
  1.7499 -    assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 );
  1.7500 -    if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){
  1.7501 -      return SQLITE_CORRUPT_BKPT;
  1.7502 -    }
  1.7503 -  }
  1.7504 -
  1.7505 -  /* Assert that the root page is of the correct type. This must be the
  1.7506 -  ** case as the call to this function that loaded the root-page (either
  1.7507 -  ** this call or a previous invocation) would have detected corruption 
  1.7508 -  ** if the assumption were not true, and it is not possible for the flags 
  1.7509 -  ** byte to have been modified while this cursor is holding a reference
  1.7510 -  ** to the page.  */
  1.7511 +  }
  1.7512    pRoot = pCur->apPage[0];
  1.7513    assert( pRoot->pgno==pCur->pgnoRoot );
  1.7514 -  assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
  1.7515 +
  1.7516 +  /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
  1.7517 +  ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
  1.7518 +  ** NULL, the caller expects a table b-tree. If this is not the case,
  1.7519 +  ** return an SQLITE_CORRUPT error. 
  1.7520 +  **
  1.7521 +  ** Earlier versions of SQLite assumed that this test could not fail
  1.7522 +  ** if the root page was already loaded when this function was called (i.e.
  1.7523 +  ** if pCur->iPage>=0). But this is not so if the database is corrupted 
  1.7524 +  ** in such a way that page pRoot is linked into a second b-tree table 
  1.7525 +  ** (or the freelist).  */
  1.7526 +  assert( pRoot->intKey==1 || pRoot->intKey==0 );
  1.7527 +  if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
  1.7528 +    return SQLITE_CORRUPT_BKPT;
  1.7529 +  }
  1.7530  
  1.7531    pCur->aiIdx[0] = 0;
  1.7532    pCur->info.nSize = 0;
  1.7533 -  pCur->atLast = 0;
  1.7534 -  pCur->validNKey = 0;
  1.7535 -
  1.7536 -  if( pRoot->nCell==0 && !pRoot->leaf ){
  1.7537 +  pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
  1.7538 +
  1.7539 +  if( pRoot->nCell>0 ){
  1.7540 +    pCur->eState = CURSOR_VALID;
  1.7541 +  }else if( !pRoot->leaf ){
  1.7542      Pgno subpage;
  1.7543      if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
  1.7544      subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
  1.7545      pCur->eState = CURSOR_VALID;
  1.7546      rc = moveToChild(pCur, subpage);
  1.7547    }else{
  1.7548 -    pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
  1.7549 +    pCur->eState = CURSOR_INVALID;
  1.7550    }
  1.7551    return rc;
  1.7552  }
  1.7553 @@ -54861,7 +56009,7 @@
  1.7554    if( rc==SQLITE_OK ){
  1.7555      pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
  1.7556      pCur->info.nSize = 0;
  1.7557 -    pCur->validNKey = 0;
  1.7558 +    pCur->curFlags &= ~BTCF_ValidNKey;
  1.7559    }
  1.7560    return rc;
  1.7561  }
  1.7562 @@ -54900,7 +56048,7 @@
  1.7563    assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  1.7564  
  1.7565    /* If the cursor already points to the last entry, this is a no-op. */
  1.7566 -  if( CURSOR_VALID==pCur->eState && pCur->atLast ){
  1.7567 +  if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
  1.7568  #ifdef SQLITE_DEBUG
  1.7569      /* This block serves to assert() that the cursor really does point 
  1.7570      ** to the last entry in the b-tree. */
  1.7571 @@ -54923,7 +56071,12 @@
  1.7572        assert( pCur->eState==CURSOR_VALID );
  1.7573        *pRes = 0;
  1.7574        rc = moveToRightmost(pCur);
  1.7575 -      pCur->atLast = rc==SQLITE_OK ?1:0;
  1.7576 +      if( rc==SQLITE_OK ){
  1.7577 +        pCur->curFlags |= BTCF_AtLast;
  1.7578 +      }else{
  1.7579 +        pCur->curFlags &= ~BTCF_AtLast;
  1.7580 +      }
  1.7581 +   
  1.7582      }
  1.7583    }
  1.7584    return rc;
  1.7585 @@ -54965,6 +56118,7 @@
  1.7586    int *pRes                /* Write search results here */
  1.7587  ){
  1.7588    int rc;
  1.7589 +  RecordCompare xRecordCompare;
  1.7590  
  1.7591    assert( cursorHoldsMutex(pCur) );
  1.7592    assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  1.7593 @@ -54973,19 +56127,30 @@
  1.7594  
  1.7595    /* If the cursor is already positioned at the point we are trying
  1.7596    ** to move to, then just return without doing any work */
  1.7597 -  if( pCur->eState==CURSOR_VALID && pCur->validNKey 
  1.7598 +  if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
  1.7599     && pCur->apPage[0]->intKey 
  1.7600    ){
  1.7601      if( pCur->info.nKey==intKey ){
  1.7602        *pRes = 0;
  1.7603        return SQLITE_OK;
  1.7604      }
  1.7605 -    if( pCur->atLast && pCur->info.nKey<intKey ){
  1.7606 +    if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
  1.7607        *pRes = -1;
  1.7608        return SQLITE_OK;
  1.7609      }
  1.7610    }
  1.7611  
  1.7612 +  if( pIdxKey ){
  1.7613 +    xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
  1.7614 +    pIdxKey->isCorrupt = 0;
  1.7615 +    assert( pIdxKey->default_rc==1 
  1.7616 +         || pIdxKey->default_rc==0 
  1.7617 +         || pIdxKey->default_rc==-1
  1.7618 +    );
  1.7619 +  }else{
  1.7620 +    xRecordCompare = 0; /* All keys are integers */
  1.7621 +  }
  1.7622 +
  1.7623    rc = moveToRoot(pCur);
  1.7624    if( rc ){
  1.7625      return rc;
  1.7626 @@ -55018,7 +56183,7 @@
  1.7627      assert( biasRight==0 || biasRight==1 );
  1.7628      idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
  1.7629      pCur->aiIdx[pCur->iPage] = (u16)idx;
  1.7630 -    if( pPage->intKey ){
  1.7631 +    if( xRecordCompare==0 ){
  1.7632        for(;;){
  1.7633          i64 nCellKey;
  1.7634          pCell = findCell(pPage, idx) + pPage->childPtrSize;
  1.7635 @@ -55036,7 +56201,7 @@
  1.7636            if( lwr>upr ){ c = +1; break; }
  1.7637          }else{
  1.7638            assert( nCellKey==intKey );
  1.7639 -          pCur->validNKey = 1;
  1.7640 +          pCur->curFlags |= BTCF_ValidNKey;
  1.7641            pCur->info.nKey = nCellKey;
  1.7642            pCur->aiIdx[pCur->iPage] = (u16)idx;
  1.7643            if( !pPage->leaf ){
  1.7644 @@ -55065,22 +56230,19 @@
  1.7645          ** 2 bytes of the cell.
  1.7646          */
  1.7647          nCell = pCell[0];
  1.7648 -        if( nCell<=pPage->max1bytePayload
  1.7649 -         /* && (pCell+nCell)<pPage->aDataEnd */
  1.7650 -        ){
  1.7651 +        if( nCell<=pPage->max1bytePayload ){
  1.7652            /* This branch runs if the record-size field of the cell is a
  1.7653            ** single byte varint and the record fits entirely on the main
  1.7654            ** b-tree page.  */
  1.7655            testcase( pCell+nCell+1==pPage->aDataEnd );
  1.7656 -          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
  1.7657 +          c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0);
  1.7658          }else if( !(pCell[1] & 0x80) 
  1.7659            && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
  1.7660 -          /* && (pCell+nCell+2)<=pPage->aDataEnd */
  1.7661          ){
  1.7662            /* The record-size field is a 2 byte varint and the record 
  1.7663            ** fits entirely on the main b-tree page.  */
  1.7664            testcase( pCell+nCell+2==pPage->aDataEnd );
  1.7665 -          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
  1.7666 +          c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0);
  1.7667          }else{
  1.7668            /* The record flows over onto one or more overflow pages. In
  1.7669            ** this case the whole cell needs to be parsed, a buffer allocated
  1.7670 @@ -55096,14 +56258,15 @@
  1.7671              goto moveto_finish;
  1.7672            }
  1.7673            pCur->aiIdx[pCur->iPage] = (u16)idx;
  1.7674 -          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
  1.7675 +          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
  1.7676            if( rc ){
  1.7677              sqlite3_free(pCellKey);
  1.7678              goto moveto_finish;
  1.7679            }
  1.7680 -          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
  1.7681 +          c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
  1.7682            sqlite3_free(pCellKey);
  1.7683          }
  1.7684 +        assert( pIdxKey->isCorrupt==0 || c==0 );
  1.7685          if( c<0 ){
  1.7686            lwr = idx+1;
  1.7687          }else if( c>0 ){
  1.7688 @@ -55113,6 +56276,7 @@
  1.7689            *pRes = 0;
  1.7690            rc = SQLITE_OK;
  1.7691            pCur->aiIdx[pCur->iPage] = (u16)idx;
  1.7692 +          if( pIdxKey->isCorrupt ) rc = SQLITE_CORRUPT;
  1.7693            goto moveto_finish;
  1.7694          }
  1.7695          if( lwr>upr ) break;
  1.7696 @@ -55141,7 +56305,7 @@
  1.7697    }
  1.7698  moveto_finish:
  1.7699    pCur->info.nSize = 0;
  1.7700 -  pCur->validNKey = 0;
  1.7701 +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  1.7702    return rc;
  1.7703  }
  1.7704  
  1.7705 @@ -55166,6 +56330,15 @@
  1.7706  ** successful then set *pRes=0.  If the cursor
  1.7707  ** was already pointing to the last entry in the database before
  1.7708  ** this routine was called, then set *pRes=1.
  1.7709 +**
  1.7710 +** The calling function will set *pRes to 0 or 1.  The initial *pRes value
  1.7711 +** will be 1 if the cursor being stepped corresponds to an SQL index and
  1.7712 +** if this routine could have been skipped if that SQL index had been
  1.7713 +** a unique index.  Otherwise the caller will have set *pRes to zero.
  1.7714 +** Zero is the common case. The btree implementation is free to use the
  1.7715 +** initial *pRes value as a hint to improve performance, but the current
  1.7716 +** SQLite btree implementation does not. (Note that the comdb2 btree
  1.7717 +** implementation does use this hint, however.)
  1.7718  */
  1.7719  SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
  1.7720    int rc;
  1.7721 @@ -55174,8 +56347,10 @@
  1.7722  
  1.7723    assert( cursorHoldsMutex(pCur) );
  1.7724    assert( pRes!=0 );
  1.7725 +  assert( *pRes==0 || *pRes==1 );
  1.7726    assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
  1.7727    if( pCur->eState!=CURSOR_VALID ){
  1.7728 +    invalidateOverflowCache(pCur);
  1.7729      rc = restoreCursorPosition(pCur);
  1.7730      if( rc!=SQLITE_OK ){
  1.7731        *pRes = 0;
  1.7732 @@ -55209,7 +56384,7 @@
  1.7733    testcase( idx>pPage->nCell );
  1.7734  
  1.7735    pCur->info.nSize = 0;
  1.7736 -  pCur->validNKey = 0;
  1.7737 +  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  1.7738    if( idx>=pPage->nCell ){
  1.7739      if( !pPage->leaf ){
  1.7740        rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
  1.7741 @@ -55252,6 +56427,15 @@
  1.7742  ** successful then set *pRes=0.  If the cursor
  1.7743  ** was already pointing to the first entry in the database before
  1.7744  ** this routine was called, then set *pRes=1.
  1.7745 +**
  1.7746 +** The calling function will set *pRes to 0 or 1.  The initial *pRes value
  1.7747 +** will be 1 if the cursor being stepped corresponds to an SQL index and
  1.7748 +** if this routine could have been skipped if that SQL index had been
  1.7749 +** a unique index.  Otherwise the caller will have set *pRes to zero.
  1.7750 +** Zero is the common case. The btree implementation is free to use the
  1.7751 +** initial *pRes value as a hint to improve performance, but the current
  1.7752 +** SQLite btree implementation does not. (Note that the comdb2 btree
  1.7753 +** implementation does use this hint, however.)
  1.7754  */
  1.7755  SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
  1.7756    int rc;
  1.7757 @@ -55259,8 +56443,9 @@
  1.7758  
  1.7759    assert( cursorHoldsMutex(pCur) );
  1.7760    assert( pRes!=0 );
  1.7761 +  assert( *pRes==0 || *pRes==1 );
  1.7762    assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
  1.7763 -  pCur->atLast = 0;
  1.7764 +  pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl);
  1.7765    if( pCur->eState!=CURSOR_VALID ){
  1.7766      if( ALWAYS(pCur->eState>=CURSOR_REQUIRESEEK) ){
  1.7767        rc = btreeRestoreCursorPosition(pCur);
  1.7768 @@ -55305,7 +56490,7 @@
  1.7769        moveToParent(pCur);
  1.7770      }
  1.7771      pCur->info.nSize = 0;
  1.7772 -    pCur->validNKey = 0;
  1.7773 +    pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  1.7774  
  1.7775      pCur->aiIdx[pCur->iPage]--;
  1.7776      pPage = pCur->apPage[pCur->iPage];
  1.7777 @@ -55638,6 +56823,7 @@
  1.7778    if( rc==SQLITE_OK ){
  1.7779      if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
  1.7780        releasePage(*ppPage);
  1.7781 +      *ppPage = 0;
  1.7782        return SQLITE_CORRUPT_BKPT;
  1.7783      }
  1.7784      (*ppPage)->isInit = 0;
  1.7785 @@ -55899,7 +57085,7 @@
  1.7786      nHeader += 4;
  1.7787    }
  1.7788    if( pPage->hasData ){
  1.7789 -    nHeader += putVarint(&pCell[nHeader], nData+nZero);
  1.7790 +    nHeader += putVarint32(&pCell[nHeader], nData+nZero);
  1.7791    }else{
  1.7792      nData = nZero = 0;
  1.7793    }
  1.7794 @@ -56027,7 +57213,6 @@
  1.7795    u32 pc;         /* Offset to cell content of cell being deleted */
  1.7796    u8 *data;       /* pPage->aData */
  1.7797    u8 *ptr;        /* Used to move bytes around within data[] */
  1.7798 -  u8 *endPtr;     /* End of loop */
  1.7799    int rc;         /* The return code */
  1.7800    int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
  1.7801  
  1.7802 @@ -56052,13 +57237,8 @@
  1.7803      *pRC = rc;
  1.7804      return;
  1.7805    }
  1.7806 -  endPtr = &pPage->aCellIdx[2*pPage->nCell - 2];
  1.7807 -  assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
  1.7808 -  while( ptr<endPtr ){
  1.7809 -    *(u16*)ptr = *(u16*)&ptr[2];
  1.7810 -    ptr += 2;
  1.7811 -  }
  1.7812    pPage->nCell--;
  1.7813 +  memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
  1.7814    put2byte(&data[hdr+3], pPage->nCell);
  1.7815    pPage->nFree += 2;
  1.7816  }
  1.7817 @@ -56095,15 +57275,13 @@
  1.7818    int ins;          /* Index in data[] where new cell pointer is inserted */
  1.7819    int cellOffset;   /* Address of first cell pointer in data[] */
  1.7820    u8 *data;         /* The content of the whole page */
  1.7821 -  u8 *ptr;          /* Used for moving information around in data[] */
  1.7822 -  u8 *endPtr;       /* End of the loop */
  1.7823 -
  1.7824    int nSkip = (iChild ? 4 : 0);
  1.7825  
  1.7826    if( *pRC ) return;
  1.7827  
  1.7828    assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
  1.7829 -  assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
  1.7830 +  assert( MX_CELL(pPage->pBt)<=10921 );
  1.7831 +  assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
  1.7832    assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
  1.7833    assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
  1.7834    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1.7835 @@ -56148,13 +57326,7 @@
  1.7836      if( iChild ){
  1.7837        put4byte(&data[idx], iChild);
  1.7838      }
  1.7839 -    ptr = &data[end];
  1.7840 -    endPtr = &data[ins];
  1.7841 -    assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
  1.7842 -    while( ptr>endPtr ){
  1.7843 -      *(u16*)ptr = *(u16*)&ptr[-2];
  1.7844 -      ptr -= 2;
  1.7845 -    }
  1.7846 +    memmove(&data[ins+2], &data[ins], end-ins);
  1.7847      put2byte(&data[ins], idx);
  1.7848      put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
  1.7849  #ifndef SQLITE_OMIT_AUTOVACUUM
  1.7850 @@ -57344,7 +58516,7 @@
  1.7851    }
  1.7852  
  1.7853    assert( cursorHoldsMutex(pCur) );
  1.7854 -  assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
  1.7855 +  assert( (pCur->curFlags & BTCF_WriteFlag)!=0 && pBt->inTransaction==TRANS_WRITE
  1.7856                && (pBt->btsFlags & BTS_READ_ONLY)==0 );
  1.7857    assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  1.7858  
  1.7859 @@ -57369,11 +58541,17 @@
  1.7860    rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
  1.7861    if( rc ) return rc;
  1.7862  
  1.7863 -  /* If this is an insert into a table b-tree, invalidate any incrblob 
  1.7864 -  ** cursors open on the row being replaced (assuming this is a replace
  1.7865 -  ** operation - if it is not, the following is a no-op).  */
  1.7866    if( pCur->pKeyInfo==0 ){
  1.7867 +    /* If this is an insert into a table b-tree, invalidate any incrblob 
  1.7868 +    ** cursors open on the row being replaced */
  1.7869      invalidateIncrblobCursors(p, nKey, 0);
  1.7870 +
  1.7871 +    /* If the cursor is currently on the last row and we are appending a
  1.7872 +    ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
  1.7873 +    ** call */
  1.7874 +    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 && pCur->info.nKey==nKey-1 ){
  1.7875 +      loc = -1;
  1.7876 +    }
  1.7877    }
  1.7878  
  1.7879    if( !loc ){
  1.7880 @@ -57424,7 +58602,7 @@
  1.7881  
  1.7882    /* If no error has occurred and pPage has an overflow cell, call balance() 
  1.7883    ** to redistribute the cells within the tree. Since balance() may move
  1.7884 -  ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
  1.7885 +  ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
  1.7886    ** variables.
  1.7887    **
  1.7888    ** Previous versions of SQLite called moveToRoot() to move the cursor
  1.7889 @@ -57443,8 +58621,8 @@
  1.7890    ** row without seeking the cursor. This can be a big performance boost.
  1.7891    */
  1.7892    pCur->info.nSize = 0;
  1.7893 -  pCur->validNKey = 0;
  1.7894    if( rc==SQLITE_OK && pPage->nOverflow ){
  1.7895 +    pCur->curFlags &= ~(BTCF_ValidNKey);
  1.7896      rc = balance(pCur);
  1.7897  
  1.7898      /* Must make sure nOverflow is reset to zero even if the balance()
  1.7899 @@ -57476,7 +58654,7 @@
  1.7900    assert( cursorHoldsMutex(pCur) );
  1.7901    assert( pBt->inTransaction==TRANS_WRITE );
  1.7902    assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
  1.7903 -  assert( pCur->wrFlag );
  1.7904 +  assert( pCur->curFlags & BTCF_WriteFlag );
  1.7905    assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  1.7906    assert( !hasReadConflicts(p, pCur->pgnoRoot) );
  1.7907  
  1.7908 @@ -57499,7 +58677,7 @@
  1.7909    ** sub-tree headed by the child page of the cell being deleted. This makes
  1.7910    ** balancing the tree following the delete operation easier.  */
  1.7911    if( !pPage->leaf ){
  1.7912 -    int notUsed;
  1.7913 +    int notUsed = 0;
  1.7914      rc = sqlite3BtreePrevious(pCur, &notUsed);
  1.7915      if( rc ) return rc;
  1.7916    }
  1.7917 @@ -57752,6 +58930,7 @@
  1.7918    int rc;
  1.7919    unsigned char *pCell;
  1.7920    int i;
  1.7921 +  int hdr;
  1.7922  
  1.7923    assert( sqlite3_mutex_held(pBt->mutex) );
  1.7924    if( pgno>btreePagecount(pBt) ){
  1.7925 @@ -57760,6 +58939,7 @@
  1.7926  
  1.7927    rc = getAndInitPage(pBt, pgno, &pPage, 0);
  1.7928    if( rc ) return rc;
  1.7929 +  hdr = pPage->hdrOffset;
  1.7930    for(i=0; i<pPage->nCell; i++){
  1.7931      pCell = findCell(pPage, i);
  1.7932      if( !pPage->leaf ){
  1.7933 @@ -57770,7 +58950,7 @@
  1.7934      if( rc ) goto cleardatabasepage_out;
  1.7935    }
  1.7936    if( !pPage->leaf ){
  1.7937 -    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
  1.7938 +    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
  1.7939      if( rc ) goto cleardatabasepage_out;
  1.7940    }else if( pnChange ){
  1.7941      assert( pPage->intKey );
  1.7942 @@ -57779,7 +58959,7 @@
  1.7943    if( freePageFlag ){
  1.7944      freePage(pPage, &rc);
  1.7945    }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
  1.7946 -    zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
  1.7947 +    zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
  1.7948    }
  1.7949  
  1.7950  cleardatabasepage_out:
  1.7951 @@ -57820,6 +59000,15 @@
  1.7952  }
  1.7953  
  1.7954  /*
  1.7955 +** Delete all information from the single table that pCur is open on.
  1.7956 +**
  1.7957 +** This routine only work for pCur on an ephemeral table.
  1.7958 +*/
  1.7959 +SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
  1.7960 +  return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
  1.7961 +}
  1.7962 +
  1.7963 +/*
  1.7964  ** Erase all information in a table and add the root of the table to
  1.7965  ** the freelist.  Except, the root of the principle table (the one on
  1.7966  ** page 1) is never added to the freelist.
  1.7967 @@ -58116,7 +59305,7 @@
  1.7968      sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
  1.7969    }
  1.7970    if( zMsg1 ){
  1.7971 -    sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
  1.7972 +    sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1);
  1.7973    }
  1.7974    sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
  1.7975    va_end(ap);
  1.7976 @@ -58778,7 +59967,7 @@
  1.7977    int rc;
  1.7978    assert( cursorHoldsMutex(pCsr) );
  1.7979    assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
  1.7980 -  assert( pCsr->isIncrblobHandle );
  1.7981 +  assert( pCsr->curFlags & BTCF_Incrblob );
  1.7982  
  1.7983    rc = restoreCursorPosition(pCsr);
  1.7984    if( rc!=SQLITE_OK ){
  1.7985 @@ -58807,7 +59996,7 @@
  1.7986    **   (d) there are no conflicting read-locks, and
  1.7987    **   (e) the cursor points at a valid row of an intKey table.
  1.7988    */
  1.7989 -  if( !pCsr->wrFlag ){
  1.7990 +  if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
  1.7991      return SQLITE_READONLY;
  1.7992    }
  1.7993    assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
  1.7994 @@ -58820,20 +60009,10 @@
  1.7995  }
  1.7996  
  1.7997  /* 
  1.7998 -** Set a flag on this cursor to cache the locations of pages from the 
  1.7999 -** overflow list for the current row. This is used by cursors opened
  1.8000 -** for incremental blob IO only.
  1.8001 -**
  1.8002 -** This function sets a flag only. The actual page location cache
  1.8003 -** (stored in BtCursor.aOverflow[]) is allocated and used by function
  1.8004 -** accessPayload() (the worker function for sqlite3BtreeData() and
  1.8005 -** sqlite3BtreePutData()).
  1.8006 -*/
  1.8007 -SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
  1.8008 -  assert( cursorHoldsMutex(pCur) );
  1.8009 -  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  1.8010 -  invalidateOverflowCache(pCur);
  1.8011 -  pCur->isIncrblobHandle = 1;
  1.8012 +** Mark this cursor as an incremental blob cursor.
  1.8013 +*/
  1.8014 +SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
  1.8015 +  pCur->curFlags |= BTCF_Incrblob;
  1.8016  }
  1.8017  #endif
  1.8018  
  1.8019 @@ -58882,6 +60061,13 @@
  1.8020    pCsr->hints = mask;
  1.8021  }
  1.8022  
  1.8023 +/*
  1.8024 +** Return true if the given Btree is read-only.
  1.8025 +*/
  1.8026 +SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
  1.8027 +  return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
  1.8028 +}
  1.8029 +
  1.8030  /************** End of btree.c ***********************************************/
  1.8031  /************** Begin file backup.c ******************************************/
  1.8032  /*
  1.8033 @@ -59410,7 +60596,7 @@
  1.8034  
  1.8035            /* Sync the database file to disk. */
  1.8036            if( rc==SQLITE_OK ){
  1.8037 -            rc = sqlite3PagerSync(pDestPager);
  1.8038 +            rc = sqlite3PagerSync(pDestPager, 0);
  1.8039            }
  1.8040          }else{
  1.8041            sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
  1.8042 @@ -59485,10 +60671,10 @@
  1.8043  
  1.8044    /* Set the error code of the destination database handle. */
  1.8045    rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
  1.8046 -  sqlite3Error(p->pDestDb, rc, 0);
  1.8047 -
  1.8048 -  /* Exit the mutexes and free the backup context structure. */
  1.8049    if( p->pDestDb ){
  1.8050 +    sqlite3Error(p->pDestDb, rc, 0);
  1.8051 +
  1.8052 +    /* Exit the mutexes and free the backup context structure. */
  1.8053      sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
  1.8054    }
  1.8055    sqlite3BtreeLeave(p->pSrc);
  1.8056 @@ -59651,6 +60837,42 @@
  1.8057  ** name sqlite_value
  1.8058  */
  1.8059  
  1.8060 +#ifdef SQLITE_DEBUG
  1.8061 +/*
  1.8062 +** Check invariants on a Mem object.
  1.8063 +**
  1.8064 +** This routine is intended for use inside of assert() statements, like
  1.8065 +** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
  1.8066 +*/
  1.8067 +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
  1.8068 +  /* The MEM_Dyn bit is set if and only if Mem.xDel is a non-NULL destructor
  1.8069 +  ** function for Mem.z 
  1.8070 +  */
  1.8071 +  assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
  1.8072 +  assert( (p->flags & MEM_Dyn)!=0 || p->xDel==0 );
  1.8073 +
  1.8074 +  /* If p holds a string or blob, the Mem.z must point to exactly
  1.8075 +  ** one of the following:
  1.8076 +  **
  1.8077 +  **   (1) Memory in Mem.zMalloc and managed by the Mem object
  1.8078 +  **   (2) Memory to be freed using Mem.xDel
  1.8079 +  **   (3) An ephermal string or blob
  1.8080 +  **   (4) A static string or blob
  1.8081 +  */
  1.8082 +  if( (p->flags & (MEM_Str|MEM_Blob)) && p->z!=0 ){
  1.8083 +    assert( 
  1.8084 +      ((p->z==p->zMalloc)? 1 : 0) +
  1.8085 +      ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
  1.8086 +      ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
  1.8087 +      ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
  1.8088 +    );
  1.8089 +  }
  1.8090 +
  1.8091 +  return 1;
  1.8092 +}
  1.8093 +#endif
  1.8094 +
  1.8095 +
  1.8096  /*
  1.8097  ** If pMem is an object with a valid string representation, this routine
  1.8098  ** ensures the internal encoding for the string representation is
  1.8099 @@ -59692,57 +60914,51 @@
  1.8100  
  1.8101  /*
  1.8102  ** Make sure pMem->z points to a writable allocation of at least 
  1.8103 -** n bytes.
  1.8104 -**
  1.8105 -** If the third argument passed to this function is true, then memory
  1.8106 -** cell pMem must contain a string or blob. In this case the content is
  1.8107 -** preserved. Otherwise, if the third parameter to this function is false,
  1.8108 -** any current string or blob value may be discarded.
  1.8109 -**
  1.8110 -** This function sets the MEM_Dyn flag and clears any xDel callback.
  1.8111 -** It also clears MEM_Ephem and MEM_Static. If the preserve flag is 
  1.8112 -** not set, Mem.n is zeroed.
  1.8113 -*/
  1.8114 -SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
  1.8115 -  assert( 1 >=
  1.8116 -    ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
  1.8117 -    (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 
  1.8118 -    ((pMem->flags&MEM_Ephem) ? 1 : 0) + 
  1.8119 -    ((pMem->flags&MEM_Static) ? 1 : 0)
  1.8120 -  );
  1.8121 +** min(n,32) bytes.
  1.8122 +**
  1.8123 +** If the bPreserve argument is true, then copy of the content of
  1.8124 +** pMem->z into the new allocation.  pMem must be either a string or
  1.8125 +** blob if bPreserve is true.  If bPreserve is false, any prior content
  1.8126 +** in pMem->z is discarded.
  1.8127 +*/
  1.8128 +SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
  1.8129 +  assert( sqlite3VdbeCheckMemInvariants(pMem) );
  1.8130    assert( (pMem->flags&MEM_RowSet)==0 );
  1.8131  
  1.8132 -  /* If the preserve flag is set to true, then the memory cell must already
  1.8133 +  /* If the bPreserve flag is set to true, then the memory cell must already
  1.8134    ** contain a valid string or blob value.  */
  1.8135 -  assert( preserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
  1.8136 -
  1.8137 -  if( n<32 ) n = 32;
  1.8138 -  if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
  1.8139 -    if( preserve && pMem->z==pMem->zMalloc ){
  1.8140 +  assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
  1.8141 +  testcase( bPreserve && pMem->z==0 );
  1.8142 +
  1.8143 +  if( pMem->zMalloc==0 || sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
  1.8144 +    if( n<32 ) n = 32;
  1.8145 +    if( bPreserve && pMem->z==pMem->zMalloc ){
  1.8146        pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
  1.8147 -      preserve = 0;
  1.8148 +      bPreserve = 0;
  1.8149      }else{
  1.8150        sqlite3DbFree(pMem->db, pMem->zMalloc);
  1.8151        pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
  1.8152      }
  1.8153 -  }
  1.8154 -
  1.8155 -  if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
  1.8156 +    if( pMem->zMalloc==0 ){
  1.8157 +      VdbeMemRelease(pMem);
  1.8158 +      pMem->z = 0;
  1.8159 +      pMem->flags = MEM_Null;  
  1.8160 +      return SQLITE_NOMEM;
  1.8161 +    }
  1.8162 +  }