Updated SQLite to version 3.7.15.1 (bug 5235).
authorNicholas Hastings <skamonkey@gmail.com>
Sun Dec 23 17:06:11 2012 -0500 (2012-12-23)
changeset 3721485b1d74b7ef
parent 3720 8048e61b5382
child 3722 641ee592dda6
Updated SQLite to version 3.7.15.1 (bug 5235).
extensions/sqlite/sqlite-source/sqlite3.c
extensions/sqlite/sqlite-source/sqlite3.h
     1.1 --- a/extensions/sqlite/sqlite-source/sqlite3.c	Sun Dec 23 16:59:50 2012 -0500
     1.2 +++ b/extensions/sqlite/sqlite-source/sqlite3.c	Sun Dec 23 17:06:11 2012 -0500
     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.7.13.  By combining all the individual C code files into this 
     1.7 +** version 3.7.15.1.  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 @@ -389,6 +389,7 @@
    1.12  **
    1.13  **     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
    1.14  **     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
    1.15 +**     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails
    1.16  **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
    1.17  **
    1.18  ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
    1.19 @@ -402,11 +403,19 @@
    1.20  ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
    1.21  ** the default.
    1.22  */
    1.23 -#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
    1.24 -# error "At most one of the following compile-time configuration options\
    1.25 - is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
    1.26 -#endif
    1.27 -#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
    1.28 +#if defined(SQLITE_SYSTEM_MALLOC) \
    1.29 +  + defined(SQLITE_WIN32_MALLOC) \
    1.30 +  + defined(SQLITE_ZERO_MALLOC) \
    1.31 +  + defined(SQLITE_MEMDEBUG)>1
    1.32 +# error "Two or more of the following compile-time configuration options\
    1.33 + are defined but at most one is allowed:\
    1.34 + SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
    1.35 + SQLITE_ZERO_MALLOC"
    1.36 +#endif
    1.37 +#if defined(SQLITE_SYSTEM_MALLOC) \
    1.38 +  + defined(SQLITE_WIN32_MALLOC) \
    1.39 +  + defined(SQLITE_ZERO_MALLOC) \
    1.40 +  + defined(SQLITE_MEMDEBUG)==0
    1.41  # define SQLITE_SYSTEM_MALLOC 1
    1.42  #endif
    1.43  
    1.44 @@ -664,9 +673,9 @@
    1.45  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
    1.46  ** [sqlite_version()] and [sqlite_source_id()].
    1.47  */
    1.48 -#define SQLITE_VERSION        "3.7.13"
    1.49 -#define SQLITE_VERSION_NUMBER 3007013
    1.50 -#define SQLITE_SOURCE_ID      "2012-06-11 02:05:22 f5b5a13f7394dc143aa136f1d4faba6839eaa6dc"
    1.51 +#define SQLITE_VERSION        "3.7.15.1"
    1.52 +#define SQLITE_VERSION_NUMBER 3007015
    1.53 +#define SQLITE_SOURCE_ID      "2012-12-19 20:39:10 6b85b767d0ff7975146156a99ad673f2c1a23318"
    1.54  
    1.55  /*
    1.56  ** CAPI3REF: Run-Time Library Version Numbers
    1.57 @@ -776,7 +785,8 @@
    1.58  ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
    1.59  ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
    1.60  ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
    1.61 -** is its destructor.  There are many other interfaces (such as
    1.62 +** and [sqlite3_close_v2()] are its destructors.  There are many other
    1.63 +** interfaces (such as
    1.64  ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
    1.65  ** [sqlite3_busy_timeout()] to name but three) that are methods on an
    1.66  ** sqlite3 object.
    1.67 @@ -823,28 +833,46 @@
    1.68  /*
    1.69  ** CAPI3REF: Closing A Database Connection
    1.70  **
    1.71 -** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
    1.72 -** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
    1.73 -** successfully destroyed and all associated resources are deallocated.
    1.74 -**
    1.75 -** Applications must [sqlite3_finalize | finalize] all [prepared statements]
    1.76 -** and [sqlite3_blob_close | close] all [BLOB handles] associated with
    1.77 -** the [sqlite3] object prior to attempting to close the object.  ^If
    1.78 +** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
    1.79 +** for the [sqlite3] object.
    1.80 +** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
    1.81 +** the [sqlite3] object is successfully destroyed and all associated
    1.82 +** resources are deallocated.
    1.83 +**
    1.84 +** ^If the database connection is associated with unfinalized prepared
    1.85 +** statements or unfinished sqlite3_backup objects then sqlite3_close()
    1.86 +** will leave the database connection open and return [SQLITE_BUSY].
    1.87 +** ^If sqlite3_close_v2() is called with unfinalized prepared statements
    1.88 +** and unfinished sqlite3_backups, then the database connection becomes
    1.89 +** an unusable "zombie" which will automatically be deallocated when the
    1.90 +** last prepared statement is finalized or the last sqlite3_backup is
    1.91 +** finished.  The sqlite3_close_v2() interface is intended for use with
    1.92 +** host languages that are garbage collected, and where the order in which
    1.93 +** destructors are called is arbitrary.
    1.94 +**
    1.95 +** Applications should [sqlite3_finalize | finalize] all [prepared statements],
    1.96 +** [sqlite3_blob_close | close] all [BLOB handles], and 
    1.97 +** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
    1.98 +** with the [sqlite3] object prior to attempting to close the object.  ^If
    1.99  ** sqlite3_close() is called on a [database connection] that still has
   1.100 -** outstanding [prepared statements] or [BLOB handles], then it returns
   1.101 -** SQLITE_BUSY.
   1.102 -**
   1.103 -** ^If [sqlite3_close()] is invoked while a transaction is open,
   1.104 +** outstanding [prepared statements], [BLOB handles], and/or
   1.105 +** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
   1.106 +** of resources is deferred until all [prepared statements], [BLOB handles],
   1.107 +** and [sqlite3_backup] objects are also destroyed.
   1.108 +**
   1.109 +** ^If an [sqlite3] object is destroyed while a transaction is open,
   1.110  ** the transaction is automatically rolled back.
   1.111  **
   1.112 -** The C parameter to [sqlite3_close(C)] must be either a NULL
   1.113 +** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
   1.114 +** must be either a NULL
   1.115  ** pointer or an [sqlite3] object pointer obtained
   1.116  ** from [sqlite3_open()], [sqlite3_open16()], or
   1.117  ** [sqlite3_open_v2()], and not previously closed.
   1.118 -** ^Calling sqlite3_close() with a NULL pointer argument is a 
   1.119 -** harmless no-op.
   1.120 -*/
   1.121 -SQLITE_API int sqlite3_close(sqlite3 *);
   1.122 +** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
   1.123 +** argument is a harmless no-op.
   1.124 +*/
   1.125 +SQLITE_API int sqlite3_close(sqlite3*);
   1.126 +SQLITE_API int sqlite3_close_v2(sqlite3*);
   1.127  
   1.128  /*
   1.129  ** The type for a callback function.
   1.130 @@ -1012,10 +1040,12 @@
   1.131  #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
   1.132  #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
   1.133  #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
   1.134 +#define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
   1.135  #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
   1.136  #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
   1.137  #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
   1.138  #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
   1.139 +#define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
   1.140  #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
   1.141  #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
   1.142  #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
   1.143 @@ -1055,7 +1085,7 @@
   1.144  ** CAPI3REF: Device Characteristics
   1.145  **
   1.146  ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
   1.147 -** object returns an integer which is a vector of the these
   1.148 +** object returns an integer which is a vector of these
   1.149  ** bit values expressing I/O characteristics of the mass storage
   1.150  ** device that holds the file that the [sqlite3_io_methods]
   1.151  ** refers to.
   1.152 @@ -1393,6 +1423,26 @@
   1.153  ** compilation of the PRAGMA fails with an error.  ^The [SQLITE_FCNTL_PRAGMA]
   1.154  ** file control occurs at the beginning of pragma statement analysis and so
   1.155  ** it is able to override built-in [PRAGMA] statements.
   1.156 +**
   1.157 +** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
   1.158 +** ^This file-control may be invoked by SQLite on the database file handle
   1.159 +** shortly after it is opened in order to provide a custom VFS with access
   1.160 +** to the connections busy-handler callback. The argument is of type (void **)
   1.161 +** - an array of two (void *) values. The first (void *) actually points
   1.162 +** to a function of type (int (*)(void *)). In order to invoke the connections
   1.163 +** busy-handler, this function should be invoked with the second (void *) in
   1.164 +** the array as the only argument. If it returns non-zero, then the operation
   1.165 +** should be retried. If it returns zero, the custom VFS should abandon the
   1.166 +** current operation.
   1.167 +**
   1.168 +** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
   1.169 +** ^Application can invoke this file-control to have SQLite generate a
   1.170 +** temporary filename using the same algorithm that is followed to generate
   1.171 +** temporary filenames for TEMP tables and other internal uses.  The
   1.172 +** argument should be a char** which will be filled with the filename
   1.173 +** written into memory obtained from [sqlite3_malloc()].  The caller should
   1.174 +** invoke [sqlite3_free()] on the result to avoid a memory leak.
   1.175 +**
   1.176  ** </ul>
   1.177  */
   1.178  #define SQLITE_FCNTL_LOCKSTATE               1
   1.179 @@ -1409,6 +1459,8 @@
   1.180  #define SQLITE_FCNTL_VFSNAME                12
   1.181  #define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
   1.182  #define SQLITE_FCNTL_PRAGMA                 14
   1.183 +#define SQLITE_FCNTL_BUSYHANDLER            15
   1.184 +#define SQLITE_FCNTL_TEMPFILENAME           16
   1.185  
   1.186  /*
   1.187  ** CAPI3REF: Mutex Handle
   1.188 @@ -2105,11 +2157,39 @@
   1.189  ** disabled. The default value may be changed by compiling with the
   1.190  ** [SQLITE_USE_URI] symbol defined.
   1.191  **
   1.192 +** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
   1.193 +** <dd> This option takes a single integer argument which is interpreted as
   1.194 +** a boolean in order to enable or disable the use of covering indices for
   1.195 +** full table scans in the query optimizer.  The default setting is determined
   1.196 +** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
   1.197 +** if that compile-time option is omitted.
   1.198 +** The ability to disable the use of covering indices for full table scans
   1.199 +** is because some incorrectly coded legacy applications might malfunction
   1.200 +** malfunction when the optimization is enabled.  Providing the ability to
   1.201 +** disable the optimization allows the older, buggy application code to work
   1.202 +** without change even with newer versions of SQLite.
   1.203 +**
   1.204  ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
   1.205  ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
   1.206  ** <dd> These options are obsolete and should not be used by new code.
   1.207  ** They are retained for backwards compatibility but are now no-ops.
   1.208  ** </dl>
   1.209 +**
   1.210 +** [[SQLITE_CONFIG_SQLLOG]]
   1.211 +** <dt>SQLITE_CONFIG_SQLLOG
   1.212 +** <dd>This option is only available if sqlite is compiled with the
   1.213 +** SQLITE_ENABLE_SQLLOG pre-processor macro defined. The first argument should
   1.214 +** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
   1.215 +** The second should be of type (void*). The callback is invoked by the library
   1.216 +** in three separate circumstances, identified by the value passed as the
   1.217 +** fourth parameter. If the fourth parameter is 0, then the database connection
   1.218 +** passed as the second argument has just been opened. The third argument
   1.219 +** points to a buffer containing the name of the main database file. If the
   1.220 +** fourth parameter is 1, then the SQL statement that the third parameter
   1.221 +** points to has just been executed. Or, if the fourth parameter is 2, then
   1.222 +** the connection being passed as the second parameter is being closed. The
   1.223 +** third parameter is passed NULL In this case.
   1.224 +** </dl>
   1.225  */
   1.226  #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
   1.227  #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
   1.228 @@ -2130,6 +2210,8 @@
   1.229  #define SQLITE_CONFIG_URI          17  /* int */
   1.230  #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
   1.231  #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
   1.232 +#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
   1.233 +#define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
   1.234  
   1.235  /*
   1.236  ** CAPI3REF: Database Connection Configuration Options
   1.237 @@ -3138,7 +3220,7 @@
   1.238  **     an error)^. 
   1.239  **     ^If "ro" is specified, then the database is opened for read-only 
   1.240  **     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the 
   1.241 -**     third argument to sqlite3_prepare_v2(). ^If the mode option is set to 
   1.242 +**     third argument to sqlite3_open_v2(). ^If the mode option is set to 
   1.243  **     "rw", then the database is opened for read-write (but not create) 
   1.244  **     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had 
   1.245  **     been set. ^Value "rwc" is equivalent to setting both 
   1.246 @@ -3205,6 +3287,12 @@
   1.247  ** codepage is currently defined.  Filenames containing international
   1.248  ** characters must be converted to UTF-8 prior to passing them into
   1.249  ** sqlite3_open() or sqlite3_open_v2().
   1.250 +**
   1.251 +** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
   1.252 +** prior to calling sqlite3_open() or sqlite3_open_v2().  Otherwise, various
   1.253 +** features that require the use of temporary files may fail.
   1.254 +**
   1.255 +** See also: [sqlite3_temp_directory]
   1.256  */
   1.257  SQLITE_API int sqlite3_open(
   1.258    const char *filename,   /* Database filename (UTF-8) */
   1.259 @@ -3284,6 +3372,11 @@
   1.260  ** However, the error string might be overwritten or deallocated by
   1.261  ** subsequent calls to other SQLite interface functions.)^
   1.262  **
   1.263 +** ^The sqlite3_errstr() interface returns the English-language text
   1.264 +** that describes the [result code], as UTF-8.
   1.265 +** ^(Memory to hold the error message string is managed internally
   1.266 +** and must not be freed by the application)^.
   1.267 +**
   1.268  ** When the serialized [threading mode] is in use, it might be the
   1.269  ** case that a second error occurs on a separate thread in between
   1.270  ** the time of the first error and the call to these interfaces.
   1.271 @@ -3302,6 +3395,7 @@
   1.272  SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
   1.273  SQLITE_API const char *sqlite3_errmsg(sqlite3*);
   1.274  SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
   1.275 +SQLITE_API const char *sqlite3_errstr(int);
   1.276  
   1.277  /*
   1.278  ** CAPI3REF: SQL Statement Object
   1.279 @@ -3697,8 +3791,11 @@
   1.280  ** ^(In those routines that have a fourth argument, its value is the
   1.281  ** number of bytes in the parameter.  To be clear: the value is the
   1.282  ** number of <u>bytes</u> in the value, not the number of characters.)^
   1.283 -** ^If the fourth parameter is negative, the length of the string is
   1.284 +** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
   1.285 +** is negative, then the length of the string is
   1.286  ** the number of bytes up to the first zero terminator.
   1.287 +** If the fourth parameter to sqlite3_bind_blob() is negative, then
   1.288 +** the behavior is undefined.
   1.289  ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
   1.290  ** or sqlite3_bind_text16() then that parameter must be the byte offset
   1.291  ** where the NUL terminator would occur assuming the string were NUL
   1.292 @@ -4695,11 +4792,11 @@
   1.293  ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
   1.294  ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
   1.295  **
   1.296 -** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
   1.297 -** indicating that a string or BLOB is too long to represent.
   1.298 -**
   1.299 -** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
   1.300 -** indicating that a memory allocation failed.
   1.301 +** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
   1.302 +** error indicating that a string or BLOB is too long to represent.
   1.303 +**
   1.304 +** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
   1.305 +** error indicating that a memory allocation failed.
   1.306  **
   1.307  ** ^The sqlite3_result_int() interface sets the return value
   1.308  ** of the application-defined function to be the 32-bit signed integer
   1.309 @@ -5006,6 +5103,21 @@
   1.310  ** Hence, if this variable is modified directly, either it should be
   1.311  ** made NULL or made to point to memory obtained from [sqlite3_malloc]
   1.312  ** or else the use of the [temp_store_directory pragma] should be avoided.
   1.313 +**
   1.314 +** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
   1.315 +** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
   1.316 +** features that require the use of temporary files may fail.  Here is an
   1.317 +** example of how to do this using C++ with the Windows Runtime:
   1.318 +**
   1.319 +** <blockquote><pre>
   1.320 +** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
   1.321 +** &nbsp;     TemporaryFolder->Path->Data();
   1.322 +** char zPathBuf&#91;MAX_PATH + 1&#93;;
   1.323 +** memset(zPathBuf, 0, sizeof(zPathBuf));
   1.324 +** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
   1.325 +** &nbsp;     NULL, NULL);
   1.326 +** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
   1.327 +** </pre></blockquote>
   1.328  */
   1.329  SQLITE_API char *sqlite3_temp_directory;
   1.330  
   1.331 @@ -5246,6 +5358,9 @@
   1.332  ** future releases of SQLite.  Applications that care about shared
   1.333  ** cache setting should set it explicitly.
   1.334  **
   1.335 +** This interface is threadsafe on processors where writing a
   1.336 +** 32-bit integer is atomic.
   1.337 +**
   1.338  ** See Also:  [SQLite Shared-Cache Mode]
   1.339  */
   1.340  SQLITE_API int sqlite3_enable_shared_cache(int);
   1.341 @@ -6051,7 +6166,6 @@
   1.342  ** implementations are available in the SQLite core:
   1.343  **
   1.344  ** <ul>
   1.345 -** <li>   SQLITE_MUTEX_OS2
   1.346  ** <li>   SQLITE_MUTEX_PTHREADS
   1.347  ** <li>   SQLITE_MUTEX_W32
   1.348  ** <li>   SQLITE_MUTEX_NOOP
   1.349 @@ -6059,9 +6173,9 @@
   1.350  **
   1.351  ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
   1.352  ** that does no real locking and is appropriate for use in
   1.353 -** a single-threaded application.  ^The SQLITE_MUTEX_OS2,
   1.354 -** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
   1.355 -** are appropriate for use on OS/2, Unix, and Windows.
   1.356 +** a single-threaded application.  ^The SQLITE_MUTEX_PTHREADS and
   1.357 +** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
   1.358 +** and Windows.
   1.359  **
   1.360  ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
   1.361  ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
   1.362 @@ -8212,6 +8326,7 @@
   1.363  typedef struct RowSet RowSet;
   1.364  typedef struct Savepoint Savepoint;
   1.365  typedef struct Select Select;
   1.366 +typedef struct SelectDest SelectDest;
   1.367  typedef struct SrcList SrcList;
   1.368  typedef struct StrAccum StrAccum;
   1.369  typedef struct Table Table;
   1.370 @@ -8308,6 +8423,9 @@
   1.371  SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
   1.372  SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
   1.373  SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
   1.374 +#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
   1.375 +SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
   1.376 +#endif
   1.377  SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
   1.378  SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
   1.379  SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
   1.380 @@ -8351,6 +8469,8 @@
   1.381  SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
   1.382  SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
   1.383  
   1.384 +SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
   1.385 +
   1.386  /*
   1.387  ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
   1.388  ** should be one of the following values. The integer values are assigned 
   1.389 @@ -8372,6 +8492,12 @@
   1.390  #define BTREE_USER_VERSION        6
   1.391  #define BTREE_INCR_VACUUM         7
   1.392  
   1.393 +/*
   1.394 +** Values that may be OR'd together to form the second argument of an
   1.395 +** sqlite3BtreeCursorHints() call.
   1.396 +*/
   1.397 +#define BTREE_BULKLOAD 0x00000001
   1.398 +
   1.399  SQLITE_PRIVATE int sqlite3BtreeCursor(
   1.400    Btree*,                              /* BTree containing table to open */
   1.401    int iTable,                          /* Index of root page */
   1.402 @@ -8415,8 +8541,8 @@
   1.403  SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
   1.404  SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
   1.405  SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
   1.406 -
   1.407  SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
   1.408 +SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
   1.409  
   1.410  #ifndef NDEBUG
   1.411  SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
   1.412 @@ -8812,7 +8938,7 @@
   1.413  #define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
   1.414  #define OPFLG_INITIALIZER {\
   1.415  /*   0 */ 0x00, 0x01, 0x01, 0x04, 0x04, 0x10, 0x00, 0x02,\
   1.416 -/*   8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
   1.417 +/*   8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x24,\
   1.418  /*  16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
   1.419  /*  24 */ 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00,\
   1.420  /*  32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
   1.421 @@ -8859,7 +8985,7 @@
   1.422  SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
   1.423  SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
   1.424  SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
   1.425 -SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
   1.426 +SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
   1.427  SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
   1.428  SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
   1.429  SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
   1.430 @@ -9049,11 +9175,14 @@
   1.431  SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
   1.432  SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
   1.433  
   1.434 -SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
   1.435 -SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
   1.436 -SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
   1.437 -SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
   1.438 -SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
   1.439 +#ifndef SQLITE_OMIT_WAL
   1.440 +SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
   1.441 +SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
   1.442 +SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
   1.443 +SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
   1.444 +SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager);
   1.445 +#endif
   1.446 +
   1.447  #ifdef SQLITE_ENABLE_ZIPVFS
   1.448  SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
   1.449  #endif
   1.450 @@ -9071,6 +9200,7 @@
   1.451  SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
   1.452  SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
   1.453  SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
   1.454 +SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
   1.455  
   1.456  /* Functions used to truncate the database file. */
   1.457  SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
   1.458 @@ -9290,7 +9420,7 @@
   1.459  /*
   1.460  ** Figure out if we are dealing with Unix, Windows, or some other
   1.461  ** operating system.  After the following block of preprocess macros,
   1.462 -** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER 
   1.463 +** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER 
   1.464  ** will defined to either 1 or 0.  One of the four will be 1.  The other 
   1.465  ** three will be 0.
   1.466  */
   1.467 @@ -9300,8 +9430,6 @@
   1.468  #   define SQLITE_OS_UNIX 0
   1.469  #   undef SQLITE_OS_WIN
   1.470  #   define SQLITE_OS_WIN 0
   1.471 -#   undef SQLITE_OS_OS2
   1.472 -#   define SQLITE_OS_OS2 0
   1.473  # else
   1.474  #   undef SQLITE_OS_OTHER
   1.475  # endif
   1.476 @@ -9312,19 +9440,12 @@
   1.477  #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
   1.478  #     define SQLITE_OS_WIN 1
   1.479  #     define SQLITE_OS_UNIX 0
   1.480 -#     define SQLITE_OS_OS2 0
   1.481 -#   elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
   1.482 -#     define SQLITE_OS_WIN 0
   1.483 -#     define SQLITE_OS_UNIX 0
   1.484 -#     define SQLITE_OS_OS2 1
   1.485  #   else
   1.486  #     define SQLITE_OS_WIN 0
   1.487  #     define SQLITE_OS_UNIX 1
   1.488 -#     define SQLITE_OS_OS2 0
   1.489  #  endif
   1.490  # else
   1.491  #  define SQLITE_OS_UNIX 0
   1.492 -#  define SQLITE_OS_OS2 0
   1.493  # endif
   1.494  #else
   1.495  # ifndef SQLITE_OS_WIN
   1.496 @@ -9336,21 +9457,6 @@
   1.497  # include <windows.h>
   1.498  #endif
   1.499  
   1.500 -#if SQLITE_OS_OS2
   1.501 -# if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
   1.502 -#  include <os2safe.h> /* has to be included before os2.h for linking to work */
   1.503 -# endif
   1.504 -# define INCL_DOSDATETIME
   1.505 -# define INCL_DOSFILEMGR
   1.506 -# define INCL_DOSERRORS
   1.507 -# define INCL_DOSMISC
   1.508 -# define INCL_DOSPROCESS
   1.509 -# define INCL_DOSMODULEMGR
   1.510 -# define INCL_DOSSEMAPHORES
   1.511 -# include <os2.h>
   1.512 -# include <uconv.h>
   1.513 -#endif
   1.514 -
   1.515  /*
   1.516  ** Determine if we are dealing with Windows NT.
   1.517  **
   1.518 @@ -9383,8 +9489,8 @@
   1.519  #endif
   1.520  
   1.521  /*
   1.522 -** Determine if we are dealing with WindowsRT (Metro) as this has a different and
   1.523 -** incompatible API from win32.
   1.524 +** Determine if we are dealing with WinRT, which provides only a subset of
   1.525 +** the full Win32 API.
   1.526  */
   1.527  #if !defined(SQLITE_OS_WINRT)
   1.528  # define SQLITE_OS_WINRT 0
   1.529 @@ -9620,8 +9726,6 @@
   1.530  **   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
   1.531  **
   1.532  **   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
   1.533 -**
   1.534 -**   SQLITE_MUTEX_OS2          For multi-threaded applications on OS/2.
   1.535  */
   1.536  #if !SQLITE_THREADSAFE
   1.537  # define SQLITE_MUTEX_OMIT
   1.538 @@ -9631,8 +9735,6 @@
   1.539  #    define SQLITE_MUTEX_PTHREADS
   1.540  #  elif SQLITE_OS_WIN
   1.541  #    define SQLITE_MUTEX_W32
   1.542 -#  elif SQLITE_OS_OS2
   1.543 -#    define SQLITE_MUTEX_OS2
   1.544  #  else
   1.545  #    define SQLITE_MUTEX_NOOP
   1.546  #  endif
   1.547 @@ -9796,6 +9898,7 @@
   1.548    unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
   1.549    int errCode;                  /* Most recent error code (SQLITE_*) */
   1.550    int errMask;                  /* & result codes with this before returning */
   1.551 +  u16 dbOptFlags;               /* Flags to enable/disable optimizations */
   1.552    u8 autoCommit;                /* The auto-commit flag. */
   1.553    u8 temp_store;                /* 1: file 2: memory 0: default */
   1.554    u8 mallocFailed;              /* True if we have seen a malloc failure */
   1.555 @@ -9900,48 +10003,59 @@
   1.556  /*
   1.557  ** Possible values for the sqlite3.flags.
   1.558  */
   1.559 -#define SQLITE_VdbeTrace      0x00000100  /* True to trace VDBE execution */
   1.560 -#define SQLITE_InternChanges  0x00000200  /* Uncommitted Hash table changes */
   1.561 -#define SQLITE_FullColNames   0x00000400  /* Show full column names on SELECT */
   1.562 -#define SQLITE_ShortColNames  0x00000800  /* Show short columns names */
   1.563 -#define SQLITE_CountRows      0x00001000  /* Count rows changed by INSERT, */
   1.564 +#define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
   1.565 +#define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
   1.566 +#define SQLITE_FullColNames   0x00000004  /* Show full column names on SELECT */
   1.567 +#define SQLITE_ShortColNames  0x00000008  /* Show short columns names */
   1.568 +#define SQLITE_CountRows      0x00000010  /* Count rows changed by INSERT, */
   1.569                                            /*   DELETE, or UPDATE and return */
   1.570                                            /*   the count using a callback. */
   1.571 -#define SQLITE_NullCallback   0x00002000  /* Invoke the callback once if the */
   1.572 +#define SQLITE_NullCallback   0x00000020  /* Invoke the callback once if the */
   1.573                                            /*   result set is empty */
   1.574 -#define SQLITE_SqlTrace       0x00004000  /* Debug print SQL as it executes */
   1.575 -#define SQLITE_VdbeListing    0x00008000  /* Debug listings of VDBE programs */
   1.576 -#define SQLITE_WriteSchema    0x00010000  /* OK to update SQLITE_MASTER */
   1.577 -                         /*   0x00020000  Unused */
   1.578 -#define SQLITE_IgnoreChecks   0x00040000  /* Do not enforce check constraints */
   1.579 -#define SQLITE_ReadUncommitted 0x0080000  /* For shared-cache mode */
   1.580 -#define SQLITE_LegacyFileFmt  0x00100000  /* Create new databases in format 1 */
   1.581 -#define SQLITE_FullFSync      0x00200000  /* Use full fsync on the backend */
   1.582 -#define SQLITE_CkptFullFSync  0x00400000  /* Use full fsync for checkpoint */
   1.583 -#define SQLITE_RecoveryMode   0x00800000  /* Ignore schema errors */
   1.584 -#define SQLITE_ReverseOrder   0x01000000  /* Reverse unordered SELECTs */
   1.585 -#define SQLITE_RecTriggers    0x02000000  /* Enable recursive triggers */
   1.586 -#define SQLITE_ForeignKeys    0x04000000  /* Enforce foreign key constraints  */
   1.587 -#define SQLITE_AutoIndex      0x08000000  /* Enable automatic indexes */
   1.588 -#define SQLITE_PreferBuiltin  0x10000000  /* Preference to built-in funcs */
   1.589 -#define SQLITE_LoadExtension  0x20000000  /* Enable load_extension */
   1.590 -#define SQLITE_EnableTrigger  0x40000000  /* True to enable triggers */
   1.591 -
   1.592 -/*
   1.593 -** Bits of the sqlite3.flags field that are used by the
   1.594 -** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
   1.595 -** These must be the low-order bits of the flags field.
   1.596 -*/
   1.597 -#define SQLITE_QueryFlattener 0x01        /* Disable query flattening */
   1.598 -#define SQLITE_ColumnCache    0x02        /* Disable the column cache */
   1.599 -#define SQLITE_IndexSort      0x04        /* Disable indexes for sorting */
   1.600 -#define SQLITE_IndexSearch    0x08        /* Disable indexes for searching */
   1.601 -#define SQLITE_IndexCover     0x10        /* Disable index covering table */
   1.602 -#define SQLITE_GroupByOrder   0x20        /* Disable GROUPBY cover of ORDERBY */
   1.603 -#define SQLITE_FactorOutConst 0x40        /* Disable factoring out constants */
   1.604 -#define SQLITE_IdxRealAsInt   0x80        /* Store REAL as INT in indices */
   1.605 -#define SQLITE_DistinctOpt    0x80        /* DISTINCT using indexes */
   1.606 -#define SQLITE_OptMask        0xff        /* Mask of all disablable opts */
   1.607 +#define SQLITE_SqlTrace       0x00000040  /* Debug print SQL as it executes */
   1.608 +#define SQLITE_VdbeListing    0x00000080  /* Debug listings of VDBE programs */
   1.609 +#define SQLITE_WriteSchema    0x00000100  /* OK to update SQLITE_MASTER */
   1.610 +                         /*   0x00000200  Unused */
   1.611 +#define SQLITE_IgnoreChecks   0x00000400  /* Do not enforce check constraints */
   1.612 +#define SQLITE_ReadUncommitted 0x0000800  /* For shared-cache mode */
   1.613 +#define SQLITE_LegacyFileFmt  0x00001000  /* Create new databases in format 1 */
   1.614 +#define SQLITE_FullFSync      0x00002000  /* Use full fsync on the backend */
   1.615 +#define SQLITE_CkptFullFSync  0x00004000  /* Use full fsync for checkpoint */
   1.616 +#define SQLITE_RecoveryMode   0x00008000  /* Ignore schema errors */
   1.617 +#define SQLITE_ReverseOrder   0x00010000  /* Reverse unordered SELECTs */
   1.618 +#define SQLITE_RecTriggers    0x00020000  /* Enable recursive triggers */
   1.619 +#define SQLITE_ForeignKeys    0x00040000  /* Enforce foreign key constraints  */
   1.620 +#define SQLITE_AutoIndex      0x00080000  /* Enable automatic indexes */
   1.621 +#define SQLITE_PreferBuiltin  0x00100000  /* Preference to built-in funcs */
   1.622 +#define SQLITE_LoadExtension  0x00200000  /* Enable load_extension */
   1.623 +#define SQLITE_EnableTrigger  0x00400000  /* True to enable triggers */
   1.624 +
   1.625 +/*
   1.626 +** Bits of the sqlite3.dbOptFlags field that are used by the
   1.627 +** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
   1.628 +** selectively disable various optimizations.
   1.629 +*/
   1.630 +#define SQLITE_QueryFlattener 0x0001   /* Query flattening */
   1.631 +#define SQLITE_ColumnCache    0x0002   /* Column cache */
   1.632 +#define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
   1.633 +#define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
   1.634 +#define SQLITE_IdxRealAsInt   0x0010   /* Store REAL as INT in indices */
   1.635 +#define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
   1.636 +#define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
   1.637 +#define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
   1.638 +#define SQLITE_SubqCoroutine  0x0100   /* Evaluate subqueries as coroutines */
   1.639 +#define SQLITE_AllOpts        0xffff   /* All optimizations */
   1.640 +
   1.641 +/*
   1.642 +** Macros for testing whether or not optimizations are enabled or disabled.
   1.643 +*/
   1.644 +#ifndef SQLITE_OMIT_BUILTIN_TEST
   1.645 +#define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
   1.646 +#define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
   1.647 +#else
   1.648 +#define OptimizationDisabled(db, mask)  0
   1.649 +#define OptimizationEnabled(db, mask)   1
   1.650 +#endif
   1.651  
   1.652  /*
   1.653  ** Possible values for the sqlite.magic field.
   1.654 @@ -9953,6 +10067,7 @@
   1.655  #define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
   1.656  #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
   1.657  #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
   1.658 +#define SQLITE_MAGIC_ZOMBIE   0x64cffc7f  /* Close with last statement close */
   1.659  
   1.660  /*
   1.661  ** Each SQL function is defined by an instance of the following
   1.662 @@ -10091,32 +10206,22 @@
   1.663    char *zDflt;     /* Original text of the default value */
   1.664    char *zType;     /* Data type for this column */
   1.665    char *zColl;     /* Collating sequence.  If NULL, use the default */
   1.666 -  u8 notNull;      /* True if there is a NOT NULL constraint */
   1.667 -  u8 isPrimKey;    /* True if this column is part of the PRIMARY KEY */
   1.668 +  u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
   1.669    char affinity;   /* One of the SQLITE_AFF_... values */
   1.670 -#ifndef SQLITE_OMIT_VIRTUALTABLE
   1.671 -  u8 isHidden;     /* True if this column is 'hidden' */
   1.672 -#endif
   1.673 -};
   1.674 +  u16 colFlags;    /* Boolean properties.  See COLFLAG_ defines below */
   1.675 +};
   1.676 +
   1.677 +/* Allowed values for Column.colFlags:
   1.678 +*/
   1.679 +#define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
   1.680 +#define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
   1.681  
   1.682  /*
   1.683  ** A "Collating Sequence" is defined by an instance of the following
   1.684  ** structure. Conceptually, a collating sequence consists of a name and
   1.685  ** a comparison routine that defines the order of that sequence.
   1.686  **
   1.687 -** There may two separate implementations of the collation function, one
   1.688 -** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
   1.689 -** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
   1.690 -** native byte order. When a collation sequence is invoked, SQLite selects
   1.691 -** the version that will require the least expensive encoding
   1.692 -** translations, if any.
   1.693 -**
   1.694 -** The CollSeq.pUser member variable is an extra parameter that passed in
   1.695 -** as the first argument to the UTF-8 comparison function, xCmp.
   1.696 -** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
   1.697 -** xCmp16.
   1.698 -**
   1.699 -** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
   1.700 +** If CollSeq.xCmp is NULL, it means that the
   1.701  ** collating sequence is undefined.  Indices built on an undefined
   1.702  ** collating sequence may not be read or written.
   1.703  */
   1.704 @@ -10254,28 +10359,28 @@
   1.705  */
   1.706  struct Table {
   1.707    char *zName;         /* Name of the table or view */
   1.708 -  int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
   1.709 -  int nCol;            /* Number of columns in this table */
   1.710    Column *aCol;        /* Information about each column */
   1.711    Index *pIndex;       /* List of SQL indexes on this table. */
   1.712 -  int tnum;            /* Root BTree node for this table (see note above) */
   1.713 -  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
   1.714    Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
   1.715 -  u16 nRef;            /* Number of pointers to this Table */
   1.716 -  u8 tabFlags;         /* Mask of TF_* values */
   1.717 -  u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
   1.718    FKey *pFKey;         /* Linked list of all foreign keys in this table */
   1.719    char *zColAff;       /* String defining the affinity of each column */
   1.720  #ifndef SQLITE_OMIT_CHECK
   1.721    ExprList *pCheck;    /* All CHECK constraints */
   1.722  #endif
   1.723 +  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
   1.724 +  int tnum;            /* Root BTree node for this table (see note above) */
   1.725 +  i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
   1.726 +  i16 nCol;            /* Number of columns in this table */
   1.727 +  u16 nRef;            /* Number of pointers to this Table */
   1.728 +  u8 tabFlags;         /* Mask of TF_* values */
   1.729 +  u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
   1.730  #ifndef SQLITE_OMIT_ALTERTABLE
   1.731    int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
   1.732  #endif
   1.733  #ifndef SQLITE_OMIT_VIRTUALTABLE
   1.734 -  VTable *pVTable;     /* List of VTable objects. */
   1.735    int nModuleArg;      /* Number of arguments to the module */
   1.736    char **azModuleArg;  /* Text of all module args. [0] is module name */
   1.737 +  VTable *pVTable;     /* List of VTable objects. */
   1.738  #endif
   1.739    Trigger *pTrigger;   /* List of triggers stored in pSchema */
   1.740    Schema *pSchema;     /* Schema that contains this table */
   1.741 @@ -10299,7 +10404,7 @@
   1.742  */
   1.743  #ifndef SQLITE_OMIT_VIRTUALTABLE
   1.744  #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
   1.745 -#  define IsHiddenColumn(X) ((X)->isHidden)
   1.746 +#  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
   1.747  #else
   1.748  #  define IsVirtual(X)      0
   1.749  #  define IsHiddenColumn(X) 0
   1.750 @@ -10644,13 +10749,15 @@
   1.751      ExprList *pList;     /* Function arguments or in "<expr> IN (<expr-list)" */
   1.752      Select *pSelect;     /* Used for sub-selects and "<expr> IN (<select>)" */
   1.753    } x;
   1.754 -  CollSeq *pColl;        /* The collation type of the column or 0 */
   1.755  
   1.756    /* If the EP_Reduced flag is set in the Expr.flags mask, then no
   1.757    ** space is allocated for the fields below this point. An attempt to
   1.758    ** access them will result in a segfault or malfunction.
   1.759    *********************************************************************/
   1.760  
   1.761 +#if SQLITE_MAX_EXPR_DEPTH>0
   1.762 +  int nHeight;           /* Height of the tree headed by this node */
   1.763 +#endif
   1.764    int iTable;            /* TK_COLUMN: cursor number of table holding column
   1.765                           ** TK_REGISTER: register number
   1.766                           ** TK_TRIGGER: 1 -> new, 0 -> old */
   1.767 @@ -10659,13 +10766,11 @@
   1.768    i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
   1.769    i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
   1.770    u8 flags2;             /* Second set of flags.  EP2_... */
   1.771 -  u8 op2;                /* If a TK_REGISTER, the original value of Expr.op */
   1.772 -                         /* If TK_COLUMN, the value of p5 for OP_Column */
   1.773 +  u8 op2;                /* TK_REGISTER: original value of Expr.op
   1.774 +                         ** TK_COLUMN: the value of p5 for OP_Column
   1.775 +                         ** TK_AGG_FUNCTION: nesting depth */
   1.776    AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
   1.777    Table *pTab;           /* Table for TK_COLUMN expressions. */
   1.778 -#if SQLITE_MAX_EXPR_DEPTH>0
   1.779 -  int nHeight;           /* Height of the tree headed by this node */
   1.780 -#endif
   1.781  };
   1.782  
   1.783  /*
   1.784 @@ -10679,7 +10784,7 @@
   1.785  #define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
   1.786  #define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
   1.787  #define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
   1.788 -#define EP_ExpCollate 0x0100  /* Collating sequence specified explicitly */
   1.789 +#define EP_Collate    0x0100  /* Tree contains a TK_COLLATE opeartor */
   1.790  #define EP_FixedDest  0x0200  /* Result needed in a specific register */
   1.791  #define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
   1.792  #define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
   1.793 @@ -10823,6 +10928,7 @@
   1.794    i16 nSrc;        /* Number of tables or subqueries in the FROM clause */
   1.795    i16 nAlloc;      /* Number of entries allocated in a[] below */
   1.796    struct SrcList_item {
   1.797 +    Schema *pSchema;  /* Schema to which this item is fixed */
   1.798      char *zDatabase;  /* Name of database holding this table */
   1.799      char *zName;      /* Name of the table */
   1.800      char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
   1.801 @@ -10831,8 +10937,9 @@
   1.802      int addrFillSub;  /* Address of subroutine to manifest a subquery */
   1.803      int regReturn;    /* Register holding return address of addrFillSub */
   1.804      u8 jointype;      /* Type of join between this able and the previous */
   1.805 -    u8 notIndexed;    /* True if there is a NOT INDEXED clause */
   1.806 -    u8 isCorrelated;  /* True if sub-query is correlated */
   1.807 +    unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
   1.808 +    unsigned isCorrelated :1;  /* True if sub-query is correlated */
   1.809 +    unsigned viaCoroutine :1;  /* Implemented as a co-routine */
   1.810  #ifndef SQLITE_OMIT_EXPLAIN
   1.811      u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
   1.812  #endif
   1.813 @@ -10873,7 +10980,8 @@
   1.814  */
   1.815  struct WherePlan {
   1.816    u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
   1.817 -  u32 nEq;                       /* Number of == constraints */
   1.818 +  u16 nEq;                       /* Number of == constraints */
   1.819 +  u16 nOBSat;                    /* Number of ORDER BY terms satisfied */
   1.820    double nRow;                   /* Estimated number of rows (for EQP) */
   1.821    union {
   1.822      Index *pIdx;                   /* Index when WHERE_INDEXED is true */
   1.823 @@ -10885,7 +10993,7 @@
   1.824  /*
   1.825  ** For each nested loop in a WHERE clause implementation, the WhereInfo
   1.826  ** structure contains a single instance of this structure.  This structure
   1.827 -** is intended to be private the the where.c module and should not be
   1.828 +** is intended to be private to the where.c module and should not be
   1.829  ** access or modified by other modules.
   1.830  **
   1.831  ** The pIdxInfo field is used to help pick the best index on a
   1.832 @@ -10915,7 +11023,9 @@
   1.833          int addrInTop;         /* Top of the IN loop */
   1.834        } *aInLoop;           /* Information about each nested IN operator */
   1.835      } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
   1.836 +    Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
   1.837    } u;
   1.838 +  double rOptCost;      /* "Optimal" cost for this level */
   1.839  
   1.840    /* The following field is really not part of the current level.  But
   1.841    ** we need a place to cache virtual table index information for each
   1.842 @@ -10948,24 +11058,28 @@
   1.843  ** into the second half to give some continuity.
   1.844  */
   1.845  struct WhereInfo {
   1.846 -  Parse *pParse;       /* Parsing and code generating context */
   1.847 -  u16 wctrlFlags;      /* Flags originally passed to sqlite3WhereBegin() */
   1.848 -  u8 okOnePass;        /* Ok to use one-pass algorithm for UPDATE or DELETE */
   1.849 -  u8 untestedTerms;    /* Not all WHERE terms resolved by outer loop */
   1.850 -  u8 eDistinct;
   1.851 -  SrcList *pTabList;             /* List of tables in the join */
   1.852 -  int iTop;                      /* The very beginning of the WHERE loop */
   1.853 -  int iContinue;                 /* Jump here to continue with next record */
   1.854 -  int iBreak;                    /* Jump here to break out of the loop */
   1.855 -  int nLevel;                    /* Number of nested loop */
   1.856 -  struct WhereClause *pWC;       /* Decomposition of the WHERE clause */
   1.857 -  double savedNQueryLoop;        /* pParse->nQueryLoop outside the WHERE loop */
   1.858 -  double nRowOut;                /* Estimated number of output rows */
   1.859 -  WhereLevel a[1];               /* Information about each nest loop in WHERE */
   1.860 -};
   1.861 -
   1.862 -#define WHERE_DISTINCT_UNIQUE 1
   1.863 -#define WHERE_DISTINCT_ORDERED 2
   1.864 +  Parse *pParse;            /* Parsing and code generating context */
   1.865 +  SrcList *pTabList;        /* List of tables in the join */
   1.866 +  u16 nOBSat;               /* Number of ORDER BY terms satisfied by indices */
   1.867 +  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
   1.868 +  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
   1.869 +  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
   1.870 +  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
   1.871 +  int iTop;                 /* The very beginning of the WHERE loop */
   1.872 +  int iContinue;            /* Jump here to continue with next record */
   1.873 +  int iBreak;               /* Jump here to break out of the loop */
   1.874 +  int nLevel;               /* Number of nested loop */
   1.875 +  struct WhereClause *pWC;  /* Decomposition of the WHERE clause */
   1.876 +  double savedNQueryLoop;   /* pParse->nQueryLoop outside the WHERE loop */
   1.877 +  double nRowOut;           /* Estimated number of output rows */
   1.878 +  WhereLevel a[1];          /* Information about each nest loop in WHERE */
   1.879 +};
   1.880 +
   1.881 +/* Allowed values for WhereInfo.eDistinct and DistinctCtx.eTnctType */
   1.882 +#define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
   1.883 +#define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
   1.884 +#define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
   1.885 +#define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
   1.886  
   1.887  /*
   1.888  ** A NameContext defines a context in which to resolve table and column
   1.889 @@ -11024,13 +11138,12 @@
   1.890  ** as the OP_OpenEphm instruction is coded because not
   1.891  ** enough information about the compound query is known at that point.
   1.892  ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
   1.893 -** for the result set.  The KeyInfo for addrOpenTran[2] contains collating
   1.894 +** for the result set.  The KeyInfo for addrOpenEphm[2] contains collating
   1.895  ** sequences for the ORDER BY clause.
   1.896  */
   1.897  struct Select {
   1.898    ExprList *pEList;      /* The fields of the result */
   1.899    u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
   1.900 -  char affinity;         /* MakeRecord with this affinity for SRT_Set */
   1.901    u16 selFlags;          /* Various SF_* values */
   1.902    int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
   1.903    int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
   1.904 @@ -11051,14 +11164,15 @@
   1.905  ** Allowed values for Select.selFlags.  The "SF" prefix stands for
   1.906  ** "Select Flag".
   1.907  */
   1.908 -#define SF_Distinct        0x01  /* Output should be DISTINCT */
   1.909 -#define SF_Resolved        0x02  /* Identifiers have been resolved */
   1.910 -#define SF_Aggregate       0x04  /* Contains aggregate functions */
   1.911 -#define SF_UsesEphemeral   0x08  /* Uses the OpenEphemeral opcode */
   1.912 -#define SF_Expanded        0x10  /* sqlite3SelectExpand() called on this */
   1.913 -#define SF_HasTypeInfo     0x20  /* FROM subqueries have Table metadata */
   1.914 -#define SF_UseSorter       0x40  /* Sort using a sorter */
   1.915 -#define SF_Values          0x80  /* Synthesized from VALUES clause */
   1.916 +#define SF_Distinct        0x0001  /* Output should be DISTINCT */
   1.917 +#define SF_Resolved        0x0002  /* Identifiers have been resolved */
   1.918 +#define SF_Aggregate       0x0004  /* Contains aggregate functions */
   1.919 +#define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
   1.920 +#define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
   1.921 +#define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
   1.922 +#define SF_UseSorter       0x0040  /* Sort using a sorter */
   1.923 +#define SF_Values          0x0080  /* Synthesized from VALUES clause */
   1.924 +#define SF_Materialize     0x0100  /* Force materialization of views */
   1.925  
   1.926  
   1.927  /*
   1.928 @@ -11081,16 +11195,15 @@
   1.929  #define SRT_Coroutine   10  /* Generate a single row of result */
   1.930  
   1.931  /*
   1.932 -** A structure used to customize the behavior of sqlite3Select(). See
   1.933 -** comments above sqlite3Select() for details.
   1.934 -*/
   1.935 -typedef struct SelectDest SelectDest;
   1.936 +** An instance of this object describes where to put of the results of
   1.937 +** a SELECT statement.
   1.938 +*/
   1.939  struct SelectDest {
   1.940 -  u8 eDest;         /* How to dispose of the results */
   1.941 -  u8 affinity;      /* Affinity used when eDest==SRT_Set */
   1.942 -  int iParm;        /* A parameter used by the eDest disposal method */
   1.943 -  int iMem;         /* Base register where results are written */
   1.944 -  int nMem;         /* Number of registers allocated */
   1.945 +  u8 eDest;         /* How to dispose of the results.  On of SRT_* above. */
   1.946 +  char affSdst;     /* Affinity used when eDest==SRT_Set */
   1.947 +  int iSDParm;      /* A parameter used by the eDest disposal method */
   1.948 +  int iSdst;        /* Base register where results are written */
   1.949 +  int nSdst;        /* Number of registers allocated */
   1.950  };
   1.951  
   1.952  /*
   1.953 @@ -11286,6 +11399,9 @@
   1.954  #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
   1.955  #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
   1.956  #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
   1.957 +#define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
   1.958 +#define OPFLAG_P2ISREG       0x02    /* P2 to OP_Open** is a register number */
   1.959 +#define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
   1.960  
   1.961  /*
   1.962   * Each trigger present in the database schema is stored as an instance of
   1.963 @@ -11385,6 +11501,7 @@
   1.964  typedef struct DbFixer DbFixer;
   1.965  struct DbFixer {
   1.966    Parse *pParse;      /* The parsing context.  Error messages written here */
   1.967 +  Schema *pSchema;    /* Fix items to this schema */
   1.968    const char *zDb;    /* Make sure all objects are contained in this database */
   1.969    const char *zType;  /* Type of the container - used for error messages */
   1.970    const Token *pName; /* Name of the container - used for error messages */
   1.971 @@ -11427,6 +11544,7 @@
   1.972    int bCoreMutex;                   /* True to enable core mutexing */
   1.973    int bFullMutex;                   /* True to enable full mutexing */
   1.974    int bOpenUri;                     /* True to interpret filenames as URIs */
   1.975 +  int bUseCis;                      /* Use covering indices for full-scans */
   1.976    int mxStrlen;                     /* Maximum string length */
   1.977    int szLookaside;                  /* Default lookaside buffer size */
   1.978    int nLookaside;                   /* Default lookaside buffer count */
   1.979 @@ -11456,6 +11574,10 @@
   1.980    void (*xLog)(void*,int,const char*); /* Function for logging */
   1.981    void *pLogArg;                       /* First argument to xLog() */
   1.982    int bLocaltimeFault;              /* True to fail localtime() calls */
   1.983 +#ifdef SQLITE_ENABLE_SQLLOG
   1.984 +  void(*xSqllog)(void*,sqlite3*,const char*, int);
   1.985 +  void *pSqllogArg;
   1.986 +#endif
   1.987  };
   1.988  
   1.989  /*
   1.990 @@ -11465,10 +11587,12 @@
   1.991    int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
   1.992    int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
   1.993    Parse *pParse;                            /* Parser context.  */
   1.994 +  int walkerDepth;                          /* Number of subqueries */
   1.995    union {                                   /* Extra data for callback */
   1.996      NameContext *pNC;                          /* Naming context */
   1.997      int i;                                     /* Integer value */
   1.998      SrcList *pSrcList;                         /* FROM clause */
   1.999 +    struct SrcCount *pSrcCount;                /* Counting column references */
  1.1000    } u;
  1.1001  };
  1.1002  
  1.1003 @@ -11741,6 +11865,7 @@
  1.1004  # define sqlite3AutoincrementBegin(X)
  1.1005  # define sqlite3AutoincrementEnd(X)
  1.1006  #endif
  1.1007 +SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
  1.1008  SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
  1.1009  SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
  1.1010  SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
  1.1011 @@ -11770,12 +11895,11 @@
  1.1012  #endif
  1.1013  SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
  1.1014  SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
  1.1015 -SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**,ExprList*,u16);
  1.1016 +SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
  1.1017  SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
  1.1018  SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
  1.1019  SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
  1.1020  SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
  1.1021 -SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
  1.1022  SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
  1.1023  SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
  1.1024  SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
  1.1025 @@ -11792,6 +11916,7 @@
  1.1026  SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
  1.1027  SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
  1.1028  SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
  1.1029 +SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
  1.1030  SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
  1.1031  SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
  1.1032  SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
  1.1033 @@ -11802,6 +11927,7 @@
  1.1034  SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*);
  1.1035  SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
  1.1036  SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
  1.1037 +SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
  1.1038  SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
  1.1039  SQLITE_PRIVATE void sqlite3PrngSaveState(void);
  1.1040  SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
  1.1041 @@ -11814,6 +11940,7 @@
  1.1042  SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
  1.1043  SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
  1.1044  SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
  1.1045 +SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
  1.1046  SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
  1.1047  SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
  1.1048  SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
  1.1049 @@ -11913,7 +12040,7 @@
  1.1050  SQLITE_PRIVATE int sqlite3Atoi(const char*);
  1.1051  SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
  1.1052  SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
  1.1053 -SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8*, const u8**);
  1.1054 +SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
  1.1055  
  1.1056  /*
  1.1057  ** Routines to read and write variable-length integers.  These used to
  1.1058 @@ -11966,8 +12093,9 @@
  1.1059  SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
  1.1060  SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
  1.1061  SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
  1.1062 -SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr*, CollSeq*);
  1.1063 -SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*);
  1.1064 +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
  1.1065 +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
  1.1066 +SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
  1.1067  SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
  1.1068  SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
  1.1069  SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
  1.1070 @@ -12020,7 +12148,7 @@
  1.1071  SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
  1.1072  SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
  1.1073  SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
  1.1074 -SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(sqlite3*, u8, CollSeq *, const char*);
  1.1075 +SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
  1.1076  SQLITE_PRIVATE char sqlite3AffinityType(const char*);
  1.1077  SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
  1.1078  SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
  1.1079 @@ -12124,8 +12252,10 @@
  1.1080  SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
  1.1081  SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
  1.1082  SQLITE_PRIVATE const char *sqlite3JournalModename(int);
  1.1083 -SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
  1.1084 -SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
  1.1085 +#ifndef SQLITE_OMIT_WAL
  1.1086 +SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
  1.1087 +SQLITE_PRIVATE   int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
  1.1088 +#endif
  1.1089  
  1.1090  /* Declarations for functions in fkey.c. All of these are replaced by
  1.1091  ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
  1.1092 @@ -12183,8 +12313,10 @@
  1.1093  SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
  1.1094  SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
  1.1095  SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
  1.1096 +SQLITE_PRIVATE   int sqlite3JournalExists(sqlite3_file *p);
  1.1097  #else
  1.1098    #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
  1.1099 +  #define sqlite3JournalExists(p) 1
  1.1100  #endif
  1.1101  
  1.1102  SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
  1.1103 @@ -12413,6 +12545,10 @@
  1.1104  # define  SQLITE_USE_URI 0
  1.1105  #endif
  1.1106  
  1.1107 +#ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
  1.1108 +# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
  1.1109 +#endif
  1.1110 +
  1.1111  /*
  1.1112  ** The following singleton contains the global configuration for
  1.1113  ** the SQLite library.
  1.1114 @@ -12422,6 +12558,7 @@
  1.1115     1,                         /* bCoreMutex */
  1.1116     SQLITE_THREADSAFE==1,      /* bFullMutex */
  1.1117     SQLITE_USE_URI,            /* bOpenUri */
  1.1118 +   SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
  1.1119     0x7ffffffe,                /* mxStrlen */
  1.1120     128,                       /* szLookaside */
  1.1121     500,                       /* nLookaside */
  1.1122 @@ -12450,6 +12587,10 @@
  1.1123     0,                         /* xLog */
  1.1124     0,                         /* pLogArg */
  1.1125     0,                         /* bLocaltimeFault */
  1.1126 +#ifdef SQLITE_ENABLE_SQLLOG
  1.1127 +   0,                         /* xSqllog */
  1.1128 +   0                          /* pSqllogArg */
  1.1129 +#endif
  1.1130  };
  1.1131  
  1.1132  
  1.1133 @@ -12840,6 +12981,9 @@
  1.1134  #ifdef SQLITE_PROXY_DEBUG
  1.1135    "PROXY_DEBUG",
  1.1136  #endif
  1.1137 +#ifdef SQLITE_RTREE_INT_ONLY
  1.1138 +  "RTREE_INT_ONLY",
  1.1139 +#endif
  1.1140  #ifdef SQLITE_SECURE_DELETE
  1.1141    "SECURE_DELETE",
  1.1142  #endif
  1.1143 @@ -12987,6 +13131,7 @@
  1.1144    Bool isIndex;         /* True if an index containing keys only - no data */
  1.1145    Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
  1.1146    Bool isSorter;        /* True if a new-style sorter */
  1.1147 +  Bool multiPseudo;     /* Multi-register pseudo-cursor */
  1.1148    sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
  1.1149    const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
  1.1150    i64 seqCount;         /* Sequence counter */
  1.1151 @@ -13111,7 +13256,9 @@
  1.1152  #define MEM_RowSet    0x0020   /* Value is a RowSet object */
  1.1153  #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
  1.1154  #define MEM_Invalid   0x0080   /* Value is undefined */
  1.1155 -#define MEM_TypeMask  0x00ff   /* Mask of type bits */
  1.1156 +#define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
  1.1157 +#define MEM_TypeMask  0x01ff   /* Mask of type bits */
  1.1158 +
  1.1159  
  1.1160  /* Whenever Mem contains a valid string or blob representation, one of
  1.1161  ** the following flags must be set to determine the memory management
  1.1162 @@ -13197,6 +13344,11 @@
  1.1163    char zBase[100];   /* Initial space */
  1.1164  };
  1.1165  
  1.1166 +/* A bitfield type for use inside of structures.  Always follow with :N where
  1.1167 +** N is the number of bits.
  1.1168 +*/
  1.1169 +typedef unsigned bft;  /* Bit Field Type */
  1.1170 +
  1.1171  /*
  1.1172  ** An instance of the virtual machine.  This structure contains the complete
  1.1173  ** state of the virtual machine.
  1.1174 @@ -13238,15 +13390,16 @@
  1.1175    int pc;                 /* The program counter */
  1.1176    int rc;                 /* Value to return */
  1.1177    u8 errorAction;         /* Recovery action to do in case of an error */
  1.1178 -  u8 explain;             /* True if EXPLAIN present on SQL command */
  1.1179 -  u8 changeCntOn;         /* True to update the change-counter */
  1.1180 -  u8 expired;             /* True if the VM needs to be recompiled */
  1.1181 -  u8 runOnlyOnce;         /* Automatically expire on reset */
  1.1182    u8 minWriteFileFormat;  /* Minimum file format for writable database files */
  1.1183 -  u8 inVtabMethod;        /* See comments above */
  1.1184 -  u8 usesStmtJournal;     /* True if uses a statement journal */
  1.1185 -  u8 readOnly;            /* True for read-only statements */
  1.1186 -  u8 isPrepareV2;         /* True if prepared with prepare_v2() */
  1.1187 +  bft explain:2;          /* True if EXPLAIN present on SQL command */
  1.1188 +  bft inVtabMethod:2;     /* See comments above */
  1.1189 +  bft changeCntOn:1;      /* True to update the change-counter */
  1.1190 +  bft expired:1;          /* True if the VM needs to be recompiled */
  1.1191 +  bft runOnlyOnce:1;      /* Automatically expire on reset */
  1.1192 +  bft usesStmtJournal:1;  /* True if uses a statement journal */
  1.1193 +  bft readOnly:1;         /* True for read-only statements */
  1.1194 +  bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
  1.1195 +  bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
  1.1196    int nChange;            /* Number of db changes made since last reset */
  1.1197    yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
  1.1198    yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
  1.1199 @@ -13355,11 +13508,11 @@
  1.1200  #else
  1.1201  SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
  1.1202  SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
  1.1203 -SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
  1.1204 -SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
  1.1205 -SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
  1.1206 -SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *);
  1.1207 -SQLITE_PRIVATE int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *);
  1.1208 +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
  1.1209 +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
  1.1210 +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
  1.1211 +SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, const VdbeCursor *, Mem *);
  1.1212 +SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int *);
  1.1213  #endif
  1.1214  
  1.1215  #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
  1.1216 @@ -13592,7 +13745,8 @@
  1.1217  
  1.1218        db->pnBytesFreed = &nByte;
  1.1219        for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
  1.1220 -        sqlite3VdbeDeleteObject(db, pVdbe);
  1.1221 +        sqlite3VdbeClearObject(db, pVdbe);
  1.1222 +        sqlite3DbFree(db, pVdbe);
  1.1223        }
  1.1224        db->pnBytesFreed = 0;
  1.1225  
  1.1226 @@ -15506,14 +15660,14 @@
  1.1227    }else{
  1.1228      /* only 1 core, use our own zone to contention over global locks, 
  1.1229      ** e.g. we have our own dedicated locks */
  1.1230 -    bool success;		
  1.1231 +    bool success;
  1.1232      malloc_zone_t* newzone = malloc_create_zone(4096, 0);
  1.1233      malloc_set_zone_name(newzone, "Sqlite_Heap");
  1.1234      do{
  1.1235        success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
  1.1236                                   (void * volatile *)&_sqliteZone_);
  1.1237      }while(!_sqliteZone_);
  1.1238 -    if( !success ){	
  1.1239 +    if( !success ){
  1.1240        /* somebody registered a zone first */
  1.1241        malloc_destroy_zone(newzone);
  1.1242      }
  1.1243 @@ -17719,282 +17873,6 @@
  1.1244  #endif /* !defined(SQLITE_MUTEX_OMIT) */
  1.1245  
  1.1246  /************** End of mutex_noop.c ******************************************/
  1.1247 -/************** Begin file mutex_os2.c ***************************************/
  1.1248 -/*
  1.1249 -** 2007 August 28
  1.1250 -**
  1.1251 -** The author disclaims copyright to this source code.  In place of
  1.1252 -** a legal notice, here is a blessing:
  1.1253 -**
  1.1254 -**    May you do good and not evil.
  1.1255 -**    May you find forgiveness for yourself and forgive others.
  1.1256 -**    May you share freely, never taking more than you give.
  1.1257 -**
  1.1258 -*************************************************************************
  1.1259 -** This file contains the C functions that implement mutexes for OS/2
  1.1260 -*/
  1.1261 -
  1.1262 -/*
  1.1263 -** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
  1.1264 -** See the mutex.h file for details.
  1.1265 -*/
  1.1266 -#ifdef SQLITE_MUTEX_OS2
  1.1267 -
  1.1268 -/********************** OS/2 Mutex Implementation **********************
  1.1269 -**
  1.1270 -** This implementation of mutexes is built using the OS/2 API.
  1.1271 -*/
  1.1272 -
  1.1273 -/*
  1.1274 -** The mutex object
  1.1275 -** Each recursive mutex is an instance of the following structure.
  1.1276 -*/
  1.1277 -struct sqlite3_mutex {
  1.1278 -  HMTX mutex;       /* Mutex controlling the lock */
  1.1279 -  int  id;          /* Mutex type */
  1.1280 -#ifdef SQLITE_DEBUG
  1.1281 - int   trace;       /* True to trace changes */
  1.1282 -#endif
  1.1283 -};
  1.1284 -
  1.1285 -#ifdef SQLITE_DEBUG
  1.1286 -#define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
  1.1287 -#else
  1.1288 -#define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
  1.1289 -#endif
  1.1290 -
  1.1291 -/*
  1.1292 -** Initialize and deinitialize the mutex subsystem.
  1.1293 -*/
  1.1294 -static int os2MutexInit(void){ return SQLITE_OK; }
  1.1295 -static int os2MutexEnd(void){ return SQLITE_OK; }
  1.1296 -
  1.1297 -/*
  1.1298 -** The sqlite3_mutex_alloc() routine allocates a new
  1.1299 -** mutex and returns a pointer to it.  If it returns NULL
  1.1300 -** that means that a mutex could not be allocated. 
  1.1301 -** SQLite will unwind its stack and return an error.  The argument
  1.1302 -** to sqlite3_mutex_alloc() is one of these integer constants:
  1.1303 -**
  1.1304 -** <ul>
  1.1305 -** <li>  SQLITE_MUTEX_FAST
  1.1306 -** <li>  SQLITE_MUTEX_RECURSIVE
  1.1307 -** <li>  SQLITE_MUTEX_STATIC_MASTER
  1.1308 -** <li>  SQLITE_MUTEX_STATIC_MEM
  1.1309 -** <li>  SQLITE_MUTEX_STATIC_MEM2
  1.1310 -** <li>  SQLITE_MUTEX_STATIC_PRNG
  1.1311 -** <li>  SQLITE_MUTEX_STATIC_LRU
  1.1312 -** <li>  SQLITE_MUTEX_STATIC_LRU2
  1.1313 -** </ul>
  1.1314 -**
  1.1315 -** The first two constants cause sqlite3_mutex_alloc() to create
  1.1316 -** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  1.1317 -** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  1.1318 -** The mutex implementation does not need to make a distinction
  1.1319 -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
  1.1320 -** not want to.  But SQLite will only request a recursive mutex in
  1.1321 -** cases where it really needs one.  If a faster non-recursive mutex
  1.1322 -** implementation is available on the host platform, the mutex subsystem
  1.1323 -** might return such a mutex in response to SQLITE_MUTEX_FAST.
  1.1324 -**
  1.1325 -** The other allowed parameters to sqlite3_mutex_alloc() each return
  1.1326 -** a pointer to a static preexisting mutex.  Six static mutexes are
  1.1327 -** used by the current version of SQLite.  Future versions of SQLite
  1.1328 -** may add additional static mutexes.  Static mutexes are for internal
  1.1329 -** use by SQLite only.  Applications that use SQLite mutexes should
  1.1330 -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
  1.1331 -** SQLITE_MUTEX_RECURSIVE.
  1.1332 -**
  1.1333 -** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  1.1334 -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  1.1335 -** returns a different mutex on every call.  But for the static
  1.1336 -** mutex types, the same mutex is returned on every call that has
  1.1337 -** the same type number.
  1.1338 -*/
  1.1339 -static sqlite3_mutex *os2MutexAlloc(int iType){
  1.1340 -  sqlite3_mutex *p = NULL;
  1.1341 -  switch( iType ){
  1.1342 -    case SQLITE_MUTEX_FAST:
  1.1343 -    case SQLITE_MUTEX_RECURSIVE: {
  1.1344 -      p = sqlite3MallocZero( sizeof(*p) );
  1.1345 -      if( p ){
  1.1346 -        p->id = iType;
  1.1347 -        if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
  1.1348 -          sqlite3_free( p );
  1.1349 -          p = NULL;
  1.1350 -        }
  1.1351 -      }
  1.1352 -      break;
  1.1353 -    }
  1.1354 -    default: {
  1.1355 -      static volatile int isInit = 0;
  1.1356 -      static sqlite3_mutex staticMutexes[6] = {
  1.1357 -        SQLITE3_MUTEX_INITIALIZER,
  1.1358 -        SQLITE3_MUTEX_INITIALIZER,
  1.1359 -        SQLITE3_MUTEX_INITIALIZER,
  1.1360 -        SQLITE3_MUTEX_INITIALIZER,
  1.1361 -        SQLITE3_MUTEX_INITIALIZER,
  1.1362 -        SQLITE3_MUTEX_INITIALIZER,
  1.1363 -      };
  1.1364 -      if ( !isInit ){
  1.1365 -        APIRET rc;
  1.1366 -        PTIB ptib;
  1.1367 -        PPIB ppib;
  1.1368 -        HMTX mutex;
  1.1369 -        char name[32];
  1.1370 -        DosGetInfoBlocks( &ptib, &ppib );
  1.1371 -        sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
  1.1372 -                          ppib->pib_ulpid );
  1.1373 -        while( !isInit ){
  1.1374 -          mutex = 0;
  1.1375 -          rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
  1.1376 -          if( rc == NO_ERROR ){
  1.1377 -            unsigned int i;
  1.1378 -            if( !isInit ){
  1.1379 -              for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
  1.1380 -                DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
  1.1381 -              }
  1.1382 -              isInit = 1;
  1.1383 -            }
  1.1384 -            DosCloseMutexSem( mutex );
  1.1385 -          }else if( rc == ERROR_DUPLICATE_NAME ){
  1.1386 -            DosSleep( 1 );
  1.1387 -          }else{
  1.1388 -            return p;
  1.1389 -          }
  1.1390 -        }
  1.1391 -      }
  1.1392 -      assert( iType-2 >= 0 );
  1.1393 -      assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
  1.1394 -      p = &staticMutexes[iType-2];
  1.1395 -      p->id = iType;
  1.1396 -      break;
  1.1397 -    }
  1.1398 -  }
  1.1399 -  return p;
  1.1400 -}
  1.1401 -
  1.1402 -
  1.1403 -/*
  1.1404 -** This routine deallocates a previously allocated mutex.
  1.1405 -** SQLite is careful to deallocate every mutex that it allocates.
  1.1406 -*/
  1.1407 -static void os2MutexFree(sqlite3_mutex *p){
  1.1408 -#ifdef SQLITE_DEBUG
  1.1409 -  TID tid;
  1.1410 -  PID pid;
  1.1411 -  ULONG ulCount;
  1.1412 -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
  1.1413 -  assert( ulCount==0 );
  1.1414 -  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
  1.1415 -#endif
  1.1416 -  DosCloseMutexSem( p->mutex );
  1.1417 -  sqlite3_free( p );
  1.1418 -}
  1.1419 -
  1.1420 -#ifdef SQLITE_DEBUG
  1.1421 -/*
  1.1422 -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
  1.1423 -** intended for use inside assert() statements.
  1.1424 -*/
  1.1425 -static int os2MutexHeld(sqlite3_mutex *p){
  1.1426 -  TID tid;
  1.1427 -  PID pid;
  1.1428 -  ULONG ulCount;
  1.1429 -  PTIB ptib;
  1.1430 -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
  1.1431 -  if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
  1.1432 -    return 0;
  1.1433 -  DosGetInfoBlocks(&ptib, NULL);
  1.1434 -  return tid==ptib->tib_ptib2->tib2_ultid;
  1.1435 -}
  1.1436 -static int os2MutexNotheld(sqlite3_mutex *p){
  1.1437 -  TID tid;
  1.1438 -  PID pid;
  1.1439 -  ULONG ulCount;
  1.1440 -  PTIB ptib;
  1.1441 -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
  1.1442 -  if( ulCount==0 )
  1.1443 -    return 1;
  1.1444 -  DosGetInfoBlocks(&ptib, NULL);
  1.1445 -  return tid!=ptib->tib_ptib2->tib2_ultid;
  1.1446 -}
  1.1447 -static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
  1.1448 -  TID   tid;
  1.1449 -  PID   pid;
  1.1450 -  ULONG ulCount;
  1.1451 -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
  1.1452 -  printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
  1.1453 -}
  1.1454 -#endif
  1.1455 -
  1.1456 -/*
  1.1457 -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
  1.1458 -** to enter a mutex.  If another thread is already within the mutex,
  1.1459 -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
  1.1460 -** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
  1.1461 -** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
  1.1462 -** be entered multiple times by the same thread.  In such cases the,
  1.1463 -** mutex must be exited an equal number of times before another thread
  1.1464 -** can enter.  If the same thread tries to enter any other kind of mutex
  1.1465 -** more than once, the behavior is undefined.
  1.1466 -*/
  1.1467 -static void os2MutexEnter(sqlite3_mutex *p){
  1.1468 -  assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
  1.1469 -  DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
  1.1470 -#ifdef SQLITE_DEBUG
  1.1471 -  if( p->trace ) os2MutexTrace(p, "enter");
  1.1472 -#endif
  1.1473 -}
  1.1474 -static int os2MutexTry(sqlite3_mutex *p){
  1.1475 -  int rc = SQLITE_BUSY;
  1.1476 -  assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
  1.1477 -  if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
  1.1478 -    rc = SQLITE_OK;
  1.1479 -#ifdef SQLITE_DEBUG
  1.1480 -    if( p->trace ) os2MutexTrace(p, "try");
  1.1481 -#endif
  1.1482 -  }
  1.1483 -  return rc;
  1.1484 -}
  1.1485 -
  1.1486 -/*
  1.1487 -** The sqlite3_mutex_leave() routine exits a mutex that was
  1.1488 -** previously entered by the same thread.  The behavior
  1.1489 -** is undefined if the mutex is not currently entered or
  1.1490 -** is not currently allocated.  SQLite will never do either.
  1.1491 -*/
  1.1492 -static void os2MutexLeave(sqlite3_mutex *p){
  1.1493 -  assert( os2MutexHeld(p) );
  1.1494 -  DosReleaseMutexSem(p->mutex);
  1.1495 -#ifdef SQLITE_DEBUG
  1.1496 -  if( p->trace ) os2MutexTrace(p, "leave");
  1.1497 -#endif
  1.1498 -}
  1.1499 -
  1.1500 -SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
  1.1501 -  static const sqlite3_mutex_methods sMutex = {
  1.1502 -    os2MutexInit,
  1.1503 -    os2MutexEnd,
  1.1504 -    os2MutexAlloc,
  1.1505 -    os2MutexFree,
  1.1506 -    os2MutexEnter,
  1.1507 -    os2MutexTry,
  1.1508 -    os2MutexLeave,
  1.1509 -#ifdef SQLITE_DEBUG
  1.1510 -    os2MutexHeld,
  1.1511 -    os2MutexNotheld
  1.1512 -#else
  1.1513 -    0,
  1.1514 -    0
  1.1515 -#endif
  1.1516 -  };
  1.1517 -
  1.1518 -  return &sMutex;
  1.1519 -}
  1.1520 -#endif /* SQLITE_MUTEX_OS2 */
  1.1521 -
  1.1522 -/************** End of mutex_os2.c *******************************************/
  1.1523  /************** Begin file mutex_unix.c **************************************/
  1.1524  /*
  1.1525  ** 2007 August 28
  1.1526 @@ -18459,7 +18337,7 @@
  1.1527  */
  1.1528  static long winMutex_lock = 0;
  1.1529  
  1.1530 -SQLITE_API extern void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
  1.1531 +SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
  1.1532  
  1.1533  static int winMutexInit(void){ 
  1.1534    /* The first to increment to 1 does actual initialization */
  1.1535 @@ -19602,7 +19480,8 @@
  1.1536  static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
  1.1537    int digit;
  1.1538    LONGDOUBLE_TYPE d;
  1.1539 -  if( (*cnt)++ >= 16 ) return '0';
  1.1540 +  if( (*cnt)<=0 ) return '0';
  1.1541 +  (*cnt)--;
  1.1542    digit = (int)*val;
  1.1543    d = digit;
  1.1544    digit += '0';
  1.1545 @@ -19906,9 +19785,12 @@
  1.1546            break;
  1.1547          }
  1.1548          if( realvalue>0.0 ){
  1.1549 -          while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
  1.1550 -          while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
  1.1551 -          while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
  1.1552 +          LONGDOUBLE_TYPE scale = 1.0;
  1.1553 +          while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
  1.1554 +          while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
  1.1555 +          while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
  1.1556 +          while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
  1.1557 +          realvalue /= scale;
  1.1558            while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
  1.1559            while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
  1.1560            if( exp>350 ){
  1.1561 @@ -19941,7 +19823,7 @@
  1.1562              xtype = etFLOAT;
  1.1563            }
  1.1564          }else{
  1.1565 -          flag_rtz = 0;
  1.1566 +          flag_rtz = flag_altform2;
  1.1567          }
  1.1568          if( xtype==etEXP ){
  1.1569            e2 = 0;
  1.1570 @@ -19956,7 +19838,7 @@
  1.1571            }
  1.1572          }
  1.1573          zOut = bufpt;
  1.1574 -        nsd = 0;
  1.1575 +        nsd = 16 + flag_altform2*10;
  1.1576          flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
  1.1577          /* The sign in front of the number */
  1.1578          if( prefix ){
  1.1579 @@ -20760,25 +20642,23 @@
  1.1580          || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
  1.1581    }
  1.1582  SQLITE_PRIVATE u32 sqlite3Utf8Read(
  1.1583 -  const unsigned char *zIn,       /* First byte of UTF-8 character */
  1.1584 -  const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
  1.1585 +  const unsigned char **pz    /* Pointer to string from which to read char */
  1.1586  ){
  1.1587    unsigned int c;
  1.1588  
  1.1589    /* Same as READ_UTF8() above but without the zTerm parameter.
  1.1590    ** For this routine, we assume the UTF8 string is always zero-terminated.
  1.1591    */
  1.1592 -  c = *(zIn++);
  1.1593 +  c = *((*pz)++);
  1.1594    if( c>=0xc0 ){
  1.1595      c = sqlite3Utf8Trans1[c-0xc0];
  1.1596 -    while( (*zIn & 0xc0)==0x80 ){
  1.1597 -      c = (c<<6) + (0x3f & *(zIn++));
  1.1598 +    while( (*(*pz) & 0xc0)==0x80 ){
  1.1599 +      c = (c<<6) + (0x3f & *((*pz)++));
  1.1600      }
  1.1601      if( c<0x80
  1.1602          || (c&0xFFFFF800)==0xD800
  1.1603          || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
  1.1604    }
  1.1605 -  *pzNext = zIn;
  1.1606    return c;
  1.1607  }
  1.1608  
  1.1609 @@ -20879,7 +20759,6 @@
  1.1610      if( desiredEnc==SQLITE_UTF16LE ){
  1.1611        /* UTF-8 -> UTF-16 Little-endian */
  1.1612        while( zIn<zTerm ){
  1.1613 -        /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
  1.1614          READ_UTF8(zIn, zTerm, c);
  1.1615          WRITE_UTF16LE(z, c);
  1.1616        }
  1.1617 @@ -20887,7 +20766,6 @@
  1.1618        assert( desiredEnc==SQLITE_UTF16BE );
  1.1619        /* UTF-8 -> UTF-16 Big-endian */
  1.1620        while( zIn<zTerm ){
  1.1621 -        /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
  1.1622          READ_UTF8(zIn, zTerm, c);
  1.1623          WRITE_UTF16BE(z, c);
  1.1624        }
  1.1625 @@ -21015,7 +20893,7 @@
  1.1626    u32 c;
  1.1627  
  1.1628    while( zIn[0] && zOut<=zIn ){
  1.1629 -    c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
  1.1630 +    c = sqlite3Utf8Read((const u8**)&zIn);
  1.1631      if( c!=0xfffd ){
  1.1632        WRITE_UTF8(zOut, c);
  1.1633      }
  1.1634 @@ -21120,7 +20998,7 @@
  1.1635      assert( n>0 && n<=4 );
  1.1636      z[0] = 0;
  1.1637      z = zBuf;
  1.1638 -    c = sqlite3Utf8Read(z, (const u8**)&z);
  1.1639 +    c = sqlite3Utf8Read((const u8**)&z);
  1.1640      t = i;
  1.1641      if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
  1.1642      if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
  1.1643 @@ -21529,7 +21407,7 @@
  1.1644      /* if exponent, scale significand as appropriate
  1.1645      ** and store in result. */
  1.1646      if( e ){
  1.1647 -      double scale = 1.0;
  1.1648 +      LONGDOUBLE_TYPE scale = 1.0;
  1.1649        /* attempt to handle extremely small/large numbers better */
  1.1650        if( e>307 && e<342 ){
  1.1651          while( e%308 ) { scale *= 1.0e+1; e -= 1; }
  1.1652 @@ -22458,7 +22336,11 @@
  1.1653  
  1.1654    /* The inability to allocates space for a larger hash table is
  1.1655    ** a performance hit but it is not a fatal error.  So mark the
  1.1656 -  ** allocation as a benign.
  1.1657 +  ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of 
  1.1658 +  ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
  1.1659 +  ** only zeroes the requested number of bytes whereas this module will
  1.1660 +  ** use the actual amount of space allocated for the hash table (which
  1.1661 +  ** may be larger than the requested amount).
  1.1662    */
  1.1663    sqlite3BeginBenignMalloc();
  1.1664    new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
  1.1665 @@ -22534,7 +22416,7 @@
  1.1666    }
  1.1667    sqlite3_free( elem );
  1.1668    pH->count--;
  1.1669 -  if( pH->count<=0 ){
  1.1670 +  if( pH->count==0 ){
  1.1671      assert( pH->first==0 );
  1.1672      assert( pH->count==0 );
  1.1673      sqlite3HashClear(pH);
  1.1674 @@ -22784,2140 +22666,6 @@
  1.1675  #endif
  1.1676  
  1.1677  /************** End of opcodes.c *********************************************/
  1.1678 -/************** Begin file os_os2.c ******************************************/
  1.1679 -/*
  1.1680 -** 2006 Feb 14
  1.1681 -**
  1.1682 -** The author disclaims copyright to this source code.  In place of
  1.1683 -** a legal notice, here is a blessing:
  1.1684 -**
  1.1685 -**    May you do good and not evil.
  1.1686 -**    May you find forgiveness for yourself and forgive others.
  1.1687 -**    May you share freely, never taking more than you give.
  1.1688 -**
  1.1689 -******************************************************************************
  1.1690 -**
  1.1691 -** This file contains code that is specific to OS/2.
  1.1692 -*/
  1.1693 -
  1.1694 -
  1.1695 -#if SQLITE_OS_OS2
  1.1696 -
  1.1697 -/*
  1.1698 -** A Note About Memory Allocation:
  1.1699 -**
  1.1700 -** This driver uses malloc()/free() directly rather than going through
  1.1701 -** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
  1.1702 -** are designed for use on embedded systems where memory is scarce and
  1.1703 -** malloc failures happen frequently.  OS/2 does not typically run on
  1.1704 -** embedded systems, and when it does the developers normally have bigger
  1.1705 -** problems to worry about than running out of memory.  So there is not
  1.1706 -** a compelling need to use the wrappers.
  1.1707 -**
  1.1708 -** But there is a good reason to not use the wrappers.  If we use the
  1.1709 -** wrappers then we will get simulated malloc() failures within this
  1.1710 -** driver.  And that causes all kinds of problems for our tests.  We
  1.1711 -** could enhance SQLite to deal with simulated malloc failures within
  1.1712 -** the OS driver, but the code to deal with those failure would not
  1.1713 -** be exercised on Linux (which does not need to malloc() in the driver)
  1.1714 -** and so we would have difficulty writing coverage tests for that
  1.1715 -** code.  Better to leave the code out, we think.
  1.1716 -**
  1.1717 -** The point of this discussion is as follows:  When creating a new
  1.1718 -** OS layer for an embedded system, if you use this file as an example,
  1.1719 -** avoid the use of malloc()/free().  Those routines work ok on OS/2
  1.1720 -** desktops but not so well in embedded systems.
  1.1721 -*/
  1.1722 -
  1.1723 -/*
  1.1724 -** Macros used to determine whether or not to use threads.
  1.1725 -*/
  1.1726 -#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
  1.1727 -# define SQLITE_OS2_THREADS 1
  1.1728 -#endif
  1.1729 -
  1.1730 -/*
  1.1731 -** Include code that is common to all os_*.c files
  1.1732 -*/
  1.1733 -/************** Include os_common.h in the middle of os_os2.c ****************/
  1.1734 -/************** Begin file os_common.h ***************************************/
  1.1735 -/*
  1.1736 -** 2004 May 22
  1.1737 -**
  1.1738 -** The author disclaims copyright to this source code.  In place of
  1.1739 -** a legal notice, here is a blessing:
  1.1740 -**
  1.1741 -**    May you do good and not evil.
  1.1742 -**    May you find forgiveness for yourself and forgive others.
  1.1743 -**    May you share freely, never taking more than you give.
  1.1744 -**
  1.1745 -******************************************************************************
  1.1746 -**
  1.1747 -** This file contains macros and a little bit of code that is common to
  1.1748 -** all of the platform-specific files (os_*.c) and is #included into those
  1.1749 -** files.
  1.1750 -**
  1.1751 -** This file should be #included by the os_*.c files only.  It is not a
  1.1752 -** general purpose header file.
  1.1753 -*/
  1.1754 -#ifndef _OS_COMMON_H_
  1.1755 -#define _OS_COMMON_H_
  1.1756 -
  1.1757 -/*
  1.1758 -** At least two bugs have slipped in because we changed the MEMORY_DEBUG
  1.1759 -** macro to SQLITE_DEBUG and some older makefiles have not yet made the
  1.1760 -** switch.  The following code should catch this problem at compile-time.
  1.1761 -*/
  1.1762 -#ifdef MEMORY_DEBUG
  1.1763 -# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
  1.1764 -#endif
  1.1765 -
  1.1766 -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
  1.1767 -# ifndef SQLITE_DEBUG_OS_TRACE
  1.1768 -#   define SQLITE_DEBUG_OS_TRACE 0
  1.1769 -# endif
  1.1770 -  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
  1.1771 -# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
  1.1772 -#else
  1.1773 -# define OSTRACE(X)
  1.1774 -#endif
  1.1775 -
  1.1776 -/*
  1.1777 -** Macros for performance tracing.  Normally turned off.  Only works
  1.1778 -** on i486 hardware.
  1.1779 -*/
  1.1780 -#ifdef SQLITE_PERFORMANCE_TRACE
  1.1781 -
  1.1782 -/* 
  1.1783 -** hwtime.h contains inline assembler code for implementing 
  1.1784 -** high-performance timing routines.
  1.1785 -*/
  1.1786 -/************** Include hwtime.h in the middle of os_common.h ****************/
  1.1787 -/************** Begin file hwtime.h ******************************************/
  1.1788 -/*
  1.1789 -** 2008 May 27
  1.1790 -**
  1.1791 -** The author disclaims copyright to this source code.  In place of
  1.1792 -** a legal notice, here is a blessing:
  1.1793 -**
  1.1794 -**    May you do good and not evil.
  1.1795 -**    May you find forgiveness for yourself and forgive others.
  1.1796 -**    May you share freely, never taking more than you give.
  1.1797 -**
  1.1798 -******************************************************************************
  1.1799 -**
  1.1800 -** This file contains inline asm code for retrieving "high-performance"
  1.1801 -** counters for x86 class CPUs.
  1.1802 -*/
  1.1803 -#ifndef _HWTIME_H_
  1.1804 -#define _HWTIME_H_
  1.1805 -
  1.1806 -/*
  1.1807 -** The following routine only works on pentium-class (or newer) processors.
  1.1808 -** It uses the RDTSC opcode to read the cycle count value out of the
  1.1809 -** processor and returns that value.  This can be used for high-res
  1.1810 -** profiling.
  1.1811 -*/
  1.1812 -#if (defined(__GNUC__) || defined(_MSC_VER)) && \
  1.1813 -      (defined(i386) || defined(__i386__) || defined(_M_IX86))
  1.1814 -
  1.1815 -  #if defined(__GNUC__)
  1.1816 -
  1.1817 -  __inline__ sqlite_uint64 sqlite3Hwtime(void){
  1.1818 -     unsigned int lo, hi;
  1.1819 -     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
  1.1820 -     return (sqlite_uint64)hi << 32 | lo;
  1.1821 -  }
  1.1822 -
  1.1823 -  #elif defined(_MSC_VER)
  1.1824 -
  1.1825 -  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
  1.1826 -     __asm {
  1.1827 -        rdtsc
  1.1828 -        ret       ; return value at EDX:EAX
  1.1829 -     }
  1.1830 -  }
  1.1831 -
  1.1832 -  #endif
  1.1833 -
  1.1834 -#elif (defined(__GNUC__) && defined(__x86_64__))
  1.1835 -
  1.1836 -  __inline__ sqlite_uint64 sqlite3Hwtime(void){
  1.1837 -      unsigned long val;
  1.1838 -      __asm__ __volatile__ ("rdtsc" : "=A" (val));
  1.1839 -      return val;
  1.1840 -  }
  1.1841 - 
  1.1842 -#elif (defined(__GNUC__) && defined(__ppc__))
  1.1843 -
  1.1844 -  __inline__ sqlite_uint64 sqlite3Hwtime(void){
  1.1845 -      unsigned long long retval;
  1.1846 -      unsigned long junk;
  1.1847 -      __asm__ __volatile__ ("\n\
  1.1848 -          1:      mftbu   %1\n\
  1.1849 -                  mftb    %L0\n\
  1.1850 -                  mftbu   %0\n\
  1.1851 -                  cmpw    %0,%1\n\
  1.1852 -                  bne     1b"
  1.1853 -                  : "=r" (retval), "=r" (junk));
  1.1854 -      return retval;
  1.1855 -  }
  1.1856 -
  1.1857 -#else
  1.1858 -
  1.1859 -  #error Need implementation of sqlite3Hwtime() for your platform.
  1.1860 -
  1.1861 -  /*
  1.1862 -  ** To compile without implementing sqlite3Hwtime() for your platform,
  1.1863 -  ** you can remove the above #error and use the following
  1.1864 -  ** stub function.  You will lose timing support for many
  1.1865 -  ** of the debugging and testing utilities, but it should at
  1.1866 -  ** least compile and run.
  1.1867 -  */
  1.1868 -SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
  1.1869 -
  1.1870 -#endif
  1.1871 -
  1.1872 -#endif /* !defined(_HWTIME_H_) */
  1.1873 -
  1.1874 -/************** End of hwtime.h **********************************************/
  1.1875 -/************** Continuing where we left off in os_common.h ******************/
  1.1876 -
  1.1877 -static sqlite_uint64 g_start;
  1.1878 -static sqlite_uint64 g_elapsed;
  1.1879 -#define TIMER_START       g_start=sqlite3Hwtime()
  1.1880 -#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
  1.1881 -#define TIMER_ELAPSED     g_elapsed
  1.1882 -#else
  1.1883 -#define TIMER_START
  1.1884 -#define TIMER_END
  1.1885 -#define TIMER_ELAPSED     ((sqlite_uint64)0)
  1.1886 -#endif
  1.1887 -
  1.1888 -/*
  1.1889 -** If we compile with the SQLITE_TEST macro set, then the following block
  1.1890 -** of code will give us the ability to simulate a disk I/O error.  This
  1.1891 -** is used for testing the I/O recovery logic.
  1.1892 -*/
  1.1893 -#ifdef SQLITE_TEST
  1.1894 -SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
  1.1895 -SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
  1.1896 -SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
  1.1897 -SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
  1.1898 -SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
  1.1899 -SQLITE_API int sqlite3_diskfull_pending = 0;
  1.1900 -SQLITE_API int sqlite3_diskfull = 0;
  1.1901 -#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
  1.1902 -#define SimulateIOError(CODE)  \
  1.1903 -  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
  1.1904 -       || sqlite3_io_error_pending-- == 1 )  \
  1.1905 -              { local_ioerr(); CODE; }
  1.1906 -static void local_ioerr(){
  1.1907 -  IOTRACE(("IOERR\n"));
  1.1908 -  sqlite3_io_error_hit++;
  1.1909 -  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
  1.1910 -}
  1.1911 -#define SimulateDiskfullError(CODE) \
  1.1912 -   if( sqlite3_diskfull_pending ){ \
  1.1913 -     if( sqlite3_diskfull_pending == 1 ){ \
  1.1914 -       local_ioerr(); \
  1.1915 -       sqlite3_diskfull = 1; \
  1.1916 -       sqlite3_io_error_hit = 1; \
  1.1917 -       CODE; \
  1.1918 -     }else{ \
  1.1919 -       sqlite3_diskfull_pending--; \
  1.1920 -     } \
  1.1921 -   }
  1.1922 -#else
  1.1923 -#define SimulateIOErrorBenign(X)
  1.1924 -#define SimulateIOError(A)
  1.1925 -#define SimulateDiskfullError(A)
  1.1926 -#endif
  1.1927 -
  1.1928 -/*
  1.1929 -** When testing, keep a count of the number of open files.
  1.1930 -*/
  1.1931 -#ifdef SQLITE_TEST
  1.1932 -SQLITE_API int sqlite3_open_file_count = 0;
  1.1933 -#define OpenCounter(X)  sqlite3_open_file_count+=(X)
  1.1934 -#else
  1.1935 -#define OpenCounter(X)
  1.1936 -#endif
  1.1937 -
  1.1938 -#endif /* !defined(_OS_COMMON_H_) */
  1.1939 -
  1.1940 -/************** End of os_common.h *******************************************/
  1.1941 -/************** Continuing where we left off in os_os2.c *********************/
  1.1942 -
  1.1943 -/* Forward references */
  1.1944 -typedef struct os2File os2File;         /* The file structure */
  1.1945 -typedef struct os2ShmNode os2ShmNode;   /* A shared descritive memory node */
  1.1946 -typedef struct os2ShmLink os2ShmLink;   /* A connection to shared-memory */
  1.1947 -
  1.1948 -/*
  1.1949 -** The os2File structure is subclass of sqlite3_file specific for the OS/2
  1.1950 -** protability layer.
  1.1951 -*/
  1.1952 -struct os2File {
  1.1953 -  const sqlite3_io_methods *pMethod;  /* Always the first entry */
  1.1954 -  HFILE h;                  /* Handle for accessing the file */
  1.1955 -  int flags;                /* Flags provided to os2Open() */
  1.1956 -  int locktype;             /* Type of lock currently held on this file */
  1.1957 -  int szChunk;              /* Chunk size configured by FCNTL_CHUNK_SIZE */
  1.1958 -  char *zFullPathCp;        /* Full path name of this file */
  1.1959 -  os2ShmLink *pShmLink;     /* Instance of shared memory on this file */
  1.1960 -};
  1.1961 -
  1.1962 -#define LOCK_TIMEOUT 10L /* the default locking timeout */
  1.1963 -
  1.1964 -/*
  1.1965 -** Missing from some versions of the OS/2 toolkit -
  1.1966 -** used to allocate from high memory if possible
  1.1967 -*/
  1.1968 -#ifndef OBJ_ANY
  1.1969 -# define OBJ_ANY 0x00000400
  1.1970 -#endif
  1.1971 -
  1.1972 -/*****************************************************************************
  1.1973 -** The next group of routines implement the I/O methods specified
  1.1974 -** by the sqlite3_io_methods object.
  1.1975 -******************************************************************************/
  1.1976 -
  1.1977 -/*
  1.1978 -** Close a file.
  1.1979 -*/
  1.1980 -static int os2Close( sqlite3_file *id ){
  1.1981 -  APIRET rc;
  1.1982 -  os2File *pFile = (os2File*)id;
  1.1983 -
  1.1984 -  assert( id!=0 );
  1.1985 -  OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
  1.1986 -
  1.1987 -  rc = DosClose( pFile->h );
  1.1988 -
  1.1989 -  if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
  1.1990 -    DosForceDelete( (PSZ)pFile->zFullPathCp );
  1.1991 -
  1.1992 -  free( pFile->zFullPathCp );
  1.1993 -  pFile->zFullPathCp = NULL;
  1.1994 -  pFile->locktype = NO_LOCK;
  1.1995 -  pFile->h = (HFILE)-1;
  1.1996 -  pFile->flags = 0;
  1.1997 -
  1.1998 -  OpenCounter( -1 );
  1.1999 -  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
  1.2000 -}
  1.2001 -
  1.2002 -/*
  1.2003 -** Read data from a file into a buffer.  Return SQLITE_OK if all
  1.2004 -** bytes were read successfully and SQLITE_IOERR if anything goes
  1.2005 -** wrong.
  1.2006 -*/
  1.2007 -static int os2Read(
  1.2008 -  sqlite3_file *id,               /* File to read from */
  1.2009 -  void *pBuf,                     /* Write content into this buffer */
  1.2010 -  int amt,                        /* Number of bytes to read */
  1.2011 -  sqlite3_int64 offset            /* Begin reading at this offset */
  1.2012 -){
  1.2013 -  ULONG fileLocation = 0L;
  1.2014 -  ULONG got;
  1.2015 -  os2File *pFile = (os2File*)id;
  1.2016 -  assert( id!=0 );
  1.2017 -  SimulateIOError( return SQLITE_IOERR_READ );
  1.2018 -  OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
  1.2019 -  if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
  1.2020 -    return SQLITE_IOERR;
  1.2021 -  }
  1.2022 -  if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
  1.2023 -    return SQLITE_IOERR_READ;
  1.2024 -  }
  1.2025 -  if( got == (ULONG)amt )
  1.2026 -    return SQLITE_OK;
  1.2027 -  else {
  1.2028 -    /* Unread portions of the input buffer must be zero-filled */
  1.2029 -    memset(&((char*)pBuf)[got], 0, amt-got);
  1.2030 -    return SQLITE_IOERR_SHORT_READ;
  1.2031 -  }
  1.2032 -}
  1.2033 -
  1.2034 -/*
  1.2035 -** Write data from a buffer into a file.  Return SQLITE_OK on success
  1.2036 -** or some other error code on failure.
  1.2037 -*/
  1.2038 -static int os2Write(
  1.2039 -  sqlite3_file *id,               /* File to write into */
  1.2040 -  const void *pBuf,               /* The bytes to be written */
  1.2041 -  int amt,                        /* Number of bytes to write */
  1.2042 -  sqlite3_int64 offset            /* Offset into the file to begin writing at */
  1.2043 -){
  1.2044 -  ULONG fileLocation = 0L;
  1.2045 -  APIRET rc = NO_ERROR;
  1.2046 -  ULONG wrote;
  1.2047 -  os2File *pFile = (os2File*)id;
  1.2048 -  assert( id!=0 );
  1.2049 -  SimulateIOError( return SQLITE_IOERR_WRITE );
  1.2050 -  SimulateDiskfullError( return SQLITE_FULL );
  1.2051 -  OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
  1.2052 -  if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
  1.2053 -    return SQLITE_IOERR;
  1.2054 -  }
  1.2055 -  assert( amt>0 );
  1.2056 -  while( amt > 0 &&
  1.2057 -         ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
  1.2058 -         wrote > 0
  1.2059 -  ){
  1.2060 -    amt -= wrote;
  1.2061 -    pBuf = &((char*)pBuf)[wrote];
  1.2062 -  }
  1.2063 -
  1.2064 -  return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
  1.2065 -}
  1.2066 -
  1.2067 -/*
  1.2068 -** Truncate an open file to a specified size
  1.2069 -*/
  1.2070 -static int os2Truncate( sqlite3_file *id, i64 nByte ){
  1.2071 -  APIRET rc;
  1.2072 -  os2File *pFile = (os2File*)id;
  1.2073 -  assert( id!=0 );
  1.2074 -  OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
  1.2075 -  SimulateIOError( return SQLITE_IOERR_TRUNCATE );
  1.2076 -
  1.2077 -  /* If the user has configured a chunk-size for this file, truncate the
  1.2078 -  ** file so that it consists of an integer number of chunks (i.e. the
  1.2079 -  ** actual file size after the operation may be larger than the requested
  1.2080 -  ** size).
  1.2081 -  */
  1.2082 -  if( pFile->szChunk ){
  1.2083 -    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
  1.2084 -  }
  1.2085 -  
  1.2086 -  rc = DosSetFileSize( pFile->h, nByte );
  1.2087 -  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
  1.2088 -}
  1.2089 -
  1.2090 -#ifdef SQLITE_TEST
  1.2091 -/*
  1.2092 -** Count the number of fullsyncs and normal syncs.  This is used to test
  1.2093 -** that syncs and fullsyncs are occuring at the right times.
  1.2094 -*/
  1.2095 -SQLITE_API int sqlite3_sync_count = 0;
  1.2096 -SQLITE_API int sqlite3_fullsync_count = 0;
  1.2097 -#endif
  1.2098 -
  1.2099 -/*
  1.2100 -** Make sure all writes to a particular file are committed to disk.
  1.2101 -*/
  1.2102 -static int os2Sync( sqlite3_file *id, int flags ){
  1.2103 -  os2File *pFile = (os2File*)id;
  1.2104 -  OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
  1.2105 -#ifdef SQLITE_TEST
  1.2106 -  if( flags & SQLITE_SYNC_FULL){
  1.2107 -    sqlite3_fullsync_count++;
  1.2108 -  }
  1.2109 -  sqlite3_sync_count++;
  1.2110 -#endif
  1.2111 -  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
  1.2112 -  ** no-op
  1.2113 -  */
  1.2114 -#ifdef SQLITE_NO_SYNC
  1.2115 -  UNUSED_PARAMETER(pFile);
  1.2116 -  return SQLITE_OK;
  1.2117 -#else
  1.2118 -  return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
  1.2119 -#endif
  1.2120 -}
  1.2121 -
  1.2122 -/*
  1.2123 -** Determine the current size of a file in bytes
  1.2124 -*/
  1.2125 -static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
  1.2126 -  APIRET rc = NO_ERROR;
  1.2127 -  FILESTATUS3 fsts3FileInfo;
  1.2128 -  memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
  1.2129 -  assert( id!=0 );
  1.2130 -  SimulateIOError( return SQLITE_IOERR_FSTAT );
  1.2131 -  rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
  1.2132 -  if( rc == NO_ERROR ){
  1.2133 -    *pSize = fsts3FileInfo.cbFile;
  1.2134 -    return SQLITE_OK;
  1.2135 -  }else{
  1.2136 -    return SQLITE_IOERR_FSTAT;
  1.2137 -  }
  1.2138 -}
  1.2139 -
  1.2140 -/*
  1.2141 -** Acquire a reader lock.
  1.2142 -*/
  1.2143 -static int getReadLock( os2File *pFile ){
  1.2144 -  FILELOCK  LockArea,
  1.2145 -            UnlockArea;
  1.2146 -  APIRET res;
  1.2147 -  memset(&LockArea, 0, sizeof(LockArea));
  1.2148 -  memset(&UnlockArea, 0, sizeof(UnlockArea));
  1.2149 -  LockArea.lOffset = SHARED_FIRST;
  1.2150 -  LockArea.lRange = SHARED_SIZE;
  1.2151 -  UnlockArea.lOffset = 0L;
  1.2152 -  UnlockArea.lRange = 0L;
  1.2153 -  res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
  1.2154 -  OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
  1.2155 -  return res;
  1.2156 -}
  1.2157 -
  1.2158 -/*
  1.2159 -** Undo a readlock
  1.2160 -*/
  1.2161 -static int unlockReadLock( os2File *id ){
  1.2162 -  FILELOCK  LockArea,
  1.2163 -            UnlockArea;
  1.2164 -  APIRET res;
  1.2165 -  memset(&LockArea, 0, sizeof(LockArea));
  1.2166 -  memset(&UnlockArea, 0, sizeof(UnlockArea));
  1.2167 -  LockArea.lOffset = 0L;
  1.2168 -  LockArea.lRange = 0L;
  1.2169 -  UnlockArea.lOffset = SHARED_FIRST;
  1.2170 -  UnlockArea.lRange = SHARED_SIZE;
  1.2171 -  res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
  1.2172 -  OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
  1.2173 -  return res;
  1.2174 -}
  1.2175 -
  1.2176 -/*
  1.2177 -** Lock the file with the lock specified by parameter locktype - one
  1.2178 -** of the following:
  1.2179 -**
  1.2180 -**     (1) SHARED_LOCK
  1.2181 -**     (2) RESERVED_LOCK
  1.2182 -**     (3) PENDING_LOCK
  1.2183 -**     (4) EXCLUSIVE_LOCK
  1.2184 -**
  1.2185 -** Sometimes when requesting one lock state, additional lock states
  1.2186 -** are inserted in between.  The locking might fail on one of the later
  1.2187 -** transitions leaving the lock state different from what it started but
  1.2188 -** still short of its goal.  The following chart shows the allowed
  1.2189 -** transitions and the inserted intermediate states:
  1.2190 -**
  1.2191 -**    UNLOCKED -> SHARED
  1.2192 -**    SHARED -> RESERVED
  1.2193 -**    SHARED -> (PENDING) -> EXCLUSIVE
  1.2194 -**    RESERVED -> (PENDING) -> EXCLUSIVE
  1.2195 -**    PENDING -> EXCLUSIVE
  1.2196 -**
  1.2197 -** This routine will only increase a lock.  The os2Unlock() routine
  1.2198 -** erases all locks at once and returns us immediately to locking level 0.
  1.2199 -** It is not possible to lower the locking level one step at a time.  You
  1.2200 -** must go straight to locking level 0.
  1.2201 -*/
  1.2202 -static int os2Lock( sqlite3_file *id, int locktype ){
  1.2203 -  int rc = SQLITE_OK;       /* Return code from subroutines */
  1.2204 -  APIRET res = NO_ERROR;    /* Result of an OS/2 lock call */
  1.2205 -  int newLocktype;       /* Set pFile->locktype to this value before exiting */
  1.2206 -  int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
  1.2207 -  FILELOCK  LockArea,
  1.2208 -            UnlockArea;
  1.2209 -  os2File *pFile = (os2File*)id;
  1.2210 -  memset(&LockArea, 0, sizeof(LockArea));
  1.2211 -  memset(&UnlockArea, 0, sizeof(UnlockArea));
  1.2212 -  assert( pFile!=0 );
  1.2213 -  OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
  1.2214 -
  1.2215 -  /* If there is already a lock of this type or more restrictive on the
  1.2216 -  ** os2File, do nothing. Don't use the end_lock: exit path, as
  1.2217 -  ** sqlite3_mutex_enter() hasn't been called yet.
  1.2218 -  */
  1.2219 -  if( pFile->locktype>=locktype ){
  1.2220 -    OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
  1.2221 -    return SQLITE_OK;
  1.2222 -  }
  1.2223 -
  1.2224 -  /* Make sure the locking sequence is correct
  1.2225 -  */
  1.2226 -  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
  1.2227 -  assert( locktype!=PENDING_LOCK );
  1.2228 -  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
  1.2229 -
  1.2230 -  /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
  1.2231 -  ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
  1.2232 -  ** the PENDING_LOCK byte is temporary.
  1.2233 -  */
  1.2234 -  newLocktype = pFile->locktype;
  1.2235 -  if( pFile->locktype==NO_LOCK
  1.2236 -      || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
  1.2237 -  ){
  1.2238 -    LockArea.lOffset = PENDING_BYTE;
  1.2239 -    LockArea.lRange = 1L;
  1.2240 -    UnlockArea.lOffset = 0L;
  1.2241 -    UnlockArea.lRange = 0L;
  1.2242 -
  1.2243 -    /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
  1.2244 -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
  1.2245 -    if( res == NO_ERROR ){
  1.2246 -      gotPendingLock = 1;
  1.2247 -      OSTRACE(( "LOCK %d pending lock boolean set.  res=%d\n", pFile->h, res ));
  1.2248 -    }
  1.2249 -  }
  1.2250 -
  1.2251 -  /* Acquire a shared lock
  1.2252 -  */
  1.2253 -  if( locktype==SHARED_LOCK && res == NO_ERROR ){
  1.2254 -    assert( pFile->locktype==NO_LOCK );
  1.2255 -    res = getReadLock(pFile);
  1.2256 -    if( res == NO_ERROR ){
  1.2257 -      newLocktype = SHARED_LOCK;
  1.2258 -    }
  1.2259 -    OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
  1.2260 -  }
  1.2261 -
  1.2262 -  /* Acquire a RESERVED lock
  1.2263 -  */
  1.2264 -  if( locktype==RESERVED_LOCK && res == NO_ERROR ){
  1.2265 -    assert( pFile->locktype==SHARED_LOCK );
  1.2266 -    LockArea.lOffset = RESERVED_BYTE;
  1.2267 -    LockArea.lRange = 1L;
  1.2268 -    UnlockArea.lOffset = 0L;
  1.2269 -    UnlockArea.lRange = 0L;
  1.2270 -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
  1.2271 -    if( res == NO_ERROR ){
  1.2272 -      newLocktype = RESERVED_LOCK;
  1.2273 -    }
  1.2274 -    OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
  1.2275 -  }
  1.2276 -
  1.2277 -  /* Acquire a PENDING lock
  1.2278 -  */
  1.2279 -  if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
  1.2280 -    newLocktype = PENDING_LOCK;
  1.2281 -    gotPendingLock = 0;
  1.2282 -    OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
  1.2283 -               pFile->h ));
  1.2284 -  }
  1.2285 -
  1.2286 -  /* Acquire an EXCLUSIVE lock
  1.2287 -  */
  1.2288 -  if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
  1.2289 -    assert( pFile->locktype>=SHARED_LOCK );
  1.2290 -    res = unlockReadLock(pFile);
  1.2291 -    OSTRACE(( "unreadlock = %d\n", res ));
  1.2292 -    LockArea.lOffset = SHARED_FIRST;
  1.2293 -    LockArea.lRange = SHARED_SIZE;
  1.2294 -    UnlockArea.lOffset = 0L;
  1.2295 -    UnlockArea.lRange = 0L;
  1.2296 -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
  1.2297 -    if( res == NO_ERROR ){
  1.2298 -      newLocktype = EXCLUSIVE_LOCK;
  1.2299 -    }else{
  1.2300 -      OSTRACE(( "OS/2 error-code = %d\n", res ));
  1.2301 -      getReadLock(pFile);
  1.2302 -    }
  1.2303 -    OSTRACE(( "LOCK %d acquire exclusive lock.  res=%d\n", pFile->h, res ));
  1.2304 -  }
  1.2305 -
  1.2306 -  /* If we are holding a PENDING lock that ought to be released, then
  1.2307 -  ** release it now.
  1.2308 -  */
  1.2309 -  if( gotPendingLock && locktype==SHARED_LOCK ){
  1.2310 -    int r;
  1.2311 -    LockArea.lOffset = 0L;
  1.2312 -    LockArea.lRange = 0L;
  1.2313 -    UnlockArea.lOffset = PENDING_BYTE;
  1.2314 -    UnlockArea.lRange = 1L;
  1.2315 -    r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
  1.2316 -    OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
  1.2317 -  }
  1.2318 -
  1.2319 -  /* Update the state of the lock has held in the file descriptor then
  1.2320 -  ** return the appropriate result code.
  1.2321 -  */
  1.2322 -  if( res == NO_ERROR ){
  1.2323 -    rc = SQLITE_OK;
  1.2324 -  }else{
  1.2325 -    OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
  1.2326 -              locktype, newLocktype ));
  1.2327 -    rc = SQLITE_BUSY;
  1.2328 -  }
  1.2329 -  pFile->locktype = newLocktype;
  1.2330 -  OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
  1.2331 -  return rc;
  1.2332 -}
  1.2333 -
  1.2334 -/*
  1.2335 -** This routine checks if there is a RESERVED lock held on the specified
  1.2336 -** file by this or any other process. If such a lock is held, return
  1.2337 -** non-zero, otherwise zero.
  1.2338 -*/
  1.2339 -static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
  1.2340 -  int r = 0;
  1.2341 -  os2File *pFile = (os2File*)id;
  1.2342 -  assert( pFile!=0 );
  1.2343 -  if( pFile->locktype>=RESERVED_LOCK ){
  1.2344 -    r = 1;
  1.2345 -    OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
  1.2346 -  }else{
  1.2347 -    FILELOCK  LockArea,
  1.2348 -              UnlockArea;
  1.2349 -    APIRET rc = NO_ERROR;
  1.2350 -    memset(&LockArea, 0, sizeof(LockArea));
  1.2351 -    memset(&UnlockArea, 0, sizeof(UnlockArea));
  1.2352 -    LockArea.lOffset = RESERVED_BYTE;
  1.2353 -    LockArea.lRange = 1L;
  1.2354 -    UnlockArea.lOffset = 0L;
  1.2355 -    UnlockArea.lRange = 0L;
  1.2356 -    rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
  1.2357 -    OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
  1.2358 -    if( rc == NO_ERROR ){
  1.2359 -      APIRET rcu = NO_ERROR; /* return code for unlocking */
  1.2360 -      LockArea.lOffset = 0L;
  1.2361 -      LockArea.lRange = 0L;
  1.2362 -      UnlockArea.lOffset = RESERVED_BYTE;
  1.2363 -      UnlockArea.lRange = 1L;
  1.2364 -      rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
  1.2365 -      OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
  1.2366 -    }
  1.2367 -    r = !(rc == NO_ERROR);
  1.2368 -    OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
  1.2369 -  }
  1.2370 -  *pOut = r;
  1.2371 -  return SQLITE_OK;
  1.2372 -}
  1.2373 -
  1.2374 -/*
  1.2375 -** Lower the locking level on file descriptor id to locktype.  locktype
  1.2376 -** must be either NO_LOCK or SHARED_LOCK.
  1.2377 -**
  1.2378 -** If the locking level of the file descriptor is already at or below
  1.2379 -** the requested locking level, this routine is a no-op.
  1.2380 -**
  1.2381 -** It is not possible for this routine to fail if the second argument
  1.2382 -** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
  1.2383 -** might return SQLITE_IOERR;
  1.2384 -*/
  1.2385 -static int os2Unlock( sqlite3_file *id, int locktype ){
  1.2386 -  int type;
  1.2387 -  os2File *pFile = (os2File*)id;
  1.2388 -  APIRET rc = SQLITE_OK;
  1.2389 -  APIRET res = NO_ERROR;
  1.2390 -  FILELOCK  LockArea,
  1.2391 -            UnlockArea;
  1.2392 -  memset(&LockArea, 0, sizeof(LockArea));
  1.2393 -  memset(&UnlockArea, 0, sizeof(UnlockArea));
  1.2394 -  assert( pFile!=0 );
  1.2395 -  assert( locktype<=SHARED_LOCK );
  1.2396 -  OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
  1.2397 -  type = pFile->locktype;
  1.2398 -  if( type>=EXCLUSIVE_LOCK ){
  1.2399 -    LockArea.lOffset = 0L;
  1.2400 -    LockArea.lRange = 0L;
  1.2401 -    UnlockArea.lOffset = SHARED_FIRST;
  1.2402 -    UnlockArea.lRange = SHARED_SIZE;
  1.2403 -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
  1.2404 -    OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
  1.2405 -    if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
  1.2406 -      /* This should never happen.  We should always be able to
  1.2407 -      ** reacquire the read lock */
  1.2408 -      OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
  1.2409 -      rc = SQLITE_IOERR_UNLOCK;
  1.2410 -    }
  1.2411 -  }
  1.2412 -  if( type>=RESERVED_LOCK ){
  1.2413 -    LockArea.lOffset = 0L;
  1.2414 -    LockArea.lRange = 0L;
  1.2415 -    UnlockArea.lOffset = RESERVED_BYTE;
  1.2416 -    UnlockArea.lRange = 1L;
  1.2417 -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
  1.2418 -    OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
  1.2419 -  }
  1.2420 -  if( locktype==NO_LOCK && type>=SHARED_LOCK ){
  1.2421 -    res = unlockReadLock(pFile);
  1.2422 -    OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
  1.2423 -              pFile->h, type, locktype, res ));
  1.2424 -  }
  1.2425 -  if( type>=PENDING_LOCK ){
  1.2426 -    LockArea.lOffset = 0L;
  1.2427 -    LockArea.lRange = 0L;
  1.2428 -    UnlockArea.lOffset = PENDING_BYTE;
  1.2429 -    UnlockArea.lRange = 1L;
  1.2430 -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
  1.2431 -    OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
  1.2432 -  }
  1.2433 -  pFile->locktype = locktype;
  1.2434 -  OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
  1.2435 -  return rc;
  1.2436 -}
  1.2437 -
  1.2438 -/*
  1.2439 -** Control and query of the open file handle.
  1.2440 -*/
  1.2441 -static int os2FileControl(sqlite3_file *id, int op, void *pArg){
  1.2442 -  switch( op ){
  1.2443 -    case SQLITE_FCNTL_LOCKSTATE: {
  1.2444 -      *(int*)pArg = ((os2File*)id)->locktype;
  1.2445 -      OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
  1.2446 -                ((os2File*)id)->h, ((os2File*)id)->locktype ));
  1.2447 -      return SQLITE_OK;
  1.2448 -    }
  1.2449 -    case SQLITE_FCNTL_CHUNK_SIZE: {
  1.2450 -      ((os2File*)id)->szChunk = *(int*)pArg;
  1.2451 -      return SQLITE_OK;
  1.2452 -    }
  1.2453 -    case SQLITE_FCNTL_SIZE_HINT: {
  1.2454 -      sqlite3_int64 sz = *(sqlite3_int64*)pArg;
  1.2455 -      SimulateIOErrorBenign(1);
  1.2456 -      os2Truncate(id, sz);
  1.2457 -      SimulateIOErrorBenign(0);
  1.2458 -      return SQLITE_OK;
  1.2459 -    }
  1.2460 -    case SQLITE_FCNTL_SYNC_OMITTED: {
  1.2461 -      return SQLITE_OK;
  1.2462 -    }
  1.2463 -  }
  1.2464 -  return SQLITE_NOTFOUND;
  1.2465 -}
  1.2466 -
  1.2467 -/*
  1.2468 -** Return the sector size in bytes of the underlying block device for
  1.2469 -** the specified file. This is almost always 512 bytes, but may be
  1.2470 -** larger for some devices.
  1.2471 -**
  1.2472 -** SQLite code assumes this function cannot fail. It also assumes that
  1.2473 -** if two files are created in the same file-system directory (i.e.
  1.2474 -** a database and its journal file) that the sector size will be the
  1.2475 -** same for both.
  1.2476 -*/
  1.2477 -static int os2SectorSize(sqlite3_file *id){
  1.2478 -  UNUSED_PARAMETER(id);
  1.2479 -  return SQLITE_DEFAULT_SECTOR_SIZE;
  1.2480 -}
  1.2481 -
  1.2482 -/*
  1.2483 -** Return a vector of device characteristics.
  1.2484 -*/
  1.2485 -static int os2DeviceCharacteristics(sqlite3_file *id){
  1.2486 -  UNUSED_PARAMETER(id);
  1.2487 -  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
  1.2488 -}
  1.2489 -
  1.2490 -
  1.2491 -/*
  1.2492 -** Character set conversion objects used by conversion routines.
  1.2493 -*/
  1.2494 -static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
  1.2495 -static UconvObject uclCp = NULL;  /* convert between local codepage and UCS-2 */
  1.2496 -
  1.2497 -/*
  1.2498 -** Helper function to initialize the conversion objects from and to UTF-8.
  1.2499 -*/
  1.2500 -static void initUconvObjects( void ){
  1.2501 -  if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
  1.2502 -    ucUtf8 = NULL;
  1.2503 -  if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
  1.2504 -    uclCp = NULL;
  1.2505 -}
  1.2506 -
  1.2507 -/*
  1.2508 -** Helper function to free the conversion objects from and to UTF-8.
  1.2509 -*/
  1.2510 -static void freeUconvObjects( void ){
  1.2511 -  if ( ucUtf8 )
  1.2512 -    UniFreeUconvObject( ucUtf8 );
  1.2513 -  if ( uclCp )
  1.2514 -    UniFreeUconvObject( uclCp );
  1.2515 -  ucUtf8 = NULL;
  1.2516 -  uclCp = NULL;
  1.2517 -}
  1.2518 -
  1.2519 -/*
  1.2520 -** Helper function to convert UTF-8 filenames to local OS/2 codepage.
  1.2521 -** The two-step process: first convert the incoming UTF-8 string
  1.2522 -** into UCS-2 and then from UCS-2 to the current codepage.
  1.2523 -** The returned char pointer has to be freed.
  1.2524 -*/
  1.2525 -static char *convertUtf8PathToCp( const char *in ){
  1.2526 -  UniChar tempPath[CCHMAXPATH];
  1.2527 -  char *out = (char *)calloc( CCHMAXPATH, 1 );
  1.2528 -
  1.2529 -  if( !out )
  1.2530 -    return NULL;
  1.2531 -
  1.2532 -  if( !ucUtf8 || !uclCp )
  1.2533 -    initUconvObjects();
  1.2534 -
  1.2535 -  /* determine string for the conversion of UTF-8 which is CP1208 */
  1.2536 -  if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
  1.2537 -    return out; /* if conversion fails, return the empty string */
  1.2538 -
  1.2539 -  /* conversion for current codepage which can be used for paths */
  1.2540 -  UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
  1.2541 -
  1.2542 -  return out;
  1.2543 -}
  1.2544 -
  1.2545 -/*
  1.2546 -** Helper function to convert filenames from local codepage to UTF-8.
  1.2547 -** The two-step process: first convert the incoming codepage-specific
  1.2548 -** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
  1.2549 -** The returned char pointer has to be freed.
  1.2550 -**
  1.2551 -** This function is non-static to be able to use this in shell.c and
  1.2552 -** similar applications that take command line arguments.
  1.2553 -*/
  1.2554 -char *convertCpPathToUtf8( const char *in ){
  1.2555 -  UniChar tempPath[CCHMAXPATH];
  1.2556 -  char *out = (char *)calloc( CCHMAXPATH, 1 );
  1.2557 -
  1.2558 -  if( !out )
  1.2559 -    return NULL;
  1.2560 -
  1.2561 -  if( !ucUtf8 || !uclCp )
  1.2562 -    initUconvObjects();
  1.2563 -
  1.2564 -  /* conversion for current codepage which can be used for paths */
  1.2565 -  if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
  1.2566 -    return out; /* if conversion fails, return the empty string */
  1.2567 -
  1.2568 -  /* determine string for the conversion of UTF-8 which is CP1208 */
  1.2569 -  UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
  1.2570 -
  1.2571 -  return out;
  1.2572 -}
  1.2573 -
  1.2574 -
  1.2575 -#ifndef SQLITE_OMIT_WAL
  1.2576 -
  1.2577 -/*
  1.2578 -** Use main database file for interprocess locking. If un-defined
  1.2579 -** a separate file is created for this purpose. The file will be
  1.2580 -** used only to set file locks. There will be no data written to it.
  1.2581 -*/
  1.2582 -#define SQLITE_OS2_NO_WAL_LOCK_FILE     
  1.2583 -
  1.2584 -#if 0
  1.2585 -static void _ERR_TRACE( const char *fmt, ... ) {
  1.2586 -  va_list  ap;
  1.2587 -  va_start(ap, fmt);
  1.2588 -  vfprintf(stderr, fmt, ap);
  1.2589 -  fflush(stderr);
  1.2590 -}
  1.2591 -#define ERR_TRACE(rc, msg)        \
  1.2592 -        if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
  1.2593 -#else
  1.2594 -#define ERR_TRACE(rc, msg)
  1.2595 -#endif
  1.2596 -
  1.2597 -/*
  1.2598 -** Helper functions to obtain and relinquish the global mutex. The
  1.2599 -** global mutex is used to protect os2ShmNodeList.
  1.2600 -**
  1.2601 -** Function os2ShmMutexHeld() is used to assert() that the global mutex 
  1.2602 -** is held when required. This function is only used as part of assert() 
  1.2603 -** statements. e.g.
  1.2604 -**
  1.2605 -**   os2ShmEnterMutex()
  1.2606 -**     assert( os2ShmMutexHeld() );
  1.2607 -**   os2ShmLeaveMutex()
  1.2608 -*/
  1.2609 -static void os2ShmEnterMutex(void){
  1.2610 -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  1.2611 -}
  1.2612 -static void os2ShmLeaveMutex(void){
  1.2613 -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  1.2614 -}
  1.2615 -#ifdef SQLITE_DEBUG
  1.2616 -static int os2ShmMutexHeld(void) {
  1.2617 -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  1.2618 -}
  1.2619 -int GetCurrentProcessId(void) {
  1.2620 -  PPIB pib;
  1.2621 -  DosGetInfoBlocks(NULL, &pib);
  1.2622 -  return (int)pib->pib_ulpid;
  1.2623 -}
  1.2624 -#endif
  1.2625 -
  1.2626 -/*
  1.2627 -** Object used to represent a the shared memory area for a single log file.
  1.2628 -** When multiple threads all reference the same log-summary, each thread has
  1.2629 -** its own os2File object, but they all point to a single instance of this 
  1.2630 -** object.  In other words, each log-summary is opened only once per process.
  1.2631 -**
  1.2632 -** os2ShmMutexHeld() must be true when creating or destroying
  1.2633 -** this object or while reading or writing the following fields:
  1.2634 -**
  1.2635 -**      nRef
  1.2636 -**      pNext 
  1.2637 -**
  1.2638 -** The following fields are read-only after the object is created:
  1.2639 -** 
  1.2640 -**      szRegion
  1.2641 -**      hLockFile
  1.2642 -**      shmBaseName
  1.2643 -**
  1.2644 -** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
  1.2645 -** os2ShmMutexHeld() is true when reading or writing any other field
  1.2646 -** in this structure.
  1.2647 -**
  1.2648 -*/
  1.2649 -struct os2ShmNode {
  1.2650 -  sqlite3_mutex *mutex;      /* Mutex to access this object */
  1.2651 -  os2ShmNode *pNext;         /* Next in list of all os2ShmNode objects */
  1.2652 -
  1.2653 -  int szRegion;              /* Size of shared-memory regions */
  1.2654 -
  1.2655 -  int nRegion;               /* Size of array apRegion */
  1.2656 -  void **apRegion;           /* Array of pointers to shared-memory regions */
  1.2657 -
  1.2658 -  int nRef;                  /* Number of os2ShmLink objects pointing to this */
  1.2659 -  os2ShmLink *pFirst;        /* First os2ShmLink object pointing to this */
  1.2660 -
  1.2661 -  HFILE hLockFile;           /* File used for inter-process memory locking */
  1.2662 -  char shmBaseName[1];       /* Name of the memory object !!! must last !!! */
  1.2663 -};
  1.2664 -
  1.2665 -
  1.2666 -/*
  1.2667 -** Structure used internally by this VFS to record the state of an
  1.2668 -** open shared memory connection.
  1.2669 -**
  1.2670 -** The following fields are initialized when this object is created and
  1.2671 -** are read-only thereafter:
  1.2672 -**
  1.2673 -**    os2Shm.pShmNode
  1.2674 -**    os2Shm.id
  1.2675 -**
  1.2676 -** All other fields are read/write.  The os2Shm.pShmNode->mutex must be held
  1.2677 -** while accessing any read/write fields.
  1.2678 -*/
  1.2679 -struct os2ShmLink {
  1.2680 -  os2ShmNode *pShmNode;      /* The underlying os2ShmNode object */
  1.2681 -  os2ShmLink *pNext;         /* Next os2Shm with the same os2ShmNode */
  1.2682 -  u32 sharedMask;            /* Mask of shared locks held */
  1.2683 -  u32 exclMask;              /* Mask of exclusive locks held */
  1.2684 -#ifdef SQLITE_DEBUG
  1.2685 -  u8 id;                     /* Id of this connection with its os2ShmNode */
  1.2686 -#endif
  1.2687 -};
  1.2688 -
  1.2689 -
  1.2690 -/*
  1.2691 -** A global list of all os2ShmNode objects.
  1.2692 -**
  1.2693 -** The os2ShmMutexHeld() must be true while reading or writing this list.
  1.2694 -*/
  1.2695 -static os2ShmNode *os2ShmNodeList = NULL;
  1.2696 -
  1.2697 -/*
  1.2698 -** Constants used for locking
  1.2699 -*/
  1.2700 -#ifdef  SQLITE_OS2_NO_WAL_LOCK_FILE
  1.2701 -#define OS2_SHM_BASE   (PENDING_BYTE + 0x10000)         /* first lock byte */
  1.2702 -#else
  1.2703 -#define OS2_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
  1.2704 -#endif
  1.2705 -
  1.2706 -#define OS2_SHM_DMS    (OS2_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
  1.2707 -
  1.2708 -/*
  1.2709 -** Apply advisory locks for all n bytes beginning at ofst.
  1.2710 -*/
  1.2711 -#define _SHM_UNLCK  1   /* no lock */
  1.2712 -#define _SHM_RDLCK  2   /* shared lock, no wait */
  1.2713 -#define _SHM_WRLCK  3   /* exlusive lock, no wait */
  1.2714 -#define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
  1.2715 -static int os2ShmSystemLock(
  1.2716 -  os2ShmNode *pNode,    /* Apply locks to this open shared-memory segment */
  1.2717 -  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
  1.2718 -  int ofst,             /* Offset to first byte to be locked/unlocked */
  1.2719 -  int nByte             /* Number of bytes to lock or unlock */
  1.2720 -){
  1.2721 -  APIRET rc;
  1.2722 -  FILELOCK area;
  1.2723 -  ULONG mode, timeout;
  1.2724 -
  1.2725 -  /* Access to the os2ShmNode object is serialized by the caller */
  1.2726 -  assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
  1.2727 -
  1.2728 -  mode = 1;     /* shared lock */
  1.2729 -  timeout = 0;  /* no wait */
  1.2730 -  area.lOffset = ofst;
  1.2731 -  area.lRange = nByte;
  1.2732 -
  1.2733 -  switch( lockType ) {
  1.2734 -    case _SHM_WRLCK_WAIT:
  1.2735 -      timeout = (ULONG)-1;      /* wait forever */
  1.2736 -    case _SHM_WRLCK:
  1.2737 -      mode = 0;                 /* exclusive lock */
  1.2738 -    case _SHM_RDLCK:
  1.2739 -      rc = DosSetFileLocks(pNode->hLockFile, 
  1.2740 -                           NULL, &area, timeout, mode);
  1.2741 -      break;
  1.2742 -    /* case _SHM_UNLCK: */
  1.2743 -    default:
  1.2744 -      rc = DosSetFileLocks(pNode->hLockFile, 
  1.2745 -                           &area, NULL, 0, 0);
  1.2746 -      break;
  1.2747 -  }
  1.2748 -                          
  1.2749 -  OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n", 
  1.2750 -           pNode->hLockFile,
  1.2751 -           rc==SQLITE_OK ? "ok" : "failed",
  1.2752 -           lockType==_SHM_UNLCK ? "Unlock" : "Lock",
  1.2753 -           rc));
  1.2754 -
  1.2755 -  ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
  1.2756 -
  1.2757 -  return ( rc == 0 ) ?  SQLITE_OK : SQLITE_BUSY;
  1.2758 -}
  1.2759 -
  1.2760 -/*
  1.2761 -** Find an os2ShmNode in global list or allocate a new one, if not found.
  1.2762 -**
  1.2763 -** This is not a VFS shared-memory method; it is a utility function called
  1.2764 -** by VFS shared-memory methods.
  1.2765 -*/
  1.2766 -static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
  1.2767 -  os2ShmLink *pLink;
  1.2768 -  os2ShmNode *pNode;
  1.2769 -  int cbShmName, rc = SQLITE_OK;
  1.2770 -  char shmName[CCHMAXPATH + 30];
  1.2771 -#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
  1.2772 -  ULONG action;
  1.2773 -#endif
  1.2774 -  
  1.2775 -  /* We need some additional space at the end to append the region number */
  1.2776 -  cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
  1.2777 -  if( cbShmName >= CCHMAXPATH-8 )
  1.2778 -    return SQLITE_IOERR_SHMOPEN; 
  1.2779 -
  1.2780 -  /* Replace colon in file name to form a valid shared memory name */
  1.2781 -  shmName[10+1] = '!';
  1.2782 -
  1.2783 -  /* Allocate link object (we free it later in case of failure) */
  1.2784 -  pLink = sqlite3_malloc( sizeof(*pLink) );
  1.2785 -  if( !pLink )
  1.2786 -    return SQLITE_NOMEM;
  1.2787 -
  1.2788 -  /* Access node list */
  1.2789 -  os2ShmEnterMutex();
  1.2790 -
  1.2791 -  /* Find node by it's shared memory base name */
  1.2792 -  for( pNode = os2ShmNodeList; 
  1.2793 -       pNode && stricmp(shmName, pNode->shmBaseName) != 0; 
  1.2794 -       pNode = pNode->pNext )   ;
  1.2795 -
  1.2796 -  /* Not found: allocate a new node */
  1.2797 -  if( !pNode ) {
  1.2798 -    pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
  1.2799 -    if( pNode ) {
  1.2800 -      memset(pNode, 0, sizeof(*pNode) );
  1.2801 -      pNode->szRegion = szRegion;
  1.2802 -      pNode->hLockFile = (HFILE)-1;      
  1.2803 -      strcpy(pNode->shmBaseName, shmName);
  1.2804 -
  1.2805 -#ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
  1.2806 -      if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
  1.2807 -#else
  1.2808 -      sprintf(shmName, "%s-lck", fd->zFullPathCp);
  1.2809 -      if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL, 
  1.2810 -                  OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  1.2811 -                  OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE | 
  1.2812 -                  OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
  1.2813 -                  NULL) != 0 ) {
  1.2814 -#endif
  1.2815 -        sqlite3_free(pNode);  
  1.2816 -        rc = SQLITE_IOERR;
  1.2817 -      } else {
  1.2818 -        pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  1.2819 -        if( !pNode->mutex ) {
  1.2820 -          sqlite3_free(pNode);  
  1.2821 -          rc = SQLITE_NOMEM;
  1.2822 -        }
  1.2823 -      }   
  1.2824 -    } else {
  1.2825 -      rc = SQLITE_NOMEM;
  1.2826 -    }
  1.2827 -    
  1.2828 -    if( rc == SQLITE_OK ) {
  1.2829 -      pNode->pNext = os2ShmNodeList;
  1.2830 -      os2ShmNodeList = pNode;
  1.2831 -    } else {
  1.2832 -      pNode = NULL;
  1.2833 -    }
  1.2834 -  } else if( pNode->szRegion != szRegion ) {
  1.2835 -    rc = SQLITE_IOERR_SHMSIZE;
  1.2836 -    pNode = NULL;
  1.2837 -  }
  1.2838 -
  1.2839 -  if( pNode ) {
  1.2840 -    sqlite3_mutex_enter(pNode->mutex);
  1.2841 -
  1.2842 -    memset(pLink, 0, sizeof(*pLink));
  1.2843 -
  1.2844 -    pLink->pShmNode = pNode;
  1.2845 -    pLink->pNext = pNode->pFirst;
  1.2846 -    pNode->pFirst = pLink;
  1.2847 -    pNode->nRef++;
  1.2848 -
  1.2849 -    fd->pShmLink = pLink;
  1.2850 -
  1.2851 -    sqlite3_mutex_leave(pNode->mutex);
  1.2852 -    
  1.2853 -  } else {
  1.2854 -    /* Error occured. Free our link object. */
  1.2855 -    sqlite3_free(pLink);  
  1.2856 -  }
  1.2857 -
  1.2858 -  os2ShmLeaveMutex();
  1.2859 -
  1.2860 -  ERR_TRACE(rc, ("os2OpenSharedMemory: %d  %s\n", rc, fd->zFullPathCp))  
  1.2861 -  
  1.2862 -  return rc;
  1.2863 -}
  1.2864 -
  1.2865 -/*
  1.2866 -** Purge the os2ShmNodeList list of all entries with nRef==0.
  1.2867 -**
  1.2868 -** This is not a VFS shared-memory method; it is a utility function called
  1.2869 -** by VFS shared-memory methods.
  1.2870 -*/
  1.2871 -static void os2PurgeShmNodes( int deleteFlag ) {
  1.2872 -  os2ShmNode *pNode;
  1.2873 -  os2ShmNode **ppNode;
  1.2874 -
  1.2875 -  os2ShmEnterMutex();
  1.2876 -  
  1.2877 -  ppNode = &os2ShmNodeList;
  1.2878 -
  1.2879 -  while( *ppNode ) {
  1.2880 -    pNode = *ppNode;
  1.2881 -
  1.2882 -    if( pNode->nRef == 0 ) {
  1.2883 -      *ppNode = pNode->pNext;   
  1.2884 -     
  1.2885 -      if( pNode->apRegion ) {
  1.2886 -        /* Prevent other processes from resizing the shared memory */
  1.2887 -        os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
  1.2888 -
  1.2889 -        while( pNode->nRegion-- ) {
  1.2890 -#ifdef SQLITE_DEBUG
  1.2891 -          int rc = 
  1.2892 -#endif          
  1.2893 -          DosFreeMem(pNode->apRegion[pNode->nRegion]);
  1.2894 -
  1.2895 -          OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
  1.2896 -                  (int)GetCurrentProcessId(), pNode->nRegion,
  1.2897 -                  rc == 0 ? "ok" : "failed"));
  1.2898 -        }
  1.2899 -
  1.2900 -        /* Allow other processes to resize the shared memory */
  1.2901 -        os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
  1.2902 -
  1.2903 -        sqlite3_free(pNode->apRegion);
  1.2904 -      }  
  1.2905 -
  1.2906 -      DosClose(pNode->hLockFile);
  1.2907 -      
  1.2908 -#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
  1.2909 -      if( deleteFlag ) {
  1.2910 -         char fileName[CCHMAXPATH];
  1.2911 -         /* Skip "\\SHAREMEM\\" */
  1.2912 -         sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
  1.2913 -         /* restore colon */
  1.2914 -         fileName[1] = ':';
  1.2915 -         
  1.2916 -         DosForceDelete(fileName); 
  1.2917 -      }
  1.2918 -#endif
  1.2919 -
  1.2920 -      sqlite3_mutex_free(pNode->mutex);
  1.2921 -
  1.2922 -      sqlite3_free(pNode);
  1.2923 -      
  1.2924 -    } else {
  1.2925 -      ppNode = &pNode->pNext;
  1.2926 -    }
  1.2927 -  } 
  1.2928 -
  1.2929 -  os2ShmLeaveMutex();
  1.2930 -}
  1.2931 -
  1.2932 -/*
  1.2933 -** This function is called to obtain a pointer to region iRegion of the
  1.2934 -** shared-memory associated with the database file id. Shared-memory regions
  1.2935 -** are numbered starting from zero. Each shared-memory region is szRegion
  1.2936 -** bytes in size.
  1.2937 -**
  1.2938 -** If an error occurs, an error code is returned and *pp is set to NULL.
  1.2939 -**
  1.2940 -** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
  1.2941 -** region has not been allocated (by any client, including one running in a
  1.2942 -** separate process), then *pp is set to NULL and SQLITE_OK returned. If
  1.2943 -** bExtend is non-zero and the requested shared-memory region has not yet
  1.2944 -** been allocated, it is allocated by this function.
  1.2945 -**
  1.2946 -** If the shared-memory region has already been allocated or is allocated by
  1.2947 -** this call as described above, then it is mapped into this processes
  1.2948 -** address space (if it is not already), *pp is set to point to the mapped
  1.2949 -** memory and SQLITE_OK returned.
  1.2950 -*/
  1.2951 -static int os2ShmMap(
  1.2952 -  sqlite3_file *id,               /* Handle open on database file */
  1.2953 -  int iRegion,                    /* Region to retrieve */
  1.2954 -  int szRegion,                   /* Size of regions */
  1.2955 -  int bExtend,                    /* True to extend block if necessary */
  1.2956 -  void volatile **pp              /* OUT: Mapped memory */
  1.2957 -){
  1.2958 -  PVOID pvTemp;
  1.2959 -  void **apRegion;
  1.2960 -  os2ShmNode *pNode;
  1.2961 -  int n, rc = SQLITE_OK;
  1.2962 -  char shmName[CCHMAXPATH];
  1.2963 -  os2File *pFile = (os2File*)id;
  1.2964 -  
  1.2965 -  *pp = NULL;
  1.2966 -
  1.2967 -  if( !pFile->pShmLink )
  1.2968 -    rc = os2OpenSharedMemory( pFile, szRegion );
  1.2969 -  
  1.2970 -  if( rc == SQLITE_OK ) {
  1.2971 -    pNode = pFile->pShmLink->pShmNode ;
  1.2972 -    
  1.2973 -    sqlite3_mutex_enter(pNode->mutex);
  1.2974 -    
  1.2975 -    assert( szRegion==pNode->szRegion );
  1.2976 -
  1.2977 -    /* Unmapped region ? */
  1.2978 -    if( iRegion >= pNode->nRegion ) {
  1.2979 -      /* Prevent other processes from resizing the shared memory */
  1.2980 -      os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
  1.2981 -
  1.2982 -      apRegion = sqlite3_realloc(
  1.2983 -        pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
  1.2984 -
  1.2985 -      if( apRegion ) {
  1.2986 -        pNode->apRegion = apRegion;
  1.2987 -
  1.2988 -        while( pNode->nRegion <= iRegion ) {
  1.2989 -          sprintf(shmName, "%s-%u", 
  1.2990 -                  pNode->shmBaseName, pNode->nRegion);
  1.2991 -
  1.2992 -          if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName, 
  1.2993 -                PAG_READ | PAG_WRITE) != NO_ERROR ) {
  1.2994 -            if( !bExtend )
  1.2995 -              break;
  1.2996 -
  1.2997 -            if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
  1.2998 -                  PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR && 
  1.2999 -                DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
  1.3000 -                  PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) { 
  1.3001 -              rc = SQLITE_NOMEM;
  1.3002 -              break;
  1.3003 -            }
  1.3004 -          }
  1.3005 -
  1.3006 -          apRegion[pNode->nRegion++] = pvTemp;
  1.3007 -        }
  1.3008 -
  1.3009 -        /* zero out remaining entries */ 
  1.3010 -        for( n = pNode->nRegion; n <= iRegion; n++ )
  1.3011 -          pNode->apRegion[n] = NULL;
  1.3012 -
  1.3013 -        /* Return this region (maybe zero) */
  1.3014 -        *pp = pNode->apRegion[iRegion];
  1.3015 -      } else {
  1.3016 -        rc = SQLITE_NOMEM;
  1.3017 -      }
  1.3018 -
  1.3019 -      /* Allow other processes to resize the shared memory */
  1.3020 -      os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
  1.3021 -      
  1.3022 -    } else {
  1.3023 -      /* Region has been mapped previously */
  1.3024 -      *pp = pNode->apRegion[iRegion];
  1.3025 -    }
  1.3026 -
  1.3027 -    sqlite3_mutex_leave(pNode->mutex);
  1.3028 -  } 
  1.3029 -
  1.3030 -  ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n", 
  1.3031 -                 pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
  1.3032 -          
  1.3033 -  return rc;
  1.3034 -}
  1.3035 -
  1.3036 -/*
  1.3037 -** Close a connection to shared-memory.  Delete the underlying
  1.3038 -** storage if deleteFlag is true.
  1.3039 -**
  1.3040 -** If there is no shared memory associated with the connection then this
  1.3041 -** routine is a harmless no-op.
  1.3042 -*/
  1.3043 -static int os2ShmUnmap(
  1.3044 -  sqlite3_file *id,               /* The underlying database file */
  1.3045 -  int deleteFlag                  /* Delete shared-memory if true */
  1.3046 -){
  1.3047 -  os2File *pFile = (os2File*)id;
  1.3048 -  os2ShmLink *pLink = pFile->pShmLink;
  1.3049 -  
  1.3050 -  if( pLink ) {
  1.3051 -    int nRef = -1;
  1.3052 -    os2ShmLink **ppLink;
  1.3053 -    os2ShmNode *pNode = pLink->pShmNode;
  1.3054 -
  1.3055 -    sqlite3_mutex_enter(pNode->mutex);
  1.3056 -    
  1.3057 -    for( ppLink = &pNode->pFirst;
  1.3058 -         *ppLink && *ppLink != pLink;
  1.3059 -         ppLink = &(*ppLink)->pNext )   ;
  1.3060 -         
  1.3061 -    assert(*ppLink);
  1.3062 -
  1.3063 -    if( *ppLink ) {
  1.3064 -      *ppLink = pLink->pNext;
  1.3065 -      nRef = --pNode->nRef;
  1.3066 -    } else {
  1.3067 -      ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n", 
  1.3068 -                    pNode->shmBaseName))
  1.3069 -    }
  1.3070 -    
  1.3071 -    pFile->pShmLink = NULL;
  1.3072 -    sqlite3_free(pLink);
  1.3073 -
  1.3074 -    sqlite3_mutex_leave(pNode->mutex);
  1.3075 -    
  1.3076 -    if( nRef == 0 )
  1.3077 -      os2PurgeShmNodes( deleteFlag );
  1.3078 -  }
  1.3079 -
  1.3080 -  return SQLITE_OK;
  1.3081 -}
  1.3082 -
  1.3083 -/*
  1.3084 -** Change the lock state for a shared-memory segment.
  1.3085 -**
  1.3086 -** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
  1.3087 -** different here than in posix.  In xShmLock(), one can go from unlocked
  1.3088 -** to shared and back or from unlocked to exclusive and back.  But one may
  1.3089 -** not go from shared to exclusive or from exclusive to shared.
  1.3090 -*/
  1.3091 -static int os2ShmLock(
  1.3092 -  sqlite3_file *id,          /* Database file holding the shared memory */
  1.3093 -  int ofst,                  /* First lock to acquire or release */
  1.3094 -  int n,                     /* Number of locks to acquire or release */
  1.3095 -  int flags                  /* What to do with the lock */
  1.3096 -){
  1.3097 -  u32 mask;                             /* Mask of locks to take or release */
  1.3098 -  int rc = SQLITE_OK;                   /* Result code */
  1.3099 -  os2File *pFile = (os2File*)id;
  1.3100 -  os2ShmLink *p = pFile->pShmLink;      /* The shared memory being locked */
  1.3101 -  os2ShmLink *pX;                       /* For looping over all siblings */
  1.3102 -  os2ShmNode *pShmNode = p->pShmNode;   /* Our node */
  1.3103 -  
  1.3104 -  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
  1.3105 -  assert( n>=1 );
  1.3106 -  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
  1.3107 -       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
  1.3108 -       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
  1.3109 -       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
  1.3110 -  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
  1.3111 -
  1.3112 -  mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
  1.3113 -  assert( n>1 || mask==(1<<ofst) );
  1.3114 -
  1.3115 -
  1.3116 -  sqlite3_mutex_enter(pShmNode->mutex);
  1.3117 -
  1.3118 -  if( flags & SQLITE_SHM_UNLOCK ){
  1.3119 -    u32 allMask = 0; /* Mask of locks held by siblings */
  1.3120 -
  1.3121 -    /* See if any siblings hold this same lock */
  1.3122 -    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
  1.3123 -      if( pX==p ) continue;
  1.3124 -      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
  1.3125 -      allMask |= pX->sharedMask;
  1.3126 -    }
  1.3127 -
  1.3128 -    /* Unlock the system-level locks */
  1.3129 -    if( (mask & allMask)==0 ){
  1.3130 -      rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
  1.3131 -    }else{
  1.3132 -      rc = SQLITE_OK;
  1.3133 -    }
  1.3134 -
  1.3135 -    /* Undo the local locks */
  1.3136 -    if( rc==SQLITE_OK ){
  1.3137 -      p->exclMask &= ~mask;
  1.3138 -      p->sharedMask &= ~mask;
  1.3139 -    } 
  1.3140 -  }else if( flags & SQLITE_SHM_SHARED ){
  1.3141 -    u32 allShared = 0;  /* Union of locks held by connections other than "p" */
  1.3142 -
  1.3143 -    /* Find out which shared locks are already held by sibling connections.
  1.3144 -    ** If any sibling already holds an exclusive lock, go ahead and return
  1.3145 -    ** SQLITE_BUSY.
  1.3146 -    */
  1.3147 -    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
  1.3148 -      if( (pX->exclMask & mask)!=0 ){
  1.3149 -        rc = SQLITE_BUSY;
  1.3150 -        break;
  1.3151 -      }
  1.3152 -      allShared |= pX->sharedMask;
  1.3153 -    }
  1.3154 -
  1.3155 -    /* Get shared locks at the system level, if necessary */
  1.3156 -    if( rc==SQLITE_OK ){
  1.3157 -      if( (allShared & mask)==0 ){
  1.3158 -        rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
  1.3159 -      }else{
  1.3160 -        rc = SQLITE_OK;
  1.3161 -      }
  1.3162 -    }
  1.3163 -
  1.3164 -    /* Get the local shared locks */
  1.3165 -    if( rc==SQLITE_OK ){
  1.3166 -      p->sharedMask |= mask;
  1.3167 -    }
  1.3168 -  }else{
  1.3169 -    /* Make sure no sibling connections hold locks that will block this
  1.3170 -    ** lock.  If any do, return SQLITE_BUSY right away.
  1.3171 -    */
  1.3172 -    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
  1.3173 -      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
  1.3174 -        rc = SQLITE_BUSY;
  1.3175 -        break;
  1.3176 -      }
  1.3177 -    }
  1.3178 -  
  1.3179 -    /* Get the exclusive locks at the system level.  Then if successful
  1.3180 -    ** also mark the local connection as being locked.
  1.3181 -    */
  1.3182 -    if( rc==SQLITE_OK ){
  1.3183 -      rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
  1.3184 -      if( rc==SQLITE_OK ){
  1.3185 -        assert( (p->sharedMask & mask)==0 );
  1.3186 -        p->exclMask |= mask;
  1.3187 -      }
  1.3188 -    }
  1.3189 -  }
  1.3190 -
  1.3191 -  sqlite3_mutex_leave(pShmNode->mutex);
  1.3192 -  
  1.3193 -  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
  1.3194 -           p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
  1.3195 -           rc ? "failed" : "ok"));
  1.3196 -
  1.3197 -  ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n", 
  1.3198 -                 ofst, n, flags, rc))
  1.3199 -                  
  1.3200 -  return rc; 
  1.3201 -}
  1.3202 -
  1.3203 -/*
  1.3204 -** Implement a memory barrier or memory fence on shared memory.
  1.3205 -**
  1.3206 -** All loads and stores begun before the barrier must complete before
  1.3207 -** any load or store begun after the barrier.
  1.3208 -*/
  1.3209 -static void os2ShmBarrier(
  1.3210 -  sqlite3_file *id                /* Database file holding the shared memory */
  1.3211 -){
  1.3212 -  UNUSED_PARAMETER(id);
  1.3213 -  os2ShmEnterMutex();
  1.3214 -  os2ShmLeaveMutex();
  1.3215 -}
  1.3216 -
  1.3217 -#else
  1.3218 -# define os2ShmMap     0
  1.3219 -# define os2ShmLock    0
  1.3220 -# define os2ShmBarrier 0
  1.3221 -# define os2ShmUnmap   0
  1.3222 -#endif /* #ifndef SQLITE_OMIT_WAL */
  1.3223 -
  1.3224 -
  1.3225 -/*
  1.3226 -** This vector defines all the methods that can operate on an
  1.3227 -** sqlite3_file for os2.
  1.3228 -*/
  1.3229 -static const sqlite3_io_methods os2IoMethod = {
  1.3230 -  2,                              /* iVersion */
  1.3231 -  os2Close,                       /* xClose */
  1.3232 -  os2Read,                        /* xRead */
  1.3233 -  os2Write,                       /* xWrite */
  1.3234 -  os2Truncate,                    /* xTruncate */
  1.3235 -  os2Sync,                        /* xSync */
  1.3236 -  os2FileSize,                    /* xFileSize */
  1.3237 -  os2Lock,                        /* xLock */
  1.3238 -  os2Unlock,                      /* xUnlock */
  1.3239 -  os2CheckReservedLock,           /* xCheckReservedLock */
  1.3240 -  os2FileControl,                 /* xFileControl */
  1.3241 -  os2SectorSize,                  /* xSectorSize */
  1.3242 -  os2DeviceCharacteristics,       /* xDeviceCharacteristics */
  1.3243 -  os2ShmMap,                      /* xShmMap */
  1.3244 -  os2ShmLock,                     /* xShmLock */
  1.3245 -  os2ShmBarrier,                  /* xShmBarrier */
  1.3246 -  os2ShmUnmap                     /* xShmUnmap */
  1.3247 -};
  1.3248 -
  1.3249 -
  1.3250 -/***************************************************************************
  1.3251 -** Here ends the I/O methods that form the sqlite3_io_methods object.
  1.3252 -**
  1.3253 -** The next block of code implements the VFS methods.
  1.3254 -****************************************************************************/
  1.3255 -
  1.3256 -/*
  1.3257 -** Create a temporary file name in zBuf.  zBuf must be big enough to
  1.3258 -** hold at pVfs->mxPathname characters.
  1.3259 -*/
  1.3260 -static int getTempname(int nBuf, char *zBuf ){
  1.3261 -  static const char zChars[] =
  1.3262 -    "abcdefghijklmnopqrstuvwxyz"
  1.3263 -    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1.3264 -    "0123456789";
  1.3265 -  int i, j;
  1.3266 -  PSZ zTempPathCp;      
  1.3267 -  char zTempPath[CCHMAXPATH];
  1.3268 -  ULONG ulDriveNum, ulDriveMap;
  1.3269 -  
  1.3270 -  /* It's odd to simulate an io-error here, but really this is just
  1.3271 -  ** using the io-error infrastructure to test that SQLite handles this
  1.3272 -  ** function failing. 
  1.3273 -  */
  1.3274 -  SimulateIOError( return SQLITE_IOERR );
  1.3275 -
  1.3276 -  if( sqlite3_temp_directory ) {
  1.3277 -    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
  1.3278 -  } else if( DosScanEnv( (PSZ)"TEMP",   &zTempPathCp ) == NO_ERROR ||
  1.3279 -             DosScanEnv( (PSZ)"TMP",    &zTempPathCp ) == NO_ERROR ||
  1.3280 -             DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
  1.3281 -    char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
  1.3282 -    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
  1.3283 -    free( zTempPathUTF );
  1.3284 -  } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
  1.3285 -    zTempPath[0] = (char)('A' + ulDriveNum - 1);
  1.3286 -    zTempPath[1] = ':'; 
  1.3287 -    zTempPath[2] = '\0'; 
  1.3288 -  } else {
  1.3289 -    zTempPath[0] = '\0'; 
  1.3290 -  }
  1.3291 -  
  1.3292 -  /* Strip off a trailing slashes or backslashes, otherwise we would get *
  1.3293 -   * multiple (back)slashes which causes DosOpen() to fail.              *
  1.3294 -   * Trailing spaces are not allowed, either.                            */
  1.3295 -  j = sqlite3Strlen30(zTempPath);
  1.3296 -  while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' || 
  1.3297 -                    zTempPath[j-1] == ' ' ) ){
  1.3298 -    j--;
  1.3299 -  }
  1.3300 -  zTempPath[j] = '\0';
  1.3301 -  
  1.3302 -  /* We use 20 bytes to randomize the name */
  1.3303 -  sqlite3_snprintf(nBuf-22, zBuf,
  1.3304 -                   "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
  1.3305 -  j = sqlite3Strlen30(zBuf);
  1.3306 -  sqlite3_randomness( 20, &zBuf[j] );
  1.3307 -  for( i = 0; i < 20; i++, j++ ){
  1.3308 -    zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
  1.3309 -  }
  1.3310 -  zBuf[j] = 0;
  1.3311 -
  1.3312 -  OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
  1.3313 -  return SQLITE_OK;
  1.3314 -}
  1.3315 -
  1.3316 -
  1.3317 -/*
  1.3318 -** Turn a relative pathname into a full pathname.  Write the full
  1.3319 -** pathname into zFull[].  zFull[] will be at least pVfs->mxPathname
  1.3320 -** bytes in size.
  1.3321 -*/
  1.3322 -static int os2FullPathname(
  1.3323 -  sqlite3_vfs *pVfs,          /* Pointer to vfs object */
  1.3324 -  const char *zRelative,      /* Possibly relative input path */
  1.3325 -  int nFull,                  /* Size of output buffer in bytes */
  1.3326 -  char *zFull                 /* Output buffer */
  1.3327 -){
  1.3328 -  char *zRelativeCp = convertUtf8PathToCp( zRelative );
  1.3329 -  char zFullCp[CCHMAXPATH] = "\0";
  1.3330 -  char *zFullUTF;
  1.3331 -  APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME, 
  1.3332 -                                zFullCp, CCHMAXPATH );
  1.3333 -  free( zRelativeCp );
  1.3334 -  zFullUTF = convertCpPathToUtf8( zFullCp );
  1.3335 -  sqlite3_snprintf( nFull, zFull, zFullUTF );
  1.3336 -  free( zFullUTF );
  1.3337 -  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
  1.3338 -}
  1.3339 -
  1.3340 -
  1.3341 -/*
  1.3342 -** Open a file.
  1.3343 -*/
  1.3344 -static int os2Open(
  1.3345 -  sqlite3_vfs *pVfs,            /* Not used */
  1.3346 -  const char *zName,            /* Name of the file (UTF-8) */
  1.3347 -  sqlite3_file *id,             /* Write the SQLite file handle here */
  1.3348 -  int flags,                    /* Open mode flags */
  1.3349 -  int *pOutFlags                /* Status return flags */
  1.3350 -){
  1.3351 -  HFILE h;
  1.3352 -  ULONG ulOpenFlags = 0;
  1.3353 -  ULONG ulOpenMode = 0;
  1.3354 -  ULONG ulAction = 0;
  1.3355 -  ULONG rc;
  1.3356 -  os2File *pFile = (os2File*)id;
  1.3357 -  const char *zUtf8Name = zName;
  1.3358 -  char *zNameCp;
  1.3359 -  char  zTmpname[CCHMAXPATH];
  1.3360 -
  1.3361 -  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
  1.3362 -  int isCreate     = (flags & SQLITE_OPEN_CREATE);
  1.3363 -  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
  1.3364 -#ifndef NDEBUG
  1.3365 -  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
  1.3366 -  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
  1.3367 -  int eType        = (flags & 0xFFFFFF00);
  1.3368 -  int isOpenJournal = (isCreate && (
  1.3369 -        eType==SQLITE_OPEN_MASTER_JOURNAL 
  1.3370 -     || eType==SQLITE_OPEN_MAIN_JOURNAL 
  1.3371 -     || eType==SQLITE_OPEN_WAL
  1.3372 -  ));
  1.3373 -#endif
  1.3374 -
  1.3375 -  UNUSED_PARAMETER(pVfs);
  1.3376 -  assert( id!=0 );
  1.3377 -
  1.3378 -  /* Check the following statements are true: 
  1.3379 -  **
  1.3380 -  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
  1.3381 -  **   (b) if CREATE is set, then READWRITE must also be set, and
  1.3382 -  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
  1.3383 -  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
  1.3384 -  */
  1.3385 -  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
  1.3386 -  assert(isCreate==0 || isReadWrite);
  1.3387 -  assert(isExclusive==0 || isCreate);
  1.3388 -  assert(isDelete==0 || isCreate);
  1.3389 -
  1.3390 -  /* The main DB, main journal, WAL file and master journal are never 
  1.3391 -  ** automatically deleted. Nor are they ever temporary files.  */
  1.3392 -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
  1.3393 -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
  1.3394 -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
  1.3395 -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
  1.3396 -
  1.3397 -  /* Assert that the upper layer has set one of the "file-type" flags. */
  1.3398 -  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
  1.3399 -       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
  1.3400 -       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
  1.3401 -       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
  1.3402 -  );
  1.3403 -
  1.3404 -  memset( pFile, 0, sizeof(*pFile) );
  1.3405 -  pFile->h = (HFILE)-1;
  1.3406 -
  1.3407 -  /* If the second argument to this function is NULL, generate a 
  1.3408 -  ** temporary file name to use 
  1.3409 -  */
  1.3410 -  if( !zUtf8Name ){
  1.3411 -    assert(isDelete && !isOpenJournal);
  1.3412 -    rc = getTempname(CCHMAXPATH, zTmpname);
  1.3413 -    if( rc!=SQLITE_OK ){
  1.3414 -      return rc;
  1.3415 -    }
  1.3416 -    zUtf8Name = zTmpname;
  1.3417 -  }
  1.3418 -
  1.3419 -  if( isReadWrite ){
  1.3420 -    ulOpenMode |= OPEN_ACCESS_READWRITE;
  1.3421 -  }else{
  1.3422 -    ulOpenMode |= OPEN_ACCESS_READONLY;
  1.3423 -  }
  1.3424 -
  1.3425 -  /* Open in random access mode for possibly better speed.  Allow full
  1.3426 -  ** sharing because file locks will provide exclusive access when needed.
  1.3427 -  ** The handle should not be inherited by child processes and we don't 
  1.3428 -  ** want popups from the critical error handler.
  1.3429 -  */
  1.3430 -  ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE | 
  1.3431 -                OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
  1.3432 -
  1.3433 -  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
  1.3434 -  ** created. SQLite doesn't use it to indicate "exclusive access" 
  1.3435 -  ** as it is usually understood.
  1.3436 -  */
  1.3437 -  if( isExclusive ){
  1.3438 -    /* Creates a new file, only if it does not already exist. */
  1.3439 -    /* If the file exists, it fails. */
  1.3440 -    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
  1.3441 -  }else if( isCreate ){
  1.3442 -    /* Open existing file, or create if it doesn't exist */
  1.3443 -    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
  1.3444 -  }else{
  1.3445 -    /* Opens a file, only if it exists. */
  1.3446 -    ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
  1.3447 -  }
  1.3448 -
  1.3449 -  zNameCp = convertUtf8PathToCp( zUtf8Name );
  1.3450 -  rc = DosOpen( (PSZ)zNameCp,
  1.3451 -                &h,
  1.3452 -                &ulAction,
  1.3453 -                0L,
  1.3454 -                FILE_NORMAL,
  1.3455 -                ulOpenFlags,
  1.3456 -                ulOpenMode,
  1.3457 -                (PEAOP2)NULL );
  1.3458 -  free( zNameCp );
  1.3459 -
  1.3460 -  if( rc != NO_ERROR ){
  1.3461 -    OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
  1.3462 -              rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
  1.3463 -
  1.3464 -    if( isReadWrite ){
  1.3465 -      return os2Open( pVfs, zName, id,
  1.3466 -                      ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
  1.3467 -                      pOutFlags );
  1.3468 -    }else{
  1.3469 -      return SQLITE_CANTOPEN;
  1.3470 -    }
  1.3471 -  }
  1.3472 -
  1.3473 -  if( pOutFlags ){
  1.3474 -    *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
  1.3475 -  }
  1.3476 -
  1.3477 -  os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
  1.3478 -  pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
  1.3479 -  pFile->pMethod = &os2IoMethod;
  1.3480 -  pFile->flags = flags;
  1.3481 -  pFile->h = h;
  1.3482 -
  1.3483 -  OpenCounter(+1);
  1.3484 -  OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
  1.3485 -  return SQLITE_OK;
  1.3486 -}
  1.3487 -
  1.3488 -/*
  1.3489 -** Delete the named file.
  1.3490 -*/
  1.3491 -static int os2Delete(
  1.3492 -  sqlite3_vfs *pVfs,                     /* Not used on os2 */
  1.3493 -  const char *zFilename,                 /* Name of file to delete */
  1.3494 -  int syncDir                            /* Not used on os2 */
  1.3495 -){
  1.3496 -  APIRET rc;
  1.3497 -  char *zFilenameCp;
  1.3498 -  SimulateIOError( return SQLITE_IOERR_DELETE );
  1.3499 -  zFilenameCp = convertUtf8PathToCp( zFilename );
  1.3500 -  rc = DosDelete( (PSZ)zFilenameCp );
  1.3501 -  free( zFilenameCp );
  1.3502 -  OSTRACE(( "DELETE \"%s\"\n", zFilename ));
  1.3503 -  return (rc == NO_ERROR ||
  1.3504 -          rc == ERROR_FILE_NOT_FOUND ||
  1.3505 -          rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
  1.3506 -}
  1.3507 -
  1.3508 -/*
  1.3509 -** Check the existance and status of a file.
  1.3510 -*/
  1.3511 -static int os2Access(
  1.3512 -  sqlite3_vfs *pVfs,        /* Not used on os2 */
  1.3513 -  const char *zFilename,    /* Name of file to check */
  1.3514 -  int flags,                /* Type of test to make on this file */
  1.3515 -  int *pOut                 /* Write results here */
  1.3516 -){
  1.3517 -  APIRET rc;
  1.3518 -  FILESTATUS3 fsts3ConfigInfo;
  1.3519 -  char *zFilenameCp;
  1.3520 -
  1.3521 -  UNUSED_PARAMETER(pVfs);
  1.3522 -  SimulateIOError( return SQLITE_IOERR_ACCESS; );
  1.3523 -  
  1.3524 -  zFilenameCp = convertUtf8PathToCp( zFilename );
  1.3525 -  rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
  1.3526 -                         &fsts3ConfigInfo, sizeof(FILESTATUS3) );
  1.3527 -  free( zFilenameCp );
  1.3528 -  OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
  1.3529 -            fsts3ConfigInfo.attrFile, flags, rc ));
  1.3530 -
  1.3531 -  switch( flags ){
  1.3532 -    case SQLITE_ACCESS_EXISTS:
  1.3533 -      /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
  1.3534 -      ** as if it does not exist.
  1.3535 -      */
  1.3536 -      if( fsts3ConfigInfo.cbFile == 0 ) 
  1.3537 -        rc = ERROR_FILE_NOT_FOUND;
  1.3538 -      break;
  1.3539 -    case SQLITE_ACCESS_READ:
  1.3540 -      break;
  1.3541 -    case SQLITE_ACCESS_READWRITE:
  1.3542 -      if( fsts3ConfigInfo.attrFile & FILE_READONLY )
  1.3543 -        rc = ERROR_ACCESS_DENIED;
  1.3544 -      break;
  1.3545 -    default:
  1.3546 -      rc = ERROR_FILE_NOT_FOUND;
  1.3547 -      assert( !"Invalid flags argument" );
  1.3548 -  }
  1.3549 -
  1.3550 -  *pOut = (rc == NO_ERROR);
  1.3551 -  OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
  1.3552 -
  1.3553 -  return SQLITE_OK;
  1.3554 -}
  1.3555 -
  1.3556 -
  1.3557 -#ifndef SQLITE_OMIT_LOAD_EXTENSION
  1.3558 -/*
  1.3559 -** Interfaces for opening a shared library, finding entry points
  1.3560 -** within the shared library, and closing the shared library.
  1.3561 -*/
  1.3562 -/*
  1.3563 -** Interfaces for opening a shared library, finding entry points
  1.3564 -** within the shared library, and closing the shared library.
  1.3565 -*/
  1.3566 -static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
  1.3567 -  HMODULE hmod;
  1.3568 -  APIRET rc;
  1.3569 -  char *zFilenameCp = convertUtf8PathToCp(zFilename);
  1.3570 -  rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
  1.3571 -  free(zFilenameCp);
  1.3572 -  return rc != NO_ERROR ? 0 : (void*)hmod;
  1.3573 -}
  1.3574 -/*
  1.3575 -** A no-op since the error code is returned on the DosLoadModule call.
  1.3576 -** os2Dlopen returns zero if DosLoadModule is not successful.
  1.3577 -*/
  1.3578 -static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
  1.3579 -/* no-op */
  1.3580 -}
  1.3581 -static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
  1.3582 -  PFN pfn;
  1.3583 -  APIRET rc;
  1.3584 -  rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
  1.3585 -  if( rc != NO_ERROR ){
  1.3586 -    /* if the symbol itself was not found, search again for the same
  1.3587 -     * symbol with an extra underscore, that might be needed depending
  1.3588 -     * on the calling convention */
  1.3589 -    char _zSymbol[256] = "_";
  1.3590 -    strncat(_zSymbol, zSymbol, 254);
  1.3591 -    rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
  1.3592 -  }
  1.3593 -  return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
  1.3594 -}
  1.3595 -static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
  1.3596 -  DosFreeModule((HMODULE)pHandle);
  1.3597 -}
  1.3598 -#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
  1.3599 -  #define os2DlOpen 0
  1.3600 -  #define os2DlError 0
  1.3601 -  #define os2DlSym 0
  1.3602 -  #define os2DlClose 0
  1.3603 -#endif
  1.3604 -
  1.3605 -
  1.3606 -/*
  1.3607 -** Write up to nBuf bytes of randomness into zBuf.
  1.3608 -*/
  1.3609 -static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
  1.3610 -  int n = 0;
  1.3611 -#if defined(SQLITE_TEST)
  1.3612 -  n = nBuf;
  1.3613 -  memset(zBuf, 0, nBuf);
  1.3614 -#else
  1.3615 -  int i;                           
  1.3616 -  PPIB ppib;
  1.3617 -  PTIB ptib;
  1.3618 -  DATETIME dt; 
  1.3619 -  static unsigned c = 0;
  1.3620 -  /* Ordered by variation probability */
  1.3621 -  static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
  1.3622 -                            QSV_MAXPRMEM, QSV_MAXSHMEM,
  1.3623 -                            QSV_TOTAVAILMEM, QSV_TOTRESMEM };
  1.3624 -
  1.3625 -  /* 8 bytes; timezone and weekday don't increase the randomness much */
  1.3626 -  if( (int)sizeof(dt)-3 <= nBuf - n ){
  1.3627 -    c += 0x0100;
  1.3628 -    DosGetDateTime(&dt);
  1.3629 -    dt.year = (USHORT)((dt.year - 1900) | c);
  1.3630 -    memcpy(&zBuf[n], &dt, sizeof(dt)-3);
  1.3631 -    n += sizeof(dt)-3;
  1.3632 -  }
  1.3633 -
  1.3634 -  /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
  1.3635 -  if( (int)sizeof(ULONG) <= nBuf - n ){
  1.3636 -    DosGetInfoBlocks(&ptib, &ppib);
  1.3637 -    *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
  1.3638 -                                 ptib->tib_ptib2->tib2_ultid);
  1.3639 -    n += sizeof(ULONG);
  1.3640 -  }
  1.3641 -
  1.3642 -  /* Up to 6 * 4 bytes; variables depend on the system state */
  1.3643 -  for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
  1.3644 -    DosQuerySysInfo(svIdx[i], svIdx[i], 
  1.3645 -                    (PULONG)&zBuf[n], sizeof(ULONG));
  1.3646 -    n += sizeof(ULONG);
  1.3647 -  } 
  1.3648 -#endif
  1.3649 -
  1.3650 -  return n;
  1.3651 -}
  1.3652 -
  1.3653 -/*
  1.3654 -** Sleep for a little while.  Return the amount of time slept.
  1.3655 -** The argument is the number of microseconds we want to sleep.
  1.3656 -** The return value is the number of microseconds of sleep actually
  1.3657 -** requested from the underlying operating system, a number which
  1.3658 -** might be greater than or equal to the argument, but not less
  1.3659 -** than the argument.
  1.3660 -*/
  1.3661 -static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
  1.3662 -  DosSleep( (microsec/1000) );
  1.3663 -  return microsec;
  1.3664 -}
  1.3665 -
  1.3666 -/*
  1.3667 -** The following variable, if set to a non-zero value, becomes the result
  1.3668 -** returned from sqlite3OsCurrentTime().  This is used for testing.
  1.3669 -*/
  1.3670 -#ifdef SQLITE_TEST
  1.3671 -SQLITE_API int sqlite3_current_time = 0;
  1.3672 -#endif
  1.3673 -
  1.3674 -/*
  1.3675 -** Find the current time (in Universal Coordinated Time).  Write into *piNow
  1.3676 -** the current time and date as a Julian Day number times 86_400_000.  In
  1.3677 -** other words, write into *piNow the number of milliseconds since the Julian
  1.3678 -** epoch of noon in Greenwich on November 24, 4714 B.C according to the
  1.3679 -** proleptic Gregorian calendar.
  1.3680 -**
  1.3681 -** On success, return 0.  Return 1 if the time and date cannot be found.
  1.3682 -*/
  1.3683 -static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
  1.3684 -#ifdef SQLITE_TEST
  1.3685 -  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
  1.3686 -#endif
  1.3687 -  int year, month, datepart, timepart;
  1.3688 - 
  1.3689 -  DATETIME dt;
  1.3690 -  DosGetDateTime( &dt );
  1.3691 -
  1.3692 -  year = dt.year;
  1.3693 -  month = dt.month;
  1.3694 -
  1.3695 -  /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
  1.3696 -  ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
  1.3697 -  ** Calculate the Julian days
  1.3698 -  */
  1.3699 -  datepart = (int)dt.day - 32076 +
  1.3700 -    1461*(year + 4800 + (month - 14)/12)/4 +
  1.3701 -    367*(month - 2 - (month - 14)/12*12)/12 -
  1.3702 -    3*((year + 4900 + (month - 14)/12)/100)/4;
  1.3703 -
  1.3704 -  /* Time in milliseconds, hours to noon added */
  1.3705 -  timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
  1.3706 -    ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
  1.3707 -
  1.3708 -  *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
  1.3709 -   
  1.3710 -#ifdef SQLITE_TEST
  1.3711 -  if( sqlite3_current_time ){
  1.3712 -    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
  1.3713 -  }
  1.3714 -#endif
  1.3715 -
  1.3716 -  UNUSED_PARAMETER(pVfs);
  1.3717 -  return 0;
  1.3718 -}
  1.3719 -
  1.3720 -/*
  1.3721 -** Find the current time (in Universal Coordinated Time).  Write the
  1.3722 -** current time and date as a Julian Day number into *prNow and
  1.3723 -** return 0.  Return 1 if the time and date cannot be found.
  1.3724 -*/
  1.3725 -static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
  1.3726 -  int rc;
  1.3727 -  sqlite3_int64 i;
  1.3728 -  rc = os2CurrentTimeInt64(pVfs, &i);
  1.3729 -  if( !rc ){
  1.3730 -    *prNow = i/86400000.0;
  1.3731 -  }
  1.3732 -  return rc;
  1.3733 -}
  1.3734 -
  1.3735 -/*
  1.3736 -** The idea is that this function works like a combination of
  1.3737 -** GetLastError() and FormatMessage() on windows (or errno and
  1.3738 -** strerror_r() on unix). After an error is returned by an OS
  1.3739 -** function, SQLite calls this function with zBuf pointing to
  1.3740 -** a buffer of nBuf bytes. The OS layer should populate the
  1.3741 -** buffer with a nul-terminated UTF-8 encoded error message
  1.3742 -** describing the last IO error to have occurred within the calling
  1.3743 -** thread.
  1.3744 -**
  1.3745 -** If the error message is too large for the supplied buffer,
  1.3746 -** it should be truncated. The return value of xGetLastError
  1.3747 -** is zero if the error message fits in the buffer, or non-zero
  1.3748 -** otherwise (if the message was truncated). If non-zero is returned,
  1.3749 -** then it is not necessary to include the nul-terminator character
  1.3750 -** in the output buffer.
  1.3751 -**
  1.3752 -** Not supplying an error message will have no adverse effect
  1.3753 -** on SQLite. It is fine to have an implementation that never
  1.3754 -** returns an error message:
  1.3755 -**
  1.3756 -**   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  1.3757 -**     assert(zBuf[0]=='\0');
  1.3758 -**     return 0;
  1.3759 -**   }
  1.3760 -**
  1.3761 -** However if an error message is supplied, it will be incorporated
  1.3762 -** by sqlite into the error message available to the user using
  1.3763 -** sqlite3_errmsg(), possibly making IO errors easier to debug.
  1.3764 -*/
  1.3765 -static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  1.3766 -  assert(zBuf[0]=='\0');
  1.3767 -  return 0;
  1.3768 -}
  1.3769 -
  1.3770 -/*
  1.3771 -** Initialize and deinitialize the operating system interface.
  1.3772 -*/
  1.3773 -SQLITE_API int sqlite3_os_init(void){
  1.3774 -  static sqlite3_vfs os2Vfs = {
  1.3775 -    3,                 /* iVersion */
  1.3776 -    sizeof(os2File),   /* szOsFile */
  1.3777 -    CCHMAXPATH,        /* mxPathname */
  1.3778 -    0,                 /* pNext */
  1.3779 -    "os2",             /* zName */
  1.3780 -    0,                 /* pAppData */
  1.3781 -
  1.3782 -    os2Open,           /* xOpen */
  1.3783 -    os2Delete,         /* xDelete */
  1.3784 -    os2Access,         /* xAccess */
  1.3785 -    os2FullPathname,   /* xFullPathname */
  1.3786 -    os2DlOpen,         /* xDlOpen */
  1.3787 -    os2DlError,        /* xDlError */
  1.3788 -    os2DlSym,          /* xDlSym */
  1.3789 -    os2DlClose,        /* xDlClose */
  1.3790 -    os2Randomness,     /* xRandomness */
  1.3791 -    os2Sleep,          /* xSleep */
  1.3792 -    os2CurrentTime,    /* xCurrentTime */
  1.3793 -    os2GetLastError,   /* xGetLastError */
  1.3794 -    os2CurrentTimeInt64, /* xCurrentTimeInt64 */
  1.3795 -    0,                 /* xSetSystemCall */
  1.3796 -    0,                 /* xGetSystemCall */
  1.3797 -    0                  /* xNextSystemCall */
  1.3798 -  };
  1.3799 -  sqlite3_vfs_register(&os2Vfs, 1);
  1.3800 -  initUconvObjects();
  1.3801 -/*  sqlite3OSTrace = 1; */
  1.3802 -  return SQLITE_OK;
  1.3803 -}
  1.3804 -SQLITE_API int sqlite3_os_end(void){
  1.3805 -  freeUconvObjects();
  1.3806 -  return SQLITE_OK;
  1.3807 -}
  1.3808 -
  1.3809 -#endif /* SQLITE_OS_OS2 */
  1.3810 -
  1.3811 -/************** End of os_os2.c **********************************************/
  1.3812  /************** Begin file os_unix.c *****************************************/
  1.3813  /*
  1.3814  ** 2004 May 22
  1.3815 @@ -24966,6 +22714,13 @@
  1.3816  */
  1.3817  #if SQLITE_OS_UNIX              /* This file is used on unix only */
  1.3818  
  1.3819 +/* Use posix_fallocate() if it is available
  1.3820 +*/
  1.3821 +#if !defined(HAVE_POSIX_FALLOCATE) \
  1.3822 +      && (_XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L)
  1.3823 +# define HAVE_POSIX_FALLOCATE 1
  1.3824 +#endif
  1.3825 +
  1.3826  /*
  1.3827  ** There are various methods for file locking used for concurrency
  1.3828  ** control:
  1.3829 @@ -25138,6 +22893,10 @@
  1.3830    const char *zPath;                  /* Name of the file */
  1.3831    unixShm *pShm;                      /* Shared memory segment information */
  1.3832    int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
  1.3833 +#ifdef __QNXNTO__
  1.3834 +  int sectorSize;                     /* Device sector size */
  1.3835 +  int deviceCharacteristics;          /* Precomputed device characteristics */
  1.3836 +#endif
  1.3837  #if SQLITE_ENABLE_LOCKING_STYLE
  1.3838    int openFlags;                      /* The flags specified at open() */
  1.3839  #endif
  1.3840 @@ -25843,9 +23602,9 @@
  1.3841    case EACCES: 
  1.3842      /* EACCES is like EAGAIN during locking operations, but not any other time*/
  1.3843      if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 
  1.3844 -	(sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
  1.3845 -	(sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
  1.3846 -	(sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
  1.3847 +        (sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
  1.3848 +        (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
  1.3849 +        (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
  1.3850        return SQLITE_BUSY;
  1.3851      }
  1.3852      /* else fall through */
  1.3853 @@ -26180,7 +23939,7 @@
  1.3854  ** The first argument passed to the macro should be the error code that
  1.3855  ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
  1.3856  ** The two subsequent arguments should be the name of the OS function that
  1.3857 -** failed (e.g. "unlink", "open") and the the associated file-system path,
  1.3858 +** failed (e.g. "unlink", "open") and the associated file-system path,
  1.3859  ** if any.
  1.3860  */
  1.3861  #define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
  1.3862 @@ -26203,7 +23962,7 @@
  1.3863    zErr = aErr;
  1.3864  
  1.3865    /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
  1.3866 -  ** assume that the system provides the the GNU version of strerror_r() that 
  1.3867 +  ** assume that the system provides the GNU version of strerror_r() that
  1.3868    ** returns a pointer to a buffer containing the error message. That pointer 
  1.3869    ** may point to aErr[], or it may point to some static storage somewhere. 
  1.3870    ** Otherwise, assume that the system provides the POSIX version of 
  1.3871 @@ -26892,7 +24651,7 @@
  1.3872          pInode->eFileLock = NO_LOCK;
  1.3873        }else{
  1.3874          rc = SQLITE_IOERR_UNLOCK;
  1.3875 -	pFile->lastErrno = errno;
  1.3876 +        pFile->lastErrno = errno;
  1.3877          pInode->eFileLock = NO_LOCK;
  1.3878          pFile->eFileLock = NO_LOCK;
  1.3879        }
  1.3880 @@ -26908,7 +24667,7 @@
  1.3881        closePendingFds(pFile);
  1.3882      }
  1.3883    }
  1.3884 -	
  1.3885 +
  1.3886  end_unlock:
  1.3887    unixLeaveMutex();
  1.3888    if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
  1.3889 @@ -27175,7 +24934,7 @@
  1.3890  
  1.3891    assert( pFile );
  1.3892    OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
  1.3893 -	   pFile->eFileLock, getpid()));
  1.3894 +           pFile->eFileLock, getpid()));
  1.3895    assert( eFileLock<=SHARED_LOCK );
  1.3896    
  1.3897    /* no-op if possible */
  1.3898 @@ -27214,13 +24973,13 @@
  1.3899  ** Close a file.  Make sure the lock has been released before closing.
  1.3900  */
  1.3901  static int dotlockClose(sqlite3_file *id) {
  1.3902 -  int rc;
  1.3903 +  int rc = SQLITE_OK;
  1.3904    if( id ){
  1.3905      unixFile *pFile = (unixFile*)id;
  1.3906      dotlockUnlock(id, NO_LOCK);
  1.3907      sqlite3_free(pFile->lockingContext);
  1.3908 -  }
  1.3909 -  rc = closeUnixFile(id);
  1.3910 +    rc = closeUnixFile(id);
  1.3911 +  }
  1.3912    return rc;
  1.3913  }
  1.3914  /****************** End of the dot-file lock implementation *******************
  1.3915 @@ -27424,10 +25183,12 @@
  1.3916  ** Close a file.
  1.3917  */
  1.3918  static int flockClose(sqlite3_file *id) {
  1.3919 +  int rc = SQLITE_OK;
  1.3920    if( id ){
  1.3921      flockUnlock(id, NO_LOCK);
  1.3922 -  }
  1.3923 -  return closeUnixFile(id);
  1.3924 +    rc = closeUnixFile(id);
  1.3925 +  }
  1.3926 +  return rc;
  1.3927  }
  1.3928  
  1.3929  #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
  1.3930 @@ -27562,7 +25323,7 @@
  1.3931    assert( pFile );
  1.3932    assert( pSem );
  1.3933    OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
  1.3934 -	   pFile->eFileLock, getpid()));
  1.3935 +           pFile->eFileLock, getpid()));
  1.3936    assert( eFileLock<=SHARED_LOCK );
  1.3937    
  1.3938    /* no-op if possible */
  1.3939 @@ -28138,6 +25899,8 @@
  1.3940    i64 newOffset;
  1.3941  #endif
  1.3942    TIMER_START;
  1.3943 +  assert( cnt==(cnt&0x1ffff) );
  1.3944 +  cnt &= 0x1ffff;
  1.3945    do{
  1.3946  #if defined(USE_PREAD)
  1.3947      got = osPread(id->h, pBuf, cnt, offset);
  1.3948 @@ -28152,7 +25915,7 @@
  1.3949        if( newOffset == -1 ){
  1.3950          ((unixFile*)id)->lastErrno = errno;
  1.3951        }else{
  1.3952 -        ((unixFile*)id)->lastErrno = 0;			
  1.3953 +        ((unixFile*)id)->lastErrno = 0;
  1.3954        }
  1.3955        return -1;
  1.3956      }
  1.3957 @@ -28227,6 +25990,8 @@
  1.3958  #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
  1.3959    i64 newOffset;
  1.3960  #endif
  1.3961 +  assert( cnt==(cnt&0x1ffff) );
  1.3962 +  cnt &= 0x1ffff;
  1.3963    TIMER_START;
  1.3964  #if defined(USE_PREAD)
  1.3965    do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
  1.3966 @@ -28240,7 +26005,7 @@
  1.3967        if( newOffset == -1 ){
  1.3968          ((unixFile*)id)->lastErrno = errno;
  1.3969        }else{
  1.3970 -        ((unixFile*)id)->lastErrno = 0;			
  1.3971 +        ((unixFile*)id)->lastErrno = 0;
  1.3972        }
  1.3973        return -1;
  1.3974      }
  1.3975 @@ -28692,6 +26457,9 @@
  1.3976    }
  1.3977  }
  1.3978  
  1.3979 +/* Forward declaration */
  1.3980 +static int unixGetTempname(int nBuf, char *zBuf);
  1.3981 +
  1.3982  /*
  1.3983  ** Information and control of an open file handle.
  1.3984  */
  1.3985 @@ -28729,6 +26497,14 @@
  1.3986        *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
  1.3987        return SQLITE_OK;
  1.3988      }
  1.3989 +    case SQLITE_FCNTL_TEMPFILENAME: {
  1.3990 +      char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
  1.3991 +      if( zTFile ){
  1.3992 +        unixGetTempname(pFile->pVfs->mxPathname, zTFile);
  1.3993 +        *(char**)pArg = zTFile;
  1.3994 +      }
  1.3995 +      return SQLITE_OK;
  1.3996 +    }
  1.3997  #ifdef SQLITE_DEBUG
  1.3998      /* The pager calls this method to signal that it has done
  1.3999      ** a rollback and that the database is therefore unchanged and
  1.4000 @@ -28760,10 +26536,92 @@
  1.4001  ** a database and its journal file) that the sector size will be the
  1.4002  ** same for both.
  1.4003  */
  1.4004 -static int unixSectorSize(sqlite3_file *pFile){
  1.4005 -  (void)pFile;
  1.4006 +#ifndef __QNXNTO__ 
  1.4007 +static int unixSectorSize(sqlite3_file *NotUsed){
  1.4008 +  UNUSED_PARAMETER(NotUsed);
  1.4009    return SQLITE_DEFAULT_SECTOR_SIZE;
  1.4010  }
  1.4011 +#endif
  1.4012 +
  1.4013 +/*
  1.4014 +** The following version of unixSectorSize() is optimized for QNX.
  1.4015 +*/
  1.4016 +#ifdef __QNXNTO__
  1.4017 +#include <sys/dcmd_blk.h>
  1.4018 +#include <sys/statvfs.h>
  1.4019 +static int unixSectorSize(sqlite3_file *id){
  1.4020 +  unixFile *pFile = (unixFile*)id;
  1.4021 +  if( pFile->sectorSize == 0 ){
  1.4022 +    struct statvfs fsInfo;
  1.4023 +       
  1.4024 +    /* Set defaults for non-supported filesystems */
  1.4025 +    pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
  1.4026 +    pFile->deviceCharacteristics = 0;
  1.4027 +    if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
  1.4028 +      return pFile->sectorSize;
  1.4029 +    }
  1.4030 +
  1.4031 +    if( !strcmp(fsInfo.f_basetype, "tmp") ) {
  1.4032 +      pFile->sectorSize = fsInfo.f_bsize;
  1.4033 +      pFile->deviceCharacteristics =
  1.4034 +        SQLITE_IOCAP_ATOMIC4K |       /* All ram filesystem writes are atomic */
  1.4035 +        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
  1.4036 +                                      ** the write succeeds */
  1.4037 +        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
  1.4038 +                                      ** so it is ordered */
  1.4039 +        0;
  1.4040 +    }else if( strstr(fsInfo.f_basetype, "etfs") ){
  1.4041 +      pFile->sectorSize = fsInfo.f_bsize;
  1.4042 +      pFile->deviceCharacteristics =
  1.4043 +        /* etfs cluster size writes are atomic */
  1.4044 +        (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
  1.4045 +        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
  1.4046 +                                      ** the write succeeds */
  1.4047 +        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
  1.4048 +                                      ** so it is ordered */
  1.4049 +        0;
  1.4050 +    }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
  1.4051 +      pFile->sectorSize = fsInfo.f_bsize;
  1.4052 +      pFile->deviceCharacteristics =
  1.4053 +        SQLITE_IOCAP_ATOMIC |         /* All filesystem writes are atomic */
  1.4054 +        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
  1.4055 +                                      ** the write succeeds */
  1.4056 +        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
  1.4057 +                                      ** so it is ordered */
  1.4058 +        0;
  1.4059 +    }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
  1.4060 +      pFile->sectorSize = fsInfo.f_bsize;
  1.4061 +      pFile->deviceCharacteristics =
  1.4062 +        /* full bitset of atomics from max sector size and smaller */
  1.4063 +        ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
  1.4064 +        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
  1.4065 +                                      ** so it is ordered */
  1.4066 +        0;
  1.4067 +    }else if( strstr(fsInfo.f_basetype, "dos") ){
  1.4068 +      pFile->sectorSize = fsInfo.f_bsize;
  1.4069 +      pFile->deviceCharacteristics =
  1.4070 +        /* full bitset of atomics from max sector size and smaller */
  1.4071 +        ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
  1.4072 +        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
  1.4073 +                                      ** so it is ordered */
  1.4074 +        0;
  1.4075 +    }else{
  1.4076 +      pFile->deviceCharacteristics =
  1.4077 +        SQLITE_IOCAP_ATOMIC512 |      /* blocks are atomic */
  1.4078 +        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
  1.4079 +                                      ** the write succeeds */
  1.4080 +        0;
  1.4081 +    }
  1.4082 +  }
  1.4083 +  /* Last chance verification.  If the sector size isn't a multiple of 512
  1.4084 +  ** then it isn't valid.*/
  1.4085 +  if( pFile->sectorSize % 512 != 0 ){
  1.4086 +    pFile->deviceCharacteristics = 0;
  1.4087 +    pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
  1.4088 +  }
  1.4089 +  return pFile->sectorSize;
  1.4090 +}
  1.4091 +#endif /* __QNXNTO__ */
  1.4092  
  1.4093  /*
  1.4094  ** Return the device characteristics for the file.
  1.4095 @@ -28780,11 +26638,15 @@
  1.4096  */
  1.4097  static int unixDeviceCharacteristics(sqlite3_file *id){
  1.4098    unixFile *p = (unixFile*)id;
  1.4099 +  int rc = 0;
  1.4100 +#ifdef __QNXNTO__
  1.4101 +  if( p->sectorSize==0 ) unixSectorSize(id);
  1.4102 +  rc = p->deviceCharacteristics;
  1.4103 +#endif
  1.4104    if( p->ctrlFlags & UNIXFILE_PSOW ){
  1.4105 -    return SQLITE_IOCAP_POWERSAFE_OVERWRITE;
  1.4106 -  }else{
  1.4107 -    return 0;
  1.4108 -  }
  1.4109 +    rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
  1.4110 +  }
  1.4111 +  return rc;
  1.4112  }
  1.4113  
  1.4114  #ifndef SQLITE_OMIT_WAL
  1.4115 @@ -29200,11 +27062,19 @@
  1.4116          ** the requested memory region.
  1.4117          */
  1.4118          if( !bExtend ) goto shmpage_out;
  1.4119 +#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
  1.4120 +        if( osFallocate(pShmNode->h, sStat.st_size, nByte)!=0 ){
  1.4121 +          rc = unixLogError(SQLITE_IOERR_SHMSIZE, "fallocate",
  1.4122 +                            pShmNode->zFilename);
  1.4123 +          goto shmpage_out;
  1.4124 +        }
  1.4125 +#else
  1.4126          if( robust_ftruncate(pShmNode->h, nByte) ){
  1.4127            rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
  1.4128                              pShmNode->zFilename);
  1.4129            goto shmpage_out;
  1.4130          }
  1.4131 +#endif
  1.4132        }
  1.4133      }
  1.4134  
  1.4135 @@ -29222,7 +27092,7 @@
  1.4136        if( pShmNode->h>=0 ){
  1.4137          pMem = mmap(0, szRegion,
  1.4138              pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
  1.4139 -            MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
  1.4140 +            MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
  1.4141          );
  1.4142          if( pMem==MAP_FAILED ){
  1.4143            rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
  1.4144 @@ -30406,8 +28276,13 @@
  1.4145    int rc = SQLITE_OK;
  1.4146    UNUSED_PARAMETER(NotUsed);
  1.4147    SimulateIOError(return SQLITE_IOERR_DELETE);
  1.4148 -  if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
  1.4149 -    return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
  1.4150 +  if( osUnlink(zPath)==(-1) ){
  1.4151 +    if( errno==ENOENT ){
  1.4152 +      rc = SQLITE_IOERR_DELETE_NOENT;
  1.4153 +    }else{
  1.4154 +      rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
  1.4155 +    }
  1.4156 +    return rc;
  1.4157    }
  1.4158  #ifndef SQLITE_DISABLE_DIRSYNC
  1.4159    if( (dirSync & 1)!=0 ){
  1.4160 @@ -30754,7 +28629,7 @@
  1.4161  ** address in the shared range is taken for a SHARED lock, the entire
  1.4162  ** shared range is taken for an EXCLUSIVE lock):
  1.4163  **
  1.4164 -**      PENDING_BYTE        0x40000000		   	
  1.4165 +**      PENDING_BYTE        0x40000000
  1.4166  **      RESERVED_BYTE       0x40000001
  1.4167  **      SHARED_RANGE        0x40000002 -> 0x40000200
  1.4168  **
  1.4169 @@ -32253,6 +30128,66 @@
  1.4170  /************** Continuing where we left off in os_win.c *********************/
  1.4171  
  1.4172  /*
  1.4173 +** Compiling and using WAL mode requires several APIs that are only
  1.4174 +** available in Windows platforms based on the NT kernel.
  1.4175 +*/
  1.4176 +#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
  1.4177 +# error "WAL mode requires support from the Windows NT kernel, compile\
  1.4178 + with SQLITE_OMIT_WAL."
  1.4179 +#endif
  1.4180 +
  1.4181 +/*
  1.4182 +** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
  1.4183 +** based on the sub-platform)?
  1.4184 +*/
  1.4185 +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
  1.4186 +#  define SQLITE_WIN32_HAS_ANSI
  1.4187 +#endif
  1.4188 +
  1.4189 +/*
  1.4190 +** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
  1.4191 +** based on the sub-platform)?
  1.4192 +*/
  1.4193 +#if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
  1.4194 +#  define SQLITE_WIN32_HAS_WIDE
  1.4195 +#endif
  1.4196 +
  1.4197 +/*
  1.4198 +** Do we need to manually define the Win32 file mapping APIs for use with WAL
  1.4199 +** mode (e.g. these APIs are available in the Windows CE SDK; however, they
  1.4200 +** are not present in the header file)?
  1.4201 +*/
  1.4202 +#if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL)
  1.4203 +/*
  1.4204 +** Two of the file mapping APIs are different under WinRT.  Figure out which
  1.4205 +** set we need.
  1.4206 +*/
  1.4207 +#if SQLITE_OS_WINRT
  1.4208 +WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
  1.4209 +        LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
  1.4210 +
  1.4211 +WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
  1.4212 +#else
  1.4213 +#if defined(SQLITE_WIN32_HAS_ANSI)
  1.4214 +WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
  1.4215 +        DWORD, DWORD, DWORD, LPCSTR);
  1.4216 +#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
  1.4217 +
  1.4218 +#if defined(SQLITE_WIN32_HAS_WIDE)
  1.4219 +WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
  1.4220 +        DWORD, DWORD, DWORD, LPCWSTR);
  1.4221 +#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
  1.4222 +
  1.4223 +WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
  1.4224 +#endif /* SQLITE_OS_WINRT */
  1.4225 +
  1.4226 +/*
  1.4227 +** This file mapping API is common to both Win32 and WinRT.
  1.4228 +*/
  1.4229 +WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
  1.4230 +#endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */
  1.4231 +
  1.4232 +/*
  1.4233  ** Macro to find the minimum of two numeric values.
  1.4234  */
  1.4235  #ifndef MIN
  1.4236 @@ -32274,9 +30209,11 @@
  1.4237  # define FILE_ATTRIBUTE_MASK     (0x0003FFF7)
  1.4238  #endif
  1.4239  
  1.4240 +#ifndef SQLITE_OMIT_WAL
  1.4241  /* Forward references */
  1.4242  typedef struct winShm winShm;           /* A connection to shared-memory */
  1.4243  typedef struct winShmNode winShmNode;   /* A region of shared-memory */
  1.4244 +#endif
  1.4245  
  1.4246  /*
  1.4247  ** WinCE lacks native support for file locking so we have to fake it
  1.4248 @@ -32304,7 +30241,9 @@
  1.4249    short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
  1.4250    u8 ctrlFlags;           /* Flags.  See WINFILE_* below */
  1.4251    DWORD lastErrno;        /* The Windows errno from the last I/O error */
  1.4252 +#ifndef SQLITE_OMIT_WAL
  1.4253    winShm *pShm;           /* Instance of shared memory on this file */
  1.4254 +#endif
  1.4255    const char *zPath;      /* Full pathname of this file */
  1.4256    int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
  1.4257  #if SQLITE_OS_WINCE
  1.4258 @@ -32330,6 +30269,22 @@
  1.4259  #endif
  1.4260  
  1.4261  /*
  1.4262 + * The value used with sqlite3_win32_set_directory() to specify that
  1.4263 + * the data directory should be changed.
  1.4264 + */
  1.4265 +#ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
  1.4266 +#  define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
  1.4267 +#endif
  1.4268 +
  1.4269 +/*
  1.4270 + * The value used with sqlite3_win32_set_directory() to specify that
  1.4271 + * the temporary directory should be changed.
  1.4272 + */
  1.4273 +#ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
  1.4274 +#  define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
  1.4275 +#endif
  1.4276 +
  1.4277 +/*
  1.4278   * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
  1.4279   * various Win32 API heap functions instead of our own.
  1.4280   */
  1.4281 @@ -32437,14 +30392,6 @@
  1.4282  static int sqlite3_os_type = 0;
  1.4283  #endif
  1.4284  
  1.4285 -#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
  1.4286 -#  define SQLITE_WIN32_HAS_ANSI
  1.4287 -#endif
  1.4288 -
  1.4289 -#if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
  1.4290 -#  define SQLITE_WIN32_HAS_WIDE
  1.4291 -#endif
  1.4292 -
  1.4293  #ifndef SYSCALL
  1.4294  #  define SYSCALL sqlite3_syscall_ptr
  1.4295  #endif
  1.4296 @@ -32516,14 +30463,25 @@
  1.4297  #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
  1.4298          LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
  1.4299  
  1.4300 -#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
  1.4301 +#if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
  1.4302 +        !defined(SQLITE_OMIT_WAL))
  1.4303 +  { "CreateFileMappingA",      (SYSCALL)CreateFileMappingA,      0 },
  1.4304 +#else
  1.4305 +  { "CreateFileMappingA",      (SYSCALL)0,                       0 },
  1.4306 +#endif
  1.4307 +
  1.4308 +#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
  1.4309 +        DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
  1.4310 +
  1.4311 +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
  1.4312 +        !defined(SQLITE_OMIT_WAL))
  1.4313    { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
  1.4314  #else
  1.4315    { "CreateFileMappingW",      (SYSCALL)0,                       0 },
  1.4316  #endif
  1.4317  
  1.4318  #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
  1.4319 -        DWORD,DWORD,DWORD,LPCWSTR))aSyscall[6].pCurrent)
  1.4320 +        DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
  1.4321  
  1.4322  #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
  1.4323    { "CreateMutexW",            (SYSCALL)CreateMutexW,            0 },
  1.4324 @@ -32532,7 +30490,7 @@
  1.4325  #endif
  1.4326  
  1.4327  #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
  1.4328 -        LPCWSTR))aSyscall[7].pCurrent)
  1.4329 +        LPCWSTR))aSyscall[8].pCurrent)
  1.4330  
  1.4331  #if defined(SQLITE_WIN32_HAS_ANSI)
  1.4332    { "DeleteFileA",             (SYSCALL)DeleteFileA,             0 },
  1.4333 @@ -32540,7 +30498,7 @@
  1.4334    { "DeleteFileA",             (SYSCALL)0,                       0 },
  1.4335  #endif
  1.4336  
  1.4337 -#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[8].pCurrent)
  1.4338 +#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
  1.4339  
  1.4340  #if defined(SQLITE_WIN32_HAS_WIDE)
  1.4341    { "DeleteFileW",             (SYSCALL)DeleteFileW,             0 },
  1.4342 @@ -32548,7 +30506,7 @@
  1.4343    { "DeleteFileW",             (SYSCALL)0,                       0 },
  1.4344  #endif
  1.4345  
  1.4346 -#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[9].pCurrent)
  1.4347 +#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
  1.4348  
  1.4349  #if SQLITE_OS_WINCE
  1.4350    { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
  1.4351 @@ -32557,7 +30515,7 @@
  1.4352  #endif
  1.4353  
  1.4354  #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
  1.4355 -        LPFILETIME))aSyscall[10].pCurrent)
  1.4356 +        LPFILETIME))aSyscall[11].pCurrent)
  1.4357  
  1.4358  #if SQLITE_OS_WINCE
  1.4359    { "FileTimeToSystemTime",    (SYSCALL)FileTimeToSystemTime,    0 },
  1.4360 @@ -32566,11 +30524,11 @@
  1.4361  #endif
  1.4362  
  1.4363  #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
  1.4364 -        LPSYSTEMTIME))aSyscall[11].pCurrent)
  1.4365 +        LPSYSTEMTIME))aSyscall[12].pCurrent)
  1.4366  
  1.4367    { "FlushFileBuffers",        (SYSCALL)FlushFileBuffers,        0 },
  1.4368  
  1.4369 -#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[12].pCurrent)
  1.4370 +#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
  1.4371  
  1.4372  #if defined(SQLITE_WIN32_HAS_ANSI)
  1.4373    { "FormatMessageA",          (SYSCALL)FormatMessageA,          0 },
  1.4374 @@ -32579,7 +30537,7 @@
  1.4375  #endif
  1.4376  
  1.4377  #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
  1.4378 -        DWORD,va_list*))aSyscall[13].pCurrent)
  1.4379 +        DWORD,va_list*))aSyscall[14].pCurrent)
  1.4380  
  1.4381  #if defined(SQLITE_WIN32_HAS_WIDE)
  1.4382    { "FormatMessageW",          (SYSCALL)FormatMessageW,          0 },
  1.4383 @@ -32588,15 +30546,19 @@
  1.4384  #endif
  1.4385  
  1.4386  #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
  1.4387 -        DWORD,va_list*))aSyscall[14].pCurrent)
  1.4388 -
  1.4389 +        DWORD,va_list*))aSyscall[15].pCurrent)
  1.4390 +
  1.4391 +#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
  1.4392    { "FreeLibrary",             (SYSCALL)FreeLibrary,             0 },
  1.4393 -
  1.4394 -#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[15].pCurrent)
  1.4395 +#else
  1.4396 +  { "FreeLibrary",             (SYSCALL)0,                       0 },
  1.4397 +#endif
  1.4398 +
  1.4399 +#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
  1.4400  
  1.4401    { "GetCurrentProcessId",     (SYSCALL)GetCurrentProcessId,     0 },
  1.4402  
  1.4403 -#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[16].pCurrent)
  1.4404 +#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
  1.4405  
  1.4406  #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
  1.4407    { "GetDiskFreeSpaceA",       (SYSCALL)GetDiskFreeSpaceA,       0 },
  1.4408 @@ -32605,7 +30567,7 @@
  1.4409  #endif
  1.4410  
  1.4411  #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
  1.4412 -        LPDWORD))aSyscall[17].pCurrent)
  1.4413 +        LPDWORD))aSyscall[18].pCurrent)
  1.4414  
  1.4415  #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
  1.4416    { "GetDiskFreeSpaceW",       (SYSCALL)GetDiskFreeSpaceW,       0 },
  1.4417 @@ -32614,7 +30576,7 @@
  1.4418  #endif
  1.4419  
  1.4420  #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
  1.4421 -        LPDWORD))aSyscall[18].pCurrent)
  1.4422 +        LPDWORD))aSyscall[19].pCurrent)
  1.4423  
  1.4424  #if defined(SQLITE_WIN32_HAS_ANSI)
  1.4425    { "GetFileAttributesA",      (SYSCALL)GetFileAttributesA,      0 },
  1.4426 @@ -32622,7 +30584,7 @@
  1.4427    { "GetFileAttributesA",      (SYSCALL)0,                       0 },
  1.4428  #endif
  1.4429  
  1.4430 -#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[19].pCurrent)
  1.4431 +#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
  1.4432  
  1.4433  #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
  1.4434    { "GetFileAttributesW",      (SYSCALL)GetFileAttributesW,      0 },
  1.4435 @@ -32630,7 +30592,7 @@
  1.4436    { "GetFileAttributesW",      (SYSCALL)0,                       0 },
  1.4437  #endif
  1.4438  
  1.4439 -#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[20].pCurrent)
  1.4440 +#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
  1.4441  
  1.4442  #if defined(SQLITE_WIN32_HAS_WIDE)
  1.4443    { "GetFileAttributesExW",    (SYSCALL)GetFileAttributesExW,    0 },
  1.4444 @@ -32639,7 +30601,7 @@
  1.4445  #endif
  1.4446  
  1.4447  #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
  1.4448 -        LPVOID))aSyscall[21].pCurrent)
  1.4449 +        LPVOID))aSyscall[22].pCurrent)
  1.4450  
  1.4451  #if !SQLITE_OS_WINRT
  1.4452    { "GetFileSize",             (SYSCALL)GetFileSize,             0 },
  1.4453 @@ -32647,7 +30609,7 @@
  1.4454    { "GetFileSize",             (SYSCALL)0,                       0 },
  1.4455  #endif
  1.4456  
  1.4457 -#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[22].pCurrent)
  1.4458 +#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
  1.4459  
  1.4460  #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
  1.4461    { "GetFullPathNameA",        (SYSCALL)GetFullPathNameA,        0 },
  1.4462 @@ -32656,7 +30618,7 @@
  1.4463  #endif
  1.4464  
  1.4465  #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
  1.4466 -        LPSTR*))aSyscall[23].pCurrent)
  1.4467 +        LPSTR*))aSyscall[24].pCurrent)
  1.4468  
  1.4469  #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
  1.4470    { "GetFullPathNameW",        (SYSCALL)GetFullPathNameW,        0 },
  1.4471 @@ -32665,12 +30627,13 @@
  1.4472  #endif
  1.4473  
  1.4474  #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
  1.4475 -        LPWSTR*))aSyscall[24].pCurrent)
  1.4476 +        LPWSTR*))aSyscall[25].pCurrent)
  1.4477  
  1.4478    { "GetLastError",            (SYSCALL)GetLastError,            0 },
  1.4479  
  1.4480 -#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[25].pCurrent)
  1.4481 -
  1.4482 +#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
  1.4483 +
  1.4484 +#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
  1.4485  #if SQLITE_OS_WINCE
  1.4486    /* The GetProcAddressA() routine is only available on Windows CE. */
  1.4487    { "GetProcAddressA",         (SYSCALL)GetProcAddressA,         0 },
  1.4488 @@ -32679,9 +30642,12 @@
  1.4489    ** an ANSI string regardless of the _UNICODE setting */
  1.4490    { "GetProcAddressA",         (SYSCALL)GetProcAddress,          0 },
  1.4491  #endif
  1.4492 +#else
  1.4493 +  { "GetProcAddressA",         (SYSCALL)0,                       0 },
  1.4494 +#endif
  1.4495  
  1.4496  #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
  1.4497 -        LPCSTR))aSyscall[26].pCurrent)
  1.4498 +        LPCSTR))aSyscall[27].pCurrent)
  1.4499  
  1.4500  #if !SQLITE_OS_WINRT
  1.4501    { "GetSystemInfo",           (SYSCALL)GetSystemInfo,           0 },
  1.4502 @@ -32689,11 +30655,11 @@
  1.4503    { "GetSystemInfo",           (SYSCALL)0,                       0 },
  1.4504  #endif
  1.4505  
  1.4506 -#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[27].pCurrent)
  1.4507 +#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
  1.4508  
  1.4509    { "GetSystemTime",           (SYSCALL)GetSystemTime,           0 },
  1.4510  
  1.4511 -#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[28].pCurrent)
  1.4512 +#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
  1.4513  
  1.4514  #if !SQLITE_OS_WINCE
  1.4515    { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
  1.4516 @@ -32702,7 +30668,7 @@
  1.4517  #endif
  1.4518  
  1.4519  #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
  1.4520 -        LPFILETIME))aSyscall[29].pCurrent)
  1.4521 +        LPFILETIME))aSyscall[30].pCurrent)
  1.4522  
  1.4523  #if defined(SQLITE_WIN32_HAS_ANSI)
  1.4524    { "GetTempPathA",            (SYSCALL)GetTempPathA,            0 },
  1.4525 @@ -32710,7 +30676,7 @@
  1.4526    { "GetTempPathA",            (SYSCALL)0,                       0 },
  1.4527  #endif
  1.4528  
  1.4529 -#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[30].pCurrent)
  1.4530 +#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
  1.4531  
  1.4532  #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
  1.4533    { "GetTempPathW",            (SYSCALL)GetTempPathW,            0 },
  1.4534 @@ -32718,7 +30684,7 @@
  1.4535    { "GetTempPathW",            (SYSCALL)0,                       0 },
  1.4536  #endif
  1.4537  
  1.4538 -#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[31].pCurrent)
  1.4539 +#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
  1.4540  
  1.4541  #if !SQLITE_OS_WINRT
  1.4542    { "GetTickCount",            (SYSCALL)GetTickCount,            0 },
  1.4543 @@ -32726,7 +30692,7 @@
  1.4544    { "GetTickCount",            (SYSCALL)0,                       0 },
  1.4545  #endif
  1.4546  
  1.4547 -#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[32].pCurrent)
  1.4548 +#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
  1.4549  
  1.4550  #if defined(SQLITE_WIN32_HAS_ANSI)
  1.4551    { "GetVersionExA",           (SYSCALL)GetVersionExA,           0 },
  1.4552 @@ -32735,12 +30701,12 @@
  1.4553  #endif
  1.4554  
  1.4555  #define osGetVersionExA ((BOOL(WINAPI*)( \
  1.4556 -        LPOSVERSIONINFOA))aSyscall[33].pCurrent)
  1.4557 +        LPOSVERSIONINFOA))aSyscall[34].pCurrent)
  1.4558  
  1.4559    { "HeapAlloc",               (SYSCALL)HeapAlloc,               0 },
  1.4560  
  1.4561  #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
  1.4562 -        SIZE_T))aSyscall[34].pCurrent)
  1.4563 +        SIZE_T))aSyscall[35].pCurrent)
  1.4564  
  1.4565  #if !SQLITE_OS_WINRT
  1.4566    { "HeapCreate",              (SYSCALL)HeapCreate,              0 },
  1.4567 @@ -32749,7 +30715,7 @@
  1.4568  #endif
  1.4569  
  1.4570  #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
  1.4571 -        SIZE_T))aSyscall[35].pCurrent)
  1.4572 +        SIZE_T))aSyscall[36].pCurrent)
  1.4573  
  1.4574  #if !SQLITE_OS_WINRT
  1.4575    { "HeapDestroy",             (SYSCALL)HeapDestroy,             0 },
  1.4576 @@ -32757,21 +30723,21 @@
  1.4577    { "HeapDestroy",             (SYSCALL)0,                       0 },
  1.4578  #endif
  1.4579  
  1.4580 -#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[36].pCurrent)
  1.4581 +#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[37].pCurrent)
  1.4582  
  1.4583    { "HeapFree",                (SYSCALL)HeapFree,                0 },
  1.4584  
  1.4585 -#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[37].pCurrent)
  1.4586 +#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[38].pCurrent)
  1.4587  
  1.4588    { "HeapReAlloc",             (SYSCALL)HeapReAlloc,             0 },
  1.4589  
  1.4590  #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
  1.4591 -        SIZE_T))aSyscall[38].pCurrent)
  1.4592 +        SIZE_T))aSyscall[39].pCurrent)
  1.4593  
  1.4594    { "HeapSize",                (SYSCALL)HeapSize,                0 },
  1.4595  
  1.4596  #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
  1.4597 -        LPCVOID))aSyscall[39].pCurrent)
  1.4598 +        LPCVOID))aSyscall[40].pCurrent)
  1.4599  
  1.4600  #if !SQLITE_OS_WINRT
  1.4601    { "HeapValidate",            (SYSCALL)HeapValidate,            0 },
  1.4602 @@ -32780,23 +30746,24 @@
  1.4603  #endif
  1.4604  
  1.4605  #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
  1.4606 -        LPCVOID))aSyscall[40].pCurrent)
  1.4607 -
  1.4608 -#if defined(SQLITE_WIN32_HAS_ANSI)
  1.4609 +        LPCVOID))aSyscall[41].pCurrent)
  1.4610 +
  1.4611 +#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
  1.4612    { "LoadLibraryA",            (SYSCALL)LoadLibraryA,            0 },
  1.4613  #else
  1.4614    { "LoadLibraryA",            (SYSCALL)0,                       0 },
  1.4615  #endif
  1.4616  
  1.4617 -#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[41].pCurrent)
  1.4618 -
  1.4619 -#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
  1.4620 +#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[42].pCurrent)
  1.4621 +
  1.4622 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
  1.4623 +        !defined(SQLITE_OMIT_LOAD_EXTENSION)
  1.4624    { "LoadLibraryW",            (SYSCALL)LoadLibraryW,            0 },
  1.4625  #else
  1.4626    { "LoadLibraryW",            (SYSCALL)0,                       0 },
  1.4627  #endif
  1.4628  
  1.4629 -#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[42].pCurrent)
  1.4630 +#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[43].pCurrent)
  1.4631  
  1.4632  #if !SQLITE_OS_WINRT
  1.4633    { "LocalFree",               (SYSCALL)LocalFree,               0 },
  1.4634 @@ -32804,7 +30771,7 @@
  1.4635    { "LocalFree",               (SYSCALL)0,                       0 },
  1.4636  #endif
  1.4637  
  1.4638 -#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[43].pCurrent)
  1.4639 +#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[44].pCurrent)
  1.4640  
  1.4641  #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
  1.4642    { "LockFile",                (SYSCALL)LockFile,                0 },
  1.4643 @@ -32814,7 +30781,7 @@
  1.4644  
  1.4645  #ifndef osLockFile
  1.4646  #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
  1.4647 -        DWORD))aSyscall[44].pCurrent)
  1.4648 +        DWORD))aSyscall[45].pCurrent)
  1.4649  #endif
  1.4650  
  1.4651  #if !SQLITE_OS_WINCE
  1.4652 @@ -32825,36 +30792,36 @@
  1.4653  
  1.4654  #ifndef osLockFileEx
  1.4655  #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
  1.4656 -        LPOVERLAPPED))aSyscall[45].pCurrent)
  1.4657 -#endif
  1.4658 -
  1.4659 -#if !SQLITE_OS_WINRT
  1.4660 +        LPOVERLAPPED))aSyscall[46].pCurrent)
  1.4661 +#endif
  1.4662 +
  1.4663 +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
  1.4664    { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
  1.4665  #else
  1.4666    { "MapViewOfFile",           (SYSCALL)0,                       0 },
  1.4667  #endif
  1.4668  
  1.4669  #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
  1.4670 -        SIZE_T))aSyscall[46].pCurrent)
  1.4671 +        SIZE_T))aSyscall[47].pCurrent)
  1.4672  
  1.4673    { "MultiByteToWideChar",     (SYSCALL)MultiByteToWideChar,     0 },
  1.4674  
  1.4675  #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
  1.4676 -        int))aSyscall[47].pCurrent)
  1.4677 +        int))aSyscall[48].pCurrent)
  1.4678  
  1.4679    { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
  1.4680  
  1.4681  #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
  1.4682 -        LARGE_INTEGER*))aSyscall[48].pCurrent)
  1.4683 +        LARGE_INTEGER*))aSyscall[49].pCurrent)
  1.4684  
  1.4685    { "ReadFile",                (SYSCALL)ReadFile,                0 },
  1.4686  
  1.4687  #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
  1.4688 -        LPOVERLAPPED))aSyscall[49].pCurrent)
  1.4689 +        LPOVERLAPPED))aSyscall[50].pCurrent)
  1.4690  
  1.4691    { "SetEndOfFile",            (SYSCALL)SetEndOfFile,            0 },
  1.4692  
  1.4693 -#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[50].pCurrent)
  1.4694 +#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[51].pCurrent)
  1.4695  
  1.4696  #if !SQLITE_OS_WINRT
  1.4697    { "SetFilePointer",          (SYSCALL)SetFilePointer,          0 },
  1.4698 @@ -32863,7 +30830,7 @@
  1.4699  #endif
  1.4700  
  1.4701  #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
  1.4702 -        DWORD))aSyscall[51].pCurrent)
  1.4703 +        DWORD))aSyscall[52].pCurrent)
  1.4704  
  1.4705  #if !SQLITE_OS_WINRT
  1.4706    { "Sleep",                   (SYSCALL)Sleep,                   0 },
  1.4707 @@ -32871,12 +30838,12 @@
  1.4708    { "Sleep",                   (SYSCALL)0,                       0 },
  1.4709  #endif
  1.4710  
  1.4711 -#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[52].pCurrent)
  1.4712 +#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[53].pCurrent)
  1.4713  
  1.4714    { "SystemTimeToFileTime",    (SYSCALL)SystemTimeToFileTime,    0 },
  1.4715  
  1.4716  #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
  1.4717 -        LPFILETIME))aSyscall[53].pCurrent)
  1.4718 +        LPFILETIME))aSyscall[54].pCurrent)
  1.4719  
  1.4720  #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
  1.4721    { "UnlockFile",              (SYSCALL)UnlockFile,              0 },
  1.4722 @@ -32886,7 +30853,7 @@
  1.4723  
  1.4724  #ifndef osUnlockFile
  1.4725  #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
  1.4726 -        DWORD))aSyscall[54].pCurrent)
  1.4727 +        DWORD))aSyscall[55].pCurrent)
  1.4728  #endif
  1.4729  
  1.4730  #if !SQLITE_OS_WINCE
  1.4731 @@ -32896,21 +30863,25 @@
  1.4732  #endif
  1.4733  
  1.4734  #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
  1.4735 -        LPOVERLAPPED))aSyscall[55].pCurrent)
  1.4736 -
  1.4737 +        LPOVERLAPPED))aSyscall[56].pCurrent)
  1.4738 +
  1.4739 +#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
  1.4740    { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
  1.4741 -
  1.4742 -#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[56].pCurrent)
  1.4743 +#else
  1.4744 +  { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
  1.4745 +#endif
  1.4746 +
  1.4747 +#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[57].pCurrent)
  1.4748  
  1.4749    { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
  1.4750  
  1.4751  #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
  1.4752 -        LPCSTR,LPBOOL))aSyscall[57].pCurrent)
  1.4753 +        LPCSTR,LPBOOL))aSyscall[58].pCurrent)
  1.4754  
  1.4755    { "WriteFile",               (SYSCALL)WriteFile,               0 },
  1.4756  
  1.4757  #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
  1.4758 -        LPOVERLAPPED))aSyscall[58].pCurrent)
  1.4759 +        LPOVERLAPPED))aSyscall[59].pCurrent)
  1.4760  
  1.4761  #if SQLITE_OS_WINRT
  1.4762    { "CreateEventExW",          (SYSCALL)CreateEventExW,          0 },
  1.4763 @@ -32919,7 +30890,7 @@
  1.4764  #endif
  1.4765  
  1.4766  #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
  1.4767 -        DWORD,DWORD))aSyscall[59].pCurrent)
  1.4768 +        DWORD,DWORD))aSyscall[60].pCurrent)
  1.4769  
  1.4770  #if !SQLITE_OS_WINRT
  1.4771    { "WaitForSingleObject",     (SYSCALL)WaitForSingleObject,     0 },
  1.4772 @@ -32928,25 +30899,25 @@
  1.4773  #endif
  1.4774  
  1.4775  #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
  1.4776 -        DWORD))aSyscall[60].pCurrent)
  1.4777 -
  1.4778 -#if !SQLITE_OS_WINCE
  1.4779 +        DWORD))aSyscall[61].pCurrent)
  1.4780 +
  1.4781 +#if SQLITE_OS_WINRT
  1.4782    { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
  1.4783  #else
  1.4784    { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
  1.4785  #endif
  1.4786  
  1.4787  #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
  1.4788 -        BOOL))aSyscall[61].pCurrent)
  1.4789 -
  1.4790 -#if !SQLITE_OS_WINCE
  1.4791 +        BOOL))aSyscall[62].pCurrent)
  1.4792 +
  1.4793 +#if SQLITE_OS_WINRT
  1.4794    { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
  1.4795  #else
  1.4796    { "SetFilePointerEx",        (SYSCALL)0,                       0 },
  1.4797  #endif
  1.4798  
  1.4799  #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
  1.4800 -        PLARGE_INTEGER,DWORD))aSyscall[62].pCurrent)
  1.4801 +        PLARGE_INTEGER,DWORD))aSyscall[63].pCurrent)
  1.4802  
  1.4803  #if SQLITE_OS_WINRT
  1.4804    { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
  1.4805 @@ -32955,16 +30926,16 @@
  1.4806  #endif
  1.4807  
  1.4808  #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
  1.4809 -        FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[63].pCurrent)
  1.4810 -
  1.4811 -#if SQLITE_OS_WINRT
  1.4812 +        FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[64].pCurrent)
  1.4813 +
  1.4814 +#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
  1.4815    { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
  1.4816  #else
  1.4817    { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
  1.4818  #endif
  1.4819  
  1.4820  #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
  1.4821 -        SIZE_T))aSyscall[64].pCurrent)
  1.4822 +        SIZE_T))aSyscall[65].pCurrent)
  1.4823  
  1.4824  #if SQLITE_OS_WINRT
  1.4825    { "CreateFile2",             (SYSCALL)CreateFile2,             0 },
  1.4826 @@ -32973,16 +30944,16 @@
  1.4827  #endif
  1.4828  
  1.4829  #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
  1.4830 -        LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[65].pCurrent)
  1.4831 -
  1.4832 -#if SQLITE_OS_WINRT
  1.4833 +        LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[66].pCurrent)
  1.4834 +
  1.4835 +#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
  1.4836    { "LoadPackagedLibrary",     (SYSCALL)LoadPackagedLibrary,     0 },
  1.4837  #else
  1.4838    { "LoadPackagedLibrary",     (SYSCALL)0,                       0 },
  1.4839  #endif
  1.4840  
  1.4841  #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
  1.4842 -        DWORD))aSyscall[66].pCurrent)
  1.4843 +        DWORD))aSyscall[67].pCurrent)
  1.4844  
  1.4845  #if SQLITE_OS_WINRT
  1.4846    { "GetTickCount64",          (SYSCALL)GetTickCount64,          0 },
  1.4847 @@ -32990,7 +30961,7 @@
  1.4848    { "GetTickCount64",          (SYSCALL)0,                       0 },
  1.4849  #endif
  1.4850  
  1.4851 -#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[67].pCurrent)
  1.4852 +#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[68].pCurrent)
  1.4853  
  1.4854  #if SQLITE_OS_WINRT
  1.4855    { "GetNativeSystemInfo",     (SYSCALL)GetNativeSystemInfo,     0 },
  1.4856 @@ -32999,7 +30970,7 @@
  1.4857  #endif
  1.4858  
  1.4859  #define osGetNativeSystemInfo ((VOID(WINAPI*)( \
  1.4860 -        LPSYSTEM_INFO))aSyscall[68].pCurrent)
  1.4861 +        LPSYSTEM_INFO))aSyscall[69].pCurrent)
  1.4862  
  1.4863  #if defined(SQLITE_WIN32_HAS_ANSI)
  1.4864    { "OutputDebugStringA",      (SYSCALL)OutputDebugStringA,      0 },
  1.4865 @@ -33007,7 +30978,7 @@
  1.4866    { "OutputDebugStringA",      (SYSCALL)0,                       0 },
  1.4867  #endif
  1.4868  
  1.4869 -#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[69].pCurrent)
  1.4870 +#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[70].pCurrent)
  1.4871  
  1.4872  #if defined(SQLITE_WIN32_HAS_WIDE)
  1.4873    { "OutputDebugStringW",      (SYSCALL)OutputDebugStringW,      0 },
  1.4874 @@ -33015,20 +30986,20 @@
  1.4875    { "OutputDebugStringW",      (SYSCALL)0,                       0 },
  1.4876  #endif
  1.4877  
  1.4878 -#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[70].pCurrent)
  1.4879 +#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[71].pCurrent)
  1.4880  
  1.4881    { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
  1.4882  
  1.4883 -#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[71].pCurrent)
  1.4884 -
  1.4885 -#if SQLITE_OS_WINRT
  1.4886 +#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[72].pCurrent)
  1.4887 +
  1.4888 +#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
  1.4889    { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
  1.4890  #else
  1.4891    { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
  1.4892  #endif
  1.4893  
  1.4894  #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
  1.4895 -        LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[72].pCurrent)
  1.4896 +        LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[73].pCurrent)
  1.4897  
  1.4898  }; /* End of the overrideable system calls */
  1.4899  
  1.4900 @@ -33186,6 +31157,8 @@
  1.4901  */
  1.4902  #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
  1.4903  # define isNT()  (1)
  1.4904 +#elif !defined(SQLITE_WIN32_HAS_WIDE)
  1.4905 +# define isNT()  (0)
  1.4906  #else
  1.4907    static int isNT(void){
  1.4908      if( sqlite3_os_type==0 ){
  1.4909 @@ -33196,7 +31169,7 @@
  1.4910      }
  1.4911      return sqlite3_os_type==2;
  1.4912    }
  1.4913 -#endif /* SQLITE_OS_WINCE */
  1.4914 +#endif
  1.4915  
  1.4916  #ifdef SQLITE_WIN32_MALLOC
  1.4917  /*
  1.4918 @@ -33406,7 +31379,7 @@
  1.4919    if( nChar==0 ){
  1.4920      return 0;
  1.4921    }
  1.4922 -  zWideFilename = sqlite3_malloc( nChar*sizeof(zWideFilename[0]) );
  1.4923 +  zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
  1.4924    if( zWideFilename==0 ){
  1.4925      return 0;
  1.4926    }
  1.4927 @@ -33431,7 +31404,7 @@
  1.4928    if( nByte == 0 ){
  1.4929      return 0;
  1.4930    }
  1.4931 -  zFilename = sqlite3_malloc( nByte );
  1.4932 +  zFilename = sqlite3MallocZero( nByte );
  1.4933    if( zFilename==0 ){
  1.4934      return 0;
  1.4935    }
  1.4936 @@ -33461,7 +31434,7 @@
  1.4937    if( nByte==0 ){
  1.4938      return 0;
  1.4939    }
  1.4940 -  zMbcsFilename = sqlite3_malloc( nByte*sizeof(zMbcsFilename[0]) );
  1.4941 +  zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
  1.4942    if( zMbcsFilename==0 ){
  1.4943      return 0;
  1.4944    }
  1.4945 @@ -33490,7 +31463,7 @@
  1.4946    if( nByte == 0 ){
  1.4947      return 0;
  1.4948    }
  1.4949 -  zFilename = sqlite3_malloc( nByte );
  1.4950 +  zFilename = sqlite3MallocZero( nByte );
  1.4951    if( zFilename==0 ){
  1.4952      return 0;
  1.4953    }
  1.4954 @@ -33537,6 +31510,42 @@
  1.4955    return zFilenameMbcs;
  1.4956  }
  1.4957  
  1.4958 +/*
  1.4959 +** This function sets the data directory or the temporary directory based on
  1.4960 +** the provided arguments.  The type argument must be 1 in order to set the
  1.4961 +** data directory or 2 in order to set the temporary directory.  The zValue
  1.4962 +** argument is the name of the directory to use.  The return value will be
  1.4963 +** SQLITE_OK if successful.
  1.4964 +*/
  1.4965 +SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
  1.4966 +  char **ppDirectory = 0;
  1.4967 +#ifndef SQLITE_OMIT_AUTOINIT
  1.4968 +  int rc = sqlite3_initialize();
  1.4969 +  if( rc ) return rc;
  1.4970 +#endif
  1.4971 +  if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
  1.4972 +    ppDirectory = &sqlite3_data_directory;
  1.4973 +  }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
  1.4974 +    ppDirectory = &sqlite3_temp_directory;
  1.4975 +  }
  1.4976 +  assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
  1.4977 +          || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
  1.4978 +  );
  1.4979 +  assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
  1.4980 +  if( ppDirectory ){
  1.4981 +    char *zValueUtf8 = 0;
  1.4982 +    if( zValue && zValue[0] ){
  1.4983 +      zValueUtf8 = unicodeToUtf8(zValue);
  1.4984 +      if ( zValueUtf8==0 ){
  1.4985 +        return SQLITE_NOMEM;
  1.4986 +      }
  1.4987 +    }
  1.4988 +    sqlite3_free(*ppDirectory);
  1.4989 +    *ppDirectory = zValueUtf8;
  1.4990 +    return SQLITE_OK;
  1.4991 +  }
  1.4992 +  return SQLITE_ERROR;
  1.4993 +}
  1.4994  
  1.4995  /*
  1.4996  ** The return value of getLastErrorMsg
  1.4997 @@ -33631,7 +31640,7 @@
  1.4998  ** The first argument passed to the macro should be the error code that
  1.4999  ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
  1.5000  ** The two subsequent arguments should be the name of the OS function that
  1.5001 -** failed and the the associated file-system path, if any.
  1.5002 +** failed and the associated file-system path, if any.
  1.5003  */
  1.5004  #define winLogError(a,b,c,d)   winLogErrorAtLine(a,b,c,d,__LINE__)
  1.5005  static int winLogErrorAtLine(
  1.5006 @@ -34153,7 +32162,9 @@
  1.5007    winFile *pFile = (winFile*)id;
  1.5008  
  1.5009    assert( id!=0 );
  1.5010 +#ifndef SQLITE_OMIT_WAL
  1.5011    assert( pFile->pShm==0 );
  1.5012 +#endif
  1.5013    OSTRACE(("CLOSE %d\n", pFile->h));
  1.5014    do{
  1.5015      rc = osCloseHandle(pFile->h);
  1.5016 @@ -34284,7 +32295,8 @@
  1.5017          if( retryIoerr(&nRetry, &lastErrno) ) continue;
  1.5018          break;
  1.5019        }
  1.5020 -      if( nWrite<=0 ){
  1.5021 +      assert( nWrite==0 || nWrite<=(DWORD)nRem );
  1.5022 +      if( nWrite==0 || nWrite>(DWORD)nRem ){
  1.5023          lastErrno = osGetLastError();
  1.5024          break;
  1.5025        }
  1.5026 @@ -34782,6 +32794,9 @@
  1.5027    }
  1.5028  }
  1.5029  
  1.5030 +/* Forward declaration */
  1.5031 +static int getTempname(int nBuf, char *zBuf);
  1.5032 +
  1.5033  /*
  1.5034  ** Control and query of the open file handle.
  1.5035  */
  1.5036 @@ -34842,6 +32857,14 @@
  1.5037        }
  1.5038        return SQLITE_OK;
  1.5039      }
  1.5040 +    case SQLITE_FCNTL_TEMPFILENAME: {
  1.5041 +      char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
  1.5042 +      if( zTFile ){
  1.5043 +        getTempname(pFile->pVfs->mxPathname, zTFile);
  1.5044 +        *(char**)pArg = zTFile;
  1.5045 +      }
  1.5046 +      return SQLITE_OK;
  1.5047 +    }
  1.5048    }
  1.5049    return SQLITE_NOTFOUND;
  1.5050  }
  1.5051 @@ -35099,16 +33122,14 @@
  1.5052    /* Allocate space for the new sqlite3_shm object.  Also speculatively
  1.5053    ** allocate space for a new winShmNode and filename.
  1.5054    */
  1.5055 -  p = sqlite3_malloc( sizeof(*p) );
  1.5056 +  p = sqlite3MallocZero( sizeof(*p) );
  1.5057    if( p==0 ) return SQLITE_IOERR_NOMEM;
  1.5058 -  memset(p, 0, sizeof(*p));
  1.5059    nName = sqlite3Strlen30(pDbFd->zPath);
  1.5060 -  pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 17 );
  1.5061 +  pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
  1.5062    if( pNew==0 ){
  1.5063      sqlite3_free(p);
  1.5064      return SQLITE_IOERR_NOMEM;
  1.5065    }
  1.5066 -  memset(pNew, 0, sizeof(*pNew) + nName + 17);
  1.5067    pNew->zFilename = (char*)&pNew[1];
  1.5068    sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
  1.5069    sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
  1.5070 @@ -35445,17 +33466,21 @@
  1.5071      pShmNode->aRegion = apNew;
  1.5072  
  1.5073      while( pShmNode->nRegion<=iRegion ){
  1.5074 -      HANDLE hMap;                /* file-mapping handle */
  1.5075 +      HANDLE hMap = NULL;         /* file-mapping handle */
  1.5076        void *pMap = 0;             /* Mapped memory region */
  1.5077       
  1.5078  #if SQLITE_OS_WINRT
  1.5079        hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
  1.5080            NULL, PAGE_READWRITE, nByte, NULL
  1.5081        );
  1.5082 -#else
  1.5083 +#elif defined(SQLITE_WIN32_HAS_WIDE)
  1.5084        hMap = osCreateFileMappingW(pShmNode->hFile.h, 
  1.5085            NULL, PAGE_READWRITE, 0, nByte, NULL
  1.5086        );
  1.5087 +#elif defined(SQLITE_WIN32_HAS_ANSI)
  1.5088 +      hMap = osCreateFileMappingA(pShmNode->hFile.h, 
  1.5089 +          NULL, PAGE_READWRITE, 0, nByte, NULL
  1.5090 +      );
  1.5091  #endif
  1.5092        OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
  1.5093                 (int)osGetCurrentProcessId(), pShmNode->nRegion, nByte,
  1.5094 @@ -35755,6 +33780,13 @@
  1.5095    assert( id!=0 );
  1.5096    UNUSED_PARAMETER(pVfs);
  1.5097  
  1.5098 +#if SQLITE_OS_WINRT
  1.5099 +  if( !sqlite3_temp_directory ){
  1.5100 +    sqlite3_log(SQLITE_ERROR,
  1.5101 +        "sqlite3_temp_directory variable should be set for WinRT");
  1.5102 +  }
  1.5103 +#endif
  1.5104 +
  1.5105    pFile->h = INVALID_HANDLE_VALUE;
  1.5106  
  1.5107    /* If the second argument to this function is NULL, generate a 
  1.5108 @@ -35903,7 +33935,9 @@
  1.5109    pFile->h = h;
  1.5110    pFile->lastErrno = NO_ERROR;
  1.5111    pFile->pVfs = pVfs;
  1.5112 +#ifndef SQLITE_OMIT_WAL
  1.5113    pFile->pShm = 0;
  1.5114 +#endif
  1.5115    pFile->zPath = zName;
  1.5116    if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
  1.5117      pFile->ctrlFlags |= WINFILE_PSOW;
  1.5118 @@ -35968,14 +34002,24 @@
  1.5119                                    &sAttrData) ){
  1.5120          attr = sAttrData.dwFileAttributes;
  1.5121        }else{
  1.5122 -        rc = SQLITE_OK; /* Already gone? */
  1.5123 +        lastErrno = osGetLastError();
  1.5124 +        if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){
  1.5125 +          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
  1.5126 +        }else{
  1.5127 +          rc = SQLITE_ERROR;
  1.5128 +        }
  1.5129          break;
  1.5130        }
  1.5131  #else
  1.5132        attr = osGetFileAttributesW(zConverted);
  1.5133  #endif
  1.5134        if ( attr==INVALID_FILE_ATTRIBUTES ){
  1.5135 -        rc = SQLITE_OK; /* Already gone? */
  1.5136 +        lastErrno = osGetLastError();
  1.5137 +        if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){
  1.5138 +          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
  1.5139 +        }else{
  1.5140 +          rc = SQLITE_ERROR;
  1.5141 +        }
  1.5142          break;
  1.5143        }
  1.5144        if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
  1.5145 @@ -35997,7 +34041,12 @@
  1.5146      do {
  1.5147        attr = osGetFileAttributesA(zConverted);
  1.5148        if ( attr==INVALID_FILE_ATTRIBUTES ){
  1.5149 -        rc = SQLITE_OK; /* Already gone? */
  1.5150 +        lastErrno = osGetLastError();
  1.5151 +        if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){
  1.5152 +          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
  1.5153 +        }else{
  1.5154 +          rc = SQLITE_ERROR;
  1.5155 +        }
  1.5156          break;
  1.5157        }
  1.5158        if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
  1.5159 @@ -36015,7 +34064,7 @@
  1.5160      } while(1);
  1.5161    }
  1.5162  #endif
  1.5163 -  if( rc ){
  1.5164 +  if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
  1.5165      rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
  1.5166               "winDelete", zFilename);
  1.5167    }else{
  1.5168 @@ -36066,7 +34115,7 @@
  1.5169        }
  1.5170      }else{
  1.5171        logIoerr(cnt);
  1.5172 -      if( lastErrno!=ERROR_FILE_NOT_FOUND ){
  1.5173 +      if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
  1.5174          winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
  1.5175          sqlite3_free(zConverted);
  1.5176          return SQLITE_IOERR_ACCESS;
  1.5177 @@ -36195,7 +34244,7 @@
  1.5178  #endif
  1.5179  
  1.5180  #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
  1.5181 -  int nByte;
  1.5182 +  DWORD nByte;
  1.5183    void *zConverted;
  1.5184    char *zOut;
  1.5185  
  1.5186 @@ -36229,13 +34278,27 @@
  1.5187    }
  1.5188    if( isNT() ){
  1.5189      LPWSTR zTemp;
  1.5190 -    nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0) + 3;
  1.5191 -    zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
  1.5192 +    nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
  1.5193 +    if( nByte==0 ){
  1.5194 +      winLogError(SQLITE_ERROR, osGetLastError(),
  1.5195 +                  "GetFullPathNameW1", zConverted);
  1.5196 +      sqlite3_free(zConverted);
  1.5197 +      return SQLITE_CANTOPEN_FULLPATH;
  1.5198 +    }
  1.5199 +    nByte += 3;
  1.5200 +    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
  1.5201      if( zTemp==0 ){
  1.5202        sqlite3_free(zConverted);
  1.5203        return SQLITE_IOERR_NOMEM;
  1.5204      }
  1.5205 -    osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
  1.5206 +    nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
  1.5207 +    if( nByte==0 ){
  1.5208 +      winLogError(SQLITE_ERROR, osGetLastError(),
  1.5209 +                  "GetFullPathNameW2", zConverted);
  1.5210 +      sqlite3_free(zConverted);
  1.5211 +      sqlite3_free(zTemp);
  1.5212 +      return SQLITE_CANTOPEN_FULLPATH;
  1.5213 +    }
  1.5214      sqlite3_free(zConverted);
  1.5215      zOut = unicodeToUtf8(zTemp);
  1.5216      sqlite3_free(zTemp);
  1.5217 @@ -36243,13 +34306,27 @@
  1.5218  #ifdef SQLITE_WIN32_HAS_ANSI
  1.5219    else{
  1.5220      char *zTemp;
  1.5221 -    nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
  1.5222 -    zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
  1.5223 +    nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
  1.5224 +    if( nByte==0 ){
  1.5225 +      winLogError(SQLITE_ERROR, osGetLastError(),
  1.5226 +                  "GetFullPathNameA1", zConverted);
  1.5227 +      sqlite3_free(zConverted);
  1.5228 +      return SQLITE_CANTOPEN_FULLPATH;
  1.5229 +    }
  1.5230 +    nByte += 3;
  1.5231 +    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
  1.5232      if( zTemp==0 ){
  1.5233        sqlite3_free(zConverted);
  1.5234        return SQLITE_IOERR_NOMEM;
  1.5235      }
  1.5236 -    osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
  1.5237 +    nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
  1.5238 +    if( nByte==0 ){
  1.5239 +      winLogError(SQLITE_ERROR, osGetLastError(),
  1.5240 +                  "GetFullPathNameA2", zConverted);
  1.5241 +      sqlite3_free(zConverted);
  1.5242 +      sqlite3_free(zTemp);
  1.5243 +      return SQLITE_CANTOPEN_FULLPATH;
  1.5244 +    }
  1.5245      sqlite3_free(zConverted);
  1.5246      zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
  1.5247      sqlite3_free(zTemp);
  1.5248 @@ -36507,7 +34584,7 @@
  1.5249  
  1.5250    /* Double-check that the aSyscall[] array has been constructed
  1.5251    ** correctly.  See ticket [bb3a86e890c8e96ab] */
  1.5252 -  assert( ArraySize(aSyscall)==73 );
  1.5253 +  assert( ArraySize(aSyscall)==74 );
  1.5254  
  1.5255  #ifndef SQLITE_OMIT_WAL
  1.5256    /* get memory map allocation granularity */
  1.5257 @@ -36526,7 +34603,7 @@
  1.5258  
  1.5259  SQLITE_API int sqlite3_os_end(void){ 
  1.5260  #if SQLITE_OS_WINRT
  1.5261 -  if( sleepObj != NULL ){
  1.5262 +  if( sleepObj!=NULL ){
  1.5263      osCloseHandle(sleepObj);
  1.5264      sleepObj = NULL;
  1.5265    }
  1.5266 @@ -36879,10 +34956,9 @@
  1.5267    /* Allocate the Bitvec to be tested and a linear array of
  1.5268    ** bits to act as the reference */
  1.5269    pBitvec = sqlite3BitvecCreate( sz );
  1.5270 -  pV = sqlite3_malloc( (sz+7)/8 + 1 );
  1.5271 +  pV = sqlite3MallocZero( (sz+7)/8 + 1 );
  1.5272    pTmpSpace = sqlite3_malloc(BITVEC_SZ);
  1.5273    if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
  1.5274 -  memset(pV, 0, (sz+7)/8 + 1);
  1.5275  
  1.5276    /* NULL pBitvec tests */
  1.5277    sqlite3BitvecSet(0, 1);
  1.5278 @@ -37966,11 +36042,10 @@
  1.5279  
  1.5280    pcache1LeaveMutex(p->pGroup);
  1.5281    if( p->nHash ){ sqlite3BeginBenignMalloc(); }
  1.5282 -  apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
  1.5283 +  apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
  1.5284    if( p->nHash ){ sqlite3EndBenignMalloc(); }
  1.5285    pcache1EnterMutex(p->pGroup);
  1.5286    if( apNew ){
  1.5287 -    memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
  1.5288      for(i=0; i<p->nHash; i++){
  1.5289        PgHdr1 *pPage;
  1.5290        PgHdr1 *pNext = p->apHash[i];
  1.5291 @@ -38154,9 +36229,8 @@
  1.5292    assert( szExtra < 300 );
  1.5293  
  1.5294    sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
  1.5295 -  pCache = (PCache1 *)sqlite3_malloc(sz);
  1.5296 +  pCache = (PCache1 *)sqlite3MallocZero(sz);
  1.5297    if( pCache ){
  1.5298 -    memset(pCache, 0, sz);
  1.5299      if( separateCache ){
  1.5300        pGroup = (PGroup*)&pCache[1];
  1.5301        pGroup->mxPinned = 10;
  1.5302 @@ -39034,7 +37108,7 @@
  1.5303  }
  1.5304  
  1.5305  /*
  1.5306 -** Check to see if element iRowid was inserted into the the rowset as
  1.5307 +** Check to see if element iRowid was inserted into the rowset as
  1.5308  ** part of any insert batch prior to iBatch.  Return 1 or 0.
  1.5309  **
  1.5310  ** If this is the first test of a new batch and if there exist entires
  1.5311 @@ -39318,7 +37392,7 @@
  1.5312  ** 
  1.5313  ** Definition: Two databases (or the same database at two points it time)
  1.5314  ** are said to be "logically equivalent" if they give the same answer to
  1.5315 -** all queries.  Note in particular the the content of freelist leaf
  1.5316 +** all queries.  Note in particular the content of freelist leaf
  1.5317  ** pages can be changed arbitarily without effecting the logical equivalence
  1.5318  ** of the database.
  1.5319  ** 
  1.5320 @@ -41184,12 +39258,13 @@
  1.5321        ** file should be closed and deleted. If this connection writes to
  1.5322        ** the database file, it will do so using an in-memory journal. 
  1.5323        */
  1.5324 +      int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
  1.5325        assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 
  1.5326             || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
  1.5327             || pPager->journalMode==PAGER_JOURNALMODE_WAL 
  1.5328        );
  1.5329        sqlite3OsClose(pPager->jfd);
  1.5330 -      if( !pPager->tempFile ){
  1.5331 +      if( bDelete ){
  1.5332          rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
  1.5333        }
  1.5334      }
  1.5335 @@ -41753,6 +39828,21 @@
  1.5336  }
  1.5337  
  1.5338  /*
  1.5339 +** Return a sanitized version of the sector-size of OS file pFile. The
  1.5340 +** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
  1.5341 +*/
  1.5342 +SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
  1.5343 +  int iRet = sqlite3OsSectorSize(pFile);
  1.5344 +  if( iRet<32 ){
  1.5345 +    iRet = 512;
  1.5346 +  }else if( iRet>MAX_SECTOR_SIZE ){
  1.5347 +    assert( MAX_SECTOR_SIZE>=512 );
  1.5348 +    iRet = MAX_SECTOR_SIZE;
  1.5349 +  }
  1.5350 +  return iRet;
  1.5351 +}
  1.5352 +
  1.5353 +/*
  1.5354  ** Set the value of the Pager.sectorSize variable for the given
  1.5355  ** pager based on the value returned by the xSectorSize method
  1.5356  ** of the open database file. The sector size will be used used 
  1.5357 @@ -41787,14 +39877,7 @@
  1.5358      ** call will segfault. */
  1.5359      pPager->sectorSize = 512;
  1.5360    }else{
  1.5361 -    pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
  1.5362 -    if( pPager->sectorSize<32 ){
  1.5363 -      pPager->sectorSize = 512;
  1.5364 -    }
  1.5365 -    if( pPager->sectorSize>MAX_SECTOR_SIZE ){
  1.5366 -      assert( MAX_SECTOR_SIZE>=512 );
  1.5367 -      pPager->sectorSize = MAX_SECTOR_SIZE;
  1.5368 -    }
  1.5369 +    pPager->sectorSize = sqlite3SectorSize(pPager->fd);
  1.5370    }
  1.5371  }
  1.5372  
  1.5373 @@ -42394,6 +40477,7 @@
  1.5374      if( rc ) return rc;
  1.5375      if( nPage==0 ){
  1.5376        rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
  1.5377 +      if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
  1.5378        isWal = 0;
  1.5379      }else{
  1.5380        rc = sqlite3OsAccess(
  1.5381 @@ -42711,9 +40795,16 @@
  1.5382    Pager *pPager,                       /* Pager object */
  1.5383    int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
  1.5384    void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
  1.5385 -){  
  1.5386 +){
  1.5387    pPager->xBusyHandler = xBusyHandler;
  1.5388    pPager->pBusyHandlerArg = pBusyHandlerArg;
  1.5389 +
  1.5390 +  if( isOpen(pPager->fd) ){
  1.5391 +    void **ap = (void **)&pPager->xBusyHandler;
  1.5392 +    assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
  1.5393 +    assert( ap[1]==pBusyHandlerArg );
  1.5394 +    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
  1.5395 +  }
  1.5396  }
  1.5397  
  1.5398  /*
  1.5399 @@ -43092,7 +41183,7 @@
  1.5400  **
  1.5401  ** If the Pager.noSync flag is set, then this function is a no-op.
  1.5402  ** Otherwise, the actions required depend on the journal-mode and the 
  1.5403 -** device characteristics of the the file-system, as follows:
  1.5404 +** device characteristics of the file-system, as follows:
  1.5405  **
  1.5406  **   * If the journal file is an in-memory journal file, no action need
  1.5407  **     be taken.
  1.5408 @@ -43691,7 +41782,7 @@
  1.5409      memcpy(pPager->zFilename, zPathname, nPathname);
  1.5410      if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
  1.5411      memcpy(pPager->zJournal, zPathname, nPathname);
  1.5412 -    memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+1);
  1.5413 +    memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
  1.5414      sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
  1.5415  #ifndef SQLITE_OMIT_WAL
  1.5416      pPager->zWal = &pPager->zJournal[nPathname+8+1];
  1.5417 @@ -44893,7 +42984,7 @@
  1.5418  # define DIRECT_MODE isDirectMode
  1.5419  #endif
  1.5420  
  1.5421 -  if( !pPager->changeCountDone && pPager->dbSize>0 ){
  1.5422 +  if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
  1.5423      PgHdr *pPgHdr;                /* Reference to page 1 */
  1.5424  
  1.5425      assert( !pPager->tempFile && isOpen(pPager->fd) );
  1.5426 @@ -45113,7 +43204,7 @@
  1.5427    
  1.5428        /* If this transaction has made the database smaller, then all pages
  1.5429        ** being discarded by the truncation must be written to the journal
  1.5430 -      ** file. This can only happen in auto-vacuum mode.
  1.5431 +      ** file.
  1.5432        **
  1.5433        ** Before reading the pages with page numbers larger than the 
  1.5434        ** current value of Pager.dbSize, set dbSize back to the value
  1.5435 @@ -45121,7 +43212,6 @@
  1.5436        ** calls to sqlite3PagerGet() return zeroed pages instead of 
  1.5437        ** reading data from the database file.
  1.5438        */
  1.5439 -  #ifndef SQLITE_OMIT_AUTOVACUUM
  1.5440        if( pPager->dbSize<pPager->dbOrigSize 
  1.5441         && pPager->journalMode!=PAGER_JOURNALMODE_OFF
  1.5442        ){
  1.5443 @@ -45141,7 +43231,6 @@
  1.5444          }
  1.5445          pPager->dbSize = dbSize;
  1.5446        } 
  1.5447 -  #endif
  1.5448    
  1.5449        /* Write the master journal name into the journal file. If a master 
  1.5450        ** journal file name has already been written to the journal file, 
  1.5451 @@ -46144,6 +44233,8 @@
  1.5452    return rc;
  1.5453  }
  1.5454  
  1.5455 +#endif /* !SQLITE_OMIT_WAL */
  1.5456 +
  1.5457  #ifdef SQLITE_ENABLE_ZIPVFS
  1.5458  /*
  1.5459  ** A read-lock must be held on the pager when this function is called. If
  1.5460 @@ -46173,8 +44264,6 @@
  1.5461  }
  1.5462  #endif /* SQLITE_HAS_CODEC */
  1.5463  
  1.5464 -#endif /* !SQLITE_OMIT_WAL */
  1.5465 -
  1.5466  #endif /* SQLITE_OMIT_DISKIO */
  1.5467  
  1.5468  /************** End of pager.c ***********************************************/
  1.5469 @@ -46323,14 +44412,15 @@
  1.5470  ** byte order of the host computer.
  1.5471  **
  1.5472  ** The purpose of the wal-index is to answer this question quickly:  Given
  1.5473 -** a page number P, return the index of the last frame for page P in the WAL,
  1.5474 -** or return NULL if there are no frames for page P in the WAL.
  1.5475 +** a page number P and a maximum frame index M, return the index of the 
  1.5476 +** last frame in the wal before frame M for page P in the WAL, or return
  1.5477 +** NULL if there are no frames for page P in the WAL prior to M.
  1.5478  **
  1.5479  ** The wal-index consists of a header region, followed by an one or
  1.5480  ** more index blocks.  
  1.5481  **
  1.5482  ** The wal-index header contains the total number of frames within the WAL
  1.5483 -** in the the mxFrame field.  
  1.5484 +** in the mxFrame field.
  1.5485  **
  1.5486  ** Each index block except for the first contains information on 
  1.5487  ** HASHTABLE_NPAGE frames. The first index block contains information on
  1.5488 @@ -47378,6 +45468,7 @@
  1.5489      pInfo->nBackfill = 0;
  1.5490      pInfo->aReadMark[0] = 0;
  1.5491      for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
  1.5492 +    if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
  1.5493  
  1.5494      /* If more than one frame was recovered from the log file, report an
  1.5495      ** event via sqlite3_log(). This is to help with identifying performance
  1.5496 @@ -47878,7 +45969,7 @@
  1.5497        assert( y<=pWal->hdr.mxFrame );
  1.5498        rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
  1.5499        if( rc==SQLITE_OK ){
  1.5500 -        pInfo->aReadMark[i] = READMARK_NOT_USED;
  1.5501 +        pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
  1.5502          walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
  1.5503        }else if( rc==SQLITE_BUSY ){
  1.5504          mxSafeFrame = y;
  1.5505 @@ -48696,7 +46787,7 @@
  1.5506        assert( walFramePgno(pWal, iFrame)!=1 );
  1.5507        rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
  1.5508      }
  1.5509 -    walCleanupHash(pWal);
  1.5510 +    if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
  1.5511    }
  1.5512    assert( rc==SQLITE_OK );
  1.5513    return rc;
  1.5514 @@ -48791,7 +46882,8 @@
  1.5515          aSalt[1] = salt1;
  1.5516          walIndexWriteHdr(pWal);
  1.5517          pInfo->nBackfill = 0;
  1.5518 -        for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
  1.5519 +        pInfo->aReadMark[1] = 0;
  1.5520 +        for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
  1.5521          assert( pInfo->aReadMark[0]==0 );
  1.5522          walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
  1.5523        }else if( rc!=SQLITE_BUSY ){
  1.5524 @@ -49005,7 +47097,7 @@
  1.5525    */
  1.5526    if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
  1.5527      if( pWal->padToSectorBoundary ){
  1.5528 -      int sectorSize = sqlite3OsSectorSize(pWal->pWalFd);
  1.5529 +      int sectorSize = sqlite3SectorSize(pWal->pWalFd);
  1.5530        w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
  1.5531        while( iOffset<w.iSyncPoint ){
  1.5532          rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
  1.5533 @@ -49794,6 +47886,7 @@
  1.5534  #ifndef SQLITE_OMIT_INCRBLOB
  1.5535    u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
  1.5536  #endif
  1.5537 +  u8 hints;                             /* As configured by CursorSetHints() */
  1.5538    i16 iPage;                            /* Index of current page in apPage */
  1.5539    u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
  1.5540    MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
  1.5541 @@ -51678,7 +49771,7 @@
  1.5542        size = get2byte(&data[pc+2]);
  1.5543        if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
  1.5544          /* Free blocks must be in ascending order. And the last byte of
  1.5545 -	** the free-block must lie on the database page.  */
  1.5546 +        ** the free-block must lie on the database page.  */
  1.5547          return SQLITE_CORRUPT_BKPT; 
  1.5548        }
  1.5549        nFree = nFree + size;
  1.5550 @@ -52417,6 +50510,24 @@
  1.5551    return p->pBt->pageSize;
  1.5552  }
  1.5553  
  1.5554 +#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
  1.5555 +/*
  1.5556 +** This function is similar to sqlite3BtreeGetReserve(), except that it
  1.5557 +** may only be called if it is guaranteed that the b-tree mutex is already
  1.5558 +** held.
  1.5559 +**
  1.5560 +** This is useful in one special case in the backup API code where it is
  1.5561 +** known that the shared b-tree mutex is held, but the mutex on the 
  1.5562 +** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
  1.5563 +** were to be called, it might collide with some other operation on the
  1.5564 +** database handle that owns *p, causing undefined behaviour.
  1.5565 +*/
  1.5566 +SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
  1.5567 +  assert( sqlite3_mutex_held(p->pBt->mutex) );
  1.5568 +  return p->pBt->pageSize - p->pBt->usableSize;
  1.5569 +}
  1.5570 +#endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */
  1.5571 +
  1.5572  #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
  1.5573  /*
  1.5574  ** Return the number of bytes of space at the end of every page that
  1.5575 @@ -52731,6 +50842,20 @@
  1.5576  }
  1.5577  
  1.5578  /*
  1.5579 +** Initialize the first page of the database file (creating a database
  1.5580 +** consisting of a single page and no schema objects). Return SQLITE_OK
  1.5581 +** if successful, or an SQLite error code otherwise.
  1.5582 +*/
  1.5583 +SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
  1.5584 +  int rc;
  1.5585 +  sqlite3BtreeEnter(p);
  1.5586 +  p->pBt->nPage = 0;
  1.5587 +  rc = newDatabase(p->pBt);
  1.5588 +  sqlite3BtreeLeave(p);
  1.5589 +  return rc;
  1.5590 +}
  1.5591 +
  1.5592 +/*
  1.5593  ** Attempt to start a new transaction. A write-transaction
  1.5594  ** is started if the second argument is nonzero, otherwise a read-
  1.5595  ** transaction.  If the second argument is 2 or more and exclusive
  1.5596 @@ -52852,7 +50977,7 @@
  1.5597        pBt->nTransaction++;
  1.5598  #ifndef SQLITE_OMIT_SHARED_CACHE
  1.5599        if( p->sharable ){
  1.5600 -	assert( p->lock.pBtree==p && p->lock.iTable==1 );
  1.5601 +        assert( p->lock.pBtree==p && p->lock.iTable==1 );
  1.5602          p->lock.eLock = READ_LOCK;
  1.5603          p->lock.pNext = pBt->pLock;
  1.5604          pBt->pLock = &p->lock;
  1.5605 @@ -55473,7 +53598,7 @@
  1.5606      return SQLITE_OK;  /* No overflow pages. Return without doing anything */
  1.5607    }
  1.5608    if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
  1.5609 -    return SQLITE_CORRUPT;  /* Cell extends past end of page */
  1.5610 +    return SQLITE_CORRUPT_BKPT;  /* Cell extends past end of page */
  1.5611    }
  1.5612    ovflPgno = get4byte(&pCell[info.iOverflow]);
  1.5613    assert( pBt->usableSize > 4 );
  1.5614 @@ -55929,7 +54054,7 @@
  1.5615    assert( pPage->nOverflow==1 );
  1.5616  
  1.5617    /* This error condition is now caught prior to reaching this function */
  1.5618 -  if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
  1.5619 +  if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;
  1.5620  
  1.5621    /* Allocate a new page. This page will become the right-sibling of 
  1.5622    ** pPage. Make the parent page writable, so that the new divider cell
  1.5623 @@ -56139,11 +54264,15 @@
  1.5624  ** If aOvflSpace is set to a null pointer, this function returns 
  1.5625  ** SQLITE_NOMEM.
  1.5626  */
  1.5627 +#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
  1.5628 +#pragma optimize("", off)
  1.5629 +#endif
  1.5630  static int balance_nonroot(
  1.5631    MemPage *pParent,               /* Parent page of siblings being balanced */
  1.5632    int iParentIdx,                 /* Index of "the page" in pParent */
  1.5633    u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
  1.5634 -  int isRoot                      /* True if pParent is a root-page */
  1.5635 +  int isRoot,                     /* True if pParent is a root-page */
  1.5636 +  int bBulk                       /* True if this call is part of a bulk load */
  1.5637  ){
  1.5638    BtShared *pBt;               /* The whole database */
  1.5639    int nCell = 0;               /* Number of cells in apCell[] */
  1.5640 @@ -56207,18 +54336,19 @@
  1.5641    i = pParent->nOverflow + pParent->nCell;
  1.5642    if( i<2 ){
  1.5643      nxDiv = 0;
  1.5644 -    nOld = i+1;
  1.5645 -  }else{
  1.5646 -    nOld = 3;
  1.5647 +  }else{
  1.5648 +    assert( bBulk==0 || bBulk==1 );
  1.5649      if( iParentIdx==0 ){                 
  1.5650        nxDiv = 0;
  1.5651      }else if( iParentIdx==i ){
  1.5652 -      nxDiv = i-2;
  1.5653 -    }else{
  1.5654 +      nxDiv = i-2+bBulk;
  1.5655 +    }else{
  1.5656 +      assert( bBulk==0 );
  1.5657        nxDiv = iParentIdx-1;
  1.5658      }
  1.5659 -    i = 2;
  1.5660 -  }
  1.5661 +    i = 2-bBulk;
  1.5662 +  }
  1.5663 +  nOld = i+1;
  1.5664    if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
  1.5665      pRight = &pParent->aData[pParent->hdrOffset+8];
  1.5666    }else{
  1.5667 @@ -56298,7 +54428,7 @@
  1.5668    /*
  1.5669    ** Load pointers to all cells on sibling pages and the divider cells
  1.5670    ** into the local apCell[] array.  Make copies of the divider cells
  1.5671 -  ** into space obtained from aSpace1[] and remove the the divider Cells
  1.5672 +  ** into space obtained from aSpace1[] and remove the divider cells
  1.5673    ** from pParent.
  1.5674    **
  1.5675    ** If the siblings are on leaf pages, then the child pointers of the
  1.5676 @@ -56427,7 +54557,9 @@
  1.5677      d = r + 1 - leafData;
  1.5678      assert( d<nMaxCells );
  1.5679      assert( r<nMaxCells );
  1.5680 -    while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
  1.5681 +    while( szRight==0 
  1.5682 +       || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) 
  1.5683 +    ){
  1.5684        szRight += szCell[d] + 2;
  1.5685        szLeft -= szCell[r] + 2;
  1.5686        cntNew[i-1]--;
  1.5687 @@ -56474,7 +54606,7 @@
  1.5688        if( rc ) goto balance_cleanup;
  1.5689      }else{
  1.5690        assert( i>0 );
  1.5691 -      rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0);
  1.5692 +      rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
  1.5693        if( rc ) goto balance_cleanup;
  1.5694        apNew[i] = pNew;
  1.5695        nNew++;
  1.5696 @@ -56686,6 +54818,7 @@
  1.5697          ** sibling page j. If the siblings are not leaf pages of an
  1.5698          ** intkey b-tree, then cell i was a divider cell. */
  1.5699          assert( j+1 < ArraySize(apCopy) );
  1.5700 +        assert( j+1 < nOld );
  1.5701          pOld = apCopy[++j];
  1.5702          iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
  1.5703          if( pOld->nOverflow ){
  1.5704 @@ -56764,6 +54897,9 @@
  1.5705  
  1.5706    return rc;
  1.5707  }
  1.5708 +#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
  1.5709 +#pragma optimize("", on)
  1.5710 +#endif
  1.5711  
  1.5712  
  1.5713  /*
  1.5714 @@ -56924,7 +55060,7 @@
  1.5715            ** pSpace buffer passed to the latter call to balance_nonroot().
  1.5716            */
  1.5717            u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
  1.5718 -          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1);
  1.5719 +          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints);
  1.5720            if( pFree ){
  1.5721              /* If pFree is not NULL, it points to the pSpace buffer used 
  1.5722              ** by a previous call to balance_nonroot(). Its contents are
  1.5723 @@ -58512,6 +56648,15 @@
  1.5724    return rc;
  1.5725  }
  1.5726  
  1.5727 +/*
  1.5728 +** set the mask of hint flags for cursor pCsr. Currently the only valid
  1.5729 +** values are 0 and BTREE_BULKLOAD.
  1.5730 +*/
  1.5731 +SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
  1.5732 +  assert( mask==BTREE_BULKLOAD || mask==0 );
  1.5733 +  pCsr->hints = mask;
  1.5734 +}
  1.5735 +
  1.5736  /************** End of btree.c ***********************************************/
  1.5737  /************** Begin file backup.c ******************************************/
  1.5738  /*
  1.5739 @@ -58678,7 +56823,7 @@
  1.5740      ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
  1.5741      ** call to sqlite3_backup_init() and is destroyed by a call to
  1.5742      ** sqlite3_backup_finish(). */
  1.5743 -    p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
  1.5744 +    p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
  1.5745      if( !p ){
  1.5746        sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
  1.5747      }
  1.5748 @@ -58686,7 +56831,6 @@
  1.5749  
  1.5750    /* If the allocation succeeded, populate the new object. */
  1.5751    if( p ){
  1.5752 -    memset(p, 0, sizeof(sqlite3_backup));
  1.5753      p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
  1.5754      p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
  1.5755      p->pDestDb = pDestDb;
  1.5756 @@ -58734,13 +56878,16 @@
  1.5757    const int nCopy = MIN(nSrcPgsz, nDestPgsz);
  1.5758    const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
  1.5759  #ifdef SQLITE_HAS_CODEC
  1.5760 -  int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc);
  1.5761 +  /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
  1.5762 +  ** guaranteed that the shared-mutex is held by this thread, handle
  1.5763 +  ** p->pSrc may not actually be the owner.  */
  1.5764 +  int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
  1.5765    int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
  1.5766  #endif
  1.5767 -
  1.5768    int rc = SQLITE_OK;
  1.5769    i64 iOff;
  1.5770  
  1.5771 +  assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
  1.5772    assert( p->bDestLocked );
  1.5773    assert( !isFatalError(p->rc) );
  1.5774    assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
  1.5775 @@ -58925,7 +57072,13 @@
  1.5776      ** same schema version.
  1.5777      */
  1.5778      if( rc==SQLITE_DONE ){
  1.5779 -      rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
  1.5780 +      if( nSrcPage==0 ){
  1.5781 +        rc = sqlite3BtreeNewDb(p->pDest);
  1.5782 +        nSrcPage = 1;
  1.5783 +      }
  1.5784 +      if( rc==SQLITE_OK || rc==SQLITE_DONE ){
  1.5785 +        rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
  1.5786 +      }
  1.5787        if( rc==SQLITE_OK ){
  1.5788          if( p->pDestDb ){
  1.5789            sqlite3ResetAllSchemasOfConnection(p->pDestDb);
  1.5790 @@ -58959,6 +57112,7 @@
  1.5791          }else{
  1.5792            nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
  1.5793          }
  1.5794 +        assert( nDestTruncate>0 );
  1.5795          sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
  1.5796  
  1.5797          if( pgszSrc<pgszDest ){
  1.5798 @@ -58977,7 +57131,8 @@
  1.5799            i64 iEnd;
  1.5800  
  1.5801            assert( pFile );
  1.5802 -          assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
  1.5803 +          assert( nDestTruncate==0 
  1.5804 +              || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
  1.5805                  nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
  1.5806               && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
  1.5807            ));
  1.5808 @@ -59057,14 +57212,14 @@
  1.5809  */
  1.5810  SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
  1.5811    sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
  1.5812 -  MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */
  1.5813 +  sqlite3 *pSrcDb;                     /* Source database connection */
  1.5814    int rc;                              /* Value to return */
  1.5815  
  1.5816    /* Enter the mutexes */
  1.5817    if( p==0 ) return SQLITE_OK;
  1.5818 -  sqlite3_mutex_enter(p->pSrcDb->mutex);
  1.5819 +  pSrcDb = p->pSrcDb;
  1.5820 +  sqlite3_mutex_enter(pSrcDb->mutex);
  1.5821    sqlite3BtreeEnter(p->pSrc);
  1.5822 -  MUTEX_LOGIC( mutex = p->pSrcDb->mutex; )
  1.5823    if( p->pDestDb ){
  1.5824      sqlite3_mutex_enter(p->pDestDb->mutex);
  1.5825    }
  1.5826 @@ -59090,7 +57245,7 @@
  1.5827  
  1.5828    /* Exit the mutexes and free the backup context structure. */
  1.5829    if( p->pDestDb ){
  1.5830 -    sqlite3_mutex_leave(p->pDestDb->mutex);
  1.5831 +    sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
  1.5832    }
  1.5833    sqlite3BtreeLeave(p->pSrc);
  1.5834    if( p->pDestDb ){
  1.5835 @@ -59099,7 +57254,7 @@
  1.5836      ** sqlite3_backup_finish(). */
  1.5837      sqlite3_free(p);
  1.5838    }
  1.5839 -  sqlite3_mutex_leave(mutex);
  1.5840 +  sqlite3LeaveMutexAndCloseZombie(pSrcDb);
  1.5841    return rc;
  1.5842  }
  1.5843  
  1.5844 @@ -60447,7 +58602,7 @@
  1.5845  SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
  1.5846    assert( isPrepareV2==1 || isPrepareV2==0 );
  1.5847    if( p==0 ) return;
  1.5848 -#ifdef SQLITE_OMIT_TRACE
  1.5849 +#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
  1.5850    if( !isPrepareV2 ) return;
  1.5851  #endif
  1.5852    assert( p->zSql==0 );
  1.5853 @@ -61117,6 +59272,7 @@
  1.5854      addr = p->nOp - 1;
  1.5855    }
  1.5856    pOp = &p->aOp[addr];
  1.5857 +  assert( pOp->p4type==P4_NOTUSED || pOp->p4type==P4_INT32 );
  1.5858    freeP4(db, pOp->p4type, pOp->p4.p);
  1.5859    pOp->p4.p = 0;
  1.5860    if( n==P4_INT32 ){
  1.5861 @@ -61139,10 +59295,9 @@
  1.5862        u8 *aSortOrder;
  1.5863        memcpy((char*)pKeyInfo, zP4, nByte - nField);
  1.5864        aSortOrder = pKeyInfo->aSortOrder;
  1.5865 -      if( aSortOrder ){
  1.5866 -        pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
  1.5867 -        memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
  1.5868 -      }
  1.5869 +      assert( aSortOrder!=0 );
  1.5870 +      pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
  1.5871 +      memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
  1.5872        pOp->p4type = P4_KEYINFO;
  1.5873      }else{
  1.5874        p->db->mallocFailed = 1;
  1.5875 @@ -61168,7 +59323,7 @@
  1.5876  
  1.5877  #ifndef NDEBUG
  1.5878  /*
  1.5879 -** Change the comment on the the most recently coded instruction.  Or
  1.5880 +** Change the comment on the most recently coded instruction.  Or
  1.5881  ** insert a No-op and add the comment to that new instruction.  This
  1.5882  ** makes the code easier to read during debugging.  None of this happens
  1.5883  ** in a production build.
  1.5884 @@ -61255,26 +59410,23 @@
  1.5885      case P4_KEYINFO: {
  1.5886        int i, j;
  1.5887        KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
  1.5888 +      assert( pKeyInfo->aSortOrder!=0 );
  1.5889        sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
  1.5890        i = sqlite3Strlen30(zTemp);
  1.5891        for(j=0; j<pKeyInfo->nField; j++){
  1.5892          CollSeq *pColl = pKeyInfo->aColl[j];
  1.5893 -        if( pColl ){
  1.5894 -          int n = sqlite3Strlen30(pColl->zName);
  1.5895 -          if( i+n>nTemp-6 ){
  1.5896 -            memcpy(&zTemp[i],",...",4);
  1.5897 -            break;
  1.5898 -          }
  1.5899 -          zTemp[i++] = ',';
  1.5900 -          if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
  1.5901 -            zTemp[i++] = '-';
  1.5902 -          }
  1.5903 -          memcpy(&zTemp[i], pColl->zName,n+1);
  1.5904 -          i += n;
  1.5905 -        }else if( i+4<nTemp-6 ){
  1.5906 -          memcpy(&zTemp[i],",nil",4);
  1.5907 -          i += 4;
  1.5908 -        }
  1.5909 +        const char *zColl = pColl ? pColl->zName : "nil";
  1.5910 +        int n = sqlite3Strlen30(zColl);
  1.5911 +        if( i+n>nTemp-6 ){
  1.5912 +          memcpy(&zTemp[i],",...",4);
  1.5913 +          break;
  1.5914 +        }
  1.5915 +        zTemp[i++] = ',';
  1.5916 +        if( pKeyInfo->aSortOrder[j] ){
  1.5917 +          zTemp[i++] = '-';
  1.5918 +        }
  1.5919 +        memcpy(&zTemp[i], zColl, n+1);
  1.5920 +        i += n;
  1.5921        }
  1.5922        zTemp[i++] = ')';
  1.5923        zTemp[i] = 0;
  1.5924 @@ -62164,7 +60316,9 @@
  1.5925      if( sqlite3BtreeIsInTrans(pBt) ){
  1.5926        needXcommit = 1;
  1.5927        if( i!=1 ) nTrans++;
  1.5928 +      sqlite3BtreeEnter(pBt);
  1.5929        rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
  1.5930 +      sqlite3BtreeLeave(pBt);
  1.5931      }
  1.5932    }
  1.5933    if( rc!=SQLITE_OK ){
  1.5934 @@ -62718,6 +60872,27 @@
  1.5935    return rc;
  1.5936  }
  1.5937  
  1.5938 +#ifdef SQLITE_ENABLE_SQLLOG
  1.5939 +/*
  1.5940 +** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, 
  1.5941 +** invoke it.
  1.5942 +*/
  1.5943 +static void vdbeInvokeSqllog(Vdbe *v){
  1.5944 +  if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
  1.5945 +    char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
  1.5946 +    assert( v->db->init.busy==0 );
  1.5947 +    if( zExpanded ){
  1.5948 +      sqlite3GlobalConfig.xSqllog(
  1.5949 +          sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
  1.5950 +      );
  1.5951 +      sqlite3DbFree(v->db, zExpanded);
  1.5952 +    }
  1.5953 +  }
  1.5954 +}
  1.5955 +#else
  1.5956 +# define vdbeInvokeSqllog(x)
  1.5957 +#endif
  1.5958 +
  1.5959  /*
  1.5960  ** Clean up a VDBE after execution but do not delete the VDBE just yet.
  1.5961  ** Write any error messages into *pzErrMsg.  Return the result code.
  1.5962 @@ -62745,6 +60920,7 @@
  1.5963    ** instructions yet, leave the main database error information unchanged.
  1.5964    */
  1.5965    if( p->pc>=0 ){
  1.5966 +    vdbeInvokeSqllog(p);
  1.5967      sqlite3VdbeTransferError(p);
  1.5968      sqlite3DbFree(db, p->zErrMsg);
  1.5969      p->zErrMsg = 0;
  1.5970 @@ -62826,12 +61002,14 @@
  1.5971  }
  1.5972  
  1.5973  /*
  1.5974 -** Free all memory associated with the Vdbe passed as the second argument.
  1.5975 +** Free all memory associated with the Vdbe passed as the second argument,
  1.5976 +** except for object itself, which is preserved.
  1.5977 +**
  1.5978  ** The difference between this function and sqlite3VdbeDelete() is that
  1.5979  ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
  1.5980 -** the database connection.
  1.5981 -*/
  1.5982 -SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
  1.5983 +** the database connection and frees the object itself.
  1.5984 +*/
  1.5985 +SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
  1.5986    SubProgram *pSub, *pNext;
  1.5987    int i;
  1.5988    assert( p->db==0 || p->db==db );
  1.5989 @@ -62849,10 +61027,9 @@
  1.5990    sqlite3DbFree(db, p->zSql);
  1.5991    sqlite3DbFree(db, p->pFree);
  1.5992  #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
  1.5993 -  sqlite3DbFree(db, p->zExplain);
  1.5994 +  sqlite3_free(p->zExplain);
  1.5995    sqlite3DbFree(db, p->pExplain);
  1.5996  #endif
  1.5997 -  sqlite3DbFree(db, p);
  1.5998  }
  1.5999  
  1.6000  /*
  1.6001 @@ -62863,6 +61040,8 @@
  1.6002  
  1.6003    if( NEVER(p==0) ) return;
  1.6004    db = p->db;
  1.6005 +  assert( sqlite3_mutex_held(db->mutex) );
  1.6006 +  sqlite3VdbeClearObject(db, p);
  1.6007    if( p->pPrev ){
  1.6008      p->pPrev->pNext = p->pNext;
  1.6009    }else{
  1.6010 @@ -62874,7 +61053,7 @@
  1.6011    }
  1.6012    p->magic = VDBE_MAGIC_DEAD;
  1.6013    p->db = 0;
  1.6014 -  sqlite3VdbeDeleteObject(db, p);
  1.6015 +  sqlite3DbFree(db, p);
  1.6016  }
  1.6017  
  1.6018  /*
  1.6019 @@ -62976,9 +61155,6 @@
  1.6020  #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
  1.6021      i64 i = pMem->u.i;
  1.6022      u64 u;
  1.6023 -    if( file_format>=4 && (i&1)==i ){
  1.6024 -      return 8+(u32)i;
  1.6025 -    }
  1.6026      if( i<0 ){
  1.6027        if( i<(-MAX_6BYTE) ) return 6;
  1.6028        /* Previous test prevents:  u = -(-9223372036854775808) */
  1.6029 @@ -62986,7 +61162,9 @@
  1.6030      }else{
  1.6031        u = i;
  1.6032      }
  1.6033 -    if( u<=127 ) return 1;
  1.6034 +    if( u<=127 ){
  1.6035 +      return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
  1.6036 +    }
  1.6037      if( u<=32767 ) return 2;
  1.6038      if( u<=8388607 ) return 3;
  1.6039      if( u<=2147483647 ) return 4;
  1.6040 @@ -63271,6 +61449,7 @@
  1.6041    }
  1.6042  
  1.6043    p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
  1.6044 +  assert( pKeyInfo->aSortOrder!=0 );
  1.6045    p->pKeyInfo = pKeyInfo;
  1.6046    p->nField = pKeyInfo->nField + 1;
  1.6047    return p;
  1.6048 @@ -63364,6 +61543,7 @@
  1.6049    idx1 = getVarint32(aKey1, szHdr1);
  1.6050    d1 = szHdr1;
  1.6051    nField = pKeyInfo->nField;
  1.6052 +  assert( pKeyInfo->aSortOrder!=0 );
  1.6053    while( idx1<szHdr1 && i<pPKey2->nField ){
  1.6054      u32 serial_type1;
  1.6055  
  1.6056 @@ -63383,7 +61563,7 @@
  1.6057        assert( mem1.zMalloc==0 );  /* See comment below */
  1.6058  
  1.6059        /* Invert the result if we are using DESC sort order. */
  1.6060 -      if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
  1.6061 +      if( i<nField && pKeyInfo->aSortOrder[i] ){
  1.6062          rc = -rc;
  1.6063        }
  1.6064      
  1.6065 @@ -63702,17 +61882,11 @@
  1.6066    }else{
  1.6067      Vdbe *v = (Vdbe*)pStmt;
  1.6068      sqlite3 *db = v->db;
  1.6069 -#if SQLITE_THREADSAFE
  1.6070 -    sqlite3_mutex *mutex;
  1.6071 -#endif
  1.6072      if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
  1.6073 -#if SQLITE_THREADSAFE
  1.6074 -    mutex = v->db->mutex;
  1.6075 -#endif
  1.6076 -    sqlite3_mutex_enter(mutex);
  1.6077 +    sqlite3_mutex_enter(db->mutex);
  1.6078      rc = sqlite3VdbeFinalize(v);
  1.6079      rc = sqlite3ApiExit(db, rc);
  1.6080 -    sqlite3_mutex_leave(mutex);
  1.6081 +    sqlite3LeaveMutexAndCloseZombie(db);
  1.6082    }
  1.6083    return rc;
  1.6084  }
  1.6085 @@ -64115,10 +62289,12 @@
  1.6086    }
  1.6087    db = v->db;
  1.6088    sqlite3_mutex_enter(db->mutex);
  1.6089 +  v->doingRerun = 0;
  1.6090    while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
  1.6091           && cnt++ < SQLITE_MAX_SCHEMA_RETRY
  1.6092           && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
  1.6093      sqlite3_reset(pStmt);
  1.6094 +    v->doingRerun = 1;
  1.6095      assert( v->expired==0 );
  1.6096    }
  1.6097    if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
  1.6098 @@ -65115,9 +63291,8 @@
  1.6099    if( pVdbe ){
  1.6100      Explain *p;
  1.6101      sqlite3BeginBenignMalloc();
  1.6102 -    p = sqlite3_malloc( sizeof(Explain) );
  1.6103 +    p = (Explain *)sqlite3MallocZero( sizeof(Explain) );
  1.6104      if( p ){
  1.6105 -      memset(p, 0, sizeof(*p));
  1.6106        p->pVdbe = pVdbe;
  1.6107        sqlite3_free(pVdbe->pExplain);
  1.6108        pVdbe->pExplain = p;
  1.6109 @@ -65641,7 +63816,9 @@
  1.6110  ** Print the value of a register for tracing purposes:
  1.6111  */
  1.6112  static void memTracePrint(FILE *out, Mem *p){
  1.6113 -  if( p->flags & MEM_Null ){
  1.6114 +  if( p->flags & MEM_Invalid ){
  1.6115 +    fprintf(out, " undefined");
  1.6116 +  }else if( p->flags & MEM_Null ){
  1.6117      fprintf(out, " NULL");
  1.6118    }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
  1.6119      fprintf(out, " si:%lld", p->u.i);
  1.6120 @@ -65892,6 +64069,7 @@
  1.6121      } aa;
  1.6122      struct OP_Null_stack_vars {
  1.6123        int cnt;
  1.6124 +      u16 nullFlag;
  1.6125      } ab;
  1.6126      struct OP_Variable_stack_vars {
  1.6127        Mem *pVar;       /* Value being transferred */
  1.6128 @@ -65902,39 +64080,43 @@
  1.6129        int p1;          /* Register to copy from */
  1.6130        int p2;          /* Register to copy to */
  1.6131      } ad;
  1.6132 +    struct OP_Copy_stack_vars {
  1.6133 +      int n;
  1.6134 +    } ae;
  1.6135      struct OP_ResultRow_stack_vars {
  1.6136        Mem *pMem;
  1.6137        int i;
  1.6138 -    } ae;
  1.6139 +    } af;
  1.6140      struct OP_Concat_stack_vars {
  1.6141        i64 nByte;
  1.6142 -    } af;
  1.6143 +    } ag;
  1.6144      struct OP_Remainder_stack_vars {
  1.6145 +      char bIntint;   /* Started out as two integer operands */
  1.6146        int flags;      /* Combined MEM_* flags from both inputs */
  1.6147        i64 iA;         /* Integer value of left operand */
  1.6148        i64 iB;         /* Integer value of right operand */
  1.6149        double rA;      /* Real value of left operand */
  1.6150        double rB;      /* Real value of right operand */
  1.6151 -    } ag;
  1.6152 +    } ah;
  1.6153      struct OP_Function_stack_vars {
  1.6154        int i;
  1.6155        Mem *pArg;
  1.6156        sqlite3_context ctx;
  1.6157        sqlite3_value **apVal;
  1.6158        int n;
  1.6159 -    } ah;
  1.6160 +    } ai;
  1.6161      struct OP_ShiftRight_stack_vars {
  1.6162        i64 iA;
  1.6163        u64 uA;
  1.6164        i64 iB;
  1.6165        u8 op;
  1.6166 -    } ai;
  1.6167 +    } aj;
  1.6168      struct OP_Ge_stack_vars {
  1.6169        int res;            /* Result of the comparison of pIn1 against pIn3 */
  1.6170        char affinity;      /* Affinity to use for comparison */
  1.6171        u16 flags1;         /* Copy of initial value of pIn1->flags */
  1.6172        u16 flags3;         /* Copy of initial value of pIn3->flags */
  1.6173 -    } aj;
  1.6174 +    } ak;
  1.6175      struct OP_Compare_stack_vars {
  1.6176        int n;
  1.6177        int i;
  1.6178 @@ -65944,14 +64126,14 @@
  1.6179        int idx;
  1.6180        CollSeq *pColl;    /* Collating sequence to use on this term */
  1.6181        int bRev;          /* True for DESCENDING sort order */
  1.6182 -    } ak;
  1.6183 +    } al;
  1.6184      struct OP_Or_stack_vars {
  1.6185        int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
  1.6186        int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
  1.6187 -    } al;
  1.6188 +    } am;
  1.6189      struct OP_IfNot_stack_vars {
  1.6190        int c;
  1.6191 -    } am;
  1.6192 +    } an;
  1.6193      struct OP_Column_stack_vars {
  1.6194        u32 payloadSize;   /* Number of bytes in the record */
  1.6195        i64 payloadSize64; /* Number of bytes in the record */
  1.6196 @@ -65976,11 +64158,11 @@
  1.6197        int avail;         /* Number of bytes of available data */
  1.6198        u32 t;             /* A type code from the record header */
  1.6199        Mem *pReg;         /* PseudoTable input register */
  1.6200 -    } an;
  1.6201 +    } ao;
  1.6202      struct OP_Affinity_stack_vars {
  1.6203        const char *zAffinity;   /* The affinity to be applied */
  1.6204        char cAff;               /* A single character of affinity */
  1.6205 -    } ao;
  1.6206 +    } ap;
  1.6207      struct OP_MakeRecord_stack_vars {
  1.6208        u8 *zNewRecord;        /* A buffer to hold the data for the new record */
  1.6209        Mem *pRec;             /* The new record */
  1.6210 @@ -65997,11 +64179,11 @@
  1.6211        int file_format;       /* File format to use for encoding */
  1.6212        int i;                 /* Space used in zNewRecord[] */
  1.6213        int len;               /* Length of a field */
  1.6214 -    } ap;
  1.6215 +    } aq;
  1.6216      struct OP_Count_stack_vars {
  1.6217        i64 nEntry;
  1.6218        BtCursor *pCrsr;
  1.6219 -    } aq;
  1.6220 +    } ar;
  1.6221      struct OP_Savepoint_stack_vars {
  1.6222        int p1;                         /* Value of P1 operand */
  1.6223        char *zName;                    /* Name of savepoint */
  1.6224 @@ -66011,28 +64193,28 @@
  1.6225        Savepoint *pTmp;
  1.6226        int iSavepoint;
  1.6227        int ii;
  1.6228 -    } ar;
  1.6229 +    } as;
  1.6230      struct OP_AutoCommit_stack_vars {
  1.6231        int desiredAutoCommit;
  1.6232        int iRollback;
  1.6233        int turnOnAC;
  1.6234 -    } as;
  1.6235 +    } at;
  1.6236      struct OP_Transaction_stack_vars {
  1.6237        Btree *pBt;
  1.6238 -    } at;
  1.6239 +    } au;
  1.6240      struct OP_ReadCookie_stack_vars {
  1.6241        int iMeta;
  1.6242        int iDb;
  1.6243        int iCookie;
  1.6244 -    } au;
  1.6245 +    } av;
  1.6246      struct OP_SetCookie_stack_vars {
  1.6247        Db *pDb;
  1.6248 -    } av;
  1.6249 +    } aw;
  1.6250      struct OP_VerifyCookie_stack_vars {
  1.6251        int iMeta;
  1.6252        int iGen;
  1.6253        Btree *pBt;
  1.6254 -    } aw;
  1.6255 +    } ax;
  1.6256      struct OP_OpenWrite_stack_vars {
  1.6257        int nField;
  1.6258        KeyInfo *pKeyInfo;
  1.6259 @@ -66042,16 +64224,16 @@
  1.6260        Btree *pX;
  1.6261        VdbeCursor *pCur;
  1.6262        Db *pDb;
  1.6263 -    } ax;
  1.6264 +    } ay;
  1.6265      struct OP_OpenEphemeral_stack_vars {
  1.6266        VdbeCursor *pCx;
  1.6267 -    } ay;
  1.6268 +    } az;
  1.6269      struct OP_SorterOpen_stack_vars {
  1.6270        VdbeCursor *pCx;
  1.6271 -    } az;
  1.6272 +    } ba;
  1.6273      struct OP_OpenPseudo_stack_vars {
  1.6274        VdbeCursor *pCx;
  1.6275 -    } ba;
  1.6276 +    } bb;
  1.6277      struct OP_SeekGt_stack_vars {
  1.6278        int res;
  1.6279        int oc;
  1.6280 @@ -66059,10 +64241,10 @@
  1.6281        UnpackedRecord r;
  1.6282        int nField;
  1.6283        i64 iKey;      /* The rowid we are to seek to */
  1.6284 -    } bb;
  1.6285 +    } bc;
  1.6286      struct OP_Seek_stack_vars {
  1.6287        VdbeCursor *pC;
  1.6288 -    } bc;
  1.6289 +    } bd;
  1.6290      struct OP_Found_stack_vars {
  1.6291        int alreadyExists;
  1.6292        VdbeCursor *pC;
  1.6293 @@ -66071,7 +64253,7 @@
  1.6294        UnpackedRecord *pIdxKey;
  1.6295        UnpackedRecord r;
  1.6296        char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
  1.6297 -    } bd;
  1.6298 +    } be;
  1.6299      struct OP_IsUnique_stack_vars {
  1.6300        u16 ii;
  1.6301        VdbeCursor *pCx;
  1.6302 @@ -66080,13 +64262,13 @@
  1.6303        Mem *aMx;
  1.6304        UnpackedRecord r;                  /* B-Tree index search key */
  1.6305        i64 R;                             /* Rowid stored in register P3 */
  1.6306 -    } be;
  1.6307 +    } bf;
  1.6308      struct OP_NotExists_stack_vars {
  1.6309        VdbeCursor *pC;
  1.6310        BtCursor *pCrsr;
  1.6311        int res;
  1.6312        u64 iKey;
  1.6313 -    } bf;
  1.6314 +    } bg;
  1.6315      struct OP_NewRowid_stack_vars {
  1.6316        i64 v;                 /* The new rowid */
  1.6317        VdbeCursor *pC;        /* Cursor of table to get the new rowid */
  1.6318 @@ -66094,7 +64276,7 @@
  1.6319        int cnt;               /* Counter to limit the number of searches */
  1.6320        Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
  1.6321        VdbeFrame *pFrame;     /* Root frame of VDBE */
  1.6322 -    } bg;
  1.6323 +    } bh;
  1.6324      struct OP_InsertInt_stack_vars {
  1.6325        Mem *pData;       /* MEM cell holding data for the record to be inserted */
  1.6326        Mem *pKey;        /* MEM cell holding key  for the record */
  1.6327 @@ -66105,89 +64287,89 @@
  1.6328        const char *zDb;  /* database name - used by the update hook */
  1.6329        const char *zTbl; /* Table name - used by the opdate hook */
  1.6330        int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
  1.6331 -    } bh;
  1.6332 +    } bi;
  1.6333      struct OP_Delete_stack_vars {
  1.6334        i64 iKey;
  1.6335        VdbeCursor *pC;
  1.6336 -    } bi;
  1.6337 +    } bj;
  1.6338      struct OP_SorterCompare_stack_vars {
  1.6339        VdbeCursor *pC;
  1.6340        int res;
  1.6341 -    } bj;
  1.6342 +    } bk;
  1.6343      struct OP_SorterData_stack_vars {
  1.6344        VdbeCursor *pC;
  1.6345 -    } bk;
  1.6346 +    } bl;
  1.6347      struct OP_RowData_stack_vars {
  1.6348        VdbeCursor *pC;
  1.6349        BtCursor *pCrsr;
  1.6350        u32 n;
  1.6351        i64 n64;
  1.6352 -    } bl;
  1.6353 +    } bm;
  1.6354      struct OP_Rowid_stack_vars {
  1.6355        VdbeCursor *pC;
  1.6356        i64 v;
  1.6357        sqlite3_vtab *pVtab;
  1.6358        const sqlite3_module *pModule;
  1.6359 -    } bm;
  1.6360 +    } bn;
  1.6361      struct OP_NullRow_stack_vars {
  1.6362        VdbeCursor *pC;
  1.6363 -    } bn;
  1.6364 +    } bo;
  1.6365      struct OP_Last_stack_vars {
  1.6366        VdbeCursor *pC;
  1.6367        BtCursor *pCrsr;
  1.6368        int res;
  1.6369 -    } bo;
  1.6370 +    } bp;
  1.6371      struct OP_Rewind_stack_vars {
  1.6372        VdbeCursor *pC;
  1.6373        BtCursor *pCrsr;
  1.6374        int res;
  1.6375 -    } bp;
  1.6376 +    } bq;
  1.6377      struct OP_Next_stack_vars {
  1.6378        VdbeCursor *pC;
  1.6379        int res;
  1.6380 -    } bq;
  1.6381 +    } br;
  1.6382      struct OP_IdxInsert_stack_vars {
  1.6383        VdbeCursor *pC;
  1.6384        BtCursor *pCrsr;
  1.6385        int nKey;
  1.6386        const char *zKey;
  1.6387 -    } br;
  1.6388 +    } bs;
  1.6389      struct OP_IdxDelete_stack_vars {
  1.6390        VdbeCursor *pC;
  1.6391        BtCursor *pCrsr;
  1.6392        int res;
  1.6393        UnpackedRecord r;
  1.6394 -    } bs;
  1.6395 +    } bt;
  1.6396      struct OP_IdxRowid_stack_vars {
  1.6397        BtCursor *pCrsr;
  1.6398        VdbeCursor *pC;
  1.6399        i64 rowid;
  1.6400 -    } bt;
  1.6401 +    } bu;
  1.6402      struct OP_IdxGE_stack_vars {
  1.6403        VdbeCursor *pC;
  1.6404        int res;
  1.6405        UnpackedRecord r;
  1.6406 -    } bu;
  1.6407 +    } bv;
  1.6408      struct OP_Destroy_stack_vars {
  1.6409        int iMoved;
  1.6410        int iCnt;
  1.6411        Vdbe *pVdbe;
  1.6412        int iDb;
  1.6413 -    } bv;
  1.6414 +    } bw;
  1.6415      struct OP_Clear_stack_vars {
  1.6416        int nChange;
  1.6417 -    } bw;
  1.6418 +    } bx;
  1.6419      struct OP_CreateTable_stack_vars {
  1.6420        int pgno;
  1.6421        int flags;
  1.6422        Db *pDb;
  1.6423 -    } bx;
  1.6424 +    } by;
  1.6425      struct OP_ParseSchema_stack_vars {
  1.6426        int iDb;
  1.6427        const char *zMaster;
  1.6428        char *zSql;
  1.6429        InitData initData;
  1.6430 -    } by;
  1.6431 +    } bz;
  1.6432      struct OP_IntegrityCk_stack_vars {
  1.6433        int nRoot;      /* Number of tables to check.  (Number of root pages.) */
  1.6434        int *aRoot;     /* Array of rootpage numbers for tables to be checked */
  1.6435 @@ -66195,14 +64377,14 @@
  1.6436        int nErr;       /* Number of errors reported */
  1.6437        char *z;        /* Text of the error report */
  1.6438        Mem *pnErr;     /* Register keeping track of errors remaining */
  1.6439 -    } bz;
  1.6440 +    } ca;
  1.6441      struct OP_RowSetRead_stack_vars {
  1.6442        i64 val;
  1.6443 -    } ca;
  1.6444 +    } cb;
  1.6445      struct OP_RowSetTest_stack_vars {
  1.6446        int iSet;
  1.6447        int exists;
  1.6448 -    } cb;
  1.6449 +    } cc;
  1.6450      struct OP_Program_stack_vars {
  1.6451        int nMem;               /* Number of memory registers for sub-program */
  1.6452        int nByte;              /* Bytes of runtime space required for sub-program */
  1.6453 @@ -66212,15 +64394,15 @@
  1.6454        VdbeFrame *pFrame;      /* New vdbe frame to execute in */
  1.6455        SubProgram *pProgram;   /* Sub-program to execute */
  1.6456        void *t;                /* Token identifying trigger */
  1.6457 -    } cc;
  1.6458 +    } cd;
  1.6459      struct OP_Param_stack_vars {
  1.6460        VdbeFrame *pFrame;
  1.6461        Mem *pIn;
  1.6462 -    } cd;
  1.6463 +    } ce;
  1.6464      struct OP_MemMax_stack_vars {
  1.6465        Mem *pIn1;
  1.6466        VdbeFrame *pFrame;
  1.6467 -    } ce;
  1.6468 +    } cf;
  1.6469      struct OP_AggStep_stack_vars {
  1.6470        int n;
  1.6471        int i;
  1.6472 @@ -66228,34 +64410,36 @@
  1.6473        Mem *pRec;
  1.6474        sqlite3_context ctx;
  1.6475        sqlite3_value **apVal;
  1.6476 -    } cf;
  1.6477 +    } cg;
  1.6478      struct OP_AggFinal_stack_vars {
  1.6479        Mem *pMem;
  1.6480 -    } cg;
  1.6481 +    } ch;
  1.6482      struct OP_Checkpoint_stack_vars {
  1.6483        int i;                          /* Loop counter */
  1.6484        int aRes[3];                    /* Results */
  1.6485        Mem *pMem;                      /* Write results here */
  1.6486 -    } ch;
  1.6487 +    } ci;
  1.6488      struct OP_JournalMode_stack_vars {
  1.6489        Btree *pBt;                     /* Btree to change journal mode of */
  1.6490        Pager *pPager;                  /* Pager associated with pBt */
  1.6491        int eNew;                       /* New journal mode */
  1.6492        int eOld;                       /* The old journal mode */
  1.6493 +#ifndef SQLITE_OMIT_WAL
  1.6494        const char *zFilename;          /* Name of database file for pPager */
  1.6495 -    } ci;
  1.6496 +#endif
  1.6497 +    } cj;
  1.6498      struct OP_IncrVacuum_stack_vars {
  1.6499        Btree *pBt;
  1.6500 -    } cj;
  1.6501 +    } ck;
  1.6502      struct OP_VBegin_stack_vars {
  1.6503        VTable *pVTab;
  1.6504 -    } ck;
  1.6505 +    } cl;
  1.6506      struct OP_VOpen_stack_vars {
  1.6507        VdbeCursor *pCur;
  1.6508        sqlite3_vtab_cursor *pVtabCursor;
  1.6509        sqlite3_vtab *pVtab;
  1.6510        sqlite3_module *pModule;
  1.6511 -    } cl;
  1.6512 +    } cm;
  1.6513      struct OP_VFilter_stack_vars {
  1.6514        int nArg;
  1.6515        int iQuery;
  1.6516 @@ -66268,23 +64452,23 @@
  1.6517        int res;
  1.6518        int i;
  1.6519        Mem **apArg;
  1.6520 -    } cm;
  1.6521 +    } cn;
  1.6522      struct OP_VColumn_stack_vars {
  1.6523        sqlite3_vtab *pVtab;
  1.6524        const sqlite3_module *pModule;
  1.6525        Mem *pDest;
  1.6526        sqlite3_context sContext;
  1.6527 -    } cn;
  1.6528 +    } co;
  1.6529      struct OP_VNext_stack_vars {
  1.6530        sqlite3_vtab *pVtab;
  1.6531        const sqlite3_module *pModule;
  1.6532        int res;
  1.6533        VdbeCursor *pCur;
  1.6534 -    } co;
  1.6535 +    } cp;
  1.6536      struct OP_VRename_stack_vars {
  1.6537        sqlite3_vtab *pVtab;
  1.6538        Mem *pName;
  1.6539 -    } cp;
  1.6540 +    } cq;
  1.6541      struct OP_VUpdate_stack_vars {
  1.6542        sqlite3_vtab *pVtab;
  1.6543        sqlite3_module *pModule;
  1.6544 @@ -66293,11 +64477,11 @@
  1.6545        sqlite_int64 rowid;
  1.6546        Mem **apArg;
  1.6547        Mem *pX;
  1.6548 -    } cq;
  1.6549 +    } cr;
  1.6550      struct OP_Trace_stack_vars {
  1.6551        char *zTrace;
  1.6552        char *z;
  1.6553 -    } cr;
  1.6554 +    } cs;
  1.6555    } u;
  1.6556    /* End automatically generated code
  1.6557    ********************************************************************/
  1.6558 @@ -66386,7 +64570,7 @@
  1.6559      }
  1.6560  #endif
  1.6561  
  1.6562 -    /* On any opcode with the "out2-prerelase" tag, free any
  1.6563 +    /* On any opcode with the "out2-prerelease" tag, free any
  1.6564      ** external allocations out of mem[p2] and set mem[p2] to be
  1.6565      ** an undefined integer.  Opcodes will either fill in the integer
  1.6566      ** value or convert mem[p2] to a different type.
  1.6567 @@ -66688,25 +64872,30 @@
  1.6568    break;
  1.6569  }
  1.6570  
  1.6571 -/* Opcode: Null * P2 P3 * *
  1.6572 +/* Opcode: Null P1 P2 P3 * *
  1.6573  **
  1.6574  ** Write a NULL into registers P2.  If P3 greater than P2, then also write
  1.6575 -** NULL into register P3 and ever register in between P2 and P3.  If P3
  1.6576 +** NULL into register P3 and every register in between P2 and P3.  If P3
  1.6577  ** is less than P2 (typically P3 is zero) then only register P2 is
  1.6578 -** set to NULL
  1.6579 +** set to NULL.
  1.6580 +**
  1.6581 +** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
  1.6582 +** NULL values will not compare equal even if SQLITE_NULLEQ is set on
  1.6583 +** OP_Ne or OP_Eq.
  1.6584  */
  1.6585  case OP_Null: {           /* out2-prerelease */
  1.6586  #if 0  /* local variables moved into u.ab */
  1.6587    int cnt;
  1.6588 +  u16 nullFlag;
  1.6589  #endif /* local variables moved into u.ab */
  1.6590    u.ab.cnt = pOp->p3-pOp->p2;
  1.6591    assert( pOp->p3<=p->nMem );
  1.6592 -  pOut->flags = MEM_Null;
  1.6593 +  pOut->flags = u.ab.nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
  1.6594    while( u.ab.cnt>0 ){
  1.6595      pOut++;
  1.6596      memAboutToChange(p, pOut);
  1.6597      VdbeMemRelease(pOut);
  1.6598 -    pOut->flags = MEM_Null;
  1.6599 +    pOut->flags = u.ab.nullFlag;
  1.6600      u.ab.cnt--;
  1.6601    }
  1.6602    break;
  1.6603 @@ -66751,10 +64940,10 @@
  1.6604  
  1.6605  /* Opcode: Move P1 P2 P3 * *
  1.6606  **
  1.6607 -** Move the values in register P1..P1+P3-1 over into
  1.6608 -** registers P2..P2+P3-1.  Registers P1..P1+P1-1 are
  1.6609 +** Move the values in register P1..P1+P3 over into
  1.6610 +** registers P2..P2+P3.  Registers P1..P1+P3 are
  1.6611  ** left holding a NULL.  It is an error for register ranges
  1.6612 -** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
  1.6613 +** P1..P1+P3 and P2..P2+P3 to overlap.
  1.6614  */
  1.6615  case OP_Move: {
  1.6616  #if 0  /* local variables moved into u.ad */
  1.6617 @@ -66764,7 +64953,7 @@
  1.6618    int p2;          /* Register to copy to */
  1.6619  #endif /* local variables moved into u.ad */
  1.6620  
  1.6621 -  u.ad.n = pOp->p3;
  1.6622 +  u.ad.n = pOp->p3 + 1;
  1.6623    u.ad.p1 = pOp->p1;
  1.6624    u.ad.p2 = pOp->p2;
  1.6625    assert( u.ad.n>0 && u.ad.p1>0 && u.ad.p2>0 );
  1.6626 @@ -66793,20 +64982,33 @@
  1.6627    break;
  1.6628  }
  1.6629  
  1.6630 -/* Opcode: Copy P1 P2 * * *
  1.6631 -**
  1.6632 -** Make a copy of register P1 into register P2.
  1.6633 +/* Opcode: Copy P1 P2 P3 * *
  1.6634 +**
  1.6635 +** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
  1.6636  **
  1.6637  ** This instruction makes a deep copy of the value.  A duplicate
  1.6638  ** is made of any string or blob constant.  See also OP_SCopy.
  1.6639  */
  1.6640 -case OP_Copy: {             /* in1, out2 */
  1.6641 +case OP_Copy: {
  1.6642 +#if 0  /* local variables moved into u.ae */
  1.6643 +  int n;
  1.6644 +#endif /* local variables moved into u.ae */
  1.6645 +
  1.6646 +  u.ae.n = pOp->p3;
  1.6647    pIn1 = &aMem[pOp->p1];
  1.6648    pOut = &aMem[pOp->p2];
  1.6649    assert( pOut!=pIn1 );
  1.6650 -  sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
  1.6651 -  Deephemeralize(pOut);
  1.6652 -  REGISTER_TRACE(pOp->p2, pOut);
  1.6653 +  while( 1 ){
  1.6654 +    sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
  1.6655 +    Deephemeralize(pOut);
  1.6656 +#ifdef SQLITE_DEBUG
  1.6657 +    pOut->pScopyFrom = 0;
  1.6658 +#endif
  1.6659 +    REGISTER_TRACE(pOp->p2+pOp->p3-u.ae.n, pOut);
  1.6660 +    if( (u.ae.n--)==0 ) break;
  1.6661 +    pOut++;
  1.6662 +    pIn1++;
  1.6663 +  }
  1.6664    break;
  1.6665  }
  1.6666  
  1.6667 @@ -66843,10 +65045,10 @@
  1.6668  ** row.
  1.6669  */
  1.6670  case OP_ResultRow: {
  1.6671 -#if 0  /* local variables moved into u.ae */
  1.6672 +#if 0  /* local variables moved into u.af */
  1.6673    Mem *pMem;
  1.6674    int i;
  1.6675 -#endif /* local variables moved into u.ae */
  1.6676 +#endif /* local variables moved into u.af */
  1.6677    assert( p->nResColumn==pOp->p2 );
  1.6678    assert( pOp->p1>0 );
  1.6679    assert( pOp->p1+pOp->p2<=p->nMem+1 );
  1.6680 @@ -66888,15 +65090,15 @@
  1.6681    ** and have an assigned type.  The results are de-ephemeralized as
  1.6682    ** a side effect.
  1.6683    */
  1.6684 -  u.ae.pMem = p->pResultSet = &aMem[pOp->p1];
  1.6685 -  for(u.ae.i=0; u.ae.i<pOp->p2; u.ae.i++){
  1.6686 -    assert( memIsValid(&u.ae.pMem[u.ae.i]) );
  1.6687 -    Deephemeralize(&u.ae.pMem[u.ae.i]);
  1.6688 -    assert( (u.ae.pMem[u.ae.i].flags & MEM_Ephem)==0
  1.6689 -            || (u.ae.pMem[u.ae.i].flags & (MEM_Str|MEM_Blob))==0 );
  1.6690 -    sqlite3VdbeMemNulTerminate(&u.ae.pMem[u.ae.i]);
  1.6691 -    sqlite3VdbeMemStoreType(&u.ae.pMem[u.ae.i]);
  1.6692 -    REGISTER_TRACE(pOp->p1+u.ae.i, &u.ae.pMem[u.ae.i]);
  1.6693 +  u.af.pMem = p->pResultSet = &aMem[pOp->p1];
  1.6694 +  for(u.af.i=0; u.af.i<pOp->p2; u.af.i++){
  1.6695 +    assert( memIsValid(&u.af.pMem[u.af.i]) );
  1.6696 +    Deephemeralize(&u.af.pMem[u.af.i]);
  1.6697 +    assert( (u.af.pMem[u.af.i].flags & MEM_Ephem)==0
  1.6698 +            || (u.af.pMem[u.af.i].flags & (MEM_Str|MEM_Blob))==0 );
  1.6699 +    sqlite3VdbeMemNulTerminate(&u.af.pMem[u.af.i]);
  1.6700 +    sqlite3VdbeMemStoreType(&u.af.pMem[u.af.i]);
  1.6701 +    REGISTER_TRACE(pOp->p1+u.af.i, &u.af.pMem[u.af.i]);
  1.6702    }
  1.6703    if( db->mallocFailed ) goto no_mem;
  1.6704  
  1.6705 @@ -66920,9 +65122,9 @@
  1.6706  ** to avoid a memcpy().
  1.6707  */
  1.6708  case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
  1.6709 -#if 0  /* local variables moved into u.af */
  1.6710 +#if 0  /* local variables moved into u.ag */
  1.6711    i64 nByte;
  1.6712 -#endif /* local variables moved into u.af */
  1.6713 +#endif /* local variables moved into u.ag */
  1.6714  
  1.6715    pIn1 = &aMem[pOp->p1];
  1.6716    pIn2 = &aMem[pOp->p2];
  1.6717 @@ -66935,22 +65137,22 @@
  1.6718    if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
  1.6719    Stringify(pIn1, encoding);
  1.6720    Stringify(pIn2, encoding);
  1.6721 -  u.af.nByte = pIn1->n + pIn2->n;
  1.6722 -  if( u.af.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1.6723 +  u.ag.nByte = pIn1->n + pIn2->n;
  1.6724 +  if( u.ag.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1.6725      goto too_big;
  1.6726    }
  1.6727    MemSetTypeFlag(pOut, MEM_Str);
  1.6728 -  if( sqlite3VdbeMemGrow(pOut, (int)u.af.nByte+2, pOut==pIn2) ){
  1.6729 +  if( sqlite3VdbeMemGrow(pOut, (int)u.ag.nByte+2, pOut==pIn2) ){
  1.6730      goto no_mem;
  1.6731    }
  1.6732    if( pOut!=pIn2 ){
  1.6733      memcpy(pOut->z, pIn2->z, pIn2->n);
  1.6734    }
  1.6735    memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
  1.6736 -  pOut->z[u.af.nByte] = 0;
  1.6737 -  pOut->z[u.af.nByte+1] = 0;
  1.6738 +  pOut->z[u.ag.nByte] = 0;
  1.6739 +  pOut->z[u.ag.nByte+1] = 0;
  1.6740    pOut->flags |= MEM_Term;
  1.6741 -  pOut->n = (int)u.af.nByte;
  1.6742 +  pOut->n = (int)u.ag.nByte;
  1.6743    pOut->enc = encoding;
  1.6744    UPDATE_MAX_BLOBSIZE(pOut);
  1.6745    break;
  1.6746 @@ -66994,76 +65196,79 @@
  1.6747  case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
  1.6748  case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
  1.6749  case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
  1.6750 -#if 0  /* local variables moved into u.ag */
  1.6751 +#if 0  /* local variables moved into u.ah */
  1.6752 +  char bIntint;   /* Started out as two integer operands */
  1.6753    int flags;      /* Combined MEM_* flags from both inputs */
  1.6754    i64 iA;         /* Integer value of left operand */
  1.6755    i64 iB;         /* Integer value of right operand */
  1.6756    double rA;      /* Real value of left operand */
  1.6757    double rB;      /* Real value of right operand */
  1.6758 -#endif /* local variables moved into u.ag */
  1.6759 +#endif /* local variables moved into u.ah */
  1.6760  
  1.6761    pIn1 = &aMem[pOp->p1];
  1.6762    applyNumericAffinity(pIn1);
  1.6763    pIn2 = &aMem[pOp->p2];
  1.6764    applyNumericAffinity(pIn2);
  1.6765    pOut = &aMem[pOp->p3];
  1.6766 -  u.ag.flags = pIn1->flags | pIn2->flags;
  1.6767 -  if( (u.ag.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
  1.6768 +  u.ah.flags = pIn1->flags | pIn2->flags;
  1.6769 +  if( (u.ah.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
  1.6770    if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
  1.6771 -    u.ag.iA = pIn1->u.i;
  1.6772 -    u.ag.iB = pIn2->u.i;
  1.6773 +    u.ah.iA = pIn1->u.i;
  1.6774 +    u.ah.iB = pIn2->u.i;
  1.6775 +    u.ah.bIntint = 1;
  1.6776      switch( pOp->opcode ){
  1.6777 -      case OP_Add:       if( sqlite3AddInt64(&u.ag.iB,u.ag.iA) ) goto fp_math;  break;
  1.6778 -      case OP_Subtract:  if( sqlite3SubInt64(&u.ag.iB,u.ag.iA) ) goto fp_math;  break;
  1.6779 -      case OP_Multiply:  if( sqlite3MulInt64(&u.ag.iB,u.ag.iA) ) goto fp_math;  break;
  1.6780 +      case OP_Add:       if( sqlite3AddInt64(&u.ah.iB,u.ah.iA) ) goto fp_math;  break;
  1.6781 +      case OP_Subtract:  if( sqlite3SubInt64(&u.ah.iB,u.ah.iA) ) goto fp_math;  break;
  1.6782 +      case OP_Multiply:  if( sqlite3MulInt64(&u.ah.iB,u.ah.iA) ) goto fp_math;  break;
  1.6783        case OP_Divide: {
  1.6784 -        if( u.ag.iA==0 ) goto arithmetic_result_is_null;
  1.6785 -        if( u.ag.iA==-1 && u.ag.iB==SMALLEST_INT64 ) goto fp_math;
  1.6786 -        u.ag.iB /= u.ag.iA;
  1.6787 +        if( u.ah.iA==0 ) goto arithmetic_result_is_null;
  1.6788 +        if( u.ah.iA==-1 && u.ah.iB==SMALLEST_INT64 ) goto fp_math;
  1.6789 +        u.ah.iB /= u.ah.iA;
  1.6790          break;
  1.6791        }
  1.6792        default: {
  1.6793 -        if( u.ag.iA==0 ) goto arithmetic_result_is_null;
  1.6794 -        if( u.ag.iA==-1 ) u.ag.iA = 1;
  1.6795 -        u.ag.iB %= u.ag.iA;
  1.6796 -        break;
  1.6797 -      }
  1.6798 -    }
  1.6799 -    pOut->u.i = u.ag.iB;
  1.6800 +        if( u.ah.iA==0 ) goto arithmetic_result_is_null;
  1.6801 +        if( u.ah.iA==-1 ) u.ah.iA = 1;
  1.6802 +        u.ah.iB %= u.ah.iA;
  1.6803 +        break;
  1.6804 +      }
  1.6805 +    }
  1.6806 +    pOut->u.i = u.ah.iB;
  1.6807      MemSetTypeFlag(pOut, MEM_Int);
  1.6808    }else{
  1.6809 +    u.ah.bIntint = 0;
  1.6810  fp_math:
  1.6811 -    u.ag.rA = sqlite3VdbeRealValue(pIn1);
  1.6812 -    u.ag.rB = sqlite3VdbeRealValue(pIn2);
  1.6813 +    u.ah.rA = sqlite3VdbeRealValue(pIn1);
  1.6814 +    u.ah.rB = sqlite3VdbeRealValue(pIn2);
  1.6815      switch( pOp->opcode ){
  1.6816 -      case OP_Add:         u.ag.rB += u.ag.rA;       break;
  1.6817 -      case OP_Subtract:    u.ag.rB -= u.ag.rA;       break;
  1.6818 -      case OP_Multiply:    u.ag.rB *= u.ag.rA;       break;
  1.6819 +      case OP_Add:         u.ah.rB += u.ah.rA;       break;
  1.6820 +      case OP_Subtract:    u.ah.rB -= u.ah.rA;       break;
  1.6821 +      case OP_Multiply:    u.ah.rB *= u.ah.rA;       break;
  1.6822        case OP_Divide: {
  1.6823          /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
  1.6824 -        if( u.ag.rA==(double)0 ) goto arithmetic_result_is_null;
  1.6825 -        u.ag.rB /= u.ag.rA;
  1.6826 +        if( u.ah.rA==(double)0 ) goto arithmetic_result_is_null;
  1.6827 +        u.ah.rB /= u.ah.rA;
  1.6828          break;
  1.6829        }
  1.6830        default: {
  1.6831 -        u.ag.iA = (i64)u.ag.rA;
  1.6832 -        u.ag.iB = (i64)u.ag.rB;
  1.6833 -        if( u.ag.iA==0 ) goto arithmetic_result_is_null;
  1.6834 -        if( u.ag.iA==-1 ) u.ag.iA = 1;
  1.6835 -        u.ag.rB = (double)(u.ag.iB % u.ag.iA);
  1.6836 +        u.ah.iA = (i64)u.ah.rA;
  1.6837 +        u.ah.iB = (i64)u.ah.rB;
  1.6838 +        if( u.ah.iA==0 ) goto arithmetic_result_is_null;
  1.6839 +        if( u.ah.iA==-1 ) u.ah.iA = 1;
  1.6840 +        u.ah.rB = (double)(u.ah.iB % u.ah.iA);
  1.6841          break;
  1.6842        }
  1.6843      }
  1.6844  #ifdef SQLITE_OMIT_FLOATING_POINT
  1.6845 -    pOut->u.i = u.ag.rB;
  1.6846 +    pOut->u.i = u.ah.rB;
  1.6847      MemSetTypeFlag(pOut, MEM_Int);
  1.6848  #else
  1.6849 -    if( sqlite3IsNaN(u.ag.rB) ){
  1.6850 +    if( sqlite3IsNaN(u.ah.rB) ){
  1.6851        goto arithmetic_result_is_null;
  1.6852      }
  1.6853 -    pOut->r = u.ag.rB;
  1.6854 +    pOut->r = u.ah.rB;
  1.6855      MemSetTypeFlag(pOut, MEM_Real);
  1.6856 -    if( (u.ag.flags & MEM_Real)==0 ){
  1.6857 +    if( (u.ah.flags & MEM_Real)==0 && !u.ah.bIntint ){
  1.6858        sqlite3VdbeIntegerAffinity(pOut);
  1.6859      }
  1.6860  #endif
  1.6861 @@ -67115,70 +65320,70 @@
  1.6862  ** See also: AggStep and AggFinal
  1.6863  */
  1.6864  case OP_Function: {
  1.6865 -#if 0  /* local variables moved into u.ah */
  1.6866 +#if 0  /* local variables moved into u.ai */
  1.6867    int i;
  1.6868    Mem *pArg;
  1.6869    sqlite3_context ctx;
  1.6870    sqlite3_value **apVal;
  1.6871    int n;
  1.6872 -#endif /* local variables moved into u.ah */
  1.6873 -
  1.6874 -  u.ah.n = pOp->p5;
  1.6875 -  u.ah.apVal = p->apArg;
  1.6876 -  assert( u.ah.apVal || u.ah.n==0 );
  1.6877 +#endif /* local variables moved into u.ai */
  1.6878 +
  1.6879 +  u.ai.n = pOp->p5;
  1.6880 +  u.ai.apVal = p->apArg;
  1.6881 +  assert( u.ai.apVal || u.ai.n==0 );
  1.6882    assert( pOp->p3>0 && pOp->p3<=p->nMem );
  1.6883    pOut = &aMem[pOp->p3];
  1.6884    memAboutToChange(p, pOut);
  1.6885  
  1.6886 -  assert( u.ah.n==0 || (pOp->p2>0 && pOp->p2+u.ah.n<=p->nMem+1) );
  1.6887 -  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ah.n );
  1.6888 -  u.ah.pArg = &aMem[pOp->p2];
  1.6889 -  for(u.ah.i=0; u.ah.i<u.ah.n; u.ah.i++, u.ah.pArg++){
  1.6890 -    assert( memIsValid(u.ah.pArg) );
  1.6891 -    u.ah.apVal[u.ah.i] = u.ah.pArg;
  1.6892 -    Deephemeralize(u.ah.pArg);
  1.6893 -    sqlite3VdbeMemStoreType(u.ah.pArg);
  1.6894 -    REGISTER_TRACE(pOp->p2+u.ah.i, u.ah.pArg);
  1.6895 +  assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=p->nMem+1) );
  1.6896 +  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ai.n );
  1.6897 +  u.ai.pArg = &aMem[pOp->p2];
  1.6898 +  for(u.ai.i=0; u.ai.i<u.ai.n; u.ai.i++, u.ai.pArg++){
  1.6899 +    assert( memIsValid(u.ai.pArg) );
  1.6900 +    u.ai.apVal[u.ai.i] = u.ai.pArg;
  1.6901 +    Deephemeralize(u.ai.pArg);
  1.6902 +    sqlite3VdbeMemStoreType(u.ai.pArg);
  1.6903 +    REGISTER_TRACE(pOp->p2+u.ai.i, u.ai.pArg);
  1.6904    }
  1.6905  
  1.6906    assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
  1.6907    if( pOp->p4type==P4_FUNCDEF ){
  1.6908 -    u.ah.ctx.pFunc = pOp->p4.pFunc;
  1.6909 -    u.ah.ctx.pVdbeFunc = 0;
  1.6910 -  }else{
  1.6911 -    u.ah.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
  1.6912 -    u.ah.ctx.pFunc = u.ah.ctx.pVdbeFunc->pFunc;
  1.6913 -  }
  1.6914 -
  1.6915 -  u.ah.ctx.s.flags = MEM_Null;
  1.6916 -  u.ah.ctx.s.db = db;
  1.6917 -  u.ah.ctx.s.xDel = 0;
  1.6918 -  u.ah.ctx.s.zMalloc = 0;
  1.6919 +    u.ai.ctx.pFunc = pOp->p4.pFunc;
  1.6920 +    u.ai.ctx.pVdbeFunc = 0;
  1.6921 +  }else{
  1.6922 +    u.ai.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
  1.6923 +    u.ai.ctx.pFunc = u.ai.ctx.pVdbeFunc->pFunc;
  1.6924 +  }
  1.6925 +
  1.6926 +  u.ai.ctx.s.flags = MEM_Null;
  1.6927 +  u.ai.ctx.s.db = db;
  1.6928 +  u.ai.ctx.s.xDel = 0;
  1.6929 +  u.ai.ctx.s.zMalloc = 0;
  1.6930  
  1.6931    /* The output cell may already have a buffer allocated. Move
  1.6932 -  ** the pointer to u.ah.ctx.s so in case the user-function can use
  1.6933 +  ** the pointer to u.ai.ctx.s so in case the user-function can use
  1.6934    ** the already allocated buffer instead of allocating a new one.
  1.6935    */
  1.6936 -  sqlite3VdbeMemMove(&u.ah.ctx.s, pOut);
  1.6937 -  MemSetTypeFlag(&u.ah.ctx.s, MEM_Null);
  1.6938 -
  1.6939 -  u.ah.ctx.isError = 0;
  1.6940 -  if( u.ah.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
  1.6941 +  sqlite3VdbeMemMove(&u.ai.ctx.s, pOut);
  1.6942 +  MemSetTypeFlag(&u.ai.ctx.s, MEM_Null);
  1.6943 +
  1.6944 +  u.ai.ctx.isError = 0;
  1.6945 +  if( u.ai.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
  1.6946      assert( pOp>aOp );
  1.6947      assert( pOp[-1].p4type==P4_COLLSEQ );
  1.6948      assert( pOp[-1].opcode==OP_CollSeq );
  1.6949 -    u.ah.ctx.pColl = pOp[-1].p4.pColl;
  1.6950 +    u.ai.ctx.pColl = pOp[-1].p4.pColl;
  1.6951    }
  1.6952    db->lastRowid = lastRowid;
  1.6953 -  (*u.ah.ctx.pFunc->xFunc)(&u.ah.ctx, u.ah.n, u.ah.apVal); /* IMP: R-24505-23230 */
  1.6954 +  (*u.ai.ctx.pFunc->xFunc)(&u.ai.ctx, u.ai.n, u.ai.apVal); /* IMP: R-24505-23230 */
  1.6955    lastRowid = db->lastRowid;
  1.6956  
  1.6957    /* If any auxiliary data functions have been called by this user function,
  1.6958    ** immediately call the destructor for any non-static values.
  1.6959    */
  1.6960 -  if( u.ah.ctx.pVdbeFunc ){
  1.6961 -    sqlite3VdbeDeleteAuxData(u.ah.ctx.pVdbeFunc, pOp->p1);
  1.6962 -    pOp->p4.pVdbeFunc = u.ah.ctx.pVdbeFunc;
  1.6963 +  if( u.ai.ctx.pVdbeFunc ){
  1.6964 +    sqlite3VdbeDeleteAuxData(u.ai.ctx.pVdbeFunc, pOp->p1);
  1.6965 +    pOp->p4.pVdbeFunc = u.ai.ctx.pVdbeFunc;
  1.6966      pOp->p4type = P4_VDBEFUNC;
  1.6967    }
  1.6968  
  1.6969 @@ -67188,19 +65393,19 @@
  1.6970      ** to return a value. The following call releases any resources
  1.6971      ** associated with such a value.
  1.6972      */
  1.6973 -    sqlite3VdbeMemRelease(&u.ah.ctx.s);
  1.6974 +    sqlite3VdbeMemRelease(&u.ai.ctx.s);
  1.6975      goto no_mem;
  1.6976    }
  1.6977  
  1.6978    /* If the function returned an error, throw an exception */
  1.6979 -  if( u.ah.ctx.isError ){
  1.6980 -    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ah.ctx.s));
  1.6981 -    rc = u.ah.ctx.isError;
  1.6982 +  if( u.ai.ctx.isError ){
  1.6983 +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ai.ctx.s));
  1.6984 +    rc = u.ai.ctx.isError;
  1.6985    }
  1.6986  
  1.6987    /* Copy the result of the function into register P3 */
  1.6988 -  sqlite3VdbeChangeEncoding(&u.ah.ctx.s, encoding);
  1.6989 -  sqlite3VdbeMemMove(pOut, &u.ah.ctx.s);
  1.6990 +  sqlite3VdbeChangeEncoding(&u.ai.ctx.s, encoding);
  1.6991 +  sqlite3VdbeMemMove(pOut, &u.ai.ctx.s);
  1.6992    if( sqlite3VdbeMemTooBig(pOut) ){
  1.6993      goto too_big;
  1.6994    }
  1.6995 @@ -67248,12 +65453,12 @@
  1.6996  case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
  1.6997  case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
  1.6998  case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
  1.6999 -#if 0  /* local variables moved into u.ai */
  1.7000 +#if 0  /* local variables moved into u.aj */
  1.7001    i64 iA;
  1.7002    u64 uA;
  1.7003    i64 iB;
  1.7004    u8 op;
  1.7005 -#endif /* local variables moved into u.ai */
  1.7006 +#endif /* local variables moved into u.aj */
  1.7007  
  1.7008    pIn1 = &aMem[pOp->p1];
  1.7009    pIn2 = &aMem[pOp->p2];
  1.7010 @@ -67262,38 +65467,38 @@
  1.7011      sqlite3VdbeMemSetNull(pOut);
  1.7012      break;
  1.7013    }
  1.7014 -  u.ai.iA = sqlite3VdbeIntValue(pIn2);
  1.7015 -  u.ai.iB = sqlite3VdbeIntValue(pIn1);
  1.7016 -  u.ai.op = pOp->opcode;
  1.7017 -  if( u.ai.op==OP_BitAnd ){
  1.7018 -    u.ai.iA &= u.ai.iB;
  1.7019 -  }else if( u.ai.op==OP_BitOr ){
  1.7020 -    u.ai.iA |= u.ai.iB;
  1.7021 -  }else if( u.ai.iB!=0 ){
  1.7022 -    assert( u.ai.op==OP_ShiftRight || u.ai.op==OP_ShiftLeft );
  1.7023 +  u.aj.iA = sqlite3VdbeIntValue(pIn2);
  1.7024 +  u.aj.iB = sqlite3VdbeIntValue(pIn1);
  1.7025 +  u.aj.op = pOp->opcode;
  1.7026 +  if( u.aj.op==OP_BitAnd ){
  1.7027 +    u.aj.iA &= u.aj.iB;
  1.7028 +  }else if( u.aj.op==OP_BitOr ){
  1.7029 +    u.aj.iA |= u.aj.iB;
  1.7030 +  }else if( u.aj.iB!=0 ){
  1.7031 +    assert( u.aj.op==OP_ShiftRight || u.aj.op==OP_ShiftLeft );
  1.7032  
  1.7033      /* If shifting by a negative amount, shift in the other direction */
  1.7034 -    if( u.ai.iB<0 ){
  1.7035 +    if( u.aj.iB<0 ){
  1.7036        assert( OP_ShiftRight==OP_ShiftLeft+1 );
  1.7037 -      u.ai.op = 2*OP_ShiftLeft + 1 - u.ai.op;
  1.7038 -      u.ai.iB = u.ai.iB>(-64) ? -u.ai.iB : 64;
  1.7039 -    }
  1.7040 -
  1.7041 -    if( u.ai.iB>=64 ){
  1.7042 -      u.ai.iA = (u.ai.iA>=0 || u.ai.op==OP_ShiftLeft) ? 0 : -1;
  1.7043 -    }else{
  1.7044 -      memcpy(&u.ai.uA, &u.ai.iA, sizeof(u.ai.uA));
  1.7045 -      if( u.ai.op==OP_ShiftLeft ){
  1.7046 -        u.ai.uA <<= u.ai.iB;
  1.7047 -      }else{
  1.7048 -        u.ai.uA >>= u.ai.iB;
  1.7049 +      u.aj.op = 2*OP_ShiftLeft + 1 - u.aj.op;
  1.7050 +      u.aj.iB = u.aj.iB>(-64) ? -u.aj.iB : 64;
  1.7051 +    }
  1.7052 +
  1.7053 +    if( u.aj.iB>=64 ){
  1.7054 +      u.aj.iA = (u.aj.iA>=0 || u.aj.op==OP_ShiftLeft) ? 0 : -1;
  1.7055 +    }else{
  1.7056 +      memcpy(&u.aj.uA, &u.aj.iA, sizeof(u.aj.uA));
  1.7057 +      if( u.aj.op==OP_ShiftLeft ){
  1.7058 +        u.aj.uA <<= u.aj.iB;
  1.7059 +      }else{
  1.7060 +        u.aj.uA >>= u.aj.iB;
  1.7061          /* Sign-extend on a right shift of a negative number */
  1.7062 -        if( u.ai.iA<0 ) u.ai.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ai.iB);
  1.7063 -      }
  1.7064 -      memcpy(&u.ai.iA, &u.ai.uA, sizeof(u.ai.iA));
  1.7065 -    }
  1.7066 -  }
  1.7067 -  pOut->u.i = u.ai.iA;
  1.7068 +        if( u.aj.iA<0 ) u.aj.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.aj.iB);
  1.7069 +      }
  1.7070 +      memcpy(&u.aj.iA, &u.aj.uA, sizeof(u.aj.iA));
  1.7071 +    }
  1.7072 +  }
  1.7073 +  pOut->u.i = u.aj.iA;
  1.7074    MemSetTypeFlag(pOut, MEM_Int);
  1.7075    break;
  1.7076  }
  1.7077 @@ -67485,6 +65690,10 @@
  1.7078  **
  1.7079  ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
  1.7080  ** store a boolean result (either 0, or 1, or NULL) in register P2.
  1.7081 +**
  1.7082 +** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
  1.7083 +** equal to one another, provided that they do not have their MEM_Cleared
  1.7084 +** bit set.
  1.7085  */
  1.7086  /* Opcode: Ne P1 P2 P3 P4 P5
  1.7087  **
  1.7088 @@ -67534,18 +65743,18 @@
  1.7089  case OP_Le:               /* same as TK_LE, jump, in1, in3 */
  1.7090  case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
  1.7091  case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
  1.7092 -#if 0  /* local variables moved into u.aj */
  1.7093 +#if 0  /* local variables moved into u.ak */
  1.7094    int res;            /* Result of the comparison of pIn1 against pIn3 */
  1.7095    char affinity;      /* Affinity to use for comparison */
  1.7096    u16 flags1;         /* Copy of initial value of pIn1->flags */
  1.7097    u16 flags3;         /* Copy of initial value of pIn3->flags */
  1.7098 -#endif /* local variables moved into u.aj */
  1.7099 +#endif /* local variables moved into u.ak */
  1.7100  
  1.7101    pIn1 = &aMem[pOp->p1];
  1.7102    pIn3 = &aMem[pOp->p3];
  1.7103 -  u.aj.flags1 = pIn1->flags;
  1.7104 -  u.aj.flags3 = pIn3->flags;
  1.7105 -  if( (u.aj.flags1 | u.aj.flags3)&MEM_Null ){
  1.7106 +  u.ak.flags1 = pIn1->flags;
  1.7107 +  u.ak.flags3 = pIn3->flags;
  1.7108 +  if( (u.ak.flags1 | u.ak.flags3)&MEM_Null ){
  1.7109      /* One or both operands are NULL */
  1.7110      if( pOp->p5 & SQLITE_NULLEQ ){
  1.7111        /* If SQLITE_NULLEQ is set (which will only happen if the operator is
  1.7112 @@ -67553,7 +65762,15 @@
  1.7113        ** or not both operands are null.
  1.7114        */
  1.7115        assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
  1.7116 -      u.aj.res = (u.aj.flags1 & u.aj.flags3 & MEM_Null)==0;
  1.7117 +      assert( (u.ak.flags1 & MEM_Cleared)==0 );
  1.7118 +      if( (u.ak.flags1&MEM_Null)!=0
  1.7119 +       && (u.ak.flags3&MEM_Null)!=0
  1.7120 +       && (u.ak.flags3&MEM_Cleared)==0
  1.7121 +      ){
  1.7122 +        u.ak.res = 0;  /* Results are equal */
  1.7123 +      }else{
  1.7124 +        u.ak.res = 1;  /* Results are not equal */
  1.7125 +      }
  1.7126      }else{
  1.7127        /* SQLITE_NULLEQ is clear and at least one operand is NULL,
  1.7128        ** then the result is always NULL.
  1.7129 @@ -67570,40 +65787,40 @@
  1.7130      }
  1.7131    }else{
  1.7132      /* Neither operand is NULL.  Do a comparison. */
  1.7133 -    u.aj.affinity = pOp->p5 & SQLITE_AFF_MASK;
  1.7134 -    if( u.aj.affinity ){
  1.7135 -      applyAffinity(pIn1, u.aj.affinity, encoding);
  1.7136 -      applyAffinity(pIn3, u.aj.affinity, encoding);
  1.7137 +    u.ak.affinity = pOp->p5 & SQLITE_AFF_MASK;
  1.7138 +    if( u.ak.affinity ){
  1.7139 +      applyAffinity(pIn1, u.ak.affinity, encoding);
  1.7140 +      applyAffinity(pIn3, u.ak.affinity, encoding);
  1.7141        if( db->mallocFailed ) goto no_mem;
  1.7142      }
  1.7143  
  1.7144      assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
  1.7145      ExpandBlob(pIn1);
  1.7146      ExpandBlob(pIn3);
  1.7147 -    u.aj.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
  1.7148 +    u.ak.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
  1.7149    }
  1.7150    switch( pOp->opcode ){
  1.7151 -    case OP_Eq:    u.aj.res = u.aj.res==0;     break;
  1.7152 -    case OP_Ne:    u.aj.res = u.aj.res!=0;     break;
  1.7153 -    case OP_Lt:    u.aj.res = u.aj.res<0;      break;
  1.7154 -    case OP_Le:    u.aj.res = u.aj.res<=0;     break;
  1.7155 -    case OP_Gt:    u.aj.res = u.aj.res>0;      break;
  1.7156 -    default:       u.aj.res = u.aj.res>=0;     break;
  1.7157 +    case OP_Eq:    u.ak.res = u.ak.res==0;     break;
  1.7158 +    case OP_Ne:    u.ak.res = u.ak.res!=0;     break;
  1.7159 +    case OP_Lt:    u.ak.res = u.ak.res<0;      break;
  1.7160 +    case OP_Le:    u.ak.res = u.ak.res<=0;     break;
  1.7161 +    case OP_Gt:    u.ak.res = u.ak.res>0;      break;
  1.7162 +    default:       u.ak.res = u.ak.res>=0;     break;
  1.7163    }
  1.7164  
  1.7165    if( pOp->p5 & SQLITE_STOREP2 ){
  1.7166      pOut = &aMem[pOp->p2];
  1.7167      memAboutToChange(p, pOut);
  1.7168      MemSetTypeFlag(pOut, MEM_Int);
  1.7169 -    pOut->u.i = u.aj.res;
  1.7170 +    pOut->u.i = u.ak.res;
  1.7171      REGISTER_TRACE(pOp->p2, pOut);
  1.7172 -  }else if( u.aj.res ){
  1.7173 +  }else if( u.ak.res ){
  1.7174      pc = pOp->p2-1;
  1.7175    }
  1.7176  
  1.7177    /* Undo any changes made by applyAffinity() to the input registers. */
  1.7178 -  pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.aj.flags1&MEM_TypeMask);
  1.7179 -  pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.aj.flags3&MEM_TypeMask);
  1.7180 +  pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.ak.flags1&MEM_TypeMask);
  1.7181 +  pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.ak.flags3&MEM_TypeMask);
  1.7182    break;
  1.7183  }
  1.7184  
  1.7185 @@ -67612,9 +65829,9 @@
  1.7186  ** Set the permutation used by the OP_Compare operator to be the array
  1.7187  ** of integers in P4.
  1.7188  **
  1.7189 -** The permutation is only valid until the next OP_Permutation, OP_Compare,
  1.7190 -** OP_Halt, or OP_ResultRow.  Typically the OP_Permutation should occur
  1.7191 -** immediately prior to the OP_Compare.
  1.7192 +** The permutation is only valid until the next OP_Compare that has
  1.7193 +** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 
  1.7194 +** occur immediately prior to the OP_Compare.
  1.7195  */
  1.7196  case OP_Permutation: {
  1.7197    assert( pOp->p4type==P4_INTARRAY );
  1.7198 @@ -67623,12 +65840,17 @@
  1.7199    break;
  1.7200  }
  1.7201  
  1.7202 -/* Opcode: Compare P1 P2 P3 P4 *
  1.7203 +/* Opcode: Compare P1 P2 P3 P4 P5
  1.7204  **
  1.7205  ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
  1.7206  ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
  1.7207  ** the comparison for use by the next OP_Jump instruct.
  1.7208  **
  1.7209 +** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
  1.7210 +** determined by the most recent OP_Permutation operator.  If the
  1.7211 +** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
  1.7212 +** order.
  1.7213 +**
  1.7214  ** P4 is a KeyInfo structure that defines collating sequences and sort
  1.7215  ** orders for the comparison.  The permutation applies to registers
  1.7216  ** only.  The KeyInfo elements are used sequentially.
  1.7217 @@ -67638,7 +65860,7 @@
  1.7218  ** and strings are less than blobs.
  1.7219  */
  1.7220  case OP_Compare: {
  1.7221 -#if 0  /* local variables moved into u.ak */
  1.7222 +#if 0  /* local variables moved into u.al */
  1.7223    int n;
  1.7224    int i;
  1.7225    int p1;
  1.7226 @@ -67647,37 +65869,38 @@
  1.7227    int idx;
  1.7228    CollSeq *pColl;    /* Collating sequence to use on this term */
  1.7229    int bRev;          /* True for DESCENDING sort order */
  1.7230 -#endif /* local variables moved into u.ak */
  1.7231 -
  1.7232 -  u.ak.n = pOp->p3;
  1.7233 -  u.ak.pKeyInfo = pOp->p4.pKeyInfo;
  1.7234 -  assert( u.ak.n>0 );
  1.7235 -  assert( u.ak.pKeyInfo!=0 );
  1.7236 -  u.ak.p1 = pOp->p1;
  1.7237 -  u.ak.p2 = pOp->p2;
  1.7238 +#endif /* local variables moved into u.al */
  1.7239 +
  1.7240 +  if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
  1.7241 +  u.al.n = pOp->p3;
  1.7242 +  u.al.pKeyInfo = pOp->p4.pKeyInfo;
  1.7243 +  assert( u.al.n>0 );
  1.7244 +  assert( u.al.pKeyInfo!=0 );
  1.7245 +  u.al.p1 = pOp->p1;
  1.7246 +  u.al.p2 = pOp->p2;
  1.7247  #if SQLITE_DEBUG
  1.7248    if( aPermute ){
  1.7249      int k, mx = 0;
  1.7250 -    for(k=0; k<u.ak.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
  1.7251 -    assert( u.ak.p1>0 && u.ak.p1+mx<=p->nMem+1 );
  1.7252 -    assert( u.ak.p2>0 && u.ak.p2+mx<=p->nMem+1 );
  1.7253 -  }else{
  1.7254 -    assert( u.ak.p1>0 && u.ak.p1+u.ak.n<=p->nMem+1 );
  1.7255 -    assert( u.ak.p2>0 && u.ak.p2+u.ak.n<=p->nMem+1 );
  1.7256 +    for(k=0; k<u.al.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
  1.7257 +    assert( u.al.p1>0 && u.al.p1+mx<=p->nMem+1 );
  1.7258 +    assert( u.al.p2>0 && u.al.p2+mx<=p->nMem+1 );
  1.7259 +  }else{
  1.7260 +    assert( u.al.p1>0 && u.al.p1+u.al.n<=p->nMem+1 );
  1.7261 +    assert( u.al.p2>0 && u.al.p2+u.al.n<=p->nMem+1 );
  1.7262    }
  1.7263  #endif /* SQLITE_DEBUG */
  1.7264 -  for(u.ak.i=0; u.ak.i<u.ak.n; u.ak.i++){
  1.7265 -    u.ak.idx = aPermute ? aPermute[u.ak.i] : u.ak.i;
  1.7266 -    assert( memIsValid(&aMem[u.ak.p1+u.ak.idx]) );
  1.7267 -    assert( memIsValid(&aMem[u.ak.p2+u.ak.idx]) );
  1.7268 -    REGISTER_TRACE(u.ak.p1+u.ak.idx, &aMem[u.ak.p1+u.ak.idx]);
  1.7269 -    REGISTER_TRACE(u.ak.p2+u.ak.idx, &aMem[u.ak.p2+u.ak.idx]);
  1.7270 -    assert( u.ak.i<u.ak.pKeyInfo->nField );
  1.7271 -    u.ak.pColl = u.ak.pKeyInfo->aColl[u.ak.i];
  1.7272 -    u.ak.bRev = u.ak.pKeyInfo->aSortOrder[u.ak.i];
  1.7273 -    iCompare = sqlite3MemCompare(&aMem[u.ak.p1+u.ak.idx], &aMem[u.ak.p2+u.ak.idx], u.ak.pColl);
  1.7274 +  for(u.al.i=0; u.al.i<u.al.n; u.al.i++){
  1.7275 +    u.al.idx = aPermute ? aPermute[u.al.i] : u.al.i;
  1.7276 +    assert( memIsValid(&aMem[u.al.p1+u.al.idx]) );
  1.7277 +    assert( memIsValid(&aMem[u.al.p2+u.al.idx]) );
  1.7278 +    REGISTER_TRACE(u.al.p1+u.al.idx, &aMem[u.al.p1+u.al.idx]);
  1.7279 +    REGISTER_TRACE(u.al.p2+u.al.idx, &aMem[u.al.p2+u.al.idx]);
  1.7280 +    assert( u.al.i<u.al.pKeyInfo->nField );
  1.7281 +    u.al.pColl = u.al.pKeyInfo->aColl[u.al.i];
  1.7282 +    u.al.bRev = u.al.pKeyInfo->aSortOrder[u.al.i];
  1.7283 +    iCompare = sqlite3MemCompare(&aMem[u.al.p1+u.al.idx], &aMem[u.al.p2+u.al.idx], u.al.pColl);
  1.7284      if( iCompare ){
  1.7285 -      if( u.ak.bRev ) iCompare = -iCompare;
  1.7286 +      if( u.al.bRev ) iCompare = -iCompare;
  1.7287        break;
  1.7288      }
  1.7289    }
  1.7290 @@ -67722,35 +65945,35 @@
  1.7291  */
  1.7292  case OP_And:              /* same as TK_AND, in1, in2, out3 */
  1.7293  case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
  1.7294 -#if 0  /* local variables moved into u.al */
  1.7295 +#if 0  /* local variables moved into u.am */
  1.7296    int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
  1.7297    int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
  1.7298 -#endif /* local variables moved into u.al */
  1.7299 +#endif /* local variables moved into u.am */
  1.7300  
  1.7301    pIn1 = &aMem[pOp->p1];
  1.7302    if( pIn1->flags & MEM_Null ){
  1.7303 -    u.al.v1 = 2;
  1.7304 -  }else{
  1.7305 -    u.al.v1 = sqlite3VdbeIntValue(pIn1)!=0;
  1.7306 +    u.am.v1 = 2;
  1.7307 +  }else{
  1.7308 +    u.am.v1 = sqlite3VdbeIntValue(pIn1)!=0;
  1.7309    }
  1.7310    pIn2 = &aMem[pOp->p2];
  1.7311    if( pIn2->flags & MEM_Null ){
  1.7312 -    u.al.v2 = 2;
  1.7313 -  }else{
  1.7314 -    u.al.v2 = sqlite3VdbeIntValue(pIn2)!=0;
  1.7315 +    u.am.v2 = 2;
  1.7316 +  }else{
  1.7317 +    u.am.v2 = sqlite3VdbeIntValue(pIn2)!=0;
  1.7318    }
  1.7319    if( pOp->opcode==OP_And ){
  1.7320      static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
  1.7321 -    u.al.v1 = and_logic[u.al.v1*3+u.al.v2];
  1.7322 +    u.am.v1 = and_logic[u.am.v1*3+u.am.v2];
  1.7323    }else{
  1.7324      static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
  1.7325 -    u.al.v1 = or_logic[u.al.v1*3+u.al.v2];
  1.7326 +    u.am.v1 = or_logic[u.am.v1*3+u.am.v2];
  1.7327    }
  1.7328    pOut = &aMem[pOp->p3];
  1.7329 -  if( u.al.v1==2 ){
  1.7330 +  if( u.am.v1==2 ){
  1.7331      MemSetTypeFlag(pOut, MEM_Null);
  1.7332    }else{
  1.7333 -    pOut->u.i = u.al.v1;
  1.7334 +    pOut->u.i = u.am.v1;
  1.7335      MemSetTypeFlag(pOut, MEM_Int);
  1.7336    }
  1.7337    break;
  1.7338 @@ -67794,8 +66017,6 @@
  1.7339  **
  1.7340  ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
  1.7341  ** set the flag and fall through to the next instruction.
  1.7342 -**
  1.7343 -** See also: JumpOnce
  1.7344  */
  1.7345  case OP_Once: {             /* jump */
  1.7346    assert( pOp->p1<p->nOnceFlag );
  1.7347 @@ -67821,21 +66042,21 @@
  1.7348  */
  1.7349  case OP_If:                 /* jump, in1 */
  1.7350  case OP_IfNot: {            /* jump, in1 */
  1.7351 -#if 0  /* local variables moved into u.am */
  1.7352 +#if 0  /* local variables moved into u.an */
  1.7353    int c;
  1.7354 -#endif /* local variables moved into u.am */
  1.7355 +#endif /* local variables moved into u.an */
  1.7356    pIn1 = &aMem[pOp->p1];
  1.7357    if( pIn1->flags & MEM_Null ){
  1.7358 -    u.am.c = pOp->p3;
  1.7359 +    u.an.c = pOp->p3;
  1.7360    }else{
  1.7361  #ifdef SQLITE_OMIT_FLOATING_POINT
  1.7362 -    u.am.c = sqlite3VdbeIntValue(pIn1)!=0;
  1.7363 -#else
  1.7364 -    u.am.c = sqlite3VdbeRealValue(pIn1)!=0.0;
  1.7365 -#endif
  1.7366 -    if( pOp->opcode==OP_IfNot ) u.am.c = !u.am.c;
  1.7367 -  }
  1.7368 -  if( u.am.c ){
  1.7369 +    u.an.c = sqlite3VdbeIntValue(pIn1)!=0;
  1.7370 +#else
  1.7371 +    u.an.c = sqlite3VdbeRealValue(pIn1)!=0.0;
  1.7372 +#endif
  1.7373 +    if( pOp->opcode==OP_IfNot ) u.an.c = !u.an.c;
  1.7374 +  }
  1.7375 +  if( u.an.c ){
  1.7376      pc = pOp->p2-1;
  1.7377    }
  1.7378    break;
  1.7379 @@ -67890,7 +66111,7 @@
  1.7380  ** skipped for length() and all content loading can be skipped for typeof().
  1.7381  */
  1.7382  case OP_Column: {
  1.7383 -#if 0  /* local variables moved into u.an */
  1.7384 +#if 0  /* local variables moved into u.ao */
  1.7385    u32 payloadSize;   /* Number of bytes in the record */
  1.7386    i64 payloadSize64; /* Number of bytes in the record */
  1.7387    int p1;            /* P1 value of the opcode */
  1.7388 @@ -67914,126 +66135,131 @@
  1.7389    int avail;         /* Number of bytes of available data */
  1.7390    u32 t;             /* A type code from the record header */
  1.7391    Mem *pReg;         /* PseudoTable input register */
  1.7392 -#endif /* local variables moved into u.an */
  1.7393 -
  1.7394 -
  1.7395 -  u.an.p1 = pOp->p1;
  1.7396 -  u.an.p2 = pOp->p2;
  1.7397 -  u.an.pC = 0;
  1.7398 -  memset(&u.an.sMem, 0, sizeof(u.an.sMem));
  1.7399 -  assert( u.an.p1<p->nCursor );
  1.7400 +#endif /* local variables moved into u.ao */
  1.7401 +
  1.7402 +
  1.7403 +  u.ao.p1 = pOp->p1;
  1.7404 +  u.ao.p2 = pOp->p2;
  1.7405 +  u.ao.pC = 0;
  1.7406 +  memset(&u.ao.sMem, 0, sizeof(u.ao.sMem));
  1.7407 +  assert( u.ao.p1<p->nCursor );
  1.7408    assert( pOp->p3>0 && pOp->p3<=p->nMem );
  1.7409 -  u.an.pDest = &aMem[pOp->p3];
  1.7410 -  memAboutToChange(p, u.an.pDest);
  1.7411 -  u.an.zRec = 0;
  1.7412 -
  1.7413 -  /* This block sets the variable u.an.payloadSize to be the total number of
  1.7414 +  u.ao.pDest = &aMem[pOp->p3];
  1.7415 +  memAboutToChange(p, u.ao.pDest);
  1.7416 +  u.ao.zRec = 0;
  1.7417 +
  1.7418 +  /* This block sets the variable u.ao.payloadSize to be the total number of
  1.7419    ** bytes in the record.
  1.7420    **
  1.7421 -  ** u.an.zRec is set to be the complete text of the record if it is available.
  1.7422 +  ** u.ao.zRec is set to be the complete text of the record if it is available.
  1.7423    ** The complete record text is always available for pseudo-tables
  1.7424    ** If the record is stored in a cursor, the complete record text
  1.7425 -  ** might be available in the  u.an.pC->aRow cache.  Or it might not be.
  1.7426 -  ** If the data is unavailable,  u.an.zRec is set to NULL.
  1.7427 +  ** might be available in the  u.ao.pC->aRow cache.  Or it might not be.
  1.7428 +  ** If the data is unavailable,  u.ao.zRec is set to NULL.
  1.7429    **
  1.7430    ** We also compute the number of columns in the record.  For cursors,
  1.7431    ** the number of columns is stored in the VdbeCursor.nField element.
  1.7432    */
  1.7433 -  u.an.pC = p->apCsr[u.an.p1];
  1.7434 -  assert( u.an.pC!=0 );
  1.7435 +  u.ao.pC = p->apCsr[u.ao.p1];
  1.7436 +  assert( u.ao.pC!=0 );
  1.7437  #ifndef SQLITE_OMIT_VIRTUALTABLE
  1.7438 -  assert( u.an.pC->pVtabCursor==0 );
  1.7439 -#endif
  1.7440 -  u.an.pCrsr = u.an.pC->pCursor;
  1.7441 -  if( u.an.pCrsr!=0 ){
  1.7442 +  assert( u.ao.pC->pVtabCursor==0 );
  1.7443 +#endif
  1.7444 +  u.ao.pCrsr = u.ao.pC->pCursor;
  1.7445 +  if( u.ao.pCrsr!=0 ){
  1.7446      /* The record is stored in a B-Tree */
  1.7447 -    rc = sqlite3VdbeCursorMoveto(u.an.pC);
  1.7448 +    rc = sqlite3VdbeCursorMoveto(u.ao.pC);
  1.7449      if( rc ) goto abort_due_to_error;
  1.7450 -    if( u.an.pC->nullRow ){
  1.7451 -      u.an.payloadSize = 0;
  1.7452 -    }else if( u.an.pC->cacheStatus==p->cacheCtr ){
  1.7453 -      u.an.payloadSize = u.an.pC->payloadSize;
  1.7454 -      u.an.zRec = (char*)u.an.pC->aRow;
  1.7455 -    }else if( u.an.pC->isIndex ){
  1.7456 -      assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
  1.7457 -      VVA_ONLY(rc =) sqlite3BtreeKeySize(u.an.pCrsr, &u.an.payloadSize64);
  1.7458 +    if( u.ao.pC->nullRow ){
  1.7459 +      u.ao.payloadSize = 0;
  1.7460 +    }else if( u.ao.pC->cacheStatus==p->cacheCtr ){
  1.7461 +      u.ao.payloadSize = u.ao.pC->payloadSize;
  1.7462 +      u.ao.zRec = (char*)u.ao.pC->aRow;
  1.7463 +    }else if( u.ao.pC->isIndex ){
  1.7464 +      assert( sqlite3BtreeCursorIsValid(u.ao.pCrsr) );
  1.7465 +      VVA_ONLY(rc =) sqlite3BtreeKeySize(u.ao.pCrsr, &u.ao.payloadSize64);
  1.7466        assert( rc==SQLITE_OK );   /* True because of CursorMoveto() call above */
  1.7467        /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
  1.7468 -      ** payload size, so it is impossible for u.an.payloadSize64 to be
  1.7469 +      ** payload size, so it is impossible for u.ao.payloadSize64 to be
  1.7470        ** larger than 32 bits. */
  1.7471 -      assert( (u.an.payloadSize64 & SQLITE_MAX_U32)==(u64)u.an.payloadSize64 );
  1.7472 -      u.an.payloadSize = (u32)u.an.payloadSize64;
  1.7473 -    }else{
  1.7474 -      assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
  1.7475 -      VVA_ONLY(rc =) sqlite3BtreeDataSize(u.an.pCrsr, &u.an.payloadSize);
  1.7476 +      assert( (u.ao.payloadSize64 & SQLITE_MAX_U32)==(u64)u.ao.payloadSize64 );
  1.7477 +      u.ao.payloadSize = (u32)u.ao.payloadSize64;
  1.7478 +    }else{
  1.7479 +      assert( sqlite3BtreeCursorIsValid(u.ao.pCrsr) );
  1.7480 +      VVA_ONLY(rc =) sqlite3BtreeDataSize(u.ao.pCrsr, &u.ao.payloadSize);
  1.7481        assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
  1.7482      }
  1.7483 -  }else if( ALWAYS(u.an.pC->pseudoTableReg>0) ){
  1.7484 -    u.an.pReg = &aMem[u.an.pC->pseudoTableReg];
  1.7485 -    assert( u.an.pReg->flags & MEM_Blob );
  1.7486 -    assert( memIsValid(u.an.pReg) );
  1.7487 -    u.an.payloadSize = u.an.pReg->n;
  1.7488 -    u.an.zRec = u.an.pReg->z;
  1.7489 -    u.an.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
  1.7490 -    assert( u.an.payloadSize==0 || u.an.zRec!=0 );
  1.7491 +  }else if( ALWAYS(u.ao.pC->pseudoTableReg>0) ){
  1.7492 +    u.ao.pReg = &aMem[u.ao.pC->pseudoTableReg];
  1.7493 +    if( u.ao.pC->multiPseudo ){
  1.7494 +      sqlite3VdbeMemShallowCopy(u.ao.pDest, u.ao.pReg+u.ao.p2, MEM_Ephem);
  1.7495 +      Deephemeralize(u.ao.pDest);
  1.7496 +      goto op_column_out;
  1.7497 +    }
  1.7498 +    assert( u.ao.pReg->flags & MEM_Blob );
  1.7499 +    assert( memIsValid(u.ao.pReg) );
  1.7500 +    u.ao.payloadSize = u.ao.pReg->n;
  1.7501 +    u.ao.zRec = u.ao.pReg->z;
  1.7502 +    u.ao.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
  1.7503 +    assert( u.ao.payloadSize==0 || u.ao.zRec!=0 );
  1.7504    }else{
  1.7505      /* Consider the row to be NULL */
  1.7506 -    u.an.payloadSize = 0;
  1.7507 -  }
  1.7508 -
  1.7509 -  /* If u.an.payloadSize is 0, then just store a NULL.  This can happen because of
  1.7510 +    u.ao.payloadSize = 0;
  1.7511 +  }
  1.7512 +
  1.7513 +  /* If u.ao.payloadSize is 0, then just store a NULL.  This can happen because of
  1.7514    ** nullRow or because of a corrupt database. */
  1.7515 -  if( u.an.payloadSize==0 ){
  1.7516 -    MemSetTypeFlag(u.an.pDest, MEM_Null);
  1.7517 +  if( u.ao.payloadSize==0 ){
  1.7518 +    MemSetTypeFlag(u.ao.pDest, MEM_Null);
  1.7519      goto op_column_out;
  1.7520    }
  1.7521    assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
  1.7522 -  if( u.an.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1.7523 +  if( u.ao.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1.7524      goto too_big;
  1.7525    }
  1.7526  
  1.7527 -  u.an.nField = u.an.pC->nField;
  1.7528 -  assert( u.an.p2<u.an.nField );
  1.7529 +  u.ao.nField = u.ao.pC->nField;
  1.7530 +  assert( u.ao.p2<u.ao.nField );
  1.7531  
  1.7532    /* Read and parse the table header.  Store the results of the parse
  1.7533    ** into the record header cache fields of the cursor.
  1.7534    */
  1.7535 -  u.an.aType = u.an.pC->aType;
  1.7536 -  if( u.an.pC->cacheStatus==p->cacheCtr ){
  1.7537 -    u.an.aOffset = u.an.pC->aOffset;
  1.7538 -  }else{
  1.7539 -    assert(u.an.aType);
  1.7540 -    u.an.avail = 0;
  1.7541 -    u.an.pC->aOffset = u.an.aOffset = &u.an.aType[u.an.nField];
  1.7542 -    u.an.pC->payloadSize = u.an.payloadSize;
  1.7543 -    u.an.pC->cacheStatus = p->cacheCtr;
  1.7544 +  u.ao.aType = u.ao.pC->aType;
  1.7545 +  if( u.ao.pC->cacheStatus==p->cacheCtr ){
  1.7546 +    u.ao.aOffset = u.ao.pC->aOffset;
  1.7547 +  }else{
  1.7548 +    assert(u.ao.aType);
  1.7549 +    u.ao.avail = 0;
  1.7550 +    u.ao.pC->aOffset = u.ao.aOffset = &u.ao.aType[u.ao.nField];
  1.7551 +    u.ao.pC->payloadSize = u.ao.payloadSize;
  1.7552 +    u.ao.pC->cacheStatus = p->cacheCtr;
  1.7553  
  1.7554      /* Figure out how many bytes are in the header */
  1.7555 -    if( u.an.zRec ){
  1.7556 -      u.an.zData = u.an.zRec;
  1.7557 -    }else{
  1.7558 -      if( u.an.pC->isIndex ){
  1.7559 -        u.an.zData = (char*)sqlite3BtreeKeyFetch(u.an.pCrsr, &u.an.avail);
  1.7560 -      }else{
  1.7561 -        u.an.zData = (char*)sqlite3BtreeDataFetch(u.an.pCrsr, &u.an.avail);
  1.7562 +    if( u.ao.zRec ){
  1.7563 +      u.ao.zData = u.ao.zRec;
  1.7564 +    }else{
  1.7565 +      if( u.ao.pC->isIndex ){
  1.7566 +        u.ao.zData = (char*)sqlite3BtreeKeyFetch(u.ao.pCrsr, &u.ao.avail);
  1.7567 +      }else{
  1.7568 +        u.ao.zData = (char*)sqlite3BtreeDataFetch(u.ao.pCrsr, &u.ao.avail);
  1.7569        }
  1.7570        /* If KeyFetch()/DataFetch() managed to get the entire payload,
  1.7571 -      ** save the payload in the u.an.pC->aRow cache.  That will save us from
  1.7572 +      ** save the payload in the u.ao.pC->aRow cache.  That will save us from
  1.7573        ** having to make additional calls to fetch the content portion of
  1.7574        ** the record.
  1.7575        */
  1.7576 -      assert( u.an.avail>=0 );
  1.7577 -      if( u.an.payloadSize <= (u32)u.an.avail ){
  1.7578 -        u.an.zRec = u.an.zData;
  1.7579 -        u.an.pC->aRow = (u8*)u.an.zData;
  1.7580 -      }else{
  1.7581 -        u.an.pC->aRow = 0;
  1.7582 +      assert( u.ao.avail>=0 );
  1.7583 +      if( u.ao.payloadSize <= (u32)u.ao.avail ){
  1.7584 +        u.ao.zRec = u.ao.zData;
  1.7585 +        u.ao.pC->aRow = (u8*)u.ao.zData;
  1.7586 +      }else{
  1.7587 +        u.ao.pC->aRow = 0;
  1.7588        }
  1.7589      }
  1.7590      /* The following assert is true in all cases except when
  1.7591      ** the database file has been corrupted externally.
  1.7592 -    **    assert( u.an.zRec!=0 || u.an.avail>=u.an.payloadSize || u.an.avail>=9 ); */
  1.7593 -    u.an.szHdr = getVarint32((u8*)u.an.zData, u.an.offset);
  1.7594 +    **    assert( u.ao.zRec!=0 || u.ao.avail>=u.ao.payloadSize || u.ao.avail>=9 ); */
  1.7595 +    u.ao.szHdr = getVarint32((u8*)u.ao.zData, u.ao.offset);
  1.7596  
  1.7597      /* Make sure a corrupt database has not given us an oversize header.
  1.7598      ** Do this now to avoid an oversize memory allocation.
  1.7599 @@ -68044,26 +66270,26 @@
  1.7600      ** 3-byte type for each of the maximum of 32768 columns plus three
  1.7601      ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
  1.7602      */
  1.7603 -    if( u.an.offset > 98307 ){
  1.7604 +    if( u.ao.offset > 98307 ){
  1.7605        rc = SQLITE_CORRUPT_BKPT;
  1.7606        goto op_column_out;
  1.7607      }
  1.7608  
  1.7609 -    /* Compute in u.an.len the number of bytes of data we need to read in order
  1.7610 -    ** to get u.an.nField type values.  u.an.offset is an upper bound on this.  But
  1.7611 -    ** u.an.nField might be significantly less than the true number of columns
  1.7612 -    ** in the table, and in that case, 5*u.an.nField+3 might be smaller than u.an.offset.
  1.7613 -    ** We want to minimize u.an.len in order to limit the size of the memory
  1.7614 -    ** allocation, especially if a corrupt database file has caused u.an.offset
  1.7615 +    /* Compute in u.ao.len the number of bytes of data we need to read in order
  1.7616 +    ** to get u.ao.nField type values.  u.ao.offset is an upper bound on this.  But
  1.7617 +    ** u.ao.nField might be significantly less than the true number of columns
  1.7618 +    ** in the table, and in that case, 5*u.ao.nField+3 might be smaller than u.ao.offset.
  1.7619 +    ** We want to minimize u.ao.len in order to limit the size of the memory
  1.7620 +    ** allocation, especially if a corrupt database file has caused u.ao.offset
  1.7621      ** to be oversized. Offset is limited to 98307 above.  But 98307 might
  1.7622      ** still exceed Robson memory allocation limits on some configurations.
  1.7623 -    ** On systems that cannot tolerate large memory allocations, u.an.nField*5+3
  1.7624 -    ** will likely be much smaller since u.an.nField will likely be less than
  1.7625 +    ** On systems that cannot tolerate large memory allocations, u.ao.nField*5+3
  1.7626 +    ** will likely be much smaller since u.ao.nField will likely be less than
  1.7627      ** 20 or so.  This insures that Robson memory allocation limits are
  1.7628      ** not exceeded even for corrupt database files.
  1.7629      */
  1.7630 -    u.an.len = u.an.nField*5 + 3;
  1.7631 -    if( u.an.len > (int)u.an.offset ) u.an.len = (int)u.an.offset;
  1.7632 +    u.ao.len = u.ao.nField*5 + 3;
  1.7633 +    if( u.ao.len > (int)u.ao.offset ) u.ao.len = (int)u.ao.offset;
  1.7634  
  1.7635      /* The KeyFetch() or DataFetch() above are fast and will get the entire
  1.7636      ** record header in most cases.  But they will fail to get the complete
  1.7637 @@ -68071,51 +66297,51 @@
  1.7638      ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to
  1.7639      ** acquire the complete header text.
  1.7640      */
  1.7641 -    if( !u.an.zRec && u.an.avail<u.an.len ){
  1.7642 -      u.an.sMem.flags = 0;
  1.7643 -      u.an.sMem.db = 0;
  1.7644 -      rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, 0, u.an.len, u.an.pC->isIndex, &u.an.sMem);
  1.7645 +    if( !u.ao.zRec && u.ao.avail<u.ao.len ){
  1.7646 +      u.ao.sMem.flags = 0;
  1.7647 +      u.ao.sMem.db = 0;
  1.7648 +      rc = sqlite3VdbeMemFromBtree(u.ao.pCrsr, 0, u.ao.len, u.ao.pC->isIndex, &u.ao.sMem);
  1.7649        if( rc!=SQLITE_OK ){
  1.7650          goto op_column_out;
  1.7651        }
  1.7652 -      u.an.zData = u.an.sMem.z;
  1.7653 -    }
  1.7654 -    u.an.zEndHdr = (u8 *)&u.an.zData[u.an.len];
  1.7655 -    u.an.zIdx = (u8 *)&u.an.zData[u.an.szHdr];
  1.7656 -
  1.7657 -    /* Scan the header and use it to fill in the u.an.aType[] and u.an.aOffset[]
  1.7658 -    ** arrays.  u.an.aType[u.an.i] will contain the type integer for the u.an.i-th
  1.7659 -    ** column and u.an.aOffset[u.an.i] will contain the u.an.offset from the beginning
  1.7660 -    ** of the record to the start of the data for the u.an.i-th column
  1.7661 -    */
  1.7662 -    for(u.an.i=0; u.an.i<u.an.nField; u.an.i++){
  1.7663 -      if( u.an.zIdx<u.an.zEndHdr ){
  1.7664 -        u.an.aOffset[u.an.i] = u.an.offset;
  1.7665 -        if( u.an.zIdx[0]<0x80 ){
  1.7666 -          u.an.t = u.an.zIdx[0];
  1.7667 -          u.an.zIdx++;
  1.7668 +      u.ao.zData = u.ao.sMem.z;
  1.7669 +    }
  1.7670 +    u.ao.zEndHdr = (u8 *)&u.ao.zData[u.ao.len];
  1.7671 +    u.ao.zIdx = (u8 *)&u.ao.zData[u.ao.szHdr];
  1.7672 +
  1.7673 +    /* Scan the header and use it to fill in the u.ao.aType[] and u.ao.aOffset[]
  1.7674 +    ** arrays.  u.ao.aType[u.ao.i] will contain the type integer for the u.ao.i-th
  1.7675 +    ** column and u.ao.aOffset[u.ao.i] will contain the u.ao.offset from the beginning
  1.7676 +    ** of the record to the start of the data for the u.ao.i-th column
  1.7677 +    */
  1.7678 +    for(u.ao.i=0; u.ao.i<u.ao.nField; u.ao.i++){
  1.7679 +      if( u.ao.zIdx<u.ao.zEndHdr ){
  1.7680 +        u.ao.aOffset[u.ao.i] = u.ao.offset;
  1.7681 +        if( u.ao.zIdx[0]<0x80 ){
  1.7682 +          u.ao.t = u.ao.zIdx[0];
  1.7683 +          u.ao.zIdx++;
  1.7684          }else{
  1.7685 -          u.an.zIdx += sqlite3GetVarint32(u.an.zIdx, &u.an.t);
  1.7686 -        }
  1.7687 -        u.an.aType[u.an.i] = u.an.t;
  1.7688 -        u.an.szField = sqlite3VdbeSerialTypeLen(u.an.t);
  1.7689 -        u.an.offset += u.an.szField;
  1.7690 -        if( u.an.offset<u.an.szField ){  /* True if u.an.offset overflows */
  1.7691 -          u.an.zIdx = &u.an.zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
  1.7692 +          u.ao.zIdx += sqlite3GetVarint32(u.ao.zIdx, &u.ao.t);
  1.7693 +        }
  1.7694 +        u.ao.aType[u.ao.i] = u.ao.t;
  1.7695 +        u.ao.szField = sqlite3VdbeSerialTypeLen(u.ao.t);
  1.7696 +        u.ao.offset += u.ao.szField;
  1.7697 +        if( u.ao.offset<u.ao.szField ){  /* True if u.ao.offset overflows */
  1.7698 +          u.ao.zIdx = &u.ao.zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
  1.7699            break;
  1.7700          }
  1.7701        }else{
  1.7702 -        /* If u.an.i is less that u.an.nField, then there are fewer fields in this
  1.7703 +        /* If u.ao.i is less that u.ao.nField, then there are fewer fields in this
  1.7704          ** record than SetNumColumns indicated there are columns in the
  1.7705 -        ** table. Set the u.an.offset for any extra columns not present in
  1.7706 +        ** table. Set the u.ao.offset for any extra columns not present in
  1.7707          ** the record to 0. This tells code below to store the default value
  1.7708          ** for the column instead of deserializing a value from the record.
  1.7709          */
  1.7710 -        u.an.aOffset[u.an.i] = 0;
  1.7711 -      }
  1.7712 -    }
  1.7713 -    sqlite3VdbeMemRelease(&u.an.sMem);
  1.7714 -    u.an.sMem.flags = MEM_Null;
  1.7715 +        u.ao.aOffset[u.ao.i] = 0;
  1.7716 +      }
  1.7717 +    }
  1.7718 +    sqlite3VdbeMemRelease(&u.ao.sMem);
  1.7719 +    u.ao.sMem.flags = MEM_Null;
  1.7720  
  1.7721      /* If we have read more header data than was contained in the header,
  1.7722      ** or if the end of the last field appears to be past the end of the
  1.7723 @@ -68123,78 +66349,78 @@
  1.7724      ** of the record (when all fields present), then we must be dealing
  1.7725      ** with a corrupt database.
  1.7726      */
  1.7727 -    if( (u.an.zIdx > u.an.zEndHdr) || (u.an.offset > u.an.payloadSize)
  1.7728 -         || (u.an.zIdx==u.an.zEndHdr && u.an.offset!=u.an.payloadSize) ){
  1.7729 +    if( (u.ao.zIdx > u.ao.zEndHdr) || (u.ao.offset > u.ao.payloadSize)
  1.7730 +         || (u.ao.zIdx==u.ao.zEndHdr && u.ao.offset!=u.ao.payloadSize) ){
  1.7731        rc = SQLITE_CORRUPT_BKPT;
  1.7732        goto op_column_out;
  1.7733      }
  1.7734    }
  1.7735  
  1.7736 -  /* Get the column information. If u.an.aOffset[u.an.p2] is non-zero, then
  1.7737 -  ** deserialize the value from the record. If u.an.aOffset[u.an.p2] is zero,
  1.7738 +  /* Get the column information. If u.ao.aOffset[u.ao.p2] is non-zero, then
  1.7739 +  ** deserialize the value from the record. If u.ao.aOffset[u.ao.p2] is zero,
  1.7740    ** then there are not enough fields in the record to satisfy the
  1.7741    ** request.  In this case, set the value NULL or to P4 if P4 is
  1.7742    ** a pointer to a Mem object.
  1.7743    */
  1.7744 -  if( u.an.aOffset[u.an.p2] ){
  1.7745 +  if( u.ao.aOffset[u.ao.p2] ){
  1.7746      assert( rc==SQLITE_OK );
  1.7747 -    if( u.an.zRec ){
  1.7748 +    if( u.ao.zRec ){
  1.7749        /* This is the common case where the whole row fits on a single page */
  1.7750 -      VdbeMemRelease(u.an.pDest);
  1.7751 -      sqlite3VdbeSerialGet((u8 *)&u.an.zRec[u.an.aOffset[u.an.p2]], u.an.aType[u.an.p2], u.an.pDest);
  1.7752 +      VdbeMemRelease(u.ao.pDest);
  1.7753 +      sqlite3VdbeSerialGet((u8 *)&u.ao.zRec[u.ao.aOffset[u.ao.p2]], u.ao.aType[u.ao.p2], u.ao.pDest);
  1.7754      }else{
  1.7755        /* This branch happens only when the row overflows onto multiple pages */
  1.7756 -      u.an.t = u.an.aType[u.an.p2];
  1.7757 +      u.ao.t = u.ao.aType[u.ao.p2];
  1.7758        if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
  1.7759 -       && ((u.an.t>=12 && (u.an.t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
  1.7760 +       && ((u.ao.t>=12 && (u.ao.t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
  1.7761        ){
  1.7762          /* Content is irrelevant for the typeof() function and for
  1.7763          ** the length(X) function if X is a blob.  So we might as well use
  1.7764          ** bogus content rather than reading content from disk.  NULL works
  1.7765 -        ** for text and blob and whatever is in the u.an.payloadSize64 variable
  1.7766 +        ** for text and blob and whatever is in the u.ao.payloadSize64 variable
  1.7767          ** will work for everything else. */
  1.7768 -        u.an.zData = u.an.t<12 ? (char*)&u.an.payloadSize64 : 0;
  1.7769 -      }else{
  1.7770 -        u.an.len = sqlite3VdbeSerialTypeLen(u.an.t);
  1.7771 -        sqlite3VdbeMemMove(&u.an.sMem, u.an.pDest);
  1.7772 -        rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, u.an.aOffset[u.an.p2], u.an.len,  u.an.pC->isIndex,
  1.7773 -                                     &u.an.sMem);
  1.7774 +        u.ao.zData = u.ao.t<12 ? (char*)&u.ao.payloadSize64 : 0;
  1.7775 +      }else{
  1.7776 +        u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.t);
  1.7777 +        sqlite3VdbeMemMove(&u.ao.sMem, u.ao.pDest);
  1.7778 +        rc = sqlite3VdbeMemFromBtree(u.ao.pCrsr, u.ao.aOffset[u.ao.p2], u.ao.len,  u.ao.pC->isIndex,
  1.7779 +                                     &u.ao.sMem);
  1.7780          if( rc!=SQLITE_OK ){
  1.7781            goto op_column_out;
  1.7782          }
  1.7783 -        u.an.zData = u.an.sMem.z;
  1.7784 -      }
  1.7785 -      sqlite3VdbeSerialGet((u8*)u.an.zData, u.an.t, u.an.pDest);
  1.7786 -    }
  1.7787 -    u.an.pDest->enc = encoding;
  1.7788 +        u.ao.zData = u.ao.sMem.z;
  1.7789 +      }
  1.7790 +      sqlite3VdbeSerialGet((u8*)u.ao.zData, u.ao.t, u.ao.pDest);
  1.7791 +    }
  1.7792 +    u.ao.pDest->enc = encoding;
  1.7793    }else{
  1.7794      if( pOp->p4type==P4_MEM ){
  1.7795 -      sqlite3VdbeMemShallowCopy(u.an.pDest, pOp->p4.pMem, MEM_Static);
  1.7796 -    }else{
  1.7797 -      MemSetTypeFlag(u.an.pDest, MEM_Null);
  1.7798 +      sqlite3VdbeMemShallowCopy(u.ao.pDest, pOp->p4.pMem, MEM_Static);
  1.7799 +    }else{
  1.7800 +      MemSetTypeFlag(u.ao.pDest, MEM_Null);
  1.7801      }
  1.7802    }
  1.7803  
  1.7804    /* If we dynamically allocated space to hold the data (in the
  1.7805    ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
  1.7806 -  ** dynamically allocated space over to the u.an.pDest structure.
  1.7807 +  ** dynamically allocated space over to the u.ao.pDest structure.
  1.7808    ** This prevents a memory copy.
  1.7809    */
  1.7810 -  if( u.an.sMem.zMalloc ){
  1.7811 -    assert( u.an.sMem.z==u.an.sMem.zMalloc );
  1.7812 -    assert( !(u.an.pDest->flags & MEM_Dyn) );
  1.7813 -    assert( !(u.an.pDest->flags & (MEM_Blob|MEM_Str)) || u.an.pDest->z==u.an.sMem.z );
  1.7814 -    u.an.pDest->flags &= ~(MEM_Ephem|MEM_Static);
  1.7815 -    u.an.pDest->flags |= MEM_Term;
  1.7816 -    u.an.pDest->z = u.an.sMem.z;
  1.7817 -    u.an.pDest->zMalloc = u.an.sMem.zMalloc;
  1.7818 -  }
  1.7819 -
  1.7820 -  rc = sqlite3VdbeMemMakeWriteable(u.an.pDest);
  1.7821 +  if( u.ao.sMem.zMalloc ){
  1.7822 +    assert( u.ao.sMem.z==u.ao.sMem.zMalloc );
  1.7823 +    assert( !(u.ao.pDest->flags & MEM_Dyn) );
  1.7824 +    assert( !(u.ao.pDest->flags & (MEM_Blob|MEM_Str)) || u.ao.pDest->z==u.ao.sMem.z );
  1.7825 +    u.ao.pDest->flags &= ~(MEM_Ephem|MEM_Static);
  1.7826 +    u.ao.pDest->flags |= MEM_Term;
  1.7827 +    u.ao.pDest->z = u.ao.sMem.z;
  1.7828 +    u.ao.pDest->zMalloc = u.ao.sMem.zMalloc;
  1.7829 +  }
  1.7830 +
  1.7831 +  rc = sqlite3VdbeMemMakeWriteable(u.ao.pDest);
  1.7832  
  1.7833  op_column_out:
  1.7834 -  UPDATE_MAX_BLOBSIZE(u.an.pDest);
  1.7835 -  REGISTER_TRACE(pOp->p3, u.an.pDest);
  1.7836 +  UPDATE_MAX_BLOBSIZE(u.ao.pDest);
  1.7837 +  REGISTER_TRACE(pOp->p3, u.ao.pDest);
  1.7838    break;
  1.7839  }
  1.7840  
  1.7841 @@ -68207,20 +66433,20 @@
  1.7842  ** memory cell in the range.
  1.7843  */
  1.7844  case OP_Affinity: {
  1.7845 -#if 0  /* local variables moved into u.ao */
  1.7846 +#if 0  /* local variables moved into u.ap */
  1.7847    const char *zAffinity;   /* The affinity to be applied */
  1.7848    char cAff;               /* A single character of affinity */
  1.7849 -#endif /* local variables moved into u.ao */
  1.7850 -
  1.7851 -  u.ao.zAffinity = pOp->p4.z;
  1.7852 -  assert( u.ao.zAffinity!=0 );
  1.7853 -  assert( u.ao.zAffinity[pOp->p2]==0 );
  1.7854 +#endif /* local variables moved into u.ap */
  1.7855 +
  1.7856 +  u.ap.zAffinity = pOp->p4.z;
  1.7857 +  assert( u.ap.zAffinity!=0 );
  1.7858 +  assert( u.ap.zAffinity[pOp->p2]==0 );
  1.7859    pIn1 = &aMem[pOp->p1];
  1.7860 -  while( (u.ao.cAff = *(u.ao.zAffinity++))!=0 ){
  1.7861 +  while( (u.ap.cAff = *(u.ap.zAffinity++))!=0 ){
  1.7862      assert( pIn1 <= &p->aMem[p->nMem] );
  1.7863      assert( memIsValid(pIn1) );
  1.7864      ExpandBlob(pIn1);
  1.7865 -    applyAffinity(pIn1, u.ao.cAff, encoding);
  1.7866 +    applyAffinity(pIn1, u.ap.cAff, encoding);
  1.7867      pIn1++;
  1.7868    }
  1.7869    break;
  1.7870 @@ -68242,7 +66468,7 @@
  1.7871  ** If P4 is NULL then all index fields have the affinity NONE.
  1.7872  */
  1.7873  case OP_MakeRecord: {
  1.7874 -#if 0  /* local variables moved into u.ap */
  1.7875 +#if 0  /* local variables moved into u.aq */
  1.7876    u8 *zNewRecord;        /* A buffer to hold the data for the new record */
  1.7877    Mem *pRec;             /* The new record */
  1.7878    u64 nData;             /* Number of bytes of data space */
  1.7879 @@ -68258,7 +66484,7 @@
  1.7880    int file_format;       /* File format to use for encoding */
  1.7881    int i;                 /* Space used in zNewRecord[] */
  1.7882    int len;               /* Length of a field */
  1.7883 -#endif /* local variables moved into u.ap */
  1.7884 +#endif /* local variables moved into u.aq */
  1.7885  
  1.7886    /* Assuming the record contains N fields, the record format looks
  1.7887    ** like this:
  1.7888 @@ -68275,16 +66501,16 @@
  1.7889    ** hdr-size field is also a varint which is the offset from the beginning
  1.7890    ** of the record to data0.
  1.7891    */
  1.7892 -  u.ap.nData = 0;         /* Number of bytes of data space */
  1.7893 -  u.ap.nHdr = 0;          /* Number of bytes of header space */
  1.7894 -  u.ap.nZero = 0;         /* Number of zero bytes at the end of the record */
  1.7895 -  u.ap.nField = pOp->p1;
  1.7896 -  u.ap.zAffinity = pOp->p4.z;
  1.7897 -  assert( u.ap.nField>0 && pOp->p2>0 && pOp->p2+u.ap.nField<=p->nMem+1 );
  1.7898 -  u.ap.pData0 = &aMem[u.ap.nField];
  1.7899 -  u.ap.nField = pOp->p2;
  1.7900 -  u.ap.pLast = &u.ap.pData0[u.ap.nField-1];
  1.7901 -  u.ap.file_format = p->minWriteFileFormat;
  1.7902 +  u.aq.nData = 0;         /* Number of bytes of data space */
  1.7903 +  u.aq.nHdr = 0;          /* Number of bytes of header space */
  1.7904 +  u.aq.nZero = 0;         /* Number of zero bytes at the end of the record */
  1.7905 +  u.aq.nField = pOp->p1;
  1.7906 +  u.aq.zAffinity = pOp->p4.z;
  1.7907 +  assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=p->nMem+1 );
  1.7908 +  u.aq.pData0 = &aMem[u.aq.nField];
  1.7909 +  u.aq.nField = pOp->p2;
  1.7910 +  u.aq.pLast = &u.aq.pData0[u.aq.nField-1];
  1.7911 +  u.aq.file_format = p->minWriteFileFormat;
  1.7912  
  1.7913    /* Identify the output register */
  1.7914    assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
  1.7915 @@ -68294,34 +66520,34 @@
  1.7916    /* Loop through the elements that will make up the record to figure
  1.7917    ** out how much space is required for the new record.
  1.7918    */
  1.7919 -  for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
  1.7920 -    assert( memIsValid(u.ap.pRec) );
  1.7921 -    if( u.ap.zAffinity ){
  1.7922 -      applyAffinity(u.ap.pRec, u.ap.zAffinity[u.ap.pRec-u.ap.pData0], encoding);
  1.7923 -    }
  1.7924 -    if( u.ap.pRec->flags&MEM_Zero && u.ap.pRec->n>0 ){
  1.7925 -      sqlite3VdbeMemExpandBlob(u.ap.pRec);
  1.7926 -    }
  1.7927 -    u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
  1.7928 -    u.ap.len = sqlite3VdbeSerialTypeLen(u.ap.serial_type);
  1.7929 -    u.ap.nData += u.ap.len;
  1.7930 -    u.ap.nHdr += sqlite3VarintLen(u.ap.serial_type);
  1.7931 -    if( u.ap.pRec->flags & MEM_Zero ){
  1.7932 +  for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){
  1.7933 +    assert( memIsValid(u.aq.pRec) );
  1.7934 +    if( u.aq.zAffinity ){
  1.7935 +      applyAffinity(u.aq.pRec, u.aq.zAffinity[u.aq.pRec-u.aq.pData0], encoding);
  1.7936 +    }
  1.7937 +    if( u.aq.pRec->flags&MEM_Zero && u.aq.pRec->n>0 ){
  1.7938 +      sqlite3VdbeMemExpandBlob(u.aq.pRec);
  1.7939 +    }
  1.7940 +    u.aq.serial_type = sqlite3VdbeSerialType(u.aq.pRec, u.aq.file_format);
  1.7941 +    u.aq.len = sqlite3VdbeSerialTypeLen(u.aq.serial_type);
  1.7942 +    u.aq.nData += u.aq.len;
  1.7943 +    u.aq.nHdr += sqlite3VarintLen(u.aq.serial_type);
  1.7944 +    if( u.aq.pRec->flags & MEM_Zero ){
  1.7945        /* Only pure zero-filled BLOBs can be input to this Opcode.
  1.7946        ** We do not allow blobs with a prefix and a zero-filled tail. */
  1.7947 -      u.ap.nZero += u.ap.pRec->u.nZero;
  1.7948 -    }else if( u.ap.len ){
  1.7949 -      u.ap.nZero = 0;
  1.7950 +      u.aq.nZero += u.aq.pRec->u.nZero;
  1.7951 +    }else if( u.aq.len ){
  1.7952 +      u.aq.nZero = 0;
  1.7953      }
  1.7954    }
  1.7955  
  1.7956    /* Add the initial header varint and total the size */
  1.7957 -  u.ap.nHdr += u.ap.nVarint = sqlite3VarintLen(u.ap.nHdr);
  1.7958 -  if( u.ap.nVarint<sqlite3VarintLen(u.ap.nHdr) ){
  1.7959 -    u.ap.nHdr++;
  1.7960 -  }
  1.7961 -  u.ap.nByte = u.ap.nHdr+u.ap.nData-u.ap.nZero;
  1.7962 -  if( u.ap.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1.7963 +  u.aq.nHdr += u.aq.nVarint = sqlite3VarintLen(u.aq.nHdr);
  1.7964 +  if( u.aq.nVarint<sqlite3VarintLen(u.aq.nHdr) ){
  1.7965 +    u.aq.nHdr++;
  1.7966 +  }
  1.7967 +  u.aq.nByte = u.aq.nHdr+u.aq.nData-u.aq.nZero;
  1.7968 +  if( u.aq.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1.7969      goto too_big;
  1.7970    }
  1.7971  
  1.7972 @@ -68330,28 +66556,28 @@
  1.7973    ** be one of the input registers (because the following call to
  1.7974    ** sqlite3VdbeMemGrow() could clobber the value before it is used).
  1.7975    */
  1.7976 -  if( sqlite3VdbeMemGrow(pOut, (int)u.ap.nByte, 0) ){
  1.7977 +  if( sqlite3VdbeMemGrow(pOut, (int)u.aq.nByte, 0) ){
  1.7978      goto no_mem;
  1.7979    }
  1.7980 -  u.ap.zNewRecord = (u8 *)pOut->z;
  1.7981 +  u.aq.zNewRecord = (u8 *)pOut->z;
  1.7982  
  1.7983    /* Write the record */
  1.7984 -  u.ap.i = putVarint32(u.ap.zNewRecord, u.ap.nHdr);
  1.7985 -  for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
  1.7986 -    u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
  1.7987 -    u.ap.i += putVarint32(&u.ap.zNewRecord[u.ap.i], u.ap.serial_type);      /* serial type */
  1.7988 -  }
  1.7989 -  for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){  /* serial data */
  1.7990 -    u.ap.i += sqlite3VdbeSerialPut(&u.ap.zNewRecord[u.ap.i], (int)(u.ap.nByte-u.ap.i), u.ap.pRec,u.ap.file_format);
  1.7991 -  }
  1.7992 -  assert( u.ap.i==u.ap.nByte );
  1.7993 +  u.aq.i = putVarint32(u.aq.zNewRecord, u.aq.nHdr);
  1.7994 +  for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){
  1.7995 +    u.aq.serial_type = sqlite3VdbeSerialType(u.aq.pRec, u.aq.file_format);
  1.7996 +    u.aq.i += putVarint32(&u.aq.zNewRecord[u.aq.i], u.aq.serial_type);      /* serial type */
  1.7997 +  }
  1.7998 +  for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){  /* serial data */
  1.7999 +    u.aq.i += sqlite3VdbeSerialPut(&u.aq.zNewRecord[u.aq.i], (int)(u.aq.nByte-u.aq.i), u.aq.pRec,u.aq.file_format);
  1.8000 +  }
  1.8001 +  assert( u.aq.i==u.aq.nByte );
  1.8002  
  1.8003    assert( pOp->p3>0 && pOp->p3<=p->nMem );
  1.8004 -  pOut->n = (int)u.ap.nByte;
  1.8005 +  pOut->n = (int)u.aq.nByte;
  1.8006    pOut->flags = MEM_Blob | MEM_Dyn;
  1.8007    pOut->xDel = 0;
  1.8008 -  if( u.ap.nZero ){
  1.8009 -    pOut->u.nZero = u.ap.nZero;
  1.8010 +  if( u.aq.nZero ){
  1.8011 +    pOut->u.nZero = u.aq.nZero;
  1.8012      pOut->flags |= MEM_Zero;
  1.8013    }
  1.8014    pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
  1.8015 @@ -68367,18 +66593,18 @@
  1.8016  */
  1.8017  #ifndef SQLITE_OMIT_BTREECOUNT
  1.8018  case OP_Count: {         /* out2-prerelease */
  1.8019 -#if 0  /* local variables moved into u.aq */
  1.8020 +#if 0  /* local variables moved into u.ar */
  1.8021    i64 nEntry;
  1.8022    BtCursor *pCrsr;
  1.8023 -#endif /* local variables moved into u.aq */
  1.8024 -
  1.8025 -  u.aq.pCrsr = p->apCsr[pOp->p1]->pCursor;
  1.8026 -  if( ALWAYS(u.aq.pCrsr) ){
  1.8027 -    rc = sqlite3BtreeCount(u.aq.pCrsr, &u.aq.nEntry);
  1.8028 -  }else{
  1.8029 -    u.aq.nEntry = 0;
  1.8030 -  }
  1.8031 -  pOut->u.i = u.aq.nEntry;
  1.8032 +#endif /* local variables moved into u.ar */
  1.8033 +
  1.8034 +  u.ar.pCrsr = p->apCsr[pOp->p1]->pCursor;
  1.8035 +  if( ALWAYS(u.ar.pCrsr) ){
  1.8036 +    rc = sqlite3BtreeCount(u.ar.pCrsr, &u.ar.nEntry);
  1.8037 +  }else{
  1.8038 +    u.ar.nEntry = 0;
  1.8039 +  }
  1.8040 +  pOut->u.i = u.ar.nEntry;
  1.8041    break;
  1.8042  }
  1.8043  #endif
  1.8044 @@ -68390,7 +66616,7 @@
  1.8045  ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
  1.8046  */
  1.8047  case OP_Savepoint: {
  1.8048 -#if 0  /* local variables moved into u.ar */
  1.8049 +#if 0  /* local variables moved into u.as */
  1.8050    int p1;                         /* Value of P1 operand */
  1.8051    char *zName;                    /* Name of savepoint */
  1.8052    int nName;
  1.8053 @@ -68399,20 +66625,20 @@
  1.8054    Savepoint *pTmp;
  1.8055    int iSavepoint;
  1.8056    int ii;
  1.8057 -#endif /* local variables moved into u.ar */
  1.8058 -
  1.8059 -  u.ar.p1 = pOp->p1;
  1.8060 -  u.ar.zName = pOp->p4.z;
  1.8061 -
  1.8062 -  /* Assert that the u.ar.p1 parameter is valid. Also that if there is no open
  1.8063 +#endif /* local variables moved into u.as */
  1.8064 +
  1.8065 +  u.as.p1 = pOp->p1;
  1.8066 +  u.as.zName = pOp->p4.z;
  1.8067 +
  1.8068 +  /* Assert that the u.as.p1 parameter is valid. Also that if there is no open
  1.8069    ** transaction, then there cannot be any savepoints.
  1.8070    */
  1.8071    assert( db->pSavepoint==0 || db->autoCommit==0 );
  1.8072 -  assert( u.ar.p1==SAVEPOINT_BEGIN||u.ar.p1==SAVEPOINT_RELEASE||u.ar.p1==SAVEPOINT_ROLLBACK );
  1.8073 +  assert( u.as.p1==SAVEPOINT_BEGIN||u.as.p1==SAVEPOINT_RELEASE||u.as.p1==SAVEPOINT_ROLLBACK );
  1.8074    assert( db->pSavepoint || db->isTransactionSavepoint==0 );
  1.8075    assert( checkSavepointCount(db) );
  1.8076  
  1.8077 -  if( u.ar.p1==SAVEPOINT_BEGIN ){
  1.8078 +  if( u.as.p1==SAVEPOINT_BEGIN ){
  1.8079      if( db->writeVdbeCnt>0 ){
  1.8080        /* A new savepoint cannot be created if there are active write
  1.8081        ** statements (i.e. open read/write incremental blob handles).
  1.8082 @@ -68421,7 +66647,7 @@
  1.8083          "SQL statements in progress");
  1.8084        rc = SQLITE_BUSY;
  1.8085      }else{
  1.8086 -      u.ar.nName = sqlite3Strlen30(u.ar.zName);
  1.8087 +      u.as.nName = sqlite3Strlen30(u.as.zName);
  1.8088  
  1.8089  #ifndef SQLITE_OMIT_VIRTUALTABLE
  1.8090        /* This call is Ok even if this savepoint is actually a transaction
  1.8091 @@ -68435,10 +66661,10 @@
  1.8092  #endif
  1.8093  
  1.8094        /* Create a new savepoint structure. */
  1.8095 -      u.ar.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.ar.nName+1);
  1.8096 -      if( u.ar.pNew ){
  1.8097 -        u.ar.pNew->zName = (char *)&u.ar.pNew[1];
  1.8098 -        memcpy(u.ar.pNew->zName, u.ar.zName, u.ar.nName+1);
  1.8099 +      u.as.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.as.nName+1);
  1.8100 +      if( u.as.pNew ){
  1.8101 +        u.as.pNew->zName = (char *)&u.as.pNew[1];
  1.8102 +        memcpy(u.as.pNew->zName, u.as.zName, u.as.nName+1);
  1.8103  
  1.8104          /* If there is no open transaction, then mark this as a special
  1.8105          ** "transaction savepoint". */
  1.8106 @@ -68450,27 +66676,27 @@
  1.8107          }
  1.8108  
  1.8109          /* Link the new savepoint into the database handle's list. */
  1.8110 -        u.ar.pNew->pNext = db->pSavepoint;
  1.8111 -        db->pSavepoint = u.ar.pNew;
  1.8112 -        u.ar.pNew->nDeferredCons = db->nDeferredCons;
  1.8113 -      }
  1.8114 -    }
  1.8115 -  }else{
  1.8116 -    u.ar.iSavepoint = 0;
  1.8117 +        u.as.pNew->pNext = db->pSavepoint;
  1.8118 +        db->pSavepoint = u.as.pNew;
  1.8119 +        u.as.pNew->nDeferredCons = db->nDeferredCons;
  1.8120 +      }
  1.8121 +    }
  1.8122 +  }else{
  1.8123 +    u.as.iSavepoint = 0;
  1.8124  
  1.8125      /* Find the named savepoint. If there is no such savepoint, then an
  1.8126      ** an error is returned to the user.  */
  1.8127      for(
  1.8128 -      u.ar.pSavepoint = db->pSavepoint;
  1.8129 -      u.ar.pSavepoint && sqlite3StrICmp(u.ar.pSavepoint->zName, u.ar.zName);
  1.8130 -      u.ar.pSavepoint = u.ar.pSavepoint->pNext
  1.8131 +      u.as.pSavepoint = db->pSavepoint;
  1.8132 +      u.as.pSavepoint && sqlite3StrICmp(u.as.pSavepoint->zName, u.as.zName);
  1.8133 +      u.as.pSavepoint = u.as.pSavepoint->pNext
  1.8134      ){
  1.8135 -      u.ar.iSavepoint++;
  1.8136 -    }
  1.8137 -    if( !u.ar.pSavepoint ){
  1.8138 -      sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.ar.zName);
  1.8139 +      u.as.iSavepoint++;
  1.8140 +    }
  1.8141 +    if( !u.as.pSavepoint ){
  1.8142 +      sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.as.zName);
  1.8143        rc = SQLITE_ERROR;
  1.8144 -    }else if( db->writeVdbeCnt>0 && u.ar.p1==SAVEPOINT_RELEASE ){
  1.8145 +    }else if( db->writeVdbeCnt>0 && u.as.p1==SAVEPOINT_RELEASE ){
  1.8146        /* It is not possible to release (commit) a savepoint if there are
  1.8147        ** active write statements.
  1.8148        */
  1.8149 @@ -68484,8 +66710,8 @@
  1.8150        ** and this is a RELEASE command, then the current transaction
  1.8151        ** is committed.
  1.8152        */
  1.8153 -      int isTransaction = u.ar.pSavepoint->pNext==0 && db->isTransactionSavepoint;
  1.8154 -      if( isTransaction && u.ar.p1==SAVEPOINT_RELEASE ){
  1.8155 +      int isTransaction = u.as.pSavepoint->pNext==0 && db->isTransactionSavepoint;
  1.8156 +      if( isTransaction && u.as.p1==SAVEPOINT_RELEASE ){
  1.8157          if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
  1.8158            goto vdbe_return;
  1.8159          }
  1.8160 @@ -68499,19 +66725,19 @@
  1.8161          db->isTransactionSavepoint = 0;
  1.8162          rc = p->rc;
  1.8163        }else{
  1.8164 -        u.ar.iSavepoint = db->nSavepoint - u.ar.iSavepoint - 1;
  1.8165 -        if( u.ar.p1==SAVEPOINT_ROLLBACK ){
  1.8166 -          for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
  1.8167 -            sqlite3BtreeTripAllCursors(db->aDb[u.ar.ii].pBt, SQLITE_ABORT);
  1.8168 -          }
  1.8169 -        }
  1.8170 -        for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
  1.8171 -          rc = sqlite3BtreeSavepoint(db->aDb[u.ar.ii].pBt, u.ar.p1, u.ar.iSavepoint);
  1.8172 +        u.as.iSavepoint = db->nSavepoint - u.as.iSavepoint - 1;
  1.8173 +        if( u.as.p1==SAVEPOINT_ROLLBACK ){
  1.8174 +          for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
  1.8175 +            sqlite3BtreeTripAllCursors(db->aDb[u.as.ii].pBt, SQLITE_ABORT);
  1.8176 +          }
  1.8177 +        }
  1.8178 +        for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
  1.8179 +          rc = sqlite3BtreeSavepoint(db->aDb[u.as.ii].pBt, u.as.p1, u.as.iSavepoint);
  1.8180            if( rc!=SQLITE_OK ){
  1.8181              goto abort_due_to_error;
  1.8182            }
  1.8183          }
  1.8184 -        if( u.ar.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
  1.8185 +        if( u.as.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
  1.8186            sqlite3ExpirePreparedStatements(db);
  1.8187            sqlite3ResetAllSchemasOfConnection(db);
  1.8188            db->flags = (db->flags | SQLITE_InternChanges);
  1.8189 @@ -68520,10 +66746,10 @@
  1.8190  
  1.8191        /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
  1.8192        ** savepoints nested inside of the savepoint being operated on. */
  1.8193 -      while( db->pSavepoint!=u.ar.pSavepoint ){
  1.8194 -        u.ar.pTmp = db->pSavepoint;
  1.8195 -        db->pSavepoint = u.ar.pTmp->pNext;
  1.8196 -        sqlite3DbFree(db, u.ar.pTmp);
  1.8197 +      while( db->pSavepoint!=u.as.pSavepoint ){
  1.8198 +        u.as.pTmp = db->pSavepoint;
  1.8199 +        db->pSavepoint = u.as.pTmp->pNext;
  1.8200 +        sqlite3DbFree(db, u.as.pTmp);
  1.8201          db->nSavepoint--;
  1.8202        }
  1.8203  
  1.8204 @@ -68531,19 +66757,19 @@
  1.8205        ** too. If it is a ROLLBACK TO, then set the number of deferred
  1.8206        ** constraint violations present in the database to the value stored
  1.8207        ** when the savepoint was created.  */
  1.8208 -      if( u.ar.p1==SAVEPOINT_RELEASE ){
  1.8209 -        assert( u.ar.pSavepoint==db->pSavepoint );
  1.8210 -        db->pSavepoint = u.ar.pSavepoint->pNext;
  1.8211 -        sqlite3DbFree(db, u.ar.pSavepoint);
  1.8212 +      if( u.as.p1==SAVEPOINT_RELEASE ){
  1.8213 +        assert( u.as.pSavepoint==db->pSavepoint );
  1.8214 +        db->pSavepoint = u.as.pSavepoint->pNext;
  1.8215 +        sqlite3DbFree(db, u.as.pSavepoint);
  1.8216          if( !isTransaction ){
  1.8217            db->nSavepoint--;
  1.8218          }
  1.8219        }else{
  1.8220 -        db->nDeferredCons = u.ar.pSavepoint->nDeferredCons;
  1.8221 +        db->nDeferredCons = u.as.pSavepoint->nDeferredCons;
  1.8222        }
  1.8223  
  1.8224        if( !isTransaction ){
  1.8225 -        rc = sqlite3VtabSavepoint(db, u.ar.p1, u.ar.iSavepoint);
  1.8226 +        rc = sqlite3VtabSavepoint(db, u.as.p1, u.as.iSavepoint);
  1.8227          if( rc!=SQLITE_OK ) goto abort_due_to_error;
  1.8228        }
  1.8229      }
  1.8230 @@ -68562,21 +66788,21 @@
  1.8231  ** This instruction causes the VM to halt.
  1.8232  */
  1.8233  case OP_AutoCommit: {
  1.8234 -#if 0  /* local variables moved into u.as */
  1.8235 +#if 0  /* local variables moved into u.at */
  1.8236    int desiredAutoCommit;
  1.8237    int iRollback;
  1.8238    int turnOnAC;
  1.8239 -#endif /* local variables moved into u.as */
  1.8240 -
  1.8241 -  u.as.desiredAutoCommit = pOp->p1;
  1.8242 -  u.as.iRollback = pOp->p2;
  1.8243 -  u.as.turnOnAC = u.as.desiredAutoCommit && !db->autoCommit;
  1.8244 -  assert( u.as.desiredAutoCommit==1 || u.as.desiredAutoCommit==0 );
  1.8245 -  assert( u.as.desiredAutoCommit==1 || u.as.iRollback==0 );
  1.8246 +#endif /* local variables moved into u.at */
  1.8247 +
  1.8248 +  u.at.desiredAutoCommit = pOp->p1;
  1.8249 +  u.at.iRollback = pOp->p2;
  1.8250 +  u.at.turnOnAC = u.at.desiredAutoCommit && !db->autoCommit;
  1.8251 +  assert( u.at.desiredAutoCommit==1 || u.at.desiredAutoCommit==0 );
  1.8252 +  assert( u.at.desiredAutoCommit==1 || u.at.iRollback==0 );
  1.8253    assert( db->activeVdbeCnt>0 );  /* At least this one VM is active */
  1.8254  
  1.8255  #if 0
  1.8256 -  if( u.as.turnOnAC && u.as.iRollback && db->activeVdbeCnt>1 ){
  1.8257 +  if( u.at.turnOnAC && u.at.iRollback && db->activeVdbeCnt>1 ){
  1.8258      /* If this instruction implements a ROLLBACK and other VMs are
  1.8259      ** still running, and a transaction is active, return an error indicating
  1.8260      ** that the other VMs must complete first.
  1.8261 @@ -68586,25 +66812,25 @@
  1.8262      rc = SQLITE_BUSY;
  1.8263    }else
  1.8264  #endif
  1.8265 -  if( u.as.turnOnAC && !u.as.iRollback && db->writeVdbeCnt>0 ){
  1.8266 +  if( u.at.turnOnAC && !u.at.iRollback && db->writeVdbeCnt>0 ){
  1.8267      /* If this instruction implements a COMMIT and other VMs are writing
  1.8268      ** return an error indicating that the other VMs must complete first.
  1.8269      */
  1.8270      sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
  1.8271          "SQL statements in progress");
  1.8272      rc = SQLITE_BUSY;
  1.8273 -  }else if( u.as.desiredAutoCommit!=db->autoCommit ){
  1.8274 -    if( u.as.iRollback ){
  1.8275 -      assert( u.as.desiredAutoCommit==1 );
  1.8276 +  }else if( u.at.desiredAutoCommit!=db->autoCommit ){
  1.8277 +    if( u.at.iRollback ){
  1.8278 +      assert( u.at.desiredAutoCommit==1 );
  1.8279        sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
  1.8280        db->autoCommit = 1;
  1.8281      }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
  1.8282        goto vdbe_return;
  1.8283      }else{
  1.8284 -      db->autoCommit = (u8)u.as.desiredAutoCommit;
  1.8285 +      db->autoCommit = (u8)u.at.desiredAutoCommit;
  1.8286        if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
  1.8287          p->pc = pc;
  1.8288 -        db->autoCommit = (u8)(1-u.as.desiredAutoCommit);
  1.8289 +        db->autoCommit = (u8)(1-u.at.desiredAutoCommit);
  1.8290          p->rc = rc = SQLITE_BUSY;
  1.8291          goto vdbe_return;
  1.8292        }
  1.8293 @@ -68619,8 +66845,8 @@
  1.8294      goto vdbe_return;
  1.8295    }else{
  1.8296      sqlite3SetString(&p->zErrMsg, db,
  1.8297 -        (!u.as.desiredAutoCommit)?"cannot start a transaction within a transaction":(
  1.8298 -        (u.as.iRollback)?"cannot rollback - no transaction is active":
  1.8299 +        (!u.at.desiredAutoCommit)?"cannot start a transaction within a transaction":(
  1.8300 +        (u.at.iRollback)?"cannot rollback - no transaction is active":
  1.8301                     "cannot commit - no transaction is active"));
  1.8302  
  1.8303      rc = SQLITE_ERROR;
  1.8304 @@ -68660,16 +66886,16 @@
  1.8305  ** If P2 is zero, then a read-lock is obtained on the database file.
  1.8306  */
  1.8307  case OP_Transaction: {
  1.8308 -#if 0  /* local variables moved into u.at */
  1.8309 +#if 0  /* local variables moved into u.au */
  1.8310    Btree *pBt;
  1.8311 -#endif /* local variables moved into u.at */
  1.8312 +#endif /* local variables moved into u.au */
  1.8313  
  1.8314    assert( pOp->p1>=0 && pOp->p1<db->nDb );
  1.8315    assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  1.8316 -  u.at.pBt = db->aDb[pOp->p1].pBt;
  1.8317 -
  1.8318 -  if( u.at.pBt ){
  1.8319 -    rc = sqlite3BtreeBeginTrans(u.at.pBt, pOp->p2);
  1.8320 +  u.au.pBt = db->aDb[pOp->p1].pBt;
  1.8321 +
  1.8322 +  if( u.au.pBt ){
  1.8323 +    rc = sqlite3BtreeBeginTrans(u.au.pBt, pOp->p2);
  1.8324      if( rc==SQLITE_BUSY ){
  1.8325        p->pc = pc;
  1.8326        p->rc = rc = SQLITE_BUSY;
  1.8327 @@ -68682,7 +66908,7 @@
  1.8328      if( pOp->p2 && p->usesStmtJournal
  1.8329       && (db->autoCommit==0 || db->activeVdbeCnt>1)
  1.8330      ){
  1.8331 -      assert( sqlite3BtreeIsInTrans(u.at.pBt) );
  1.8332 +      assert( sqlite3BtreeIsInTrans(u.au.pBt) );
  1.8333        if( p->iStatement==0 ){
  1.8334          assert( db->nStatement>=0 && db->nSavepoint>=0 );
  1.8335          db->nStatement++;
  1.8336 @@ -68691,7 +66917,7 @@
  1.8337  
  1.8338        rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
  1.8339        if( rc==SQLITE_OK ){
  1.8340 -        rc = sqlite3BtreeBeginStmt(u.at.pBt, p->iStatement);
  1.8341 +        rc = sqlite3BtreeBeginStmt(u.au.pBt, p->iStatement);
  1.8342        }
  1.8343  
  1.8344        /* Store the current value of the database handles deferred constraint
  1.8345 @@ -68716,21 +66942,21 @@
  1.8346  ** executing this instruction.
  1.8347  */
  1.8348  case OP_ReadCookie: {               /* out2-prerelease */
  1.8349 -#if 0  /* local variables moved into u.au */
  1.8350 +#if 0  /* local variables moved into u.av */
  1.8351    int iMeta;
  1.8352    int iDb;
  1.8353    int iCookie;
  1.8354 -#endif /* local variables moved into u.au */
  1.8355 -
  1.8356 -  u.au.iDb = pOp->p1;
  1.8357 -  u.au.iCookie = pOp->p3;
  1.8358 +#endif /* local variables moved into u.av */
  1.8359 +
  1.8360 +  u.av.iDb = pOp->p1;
  1.8361 +  u.av.iCookie = pOp->p3;
  1.8362    assert( pOp->p3<SQLITE_N_BTREE_META );
  1.8363 -  assert( u.au.iDb>=0 && u.au.iDb<db->nDb );
  1.8364 -  assert( db->aDb[u.au.iDb].pBt!=0 );
  1.8365 -  assert( (p->btreeMask & (((yDbMask)1)<<u.au.iDb))!=0 );
  1.8366 -
  1.8367 -  sqlite3BtreeGetMeta(db->aDb[u.au.iDb].pBt, u.au.iCookie, (u32 *)&u.au.iMeta);
  1.8368 -  pOut->u.i = u.au.iMeta;
  1.8369 +  assert( u.av.iDb>=0 && u.av.iDb<db->nDb );
  1.8370 +  assert( db->aDb[u.av.iDb].pBt!=0 );
  1.8371 +  assert( (p->btreeMask & (((yDbMask)1)<<u.av.iDb))!=0 );
  1.8372 +
  1.8373 +  sqlite3BtreeGetMeta(db->aDb[u.av.iDb].pBt, u.av.iCookie, (u32 *)&u.av.iMeta);
  1.8374 +  pOut->u.i = u.av.iMeta;
  1.8375    break;
  1.8376  }
  1.8377  
  1.8378 @@ -68745,26 +66971,26 @@
  1.8379  ** A transaction must be started before executing this opcode.
  1.8380  */
  1.8381  case OP_SetCookie: {       /* in3 */
  1.8382 -#if 0  /* local variables moved into u.av */
  1.8383 +#if 0  /* local variables moved into u.aw */
  1.8384    Db *pDb;
  1.8385 -#endif /* local variables moved into u.av */
  1.8386 +#endif /* local variables moved into u.aw */
  1.8387    assert( pOp->p2<SQLITE_N_BTREE_META );
  1.8388    assert( pOp->p1>=0 && pOp->p1<db->nDb );
  1.8389    assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  1.8390 -  u.av.pDb = &db->aDb[pOp->p1];
  1.8391 -  assert( u.av.pDb->pBt!=0 );
  1.8392 +  u.aw.pDb = &db->aDb[pOp->p1];
  1.8393 +  assert( u.aw.pDb->pBt!=0 );
  1.8394    assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
  1.8395    pIn3 = &aMem[pOp->p3];
  1.8396    sqlite3VdbeMemIntegerify(pIn3);
  1.8397    /* See note about index shifting on OP_ReadCookie */
  1.8398 -  rc = sqlite3BtreeUpdateMeta(u.av.pDb->pBt, pOp->p2, (int)pIn3->u.i);
  1.8399 +  rc = sqlite3BtreeUpdateMeta(u.aw.pDb->pBt, pOp->p2, (int)pIn3->u.i);
  1.8400    if( pOp->p2==BTREE_SCHEMA_VERSION ){
  1.8401      /* When the schema cookie changes, record the new cookie internally */
  1.8402 -    u.av.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
  1.8403 +    u.aw.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
  1.8404      db->flags |= SQLITE_InternChanges;
  1.8405    }else if( pOp->p2==BTREE_FILE_FORMAT ){
  1.8406      /* Record changes in the file format */
  1.8407 -    u.av.pDb->pSchema->file_format = (u8)pIn3->u.i;
  1.8408 +    u.aw.pDb->pSchema->file_format = (u8)pIn3->u.i;
  1.8409    }
  1.8410    if( pOp->p1==1 ){
  1.8411      /* Invalidate all prepared statements whenever the TEMP database
  1.8412 @@ -68794,23 +67020,23 @@
  1.8413  ** invoked.
  1.8414  */
  1.8415  case OP_VerifyCookie: {
  1.8416 -#if 0  /* local variables moved into u.aw */
  1.8417 +#if 0  /* local variables moved into u.ax */
  1.8418    int iMeta;
  1.8419    int iGen;
  1.8420    Btree *pBt;
  1.8421 -#endif /* local variables moved into u.aw */
  1.8422 +#endif /* local variables moved into u.ax */
  1.8423  
  1.8424    assert( pOp->p1>=0 && pOp->p1<db->nDb );
  1.8425    assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  1.8426    assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
  1.8427 -  u.aw.pBt = db->aDb[pOp->p1].pBt;
  1.8428 -  if( u.aw.pBt ){
  1.8429 -    sqlite3BtreeGetMeta(u.aw.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.aw.iMeta);
  1.8430 -    u.aw.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
  1.8431 -  }else{
  1.8432 -    u.aw.iGen = u.aw.iMeta = 0;
  1.8433 -  }
  1.8434 -  if( u.aw.iMeta!=pOp->p2 || u.aw.iGen!=pOp->p3 ){
  1.8435 +  u.ax.pBt = db->aDb[pOp->p1].pBt;
  1.8436 +  if( u.ax.pBt ){
  1.8437 +    sqlite3BtreeGetMeta(u.ax.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.ax.iMeta);
  1.8438 +    u.ax.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
  1.8439 +  }else{
  1.8440 +    u.ax.iGen = u.ax.iMeta = 0;
  1.8441 +  }
  1.8442 +  if( u.ax.iMeta!=pOp->p2 || u.ax.iGen!=pOp->p3 ){
  1.8443      sqlite3DbFree(db, p->zErrMsg);
  1.8444      p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
  1.8445      /* If the schema-cookie from the database file matches the cookie
  1.8446 @@ -68826,7 +67052,7 @@
  1.8447      ** to be invalidated whenever sqlite3_step() is called from within
  1.8448      ** a v-table method.
  1.8449      */
  1.8450 -    if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.aw.iMeta ){
  1.8451 +    if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.ax.iMeta ){
  1.8452        sqlite3ResetOneSchema(db, pOp->p1);
  1.8453      }
  1.8454  
  1.8455 @@ -68887,7 +67113,7 @@
  1.8456  */
  1.8457  case OP_OpenRead:
  1.8458  case OP_OpenWrite: {
  1.8459 -#if 0  /* local variables moved into u.ax */
  1.8460 +#if 0  /* local variables moved into u.ay */
  1.8461    int nField;
  1.8462    KeyInfo *pKeyInfo;
  1.8463    int p2;
  1.8464 @@ -68896,62 +67122,67 @@
  1.8465    Btree *pX;
  1.8466    VdbeCursor *pCur;
  1.8467    Db *pDb;
  1.8468 -#endif /* local variables moved into u.ax */
  1.8469 +#endif /* local variables moved into u.ay */
  1.8470 +
  1.8471 +  assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
  1.8472 +  assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
  1.8473  
  1.8474    if( p->expired ){
  1.8475      rc = SQLITE_ABORT;
  1.8476      break;
  1.8477    }
  1.8478  
  1.8479 -  u.ax.nField = 0;
  1.8480 -  u.ax.pKeyInfo = 0;
  1.8481 -  u.ax.p2 = pOp->p2;
  1.8482 -  u.ax.iDb = pOp->p3;
  1.8483 -  assert( u.ax.iDb>=0 && u.ax.iDb<db->nDb );
  1.8484 -  assert( (p->btreeMask & (((yDbMask)1)<<u.ax.iDb))!=0 );
  1.8485 -  u.ax.pDb = &db->aDb[u.ax.iDb];
  1.8486 -  u.ax.pX = u.ax.pDb->pBt;
  1.8487 -  assert( u.ax.pX!=0 );
  1.8488 +  u.ay.nField = 0;
  1.8489 +  u.ay.pKeyInfo = 0;
  1.8490 +  u.ay.p2 = pOp->p2;
  1.8491 +  u.ay.iDb = pOp->p3;
  1.8492 +  assert( u.ay.iDb>=0 && u.ay.iDb<db->nDb );
  1.8493 +  assert( (p->btreeMask & (((yDbMask)1)<<u.ay.iDb))!=0 );
  1.8494 +  u.ay.pDb = &db->aDb[u.ay.iDb];
  1.8495 +  u.ay.pX = u.ay.pDb->pBt;
  1.8496 +  assert( u.ay.pX!=0 );
  1.8497    if( pOp->opcode==OP_OpenWrite ){
  1.8498 -    u.ax.wrFlag = 1;
  1.8499 -    assert( sqlite3SchemaMutexHeld(db, u.ax.iDb, 0) );
  1.8500 -    if( u.ax.pDb->pSchema->file_format < p->minWriteFileFormat ){
  1.8501 -      p->minWriteFileFormat = u.ax.pDb->pSchema->file_format;
  1.8502 -    }
  1.8503 -  }else{
  1.8504 -    u.ax.wrFlag = 0;
  1.8505 -  }
  1.8506 -  if( pOp->p5 ){
  1.8507 -    assert( u.ax.p2>0 );
  1.8508 -    assert( u.ax.p2<=p->nMem );
  1.8509 -    pIn2 = &aMem[u.ax.p2];
  1.8510 +    u.ay.wrFlag = 1;
  1.8511 +    assert( sqlite3SchemaMutexHeld(db, u.ay.iDb, 0) );
  1.8512 +    if( u.ay.pDb->pSchema->file_format < p->minWriteFileFormat ){
  1.8513 +      p->minWriteFileFormat = u.ay.pDb->pSchema->file_format;
  1.8514 +    }
  1.8515 +  }else{
  1.8516 +    u.ay.wrFlag = 0;
  1.8517 +  }
  1.8518 +  if( pOp->p5 & OPFLAG_P2ISREG ){
  1.8519 +    assert( u.ay.p2>0 );
  1.8520 +    assert( u.ay.p2<=p->nMem );
  1.8521 +    pIn2 = &aMem[u.ay.p2];
  1.8522      assert( memIsValid(pIn2) );
  1.8523      assert( (pIn2->flags & MEM_Int)!=0 );
  1.8524      sqlite3VdbeMemIntegerify(pIn2);
  1.8525 -    u.ax.p2 = (int)pIn2->u.i;
  1.8526 -    /* The u.ax.p2 value always comes from a prior OP_CreateTable opcode and
  1.8527 -    ** that opcode will always set the u.ax.p2 value to 2 or more or else fail.
  1.8528 +    u.ay.p2 = (int)pIn2->u.i;
  1.8529 +    /* The u.ay.p2 value always comes from a prior OP_CreateTable opcode and
  1.8530 +    ** that opcode will always set the u.ay.p2 value to 2 or more or else fail.
  1.8531      ** If there were a failure, the prepared statement would have halted
  1.8532      ** before reaching this instruction. */
  1.8533 -    if( NEVER(u.ax.p2<2) ) {
  1.8534 +    if( NEVER(u.ay.p2<2) ) {
  1.8535        rc = SQLITE_CORRUPT_BKPT;
  1.8536        goto abort_due_to_error;
  1.8537      }
  1.8538    }
  1.8539    if( pOp->p4type==P4_KEYINFO ){
  1.8540 -    u.ax.pKeyInfo = pOp->p4.pKeyInfo;
  1.8541 -    u.ax.pKeyInfo->enc = ENC(p->db);
  1.8542 -    u.ax.nField = u.ax.pKeyInfo->nField+1;
  1.8543 +    u.ay.pKeyInfo = pOp->p4.pKeyInfo;
  1.8544 +    u.ay.pKeyInfo->enc = ENC(p->db);
  1.8545 +    u.ay.nField = u.ay.pKeyInfo->nField+1;
  1.8546    }else if( pOp->p4type==P4_INT32 ){
  1.8547 -    u.ax.nField = pOp->p4.i;
  1.8548 +    u.ay.nField = pOp->p4.i;
  1.8549    }
  1.8550    assert( pOp->p1>=0 );
  1.8551 -  u.ax.pCur = allocateCursor(p, pOp->p1, u.ax.nField, u.ax.iDb, 1);
  1.8552 -  if( u.ax.pCur==0 ) goto no_mem;
  1.8553 -  u.ax.pCur->nullRow = 1;
  1.8554 -  u.ax.pCur->isOrdered = 1;
  1.8555 -  rc = sqlite3BtreeCursor(u.ax.pX, u.ax.p2, u.ax.wrFlag, u.ax.pKeyInfo, u.ax.pCur->pCursor);
  1.8556 -  u.ax.pCur->pKeyInfo = u.ax.pKeyInfo;
  1.8557 +  u.ay.pCur = allocateCursor(p, pOp->p1, u.ay.nField, u.ay.iDb, 1);
  1.8558 +  if( u.ay.pCur==0 ) goto no_mem;
  1.8559 +  u.ay.pCur->nullRow = 1;
  1.8560 +  u.ay.pCur->isOrdered = 1;
  1.8561 +  rc = sqlite3BtreeCursor(u.ay.pX, u.ay.p2, u.ay.wrFlag, u.ay.pKeyInfo, u.ay.pCur->pCursor);
  1.8562 +  u.ay.pCur->pKeyInfo = u.ay.pKeyInfo;
  1.8563 +  assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
  1.8564 +  sqlite3BtreeCursorHints(u.ay.pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
  1.8565  
  1.8566    /* Since it performs no memory allocation or IO, the only value that
  1.8567    ** sqlite3BtreeCursor() may return is SQLITE_OK. */
  1.8568 @@ -68961,8 +67192,8 @@
  1.8569    ** SQLite used to check if the root-page flags were sane at this point
  1.8570    ** and report database corruption if they were not, but this check has
  1.8571    ** since moved into the btree layer.  */
  1.8572 -  u.ax.pCur->isTable = pOp->p4type!=P4_KEYINFO;
  1.8573 -  u.ax.pCur->isIndex = !u.ax.pCur->isTable;
  1.8574 +  u.ay.pCur->isTable = pOp->p4type!=P4_KEYINFO;
  1.8575 +  u.ay.pCur->isIndex = !u.ay.pCur->isTable;
  1.8576    break;
  1.8577  }
  1.8578  
  1.8579 @@ -68998,9 +67229,9 @@
  1.8580  */
  1.8581  case OP_OpenAutoindex: 
  1.8582  case OP_OpenEphemeral: {
  1.8583 -#if 0  /* local variables moved into u.ay */
  1.8584 +#if 0  /* local variables moved into u.az */
  1.8585    VdbeCursor *pCx;
  1.8586 -#endif /* local variables moved into u.ay */
  1.8587 +#endif /* local variables moved into u.az */
  1.8588    static const int vfsFlags =
  1.8589        SQLITE_OPEN_READWRITE |
  1.8590        SQLITE_OPEN_CREATE |
  1.8591 @@ -69009,13 +67240,13 @@
  1.8592        SQLITE_OPEN_TRANSIENT_DB;
  1.8593  
  1.8594    assert( pOp->p1>=0 );
  1.8595 -  u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  1.8596 -  if( u.ay.pCx==0 ) goto no_mem;
  1.8597 -  u.ay.pCx->nullRow = 1;
  1.8598 -  rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ay.pCx->pBt,
  1.8599 +  u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  1.8600 +  if( u.az.pCx==0 ) goto no_mem;
  1.8601 +  u.az.pCx->nullRow = 1;
  1.8602 +  rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.az.pCx->pBt,
  1.8603                          BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
  1.8604    if( rc==SQLITE_OK ){
  1.8605 -    rc = sqlite3BtreeBeginTrans(u.ay.pCx->pBt, 1);
  1.8606 +    rc = sqlite3BtreeBeginTrans(u.az.pCx->pBt, 1);
  1.8607    }
  1.8608    if( rc==SQLITE_OK ){
  1.8609      /* If a transient index is required, create it by calling
  1.8610 @@ -69026,42 +67257,43 @@
  1.8611      if( pOp->p4.pKeyInfo ){
  1.8612        int pgno;
  1.8613        assert( pOp->p4type==P4_KEYINFO );
  1.8614 -      rc = sqlite3BtreeCreateTable(u.ay.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
  1.8615 +      rc = sqlite3BtreeCreateTable(u.az.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
  1.8616        if( rc==SQLITE_OK ){
  1.8617          assert( pgno==MASTER_ROOT+1 );
  1.8618 -        rc = sqlite3BtreeCursor(u.ay.pCx->pBt, pgno, 1,
  1.8619 -                                (KeyInfo*)pOp->p4.z, u.ay.pCx->pCursor);
  1.8620 -        u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
  1.8621 -        u.ay.pCx->pKeyInfo->enc = ENC(p->db);
  1.8622 -      }
  1.8623 -      u.ay.pCx->isTable = 0;
  1.8624 -    }else{
  1.8625 -      rc = sqlite3BtreeCursor(u.ay.pCx->pBt, MASTER_ROOT, 1, 0, u.ay.pCx->pCursor);
  1.8626 -      u.ay.pCx->isTable = 1;
  1.8627 -    }
  1.8628 -  }
  1.8629 -  u.ay.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
  1.8630 -  u.ay.pCx->isIndex = !u.ay.pCx->isTable;
  1.8631 +        rc = sqlite3BtreeCursor(u.az.pCx->pBt, pgno, 1,
  1.8632 +                                (KeyInfo*)pOp->p4.z, u.az.pCx->pCursor);
  1.8633 +        u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
  1.8634 +        u.az.pCx->pKeyInfo->enc = ENC(p->db);
  1.8635 +      }
  1.8636 +      u.az.pCx->isTable = 0;
  1.8637 +    }else{
  1.8638 +      rc = sqlite3BtreeCursor(u.az.pCx->pBt, MASTER_ROOT, 1, 0, u.az.pCx->pCursor);
  1.8639 +      u.az.pCx->isTable = 1;
  1.8640 +    }
  1.8641 +  }
  1.8642 +  u.az.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
  1.8643 +  u.az.pCx->isIndex = !u.az.pCx->isTable;
  1.8644    break;
  1.8645  }
  1.8646  
  1.8647 -/* Opcode: OpenSorter P1 P2 * P4 *
  1.8648 +/* Opcode: SorterOpen P1 P2 * P4 *
  1.8649  **
  1.8650  ** This opcode works like OP_OpenEphemeral except that it opens
  1.8651  ** a transient index that is specifically designed to sort large
  1.8652  ** tables using an external merge-sort algorithm.
  1.8653  */
  1.8654  case OP_SorterOpen: {
  1.8655 -#if 0  /* local variables moved into u.az */
  1.8656 +#if 0  /* local variables moved into u.ba */
  1.8657    VdbeCursor *pCx;
  1.8658 -#endif /* local variables moved into u.az */
  1.8659 +#endif /* local variables moved into u.ba */
  1.8660 +
  1.8661  #ifndef SQLITE_OMIT_MERGE_SORT
  1.8662 -  u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  1.8663 -  if( u.az.pCx==0 ) goto no_mem;
  1.8664 -  u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
  1.8665 -  u.az.pCx->pKeyInfo->enc = ENC(p->db);
  1.8666 -  u.az.pCx->isSorter = 1;
  1.8667 -  rc = sqlite3VdbeSorterInit(db, u.az.pCx);
  1.8668 +  u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  1.8669 +  if( u.ba.pCx==0 ) goto no_mem;
  1.8670 +  u.ba.pCx->pKeyInfo = pOp->p4.pKeyInfo;
  1.8671 +  u.ba.pCx->pKeyInfo->enc = ENC(p->db);
  1.8672 +  u.ba.pCx->isSorter = 1;
  1.8673 +  rc = sqlite3VdbeSorterInit(db, u.ba.pCx);
  1.8674  #else
  1.8675    pOp->opcode = OP_OpenEphemeral;
  1.8676    pc--;
  1.8677 @@ -69069,12 +67301,13 @@
  1.8678    break;
  1.8679  }
  1.8680  
  1.8681 -/* Opcode: OpenPseudo P1 P2 P3 * *
  1.8682 +/* Opcode: OpenPseudo P1 P2 P3 * P5
  1.8683  **
  1.8684  ** Open a new cursor that points to a fake table that contains a single
  1.8685  ** row of data.  The content of that one row in the content of memory
  1.8686 -** register P2.  In other words, cursor P1 becomes an alias for the 
  1.8687 -** MEM_Blob content contained in register P2.
  1.8688 +** register P2 when P5==0.  In other words, cursor P1 becomes an alias for the 
  1.8689 +** MEM_Blob content contained in register P2.  When P5==1, then the
  1.8690 +** row is represented by P3 consecutive registers beginning with P2.
  1.8691  **
  1.8692  ** A pseudo-table created by this opcode is used to hold a single
  1.8693  ** row output from the sorter so that the row can be decomposed into
  1.8694 @@ -69085,17 +67318,18 @@
  1.8695  ** the pseudo-table.
  1.8696  */
  1.8697  case OP_OpenPseudo: {
  1.8698 -#if 0  /* local variables moved into u.ba */
  1.8699 +#if 0  /* local variables moved into u.bb */
  1.8700    VdbeCursor *pCx;
  1.8701 -#endif /* local variables moved into u.ba */
  1.8702 +#endif /* local variables moved into u.bb */
  1.8703  
  1.8704    assert( pOp->p1>=0 );
  1.8705 -  u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
  1.8706 -  if( u.ba.pCx==0 ) goto no_mem;
  1.8707 -  u.ba.pCx->nullRow = 1;
  1.8708 -  u.ba.pCx->pseudoTableReg = pOp->p2;
  1.8709 -  u.ba.pCx->isTable = 1;
  1.8710 -  u.ba.pCx->isIndex = 0;
  1.8711 +  u.bb.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
  1.8712 +  if( u.bb.pCx==0 ) goto no_mem;
  1.8713 +  u.bb.pCx->nullRow = 1;
  1.8714 +  u.bb.pCx->pseudoTableReg = pOp->p2;
  1.8715 +  u.bb.pCx->isTable = 1;
  1.8716 +  u.bb.pCx->isIndex = 0;
  1.8717 +  u.bb.pCx->multiPseudo = pOp->p5;
  1.8718    break;
  1.8719  }
  1.8720  
  1.8721 @@ -69167,35 +67401,35 @@
  1.8722  case OP_SeekLe:         /* jump, in3 */
  1.8723  case OP_SeekGe:         /* jump, in3 */
  1.8724  case OP_SeekGt: {       /* jump, in3 */
  1.8725 -#if 0  /* local variables moved into u.bb */
  1.8726 +#if 0  /* local variables moved into u.bc */
  1.8727    int res;
  1.8728    int oc;
  1.8729    VdbeCursor *pC;
  1.8730    UnpackedRecord r;
  1.8731    int nField;
  1.8732    i64 iKey;      /* The rowid we are to seek to */
  1.8733 -#endif /* local variables moved into u.bb */
  1.8734 +#endif /* local variables moved into u.bc */
  1.8735  
  1.8736    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.8737    assert( pOp->p2!=0 );
  1.8738 -  u.bb.pC = p->apCsr[pOp->p1];
  1.8739 -  assert( u.bb.pC!=0 );
  1.8740 -  assert( u.bb.pC->pseudoTableReg==0 );
  1.8741 +  u.bc.pC = p->apCsr[pOp->p1];
  1.8742 +  assert( u.bc.pC!=0 );
  1.8743 +  assert( u.bc.pC->pseudoTableReg==0 );
  1.8744    assert( OP_SeekLe == OP_SeekLt+1 );
  1.8745    assert( OP_SeekGe == OP_SeekLt+2 );
  1.8746    assert( OP_SeekGt == OP_SeekLt+3 );
  1.8747 -  assert( u.bb.pC->isOrdered );
  1.8748 -  if( ALWAYS(u.bb.pC->pCursor!=0) ){
  1.8749 -    u.bb.oc = pOp->opcode;
  1.8750 -    u.bb.pC->nullRow = 0;
  1.8751 -    if( u.bb.pC->isTable ){
  1.8752 +  assert( u.bc.pC->isOrdered );
  1.8753 +  if( ALWAYS(u.bc.pC->pCursor!=0) ){
  1.8754 +    u.bc.oc = pOp->opcode;
  1.8755 +    u.bc.pC->nullRow = 0;
  1.8756 +    if( u.bc.pC->isTable ){
  1.8757        /* The input value in P3 might be of any type: integer, real, string,
  1.8758        ** blob, or NULL.  But it needs to be an integer before we can do
  1.8759        ** the seek, so covert it. */
  1.8760        pIn3 = &aMem[pOp->p3];
  1.8761        applyNumericAffinity(pIn3);
  1.8762 -      u.bb.iKey = sqlite3VdbeIntValue(pIn3);
  1.8763 -      u.bb.pC->rowidIsValid = 0;
  1.8764 +      u.bc.iKey = sqlite3VdbeIntValue(pIn3);
  1.8765 +      u.bc.pC->rowidIsValid = 0;
  1.8766  
  1.8767        /* If the P3 value could not be converted into an integer without
  1.8768        ** loss of information, then special processing is required... */
  1.8769 @@ -69210,101 +67444,101 @@
  1.8770          ** point number. */
  1.8771          assert( (pIn3->flags & MEM_Real)!=0 );
  1.8772  
  1.8773 -        if( u.bb.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bb.iKey || pIn3->r>0) ){
  1.8774 +        if( u.bc.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bc.iKey || pIn3->r>0) ){
  1.8775            /* The P3 value is too large in magnitude to be expressed as an
  1.8776            ** integer. */
  1.8777 -          u.bb.res = 1;
  1.8778 +          u.bc.res = 1;
  1.8779            if( pIn3->r<0 ){
  1.8780 -            if( u.bb.oc>=OP_SeekGe ){  assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
  1.8781 -              rc = sqlite3BtreeFirst(u.bb.pC->pCursor, &u.bb.res);
  1.8782 +            if( u.bc.oc>=OP_SeekGe ){  assert( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt );
  1.8783 +              rc = sqlite3BtreeFirst(u.bc.pC->pCursor, &u.bc.res);
  1.8784                if( rc!=SQLITE_OK ) goto abort_due_to_error;
  1.8785              }
  1.8786            }else{
  1.8787 -            if( u.bb.oc<=OP_SeekLe ){  assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
  1.8788 -              rc = sqlite3BtreeLast(u.bb.pC->pCursor, &u.bb.res);
  1.8789 +            if( u.bc.oc<=OP_SeekLe ){  assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
  1.8790 +              rc = sqlite3BtreeLast(u.bc.pC->pCursor, &u.bc.res);
  1.8791                if( rc!=SQLITE_OK ) goto abort_due_to_error;
  1.8792              }
  1.8793            }
  1.8794 -          if( u.bb.res ){
  1.8795 +          if( u.bc.res ){
  1.8796              pc = pOp->p2 - 1;
  1.8797            }
  1.8798            break;
  1.8799 -        }else if( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekGe ){
  1.8800 +        }else if( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekGe ){
  1.8801            /* Use the ceiling() function to convert real->int */
  1.8802 -          if( pIn3->r > (double)u.bb.iKey ) u.bb.iKey++;
  1.8803 +          if( pIn3->r > (double)u.bc.iKey ) u.bc.iKey++;
  1.8804          }else{
  1.8805            /* Use the floor() function to convert real->int */
  1.8806 -          assert( u.bb.oc==OP_SeekLe || u.bb.oc==OP_SeekGt );
  1.8807 -          if( pIn3->r < (double)u.bb.iKey ) u.bb.iKey--;
  1.8808 -        }
  1.8809 -      }
  1.8810 -      rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, 0, (u64)u.bb.iKey, 0, &u.bb.res);
  1.8811 +          assert( u.bc.oc==OP_SeekLe || u.bc.oc==OP_SeekGt );
  1.8812 +          if( pIn3->r < (double)u.bc.iKey ) u.bc.iKey--;
  1.8813 +        }
  1.8814 +      }
  1.8815 +      rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, 0, (u64)u.bc.iKey, 0, &u.bc.res);
  1.8816        if( rc!=SQLITE_OK ){
  1.8817          goto abort_due_to_error;
  1.8818        }
  1.8819 -      if( u.bb.res==0 ){
  1.8820 -        u.bb.pC->rowidIsValid = 1;
  1.8821 -        u.bb.pC->lastRowid = u.bb.iKey;
  1.8822 -      }
  1.8823 -    }else{
  1.8824 -      u.bb.nField = pOp->p4.i;
  1.8825 +      if( u.bc.res==0 ){
  1.8826 +        u.bc.pC->rowidIsValid = 1;
  1.8827 +        u.bc.pC->lastRowid = u.bc.iKey;
  1.8828 +      }
  1.8829 +    }else{
  1.8830 +      u.bc.nField = pOp->p4.i;
  1.8831        assert( pOp->p4type==P4_INT32 );
  1.8832 -      assert( u.bb.nField>0 );
  1.8833 -      u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
  1.8834 -      u.bb.r.nField = (u16)u.bb.nField;
  1.8835 +      assert( u.bc.nField>0 );
  1.8836 +      u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
  1.8837 +      u.bc.r.nField = (u16)u.bc.nField;
  1.8838  
  1.8839        /* The next line of code computes as follows, only faster:
  1.8840 -      **   if( u.bb.oc==OP_SeekGt || u.bb.oc==OP_SeekLe ){
  1.8841 -      **     u.bb.r.flags = UNPACKED_INCRKEY;
  1.8842 +      **   if( u.bc.oc==OP_SeekGt || u.bc.oc==OP_SeekLe ){
  1.8843 +      **     u.bc.r.flags = UNPACKED_INCRKEY;
  1.8844        **   }else{
  1.8845 -      **     u.bb.r.flags = 0;
  1.8846 +      **     u.bc.r.flags = 0;
  1.8847        **   }
  1.8848        */
  1.8849 -      u.bb.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bb.oc - OP_SeekLt)));
  1.8850 -      assert( u.bb.oc!=OP_SeekGt || u.bb.r.flags==UNPACKED_INCRKEY );
  1.8851 -      assert( u.bb.oc!=OP_SeekLe || u.bb.r.flags==UNPACKED_INCRKEY );
  1.8852 -      assert( u.bb.oc!=OP_SeekGe || u.bb.r.flags==0 );
  1.8853 -      assert( u.bb.oc!=OP_SeekLt || u.bb.r.flags==0 );
  1.8854 -
  1.8855 -      u.bb.r.aMem = &aMem[pOp->p3];
  1.8856 +      u.bc.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bc.oc - OP_SeekLt)));
  1.8857 +      assert( u.bc.oc!=OP_SeekGt || u.bc.r.flags==UNPACKED_INCRKEY );
  1.8858 +      assert( u.bc.oc!=OP_SeekLe || u.bc.r.flags==UNPACKED_INCRKEY );
  1.8859 +      assert( u.bc.oc!=OP_SeekGe || u.bc.r.flags==0 );
  1.8860 +      assert( u.bc.oc!=OP_SeekLt || u.bc.r.flags==0 );
  1.8861 +
  1.8862 +      u.bc.r.aMem = &aMem[pOp->p3];
  1.8863  #ifdef SQLITE_DEBUG
  1.8864 -      { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
  1.8865 -#endif
  1.8866 -      ExpandBlob(u.bb.r.aMem);
  1.8867 -      rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, &u.bb.r, 0, 0, &u.bb.res);
  1.8868 +      { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
  1.8869 +#endif
  1.8870 +      ExpandBlob(u.bc.r.aMem);
  1.8871 +      rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, &u.bc.r, 0, 0, &u.bc.res);
  1.8872        if( rc!=SQLITE_OK ){
  1.8873          goto abort_due_to_error;
  1.8874        }
  1.8875 -      u.bb.pC->rowidIsValid = 0;
  1.8876 -    }
  1.8877 -    u.bb.pC->deferredMoveto = 0;
  1.8878 -    u.bb.pC->cacheStatus = CACHE_STALE;
  1.8879 +      u.bc.pC->rowidIsValid = 0;
  1.8880 +    }
  1.8881 +    u.bc.pC->deferredMoveto = 0;
  1.8882 +    u.bc.pC->cacheStatus = CACHE_STALE;
  1.8883  #ifdef SQLITE_TEST
  1.8884      sqlite3_search_count++;
  1.8885  #endif
  1.8886 -    if( u.bb.oc>=OP_SeekGe ){  assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
  1.8887 -      if( u.bb.res<0 || (u.bb.res==0 && u.bb.oc==OP_SeekGt) ){
  1.8888 -        rc = sqlite3BtreeNext(u.bb.pC->pCursor, &u.bb.res);
  1.8889 +    if( u.bc.oc>=OP_SeekGe ){  assert( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt );
  1.8890 +      if( u.bc.res<0 || (u.bc.res==0 && u.bc.oc==OP_SeekGt) ){
  1.8891 +        rc = sqlite3BtreeNext(u.bc.pC->pCursor, &u.bc.res);
  1.8892          if( rc!=SQLITE_OK ) goto abort_due_to_error;
  1.8893 -        u.bb.pC->rowidIsValid = 0;
  1.8894 -      }else{
  1.8895 -        u.bb.res = 0;
  1.8896 -      }
  1.8897 -    }else{
  1.8898 -      assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
  1.8899 -      if( u.bb.res>0 || (u.bb.res==0 && u.bb.oc==OP_SeekLt) ){
  1.8900 -        rc = sqlite3BtreePrevious(u.bb.pC->pCursor, &u.bb.res);
  1.8901 +        u.bc.pC->rowidIsValid = 0;
  1.8902 +      }else{
  1.8903 +        u.bc.res = 0;
  1.8904 +      }
  1.8905 +    }else{
  1.8906 +      assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
  1.8907 +      if( u.bc.res>0 || (u.bc.res==0 && u.bc.oc==OP_SeekLt) ){
  1.8908 +        rc = sqlite3BtreePrevious(u.bc.pC->pCursor, &u.bc.res);
  1.8909          if( rc!=SQLITE_OK ) goto abort_due_to_error;
  1.8910 -        u.bb.pC->rowidIsValid = 0;
  1.8911 -      }else{
  1.8912 -        /* u.bb.res might be negative because the table is empty.  Check to
  1.8913 +        u.bc.pC->rowidIsValid = 0;
  1.8914 +      }else{
  1.8915 +        /* u.bc.res might be negative because the table is empty.  Check to
  1.8916          ** see if this is the case.
  1.8917          */
  1.8918 -        u.bb.res = sqlite3BtreeEof(u.bb.pC->pCursor);
  1.8919 +        u.bc.res = sqlite3BtreeEof(u.bc.pC->pCursor);
  1.8920        }
  1.8921      }
  1.8922      assert( pOp->p2>0 );
  1.8923 -    if( u.bb.res ){
  1.8924 +    if( u.bc.res ){
  1.8925        pc = pOp->p2 - 1;
  1.8926      }
  1.8927    }else{
  1.8928 @@ -69327,20 +67561,20 @@
  1.8929  ** occur, no unnecessary I/O happens.
  1.8930  */
  1.8931  case OP_Seek: {    /* in2 */
  1.8932 -#if 0  /* local variables moved into u.bc */
  1.8933 +#if 0  /* local variables moved into u.bd */
  1.8934    VdbeCursor *pC;
  1.8935 -#endif /* local variables moved into u.bc */
  1.8936 +#endif /* local variables moved into u.bd */
  1.8937  
  1.8938    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.8939 -  u.bc.pC = p->apCsr[pOp->p1];
  1.8940 -  assert( u.bc.pC!=0 );
  1.8941 -  if( ALWAYS(u.bc.pC->pCursor!=0) ){
  1.8942 -    assert( u.bc.pC->isTable );
  1.8943 -    u.bc.pC->nullRow = 0;
  1.8944 +  u.bd.pC = p->apCsr[pOp->p1];
  1.8945 +  assert( u.bd.pC!=0 );
  1.8946 +  if( ALWAYS(u.bd.pC->pCursor!=0) ){
  1.8947 +    assert( u.bd.pC->isTable );
  1.8948 +    u.bd.pC->nullRow = 0;
  1.8949      pIn2 = &aMem[pOp->p2];
  1.8950 -    u.bc.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
  1.8951 -    u.bc.pC->rowidIsValid = 0;
  1.8952 -    u.bc.pC->deferredMoveto = 1;
  1.8953 +    u.bd.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
  1.8954 +    u.bd.pC->rowidIsValid = 0;
  1.8955 +    u.bd.pC->deferredMoveto = 1;
  1.8956    }
  1.8957    break;
  1.8958  }
  1.8959 @@ -69372,7 +67606,7 @@
  1.8960  */
  1.8961  case OP_NotFound:       /* jump, in3 */
  1.8962  case OP_Found: {        /* jump, in3 */
  1.8963 -#if 0  /* local variables moved into u.bd */
  1.8964 +#if 0  /* local variables moved into u.be */
  1.8965    int alreadyExists;
  1.8966    VdbeCursor *pC;
  1.8967    int res;
  1.8968 @@ -69380,55 +67614,55 @@
  1.8969    UnpackedRecord *pIdxKey;
  1.8970    UnpackedRecord r;
  1.8971    char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
  1.8972 -#endif /* local variables moved into u.bd */
  1.8973 +#endif /* local variables moved into u.be */
  1.8974  
  1.8975  #ifdef SQLITE_TEST
  1.8976    sqlite3_found_count++;
  1.8977  #endif
  1.8978  
  1.8979 -  u.bd.alreadyExists = 0;
  1.8980 +  u.be.alreadyExists = 0;
  1.8981    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.8982    assert( pOp->p4type==P4_INT32 );
  1.8983 -  u.bd.pC = p->apCsr[pOp->p1];
  1.8984 -  assert( u.bd.pC!=0 );
  1.8985 +  u.be.pC = p->apCsr[pOp->p1];
  1.8986 +  assert( u.be.pC!=0 );
  1.8987    pIn3 = &aMem[pOp->p3];
  1.8988 -  if( ALWAYS(u.bd.pC->pCursor!=0) ){
  1.8989 -
  1.8990 -    assert( u.bd.pC->isTable==0 );
  1.8991 +  if( ALWAYS(u.be.pC->pCursor!=0) ){
  1.8992 +
  1.8993 +    assert( u.be.pC->isTable==0 );
  1.8994      if( pOp->p4.i>0 ){
  1.8995 -      u.bd.r.pKeyInfo = u.bd.pC->pKeyInfo;
  1.8996 -      u.bd.r.nField = (u16)pOp->p4.i;
  1.8997 -      u.bd.r.aMem = pIn3;
  1.8998 +      u.be.r.pKeyInfo = u.be.pC->pKeyInfo;
  1.8999 +      u.be.r.nField = (u16)pOp->p4.i;
  1.9000 +      u.be.r.aMem = pIn3;
  1.9001  #ifdef SQLITE_DEBUG
  1.9002 -      { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
  1.9003 -#endif
  1.9004 -      u.bd.r.flags = UNPACKED_PREFIX_MATCH;
  1.9005 -      u.bd.pIdxKey = &u.bd.r;
  1.9006 -    }else{
  1.9007 -      u.bd.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
  1.9008 -          u.bd.pC->pKeyInfo, u.bd.aTempRec, sizeof(u.bd.aTempRec), &u.bd.pFree
  1.9009 +      { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
  1.9010 +#endif
  1.9011 +      u.be.r.flags = UNPACKED_PREFIX_MATCH;
  1.9012 +      u.be.pIdxKey = &u.be.r;
  1.9013 +    }else{
  1.9014 +      u.be.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
  1.9015 +          u.be.pC->pKeyInfo, u.be.aTempRec, sizeof(u.be.aTempRec), &u.be.pFree
  1.9016        );
  1.9017 -      if( u.bd.pIdxKey==0 ) goto no_mem;
  1.9018 +      if( u.be.pIdxKey==0 ) goto no_mem;
  1.9019        assert( pIn3->flags & MEM_Blob );
  1.9020        assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
  1.9021 -      sqlite3VdbeRecordUnpack(u.bd.pC->pKeyInfo, pIn3->n, pIn3->z, u.bd.pIdxKey);
  1.9022 -      u.bd.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
  1.9023 -    }
  1.9024 -    rc = sqlite3BtreeMovetoUnpacked(u.bd.pC->pCursor, u.bd.pIdxKey, 0, 0, &u.bd.res);
  1.9025 +      sqlite3VdbeRecordUnpack(u.be.pC->pKeyInfo, pIn3->n, pIn3->z, u.be.pIdxKey);
  1.9026 +      u.be.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
  1.9027 +    }
  1.9028 +    rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, u.be.pIdxKey, 0, 0, &u.be.res);
  1.9029      if( pOp->p4.i==0 ){
  1.9030 -      sqlite3DbFree(db, u.bd.pFree);
  1.9031 +      sqlite3DbFree(db, u.be.pFree);
  1.9032      }
  1.9033      if( rc!=SQLITE_OK ){
  1.9034        break;
  1.9035      }
  1.9036 -    u.bd.alreadyExists = (u.bd.res==0);
  1.9037 -    u.bd.pC->deferredMoveto = 0;
  1.9038 -    u.bd.pC->cacheStatus = CACHE_STALE;
  1.9039 +    u.be.alreadyExists = (u.be.res==0);
  1.9040 +    u.be.pC->deferredMoveto = 0;
  1.9041 +    u.be.pC->cacheStatus = CACHE_STALE;
  1.9042    }
  1.9043    if( pOp->opcode==OP_Found ){
  1.9044 -    if( u.bd.alreadyExists ) pc = pOp->p2 - 1;
  1.9045 -  }else{
  1.9046 -    if( !u.bd.alreadyExists ) pc = pOp->p2 - 1;
  1.9047 +    if( u.be.alreadyExists ) pc = pOp->p2 - 1;
  1.9048 +  }else{
  1.9049 +    if( !u.be.alreadyExists ) pc = pOp->p2 - 1;
  1.9050    }
  1.9051    break;
  1.9052  }
  1.9053 @@ -69460,7 +67694,7 @@
  1.9054  ** See also: NotFound, NotExists, Found
  1.9055  */
  1.9056  case OP_IsUnique: {        /* jump, in3 */
  1.9057 -#if 0  /* local variables moved into u.be */
  1.9058 +#if 0  /* local variables moved into u.bf */
  1.9059    u16 ii;
  1.9060    VdbeCursor *pCx;
  1.9061    BtCursor *pCrsr;
  1.9062 @@ -69468,55 +67702,55 @@
  1.9063    Mem *aMx;
  1.9064    UnpackedRecord r;                  /* B-Tree index search key */
  1.9065    i64 R;                             /* Rowid stored in register P3 */
  1.9066 -#endif /* local variables moved into u.be */
  1.9067 +#endif /* local variables moved into u.bf */
  1.9068  
  1.9069    pIn3 = &aMem[pOp->p3];
  1.9070 -  u.be.aMx = &aMem[pOp->p4.i];
  1.9071 +  u.bf.aMx = &aMem[pOp->p4.i];
  1.9072    /* Assert that the values of parameters P1 and P4 are in range. */
  1.9073    assert( pOp->p4type==P4_INT32 );
  1.9074    assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
  1.9075    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9076  
  1.9077    /* Find the index cursor. */
  1.9078 -  u.be.pCx = p->apCsr[pOp->p1];
  1.9079 -  assert( u.be.pCx->deferredMoveto==0 );
  1.9080 -  u.be.pCx->seekResult = 0;
  1.9081 -  u.be.pCx->cacheStatus = CACHE_STALE;
  1.9082 -  u.be.pCrsr = u.be.pCx->pCursor;
  1.9083 +  u.bf.pCx = p->apCsr[pOp->p1];
  1.9084 +  assert( u.bf.pCx->deferredMoveto==0 );
  1.9085 +  u.bf.pCx->seekResult = 0;
  1.9086 +  u.bf.pCx->cacheStatus = CACHE_STALE;
  1.9087 +  u.bf.pCrsr = u.bf.pCx->pCursor;
  1.9088  
  1.9089    /* If any of the values are NULL, take the jump. */
  1.9090 -  u.be.nField = u.be.pCx->pKeyInfo->nField;
  1.9091 -  for(u.be.ii=0; u.be.ii<u.be.nField; u.be.ii++){
  1.9092 -    if( u.be.aMx[u.be.ii].flags & MEM_Null ){
  1.9093 +  u.bf.nField = u.bf.pCx->pKeyInfo->nField;
  1.9094 +  for(u.bf.ii=0; u.bf.ii<u.bf.nField; u.bf.ii++){
  1.9095 +    if( u.bf.aMx[u.bf.ii].flags & MEM_Null ){
  1.9096        pc = pOp->p2 - 1;
  1.9097 -      u.be.pCrsr = 0;
  1.9098 -      break;
  1.9099 -    }
  1.9100 -  }
  1.9101 -  assert( (u.be.aMx[u.be.nField].flags & MEM_Null)==0 );
  1.9102 -
  1.9103 -  if( u.be.pCrsr!=0 ){
  1.9104 +      u.bf.pCrsr = 0;
  1.9105 +      break;
  1.9106 +    }
  1.9107 +  }
  1.9108 +  assert( (u.bf.aMx[u.bf.nField].flags & MEM_Null)==0 );
  1.9109 +
  1.9110 +  if( u.bf.pCrsr!=0 ){
  1.9111      /* Populate the index search key. */
  1.9112 -    u.be.r.pKeyInfo = u.be.pCx->pKeyInfo;
  1.9113 -    u.be.r.nField = u.be.nField + 1;
  1.9114 -    u.be.r.flags = UNPACKED_PREFIX_SEARCH;
  1.9115 -    u.be.r.aMem = u.be.aMx;
  1.9116 +    u.bf.r.pKeyInfo = u.bf.pCx->pKeyInfo;
  1.9117 +    u.bf.r.nField = u.bf.nField + 1;
  1.9118 +    u.bf.r.flags = UNPACKED_PREFIX_SEARCH;
  1.9119 +    u.bf.r.aMem = u.bf.aMx;
  1.9120  #ifdef SQLITE_DEBUG
  1.9121 -    { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
  1.9122 -#endif
  1.9123 -
  1.9124 -    /* Extract the value of u.be.R from register P3. */
  1.9125 +    { int i; for(i=0; i<u.bf.r.nField; i++) assert( memIsValid(&u.bf.r.aMem[i]) ); }
  1.9126 +#endif
  1.9127 +
  1.9128 +    /* Extract the value of u.bf.R from register P3. */
  1.9129      sqlite3VdbeMemIntegerify(pIn3);
  1.9130 -    u.be.R = pIn3->u.i;
  1.9131 +    u.bf.R = pIn3->u.i;
  1.9132  
  1.9133      /* Search the B-Tree index. If no conflicting record is found, jump
  1.9134      ** to P2. Otherwise, copy the rowid of the conflicting record to
  1.9135      ** register P3 and fall through to the next instruction.  */
  1.9136 -    rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, &u.be.r, 0, 0, &u.be.pCx->seekResult);
  1.9137 -    if( (u.be.r.flags & UNPACKED_PREFIX_SEARCH) || u.be.r.rowid==u.be.R ){
  1.9138 +    rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, &u.bf.r, 0, 0, &u.bf.pCx->seekResult);
  1.9139 +    if( (u.bf.r.flags & UNPACKED_PREFIX_SEARCH) || u.bf.r.rowid==u.bf.R ){
  1.9140        pc = pOp->p2 - 1;
  1.9141      }else{
  1.9142 -      pIn3->u.i = u.be.r.rowid;
  1.9143 +      pIn3->u.i = u.bf.r.rowid;
  1.9144      }
  1.9145    }
  1.9146    break;
  1.9147 @@ -69537,42 +67771,42 @@
  1.9148  ** See also: Found, NotFound, IsUnique
  1.9149  */
  1.9150  case OP_NotExists: {        /* jump, in3 */
  1.9151 -#if 0  /* local variables moved into u.bf */
  1.9152 +#if 0  /* local variables moved into u.bg */
  1.9153    VdbeCursor *pC;
  1.9154    BtCursor *pCrsr;
  1.9155    int res;
  1.9156    u64 iKey;
  1.9157 -#endif /* local variables moved into u.bf */
  1.9158 +#endif /* local variables moved into u.bg */
  1.9159  
  1.9160    pIn3 = &aMem[pOp->p3];
  1.9161    assert( pIn3->flags & MEM_Int );
  1.9162    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9163 -  u.bf.pC = p->apCsr[pOp->p1];
  1.9164 -  assert( u.bf.pC!=0 );
  1.9165 -  assert( u.bf.pC->isTable );
  1.9166 -  assert( u.bf.pC->pseudoTableReg==0 );
  1.9167 -  u.bf.pCrsr = u.bf.pC->pCursor;
  1.9168 -  if( ALWAYS(u.bf.pCrsr!=0) ){
  1.9169 -    u.bf.res = 0;
  1.9170 -    u.bf.iKey = pIn3->u.i;
  1.9171 -    rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, 0, u.bf.iKey, 0, &u.bf.res);
  1.9172 -    u.bf.pC->lastRowid = pIn3->u.i;
  1.9173 -    u.bf.pC->rowidIsValid = u.bf.res==0 ?1:0;
  1.9174 -    u.bf.pC->nullRow = 0;
  1.9175 -    u.bf.pC->cacheStatus = CACHE_STALE;
  1.9176 -    u.bf.pC->deferredMoveto = 0;
  1.9177 -    if( u.bf.res!=0 ){
  1.9178 +  u.bg.pC = p->apCsr[pOp->p1];
  1.9179 +  assert( u.bg.pC!=0 );
  1.9180 +  assert( u.bg.pC->isTable );
  1.9181 +  assert( u.bg.pC->pseudoTableReg==0 );
  1.9182 +  u.bg.pCrsr = u.bg.pC->pCursor;
  1.9183 +  if( ALWAYS(u.bg.pCrsr!=0) ){
  1.9184 +    u.bg.res = 0;
  1.9185 +    u.bg.iKey = pIn3->u.i;
  1.9186 +    rc = sqlite3BtreeMovetoUnpacked(u.bg.pCrsr, 0, u.bg.iKey, 0, &u.bg.res);
  1.9187 +    u.bg.pC->lastRowid = pIn3->u.i;
  1.9188 +    u.bg.pC->rowidIsValid = u.bg.res==0 ?1:0;
  1.9189 +    u.bg.pC->nullRow = 0;
  1.9190 +    u.bg.pC->cacheStatus = CACHE_STALE;
  1.9191 +    u.bg.pC->deferredMoveto = 0;
  1.9192 +    if( u.bg.res!=0 ){
  1.9193        pc = pOp->p2 - 1;
  1.9194 -      assert( u.bf.pC->rowidIsValid==0 );
  1.9195 -    }
  1.9196 -    u.bf.pC->seekResult = u.bf.res;
  1.9197 +      assert( u.bg.pC->rowidIsValid==0 );
  1.9198 +    }
  1.9199 +    u.bg.pC->seekResult = u.bg.res;
  1.9200    }else{
  1.9201      /* This happens when an attempt to open a read cursor on the
  1.9202      ** sqlite_master table returns SQLITE_EMPTY.
  1.9203      */
  1.9204      pc = pOp->p2 - 1;
  1.9205 -    assert( u.bf.pC->rowidIsValid==0 );
  1.9206 -    u.bf.pC->seekResult = 0;
  1.9207 +    assert( u.bg.pC->rowidIsValid==0 );
  1.9208 +    u.bg.pC->seekResult = 0;
  1.9209    }
  1.9210    break;
  1.9211  }
  1.9212 @@ -69607,21 +67841,21 @@
  1.9213  ** AUTOINCREMENT feature.
  1.9214  */
  1.9215  case OP_NewRowid: {           /* out2-prerelease */
  1.9216 -#if 0  /* local variables moved into u.bg */
  1.9217 +#if 0  /* local variables moved into u.bh */
  1.9218    i64 v;                 /* The new rowid */
  1.9219    VdbeCursor *pC;        /* Cursor of table to get the new rowid */
  1.9220    int res;               /* Result of an sqlite3BtreeLast() */
  1.9221    int cnt;               /* Counter to limit the number of searches */
  1.9222    Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
  1.9223    VdbeFrame *pFrame;     /* Root frame of VDBE */
  1.9224 -#endif /* local variables moved into u.bg */
  1.9225 -
  1.9226 -  u.bg.v = 0;
  1.9227 -  u.bg.res = 0;
  1.9228 +#endif /* local variables moved into u.bh */
  1.9229 +
  1.9230 +  u.bh.v = 0;
  1.9231 +  u.bh.res = 0;
  1.9232    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9233 -  u.bg.pC = p->apCsr[pOp->p1];
  1.9234 -  assert( u.bg.pC!=0 );
  1.9235 -  if( NEVER(u.bg.pC->pCursor==0) ){
  1.9236 +  u.bh.pC = p->apCsr[pOp->p1];
  1.9237 +  assert( u.bh.pC!=0 );
  1.9238 +  if( NEVER(u.bh.pC->pCursor==0) ){
  1.9239      /* The zero initialization above is all that is needed */
  1.9240    }else{
  1.9241      /* The next rowid or record number (different terms for the same
  1.9242 @@ -69637,7 +67871,7 @@
  1.9243      ** succeeded.  If the random rowid does exist, we select a new one
  1.9244      ** and try again, up to 100 times.
  1.9245      */
  1.9246 -    assert( u.bg.pC->isTable );
  1.9247 +    assert( u.bh.pC->isTable );
  1.9248  
  1.9249  #ifdef SQLITE_32BIT_ROWID
  1.9250  #   define MAX_ROWID 0x7fffffff
  1.9251 @@ -69649,23 +67883,23 @@
  1.9252  #   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
  1.9253  #endif
  1.9254  
  1.9255 -    if( !u.bg.pC->useRandomRowid ){
  1.9256 -      u.bg.v = sqlite3BtreeGetCachedRowid(u.bg.pC->pCursor);
  1.9257 -      if( u.bg.v==0 ){
  1.9258 -        rc = sqlite3BtreeLast(u.bg.pC->pCursor, &u.bg.res);
  1.9259 +    if( !u.bh.pC->useRandomRowid ){
  1.9260 +      u.bh.v = sqlite3BtreeGetCachedRowid(u.bh.pC->pCursor);
  1.9261 +      if( u.bh.v==0 ){
  1.9262 +        rc = sqlite3BtreeLast(u.bh.pC->pCursor, &u.bh.res);
  1.9263          if( rc!=SQLITE_OK ){
  1.9264            goto abort_due_to_error;
  1.9265          }
  1.9266 -        if( u.bg.res ){
  1.9267 -          u.bg.v = 1;   /* IMP: R-61914-48074 */
  1.9268 +        if( u.bh.res ){
  1.9269 +          u.bh.v = 1;   /* IMP: R-61914-48074 */
  1.9270          }else{
  1.9271 -          assert( sqlite3BtreeCursorIsValid(u.bg.pC->pCursor) );
  1.9272 -          rc = sqlite3BtreeKeySize(u.bg.pC->pCursor, &u.bg.v);
  1.9273 +          assert( sqlite3BtreeCursorIsValid(u.bh.pC->pCursor) );
  1.9274 +          rc = sqlite3BtreeKeySize(u.bh.pC->pCursor, &u.bh.v);
  1.9275            assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
  1.9276 -          if( u.bg.v>=MAX_ROWID ){
  1.9277 -            u.bg.pC->useRandomRowid = 1;
  1.9278 +          if( u.bh.v>=MAX_ROWID ){
  1.9279 +            u.bh.pC->useRandomRowid = 1;
  1.9280            }else{
  1.9281 -            u.bg.v++;   /* IMP: R-29538-34987 */
  1.9282 +            u.bh.v++;   /* IMP: R-29538-34987 */
  1.9283            }
  1.9284          }
  1.9285        }
  1.9286 @@ -69675,35 +67909,35 @@
  1.9287          /* Assert that P3 is a valid memory cell. */
  1.9288          assert( pOp->p3>0 );
  1.9289          if( p->pFrame ){
  1.9290 -          for(u.bg.pFrame=p->pFrame; u.bg.pFrame->pParent; u.bg.pFrame=u.bg.pFrame->pParent);
  1.9291 +          for(u.bh.pFrame=p->pFrame; u.bh.pFrame->pParent; u.bh.pFrame=u.bh.pFrame->pParent);
  1.9292            /* Assert that P3 is a valid memory cell. */
  1.9293 -          assert( pOp->p3<=u.bg.pFrame->nMem );
  1.9294 -          u.bg.pMem = &u.bg.pFrame->aMem[pOp->p3];
  1.9295 +          assert( pOp->p3<=u.bh.pFrame->nMem );
  1.9296 +          u.bh.pMem = &u.bh.pFrame->aMem[pOp->p3];
  1.9297          }else{
  1.9298            /* Assert that P3 is a valid memory cell. */
  1.9299            assert( pOp->p3<=p->nMem );
  1.9300 -          u.bg.pMem = &aMem[pOp->p3];
  1.9301 -          memAboutToChange(p, u.bg.pMem);
  1.9302 -        }
  1.9303 -        assert( memIsValid(u.bg.pMem) );
  1.9304 -
  1.9305 -        REGISTER_TRACE(pOp->p3, u.bg.pMem);
  1.9306 -        sqlite3VdbeMemIntegerify(u.bg.pMem);
  1.9307 -        assert( (u.bg.pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
  1.9308 -        if( u.bg.pMem->u.i==MAX_ROWID || u.bg.pC->useRandomRowid ){
  1.9309 +          u.bh.pMem = &aMem[pOp->p3];
  1.9310 +          memAboutToChange(p, u.bh.pMem);
  1.9311 +        }
  1.9312 +        assert( memIsValid(u.bh.pMem) );
  1.9313 +
  1.9314 +        REGISTER_TRACE(pOp->p3, u.bh.pMem);
  1.9315 +        sqlite3VdbeMemIntegerify(u.bh.pMem);
  1.9316 +        assert( (u.bh.pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
  1.9317 +        if( u.bh.pMem->u.i==MAX_ROWID || u.bh.pC->useRandomRowid ){
  1.9318            rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
  1.9319            goto abort_due_to_error;
  1.9320          }
  1.9321 -        if( u.bg.v<u.bg.pMem->u.i+1 ){
  1.9322 -          u.bg.v = u.bg.pMem->u.i + 1;
  1.9323 -        }
  1.9324 -        u.bg.pMem->u.i = u.bg.v;
  1.9325 -      }
  1.9326 -#endif
  1.9327 -
  1.9328 -      sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, u.bg.v<MAX_ROWID ? u.bg.v+1 : 0);
  1.9329 -    }
  1.9330 -    if( u.bg.pC->useRandomRowid ){
  1.9331 +        if( u.bh.v<u.bh.pMem->u.i+1 ){
  1.9332 +          u.bh.v = u.bh.pMem->u.i + 1;
  1.9333 +        }
  1.9334 +        u.bh.pMem->u.i = u.bh.v;
  1.9335 +      }
  1.9336 +#endif
  1.9337 +
  1.9338 +      sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, u.bh.v<MAX_ROWID ? u.bh.v+1 : 0);
  1.9339 +    }
  1.9340 +    if( u.bh.pC->useRandomRowid ){
  1.9341        /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
  1.9342        ** largest possible integer (9223372036854775807) then the database
  1.9343        ** engine starts picking positive candidate ROWIDs at random until
  1.9344 @@ -69711,35 +67945,35 @@
  1.9345        assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
  1.9346                               ** an AUTOINCREMENT table. */
  1.9347        /* on the first attempt, simply do one more than previous */
  1.9348 -      u.bg.v = lastRowid;
  1.9349 -      u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
  1.9350 -      u.bg.v++; /* ensure non-zero */
  1.9351 -      u.bg.cnt = 0;
  1.9352 -      while(   ((rc = sqlite3BtreeMovetoUnpacked(u.bg.pC->pCursor, 0, (u64)u.bg.v,
  1.9353 -                                                 0, &u.bg.res))==SQLITE_OK)
  1.9354 -            && (u.bg.res==0)
  1.9355 -            && (++u.bg.cnt<100)){
  1.9356 +      u.bh.v = lastRowid;
  1.9357 +      u.bh.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
  1.9358 +      u.bh.v++; /* ensure non-zero */
  1.9359 +      u.bh.cnt = 0;
  1.9360 +      while(   ((rc = sqlite3BtreeMovetoUnpacked(u.bh.pC->pCursor, 0, (u64)u.bh.v,
  1.9361 +                                                 0, &u.bh.res))==SQLITE_OK)
  1.9362 +            && (u.bh.res==0)
  1.9363 +            && (++u.bh.cnt<100)){
  1.9364          /* collision - try another random rowid */
  1.9365 -        sqlite3_randomness(sizeof(u.bg.v), &u.bg.v);
  1.9366 -        if( u.bg.cnt<5 ){
  1.9367 +        sqlite3_randomness(sizeof(u.bh.v), &u.bh.v);
  1.9368 +        if( u.bh.cnt<5 ){
  1.9369            /* try "small" random rowids for the initial attempts */
  1.9370 -          u.bg.v &= 0xffffff;
  1.9371 +          u.bh.v &= 0xffffff;
  1.9372          }else{
  1.9373 -          u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
  1.9374 -        }
  1.9375 -        u.bg.v++; /* ensure non-zero */
  1.9376 -      }
  1.9377 -      if( rc==SQLITE_OK && u.bg.res==0 ){
  1.9378 +          u.bh.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
  1.9379 +        }
  1.9380 +        u.bh.v++; /* ensure non-zero */
  1.9381 +      }
  1.9382 +      if( rc==SQLITE_OK && u.bh.res==0 ){
  1.9383          rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
  1.9384          goto abort_due_to_error;
  1.9385        }
  1.9386 -      assert( u.bg.v>0 );  /* EV: R-40812-03570 */
  1.9387 -    }
  1.9388 -    u.bg.pC->rowidIsValid = 0;
  1.9389 -    u.bg.pC->deferredMoveto = 0;
  1.9390 -    u.bg.pC->cacheStatus = CACHE_STALE;
  1.9391 -  }
  1.9392 -  pOut->u.i = u.bg.v;
  1.9393 +      assert( u.bh.v>0 );  /* EV: R-40812-03570 */
  1.9394 +    }
  1.9395 +    u.bh.pC->rowidIsValid = 0;
  1.9396 +    u.bh.pC->deferredMoveto = 0;
  1.9397 +    u.bh.pC->cacheStatus = CACHE_STALE;
  1.9398 +  }
  1.9399 +  pOut->u.i = u.bh.v;
  1.9400    break;
  1.9401  }
  1.9402  
  1.9403 @@ -69789,7 +68023,7 @@
  1.9404  */
  1.9405  case OP_Insert: 
  1.9406  case OP_InsertInt: {
  1.9407 -#if 0  /* local variables moved into u.bh */
  1.9408 +#if 0  /* local variables moved into u.bi */
  1.9409    Mem *pData;       /* MEM cell holding data for the record to be inserted */
  1.9410    Mem *pKey;        /* MEM cell holding key  for the record */
  1.9411    i64 iKey;         /* The integer ROWID or key for the record to be inserted */
  1.9412 @@ -69799,60 +68033,60 @@
  1.9413    const char *zDb;  /* database name - used by the update hook */
  1.9414    const char *zTbl; /* Table name - used by the opdate hook */
  1.9415    int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
  1.9416 -#endif /* local variables moved into u.bh */
  1.9417 -
  1.9418 -  u.bh.pData = &aMem[pOp->p2];
  1.9419 +#endif /* local variables moved into u.bi */
  1.9420 +
  1.9421 +  u.bi.pData = &aMem[pOp->p2];
  1.9422    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9423 -  assert( memIsValid(u.bh.pData) );
  1.9424 -  u.bh.pC = p->apCsr[pOp->p1];
  1.9425 -  assert( u.bh.pC!=0 );
  1.9426 -  assert( u.bh.pC->pCursor!=0 );
  1.9427 -  assert( u.bh.pC->pseudoTableReg==0 );
  1.9428 -  assert( u.bh.pC->isTable );
  1.9429 -  REGISTER_TRACE(pOp->p2, u.bh.pData);
  1.9430 +  assert( memIsValid(u.bi.pData) );
  1.9431 +  u.bi.pC = p->apCsr[pOp->p1];
  1.9432 +  assert( u.bi.pC!=0 );
  1.9433 +  assert( u.bi.pC->pCursor!=0 );
  1.9434 +  assert( u.bi.pC->pseudoTableReg==0 );
  1.9435 +  assert( u.bi.pC->isTable );
  1.9436 +  REGISTER_TRACE(pOp->p2, u.bi.pData);
  1.9437  
  1.9438    if( pOp->opcode==OP_Insert ){
  1.9439 -    u.bh.pKey = &aMem[pOp->p3];
  1.9440 -    assert( u.bh.pKey->flags & MEM_Int );
  1.9441 -    assert( memIsValid(u.bh.pKey) );
  1.9442 -    REGISTER_TRACE(pOp->p3, u.bh.pKey);
  1.9443 -    u.bh.iKey = u.bh.pKey->u.i;
  1.9444 +    u.bi.pKey = &aMem[pOp->p3];
  1.9445 +    assert( u.bi.pKey->flags & MEM_Int );
  1.9446 +    assert( memIsValid(u.bi.pKey) );
  1.9447 +    REGISTER_TRACE(pOp->p3, u.bi.pKey);
  1.9448 +    u.bi.iKey = u.bi.pKey->u.i;
  1.9449    }else{
  1.9450      assert( pOp->opcode==OP_InsertInt );
  1.9451 -    u.bh.iKey = pOp->p3;
  1.9452 +    u.bi.iKey = pOp->p3;
  1.9453    }
  1.9454  
  1.9455    if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
  1.9456 -  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bh.iKey;
  1.9457 -  if( u.bh.pData->flags & MEM_Null ){
  1.9458 -    u.bh.pData->z = 0;
  1.9459 -    u.bh.pData->n = 0;
  1.9460 -  }else{
  1.9461 -    assert( u.bh.pData->flags & (MEM_Blob|MEM_Str) );
  1.9462 -  }
  1.9463 -  u.bh.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bh.pC->seekResult : 0);
  1.9464 -  if( u.bh.pData->flags & MEM_Zero ){
  1.9465 -    u.bh.nZero = u.bh.pData->u.nZero;
  1.9466 -  }else{
  1.9467 -    u.bh.nZero = 0;
  1.9468 -  }
  1.9469 -  sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
  1.9470 -  rc = sqlite3BtreeInsert(u.bh.pC->pCursor, 0, u.bh.iKey,
  1.9471 -                          u.bh.pData->z, u.bh.pData->n, u.bh.nZero,
  1.9472 -                          pOp->p5 & OPFLAG_APPEND, u.bh.seekResult
  1.9473 +  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bi.iKey;
  1.9474 +  if( u.bi.pData->flags & MEM_Null ){
  1.9475 +    u.bi.pData->z = 0;
  1.9476 +    u.bi.pData->n = 0;
  1.9477 +  }else{
  1.9478 +    assert( u.bi.pData->flags & (MEM_Blob|MEM_Str) );
  1.9479 +  }
  1.9480 +  u.bi.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bi.pC->seekResult : 0);
  1.9481 +  if( u.bi.pData->flags & MEM_Zero ){
  1.9482 +    u.bi.nZero = u.bi.pData->u.nZero;
  1.9483 +  }else{
  1.9484 +    u.bi.nZero = 0;
  1.9485 +  }
  1.9486 +  sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
  1.9487 +  rc = sqlite3BtreeInsert(u.bi.pC->pCursor, 0, u.bi.iKey,
  1.9488 +                          u.bi.pData->z, u.bi.pData->n, u.bi.nZero,
  1.9489 +                          pOp->p5 & OPFLAG_APPEND, u.bi.seekResult
  1.9490    );
  1.9491 -  u.bh.pC->rowidIsValid = 0;
  1.9492 -  u.bh.pC->deferredMoveto = 0;
  1.9493 -  u.bh.pC->cacheStatus = CACHE_STALE;
  1.9494 +  u.bi.pC->rowidIsValid = 0;
  1.9495 +  u.bi.pC->deferredMoveto = 0;
  1.9496 +  u.bi.pC->cacheStatus = CACHE_STALE;
  1.9497  
  1.9498    /* Invoke the update-hook if required. */
  1.9499    if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
  1.9500 -    u.bh.zDb = db->aDb[u.bh.pC->iDb].zName;
  1.9501 -    u.bh.zTbl = pOp->p4.z;
  1.9502 -    u.bh.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
  1.9503 -    assert( u.bh.pC->isTable );
  1.9504 -    db->xUpdateCallback(db->pUpdateArg, u.bh.op, u.bh.zDb, u.bh.zTbl, u.bh.iKey);
  1.9505 -    assert( u.bh.pC->iDb>=0 );
  1.9506 +    u.bi.zDb = db->aDb[u.bi.pC->iDb].zName;
  1.9507 +    u.bi.zTbl = pOp->p4.z;
  1.9508 +    u.bi.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
  1.9509 +    assert( u.bi.pC->isTable );
  1.9510 +    db->xUpdateCallback(db->pUpdateArg, u.bi.op, u.bi.zDb, u.bi.zTbl, u.bi.iKey);
  1.9511 +    assert( u.bi.pC->iDb>=0 );
  1.9512    }
  1.9513    break;
  1.9514  }
  1.9515 @@ -69878,47 +68112,47 @@
  1.9516  ** using OP_NotFound prior to invoking this opcode.
  1.9517  */
  1.9518  case OP_Delete: {
  1.9519 -#if 0  /* local variables moved into u.bi */
  1.9520 +#if 0  /* local variables moved into u.bj */
  1.9521    i64 iKey;
  1.9522    VdbeCursor *pC;
  1.9523 -#endif /* local variables moved into u.bi */
  1.9524 -
  1.9525 -  u.bi.iKey = 0;
  1.9526 +#endif /* local variables moved into u.bj */
  1.9527 +
  1.9528 +  u.bj.iKey = 0;
  1.9529    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9530 -  u.bi.pC = p->apCsr[pOp->p1];
  1.9531 -  assert( u.bi.pC!=0 );
  1.9532 -  assert( u.bi.pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
  1.9533 -
  1.9534 -  /* If the update-hook will be invoked, set u.bi.iKey to the rowid of the
  1.9535 +  u.bj.pC = p->apCsr[pOp->p1];
  1.9536 +  assert( u.bj.pC!=0 );
  1.9537 +  assert( u.bj.pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
  1.9538 +
  1.9539 +  /* If the update-hook will be invoked, set u.bj.iKey to the rowid of the
  1.9540    ** row being deleted.
  1.9541    */
  1.9542    if( db->xUpdateCallback && pOp->p4.z ){
  1.9543 -    assert( u.bi.pC->isTable );
  1.9544 -    assert( u.bi.pC->rowidIsValid );  /* lastRowid set by previous OP_NotFound */
  1.9545 -    u.bi.iKey = u.bi.pC->lastRowid;
  1.9546 +    assert( u.bj.pC->isTable );
  1.9547 +    assert( u.bj.pC->rowidIsValid );  /* lastRowid set by previous OP_NotFound */
  1.9548 +    u.bj.iKey = u.bj.pC->lastRowid;
  1.9549    }
  1.9550  
  1.9551    /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
  1.9552    ** OP_Column on the same table without any intervening operations that
  1.9553 -  ** might move or invalidate the cursor.  Hence cursor u.bi.pC is always pointing
  1.9554 +  ** might move or invalidate the cursor.  Hence cursor u.bj.pC is always pointing
  1.9555    ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
  1.9556    ** below is always a no-op and cannot fail.  We will run it anyhow, though,
  1.9557    ** to guard against future changes to the code generator.
  1.9558    **/
  1.9559 -  assert( u.bi.pC->deferredMoveto==0 );
  1.9560 -  rc = sqlite3VdbeCursorMoveto(u.bi.pC);
  1.9561 +  assert( u.bj.pC->deferredMoveto==0 );
  1.9562 +  rc = sqlite3VdbeCursorMoveto(u.bj.pC);
  1.9563    if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
  1.9564  
  1.9565 -  sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
  1.9566 -  rc = sqlite3BtreeDelete(u.bi.pC->pCursor);
  1.9567 -  u.bi.pC->cacheStatus = CACHE_STALE;
  1.9568 +  sqlite3BtreeSetCachedRowid(u.bj.pC->pCursor, 0);
  1.9569 +  rc = sqlite3BtreeDelete(u.bj.pC->pCursor);
  1.9570 +  u.bj.pC->cacheStatus = CACHE_STALE;
  1.9571  
  1.9572    /* Invoke the update-hook if required. */
  1.9573    if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
  1.9574 -    const char *zDb = db->aDb[u.bi.pC->iDb].zName;
  1.9575 +    const char *zDb = db->aDb[u.bj.pC->iDb].zName;
  1.9576      const char *zTbl = pOp->p4.z;
  1.9577 -    db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bi.iKey);
  1.9578 -    assert( u.bi.pC->iDb>=0 );
  1.9579 +    db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bj.iKey);
  1.9580 +    assert( u.bj.pC->iDb>=0 );
  1.9581    }
  1.9582    if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
  1.9583    break;
  1.9584 @@ -69944,16 +68178,16 @@
  1.9585  ** fall through to the next instruction. Otherwise, jump to instruction P2.
  1.9586  */
  1.9587  case OP_SorterCompare: {
  1.9588 -#if 0  /* local variables moved into u.bj */
  1.9589 +#if 0  /* local variables moved into u.bk */
  1.9590    VdbeCursor *pC;
  1.9591    int res;
  1.9592 -#endif /* local variables moved into u.bj */
  1.9593 -
  1.9594 -  u.bj.pC = p->apCsr[pOp->p1];
  1.9595 -  assert( isSorter(u.bj.pC) );
  1.9596 +#endif /* local variables moved into u.bk */
  1.9597 +
  1.9598 +  u.bk.pC = p->apCsr[pOp->p1];
  1.9599 +  assert( isSorter(u.bk.pC) );
  1.9600    pIn3 = &aMem[pOp->p3];
  1.9601 -  rc = sqlite3VdbeSorterCompare(u.bj.pC, pIn3, &u.bj.res);
  1.9602 -  if( u.bj.res ){
  1.9603 +  rc = sqlite3VdbeSorterCompare(u.bk.pC, pIn3, &u.bk.res);
  1.9604 +  if( u.bk.res ){
  1.9605      pc = pOp->p2-1;
  1.9606    }
  1.9607    break;
  1.9608 @@ -69964,14 +68198,15 @@
  1.9609  ** Write into register P2 the current sorter data for sorter cursor P1.
  1.9610  */
  1.9611  case OP_SorterData: {
  1.9612 -#if 0  /* local variables moved into u.bk */
  1.9613 +#if 0  /* local variables moved into u.bl */
  1.9614    VdbeCursor *pC;
  1.9615 -#endif /* local variables moved into u.bk */
  1.9616 +#endif /* local variables moved into u.bl */
  1.9617 +
  1.9618  #ifndef SQLITE_OMIT_MERGE_SORT
  1.9619    pOut = &aMem[pOp->p2];
  1.9620 -  u.bk.pC = p->apCsr[pOp->p1];
  1.9621 -  assert( u.bk.pC->isSorter );
  1.9622 -  rc = sqlite3VdbeSorterRowkey(u.bk.pC, pOut);
  1.9623 +  u.bl.pC = p->apCsr[pOp->p1];
  1.9624 +  assert( u.bl.pC->isSorter );
  1.9625 +  rc = sqlite3VdbeSorterRowkey(u.bl.pC, pOut);
  1.9626  #else
  1.9627    pOp->opcode = OP_RowKey;
  1.9628    pc--;
  1.9629 @@ -70001,62 +68236,62 @@
  1.9630  */
  1.9631  case OP_RowKey:
  1.9632  case OP_RowData: {
  1.9633 -#if 0  /* local variables moved into u.bl */
  1.9634 +#if 0  /* local variables moved into u.bm */
  1.9635    VdbeCursor *pC;
  1.9636    BtCursor *pCrsr;
  1.9637    u32 n;
  1.9638    i64 n64;
  1.9639 -#endif /* local variables moved into u.bl */
  1.9640 +#endif /* local variables moved into u.bm */
  1.9641  
  1.9642    pOut = &aMem[pOp->p2];
  1.9643    memAboutToChange(p, pOut);
  1.9644  
  1.9645    /* Note that RowKey and RowData are really exactly the same instruction */
  1.9646    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9647 -  u.bl.pC = p->apCsr[pOp->p1];
  1.9648 -  assert( u.bl.pC->isSorter==0 );
  1.9649 -  assert( u.bl.pC->isTable || pOp->opcode!=OP_RowData );
  1.9650 -  assert( u.bl.pC->isIndex || pOp->opcode==OP_RowData );
  1.9651 -  assert( u.bl.pC!=0 );
  1.9652 -  assert( u.bl.pC->nullRow==0 );
  1.9653 -  assert( u.bl.pC->pseudoTableReg==0 );
  1.9654 -  assert( u.bl.pC->pCursor!=0 );
  1.9655 -  u.bl.pCrsr = u.bl.pC->pCursor;
  1.9656 -  assert( sqlite3BtreeCursorIsValid(u.bl.pCrsr) );
  1.9657 +  u.bm.pC = p->apCsr[pOp->p1];
  1.9658 +  assert( u.bm.pC->isSorter==0 );
  1.9659 +  assert( u.bm.pC->isTable || pOp->opcode!=OP_RowData );
  1.9660 +  assert( u.bm.pC->isIndex || pOp->opcode==OP_RowData );
  1.9661 +  assert( u.bm.pC!=0 );
  1.9662 +  assert( u.bm.pC->nullRow==0 );
  1.9663 +  assert( u.bm.pC->pseudoTableReg==0 );
  1.9664 +  assert( u.bm.pC->pCursor!=0 );
  1.9665 +  u.bm.pCrsr = u.bm.pC->pCursor;
  1.9666 +  assert( sqlite3BtreeCursorIsValid(u.bm.pCrsr) );
  1.9667  
  1.9668    /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
  1.9669    ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
  1.9670    ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
  1.9671    ** a no-op and can never fail.  But we leave it in place as a safety.
  1.9672    */
  1.9673 -  assert( u.bl.pC->deferredMoveto==0 );
  1.9674 -  rc = sqlite3VdbeCursorMoveto(u.bl.pC);
  1.9675 +  assert( u.bm.pC->deferredMoveto==0 );
  1.9676 +  rc = sqlite3VdbeCursorMoveto(u.bm.pC);
  1.9677    if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
  1.9678  
  1.9679 -  if( u.bl.pC->isIndex ){
  1.9680 -    assert( !u.bl.pC->isTable );
  1.9681 -    VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bl.pCrsr, &u.bl.n64);
  1.9682 +  if( u.bm.pC->isIndex ){
  1.9683 +    assert( !u.bm.pC->isTable );
  1.9684 +    VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bm.pCrsr, &u.bm.n64);
  1.9685      assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
  1.9686 -    if( u.bl.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1.9687 +    if( u.bm.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1.9688        goto too_big;
  1.9689      }
  1.9690 -    u.bl.n = (u32)u.bl.n64;
  1.9691 -  }else{
  1.9692 -    VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bl.pCrsr, &u.bl.n);
  1.9693 +    u.bm.n = (u32)u.bm.n64;
  1.9694 +  }else{
  1.9695 +    VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bm.pCrsr, &u.bm.n);
  1.9696      assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
  1.9697 -    if( u.bl.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1.9698 +    if( u.bm.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1.9699        goto too_big;
  1.9700      }
  1.9701    }
  1.9702 -  if( sqlite3VdbeMemGrow(pOut, u.bl.n, 0) ){
  1.9703 +  if( sqlite3VdbeMemGrow(pOut, u.bm.n, 0) ){
  1.9704      goto no_mem;
  1.9705    }
  1.9706 -  pOut->n = u.bl.n;
  1.9707 +  pOut->n = u.bm.n;
  1.9708    MemSetTypeFlag(pOut, MEM_Blob);
  1.9709 -  if( u.bl.pC->isIndex ){
  1.9710 -    rc = sqlite3BtreeKey(u.bl.pCrsr, 0, u.bl.n, pOut->z);
  1.9711 -  }else{
  1.9712 -    rc = sqlite3BtreeData(u.bl.pCrsr, 0, u.bl.n, pOut->z);
  1.9713 +  if( u.bm.pC->isIndex ){
  1.9714 +    rc = sqlite3BtreeKey(u.bm.pCrsr, 0, u.bm.n, pOut->z);
  1.9715 +  }else{
  1.9716 +    rc = sqlite3BtreeData(u.bm.pCrsr, 0, u.bm.n, pOut->z);
  1.9717    }
  1.9718    pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
  1.9719    UPDATE_MAX_BLOBSIZE(pOut);
  1.9720 @@ -70073,42 +68308,42 @@
  1.9721  ** one opcode now works for both table types.
  1.9722  */
  1.9723  case OP_Rowid: {                 /* out2-prerelease */
  1.9724 -#if 0  /* local variables moved into u.bm */
  1.9725 +#if 0  /* local variables moved into u.bn */
  1.9726    VdbeCursor *pC;
  1.9727    i64 v;
  1.9728    sqlite3_vtab *pVtab;
  1.9729    const sqlite3_module *pModule;
  1.9730 -#endif /* local variables moved into u.bm */
  1.9731 +#endif /* local variables moved into u.bn */
  1.9732  
  1.9733    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9734 -  u.bm.pC = p->apCsr[pOp->p1];
  1.9735 -  assert( u.bm.pC!=0 );
  1.9736 -  assert( u.bm.pC->pseudoTableReg==0 );
  1.9737 -  if( u.bm.pC->nullRow ){
  1.9738 +  u.bn.pC = p->apCsr[pOp->p1];
  1.9739 +  assert( u.bn.pC!=0 );
  1.9740 +  assert( u.bn.pC->pseudoTableReg==0 || u.bn.pC->nullRow );
  1.9741 +  if( u.bn.pC->nullRow ){
  1.9742      pOut->flags = MEM_Null;
  1.9743      break;
  1.9744 -  }else if( u.bm.pC->deferredMoveto ){
  1.9745 -    u.bm.v = u.bm.pC->movetoTarget;
  1.9746 +  }else if( u.bn.pC->deferredMoveto ){
  1.9747 +    u.bn.v = u.bn.pC->movetoTarget;
  1.9748  #ifndef SQLITE_OMIT_VIRTUALTABLE
  1.9749 -  }else if( u.bm.pC->pVtabCursor ){
  1.9750 -    u.bm.pVtab = u.bm.pC->pVtabCursor->pVtab;
  1.9751 -    u.bm.pModule = u.bm.pVtab->pModule;
  1.9752 -    assert( u.bm.pModule->xRowid );
  1.9753 -    rc = u.bm.pModule->xRowid(u.bm.pC->pVtabCursor, &u.bm.v);
  1.9754 -    importVtabErrMsg(p, u.bm.pVtab);
  1.9755 +  }else if( u.bn.pC->pVtabCursor ){
  1.9756 +    u.bn.pVtab = u.bn.pC->pVtabCursor->pVtab;
  1.9757 +    u.bn.pModule = u.bn.pVtab->pModule;
  1.9758 +    assert( u.bn.pModule->xRowid );
  1.9759 +    rc = u.bn.pModule->xRowid(u.bn.pC->pVtabCursor, &u.bn.v);
  1.9760 +    importVtabErrMsg(p, u.bn.pVtab);
  1.9761  #endif /* SQLITE_OMIT_VIRTUALTABLE */
  1.9762    }else{
  1.9763 -    assert( u.bm.pC->pCursor!=0 );
  1.9764 -    rc = sqlite3VdbeCursorMoveto(u.bm.pC);
  1.9765 +    assert( u.bn.pC->pCursor!=0 );
  1.9766 +    rc = sqlite3VdbeCursorMoveto(u.bn.pC);
  1.9767      if( rc ) goto abort_due_to_error;
  1.9768 -    if( u.bm.pC->rowidIsValid ){
  1.9769 -      u.bm.v = u.bm.pC->lastRowid;
  1.9770 -    }else{
  1.9771 -      rc = sqlite3BtreeKeySize(u.bm.pC->pCursor, &u.bm.v);
  1.9772 +    if( u.bn.pC->rowidIsValid ){
  1.9773 +      u.bn.v = u.bn.pC->lastRowid;
  1.9774 +    }else{
  1.9775 +      rc = sqlite3BtreeKeySize(u.bn.pC->pCursor, &u.bn.v);
  1.9776        assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
  1.9777      }
  1.9778    }
  1.9779 -  pOut->u.i = u.bm.v;
  1.9780 +  pOut->u.i = u.bn.v;
  1.9781    break;
  1.9782  }
  1.9783  
  1.9784 @@ -70119,18 +68354,18 @@
  1.9785  ** write a NULL.
  1.9786  */
  1.9787  case OP_NullRow: {
  1.9788 -#if 0  /* local variables moved into u.bn */
  1.9789 +#if 0  /* local variables moved into u.bo */
  1.9790    VdbeCursor *pC;
  1.9791 -#endif /* local variables moved into u.bn */
  1.9792 +#endif /* local variables moved into u.bo */
  1.9793  
  1.9794    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9795 -  u.bn.pC = p->apCsr[pOp->p1];
  1.9796 -  assert( u.bn.pC!=0 );
  1.9797 -  u.bn.pC->nullRow = 1;
  1.9798 -  u.bn.pC->rowidIsValid = 0;
  1.9799 -  assert( u.bn.pC->pCursor || u.bn.pC->pVtabCursor );
  1.9800 -  if( u.bn.pC->pCursor ){
  1.9801 -    sqlite3BtreeClearCursor(u.bn.pC->pCursor);
  1.9802 +  u.bo.pC = p->apCsr[pOp->p1];
  1.9803 +  assert( u.bo.pC!=0 );
  1.9804 +  u.bo.pC->nullRow = 1;
  1.9805 +  u.bo.pC->rowidIsValid = 0;
  1.9806 +  assert( u.bo.pC->pCursor || u.bo.pC->pVtabCursor );
  1.9807 +  if( u.bo.pC->pCursor ){
  1.9808 +    sqlite3BtreeClearCursor(u.bo.pC->pCursor);
  1.9809    }
  1.9810    break;
  1.9811  }
  1.9812 @@ -70144,25 +68379,25 @@
  1.9813  ** to the following instruction.
  1.9814  */
  1.9815  case OP_Last: {        /* jump */
  1.9816 -#if 0  /* local variables moved into u.bo */
  1.9817 +#if 0  /* local variables moved into u.bp */
  1.9818    VdbeCursor *pC;
  1.9819    BtCursor *pCrsr;
  1.9820    int res;
  1.9821 -#endif /* local variables moved into u.bo */
  1.9822 +#endif /* local variables moved into u.bp */
  1.9823  
  1.9824    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9825 -  u.bo.pC = p->apCsr[pOp->p1];
  1.9826 -  assert( u.bo.pC!=0 );
  1.9827 -  u.bo.pCrsr = u.bo.pC->pCursor;
  1.9828 -  u.bo.res = 0;
  1.9829 -  if( ALWAYS(u.bo.pCrsr!=0) ){
  1.9830 -    rc = sqlite3BtreeLast(u.bo.pCrsr, &u.bo.res);
  1.9831 -  }
  1.9832 -  u.bo.pC->nullRow = (u8)u.bo.res;
  1.9833 -  u.bo.pC->deferredMoveto = 0;
  1.9834 -  u.bo.pC->rowidIsValid = 0;
  1.9835 -  u.bo.pC->cacheStatus = CACHE_STALE;
  1.9836 -  if( pOp->p2>0 && u.bo.res ){
  1.9837 +  u.bp.pC = p->apCsr[pOp->p1];
  1.9838 +  assert( u.bp.pC!=0 );
  1.9839 +  u.bp.pCrsr = u.bp.pC->pCursor;
  1.9840 +  u.bp.res = 0;
  1.9841 +  if( ALWAYS(u.bp.pCrsr!=0) ){
  1.9842 +    rc = sqlite3BtreeLast(u.bp.pCrsr, &u.bp.res);
  1.9843 +  }
  1.9844 +  u.bp.pC->nullRow = (u8)u.bp.res;
  1.9845 +  u.bp.pC->deferredMoveto = 0;
  1.9846 +  u.bp.pC->rowidIsValid = 0;
  1.9847 +  u.bp.pC->cacheStatus = CACHE_STALE;
  1.9848 +  if( pOp->p2>0 && u.bp.res ){
  1.9849      pc = pOp->p2 - 1;
  1.9850    }
  1.9851    break;
  1.9852 @@ -70202,31 +68437,31 @@
  1.9853  ** to the following instruction.
  1.9854  */
  1.9855  case OP_Rewind: {        /* jump */
  1.9856 -#if 0  /* local variables moved into u.bp */
  1.9857 +#if 0  /* local variables moved into u.bq */
  1.9858    VdbeCursor *pC;
  1.9859    BtCursor *pCrsr;
  1.9860    int res;
  1.9861 -#endif /* local variables moved into u.bp */
  1.9862 +#endif /* local variables moved into u.bq */
  1.9863  
  1.9864    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9865 -  u.bp.pC = p->apCsr[pOp->p1];
  1.9866 -  assert( u.bp.pC!=0 );
  1.9867 -  assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterSort) );
  1.9868 -  u.bp.res = 1;
  1.9869 -  if( isSorter(u.bp.pC) ){
  1.9870 -    rc = sqlite3VdbeSorterRewind(db, u.bp.pC, &u.bp.res);
  1.9871 -  }else{
  1.9872 -    u.bp.pCrsr = u.bp.pC->pCursor;
  1.9873 -    assert( u.bp.pCrsr );
  1.9874 -    rc = sqlite3BtreeFirst(u.bp.pCrsr, &u.bp.res);
  1.9875 -    u.bp.pC->atFirst = u.bp.res==0 ?1:0;
  1.9876 -    u.bp.pC->deferredMoveto = 0;
  1.9877 -    u.bp.pC->cacheStatus = CACHE_STALE;
  1.9878 -    u.bp.pC->rowidIsValid = 0;
  1.9879 -  }
  1.9880 -  u.bp.pC->nullRow = (u8)u.bp.res;
  1.9881 +  u.bq.pC = p->apCsr[pOp->p1];
  1.9882 +  assert( u.bq.pC!=0 );
  1.9883 +  assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterSort) );
  1.9884 +  u.bq.res = 1;
  1.9885 +  if( isSorter(u.bq.pC) ){
  1.9886 +    rc = sqlite3VdbeSorterRewind(db, u.bq.pC, &u.bq.res);
  1.9887 +  }else{
  1.9888 +    u.bq.pCrsr = u.bq.pC->pCursor;
  1.9889 +    assert( u.bq.pCrsr );
  1.9890 +    rc = sqlite3BtreeFirst(u.bq.pCrsr, &u.bq.res);
  1.9891 +    u.bq.pC->atFirst = u.bq.res==0 ?1:0;
  1.9892 +    u.bq.pC->deferredMoveto = 0;
  1.9893 +    u.bq.pC->cacheStatus = CACHE_STALE;
  1.9894 +    u.bq.pC->rowidIsValid = 0;
  1.9895 +  }
  1.9896 +  u.bq.pC->nullRow = (u8)u.bq.res;
  1.9897    assert( pOp->p2>0 && pOp->p2<p->nOp );
  1.9898 -  if( u.bp.res ){
  1.9899 +  if( u.bq.res ){
  1.9900      pc = pOp->p2 - 1;
  1.9901    }
  1.9902    break;
  1.9903 @@ -70270,40 +68505,40 @@
  1.9904  #endif
  1.9905  case OP_Prev:          /* jump */
  1.9906  case OP_Next: {        /* jump */
  1.9907 -#if 0  /* local variables moved into u.bq */
  1.9908 +#if 0  /* local variables moved into u.br */
  1.9909    VdbeCursor *pC;
  1.9910    int res;
  1.9911 -#endif /* local variables moved into u.bq */
  1.9912 +#endif /* local variables moved into u.br */
  1.9913  
  1.9914    CHECK_FOR_INTERRUPT;
  1.9915    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9916    assert( pOp->p5<=ArraySize(p->aCounter) );
  1.9917 -  u.bq.pC = p->apCsr[pOp->p1];
  1.9918 -  if( u.bq.pC==0 ){
  1.9919 +  u.br.pC = p->apCsr[pOp->p1];
  1.9920 +  if( u.br.pC==0 ){
  1.9921      break;  /* See ticket #2273 */
  1.9922    }
  1.9923 -  assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterNext) );
  1.9924 -  if( isSorter(u.bq.pC) ){
  1.9925 +  assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterNext) );
  1.9926 +  if( isSorter(u.br.pC) ){
  1.9927      assert( pOp->opcode==OP_SorterNext );
  1.9928 -    rc = sqlite3VdbeSorterNext(db, u.bq.pC, &u.bq.res);
  1.9929 -  }else{
  1.9930 -    u.bq.res = 1;
  1.9931 -    assert( u.bq.pC->deferredMoveto==0 );
  1.9932 -    assert( u.bq.pC->pCursor );
  1.9933 +    rc = sqlite3VdbeSorterNext(db, u.br.pC, &u.br.res);
  1.9934 +  }else{
  1.9935 +    u.br.res = 1;
  1.9936 +    assert( u.br.pC->deferredMoveto==0 );
  1.9937 +    assert( u.br.pC->pCursor );
  1.9938      assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
  1.9939      assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
  1.9940 -    rc = pOp->p4.xAdvance(u.bq.pC->pCursor, &u.bq.res);
  1.9941 -  }
  1.9942 -  u.bq.pC->nullRow = (u8)u.bq.res;
  1.9943 -  u.bq.pC->cacheStatus = CACHE_STALE;
  1.9944 -  if( u.bq.res==0 ){
  1.9945 +    rc = pOp->p4.xAdvance(u.br.pC->pCursor, &u.br.res);
  1.9946 +  }
  1.9947 +  u.br.pC->nullRow = (u8)u.br.res;
  1.9948 +  u.br.pC->cacheStatus = CACHE_STALE;
  1.9949 +  if( u.br.res==0 ){
  1.9950      pc = pOp->p2 - 1;
  1.9951      if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
  1.9952  #ifdef SQLITE_TEST
  1.9953      sqlite3_search_count++;
  1.9954  #endif
  1.9955    }
  1.9956 -  u.bq.pC->rowidIsValid = 0;
  1.9957 +  u.br.pC->rowidIsValid = 0;
  1.9958    break;
  1.9959  }
  1.9960  
  1.9961 @@ -70324,34 +68559,34 @@
  1.9962    pOp->opcode = OP_IdxInsert;
  1.9963  #endif
  1.9964  case OP_IdxInsert: {        /* in2 */
  1.9965 -#if 0  /* local variables moved into u.br */
  1.9966 +#if 0  /* local variables moved into u.bs */
  1.9967    VdbeCursor *pC;
  1.9968    BtCursor *pCrsr;
  1.9969    int nKey;
  1.9970    const char *zKey;
  1.9971 -#endif /* local variables moved into u.br */
  1.9972 +#endif /* local variables moved into u.bs */
  1.9973  
  1.9974    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  1.9975 -  u.br.pC = p->apCsr[pOp->p1];
  1.9976 -  assert( u.br.pC!=0 );
  1.9977 -  assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
  1.9978 +  u.bs.pC = p->apCsr[pOp->p1];
  1.9979 +  assert( u.bs.pC!=0 );
  1.9980 +  assert( u.bs.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
  1.9981    pIn2 = &aMem[pOp->p2];
  1.9982    assert( pIn2->flags & MEM_Blob );
  1.9983 -  u.br.pCrsr = u.br.pC->pCursor;
  1.9984 -  if( ALWAYS(u.br.pCrsr!=0) ){
  1.9985 -    assert( u.br.pC->isTable==0 );
  1.9986 +  u.bs.pCrsr = u.bs.pC->pCursor;
  1.9987 +  if( ALWAYS(u.bs.pCrsr!=0) ){
  1.9988 +    assert( u.bs.pC->isTable==0 );
  1.9989      rc = ExpandBlob(pIn2);
  1.9990      if( rc==SQLITE_OK ){
  1.9991 -      if( isSorter(u.br.pC) ){
  1.9992 -        rc = sqlite3VdbeSorterWrite(db, u.br.pC, pIn2);
  1.9993 -      }else{
  1.9994 -        u.br.nKey = pIn2->n;
  1.9995 -        u.br.zKey = pIn2->z;
  1.9996 -        rc = sqlite3BtreeInsert(u.br.pCrsr, u.br.zKey, u.br.nKey, "", 0, 0, pOp->p3,
  1.9997 -            ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.br.pC->seekResult : 0)
  1.9998 +      if( isSorter(u.bs.pC) ){
  1.9999 +        rc = sqlite3VdbeSorterWrite(db, u.bs.pC, pIn2);
 1.10000 +      }else{
 1.10001 +        u.bs.nKey = pIn2->n;
 1.10002 +        u.bs.zKey = pIn2->z;
 1.10003 +        rc = sqlite3BtreeInsert(u.bs.pCrsr, u.bs.zKey, u.bs.nKey, "", 0, 0, pOp->p3,
 1.10004 +            ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bs.pC->seekResult : 0)
 1.10005              );
 1.10006 -        assert( u.br.pC->deferredMoveto==0 );
 1.10007 -        u.br.pC->cacheStatus = CACHE_STALE;
 1.10008 +        assert( u.bs.pC->deferredMoveto==0 );
 1.10009 +        u.bs.pC->cacheStatus = CACHE_STALE;
 1.10010        }
 1.10011      }
 1.10012    }
 1.10013 @@ -70365,33 +68600,33 @@
 1.10014  ** index opened by cursor P1.
 1.10015  */
 1.10016  case OP_IdxDelete: {
 1.10017 -#if 0  /* local variables moved into u.bs */
 1.10018 +#if 0  /* local variables moved into u.bt */
 1.10019    VdbeCursor *pC;
 1.10020    BtCursor *pCrsr;
 1.10021    int res;
 1.10022    UnpackedRecord r;
 1.10023 -#endif /* local variables moved into u.bs */
 1.10024 +#endif /* local variables moved into u.bt */
 1.10025  
 1.10026    assert( pOp->p3>0 );
 1.10027    assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
 1.10028    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.10029 -  u.bs.pC = p->apCsr[pOp->p1];
 1.10030 -  assert( u.bs.pC!=0 );
 1.10031 -  u.bs.pCrsr = u.bs.pC->pCursor;
 1.10032 -  if( ALWAYS(u.bs.pCrsr!=0) ){
 1.10033 -    u.bs.r.pKeyInfo = u.bs.pC->pKeyInfo;
 1.10034 -    u.bs.r.nField = (u16)pOp->p3;
 1.10035 -    u.bs.r.flags = 0;
 1.10036 -    u.bs.r.aMem = &aMem[pOp->p2];
 1.10037 +  u.bt.pC = p->apCsr[pOp->p1];
 1.10038 +  assert( u.bt.pC!=0 );
 1.10039 +  u.bt.pCrsr = u.bt.pC->pCursor;
 1.10040 +  if( ALWAYS(u.bt.pCrsr!=0) ){
 1.10041 +    u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
 1.10042 +    u.bt.r.nField = (u16)pOp->p3;
 1.10043 +    u.bt.r.flags = 0;
 1.10044 +    u.bt.r.aMem = &aMem[pOp->p2];
 1.10045  #ifdef SQLITE_DEBUG
 1.10046 -    { int i; for(i=0; i<u.bs.r.nField; i++) assert( memIsValid(&u.bs.r.aMem[i]) ); }
 1.10047 -#endif
 1.10048 -    rc = sqlite3BtreeMovetoUnpacked(u.bs.pCrsr, &u.bs.r, 0, 0, &u.bs.res);
 1.10049 -    if( rc==SQLITE_OK && u.bs.res==0 ){
 1.10050 -      rc = sqlite3BtreeDelete(u.bs.pCrsr);
 1.10051 -    }
 1.10052 -    assert( u.bs.pC->deferredMoveto==0 );
 1.10053 -    u.bs.pC->cacheStatus = CACHE_STALE;
 1.10054 +    { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); }
 1.10055 +#endif
 1.10056 +    rc = sqlite3BtreeMovetoUnpacked(u.bt.pCrsr, &u.bt.r, 0, 0, &u.bt.res);
 1.10057 +    if( rc==SQLITE_OK && u.bt.res==0 ){
 1.10058 +      rc = sqlite3BtreeDelete(u.bt.pCrsr);
 1.10059 +    }
 1.10060 +    assert( u.bt.pC->deferredMoveto==0 );
 1.10061 +    u.bt.pC->cacheStatus = CACHE_STALE;
 1.10062    }
 1.10063    break;
 1.10064  }
 1.10065 @@ -70405,28 +68640,28 @@
 1.10066  ** See also: Rowid, MakeRecord.
 1.10067  */
 1.10068  case OP_IdxRowid: {              /* out2-prerelease */
 1.10069 -#if 0  /* local variables moved into u.bt */
 1.10070 +#if 0  /* local variables moved into u.bu */
 1.10071    BtCursor *pCrsr;
 1.10072    VdbeCursor *pC;
 1.10073    i64 rowid;
 1.10074 -#endif /* local variables moved into u.bt */
 1.10075 +#endif /* local variables moved into u.bu */
 1.10076  
 1.10077    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.10078 -  u.bt.pC = p->apCsr[pOp->p1];
 1.10079 -  assert( u.bt.pC!=0 );
 1.10080 -  u.bt.pCrsr = u.bt.pC->pCursor;
 1.10081 +  u.bu.pC = p->apCsr[pOp->p1];
 1.10082 +  assert( u.bu.pC!=0 );
 1.10083 +  u.bu.pCrsr = u.bu.pC->pCursor;
 1.10084    pOut->flags = MEM_Null;
 1.10085 -  if( ALWAYS(u.bt.pCrsr!=0) ){
 1.10086 -    rc = sqlite3VdbeCursorMoveto(u.bt.pC);
 1.10087 +  if( ALWAYS(u.bu.pCrsr!=0) ){
 1.10088 +    rc = sqlite3VdbeCursorMoveto(u.bu.pC);
 1.10089      if( NEVER(rc) ) goto abort_due_to_error;
 1.10090 -    assert( u.bt.pC->deferredMoveto==0 );
 1.10091 -    assert( u.bt.pC->isTable==0 );
 1.10092 -    if( !u.bt.pC->nullRow ){
 1.10093 -      rc = sqlite3VdbeIdxRowid(db, u.bt.pCrsr, &u.bt.rowid);
 1.10094 +    assert( u.bu.pC->deferredMoveto==0 );
 1.10095 +    assert( u.bu.pC->isTable==0 );
 1.10096 +    if( !u.bu.pC->nullRow ){
 1.10097 +      rc = sqlite3VdbeIdxRowid(db, u.bu.pCrsr, &u.bu.rowid);
 1.10098        if( rc!=SQLITE_OK ){
 1.10099          goto abort_due_to_error;
 1.10100        }
 1.10101 -      pOut->u.i = u.bt.rowid;
 1.10102 +      pOut->u.i = u.bu.rowid;
 1.10103        pOut->flags = MEM_Int;
 1.10104      }
 1.10105    }
 1.10106 @@ -70461,39 +68696,39 @@
 1.10107  */
 1.10108  case OP_IdxLT:          /* jump */
 1.10109  case OP_IdxGE: {        /* jump */
 1.10110 -#if 0  /* local variables moved into u.bu */
 1.10111 +#if 0  /* local variables moved into u.bv */
 1.10112    VdbeCursor *pC;
 1.10113    int res;
 1.10114    UnpackedRecord r;
 1.10115 -#endif /* local variables moved into u.bu */
 1.10116 +#endif /* local variables moved into u.bv */
 1.10117  
 1.10118    assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.10119 -  u.bu.pC = p->apCsr[pOp->p1];
 1.10120 -  assert( u.bu.pC!=0 );
 1.10121 -  assert( u.bu.pC->isOrdered );
 1.10122 -  if( ALWAYS(u.bu.pC->pCursor!=0) ){
 1.10123 -    assert( u.bu.pC->deferredMoveto==0 );
 1.10124 +  u.bv.pC = p->apCsr[pOp->p1];
 1.10125 +  assert( u.bv.pC!=0 );
 1.10126 +  assert( u.bv.pC->isOrdered );
 1.10127 +  if( ALWAYS(u.bv.pC->pCursor!=0) ){
 1.10128 +    assert( u.bv.pC->deferredMoveto==0 );
 1.10129      assert( pOp->p5==0 || pOp->p5==1 );
 1.10130      assert( pOp->p4type==P4_INT32 );
 1.10131 -    u.bu.r.pKeyInfo = u.bu.pC->pKeyInfo;
 1.10132 -    u.bu.r.nField = (u16)pOp->p4.i;
 1.10133 +    u.bv.r.pKeyInfo = u.bv.pC->pKeyInfo;
 1.10134 +    u.bv.r.nField = (u16)pOp->p4.i;
 1.10135      if( pOp->p5 ){
 1.10136 -      u.bu.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
 1.10137 -    }else{
 1.10138 -      u.bu.r.flags = UNPACKED_PREFIX_MATCH;
 1.10139 -    }
 1.10140 -    u.bu.r.aMem = &aMem[pOp->p3];
 1.10141 +      u.bv.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
 1.10142 +    }else{
 1.10143 +      u.bv.r.flags = UNPACKED_PREFIX_MATCH;
 1.10144 +    }
 1.10145 +    u.bv.r.aMem = &aMem[pOp->p3];
 1.10146  #ifdef SQLITE_DEBUG
 1.10147 -    { int i; for(i=0; i<u.bu.r.nField; i++) assert( memIsValid(&u.bu.r.aMem[i]) ); }
 1.10148 -#endif
 1.10149 -    rc = sqlite3VdbeIdxKeyCompare(u.bu.pC, &u.bu.r, &u.bu.res);
 1.10150 +    { int i; for(i=0; i<u.bv.r.nField; i++) assert( memIsValid(&u.bv.r.aMem[i]) ); }
 1.10151 +#endif
 1.10152 +    rc = sqlite3VdbeIdxKeyCompare(u.bv.pC, &u.bv.r, &u.bv.res);
 1.10153      if( pOp->opcode==OP_IdxLT ){
 1.10154 -      u.bu.res = -u.bu.res;
 1.10155 +      u.bv.res = -u.bv.res;
 1.10156      }else{
 1.10157        assert( pOp->opcode==OP_IdxGE );
 1.10158 -      u.bu.res++;
 1.10159 -    }
 1.10160 -    if( u.bu.res>0 ){
 1.10161 +      u.bv.res++;
 1.10162 +    }
 1.10163 +    if( u.bv.res>0 ){
 1.10164        pc = pOp->p2 - 1 ;
 1.10165      }
 1.10166    }
 1.10167 @@ -70521,39 +68756,40 @@
 1.10168  ** See also: Clear
 1.10169  */
 1.10170  case OP_Destroy: {     /* out2-prerelease */
 1.10171 -#if 0  /* local variables moved into u.bv */
 1.10172 +#if 0  /* local variables moved into u.bw */
 1.10173    int iMoved;
 1.10174    int iCnt;
 1.10175    Vdbe *pVdbe;
 1.10176    int iDb;
 1.10177 -#endif /* local variables moved into u.bv */
 1.10178 +#endif /* local variables moved into u.bw */
 1.10179 +
 1.10180  #ifndef SQLITE_OMIT_VIRTUALTABLE
 1.10181 -  u.bv.iCnt = 0;
 1.10182 -  for(u.bv.pVdbe=db->pVdbe; u.bv.pVdbe; u.bv.pVdbe = u.bv.pVdbe->pNext){
 1.10183 -    if( u.bv.pVdbe->magic==VDBE_MAGIC_RUN && u.bv.pVdbe->inVtabMethod<2 && u.bv.pVdbe->pc>=0 ){
 1.10184 -      u.bv.iCnt++;
 1.10185 -    }
 1.10186 -  }
 1.10187 -#else
 1.10188 -  u.bv.iCnt = db->activeVdbeCnt;
 1.10189 +  u.bw.iCnt = 0;
 1.10190 +  for(u.bw.pVdbe=db->pVdbe; u.bw.pVdbe; u.bw.pVdbe = u.bw.pVdbe->pNext){
 1.10191 +    if( u.bw.pVdbe->magic==VDBE_MAGIC_RUN && u.bw.pVdbe->inVtabMethod<2 && u.bw.pVdbe->pc>=0 ){
 1.10192 +      u.bw.iCnt++;
 1.10193 +    }
 1.10194 +  }
 1.10195 +#else
 1.10196 +  u.bw.iCnt = db->activeVdbeCnt;
 1.10197  #endif
 1.10198    pOut->flags = MEM_Null;
 1.10199 -  if( u.bv.iCnt>1 ){
 1.10200 +  if( u.bw.iCnt>1 ){
 1.10201      rc = SQLITE_LOCKED;
 1.10202      p->errorAction = OE_Abort;
 1.10203    }else{
 1.10204 -    u.bv.iDb = pOp->p3;
 1.10205 -    assert( u.bv.iCnt==1 );
 1.10206 -    assert( (p->btreeMask & (((yDbMask)1)<<u.bv.iDb))!=0 );
 1.10207 -    rc = sqlite3BtreeDropTable(db->aDb[u.bv.iDb].pBt, pOp->p1, &u.bv.iMoved);
 1.10208 +    u.bw.iDb = pOp->p3;
 1.10209 +    assert( u.bw.iCnt==1 );
 1.10210 +    assert( (p->btreeMask & (((yDbMask)1)<<u.bw.iDb))!=0 );
 1.10211 +    rc = sqlite3BtreeDropTable(db->aDb[u.bw.iDb].pBt, pOp->p1, &u.bw.iMoved);
 1.10212      pOut->flags = MEM_Int;
 1.10213 -    pOut->u.i = u.bv.iMoved;
 1.10214 +    pOut->u.i = u.bw.iMoved;
 1.10215  #ifndef SQLITE_OMIT_AUTOVACUUM
 1.10216 -    if( rc==SQLITE_OK && u.bv.iMoved!=0 ){
 1.10217 -      sqlite3RootPageMoved(db, u.bv.iDb, u.bv.iMoved, pOp->p1);
 1.10218 +    if( rc==SQLITE_OK && u.bw.iMoved!=0 ){
 1.10219 +      sqlite3RootPageMoved(db, u.bw.iDb, u.bw.iMoved, pOp->p1);
 1.10220        /* All OP_Destroy operations occur on the same btree */
 1.10221 -      assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bv.iDb+1 );
 1.10222 -      resetSchemaOnFault = u.bv.iDb+1;
 1.10223 +      assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bw.iDb+1 );
 1.10224 +      resetSchemaOnFault = u.bw.iDb+1;
 1.10225      }
 1.10226  #endif
 1.10227    }
 1.10228 @@ -70579,21 +68815,21 @@
 1.10229  ** See also: Destroy
 1.10230  */
 1.10231  case OP_Clear: {
 1.10232 -#if 0  /* local variables moved into u.bw */
 1.10233 +#if 0  /* local variables moved into u.bx */
 1.10234    int nChange;
 1.10235 -#endif /* local variables moved into u.bw */
 1.10236 -
 1.10237 -  u.bw.nChange = 0;
 1.10238 +#endif /* local variables moved into u.bx */
 1.10239 +
 1.10240 +  u.bx.nChange = 0;
 1.10241    assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
 1.10242    rc = sqlite3BtreeClearTable(
 1.10243 -      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bw.nChange : 0)
 1.10244 +      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bx.nChange : 0)
 1.10245    );
 1.10246    if( pOp->p3 ){
 1.10247 -    p->nChange += u.bw.nChange;
 1.10248 +    p->nChange += u.bx.nChange;
 1.10249      if( pOp->p3>0 ){
 1.10250        assert( memIsValid(&aMem[pOp->p3]) );
 1.10251        memAboutToChange(p, &aMem[pOp->p3]);
 1.10252 -      aMem[pOp->p3].u.i += u.bw.nChange;
 1.10253 +      aMem[pOp->p3].u.i += u.bx.nChange;
 1.10254      }
 1.10255    }
 1.10256    break;
 1.10257 @@ -70623,25 +68859,25 @@
 1.10258  */
 1.10259  case OP_CreateIndex:            /* out2-prerelease */
 1.10260  case OP_CreateTable: {          /* out2-prerelease */
 1.10261 -#if 0  /* local variables moved into u.bx */
 1.10262 +#if 0  /* local variables moved into u.by */
 1.10263    int pgno;
 1.10264    int flags;
 1.10265    Db *pDb;
 1.10266 -#endif /* local variables moved into u.bx */
 1.10267 -
 1.10268 -  u.bx.pgno = 0;
 1.10269 +#endif /* local variables moved into u.by */
 1.10270 +
 1.10271 +  u.by.pgno = 0;
 1.10272    assert( pOp->p1>=0 && pOp->p1<db->nDb );
 1.10273    assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 1.10274 -  u.bx.pDb = &db->aDb[pOp->p1];
 1.10275 -  assert( u.bx.pDb->pBt!=0 );
 1.10276 +  u.by.pDb = &db->aDb[pOp->p1];
 1.10277 +  assert( u.by.pDb->pBt!=0 );
 1.10278    if( pOp->opcode==OP_CreateTable ){
 1.10279 -    /* u.bx.flags = BTREE_INTKEY; */
 1.10280 -    u.bx.flags = BTREE_INTKEY;
 1.10281 -  }else{
 1.10282 -    u.bx.flags = BTREE_BLOBKEY;
 1.10283 -  }
 1.10284 -  rc = sqlite3BtreeCreateTable(u.bx.pDb->pBt, &u.bx.pgno, u.bx.flags);
 1.10285 -  pOut->u.i = u.bx.pgno;
 1.10286 +    /* u.by.flags = BTREE_INTKEY; */
 1.10287 +    u.by.flags = BTREE_INTKEY;
 1.10288 +  }else{
 1.10289 +    u.by.flags = BTREE_BLOBKEY;
 1.10290 +  }
 1.10291 +  rc = sqlite3BtreeCreateTable(u.by.pDb->pBt, &u.by.pgno, u.by.flags);
 1.10292 +  pOut->u.i = u.by.pgno;
 1.10293    break;
 1.10294  }
 1.10295  
 1.10296 @@ -70654,44 +68890,44 @@
 1.10297  ** then runs the new virtual machine.  It is thus a re-entrant opcode.
 1.10298  */
 1.10299  case OP_ParseSchema: {
 1.10300 -#if 0  /* local variables moved into u.by */
 1.10301 +#if 0  /* local variables moved into u.bz */
 1.10302    int iDb;
 1.10303    const char *zMaster;
 1.10304    char *zSql;
 1.10305    InitData initData;
 1.10306 -#endif /* local variables moved into u.by */
 1.10307 +#endif /* local variables moved into u.bz */
 1.10308  
 1.10309    /* Any prepared statement that invokes this opcode will hold mutexes
 1.10310    ** on every btree.  This is a prerequisite for invoking
 1.10311    ** sqlite3InitCallback().
 1.10312    */
 1.10313  #ifdef SQLITE_DEBUG
 1.10314 -  for(u.by.iDb=0; u.by.iDb<db->nDb; u.by.iDb++){
 1.10315 -    assert( u.by.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.by.iDb].pBt) );
 1.10316 -  }
 1.10317 -#endif
 1.10318 -
 1.10319 -  u.by.iDb = pOp->p1;
 1.10320 -  assert( u.by.iDb>=0 && u.by.iDb<db->nDb );
 1.10321 -  assert( DbHasProperty(db, u.by.iDb, DB_SchemaLoaded) );
 1.10322 +  for(u.bz.iDb=0; u.bz.iDb<db->nDb; u.bz.iDb++){
 1.10323 +    assert( u.bz.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bz.iDb].pBt) );
 1.10324 +  }
 1.10325 +#endif
 1.10326 +
 1.10327 +  u.bz.iDb = pOp->p1;
 1.10328 +  assert( u.bz.iDb>=0 && u.bz.iDb<db->nDb );
 1.10329 +  assert( DbHasProperty(db, u.bz.iDb, DB_SchemaLoaded) );
 1.10330    /* Used to be a conditional */ {
 1.10331 -    u.by.zMaster = SCHEMA_TABLE(u.by.iDb);
 1.10332 -    u.by.initData.db = db;
 1.10333 -    u.by.initData.iDb = pOp->p1;
 1.10334 -    u.by.initData.pzErrMsg = &p->zErrMsg;
 1.10335 -    u.by.zSql = sqlite3MPrintf(db,
 1.10336 +    u.bz.zMaster = SCHEMA_TABLE(u.bz.iDb);
 1.10337 +    u.bz.initData.db = db;
 1.10338 +    u.bz.initData.iDb = pOp->p1;
 1.10339 +    u.bz.initData.pzErrMsg = &p->zErrMsg;
 1.10340 +    u.bz.zSql = sqlite3MPrintf(db,
 1.10341         "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
 1.10342 -       db->aDb[u.by.iDb].zName, u.by.zMaster, pOp->p4.z);
 1.10343 -    if( u.by.zSql==0 ){
 1.10344 +       db->aDb[u.bz.iDb].zName, u.bz.zMaster, pOp->p4.z);
 1.10345 +    if( u.bz.zSql==0 ){
 1.10346        rc = SQLITE_NOMEM;
 1.10347      }else{
 1.10348        assert( db->init.busy==0 );
 1.10349        db->init.busy = 1;
 1.10350 -      u.by.initData.rc = SQLITE_OK;
 1.10351 +      u.bz.initData.rc = SQLITE_OK;
 1.10352        assert( !db->mallocFailed );
 1.10353 -      rc = sqlite3_exec(db, u.by.zSql, sqlite3InitCallback, &u.by.initData, 0);
 1.10354 -      if( rc==SQLITE_OK ) rc = u.by.initData.rc;
 1.10355 -      sqlite3DbFree(db, u.by.zSql);
 1.10356 +      rc = sqlite3_exec(db, u.bz.zSql, sqlite3InitCallback, &u.bz.initData, 0);
 1.10357 +      if( rc==SQLITE_OK ) rc = u.bz.initData.rc;
 1.10358 +      sqlite3DbFree(db, u.bz.zSql);
 1.10359        db->init.busy = 0;
 1.10360      }
 1.10361    }
 1.10362 @@ -70775,41 +69011,41 @@
 1.10363  ** This opcode is used to implement the integrity_check pragma.
 1.10364  */
 1.10365  case OP_IntegrityCk: {
 1.10366 -#if 0  /* local variables moved into u.bz */
 1.10367 +#if 0  /* local variables moved into u.ca */
 1.10368    int nRoot;      /* Number of tables to check.  (Number of root pages.) */
 1.10369    int *aRoot;     /* Array of rootpage numbers for tables to be checked */
 1.10370    int j;          /* Loop counter */
 1.10371    int nErr;       /* Number of errors reported */
 1.10372    char *z;        /* Text of the error report */
 1.10373    Mem *pnErr;     /* Register keeping track of errors remaining */
 1.10374 -#endif /* local variables moved into u.bz */
 1.10375 -
 1.10376 -  u.bz.nRoot = pOp->p2;
 1.10377 -  assert( u.bz.nRoot>0 );
 1.10378 -  u.bz.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bz.nRoot+1) );
 1.10379 -  if( u.bz.aRoot==0 ) goto no_mem;
 1.10380 +#endif /* local variables moved into u.ca */
 1.10381 +
 1.10382 +  u.ca.nRoot = pOp->p2;
 1.10383 +  assert( u.ca.nRoot>0 );
 1.10384 +  u.ca.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.ca.nRoot+1) );
 1.10385 +  if( u.ca.aRoot==0 ) goto no_mem;
 1.10386    assert( pOp->p3>0 && pOp->p3<=p->nMem );
 1.10387 -  u.bz.pnErr = &aMem[pOp->p3];
 1.10388 -  assert( (u.bz.pnErr->flags & MEM_Int)!=0 );
 1.10389 -  assert( (u.bz.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
 1.10390 +  u.ca.pnErr = &aMem[pOp->p3];
 1.10391 +  assert( (u.ca.pnErr->flags & MEM_Int)!=0 );
 1.10392 +  assert( (u.ca.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
 1.10393    pIn1 = &aMem[pOp->p1];
 1.10394 -  for(u.bz.j=0; u.bz.j<u.bz.nRoot; u.bz.j++){
 1.10395 -    u.bz.aRoot[u.bz.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bz.j]);
 1.10396 -  }
 1.10397 -  u.bz.aRoot[u.bz.j] = 0;
 1.10398 +  for(u.ca.j=0; u.ca.j<u.ca.nRoot; u.ca.j++){
 1.10399 +    u.ca.aRoot[u.ca.j] = (int)sqlite3VdbeIntValue(&pIn1[u.ca.j]);
 1.10400 +  }
 1.10401 +  u.ca.aRoot[u.ca.j] = 0;
 1.10402    assert( pOp->p5<db->nDb );
 1.10403    assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
 1.10404 -  u.bz.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bz.aRoot, u.bz.nRoot,
 1.10405 -                                 (int)u.bz.pnErr->u.i, &u.bz.nErr);
 1.10406 -  sqlite3DbFree(db, u.bz.aRoot);
 1.10407 -  u.bz.pnErr->u.i -= u.bz.nErr;
 1.10408 +  u.ca.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.ca.aRoot, u.ca.nRoot,
 1.10409 +                                 (int)u.ca.pnErr->u.i, &u.ca.nErr);
 1.10410 +  sqlite3DbFree(db, u.ca.aRoot);
 1.10411 +  u.ca.pnErr->u.i -= u.ca.nErr;
 1.10412    sqlite3VdbeMemSetNull(pIn1);
 1.10413 -  if( u.bz.nErr==0 ){
 1.10414 -    assert( u.bz.z==0 );
 1.10415 -  }else if( u.bz.z==0 ){
 1.10416 +  if( u.ca.nErr==0 ){
 1.10417 +    assert( u.ca.z==0 );
 1.10418 +  }else if( u.ca.z==0 ){
 1.10419      goto no_mem;
 1.10420    }else{
 1.10421 -    sqlite3VdbeMemSetStr(pIn1, u.bz.z, -1, SQLITE_UTF8, sqlite3_free);
 1.10422 +    sqlite3VdbeMemSetStr(pIn1, u.ca.z, -1, SQLITE_UTF8, sqlite3_free);
 1.10423    }
 1.10424    UPDATE_MAX_BLOBSIZE(pIn1);
 1.10425    sqlite3VdbeChangeEncoding(pIn1, encoding);
 1.10426 @@ -70843,20 +69079,20 @@
 1.10427  ** unchanged and jump to instruction P2.
 1.10428  */
 1.10429  case OP_RowSetRead: {       /* jump, in1, out3 */
 1.10430 -#if 0  /* local variables moved into u.ca */
 1.10431 +#if 0  /* local variables moved into u.cb */
 1.10432    i64 val;
 1.10433 -#endif /* local variables moved into u.ca */
 1.10434 +#endif /* local variables moved into u.cb */
 1.10435    CHECK_FOR_INTERRUPT;
 1.10436    pIn1 = &aMem[pOp->p1];
 1.10437    if( (pIn1->flags & MEM_RowSet)==0
 1.10438 -   || sqlite3RowSetNext(pIn1->u.pRowSet, &u.ca.val)==0
 1.10439 +   || sqlite3RowSetNext(pIn1->u.pRowSet, &u.cb.val)==0
 1.10440    ){
 1.10441      /* The boolean index is empty */
 1.10442      sqlite3VdbeMemSetNull(pIn1);
 1.10443      pc = pOp->p2 - 1;
 1.10444    }else{
 1.10445      /* A value was pulled from the index */
 1.10446 -    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.ca.val);
 1.10447 +    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.cb.val);
 1.10448    }
 1.10449    break;
 1.10450  }
 1.10451 @@ -70885,14 +69121,14 @@
 1.10452  ** inserted as part of some other set).
 1.10453  */
 1.10454  case OP_RowSetTest: {                     /* jump, in1, in3 */
 1.10455 -#if 0  /* local variables moved into u.cb */
 1.10456 +#if 0  /* local variables moved into u.cc */
 1.10457    int iSet;
 1.10458    int exists;
 1.10459 -#endif /* local variables moved into u.cb */
 1.10460 +#endif /* local variables moved into u.cc */
 1.10461  
 1.10462    pIn1 = &aMem[pOp->p1];
 1.10463    pIn3 = &aMem[pOp->p3];
 1.10464 -  u.cb.iSet = pOp->p4.i;
 1.10465 +  u.cc.iSet = pOp->p4.i;
 1.10466    assert( pIn3->flags&MEM_Int );
 1.10467  
 1.10468    /* If there is anything other than a rowset object in memory cell P1,
 1.10469 @@ -70904,17 +69140,17 @@
 1.10470    }
 1.10471  
 1.10472    assert( pOp->p4type==P4_INT32 );
 1.10473 -  assert( u.cb.iSet==-1 || u.cb.iSet>=0 );
 1.10474 -  if( u.cb.iSet ){
 1.10475 -    u.cb.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
 1.10476 -                               (u8)(u.cb.iSet>=0 ? u.cb.iSet & 0xf : 0xff),
 1.10477 +  assert( u.cc.iSet==-1 || u.cc.iSet>=0 );
 1.10478 +  if( u.cc.iSet ){
 1.10479 +    u.cc.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
 1.10480 +                               (u8)(u.cc.iSet>=0 ? u.cc.iSet & 0xf : 0xff),
 1.10481                                 pIn3->u.i);
 1.10482 -    if( u.cb.exists ){
 1.10483 +    if( u.cc.exists ){
 1.10484        pc = pOp->p2 - 1;
 1.10485        break;
 1.10486      }
 1.10487    }
 1.10488 -  if( u.cb.iSet>=0 ){
 1.10489 +  if( u.cc.iSet>=0 ){
 1.10490      sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
 1.10491    }
 1.10492    break;
 1.10493 @@ -70937,7 +69173,7 @@
 1.10494  ** P4 is a pointer to the VM containing the trigger program.
 1.10495  */
 1.10496  case OP_Program: {        /* jump */
 1.10497 -#if 0  /* local variables moved into u.cc */
 1.10498 +#if 0  /* local variables moved into u.cd */
 1.10499    int nMem;               /* Number of memory registers for sub-program */
 1.10500    int nByte;              /* Bytes of runtime space required for sub-program */
 1.10501    Mem *pRt;               /* Register to allocate runtime space */
 1.10502 @@ -70946,11 +69182,11 @@
 1.10503    VdbeFrame *pFrame;      /* New vdbe frame to execute in */
 1.10504    SubProgram *pProgram;   /* Sub-program to execute */
 1.10505    void *t;                /* Token identifying trigger */
 1.10506 -#endif /* local variables moved into u.cc */
 1.10507 -
 1.10508 -  u.cc.pProgram = pOp->p4.pProgram;
 1.10509 -  u.cc.pRt = &aMem[pOp->p3];
 1.10510 -  assert( u.cc.pProgram->nOp>0 );
 1.10511 +#endif /* local variables moved into u.cd */
 1.10512 +
 1.10513 +  u.cd.pProgram = pOp->p4.pProgram;
 1.10514 +  u.cd.pRt = &aMem[pOp->p3];
 1.10515 +  assert( u.cd.pProgram->nOp>0 );
 1.10516  
 1.10517    /* If the p5 flag is clear, then recursive invocation of triggers is
 1.10518    ** disabled for backwards compatibility (p5 is set if this sub-program
 1.10519 @@ -70964,9 +69200,9 @@
 1.10520    ** single trigger all have the same value for the SubProgram.token
 1.10521    ** variable.  */
 1.10522    if( pOp->p5 ){
 1.10523 -    u.cc.t = u.cc.pProgram->token;
 1.10524 -    for(u.cc.pFrame=p->pFrame; u.cc.pFrame && u.cc.pFrame->token!=u.cc.t; u.cc.pFrame=u.cc.pFrame->pParent);
 1.10525 -    if( u.cc.pFrame ) break;
 1.10526 +    u.cd.t = u.cd.pProgram->token;
 1.10527 +    for(u.cd.pFrame=p->pFrame; u.cd.pFrame && u.cd.pFrame->token!=u.cd.t; u.cd.pFrame=u.cd.pFrame->pParent);
 1.10528 +    if( u.cd.pFrame ) break;
 1.10529    }
 1.10530  
 1.10531    if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
 1.10532 @@ -70975,69 +69211,69 @@
 1.10533      break;
 1.10534    }
 1.10535  
 1.10536 -  /* Register u.cc.pRt is used to store the memory required to save the state
 1.10537 +  /* Register u.cd.pRt is used to store the memory required to save the state
 1.10538    ** of the current program, and the memory required at runtime to execute
 1.10539 -  ** the trigger program. If this trigger has been fired before, then u.cc.pRt
 1.10540 +  ** the trigger program. If this trigger has been fired before, then u.cd.pRt
 1.10541    ** is already allocated. Otherwise, it must be initialized.  */
 1.10542 -  if( (u.cc.pRt->flags&MEM_Frame)==0 ){
 1.10543 +  if( (u.cd.pRt->flags&MEM_Frame)==0 ){
 1.10544      /* SubProgram.nMem is set to the number of memory cells used by the
 1.10545      ** program stored in SubProgram.aOp. As well as these, one memory
 1.10546      ** cell is required for each cursor used by the program. Set local
 1.10547 -    ** variable u.cc.nMem (and later, VdbeFrame.nChildMem) to this value.
 1.10548 -    */
 1.10549 -    u.cc.nMem = u.cc.pProgram->nMem + u.cc.pProgram->nCsr;
 1.10550 -    u.cc.nByte = ROUND8(sizeof(VdbeFrame))
 1.10551 -              + u.cc.nMem * sizeof(Mem)
 1.10552 -              + u.cc.pProgram->nCsr * sizeof(VdbeCursor *)
 1.10553 -              + u.cc.pProgram->nOnce * sizeof(u8);
 1.10554 -    u.cc.pFrame = sqlite3DbMallocZero(db, u.cc.nByte);
 1.10555 -    if( !u.cc.pFrame ){
 1.10556 +    ** variable u.cd.nMem (and later, VdbeFrame.nChildMem) to this value.
 1.10557 +    */
 1.10558 +    u.cd.nMem = u.cd.pProgram->nMem + u.cd.pProgram->nCsr;
 1.10559 +    u.cd.nByte = ROUND8(sizeof(VdbeFrame))
 1.10560 +              + u.cd.nMem * sizeof(Mem)
 1.10561 +              + u.cd.pProgram->nCsr * sizeof(VdbeCursor *)
 1.10562 +              + u.cd.pProgram->nOnce * sizeof(u8);
 1.10563 +    u.cd.pFrame = sqlite3DbMallocZero(db, u.cd.nByte);
 1.10564 +    if( !u.cd.pFrame ){
 1.10565        goto no_mem;
 1.10566      }
 1.10567 -    sqlite3VdbeMemRelease(u.cc.pRt);
 1.10568 -    u.cc.pRt->flags = MEM_Frame;
 1.10569 -    u.cc.pRt->u.pFrame = u.cc.pFrame;
 1.10570 -
 1.10571 -    u.cc.pFrame->v = p;
 1.10572 -    u.cc.pFrame->nChildMem = u.cc.nMem;
 1.10573 -    u.cc.pFrame->nChildCsr = u.cc.pProgram->nCsr;
 1.10574 -    u.cc.pFrame->pc = pc;
 1.10575 -    u.cc.pFrame->aMem = p->aMem;
 1.10576 -    u.cc.pFrame->nMem = p->nMem;
 1.10577 -    u.cc.pFrame->apCsr = p->apCsr;
 1.10578 -    u.cc.pFrame->nCursor = p->nCursor;
 1.10579 -    u.cc.pFrame->aOp = p->aOp;
 1.10580 -    u.cc.pFrame->nOp = p->nOp;
 1.10581 -    u.cc.pFrame->token = u.cc.pProgram->token;
 1.10582 -    u.cc.pFrame->aOnceFlag = p->aOnceFlag;
 1.10583 -    u.cc.pFrame->nOnceFlag = p->nOnceFlag;
 1.10584 -
 1.10585 -    u.cc.pEnd = &VdbeFrameMem(u.cc.pFrame)[u.cc.pFrame->nChildMem];
 1.10586 -    for(u.cc.pMem=VdbeFrameMem(u.cc.pFrame); u.cc.pMem!=u.cc.pEnd; u.cc.pMem++){
 1.10587 -      u.cc.pMem->flags = MEM_Invalid;
 1.10588 -      u.cc.pMem->db = db;
 1.10589 -    }
 1.10590 -  }else{
 1.10591 -    u.cc.pFrame = u.cc.pRt->u.pFrame;
 1.10592 -    assert( u.cc.pProgram->nMem+u.cc.pProgram->nCsr==u.cc.pFrame->nChildMem );
 1.10593 -    assert( u.cc.pProgram->nCsr==u.cc.pFrame->nChildCsr );
 1.10594 -    assert( pc==u.cc.pFrame->pc );
 1.10595 +    sqlite3VdbeMemRelease(u.cd.pRt);
 1.10596 +    u.cd.pRt->flags = MEM_Frame;
 1.10597 +    u.cd.pRt->u.pFrame = u.cd.pFrame;
 1.10598 +
 1.10599 +    u.cd.pFrame->v = p;
 1.10600 +    u.cd.pFrame->nChildMem = u.cd.nMem;
 1.10601 +    u.cd.pFrame->nChildCsr = u.cd.pProgram->nCsr;
 1.10602 +    u.cd.pFrame->pc = pc;
 1.10603 +    u.cd.pFrame->aMem = p->aMem;
 1.10604 +    u.cd.pFrame->nMem = p->nMem;
 1.10605 +    u.cd.pFrame->apCsr = p->apCsr;
 1.10606 +    u.cd.pFrame->nCursor = p->nCursor;
 1.10607 +    u.cd.pFrame->aOp = p->aOp;
 1.10608 +    u.cd.pFrame->nOp = p->nOp;
 1.10609 +    u.cd.pFrame->token = u.cd.pProgram->token;
 1.10610 +    u.cd.pFrame->aOnceFlag = p->aOnceFlag;
 1.10611 +    u.cd.pFrame->nOnceFlag = p->nOnceFlag;
 1.10612 +
 1.10613 +    u.cd.pEnd = &VdbeFrameMem(u.cd.pFrame)[u.cd.pFrame->nChildMem];
 1.10614 +    for(u.cd.pMem=VdbeFrameMem(u.cd.pFrame); u.cd.pMem!=u.cd.pEnd; u.cd.pMem++){
 1.10615 +      u.cd.pMem->flags = MEM_Invalid;
 1.10616 +      u.cd.pMem->db = db;
 1.10617 +    }
 1.10618 +  }else{
 1.10619 +    u.cd.pFrame = u.cd.pRt->u.pFrame;
 1.10620 +    assert( u.cd.pProgram->nMem+u.cd.pProgram->nCsr==u.cd.pFrame->nChildMem );
 1.10621 +    assert( u.cd.pProgram->nCsr==u.cd.pFrame->nChildCsr );
 1.10622 +    assert( pc==u.cd.pFrame->pc );
 1.10623    }
 1.10624  
 1.10625    p->nFrame++;
 1.10626 -  u.cc.pFrame->pParent = p->pFrame;
 1.10627 -  u.cc.pFrame->lastRowid = lastRowid;
 1.10628 -  u.cc.pFrame->nChange = p->nChange;
 1.10629 +  u.cd.pFrame->pParent = p->pFrame;
 1.10630 +  u.cd.pFrame->lastRowid = lastRowid;
 1.10631 +  u.cd.pFrame->nChange = p->nChange;
 1.10632    p->nChange = 0;
 1.10633 -  p->pFrame = u.cc.pFrame;
 1.10634 -  p->aMem = aMem = &VdbeFrameMem(u.cc.pFrame)[-1];
 1.10635 -  p->nMem = u.cc.pFrame->nChildMem;
 1.10636 -  p->nCursor = (u16)u.cc.pFrame->nChildCsr;
 1.10637 +  p->pFrame = u.cd.pFrame;
 1.10638 +  p->aMem = aMem = &VdbeFrameMem(u.cd.pFrame)[-1];
 1.10639 +  p->nMem = u.cd.pFrame->nChildMem;
 1.10640 +  p->nCursor = (u16)u.cd.pFrame->nChildCsr;
 1.10641    p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
 1.10642 -  p->aOp = aOp = u.cc.pProgram->aOp;
 1.10643 -  p->nOp = u.cc.pProgram->nOp;
 1.10644 +  p->aOp = aOp = u.cd.pProgram->aOp;
 1.10645 +  p->nOp = u.cd.pProgram->nOp;
 1.10646    p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
 1.10647 -  p->nOnceFlag = u.cc.pProgram->nOnce;
 1.10648 +  p->nOnceFlag = u.cd.pProgram->nOnce;
 1.10649    pc = -1;
 1.10650    memset(p->aOnceFlag, 0, p->nOnceFlag);
 1.10651  
 1.10652 @@ -71057,13 +69293,13 @@
 1.10653  ** calling OP_Program instruction.
 1.10654  */
 1.10655  case OP_Param: {           /* out2-prerelease */
 1.10656 -#if 0  /* local variables moved into u.cd */
 1.10657 +#if 0  /* local variables moved into u.ce */
 1.10658    VdbeFrame *pFrame;
 1.10659    Mem *pIn;
 1.10660 -#endif /* local variables moved into u.cd */
 1.10661 -  u.cd.pFrame = p->pFrame;
 1.10662 -  u.cd.pIn = &u.cd.pFrame->aMem[pOp->p1 + u.cd.pFrame->aOp[u.cd.pFrame->pc].p1];
 1.10663 -  sqlite3VdbeMemShallowCopy(pOut, u.cd.pIn, MEM_Ephem);
 1.10664 +#endif /* local variables moved into u.ce */
 1.10665 +  u.ce.pFrame = p->pFrame;
 1.10666 +  u.ce.pIn = &u.ce.pFrame->aMem[pOp->p1 + u.ce.pFrame->aOp[u.ce.pFrame->pc].p1];
 1.10667 +  sqlite3VdbeMemShallowCopy(pOut, u.ce.pIn, MEM_Ephem);
 1.10668    break;
 1.10669  }
 1.10670  
 1.10671 @@ -71119,22 +69355,22 @@
 1.10672  ** an integer.
 1.10673  */
 1.10674  case OP_MemMax: {        /* in2 */
 1.10675 -#if 0  /* local variables moved into u.ce */
 1.10676 +#if 0  /* local variables moved into u.cf */
 1.10677    Mem *pIn1;
 1.10678    VdbeFrame *pFrame;
 1.10679 -#endif /* local variables moved into u.ce */
 1.10680 +#endif /* local variables moved into u.cf */
 1.10681    if( p->pFrame ){
 1.10682 -    for(u.ce.pFrame=p->pFrame; u.ce.pFrame->pParent; u.ce.pFrame=u.ce.pFrame->pParent);
 1.10683 -    u.ce.pIn1 = &u.ce.pFrame->aMem[pOp->p1];
 1.10684 -  }else{
 1.10685 -    u.ce.pIn1 = &aMem[pOp->p1];
 1.10686 -  }
 1.10687 -  assert( memIsValid(u.ce.pIn1) );
 1.10688 -  sqlite3VdbeMemIntegerify(u.ce.pIn1);
 1.10689 +    for(u.cf.pFrame=p->pFrame; u.cf.pFrame->pParent; u.cf.pFrame=u.cf.pFrame->pParent);
 1.10690 +    u.cf.pIn1 = &u.cf.pFrame->aMem[pOp->p1];
 1.10691 +  }else{
 1.10692 +    u.cf.pIn1 = &aMem[pOp->p1];
 1.10693 +  }
 1.10694 +  assert( memIsValid(u.cf.pIn1) );
 1.10695 +  sqlite3VdbeMemIntegerify(u.cf.pIn1);
 1.10696    pIn2 = &aMem[pOp->p2];
 1.10697    sqlite3VdbeMemIntegerify(pIn2);
 1.10698 -  if( u.ce.pIn1->u.i<pIn2->u.i){
 1.10699 -    u.ce.pIn1->u.i = pIn2->u.i;
 1.10700 +  if( u.cf.pIn1->u.i<pIn2->u.i){
 1.10701 +    u.cf.pIn1->u.i = pIn2->u.i;
 1.10702    }
 1.10703    break;
 1.10704  }
 1.10705 @@ -71201,56 +69437,56 @@
 1.10706  ** successors.
 1.10707  */
 1.10708  case OP_AggStep: {
 1.10709 -#if 0  /* local variables moved into u.cf */
 1.10710 +#if 0  /* local variables moved into u.cg */
 1.10711    int n;
 1.10712    int i;
 1.10713    Mem *pMem;
 1.10714    Mem *pRec;
 1.10715    sqlite3_context ctx;
 1.10716    sqlite3_value **apVal;
 1.10717 -#endif /* local variables moved into u.cf */
 1.10718 -
 1.10719 -  u.cf.n = pOp->p5;
 1.10720 -  assert( u.cf.n>=0 );
 1.10721 -  u.cf.pRec = &aMem[pOp->p2];
 1.10722 -  u.cf.apVal = p->apArg;
 1.10723 -  assert( u.cf.apVal || u.cf.n==0 );
 1.10724 -  for(u.cf.i=0; u.cf.i<u.cf.n; u.cf.i++, u.cf.pRec++){
 1.10725 -    assert( memIsValid(u.cf.pRec) );
 1.10726 -    u.cf.apVal[u.cf.i] = u.cf.pRec;
 1.10727 -    memAboutToChange(p, u.cf.pRec);
 1.10728 -    sqlite3VdbeMemStoreType(u.cf.pRec);
 1.10729 -  }
 1.10730 -  u.cf.ctx.pFunc = pOp->p4.pFunc;
 1.10731 +#endif /* local variables moved into u.cg */
 1.10732 +
 1.10733 +  u.cg.n = pOp->p5;
 1.10734 +  assert( u.cg.n>=0 );
 1.10735 +  u.cg.pRec = &aMem[pOp->p2];
 1.10736 +  u.cg.apVal = p->apArg;
 1.10737 +  assert( u.cg.apVal || u.cg.n==0 );
 1.10738 +  for(u.cg.i=0; u.cg.i<u.cg.n; u.cg.i++, u.cg.pRec++){
 1.10739 +    assert( memIsValid(u.cg.pRec) );
 1.10740 +    u.cg.apVal[u.cg.i] = u.cg.pRec;
 1.10741 +    memAboutToChange(p, u.cg.pRec);
 1.10742 +    sqlite3VdbeMemStoreType(u.cg.pRec);
 1.10743 +  }
 1.10744 +  u.cg.ctx.pFunc = pOp->p4.pFunc;
 1.10745    assert( pOp->p3>0 && pOp->p3<=p->nMem );
 1.10746 -  u.cf.ctx.pMem = u.cf.pMem = &aMem[pOp->p3];
 1.10747 -  u.cf.pMem->n++;
 1.10748 -  u.cf.ctx.s.flags = MEM_Null;
 1.10749 -  u.cf.ctx.s.z = 0;
 1.10750 -  u.cf.ctx.s.zMalloc = 0;
 1.10751 -  u.cf.ctx.s.xDel = 0;
 1.10752 -  u.cf.ctx.s.db = db;
 1.10753 -  u.cf.ctx.isError = 0;
 1.10754 -  u.cf.ctx.pColl = 0;
 1.10755 -  u.cf.ctx.skipFlag = 0;
 1.10756 -  if( u.cf.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
 1.10757 +  u.cg.ctx.pMem = u.cg.pMem = &aMem[pOp->p3];
 1.10758 +  u.cg.pMem->n++;
 1.10759 +  u.cg.ctx.s.flags = MEM_Null;
 1.10760 +  u.cg.ctx.s.z = 0;
 1.10761 +  u.cg.ctx.s.zMalloc = 0;
 1.10762 +  u.cg.ctx.s.xDel = 0;
 1.10763 +  u.cg.ctx.s.db = db;
 1.10764 +  u.cg.ctx.isError = 0;
 1.10765 +  u.cg.ctx.pColl = 0;
 1.10766 +  u.cg.ctx.skipFlag = 0;
 1.10767 +  if( u.cg.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
 1.10768      assert( pOp>p->aOp );
 1.10769      assert( pOp[-1].p4type==P4_COLLSEQ );
 1.10770      assert( pOp[-1].opcode==OP_CollSeq );
 1.10771 -    u.cf.ctx.pColl = pOp[-1].p4.pColl;
 1.10772 -  }
 1.10773 -  (u.cf.ctx.pFunc->xStep)(&u.cf.ctx, u.cf.n, u.cf.apVal); /* IMP: R-24505-23230 */
 1.10774 -  if( u.cf.ctx.isError ){
 1.10775 -    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cf.ctx.s));
 1.10776 -    rc = u.cf.ctx.isError;
 1.10777 -  }
 1.10778 -  if( u.cf.ctx.skipFlag ){
 1.10779 +    u.cg.ctx.pColl = pOp[-1].p4.pColl;
 1.10780 +  }
 1.10781 +  (u.cg.ctx.pFunc->xStep)(&u.cg.ctx, u.cg.n, u.cg.apVal); /* IMP: R-24505-23230 */
 1.10782 +  if( u.cg.ctx.isError ){
 1.10783 +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cg.ctx.s));
 1.10784 +    rc = u.cg.ctx.isError;
 1.10785 +  }
 1.10786 +  if( u.cg.ctx.skipFlag ){
 1.10787      assert( pOp[-1].opcode==OP_CollSeq );
 1.10788 -    u.cf.i = pOp[-1].p1;
 1.10789 -    if( u.cf.i ) sqlite3VdbeMemSetInt64(&aMem[u.cf.i], 1);
 1.10790 -  }
 1.10791 -
 1.10792 -  sqlite3VdbeMemRelease(&u.cf.ctx.s);
 1.10793 +    u.cg.i = pOp[-1].p1;
 1.10794 +    if( u.cg.i ) sqlite3VdbeMemSetInt64(&aMem[u.cg.i], 1);
 1.10795 +  }
 1.10796 +
 1.10797 +  sqlite3VdbeMemRelease(&u.cg.ctx.s);
 1.10798  
 1.10799    break;
 1.10800  }
 1.10801 @@ -71268,19 +69504,19 @@
 1.10802  ** the step function was not previously called.
 1.10803  */
 1.10804  case OP_AggFinal: {
 1.10805 -#if 0  /* local variables moved into u.cg */
 1.10806 +#if 0  /* local variables moved into u.ch */
 1.10807    Mem *pMem;
 1.10808 -#endif /* local variables moved into u.cg */
 1.10809 +#endif /* local variables moved into u.ch */
 1.10810    assert( pOp->p1>0 && pOp->p1<=p->nMem );
 1.10811 -  u.cg.pMem = &aMem[pOp->p1];
 1.10812 -  assert( (u.cg.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
 1.10813 -  rc = sqlite3VdbeMemFinalize(u.cg.pMem, pOp->p4.pFunc);
 1.10814 +  u.ch.pMem = &aMem[pOp->p1];
 1.10815 +  assert( (u.ch.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
 1.10816 +  rc = sqlite3VdbeMemFinalize(u.ch.pMem, pOp->p4.pFunc);
 1.10817    if( rc ){
 1.10818 -    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cg.pMem));
 1.10819 -  }
 1.10820 -  sqlite3VdbeChangeEncoding(u.cg.pMem, encoding);
 1.10821 -  UPDATE_MAX_BLOBSIZE(u.cg.pMem);
 1.10822 -  if( sqlite3VdbeMemTooBig(u.cg.pMem) ){
 1.10823 +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.ch.pMem));
 1.10824 +  }
 1.10825 +  sqlite3VdbeChangeEncoding(u.ch.pMem, encoding);
 1.10826 +  UPDATE_MAX_BLOBSIZE(u.ch.pMem);
 1.10827 +  if( sqlite3VdbeMemTooBig(u.ch.pMem) ){
 1.10828      goto too_big;
 1.10829    }
 1.10830    break;
 1.10831 @@ -71299,25 +69535,25 @@
 1.10832  ** mem[P3+2] are initialized to -1.
 1.10833  */
 1.10834  case OP_Checkpoint: {
 1.10835 -#if 0  /* local variables moved into u.ch */
 1.10836 +#if 0  /* local variables moved into u.ci */
 1.10837    int i;                          /* Loop counter */
 1.10838    int aRes[3];                    /* Results */
 1.10839    Mem *pMem;                      /* Write results here */
 1.10840 -#endif /* local variables moved into u.ch */
 1.10841 -
 1.10842 -  u.ch.aRes[0] = 0;
 1.10843 -  u.ch.aRes[1] = u.ch.aRes[2] = -1;
 1.10844 +#endif /* local variables moved into u.ci */
 1.10845 +
 1.10846 +  u.ci.aRes[0] = 0;
 1.10847 +  u.ci.aRes[1] = u.ci.aRes[2] = -1;
 1.10848    assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
 1.10849         || pOp->p2==SQLITE_CHECKPOINT_FULL
 1.10850         || pOp->p2==SQLITE_CHECKPOINT_RESTART
 1.10851    );
 1.10852 -  rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ch.aRes[1], &u.ch.aRes[2]);
 1.10853 +  rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ci.aRes[1], &u.ci.aRes[2]);
 1.10854    if( rc==SQLITE_BUSY ){
 1.10855      rc = SQLITE_OK;
 1.10856 -    u.ch.aRes[0] = 1;
 1.10857 -  }
 1.10858 -  for(u.ch.i=0, u.ch.pMem = &aMem[pOp->p3]; u.ch.i<3; u.ch.i++, u.ch.pMem++){
 1.10859 -    sqlite3VdbeMemSetInt64(u.ch.pMem, (i64)u.ch.aRes[u.ch.i]);
 1.10860 +    u.ci.aRes[0] = 1;
 1.10861 +  }
 1.10862 +  for(u.ci.i=0, u.ci.pMem = &aMem[pOp->p3]; u.ci.i<3; u.ci.i++, u.ci.pMem++){
 1.10863 +    sqlite3VdbeMemSetInt64(u.ci.pMem, (i64)u.ci.aRes[u.ci.i]);
 1.10864    }
 1.10865    break;
 1.10866  };  
 1.10867 @@ -71336,91 +69572,93 @@
 1.10868  ** Write a string containing the final journal-mode to register P2.
 1.10869  */
 1.10870  case OP_JournalMode: {    /* out2-prerelease */
 1.10871 -#if 0  /* local variables moved into u.ci */
 1.10872 +#if 0  /* local variables moved into u.cj */
 1.10873    Btree *pBt;                     /* Btree to change journal mode of */
 1.10874    Pager *pPager;                  /* Pager associated with pBt */
 1.10875    int eNew;                       /* New journal mode */
 1.10876    int eOld;                       /* The old journal mode */
 1.10877 +#ifndef SQLITE_OMIT_WAL
 1.10878    const char *zFilename;          /* Name of database file for pPager */
 1.10879 -#endif /* local variables moved into u.ci */
 1.10880 -
 1.10881 -  u.ci.eNew = pOp->p3;
 1.10882 -  assert( u.ci.eNew==PAGER_JOURNALMODE_DELETE
 1.10883 -       || u.ci.eNew==PAGER_JOURNALMODE_TRUNCATE
 1.10884 -       || u.ci.eNew==PAGER_JOURNALMODE_PERSIST
 1.10885 -       || u.ci.eNew==PAGER_JOURNALMODE_OFF
 1.10886 -       || u.ci.eNew==PAGER_JOURNALMODE_MEMORY
 1.10887 -       || u.ci.eNew==PAGER_JOURNALMODE_WAL
 1.10888 -       || u.ci.eNew==PAGER_JOURNALMODE_QUERY
 1.10889 +#endif
 1.10890 +#endif /* local variables moved into u.cj */
 1.10891 +
 1.10892 +  u.cj.eNew = pOp->p3;
 1.10893 +  assert( u.cj.eNew==PAGER_JOURNALMODE_DELETE
 1.10894 +       || u.cj.eNew==PAGER_JOURNALMODE_TRUNCATE
 1.10895 +       || u.cj.eNew==PAGER_JOURNALMODE_PERSIST
 1.10896 +       || u.cj.eNew==PAGER_JOURNALMODE_OFF
 1.10897 +       || u.cj.eNew==PAGER_JOURNALMODE_MEMORY
 1.10898 +       || u.cj.eNew==PAGER_JOURNALMODE_WAL
 1.10899 +       || u.cj.eNew==PAGER_JOURNALMODE_QUERY
 1.10900    );
 1.10901    assert( pOp->p1>=0 && pOp->p1<db->nDb );
 1.10902  
 1.10903 -  u.ci.pBt = db->aDb[pOp->p1].pBt;
 1.10904 -  u.ci.pPager = sqlite3BtreePager(u.ci.pBt);
 1.10905 -  u.ci.eOld = sqlite3PagerGetJournalMode(u.ci.pPager);
 1.10906 -  if( u.ci.eNew==PAGER_JOURNALMODE_QUERY ) u.ci.eNew = u.ci.eOld;
 1.10907 -  if( !sqlite3PagerOkToChangeJournalMode(u.ci.pPager) ) u.ci.eNew = u.ci.eOld;
 1.10908 +  u.cj.pBt = db->aDb[pOp->p1].pBt;
 1.10909 +  u.cj.pPager = sqlite3BtreePager(u.cj.pBt);
 1.10910 +  u.cj.eOld = sqlite3PagerGetJournalMode(u.cj.pPager);
 1.10911 +  if( u.cj.eNew==PAGER_JOURNALMODE_QUERY ) u.cj.eNew = u.cj.eOld;
 1.10912 +  if( !sqlite3PagerOkToChangeJournalMode(u.cj.pPager) ) u.cj.eNew = u.cj.eOld;
 1.10913  
 1.10914  #ifndef SQLITE_OMIT_WAL
 1.10915 -  u.ci.zFilename = sqlite3PagerFilename(u.ci.pPager, 1);
 1.10916 +  u.cj.zFilename = sqlite3PagerFilename(u.cj.pPager, 1);
 1.10917  
 1.10918    /* Do not allow a transition to journal_mode=WAL for a database
 1.10919    ** in temporary storage or if the VFS does not support shared memory
 1.10920    */
 1.10921 -  if( u.ci.eNew==PAGER_JOURNALMODE_WAL
 1.10922 -   && (sqlite3Strlen30(u.ci.zFilename)==0           /* Temp file */
 1.10923 -       || !sqlite3PagerWalSupported(u.ci.pPager))   /* No shared-memory support */
 1.10924 +  if( u.cj.eNew==PAGER_JOURNALMODE_WAL
 1.10925 +   && (sqlite3Strlen30(u.cj.zFilename)==0           /* Temp file */
 1.10926 +       || !sqlite3PagerWalSupported(u.cj.pPager))   /* No shared-memory support */
 1.10927    ){
 1.10928 -    u.ci.eNew = u.ci.eOld;
 1.10929 -  }
 1.10930 -
 1.10931 -  if( (u.ci.eNew!=u.ci.eOld)
 1.10932 -   && (u.ci.eOld==PAGER_JOURNALMODE_WAL || u.ci.eNew==PAGER_JOURNALMODE_WAL)
 1.10933 +    u.cj.eNew = u.cj.eOld;
 1.10934 +  }
 1.10935 +
 1.10936 +  if( (u.cj.eNew!=u.cj.eOld)
 1.10937 +   && (u.cj.eOld==PAGER_JOURNALMODE_WAL || u.cj.eNew==PAGER_JOURNALMODE_WAL)
 1.10938    ){
 1.10939      if( !db->autoCommit || db->activeVdbeCnt>1 ){
 1.10940        rc = SQLITE_ERROR;
 1.10941        sqlite3SetString(&p->zErrMsg, db,
 1.10942            "cannot change %s wal mode from within a transaction",
 1.10943 -          (u.ci.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
 1.10944 +          (u.cj.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
 1.10945        );
 1.10946        break;
 1.10947      }else{
 1.10948  
 1.10949 -      if( u.ci.eOld==PAGER_JOURNALMODE_WAL ){
 1.10950 +      if( u.cj.eOld==PAGER_JOURNALMODE_WAL ){
 1.10951          /* If leaving WAL mode, close the log file. If successful, the call
 1.10952          ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
 1.10953          ** file. An EXCLUSIVE lock may still be held on the database file
 1.10954          ** after a successful return.
 1.10955          */
 1.10956 -        rc = sqlite3PagerCloseWal(u.ci.pPager);
 1.10957 +        rc = sqlite3PagerCloseWal(u.cj.pPager);
 1.10958          if( rc==SQLITE_OK ){
 1.10959 -          sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
 1.10960 -        }
 1.10961 -      }else if( u.ci.eOld==PAGER_JOURNALMODE_MEMORY ){
 1.10962 +          sqlite3PagerSetJournalMode(u.cj.pPager, u.cj.eNew);
 1.10963 +        }
 1.10964 +      }else if( u.cj.eOld==PAGER_JOURNALMODE_MEMORY ){
 1.10965          /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
 1.10966          ** as an intermediate */
 1.10967 -        sqlite3PagerSetJournalMode(u.ci.pPager, PAGER_JOURNALMODE_OFF);
 1.10968 +        sqlite3PagerSetJournalMode(u.cj.pPager, PAGER_JOURNALMODE_OFF);
 1.10969        }
 1.10970  
 1.10971        /* Open a transaction on the database file. Regardless of the journal
 1.10972        ** mode, this transaction always uses a rollback journal.
 1.10973        */
 1.10974 -      assert( sqlite3BtreeIsInTrans(u.ci.pBt)==0 );
 1.10975 +      assert( sqlite3BtreeIsInTrans(u.cj.pBt)==0 );
 1.10976        if( rc==SQLITE_OK ){
 1.10977 -        rc = sqlite3BtreeSetVersion(u.ci.pBt, (u.ci.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
 1.10978 +        rc = sqlite3BtreeSetVersion(u.cj.pBt, (u.cj.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
 1.10979        }
 1.10980      }
 1.10981    }
 1.10982  #endif /* ifndef SQLITE_OMIT_WAL */
 1.10983  
 1.10984    if( rc ){
 1.10985 -    u.ci.eNew = u.ci.eOld;
 1.10986 -  }
 1.10987 -  u.ci.eNew = sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
 1.10988 +    u.cj.eNew = u.cj.eOld;
 1.10989 +  }
 1.10990 +  u.cj.eNew = sqlite3PagerSetJournalMode(u.cj.pPager, u.cj.eNew);
 1.10991  
 1.10992    pOut = &aMem[pOp->p2];
 1.10993    pOut->flags = MEM_Str|MEM_Static|MEM_Term;
 1.10994 -  pOut->z = (char *)sqlite3JournalModename(u.ci.eNew);
 1.10995 +  pOut->z = (char *)sqlite3JournalModename(u.cj.eNew);
 1.10996    pOut->n = sqlite3Strlen30(pOut->z);
 1.10997    pOut->enc = SQLITE_UTF8;
 1.10998    sqlite3VdbeChangeEncoding(pOut, encoding);
 1.10999 @@ -71449,14 +69687,14 @@
 1.11000  ** P2. Otherwise, fall through to the next instruction.
 1.11001  */
 1.11002  case OP_IncrVacuum: {        /* jump */
 1.11003 -#if 0  /* local variables moved into u.cj */
 1.11004 +#if 0  /* local variables moved into u.ck */
 1.11005    Btree *pBt;
 1.11006 -#endif /* local variables moved into u.cj */
 1.11007 +#endif /* local variables moved into u.ck */
 1.11008  
 1.11009    assert( pOp->p1>=0 && pOp->p1<db->nDb );
 1.11010    assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 1.11011 -  u.cj.pBt = db->aDb[pOp->p1].pBt;
 1.11012 -  rc = sqlite3BtreeIncrVacuum(u.cj.pBt);
 1.11013 +  u.ck.pBt = db->aDb[pOp->p1].pBt;
 1.11014 +  rc = sqlite3BtreeIncrVacuum(u.ck.pBt);
 1.11015    if( rc==SQLITE_DONE ){
 1.11016      pc = pOp->p2 - 1;
 1.11017      rc = SQLITE_OK;
 1.11018 @@ -71526,12 +69764,12 @@
 1.11019  ** code will be set to SQLITE_LOCKED.
 1.11020  */
 1.11021  case OP_VBegin: {
 1.11022 -#if 0  /* local variables moved into u.ck */
 1.11023 +#if 0  /* local variables moved into u.cl */
 1.11024    VTable *pVTab;
 1.11025 -#endif /* local variables moved into u.ck */
 1.11026 -  u.ck.pVTab = pOp->p4.pVtab;
 1.11027 -  rc = sqlite3VtabBegin(db, u.ck.pVTab);
 1.11028 -  if( u.ck.pVTab ) importVtabErrMsg(p, u.ck.pVTab->pVtab);
 1.11029 +#endif /* local variables moved into u.cl */
 1.11030 +  u.cl.pVTab = pOp->p4.pVtab;
 1.11031 +  rc = sqlite3VtabBegin(db, u.cl.pVTab);
 1.11032 +  if( u.cl.pVTab ) importVtabErrMsg(p, u.cl.pVTab->pVtab);
 1.11033    break;
 1.11034  }
 1.11035  #endif /* SQLITE_OMIT_VIRTUALTABLE */
 1.11036 @@ -71570,32 +69808,32 @@
 1.11037  ** table and stores that cursor in P1.
 1.11038  */
 1.11039  case OP_VOpen: {
 1.11040 -#if 0  /* local variables moved into u.cl */
 1.11041 +#if 0  /* local variables moved into u.cm */
 1.11042    VdbeCursor *pCur;
 1.11043    sqlite3_vtab_cursor *pVtabCursor;
 1.11044    sqlite3_vtab *pVtab;
 1.11045    sqlite3_module *pModule;
 1.11046 -#endif /* local variables moved into u.cl */
 1.11047 -
 1.11048 -  u.cl.pCur = 0;
 1.11049 -  u.cl.pVtabCursor = 0;
 1.11050 -  u.cl.pVtab = pOp->p4.pVtab->pVtab;
 1.11051 -  u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
 1.11052 -  assert(u.cl.pVtab && u.cl.pModule);
 1.11053 -  rc = u.cl.pModule->xOpen(u.cl.pVtab, &u.cl.pVtabCursor);
 1.11054 -  importVtabErrMsg(p, u.cl.pVtab);
 1.11055 +#endif /* local variables moved into u.cm */
 1.11056 +
 1.11057 +  u.cm.pCur = 0;
 1.11058 +  u.cm.pVtabCursor = 0;
 1.11059 +  u.cm.pVtab = pOp->p4.pVtab->pVtab;
 1.11060 +  u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
 1.11061 +  assert(u.cm.pVtab && u.cm.pModule);
 1.11062 +  rc = u.cm.pModule->xOpen(u.cm.pVtab, &u.cm.pVtabCursor);
 1.11063 +  importVtabErrMsg(p, u.cm.pVtab);
 1.11064    if( SQLITE_OK==rc ){
 1.11065      /* Initialize sqlite3_vtab_cursor base class */
 1.11066 -    u.cl.pVtabCursor->pVtab = u.cl.pVtab;
 1.11067 +    u.cm.pVtabCursor->pVtab = u.cm.pVtab;
 1.11068  
 1.11069      /* Initialise vdbe cursor object */
 1.11070 -    u.cl.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
 1.11071 -    if( u.cl.pCur ){
 1.11072 -      u.cl.pCur->pVtabCursor = u.cl.pVtabCursor;
 1.11073 -      u.cl.pCur->pModule = u.cl.pVtabCursor->pVtab->pModule;
 1.11074 +    u.cm.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
 1.11075 +    if( u.cm.pCur ){
 1.11076 +      u.cm.pCur->pVtabCursor = u.cm.pVtabCursor;
 1.11077 +      u.cm.pCur->pModule = u.cm.pVtabCursor->pVtab->pModule;
 1.11078      }else{
 1.11079        db->mallocFailed = 1;
 1.11080 -      u.cl.pModule->xClose(u.cl.pVtabCursor);
 1.11081 +      u.cm.pModule->xClose(u.cm.pVtabCursor);
 1.11082      }
 1.11083    }
 1.11084    break;
 1.11085 @@ -71622,7 +69860,7 @@
 1.11086  ** A jump is made to P2 if the result set after filtering would be empty.
 1.11087  */
 1.11088  case OP_VFilter: {   /* jump */
 1.11089 -#if 0  /* local variables moved into u.cm */
 1.11090 +#if 0  /* local variables moved into u.cn */
 1.11091    int nArg;
 1.11092    int iQuery;
 1.11093    const sqlite3_module *pModule;
 1.11094 @@ -71634,45 +69872,45 @@
 1.11095    int res;
 1.11096    int i;
 1.11097    Mem **apArg;
 1.11098 -#endif /* local variables moved into u.cm */
 1.11099 -
 1.11100 -  u.cm.pQuery = &aMem[pOp->p3];
 1.11101 -  u.cm.pArgc = &u.cm.pQuery[1];
 1.11102 -  u.cm.pCur = p->apCsr[pOp->p1];
 1.11103 -  assert( memIsValid(u.cm.pQuery) );
 1.11104 -  REGISTER_TRACE(pOp->p3, u.cm.pQuery);
 1.11105 -  assert( u.cm.pCur->pVtabCursor );
 1.11106 -  u.cm.pVtabCursor = u.cm.pCur->pVtabCursor;
 1.11107 -  u.cm.pVtab = u.cm.pVtabCursor->pVtab;
 1.11108 -  u.cm.pModule = u.cm.pVtab->pModule;
 1.11109 +#endif /* local variables moved into u.cn */
 1.11110 +
 1.11111 +  u.cn.pQuery = &aMem[pOp->p3];
 1.11112 +  u.cn.pArgc = &u.cn.pQuery[1];
 1.11113 +  u.cn.pCur = p->apCsr[pOp->p1];
 1.11114 +  assert( memIsValid(u.cn.pQuery) );
 1.11115 +  REGISTER_TRACE(pOp->p3, u.cn.pQuery);
 1.11116 +  assert( u.cn.pCur->pVtabCursor );
 1.11117 +  u.cn.pVtabCursor = u.cn.pCur->pVtabCursor;
 1.11118 +  u.cn.pVtab = u.cn.pVtabCursor->pVtab;
 1.11119 +  u.cn.pModule = u.cn.pVtab->pModule;
 1.11120  
 1.11121    /* Grab the index number and argc parameters */
 1.11122 -  assert( (u.cm.pQuery->flags&MEM_Int)!=0 && u.cm.pArgc->flags==MEM_Int );
 1.11123 -  u.cm.nArg = (int)u.cm.pArgc->u.i;
 1.11124 -  u.cm.iQuery = (int)u.cm.pQuery->u.i;
 1.11125 +  assert( (u.cn.pQuery->flags&MEM_Int)!=0 && u.cn.pArgc->flags==MEM_Int );
 1.11126 +  u.cn.nArg = (int)u.cn.pArgc->u.i;
 1.11127 +  u.cn.iQuery = (int)u.cn.pQuery->u.i;
 1.11128  
 1.11129    /* Invoke the xFilter method */
 1.11130    {
 1.11131 -    u.cm.res = 0;
 1.11132 -    u.cm.apArg = p->apArg;
 1.11133 -    for(u.cm.i = 0; u.cm.i<u.cm.nArg; u.cm.i++){
 1.11134 -      u.cm.apArg[u.cm.i] = &u.cm.pArgc[u.cm.i+1];
 1.11135 -      sqlite3VdbeMemStoreType(u.cm.apArg[u.cm.i]);
 1.11136 +    u.cn.res = 0;
 1.11137 +    u.cn.apArg = p->apArg;
 1.11138 +    for(u.cn.i = 0; u.cn.i<u.cn.nArg; u.cn.i++){
 1.11139 +      u.cn.apArg[u.cn.i] = &u.cn.pArgc[u.cn.i+1];
 1.11140 +      sqlite3VdbeMemStoreType(u.cn.apArg[u.cn.i]);
 1.11141      }
 1.11142  
 1.11143      p->inVtabMethod = 1;
 1.11144 -    rc = u.cm.pModule->xFilter(u.cm.pVtabCursor, u.cm.iQuery, pOp->p4.z, u.cm.nArg, u.cm.apArg);
 1.11145 +    rc = u.cn.pModule->xFilter(u.cn.pVtabCursor, u.cn.iQuery, pOp->p4.z, u.cn.nArg, u.cn.apArg);
 1.11146      p->inVtabMethod = 0;
 1.11147 -    importVtabErrMsg(p, u.cm.pVtab);
 1.11148 +    importVtabErrMsg(p, u.cn.pVtab);
 1.11149      if( rc==SQLITE_OK ){
 1.11150 -      u.cm.res = u.cm.pModule->xEof(u.cm.pVtabCursor);
 1.11151 -    }
 1.11152 -
 1.11153 -    if( u.cm.res ){
 1.11154 +      u.cn.res = u.cn.pModule->xEof(u.cn.pVtabCursor);
 1.11155 +    }
 1.11156 +
 1.11157 +    if( u.cn.res ){
 1.11158        pc = pOp->p2 - 1;
 1.11159      }
 1.11160    }
 1.11161 -  u.cm.pCur->nullRow = 0;
 1.11162 +  u.cn.pCur->nullRow = 0;
 1.11163  
 1.11164    break;
 1.11165  }
 1.11166 @@ -71686,51 +69924,51 @@
 1.11167  ** P1 cursor is pointing to into register P3.
 1.11168  */
 1.11169  case OP_VColumn: {
 1.11170 -#if 0  /* local variables moved into u.cn */
 1.11171 +#if 0  /* local variables moved into u.co */
 1.11172    sqlite3_vtab *pVtab;
 1.11173    const sqlite3_module *pModule;
 1.11174    Mem *pDest;
 1.11175    sqlite3_context sContext;
 1.11176 -#endif /* local variables moved into u.cn */
 1.11177 +#endif /* local variables moved into u.co */
 1.11178  
 1.11179    VdbeCursor *pCur = p->apCsr[pOp->p1];
 1.11180    assert( pCur->pVtabCursor );
 1.11181    assert( pOp->p3>0 && pOp->p3<=p->nMem );
 1.11182 -  u.cn.pDest = &aMem[pOp->p3];
 1.11183 -  memAboutToChange(p, u.cn.pDest);
 1.11184 +  u.co.pDest = &aMem[pOp->p3];
 1.11185 +  memAboutToChange(p, u.co.pDest);
 1.11186    if( pCur->nullRow ){
 1.11187 -    sqlite3VdbeMemSetNull(u.cn.pDest);
 1.11188 +    sqlite3VdbeMemSetNull(u.co.pDest);
 1.11189      break;
 1.11190    }
 1.11191 -  u.cn.pVtab = pCur->pVtabCursor->pVtab;
 1.11192 -  u.cn.pModule = u.cn.pVtab->pModule;
 1.11193 -  assert( u.cn.pModule->xColumn );
 1.11194 -  memset(&u.cn.sContext, 0, sizeof(u.cn.sContext));
 1.11195 +  u.co.pVtab = pCur->pVtabCursor->pVtab;
 1.11196 +  u.co.pModule = u.co.pVtab->pModule;
 1.11197 +  assert( u.co.pModule->xColumn );
 1.11198 +  memset(&u.co.sContext, 0, sizeof(u.co.sContext));
 1.11199  
 1.11200    /* The output cell may already have a buffer allocated. Move
 1.11201 -  ** the current contents to u.cn.sContext.s so in case the user-function
 1.11202 +  ** the current contents to u.co.sContext.s so in case the user-function
 1.11203    ** can use the already allocated buffer instead of allocating a
 1.11204    ** new one.
 1.11205    */
 1.11206 -  sqlite3VdbeMemMove(&u.cn.sContext.s, u.cn.pDest);
 1.11207 -  MemSetTypeFlag(&u.cn.sContext.s, MEM_Null);
 1.11208 -
 1.11209 -  rc = u.cn.pModule->xColumn(pCur->pVtabCursor, &u.cn.sContext, pOp->p2);
 1.11210 -  importVtabErrMsg(p, u.cn.pVtab);
 1.11211 -  if( u.cn.sContext.isError ){
 1.11212 -    rc = u.cn.sContext.isError;
 1.11213 +  sqlite3VdbeMemMove(&u.co.sContext.s, u.co.pDest);
 1.11214 +  MemSetTypeFlag(&u.co.sContext.s, MEM_Null);
 1.11215 +
 1.11216 +  rc = u.co.pModule->xColumn(pCur->pVtabCursor, &u.co.sContext, pOp->p2);
 1.11217 +  importVtabErrMsg(p, u.co.pVtab);
 1.11218 +  if( u.co.sContext.isError ){
 1.11219 +    rc = u.co.sContext.isError;
 1.11220    }
 1.11221  
 1.11222    /* Copy the result of the function to the P3 register. We
 1.11223    ** do this regardless of whether or not an error occurred to ensure any
 1.11224 -  ** dynamic allocation in u.cn.sContext.s (a Mem struct) is  released.
 1.11225 -  */
 1.11226 -  sqlite3VdbeChangeEncoding(&u.cn.sContext.s, encoding);
 1.11227 -  sqlite3VdbeMemMove(u.cn.pDest, &u.cn.sContext.s);
 1.11228 -  REGISTER_TRACE(pOp->p3, u.cn.pDest);
 1.11229 -  UPDATE_MAX_BLOBSIZE(u.cn.pDest);
 1.11230 -
 1.11231 -  if( sqlite3VdbeMemTooBig(u.cn.pDest) ){
 1.11232 +  ** dynamic allocation in u.co.sContext.s (a Mem struct) is  released.
 1.11233 +  */
 1.11234 +  sqlite3VdbeChangeEncoding(&u.co.sContext.s, encoding);
 1.11235 +  sqlite3VdbeMemMove(u.co.pDest, &u.co.sContext.s);
 1.11236 +  REGISTER_TRACE(pOp->p3, u.co.pDest);
 1.11237 +  UPDATE_MAX_BLOBSIZE(u.co.pDest);
 1.11238 +
 1.11239 +  if( sqlite3VdbeMemTooBig(u.co.pDest) ){
 1.11240      goto too_big;
 1.11241    }
 1.11242    break;
 1.11243 @@ -71745,22 +69983,22 @@
 1.11244  ** the end of its result set, then fall through to the next instruction.
 1.11245  */
 1.11246  case OP_VNext: {   /* jump */
 1.11247 -#if 0  /* local variables moved into u.co */
 1.11248 +#if 0  /* local variables moved into u.cp */
 1.11249    sqlite3_vtab *pVtab;
 1.11250    const sqlite3_module *pModule;
 1.11251    int res;
 1.11252    VdbeCursor *pCur;
 1.11253 -#endif /* local variables moved into u.co */
 1.11254 -
 1.11255 -  u.co.res = 0;
 1.11256 -  u.co.pCur = p->apCsr[pOp->p1];
 1.11257 -  assert( u.co.pCur->pVtabCursor );
 1.11258 -  if( u.co.pCur->nullRow ){
 1.11259 +#endif /* local variables moved into u.cp */
 1.11260 +
 1.11261 +  u.cp.res = 0;
 1.11262 +  u.cp.pCur = p->apCsr[pOp->p1];
 1.11263 +  assert( u.cp.pCur->pVtabCursor );
 1.11264 +  if( u.cp.pCur->nullRow ){
 1.11265      break;
 1.11266    }
 1.11267 -  u.co.pVtab = u.co.pCur->pVtabCursor->pVtab;
 1.11268 -  u.co.pModule = u.co.pVtab->pModule;
 1.11269 -  assert( u.co.pModule->xNext );
 1.11270 +  u.cp.pVtab = u.cp.pCur->pVtabCursor->pVtab;
 1.11271 +  u.cp.pModule = u.cp.pVtab->pModule;
 1.11272 +  assert( u.cp.pModule->xNext );
 1.11273  
 1.11274    /* Invoke the xNext() method of the module. There is no way for the
 1.11275    ** underlying implementation to return an error if one occurs during
 1.11276 @@ -71769,14 +70007,14 @@
 1.11277    ** some other method is next invoked on the save virtual table cursor.
 1.11278    */
 1.11279    p->inVtabMethod = 1;
 1.11280 -  rc = u.co.pModule->xNext(u.co.pCur->pVtabCursor);
 1.11281 +  rc = u.cp.pModule->xNext(u.cp.pCur->pVtabCursor);
 1.11282    p->inVtabMethod = 0;
 1.11283 -  importVtabErrMsg(p, u.co.pVtab);
 1.11284 +  importVtabErrMsg(p, u.cp.pVtab);
 1.11285    if( rc==SQLITE_OK ){
 1.11286 -    u.co.res = u.co.pModule->xEof(u.co.pCur->pVtabCursor);
 1.11287 -  }
 1.11288 -
 1.11289 -  if( !u.co.res ){
 1.11290 +    u.cp.res = u.cp.pModule->xEof(u.cp.pCur->pVtabCursor);
 1.11291 +  }
 1.11292 +
 1.11293 +  if( !u.cp.res ){
 1.11294      /* If there is data, jump to P2 */
 1.11295      pc = pOp->p2 - 1;
 1.11296    }
 1.11297 @@ -71792,24 +70030,24 @@
 1.11298  ** in register P1 is passed as the zName argument to the xRename method.
 1.11299  */
 1.11300  case OP_VRename: {
 1.11301 -#if 0  /* local variables moved into u.cp */
 1.11302 +#if 0  /* local variables moved into u.cq */
 1.11303    sqlite3_vtab *pVtab;
 1.11304    Mem *pName;
 1.11305 -#endif /* local variables moved into u.cp */
 1.11306 -
 1.11307 -  u.cp.pVtab = pOp->p4.pVtab->pVtab;
 1.11308 -  u.cp.pName = &aMem[pOp->p1];
 1.11309 -  assert( u.cp.pVtab->pModule->xRename );
 1.11310 -  assert( memIsValid(u.cp.pName) );
 1.11311 -  REGISTER_TRACE(pOp->p1, u.cp.pName);
 1.11312 -  assert( u.cp.pName->flags & MEM_Str );
 1.11313 -  testcase( u.cp.pName->enc==SQLITE_UTF8 );
 1.11314 -  testcase( u.cp.pName->enc==SQLITE_UTF16BE );
 1.11315 -  testcase( u.cp.pName->enc==SQLITE_UTF16LE );
 1.11316 -  rc = sqlite3VdbeChangeEncoding(u.cp.pName, SQLITE_UTF8);
 1.11317 +#endif /* local variables moved into u.cq */
 1.11318 +
 1.11319 +  u.cq.pVtab = pOp->p4.pVtab->pVtab;
 1.11320 +  u.cq.pName = &aMem[pOp->p1];
 1.11321 +  assert( u.cq.pVtab->pModule->xRename );
 1.11322 +  assert( memIsValid(u.cq.pName) );
 1.11323 +  REGISTER_TRACE(pOp->p1, u.cq.pName);
 1.11324 +  assert( u.cq.pName->flags & MEM_Str );
 1.11325 +  testcase( u.cq.pName->enc==SQLITE_UTF8 );
 1.11326 +  testcase( u.cq.pName->enc==SQLITE_UTF16BE );
 1.11327 +  testcase( u.cq.pName->enc==SQLITE_UTF16LE );
 1.11328 +  rc = sqlite3VdbeChangeEncoding(u.cq.pName, SQLITE_UTF8);
 1.11329    if( rc==SQLITE_OK ){
 1.11330 -    rc = u.cp.pVtab->pModule->xRename(u.cp.pVtab, u.cp.pName->z);
 1.11331 -    importVtabErrMsg(p, u.cp.pVtab);
 1.11332 +    rc = u.cq.pVtab->pModule->xRename(u.cq.pVtab, u.cq.pName->z);
 1.11333 +    importVtabErrMsg(p, u.cq.pVtab);
 1.11334      p->expired = 0;
 1.11335    }
 1.11336    break;
 1.11337 @@ -71841,7 +70079,7 @@
 1.11338  ** is set to the value of the rowid for the row just inserted.
 1.11339  */
 1.11340  case OP_VUpdate: {
 1.11341 -#if 0  /* local variables moved into u.cq */
 1.11342 +#if 0  /* local variables moved into u.cr */
 1.11343    sqlite3_vtab *pVtab;
 1.11344    sqlite3_module *pModule;
 1.11345    int nArg;
 1.11346 @@ -71849,33 +70087,33 @@
 1.11347    sqlite_int64 rowid;
 1.11348    Mem **apArg;
 1.11349    Mem *pX;
 1.11350 -#endif /* local variables moved into u.cq */
 1.11351 +#endif /* local variables moved into u.cr */
 1.11352  
 1.11353    assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback
 1.11354         || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
 1.11355    );
 1.11356 -  u.cq.pVtab = pOp->p4.pVtab->pVtab;
 1.11357 -  u.cq.pModule = (sqlite3_module *)u.cq.pVtab->pModule;
 1.11358 -  u.cq.nArg = pOp->p2;
 1.11359 +  u.cr.pVtab = pOp->p4.pVtab->pVtab;
 1.11360 +  u.cr.pModule = (sqlite3_module *)u.cr.pVtab->pModule;
 1.11361 +  u.cr.nArg = pOp->p2;
 1.11362    assert( pOp->p4type==P4_VTAB );
 1.11363 -  if( ALWAYS(u.cq.pModule->xUpdate) ){
 1.11364 +  if( ALWAYS(u.cr.pModule->xUpdate) ){
 1.11365      u8 vtabOnConflict = db->vtabOnConflict;
 1.11366 -    u.cq.apArg = p->apArg;
 1.11367 -    u.cq.pX = &aMem[pOp->p3];
 1.11368 -    for(u.cq.i=0; u.cq.i<u.cq.nArg; u.cq.i++){
 1.11369 -      assert( memIsValid(u.cq.pX) );
 1.11370 -      memAboutToChange(p, u.cq.pX);
 1.11371 -      sqlite3VdbeMemStoreType(u.cq.pX);
 1.11372 -      u.cq.apArg[u.cq.i] = u.cq.pX;
 1.11373 -      u.cq.pX++;
 1.11374 +    u.cr.apArg = p->apArg;
 1.11375 +    u.cr.pX = &aMem[pOp->p3];
 1.11376 +    for(u.cr.i=0; u.cr.i<u.cr.nArg; u.cr.i++){
 1.11377 +      assert( memIsValid(u.cr.pX) );
 1.11378 +      memAboutToChange(p, u.cr.pX);
 1.11379 +      sqlite3VdbeMemStoreType(u.cr.pX);
 1.11380 +      u.cr.apArg[u.cr.i] = u.cr.pX;
 1.11381 +      u.cr.pX++;
 1.11382      }
 1.11383      db->vtabOnConflict = pOp->p5;
 1.11384 -    rc = u.cq.pModule->xUpdate(u.cq.pVtab, u.cq.nArg, u.cq.apArg, &u.cq.rowid);
 1.11385 +    rc = u.cr.pModule->xUpdate(u.cr.pVtab, u.cr.nArg, u.cr.apArg, &u.cr.rowid);
 1.11386      db->vtabOnConflict = vtabOnConflict;
 1.11387 -    importVtabErrMsg(p, u.cq.pVtab);
 1.11388 +    importVtabErrMsg(p, u.cr.pVtab);
 1.11389      if( rc==SQLITE_OK && pOp->p1 ){
 1.11390 -      assert( u.cq.nArg>1 && u.cq.apArg[0] && (u.cq.apArg[0]->flags&MEM_Null) );
 1.11391 -      db->lastRowid = lastRowid = u.cq.rowid;
 1.11392 +      assert( u.cr.nArg>1 && u.cr.apArg[0] && (u.cr.apArg[0]->flags&MEM_Null) );
 1.11393 +      db->lastRowid = lastRowid = u.cr.rowid;
 1.11394      }
 1.11395      if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
 1.11396        if( pOp->p5==OE_Ignore ){
 1.11397 @@ -71935,21 +70173,24 @@
 1.11398  ** the UTF-8 string contained in P4 is emitted on the trace callback.
 1.11399  */
 1.11400  case OP_Trace: {
 1.11401 -#if 0  /* local variables moved into u.cr */
 1.11402 +#if 0  /* local variables moved into u.cs */
 1.11403    char *zTrace;
 1.11404    char *z;
 1.11405 -#endif /* local variables moved into u.cr */
 1.11406 -
 1.11407 -  if( db->xTrace && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
 1.11408 -    u.cr.z = sqlite3VdbeExpandSql(p, u.cr.zTrace);
 1.11409 -    db->xTrace(db->pTraceArg, u.cr.z);
 1.11410 -    sqlite3DbFree(db, u.cr.z);
 1.11411 +#endif /* local variables moved into u.cs */
 1.11412 +
 1.11413 +  if( db->xTrace
 1.11414 +   && !p->doingRerun
 1.11415 +   && (u.cs.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 1.11416 +  ){
 1.11417 +    u.cs.z = sqlite3VdbeExpandSql(p, u.cs.zTrace);
 1.11418 +    db->xTrace(db->pTraceArg, u.cs.z);
 1.11419 +    sqlite3DbFree(db, u.cs.z);
 1.11420    }
 1.11421  #ifdef SQLITE_DEBUG
 1.11422    if( (db->flags & SQLITE_SqlTrace)!=0
 1.11423 -   && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 1.11424 +   && (u.cs.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 1.11425    ){
 1.11426 -    sqlite3DebugPrintf("SQL-trace: %s\n", u.cr.zTrace);
 1.11427 +    sqlite3DebugPrintf("SQL-trace: %s\n", u.cs.zTrace);
 1.11428    }
 1.11429  #endif /* SQLITE_DEBUG */
 1.11430    break;
 1.11431 @@ -72571,6 +70812,7 @@
 1.11432  
 1.11433  typedef struct VdbeSorterIter VdbeSorterIter;
 1.11434  typedef struct SorterRecord SorterRecord;
 1.11435 +typedef struct FileWriter FileWriter;
 1.11436  
 1.11437  /*
 1.11438  ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
 1.11439 @@ -72668,6 +70910,24 @@
 1.11440    sqlite3_file *pFile;            /* File iterator is reading from */
 1.11441    u8 *aAlloc;                     /* Allocated space */
 1.11442    u8 *aKey;                       /* Pointer to current key */
 1.11443 +  u8 *aBuffer;                    /* Current read buffer */
 1.11444 +  int nBuffer;                    /* Size of read buffer in bytes */
 1.11445 +};
 1.11446 +
 1.11447 +/*
 1.11448 +** An instance of this structure is used to organize the stream of records
 1.11449 +** being written to files by the merge-sort code into aligned, page-sized
 1.11450 +** blocks.  Doing all I/O in aligned page-sized blocks helps I/O to go
 1.11451 +** faster on many operating systems.
 1.11452 +*/
 1.11453 +struct FileWriter {
 1.11454 +  int eFWErr;                     /* Non-zero if in an error state */
 1.11455 +  u8 *aBuffer;                    /* Pointer to write buffer */
 1.11456 +  int nBuffer;                    /* Size of write buffer in bytes */
 1.11457 +  int iBufStart;                  /* First byte of buffer to write */
 1.11458 +  int iBufEnd;                    /* Last byte of buffer to write */
 1.11459 +  i64 iWriteOff;                  /* Offset of start of buffer in file */
 1.11460 +  sqlite3_file *pFile;            /* File to write to */
 1.11461  };
 1.11462  
 1.11463  /*
 1.11464 @@ -72693,10 +70953,127 @@
 1.11465  */
 1.11466  static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
 1.11467    sqlite3DbFree(db, pIter->aAlloc);
 1.11468 +  sqlite3DbFree(db, pIter->aBuffer);
 1.11469    memset(pIter, 0, sizeof(VdbeSorterIter));
 1.11470  }
 1.11471  
 1.11472  /*
 1.11473 +** Read nByte bytes of data from the stream of data iterated by object p.
 1.11474 +** If successful, set *ppOut to point to a buffer containing the data
 1.11475 +** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
 1.11476 +** error code.
 1.11477 +**
 1.11478 +** The buffer indicated by *ppOut may only be considered valid until the
 1.11479 +** next call to this function.
 1.11480 +*/
 1.11481 +static int vdbeSorterIterRead(
 1.11482 +  sqlite3 *db,                    /* Database handle (for malloc) */
 1.11483 +  VdbeSorterIter *p,              /* Iterator */
 1.11484 +  int nByte,                      /* Bytes of data to read */
 1.11485 +  u8 **ppOut                      /* OUT: Pointer to buffer containing data */
 1.11486 +){
 1.11487 +  int iBuf;                       /* Offset within buffer to read from */
 1.11488 +  int nAvail;                     /* Bytes of data available in buffer */
 1.11489 +  assert( p->aBuffer );
 1.11490 +
 1.11491 +  /* If there is no more data to be read from the buffer, read the next 
 1.11492 +  ** p->nBuffer bytes of data from the file into it. Or, if there are less
 1.11493 +  ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
 1.11494 +  iBuf = p->iReadOff % p->nBuffer;
 1.11495 +  if( iBuf==0 ){
 1.11496 +    int nRead;                    /* Bytes to read from disk */
 1.11497 +    int rc;                       /* sqlite3OsRead() return code */
 1.11498 +
 1.11499 +    /* Determine how many bytes of data to read. */
 1.11500 +    if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
 1.11501 +      nRead = p->nBuffer;
 1.11502 +    }else{
 1.11503 +      nRead = (int)(p->iEof - p->iReadOff);
 1.11504 +    }
 1.11505 +    assert( nRead>0 );
 1.11506 +
 1.11507 +    /* Read data from the file. Return early if an error occurs. */
 1.11508 +    rc = sqlite3OsRead(p->pFile, p->aBuffer, nRead, p->iReadOff);
 1.11509 +    assert( rc!=SQLITE_IOERR_SHORT_READ );
 1.11510 +    if( rc!=SQLITE_OK ) return rc;
 1.11511 +  }
 1.11512 +  nAvail = p->nBuffer - iBuf; 
 1.11513 +
 1.11514 +  if( nByte<=nAvail ){
 1.11515 +    /* The requested data is available in the in-memory buffer. In this
 1.11516 +    ** case there is no need to make a copy of the data, just return a 
 1.11517 +    ** pointer into the buffer to the caller.  */
 1.11518 +    *ppOut = &p->aBuffer[iBuf];
 1.11519 +    p->iReadOff += nByte;
 1.11520 +  }else{
 1.11521 +    /* The requested data is not all available in the in-memory buffer.
 1.11522 +    ** In this case, allocate space at p->aAlloc[] to copy the requested
 1.11523 +    ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
 1.11524 +    int nRem;                     /* Bytes remaining to copy */
 1.11525 +
 1.11526 +    /* Extend the p->aAlloc[] allocation if required. */
 1.11527 +    if( p->nAlloc<nByte ){
 1.11528 +      int nNew = p->nAlloc*2;
 1.11529 +      while( nByte>nNew ) nNew = nNew*2;
 1.11530 +      p->aAlloc = sqlite3DbReallocOrFree(db, p->aAlloc, nNew);
 1.11531 +      if( !p->aAlloc ) return SQLITE_NOMEM;
 1.11532 +      p->nAlloc = nNew;
 1.11533 +    }
 1.11534 +
 1.11535 +    /* Copy as much data as is available in the buffer into the start of
 1.11536 +    ** p->aAlloc[].  */
 1.11537 +    memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
 1.11538 +    p->iReadOff += nAvail;
 1.11539 +    nRem = nByte - nAvail;
 1.11540 +
 1.11541 +    /* The following loop copies up to p->nBuffer bytes per iteration into
 1.11542 +    ** the p->aAlloc[] buffer.  */
 1.11543 +    while( nRem>0 ){
 1.11544 +      int rc;                     /* vdbeSorterIterRead() return code */
 1.11545 +      int nCopy;                  /* Number of bytes to copy */
 1.11546 +      u8 *aNext;                  /* Pointer to buffer to copy data from */
 1.11547 +
 1.11548 +      nCopy = nRem;
 1.11549 +      if( nRem>p->nBuffer ) nCopy = p->nBuffer;
 1.11550 +      rc = vdbeSorterIterRead(db, p, nCopy, &aNext);
 1.11551 +      if( rc!=SQLITE_OK ) return rc;
 1.11552 +      assert( aNext!=p->aAlloc );
 1.11553 +      memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
 1.11554 +      nRem -= nCopy;
 1.11555 +    }
 1.11556 +
 1.11557 +    *ppOut = p->aAlloc;
 1.11558 +  }
 1.11559 +
 1.11560 +  return SQLITE_OK;
 1.11561 +}
 1.11562 +
 1.11563 +/*
 1.11564 +** Read a varint from the stream of data accessed by p. Set *pnOut to
 1.11565 +** the value read.
 1.11566 +*/
 1.11567 +static int vdbeSorterIterVarint(sqlite3 *db, VdbeSorterIter *p, u64 *pnOut){
 1.11568 +  int iBuf;
 1.11569 +
 1.11570 +  iBuf = p->iReadOff % p->nBuffer;
 1.11571 +  if( iBuf && (p->nBuffer-iBuf)>=9 ){
 1.11572 +    p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
 1.11573 +  }else{
 1.11574 +    u8 aVarint[16], *a;
 1.11575 +    int i = 0, rc;
 1.11576 +    do{
 1.11577 +      rc = vdbeSorterIterRead(db, p, 1, &a);
 1.11578 +      if( rc ) return rc;
 1.11579 +      aVarint[(i++)&0xf] = a[0];
 1.11580 +    }while( (a[0]&0x80)!=0 );
 1.11581 +    sqlite3GetVarint(aVarint, pnOut);
 1.11582 +  }
 1.11583 +
 1.11584 +  return SQLITE_OK;
 1.11585 +}
 1.11586 +
 1.11587 +
 1.11588 +/*
 1.11589  ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
 1.11590  ** no error occurs, or an SQLite error code if one does.
 1.11591  */
 1.11592 @@ -72705,96 +71082,18 @@
 1.11593    VdbeSorterIter *pIter           /* Iterator to advance */
 1.11594  ){
 1.11595    int rc;                         /* Return Code */
 1.11596 -  int nRead;                      /* Number of bytes read */
 1.11597 -  int nRec = 0;                   /* Size of record in bytes */
 1.11598 -  int iOff = 0;                   /* Size of serialized size varint in bytes */
 1.11599 -
 1.11600 -  assert( pIter->iEof>=pIter->iReadOff );
 1.11601 -  if( pIter->iEof-pIter->iReadOff>5 ){
 1.11602 -    nRead = 5;
 1.11603 -  }else{
 1.11604 -    nRead = (int)(pIter->iEof - pIter->iReadOff);
 1.11605 -  }
 1.11606 -  if( nRead<=0 ){
 1.11607 +  u64 nRec = 0;                   /* Size of record in bytes */
 1.11608 +
 1.11609 +  if( pIter->iReadOff>=pIter->iEof ){
 1.11610      /* This is an EOF condition */
 1.11611      vdbeSorterIterZero(db, pIter);
 1.11612      return SQLITE_OK;
 1.11613    }
 1.11614  
 1.11615 -  rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
 1.11616 +  rc = vdbeSorterIterVarint(db, pIter, &nRec);
 1.11617    if( rc==SQLITE_OK ){
 1.11618 -    iOff = getVarint32(pIter->aAlloc, nRec);
 1.11619 -    if( (iOff+nRec)>nRead ){
 1.11620 -      int nRead2;                   /* Number of extra bytes to read */
 1.11621 -      if( (iOff+nRec)>pIter->nAlloc ){
 1.11622 -        int nNew = pIter->nAlloc*2;
 1.11623 -        while( (iOff+nRec)>nNew ) nNew = nNew*2;
 1.11624 -        pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
 1.11625 -        if( !pIter->aAlloc ) return SQLITE_NOMEM;
 1.11626 -        pIter->nAlloc = nNew;
 1.11627 -      }
 1.11628 -  
 1.11629 -      nRead2 = iOff + nRec - nRead;
 1.11630 -      rc = sqlite3OsRead(
 1.11631 -          pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
 1.11632 -      );
 1.11633 -    }
 1.11634 -  }
 1.11635 -
 1.11636 -  assert( rc!=SQLITE_OK || nRec>0 );
 1.11637 -  pIter->iReadOff += iOff+nRec;
 1.11638 -  pIter->nKey = nRec;
 1.11639 -  pIter->aKey = &pIter->aAlloc[iOff];
 1.11640 -  return rc;
 1.11641 -}
 1.11642 -
 1.11643 -/*
 1.11644 -** Write a single varint, value iVal, to file-descriptor pFile. Return
 1.11645 -** SQLITE_OK if successful, or an SQLite error code if some error occurs.
 1.11646 -**
 1.11647 -** The value of *piOffset when this function is called is used as the byte
 1.11648 -** offset in file pFile to write to. Before returning, *piOffset is 
 1.11649 -** incremented by the number of bytes written.
 1.11650 -*/
 1.11651 -static int vdbeSorterWriteVarint(
 1.11652 -  sqlite3_file *pFile,            /* File to write to */
 1.11653 -  i64 iVal,                       /* Value to write as a varint */
 1.11654 -  i64 *piOffset                   /* IN/OUT: Write offset in file pFile */
 1.11655 -){
 1.11656 -  u8 aVarint[9];                  /* Buffer large enough for a varint */
 1.11657 -  int nVarint;                    /* Number of used bytes in varint */
 1.11658 -  int rc;                         /* Result of write() call */
 1.11659 -
 1.11660 -  nVarint = sqlite3PutVarint(aVarint, iVal);
 1.11661 -  rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset);
 1.11662 -  *piOffset += nVarint;
 1.11663 -
 1.11664 -  return rc;
 1.11665 -}
 1.11666 -
 1.11667 -/*
 1.11668 -** Read a single varint from file-descriptor pFile. Return SQLITE_OK if
 1.11669 -** successful, or an SQLite error code if some error occurs.
 1.11670 -**
 1.11671 -** The value of *piOffset when this function is called is used as the
 1.11672 -** byte offset in file pFile from whence to read the varint. If successful
 1.11673 -** (i.e. if no IO error occurs), then *piOffset is set to the offset of
 1.11674 -** the first byte past the end of the varint before returning. *piVal is
 1.11675 -** set to the integer value read. If an error occurs, the final values of
 1.11676 -** both *piOffset and *piVal are undefined.
 1.11677 -*/
 1.11678 -static int vdbeSorterReadVarint(
 1.11679 -  sqlite3_file *pFile,            /* File to read from */
 1.11680 -  i64 *piOffset,                  /* IN/OUT: Read offset in pFile */
 1.11681 -  i64 *piVal                      /* OUT: Value read from file */
 1.11682 -){
 1.11683 -  u8 aVarint[9];                  /* Buffer large enough for a varint */
 1.11684 -  i64 iOff = *piOffset;           /* Offset in file to read from */
 1.11685 -  int rc;                         /* Return code */
 1.11686 -
 1.11687 -  rc = sqlite3OsRead(pFile, aVarint, 9, iOff);
 1.11688 -  if( rc==SQLITE_OK ){
 1.11689 -    *piOffset += getVarint(aVarint, (u64 *)piVal);
 1.11690 +    pIter->nKey = (int)nRec;
 1.11691 +    rc = vdbeSorterIterRead(db, pIter, (int)nRec, &pIter->aKey);
 1.11692    }
 1.11693  
 1.11694    return rc;
 1.11695 @@ -72808,27 +71107,52 @@
 1.11696  */
 1.11697  static int vdbeSorterIterInit(
 1.11698    sqlite3 *db,                    /* Database handle */
 1.11699 -  VdbeSorter *pSorter,            /* Sorter object */
 1.11700 +  const VdbeSorter *pSorter,      /* Sorter object */
 1.11701    i64 iStart,                     /* Start offset in pFile */
 1.11702    VdbeSorterIter *pIter,          /* Iterator to populate */
 1.11703    i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
 1.11704  ){
 1.11705 -  int rc;
 1.11706 +  int rc = SQLITE_OK;
 1.11707 +  int nBuf;
 1.11708 +
 1.11709 +  nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 1.11710  
 1.11711    assert( pSorter->iWriteOff>iStart );
 1.11712    assert( pIter->aAlloc==0 );
 1.11713 +  assert( pIter->aBuffer==0 );
 1.11714    pIter->pFile = pSorter->pTemp1;
 1.11715    pIter->iReadOff = iStart;
 1.11716    pIter->nAlloc = 128;
 1.11717    pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
 1.11718 -  if( !pIter->aAlloc ){
 1.11719 +  pIter->nBuffer = nBuf;
 1.11720 +  pIter->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
 1.11721 +
 1.11722 +  if( !pIter->aBuffer ){
 1.11723      rc = SQLITE_NOMEM;
 1.11724    }else{
 1.11725 -    i64 nByte;                         /* Total size of PMA in bytes */
 1.11726 -    rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte);
 1.11727 -    *pnByte += nByte;
 1.11728 -    pIter->iEof = pIter->iReadOff + nByte;
 1.11729 -  }
 1.11730 +    int iBuf;
 1.11731 +
 1.11732 +    iBuf = iStart % nBuf;
 1.11733 +    if( iBuf ){
 1.11734 +      int nRead = nBuf - iBuf;
 1.11735 +      if( (iStart + nRead) > pSorter->iWriteOff ){
 1.11736 +        nRead = (int)(pSorter->iWriteOff - iStart);
 1.11737 +      }
 1.11738 +      rc = sqlite3OsRead(
 1.11739 +          pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
 1.11740 +      );
 1.11741 +      assert( rc!=SQLITE_IOERR_SHORT_READ );
 1.11742 +    }
 1.11743 +
 1.11744 +    if( rc==SQLITE_OK ){
 1.11745 +      u64 nByte;                       /* Size of PMA in bytes */
 1.11746 +      pIter->iEof = pSorter->iWriteOff;
 1.11747 +      rc = vdbeSorterIterVarint(db, pIter, &nByte);
 1.11748 +      pIter->iEof = pIter->iReadOff + nByte;
 1.11749 +      *pnByte += nByte;
 1.11750 +    }
 1.11751 +  }
 1.11752 +
 1.11753    if( rc==SQLITE_OK ){
 1.11754      rc = vdbeSorterIterNext(db, pIter);
 1.11755    }
 1.11756 @@ -72852,10 +71176,10 @@
 1.11757  ** has been allocated and contains an unpacked record that is used as key2.
 1.11758  */
 1.11759  static void vdbeSorterCompare(
 1.11760 -  VdbeCursor *pCsr,               /* Cursor object (for pKeyInfo) */
 1.11761 +  const VdbeCursor *pCsr,         /* Cursor object (for pKeyInfo) */
 1.11762    int bOmitRowid,                 /* Ignore rowid field at end of keys */
 1.11763 -  void *pKey1, int nKey1,         /* Left side of comparison */
 1.11764 -  void *pKey2, int nKey2,         /* Right side of comparison */
 1.11765 +  const void *pKey1, int nKey1,   /* Left side of comparison */
 1.11766 +  const void *pKey2, int nKey2,   /* Right side of comparison */
 1.11767    int *pRes                       /* OUT: Result of comparison */
 1.11768  ){
 1.11769    KeyInfo *pKeyInfo = pCsr->pKeyInfo;
 1.11770 @@ -72887,7 +71211,7 @@
 1.11771  ** multiple b-tree segments. Parameter iOut is the index of the aTree[] 
 1.11772  ** value to recalculate.
 1.11773  */
 1.11774 -static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
 1.11775 +static int vdbeSorterDoCompare(const VdbeCursor *pCsr, int iOut){
 1.11776    VdbeSorter *pSorter = pCsr->pSorter;
 1.11777    int i1;
 1.11778    int i2;
 1.11779 @@ -73013,7 +71337,7 @@
 1.11780  ** Set *ppOut to the head of the new list.
 1.11781  */
 1.11782  static void vdbeSorterMerge(
 1.11783 -  VdbeCursor *pCsr,               /* For pKeyInfo */
 1.11784 +  const VdbeCursor *pCsr,         /* For pKeyInfo */
 1.11785    SorterRecord *p1,               /* First list to merge */
 1.11786    SorterRecord *p2,               /* Second list to merge */
 1.11787    SorterRecord **ppOut            /* OUT: Head of merged list */
 1.11788 @@ -73047,7 +71371,7 @@
 1.11789  ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
 1.11790  ** occurs.
 1.11791  */
 1.11792 -static int vdbeSorterSort(VdbeCursor *pCsr){
 1.11793 +static int vdbeSorterSort(const VdbeCursor *pCsr){
 1.11794    int i;
 1.11795    SorterRecord **aSlot;
 1.11796    SorterRecord *p;
 1.11797 @@ -73080,6 +71404,91 @@
 1.11798    return SQLITE_OK;
 1.11799  }
 1.11800  
 1.11801 +/*
 1.11802 +** Initialize a file-writer object.
 1.11803 +*/
 1.11804 +static void fileWriterInit(
 1.11805 +  sqlite3 *db,                    /* Database (for malloc) */
 1.11806 +  sqlite3_file *pFile,            /* File to write to */
 1.11807 +  FileWriter *p,                  /* Object to populate */
 1.11808 +  i64 iStart                      /* Offset of pFile to begin writing at */
 1.11809 +){
 1.11810 +  int nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 1.11811 +
 1.11812 +  memset(p, 0, sizeof(FileWriter));
 1.11813 +  p->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
 1.11814 +  if( !p->aBuffer ){
 1.11815 +    p->eFWErr = SQLITE_NOMEM;
 1.11816 +  }else{
 1.11817 +    p->iBufEnd = p->iBufStart = (iStart % nBuf);
 1.11818 +    p->iWriteOff = iStart - p->iBufStart;
 1.11819 +    p->nBuffer = nBuf;
 1.11820 +    p->pFile = pFile;
 1.11821 +  }
 1.11822 +}
 1.11823 +
 1.11824 +/*
 1.11825 +** Write nData bytes of data to the file-write object. Return SQLITE_OK
 1.11826 +** if successful, or an SQLite error code if an error occurs.
 1.11827 +*/
 1.11828 +static void fileWriterWrite(FileWriter *p, u8 *pData, int nData){
 1.11829 +  int nRem = nData;
 1.11830 +  while( nRem>0 && p->eFWErr==0 ){
 1.11831 +    int nCopy = nRem;
 1.11832 +    if( nCopy>(p->nBuffer - p->iBufEnd) ){
 1.11833 +      nCopy = p->nBuffer - p->iBufEnd;
 1.11834 +    }
 1.11835 +
 1.11836 +    memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
 1.11837 +    p->iBufEnd += nCopy;
 1.11838 +    if( p->iBufEnd==p->nBuffer ){
 1.11839 +      p->eFWErr = sqlite3OsWrite(p->pFile, 
 1.11840 +          &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
 1.11841 +          p->iWriteOff + p->iBufStart
 1.11842 +      );
 1.11843 +      p->iBufStart = p->iBufEnd = 0;
 1.11844 +      p->iWriteOff += p->nBuffer;
 1.11845 +    }
 1.11846 +    assert( p->iBufEnd<p->nBuffer );
 1.11847 +
 1.11848 +    nRem -= nCopy;
 1.11849 +  }
 1.11850 +}
 1.11851 +
 1.11852 +/*
 1.11853 +** Flush any buffered data to disk and clean up the file-writer object.
 1.11854 +** The results of using the file-writer after this call are undefined.
 1.11855 +** Return SQLITE_OK if flushing the buffered data succeeds or is not 
 1.11856 +** required. Otherwise, return an SQLite error code.
 1.11857 +**
 1.11858 +** Before returning, set *piEof to the offset immediately following the
 1.11859 +** last byte written to the file.
 1.11860 +*/
 1.11861 +static int fileWriterFinish(sqlite3 *db, FileWriter *p, i64 *piEof){
 1.11862 +  int rc;
 1.11863 +  if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
 1.11864 +    p->eFWErr = sqlite3OsWrite(p->pFile, 
 1.11865 +        &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
 1.11866 +        p->iWriteOff + p->iBufStart
 1.11867 +    );
 1.11868 +  }
 1.11869 +  *piEof = (p->iWriteOff + p->iBufEnd);
 1.11870 +  sqlite3DbFree(db, p->aBuffer);
 1.11871 +  rc = p->eFWErr;
 1.11872 +  memset(p, 0, sizeof(FileWriter));
 1.11873 +  return rc;
 1.11874 +}
 1.11875 +
 1.11876 +/*
 1.11877 +** Write value iVal encoded as a varint to the file-write object. Return 
 1.11878 +** SQLITE_OK if successful, or an SQLite error code if an error occurs.
 1.11879 +*/
 1.11880 +static void fileWriterWriteVarint(FileWriter *p, u64 iVal){
 1.11881 +  int nByte; 
 1.11882 +  u8 aByte[10];
 1.11883 +  nByte = sqlite3PutVarint(aByte, iVal);
 1.11884 +  fileWriterWrite(p, aByte, nByte);
 1.11885 +}
 1.11886  
 1.11887  /*
 1.11888  ** Write the current contents of the in-memory linked-list to a PMA. Return
 1.11889 @@ -73094,9 +71503,12 @@
 1.11890  **       Each record consists of a varint followed by a blob of data (the 
 1.11891  **       key). The varint is the number of bytes in the blob of data.
 1.11892  */
 1.11893 -static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){
 1.11894 +static int vdbeSorterListToPMA(sqlite3 *db, const VdbeCursor *pCsr){
 1.11895    int rc = SQLITE_OK;             /* Return code */
 1.11896    VdbeSorter *pSorter = pCsr->pSorter;
 1.11897 +  FileWriter writer;
 1.11898 +
 1.11899 +  memset(&writer, 0, sizeof(FileWriter));
 1.11900  
 1.11901    if( pSorter->nInMemory==0 ){
 1.11902      assert( pSorter->pRecord==0 );
 1.11903 @@ -73114,39 +71526,20 @@
 1.11904    }
 1.11905  
 1.11906    if( rc==SQLITE_OK ){
 1.11907 -    i64 iOff = pSorter->iWriteOff;
 1.11908      SorterRecord *p;
 1.11909      SorterRecord *pNext = 0;
 1.11910 -    static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 1.11911 -
 1.11912 +
 1.11913 +    fileWriterInit(db, pSorter->pTemp1, &writer, pSorter->iWriteOff);
 1.11914      pSorter->nPMA++;
 1.11915 -    rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff);
 1.11916 -    for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){
 1.11917 +    fileWriterWriteVarint(&writer, pSorter->nInMemory);
 1.11918 +    for(p=pSorter->pRecord; p; p=pNext){
 1.11919        pNext = p->pNext;
 1.11920 -      rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff);
 1.11921 -
 1.11922 -      if( rc==SQLITE_OK ){
 1.11923 -        rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff);
 1.11924 -        iOff += p->nVal;
 1.11925 -      }
 1.11926 -
 1.11927 +      fileWriterWriteVarint(&writer, p->nVal);
 1.11928 +      fileWriterWrite(&writer, p->pVal, p->nVal);
 1.11929        sqlite3DbFree(db, p);
 1.11930      }
 1.11931 -
 1.11932 -    /* This assert verifies that unless an error has occurred, the size of 
 1.11933 -    ** the PMA on disk is the same as the expected size stored in
 1.11934 -    ** pSorter->nInMemory. */ 
 1.11935 -    assert( rc!=SQLITE_OK || pSorter->nInMemory==(
 1.11936 -          iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory)
 1.11937 -    ));
 1.11938 -
 1.11939 -    pSorter->iWriteOff = iOff;
 1.11940 -    if( rc==SQLITE_OK ){
 1.11941 -      /* Terminate each file with 8 extra bytes so that from any offset
 1.11942 -      ** in the file we can always read 9 bytes without a SHORT_READ error */
 1.11943 -      rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff);
 1.11944 -    }
 1.11945      pSorter->pRecord = p;
 1.11946 +    rc = fileWriterFinish(db, &writer, &pSorter->iWriteOff);
 1.11947    }
 1.11948  
 1.11949    return rc;
 1.11950 @@ -73157,7 +71550,7 @@
 1.11951  */
 1.11952  SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
 1.11953    sqlite3 *db,                    /* Database handle */
 1.11954 -  VdbeCursor *pCsr,               /* Sorter cursor */
 1.11955 +  const VdbeCursor *pCsr,               /* Sorter cursor */
 1.11956    Mem *pVal                       /* Memory cell containing record */
 1.11957  ){
 1.11958    VdbeSorter *pSorter = pCsr->pSorter;
 1.11959 @@ -73191,8 +71584,14 @@
 1.11960          (pSorter->nInMemory>pSorter->mxPmaSize)
 1.11961       || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
 1.11962    )){
 1.11963 +#ifdef SQLITE_DEBUG
 1.11964 +    i64 nExpect = pSorter->iWriteOff
 1.11965 +                + sqlite3VarintLen(pSorter->nInMemory)
 1.11966 +                + pSorter->nInMemory;
 1.11967 +#endif
 1.11968      rc = vdbeSorterListToPMA(db, pCsr);
 1.11969      pSorter->nInMemory = 0;
 1.11970 +    assert( rc!=SQLITE_OK || (nExpect==pSorter->iWriteOff) );
 1.11971    }
 1.11972  
 1.11973    return rc;
 1.11974 @@ -73203,7 +71602,7 @@
 1.11975  */
 1.11976  static int vdbeSorterInitMerge(
 1.11977    sqlite3 *db,                    /* Database handle */
 1.11978 -  VdbeCursor *pCsr,               /* Cursor handle for this sorter */
 1.11979 +  const VdbeCursor *pCsr,         /* Cursor handle for this sorter */
 1.11980    i64 *pnByte                     /* Sum of bytes in all opened PMAs */
 1.11981  ){
 1.11982    VdbeSorter *pSorter = pCsr->pSorter;
 1.11983 @@ -73233,7 +71632,7 @@
 1.11984  ** Once the sorter has been populated, this function is called to prepare
 1.11985  ** for iterating through its contents in sorted order.
 1.11986  */
 1.11987 -SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
 1.11988 +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
 1.11989    VdbeSorter *pSorter = pCsr->pSorter;
 1.11990    int rc;                         /* Return code */
 1.11991    sqlite3_file *pTemp2 = 0;       /* Second temp file to use */
 1.11992 @@ -73253,7 +71652,7 @@
 1.11993      return vdbeSorterSort(pCsr);
 1.11994    }
 1.11995  
 1.11996 -  /* Write the current b-tree to a PMA. Close the b-tree cursor. */
 1.11997 +  /* Write the current in-memory list to a PMA. */
 1.11998    rc = vdbeSorterListToPMA(db, pCsr);
 1.11999    if( rc!=SQLITE_OK ) return rc;
 1.12000  
 1.12001 @@ -73275,8 +71674,12 @@
 1.12002          rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA; 
 1.12003          iNew++
 1.12004      ){
 1.12005 +      int rc2;                    /* Return code from fileWriterFinish() */
 1.12006 +      FileWriter writer;          /* Object used to write to disk */
 1.12007        i64 nWrite;                 /* Number of bytes in new PMA */
 1.12008  
 1.12009 +      memset(&writer, 0, sizeof(FileWriter));
 1.12010 +
 1.12011        /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
 1.12012        ** initialize an iterator for each of them and break out of the loop.
 1.12013        ** These iterators will be incrementally merged as the VDBE layer calls
 1.12014 @@ -73299,22 +71702,19 @@
 1.12015        }
 1.12016  
 1.12017        if( rc==SQLITE_OK ){
 1.12018 -        rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2);
 1.12019 -      }
 1.12020 -
 1.12021 -      if( rc==SQLITE_OK ){
 1.12022          int bEof = 0;
 1.12023 +        fileWriterInit(db, pTemp2, &writer, iWrite2);
 1.12024 +        fileWriterWriteVarint(&writer, nWrite);
 1.12025          while( rc==SQLITE_OK && bEof==0 ){
 1.12026 -          int nToWrite;
 1.12027            VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
 1.12028            assert( pIter->pFile );
 1.12029 -          nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey);
 1.12030 -          rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2);
 1.12031 -          iWrite2 += nToWrite;
 1.12032 -          if( rc==SQLITE_OK ){
 1.12033 -            rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
 1.12034 -          }
 1.12035 -        }
 1.12036 +
 1.12037 +          fileWriterWriteVarint(&writer, pIter->nKey);
 1.12038 +          fileWriterWrite(&writer, pIter->aKey, pIter->nKey);
 1.12039 +          rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
 1.12040 +        }
 1.12041 +        rc2 = fileWriterFinish(db, &writer, &iWrite2);
 1.12042 +        if( rc==SQLITE_OK ) rc = rc2;
 1.12043        }
 1.12044      }
 1.12045  
 1.12046 @@ -73341,7 +71741,7 @@
 1.12047  /*
 1.12048  ** Advance to the next element in the sorter.
 1.12049  */
 1.12050 -SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
 1.12051 +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
 1.12052    VdbeSorter *pSorter = pCsr->pSorter;
 1.12053    int rc;                         /* Return code */
 1.12054  
 1.12055 @@ -73371,7 +71771,7 @@
 1.12056  ** current key.
 1.12057  */
 1.12058  static void *vdbeSorterRowkey(
 1.12059 -  VdbeSorter *pSorter,            /* Sorter object */
 1.12060 +  const VdbeSorter *pSorter,      /* Sorter object */
 1.12061    int *pnKey                      /* OUT: Size of current key in bytes */
 1.12062  ){
 1.12063    void *pKey;
 1.12064 @@ -73390,7 +71790,7 @@
 1.12065  /*
 1.12066  ** Copy the current sorter key into the memory cell pOut.
 1.12067  */
 1.12068 -SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
 1.12069 +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
 1.12070    VdbeSorter *pSorter = pCsr->pSorter;
 1.12071    void *pKey; int nKey;           /* Sorter key to copy into pOut */
 1.12072  
 1.12073 @@ -73416,7 +71816,7 @@
 1.12074  ** key.
 1.12075  */
 1.12076  SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
 1.12077 -  VdbeCursor *pCsr,               /* Sorter cursor */
 1.12078 +  const VdbeCursor *pCsr,         /* Sorter cursor */
 1.12079    Mem *pVal,                      /* Value to compare to current sorter key */
 1.12080    int *pRes                       /* OUT: Result of comparison */
 1.12081  ){
 1.12082 @@ -73661,6 +72061,16 @@
 1.12083    return createFile((JournalFile *)p);
 1.12084  }
 1.12085  
 1.12086 +/*
 1.12087 +** The file-handle passed as the only argument is guaranteed to be an open
 1.12088 +** file. It may or may not be of class JournalFile. If the file is a
 1.12089 +** JournalFile, and the underlying file on disk has not yet been opened,
 1.12090 +** return 0. Otherwise, return 1.
 1.12091 +*/
 1.12092 +SQLITE_PRIVATE int sqlite3JournalExists(sqlite3_file *p){
 1.12093 +  return (p->pMethods!=&JournalFileMethods || ((JournalFile *)p)->pReal!=0);
 1.12094 +}
 1.12095 +
 1.12096  /* 
 1.12097  ** Return the number of bytes required to store a JournalFile that uses vfs
 1.12098  ** pVfs to create the underlying on-disk files.
 1.12099 @@ -74059,13 +72469,19 @@
 1.12100    int rc;
 1.12101    if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
 1.12102    rc = WRC_Continue;
 1.12103 -  while( p  ){
 1.12104 +  pWalker->walkerDepth++;
 1.12105 +  while( p ){
 1.12106      rc = pWalker->xSelectCallback(pWalker, p);
 1.12107      if( rc ) break;
 1.12108 -    if( sqlite3WalkSelectExpr(pWalker, p) ) return WRC_Abort;
 1.12109 -    if( sqlite3WalkSelectFrom(pWalker, p) ) return WRC_Abort;
 1.12110 +    if( sqlite3WalkSelectExpr(pWalker, p)
 1.12111 +     || sqlite3WalkSelectFrom(pWalker, p)
 1.12112 +    ){
 1.12113 +      pWalker->walkerDepth--;
 1.12114 +      return WRC_Abort;
 1.12115 +    }
 1.12116      p = p->pPrior;
 1.12117    }
 1.12118 +  pWalker->walkerDepth--;
 1.12119    return rc & WRC_Abort;
 1.12120  }
 1.12121  
 1.12122 @@ -74091,6 +72507,29 @@
 1.12123  /* #include <string.h> */
 1.12124  
 1.12125  /*
 1.12126 +** Walk the expression tree pExpr and increase the aggregate function
 1.12127 +** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
 1.12128 +** This needs to occur when copying a TK_AGG_FUNCTION node from an
 1.12129 +** outer query into an inner subquery.
 1.12130 +**
 1.12131 +** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
 1.12132 +** is a helper function - a callback for the tree walker.
 1.12133 +*/
 1.12134 +static int incrAggDepth(Walker *pWalker, Expr *pExpr){
 1.12135 +  if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i;
 1.12136 +  return WRC_Continue;
 1.12137 +}
 1.12138 +static void incrAggFunctionDepth(Expr *pExpr, int N){
 1.12139 +  if( N>0 ){
 1.12140 +    Walker w;
 1.12141 +    memset(&w, 0, sizeof(w));
 1.12142 +    w.xExprCallback = incrAggDepth;
 1.12143 +    w.u.i = N;
 1.12144 +    sqlite3WalkExpr(&w, pExpr);
 1.12145 +  }
 1.12146 +}
 1.12147 +
 1.12148 +/*
 1.12149  ** Turn the pExpr expression into an alias for the iCol-th column of the
 1.12150  ** result set in pEList.
 1.12151  **
 1.12152 @@ -74116,13 +72555,29 @@
 1.12153  ** The result of random()%5 in the GROUP BY clause is probably different
 1.12154  ** from the result in the result-set.  We might fix this someday.  Or
 1.12155  ** then again, we might not...
 1.12156 +**
 1.12157 +** If the reference is followed by a COLLATE operator, then make sure
 1.12158 +** the COLLATE operator is preserved.  For example:
 1.12159 +**
 1.12160 +**     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
 1.12161 +**
 1.12162 +** Should be transformed into:
 1.12163 +**
 1.12164 +**     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
 1.12165 +**
 1.12166 +** The nSubquery parameter specifies how many levels of subquery the
 1.12167 +** alias is removed from the original expression.  The usually value is
 1.12168 +** zero but it might be more if the alias is contained within a subquery
 1.12169 +** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
 1.12170 +** structures must be increased by the nSubquery amount.
 1.12171  */
 1.12172  static void resolveAlias(
 1.12173    Parse *pParse,         /* Parsing context */
 1.12174    ExprList *pEList,      /* A result set */
 1.12175    int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
 1.12176    Expr *pExpr,           /* Transform this into an alias to the result set */
 1.12177 -  const char *zType      /* "GROUP" or "ORDER" or "" */
 1.12178 +  const char *zType,     /* "GROUP" or "ORDER" or "" */
 1.12179 +  int nSubquery          /* Number of subqueries that the label is moving */
 1.12180  ){
 1.12181    Expr *pOrig;           /* The iCol-th column of the result set */
 1.12182    Expr *pDup;            /* Copy of pOrig */
 1.12183 @@ -74133,40 +72588,36 @@
 1.12184    assert( pOrig!=0 );
 1.12185    assert( pOrig->flags & EP_Resolved );
 1.12186    db = pParse->db;
 1.12187 +  pDup = sqlite3ExprDup(db, pOrig, 0);
 1.12188 +  if( pDup==0 ) return;
 1.12189    if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
 1.12190 -    pDup = sqlite3ExprDup(db, pOrig, 0);
 1.12191 +    incrAggFunctionDepth(pDup, nSubquery);
 1.12192      pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
 1.12193      if( pDup==0 ) return;
 1.12194      if( pEList->a[iCol].iAlias==0 ){
 1.12195        pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
 1.12196      }
 1.12197      pDup->iTable = pEList->a[iCol].iAlias;
 1.12198 -  }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
 1.12199 -    pDup = sqlite3ExprDup(db, pOrig, 0);
 1.12200 -    if( pDup==0 ) return;
 1.12201 -  }else{
 1.12202 -    char *zToken = pOrig->u.zToken;
 1.12203 -    assert( zToken!=0 );
 1.12204 -    pOrig->u.zToken = 0;
 1.12205 -    pDup = sqlite3ExprDup(db, pOrig, 0);
 1.12206 -    pOrig->u.zToken = zToken;
 1.12207 -    if( pDup==0 ) return;
 1.12208 -    assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
 1.12209 -    pDup->flags2 |= EP2_MallocedToken;
 1.12210 -    pDup->u.zToken = sqlite3DbStrDup(db, zToken);
 1.12211 -  }
 1.12212 -  if( pExpr->flags & EP_ExpCollate ){
 1.12213 -    pDup->pColl = pExpr->pColl;
 1.12214 -    pDup->flags |= EP_ExpCollate;
 1.12215 +  }
 1.12216 +  if( pExpr->op==TK_COLLATE ){
 1.12217 +    pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
 1.12218    }
 1.12219  
 1.12220    /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
 1.12221    ** prevents ExprDelete() from deleting the Expr structure itself,
 1.12222    ** allowing it to be repopulated by the memcpy() on the following line.
 1.12223 +  ** The pExpr->u.zToken might point into memory that will be freed by the
 1.12224 +  ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
 1.12225 +  ** make a copy of the token before doing the sqlite3DbFree().
 1.12226    */
 1.12227    ExprSetProperty(pExpr, EP_Static);
 1.12228    sqlite3ExprDelete(db, pExpr);
 1.12229    memcpy(pExpr, pDup, sizeof(*pExpr));
 1.12230 +  if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
 1.12231 +    assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
 1.12232 +    pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
 1.12233 +    pExpr->flags2 |= EP2_MallocedToken;
 1.12234 +  }
 1.12235    sqlite3DbFree(db, pDup);
 1.12236  }
 1.12237  
 1.12238 @@ -74223,9 +72674,10 @@
 1.12239    NameContext *pNC,    /* The name context used to resolve the name */
 1.12240    Expr *pExpr          /* Make this EXPR node point to the selected column */
 1.12241  ){
 1.12242 -  int i, j;            /* Loop counters */
 1.12243 +  int i, j;                         /* Loop counters */
 1.12244    int cnt = 0;                      /* Number of matching column names */
 1.12245    int cntTab = 0;                   /* Number of matching table names */
 1.12246 +  int nSubquery = 0;                /* How many levels of subquery */
 1.12247    sqlite3 *db = pParse->db;         /* The database connection */
 1.12248    struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
 1.12249    struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
 1.12250 @@ -74235,7 +72687,7 @@
 1.12251  
 1.12252    assert( pNC );     /* the name context cannot be NULL. */
 1.12253    assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
 1.12254 -  assert( ~ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 1.12255 +  assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 1.12256  
 1.12257    /* Initialize the node to no-match */
 1.12258    pExpr->iTable = -1;
 1.12259 @@ -74387,7 +72839,7 @@
 1.12260              sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
 1.12261              return WRC_Abort;
 1.12262            }
 1.12263 -          resolveAlias(pParse, pEList, j, pExpr, "");
 1.12264 +          resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
 1.12265            cnt = 1;
 1.12266            pMatch = 0;
 1.12267            assert( zTab==0 && zDb==0 );
 1.12268 @@ -74401,6 +72853,7 @@
 1.12269      */
 1.12270      if( cnt==0 ){
 1.12271        pNC = pNC->pNext;
 1.12272 +      nSubquery++;
 1.12273      }
 1.12274    }
 1.12275  
 1.12276 @@ -74640,13 +73093,19 @@
 1.12277               nId, zId);
 1.12278          pNC->nErr++;
 1.12279        }
 1.12280 -      if( is_agg ){
 1.12281 -        pExpr->op = TK_AGG_FUNCTION;
 1.12282 -        pNC->ncFlags |= NC_HasAgg;
 1.12283 -      }
 1.12284        if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
 1.12285        sqlite3WalkExprList(pWalker, pList);
 1.12286 -      if( is_agg ) pNC->ncFlags |= NC_AllowAgg;
 1.12287 +      if( is_agg ){
 1.12288 +        NameContext *pNC2 = pNC;
 1.12289 +        pExpr->op = TK_AGG_FUNCTION;
 1.12290 +        pExpr->op2 = 0;
 1.12291 +        while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
 1.12292 +          pExpr->op2++;
 1.12293 +          pNC2 = pNC2->pNext;
 1.12294 +        }
 1.12295 +        if( pNC2 ) pNC2->ncFlags |= NC_HasAgg;
 1.12296 +        pNC->ncFlags |= NC_AllowAgg;
 1.12297 +      }
 1.12298        /* FIX ME:  Compute pExpr->affinity based on the expected return
 1.12299        ** type of the function 
 1.12300        */
 1.12301 @@ -74845,7 +73304,7 @@
 1.12302        int iCol = -1;
 1.12303        Expr *pE, *pDup;
 1.12304        if( pItem->done ) continue;
 1.12305 -      pE = pItem->pExpr;
 1.12306 +      pE = sqlite3ExprSkipCollate(pItem->pExpr);
 1.12307        if( sqlite3ExprIsInteger(pE, &iCol) ){
 1.12308          if( iCol<=0 || iCol>pEList->nExpr ){
 1.12309            resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
 1.12310 @@ -74863,14 +73322,20 @@
 1.12311          }
 1.12312        }
 1.12313        if( iCol>0 ){
 1.12314 -        CollSeq *pColl = pE->pColl;
 1.12315 -        int flags = pE->flags & EP_ExpCollate;
 1.12316 +        /* Convert the ORDER BY term into an integer column number iCol,
 1.12317 +        ** taking care to preserve the COLLATE clause if it exists */
 1.12318 +        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
 1.12319 +        if( pNew==0 ) return 1;
 1.12320 +        pNew->flags |= EP_IntValue;
 1.12321 +        pNew->u.iValue = iCol;
 1.12322 +        if( pItem->pExpr==pE ){
 1.12323 +          pItem->pExpr = pNew;
 1.12324 +        }else{
 1.12325 +          assert( pItem->pExpr->op==TK_COLLATE );
 1.12326 +          assert( pItem->pExpr->pLeft==pE );
 1.12327 +          pItem->pExpr->pLeft = pNew;
 1.12328 +        }
 1.12329          sqlite3ExprDelete(db, pE);
 1.12330 -        pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
 1.12331 -        if( pE==0 ) return 1;
 1.12332 -        pE->pColl = pColl;
 1.12333 -        pE->flags |= EP_IntValue | flags;
 1.12334 -        pE->u.iValue = iCol;
 1.12335          pItem->iOrderByCol = (u16)iCol;
 1.12336          pItem->done = 1;
 1.12337        }else{
 1.12338 @@ -74925,7 +73390,7 @@
 1.12339          resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
 1.12340          return 1;
 1.12341        }
 1.12342 -      resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType);
 1.12343 +      resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0);
 1.12344      }
 1.12345    }
 1.12346    return 0;
 1.12347 @@ -74975,11 +73440,11 @@
 1.12348        pItem->iOrderByCol = (u16)iCol;
 1.12349        continue;
 1.12350      }
 1.12351 -    if( sqlite3ExprIsInteger(pE, &iCol) ){
 1.12352 +    if( sqlite3ExprIsInteger(sqlite3ExprSkipCollate(pE), &iCol) ){
 1.12353        /* The ORDER BY term is an integer constant.  Again, set the column
 1.12354        ** number so that sqlite3ResolveOrderGroupBy() will convert the
 1.12355        ** order-by term to a copy of the result-set expression */
 1.12356 -      if( iCol<1 ){
 1.12357 +      if( iCol<1 || iCol>0xffff ){
 1.12358          resolveOutOfRangeError(pParse, zType, i+1, nResult);
 1.12359          return 1;
 1.12360        }
 1.12361 @@ -75333,7 +73798,9 @@
 1.12362  ** SELECT * FROM t1 WHERE (select a from t1);
 1.12363  */
 1.12364  SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
 1.12365 -  int op = pExpr->op;
 1.12366 +  int op;
 1.12367 +  pExpr = sqlite3ExprSkipCollate(pExpr);
 1.12368 +  op = pExpr->op;
 1.12369    if( op==TK_SELECT ){
 1.12370      assert( pExpr->flags&EP_xIsSelect );
 1.12371      return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
 1.12372 @@ -75358,66 +73825,94 @@
 1.12373  }
 1.12374  
 1.12375  /*
 1.12376 -** Set the explicit collating sequence for an expression to the
 1.12377 -** collating sequence supplied in the second argument.
 1.12378 -*/
 1.12379 -SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr *pExpr, CollSeq *pColl){
 1.12380 -  if( pExpr && pColl ){
 1.12381 -    pExpr->pColl = pColl;
 1.12382 -    pExpr->flags |= EP_ExpCollate;
 1.12383 +** Set the collating sequence for expression pExpr to be the collating
 1.12384 +** sequence named by pToken.   Return a pointer to a new Expr node that
 1.12385 +** implements the COLLATE operator.
 1.12386 +**
 1.12387 +** If a memory allocation error occurs, that fact is recorded in pParse->db
 1.12388 +** and the pExpr parameter is returned unchanged.
 1.12389 +*/
 1.12390 +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr *pExpr, Token *pCollName){
 1.12391 +  if( pCollName->n>0 ){
 1.12392 +    Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
 1.12393 +    if( pNew ){
 1.12394 +      pNew->pLeft = pExpr;
 1.12395 +      pNew->flags |= EP_Collate;
 1.12396 +      pExpr = pNew;
 1.12397 +    }
 1.12398    }
 1.12399    return pExpr;
 1.12400  }
 1.12401 -
 1.12402 -/*
 1.12403 -** Set the collating sequence for expression pExpr to be the collating
 1.12404 -** sequence named by pToken.   Return a pointer to the revised expression.
 1.12405 -** The collating sequence is marked as "explicit" using the EP_ExpCollate
 1.12406 -** flag.  An explicit collating sequence will override implicit
 1.12407 -** collating sequences.
 1.12408 -*/
 1.12409 -SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){
 1.12410 -  char *zColl = 0;            /* Dequoted name of collation sequence */
 1.12411 -  CollSeq *pColl;
 1.12412 -  sqlite3 *db = pParse->db;
 1.12413 -  zColl = sqlite3NameFromToken(db, pCollName);
 1.12414 -  pColl = sqlite3LocateCollSeq(pParse, zColl);
 1.12415 -  sqlite3ExprSetColl(pExpr, pColl);
 1.12416 -  sqlite3DbFree(db, zColl);
 1.12417 +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
 1.12418 +  Token s;
 1.12419 +  assert( zC!=0 );
 1.12420 +  s.z = zC;
 1.12421 +  s.n = sqlite3Strlen30(s.z);
 1.12422 +  return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
 1.12423 +}
 1.12424 +
 1.12425 +/*
 1.12426 +** Skip over any TK_COLLATE and/or TK_AS operators at the root of
 1.12427 +** an expression.
 1.12428 +*/
 1.12429 +SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
 1.12430 +  while( pExpr && (pExpr->op==TK_COLLATE || pExpr->op==TK_AS) ){
 1.12431 +    pExpr = pExpr->pLeft;
 1.12432 +  }
 1.12433    return pExpr;
 1.12434  }
 1.12435  
 1.12436  /*
 1.12437 -** Return the default collation sequence for the expression pExpr. If
 1.12438 -** there is no default collation type, return 0.
 1.12439 +** Return the collation sequence for the expression pExpr. If
 1.12440 +** there is no defined collating sequence, return NULL.
 1.12441 +**
 1.12442 +** The collating sequence might be determined by a COLLATE operator
 1.12443 +** or by the presence of a column with a defined collating sequence.
 1.12444 +** COLLATE operators take first precedence.  Left operands take
 1.12445 +** precedence over right operands.
 1.12446  */
 1.12447  SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
 1.12448 +  sqlite3 *db = pParse->db;
 1.12449    CollSeq *pColl = 0;
 1.12450    Expr *p = pExpr;
 1.12451    while( p ){
 1.12452 -    int op;
 1.12453 -    pColl = p->pColl;
 1.12454 -    if( pColl ) break;
 1.12455 -    op = p->op;
 1.12456 -    if( p->pTab!=0 && (
 1.12457 -        op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER
 1.12458 -    )){
 1.12459 +    int op = p->op;
 1.12460 +    if( op==TK_CAST || op==TK_UPLUS ){
 1.12461 +      p = p->pLeft;
 1.12462 +      continue;
 1.12463 +    }
 1.12464 +    assert( op!=TK_REGISTER || p->op2!=TK_COLLATE );
 1.12465 +    if( op==TK_COLLATE ){
 1.12466 +      if( db->init.busy ){
 1.12467 +        /* Do not report errors when parsing while the schema */
 1.12468 +        pColl = sqlite3FindCollSeq(db, ENC(db), p->u.zToken, 0);
 1.12469 +      }else{
 1.12470 +        pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
 1.12471 +      }
 1.12472 +      break;
 1.12473 +    }
 1.12474 +    if( p->pTab!=0
 1.12475 +     && (op==TK_AGG_COLUMN || op==TK_COLUMN
 1.12476 +          || op==TK_REGISTER || op==TK_TRIGGER)
 1.12477 +    ){
 1.12478        /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
 1.12479        ** a TK_COLUMN but was previously evaluated and cached in a register */
 1.12480 -      const char *zColl;
 1.12481        int j = p->iColumn;
 1.12482        if( j>=0 ){
 1.12483 -        sqlite3 *db = pParse->db;
 1.12484 -        zColl = p->pTab->aCol[j].zColl;
 1.12485 +        const char *zColl = p->pTab->aCol[j].zColl;
 1.12486          pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
 1.12487 -        pExpr->pColl = pColl;
 1.12488 -      }
 1.12489 -      break;
 1.12490 -    }
 1.12491 -    if( op!=TK_CAST && op!=TK_UPLUS ){
 1.12492 -      break;
 1.12493 -    }
 1.12494 -    p = p->pLeft;
 1.12495 +      }
 1.12496 +      break;
 1.12497 +    }
 1.12498 +    if( p->flags & EP_Collate ){
 1.12499 +      if( ALWAYS(p->pLeft) && (p->pLeft->flags & EP_Collate)!=0 ){
 1.12500 +        p = p->pLeft;
 1.12501 +      }else{
 1.12502 +        p = p->pRight;
 1.12503 +      }
 1.12504 +    }else{
 1.12505 +      break;
 1.12506 +    }
 1.12507    }
 1.12508    if( sqlite3CheckCollSeq(pParse, pColl) ){ 
 1.12509      pColl = 0;
 1.12510 @@ -75521,12 +74016,10 @@
 1.12511  ){
 1.12512    CollSeq *pColl;
 1.12513    assert( pLeft );
 1.12514 -  if( pLeft->flags & EP_ExpCollate ){
 1.12515 -    assert( pLeft->pColl );
 1.12516 -    pColl = pLeft->pColl;
 1.12517 -  }else if( pRight && pRight->flags & EP_ExpCollate ){
 1.12518 -    assert( pRight->pColl );
 1.12519 -    pColl = pRight->pColl;
 1.12520 +  if( pLeft->flags & EP_Collate ){
 1.12521 +    pColl = sqlite3ExprCollSeq(pParse, pLeft);
 1.12522 +  }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
 1.12523 +    pColl = sqlite3ExprCollSeq(pParse, pRight);
 1.12524    }else{
 1.12525      pColl = sqlite3ExprCollSeq(pParse, pLeft);
 1.12526      if( !pColl ){
 1.12527 @@ -75756,17 +74249,11 @@
 1.12528    }else{
 1.12529      if( pRight ){
 1.12530        pRoot->pRight = pRight;
 1.12531 -      if( pRight->flags & EP_ExpCollate ){
 1.12532 -        pRoot->flags |= EP_ExpCollate;
 1.12533 -        pRoot->pColl = pRight->pColl;
 1.12534 -      }
 1.12535 +      pRoot->flags |= EP_Collate & pRight->flags;
 1.12536      }
 1.12537      if( pLeft ){
 1.12538        pRoot->pLeft = pLeft;
 1.12539 -      if( pLeft->flags & EP_ExpCollate ){
 1.12540 -        pRoot->flags |= EP_ExpCollate;
 1.12541 -        pRoot->pColl = pLeft->pColl;
 1.12542 -      }
 1.12543 +      pRoot->flags |= EP_Collate & pLeft->flags;
 1.12544      }
 1.12545      exprSetHeight(pRoot);
 1.12546    }
 1.12547 @@ -76024,7 +74511,7 @@
 1.12548      assert( !ExprHasProperty(p, EP_FromJoin) ); 
 1.12549      assert( (p->flags2 & EP2_MallocedToken)==0 );
 1.12550      assert( (p->flags2 & EP2_Irreducible)==0 );
 1.12551 -    if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
 1.12552 +    if( p->pLeft || p->pRight || p->x.pList ){
 1.12553        nSize = EXPR_REDUCEDSIZE | EP_Reduced;
 1.12554      }else{
 1.12555        nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
 1.12556 @@ -76232,6 +74719,7 @@
 1.12557      struct SrcList_item *pNewItem = &pNew->a[i];
 1.12558      struct SrcList_item *pOldItem = &p->a[i];
 1.12559      Table *pTab;
 1.12560 +    pNewItem->pSchema = pOldItem->pSchema;
 1.12561      pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
 1.12562      pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 1.12563      pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
 1.12564 @@ -76240,6 +74728,7 @@
 1.12565      pNewItem->addrFillSub = pOldItem->addrFillSub;
 1.12566      pNewItem->regReturn = pOldItem->regReturn;
 1.12567      pNewItem->isCorrelated = pOldItem->isCorrelated;
 1.12568 +    pNewItem->viaCoroutine = pOldItem->viaCoroutine;
 1.12569      pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
 1.12570      pNewItem->notIndexed = pOldItem->notIndexed;
 1.12571      pNewItem->pIndex = pOldItem->pIndex;
 1.12572 @@ -76722,12 +75211,16 @@
 1.12573  
 1.12574  /*
 1.12575  ** This function is used by the implementation of the IN (...) operator.
 1.12576 -** It's job is to find or create a b-tree structure that may be used
 1.12577 -** either to test for membership of the (...) set or to iterate through
 1.12578 -** its members, skipping duplicates.
 1.12579 -**
 1.12580 -** The index of the cursor opened on the b-tree (database table, database index 
 1.12581 -** or ephermal table) is stored in pX->iTable before this function returns.
 1.12582 +** The pX parameter is the expression on the RHS of the IN operator, which
 1.12583 +** might be either a list of expressions or a subquery.
 1.12584 +**
 1.12585 +** The job of this routine is to find or create a b-tree object that can
 1.12586 +** be used either to test for membership in the RHS set or to iterate through
 1.12587 +** all members of the RHS set, skipping duplicates.
 1.12588 +**
 1.12589 +** A cursor is opened on the b-tree object that the RHS of the IN operator
 1.12590 +** and pX->iTable is set to the index of that cursor.
 1.12591 +**
 1.12592  ** The returned value of this function indicates the b-tree type, as follows:
 1.12593  **
 1.12594  **   IN_INDEX_ROWID - The cursor was opened on a database table.
 1.12595 @@ -76735,11 +75228,16 @@
 1.12596  **   IN_INDEX_EPH -   The cursor was opened on a specially created and
 1.12597  **                    populated epheremal table.
 1.12598  **
 1.12599 -** An existing b-tree may only be used if the SELECT is of the simple
 1.12600 -** form:
 1.12601 +** An existing b-tree might be used if the RHS expression pX is a simple
 1.12602 +** subquery such as:
 1.12603  **
 1.12604  **     SELECT <column> FROM <table>
 1.12605  **
 1.12606 +** If the RHS of the IN operator is a list or a more complex subquery, then
 1.12607 +** an ephemeral table might need to be generated from the RHS and then
 1.12608 +** pX->iTable made to point to the ephermeral table instead of an
 1.12609 +** existing table.  
 1.12610 +**
 1.12611  ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
 1.12612  ** through the set members, skipping any duplicates. In this case an
 1.12613  ** epheremal table must be used unless the selected <column> is guaranteed
 1.12614 @@ -76835,8 +75333,7 @@
 1.12615        ** comparison is the same as the affinity of the column. If
 1.12616        ** it is not, it is not possible to use any index.
 1.12617        */
 1.12618 -      char aff = comparisonAffinity(pX);
 1.12619 -      int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
 1.12620 +      int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
 1.12621  
 1.12622        for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
 1.12623          if( (pIdx->aiColumn[0]==iCol)
 1.12624 @@ -76964,6 +75461,7 @@
 1.12625      case TK_IN: {
 1.12626        char affinity;              /* Affinity of the LHS of the IN */
 1.12627        KeyInfo keyInfo;            /* Keyinfo for the generated table */
 1.12628 +      static u8 sortOrder = 0;    /* Fake aSortOrder for keyInfo */
 1.12629        int addr;                   /* Address of OP_OpenEphemeral instruction */
 1.12630        Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
 1.12631  
 1.12632 @@ -76991,6 +75489,7 @@
 1.12633        if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 1.12634        memset(&keyInfo, 0, sizeof(keyInfo));
 1.12635        keyInfo.nField = 1;
 1.12636 +      keyInfo.aSortOrder = &sortOrder;
 1.12637  
 1.12638        if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 1.12639          /* Case 1:     expr IN (SELECT ...)
 1.12640 @@ -77003,7 +75502,7 @@
 1.12641  
 1.12642          assert( !isRowid );
 1.12643          sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
 1.12644 -        dest.affinity = (u8)affinity;
 1.12645 +        dest.affSdst = (u8)affinity;
 1.12646          assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
 1.12647          pExpr->x.pSelect->iLimit = 0;
 1.12648          if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
 1.12649 @@ -77031,6 +75530,7 @@
 1.12650            affinity = SQLITE_AFF_NONE;
 1.12651          }
 1.12652          keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 1.12653 +        keyInfo.aSortOrder = &sortOrder;
 1.12654  
 1.12655          /* Loop through each expression in <exprlist>. */
 1.12656          r1 = sqlite3GetTempReg(pParse);
 1.12657 @@ -77096,11 +75596,11 @@
 1.12658        sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
 1.12659        if( pExpr->op==TK_SELECT ){
 1.12660          dest.eDest = SRT_Mem;
 1.12661 -        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
 1.12662 +        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
 1.12663          VdbeComment((v, "Init subquery result"));
 1.12664        }else{
 1.12665          dest.eDest = SRT_Exists;
 1.12666 -        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
 1.12667 +        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
 1.12668          VdbeComment((v, "Init EXISTS result"));
 1.12669        }
 1.12670        sqlite3ExprDelete(pParse->db, pSel->pLimit);
 1.12671 @@ -77110,7 +75610,7 @@
 1.12672        if( sqlite3Select(pParse, pSel, &dest) ){
 1.12673          return 0;
 1.12674        }
 1.12675 -      rReg = dest.iParm;
 1.12676 +      rReg = dest.iSDParm;
 1.12677        ExprSetIrreducible(pExpr);
 1.12678        break;
 1.12679      }
 1.12680 @@ -77357,7 +75857,7 @@
 1.12681    ** for testing only - to verify that SQLite always gets the same answer
 1.12682    ** with and without the column cache.
 1.12683    */
 1.12684 -  if( pParse->db->flags & SQLITE_ColumnCache ) return;
 1.12685 +  if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
 1.12686  
 1.12687    /* First replace any existing entry.
 1.12688    **
 1.12689 @@ -77554,8 +76054,8 @@
 1.12690  SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
 1.12691    int i;
 1.12692    struct yColCache *p;
 1.12693 -  if( NEVER(iFrom==iTo) ) return;
 1.12694 -  sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
 1.12695 +  assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
 1.12696 +  sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg-1);
 1.12697    for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.12698      int x = p->iReg;
 1.12699      if( x>=iFrom && x<iFrom+nReg ){
 1.12700 @@ -77564,18 +76064,6 @@
 1.12701    }
 1.12702  }
 1.12703  
 1.12704 -/*
 1.12705 -** Generate code to copy content from registers iFrom...iFrom+nReg-1
 1.12706 -** over to iTo..iTo+nReg-1.
 1.12707 -*/
 1.12708 -SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
 1.12709 -  int i;
 1.12710 -  if( NEVER(iFrom==iTo) ) return;
 1.12711 -  for(i=0; i<nReg; i++){
 1.12712 -    sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
 1.12713 -  }
 1.12714 -}
 1.12715 -
 1.12716  #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
 1.12717  /*
 1.12718  ** Return true if any register in the range iFrom..iTo (inclusive)
 1.12719 @@ -78047,6 +76535,7 @@
 1.12720        sqlite3ReleaseTempReg(pParse, r4);
 1.12721        break;
 1.12722      }
 1.12723 +    case TK_COLLATE: 
 1.12724      case TK_UPLUS: {
 1.12725        inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 1.12726        break;
 1.12727 @@ -78416,6 +76905,12 @@
 1.12728      case TK_ISNULL:  zUniOp = "ISNULL"; break;
 1.12729      case TK_NOTNULL: zUniOp = "NOTNULL"; break;
 1.12730  
 1.12731 +    case TK_COLLATE: {
 1.12732 +      sqlite3ExplainExpr(pOut, pExpr->pLeft);
 1.12733 +      sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
 1.12734 +      break;
 1.12735 +    }
 1.12736 +
 1.12737      case TK_AGG_FUNCTION:
 1.12738      case TK_CONST_FUNC:
 1.12739      case TK_FUNCTION: {
 1.12740 @@ -78425,9 +76920,12 @@
 1.12741        }else{
 1.12742          pFarg = pExpr->x.pList;
 1.12743        }
 1.12744 -      sqlite3ExplainPrintf(pOut, "%sFUNCTION:%s(",
 1.12745 -                           op==TK_AGG_FUNCTION ? "AGG_" : "",
 1.12746 -                           pExpr->u.zToken);
 1.12747 +      if( op==TK_AGG_FUNCTION ){
 1.12748 +        sqlite3ExplainPrintf(pOut, "AGG_FUNCTION%d:%s(",
 1.12749 +                             pExpr->op2, pExpr->u.zToken);
 1.12750 +      }else{
 1.12751 +        sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken);
 1.12752 +      }
 1.12753        if( pFarg ){
 1.12754          sqlite3ExplainExprList(pOut, pFarg);
 1.12755        }
 1.12756 @@ -78631,6 +77129,9 @@
 1.12757      case TK_REGISTER: {
 1.12758        return WRC_Prune;
 1.12759      }
 1.12760 +    case TK_COLLATE: {
 1.12761 +      return WRC_Continue;
 1.12762 +    }
 1.12763      case TK_FUNCTION:
 1.12764      case TK_AGG_FUNCTION:
 1.12765      case TK_CONST_FUNC: {
 1.12766 @@ -78652,9 +77153,11 @@
 1.12767    }
 1.12768    if( isAppropriateForFactoring(pExpr) ){
 1.12769      int r1 = ++pParse->nMem;
 1.12770 -    int r2;
 1.12771 -    r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
 1.12772 -    if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1);
 1.12773 +    int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
 1.12774 +    /* If r2!=r1, it means that register r1 is never used.  That is harmless
 1.12775 +    ** but suboptimal, so we want to know about the situation to fix it.
 1.12776 +    ** Hence the following assert: */
 1.12777 +    assert( r2==r1 );
 1.12778      pExpr->op2 = pExpr->op;
 1.12779      pExpr->op = TK_REGISTER;
 1.12780      pExpr->iTable = r2;
 1.12781 @@ -78682,7 +77185,7 @@
 1.12782  SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
 1.12783    Walker w;
 1.12784    if( pParse->cookieGoto ) return;
 1.12785 -  if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return;
 1.12786 +  if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
 1.12787    w.xExprCallback = evalConstExpr;
 1.12788    w.xSelectCallback = 0;
 1.12789    w.pParse = pParse;
 1.12790 @@ -79071,7 +77574,15 @@
 1.12791      return 2;
 1.12792    }
 1.12793    if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
 1.12794 -  if( pA->op!=pB->op ) return 2;
 1.12795 +  if( pA->op!=pB->op ){
 1.12796 +    if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB)<2 ){
 1.12797 +      return 1;
 1.12798 +    }
 1.12799 +    if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft)<2 ){
 1.12800 +      return 1;
 1.12801 +    }
 1.12802 +    return 2;
 1.12803 +  }
 1.12804    if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
 1.12805    if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
 1.12806    if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
 1.12807 @@ -79083,11 +77594,9 @@
 1.12808    }else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){
 1.12809      if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
 1.12810      if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
 1.12811 -      return 2;
 1.12812 -    }
 1.12813 -  }
 1.12814 -  if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
 1.12815 -  if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
 1.12816 +      return pA->op==TK_COLLATE ? 1 : 2;
 1.12817 +    }
 1.12818 +  }
 1.12819    return 0;
 1.12820  }
 1.12821  
 1.12822 @@ -79118,38 +77627,60 @@
 1.12823  }
 1.12824  
 1.12825  /*
 1.12826 -** This is the expression callback for sqlite3FunctionUsesOtherSrc().
 1.12827 -**
 1.12828 -** Determine if an expression references any table other than one of the
 1.12829 -** tables in pWalker->u.pSrcList and abort if it does.
 1.12830 -*/
 1.12831 -static int exprUsesOtherSrc(Walker *pWalker, Expr *pExpr){
 1.12832 -  if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
 1.12833 +** An instance of the following structure is used by the tree walker
 1.12834 +** to count references to table columns in the arguments of an 
 1.12835 +** aggregate function, in order to implement the
 1.12836 +** sqlite3FunctionThisSrc() routine.
 1.12837 +*/
 1.12838 +struct SrcCount {
 1.12839 +  SrcList *pSrc;   /* One particular FROM clause in a nested query */
 1.12840 +  int nThis;       /* Number of references to columns in pSrcList */
 1.12841 +  int nOther;      /* Number of references to columns in other FROM clauses */
 1.12842 +};
 1.12843 +
 1.12844 +/*
 1.12845 +** Count the number of references to columns.
 1.12846 +*/
 1.12847 +static int exprSrcCount(Walker *pWalker, Expr *pExpr){
 1.12848 +  /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
 1.12849 +  ** is always called before sqlite3ExprAnalyzeAggregates() and so the
 1.12850 +  ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN.  If
 1.12851 +  ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
 1.12852 +  ** NEVER() will need to be removed. */
 1.12853 +  if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
 1.12854      int i;
 1.12855 -    SrcList *pSrc = pWalker->u.pSrcList;
 1.12856 +    struct SrcCount *p = pWalker->u.pSrcCount;
 1.12857 +    SrcList *pSrc = p->pSrc;
 1.12858      for(i=0; i<pSrc->nSrc; i++){
 1.12859 -      if( pExpr->iTable==pSrc->a[i].iCursor ) return WRC_Continue;
 1.12860 -    }
 1.12861 -    return WRC_Abort;
 1.12862 -  }else{
 1.12863 -    return WRC_Continue;
 1.12864 -  }
 1.12865 -}
 1.12866 -
 1.12867 -/*
 1.12868 -** Determine if any of the arguments to the pExpr Function references
 1.12869 -** any SrcList other than pSrcList.  Return true if they do.  Return
 1.12870 -** false if pExpr has no argument or has only constant arguments or
 1.12871 -** only references tables named in pSrcList.
 1.12872 -*/
 1.12873 -static int sqlite3FunctionUsesOtherSrc(Expr *pExpr, SrcList *pSrcList){
 1.12874 +      if( pExpr->iTable==pSrc->a[i].iCursor ) break;
 1.12875 +    }
 1.12876 +    if( i<pSrc->nSrc ){
 1.12877 +      p->nThis++;
 1.12878 +    }else{
 1.12879 +      p->nOther++;
 1.12880 +    }
 1.12881 +  }
 1.12882 +  return WRC_Continue;
 1.12883 +}
 1.12884 +
 1.12885 +/*
 1.12886 +** Determine if any of the arguments to the pExpr Function reference
 1.12887 +** pSrcList.  Return true if they do.  Also return true if the function
 1.12888 +** has no arguments or has only constant arguments.  Return false if pExpr
 1.12889 +** references columns but not columns of tables found in pSrcList.
 1.12890 +*/
 1.12891 +SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
 1.12892    Walker w;
 1.12893 +  struct SrcCount cnt;
 1.12894    assert( pExpr->op==TK_AGG_FUNCTION );
 1.12895    memset(&w, 0, sizeof(w));
 1.12896 -  w.xExprCallback = exprUsesOtherSrc;
 1.12897 -  w.u.pSrcList = pSrcList;
 1.12898 -  if( sqlite3WalkExprList(&w, pExpr->x.pList)!=WRC_Continue ) return 1;
 1.12899 -  return 0;
 1.12900 +  w.xExprCallback = exprSrcCount;
 1.12901 +  w.u.pSrcCount = &cnt;
 1.12902 +  cnt.pSrc = pSrcList;
 1.12903 +  cnt.nThis = 0;
 1.12904 +  cnt.nOther = 0;
 1.12905 +  sqlite3WalkExprList(&w, pExpr->x.pList);
 1.12906 +  return cnt.nThis>0 || cnt.nOther==0;
 1.12907  }
 1.12908  
 1.12909  /*
 1.12910 @@ -79268,7 +77799,7 @@
 1.12911      }
 1.12912      case TK_AGG_FUNCTION: {
 1.12913        if( (pNC->ncFlags & NC_InAggFunc)==0
 1.12914 -       && !sqlite3FunctionUsesOtherSrc(pExpr, pSrcList)
 1.12915 +       && pWalker->walkerDepth==pExpr->op2
 1.12916        ){
 1.12917          /* Check to see if pExpr is a duplicate of another aggregate 
 1.12918          ** function that is already in the pAggInfo structure
 1.12919 @@ -79306,8 +77837,10 @@
 1.12920          ExprSetIrreducible(pExpr);
 1.12921          pExpr->iAgg = (i16)i;
 1.12922          pExpr->pAggInfo = pAggInfo;
 1.12923 -      }
 1.12924 -      return WRC_Prune;
 1.12925 +        return WRC_Prune;
 1.12926 +      }else{
 1.12927 +        return WRC_Continue;
 1.12928 +      }
 1.12929      }
 1.12930    }
 1.12931    return WRC_Continue;
 1.12932 @@ -79319,9 +77852,10 @@
 1.12933  }
 1.12934  
 1.12935  /*
 1.12936 -** Analyze the given expression looking for aggregate functions and
 1.12937 -** for variables that need to be added to the pParse->aAgg[] array.
 1.12938 -** Make additional entries to the pParse->aAgg[] array as necessary.
 1.12939 +** Analyze the pExpr expression looking for aggregate functions and
 1.12940 +** for variables that need to be added to AggInfo object that pNC->pAggInfo
 1.12941 +** points to.  Additional entries are made on the AggInfo object as
 1.12942 +** necessary.
 1.12943  **
 1.12944  ** This routine should only be called after the expression has been
 1.12945  ** analyzed by sqlite3ResolveExprNames().
 1.12946 @@ -79834,7 +78368,7 @@
 1.12947    assert( pSrc->nSrc==1 );
 1.12948    assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 1.12949  
 1.12950 -  pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
 1.12951 +  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
 1.12952    if( !pTab ) goto exit_rename_table;
 1.12953    iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 1.12954    zDb = db->aDb[iDb].zName;
 1.12955 @@ -80084,7 +78618,7 @@
 1.12956    ** If there is a NOT NULL constraint, then the default value for the
 1.12957    ** column must not be NULL.
 1.12958    */
 1.12959 -  if( pCol->isPrimKey ){
 1.12960 +  if( pCol->colFlags & COLFLAG_PRIMKEY ){
 1.12961      sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
 1.12962      return;
 1.12963    }
 1.12964 @@ -80177,7 +78711,7 @@
 1.12965    assert( pParse->pNewTable==0 );
 1.12966    assert( sqlite3BtreeHoldsAllMutexes(db) );
 1.12967    if( db->mallocFailed ) goto exit_begin_add_column;
 1.12968 -  pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
 1.12969 +  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
 1.12970    if( !pTab ) goto exit_begin_add_column;
 1.12971  
 1.12972  #ifndef SQLITE_OMIT_VIRTUALTABLE
 1.12973 @@ -80424,7 +78958,7 @@
 1.12974            "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
 1.12975        );
 1.12976        aRoot[i] = pParse->regRoot;
 1.12977 -      aCreateTbl[i] = 1;
 1.12978 +      aCreateTbl[i] = OPFLAG_P2ISREG;
 1.12979      }else{
 1.12980        /* The table already exists. If zWhere is not NULL, delete all entries 
 1.12981        ** associated with the table zWhere. If zWhere is NULL, delete the
 1.12982 @@ -80504,12 +79038,11 @@
 1.12983    nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
 1.12984    mxSample = sqlite3_value_int(argv[1]);
 1.12985    n = sizeof(*p) + sizeof(p->a[0])*mxSample;
 1.12986 -  p = sqlite3_malloc( n );
 1.12987 +  p = sqlite3MallocZero( n );
 1.12988    if( p==0 ){
 1.12989      sqlite3_result_error_nomem(context);
 1.12990      return;
 1.12991    }
 1.12992 -  memset(p, 0, n);
 1.12993    p->a = (struct Stat3Sample*)&p[1];
 1.12994    p->nRow = nRow;
 1.12995    p->mxSample = mxSample;
 1.12996 @@ -81809,6 +80342,7 @@
 1.12997    assert( db->nDb>iDb );
 1.12998    pFix->pParse = pParse;
 1.12999    pFix->zDb = db->aDb[iDb].zName;
 1.13000 +  pFix->pSchema = db->aDb[iDb].pSchema;
 1.13001    pFix->zType = zType;
 1.13002    pFix->pName = pName;
 1.13003    return 1;
 1.13004 @@ -81839,14 +80373,15 @@
 1.13005    if( NEVER(pList==0) ) return 0;
 1.13006    zDb = pFix->zDb;
 1.13007    for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
 1.13008 -    if( pItem->zDatabase==0 ){
 1.13009 -      pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
 1.13010 -    }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
 1.13011 +    if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
 1.13012        sqlite3ErrorMsg(pFix->pParse,
 1.13013           "%s %T cannot reference objects in database %s",
 1.13014           pFix->zType, pFix->pName, pItem->zDatabase);
 1.13015        return 1;
 1.13016      }
 1.13017 +    sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
 1.13018 +    pItem->zDatabase = 0;
 1.13019 +    pItem->pSchema = pFix->pSchema;
 1.13020  #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
 1.13021      if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
 1.13022      if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
 1.13023 @@ -82312,6 +80847,7 @@
 1.13024    sqlite3 *db;
 1.13025    Vdbe *v;
 1.13026  
 1.13027 +  assert( pParse->pToplevel==0 );
 1.13028    db = pParse->db;
 1.13029    if( db->mallocFailed ) return;
 1.13030    if( pParse->nested ) return;
 1.13031 @@ -82505,6 +81041,31 @@
 1.13032  }
 1.13033  
 1.13034  /*
 1.13035 +** Locate the table identified by *p.
 1.13036 +**
 1.13037 +** This is a wrapper around sqlite3LocateTable(). The difference between
 1.13038 +** sqlite3LocateTable() and this function is that this function restricts
 1.13039 +** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
 1.13040 +** non-NULL if it is part of a view or trigger program definition. See
 1.13041 +** sqlite3FixSrcList() for details.
 1.13042 +*/
 1.13043 +SQLITE_PRIVATE Table *sqlite3LocateTableItem(
 1.13044 +  Parse *pParse, 
 1.13045 +  int isView, 
 1.13046 +  struct SrcList_item *p
 1.13047 +){
 1.13048 +  const char *zDb;
 1.13049 +  assert( p->pSchema==0 || p->zDatabase==0 );
 1.13050 +  if( p->pSchema ){
 1.13051 +    int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
 1.13052 +    zDb = pParse->db->aDb[iDb].zName;
 1.13053 +  }else{
 1.13054 +    zDb = p->zDatabase;
 1.13055 +  }
 1.13056 +  return sqlite3LocateTable(pParse, isView, p->zName, zDb);
 1.13057 +}
 1.13058 +
 1.13059 +/*
 1.13060  ** Locate the in-memory structure that describes 
 1.13061  ** a particular index given the name of that index
 1.13062  ** and the name of the database that contains the index.
 1.13063 @@ -82719,7 +81280,7 @@
 1.13064      if( !db || db->pnBytesFreed==0 ){
 1.13065        char *zName = pIndex->zName; 
 1.13066        TESTONLY ( Index *pOld = ) sqlite3HashInsert(
 1.13067 -	  &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
 1.13068 +         &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
 1.13069        );
 1.13070        assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
 1.13071        assert( pOld==pIndex || pOld==0 );
 1.13072 @@ -83354,7 +81915,7 @@
 1.13073    pTab->tabFlags |= TF_HasPrimaryKey;
 1.13074    if( pList==0 ){
 1.13075      iCol = pTab->nCol - 1;
 1.13076 -    pTab->aCol[iCol].isPrimKey = 1;
 1.13077 +    pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
 1.13078    }else{
 1.13079      for(i=0; i<pList->nExpr; i++){
 1.13080        for(iCol=0; iCol<pTab->nCol; iCol++){
 1.13081 @@ -83363,7 +81924,7 @@
 1.13082          }
 1.13083        }
 1.13084        if( iCol<pTab->nCol ){
 1.13085 -        pTab->aCol[iCol].isPrimKey = 1;
 1.13086 +        pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
 1.13087        }
 1.13088      }
 1.13089      if( pList->nExpr>1 ) iCol = -1;
 1.13090 @@ -83480,10 +82041,7 @@
 1.13091  
 1.13092    pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
 1.13093    if( !initbusy && (!pColl || !pColl->xCmp) ){
 1.13094 -    pColl = sqlite3GetCollSeq(db, enc, pColl, zName);
 1.13095 -    if( !pColl ){
 1.13096 -      sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
 1.13097 -    }
 1.13098 +    pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
 1.13099    }
 1.13100  
 1.13101    return pColl;
 1.13102 @@ -83766,7 +82324,7 @@
 1.13103  
 1.13104        assert(pParse->nTab==1);
 1.13105        sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
 1.13106 -      sqlite3VdbeChangeP5(v, 1);
 1.13107 +      sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
 1.13108        pParse->nTab = 2;
 1.13109        sqlite3SelectDestInit(&dest, SRT_Table, 1);
 1.13110        sqlite3Select(pParse, pSelect, &dest);
 1.13111 @@ -84182,6 +82740,7 @@
 1.13112        return;
 1.13113      }else{
 1.13114        int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 1.13115 +      assert( iDb>=0 && iDb<pParse->db->nDb );
 1.13116        destroyRootPage(pParse, iLargest, iDb);
 1.13117        iDestroyed = iLargest;
 1.13118      }
 1.13119 @@ -84299,8 +82858,7 @@
 1.13120    assert( pParse->nErr==0 );
 1.13121    assert( pName->nSrc==1 );
 1.13122    if( noErr ) db->suppressErr++;
 1.13123 -  pTab = sqlite3LocateTable(pParse, isView, 
 1.13124 -                            pName->a[0].zName, pName->a[0].zDatabase);
 1.13125 +  pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
 1.13126    if( noErr ) db->suppressErr--;
 1.13127  
 1.13128    if( pTab==0 ){
 1.13129 @@ -84582,9 +83140,7 @@
 1.13130    pKey = sqlite3IndexKeyinfo(pParse, pIndex);
 1.13131    sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
 1.13132                      (char *)pKey, P4_KEYINFO_HANDOFF);
 1.13133 -  if( memRootPage>=0 ){
 1.13134 -    sqlite3VdbeChangeP5(v, 1);
 1.13135 -  }
 1.13136 +  sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
 1.13137  
 1.13138  #ifndef SQLITE_OMIT_MERGE_SORT
 1.13139    /* Open the sorter cursor if we are to use one. */
 1.13140 @@ -84723,7 +83279,7 @@
 1.13141      assert( pName && pName->z );
 1.13142  
 1.13143  #ifndef SQLITE_OMIT_TEMPDB
 1.13144 -    /* If the index name was unqualified, check if the the table
 1.13145 +    /* If the index name was unqualified, check if the table
 1.13146      ** is a temp table. If so, set the database to 1. Do not do this
 1.13147      ** if initialising a database schema.
 1.13148      */
 1.13149 @@ -84742,9 +83298,9 @@
 1.13150        ** sqlite3FixSrcList can never fail. */
 1.13151        assert(0);
 1.13152      }
 1.13153 -    pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName, 
 1.13154 -        pTblName->a[0].zDatabase);
 1.13155 -    if( !pTab || db->mallocFailed ) goto exit_create_index;
 1.13156 +    pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
 1.13157 +    assert( db->mallocFailed==0 || pTab==0 );
 1.13158 +    if( pTab==0 ) goto exit_create_index;
 1.13159      assert( db->aDb[iDb].pSchema==pTab->pSchema );
 1.13160    }else{
 1.13161      assert( pName==0 );
 1.13162 @@ -84855,10 +83411,8 @@
 1.13163    for(i=0; i<pList->nExpr; i++){
 1.13164      Expr *pExpr = pList->a[i].pExpr;
 1.13165      if( pExpr ){
 1.13166 -      CollSeq *pColl = pExpr->pColl;
 1.13167 -      /* Either pColl!=0 or there was an OOM failure.  But if an OOM
 1.13168 -      ** failure we have quit before reaching this point. */
 1.13169 -      if( ALWAYS(pColl) ){
 1.13170 +      CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
 1.13171 +      if( pColl ){
 1.13172          nExtra += (1 + sqlite3Strlen30(pColl->zName));
 1.13173        }
 1.13174      }
 1.13175 @@ -84921,6 +83475,7 @@
 1.13176      const char *zColName = pListItem->zName;
 1.13177      Column *pTabCol;
 1.13178      int requestedSortOrder;
 1.13179 +    CollSeq *pColl;                /* Collating sequence */
 1.13180      char *zColl;                   /* Collation sequence name */
 1.13181  
 1.13182      for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
 1.13183 @@ -84933,14 +83488,11 @@
 1.13184        goto exit_create_index;
 1.13185      }
 1.13186      pIndex->aiColumn[i] = j;
 1.13187 -    /* Justification of the ALWAYS(pListItem->pExpr->pColl):  Because of
 1.13188 -    ** the way the "idxlist" non-terminal is constructed by the parser,
 1.13189 -    ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
 1.13190 -    ** must exist or else there must have been an OOM error.  But if there
 1.13191 -    ** was an OOM error, we would never reach this point. */
 1.13192 -    if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
 1.13193 +    if( pListItem->pExpr
 1.13194 +     && (pColl = sqlite3ExprCollSeq(pParse, pListItem->pExpr))!=0
 1.13195 +    ){
 1.13196        int nColl;
 1.13197 -      zColl = pListItem->pExpr->pColl->zName;
 1.13198 +      zColl = pColl->zName;
 1.13199        nColl = sqlite3Strlen30(zColl) + 1;
 1.13200        assert( nExtra>=nColl );
 1.13201        memcpy(zExtra, zColl, nColl);
 1.13202 @@ -85754,6 +84306,15 @@
 1.13203  SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
 1.13204    Parse *pToplevel = sqlite3ParseToplevel(pParse);
 1.13205  
 1.13206 +#ifndef SQLITE_OMIT_TRIGGER
 1.13207 +  if( pToplevel!=pParse ){
 1.13208 +    /* This branch is taken if a trigger is currently being coded. In this
 1.13209 +    ** case, set cookieGoto to a non-zero value to show that this function
 1.13210 +    ** has been called. This is used by the sqlite3ExprCodeConstants()
 1.13211 +    ** function. */
 1.13212 +    pParse->cookieGoto = -1;
 1.13213 +  }
 1.13214 +#endif
 1.13215    if( pToplevel->cookieGoto==0 ){
 1.13216      Vdbe *v = sqlite3GetVdbe(pToplevel);
 1.13217      if( v==0 ) return;  /* This only happens if there was a prior error */
 1.13218 @@ -86103,17 +84664,18 @@
 1.13219  **
 1.13220  ** The return value is either the collation sequence to be used in database
 1.13221  ** db for collation type name zName, length nName, or NULL, if no collation
 1.13222 -** sequence can be found.
 1.13223 +** sequence can be found.  If no collation is found, leave an error message.
 1.13224  **
 1.13225  ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
 1.13226  */
 1.13227  SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
 1.13228 -  sqlite3* db,          /* The database connection */
 1.13229 +  Parse *pParse,        /* Parsing context */
 1.13230    u8 enc,               /* The desired encoding for the collating sequence */
 1.13231    CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
 1.13232    const char *zName     /* Collating sequence name */
 1.13233  ){
 1.13234    CollSeq *p;
 1.13235 +  sqlite3 *db = pParse->db;
 1.13236  
 1.13237    p = pColl;
 1.13238    if( !p ){
 1.13239 @@ -86130,6 +84692,9 @@
 1.13240      p = 0;
 1.13241    }
 1.13242    assert( !p || p->xCmp );
 1.13243 +  if( p==0 ){
 1.13244 +    sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
 1.13245 +  }
 1.13246    return p;
 1.13247  }
 1.13248  
 1.13249 @@ -86148,10 +84713,8 @@
 1.13250    if( pColl ){
 1.13251      const char *zName = pColl->zName;
 1.13252      sqlite3 *db = pParse->db;
 1.13253 -    CollSeq *p = sqlite3GetCollSeq(db, ENC(db), pColl, zName);
 1.13254 +    CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
 1.13255      if( !p ){
 1.13256 -      sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
 1.13257 -      pParse->nErr++;
 1.13258        return SQLITE_ERROR;
 1.13259      }
 1.13260      assert( p==pColl );
 1.13261 @@ -86538,7 +85101,7 @@
 1.13262    struct SrcList_item *pItem = pSrc->a;
 1.13263    Table *pTab;
 1.13264    assert( pItem && pSrc->nSrc==1 );
 1.13265 -  pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
 1.13266 +  pTab = sqlite3LocateTableItem(pParse, 0, pItem);
 1.13267    sqlite3DeleteTable(pParse->db, pItem->pTab);
 1.13268    pItem->pTab = pTab;
 1.13269    if( pTab ){
 1.13270 @@ -86618,6 +85181,7 @@
 1.13271        sqlite3SelectDelete(db, pDup);
 1.13272      }
 1.13273      pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
 1.13274 +    if( pDup ) pDup->selFlags |= SF_Materialize;
 1.13275    }
 1.13276    sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
 1.13277    sqlite3Select(pParse, pDup, &dest);
 1.13278 @@ -86877,7 +85441,7 @@
 1.13279      */
 1.13280      sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
 1.13281      pWInfo = sqlite3WhereBegin(
 1.13282 -        pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK
 1.13283 +        pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0
 1.13284      );
 1.13285      if( pWInfo==0 ) goto delete_from_cleanup;
 1.13286      regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid, 0);
 1.13287 @@ -87144,7 +85708,9 @@
 1.13288    }
 1.13289    if( doMakeRec ){
 1.13290      const char *zAff;
 1.13291 -    if( pTab->pSelect || (pParse->db->flags & SQLITE_IdxRealAsInt)!=0 ){
 1.13292 +    if( pTab->pSelect
 1.13293 +     || OptimizationDisabled(pParse->db, SQLITE_IdxRealAsInt)
 1.13294 +    ){
 1.13295        zAff = 0;
 1.13296      }else{
 1.13297        zAff = sqlite3IndexAffinityStr(v, pIdx);
 1.13298 @@ -87327,6 +85893,56 @@
 1.13299  }
 1.13300  
 1.13301  /*
 1.13302 +** Implementation of the instr() function.
 1.13303 +**
 1.13304 +** instr(haystack,needle) finds the first occurrence of needle
 1.13305 +** in haystack and returns the number of previous characters plus 1,
 1.13306 +** or 0 if needle does not occur within haystack.
 1.13307 +**
 1.13308 +** If both haystack and needle are BLOBs, then the result is one more than
 1.13309 +** the number of bytes in haystack prior to the first occurrence of needle,
 1.13310 +** or 0 if needle never occurs in haystack.
 1.13311 +*/
 1.13312 +static void instrFunc(
 1.13313 +  sqlite3_context *context,
 1.13314 +  int argc,
 1.13315 +  sqlite3_value **argv
 1.13316 +){
 1.13317 +  const unsigned char *zHaystack;
 1.13318 +  const unsigned char *zNeedle;
 1.13319 +  int nHaystack;
 1.13320 +  int nNeedle;
 1.13321 +  int typeHaystack, typeNeedle;
 1.13322 +  int N = 1;
 1.13323 +  int isText;
 1.13324 +
 1.13325 +  UNUSED_PARAMETER(argc);
 1.13326 +  typeHaystack = sqlite3_value_type(argv[0]);
 1.13327 +  typeNeedle = sqlite3_value_type(argv[1]);
 1.13328 +  if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
 1.13329 +  nHaystack = sqlite3_value_bytes(argv[0]);
 1.13330 +  nNeedle = sqlite3_value_bytes(argv[1]);
 1.13331 +  if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
 1.13332 +    zHaystack = sqlite3_value_blob(argv[0]);
 1.13333 +    zNeedle = sqlite3_value_blob(argv[1]);
 1.13334 +    isText = 0;
 1.13335 +  }else{
 1.13336 +    zHaystack = sqlite3_value_text(argv[0]);
 1.13337 +    zNeedle = sqlite3_value_text(argv[1]);
 1.13338 +    isText = 1;
 1.13339 +  }
 1.13340 +  while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
 1.13341 +    N++;
 1.13342 +    do{
 1.13343 +      nHaystack--;
 1.13344 +      zHaystack++;
 1.13345 +    }while( isText && (zHaystack[0]&0xc0)==0x80 );
 1.13346 +  }
 1.13347 +  if( nNeedle>nHaystack ) N = 0;
 1.13348 +  sqlite3_result_int(context, N);
 1.13349 +}
 1.13350 +
 1.13351 +/*
 1.13352  ** Implementation of the substr() function.
 1.13353  **
 1.13354  ** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
 1.13355 @@ -87525,33 +86141,14 @@
 1.13356    }
 1.13357  }
 1.13358  
 1.13359 -
 1.13360 -#if 0  /* This function is never used. */
 1.13361 -/*
 1.13362 -** The COALESCE() and IFNULL() functions used to be implemented as shown
 1.13363 -** here.  But now they are implemented as VDBE code so that unused arguments
 1.13364 -** do not have to be computed.  This legacy implementation is retained as
 1.13365 -** comment.
 1.13366 -*/
 1.13367 -/*
 1.13368 -** Implementation of the IFNULL(), NVL(), and COALESCE() functions.  
 1.13369 -** All three do the same thing.  They return the first non-NULL
 1.13370 -** argument.
 1.13371 -*/
 1.13372 -static void ifnullFunc(
 1.13373 -  sqlite3_context *context,
 1.13374 -  int argc,
 1.13375 -  sqlite3_value **argv
 1.13376 -){
 1.13377 -  int i;
 1.13378 -  for(i=0; i<argc; i++){
 1.13379 -    if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
 1.13380 -      sqlite3_result_value(context, argv[i]);
 1.13381 -      break;
 1.13382 -    }
 1.13383 -  }
 1.13384 -}
 1.13385 -#endif /* NOT USED */
 1.13386 +/*
 1.13387 +** The COALESCE() and IFNULL() functions are implemented as VDBE code so
 1.13388 +** that unused argument values do not have to be computed.  However, we
 1.13389 +** still need some kind of function implementation for this routines in
 1.13390 +** the function table.  That function implementation will never be called
 1.13391 +** so it doesn't matter what the implementation is.  We might as well use
 1.13392 +** the "version()" function as a substitute.
 1.13393 +*/
 1.13394  #define ifnullFunc versionFunc   /* Substitute function - never called */
 1.13395  
 1.13396  /*
 1.13397 @@ -87670,7 +86267,7 @@
 1.13398  ** whereas only characters less than 0x80 do in ASCII.
 1.13399  */
 1.13400  #if defined(SQLITE_EBCDIC)
 1.13401 -# define sqlite3Utf8Read(A,C)  (*(A++))
 1.13402 +# define sqlite3Utf8Read(A)    (*((*A)++))
 1.13403  # define GlogUpperToLower(A)   A = sqlite3UpperToLower[A]
 1.13404  #else
 1.13405  # define GlogUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
 1.13406 @@ -87727,18 +86324,18 @@
 1.13407    u8 noCase = pInfo->noCase; 
 1.13408    int prevEscape = 0;     /* True if the previous character was 'escape' */
 1.13409  
 1.13410 -  while( (c = sqlite3Utf8Read(zPattern,&zPattern))!=0 ){
 1.13411 -    if( !prevEscape && c==matchAll ){
 1.13412 -      while( (c=sqlite3Utf8Read(zPattern,&zPattern)) == matchAll
 1.13413 +  while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
 1.13414 +    if( c==matchAll && !prevEscape ){
 1.13415 +      while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
 1.13416                 || c == matchOne ){
 1.13417 -        if( c==matchOne && sqlite3Utf8Read(zString, &zString)==0 ){
 1.13418 +        if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
 1.13419            return 0;
 1.13420          }
 1.13421        }
 1.13422        if( c==0 ){
 1.13423          return 1;
 1.13424        }else if( c==esc ){
 1.13425 -        c = sqlite3Utf8Read(zPattern, &zPattern);
 1.13426 +        c = sqlite3Utf8Read(&zPattern);
 1.13427          if( c==0 ){
 1.13428            return 0;
 1.13429          }
 1.13430 @@ -87750,25 +86347,25 @@
 1.13431          }
 1.13432          return *zString!=0;
 1.13433        }
 1.13434 -      while( (c2 = sqlite3Utf8Read(zString,&zString))!=0 ){
 1.13435 +      while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
 1.13436          if( noCase ){
 1.13437            GlogUpperToLower(c2);
 1.13438            GlogUpperToLower(c);
 1.13439            while( c2 != 0 && c2 != c ){
 1.13440 -            c2 = sqlite3Utf8Read(zString, &zString);
 1.13441 +            c2 = sqlite3Utf8Read(&zString);
 1.13442              GlogUpperToLower(c2);
 1.13443            }
 1.13444          }else{
 1.13445            while( c2 != 0 && c2 != c ){
 1.13446 -            c2 = sqlite3Utf8Read(zString, &zString);
 1.13447 +            c2 = sqlite3Utf8Read(&zString);
 1.13448            }
 1.13449          }
 1.13450          if( c2==0 ) return 0;
 1.13451          if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
 1.13452        }
 1.13453        return 0;
 1.13454 -    }else if( !prevEscape && c==matchOne ){
 1.13455 -      if( sqlite3Utf8Read(zString, &zString)==0 ){
 1.13456 +    }else if( c==matchOne && !prevEscape ){
 1.13457 +      if( sqlite3Utf8Read(&zString)==0 ){
 1.13458          return 0;
 1.13459        }
 1.13460      }else if( c==matchSet ){
 1.13461 @@ -87776,20 +86373,20 @@
 1.13462        assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
 1.13463        seen = 0;
 1.13464        invert = 0;
 1.13465 -      c = sqlite3Utf8Read(zString, &zString);
 1.13466 +      c = sqlite3Utf8Read(&zString);
 1.13467        if( c==0 ) return 0;
 1.13468 -      c2 = sqlite3Utf8Read(zPattern, &zPattern);
 1.13469 +      c2 = sqlite3Utf8Read(&zPattern);
 1.13470        if( c2=='^' ){
 1.13471          invert = 1;
 1.13472 -        c2 = sqlite3Utf8Read(zPattern, &zPattern);
 1.13473 +        c2 = sqlite3Utf8Read(&zPattern);
 1.13474        }
 1.13475        if( c2==']' ){
 1.13476          if( c==']' ) seen = 1;
 1.13477 -        c2 = sqlite3Utf8Read(zPattern, &zPattern);
 1.13478 +        c2 = sqlite3Utf8Read(&zPattern);
 1.13479        }
 1.13480        while( c2 && c2!=']' ){
 1.13481          if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
 1.13482 -          c2 = sqlite3Utf8Read(zPattern, &zPattern);
 1.13483 +          c2 = sqlite3Utf8Read(&zPattern);
 1.13484            if( c>=prior_c && c<=c2 ) seen = 1;
 1.13485            prior_c = 0;
 1.13486          }else{
 1.13487 @@ -87798,7 +86395,7 @@
 1.13488            }
 1.13489            prior_c = c2;
 1.13490          }
 1.13491 -        c2 = sqlite3Utf8Read(zPattern, &zPattern);
 1.13492 +        c2 = sqlite3Utf8Read(&zPattern);
 1.13493        }
 1.13494        if( c2==0 || (seen ^ invert)==0 ){
 1.13495          return 0;
 1.13496 @@ -87806,7 +86403,7 @@
 1.13497      }else if( esc==c && !prevEscape ){
 1.13498        prevEscape = 1;
 1.13499      }else{
 1.13500 -      c2 = sqlite3Utf8Read(zString, &zString);
 1.13501 +      c2 = sqlite3Utf8Read(&zString);
 1.13502        if( noCase ){
 1.13503          GlogUpperToLower(c);
 1.13504          GlogUpperToLower(c2);
 1.13505 @@ -87878,7 +86475,7 @@
 1.13506            "ESCAPE expression must be a single character", -1);
 1.13507        return;
 1.13508      }
 1.13509 -    escape = sqlite3Utf8Read(zEsc, &zEsc);
 1.13510 +    escape = sqlite3Utf8Read(&zEsc);
 1.13511    }
 1.13512    if( zA && zB ){
 1.13513      struct compareInfo *pInfo = sqlite3_user_data(context);
 1.13514 @@ -88021,8 +86618,19 @@
 1.13515    assert( argc==1 );
 1.13516    UNUSED_PARAMETER(argc);
 1.13517    switch( sqlite3_value_type(argv[0]) ){
 1.13518 -    case SQLITE_INTEGER:
 1.13519      case SQLITE_FLOAT: {
 1.13520 +      double r1, r2;
 1.13521 +      char zBuf[50];
 1.13522 +      r1 = sqlite3_value_double(argv[0]);
 1.13523 +      sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
 1.13524 +      sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
 1.13525 +      if( r1!=r2 ){
 1.13526 +        sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
 1.13527 +      }
 1.13528 +      sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 1.13529 +      break;
 1.13530 +    }
 1.13531 +    case SQLITE_INTEGER: {
 1.13532        sqlite3_result_value(context, argv[0]);
 1.13533        break;
 1.13534      }
 1.13535 @@ -88702,6 +87310,7 @@
 1.13536      AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
 1.13537      FUNCTION2(typeof,            1, 0, 0, typeofFunc,  SQLITE_FUNC_TYPEOF),
 1.13538      FUNCTION2(length,            1, 0, 0, lengthFunc,  SQLITE_FUNC_LENGTH),
 1.13539 +    FUNCTION(instr,              2, 0, 0, instrFunc        ),
 1.13540      FUNCTION(substr,             2, 0, 0, substrFunc       ),
 1.13541      FUNCTION(substr,             3, 0, 0, substrFunc       ),
 1.13542      FUNCTION(abs,                1, 0, 0, absFunc          ),
 1.13543 @@ -89285,12 +87894,15 @@
 1.13544        ** expression to the parent key column defaults.  */
 1.13545        if( pIdx ){
 1.13546          Column *pCol;
 1.13547 +        const char *zColl;
 1.13548          iCol = pIdx->aiColumn[i];
 1.13549          pCol = &pTab->aCol[iCol];
 1.13550          if( pTab->iPKey==iCol ) iCol = -1;
 1.13551          pLeft->iTable = regData+iCol+1;
 1.13552          pLeft->affinity = pCol->affinity;
 1.13553 -        pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl);
 1.13554 +        zColl = pCol->zColl;
 1.13555 +        if( zColl==0 ) zColl = db->pDfltColl->zName;
 1.13556 +        pLeft = sqlite3ExprAddCollateString(pParse, pLeft, zColl);
 1.13557        }else{
 1.13558          pLeft->iTable = regData;
 1.13559          pLeft->affinity = SQLITE_AFF_INTEGER;
 1.13560 @@ -89334,7 +87946,7 @@
 1.13561    ** clause. If the constraint is not deferred, throw an exception for
 1.13562    ** each row found. Otherwise, for deferred constraints, increment the
 1.13563    ** deferred constraint counter by nIncr for each row selected.  */
 1.13564 -  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0);
 1.13565 +  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
 1.13566    if( nIncr>0 && pFKey->isDeferred==0 ){
 1.13567      sqlite3ParseToplevel(pParse)->mayAbort = 1;
 1.13568    }
 1.13569 @@ -89699,7 +88311,8 @@
 1.13570            int iKey;
 1.13571            for(iKey=0; iKey<pTab->nCol; iKey++){
 1.13572              Column *pCol = &pTab->aCol[iKey];
 1.13573 -            if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){
 1.13574 +            if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey)
 1.13575 +                      : (pCol->colFlags & COLFLAG_PRIMKEY)!=0) ){
 1.13576                if( aChange[iKey]>=0 ) return 1;
 1.13577                if( iKey==pTab->iPKey && chngRowid ) return 1;
 1.13578              }
 1.13579 @@ -90020,7 +88633,7 @@
 1.13580    int opcode      /* OP_OpenRead or OP_OpenWrite */
 1.13581  ){
 1.13582    Vdbe *v;
 1.13583 -  if( IsVirtual(pTab) ) return;
 1.13584 +  assert( !IsVirtual(pTab) );
 1.13585    v = sqlite3GetVdbe(p);
 1.13586    assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
 1.13587    sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
 1.13588 @@ -90315,6 +88928,97 @@
 1.13589  #endif /* SQLITE_OMIT_AUTOINCREMENT */
 1.13590  
 1.13591  
 1.13592 +/*
 1.13593 +** Generate code for a co-routine that will evaluate a subquery one
 1.13594 +** row at a time.
 1.13595 +**
 1.13596 +** The pSelect parameter is the subquery that the co-routine will evaluation.
 1.13597 +** Information about the location of co-routine and the registers it will use
 1.13598 +** is returned by filling in the pDest object.
 1.13599 +**
 1.13600 +** Registers are allocated as follows:
 1.13601 +**
 1.13602 +**   pDest->iSDParm      The register holding the next entry-point of the
 1.13603 +**                       co-routine.  Run the co-routine to its next breakpoint
 1.13604 +**                       by calling "OP_Yield $X" where $X is pDest->iSDParm.
 1.13605 +**
 1.13606 +**   pDest->iSDParm+1    The register holding the "completed" flag for the
 1.13607 +**                       co-routine. This register is 0 if the previous Yield
 1.13608 +**                       generated a new result row, or 1 if the subquery
 1.13609 +**                       has completed.  If the Yield is called again
 1.13610 +**                       after this register becomes 1, then the VDBE will
 1.13611 +**                       halt with an SQLITE_INTERNAL error.
 1.13612 +**
 1.13613 +**   pDest->iSdst        First result register.
 1.13614 +**
 1.13615 +**   pDest->nSdst        Number of result registers.
 1.13616 +**
 1.13617 +** This routine handles all of the register allocation and fills in the
 1.13618 +** pDest structure appropriately.
 1.13619 +**
 1.13620 +** Here is a schematic of the generated code assuming that X is the 
 1.13621 +** co-routine entry-point register reg[pDest->iSDParm], that EOF is the
 1.13622 +** completed flag reg[pDest->iSDParm+1], and R and S are the range of
 1.13623 +** registers that hold the result set, reg[pDest->iSdst] through
 1.13624 +** reg[pDest->iSdst+pDest->nSdst-1]:
 1.13625 +**
 1.13626 +**         X <- A
 1.13627 +**         EOF <- 0
 1.13628 +**         goto B
 1.13629 +**      A: setup for the SELECT
 1.13630 +**         loop rows in the SELECT
 1.13631 +**           load results into registers R..S
 1.13632 +**           yield X
 1.13633 +**         end loop
 1.13634 +**         cleanup after the SELECT
 1.13635 +**         EOF <- 1
 1.13636 +**         yield X
 1.13637 +**         halt-error
 1.13638 +**      B:
 1.13639 +**
 1.13640 +** To use this subroutine, the caller generates code as follows:
 1.13641 +**
 1.13642 +**         [ Co-routine generated by this subroutine, shown above ]
 1.13643 +**      S: yield X
 1.13644 +**         if EOF goto E
 1.13645 +**         if skip this row, goto C
 1.13646 +**         if terminate loop, goto E
 1.13647 +**         deal with this row
 1.13648 +**      C: goto S
 1.13649 +**      E:
 1.13650 +*/
 1.13651 +SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse *pParse, Select *pSelect, SelectDest *pDest){
 1.13652 +  int regYield;       /* Register holding co-routine entry-point */
 1.13653 +  int regEof;         /* Register holding co-routine completion flag */
 1.13654 +  int addrTop;        /* Top of the co-routine */
 1.13655 +  int j1;             /* Jump instruction */
 1.13656 +  int rc;             /* Result code */
 1.13657 +  Vdbe *v;            /* VDBE under construction */
 1.13658 +
 1.13659 +  regYield = ++pParse->nMem;
 1.13660 +  regEof = ++pParse->nMem;
 1.13661 +  v = sqlite3GetVdbe(pParse);
 1.13662 +  addrTop = sqlite3VdbeCurrentAddr(v);
 1.13663 +  sqlite3VdbeAddOp2(v, OP_Integer, addrTop+2, regYield); /* X <- A */
 1.13664 +  VdbeComment((v, "Co-routine entry point"));
 1.13665 +  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);           /* EOF <- 0 */
 1.13666 +  VdbeComment((v, "Co-routine completion flag"));
 1.13667 +  sqlite3SelectDestInit(pDest, SRT_Coroutine, regYield);
 1.13668 +  j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
 1.13669 +  rc = sqlite3Select(pParse, pSelect, pDest);
 1.13670 +  assert( pParse->nErr==0 || rc );
 1.13671 +  if( pParse->db->mallocFailed && rc==SQLITE_OK ) rc = SQLITE_NOMEM;
 1.13672 +  if( rc ) return rc;
 1.13673 +  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);            /* EOF <- 1 */
 1.13674 +  sqlite3VdbeAddOp1(v, OP_Yield, regYield);   /* yield X */
 1.13675 +  sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
 1.13676 +  VdbeComment((v, "End of coroutine"));
 1.13677 +  sqlite3VdbeJumpHere(v, j1);                             /* label B: */
 1.13678 +  return rc;
 1.13679 +}
 1.13680 +
 1.13681 +
 1.13682 +
 1.13683  /* Forward declaration */
 1.13684  static int xferOptimization(
 1.13685    Parse *pParse,        /* Parser context */
 1.13686 @@ -90563,55 +89267,16 @@
 1.13687    ** co-routine is the common header to the 3rd and 4th templates.
 1.13688    */
 1.13689    if( pSelect ){
 1.13690 -    /* Data is coming from a SELECT.  Generate code to implement that SELECT
 1.13691 -    ** as a co-routine.  The code is common to both the 3rd and 4th
 1.13692 -    ** templates:
 1.13693 -    **
 1.13694 -    **         EOF <- 0
 1.13695 -    **         X <- A
 1.13696 -    **         goto B
 1.13697 -    **      A: setup for the SELECT
 1.13698 -    **         loop over the tables in the SELECT
 1.13699 -    **           load value into register R..R+n
 1.13700 -    **           yield X
 1.13701 -    **         end loop
 1.13702 -    **         cleanup after the SELECT
 1.13703 -    **         EOF <- 1
 1.13704 -    **         yield X
 1.13705 -    **         halt-error
 1.13706 -    **
 1.13707 -    ** On each invocation of the co-routine, it puts a single row of the
 1.13708 -    ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
 1.13709 -    ** (These output registers are allocated by sqlite3Select().)  When
 1.13710 -    ** the SELECT completes, it sets the EOF flag stored in regEof.
 1.13711 -    */
 1.13712 -    int rc, j1;
 1.13713 -
 1.13714 -    regEof = ++pParse->nMem;
 1.13715 -    sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);      /* EOF <- 0 */
 1.13716 -    VdbeComment((v, "SELECT eof flag"));
 1.13717 -    sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
 1.13718 -    addrSelect = sqlite3VdbeCurrentAddr(v)+2;
 1.13719 -    sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
 1.13720 -    j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
 1.13721 -    VdbeComment((v, "Jump over SELECT coroutine"));
 1.13722 -
 1.13723 -    /* Resolve the expressions in the SELECT statement and execute it. */
 1.13724 -    rc = sqlite3Select(pParse, pSelect, &dest);
 1.13725 -    assert( pParse->nErr==0 || rc );
 1.13726 -    if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
 1.13727 -      goto insert_cleanup;
 1.13728 -    }
 1.13729 -    sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);         /* EOF <- 1 */
 1.13730 -    sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);   /* yield X */
 1.13731 -    sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
 1.13732 -    VdbeComment((v, "End of SELECT coroutine"));
 1.13733 -    sqlite3VdbeJumpHere(v, j1);                          /* label B: */
 1.13734 -
 1.13735 -    regFromSelect = dest.iMem;
 1.13736 +    /* Data is coming from a SELECT.  Generate a co-routine to run that
 1.13737 +    ** SELECT. */
 1.13738 +    int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
 1.13739 +    if( rc ) goto insert_cleanup;
 1.13740 +
 1.13741 +    regEof = dest.iSDParm + 1;
 1.13742 +    regFromSelect = dest.iSdst;
 1.13743      assert( pSelect->pEList );
 1.13744      nColumn = pSelect->pEList->nExpr;
 1.13745 -    assert( dest.nMem==nColumn );
 1.13746 +    assert( dest.nSdst==nColumn );
 1.13747  
 1.13748      /* Set useTempTable to TRUE if the result of the SELECT statement
 1.13749      ** should be written into a temporary table (template 4).  Set to
 1.13750 @@ -90647,7 +89312,7 @@
 1.13751        regRec = sqlite3GetTempReg(pParse);
 1.13752        regTempRowid = sqlite3GetTempReg(pParse);
 1.13753        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
 1.13754 -      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
 1.13755 +      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
 1.13756        addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
 1.13757        sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
 1.13758        sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
 1.13759 @@ -90784,7 +89449,7 @@
 1.13760      **         goto C
 1.13761      **      D: ...
 1.13762      */
 1.13763 -    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
 1.13764 +    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
 1.13765      addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
 1.13766    }
 1.13767  
 1.13768 @@ -91266,7 +89931,7 @@
 1.13769        case OE_Replace: {
 1.13770          /* If there are DELETE triggers on this table and the
 1.13771          ** recursive-triggers flag is set, call GenerateRowDelete() to
 1.13772 -        ** remove the conflicting row from the the table. This will fire
 1.13773 +        ** remove the conflicting row from the table. This will fire
 1.13774          ** the triggers and remove both the table and index b-tree entries.
 1.13775          **
 1.13776          ** Otherwise, if there are no triggers or the recursive-triggers
 1.13777 @@ -91686,7 +90351,7 @@
 1.13778    ** we have to check the semantics.
 1.13779    */
 1.13780    pItem = pSelect->pSrc->a;
 1.13781 -  pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
 1.13782 +  pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
 1.13783    if( pSrc==0 ){
 1.13784      return 0;   /* FROM clause does not contain a real table */
 1.13785    }
 1.13786 @@ -93464,6 +92129,7 @@
 1.13787    aFcntl[1] = zLeft;
 1.13788    aFcntl[2] = zRight;
 1.13789    aFcntl[3] = 0;
 1.13790 +  db->busyHandler.nBusy = 0;
 1.13791    rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
 1.13792    if( rc==SQLITE_OK ){
 1.13793      if( aFcntl[0] ){
 1.13794 @@ -94081,7 +92747,8 @@
 1.13795          }else{
 1.13796            sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
 1.13797          }
 1.13798 -        sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
 1.13799 +        sqlite3VdbeAddOp2(v, OP_Integer,
 1.13800 +                            (pCol->colFlags&COLFLAG_PRIMKEY)!=0, 6);
 1.13801          sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
 1.13802        }
 1.13803      }
 1.13804 @@ -94267,6 +92934,19 @@
 1.13805  
 1.13806      int isQuick = (sqlite3Tolower(zLeft[0])=='q');
 1.13807  
 1.13808 +    /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
 1.13809 +    ** then iDb is set to the index of the database identified by <db>.
 1.13810 +    ** In this case, the integrity of database iDb only is verified by
 1.13811 +    ** the VDBE created below.
 1.13812 +    **
 1.13813 +    ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
 1.13814 +    ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
 1.13815 +    ** to -1 here, to indicate that the VDBE should verify the integrity
 1.13816 +    ** of all attached databases.  */
 1.13817 +    assert( iDb>=0 );
 1.13818 +    assert( iDb==0 || pId2->z );
 1.13819 +    if( pId2->z==0 ) iDb = -1;
 1.13820 +
 1.13821      /* Initialize the VDBE program */
 1.13822      if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.13823      pParse->nMem = 6;
 1.13824 @@ -94290,6 +92970,7 @@
 1.13825        int cnt = 0;
 1.13826  
 1.13827        if( OMIT_TEMPDB && i==1 ) continue;
 1.13828 +      if( iDb>=0 && i!=iDb ) continue;
 1.13829  
 1.13830        sqlite3CodeVerifySchema(pParse, i);
 1.13831        addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
 1.13832 @@ -94301,7 +92982,7 @@
 1.13833        ** Begin by filling registers 2, 3, ... with the root pages numbers
 1.13834        ** for all tables and indices in the database.
 1.13835        */
 1.13836 -      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.13837 +      assert( sqlite3SchemaMutexHeld(db, i, 0) );
 1.13838        pTbls = &db->aDb[i].pSchema->tblHash;
 1.13839        for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
 1.13840          Table *pTab = sqliteHashData(x);
 1.13841 @@ -94326,7 +93007,7 @@
 1.13842        sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 1.13843           sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
 1.13844           P4_DYNAMIC);
 1.13845 -      sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
 1.13846 +      sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
 1.13847        sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
 1.13848        sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
 1.13849        sqlite3VdbeJumpHere(v, addr);
 1.13850 @@ -94629,6 +93310,22 @@
 1.13851      sqlite3_db_release_memory(db);
 1.13852    }else
 1.13853  
 1.13854 +  /*
 1.13855 +  **   PRAGMA busy_timeout
 1.13856 +  **   PRAGMA busy_timeout = N
 1.13857 +  **
 1.13858 +  ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
 1.13859 +  ** if one is set.  If no busy handler or a different busy handler is set
 1.13860 +  ** then 0 is returned.  Setting the busy_timeout to 0 or negative
 1.13861 +  ** disables the timeout.
 1.13862 +  */
 1.13863 +  if( sqlite3StrICmp(zLeft, "busy_timeout")==0 ){
 1.13864 +    if( zRight ){
 1.13865 +      sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
 1.13866 +    }
 1.13867 +    returnSingleInt(pParse, "timeout",  db->busyTimeout);
 1.13868 +  }else
 1.13869 +
 1.13870  #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
 1.13871    /*
 1.13872    ** Report the current state of file logs for all databases
 1.13873 @@ -94644,13 +93341,12 @@
 1.13874      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
 1.13875      for(i=0; i<db->nDb; i++){
 1.13876        Btree *pBt;
 1.13877 -      Pager *pPager;
 1.13878        const char *zState = "unknown";
 1.13879        int j;
 1.13880        if( db->aDb[i].zName==0 ) continue;
 1.13881        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
 1.13882        pBt = db->aDb[i].pBt;
 1.13883 -      if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
 1.13884 +      if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
 1.13885          zState = "closed";
 1.13886        }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 
 1.13887                                       SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
 1.13888 @@ -94859,7 +93555,9 @@
 1.13889  static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
 1.13890    int rc;
 1.13891    int i;
 1.13892 +#ifndef SQLITE_OMIT_DEPRECATED
 1.13893    int size;
 1.13894 +#endif
 1.13895    Table *pTab;
 1.13896    Db *pDb;
 1.13897    char const *azArg[4];
 1.13898 @@ -95625,10 +94323,10 @@
 1.13899  */
 1.13900  SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
 1.13901    pDest->eDest = (u8)eDest;
 1.13902 -  pDest->iParm = iParm;
 1.13903 -  pDest->affinity = 0;
 1.13904 -  pDest->iMem = 0;
 1.13905 -  pDest->nMem = 0;
 1.13906 +  pDest->iSDParm = iParm;
 1.13907 +  pDest->affSdst = 0;
 1.13908 +  pDest->iSdst = 0;
 1.13909 +  pDest->nSdst = 0;
 1.13910  }
 1.13911  
 1.13912  
 1.13913 @@ -96115,6 +94813,19 @@
 1.13914  #endif
 1.13915  
 1.13916  /*
 1.13917 +** An instance of the following object is used to record information about
 1.13918 +** how to process the DISTINCT keyword, to simplify passing that information
 1.13919 +** into the selectInnerLoop() routine.
 1.13920 +*/
 1.13921 +typedef struct DistinctCtx DistinctCtx;
 1.13922 +struct DistinctCtx {
 1.13923 +  u8 isTnct;      /* True if the DISTINCT keyword is present */
 1.13924 +  u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */
 1.13925 +  int tabTnct;    /* Ephemeral table used for DISTINCT processing */
 1.13926 +  int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
 1.13927 +};
 1.13928 +
 1.13929 +/*
 1.13930  ** This routine generates the code for the inside of the inner loop
 1.13931  ** of a SELECT.
 1.13932  **
 1.13933 @@ -96130,7 +94841,7 @@
 1.13934    int srcTab,             /* Pull data from this table */
 1.13935    int nColumn,            /* Number of columns in the source table */
 1.13936    ExprList *pOrderBy,     /* If not NULL, sort results using this key */
 1.13937 -  int distinct,           /* If >=0, make sure results are distinct */
 1.13938 +  DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
 1.13939    SelectDest *pDest,      /* How to dispose of the results */
 1.13940    int iContinue,          /* Jump here to continue with next row */
 1.13941    int iBreak              /* Jump here to break out of the inner loop */
 1.13942 @@ -96140,13 +94851,13 @@
 1.13943    int hasDistinct;        /* True if the DISTINCT keyword is present */
 1.13944    int regResult;              /* Start of memory holding result set */
 1.13945    int eDest = pDest->eDest;   /* How to dispose of results */
 1.13946 -  int iParm = pDest->iParm;   /* First argument to disposal method */
 1.13947 +  int iParm = pDest->iSDParm; /* First argument to disposal method */
 1.13948    int nResultCol;             /* Number of result columns */
 1.13949  
 1.13950    assert( v );
 1.13951    if( NEVER(v==0) ) return;
 1.13952    assert( pEList!=0 );
 1.13953 -  hasDistinct = distinct>=0;
 1.13954 +  hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
 1.13955    if( pOrderBy==0 && !hasDistinct ){
 1.13956      codeOffset(v, p, iContinue);
 1.13957    }
 1.13958 @@ -96158,14 +94869,14 @@
 1.13959    }else{
 1.13960      nResultCol = pEList->nExpr;
 1.13961    }
 1.13962 -  if( pDest->iMem==0 ){
 1.13963 -    pDest->iMem = pParse->nMem+1;
 1.13964 -    pDest->nMem = nResultCol;
 1.13965 +  if( pDest->iSdst==0 ){
 1.13966 +    pDest->iSdst = pParse->nMem+1;
 1.13967 +    pDest->nSdst = nResultCol;
 1.13968      pParse->nMem += nResultCol;
 1.13969    }else{ 
 1.13970 -    assert( pDest->nMem==nResultCol );
 1.13971 -  }
 1.13972 -  regResult = pDest->iMem;
 1.13973 +    assert( pDest->nSdst==nResultCol );
 1.13974 +  }
 1.13975 +  regResult = pDest->iSdst;
 1.13976    if( nColumn>0 ){
 1.13977      for(i=0; i<nColumn; i++){
 1.13978        sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
 1.13979 @@ -96186,7 +94897,55 @@
 1.13980    if( hasDistinct ){
 1.13981      assert( pEList!=0 );
 1.13982      assert( pEList->nExpr==nColumn );
 1.13983 -    codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
 1.13984 +    switch( pDistinct->eTnctType ){
 1.13985 +      case WHERE_DISTINCT_ORDERED: {
 1.13986 +        VdbeOp *pOp;            /* No longer required OpenEphemeral instr. */
 1.13987 +        int iJump;              /* Jump destination */
 1.13988 +        int regPrev;            /* Previous row content */
 1.13989 +
 1.13990 +        /* Allocate space for the previous row */
 1.13991 +        regPrev = pParse->nMem+1;
 1.13992 +        pParse->nMem += nColumn;
 1.13993 +
 1.13994 +        /* Change the OP_OpenEphemeral coded earlier to an OP_Null
 1.13995 +        ** sets the MEM_Cleared bit on the first register of the
 1.13996 +        ** previous value.  This will cause the OP_Ne below to always
 1.13997 +        ** fail on the first iteration of the loop even if the first
 1.13998 +        ** row is all NULLs.
 1.13999 +        */
 1.14000 +        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
 1.14001 +        pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
 1.14002 +        pOp->opcode = OP_Null;
 1.14003 +        pOp->p1 = 1;
 1.14004 +        pOp->p2 = regPrev;
 1.14005 +
 1.14006 +        iJump = sqlite3VdbeCurrentAddr(v) + nColumn;
 1.14007 +        for(i=0; i<nColumn; i++){
 1.14008 +          CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
 1.14009 +          if( i<nColumn-1 ){
 1.14010 +            sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
 1.14011 +          }else{
 1.14012 +            sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
 1.14013 +          }
 1.14014 +          sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
 1.14015 +          sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
 1.14016 +        }
 1.14017 +        assert( sqlite3VdbeCurrentAddr(v)==iJump );
 1.14018 +        sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nColumn-1);
 1.14019 +        break;
 1.14020 +      }
 1.14021 +
 1.14022 +      case WHERE_DISTINCT_UNIQUE: {
 1.14023 +        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
 1.14024 +        break;
 1.14025 +      }
 1.14026 +
 1.14027 +      default: {
 1.14028 +        assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
 1.14029 +        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nColumn, regResult);
 1.14030 +        break;
 1.14031 +      }
 1.14032 +    }
 1.14033      if( pOrderBy==0 ){
 1.14034        codeOffset(v, p, iContinue);
 1.14035      }
 1.14036 @@ -96244,7 +95003,8 @@
 1.14037      */
 1.14038      case SRT_Set: {
 1.14039        assert( nColumn==1 );
 1.14040 -      p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity);
 1.14041 +      pDest->affSdst =
 1.14042 +                  sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
 1.14043        if( pOrderBy ){
 1.14044          /* At first glance you would think we could optimize out the
 1.14045          ** ORDER BY in this case since the order of entries in the set
 1.14046 @@ -96253,7 +95013,7 @@
 1.14047          pushOntoSorter(pParse, pOrderBy, p, regResult);
 1.14048        }else{
 1.14049          int r1 = sqlite3GetTempReg(pParse);
 1.14050 -        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
 1.14051 +        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
 1.14052          sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
 1.14053          sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
 1.14054          sqlite3ReleaseTempReg(pParse, r1);
 1.14055 @@ -96299,7 +95059,7 @@
 1.14056          pushOntoSorter(pParse, pOrderBy, p, r1);
 1.14057          sqlite3ReleaseTempReg(pParse, r1);
 1.14058        }else if( eDest==SRT_Coroutine ){
 1.14059 -        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
 1.14060 +        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 1.14061        }else{
 1.14062          sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
 1.14063          sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
 1.14064 @@ -96479,7 +95239,7 @@
 1.14065    ExprList *pOrderBy = p->pOrderBy;
 1.14066  
 1.14067    int eDest = pDest->eDest;
 1.14068 -  int iParm = pDest->iParm;
 1.14069 +  int iParm = pDest->iSDParm;
 1.14070  
 1.14071    int regRow;
 1.14072    int regRowid;
 1.14073 @@ -96520,7 +95280,8 @@
 1.14074  #ifndef SQLITE_OMIT_SUBQUERY
 1.14075      case SRT_Set: {
 1.14076        assert( nColumn==1 );
 1.14077 -      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
 1.14078 +      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
 1.14079 +                        &pDest->affSdst, 1);
 1.14080        sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
 1.14081        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
 1.14082        break;
 1.14083 @@ -96538,17 +95299,17 @@
 1.14084        testcase( eDest==SRT_Output );
 1.14085        testcase( eDest==SRT_Coroutine );
 1.14086        for(i=0; i<nColumn; i++){
 1.14087 -        assert( regRow!=pDest->iMem+i );
 1.14088 -        sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
 1.14089 +        assert( regRow!=pDest->iSdst+i );
 1.14090 +        sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iSdst+i);
 1.14091          if( i==0 ){
 1.14092            sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 1.14093          }
 1.14094        }
 1.14095        if( eDest==SRT_Output ){
 1.14096 -        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
 1.14097 -        sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn);
 1.14098 -      }else{
 1.14099 -        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
 1.14100 +        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
 1.14101 +        sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
 1.14102 +      }else{
 1.14103 +        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 1.14104        }
 1.14105        break;
 1.14106      }
 1.14107 @@ -96835,7 +95596,7 @@
 1.14108  static int selectColumnsFromExprList(
 1.14109    Parse *pParse,          /* Parsing context */
 1.14110    ExprList *pEList,       /* Expr list from which to derive column names */
 1.14111 -  int *pnCol,             /* Write the number of columns here */
 1.14112 +  i16 *pnCol,             /* Write the number of columns here */
 1.14113    Column **paCol          /* Write the new column list here */
 1.14114  ){
 1.14115    sqlite3 *db = pParse->db;   /* Database connection */
 1.14116 @@ -96861,7 +95622,7 @@
 1.14117    for(i=0, pCol=aCol; i<nCol; i++, pCol++){
 1.14118      /* Get an appropriate name for the column
 1.14119      */
 1.14120 -    p = pEList->a[i].pExpr;
 1.14121 +    p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
 1.14122      assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
 1.14123                 || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
 1.14124      if( (zName = pEList->a[i].zName)!=0 ){
 1.14125 @@ -97199,7 +95960,7 @@
 1.14126    */
 1.14127    if( dest.eDest==SRT_EphemTab ){
 1.14128      assert( p->pEList );
 1.14129 -    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr);
 1.14130 +    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
 1.14131      sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 1.14132      dest.eDest = SRT_Table;
 1.14133    }
 1.14134 @@ -97285,7 +96046,7 @@
 1.14135                                       ** of a 3-way or more compound */
 1.14136          assert( p->pLimit==0 );      /* Not allowed on leftward elements */
 1.14137          assert( p->pOffset==0 );     /* Not allowed on leftward elements */
 1.14138 -        unionTab = dest.iParm;
 1.14139 +        unionTab = dest.iSDParm;
 1.14140        }else{
 1.14141          /* We will need to create our own temporary table to hold the
 1.14142          ** intermediate results.
 1.14143 @@ -97342,7 +96103,7 @@
 1.14144        /* Convert the data in the temporary table into whatever form
 1.14145        ** it is that we currently need.
 1.14146        */
 1.14147 -      assert( unionTab==dest.iParm || dest.eDest!=priorOp );
 1.14148 +      assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
 1.14149        if( dest.eDest!=priorOp ){
 1.14150          int iCont, iBreak, iStart;
 1.14151          assert( p->pEList );
 1.14152 @@ -97357,7 +96118,7 @@
 1.14153          sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
 1.14154          iStart = sqlite3VdbeCurrentAddr(v);
 1.14155          selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
 1.14156 -                        0, -1, &dest, iCont, iBreak);
 1.14157 +                        0, 0, &dest, iCont, iBreak);
 1.14158          sqlite3VdbeResolveLabel(v, iCont);
 1.14159          sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
 1.14160          sqlite3VdbeResolveLabel(v, iBreak);
 1.14161 @@ -97406,7 +96167,7 @@
 1.14162        p->pLimit = 0;
 1.14163        pOffset = p->pOffset;
 1.14164        p->pOffset = 0;
 1.14165 -      intersectdest.iParm = tab2;
 1.14166 +      intersectdest.iSDParm = tab2;
 1.14167        explainSetInteger(iSub2, pParse->iNextSelectId);
 1.14168        rc = sqlite3Select(pParse, p, &intersectdest);
 1.14169        testcase( rc!=SQLITE_OK );
 1.14170 @@ -97435,7 +96196,7 @@
 1.14171        sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
 1.14172        sqlite3ReleaseTempReg(pParse, r1);
 1.14173        selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
 1.14174 -                      0, -1, &dest, iCont, iBreak);
 1.14175 +                      0, 0, &dest, iCont, iBreak);
 1.14176        sqlite3VdbeResolveLabel(v, iCont);
 1.14177        sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
 1.14178        sqlite3VdbeResolveLabel(v, iBreak);
 1.14179 @@ -97481,6 +96242,7 @@
 1.14180          *apColl = db->pDfltColl;
 1.14181        }
 1.14182      }
 1.14183 +    pKeyInfo->aSortOrder = (u8*)apColl;
 1.14184  
 1.14185      for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
 1.14186        for(i=0; i<2; i++){
 1.14187 @@ -97500,8 +96262,8 @@
 1.14188    }
 1.14189  
 1.14190  multi_select_end:
 1.14191 -  pDest->iMem = dest.iMem;
 1.14192 -  pDest->nMem = dest.nMem;
 1.14193 +  pDest->iSdst = dest.iSdst;
 1.14194 +  pDest->nSdst = dest.nSdst;
 1.14195    sqlite3SelectDelete(db, pDelete);
 1.14196    return rc;
 1.14197  }
 1.14198 @@ -97511,8 +96273,8 @@
 1.14199  ** Code an output subroutine for a coroutine implementation of a
 1.14200  ** SELECT statment.
 1.14201  **
 1.14202 -** The data to be output is contained in pIn->iMem.  There are
 1.14203 -** pIn->nMem columns to be output.  pDest is where the output should
 1.14204 +** The data to be output is contained in pIn->iSdst.  There are
 1.14205 +** pIn->nSdst columns to be output.  pDest is where the output should
 1.14206  ** be sent.
 1.14207  **
 1.14208  ** regReturn is the number of the register holding the subroutine
 1.14209 @@ -97550,16 +96312,16 @@
 1.14210    if( regPrev ){
 1.14211      int j1, j2;
 1.14212      j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
 1.14213 -    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iMem, regPrev+1, pIn->nMem,
 1.14214 +    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
 1.14215                                (char*)pKeyInfo, p4type);
 1.14216      sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
 1.14217      sqlite3VdbeJumpHere(v, j1);
 1.14218 -    sqlite3ExprCodeCopy(pParse, pIn->iMem, regPrev+1, pIn->nMem);
 1.14219 +    sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
 1.14220      sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
 1.14221    }
 1.14222    if( pParse->db->mallocFailed ) return 0;
 1.14223  
 1.14224 -  /* Suppress the the first OFFSET entries if there is an OFFSET clause
 1.14225 +  /* Suppress the first OFFSET entries if there is an OFFSET clause
 1.14226    */
 1.14227    codeOffset(v, p, iContinue);
 1.14228  
 1.14229 @@ -97572,9 +96334,9 @@
 1.14230        int r2 = sqlite3GetTempReg(pParse);
 1.14231        testcase( pDest->eDest==SRT_Table );
 1.14232        testcase( pDest->eDest==SRT_EphemTab );
 1.14233 -      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iMem, pIn->nMem, r1);
 1.14234 -      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iParm, r2);
 1.14235 -      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iParm, r1, r2);
 1.14236 +      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
 1.14237 +      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
 1.14238 +      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
 1.14239        sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 1.14240        sqlite3ReleaseTempReg(pParse, r2);
 1.14241        sqlite3ReleaseTempReg(pParse, r1);
 1.14242 @@ -97588,13 +96350,13 @@
 1.14243      */
 1.14244      case SRT_Set: {
 1.14245        int r1;
 1.14246 -      assert( pIn->nMem==1 );
 1.14247 -      p->affinity = 
 1.14248 -         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affinity);
 1.14249 +      assert( pIn->nSdst==1 );
 1.14250 +      pDest->affSdst = 
 1.14251 +         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
 1.14252        r1 = sqlite3GetTempReg(pParse);
 1.14253 -      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iMem, 1, r1, &p->affinity, 1);
 1.14254 -      sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, 1);
 1.14255 -      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iParm, r1);
 1.14256 +      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
 1.14257 +      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
 1.14258 +      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
 1.14259        sqlite3ReleaseTempReg(pParse, r1);
 1.14260        break;
 1.14261      }
 1.14262 @@ -97603,7 +96365,7 @@
 1.14263      /* If any row exist in the result set, record that fact and abort.
 1.14264      */
 1.14265      case SRT_Exists: {
 1.14266 -      sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iParm);
 1.14267 +      sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
 1.14268        /* The LIMIT clause will terminate the loop for us */
 1.14269        break;
 1.14270      }
 1.14271 @@ -97614,23 +96376,23 @@
 1.14272      ** of the scan loop.
 1.14273      */
 1.14274      case SRT_Mem: {
 1.14275 -      assert( pIn->nMem==1 );
 1.14276 -      sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iParm, 1);
 1.14277 +      assert( pIn->nSdst==1 );
 1.14278 +      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
 1.14279        /* The LIMIT clause will jump out of the loop for us */
 1.14280        break;
 1.14281      }
 1.14282  #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
 1.14283  
 1.14284      /* The results are stored in a sequence of registers
 1.14285 -    ** starting at pDest->iMem.  Then the co-routine yields.
 1.14286 +    ** starting at pDest->iSdst.  Then the co-routine yields.
 1.14287      */
 1.14288      case SRT_Coroutine: {
 1.14289 -      if( pDest->iMem==0 ){
 1.14290 -        pDest->iMem = sqlite3GetTempRange(pParse, pIn->nMem);
 1.14291 -        pDest->nMem = pIn->nMem;
 1.14292 -      }
 1.14293 -      sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iMem, pDest->nMem);
 1.14294 -      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
 1.14295 +      if( pDest->iSdst==0 ){
 1.14296 +        pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
 1.14297 +        pDest->nSdst = pIn->nSdst;
 1.14298 +      }
 1.14299 +      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pDest->nSdst);
 1.14300 +      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 1.14301        break;
 1.14302      }
 1.14303  
 1.14304 @@ -97644,8 +96406,8 @@
 1.14305      */
 1.14306      default: {
 1.14307        assert( pDest->eDest==SRT_Output );
 1.14308 -      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iMem, pIn->nMem);
 1.14309 -      sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, pIn->nMem);
 1.14310 +      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
 1.14311 +      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
 1.14312        break;
 1.14313      }
 1.14314    }
 1.14315 @@ -97858,12 +96620,13 @@
 1.14316        for(i=0; i<nOrderBy; i++){
 1.14317          CollSeq *pColl;
 1.14318          Expr *pTerm = pOrderBy->a[i].pExpr;
 1.14319 -        if( pTerm->flags & EP_ExpCollate ){
 1.14320 -          pColl = pTerm->pColl;
 1.14321 +        if( pTerm->flags & EP_Collate ){
 1.14322 +          pColl = sqlite3ExprCollSeq(pParse, pTerm);
 1.14323          }else{
 1.14324            pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
 1.14325 -          pTerm->flags |= EP_ExpCollate;
 1.14326 -          pTerm->pColl = pColl;
 1.14327 +          if( pColl==0 ) pColl = db->pDfltColl;
 1.14328 +          pOrderBy->a[i].pExpr =
 1.14329 +             sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
 1.14330          }
 1.14331          pKeyMerge->aColl[i] = pColl;
 1.14332          pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
 1.14333 @@ -98064,8 +96827,9 @@
 1.14334    */
 1.14335    sqlite3VdbeResolveLabel(v, labelCmpr);
 1.14336    sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
 1.14337 -  sqlite3VdbeAddOp4(v, OP_Compare, destA.iMem, destB.iMem, nOrderBy,
 1.14338 +  sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
 1.14339                           (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
 1.14340 +  sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
 1.14341    sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
 1.14342  
 1.14343    /* Release temporary registers
 1.14344 @@ -98133,9 +96897,6 @@
 1.14345        assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
 1.14346        assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 1.14347        pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
 1.14348 -      if( pNew && pExpr->pColl ){
 1.14349 -        pNew->pColl = pExpr->pColl;
 1.14350 -      }
 1.14351        sqlite3ExprDelete(db, pExpr);
 1.14352        pExpr = pNew;
 1.14353      }
 1.14354 @@ -98278,6 +97039,12 @@
 1.14355  **        operators have an implied DISTINCT which is disallowed by
 1.14356  **        restriction (4).
 1.14357  **
 1.14358 +**        Also, each component of the sub-query must return the same number
 1.14359 +**        of result columns. This is actually a requirement for any compound
 1.14360 +**        SELECT statement, but all the code here does is make sure that no
 1.14361 +**        such (illegal) sub-query is flattened. The caller will detect the
 1.14362 +**        syntax error and return a detailed message.
 1.14363 +**
 1.14364  **  (18)  If the sub-query is a compound select, then all terms of the
 1.14365  **        ORDER by clause of the parent must be simple references to 
 1.14366  **        columns of the sub-query.
 1.14367 @@ -98328,7 +97095,7 @@
 1.14368    */
 1.14369    assert( p!=0 );
 1.14370    assert( p->pPrior==0 );  /* Unable to flatten compound queries */
 1.14371 -  if( db->flags & SQLITE_QueryFlattener ) return 0;
 1.14372 +  if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
 1.14373    pSrc = p->pSrc;
 1.14374    assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
 1.14375    pSubitem = &pSrc->a[iFrom];
 1.14376 @@ -98421,6 +97188,7 @@
 1.14377        if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
 1.14378         || (pSub1->pPrior && pSub1->op!=TK_ALL) 
 1.14379         || pSub1->pSrc->nSrc<1
 1.14380 +       || pSub->pEList->nExpr!=pSub1->pEList->nExpr
 1.14381        ){
 1.14382          return 0;
 1.14383        }
 1.14384 @@ -98616,10 +97384,9 @@
 1.14385      pList = pParent->pEList;
 1.14386      for(i=0; i<pList->nExpr; i++){
 1.14387        if( pList->a[i].zName==0 ){
 1.14388 -        const char *zSpan = pList->a[i].zSpan;
 1.14389 -        if( ALWAYS(zSpan) ){
 1.14390 -          pList->a[i].zName = sqlite3DbStrDup(db, zSpan);
 1.14391 -        }
 1.14392 +        char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
 1.14393 +        sqlite3Dequote(zName);
 1.14394 +        pList->a[i].zName = zName;
 1.14395        }
 1.14396      }
 1.14397      substExprList(db, pParent->pEList, iParent, pSub->pEList);
 1.14398 @@ -98738,7 +97505,7 @@
 1.14399  
 1.14400    if( IsVirtual(pTab) ) return 0;
 1.14401    if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
 1.14402 -  if( pAggInfo->nFunc==0 ) return 0;
 1.14403 +  if( NEVER(pAggInfo->nFunc==0) ) return 0;
 1.14404    if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0;
 1.14405    if( pExpr->flags&EP_Distinct ) return 0;
 1.14406  
 1.14407 @@ -98850,8 +97617,7 @@
 1.14408      }else{
 1.14409        /* An ordinary table or view name in the FROM clause */
 1.14410        assert( pFrom->pTab==0 );
 1.14411 -      pFrom->pTab = pTab = 
 1.14412 -        sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase);
 1.14413 +      pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
 1.14414        if( pTab==0 ) return WRC_Abort;
 1.14415        pTab->nRef++;
 1.14416  #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
 1.14417 @@ -99110,7 +97876,7 @@
 1.14418  
 1.14419  
 1.14420  /*
 1.14421 -** This routine sets of a SELECT statement for processing.  The
 1.14422 +** This routine sets up a SELECT statement for processing.  The
 1.14423  ** following is accomplished:
 1.14424  **
 1.14425  **     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
 1.14426 @@ -99142,7 +97908,8 @@
 1.14427  **
 1.14428  ** The aggregate accumulator is a set of memory cells that hold
 1.14429  ** intermediate results while calculating an aggregate.  This
 1.14430 -** routine simply stores NULLs in all of those memory cells.
 1.14431 +** routine generates code that stores NULLs in all of those memory
 1.14432 +** cells.
 1.14433  */
 1.14434  static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
 1.14435    Vdbe *v = pParse->pVdbe;
 1.14436 @@ -99310,23 +98077,24 @@
 1.14437  **
 1.14438  **     SRT_Mem         Only valid if the result is a single column.
 1.14439  **                     Store the first column of the first result row
 1.14440 -**                     in register pDest->iParm then abandon the rest
 1.14441 +**                     in register pDest->iSDParm then abandon the rest
 1.14442  **                     of the query.  This destination implies "LIMIT 1".
 1.14443  **
 1.14444  **     SRT_Set         The result must be a single column.  Store each
 1.14445 -**                     row of result as the key in table pDest->iParm. 
 1.14446 -**                     Apply the affinity pDest->affinity before storing
 1.14447 +**                     row of result as the key in table pDest->iSDParm. 
 1.14448 +**                     Apply the affinity pDest->affSdst before storing
 1.14449  **                     results.  Used to implement "IN (SELECT ...)".
 1.14450  **
 1.14451 -**     SRT_Union       Store results as a key in a temporary table pDest->iParm.
 1.14452 -**
 1.14453 -**     SRT_Except      Remove results from the temporary table pDest->iParm.
 1.14454 -**
 1.14455 -**     SRT_Table       Store results in temporary table pDest->iParm.
 1.14456 +**     SRT_Union       Store results as a key in a temporary table 
 1.14457 +**                     identified by pDest->iSDParm.
 1.14458 +**
 1.14459 +**     SRT_Except      Remove results from the temporary table pDest->iSDParm.
 1.14460 +**
 1.14461 +**     SRT_Table       Store results in temporary table pDest->iSDParm.
 1.14462  **                     This is like SRT_EphemTab except that the table
 1.14463  **                     is assumed to already be open.
 1.14464  **
 1.14465 -**     SRT_EphemTab    Create an temporary table pDest->iParm and store
 1.14466 +**     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
 1.14467  **                     the result there. The cursor is left open after
 1.14468  **                     returning.  This is like SRT_Table except that
 1.14469  **                     this destination uses OP_OpenEphemeral to create
 1.14470 @@ -99334,9 +98102,9 @@
 1.14471  **
 1.14472  **     SRT_Coroutine   Generate a co-routine that returns a new row of
 1.14473  **                     results each time it is invoked.  The entry point
 1.14474 -**                     of the co-routine is stored in register pDest->iParm.
 1.14475 -**
 1.14476 -**     SRT_Exists      Store a 1 in memory cell pDest->iParm if the result
 1.14477 +**                     of the co-routine is stored in register pDest->iSDParm.
 1.14478 +**
 1.14479 +**     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
 1.14480  **                     set is not empty.
 1.14481  **
 1.14482  **     SRT_Discard     Throw the results away.  This is used by SELECT
 1.14483 @@ -99365,11 +98133,9 @@
 1.14484    ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
 1.14485    ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
 1.14486    Expr *pHaving;         /* The HAVING clause.  May be NULL */
 1.14487 -  int isDistinct;        /* True if the DISTINCT keyword is present */
 1.14488 -  int distinct;          /* Table to use for the distinct set */
 1.14489    int rc = 1;            /* Value to return from this function */
 1.14490    int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
 1.14491 -  int addrDistinctIndex; /* Address of an OP_OpenEphemeral instruction */
 1.14492 +  DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
 1.14493    AggInfo sAggInfo;      /* Information used by aggregate queries */
 1.14494    int iEnd;              /* Address of the end of the query */
 1.14495    sqlite3 *db;           /* The database connection */
 1.14496 @@ -99429,8 +98195,17 @@
 1.14497      int isAggSub;
 1.14498  
 1.14499      if( pSub==0 ) continue;
 1.14500 +
 1.14501 +    /* Sometimes the code for a subquery will be generated more than
 1.14502 +    ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
 1.14503 +    ** for example.  In that case, do not regenerate the code to manifest
 1.14504 +    ** a view or the co-routine to implement a view.  The first instance
 1.14505 +    ** is sufficient, though the subroutine to manifest the view does need
 1.14506 +    ** to be invoked again. */
 1.14507      if( pItem->addrFillSub ){
 1.14508 -      sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
 1.14509 +      if( pItem->viaCoroutine==0 ){
 1.14510 +        sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
 1.14511 +      }
 1.14512        continue;
 1.14513      }
 1.14514  
 1.14515 @@ -99451,6 +98226,44 @@
 1.14516          p->selFlags |= SF_Aggregate;
 1.14517        }
 1.14518        i = -1;
 1.14519 +    }else if( pTabList->nSrc==1 && (p->selFlags & SF_Materialize)==0
 1.14520 +      && OptimizationEnabled(db, SQLITE_SubqCoroutine)
 1.14521 +    ){
 1.14522 +      /* Implement a co-routine that will return a single row of the result
 1.14523 +      ** set on each invocation.
 1.14524 +      */
 1.14525 +      int addrTop;
 1.14526 +      int addrEof;
 1.14527 +      pItem->regReturn = ++pParse->nMem;
 1.14528 +      addrEof = ++pParse->nMem;
 1.14529 +      /* Before coding the OP_Goto to jump to the start of the main routine,
 1.14530 +      ** ensure that the jump to the verify-schema routine has already
 1.14531 +      ** been coded. Otherwise, the verify-schema would likely be coded as 
 1.14532 +      ** part of the co-routine. If the main routine then accessed the 
 1.14533 +      ** database before invoking the co-routine for the first time (for 
 1.14534 +      ** example to initialize a LIMIT register from a sub-select), it would 
 1.14535 +      ** be doing so without having verified the schema version and obtained 
 1.14536 +      ** the required db locks. See ticket d6b36be38.  */
 1.14537 +      sqlite3CodeVerifySchema(pParse, -1);
 1.14538 +      sqlite3VdbeAddOp0(v, OP_Goto);
 1.14539 +      addrTop = sqlite3VdbeAddOp1(v, OP_OpenPseudo, pItem->iCursor);
 1.14540 +      sqlite3VdbeChangeP5(v, 1);
 1.14541 +      VdbeComment((v, "coroutine for %s", pItem->pTab->zName));
 1.14542 +      pItem->addrFillSub = addrTop;
 1.14543 +      sqlite3VdbeAddOp2(v, OP_Integer, 0, addrEof);
 1.14544 +      sqlite3VdbeChangeP5(v, 1);
 1.14545 +      sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
 1.14546 +      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
 1.14547 +      sqlite3Select(pParse, pSub, &dest);
 1.14548 +      pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
 1.14549 +      pItem->viaCoroutine = 1;
 1.14550 +      sqlite3VdbeChangeP2(v, addrTop, dest.iSdst);
 1.14551 +      sqlite3VdbeChangeP3(v, addrTop, dest.nSdst);
 1.14552 +      sqlite3VdbeAddOp2(v, OP_Integer, 1, addrEof);
 1.14553 +      sqlite3VdbeAddOp1(v, OP_Yield, pItem->regReturn);
 1.14554 +      VdbeComment((v, "end %s", pItem->pTab->zName));
 1.14555 +      sqlite3VdbeJumpHere(v, addrTop-1);
 1.14556 +      sqlite3ClearTempRegCache(pParse);
 1.14557      }else{
 1.14558        /* Generate a subroutine that will fill an ephemeral table with
 1.14559        ** the content of this subquery.  pItem->addrFillSub will point
 1.14560 @@ -99495,7 +98308,7 @@
 1.14561    pWhere = p->pWhere;
 1.14562    pGroupBy = p->pGroupBy;
 1.14563    pHaving = p->pHaving;
 1.14564 -  isDistinct = (p->selFlags & SF_Distinct)!=0;
 1.14565 +  sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
 1.14566  
 1.14567  #ifndef SQLITE_OMIT_COMPOUND_SELECT
 1.14568    /* If there is are a sequence of queries, do the earlier ones first.
 1.14569 @@ -99530,7 +98343,7 @@
 1.14570    ** to disable this optimization for testing purposes.
 1.14571    */
 1.14572    if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
 1.14573 -         && (db->flags & SQLITE_GroupByOrder)==0 ){
 1.14574 +         && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
 1.14575      pOrderBy = 0;
 1.14576    }
 1.14577  
 1.14578 @@ -99556,6 +98369,10 @@
 1.14579      p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
 1.14580      pGroupBy = p->pGroupBy;
 1.14581      pOrderBy = 0;
 1.14582 +    /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
 1.14583 +    ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
 1.14584 +    ** original setting of the SF_Distinct flag, not the current setting */
 1.14585 +    assert( sDistinct.isTnct );
 1.14586    }
 1.14587  
 1.14588    /* If there is an ORDER BY clause, then this sorting
 1.14589 @@ -99580,7 +98397,7 @@
 1.14590    /* If the output is destined for a temporary table, open that table.
 1.14591    */
 1.14592    if( pDest->eDest==SRT_EphemTab ){
 1.14593 -    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr);
 1.14594 +    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
 1.14595    }
 1.14596  
 1.14597    /* Set the limiter.
 1.14598 @@ -99596,24 +98413,27 @@
 1.14599    /* Open a virtual index to use for the distinct set.
 1.14600    */
 1.14601    if( p->selFlags & SF_Distinct ){
 1.14602 -    KeyInfo *pKeyInfo;
 1.14603 -    distinct = pParse->nTab++;
 1.14604 -    pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
 1.14605 -    addrDistinctIndex = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
 1.14606 -        (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
 1.14607 +    sDistinct.tabTnct = pParse->nTab++;
 1.14608 +    sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
 1.14609 +                                sDistinct.tabTnct, 0, 0,
 1.14610 +                                (char*)keyInfoFromExprList(pParse, p->pEList),
 1.14611 +                                P4_KEYINFO_HANDOFF);
 1.14612      sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 1.14613 -  }else{
 1.14614 -    distinct = addrDistinctIndex = -1;
 1.14615 -  }
 1.14616 -
 1.14617 -  /* Aggregate and non-aggregate queries are handled differently */
 1.14618 +    sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
 1.14619 +  }else{
 1.14620 +    sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
 1.14621 +  }
 1.14622 +
 1.14623    if( !isAgg && pGroupBy==0 ){
 1.14624 -    ExprList *pDist = (isDistinct ? p->pEList : 0);
 1.14625 +    /* No aggregate functions and no GROUP BY clause */
 1.14626 +    ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0);
 1.14627  
 1.14628      /* Begin the database scan. */
 1.14629 -    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, pDist, 0);
 1.14630 +    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0);
 1.14631      if( pWInfo==0 ) goto select_end;
 1.14632      if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
 1.14633 +    if( pWInfo->eDistinct ) sDistinct.eTnctType = pWInfo->eDistinct;
 1.14634 +    if( pOrderBy && pWInfo->nOBSat==pOrderBy->nExpr ) pOrderBy = 0;
 1.14635  
 1.14636      /* If sorting index that was created by a prior OP_OpenEphemeral 
 1.14637      ** instruction ended up not being needed, then change the OP_OpenEphemeral
 1.14638 @@ -99624,59 +98444,16 @@
 1.14639        p->addrOpenEphm[2] = -1;
 1.14640      }
 1.14641  
 1.14642 -    if( pWInfo->eDistinct ){
 1.14643 -      VdbeOp *pOp;                /* No longer required OpenEphemeral instr. */
 1.14644 -     
 1.14645 -      assert( addrDistinctIndex>=0 );
 1.14646 -      pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
 1.14647 -
 1.14648 -      assert( isDistinct );
 1.14649 -      assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED 
 1.14650 -           || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE 
 1.14651 -      );
 1.14652 -      distinct = -1;
 1.14653 -      if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED ){
 1.14654 -        int iJump;
 1.14655 -        int iExpr;
 1.14656 -        int iFlag = ++pParse->nMem;
 1.14657 -        int iBase = pParse->nMem+1;
 1.14658 -        int iBase2 = iBase + pEList->nExpr;
 1.14659 -        pParse->nMem += (pEList->nExpr*2);
 1.14660 -
 1.14661 -        /* Change the OP_OpenEphemeral coded earlier to an OP_Integer. The
 1.14662 -        ** OP_Integer initializes the "first row" flag.  */
 1.14663 -        pOp->opcode = OP_Integer;
 1.14664 -        pOp->p1 = 1;
 1.14665 -        pOp->p2 = iFlag;
 1.14666 -
 1.14667 -        sqlite3ExprCodeExprList(pParse, pEList, iBase, 1);
 1.14668 -        iJump = sqlite3VdbeCurrentAddr(v) + 1 + pEList->nExpr + 1 + 1;
 1.14669 -        sqlite3VdbeAddOp2(v, OP_If, iFlag, iJump-1);
 1.14670 -        for(iExpr=0; iExpr<pEList->nExpr; iExpr++){
 1.14671 -          CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[iExpr].pExpr);
 1.14672 -          sqlite3VdbeAddOp3(v, OP_Ne, iBase+iExpr, iJump, iBase2+iExpr);
 1.14673 -          sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
 1.14674 -          sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
 1.14675 -        }
 1.14676 -        sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iContinue);
 1.14677 -
 1.14678 -        sqlite3VdbeAddOp2(v, OP_Integer, 0, iFlag);
 1.14679 -        assert( sqlite3VdbeCurrentAddr(v)==iJump );
 1.14680 -        sqlite3VdbeAddOp3(v, OP_Move, iBase, iBase2, pEList->nExpr);
 1.14681 -      }else{
 1.14682 -        pOp->opcode = OP_Noop;
 1.14683 -      }
 1.14684 -    }
 1.14685 -
 1.14686      /* Use the standard inner loop. */
 1.14687 -    selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, pDest,
 1.14688 +    selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest,
 1.14689                      pWInfo->iContinue, pWInfo->iBreak);
 1.14690  
 1.14691      /* End the database scan loop.
 1.14692      */
 1.14693      sqlite3WhereEnd(pWInfo);
 1.14694    }else{
 1.14695 -    /* This is the processing for aggregate queries */
 1.14696 +    /* This case when there exist aggregate functions or a GROUP BY clause
 1.14697 +    ** or both */
 1.14698      NameContext sNC;    /* Name context for processing aggregate information */
 1.14699      int iAMem;          /* First Mem address for storing current GROUP BY */
 1.14700      int iBMem;          /* First Mem address for previous GROUP BY */
 1.14701 @@ -99784,14 +98561,13 @@
 1.14702        ** in the right order to begin with.
 1.14703        */
 1.14704        sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
 1.14705 -      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0, 0);
 1.14706 +      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0);
 1.14707        if( pWInfo==0 ) goto select_end;
 1.14708 -      if( pGroupBy==0 ){
 1.14709 +      if( pWInfo->nOBSat==pGroupBy->nExpr ){
 1.14710          /* The optimizer is able to deliver rows in group by order so
 1.14711          ** we do not have to sort.  The OP_OpenEphemeral table will be
 1.14712          ** cancelled later because we still need to use the pKeyInfo
 1.14713          */
 1.14714 -        pGroupBy = p->pGroupBy;
 1.14715          groupBySort = 0;
 1.14716        }else{
 1.14717          /* Rows are coming out in undetermined order.  We have to push
 1.14718 @@ -99805,7 +98581,8 @@
 1.14719          int nGroupBy;
 1.14720  
 1.14721          explainTempTable(pParse, 
 1.14722 -            isDistinct && !(p->selFlags&SF_Distinct)?"DISTINCT":"GROUP BY");
 1.14723 +            (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
 1.14724 +                    "DISTINCT" : "GROUP BY");
 1.14725  
 1.14726          groupBySort = 1;
 1.14727          nGroupBy = pGroupBy->nExpr;
 1.14728 @@ -99937,7 +98714,7 @@
 1.14729        finalizeAggFunctions(pParse, &sAggInfo);
 1.14730        sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
 1.14731        selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
 1.14732 -                      distinct, pDest,
 1.14733 +                      &sDistinct, pDest,
 1.14734                        addrOutputRow+1, addrSetAbort);
 1.14735        sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
 1.14736        VdbeComment((v, "end groupby result generator"));
 1.14737 @@ -100040,6 +98817,7 @@
 1.14738          u8 flag = minMaxQuery(p);
 1.14739          if( flag ){
 1.14740            assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
 1.14741 +          assert( p->pEList->a[0].pExpr->x.pList->nExpr==1 );
 1.14742            pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
 1.14743            pDel = pMinMax;
 1.14744            if( pMinMax && !db->mallocFailed ){
 1.14745 @@ -100053,13 +98831,14 @@
 1.14746          ** of output.
 1.14747          */
 1.14748          resetAccumulator(pParse, &sAggInfo);
 1.14749 -        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax, 0, flag);
 1.14750 +        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
 1.14751          if( pWInfo==0 ){
 1.14752            sqlite3ExprListDelete(db, pDel);
 1.14753            goto select_end;
 1.14754          }
 1.14755          updateAccumulator(pParse, &sAggInfo);
 1.14756 -        if( !pMinMax && flag ){
 1.14757 +        assert( pMinMax==0 || pMinMax->nExpr==1 );
 1.14758 +        if( pWInfo->nOBSat>0 ){
 1.14759            sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
 1.14760            VdbeComment((v, "%s() by index",
 1.14761                  (flag==WHERE_ORDERBY_MIN?"min":"max")));
 1.14762 @@ -100070,7 +98849,7 @@
 1.14763  
 1.14764        pOrderBy = 0;
 1.14765        sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
 1.14766 -      selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1, 
 1.14767 +      selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, 0, 
 1.14768                        pDest, addrEnd, addrEnd);
 1.14769        sqlite3ExprListDelete(db, pDel);
 1.14770      }
 1.14771 @@ -100078,7 +98857,7 @@
 1.14772      
 1.14773    } /* endif aggregate query */
 1.14774  
 1.14775 -  if( distinct>=0 ){
 1.14776 +  if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
 1.14777      explainTempTable(pParse, "DISTINCT");
 1.14778    }
 1.14779  
 1.14780 @@ -100525,7 +99304,7 @@
 1.14781      iDb = 1;
 1.14782      pName = pName1;
 1.14783    }else{
 1.14784 -    /* Figure out the db that the the trigger will be created in */
 1.14785 +    /* Figure out the db that the trigger will be created in */
 1.14786      iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 1.14787      if( iDb<0 ){
 1.14788        goto trigger_cleanup;
 1.14789 @@ -101143,6 +99922,15 @@
 1.14790      */
 1.14791      pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
 1.14792  
 1.14793 +    /* Clear the cookieGoto flag. When coding triggers, the cookieGoto 
 1.14794 +    ** variable is used as a flag to indicate to sqlite3ExprCodeConstants()
 1.14795 +    ** that it is not safe to refactor constants (this happens after the
 1.14796 +    ** start of the first loop in the SQL statement is coded - at that 
 1.14797 +    ** point code may be conditionally executed, so it is no longer safe to 
 1.14798 +    ** initialize constant register values).  */
 1.14799 +    assert( pParse->cookieGoto==0 || pParse->cookieGoto==-1 );
 1.14800 +    pParse->cookieGoto = 0;
 1.14801 +
 1.14802      switch( pStep->op ){
 1.14803        case TK_UPDATE: {
 1.14804          sqlite3Update(pParse, 
 1.14805 @@ -101853,7 +100641,7 @@
 1.14806    */
 1.14807    sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
 1.14808    pWInfo = sqlite3WhereBegin(
 1.14809 -      pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED
 1.14810 +      pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, 0
 1.14811    );
 1.14812    if( pWInfo==0 ) goto update_cleanup;
 1.14813    okOnePass = pWInfo->okOnePass;
 1.14814 @@ -102299,6 +101087,7 @@
 1.14815    Vdbe *v = sqlite3GetVdbe(pParse);
 1.14816    if( v ){
 1.14817      sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
 1.14818 +    sqlite3VdbeUsesBtree(v, 0);
 1.14819    }
 1.14820    return;
 1.14821  }
 1.14822 @@ -102694,7 +101483,7 @@
 1.14823  
 1.14824    assert( db );
 1.14825    assert( pVTab->nRef>0 );
 1.14826 -  assert( sqlite3SafetyCheckOk(db) );
 1.14827 +  assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
 1.14828  
 1.14829    pVTab->nRef--;
 1.14830    if( pVTab->nRef==0 ){
 1.14831 @@ -102827,7 +101616,7 @@
 1.14832    if( p->azModuleArg ){
 1.14833      int i;
 1.14834      for(i=0; i<p->nModuleArg; i++){
 1.14835 -      sqlite3DbFree(db, p->azModuleArg[i]);
 1.14836 +      if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
 1.14837      }
 1.14838      sqlite3DbFree(db, p->azModuleArg);
 1.14839    }
 1.14840 @@ -102887,7 +101676,7 @@
 1.14841    pTable->tabFlags |= TF_Virtual;
 1.14842    pTable->nModuleArg = 0;
 1.14843    addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
 1.14844 -  addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
 1.14845 +  addModuleArgument(db, pTable, 0);
 1.14846    addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
 1.14847    pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
 1.14848  
 1.14849 @@ -103044,6 +101833,7 @@
 1.14850    int nArg = pTab->nModuleArg;
 1.14851    char *zErr = 0;
 1.14852    char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
 1.14853 +  int iDb;
 1.14854  
 1.14855    if( !zModuleName ){
 1.14856      return SQLITE_NOMEM;
 1.14857 @@ -103057,6 +101847,9 @@
 1.14858    pVTable->db = db;
 1.14859    pVTable->pMod = pMod;
 1.14860  
 1.14861 +  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.14862 +  pTab->azModuleArg[1] = db->aDb[iDb].zName;
 1.14863 +
 1.14864    /* Invoke the virtual table constructor */
 1.14865    assert( &db->pVtabCtx );
 1.14866    assert( xConstruct );
 1.14867 @@ -103091,7 +101884,7 @@
 1.14868        /* If everything went according to plan, link the new VTable structure
 1.14869        ** into the linked list headed by pTab->pVTable. Then loop through the 
 1.14870        ** columns of the table to see if any of them contain the token "hidden".
 1.14871 -      ** If so, set the Column.isHidden flag and remove the token from
 1.14872 +      ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
 1.14873        ** the type string.  */
 1.14874        pVTable->pNext = pTab->pVTable;
 1.14875        pTab->pVTable = pVTable;
 1.14876 @@ -103122,7 +101915,7 @@
 1.14877              assert(zType[i-1]==' ');
 1.14878              zType[i-1] = '\0';
 1.14879            }
 1.14880 -          pTab->aCol[iCol].isHidden = 1;
 1.14881 +          pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
 1.14882          }
 1.14883        }
 1.14884      }
 1.14885 @@ -103683,9 +102476,10 @@
 1.14886  ** Trace output macros
 1.14887  */
 1.14888  #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
 1.14889 -SQLITE_PRIVATE int sqlite3WhereTrace = 0;
 1.14890 -#endif
 1.14891 -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 1.14892 +/***/ int sqlite3WhereTrace = 0;
 1.14893 +#endif
 1.14894 +#if defined(SQLITE_DEBUG) \
 1.14895 +    && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
 1.14896  # define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
 1.14897  #else
 1.14898  # define WHERETRACE(X)
 1.14899 @@ -103916,14 +102710,49 @@
 1.14900  #define WHERE_TOP_LIMIT    0x00100000  /* x<EXPR or x<=EXPR constraint */
 1.14901  #define WHERE_BTM_LIMIT    0x00200000  /* x>EXPR or x>=EXPR constraint */
 1.14902  #define WHERE_BOTH_LIMIT   0x00300000  /* Both x>EXPR and x<EXPR */
 1.14903 -#define WHERE_IDX_ONLY     0x00800000  /* Use index only - omit table */
 1.14904 -#define WHERE_ORDERBY      0x01000000  /* Output will appear in correct order */
 1.14905 -#define WHERE_REVERSE      0x02000000  /* Scan in reverse order */
 1.14906 -#define WHERE_UNIQUE       0x04000000  /* Selects no more than one row */
 1.14907 +#define WHERE_IDX_ONLY     0x00400000  /* Use index only - omit table */
 1.14908 +#define WHERE_ORDERED      0x00800000  /* Output will appear in correct order */
 1.14909 +#define WHERE_REVERSE      0x01000000  /* Scan in reverse order */
 1.14910 +#define WHERE_UNIQUE       0x02000000  /* Selects no more than one row */
 1.14911 +#define WHERE_ALL_UNIQUE   0x04000000  /* This and all prior have one row */
 1.14912  #define WHERE_VIRTUALTABLE 0x08000000  /* Use virtual-table processing */
 1.14913  #define WHERE_MULTI_OR     0x10000000  /* OR using multiple indices */
 1.14914  #define WHERE_TEMP_INDEX   0x20000000  /* Uses an ephemeral index */
 1.14915  #define WHERE_DISTINCT     0x40000000  /* Correct order for DISTINCT */
 1.14916 +#define WHERE_COVER_SCAN   0x80000000  /* Full scan of a covering index */
 1.14917 +
 1.14918 +/*
 1.14919 +** This module contains many separate subroutines that work together to
 1.14920 +** find the best indices to use for accessing a particular table in a query.
 1.14921 +** An instance of the following structure holds context information about the
 1.14922 +** index search so that it can be more easily passed between the various
 1.14923 +** routines.
 1.14924 +*/
 1.14925 +typedef struct WhereBestIdx WhereBestIdx;
 1.14926 +struct WhereBestIdx {
 1.14927 +  Parse *pParse;                  /* Parser context */
 1.14928 +  WhereClause *pWC;               /* The WHERE clause */
 1.14929 +  struct SrcList_item *pSrc;      /* The FROM clause term to search */
 1.14930 +  Bitmask notReady;               /* Mask of cursors not available */
 1.14931 +  Bitmask notValid;               /* Cursors not available for any purpose */
 1.14932 +  ExprList *pOrderBy;             /* The ORDER BY clause */
 1.14933 +  ExprList *pDistinct;            /* The select-list if query is DISTINCT */
 1.14934 +  sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
 1.14935 +  int i, n;                       /* Which loop is being coded; # of loops */
 1.14936 +  WhereLevel *aLevel;             /* Info about outer loops */
 1.14937 +  WhereCost cost;                 /* Lowest cost query plan */
 1.14938 +};
 1.14939 +
 1.14940 +/*
 1.14941 +** Return TRUE if the probe cost is less than the baseline cost
 1.14942 +*/
 1.14943 +static int compareCost(const WhereCost *pProbe, const WhereCost *pBaseline){
 1.14944 +  if( pProbe->rCost<pBaseline->rCost ) return 1;
 1.14945 +  if( pProbe->rCost>pBaseline->rCost ) return 0;
 1.14946 +  if( pProbe->plan.nOBSat>pBaseline->plan.nOBSat ) return 1;
 1.14947 +  if( pProbe->plan.nRow<pBaseline->plan.nRow ) return 1;
 1.14948 +  return 0;
 1.14949 +}
 1.14950  
 1.14951  /*
 1.14952  ** Initialize a preallocated WhereClause structure.
 1.14953 @@ -104027,7 +102856,7 @@
 1.14954      pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
 1.14955    }
 1.14956    pTerm = &pWC->a[idx = pWC->nTerm++];
 1.14957 -  pTerm->pExpr = p;
 1.14958 +  pTerm->pExpr = sqlite3ExprSkipCollate(p);
 1.14959    pTerm->wtFlags = wtFlags;
 1.14960    pTerm->pWC = pWC;
 1.14961    pTerm->iParent = -1;
 1.14962 @@ -104187,23 +103016,32 @@
 1.14963  ** Commute a comparison operator.  Expressions of the form "X op Y"
 1.14964  ** are converted into "Y op X".
 1.14965  **
 1.14966 -** If a collation sequence is associated with either the left or right
 1.14967 +** If left/right precendence rules come into play when determining the
 1.14968 +** collating
 1.14969  ** side of the comparison, it remains associated with the same side after
 1.14970  ** the commutation. So "Y collate NOCASE op X" becomes 
 1.14971 -** "X collate NOCASE op Y". This is because any collation sequence on
 1.14972 +** "X op Y". This is because any collation sequence on
 1.14973  ** the left hand side of a comparison overrides any collation sequence 
 1.14974 -** attached to the right. For the same reason the EP_ExpCollate flag
 1.14975 +** attached to the right. For the same reason the EP_Collate flag
 1.14976  ** is not commuted.
 1.14977  */
 1.14978  static void exprCommute(Parse *pParse, Expr *pExpr){
 1.14979 -  u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
 1.14980 -  u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
 1.14981 +  u16 expRight = (pExpr->pRight->flags & EP_Collate);
 1.14982 +  u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
 1.14983    assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
 1.14984 -  pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
 1.14985 -  pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 1.14986 -  SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
 1.14987 -  pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
 1.14988 -  pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
 1.14989 +  if( expRight==expLeft ){
 1.14990 +    /* Either X and Y both have COLLATE operator or neither do */
 1.14991 +    if( expRight ){
 1.14992 +      /* Both X and Y have COLLATE operators.  Make sure X is always
 1.14993 +      ** used by clearing the EP_Collate flag from Y. */
 1.14994 +      pExpr->pRight->flags &= ~EP_Collate;
 1.14995 +    }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
 1.14996 +      /* Neither X nor Y have COLLATE operators, but X has a non-default
 1.14997 +      ** collating sequence.  So add the EP_Collate marker on X to cause
 1.14998 +      ** it to be searched first. */
 1.14999 +      pExpr->pLeft->flags |= EP_Collate;
 1.15000 +    }
 1.15001 +  }
 1.15002    SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
 1.15003    if( pExpr->op>=TK_GT ){
 1.15004      assert( TK_LT==TK_GT+2 );
 1.15005 @@ -104280,12 +103118,12 @@
 1.15006            */
 1.15007            assert(pX->pLeft);
 1.15008            pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
 1.15009 -          assert(pColl || pParse->nErr);
 1.15010 +          if( pColl==0 ) pColl = pParse->db->pDfltColl;
 1.15011    
 1.15012            for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
 1.15013              if( NEVER(j>=pIdx->nColumn) ) return 0;
 1.15014            }
 1.15015 -          if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
 1.15016 +          if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
 1.15017          }
 1.15018          return pTerm;
 1.15019        }
 1.15020 @@ -104804,6 +103642,7 @@
 1.15021    pTerm = &pWC->a[idxTerm];
 1.15022    pMaskSet = pWC->pMaskSet;
 1.15023    pExpr = pTerm->pExpr;
 1.15024 +  assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
 1.15025    prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
 1.15026    op = pExpr->op;
 1.15027    if( op==TK_IN ){
 1.15028 @@ -104830,8 +103669,8 @@
 1.15029    pTerm->iParent = -1;
 1.15030    pTerm->eOperator = 0;
 1.15031    if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
 1.15032 -    Expr *pLeft = pExpr->pLeft;
 1.15033 -    Expr *pRight = pExpr->pRight;
 1.15034 +    Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
 1.15035 +    Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
 1.15036      if( pLeft->op==TK_COLUMN ){
 1.15037        pTerm->leftCursor = pLeft->iTable;
 1.15038        pTerm->u.leftColumn = pLeft->iColumn;
 1.15039 @@ -104859,7 +103698,7 @@
 1.15040          pNew = pTerm;
 1.15041        }
 1.15042        exprCommute(pParse, pDup);
 1.15043 -      pLeft = pDup->pLeft;
 1.15044 +      pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
 1.15045        pNew->leftCursor = pLeft->iTable;
 1.15046        pNew->u.leftColumn = pLeft->iColumn;
 1.15047        testcase( (prereqLeft | extraRight) != prereqLeft );
 1.15048 @@ -104938,7 +103777,7 @@
 1.15049      Expr *pNewExpr2;
 1.15050      int idxNew1;
 1.15051      int idxNew2;
 1.15052 -    CollSeq *pColl;    /* Collating sequence to use */
 1.15053 +    Token sCollSeqName;  /* Name of collating sequence */
 1.15054  
 1.15055      pLeft = pExpr->x.pList->a[1].pExpr;
 1.15056      pStr2 = sqlite3ExprDup(db, pStr1, 0);
 1.15057 @@ -104960,16 +103799,19 @@
 1.15058        }
 1.15059        *pC = c + 1;
 1.15060      }
 1.15061 -    pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0);
 1.15062 +    sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
 1.15063 +    sCollSeqName.n = 6;
 1.15064 +    pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
 1.15065      pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 
 1.15066 -                     sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
 1.15067 -                     pStr1, 0);
 1.15068 +           sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
 1.15069 +           pStr1, 0);
 1.15070      idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
 1.15071      testcase( idxNew1==0 );
 1.15072      exprAnalyze(pSrc, pWC, idxNew1);
 1.15073 +    pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
 1.15074      pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
 1.15075 -                     sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
 1.15076 -                     pStr2, 0);
 1.15077 +           sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
 1.15078 +           pStr2, 0);
 1.15079      idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
 1.15080      testcase( idxNew2==0 );
 1.15081      exprAnalyze(pSrc, pWC, idxNew2);
 1.15082 @@ -105067,25 +103909,6 @@
 1.15083  }
 1.15084  
 1.15085  /*
 1.15086 -** Return TRUE if any of the expressions in pList->a[iFirst...] contain
 1.15087 -** a reference to any table other than the iBase table.
 1.15088 -*/
 1.15089 -static int referencesOtherTables(
 1.15090 -  ExprList *pList,          /* Search expressions in ths list */
 1.15091 -  WhereMaskSet *pMaskSet,   /* Mapping from tables to bitmaps */
 1.15092 -  int iFirst,               /* Be searching with the iFirst-th expression */
 1.15093 -  int iBase                 /* Ignore references to this table */
 1.15094 -){
 1.15095 -  Bitmask allowed = ~getMask(pMaskSet, iBase);
 1.15096 -  while( iFirst<pList->nExpr ){
 1.15097 -    if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
 1.15098 -      return 1;
 1.15099 -    }
 1.15100 -  }
 1.15101 -  return 0;
 1.15102 -}
 1.15103 -
 1.15104 -/*
 1.15105  ** This function searches the expression list passed as the second argument
 1.15106  ** for an expression of type TK_COLUMN that refers to the same column and
 1.15107  ** uses the same collation sequence as the iCol'th column of index pIdx.
 1.15108 @@ -105106,12 +103929,12 @@
 1.15109    const char *zColl = pIdx->azColl[iCol];
 1.15110  
 1.15111    for(i=0; i<pList->nExpr; i++){
 1.15112 -    Expr *p = pList->a[i].pExpr;
 1.15113 +    Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
 1.15114      if( p->op==TK_COLUMN
 1.15115       && p->iColumn==pIdx->aiColumn[iCol]
 1.15116       && p->iTable==iBase
 1.15117      ){
 1.15118 -      CollSeq *pColl = sqlite3ExprCollSeq(pParse, p);
 1.15119 +      CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
 1.15120        if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
 1.15121          return i;
 1.15122        }
 1.15123 @@ -105144,7 +103967,8 @@
 1.15124    Bitmask mask = 0;               /* Mask of unaccounted for pDistinct exprs */
 1.15125    int i;                          /* Iterator variable */
 1.15126  
 1.15127 -  if( pIdx->zName==0 || pDistinct==0 || pDistinct->nExpr>=BMS ) return 0;
 1.15128 +  assert( pDistinct!=0 );
 1.15129 +  if( pIdx->zName==0 || pDistinct->nExpr>=BMS ) return 0;
 1.15130    testcase( pDistinct->nExpr==BMS-1 );
 1.15131  
 1.15132    /* Loop through all the expressions in the distinct list. If any of them
 1.15133 @@ -105157,7 +103981,7 @@
 1.15134    */
 1.15135    for(i=0; i<pDistinct->nExpr; i++){
 1.15136      WhereTerm *pTerm;
 1.15137 -    Expr *p = pDistinct->a[i].pExpr;
 1.15138 +    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
 1.15139      if( p->op!=TK_COLUMN ) return 0;
 1.15140      pTerm = findTerm(pWC, p->iTable, p->iColumn, ~(Bitmask)0, WO_EQ, 0);
 1.15141      if( pTerm ){
 1.15142 @@ -105209,7 +104033,7 @@
 1.15143    ** current SELECT is a correlated sub-query.
 1.15144    */
 1.15145    for(i=0; i<pDistinct->nExpr; i++){
 1.15146 -    Expr *p = pDistinct->a[i].pExpr;
 1.15147 +    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
 1.15148      if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
 1.15149    }
 1.15150  
 1.15151 @@ -105247,165 +104071,6 @@
 1.15152  }
 1.15153  
 1.15154  /*
 1.15155 -** This routine decides if pIdx can be used to satisfy the ORDER BY
 1.15156 -** clause.  If it can, it returns 1.  If pIdx cannot satisfy the
 1.15157 -** ORDER BY clause, this routine returns 0.
 1.15158 -**
 1.15159 -** pOrderBy is an ORDER BY clause from a SELECT statement.  pTab is the
 1.15160 -** left-most table in the FROM clause of that same SELECT statement and
 1.15161 -** the table has a cursor number of "base".  pIdx is an index on pTab.
 1.15162 -**
 1.15163 -** nEqCol is the number of columns of pIdx that are used as equality
 1.15164 -** constraints.  Any of these columns may be missing from the ORDER BY
 1.15165 -** clause and the match can still be a success.
 1.15166 -**
 1.15167 -** All terms of the ORDER BY that match against the index must be either
 1.15168 -** ASC or DESC.  (Terms of the ORDER BY clause past the end of a UNIQUE
 1.15169 -** index do not need to satisfy this constraint.)  The *pbRev value is
 1.15170 -** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
 1.15171 -** the ORDER BY clause is all ASC.
 1.15172 -*/
 1.15173 -static int isSortingIndex(
 1.15174 -  Parse *pParse,          /* Parsing context */
 1.15175 -  WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
 1.15176 -  Index *pIdx,            /* The index we are testing */
 1.15177 -  int base,               /* Cursor number for the table to be sorted */
 1.15178 -  ExprList *pOrderBy,     /* The ORDER BY clause */
 1.15179 -  int nEqCol,             /* Number of index columns with == constraints */
 1.15180 -  int wsFlags,            /* Index usages flags */
 1.15181 -  int *pbRev              /* Set to 1 if ORDER BY is DESC */
 1.15182 -){
 1.15183 -  int i, j;                       /* Loop counters */
 1.15184 -  int sortOrder = 0;              /* XOR of index and ORDER BY sort direction */
 1.15185 -  int nTerm;                      /* Number of ORDER BY terms */
 1.15186 -  struct ExprList_item *pTerm;    /* A term of the ORDER BY clause */
 1.15187 -  sqlite3 *db = pParse->db;
 1.15188 -
 1.15189 -  if( !pOrderBy ) return 0;
 1.15190 -  if( wsFlags & WHERE_COLUMN_IN ) return 0;
 1.15191 -  if( pIdx->bUnordered ) return 0;
 1.15192 -
 1.15193 -  nTerm = pOrderBy->nExpr;
 1.15194 -  assert( nTerm>0 );
 1.15195 -
 1.15196 -  /* Argument pIdx must either point to a 'real' named index structure, 
 1.15197 -  ** or an index structure allocated on the stack by bestBtreeIndex() to
 1.15198 -  ** represent the rowid index that is part of every table.  */
 1.15199 -  assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
 1.15200 -
 1.15201 -  /* Match terms of the ORDER BY clause against columns of
 1.15202 -  ** the index.
 1.15203 -  **
 1.15204 -  ** Note that indices have pIdx->nColumn regular columns plus
 1.15205 -  ** one additional column containing the rowid.  The rowid column
 1.15206 -  ** of the index is also allowed to match against the ORDER BY
 1.15207 -  ** clause.
 1.15208 -  */
 1.15209 -  for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
 1.15210 -    Expr *pExpr;       /* The expression of the ORDER BY pTerm */
 1.15211 -    CollSeq *pColl;    /* The collating sequence of pExpr */
 1.15212 -    int termSortOrder; /* Sort order for this term */
 1.15213 -    int iColumn;       /* The i-th column of the index.  -1 for rowid */
 1.15214 -    int iSortOrder;    /* 1 for DESC, 0 for ASC on the i-th index term */
 1.15215 -    const char *zColl; /* Name of the collating sequence for i-th index term */
 1.15216 -
 1.15217 -    pExpr = pTerm->pExpr;
 1.15218 -    if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
 1.15219 -      /* Can not use an index sort on anything that is not a column in the
 1.15220 -      ** left-most table of the FROM clause */
 1.15221 -      break;
 1.15222 -    }
 1.15223 -    pColl = sqlite3ExprCollSeq(pParse, pExpr);
 1.15224 -    if( !pColl ){
 1.15225 -      pColl = db->pDfltColl;
 1.15226 -    }
 1.15227 -    if( pIdx->zName && i<pIdx->nColumn ){
 1.15228 -      iColumn = pIdx->aiColumn[i];
 1.15229 -      if( iColumn==pIdx->pTable->iPKey ){
 1.15230 -        iColumn = -1;
 1.15231 -      }
 1.15232 -      iSortOrder = pIdx->aSortOrder[i];
 1.15233 -      zColl = pIdx->azColl[i];
 1.15234 -    }else{
 1.15235 -      iColumn = -1;
 1.15236 -      iSortOrder = 0;
 1.15237 -      zColl = pColl->zName;
 1.15238 -    }
 1.15239 -    if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
 1.15240 -      /* Term j of the ORDER BY clause does not match column i of the index */
 1.15241 -      if( i<nEqCol ){
 1.15242 -        /* If an index column that is constrained by == fails to match an
 1.15243 -        ** ORDER BY term, that is OK.  Just ignore that column of the index
 1.15244 -        */
 1.15245 -        continue;
 1.15246 -      }else if( i==pIdx->nColumn ){
 1.15247 -        /* Index column i is the rowid.  All other terms match. */
 1.15248 -        break;
 1.15249 -      }else{
 1.15250 -        /* If an index column fails to match and is not constrained by ==
 1.15251 -        ** then the index cannot satisfy the ORDER BY constraint.
 1.15252 -        */
 1.15253 -        return 0;
 1.15254 -      }
 1.15255 -    }
 1.15256 -    assert( pIdx->aSortOrder!=0 || iColumn==-1 );
 1.15257 -    assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
 1.15258 -    assert( iSortOrder==0 || iSortOrder==1 );
 1.15259 -    termSortOrder = iSortOrder ^ pTerm->sortOrder;
 1.15260 -    if( i>nEqCol ){
 1.15261 -      if( termSortOrder!=sortOrder ){
 1.15262 -        /* Indices can only be used if all ORDER BY terms past the
 1.15263 -        ** equality constraints are all either DESC or ASC. */
 1.15264 -        return 0;
 1.15265 -      }
 1.15266 -    }else{
 1.15267 -      sortOrder = termSortOrder;
 1.15268 -    }
 1.15269 -    j++;
 1.15270 -    pTerm++;
 1.15271 -    if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
 1.15272 -      /* If the indexed column is the primary key and everything matches
 1.15273 -      ** so far and none of the ORDER BY terms to the right reference other
 1.15274 -      ** tables in the join, then we are assured that the index can be used 
 1.15275 -      ** to sort because the primary key is unique and so none of the other
 1.15276 -      ** columns will make any difference
 1.15277 -      */
 1.15278 -      j = nTerm;
 1.15279 -    }
 1.15280 -  }
 1.15281 -
 1.15282 -  *pbRev = sortOrder!=0;
 1.15283 -  if( j>=nTerm ){
 1.15284 -    /* All terms of the ORDER BY clause are covered by this index so
 1.15285 -    ** this index can be used for sorting. */
 1.15286 -    return 1;
 1.15287 -  }
 1.15288 -  if( pIdx->onError!=OE_None && i==pIdx->nColumn
 1.15289 -      && (wsFlags & WHERE_COLUMN_NULL)==0
 1.15290 -      && !referencesOtherTables(pOrderBy, pMaskSet, j, base) 
 1.15291 -  ){
 1.15292 -    Column *aCol = pIdx->pTable->aCol;
 1.15293 -
 1.15294 -    /* All terms of this index match some prefix of the ORDER BY clause,
 1.15295 -    ** the index is UNIQUE, and no terms on the tail of the ORDER BY
 1.15296 -    ** refer to other tables in a join. So, assuming that the index entries
 1.15297 -    ** visited contain no NULL values, then this index delivers rows in
 1.15298 -    ** the required order.
 1.15299 -    **
 1.15300 -    ** It is not possible for any of the first nEqCol index fields to be
 1.15301 -    ** NULL (since the corresponding "=" operator in the WHERE clause would 
 1.15302 -    ** not be true). So if all remaining index columns have NOT NULL 
 1.15303 -    ** constaints attached to them, we can be confident that the visited
 1.15304 -    ** index entries are free of NULLs.  */
 1.15305 -    for(i=nEqCol; i<pIdx->nColumn; i++){
 1.15306 -      if( aCol[pIdx->aiColumn[i]].notNull==0 ) break;
 1.15307 -    }
 1.15308 -    return (i==pIdx->nColumn);
 1.15309 -  }
 1.15310 -  return 0;
 1.15311 -}
 1.15312 -
 1.15313 -/*
 1.15314  ** Prepare a crude estimate of the logarithm of the input value.
 1.15315  ** The results need not be exact.  This is only used for estimating
 1.15316  ** the total cost of performing operations with O(logN) or O(NlogN)
 1.15317 @@ -105469,9 +104134,7 @@
 1.15318  /* 
 1.15319  ** Required because bestIndex() is called by bestOrClauseIndex() 
 1.15320  */
 1.15321 -static void bestIndex(
 1.15322 -    Parse*, WhereClause*, struct SrcList_item*,
 1.15323 -    Bitmask, Bitmask, ExprList*, WhereCost*);
 1.15324 +static void bestIndex(WhereBestIdx*);
 1.15325  
 1.15326  /*
 1.15327  ** This routine attempts to find an scanning strategy that can be used 
 1.15328 @@ -105480,20 +104143,14 @@
 1.15329  ** The table associated with FROM clause term pSrc may be either a
 1.15330  ** regular B-Tree table or a virtual table.
 1.15331  */
 1.15332 -static void bestOrClauseIndex(
 1.15333 -  Parse *pParse,              /* The parsing context */
 1.15334 -  WhereClause *pWC,           /* The WHERE clause */
 1.15335 -  struct SrcList_item *pSrc,  /* The FROM clause term to search */
 1.15336 -  Bitmask notReady,           /* Mask of cursors not available for indexing */
 1.15337 -  Bitmask notValid,           /* Cursors not available for any purpose */
 1.15338 -  ExprList *pOrderBy,         /* The ORDER BY clause */
 1.15339 -  WhereCost *pCost            /* Lowest cost query plan */
 1.15340 -){
 1.15341 +static void bestOrClauseIndex(WhereBestIdx *p){
 1.15342  #ifndef SQLITE_OMIT_OR_OPTIMIZATION
 1.15343 -  const int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
 1.15344 +  WhereClause *pWC = p->pWC;           /* The WHERE clause */
 1.15345 +  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
 1.15346 +  const int iCur = pSrc->iCursor;      /* The cursor of the table  */
 1.15347    const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
 1.15348    WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
 1.15349 -  WhereTerm *pTerm;                 /* A single term of the WHERE clause */
 1.15350 +  WhereTerm *pTerm;                    /* A single term of the WHERE clause */
 1.15351  
 1.15352    /* The OR-clause optimization is disallowed if the INDEXED BY or
 1.15353    ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
 1.15354 @@ -105507,7 +104164,7 @@
 1.15355    /* Search the WHERE clause terms for a usable WO_OR term. */
 1.15356    for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
 1.15357      if( pTerm->eOperator==WO_OR 
 1.15358 -     && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
 1.15359 +     && ((pTerm->prereqAll & ~maskSrc) & p->notReady)==0
 1.15360       && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 
 1.15361      ){
 1.15362        WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
 1.15363 @@ -105517,15 +104174,19 @@
 1.15364        double rTotal = 0;
 1.15365        double nRow = 0;
 1.15366        Bitmask used = 0;
 1.15367 -
 1.15368 +      WhereBestIdx sBOI;
 1.15369 +
 1.15370 +      sBOI = *p;
 1.15371 +      sBOI.pOrderBy = 0;
 1.15372 +      sBOI.pDistinct = 0;
 1.15373 +      sBOI.ppIdxInfo = 0;
 1.15374        for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
 1.15375 -        WhereCost sTermCost;
 1.15376          WHERETRACE(("... Multi-index OR testing for term %d of %d....\n", 
 1.15377            (pOrTerm - pOrWC->a), (pTerm - pWC->a)
 1.15378          ));
 1.15379          if( pOrTerm->eOperator==WO_AND ){
 1.15380 -          WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
 1.15381 -          bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
 1.15382 +          sBOI.pWC = &pOrTerm->u.pAndInfo->wc;
 1.15383 +          bestIndex(&sBOI);
 1.15384          }else if( pOrTerm->leftCursor==iCur ){
 1.15385            WhereClause tempWC;
 1.15386            tempWC.pParse = pWC->pParse;
 1.15387 @@ -105535,19 +104196,20 @@
 1.15388            tempWC.a = pOrTerm;
 1.15389            tempWC.wctrlFlags = 0;
 1.15390            tempWC.nTerm = 1;
 1.15391 -          bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
 1.15392 +          sBOI.pWC = &tempWC;
 1.15393 +          bestIndex(&sBOI);
 1.15394          }else{
 1.15395            continue;
 1.15396          }
 1.15397 -        rTotal += sTermCost.rCost;
 1.15398 -        nRow += sTermCost.plan.nRow;
 1.15399 -        used |= sTermCost.used;
 1.15400 -        if( rTotal>=pCost->rCost ) break;
 1.15401 +        rTotal += sBOI.cost.rCost;
 1.15402 +        nRow += sBOI.cost.plan.nRow;
 1.15403 +        used |= sBOI.cost.used;
 1.15404 +        if( rTotal>=p->cost.rCost ) break;
 1.15405        }
 1.15406  
 1.15407        /* If there is an ORDER BY clause, increase the scan cost to account 
 1.15408        ** for the cost of the sort. */
 1.15409 -      if( pOrderBy!=0 ){
 1.15410 +      if( p->pOrderBy!=0 ){
 1.15411          WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
 1.15412                      rTotal, rTotal+nRow*estLog(nRow)));
 1.15413          rTotal += nRow*estLog(nRow);
 1.15414 @@ -105557,12 +104219,13 @@
 1.15415        ** less than the current cost stored in pCost, replace the contents
 1.15416        ** of pCost. */
 1.15417        WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
 1.15418 -      if( rTotal<pCost->rCost ){
 1.15419 -        pCost->rCost = rTotal;
 1.15420 -        pCost->used = used;
 1.15421 -        pCost->plan.nRow = nRow;
 1.15422 -        pCost->plan.wsFlags = flags;
 1.15423 -        pCost->plan.u.pTerm = pTerm;
 1.15424 +      if( rTotal<p->cost.rCost ){
 1.15425 +        p->cost.rCost = rTotal;
 1.15426 +        p->cost.used = used;
 1.15427 +        p->cost.plan.nRow = nRow;
 1.15428 +        p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
 1.15429 +        p->cost.plan.wsFlags = flags;
 1.15430 +        p->cost.plan.u.pTerm = pTerm;
 1.15431        }
 1.15432      }
 1.15433    }
 1.15434 @@ -105599,15 +104262,12 @@
 1.15435  ** is taken into account, then alter the query plan to use the
 1.15436  ** transient index.
 1.15437  */
 1.15438 -static void bestAutomaticIndex(
 1.15439 -  Parse *pParse,              /* The parsing context */
 1.15440 -  WhereClause *pWC,           /* The WHERE clause */
 1.15441 -  struct SrcList_item *pSrc,  /* The FROM clause term to search */
 1.15442 -  Bitmask notReady,           /* Mask of cursors that are not available */
 1.15443 -  WhereCost *pCost            /* Lowest cost query plan */
 1.15444 -){
 1.15445 -  double nTableRow;           /* Rows in the input table */
 1.15446 -  double logN;                /* log(nTableRow) */
 1.15447 +static void bestAutomaticIndex(WhereBestIdx *p){
 1.15448 +  Parse *pParse = p->pParse;            /* The parsing context */
 1.15449 +  WhereClause *pWC = p->pWC;            /* The WHERE clause */
 1.15450 +  struct SrcList_item *pSrc = p->pSrc;  /* The FROM clause term to search */
 1.15451 +  double nTableRow;                     /* Rows in the input table */
 1.15452 +  double logN;                          /* log(nTableRow) */
 1.15453    double costTempIdx;         /* per-query cost of the transient index */
 1.15454    WhereTerm *pTerm;           /* A single term of the WHERE clause */
 1.15455    WhereTerm *pWCEnd;          /* End of pWC->a[] */
 1.15456 @@ -105621,10 +104281,16 @@
 1.15457      /* Automatic indices are disabled at run-time */
 1.15458      return;
 1.15459    }
 1.15460 -  if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
 1.15461 +  if( (p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0
 1.15462 +   && (p->cost.plan.wsFlags & WHERE_COVER_SCAN)==0
 1.15463 +  ){
 1.15464      /* We already have some kind of index in use for this query. */
 1.15465      return;
 1.15466    }
 1.15467 +  if( pSrc->viaCoroutine ){
 1.15468 +    /* Cannot index a co-routine */
 1.15469 +    return;
 1.15470 +  }
 1.15471    if( pSrc->notIndexed ){
 1.15472      /* The NOT INDEXED clause appears in the SQL. */
 1.15473      return;
 1.15474 @@ -105639,7 +104305,7 @@
 1.15475    nTableRow = pTable->nRowEst;
 1.15476    logN = estLog(nTableRow);
 1.15477    costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
 1.15478 -  if( costTempIdx>=pCost->rCost ){
 1.15479 +  if( costTempIdx>=p->cost.rCost ){
 1.15480      /* The cost of creating the transient table would be greater than
 1.15481      ** doing the full table scan */
 1.15482      return;
 1.15483 @@ -105648,19 +104314,19 @@
 1.15484    /* Search for any equality comparison term */
 1.15485    pWCEnd = &pWC->a[pWC->nTerm];
 1.15486    for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
 1.15487 -    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
 1.15488 +    if( termCanDriveIndex(pTerm, pSrc, p->notReady) ){
 1.15489        WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
 1.15490 -                    pCost->rCost, costTempIdx));
 1.15491 -      pCost->rCost = costTempIdx;
 1.15492 -      pCost->plan.nRow = logN + 1;
 1.15493 -      pCost->plan.wsFlags = WHERE_TEMP_INDEX;
 1.15494 -      pCost->used = pTerm->prereqRight;
 1.15495 -      break;
 1.15496 -    }
 1.15497 -  }
 1.15498 -}
 1.15499 -#else
 1.15500 -# define bestAutomaticIndex(A,B,C,D,E)  /* no-op */
 1.15501 +                    p->cost.rCost, costTempIdx));
 1.15502 +      p->cost.rCost = costTempIdx;
 1.15503 +      p->cost.plan.nRow = logN + 1;
 1.15504 +      p->cost.plan.wsFlags = WHERE_TEMP_INDEX;
 1.15505 +      p->cost.used = pTerm->prereqRight;
 1.15506 +      break;
 1.15507 +    }
 1.15508 +  }
 1.15509 +}
 1.15510 +#else
 1.15511 +# define bestAutomaticIndex(A)  /* no-op */
 1.15512  #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
 1.15513  
 1.15514  
 1.15515 @@ -105821,12 +104487,11 @@
 1.15516  ** responsibility of the caller to eventually release the structure
 1.15517  ** by passing the pointer returned by this function to sqlite3_free().
 1.15518  */
 1.15519 -static sqlite3_index_info *allocateIndexInfo(
 1.15520 -  Parse *pParse, 
 1.15521 -  WhereClause *pWC,
 1.15522 -  struct SrcList_item *pSrc,
 1.15523 -  ExprList *pOrderBy
 1.15524 -){
 1.15525 +static sqlite3_index_info *allocateIndexInfo(WhereBestIdx *p){
 1.15526 +  Parse *pParse = p->pParse; 
 1.15527 +  WhereClause *pWC = p->pWC;
 1.15528 +  struct SrcList_item *pSrc = p->pSrc;
 1.15529 +  ExprList *pOrderBy = p->pOrderBy;
 1.15530    int i, j;
 1.15531    int nTerm;
 1.15532    struct sqlite3_index_constraint *pIdxCons;
 1.15533 @@ -105856,12 +104521,13 @@
 1.15534    */
 1.15535    nOrderBy = 0;
 1.15536    if( pOrderBy ){
 1.15537 -    for(i=0; i<pOrderBy->nExpr; i++){
 1.15538 +    int n = pOrderBy->nExpr;
 1.15539 +    for(i=0; i<n; i++){
 1.15540        Expr *pExpr = pOrderBy->a[i].pExpr;
 1.15541        if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
 1.15542      }
 1.15543 -    if( i==pOrderBy->nExpr ){
 1.15544 -      nOrderBy = pOrderBy->nExpr;
 1.15545 +    if( i==n){
 1.15546 +      nOrderBy = n;
 1.15547      }
 1.15548    }
 1.15549  
 1.15550 @@ -105985,16 +104651,10 @@
 1.15551  ** routine takes care of freeing the sqlite3_index_info structure after
 1.15552  ** everybody has finished with it.
 1.15553  */
 1.15554 -static void bestVirtualIndex(
 1.15555 -  Parse *pParse,                  /* The parsing context */
 1.15556 -  WhereClause *pWC,               /* The WHERE clause */
 1.15557 -  struct SrcList_item *pSrc,      /* The FROM clause term to search */
 1.15558 -  Bitmask notReady,               /* Mask of cursors not available for index */
 1.15559 -  Bitmask notValid,               /* Cursors not valid for any purpose */
 1.15560 -  ExprList *pOrderBy,             /* The order by clause */
 1.15561 -  WhereCost *pCost,               /* Lowest cost query plan */
 1.15562 -  sqlite3_index_info **ppIdxInfo  /* Index information passed to xBestIndex */
 1.15563 -){
 1.15564 +static void bestVirtualIndex(WhereBestIdx *p){
 1.15565 +  Parse *pParse = p->pParse;      /* The parsing context */
 1.15566 +  WhereClause *pWC = p->pWC;      /* The WHERE clause */
 1.15567 +  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
 1.15568    Table *pTab = pSrc->pTab;
 1.15569    sqlite3_index_info *pIdxInfo;
 1.15570    struct sqlite3_index_constraint *pIdxCons;
 1.15571 @@ -106008,15 +104668,15 @@
 1.15572    ** malloc in allocateIndexInfo() fails and this function returns leaving
 1.15573    ** wsFlags in an uninitialized state, the caller may behave unpredictably.
 1.15574    */
 1.15575 -  memset(pCost, 0, sizeof(*pCost));
 1.15576 -  pCost->plan.wsFlags = WHERE_VIRTUALTABLE;
 1.15577 +  memset(&p->cost, 0, sizeof(p->cost));
 1.15578 +  p->cost.plan.wsFlags = WHERE_VIRTUALTABLE;
 1.15579  
 1.15580    /* If the sqlite3_index_info structure has not been previously
 1.15581    ** allocated and initialized, then allocate and initialize it now.
 1.15582    */
 1.15583 -  pIdxInfo = *ppIdxInfo;
 1.15584 +  pIdxInfo = *p->ppIdxInfo;
 1.15585    if( pIdxInfo==0 ){
 1.15586 -    *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
 1.15587 +    *p->ppIdxInfo = pIdxInfo = allocateIndexInfo(p);
 1.15588    }
 1.15589    if( pIdxInfo==0 ){
 1.15590      return;
 1.15591 @@ -106061,7 +104721,7 @@
 1.15592    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
 1.15593      j = pIdxCons->iTermOffset;
 1.15594      pTerm = &pWC->a[j];
 1.15595 -    pIdxCons->usable = (pTerm->prereqRight&notReady) ? 0 : 1;
 1.15596 +    pIdxCons->usable = (pTerm->prereqRight&p->notReady) ? 0 : 1;
 1.15597    }
 1.15598    memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
 1.15599    if( pIdxInfo->needToFreeIdxStr ){
 1.15600 @@ -106074,7 +104734,7 @@
 1.15601    /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
 1.15602    pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
 1.15603    nOrderBy = pIdxInfo->nOrderBy;
 1.15604 -  if( !pOrderBy ){
 1.15605 +  if( !p->pOrderBy ){
 1.15606      pIdxInfo->nOrderBy = 0;
 1.15607    }
 1.15608  
 1.15609 @@ -106085,7 +104745,7 @@
 1.15610    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
 1.15611    for(i=0; i<pIdxInfo->nConstraint; i++){
 1.15612      if( pUsage[i].argvIndex>0 ){
 1.15613 -      pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
 1.15614 +      p->cost.used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
 1.15615      }
 1.15616    }
 1.15617  
 1.15618 @@ -106094,7 +104754,7 @@
 1.15619    ** matches the processing for non-virtual tables in bestBtreeIndex().
 1.15620    */
 1.15621    rCost = pIdxInfo->estimatedCost;
 1.15622 -  if( pOrderBy && pIdxInfo->orderByConsumed==0 ){
 1.15623 +  if( p->pOrderBy && pIdxInfo->orderByConsumed==0 ){
 1.15624      rCost += estLog(rCost)*rCost;
 1.15625    }
 1.15626  
 1.15627 @@ -106106,21 +104766,24 @@
 1.15628    ** is defined.
 1.15629    */
 1.15630    if( (SQLITE_BIG_DBL/((double)2))<rCost ){
 1.15631 -    pCost->rCost = (SQLITE_BIG_DBL/((double)2));
 1.15632 -  }else{
 1.15633 -    pCost->rCost = rCost;
 1.15634 -  }
 1.15635 -  pCost->plan.u.pVtabIdx = pIdxInfo;
 1.15636 +    p->cost.rCost = (SQLITE_BIG_DBL/((double)2));
 1.15637 +  }else{
 1.15638 +    p->cost.rCost = rCost;
 1.15639 +  }
 1.15640 +  p->cost.plan.u.pVtabIdx = pIdxInfo;
 1.15641    if( pIdxInfo->orderByConsumed ){
 1.15642 -    pCost->plan.wsFlags |= WHERE_ORDERBY;
 1.15643 -  }
 1.15644 -  pCost->plan.nEq = 0;
 1.15645 +    p->cost.plan.wsFlags |= WHERE_ORDERED;
 1.15646 +    p->cost.plan.nOBSat = nOrderBy;
 1.15647 +  }else{
 1.15648 +    p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
 1.15649 +  }
 1.15650 +  p->cost.plan.nEq = 0;
 1.15651    pIdxInfo->nOrderBy = nOrderBy;
 1.15652  
 1.15653    /* Try to find a more efficient access pattern by using multiple indexes
 1.15654    ** to optimize an OR expression within the WHERE clause. 
 1.15655    */
 1.15656 -  bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
 1.15657 +  bestOrClauseIndex(p);
 1.15658  }
 1.15659  #endif /* SQLITE_OMIT_VIRTUALTABLE */
 1.15660  
 1.15661 @@ -106208,10 +104871,8 @@
 1.15662          pColl = db->pDfltColl;
 1.15663          assert( pColl->enc==SQLITE_UTF8 );
 1.15664        }else{
 1.15665 -        pColl = sqlite3GetCollSeq(db, SQLITE_UTF8, 0, *pIdx->azColl);
 1.15666 +        pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl);
 1.15667          if( pColl==0 ){
 1.15668 -          sqlite3ErrorMsg(pParse, "no such collation sequence: %s",
 1.15669 -                          *pIdx->azColl);
 1.15670            return SQLITE_ERROR;
 1.15671          }
 1.15672          z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
 1.15673 @@ -106519,11 +105180,275 @@
 1.15674  }
 1.15675  #endif /* defined(SQLITE_ENABLE_STAT3) */
 1.15676  
 1.15677 +/*
 1.15678 +** Check to see if column iCol of the table with cursor iTab will appear
 1.15679 +** in sorted order according to the current query plan.
 1.15680 +**
 1.15681 +** Return values:
 1.15682 +**
 1.15683 +**    0   iCol is not ordered
 1.15684 +**    1   iCol has only a single value
 1.15685 +**    2   iCol is in ASC order
 1.15686 +**    3   iCol is in DESC order
 1.15687 +*/
 1.15688 +static int isOrderedColumn(
 1.15689 +  WhereBestIdx *p,
 1.15690 +  int iTab,
 1.15691 +  int iCol
 1.15692 +){
 1.15693 +  int i, j;
 1.15694 +  WhereLevel *pLevel = &p->aLevel[p->i-1];
 1.15695 +  Index *pIdx;
 1.15696 +  u8 sortOrder;
 1.15697 +  for(i=p->i-1; i>=0; i--, pLevel--){
 1.15698 +    if( pLevel->iTabCur!=iTab ) continue;
 1.15699 +    if( (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
 1.15700 +      return 1;
 1.15701 +    }
 1.15702 +    assert( (pLevel->plan.wsFlags & WHERE_ORDERED)!=0 );
 1.15703 +    if( (pIdx = pLevel->plan.u.pIdx)!=0 ){
 1.15704 +      if( iCol<0 ){
 1.15705 +        sortOrder = 0;
 1.15706 +        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
 1.15707 +      }else{
 1.15708 +        int n = pIdx->nColumn;
 1.15709 +        for(j=0; j<n; j++){
 1.15710 +          if( iCol==pIdx->aiColumn[j] ) break;
 1.15711 +        }
 1.15712 +        if( j>=n ) return 0;
 1.15713 +        sortOrder = pIdx->aSortOrder[j];
 1.15714 +        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
 1.15715 +      }
 1.15716 +    }else{
 1.15717 +      if( iCol!=(-1) ) return 0;
 1.15718 +      sortOrder = 0;
 1.15719 +      testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
 1.15720 +    }
 1.15721 +    if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ){
 1.15722 +      assert( sortOrder==0 || sortOrder==1 );
 1.15723 +      testcase( sortOrder==1 );
 1.15724 +      sortOrder = 1 - sortOrder;
 1.15725 +    }
 1.15726 +    return sortOrder+2;
 1.15727 +  }
 1.15728 +  return 0;
 1.15729 +}
 1.15730 +
 1.15731 +/*
 1.15732 +** This routine decides if pIdx can be used to satisfy the ORDER BY
 1.15733 +** clause, either in whole or in part.  The return value is the 
 1.15734 +** cumulative number of terms in the ORDER BY clause that are satisfied
 1.15735 +** by the index pIdx and other indices in outer loops.
 1.15736 +**
 1.15737 +** The table being queried has a cursor number of "base".  pIdx is the
 1.15738 +** index that is postulated for use to access the table.
 1.15739 +**
 1.15740 +** The *pbRev value is set to 0 order 1 depending on whether or not
 1.15741 +** pIdx should be run in the forward order or in reverse order.
 1.15742 +*/
 1.15743 +static int isSortingIndex(
 1.15744 +  WhereBestIdx *p,    /* Best index search context */
 1.15745 +  Index *pIdx,        /* The index we are testing */
 1.15746 +  int base,           /* Cursor number for the table to be sorted */
 1.15747 +  int *pbRev          /* Set to 1 for reverse-order scan of pIdx */
 1.15748 +){
 1.15749 +  int i;                        /* Number of pIdx terms used */
 1.15750 +  int j;                        /* Number of ORDER BY terms satisfied */
 1.15751 +  int sortOrder = 2;            /* 0: forward.  1: backward.  2: unknown */
 1.15752 +  int nTerm;                    /* Number of ORDER BY terms */
 1.15753 +  struct ExprList_item *pOBItem;/* A term of the ORDER BY clause */
 1.15754 +  Table *pTab = pIdx->pTable;   /* Table that owns index pIdx */
 1.15755 +  ExprList *pOrderBy;           /* The ORDER BY clause */
 1.15756 +  Parse *pParse = p->pParse;    /* Parser context */
 1.15757 +  sqlite3 *db = pParse->db;     /* Database connection */
 1.15758 +  int nPriorSat;                /* ORDER BY terms satisfied by outer loops */
 1.15759 +  int seenRowid = 0;            /* True if an ORDER BY rowid term is seen */
 1.15760 +  int uniqueNotNull;            /* pIdx is UNIQUE with all terms are NOT NULL */
 1.15761 +
 1.15762 +  if( p->i==0 ){
 1.15763 +    nPriorSat = 0;
 1.15764 +  }else{
 1.15765 +    nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
 1.15766 +    if( (p->aLevel[p->i-1].plan.wsFlags & WHERE_ORDERED)==0 ){
 1.15767 +      /* This loop cannot be ordered unless the next outer loop is
 1.15768 +      ** also ordered */
 1.15769 +      return nPriorSat;
 1.15770 +    }
 1.15771 +    if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ){
 1.15772 +      /* Only look at the outer-most loop if the OrderByIdxJoin
 1.15773 +      ** optimization is disabled */
 1.15774 +      return nPriorSat;
 1.15775 +    }
 1.15776 +  }
 1.15777 +  pOrderBy = p->pOrderBy;
 1.15778 +  assert( pOrderBy!=0 );
 1.15779 +  if( pIdx->bUnordered ){
 1.15780 +    /* Hash indices (indicated by the "unordered" tag on sqlite_stat1) cannot
 1.15781 +    ** be used for sorting */
 1.15782 +    return nPriorSat;
 1.15783 +  }
 1.15784 +  nTerm = pOrderBy->nExpr;
 1.15785 +  uniqueNotNull = pIdx->onError!=OE_None;
 1.15786 +  assert( nTerm>0 );
 1.15787 +
 1.15788 +  /* Argument pIdx must either point to a 'real' named index structure, 
 1.15789 +  ** or an index structure allocated on the stack by bestBtreeIndex() to
 1.15790 +  ** represent the rowid index that is part of every table.  */
 1.15791 +  assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
 1.15792 +
 1.15793 +  /* Match terms of the ORDER BY clause against columns of
 1.15794 +  ** the index.
 1.15795 +  **
 1.15796 +  ** Note that indices have pIdx->nColumn regular columns plus
 1.15797 +  ** one additional column containing the rowid.  The rowid column
 1.15798 +  ** of the index is also allowed to match against the ORDER BY
 1.15799 +  ** clause.
 1.15800 +  */
 1.15801 +  j = nPriorSat;
 1.15802 +  for(i=0,pOBItem=&pOrderBy->a[j]; j<nTerm && i<=pIdx->nColumn; i++){
 1.15803 +    Expr *pOBExpr;          /* The expression of the ORDER BY pOBItem */
 1.15804 +    CollSeq *pColl;         /* The collating sequence of pOBExpr */
 1.15805 +    int termSortOrder;      /* Sort order for this term */
 1.15806 +    int iColumn;            /* The i-th column of the index.  -1 for rowid */
 1.15807 +    int iSortOrder;         /* 1 for DESC, 0 for ASC on the i-th index term */
 1.15808 +    int isEq;               /* Subject to an == or IS NULL constraint */
 1.15809 +    int isMatch;            /* ORDER BY term matches the index term */
 1.15810 +    const char *zColl;      /* Name of collating sequence for i-th index term */
 1.15811 +    WhereTerm *pConstraint; /* A constraint in the WHERE clause */
 1.15812 +
 1.15813 +    /* If the next term of the ORDER BY clause refers to anything other than
 1.15814 +    ** a column in the "base" table, then this index will not be of any
 1.15815 +    ** further use in handling the ORDER BY. */
 1.15816 +    pOBExpr = sqlite3ExprSkipCollate(pOBItem->pExpr);
 1.15817 +    if( pOBExpr->op!=TK_COLUMN || pOBExpr->iTable!=base ){
 1.15818 +      break;
 1.15819 +    }
 1.15820 +
 1.15821 +    /* Find column number and collating sequence for the next entry
 1.15822 +    ** in the index */
 1.15823 +    if( pIdx->zName && i<pIdx->nColumn ){
 1.15824 +      iColumn = pIdx->aiColumn[i];
 1.15825 +      if( iColumn==pIdx->pTable->iPKey ){
 1.15826 +        iColumn = -1;
 1.15827 +      }
 1.15828 +      iSortOrder = pIdx->aSortOrder[i];
 1.15829 +      zColl = pIdx->azColl[i];
 1.15830 +      assert( zColl!=0 );
 1.15831 +    }else{
 1.15832 +      iColumn = -1;
 1.15833 +      iSortOrder = 0;
 1.15834 +      zColl = 0;
 1.15835 +    }
 1.15836 +
 1.15837 +    /* Check to see if the column number and collating sequence of the
 1.15838 +    ** index match the column number and collating sequence of the ORDER BY
 1.15839 +    ** clause entry.  Set isMatch to 1 if they both match. */
 1.15840 +    if( pOBExpr->iColumn==iColumn ){
 1.15841 +      if( zColl ){
 1.15842 +        pColl = sqlite3ExprCollSeq(pParse, pOBItem->pExpr);
 1.15843 +        if( !pColl ) pColl = db->pDfltColl;
 1.15844 +        isMatch = sqlite3StrICmp(pColl->zName, zColl)==0;
 1.15845 +      }else{
 1.15846 +        isMatch = 1;
 1.15847 +      }
 1.15848 +    }else{
 1.15849 +      isMatch = 0;
 1.15850 +    }
 1.15851 +
 1.15852 +    /* termSortOrder is 0 or 1 for whether or not the access loop should
 1.15853 +    ** run forward or backwards (respectively) in order to satisfy this 
 1.15854 +    ** term of the ORDER BY clause. */
 1.15855 +    assert( pOBItem->sortOrder==0 || pOBItem->sortOrder==1 );
 1.15856 +    assert( iSortOrder==0 || iSortOrder==1 );
 1.15857 +    termSortOrder = iSortOrder ^ pOBItem->sortOrder;
 1.15858 +
 1.15859 +    /* If X is the column in the index and ORDER BY clause, check to see
 1.15860 +    ** if there are any X= or X IS NULL constraints in the WHERE clause. */
 1.15861 +    pConstraint = findTerm(p->pWC, base, iColumn, p->notReady,
 1.15862 +                           WO_EQ|WO_ISNULL|WO_IN, pIdx);
 1.15863 +    if( pConstraint==0 ){
 1.15864 +      isEq = 0;
 1.15865 +    }else if( pConstraint->eOperator==WO_IN ){
 1.15866 +      /* Constraints of the form: "X IN ..." cannot be used with an ORDER BY
 1.15867 +      ** because we do not know in what order the values on the RHS of the IN
 1.15868 +      ** operator will occur. */
 1.15869 +      break;
 1.15870 +    }else if( pConstraint->eOperator==WO_ISNULL ){
 1.15871 +      uniqueNotNull = 0;
 1.15872 +      isEq = 1;  /* "X IS NULL" means X has only a single value */
 1.15873 +    }else if( pConstraint->prereqRight==0 ){
 1.15874 +      isEq = 1;  /* Constraint "X=constant" means X has only a single value */
 1.15875 +    }else{
 1.15876 +      Expr *pRight = pConstraint->pExpr->pRight;
 1.15877 +      if( pRight->op==TK_COLUMN ){
 1.15878 +        WHERETRACE(("       .. isOrderedColumn(tab=%d,col=%d)",
 1.15879 +                    pRight->iTable, pRight->iColumn));
 1.15880 +        isEq = isOrderedColumn(p, pRight->iTable, pRight->iColumn);
 1.15881 +        WHERETRACE((" -> isEq=%d\n", isEq));
 1.15882 +
 1.15883 +        /* If the constraint is of the form X=Y where Y is an ordered value
 1.15884 +        ** in an outer loop, then make sure the sort order of Y matches the
 1.15885 +        ** sort order required for X. */
 1.15886 +        if( isMatch && isEq>=2 && isEq!=pOBItem->sortOrder+2 ){
 1.15887 +          testcase( isEq==2 );
 1.15888 +          testcase( isEq==3 );
 1.15889 +          break;
 1.15890 +        }
 1.15891 +      }else{
 1.15892 +        isEq = 0;  /* "X=expr" places no ordering constraints on X */
 1.15893 +      }
 1.15894 +    }
 1.15895 +    if( !isMatch ){
 1.15896 +      if( isEq==0 ){
 1.15897 +        break;
 1.15898 +      }else{
 1.15899 +        continue;
 1.15900 +      }
 1.15901 +    }else if( isEq!=1 ){
 1.15902 +      if( sortOrder==2 ){
 1.15903 +        sortOrder = termSortOrder;
 1.15904 +      }else if( termSortOrder!=sortOrder ){
 1.15905 +        break;
 1.15906 +      }
 1.15907 +    }
 1.15908 +    j++;
 1.15909 +    pOBItem++;
 1.15910 +    if( iColumn<0 ){
 1.15911 +      seenRowid = 1;
 1.15912 +      break;
 1.15913 +    }else if( pTab->aCol[iColumn].notNull==0 && isEq!=1 ){
 1.15914 +      testcase( isEq==0 );
 1.15915 +      testcase( isEq==2 );
 1.15916 +      testcase( isEq==3 );
 1.15917 +      uniqueNotNull = 0;
 1.15918 +    }
 1.15919 +  }
 1.15920 +
 1.15921 +  /* If we have not found at least one ORDER BY term that matches the
 1.15922 +  ** index, then show no progress. */
 1.15923 +  if( pOBItem==&pOrderBy->a[nPriorSat] ) return nPriorSat;
 1.15924 +
 1.15925 +  /* Return the necessary scan order back to the caller */
 1.15926 +  *pbRev = sortOrder & 1;
 1.15927 +
 1.15928 +  /* If there was an "ORDER BY rowid" term that matched, or it is only
 1.15929 +  ** possible for a single row from this table to match, then skip over
 1.15930 +  ** any additional ORDER BY terms dealing with this table.
 1.15931 +  */
 1.15932 +  if( seenRowid || (uniqueNotNull && i>=pIdx->nColumn) ){
 1.15933 +    /* Advance j over additional ORDER BY terms associated with base */
 1.15934 +    WhereMaskSet *pMS = p->pWC->pMaskSet;
 1.15935 +    Bitmask m = ~getMask(pMS, base);
 1.15936 +    while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
 1.15937 +      j++;
 1.15938 +    }
 1.15939 +  }
 1.15940 +  return j;
 1.15941 +}
 1.15942  
 1.15943  /*
 1.15944  ** Find the best query plan for accessing a particular table.  Write the
 1.15945 -** best query plan and its cost into the WhereCost object supplied as the
 1.15946 -** last parameter.
 1.15947 +** best query plan and its cost into the p->cost.
 1.15948  **
 1.15949  ** The lowest cost plan wins.  The cost is an estimate of the amount of
 1.15950  ** CPU and disk I/O needed to process the requested result.
 1.15951 @@ -106543,21 +105468,15 @@
 1.15952  ** SQLITE_BIG_DBL. If a plan is found that uses the named index, 
 1.15953  ** then the cost is calculated in the usual way.
 1.15954  **
 1.15955 -** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table 
 1.15956 +** If a NOT INDEXED clause was attached to the table 
 1.15957  ** in the SELECT statement, then no indexes are considered. However, the 
 1.15958  ** selected plan may still take advantage of the built-in rowid primary key
 1.15959  ** index.
 1.15960  */
 1.15961 -static void bestBtreeIndex(
 1.15962 -  Parse *pParse,              /* The parsing context */
 1.15963 -  WhereClause *pWC,           /* The WHERE clause */
 1.15964 -  struct SrcList_item *pSrc,  /* The FROM clause term to search */
 1.15965 -  Bitmask notReady,           /* Mask of cursors not available for indexing */
 1.15966 -  Bitmask notValid,           /* Cursors not available for any purpose */
 1.15967 -  ExprList *pOrderBy,         /* The ORDER BY clause */
 1.15968 -  ExprList *pDistinct,        /* The select-list if query is DISTINCT */
 1.15969 -  WhereCost *pCost            /* Lowest cost query plan */
 1.15970 -){
 1.15971 +static void bestBtreeIndex(WhereBestIdx *p){
 1.15972 +  Parse *pParse = p->pParse;  /* The parsing context */
 1.15973 +  WhereClause *pWC = p->pWC;  /* The WHERE clause */
 1.15974 +  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
 1.15975    int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
 1.15976    Index *pProbe;              /* An index we are evaluating */
 1.15977    Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
 1.15978 @@ -106566,11 +105485,16 @@
 1.15979    Index sPk;                  /* A fake index object for the primary key */
 1.15980    tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
 1.15981    int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
 1.15982 -  int wsFlagMask;             /* Allowed flags in pCost->plan.wsFlag */
 1.15983 +  int wsFlagMask;             /* Allowed flags in p->cost.plan.wsFlag */
 1.15984 +  int nPriorSat;              /* ORDER BY terms satisfied by outer loops */
 1.15985 +  int nOrderBy;               /* Number of ORDER BY terms */
 1.15986 +  char bSortInit;             /* Initializer for bSort in inner loop */
 1.15987 +  char bDistInit;             /* Initializer for bDist in inner loop */
 1.15988 +
 1.15989  
 1.15990    /* Initialize the cost to a worst-case value */
 1.15991 -  memset(pCost, 0, sizeof(*pCost));
 1.15992 -  pCost->rCost = SQLITE_BIG_DBL;
 1.15993 +  memset(&p->cost, 0, sizeof(p->cost));
 1.15994 +  p->cost.rCost = SQLITE_BIG_DBL;
 1.15995  
 1.15996    /* If the pSrc table is the right table of a LEFT JOIN then we may not
 1.15997    ** use an index to satisfy IS NULL constraints on that table.  This is
 1.15998 @@ -106616,22 +105540,29 @@
 1.15999      pIdx = 0;
 1.16000    }
 1.16001  
 1.16002 +  nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
 1.16003 +  if( p->i ){
 1.16004 +    nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
 1.16005 +    bSortInit = nPriorSat<nOrderBy;
 1.16006 +    bDistInit = 0;
 1.16007 +  }else{
 1.16008 +    nPriorSat = 0;
 1.16009 +    bSortInit = nOrderBy>0;
 1.16010 +    bDistInit = p->pDistinct!=0;
 1.16011 +  }
 1.16012 +
 1.16013    /* Loop over all indices looking for the best one to use
 1.16014    */
 1.16015    for(; pProbe; pIdx=pProbe=pProbe->pNext){
 1.16016      const tRowcnt * const aiRowEst = pProbe->aiRowEst;
 1.16017 -    double cost;                /* Cost of using pProbe */
 1.16018 -    double nRow;                /* Estimated number of rows in result set */
 1.16019 +    WhereCost pc;               /* Cost of using pProbe */
 1.16020      double log10N = (double)1;  /* base-10 logarithm of nRow (inexact) */
 1.16021 -    int rev;                    /* True to scan in reverse order */
 1.16022 -    int wsFlags = 0;
 1.16023 -    Bitmask used = 0;
 1.16024  
 1.16025      /* The following variables are populated based on the properties of
 1.16026      ** index being evaluated. They are then used to determine the expected
 1.16027      ** cost and number of rows returned.
 1.16028      **
 1.16029 -    **  nEq: 
 1.16030 +    **  pc.plan.nEq: 
 1.16031      **    Number of equality terms that can be implemented using the index.
 1.16032      **    In other words, the number of initial fields in the index that
 1.16033      **    are used in == or IN or NOT NULL constraints of the WHERE clause.
 1.16034 @@ -106674,6 +105605,10 @@
 1.16035      **    external sort (i.e. scanning the index being evaluated will not 
 1.16036      **    correctly order records).
 1.16037      **
 1.16038 +    **  bDist:
 1.16039 +    **    Boolean. True if there is a DISTINCT clause that will require an 
 1.16040 +    **    external btree.
 1.16041 +    **
 1.16042      **  bLookup: 
 1.16043      **    Boolean. True if a table lookup is required for each index entry
 1.16044      **    visited.  In other words, true if this is not a covering index.
 1.16045 @@ -106689,29 +105624,35 @@
 1.16046      **             SELECT a, b    FROM tbl WHERE a = 1;
 1.16047      **             SELECT a, b, c FROM tbl WHERE a = 1;
 1.16048      */
 1.16049 -    int nEq;                      /* Number of == or IN terms matching index */
 1.16050      int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
 1.16051      int nInMul = 1;               /* Number of distinct equalities to lookup */
 1.16052      double rangeDiv = (double)1;  /* Estimated reduction in search space */
 1.16053      int nBound = 0;               /* Number of range constraints seen */
 1.16054 -    int bSort = !!pOrderBy;       /* True if external sort required */
 1.16055 -    int bDist = !!pDistinct;      /* True if index cannot help with DISTINCT */
 1.16056 -    int bLookup = 0;              /* True if not a covering index */
 1.16057 +    char bSort = bSortInit;       /* True if external sort required */
 1.16058 +    char bDist = bDistInit;       /* True if index cannot help with DISTINCT */
 1.16059 +    char bLookup = 0;             /* True if not a covering index */
 1.16060      WhereTerm *pTerm;             /* A single term of the WHERE clause */
 1.16061  #ifdef SQLITE_ENABLE_STAT3
 1.16062      WhereTerm *pFirstTerm = 0;    /* First term matching the index */
 1.16063  #endif
 1.16064  
 1.16065 -    /* Determine the values of nEq and nInMul */
 1.16066 -    for(nEq=0; nEq<pProbe->nColumn; nEq++){
 1.16067 -      int j = pProbe->aiColumn[nEq];
 1.16068 -      pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
 1.16069 +    WHERETRACE((
 1.16070 +      "   %s(%s):\n",
 1.16071 +      pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk")
 1.16072 +    ));
 1.16073 +    memset(&pc, 0, sizeof(pc));
 1.16074 +    pc.plan.nOBSat = nPriorSat;
 1.16075 +
 1.16076 +    /* Determine the values of pc.plan.nEq and nInMul */
 1.16077 +    for(pc.plan.nEq=0; pc.plan.nEq<pProbe->nColumn; pc.plan.nEq++){
 1.16078 +      int j = pProbe->aiColumn[pc.plan.nEq];
 1.16079 +      pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
 1.16080        if( pTerm==0 ) break;
 1.16081 -      wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
 1.16082 +      pc.plan.wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
 1.16083        testcase( pTerm->pWC!=pWC );
 1.16084        if( pTerm->eOperator & WO_IN ){
 1.16085          Expr *pExpr = pTerm->pExpr;
 1.16086 -        wsFlags |= WHERE_COLUMN_IN;
 1.16087 +        pc.plan.wsFlags |= WHERE_COLUMN_IN;
 1.16088          if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 1.16089            /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
 1.16090            nInMul *= 25;
 1.16091 @@ -106721,12 +105662,12 @@
 1.16092            nInMul *= pExpr->x.pList->nExpr;
 1.16093          }
 1.16094        }else if( pTerm->eOperator & WO_ISNULL ){
 1.16095 -        wsFlags |= WHERE_COLUMN_NULL;
 1.16096 +        pc.plan.wsFlags |= WHERE_COLUMN_NULL;
 1.16097        }
 1.16098  #ifdef SQLITE_ENABLE_STAT3
 1.16099 -      if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
 1.16100 -#endif
 1.16101 -      used |= pTerm->prereqRight;
 1.16102 +      if( pc.plan.nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
 1.16103 +#endif
 1.16104 +      pc.used |= pTerm->prereqRight;
 1.16105      }
 1.16106   
 1.16107      /* If the index being considered is UNIQUE, and there is an equality 
 1.16108 @@ -106735,65 +105676,80 @@
 1.16109      ** indicate this to the caller.
 1.16110      **
 1.16111      ** Otherwise, if the search may find more than one row, test to see if
 1.16112 -    ** there is a range constraint on indexed column (nEq+1) that can be 
 1.16113 +    ** there is a range constraint on indexed column (pc.plan.nEq+1) that can be 
 1.16114      ** optimized using the index. 
 1.16115      */
 1.16116 -    if( nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
 1.16117 -      testcase( wsFlags & WHERE_COLUMN_IN );
 1.16118 -      testcase( wsFlags & WHERE_COLUMN_NULL );
 1.16119 -      if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
 1.16120 -        wsFlags |= WHERE_UNIQUE;
 1.16121 +    if( pc.plan.nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
 1.16122 +      testcase( pc.plan.wsFlags & WHERE_COLUMN_IN );
 1.16123 +      testcase( pc.plan.wsFlags & WHERE_COLUMN_NULL );
 1.16124 +      if( (pc.plan.wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
 1.16125 +        pc.plan.wsFlags |= WHERE_UNIQUE;
 1.16126 +        if( p->i==0 || (p->aLevel[p->i-1].plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
 1.16127 +          pc.plan.wsFlags |= WHERE_ALL_UNIQUE;
 1.16128 +        }
 1.16129        }
 1.16130      }else if( pProbe->bUnordered==0 ){
 1.16131 -      int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
 1.16132 -      if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
 1.16133 -        WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
 1.16134 -        WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
 1.16135 -        whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
 1.16136 +      int j;
 1.16137 +      j = (pc.plan.nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[pc.plan.nEq]);
 1.16138 +      if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
 1.16139 +        WhereTerm *pTop, *pBtm;
 1.16140 +        pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
 1.16141 +        pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
 1.16142 +        whereRangeScanEst(pParse, pProbe, pc.plan.nEq, pBtm, pTop, &rangeDiv);
 1.16143          if( pTop ){
 1.16144            nBound = 1;
 1.16145 -          wsFlags |= WHERE_TOP_LIMIT;
 1.16146 -          used |= pTop->prereqRight;
 1.16147 +          pc.plan.wsFlags |= WHERE_TOP_LIMIT;
 1.16148 +          pc.used |= pTop->prereqRight;
 1.16149            testcase( pTop->pWC!=pWC );
 1.16150          }
 1.16151          if( pBtm ){
 1.16152            nBound++;
 1.16153 -          wsFlags |= WHERE_BTM_LIMIT;
 1.16154 -          used |= pBtm->prereqRight;
 1.16155 +          pc.plan.wsFlags |= WHERE_BTM_LIMIT;
 1.16156 +          pc.used |= pBtm->prereqRight;
 1.16157            testcase( pBtm->pWC!=pWC );
 1.16158          }
 1.16159 -        wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
 1.16160 +        pc.plan.wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
 1.16161        }
 1.16162      }
 1.16163  
 1.16164      /* If there is an ORDER BY clause and the index being considered will
 1.16165      ** naturally scan rows in the required order, set the appropriate flags
 1.16166 -    ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
 1.16167 -    ** will scan rows in a different order, set the bSort variable.  */
 1.16168 -    if( isSortingIndex(
 1.16169 -          pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy, nEq, wsFlags, &rev)
 1.16170 -    ){
 1.16171 -      bSort = 0;
 1.16172 -      wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
 1.16173 -      wsFlags |= (rev ? WHERE_REVERSE : 0);
 1.16174 +    ** in pc.plan.wsFlags. Otherwise, if there is an ORDER BY clause but
 1.16175 +    ** the index will scan rows in a different order, set the bSort
 1.16176 +    ** variable.  */
 1.16177 +    if( bSort && (pSrc->jointype & JT_LEFT)==0 ){
 1.16178 +      int bRev = 2;
 1.16179 +      WHERETRACE(("      --> before isSortingIndex: nPriorSat=%d\n",nPriorSat));
 1.16180 +      pc.plan.nOBSat = isSortingIndex(p, pProbe, iCur, &bRev);
 1.16181 +      WHERETRACE(("      --> after  isSortingIndex: bRev=%d nOBSat=%d\n",
 1.16182 +                  bRev, pc.plan.nOBSat));
 1.16183 +      if( nPriorSat<pc.plan.nOBSat || (pc.plan.wsFlags & WHERE_UNIQUE)!=0 ){
 1.16184 +        pc.plan.wsFlags |= WHERE_ORDERED;
 1.16185 +      }
 1.16186 +      if( nOrderBy==pc.plan.nOBSat ){
 1.16187 +        bSort = 0;
 1.16188 +        pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE;
 1.16189 +      }
 1.16190 +      if( bRev & 1 ) pc.plan.wsFlags |= WHERE_REVERSE;
 1.16191      }
 1.16192  
 1.16193      /* If there is a DISTINCT qualifier and this index will scan rows in
 1.16194      ** order of the DISTINCT expressions, clear bDist and set the appropriate
 1.16195 -    ** flags in wsFlags. */
 1.16196 -    if( isDistinctIndex(pParse, pWC, pProbe, iCur, pDistinct, nEq)
 1.16197 -     && (wsFlags & WHERE_COLUMN_IN)==0
 1.16198 +    ** flags in pc.plan.wsFlags. */
 1.16199 +    if( bDist
 1.16200 +     && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, pc.plan.nEq)
 1.16201 +     && (pc.plan.wsFlags & WHERE_COLUMN_IN)==0
 1.16202      ){
 1.16203        bDist = 0;
 1.16204 -      wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
 1.16205 +      pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
 1.16206      }
 1.16207  
 1.16208      /* If currently calculating the cost of using an index (not the IPK
 1.16209      ** index), determine if all required column data may be obtained without 
 1.16210      ** using the main table (i.e. if the index is a covering
 1.16211      ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
 1.16212 -    ** wsFlags. Otherwise, set the bLookup variable to true.  */
 1.16213 -    if( pIdx && wsFlags ){
 1.16214 +    ** pc.plan.wsFlags. Otherwise, set the bLookup variable to true.  */
 1.16215 +    if( pIdx ){
 1.16216        Bitmask m = pSrc->colUsed;
 1.16217        int j;
 1.16218        for(j=0; j<pIdx->nColumn; j++){
 1.16219 @@ -106803,7 +105759,7 @@
 1.16220          }
 1.16221        }
 1.16222        if( m==0 ){
 1.16223 -        wsFlags |= WHERE_IDX_ONLY;
 1.16224 +        pc.plan.wsFlags |= WHERE_IDX_ONLY;
 1.16225        }else{
 1.16226          bLookup = 1;
 1.16227        }
 1.16228 @@ -106813,10 +105769,10 @@
 1.16229      ** Estimate the number of rows of output.  For an "x IN (SELECT...)"
 1.16230      ** constraint, do not let the estimate exceed half the rows in the table.
 1.16231      */
 1.16232 -    nRow = (double)(aiRowEst[nEq] * nInMul);
 1.16233 -    if( bInEst && nRow*2>aiRowEst[0] ){
 1.16234 -      nRow = aiRowEst[0]/2;
 1.16235 -      nInMul = (int)(nRow / aiRowEst[nEq]);
 1.16236 +    pc.plan.nRow = (double)(aiRowEst[pc.plan.nEq] * nInMul);
 1.16237 +    if( bInEst && pc.plan.nRow*2>aiRowEst[0] ){
 1.16238 +      pc.plan.nRow = aiRowEst[0]/2;
 1.16239 +      nInMul = (int)(pc.plan.nRow / aiRowEst[pc.plan.nEq]);
 1.16240      }
 1.16241  
 1.16242  #ifdef SQLITE_ENABLE_STAT3
 1.16243 @@ -106826,15 +105782,18 @@
 1.16244      ** to get a better estimate on the number of rows based on
 1.16245      ** VALUE and how common that value is according to the histogram.
 1.16246      */
 1.16247 -    if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
 1.16248 +    if( pc.plan.nRow>(double)1 && pc.plan.nEq==1
 1.16249 +     && pFirstTerm!=0 && aiRowEst[1]>1 ){
 1.16250        assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
 1.16251        if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
 1.16252          testcase( pFirstTerm->eOperator==WO_EQ );
 1.16253          testcase( pFirstTerm->eOperator==WO_ISNULL );
 1.16254 -        whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
 1.16255 +        whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight,
 1.16256 +                          &pc.plan.nRow);
 1.16257        }else if( bInEst==0 ){
 1.16258          assert( pFirstTerm->eOperator==WO_IN );
 1.16259 -        whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
 1.16260 +        whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList,
 1.16261 +                       &pc.plan.nRow);
 1.16262        }
 1.16263      }
 1.16264  #endif /* SQLITE_ENABLE_STAT3 */
 1.16265 @@ -106842,8 +105801,8 @@
 1.16266      /* Adjust the number of output rows and downward to reflect rows
 1.16267      ** that are excluded by range constraints.
 1.16268      */
 1.16269 -    nRow = nRow/rangeDiv;
 1.16270 -    if( nRow<1 ) nRow = 1;
 1.16271 +    pc.plan.nRow = pc.plan.nRow/rangeDiv;
 1.16272 +    if( pc.plan.nRow<1 ) pc.plan.nRow = 1;
 1.16273  
 1.16274      /* Experiments run on real SQLite databases show that the time needed
 1.16275      ** to do a binary search to locate a row in a table or index is roughly
 1.16276 @@ -106858,7 +105817,18 @@
 1.16277      ** So this computation assumes table records are about twice as big
 1.16278      ** as index records
 1.16279      */
 1.16280 -    if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
 1.16281 +    if( (pc.plan.wsFlags&~(WHERE_REVERSE|WHERE_ORDERED))==WHERE_IDX_ONLY
 1.16282 +     && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
 1.16283 +     && sqlite3GlobalConfig.bUseCis
 1.16284 +     && OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
 1.16285 +    ){
 1.16286 +      /* This index is not useful for indexing, but it is a covering index.
 1.16287 +      ** A full-scan of the index might be a little faster than a full-scan
 1.16288 +      ** of the table, so give this case a cost slightly less than a table
 1.16289 +      ** scan. */
 1.16290 +      pc.rCost = aiRowEst[0]*3 + pProbe->nColumn;
 1.16291 +      pc.plan.wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
 1.16292 +    }else if( (pc.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
 1.16293        /* The cost of a full table scan is a number of move operations equal
 1.16294        ** to the number of rows in the table.
 1.16295        **
 1.16296 @@ -106868,10 +105838,15 @@
 1.16297        ** decision and one which we expect to revisit in the future.  But
 1.16298        ** it seems to be working well enough at the moment.
 1.16299        */
 1.16300 -      cost = aiRowEst[0]*4;
 1.16301 +      pc.rCost = aiRowEst[0]*4;
 1.16302 +      pc.plan.wsFlags &= ~WHERE_IDX_ONLY;
 1.16303 +      if( pIdx ){
 1.16304 +        pc.plan.wsFlags &= ~WHERE_ORDERED;
 1.16305 +        pc.plan.nOBSat = nPriorSat;
 1.16306 +      }
 1.16307      }else{
 1.16308        log10N = estLog(aiRowEst[0]);
 1.16309 -      cost = nRow;
 1.16310 +      pc.rCost = pc.plan.nRow;
 1.16311        if( pIdx ){
 1.16312          if( bLookup ){
 1.16313            /* For an index lookup followed by a table lookup:
 1.16314 @@ -106879,20 +105854,20 @@
 1.16315            **  + nRow steps through the index
 1.16316            **  + nRow table searches to lookup the table entry using the rowid
 1.16317            */
 1.16318 -          cost += (nInMul + nRow)*log10N;
 1.16319 +          pc.rCost += (nInMul + pc.plan.nRow)*log10N;
 1.16320          }else{
 1.16321            /* For a covering index:
 1.16322            **     nInMul index searches to find the initial entry 
 1.16323            **   + nRow steps through the index
 1.16324            */
 1.16325 -          cost += nInMul*log10N;
 1.16326 +          pc.rCost += nInMul*log10N;
 1.16327          }
 1.16328        }else{
 1.16329          /* For a rowid primary key lookup:
 1.16330          **    nInMult table searches to find the initial entry for each range
 1.16331          **  + nRow steps through the table
 1.16332          */
 1.16333 -        cost += nInMul*log10N;
 1.16334 +        pc.rCost += nInMul*log10N;
 1.16335        }
 1.16336      }
 1.16337  
 1.16338 @@ -106903,10 +105878,12 @@
 1.16339      ** difference and select C of 3.0.
 1.16340      */
 1.16341      if( bSort ){
 1.16342 -      cost += nRow*estLog(nRow)*3;
 1.16343 +      double m = estLog(pc.plan.nRow*(nOrderBy - pc.plan.nOBSat)/nOrderBy);
 1.16344 +      m *= (double)(pc.plan.nOBSat ? 2 : 3);
 1.16345 +      pc.rCost += pc.plan.nRow*m;
 1.16346      }
 1.16347      if( bDist ){
 1.16348 -      cost += nRow*estLog(nRow)*3;
 1.16349 +      pc.rCost += pc.plan.nRow*estLog(pc.plan.nRow)*3;
 1.16350      }
 1.16351  
 1.16352      /**** Cost of using this index has now been computed ****/
 1.16353 @@ -106927,25 +105904,25 @@
 1.16354      ** might be selected even when there exists an optimal index that has
 1.16355      ** no such dependency.
 1.16356      */
 1.16357 -    if( nRow>2 && cost<=pCost->rCost ){
 1.16358 +    if( pc.plan.nRow>2 && pc.rCost<=p->cost.rCost ){
 1.16359        int k;                       /* Loop counter */
 1.16360 -      int nSkipEq = nEq;           /* Number of == constraints to skip */
 1.16361 +      int nSkipEq = pc.plan.nEq;   /* Number of == constraints to skip */
 1.16362        int nSkipRange = nBound;     /* Number of < constraints to skip */
 1.16363        Bitmask thisTab;             /* Bitmap for pSrc */
 1.16364  
 1.16365        thisTab = getMask(pWC->pMaskSet, iCur);
 1.16366 -      for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
 1.16367 +      for(pTerm=pWC->a, k=pWC->nTerm; pc.plan.nRow>2 && k; k--, pTerm++){
 1.16368          if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
 1.16369 -        if( (pTerm->prereqAll & notValid)!=thisTab ) continue;
 1.16370 +        if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
 1.16371          if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
 1.16372            if( nSkipEq ){
 1.16373 -            /* Ignore the first nEq equality matches since the index
 1.16374 +            /* Ignore the first pc.plan.nEq equality matches since the index
 1.16375              ** has already accounted for these */
 1.16376              nSkipEq--;
 1.16377            }else{
 1.16378              /* Assume each additional equality match reduces the result
 1.16379              ** set size by a factor of 10 */
 1.16380 -            nRow /= 10;
 1.16381 +            pc.plan.nRow /= 10;
 1.16382            }
 1.16383          }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
 1.16384            if( nSkipRange ){
 1.16385 @@ -106959,37 +105936,33 @@
 1.16386              ** more selective intentionally because of the subjective 
 1.16387              ** observation that indexed range constraints really are more
 1.16388              ** selective in practice, on average. */
 1.16389 -            nRow /= 3;
 1.16390 +            pc.plan.nRow /= 3;
 1.16391            }
 1.16392          }else if( pTerm->eOperator!=WO_NOOP ){
 1.16393            /* Any other expression lowers the output row count by half */
 1.16394 -          nRow /= 2;
 1.16395 -        }
 1.16396 -      }
 1.16397 -      if( nRow<2 ) nRow = 2;
 1.16398 +          pc.plan.nRow /= 2;
 1.16399 +        }
 1.16400 +      }
 1.16401 +      if( pc.plan.nRow<2 ) pc.plan.nRow = 2;
 1.16402      }
 1.16403  
 1.16404  
 1.16405      WHERETRACE((
 1.16406 -      "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
 1.16407 -      "         notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
 1.16408 -      pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), 
 1.16409 -      nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
 1.16410 -      notReady, log10N, nRow, cost, used
 1.16411 +      "      nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
 1.16412 +      "      notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
 1.16413 +      "      used=0x%llx nOBSat=%d\n",
 1.16414 +      pc.plan.nEq, nInMul, (int)rangeDiv, bSort, bLookup, pc.plan.wsFlags,
 1.16415 +      p->notReady, log10N, pc.plan.nRow, pc.rCost, pc.used,
 1.16416 +      pc.plan.nOBSat
 1.16417      ));
 1.16418  
 1.16419      /* If this index is the best we have seen so far, then record this
 1.16420 -    ** index and its cost in the pCost structure.
 1.16421 -    */
 1.16422 -    if( (!pIdx || wsFlags)
 1.16423 -     && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->plan.nRow))
 1.16424 -    ){
 1.16425 -      pCost->rCost = cost;
 1.16426 -      pCost->used = used;
 1.16427 -      pCost->plan.nRow = nRow;
 1.16428 -      pCost->plan.wsFlags = (wsFlags&wsFlagMask);
 1.16429 -      pCost->plan.nEq = nEq;
 1.16430 -      pCost->plan.u.pIdx = pIdx;
 1.16431 +    ** index and its cost in the p->cost structure.
 1.16432 +    */
 1.16433 +    if( (!pIdx || pc.plan.wsFlags) && compareCost(&pc, &p->cost) ){
 1.16434 +      p->cost = pc;
 1.16435 +      p->cost.plan.wsFlags &= wsFlagMask;
 1.16436 +      p->cost.plan.u.pIdx = pIdx;
 1.16437      }
 1.16438  
 1.16439      /* If there was an INDEXED BY clause, then only that one index is
 1.16440 @@ -107006,25 +105979,23 @@
 1.16441    ** in. This is used for application testing, to help find cases
 1.16442    ** where application behaviour depends on the (undefined) order that
 1.16443    ** SQLite outputs rows in in the absence of an ORDER BY clause.  */
 1.16444 -  if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
 1.16445 -    pCost->plan.wsFlags |= WHERE_REVERSE;
 1.16446 -  }
 1.16447 -
 1.16448 -  assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 );
 1.16449 -  assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 );
 1.16450 +  if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
 1.16451 +    p->cost.plan.wsFlags |= WHERE_REVERSE;
 1.16452 +  }
 1.16453 +
 1.16454 +  assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERED)==0 );
 1.16455 +  assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
 1.16456    assert( pSrc->pIndex==0 
 1.16457 -       || pCost->plan.u.pIdx==0 
 1.16458 -       || pCost->plan.u.pIdx==pSrc->pIndex 
 1.16459 +       || p->cost.plan.u.pIdx==0 
 1.16460 +       || p->cost.plan.u.pIdx==pSrc->pIndex 
 1.16461    );
 1.16462  
 1.16463 -  WHERETRACE(("best index is: %s\n", 
 1.16464 -    ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" : 
 1.16465 -         pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
 1.16466 -  ));
 1.16467 -  
 1.16468 -  bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
 1.16469 -  bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
 1.16470 -  pCost->plan.wsFlags |= eqTermMask;
 1.16471 +  WHERETRACE(("   best index is: %s\n",
 1.16472 +         p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk"));
 1.16473 +  
 1.16474 +  bestOrClauseIndex(p);
 1.16475 +  bestAutomaticIndex(p);
 1.16476 +  p->cost.plan.wsFlags |= eqTermMask;
 1.16477  }
 1.16478  
 1.16479  /*
 1.16480 @@ -107032,28 +106003,27 @@
 1.16481  ** best query plan and its cost into the WhereCost object supplied 
 1.16482  ** as the last parameter. This function may calculate the cost of
 1.16483  ** both real and virtual table scans.
 1.16484 -*/
 1.16485 -static void bestIndex(
 1.16486 -  Parse *pParse,              /* The parsing context */
 1.16487 -  WhereClause *pWC,           /* The WHERE clause */
 1.16488 -  struct SrcList_item *pSrc,  /* The FROM clause term to search */
 1.16489 -  Bitmask notReady,           /* Mask of cursors not available for indexing */
 1.16490 -  Bitmask notValid,           /* Cursors not available for any purpose */
 1.16491 -  ExprList *pOrderBy,         /* The ORDER BY clause */
 1.16492 -  WhereCost *pCost            /* Lowest cost query plan */
 1.16493 -){
 1.16494 +**
 1.16495 +** This function does not take ORDER BY or DISTINCT into account.  Nor
 1.16496 +** does it remember the virtual table query plan.  All it does is compute
 1.16497 +** the cost while determining if an OR optimization is applicable.  The
 1.16498 +** details will be reconsidered later if the optimization is found to be
 1.16499 +** applicable.
 1.16500 +*/
 1.16501 +static void bestIndex(WhereBestIdx *p){
 1.16502  #ifndef SQLITE_OMIT_VIRTUALTABLE
 1.16503 -  if( IsVirtual(pSrc->pTab) ){
 1.16504 -    sqlite3_index_info *p = 0;
 1.16505 -    bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p);
 1.16506 -    if( p->needToFreeIdxStr ){
 1.16507 -      sqlite3_free(p->idxStr);
 1.16508 -    }
 1.16509 -    sqlite3DbFree(pParse->db, p);
 1.16510 +  if( IsVirtual(p->pSrc->pTab) ){
 1.16511 +    sqlite3_index_info *pIdxInfo = 0;
 1.16512 +    p->ppIdxInfo = &pIdxInfo;
 1.16513 +    bestVirtualIndex(p);
 1.16514 +    if( pIdxInfo->needToFreeIdxStr ){
 1.16515 +      sqlite3_free(pIdxInfo->idxStr);
 1.16516 +    }
 1.16517 +    sqlite3DbFree(p->pParse->db, pIdxInfo);
 1.16518    }else
 1.16519  #endif
 1.16520    {
 1.16521 -    bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, 0, pCost);
 1.16522 +    bestBtreeIndex(p);
 1.16523    }
 1.16524  }
 1.16525  
 1.16526 @@ -107282,7 +106252,7 @@
 1.16527      int r1;
 1.16528      int k = pIdx->aiColumn[j];
 1.16529      pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
 1.16530 -    if( NEVER(pTerm==0) ) break;
 1.16531 +    if( pTerm==0 ) break;
 1.16532      /* The following true for indices with redundant columns. 
 1.16533      ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
 1.16534      testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
 1.16535 @@ -107531,6 +106501,16 @@
 1.16536      VdbeComment((v, "init LEFT JOIN no-match flag"));
 1.16537    }
 1.16538  
 1.16539 +  /* Special case of a FROM clause subquery implemented as a co-routine */
 1.16540 +  if( pTabItem->viaCoroutine ){
 1.16541 +    int regYield = pTabItem->regReturn;
 1.16542 +    sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield);
 1.16543 +    pLevel->p2 =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
 1.16544 +    VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName));
 1.16545 +    sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk);
 1.16546 +    pLevel->op = OP_Goto;
 1.16547 +  }else
 1.16548 +
 1.16549  #ifndef SQLITE_OMIT_VIRTUALTABLE
 1.16550    if(  (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
 1.16551      /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
 1.16552 @@ -107749,7 +106729,7 @@
 1.16553      ** this requires some special handling.
 1.16554      */
 1.16555      if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
 1.16556 -     && (pLevel->plan.wsFlags&WHERE_ORDERBY)
 1.16557 +     && (pLevel->plan.wsFlags&WHERE_ORDERED)
 1.16558       && (pIdx->nColumn>nEq)
 1.16559      ){
 1.16560        /* assert( pOrderBy->nExpr==1 ); */
 1.16561 @@ -107912,6 +106892,11 @@
 1.16562        pLevel->op = OP_Next;
 1.16563      }
 1.16564      pLevel->p1 = iIdxCur;
 1.16565 +    if( pLevel->plan.wsFlags & WHERE_COVER_SCAN ){
 1.16566 +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
 1.16567 +    }else{
 1.16568 +      assert( pLevel->p5==0 );
 1.16569 +    }
 1.16570    }else
 1.16571  
 1.16572  #ifndef SQLITE_OMIT_OR_OPTIMIZATION
 1.16573 @@ -107957,6 +106942,8 @@
 1.16574      */
 1.16575      WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
 1.16576      SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
 1.16577 +    Index *pCov = 0;             /* Potential covering index (or NULL) */
 1.16578 +    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
 1.16579  
 1.16580      int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
 1.16581      int regRowset = 0;                        /* Register for RowSet object */
 1.16582 @@ -107975,7 +106962,7 @@
 1.16583      pLevel->op = OP_Return;
 1.16584      pLevel->p1 = regReturn;
 1.16585  
 1.16586 -    /* Set up a new SrcList ni pOrTab containing the table being scanned
 1.16587 +    /* Set up a new SrcList in pOrTab containing the table being scanned
 1.16588      ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
 1.16589      ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
 1.16590      */
 1.16591 @@ -108052,8 +107039,10 @@
 1.16592          /* Loop through table entries that match term pOrTerm. */
 1.16593          pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
 1.16594                          WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
 1.16595 -                        WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
 1.16596 +                        WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
 1.16597 +        assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed );
 1.16598          if( pSubWInfo ){
 1.16599 +          WhereLevel *pLvl;
 1.16600            explainOneScan(
 1.16601                pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
 1.16602            );
 1.16603 @@ -108074,11 +107063,36 @@
 1.16604            */
 1.16605            if( pSubWInfo->untestedTerms ) untestedTerms = 1;
 1.16606  
 1.16607 +          /* If all of the OR-connected terms are optimized using the same
 1.16608 +          ** index, and the index is opened using the same cursor number
 1.16609 +          ** by each call to sqlite3WhereBegin() made by this loop, it may
 1.16610 +          ** be possible to use that index as a covering index.
 1.16611 +          **
 1.16612 +          ** If the call to sqlite3WhereBegin() above resulted in a scan that
 1.16613 +          ** uses an index, and this is either the first OR-connected term
 1.16614 +          ** processed or the index is the same as that used by all previous
 1.16615 +          ** terms, set pCov to the candidate covering index. Otherwise, set 
 1.16616 +          ** pCov to NULL to indicate that no candidate covering index will 
 1.16617 +          ** be available.
 1.16618 +          */
 1.16619 +          pLvl = &pSubWInfo->a[0];
 1.16620 +          if( (pLvl->plan.wsFlags & WHERE_INDEXED)!=0
 1.16621 +           && (pLvl->plan.wsFlags & WHERE_TEMP_INDEX)==0
 1.16622 +           && (ii==0 || pLvl->plan.u.pIdx==pCov)
 1.16623 +          ){
 1.16624 +            assert( pLvl->iIdxCur==iCovCur );
 1.16625 +            pCov = pLvl->plan.u.pIdx;
 1.16626 +          }else{
 1.16627 +            pCov = 0;
 1.16628 +          }
 1.16629 +
 1.16630            /* Finish the loop through table entries that match term pOrTerm. */
 1.16631            sqlite3WhereEnd(pSubWInfo);
 1.16632          }
 1.16633        }
 1.16634      }
 1.16635 +    pLevel->u.pCovidx = pCov;
 1.16636 +    if( pCov ) pLevel->iIdxCur = iCovCur;
 1.16637      if( pAndExpr ){
 1.16638        pAndExpr->pLeft = 0;
 1.16639        sqlite3ExprDelete(pParse->db, pAndExpr);
 1.16640 @@ -108277,41 +107291,46 @@
 1.16641  **
 1.16642  ** ORDER BY CLAUSE PROCESSING
 1.16643  **
 1.16644 -** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
 1.16645 +** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
 1.16646  ** if there is one.  If there is no ORDER BY clause or if this routine
 1.16647 -** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
 1.16648 +** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
 1.16649  **
 1.16650  ** If an index can be used so that the natural output order of the table
 1.16651  ** scan is correct for the ORDER BY clause, then that index is used and
 1.16652 -** *ppOrderBy is set to NULL.  This is an optimization that prevents an
 1.16653 -** unnecessary sort of the result set if an index appropriate for the
 1.16654 -** ORDER BY clause already exists.
 1.16655 +** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr.  This
 1.16656 +** is an optimization that prevents an unnecessary sort of the result set
 1.16657 +** if an index appropriate for the ORDER BY clause already exists.
 1.16658  **
 1.16659  ** If the where clause loops cannot be arranged to provide the correct
 1.16660 -** output order, then the *ppOrderBy is unchanged.
 1.16661 +** output order, then WhereInfo.nOBSat is 0.
 1.16662  */
 1.16663  SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
 1.16664    Parse *pParse,        /* The parser context */
 1.16665    SrcList *pTabList,    /* A list of all tables to be scanned */
 1.16666    Expr *pWhere,         /* The WHERE clause */
 1.16667 -  ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
 1.16668 +  ExprList *pOrderBy,   /* An ORDER BY clause, or NULL */
 1.16669    ExprList *pDistinct,  /* The select-list for DISTINCT queries - or NULL */
 1.16670 -  u16 wctrlFlags        /* One of the WHERE_* flags defined in sqliteInt.h */
 1.16671 -){
 1.16672 -  int i;                     /* Loop counter */
 1.16673 +  u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
 1.16674 +  int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
 1.16675 +){
 1.16676    int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
 1.16677    int nTabList;              /* Number of elements in pTabList */
 1.16678    WhereInfo *pWInfo;         /* Will become the return value of this function */
 1.16679    Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
 1.16680    Bitmask notReady;          /* Cursors that are not yet positioned */
 1.16681 +  WhereBestIdx sWBI;         /* Best index search context */
 1.16682    WhereMaskSet *pMaskSet;    /* The expression mask set */
 1.16683 -  WhereClause *pWC;               /* Decomposition of the WHERE clause */
 1.16684 -  struct SrcList_item *pTabItem;  /* A single entry from pTabList */
 1.16685 -  WhereLevel *pLevel;             /* A single level in the pWInfo list */
 1.16686 -  int iFrom;                      /* First unused FROM clause element */
 1.16687 +  WhereLevel *pLevel;        /* A single level in pWInfo->a[] */
 1.16688 +  int iFrom;                 /* First unused FROM clause element */
 1.16689    int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
 1.16690 +  int ii;                    /* Loop counter */
 1.16691    sqlite3 *db;               /* Database connection */
 1.16692  
 1.16693 +
 1.16694 +  /* Variable initialization */
 1.16695 +  memset(&sWBI, 0, sizeof(sWBI));
 1.16696 +  sWBI.pParse = pParse;
 1.16697 +
 1.16698    /* The number of tables in the FROM clause is limited by the number of
 1.16699    ** bits in a Bitmask 
 1.16700    */
 1.16701 @@ -108351,22 +107370,23 @@
 1.16702    pWInfo->pParse = pParse;
 1.16703    pWInfo->pTabList = pTabList;
 1.16704    pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
 1.16705 -  pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
 1.16706 +  pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
 1.16707    pWInfo->wctrlFlags = wctrlFlags;
 1.16708    pWInfo->savedNQueryLoop = pParse->nQueryLoop;
 1.16709 -  pMaskSet = (WhereMaskSet*)&pWC[1];
 1.16710 +  pMaskSet = (WhereMaskSet*)&sWBI.pWC[1];
 1.16711 +  sWBI.aLevel = pWInfo->a;
 1.16712  
 1.16713    /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
 1.16714    ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
 1.16715 -  if( db->flags & SQLITE_DistinctOpt ) pDistinct = 0;
 1.16716 +  if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0;
 1.16717  
 1.16718    /* Split the WHERE clause into separate subexpressions where each
 1.16719    ** subexpression is separated by an AND operator.
 1.16720    */
 1.16721    initMaskSet(pMaskSet);
 1.16722 -  whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags);
 1.16723 +  whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags);
 1.16724    sqlite3ExprCodeConstants(pParse, pWhere);
 1.16725 -  whereSplit(pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
 1.16726 +  whereSplit(sWBI.pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
 1.16727      
 1.16728    /* Special case: a WHERE clause that is constant.  Evaluate the
 1.16729    ** expression and either jump over all of the code or fall thru.
 1.16730 @@ -108397,20 +107417,20 @@
 1.16731    ** equal to pTabList->nSrc but might be shortened to 1 if the
 1.16732    ** WHERE_ONETABLE_ONLY flag is set.
 1.16733    */
 1.16734 -  assert( pWC->vmask==0 && pMaskSet->n==0 );
 1.16735 -  for(i=0; i<pTabList->nSrc; i++){
 1.16736 -    createMask(pMaskSet, pTabList->a[i].iCursor);
 1.16737 +  assert( sWBI.pWC->vmask==0 && pMaskSet->n==0 );
 1.16738 +  for(ii=0; ii<pTabList->nSrc; ii++){
 1.16739 +    createMask(pMaskSet, pTabList->a[ii].iCursor);
 1.16740  #ifndef SQLITE_OMIT_VIRTUALTABLE
 1.16741 -    if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
 1.16742 -      pWC->vmask |= ((Bitmask)1 << i);
 1.16743 +    if( ALWAYS(pTabList->a[ii].pTab) && IsVirtual(pTabList->a[ii].pTab) ){
 1.16744 +      sWBI.pWC->vmask |= ((Bitmask)1 << ii);
 1.16745      }
 1.16746  #endif
 1.16747    }
 1.16748  #ifndef NDEBUG
 1.16749    {
 1.16750      Bitmask toTheLeft = 0;
 1.16751 -    for(i=0; i<pTabList->nSrc; i++){
 1.16752 -      Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor);
 1.16753 +    for(ii=0; ii<pTabList->nSrc; ii++){
 1.16754 +      Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
 1.16755        assert( (m-1)==toTheLeft );
 1.16756        toTheLeft |= m;
 1.16757      }
 1.16758 @@ -108422,7 +107442,7 @@
 1.16759    ** want to analyze these virtual terms, so start analyzing at the end
 1.16760    ** and work forward so that the added virtual terms are never processed.
 1.16761    */
 1.16762 -  exprAnalyzeAll(pTabList, pWC);
 1.16763 +  exprAnalyzeAll(pTabList, sWBI.pWC);
 1.16764    if( db->mallocFailed ){
 1.16765      goto whereBeginError;
 1.16766    }
 1.16767 @@ -108431,7 +107451,7 @@
 1.16768    ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
 1.16769    ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
 1.16770    */
 1.16771 -  if( pDistinct && isDistinctRedundant(pParse, pTabList, pWC, pDistinct) ){
 1.16772 +  if( pDistinct && isDistinctRedundant(pParse, pTabList, sWBI.pWC, pDistinct) ){
 1.16773      pDistinct = 0;
 1.16774      pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
 1.16775    }
 1.16776 @@ -108451,10 +107471,13 @@
 1.16777    ** This loop also figures out the nesting order of tables in the FROM
 1.16778    ** clause.
 1.16779    */
 1.16780 -  notReady = ~(Bitmask)0;
 1.16781 +  sWBI.notValid = ~(Bitmask)0;
 1.16782 +  sWBI.pOrderBy = pOrderBy;
 1.16783 +  sWBI.n = nTabList;
 1.16784 +  sWBI.pDistinct = pDistinct;
 1.16785    andFlags = ~0;
 1.16786    WHERETRACE(("*** Optimizer Start ***\n"));
 1.16787 -  for(i=iFrom=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
 1.16788 +  for(sWBI.i=iFrom=0, pLevel=pWInfo->a; sWBI.i<nTabList; sWBI.i++, pLevel++){
 1.16789      WhereCost bestPlan;         /* Most efficient plan seen so far */
 1.16790      Index *pIdx;                /* Index for FROM table at pTabItem */
 1.16791      int j;                      /* For looping over FROM tables */
 1.16792 @@ -108466,7 +107489,7 @@
 1.16793  
 1.16794      memset(&bestPlan, 0, sizeof(bestPlan));
 1.16795      bestPlan.rCost = SQLITE_BIG_DBL;
 1.16796 -    WHERETRACE(("*** Begin search for loop %d ***\n", i));
 1.16797 +    WHERETRACE(("*** Begin search for loop %d ***\n", sWBI.i));
 1.16798  
 1.16799      /* Loop through the remaining entries in the FROM clause to find the
 1.16800      ** next nested loop. The loop tests all FROM clause entries
 1.16801 @@ -108482,8 +107505,8 @@
 1.16802      ** by waiting for other tables to run first.  This "optimal" test works
 1.16803      ** by first assuming that the FROM clause is on the inner loop and finding
 1.16804      ** its query plan, then checking to see if that query plan uses any
 1.16805 -    ** other FROM clause terms that are notReady.  If no notReady terms are
 1.16806 -    ** used then the "optimal" query plan works.
 1.16807 +    ** other FROM clause terms that are sWBI.notValid.  If no notValid terms
 1.16808 +    ** are used then the "optimal" query plan works.
 1.16809      **
 1.16810      ** Note that the WhereCost.nRow parameter for an optimal scan might
 1.16811      ** not be as small as it would be if the table really were the innermost
 1.16812 @@ -108514,58 +107537,64 @@
 1.16813      nUnconstrained = 0;
 1.16814      notIndexed = 0;
 1.16815      for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
 1.16816 -      Bitmask mask;             /* Mask of tables not yet ready */
 1.16817 -      for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
 1.16818 +      for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
 1.16819          int doNotReorder;    /* True if this table should not be reordered */
 1.16820 -        WhereCost sCost;     /* Cost information from best[Virtual]Index() */
 1.16821 -        ExprList *pOrderBy;  /* ORDER BY clause for index to optimize */
 1.16822 -        ExprList *pDist;     /* DISTINCT clause for index to optimize */
 1.16823 -  
 1.16824 -        doNotReorder =  (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
 1.16825 +  
 1.16826 +        doNotReorder =  (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0;
 1.16827          if( j!=iFrom && doNotReorder ) break;
 1.16828 -        m = getMask(pMaskSet, pTabItem->iCursor);
 1.16829 -        if( (m & notReady)==0 ){
 1.16830 +        m = getMask(pMaskSet, sWBI.pSrc->iCursor);
 1.16831 +        if( (m & sWBI.notValid)==0 ){
 1.16832            if( j==iFrom ) iFrom++;
 1.16833            continue;
 1.16834          }
 1.16835 -        mask = (isOptimal ? m : notReady);
 1.16836 -        pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
 1.16837 -        pDist = (i==0 ? pDistinct : 0);
 1.16838 -        if( pTabItem->pIndex==0 ) nUnconstrained++;
 1.16839 -  
 1.16840 -        WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
 1.16841 -                    j, isOptimal));
 1.16842 -        assert( pTabItem->pTab );
 1.16843 +        sWBI.notReady = (isOptimal ? m : sWBI.notValid);
 1.16844 +        if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
 1.16845 +  
 1.16846 +        WHERETRACE(("   === trying table %d (%s) with isOptimal=%d ===\n",
 1.16847 +                    j, sWBI.pSrc->pTab->zName, isOptimal));
 1.16848 +        assert( sWBI.pSrc->pTab );
 1.16849  #ifndef SQLITE_OMIT_VIRTUALTABLE
 1.16850 -        if( IsVirtual(pTabItem->pTab) ){
 1.16851 -          sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
 1.16852 -          bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
 1.16853 -                           &sCost, pp);
 1.16854 +        if( IsVirtual(sWBI.pSrc->pTab) ){
 1.16855 +          sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
 1.16856 +          bestVirtualIndex(&sWBI);
 1.16857          }else 
 1.16858  #endif
 1.16859          {
 1.16860 -          bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
 1.16861 -              pDist, &sCost);
 1.16862 -        }
 1.16863 -        assert( isOptimal || (sCost.used&notReady)==0 );
 1.16864 +          bestBtreeIndex(&sWBI);
 1.16865 +        }
 1.16866 +        assert( isOptimal || (sWBI.cost.used&sWBI.notValid)==0 );
 1.16867  
 1.16868          /* If an INDEXED BY clause is present, then the plan must use that
 1.16869          ** index if it uses any index at all */
 1.16870 -        assert( pTabItem->pIndex==0 
 1.16871 -                  || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
 1.16872 -                  || sCost.plan.u.pIdx==pTabItem->pIndex );
 1.16873 -
 1.16874 -        if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
 1.16875 +        assert( sWBI.pSrc->pIndex==0 
 1.16876 +                  || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
 1.16877 +                  || sWBI.cost.plan.u.pIdx==sWBI.pSrc->pIndex );
 1.16878 +
 1.16879 +        if( isOptimal && (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
 1.16880            notIndexed |= m;
 1.16881          }
 1.16882 +        if( isOptimal ){
 1.16883 +          pWInfo->a[j].rOptCost = sWBI.cost.rCost;
 1.16884 +        }else if( iFrom<nTabList-1 ){
 1.16885 +          /* If two or more tables have nearly the same outer loop cost,
 1.16886 +          ** very different inner loop (optimal) cost, we want to choose
 1.16887 +          ** for the outer loop that table which benefits the least from
 1.16888 +          ** being in the inner loop.  The following code scales the 
 1.16889 +          ** outer loop cost estimate to accomplish that. */
 1.16890 +          WHERETRACE(("   scaling cost from %.1f to %.1f\n",
 1.16891 +                      sWBI.cost.rCost,
 1.16892 +                      sWBI.cost.rCost/pWInfo->a[j].rOptCost));
 1.16893 +          sWBI.cost.rCost /= pWInfo->a[j].rOptCost;
 1.16894 +        }
 1.16895  
 1.16896          /* Conditions under which this table becomes the best so far:
 1.16897          **
 1.16898          **   (1) The table must not depend on other tables that have not
 1.16899 -        **       yet run.
 1.16900 +        **       yet run.  (In other words, it must not depend on tables
 1.16901 +        **       in inner loops.)
 1.16902          **
 1.16903 -        **   (2) A full-table-scan plan cannot supercede indexed plan unless
 1.16904 -        **       the full-table-scan is an "optimal" plan as defined above.
 1.16905 +        **   (2) (This rule was removed on 2012-11-09.  The scaling of the
 1.16906 +        **       cost using the optimal scan cost made this rule obsolete.)
 1.16907          **
 1.16908          **   (3) All tables have an INDEXED BY clause or this table lacks an
 1.16909          **       INDEXED BY clause or this table uses the specific
 1.16910 @@ -108576,51 +107605,53 @@
 1.16911          **       The NEVER() comes about because rule (2) above prevents
 1.16912          **       An indexable full-table-scan from reaching rule (3).
 1.16913          **
 1.16914 -        **   (4) The plan cost must be lower than prior plans or else the
 1.16915 -        **       cost must be the same and the number of rows must be lower.
 1.16916 +        **   (4) The plan cost must be lower than prior plans, where "cost"
 1.16917 +        **       is defined by the compareCost() function above. 
 1.16918          */
 1.16919 -        if( (sCost.used&notReady)==0                       /* (1) */
 1.16920 -            && (bestJ<0 || (notIndexed&m)!=0               /* (2) */
 1.16921 -                || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
 1.16922 -                || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
 1.16923 -            && (nUnconstrained==0 || pTabItem->pIndex==0   /* (3) */
 1.16924 -                || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
 1.16925 -            && (bestJ<0 || sCost.rCost<bestPlan.rCost      /* (4) */
 1.16926 -                || (sCost.rCost<=bestPlan.rCost 
 1.16927 -                 && sCost.plan.nRow<bestPlan.plan.nRow))
 1.16928 +        if( (sWBI.cost.used&sWBI.notValid)==0                    /* (1) */
 1.16929 +            && (nUnconstrained==0 || sWBI.pSrc->pIndex==0        /* (3) */
 1.16930 +                || NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
 1.16931 +            && (bestJ<0 || compareCost(&sWBI.cost, &bestPlan))   /* (4) */
 1.16932          ){
 1.16933 -          WHERETRACE(("=== table %d is best so far"
 1.16934 -                      " with cost=%g and nRow=%g\n",
 1.16935 -                      j, sCost.rCost, sCost.plan.nRow));
 1.16936 -          bestPlan = sCost;
 1.16937 +          WHERETRACE(("   === table %d (%s) is best so far\n"
 1.16938 +                      "       cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=%08x\n",
 1.16939 +                      j, sWBI.pSrc->pTab->zName,
 1.16940 +                      sWBI.cost.rCost, sWBI.cost.plan.nRow,
 1.16941 +                      sWBI.cost.plan.nOBSat, sWBI.cost.plan.wsFlags));
 1.16942 +          bestPlan = sWBI.cost;
 1.16943            bestJ = j;
 1.16944          }
 1.16945          if( doNotReorder ) break;
 1.16946        }
 1.16947      }
 1.16948      assert( bestJ>=0 );
 1.16949 -    assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
 1.16950 -    WHERETRACE(("*** Optimizer selects table %d for loop %d"
 1.16951 -                " with cost=%g and nRow=%g\n",
 1.16952 -                bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
 1.16953 -    /* The ALWAYS() that follows was added to hush up clang scan-build */
 1.16954 -    if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){
 1.16955 -      *ppOrderBy = 0;
 1.16956 -    }
 1.16957 +    assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
 1.16958 +    WHERETRACE(("*** Optimizer selects table %d (%s) for loop %d with:\n"
 1.16959 +                "    cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=0x%08x\n",
 1.16960 +                bestJ, pTabList->a[bestJ].pTab->zName,
 1.16961 +                pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
 1.16962 +                bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
 1.16963      if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
 1.16964        assert( pWInfo->eDistinct==0 );
 1.16965        pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
 1.16966      }
 1.16967      andFlags &= bestPlan.plan.wsFlags;
 1.16968      pLevel->plan = bestPlan.plan;
 1.16969 +    pLevel->iTabCur = pTabList->a[bestJ].iCursor;
 1.16970      testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
 1.16971      testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
 1.16972      if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
 1.16973 -      pLevel->iIdxCur = pParse->nTab++;
 1.16974 +      if( (wctrlFlags & WHERE_ONETABLE_ONLY) 
 1.16975 +       && (bestPlan.plan.wsFlags & WHERE_TEMP_INDEX)==0 
 1.16976 +      ){
 1.16977 +        pLevel->iIdxCur = iIdxCur;
 1.16978 +      }else{
 1.16979 +        pLevel->iIdxCur = pParse->nTab++;
 1.16980 +      }
 1.16981      }else{
 1.16982        pLevel->iIdxCur = -1;
 1.16983      }
 1.16984 -    notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
 1.16985 +    sWBI.notValid &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
 1.16986      pLevel->iFrom = (u8)bestJ;
 1.16987      if( bestPlan.plan.nRow>=(double)1 ){
 1.16988        pParse->nQueryLoop *= bestPlan.plan.nRow;
 1.16989 @@ -108648,12 +107679,19 @@
 1.16990    if( pParse->nErr || db->mallocFailed ){
 1.16991      goto whereBeginError;
 1.16992    }
 1.16993 +  if( nTabList ){
 1.16994 +    pLevel--;
 1.16995 +    pWInfo->nOBSat = pLevel->plan.nOBSat;
 1.16996 +  }else{
 1.16997 +    pWInfo->nOBSat = 0;
 1.16998 +  }
 1.16999  
 1.17000    /* If the total query only selects a single row, then the ORDER BY
 1.17001    ** clause is irrelevant.
 1.17002    */
 1.17003 -  if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
 1.17004 -    *ppOrderBy = 0;
 1.17005 +  if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
 1.17006 +    assert( nTabList==0 || (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 );
 1.17007 +    pWInfo->nOBSat = pOrderBy->nExpr;
 1.17008    }
 1.17009  
 1.17010    /* If the caller is an UPDATE or DELETE statement that is requesting
 1.17011 @@ -108673,13 +107711,13 @@
 1.17012    sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
 1.17013    notReady = ~(Bitmask)0;
 1.17014    pWInfo->nRowOut = (double)1;
 1.17015 -  for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
 1.17016 +  for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
 1.17017      Table *pTab;     /* Table to open */
 1.17018      int iDb;         /* Index of database containing table/index */
 1.17019 +    struct SrcList_item *pTabItem;
 1.17020  
 1.17021      pTabItem = &pTabList->a[pLevel->iFrom];
 1.17022      pTab = pTabItem->pTab;
 1.17023 -    pLevel->iTabCur = pTabItem->iCursor;
 1.17024      pWInfo->nRowOut *= pLevel->plan.nRow;
 1.17025      iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.17026      if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
 1.17027 @@ -108690,6 +107728,8 @@
 1.17028        const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
 1.17029        int iCur = pTabItem->iCursor;
 1.17030        sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
 1.17031 +    }else if( IsVirtual(pTab) ){
 1.17032 +      /* noop */
 1.17033      }else
 1.17034  #endif
 1.17035      if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
 1.17036 @@ -108711,21 +107751,21 @@
 1.17037      }
 1.17038  #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
 1.17039      if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
 1.17040 -      constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel);
 1.17041 +      constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel);
 1.17042      }else
 1.17043  #endif
 1.17044      if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
 1.17045        Index *pIx = pLevel->plan.u.pIdx;
 1.17046        KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
 1.17047 -      int iIdxCur = pLevel->iIdxCur;
 1.17048 +      int iIndexCur = pLevel->iIdxCur;
 1.17049        assert( pIx->pSchema==pTab->pSchema );
 1.17050 -      assert( iIdxCur>=0 );
 1.17051 -      sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb,
 1.17052 +      assert( iIndexCur>=0 );
 1.17053 +      sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb,
 1.17054                          (char*)pKey, P4_KEYINFO_HANDOFF);
 1.17055        VdbeComment((v, "%s", pIx->zName));
 1.17056      }
 1.17057      sqlite3CodeVerifySchema(pParse, iDb);
 1.17058 -    notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor);
 1.17059 +    notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor);
 1.17060    }
 1.17061    pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
 1.17062    if( db->mallocFailed ) goto whereBeginError;
 1.17063 @@ -108735,10 +107775,10 @@
 1.17064    ** program.
 1.17065    */
 1.17066    notReady = ~(Bitmask)0;
 1.17067 -  for(i=0; i<nTabList; i++){
 1.17068 -    pLevel = &pWInfo->a[i];
 1.17069 -    explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
 1.17070 -    notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
 1.17071 +  for(ii=0; ii<nTabList; ii++){
 1.17072 +    pLevel = &pWInfo->a[ii];
 1.17073 +    explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
 1.17074 +    notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady);
 1.17075      pWInfo->iContinue = pLevel->addrCont;
 1.17076    }
 1.17077  
 1.17078 @@ -108749,16 +107789,20 @@
 1.17079    ** the index is listed as "{}".  If the primary key is used the
 1.17080    ** index name is '*'.
 1.17081    */
 1.17082 -  for(i=0; i<nTabList; i++){
 1.17083 +  for(ii=0; ii<nTabList; ii++){
 1.17084      char *z;
 1.17085      int n;
 1.17086 -    pLevel = &pWInfo->a[i];
 1.17087 +    int w;
 1.17088 +    struct SrcList_item *pTabItem;
 1.17089 +
 1.17090 +    pLevel = &pWInfo->a[ii];
 1.17091 +    w = pLevel->plan.wsFlags;
 1.17092      pTabItem = &pTabList->a[pLevel->iFrom];
 1.17093      z = pTabItem->zAlias;
 1.17094      if( z==0 ) z = pTabItem->pTab->zName;
 1.17095      n = sqlite3Strlen30(z);
 1.17096      if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
 1.17097 -      if( pLevel->plan.wsFlags & WHERE_IDX_ONLY ){
 1.17098 +      if( (w & WHERE_IDX_ONLY)!=0 && (w & WHERE_COVER_SCAN)==0 ){
 1.17099          memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
 1.17100          nQPlan += 2;
 1.17101        }else{
 1.17102 @@ -108767,12 +107811,12 @@
 1.17103        }
 1.17104        sqlite3_query_plan[nQPlan++] = ' ';
 1.17105      }
 1.17106 -    testcase( pLevel->plan.wsFlags & WHERE_ROWID_EQ );
 1.17107 -    testcase( pLevel->plan.wsFlags & WHERE_ROWID_RANGE );
 1.17108 -    if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
 1.17109 +    testcase( w & WHERE_ROWID_EQ );
 1.17110 +    testcase( w & WHERE_ROWID_RANGE );
 1.17111 +    if( w & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
 1.17112        memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
 1.17113        nQPlan += 2;
 1.17114 -    }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
 1.17115 +    }else if( (w & WHERE_INDEXED)!=0 && (w & WHERE_COVER_SCAN)==0 ){
 1.17116        n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
 1.17117        if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
 1.17118          memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
 1.17119 @@ -108868,6 +107912,7 @@
 1.17120    */
 1.17121    assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
 1.17122    for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
 1.17123 +    Index *pIdx = 0;
 1.17124      struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
 1.17125      Table *pTab = pTabItem->pTab;
 1.17126      assert( pTab!=0 );
 1.17127 @@ -108897,12 +107942,15 @@
 1.17128      ** that reference the table and converts them into opcodes that
 1.17129      ** reference the index.
 1.17130      */
 1.17131 -    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 && !db->mallocFailed){
 1.17132 +    if( pLevel->plan.wsFlags & WHERE_INDEXED ){
 1.17133 +      pIdx = pLevel->plan.u.pIdx;
 1.17134 +    }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
 1.17135 +      pIdx = pLevel->u.pCovidx;
 1.17136 +    }
 1.17137 +    if( pIdx && !db->mallocFailed){
 1.17138        int k, j, last;
 1.17139        VdbeOp *pOp;
 1.17140 -      Index *pIdx = pLevel->plan.u.pIdx;
 1.17141 -
 1.17142 -      assert( pIdx!=0 );
 1.17143 +
 1.17144        pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
 1.17145        last = sqlite3VdbeCurrentAddr(v);
 1.17146        for(k=pWInfo->iTop; k<last; k++, pOp++){
 1.17147 @@ -111627,7 +110675,7 @@
 1.17148          break;
 1.17149        case 194: /* expr ::= expr COLLATE ids */
 1.17150  {
 1.17151 -  yygotominor.yy342.pExpr = sqlite3ExprSetCollByToken(pParse, yymsp[-2].minor.yy342.pExpr, &yymsp[0].minor.yy0);
 1.17152 +  yygotominor.yy342.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy342.pExpr, &yymsp[0].minor.yy0);
 1.17153    yygotominor.yy342.zStart = yymsp[-2].minor.yy342.zStart;
 1.17154    yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 1.17155  }
 1.17156 @@ -111886,11 +110934,7 @@
 1.17157          break;
 1.17158        case 247: /* idxlist ::= idxlist COMMA nm collate sortorder */
 1.17159  {
 1.17160 -  Expr *p = 0;
 1.17161 -  if( yymsp[-1].minor.yy0.n>0 ){
 1.17162 -    p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
 1.17163 -    sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
 1.17164 -  }
 1.17165 +  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
 1.17166    yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy442, p);
 1.17167    sqlite3ExprListSetName(pParse,yygotominor.yy442,&yymsp[-2].minor.yy0,1);
 1.17168    sqlite3ExprListCheckLength(pParse, yygotominor.yy442, "index");
 1.17169 @@ -111899,11 +110943,7 @@
 1.17170          break;
 1.17171        case 248: /* idxlist ::= nm collate sortorder */
 1.17172  {
 1.17173 -  Expr *p = 0;
 1.17174 -  if( yymsp[-1].minor.yy0.n>0 ){
 1.17175 -    p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
 1.17176 -    sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
 1.17177 -  }
 1.17178 +  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
 1.17179    yygotominor.yy442 = sqlite3ExprListAppend(pParse,0, p);
 1.17180    sqlite3ExprListSetName(pParse, yygotominor.yy442, &yymsp[-2].minor.yy0, 1);
 1.17181    sqlite3ExprListCheckLength(pParse, yygotominor.yy442, "index");
 1.17182 @@ -112152,6 +111192,7 @@
 1.17183        /* (326) anylist ::= anylist ANY */ yytestcase(yyruleno==326);
 1.17184          break;
 1.17185    };
 1.17186 +  assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
 1.17187    yygoto = yyRuleInfo[yyruleno].lhs;
 1.17188    yysize = yyRuleInfo[yyruleno].nrhs;
 1.17189    yypParser->yyidx -= yysize;
 1.17190 @@ -113717,6 +112758,13 @@
 1.17191    */
 1.17192    if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
 1.17193  
 1.17194 +#ifdef SQLITE_ENABLE_SQLLOG
 1.17195 +  {
 1.17196 +    extern void sqlite3_init_sqllog(void);
 1.17197 +    sqlite3_init_sqllog();
 1.17198 +  }
 1.17199 +#endif
 1.17200 +
 1.17201    /* Make sure the mutex subsystem is initialized.  If unable to 
 1.17202    ** initialize the mutex subsystem, return early with the error.
 1.17203    ** If the system is so sick that we are unable to allocate a mutex,
 1.17204 @@ -114060,6 +113108,20 @@
 1.17205        break;
 1.17206      }
 1.17207  
 1.17208 +    case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
 1.17209 +      sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
 1.17210 +      break;
 1.17211 +    }
 1.17212 +
 1.17213 +#ifdef SQLITE_ENABLE_SQLLOG
 1.17214 +    case SQLITE_CONFIG_SQLLOG: {
 1.17215 +      typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
 1.17216 +      sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
 1.17217 +      sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
 1.17218 +      break;
 1.17219 +    }
 1.17220 +#endif
 1.17221 +
 1.17222      default: {
 1.17223        rc = SQLITE_ERROR;
 1.17224        break;
 1.17225 @@ -114351,12 +113413,24 @@
 1.17226  }
 1.17227  
 1.17228  /*
 1.17229 +** Return TRUE if database connection db has unfinalized prepared
 1.17230 +** statements or unfinished sqlite3_backup objects.  
 1.17231 +*/
 1.17232 +static int connectionIsBusy(sqlite3 *db){
 1.17233 +  int j;
 1.17234 +  assert( sqlite3_mutex_held(db->mutex) );
 1.17235 +  if( db->pVdbe ) return 1;
 1.17236 +  for(j=0; j<db->nDb; j++){
 1.17237 +    Btree *pBt = db->aDb[j].pBt;
 1.17238 +    if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
 1.17239 +  }
 1.17240 +  return 0;
 1.17241 +}
 1.17242 +
 1.17243 +/*
 1.17244  ** Close an existing SQLite database
 1.17245  */
 1.17246 -SQLITE_API int sqlite3_close(sqlite3 *db){
 1.17247 -  HashElem *i;                    /* Hash table iterator */
 1.17248 -  int j;
 1.17249 -
 1.17250 +static int sqlite3Close(sqlite3 *db, int forceZombie){
 1.17251    if( !db ){
 1.17252      return SQLITE_OK;
 1.17253    }
 1.17254 @@ -114377,24 +113451,69 @@
 1.17255    */
 1.17256    sqlite3VtabRollback(db);
 1.17257  
 1.17258 -  /* If there are any outstanding VMs, return SQLITE_BUSY. */
 1.17259 -  if( db->pVdbe ){
 1.17260 -    sqlite3Error(db, SQLITE_BUSY, 
 1.17261 -        "unable to close due to unfinalised statements");
 1.17262 +  /* Legacy behavior (sqlite3_close() behavior) is to return
 1.17263 +  ** SQLITE_BUSY if the connection can not be closed immediately.
 1.17264 +  */
 1.17265 +  if( !forceZombie && connectionIsBusy(db) ){
 1.17266 +    sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
 1.17267 +       "statements or unfinished backups");
 1.17268      sqlite3_mutex_leave(db->mutex);
 1.17269      return SQLITE_BUSY;
 1.17270    }
 1.17271 -  assert( sqlite3SafetyCheckSickOrOk(db) );
 1.17272 -
 1.17273 -  for(j=0; j<db->nDb; j++){
 1.17274 -    Btree *pBt = db->aDb[j].pBt;
 1.17275 -    if( pBt && sqlite3BtreeIsInBackup(pBt) ){
 1.17276 -      sqlite3Error(db, SQLITE_BUSY, 
 1.17277 -          "unable to close due to unfinished backup operation");
 1.17278 -      sqlite3_mutex_leave(db->mutex);
 1.17279 -      return SQLITE_BUSY;
 1.17280 -    }
 1.17281 -  }
 1.17282 +
 1.17283 +#ifdef SQLITE_ENABLE_SQLLOG
 1.17284 +  if( sqlite3GlobalConfig.xSqllog ){
 1.17285 +    /* Closing the handle. Fourth parameter is passed the value 2. */
 1.17286 +    sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
 1.17287 +  }
 1.17288 +#endif
 1.17289 +
 1.17290 +  /* Convert the connection into a zombie and then close it.
 1.17291 +  */
 1.17292 +  db->magic = SQLITE_MAGIC_ZOMBIE;
 1.17293 +  sqlite3LeaveMutexAndCloseZombie(db);
 1.17294 +  return SQLITE_OK;
 1.17295 +}
 1.17296 +
 1.17297 +/*
 1.17298 +** Two variations on the public interface for closing a database
 1.17299 +** connection. The sqlite3_close() version returns SQLITE_BUSY and
 1.17300 +** leaves the connection option if there are unfinalized prepared
 1.17301 +** statements or unfinished sqlite3_backups.  The sqlite3_close_v2()
 1.17302 +** version forces the connection to become a zombie if there are
 1.17303 +** unclosed resources, and arranges for deallocation when the last
 1.17304 +** prepare statement or sqlite3_backup closes.
 1.17305 +*/
 1.17306 +SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
 1.17307 +SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
 1.17308 +
 1.17309 +
 1.17310 +/*
 1.17311 +** Close the mutex on database connection db.
 1.17312 +**
 1.17313 +** Furthermore, if database connection db is a zombie (meaning that there
 1.17314 +** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
 1.17315 +** every sqlite3_stmt has now been finalized and every sqlite3_backup has
 1.17316 +** finished, then free all resources.
 1.17317 +*/
 1.17318 +SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
 1.17319 +  HashElem *i;                    /* Hash table iterator */
 1.17320 +  int j;
 1.17321 +
 1.17322 +  /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
 1.17323 +  ** or if the connection has not yet been closed by sqlite3_close_v2(),
 1.17324 +  ** then just leave the mutex and return.
 1.17325 +  */
 1.17326 +  if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
 1.17327 +    sqlite3_mutex_leave(db->mutex);
 1.17328 +    return;
 1.17329 +  }
 1.17330 +
 1.17331 +  /* If we reach this point, it means that the database connection has
 1.17332 +  ** closed all sqlite3_stmt and sqlite3_backup objects and has been
 1.17333 +  ** pased to sqlite3_close (meaning that it is a zombie).  Therefore,
 1.17334 +  ** go ahead and free all resources.
 1.17335 +  */
 1.17336  
 1.17337    /* Free any outstanding Savepoint structures. */
 1.17338    sqlite3CloseSavepoints(db);
 1.17339 @@ -114483,7 +113602,6 @@
 1.17340      sqlite3_free(db->lookaside.pStart);
 1.17341    }
 1.17342    sqlite3_free(db);
 1.17343 -  return SQLITE_OK;
 1.17344  }
 1.17345  
 1.17346  /*
 1.17347 @@ -114652,6 +113770,7 @@
 1.17348    db->busyHandler.xFunc = xBusy;
 1.17349    db->busyHandler.pArg = pArg;
 1.17350    db->busyHandler.nBusy = 0;
 1.17351 +  db->busyTimeout = 0;
 1.17352    sqlite3_mutex_leave(db->mutex);
 1.17353    return SQLITE_OK;
 1.17354  }
 1.17355 @@ -114689,8 +113808,8 @@
 1.17356  */
 1.17357  SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
 1.17358    if( ms>0 ){
 1.17359 +    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
 1.17360      db->busyTimeout = ms;
 1.17361 -    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
 1.17362    }else{
 1.17363      sqlite3_busy_handler(db, 0, 0);
 1.17364    }
 1.17365 @@ -115304,6 +114423,15 @@
 1.17366  }
 1.17367  
 1.17368  /*
 1.17369 +** Return a string that describes the kind of error specified in the
 1.17370 +** argument.  For now, this simply calls the internal sqlite3ErrStr()
 1.17371 +** function.
 1.17372 +*/
 1.17373 +SQLITE_API const char *sqlite3_errstr(int rc){
 1.17374 +  return sqlite3ErrStr(rc);
 1.17375 +}
 1.17376 +
 1.17377 +/*
 1.17378  ** Create a new collating function for database "db".  The name is zName
 1.17379  ** and the encoding is enc.
 1.17380  */
 1.17381 @@ -115972,6 +115100,13 @@
 1.17382      db->magic = SQLITE_MAGIC_SICK;
 1.17383    }
 1.17384    *ppDb = db;
 1.17385 +#ifdef SQLITE_ENABLE_SQLLOG
 1.17386 +  if( sqlite3GlobalConfig.xSqllog ){
 1.17387 +    /* Opening a db handle. Fourth parameter is passed 0. */
 1.17388 +    void *pArg = sqlite3GlobalConfig.pSqllogArg;
 1.17389 +    sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
 1.17390 +  }
 1.17391 +#endif
 1.17392    return sqlite3ApiExit(0, rc);
 1.17393  }
 1.17394  
 1.17395 @@ -116277,7 +115412,7 @@
 1.17396      zDataType = pCol->zType;
 1.17397      zCollSeq = pCol->zColl;
 1.17398      notnull = pCol->notNull!=0;
 1.17399 -    primarykey  = pCol->isPrimKey!=0;
 1.17400 +    primarykey  = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
 1.17401      autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
 1.17402    }else{
 1.17403      zDataType = "INTEGER";
 1.17404 @@ -116540,8 +115675,7 @@
 1.17405      */
 1.17406      case SQLITE_TESTCTRL_OPTIMIZATIONS: {
 1.17407        sqlite3 *db = va_arg(ap, sqlite3*);
 1.17408 -      int x = va_arg(ap,int);
 1.17409 -      db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
 1.17410 +      db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
 1.17411        break;
 1.17412      }
 1.17413  
 1.17414 @@ -118027,10 +117161,20 @@
 1.17415  SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
 1.17416  SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
 1.17417  
 1.17418 +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
 1.17419  SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
 1.17420  SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
 1.17421  SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
 1.17422  SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
 1.17423 +SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
 1.17424 +#else
 1.17425 +# define sqlite3Fts3FreeDeferredTokens(x)
 1.17426 +# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
 1.17427 +# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
 1.17428 +# define sqlite3Fts3FreeDeferredDoclists(x)
 1.17429 +# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
 1.17430 +#endif
 1.17431 +
 1.17432  SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
 1.17433  SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
 1.17434  
 1.17435 @@ -118139,8 +117283,6 @@
 1.17436  SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
 1.17437  SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
 1.17438  
 1.17439 -SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
 1.17440 -
 1.17441  /* fts3_unicode2.c (functions generated by parsing unicode text files) */
 1.17442  #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 1.17443  SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
 1.17444 @@ -122298,6 +121440,7 @@
 1.17445    fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
 1.17446  
 1.17447    /* Determine which, if any, tokens in the expression should be deferred. */
 1.17448 +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
 1.17449    if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
 1.17450      Fts3TokenAndCost *aTC;
 1.17451      Fts3Expr **apOr;
 1.17452 @@ -122328,6 +121471,7 @@
 1.17453        sqlite3_free(aTC);
 1.17454      }
 1.17455    }
 1.17456 +#endif
 1.17457  
 1.17458    fts3EvalStartReaders(pCsr, pCsr->pExpr, 1, &rc);
 1.17459    return rc;
 1.17460 @@ -122603,35 +121747,39 @@
 1.17461        nTmp += p->pRight->pPhrase->doclist.nList;
 1.17462      }
 1.17463      nTmp += p->pPhrase->doclist.nList;
 1.17464 -    aTmp = sqlite3_malloc(nTmp*2);
 1.17465 -    if( !aTmp ){
 1.17466 -      *pRc = SQLITE_NOMEM;
 1.17467 +    if( nTmp==0 ){
 1.17468        res = 0;
 1.17469      }else{
 1.17470 -      char *aPoslist = p->pPhrase->doclist.pList;
 1.17471 -      int nToken = p->pPhrase->nToken;
 1.17472 -
 1.17473 -      for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
 1.17474 -        Fts3Phrase *pPhrase = p->pRight->pPhrase;
 1.17475 -        int nNear = p->nNear;
 1.17476 -        res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
 1.17477 -      }
 1.17478 -  
 1.17479 -      aPoslist = pExpr->pRight->pPhrase->doclist.pList;
 1.17480 -      nToken = pExpr->pRight->pPhrase->nToken;
 1.17481 -      for(p=pExpr->pLeft; p && res; p=p->pLeft){
 1.17482 -        int nNear;
 1.17483 -        Fts3Phrase *pPhrase;
 1.17484 -        assert( p->pParent && p->pParent->pLeft==p );
 1.17485 -        nNear = p->pParent->nNear;
 1.17486 -        pPhrase = (
 1.17487 -            p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
 1.17488 -        );
 1.17489 -        res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
 1.17490 -      }
 1.17491 -    }
 1.17492 -
 1.17493 -    sqlite3_free(aTmp);
 1.17494 +      aTmp = sqlite3_malloc(nTmp*2);
 1.17495 +      if( !aTmp ){
 1.17496 +        *pRc = SQLITE_NOMEM;
 1.17497 +        res = 0;
 1.17498 +      }else{
 1.17499 +        char *aPoslist = p->pPhrase->doclist.pList;
 1.17500 +        int nToken = p->pPhrase->nToken;
 1.17501 +
 1.17502 +        for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
 1.17503 +          Fts3Phrase *pPhrase = p->pRight->pPhrase;
 1.17504 +          int nNear = p->nNear;
 1.17505 +          res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
 1.17506 +        }
 1.17507 +
 1.17508 +        aPoslist = pExpr->pRight->pPhrase->doclist.pList;
 1.17509 +        nToken = pExpr->pRight->pPhrase->nToken;
 1.17510 +        for(p=pExpr->pLeft; p && res; p=p->pLeft){
 1.17511 +          int nNear;
 1.17512 +          Fts3Phrase *pPhrase;
 1.17513 +          assert( p->pParent && p->pParent->pLeft==p );
 1.17514 +          nNear = p->pParent->nNear;
 1.17515 +          pPhrase = (
 1.17516 +              p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
 1.17517 +              );
 1.17518 +          res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
 1.17519 +        }
 1.17520 +      }
 1.17521 +
 1.17522 +      sqlite3_free(aTmp);
 1.17523 +    }
 1.17524    }
 1.17525  
 1.17526    return res;
 1.17527 @@ -122711,6 +121859,7 @@
 1.17528          break;
 1.17529  
 1.17530        default: {
 1.17531 +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
 1.17532          if( pCsr->pDeferred 
 1.17533           && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
 1.17534          ){
 1.17535 @@ -122722,7 +121871,9 @@
 1.17536            *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
 1.17537            bHit = (pPhrase->doclist.pList!=0);
 1.17538            pExpr->iDocid = pCsr->iPrevId;
 1.17539 -        }else{
 1.17540 +        }else
 1.17541 +#endif
 1.17542 +        {
 1.17543            bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
 1.17544          }
 1.17545          break;
 1.17546 @@ -123866,7 +123017,7 @@
 1.17547    rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
 1.17548    if( rc==SQLITE_OK ){
 1.17549      const char *zToken;
 1.17550 -    int nToken, iStart, iEnd, iPosition;
 1.17551 +    int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
 1.17552      int nByte;                               /* total space to allocate */
 1.17553  
 1.17554      rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
 1.17555 @@ -123981,7 +123132,7 @@
 1.17556      int ii;
 1.17557      for(ii=0; rc==SQLITE_OK; ii++){
 1.17558        const char *zByte;
 1.17559 -      int nByte, iBegin, iEnd, iPos;
 1.17560 +      int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
 1.17561        rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
 1.17562        if( rc==SQLITE_OK ){
 1.17563          Fts3PhraseToken *pToken;
 1.17564 @@ -125978,10 +125129,10 @@
 1.17565    const char *azArg[64];
 1.17566  
 1.17567    const char *zToken;
 1.17568 -  int nToken;
 1.17569 -  int iStart;
 1.17570 -  int iEnd;
 1.17571 -  int iPos;
 1.17572 +  int nToken = 0;
 1.17573 +  int iStart = 0;
 1.17574 +  int iEnd = 0;
 1.17575 +  int iPos = 0;
 1.17576    int i;
 1.17577  
 1.17578    Tcl_Obj *pRet;
 1.17579 @@ -127228,16 +126379,16 @@
 1.17580    int iLangid,                    /* Language id to use */
 1.17581    const char *zText,              /* Text of document to be inserted */
 1.17582    int iCol,                       /* Column into which text is being inserted */
 1.17583 -  u32 *pnWord                     /* OUT: Number of tokens inserted */
 1.17584 -){
 1.17585 -  int rc;
 1.17586 -  int iStart;
 1.17587 -  int iEnd;
 1.17588 -  int iPos;
 1.17589 +  u32 *pnWord                     /* IN/OUT: Incr. by number tokens inserted */
 1.17590 +){
 1.17591 +  int rc;
 1.17592 +  int iStart = 0;
 1.17593 +  int iEnd = 0;
 1.17594 +  int iPos = 0;
 1.17595    int nWord = 0;
 1.17596  
 1.17597    char const *zToken;
 1.17598 -  int nToken;
 1.17599 +  int nToken = 0;
 1.17600  
 1.17601    sqlite3_tokenizer *pTokenizer = p->pTokenizer;
 1.17602    sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
 1.17603 @@ -127292,7 +126443,7 @@
 1.17604    }
 1.17605  
 1.17606    pModule->xClose(pCsr);
 1.17607 -  *pnWord = nWord;
 1.17608 +  *pnWord += nWord;
 1.17609    return (rc==SQLITE_DONE ? SQLITE_OK : rc);
 1.17610  }
 1.17611  
 1.17612 @@ -127496,11 +126647,13 @@
 1.17613    int *pRC,               /* Result code */
 1.17614    Fts3Table *p,           /* The FTS table to delete from */
 1.17615    sqlite3_value *pRowid,  /* The docid to be deleted */
 1.17616 -  u32 *aSz                /* Sizes of deleted document written here */
 1.17617 +  u32 *aSz,               /* Sizes of deleted document written here */
 1.17618 +  int *pbFound            /* OUT: Set to true if row really does exist */
 1.17619  ){
 1.17620    int rc;
 1.17621    sqlite3_stmt *pSelect;
 1.17622  
 1.17623 +  assert( *pbFound==0 );
 1.17624    if( *pRC ) return;
 1.17625    rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
 1.17626    if( rc==SQLITE_OK ){
 1.17627 @@ -127518,6 +126671,7 @@
 1.17628          *pRC = rc;
 1.17629          return;
 1.17630        }
 1.17631 +      *pbFound = 1;
 1.17632      }
 1.17633      rc = sqlite3_reset(pSelect);
 1.17634    }else{
 1.17635 @@ -129421,7 +128575,7 @@
 1.17636  
 1.17637    if( iLevel==FTS3_SEGCURSOR_ALL ){
 1.17638      /* This call is to merge all segments in the database to a single
 1.17639 -    ** segment. The level of the new segment is equal to the the numerically 
 1.17640 +    ** segment. The level of the new segment is equal to the numerically
 1.17641      ** greatest segment level currently present in the database for this
 1.17642      ** index. The idx of the new segment is always 0.  */
 1.17643      if( csr.nSegment==1 ){
 1.17644 @@ -129742,7 +128896,7 @@
 1.17645        int iCol;
 1.17646        int iLangid = langidFromSelect(p, pStmt);
 1.17647        rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pStmt, 0));
 1.17648 -      aSz[p->nColumn] = 0;
 1.17649 +      memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
 1.17650        for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
 1.17651          const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
 1.17652          rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
 1.17653 @@ -130051,7 +129205,7 @@
 1.17654          pNode->key.n = nTerm;
 1.17655        }
 1.17656      }else{
 1.17657 -      /* Otherwise, flush the the current node of layer iLayer to disk.
 1.17658 +      /* Otherwise, flush the current node of layer iLayer to disk.
 1.17659        ** Then allocate a new, empty sibling node. The key will be written
 1.17660        ** into the parent of this node. */
 1.17661        rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
 1.17662 @@ -131386,9 +130540,9 @@
 1.17663          rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText, &pT);
 1.17664          while( rc==SQLITE_OK ){
 1.17665            char const *zToken;       /* Buffer containing token */
 1.17666 -          int nToken;               /* Number of bytes in token */
 1.17667 -          int iDum1, iDum2;         /* Dummy variables */
 1.17668 -          int iPos;                 /* Position of token in zText */
 1.17669 +          int nToken = 0;           /* Number of bytes in token */
 1.17670 +          int iDum1 = 0, iDum2 = 0; /* Dummy variables */
 1.17671 +          int iPos = 0;             /* Position of token in zText */
 1.17672  
 1.17673            rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
 1.17674            if( rc==SQLITE_OK ){
 1.17675 @@ -131498,6 +130652,7 @@
 1.17676    return rc;
 1.17677  }
 1.17678  
 1.17679 +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
 1.17680  /*
 1.17681  ** Delete all cached deferred doclists. Deferred doclists are cached
 1.17682  ** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
 1.17683 @@ -131554,9 +130709,9 @@
 1.17684        rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
 1.17685        while( rc==SQLITE_OK ){
 1.17686          char const *zToken;       /* Buffer containing token */
 1.17687 -        int nToken;               /* Number of bytes in token */
 1.17688 -        int iDum1, iDum2;         /* Dummy variables */
 1.17689 -        int iPos;                 /* Position of token in zText */
 1.17690 +        int nToken = 0;           /* Number of bytes in token */
 1.17691 +        int iDum1 = 0, iDum2 = 0; /* Dummy variables */
 1.17692 +        int iPos = 0;             /* Position of token in zText */
 1.17693    
 1.17694          rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
 1.17695          for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
 1.17696 @@ -131635,6 +130790,7 @@
 1.17697  
 1.17698    return SQLITE_OK;
 1.17699  }
 1.17700 +#endif
 1.17701  
 1.17702  /*
 1.17703  ** SQLite value pRowid contains the rowid of a row that may or may not be
 1.17704 @@ -131644,28 +130800,32 @@
 1.17705  static int fts3DeleteByRowid(
 1.17706    Fts3Table *p, 
 1.17707    sqlite3_value *pRowid, 
 1.17708 -  int *pnDoc,
 1.17709 +  int *pnChng,                    /* IN/OUT: Decrement if row is deleted */
 1.17710    u32 *aSzDel
 1.17711  ){
 1.17712 -  int isEmpty = 0;
 1.17713 -  int rc = fts3IsEmpty(p, pRowid, &isEmpty);
 1.17714 -  if( rc==SQLITE_OK ){
 1.17715 -    if( isEmpty ){
 1.17716 -      /* Deleting this row means the whole table is empty. In this case
 1.17717 -      ** delete the contents of all three tables and throw away any
 1.17718 -      ** data in the pendingTerms hash table.  */
 1.17719 -      rc = fts3DeleteAll(p, 1);
 1.17720 -      *pnDoc = *pnDoc - 1;
 1.17721 -    }else{
 1.17722 -      fts3DeleteTerms(&rc, p, pRowid, aSzDel);
 1.17723 -      if( p->zContentTbl==0 ){
 1.17724 -        fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
 1.17725 -        if( sqlite3_changes(p->db) ) *pnDoc = *pnDoc - 1;
 1.17726 -      }else{
 1.17727 -        *pnDoc = *pnDoc - 1;
 1.17728 -      }
 1.17729 -      if( p->bHasDocsize ){
 1.17730 -        fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
 1.17731 +  int rc = SQLITE_OK;             /* Return code */
 1.17732 +  int bFound = 0;                 /* True if *pRowid really is in the table */
 1.17733 +
 1.17734 +  fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
 1.17735 +  if( bFound && rc==SQLITE_OK ){
 1.17736 +    int isEmpty = 0;              /* Deleting *pRowid leaves the table empty */
 1.17737 +    rc = fts3IsEmpty(p, pRowid, &isEmpty);
 1.17738 +    if( rc==SQLITE_OK ){
 1.17739 +      if( isEmpty ){
 1.17740 +        /* Deleting this row means the whole table is empty. In this case
 1.17741 +        ** delete the contents of all three tables and throw away any
 1.17742 +        ** data in the pendingTerms hash table.  */
 1.17743 +        rc = fts3DeleteAll(p, 1);
 1.17744 +        *pnChng = 0;
 1.17745 +        memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
 1.17746 +      }else{
 1.17747 +        *pnChng = *pnChng - 1;
 1.17748 +        if( p->zContentTbl==0 ){
 1.17749 +          fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
 1.17750 +        }
 1.17751 +        if( p->bHasDocsize ){
 1.17752 +          fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
 1.17753 +        }
 1.17754        }
 1.17755      }
 1.17756    }
 1.17757 @@ -131696,7 +130856,7 @@
 1.17758    int rc = SQLITE_OK;             /* Return Code */
 1.17759    int isRemove = 0;               /* True for an UPDATE or DELETE */
 1.17760    u32 *aSzIns = 0;                /* Sizes of inserted documents */
 1.17761 -  u32 *aSzDel;                    /* Sizes of deleted documents */
 1.17762 +  u32 *aSzDel = 0;                /* Sizes of deleted documents */
 1.17763    int nChng = 0;                  /* Net change in number of documents */
 1.17764    int bInsertDone = 0;
 1.17765  
 1.17766 @@ -131724,13 +130884,13 @@
 1.17767    }
 1.17768  
 1.17769    /* Allocate space to hold the change in document sizes */
 1.17770 -  aSzIns = sqlite3_malloc( sizeof(aSzIns[0])*(p->nColumn+1)*2 );
 1.17771 -  if( aSzIns==0 ){
 1.17772 +  aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
 1.17773 +  if( aSzDel==0 ){
 1.17774      rc = SQLITE_NOMEM;
 1.17775      goto update_out;
 1.17776    }
 1.17777 -  aSzDel = &aSzIns[p->nColumn+1];
 1.17778 -  memset(aSzIns, 0, sizeof(aSzIns[0])*(p->nColumn+1)*2);
 1.17779 +  aSzIns = &aSzDel[p->nColumn+1];
 1.17780 +  memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
 1.17781  
 1.17782    /* If this is an INSERT operation, or an UPDATE that modifies the rowid
 1.17783    ** value, then this operation requires constraint handling.
 1.17784 @@ -131815,7 +130975,7 @@
 1.17785    }
 1.17786  
 1.17787   update_out:
 1.17788 -  sqlite3_free(aSzIns);
 1.17789 +  sqlite3_free(aSzDel);
 1.17790    sqlite3Fts3SegmentsClose(p);
 1.17791    return rc;
 1.17792  }
 1.17793 @@ -132423,7 +131583,7 @@
 1.17794          return rc;
 1.17795        }
 1.17796        while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
 1.17797 -        const char *ZDUMMY; int DUMMY1, DUMMY2, DUMMY3;
 1.17798 +        const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
 1.17799          rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
 1.17800        }
 1.17801        pMod->xClose(pC);
 1.17802 @@ -132467,8 +131627,6 @@
 1.17803    int iCol = pFragment->iCol+1;   /* Query column to extract text from */
 1.17804    sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
 1.17805    sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
 1.17806 -  const char *ZDUMMY;             /* Dummy argument used with tokenizer */
 1.17807 -  int DUMMY1;                     /* Dummy argument used with tokenizer */
 1.17808    
 1.17809    zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
 1.17810    if( zDoc==0 ){
 1.17811 @@ -132487,10 +131645,23 @@
 1.17812    }
 1.17813  
 1.17814    while( rc==SQLITE_OK ){
 1.17815 -    int iBegin;                   /* Offset in zDoc of start of token */
 1.17816 -    int iFin;                     /* Offset in zDoc of end of token */
 1.17817 -    int isHighlight;              /* True for highlighted terms */
 1.17818 -
 1.17819 +    const char *ZDUMMY;           /* Dummy argument used with tokenizer */
 1.17820 +    int DUMMY1 = -1;              /* Dummy argument used with tokenizer */
 1.17821 +    int iBegin = 0;               /* Offset in zDoc of start of token */
 1.17822 +    int iFin = 0;                 /* Offset in zDoc of end of token */
 1.17823 +    int isHighlight = 0;          /* True for highlighted terms */
 1.17824 +
 1.17825 +    /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
 1.17826 +    ** in the FTS code the variable that the third argument to xNext points to
 1.17827 +    ** is initialized to zero before the first (*but not necessarily
 1.17828 +    ** subsequent*) call to xNext(). This is done for a particular application
 1.17829 +    ** that needs to know whether or not the tokenizer is being used for
 1.17830 +    ** snippet generation or for some other purpose.
 1.17831 +    **
 1.17832 +    ** Extreme care is required when writing code to depend on this
 1.17833 +    ** initialization. It is not a documented part of the tokenizer interface.
 1.17834 +    ** If a tokenizer is used directly by any code outside of FTS, this
 1.17835 +    ** convention might not be respected.  */
 1.17836      rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
 1.17837      if( rc!=SQLITE_OK ){
 1.17838        if( rc==SQLITE_DONE ){
 1.17839 @@ -133180,8 +132351,6 @@
 1.17840  ){
 1.17841    Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 1.17842    sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
 1.17843 -  const char *ZDUMMY;             /* Dummy argument used with xNext() */
 1.17844 -  int NDUMMY;                     /* Dummy argument used with xNext() */
 1.17845    int rc;                         /* Return Code */
 1.17846    int nToken;                     /* Number of tokens in query */
 1.17847    int iCol;                       /* Column currently being processed */
 1.17848 @@ -133214,9 +132383,11 @@
 1.17849    */
 1.17850    for(iCol=0; iCol<pTab->nColumn; iCol++){
 1.17851      sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
 1.17852 -    int iStart;
 1.17853 -    int iEnd;
 1.17854 -    int iCurrent;
 1.17855 +    const char *ZDUMMY;           /* Dummy argument used with xNext() */
 1.17856 +    int NDUMMY = 0;               /* Dummy argument used with xNext() */
 1.17857 +    int iStart = 0;
 1.17858 +    int iEnd = 0;
 1.17859 +    int iCurrent = 0;
 1.17860      const char *zDoc;
 1.17861      int nDoc;
 1.17862  
 1.17863 @@ -133899,7 +133070,7 @@
 1.17864      }
 1.17865      assert( aEntry[0]<key );
 1.17866      assert( key>=aEntry[iRes] );
 1.17867 -    return (c >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
 1.17868 +    return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
 1.17869    }
 1.17870    return 1;
 1.17871  }
 1.17872 @@ -136780,7 +135951,7 @@
 1.17873  */
 1.17874  static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
 1.17875    int rc;                         /* Return code */
 1.17876 -  RtreeNode *pLeaf;               /* Leaf node containing record iDelete */
 1.17877 +  RtreeNode *pLeaf = 0;           /* Leaf node containing record iDelete */
 1.17878    int iCell;                      /* Index of iDelete cell in pLeaf */
 1.17879    RtreeNode *pRoot;               /* Root node of rtree structure */
 1.17880  
 1.17881 @@ -136983,7 +136154,7 @@
 1.17882    */
 1.17883    if( rc==SQLITE_OK && nData>1 ){
 1.17884      /* Insert the new record into the r-tree */
 1.17885 -    RtreeNode *pLeaf;
 1.17886 +    RtreeNode *pLeaf = 0;
 1.17887  
 1.17888      /* Figure out the rowid of the new row. */
 1.17889      if( bHaveRowid==0 ){
 1.17890 @@ -138099,7 +137270,7 @@
 1.17891    nChar = nInput+1;
 1.17892    pCsr = (IcuCursor *)sqlite3_malloc(
 1.17893        sizeof(IcuCursor) +                /* IcuCursor */
 1.17894 -      nChar * sizeof(UChar) +            /* IcuCursor.aChar[] */
 1.17895 +      ((nChar+3)&~3) * sizeof(UChar) +   /* IcuCursor.aChar[] */
 1.17896        (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
 1.17897    );
 1.17898    if( !pCsr ){
 1.17899 @@ -138107,7 +137278,7 @@
 1.17900    }
 1.17901    memset(pCsr, 0, sizeof(IcuCursor));
 1.17902    pCsr->aChar = (UChar *)&pCsr[1];
 1.17903 -  pCsr->aOffset = (int *)&pCsr->aChar[nChar];
 1.17904 +  pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
 1.17905  
 1.17906    pCsr->aOffset[iOut] = iInput;
 1.17907    U8_NEXT(zInput, iInput, nInput, c); 
 1.17908 @@ -138179,7 +137350,7 @@
 1.17909  
 1.17910      while( iStart<iEnd ){
 1.17911        int iWhite = iStart;
 1.17912 -      U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
 1.17913 +      U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
 1.17914        if( u_isspace(c) ){
 1.17915          iStart = iWhite;
 1.17916        }else{
     2.1 --- a/extensions/sqlite/sqlite-source/sqlite3.h	Sun Dec 23 16:59:50 2012 -0500
     2.2 +++ b/extensions/sqlite/sqlite-source/sqlite3.h	Sun Dec 23 17:06:11 2012 -0500
     2.3 @@ -107,9 +107,9 @@
     2.4  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
     2.5  ** [sqlite_version()] and [sqlite_source_id()].
     2.6  */
     2.7 -#define SQLITE_VERSION        "3.7.13"
     2.8 -#define SQLITE_VERSION_NUMBER 3007013
     2.9 -#define SQLITE_SOURCE_ID      "2012-06-11 02:05:22 f5b5a13f7394dc143aa136f1d4faba6839eaa6dc"
    2.10 +#define SQLITE_VERSION        "3.7.15.1"
    2.11 +#define SQLITE_VERSION_NUMBER 3007015
    2.12 +#define SQLITE_SOURCE_ID      "2012-12-19 20:39:10 6b85b767d0ff7975146156a99ad673f2c1a23318"
    2.13  
    2.14  /*
    2.15  ** CAPI3REF: Run-Time Library Version Numbers
    2.16 @@ -219,7 +219,8 @@
    2.17  ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
    2.18  ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
    2.19  ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
    2.20 -** is its destructor.  There are many other interfaces (such as
    2.21 +** and [sqlite3_close_v2()] are its destructors.  There are many other
    2.22 +** interfaces (such as
    2.23  ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
    2.24  ** [sqlite3_busy_timeout()] to name but three) that are methods on an
    2.25  ** sqlite3 object.
    2.26 @@ -266,28 +267,46 @@
    2.27  /*
    2.28  ** CAPI3REF: Closing A Database Connection
    2.29  **
    2.30 -** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
    2.31 -** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
    2.32 -** successfully destroyed and all associated resources are deallocated.
    2.33 -**
    2.34 -** Applications must [sqlite3_finalize | finalize] all [prepared statements]
    2.35 -** and [sqlite3_blob_close | close] all [BLOB handles] associated with
    2.36 -** the [sqlite3] object prior to attempting to close the object.  ^If
    2.37 +** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
    2.38 +** for the [sqlite3] object.
    2.39 +** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
    2.40 +** the [sqlite3] object is successfully destroyed and all associated
    2.41 +** resources are deallocated.
    2.42 +**
    2.43 +** ^If the database connection is associated with unfinalized prepared
    2.44 +** statements or unfinished sqlite3_backup objects then sqlite3_close()
    2.45 +** will leave the database connection open and return [SQLITE_BUSY].
    2.46 +** ^If sqlite3_close_v2() is called with unfinalized prepared statements
    2.47 +** and unfinished sqlite3_backups, then the database connection becomes
    2.48 +** an unusable "zombie" which will automatically be deallocated when the
    2.49 +** last prepared statement is finalized or the last sqlite3_backup is
    2.50 +** finished.  The sqlite3_close_v2() interface is intended for use with
    2.51 +** host languages that are garbage collected, and where the order in which
    2.52 +** destructors are called is arbitrary.
    2.53 +**
    2.54 +** Applications should [sqlite3_finalize | finalize] all [prepared statements],
    2.55 +** [sqlite3_blob_close | close] all [BLOB handles], and 
    2.56 +** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
    2.57 +** with the [sqlite3] object prior to attempting to close the object.  ^If
    2.58  ** sqlite3_close() is called on a [database connection] that still has
    2.59 -** outstanding [prepared statements] or [BLOB handles], then it returns
    2.60 -** SQLITE_BUSY.
    2.61 -**
    2.62 -** ^If [sqlite3_close()] is invoked while a transaction is open,
    2.63 +** outstanding [prepared statements], [BLOB handles], and/or
    2.64 +** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
    2.65 +** of resources is deferred until all [prepared statements], [BLOB handles],
    2.66 +** and [sqlite3_backup] objects are also destroyed.
    2.67 +**
    2.68 +** ^If an [sqlite3] object is destroyed while a transaction is open,
    2.69  ** the transaction is automatically rolled back.
    2.70  **
    2.71 -** The C parameter to [sqlite3_close(C)] must be either a NULL
    2.72 +** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
    2.73 +** must be either a NULL
    2.74  ** pointer or an [sqlite3] object pointer obtained
    2.75  ** from [sqlite3_open()], [sqlite3_open16()], or
    2.76  ** [sqlite3_open_v2()], and not previously closed.
    2.77 -** ^Calling sqlite3_close() with a NULL pointer argument is a 
    2.78 -** harmless no-op.
    2.79 -*/
    2.80 -SQLITE_API int sqlite3_close(sqlite3 *);
    2.81 +** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
    2.82 +** argument is a harmless no-op.
    2.83 +*/
    2.84 +SQLITE_API int sqlite3_close(sqlite3*);
    2.85 +SQLITE_API int sqlite3_close_v2(sqlite3*);
    2.86  
    2.87  /*
    2.88  ** The type for a callback function.
    2.89 @@ -455,10 +474,12 @@
    2.90  #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
    2.91  #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
    2.92  #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
    2.93 +#define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
    2.94  #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
    2.95  #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
    2.96  #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
    2.97  #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
    2.98 +#define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
    2.99  #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
   2.100  #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
   2.101  #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
   2.102 @@ -498,7 +519,7 @@
   2.103  ** CAPI3REF: Device Characteristics
   2.104  **
   2.105  ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
   2.106 -** object returns an integer which is a vector of the these
   2.107 +** object returns an integer which is a vector of these
   2.108  ** bit values expressing I/O characteristics of the mass storage
   2.109  ** device that holds the file that the [sqlite3_io_methods]
   2.110  ** refers to.
   2.111 @@ -836,6 +857,26 @@
   2.112  ** compilation of the PRAGMA fails with an error.  ^The [SQLITE_FCNTL_PRAGMA]
   2.113  ** file control occurs at the beginning of pragma statement analysis and so
   2.114  ** it is able to override built-in [PRAGMA] statements.
   2.115 +**
   2.116 +** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
   2.117 +** ^This file-control may be invoked by SQLite on the database file handle
   2.118 +** shortly after it is opened in order to provide a custom VFS with access
   2.119 +** to the connections busy-handler callback. The argument is of type (void **)
   2.120 +** - an array of two (void *) values. The first (void *) actually points
   2.121 +** to a function of type (int (*)(void *)). In order to invoke the connections
   2.122 +** busy-handler, this function should be invoked with the second (void *) in
   2.123 +** the array as the only argument. If it returns non-zero, then the operation
   2.124 +** should be retried. If it returns zero, the custom VFS should abandon the
   2.125 +** current operation.
   2.126 +**
   2.127 +** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
   2.128 +** ^Application can invoke this file-control to have SQLite generate a
   2.129 +** temporary filename using the same algorithm that is followed to generate
   2.130 +** temporary filenames for TEMP tables and other internal uses.  The
   2.131 +** argument should be a char** which will be filled with the filename
   2.132 +** written into memory obtained from [sqlite3_malloc()].  The caller should
   2.133 +** invoke [sqlite3_free()] on the result to avoid a memory leak.
   2.134 +**
   2.135  ** </ul>
   2.136  */
   2.137  #define SQLITE_FCNTL_LOCKSTATE               1
   2.138 @@ -852,6 +893,8 @@
   2.139  #define SQLITE_FCNTL_VFSNAME                12
   2.140  #define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
   2.141  #define SQLITE_FCNTL_PRAGMA                 14
   2.142 +#define SQLITE_FCNTL_BUSYHANDLER            15
   2.143 +#define SQLITE_FCNTL_TEMPFILENAME           16
   2.144  
   2.145  /*
   2.146  ** CAPI3REF: Mutex Handle
   2.147 @@ -1548,11 +1591,39 @@
   2.148  ** disabled. The default value may be changed by compiling with the
   2.149  ** [SQLITE_USE_URI] symbol defined.
   2.150  **
   2.151 +** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
   2.152 +** <dd> This option takes a single integer argument which is interpreted as
   2.153 +** a boolean in order to enable or disable the use of covering indices for
   2.154 +** full table scans in the query optimizer.  The default setting is determined
   2.155 +** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
   2.156 +** if that compile-time option is omitted.
   2.157 +** The ability to disable the use of covering indices for full table scans
   2.158 +** is because some incorrectly coded legacy applications might malfunction
   2.159 +** malfunction when the optimization is enabled.  Providing the ability to
   2.160 +** disable the optimization allows the older, buggy application code to work
   2.161 +** without change even with newer versions of SQLite.
   2.162 +**
   2.163  ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
   2.164  ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
   2.165  ** <dd> These options are obsolete and should not be used by new code.
   2.166  ** They are retained for backwards compatibility but are now no-ops.
   2.167  ** </dl>
   2.168 +**
   2.169 +** [[SQLITE_CONFIG_SQLLOG]]
   2.170 +** <dt>SQLITE_CONFIG_SQLLOG
   2.171 +** <dd>This option is only available if sqlite is compiled with the
   2.172 +** SQLITE_ENABLE_SQLLOG pre-processor macro defined. The first argument should
   2.173 +** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
   2.174 +** The second should be of type (void*). The callback is invoked by the library
   2.175 +** in three separate circumstances, identified by the value passed as the
   2.176 +** fourth parameter. If the fourth parameter is 0, then the database connection
   2.177 +** passed as the second argument has just been opened. The third argument
   2.178 +** points to a buffer containing the name of the main database file. If the
   2.179 +** fourth parameter is 1, then the SQL statement that the third parameter
   2.180 +** points to has just been executed. Or, if the fourth parameter is 2, then
   2.181 +** the connection being passed as the second parameter is being closed. The
   2.182 +** third parameter is passed NULL In this case.
   2.183 +** </dl>
   2.184  */
   2.185  #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
   2.186  #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
   2.187 @@ -1573,6 +1644,8 @@
   2.188  #define SQLITE_CONFIG_URI          17  /* int */
   2.189  #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
   2.190  #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
   2.191 +#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
   2.192 +#define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
   2.193  
   2.194  /*
   2.195  ** CAPI3REF: Database Connection Configuration Options
   2.196 @@ -2581,7 +2654,7 @@
   2.197  **     an error)^. 
   2.198  **     ^If "ro" is specified, then the database is opened for read-only 
   2.199  **     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the 
   2.200 -**     third argument to sqlite3_prepare_v2(). ^If the mode option is set to 
   2.201 +**     third argument to sqlite3_open_v2(). ^If the mode option is set to 
   2.202  **     "rw", then the database is opened for read-write (but not create) 
   2.203  **     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had 
   2.204  **     been set. ^Value "rwc" is equivalent to setting both 
   2.205 @@ -2648,6 +2721,12 @@
   2.206  ** codepage is currently defined.  Filenames containing international
   2.207  ** characters must be converted to UTF-8 prior to passing them into
   2.208  ** sqlite3_open() or sqlite3_open_v2().
   2.209 +**
   2.210 +** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
   2.211 +** prior to calling sqlite3_open() or sqlite3_open_v2().  Otherwise, various
   2.212 +** features that require the use of temporary files may fail.
   2.213 +**
   2.214 +** See also: [sqlite3_temp_directory]
   2.215  */
   2.216  SQLITE_API int sqlite3_open(
   2.217    const char *filename,   /* Database filename (UTF-8) */
   2.218 @@ -2727,6 +2806,11 @@
   2.219  ** However, the error string might be overwritten or deallocated by
   2.220  ** subsequent calls to other SQLite interface functions.)^
   2.221  **
   2.222 +** ^The sqlite3_errstr() interface returns the English-language text
   2.223 +** that describes the [result code], as UTF-8.
   2.224 +** ^(Memory to hold the error message string is managed internally
   2.225 +** and must not be freed by the application)^.
   2.226 +**
   2.227  ** When the serialized [threading mode] is in use, it might be the
   2.228  ** case that a second error occurs on a separate thread in between
   2.229  ** the time of the first error and the call to these interfaces.
   2.230 @@ -2745,6 +2829,7 @@
   2.231  SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
   2.232  SQLITE_API const char *sqlite3_errmsg(sqlite3*);
   2.233  SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
   2.234 +SQLITE_API const char *sqlite3_errstr(int);
   2.235  
   2.236  /*
   2.237  ** CAPI3REF: SQL Statement Object
   2.238 @@ -3140,8 +3225,11 @@
   2.239  ** ^(In those routines that have a fourth argument, its value is the
   2.240  ** number of bytes in the parameter.  To be clear: the value is the
   2.241  ** number of <u>bytes</u> in the value, not the number of characters.)^
   2.242 -** ^If the fourth parameter is negative, the length of the string is
   2.243 +** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
   2.244 +** is negative, then the length of the string is
   2.245  ** the number of bytes up to the first zero terminator.
   2.246 +** If the fourth parameter to sqlite3_bind_blob() is negative, then
   2.247 +** the behavior is undefined.
   2.248  ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
   2.249  ** or sqlite3_bind_text16() then that parameter must be the byte offset
   2.250  ** where the NUL terminator would occur assuming the string were NUL
   2.251 @@ -4138,11 +4226,11 @@
   2.252  ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
   2.253  ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
   2.254  **
   2.255 -** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
   2.256 -** indicating that a string or BLOB is too long to represent.
   2.257 -**
   2.258 -** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
   2.259 -** indicating that a memory allocation failed.
   2.260 +** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
   2.261 +** error indicating that a string or BLOB is too long to represent.
   2.262 +**
   2.263 +** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
   2.264 +** error indicating that a memory allocation failed.
   2.265  **
   2.266  ** ^The sqlite3_result_int() interface sets the return value
   2.267  ** of the application-defined function to be the 32-bit signed integer
   2.268 @@ -4449,6 +4537,21 @@
   2.269  ** Hence, if this variable is modified directly, either it should be
   2.270  ** made NULL or made to point to memory obtained from [sqlite3_malloc]
   2.271  ** or else the use of the [temp_store_directory pragma] should be avoided.
   2.272 +**
   2.273 +** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
   2.274 +** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
   2.275 +** features that require the use of temporary files may fail.  Here is an
   2.276 +** example of how to do this using C++ with the Windows Runtime:
   2.277 +**
   2.278 +** <blockquote><pre>
   2.279 +** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
   2.280 +** &nbsp;     TemporaryFolder->Path->Data();
   2.281 +** char zPathBuf&#91;MAX_PATH + 1&#93;;
   2.282 +** memset(zPathBuf, 0, sizeof(zPathBuf));
   2.283 +** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
   2.284 +** &nbsp;     NULL, NULL);
   2.285 +** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
   2.286 +** </pre></blockquote>
   2.287  */
   2.288  SQLITE_API SQLITE_EXTERN char *sqlite3_temp_directory;
   2.289  
   2.290 @@ -4689,6 +4792,9 @@
   2.291  ** future releases of SQLite.  Applications that care about shared
   2.292  ** cache setting should set it explicitly.
   2.293  **
   2.294 +** This interface is threadsafe on processors where writing a
   2.295 +** 32-bit integer is atomic.
   2.296 +**
   2.297  ** See Also:  [SQLite Shared-Cache Mode]
   2.298  */
   2.299  SQLITE_API int sqlite3_enable_shared_cache(int);
   2.300 @@ -5494,7 +5600,6 @@
   2.301  ** implementations are available in the SQLite core:
   2.302  **
   2.303  ** <ul>
   2.304 -** <li>   SQLITE_MUTEX_OS2
   2.305  ** <li>   SQLITE_MUTEX_PTHREADS
   2.306  ** <li>   SQLITE_MUTEX_W32
   2.307  ** <li>   SQLITE_MUTEX_NOOP
   2.308 @@ -5502,9 +5607,9 @@
   2.309  **
   2.310  ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
   2.311  ** that does no real locking and is appropriate for use in
   2.312 -** a single-threaded application.  ^The SQLITE_MUTEX_OS2,
   2.313 -** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
   2.314 -** are appropriate for use on OS/2, Unix, and Windows.
   2.315 +** a single-threaded application.  ^The SQLITE_MUTEX_PTHREADS and
   2.316 +** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
   2.317 +** and Windows.
   2.318  **
   2.319  ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
   2.320  ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex