src/sqlite3.c
changeset 716 498156bf9993
parent 491 d32c60ada438
child 1912 b2f4e198adef
     1.1 --- a/src/sqlite3.c	Mon Jun 06 16:36:22 2016 +0200
     1.2 +++ b/src/sqlite3.c	Mon Jun 06 17:35:13 2016 +0200
     1.3 @@ -1,6 +1,6 @@
     1.4  /******************************************************************************
     1.5  ** This file is an amalgamation of many separate C source files from SQLite
     1.6 -** version 3.11.0.  By combining all the individual C code files into this 
     1.7 +** version 3.13.0.  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 @@ -40,6 +40,41 @@
    1.12  #ifndef _SQLITEINT_H_
    1.13  #define _SQLITEINT_H_
    1.14  
    1.15 +/* Special Comments:
    1.16 +**
    1.17 +** Some comments have special meaning to the tools that measure test
    1.18 +** coverage:
    1.19 +**
    1.20 +**    NO_TEST                     - The branches on this line are not
    1.21 +**                                  measured by branch coverage.  This is
    1.22 +**                                  used on lines of code that actually
    1.23 +**                                  implement parts of coverage testing.
    1.24 +**
    1.25 +**    OPTIMIZATION-IF-TRUE        - This branch is allowed to alway be false
    1.26 +**                                  and the correct answer is still obtained,
    1.27 +**                                  though perhaps more slowly.
    1.28 +**
    1.29 +**    OPTIMIZATION-IF-FALSE       - This branch is allowed to alway be true
    1.30 +**                                  and the correct answer is still obtained,
    1.31 +**                                  though perhaps more slowly.
    1.32 +**
    1.33 +**    PREVENTS-HARMLESS-OVERREAD  - This branch prevents a buffer overread
    1.34 +**                                  that would be harmless and undetectable
    1.35 +**                                  if it did occur.  
    1.36 +**
    1.37 +** In all cases, the special comment must be enclosed in the usual
    1.38 +** slash-asterisk...asterisk-slash comment marks, with no spaces between the 
    1.39 +** asterisks and the comment text.
    1.40 +*/
    1.41 +
    1.42 +/*
    1.43 +** Make sure that rand_s() is available on Windows systems with MSVC 2005
    1.44 +** or higher.
    1.45 +*/
    1.46 +#if defined(_MSC_VER) && _MSC_VER>=1400
    1.47 +#  define _CRT_RAND_S
    1.48 +#endif
    1.49 +
    1.50  /*
    1.51  ** Include the header file used to customize the compiler options for MSVC.
    1.52  ** This should be done first so that it can successfully prevent spurious
    1.53 @@ -328,9 +363,9 @@
    1.54  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
    1.55  ** [sqlite_version()] and [sqlite_source_id()].
    1.56  */
    1.57 -#define SQLITE_VERSION        "3.11.0"
    1.58 -#define SQLITE_VERSION_NUMBER 3011000
    1.59 -#define SQLITE_SOURCE_ID      "2016-02-15 17:29:24 3d862f207e3adc00f78066799ac5a8c282430a5f"
    1.60 +#define SQLITE_VERSION        "3.13.0"
    1.61 +#define SQLITE_VERSION_NUMBER 3013000
    1.62 +#define SQLITE_SOURCE_ID      "2016-05-18 10:57:30 fc49f556e48970561d7ab6a2f24fdd7d9eb81ff2"
    1.63  
    1.64  /*
    1.65  ** CAPI3REF: Run-Time Library Version Numbers
    1.66 @@ -1445,7 +1480,7 @@
    1.67    const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
    1.68    /*
    1.69    ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
    1.70 -  ** New fields may be appended in figure versions.  The iVersion
    1.71 +  ** New fields may be appended in future versions.  The iVersion
    1.72    ** value will increment whenever this happens. 
    1.73    */
    1.74  };
    1.75 @@ -2037,6 +2072,20 @@
    1.76  ** is enabled (using the [PRAGMA threads] command) and the amount of content
    1.77  ** to be sorted exceeds the page size times the minimum of the
    1.78  ** [PRAGMA cache_size] setting and this value.
    1.79 +**
    1.80 +** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
    1.81 +** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
    1.82 +** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
    1.83 +** becomes the [statement journal] spill-to-disk threshold.  
    1.84 +** [Statement journals] are held in memory until their size (in bytes)
    1.85 +** exceeds this threshold, at which point they are written to disk.
    1.86 +** Or if the threshold is -1, statement journals are always held
    1.87 +** exclusively in memory.
    1.88 +** Since many statement journals never become large, setting the spill
    1.89 +** threshold to a value such as 64KiB can greatly reduce the amount of
    1.90 +** I/O required to support statement rollback.
    1.91 +** The default value for this setting is controlled by the
    1.92 +** [SQLITE_STMTJRNL_SPILL] compile-time option.
    1.93  ** </dl>
    1.94  */
    1.95  #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
    1.96 @@ -2064,6 +2113,7 @@
    1.97  #define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
    1.98  #define SQLITE_CONFIG_PCACHE_HDRSZ        24  /* int *psz */
    1.99  #define SQLITE_CONFIG_PMASZ               25  /* unsigned int szPma */
   1.100 +#define SQLITE_CONFIG_STMTJRNL_SPILL      26  /* int nByte */
   1.101  
   1.102  /*
   1.103  ** CAPI3REF: Database Connection Configuration Options
   1.104 @@ -2121,11 +2171,43 @@
   1.105  ** following this call.  The second parameter may be a NULL pointer, in
   1.106  ** which case the trigger setting is not reported back. </dd>
   1.107  **
   1.108 +** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
   1.109 +** <dd> ^This option is used to enable or disable the two-argument
   1.110 +** version of the [fts3_tokenizer()] function which is part of the
   1.111 +** [FTS3] full-text search engine extension.
   1.112 +** There should be two additional arguments.
   1.113 +** The first argument is an integer which is 0 to disable fts3_tokenizer() or
   1.114 +** positive to enable fts3_tokenizer() or negative to leave the setting
   1.115 +** unchanged.
   1.116 +** The second parameter is a pointer to an integer into which
   1.117 +** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
   1.118 +** following this call.  The second parameter may be a NULL pointer, in
   1.119 +** which case the new setting is not reported back. </dd>
   1.120 +**
   1.121 +** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
   1.122 +** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
   1.123 +** interface independently of the [load_extension()] SQL function.
   1.124 +** The [sqlite3_enable_load_extension()] API enables or disables both the
   1.125 +** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
   1.126 +** There should be two additional arguments.
   1.127 +** When the first argument to this interface is 1, then only the C-API is
   1.128 +** enabled and the SQL function remains disabled.  If the first argment to
   1.129 +** this interface is 0, then both the C-API and the SQL function are disabled.
   1.130 +** If the first argument is -1, then no changes are made to state of either the
   1.131 +** C-API or the SQL function.
   1.132 +** The second parameter is a pointer to an integer into which
   1.133 +** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
   1.134 +** is disabled or enabled following this call.  The second parameter may
   1.135 +** be a NULL pointer, in which case the new setting is not reported back.
   1.136 +** </dd>
   1.137 +**
   1.138  ** </dl>
   1.139  */
   1.140 -#define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
   1.141 -#define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
   1.142 -#define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
   1.143 +#define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
   1.144 +#define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
   1.145 +#define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
   1.146 +#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
   1.147 +#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
   1.148  
   1.149  
   1.150  /*
   1.151 @@ -5375,7 +5457,7 @@
   1.152  ** ^The sqlite3_update_hook() interface registers a callback function
   1.153  ** with the [database connection] identified by the first argument
   1.154  ** to be invoked whenever a row is updated, inserted or deleted in
   1.155 -** a rowid table.
   1.156 +** a [rowid table].
   1.157  ** ^Any callback set by a previous call to this function
   1.158  ** for the same database connection is overridden.
   1.159  **
   1.160 @@ -5414,8 +5496,8 @@
   1.161  ** on the same [database connection] D, or NULL for
   1.162  ** the first call on D.
   1.163  **
   1.164 -** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
   1.165 -** interfaces.
   1.166 +** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
   1.167 +** and [sqlite3_preupdate_hook()] interfaces.
   1.168  */
   1.169  SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
   1.170    sqlite3*, 
   1.171 @@ -5662,9 +5744,18 @@
   1.172  ** should free this memory by calling [sqlite3_free()].
   1.173  **
   1.174  ** ^Extension loading must be enabled using
   1.175 -** [sqlite3_enable_load_extension()] prior to calling this API,
   1.176 +** [sqlite3_enable_load_extension()] or
   1.177 +** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
   1.178 +** prior to calling this API,
   1.179  ** otherwise an error will be returned.
   1.180  **
   1.181 +** <b>Security warning:</b> It is recommended that the 
   1.182 +** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
   1.183 +** interface.  The use of the [sqlite3_enable_load_extension()] interface
   1.184 +** should be avoided.  This will keep the SQL function [load_extension()]
   1.185 +** disabled and prevent SQL injections from giving attackers
   1.186 +** access to extension loading capabilities.
   1.187 +**
   1.188  ** See also the [load_extension() SQL function].
   1.189  */
   1.190  SQLITE_API int SQLITE_STDCALL sqlite3_load_extension(
   1.191 @@ -5687,6 +5778,17 @@
   1.192  ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
   1.193  ** to turn extension loading on and call it with onoff==0 to turn
   1.194  ** it back off again.
   1.195 +**
   1.196 +** ^This interface enables or disables both the C-API
   1.197 +** [sqlite3_load_extension()] and the SQL function [load_extension()].
   1.198 +** Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
   1.199 +** to enable or disable only the C-API.
   1.200 +**
   1.201 +** <b>Security warning:</b> It is recommended that extension loading
   1.202 +** be disabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
   1.203 +** rather than this interface, so the [load_extension()] SQL function
   1.204 +** remains disabled. This will prevent SQL injections from giving attackers
   1.205 +** access to extension loading capabilities.
   1.206  */
   1.207  SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int onoff);
   1.208  
   1.209 @@ -7325,7 +7427,7 @@
   1.210  ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
   1.211  ** an error.
   1.212  **
   1.213 -** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if 
   1.214 +** ^A call to sqlite3_backup_init() will fail, returning NULL, if 
   1.215  ** there is already a read or read-write transaction open on the 
   1.216  ** destination database.
   1.217  **
   1.218 @@ -7706,7 +7808,7 @@
   1.219  ** previously registered write-ahead log callback. ^Note that the
   1.220  ** [sqlite3_wal_autocheckpoint()] interface and the
   1.221  ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
   1.222 -** those overwrite any prior [sqlite3_wal_hook()] settings.
   1.223 +** overwrite any prior [sqlite3_wal_hook()] settings.
   1.224  */
   1.225  SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook(
   1.226    sqlite3*, 
   1.227 @@ -8104,6 +8206,114 @@
   1.228  SQLITE_API int SQLITE_STDCALL sqlite3_db_cacheflush(sqlite3*);
   1.229  
   1.230  /*
   1.231 +** CAPI3REF: The pre-update hook.
   1.232 +**
   1.233 +** ^These interfaces are only available if SQLite is compiled using the
   1.234 +** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
   1.235 +**
   1.236 +** ^The [sqlite3_preupdate_hook()] interface registers a callback function
   1.237 +** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
   1.238 +** on a [rowid table].
   1.239 +** ^At most one preupdate hook may be registered at a time on a single
   1.240 +** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
   1.241 +** the previous setting.
   1.242 +** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
   1.243 +** with a NULL pointer as the second parameter.
   1.244 +** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
   1.245 +** the first parameter to callbacks.
   1.246 +**
   1.247 +** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate
   1.248 +** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID]
   1.249 +** tables.
   1.250 +**
   1.251 +** ^The second parameter to the preupdate callback is a pointer to
   1.252 +** the [database connection] that registered the preupdate hook.
   1.253 +** ^The third parameter to the preupdate callback is one of the constants
   1.254 +** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to indentify the
   1.255 +** kind of update operation that is about to occur.
   1.256 +** ^(The fourth parameter to the preupdate callback is the name of the
   1.257 +** database within the database connection that is being modified.  This
   1.258 +** will be "main" for the main database or "temp" for TEMP tables or 
   1.259 +** the name given after the AS keyword in the [ATTACH] statement for attached
   1.260 +** databases.)^
   1.261 +** ^The fifth parameter to the preupdate callback is the name of the
   1.262 +** table that is being modified.
   1.263 +** ^The sixth parameter to the preupdate callback is the initial [rowid] of the
   1.264 +** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is
   1.265 +** undefined for SQLITE_INSERT changes.
   1.266 +** ^The seventh parameter to the preupdate callback is the final [rowid] of
   1.267 +** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is
   1.268 +** undefined for SQLITE_DELETE changes.
   1.269 +**
   1.270 +** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
   1.271 +** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
   1.272 +** provide additional information about a preupdate event. These routines
   1.273 +** may only be called from within a preupdate callback.  Invoking any of
   1.274 +** these routines from outside of a preupdate callback or with a
   1.275 +** [database connection] pointer that is different from the one supplied
   1.276 +** to the preupdate callback results in undefined and probably undesirable
   1.277 +** behavior.
   1.278 +**
   1.279 +** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
   1.280 +** in the row that is being inserted, updated, or deleted.
   1.281 +**
   1.282 +** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
   1.283 +** a [protected sqlite3_value] that contains the value of the Nth column of
   1.284 +** the table row before it is updated.  The N parameter must be between 0
   1.285 +** and one less than the number of columns or the behavior will be
   1.286 +** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
   1.287 +** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
   1.288 +** behavior is undefined.  The [sqlite3_value] that P points to
   1.289 +** will be destroyed when the preupdate callback returns.
   1.290 +**
   1.291 +** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
   1.292 +** a [protected sqlite3_value] that contains the value of the Nth column of
   1.293 +** the table row after it is updated.  The N parameter must be between 0
   1.294 +** and one less than the number of columns or the behavior will be
   1.295 +** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
   1.296 +** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
   1.297 +** behavior is undefined.  The [sqlite3_value] that P points to
   1.298 +** will be destroyed when the preupdate callback returns.
   1.299 +**
   1.300 +** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
   1.301 +** callback was invoked as a result of a direct insert, update, or delete
   1.302 +** operation; or 1 for inserts, updates, or deletes invoked by top-level 
   1.303 +** triggers; or 2 for changes resulting from triggers called by top-level
   1.304 +** triggers; and so forth.
   1.305 +**
   1.306 +** See also:  [sqlite3_update_hook()]
   1.307 +*/
   1.308 +SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_preupdate_hook(
   1.309 +  sqlite3 *db,
   1.310 +  void(*xPreUpdate)(
   1.311 +    void *pCtx,                   /* Copy of third arg to preupdate_hook() */
   1.312 +    sqlite3 *db,                  /* Database handle */
   1.313 +    int op,                       /* SQLITE_UPDATE, DELETE or INSERT */
   1.314 +    char const *zDb,              /* Database name */
   1.315 +    char const *zName,            /* Table name */
   1.316 +    sqlite3_int64 iKey1,          /* Rowid of row about to be deleted/updated */
   1.317 +    sqlite3_int64 iKey2           /* New rowid value (for a rowid UPDATE) */
   1.318 +  ),
   1.319 +  void*
   1.320 +);
   1.321 +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
   1.322 +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_count(sqlite3 *);
   1.323 +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_depth(sqlite3 *);
   1.324 +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
   1.325 +
   1.326 +/*
   1.327 +** CAPI3REF: Low-level system error code
   1.328 +**
   1.329 +** ^Attempt to return the underlying operating system error code or error
   1.330 +** number that caused the most recent I/O error or failure to open a file.
   1.331 +** The return value is OS-dependent.  For example, on unix systems, after
   1.332 +** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
   1.333 +** called to get back the underlying "errno" that caused the problem, such
   1.334 +** as ENOSPC, EAUTH, EISDIR, and so forth.  
   1.335 +*/
   1.336 +SQLITE_API int SQLITE_STDCALL sqlite3_system_errno(sqlite3*);
   1.337 +
   1.338 +/*
   1.339  ** CAPI3REF: Database Snapshot
   1.340  ** KEYWORDS: {snapshot}
   1.341  ** EXPERIMENTAL
   1.342 @@ -8161,17 +8371,30 @@
   1.343  ** CAPI3REF: Start a read transaction on an historical snapshot
   1.344  ** EXPERIMENTAL
   1.345  **
   1.346 -** ^The [sqlite3_snapshot_open(D,S,P)] interface attempts to move the
   1.347 -** read transaction that is currently open on schema S of
   1.348 -** [database connection] D so that it refers to historical [snapshot] P.
   1.349 +** ^The [sqlite3_snapshot_open(D,S,P)] interface starts a
   1.350 +** read transaction for schema S of
   1.351 +** [database connection] D such that the read transaction
   1.352 +** refers to historical [snapshot] P, rather than the most
   1.353 +** recent change to the database.
   1.354  ** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success
   1.355  ** or an appropriate [error code] if it fails.
   1.356  **
   1.357  ** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be
   1.358 -** the first operation, apart from other sqlite3_snapshot_open() calls,
   1.359 -** following the [BEGIN] that starts a new read transaction.
   1.360 -** ^A [snapshot] will fail to open if it has been overwritten by a 
   1.361 -** [checkpoint].  
   1.362 +** the first operation following the [BEGIN] that takes the schema S
   1.363 +** out of [autocommit mode].
   1.364 +** ^In other words, schema S must not currently be in
   1.365 +** a transaction for [sqlite3_snapshot_open(D,S,P)] to work, but the
   1.366 +** database connection D must be out of [autocommit mode].
   1.367 +** ^A [snapshot] will fail to open if it has been overwritten by a
   1.368 +** [checkpoint].
   1.369 +** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
   1.370 +** database connection D does not know that the database file for
   1.371 +** schema S is in [WAL mode].  A database connection might not know
   1.372 +** that the database file is in [WAL mode] if there has been no prior
   1.373 +** I/O on that database connection, or if the database entered [WAL mode] 
   1.374 +** after the most recent I/O on the database connection.)^
   1.375 +** (Hint: Run "[PRAGMA application_id]" against a newly opened
   1.376 +** database connection in order to make it ready to use snapshots.)
   1.377  **
   1.378  ** The [sqlite3_snapshot_open()] interface is only available when the
   1.379  ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
   1.380 @@ -8196,6 +8419,33 @@
   1.381  SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_snapshot_free(sqlite3_snapshot*);
   1.382  
   1.383  /*
   1.384 +** CAPI3REF: Compare the ages of two snapshot handles.
   1.385 +** EXPERIMENTAL
   1.386 +**
   1.387 +** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
   1.388 +** of two valid snapshot handles. 
   1.389 +**
   1.390 +** If the two snapshot handles are not associated with the same database 
   1.391 +** file, the result of the comparison is undefined. 
   1.392 +**
   1.393 +** Additionally, the result of the comparison is only valid if both of the
   1.394 +** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
   1.395 +** last time the wal file was deleted. The wal file is deleted when the
   1.396 +** database is changed back to rollback mode or when the number of database
   1.397 +** clients drops to zero. If either snapshot handle was obtained before the 
   1.398 +** wal file was last deleted, the value returned by this function 
   1.399 +** is undefined.
   1.400 +**
   1.401 +** Otherwise, this API returns a negative value if P1 refers to an older
   1.402 +** snapshot than P2, zero if the two handles refer to the same database
   1.403 +** snapshot, and a positive value if P1 is a newer snapshot than P2.
   1.404 +*/
   1.405 +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_cmp(
   1.406 +  sqlite3_snapshot *p1,
   1.407 +  sqlite3_snapshot *p2
   1.408 +);
   1.409 +
   1.410 +/*
   1.411  ** Undo the hack that converts floating point types to integer for
   1.412  ** builds on processors without floating point support.
   1.413  */
   1.414 @@ -8208,6 +8458,7 @@
   1.415  #endif
   1.416  #endif /* _SQLITE3_H_ */
   1.417  
   1.418 +/******** Begin file sqlite3rtree.h *********/
   1.419  /*
   1.420  ** 2010 August 30
   1.421  **
   1.422 @@ -8325,6 +8576,1287 @@
   1.423  
   1.424  #endif  /* ifndef _SQLITE3RTREE_H_ */
   1.425  
   1.426 +/******** End of sqlite3rtree.h *********/
   1.427 +/******** Begin file sqlite3session.h *********/
   1.428 +
   1.429 +#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
   1.430 +#define __SQLITESESSION_H_ 1
   1.431 +
   1.432 +/*
   1.433 +** Make sure we can call this stuff from C++.
   1.434 +*/
   1.435 +#if 0
   1.436 +extern "C" {
   1.437 +#endif
   1.438 +
   1.439 +
   1.440 +/*
   1.441 +** CAPI3REF: Session Object Handle
   1.442 +*/
   1.443 +typedef struct sqlite3_session sqlite3_session;
   1.444 +
   1.445 +/*
   1.446 +** CAPI3REF: Changeset Iterator Handle
   1.447 +*/
   1.448 +typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
   1.449 +
   1.450 +/*
   1.451 +** CAPI3REF: Create A New Session Object
   1.452 +**
   1.453 +** Create a new session object attached to database handle db. If successful,
   1.454 +** a pointer to the new object is written to *ppSession and SQLITE_OK is
   1.455 +** returned. If an error occurs, *ppSession is set to NULL and an SQLite
   1.456 +** error code (e.g. SQLITE_NOMEM) is returned.
   1.457 +**
   1.458 +** It is possible to create multiple session objects attached to a single
   1.459 +** database handle.
   1.460 +**
   1.461 +** Session objects created using this function should be deleted using the
   1.462 +** [sqlite3session_delete()] function before the database handle that they
   1.463 +** are attached to is itself closed. If the database handle is closed before
   1.464 +** the session object is deleted, then the results of calling any session
   1.465 +** module function, including [sqlite3session_delete()] on the session object
   1.466 +** are undefined.
   1.467 +**
   1.468 +** Because the session module uses the [sqlite3_preupdate_hook()] API, it
   1.469 +** is not possible for an application to register a pre-update hook on a
   1.470 +** database handle that has one or more session objects attached. Nor is
   1.471 +** it possible to create a session object attached to a database handle for
   1.472 +** which a pre-update hook is already defined. The results of attempting 
   1.473 +** either of these things are undefined.
   1.474 +**
   1.475 +** The session object will be used to create changesets for tables in
   1.476 +** database zDb, where zDb is either "main", or "temp", or the name of an
   1.477 +** attached database. It is not an error if database zDb is not attached
   1.478 +** to the database when the session object is created.
   1.479 +*/
   1.480 +int sqlite3session_create(
   1.481 +  sqlite3 *db,                    /* Database handle */
   1.482 +  const char *zDb,                /* Name of db (e.g. "main") */
   1.483 +  sqlite3_session **ppSession     /* OUT: New session object */
   1.484 +);
   1.485 +
   1.486 +/*
   1.487 +** CAPI3REF: Delete A Session Object
   1.488 +**
   1.489 +** Delete a session object previously allocated using 
   1.490 +** [sqlite3session_create()]. Once a session object has been deleted, the
   1.491 +** results of attempting to use pSession with any other session module
   1.492 +** function are undefined.
   1.493 +**
   1.494 +** Session objects must be deleted before the database handle to which they
   1.495 +** are attached is closed. Refer to the documentation for 
   1.496 +** [sqlite3session_create()] for details.
   1.497 +*/
   1.498 +void sqlite3session_delete(sqlite3_session *pSession);
   1.499 +
   1.500 +
   1.501 +/*
   1.502 +** CAPI3REF: Enable Or Disable A Session Object
   1.503 +**
   1.504 +** Enable or disable the recording of changes by a session object. When
   1.505 +** enabled, a session object records changes made to the database. When
   1.506 +** disabled - it does not. A newly created session object is enabled.
   1.507 +** Refer to the documentation for [sqlite3session_changeset()] for further
   1.508 +** details regarding how enabling and disabling a session object affects
   1.509 +** the eventual changesets.
   1.510 +**
   1.511 +** Passing zero to this function disables the session. Passing a value
   1.512 +** greater than zero enables it. Passing a value less than zero is a 
   1.513 +** no-op, and may be used to query the current state of the session.
   1.514 +**
   1.515 +** The return value indicates the final state of the session object: 0 if 
   1.516 +** the session is disabled, or 1 if it is enabled.
   1.517 +*/
   1.518 +int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
   1.519 +
   1.520 +/*
   1.521 +** CAPI3REF: Set Or Clear the Indirect Change Flag
   1.522 +**
   1.523 +** Each change recorded by a session object is marked as either direct or
   1.524 +** indirect. A change is marked as indirect if either:
   1.525 +**
   1.526 +** <ul>
   1.527 +**   <li> The session object "indirect" flag is set when the change is
   1.528 +**        made, or
   1.529 +**   <li> The change is made by an SQL trigger or foreign key action 
   1.530 +**        instead of directly as a result of a users SQL statement.
   1.531 +** </ul>
   1.532 +**
   1.533 +** If a single row is affected by more than one operation within a session,
   1.534 +** then the change is considered indirect if all operations meet the criteria
   1.535 +** for an indirect change above, or direct otherwise.
   1.536 +**
   1.537 +** This function is used to set, clear or query the session object indirect
   1.538 +** flag.  If the second argument passed to this function is zero, then the
   1.539 +** indirect flag is cleared. If it is greater than zero, the indirect flag
   1.540 +** is set. Passing a value less than zero does not modify the current value
   1.541 +** of the indirect flag, and may be used to query the current state of the 
   1.542 +** indirect flag for the specified session object.
   1.543 +**
   1.544 +** The return value indicates the final state of the indirect flag: 0 if 
   1.545 +** it is clear, or 1 if it is set.
   1.546 +*/
   1.547 +int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
   1.548 +
   1.549 +/*
   1.550 +** CAPI3REF: Attach A Table To A Session Object
   1.551 +**
   1.552 +** If argument zTab is not NULL, then it is the name of a table to attach
   1.553 +** to the session object passed as the first argument. All subsequent changes 
   1.554 +** made to the table while the session object is enabled will be recorded. See 
   1.555 +** documentation for [sqlite3session_changeset()] for further details.
   1.556 +**
   1.557 +** Or, if argument zTab is NULL, then changes are recorded for all tables
   1.558 +** in the database. If additional tables are added to the database (by 
   1.559 +** executing "CREATE TABLE" statements) after this call is made, changes for 
   1.560 +** the new tables are also recorded.
   1.561 +**
   1.562 +** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
   1.563 +** defined as part of their CREATE TABLE statement. It does not matter if the 
   1.564 +** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
   1.565 +** KEY may consist of a single column, or may be a composite key.
   1.566 +** 
   1.567 +** It is not an error if the named table does not exist in the database. Nor
   1.568 +** is it an error if the named table does not have a PRIMARY KEY. However,
   1.569 +** no changes will be recorded in either of these scenarios.
   1.570 +**
   1.571 +** Changes are not recorded for individual rows that have NULL values stored
   1.572 +** in one or more of their PRIMARY KEY columns.
   1.573 +**
   1.574 +** SQLITE_OK is returned if the call completes without error. Or, if an error 
   1.575 +** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
   1.576 +*/
   1.577 +int sqlite3session_attach(
   1.578 +  sqlite3_session *pSession,      /* Session object */
   1.579 +  const char *zTab                /* Table name */
   1.580 +);
   1.581 +
   1.582 +/*
   1.583 +** CAPI3REF: Set a table filter on a Session Object.
   1.584 +**
   1.585 +** The second argument (xFilter) is the "filter callback". For changes to rows 
   1.586 +** in tables that are not attached to the Session oject, the filter is called
   1.587 +** to determine whether changes to the table's rows should be tracked or not. 
   1.588 +** If xFilter returns 0, changes is not tracked. Note that once a table is 
   1.589 +** attached, xFilter will not be called again.
   1.590 +*/
   1.591 +void sqlite3session_table_filter(
   1.592 +  sqlite3_session *pSession,      /* Session object */
   1.593 +  int(*xFilter)(
   1.594 +    void *pCtx,                   /* Copy of third arg to _filter_table() */
   1.595 +    const char *zTab              /* Table name */
   1.596 +  ),
   1.597 +  void *pCtx                      /* First argument passed to xFilter */
   1.598 +);
   1.599 +
   1.600 +/*
   1.601 +** CAPI3REF: Generate A Changeset From A Session Object
   1.602 +**
   1.603 +** Obtain a changeset containing changes to the tables attached to the 
   1.604 +** session object passed as the first argument. If successful, 
   1.605 +** set *ppChangeset to point to a buffer containing the changeset 
   1.606 +** and *pnChangeset to the size of the changeset in bytes before returning
   1.607 +** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
   1.608 +** zero and return an SQLite error code.
   1.609 +**
   1.610 +** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
   1.611 +** each representing a change to a single row of an attached table. An INSERT
   1.612 +** change contains the values of each field of a new database row. A DELETE
   1.613 +** contains the original values of each field of a deleted database row. An
   1.614 +** UPDATE change contains the original values of each field of an updated
   1.615 +** database row along with the updated values for each updated non-primary-key
   1.616 +** column. It is not possible for an UPDATE change to represent a change that
   1.617 +** modifies the values of primary key columns. If such a change is made, it
   1.618 +** is represented in a changeset as a DELETE followed by an INSERT.
   1.619 +**
   1.620 +** Changes are not recorded for rows that have NULL values stored in one or 
   1.621 +** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
   1.622 +** no corresponding change is present in the changesets returned by this
   1.623 +** function. If an existing row with one or more NULL values stored in
   1.624 +** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
   1.625 +** only an INSERT is appears in the changeset. Similarly, if an existing row
   1.626 +** with non-NULL PRIMARY KEY values is updated so that one or more of its
   1.627 +** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
   1.628 +** DELETE change only.
   1.629 +**
   1.630 +** The contents of a changeset may be traversed using an iterator created
   1.631 +** using the [sqlite3changeset_start()] API. A changeset may be applied to
   1.632 +** a database with a compatible schema using the [sqlite3changeset_apply()]
   1.633 +** API.
   1.634 +**
   1.635 +** Within a changeset generated by this function, all changes related to a
   1.636 +** single table are grouped together. In other words, when iterating through
   1.637 +** a changeset or when applying a changeset to a database, all changes related
   1.638 +** to a single table are processed before moving on to the next table. Tables
   1.639 +** are sorted in the same order in which they were attached (or auto-attached)
   1.640 +** to the sqlite3_session object. The order in which the changes related to
   1.641 +** a single table are stored is undefined.
   1.642 +**
   1.643 +** Following a successful call to this function, it is the responsibility of
   1.644 +** the caller to eventually free the buffer that *ppChangeset points to using
   1.645 +** [sqlite3_free()].
   1.646 +**
   1.647 +** <h3>Changeset Generation</h3>
   1.648 +**
   1.649 +** Once a table has been attached to a session object, the session object
   1.650 +** records the primary key values of all new rows inserted into the table.
   1.651 +** It also records the original primary key and other column values of any
   1.652 +** deleted or updated rows. For each unique primary key value, data is only
   1.653 +** recorded once - the first time a row with said primary key is inserted,
   1.654 +** updated or deleted in the lifetime of the session.
   1.655 +**
   1.656 +** There is one exception to the previous paragraph: when a row is inserted,
   1.657 +** updated or deleted, if one or more of its primary key columns contain a
   1.658 +** NULL value, no record of the change is made.
   1.659 +**
   1.660 +** The session object therefore accumulates two types of records - those
   1.661 +** that consist of primary key values only (created when the user inserts
   1.662 +** a new record) and those that consist of the primary key values and the
   1.663 +** original values of other table columns (created when the users deletes
   1.664 +** or updates a record).
   1.665 +**
   1.666 +** When this function is called, the requested changeset is created using
   1.667 +** both the accumulated records and the current contents of the database
   1.668 +** file. Specifically:
   1.669 +**
   1.670 +** <ul>
   1.671 +**   <li> For each record generated by an insert, the database is queried
   1.672 +**        for a row with a matching primary key. If one is found, an INSERT
   1.673 +**        change is added to the changeset. If no such row is found, no change 
   1.674 +**        is added to the changeset.
   1.675 +**
   1.676 +**   <li> For each record generated by an update or delete, the database is 
   1.677 +**        queried for a row with a matching primary key. If such a row is
   1.678 +**        found and one or more of the non-primary key fields have been
   1.679 +**        modified from their original values, an UPDATE change is added to 
   1.680 +**        the changeset. Or, if no such row is found in the table, a DELETE 
   1.681 +**        change is added to the changeset. If there is a row with a matching
   1.682 +**        primary key in the database, but all fields contain their original
   1.683 +**        values, no change is added to the changeset.
   1.684 +** </ul>
   1.685 +**
   1.686 +** This means, amongst other things, that if a row is inserted and then later
   1.687 +** deleted while a session object is active, neither the insert nor the delete
   1.688 +** will be present in the changeset. Or if a row is deleted and then later a 
   1.689 +** row with the same primary key values inserted while a session object is
   1.690 +** active, the resulting changeset will contain an UPDATE change instead of
   1.691 +** a DELETE and an INSERT.
   1.692 +**
   1.693 +** When a session object is disabled (see the [sqlite3session_enable()] API),
   1.694 +** it does not accumulate records when rows are inserted, updated or deleted.
   1.695 +** This may appear to have some counter-intuitive effects if a single row
   1.696 +** is written to more than once during a session. For example, if a row
   1.697 +** is inserted while a session object is enabled, then later deleted while 
   1.698 +** the same session object is disabled, no INSERT record will appear in the
   1.699 +** changeset, even though the delete took place while the session was disabled.
   1.700 +** Or, if one field of a row is updated while a session is disabled, and 
   1.701 +** another field of the same row is updated while the session is enabled, the
   1.702 +** resulting changeset will contain an UPDATE change that updates both fields.
   1.703 +*/
   1.704 +int sqlite3session_changeset(
   1.705 +  sqlite3_session *pSession,      /* Session object */
   1.706 +  int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
   1.707 +  void **ppChangeset              /* OUT: Buffer containing changeset */
   1.708 +);
   1.709 +
   1.710 +/*
   1.711 +** CAPI3REF: Load The Difference Between Tables Into A Session 
   1.712 +**
   1.713 +** If it is not already attached to the session object passed as the first
   1.714 +** argument, this function attaches table zTbl in the same manner as the
   1.715 +** [sqlite3session_attach()] function. If zTbl does not exist, or if it
   1.716 +** does not have a primary key, this function is a no-op (but does not return
   1.717 +** an error).
   1.718 +**
   1.719 +** Argument zFromDb must be the name of a database ("main", "temp" etc.)
   1.720 +** attached to the same database handle as the session object that contains 
   1.721 +** a table compatible with the table attached to the session by this function.
   1.722 +** A table is considered compatible if it:
   1.723 +**
   1.724 +** <ul>
   1.725 +**   <li> Has the same name,
   1.726 +**   <li> Has the same set of columns declared in the same order, and
   1.727 +**   <li> Has the same PRIMARY KEY definition.
   1.728 +** </ul>
   1.729 +**
   1.730 +** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
   1.731 +** are compatible but do not have any PRIMARY KEY columns, it is not an error
   1.732 +** but no changes are added to the session object. As with other session
   1.733 +** APIs, tables without PRIMARY KEYs are simply ignored.
   1.734 +**
   1.735 +** This function adds a set of changes to the session object that could be
   1.736 +** used to update the table in database zFrom (call this the "from-table") 
   1.737 +** so that its content is the same as the table attached to the session 
   1.738 +** object (call this the "to-table"). Specifically:
   1.739 +**
   1.740 +** <ul>
   1.741 +**   <li> For each row (primary key) that exists in the to-table but not in 
   1.742 +**     the from-table, an INSERT record is added to the session object.
   1.743 +**
   1.744 +**   <li> For each row (primary key) that exists in the to-table but not in 
   1.745 +**     the from-table, a DELETE record is added to the session object.
   1.746 +**
   1.747 +**   <li> For each row (primary key) that exists in both tables, but features 
   1.748 +**     different in each, an UPDATE record is added to the session.
   1.749 +** </ul>
   1.750 +**
   1.751 +** To clarify, if this function is called and then a changeset constructed
   1.752 +** using [sqlite3session_changeset()], then after applying that changeset to 
   1.753 +** database zFrom the contents of the two compatible tables would be 
   1.754 +** identical.
   1.755 +**
   1.756 +** It an error if database zFrom does not exist or does not contain the
   1.757 +** required compatible table.
   1.758 +**
   1.759 +** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
   1.760 +** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
   1.761 +** may be set to point to a buffer containing an English language error 
   1.762 +** message. It is the responsibility of the caller to free this buffer using
   1.763 +** sqlite3_free().
   1.764 +*/
   1.765 +int sqlite3session_diff(
   1.766 +  sqlite3_session *pSession,
   1.767 +  const char *zFromDb,
   1.768 +  const char *zTbl,
   1.769 +  char **pzErrMsg
   1.770 +);
   1.771 +
   1.772 +
   1.773 +/*
   1.774 +** CAPI3REF: Generate A Patchset From A Session Object
   1.775 +**
   1.776 +** The differences between a patchset and a changeset are that:
   1.777 +**
   1.778 +** <ul>
   1.779 +**   <li> DELETE records consist of the primary key fields only. The 
   1.780 +**        original values of other fields are omitted.
   1.781 +**   <li> The original values of any modified fields are omitted from 
   1.782 +**        UPDATE records.
   1.783 +** </ul>
   1.784 +**
   1.785 +** A patchset blob may be used with up to date versions of all 
   1.786 +** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(), 
   1.787 +** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
   1.788 +** attempting to use a patchset blob with old versions of the
   1.789 +** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error. 
   1.790 +**
   1.791 +** Because the non-primary key "old.*" fields are omitted, no 
   1.792 +** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
   1.793 +** is passed to the sqlite3changeset_apply() API. Other conflict types work
   1.794 +** in the same way as for changesets.
   1.795 +**
   1.796 +** Changes within a patchset are ordered in the same way as for changesets
   1.797 +** generated by the sqlite3session_changeset() function (i.e. all changes for
   1.798 +** a single table are grouped together, tables appear in the order in which
   1.799 +** they were attached to the session object).
   1.800 +*/
   1.801 +int sqlite3session_patchset(
   1.802 +  sqlite3_session *pSession,      /* Session object */
   1.803 +  int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
   1.804 +  void **ppPatchset               /* OUT: Buffer containing changeset */
   1.805 +);
   1.806 +
   1.807 +/*
   1.808 +** CAPI3REF: Test if a changeset has recorded any changes.
   1.809 +**
   1.810 +** Return non-zero if no changes to attached tables have been recorded by 
   1.811 +** the session object passed as the first argument. Otherwise, if one or 
   1.812 +** more changes have been recorded, return zero.
   1.813 +**
   1.814 +** Even if this function returns zero, it is possible that calling
   1.815 +** [sqlite3session_changeset()] on the session handle may still return a
   1.816 +** changeset that contains no changes. This can happen when a row in 
   1.817 +** an attached table is modified and then later on the original values 
   1.818 +** are restored. However, if this function returns non-zero, then it is
   1.819 +** guaranteed that a call to sqlite3session_changeset() will return a 
   1.820 +** changeset containing zero changes.
   1.821 +*/
   1.822 +int sqlite3session_isempty(sqlite3_session *pSession);
   1.823 +
   1.824 +/*
   1.825 +** CAPI3REF: Create An Iterator To Traverse A Changeset 
   1.826 +**
   1.827 +** Create an iterator used to iterate through the contents of a changeset.
   1.828 +** If successful, *pp is set to point to the iterator handle and SQLITE_OK
   1.829 +** is returned. Otherwise, if an error occurs, *pp is set to zero and an
   1.830 +** SQLite error code is returned.
   1.831 +**
   1.832 +** The following functions can be used to advance and query a changeset 
   1.833 +** iterator created by this function:
   1.834 +**
   1.835 +** <ul>
   1.836 +**   <li> [sqlite3changeset_next()]
   1.837 +**   <li> [sqlite3changeset_op()]
   1.838 +**   <li> [sqlite3changeset_new()]
   1.839 +**   <li> [sqlite3changeset_old()]
   1.840 +** </ul>
   1.841 +**
   1.842 +** It is the responsibility of the caller to eventually destroy the iterator
   1.843 +** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
   1.844 +** changeset (pChangeset) must remain valid until after the iterator is
   1.845 +** destroyed.
   1.846 +**
   1.847 +** Assuming the changeset blob was created by one of the
   1.848 +** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
   1.849 +** [sqlite3changeset_invert()] functions, all changes within the changeset 
   1.850 +** that apply to a single table are grouped together. This means that when 
   1.851 +** an application iterates through a changeset using an iterator created by 
   1.852 +** this function, all changes that relate to a single table are visted 
   1.853 +** consecutively. There is no chance that the iterator will visit a change 
   1.854 +** the applies to table X, then one for table Y, and then later on visit 
   1.855 +** another change for table X.
   1.856 +*/
   1.857 +int sqlite3changeset_start(
   1.858 +  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
   1.859 +  int nChangeset,                 /* Size of changeset blob in bytes */
   1.860 +  void *pChangeset                /* Pointer to blob containing changeset */
   1.861 +);
   1.862 +
   1.863 +
   1.864 +/*
   1.865 +** CAPI3REF: Advance A Changeset Iterator
   1.866 +**
   1.867 +** This function may only be used with iterators created by function
   1.868 +** [sqlite3changeset_start()]. If it is called on an iterator passed to
   1.869 +** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
   1.870 +** is returned and the call has no effect.
   1.871 +**
   1.872 +** Immediately after an iterator is created by sqlite3changeset_start(), it
   1.873 +** does not point to any change in the changeset. Assuming the changeset
   1.874 +** is not empty, the first call to this function advances the iterator to
   1.875 +** point to the first change in the changeset. Each subsequent call advances
   1.876 +** the iterator to point to the next change in the changeset (if any). If
   1.877 +** no error occurs and the iterator points to a valid change after a call
   1.878 +** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned. 
   1.879 +** Otherwise, if all changes in the changeset have already been visited,
   1.880 +** SQLITE_DONE is returned.
   1.881 +**
   1.882 +** If an error occurs, an SQLite error code is returned. Possible error 
   1.883 +** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
   1.884 +** SQLITE_NOMEM.
   1.885 +*/
   1.886 +int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
   1.887 +
   1.888 +/*
   1.889 +** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
   1.890 +**
   1.891 +** The pIter argument passed to this function may either be an iterator
   1.892 +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
   1.893 +** created by [sqlite3changeset_start()]. In the latter case, the most recent
   1.894 +** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
   1.895 +** is not the case, this function returns [SQLITE_MISUSE].
   1.896 +**
   1.897 +** If argument pzTab is not NULL, then *pzTab is set to point to a
   1.898 +** nul-terminated utf-8 encoded string containing the name of the table
   1.899 +** affected by the current change. The buffer remains valid until either
   1.900 +** sqlite3changeset_next() is called on the iterator or until the 
   1.901 +** conflict-handler function returns. If pnCol is not NULL, then *pnCol is 
   1.902 +** set to the number of columns in the table affected by the change. If
   1.903 +** pbIncorrect is not NULL, then *pbIndirect is set to true (1) if the change
   1.904 +** is an indirect change, or false (0) otherwise. See the documentation for
   1.905 +** [sqlite3session_indirect()] for a description of direct and indirect
   1.906 +** changes. Finally, if pOp is not NULL, then *pOp is set to one of 
   1.907 +** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the 
   1.908 +** type of change that the iterator currently points to.
   1.909 +**
   1.910 +** If no error occurs, SQLITE_OK is returned. If an error does occur, an
   1.911 +** SQLite error code is returned. The values of the output variables may not
   1.912 +** be trusted in this case.
   1.913 +*/
   1.914 +int sqlite3changeset_op(
   1.915 +  sqlite3_changeset_iter *pIter,  /* Iterator object */
   1.916 +  const char **pzTab,             /* OUT: Pointer to table name */
   1.917 +  int *pnCol,                     /* OUT: Number of columns in table */
   1.918 +  int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
   1.919 +  int *pbIndirect                 /* OUT: True for an 'indirect' change */
   1.920 +);
   1.921 +
   1.922 +/*
   1.923 +** CAPI3REF: Obtain The Primary Key Definition Of A Table
   1.924 +**
   1.925 +** For each modified table, a changeset includes the following:
   1.926 +**
   1.927 +** <ul>
   1.928 +**   <li> The number of columns in the table, and
   1.929 +**   <li> Which of those columns make up the tables PRIMARY KEY.
   1.930 +** </ul>
   1.931 +**
   1.932 +** This function is used to find which columns comprise the PRIMARY KEY of
   1.933 +** the table modified by the change that iterator pIter currently points to.
   1.934 +** If successful, *pabPK is set to point to an array of nCol entries, where
   1.935 +** nCol is the number of columns in the table. Elements of *pabPK are set to
   1.936 +** 0x01 if the corresponding column is part of the tables primary key, or
   1.937 +** 0x00 if it is not.
   1.938 +**
   1.939 +** If argumet pnCol is not NULL, then *pnCol is set to the number of columns
   1.940 +** in the table.
   1.941 +**
   1.942 +** If this function is called when the iterator does not point to a valid
   1.943 +** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
   1.944 +** SQLITE_OK is returned and the output variables populated as described
   1.945 +** above.
   1.946 +*/
   1.947 +int sqlite3changeset_pk(
   1.948 +  sqlite3_changeset_iter *pIter,  /* Iterator object */
   1.949 +  unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
   1.950 +  int *pnCol                      /* OUT: Number of entries in output array */
   1.951 +);
   1.952 +
   1.953 +/*
   1.954 +** CAPI3REF: Obtain old.* Values From A Changeset Iterator
   1.955 +**
   1.956 +** The pIter argument passed to this function may either be an iterator
   1.957 +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
   1.958 +** created by [sqlite3changeset_start()]. In the latter case, the most recent
   1.959 +** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
   1.960 +** Furthermore, it may only be called if the type of change that the iterator
   1.961 +** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
   1.962 +** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
   1.963 +**
   1.964 +** Argument iVal must be greater than or equal to 0, and less than the number
   1.965 +** of columns in the table affected by the current change. Otherwise,
   1.966 +** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
   1.967 +**
   1.968 +** If successful, this function sets *ppValue to point to a protected
   1.969 +** sqlite3_value object containing the iVal'th value from the vector of 
   1.970 +** original row values stored as part of the UPDATE or DELETE change and
   1.971 +** returns SQLITE_OK. The name of the function comes from the fact that this 
   1.972 +** is similar to the "old.*" columns available to update or delete triggers.
   1.973 +**
   1.974 +** If some other error occurs (e.g. an OOM condition), an SQLite error code
   1.975 +** is returned and *ppValue is set to NULL.
   1.976 +*/
   1.977 +int sqlite3changeset_old(
   1.978 +  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
   1.979 +  int iVal,                       /* Column number */
   1.980 +  sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
   1.981 +);
   1.982 +
   1.983 +/*
   1.984 +** CAPI3REF: Obtain new.* Values From A Changeset Iterator
   1.985 +**
   1.986 +** The pIter argument passed to this function may either be an iterator
   1.987 +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
   1.988 +** created by [sqlite3changeset_start()]. In the latter case, the most recent
   1.989 +** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
   1.990 +** Furthermore, it may only be called if the type of change that the iterator
   1.991 +** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
   1.992 +** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
   1.993 +**
   1.994 +** Argument iVal must be greater than or equal to 0, and less than the number
   1.995 +** of columns in the table affected by the current change. Otherwise,
   1.996 +** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
   1.997 +**
   1.998 +** If successful, this function sets *ppValue to point to a protected
   1.999 +** sqlite3_value object containing the iVal'th value from the vector of 
  1.1000 +** new row values stored as part of the UPDATE or INSERT change and
  1.1001 +** returns SQLITE_OK. If the change is an UPDATE and does not include
  1.1002 +** a new value for the requested column, *ppValue is set to NULL and 
  1.1003 +** SQLITE_OK returned. The name of the function comes from the fact that 
  1.1004 +** this is similar to the "new.*" columns available to update or delete 
  1.1005 +** triggers.
  1.1006 +**
  1.1007 +** If some other error occurs (e.g. an OOM condition), an SQLite error code
  1.1008 +** is returned and *ppValue is set to NULL.
  1.1009 +*/
  1.1010 +int sqlite3changeset_new(
  1.1011 +  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  1.1012 +  int iVal,                       /* Column number */
  1.1013 +  sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
  1.1014 +);
  1.1015 +
  1.1016 +/*
  1.1017 +** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
  1.1018 +**
  1.1019 +** This function should only be used with iterator objects passed to a
  1.1020 +** conflict-handler callback by [sqlite3changeset_apply()] with either
  1.1021 +** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
  1.1022 +** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
  1.1023 +** is set to NULL.
  1.1024 +**
  1.1025 +** Argument iVal must be greater than or equal to 0, and less than the number
  1.1026 +** of columns in the table affected by the current change. Otherwise,
  1.1027 +** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
  1.1028 +**
  1.1029 +** If successful, this function sets *ppValue to point to a protected
  1.1030 +** sqlite3_value object containing the iVal'th value from the 
  1.1031 +** "conflicting row" associated with the current conflict-handler callback
  1.1032 +** and returns SQLITE_OK.
  1.1033 +**
  1.1034 +** If some other error occurs (e.g. an OOM condition), an SQLite error code
  1.1035 +** is returned and *ppValue is set to NULL.
  1.1036 +*/
  1.1037 +int sqlite3changeset_conflict(
  1.1038 +  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  1.1039 +  int iVal,                       /* Column number */
  1.1040 +  sqlite3_value **ppValue         /* OUT: Value from conflicting row */
  1.1041 +);
  1.1042 +
  1.1043 +/*
  1.1044 +** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
  1.1045 +**
  1.1046 +** This function may only be called with an iterator passed to an
  1.1047 +** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
  1.1048 +** it sets the output variable to the total number of known foreign key
  1.1049 +** violations in the destination database and returns SQLITE_OK.
  1.1050 +**
  1.1051 +** In all other cases this function returns SQLITE_MISUSE.
  1.1052 +*/
  1.1053 +int sqlite3changeset_fk_conflicts(
  1.1054 +  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  1.1055 +  int *pnOut                      /* OUT: Number of FK violations */
  1.1056 +);
  1.1057 +
  1.1058 +
  1.1059 +/*
  1.1060 +** CAPI3REF: Finalize A Changeset Iterator
  1.1061 +**
  1.1062 +** This function is used to finalize an iterator allocated with
  1.1063 +** [sqlite3changeset_start()].
  1.1064 +**
  1.1065 +** This function should only be called on iterators created using the
  1.1066 +** [sqlite3changeset_start()] function. If an application calls this
  1.1067 +** function with an iterator passed to a conflict-handler by
  1.1068 +** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
  1.1069 +** call has no effect.
  1.1070 +**
  1.1071 +** If an error was encountered within a call to an sqlite3changeset_xxx()
  1.1072 +** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
  1.1073 +** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
  1.1074 +** to that error is returned by this function. Otherwise, SQLITE_OK is
  1.1075 +** returned. This is to allow the following pattern (pseudo-code):
  1.1076 +**
  1.1077 +**   sqlite3changeset_start();
  1.1078 +**   while( SQLITE_ROW==sqlite3changeset_next() ){
  1.1079 +**     // Do something with change.
  1.1080 +**   }
  1.1081 +**   rc = sqlite3changeset_finalize();
  1.1082 +**   if( rc!=SQLITE_OK ){
  1.1083 +**     // An error has occurred 
  1.1084 +**   }
  1.1085 +*/
  1.1086 +int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
  1.1087 +
  1.1088 +/*
  1.1089 +** CAPI3REF: Invert A Changeset
  1.1090 +**
  1.1091 +** This function is used to "invert" a changeset object. Applying an inverted
  1.1092 +** changeset to a database reverses the effects of applying the uninverted
  1.1093 +** changeset. Specifically:
  1.1094 +**
  1.1095 +** <ul>
  1.1096 +**   <li> Each DELETE change is changed to an INSERT, and
  1.1097 +**   <li> Each INSERT change is changed to a DELETE, and
  1.1098 +**   <li> For each UPDATE change, the old.* and new.* values are exchanged.
  1.1099 +** </ul>
  1.1100 +**
  1.1101 +** This function does not change the order in which changes appear within
  1.1102 +** the changeset. It merely reverses the sense of each individual change.
  1.1103 +**
  1.1104 +** If successful, a pointer to a buffer containing the inverted changeset
  1.1105 +** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
  1.1106 +** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
  1.1107 +** zeroed and an SQLite error code returned.
  1.1108 +**
  1.1109 +** It is the responsibility of the caller to eventually call sqlite3_free()
  1.1110 +** on the *ppOut pointer to free the buffer allocation following a successful 
  1.1111 +** call to this function.
  1.1112 +**
  1.1113 +** WARNING/TODO: This function currently assumes that the input is a valid
  1.1114 +** changeset. If it is not, the results are undefined.
  1.1115 +*/
  1.1116 +int sqlite3changeset_invert(
  1.1117 +  int nIn, const void *pIn,       /* Input changeset */
  1.1118 +  int *pnOut, void **ppOut        /* OUT: Inverse of input */
  1.1119 +);
  1.1120 +
  1.1121 +/*
  1.1122 +** CAPI3REF: Concatenate Two Changeset Objects
  1.1123 +**
  1.1124 +** This function is used to concatenate two changesets, A and B, into a 
  1.1125 +** single changeset. The result is a changeset equivalent to applying
  1.1126 +** changeset A followed by changeset B. 
  1.1127 +**
  1.1128 +** This function combines the two input changesets using an 
  1.1129 +** sqlite3_changegroup object. Calling it produces similar results as the
  1.1130 +** following code fragment:
  1.1131 +**
  1.1132 +**   sqlite3_changegroup *pGrp;
  1.1133 +**   rc = sqlite3_changegroup_new(&pGrp);
  1.1134 +**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
  1.1135 +**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
  1.1136 +**   if( rc==SQLITE_OK ){
  1.1137 +**     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
  1.1138 +**   }else{
  1.1139 +**     *ppOut = 0;
  1.1140 +**     *pnOut = 0;
  1.1141 +**   }
  1.1142 +**
  1.1143 +** Refer to the sqlite3_changegroup documentation below for details.
  1.1144 +*/
  1.1145 +int sqlite3changeset_concat(
  1.1146 +  int nA,                         /* Number of bytes in buffer pA */
  1.1147 +  void *pA,                       /* Pointer to buffer containing changeset A */
  1.1148 +  int nB,                         /* Number of bytes in buffer pB */
  1.1149 +  void *pB,                       /* Pointer to buffer containing changeset B */
  1.1150 +  int *pnOut,                     /* OUT: Number of bytes in output changeset */
  1.1151 +  void **ppOut                    /* OUT: Buffer containing output changeset */
  1.1152 +);
  1.1153 +
  1.1154 +
  1.1155 +/*
  1.1156 +** Changegroup handle.
  1.1157 +*/
  1.1158 +typedef struct sqlite3_changegroup sqlite3_changegroup;
  1.1159 +
  1.1160 +/*
  1.1161 +** CAPI3REF: Combine two or more changesets into a single changeset.
  1.1162 +**
  1.1163 +** An sqlite3_changegroup object is used to combine two or more changesets
  1.1164 +** (or patchsets) into a single changeset (or patchset). A single changegroup
  1.1165 +** object may combine changesets or patchsets, but not both. The output is
  1.1166 +** always in the same format as the input.
  1.1167 +**
  1.1168 +** If successful, this function returns SQLITE_OK and populates (*pp) with
  1.1169 +** a pointer to a new sqlite3_changegroup object before returning. The caller
  1.1170 +** should eventually free the returned object using a call to 
  1.1171 +** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
  1.1172 +** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
  1.1173 +**
  1.1174 +** The usual usage pattern for an sqlite3_changegroup object is as follows:
  1.1175 +**
  1.1176 +** <ul>
  1.1177 +**   <li> It is created using a call to sqlite3changegroup_new().
  1.1178 +**
  1.1179 +**   <li> Zero or more changesets (or patchsets) are added to the object
  1.1180 +**        by calling sqlite3changegroup_add().
  1.1181 +**
  1.1182 +**   <li> The result of combining all input changesets together is obtained 
  1.1183 +**        by the application via a call to sqlite3changegroup_output().
  1.1184 +**
  1.1185 +**   <li> The object is deleted using a call to sqlite3changegroup_delete().
  1.1186 +** </ul>
  1.1187 +**
  1.1188 +** Any number of calls to add() and output() may be made between the calls to
  1.1189 +** new() and delete(), and in any order.
  1.1190 +**
  1.1191 +** As well as the regular sqlite3changegroup_add() and 
  1.1192 +** sqlite3changegroup_output() functions, also available are the streaming
  1.1193 +** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
  1.1194 +*/
  1.1195 +int sqlite3changegroup_new(sqlite3_changegroup **pp);
  1.1196 +
  1.1197 +/*
  1.1198 +** Add all changes within the changeset (or patchset) in buffer pData (size
  1.1199 +** nData bytes) to the changegroup. 
  1.1200 +**
  1.1201 +** If the buffer contains a patchset, then all prior calls to this function
  1.1202 +** on the same changegroup object must also have specified patchsets. Or, if
  1.1203 +** the buffer contains a changeset, so must have the earlier calls to this
  1.1204 +** function. Otherwise, SQLITE_ERROR is returned and no changes are added
  1.1205 +** to the changegroup.
  1.1206 +**
  1.1207 +** Rows within the changeset and changegroup are identified by the values in
  1.1208 +** their PRIMARY KEY columns. A change in the changeset is considered to
  1.1209 +** apply to the same row as a change already present in the changegroup if
  1.1210 +** the two rows have the same primary key.
  1.1211 +**
  1.1212 +** Changes to rows that that do not already appear in the changegroup are
  1.1213 +** simply copied into it. Or, if both the new changeset and the changegroup
  1.1214 +** contain changes that apply to a single row, the final contents of the
  1.1215 +** changegroup depends on the type of each change, as follows:
  1.1216 +**
  1.1217 +** <table border=1 style="margin-left:8ex;margin-right:8ex">
  1.1218 +**   <tr><th style="white-space:pre">Existing Change  </th>
  1.1219 +**       <th style="white-space:pre">New Change       </th>
  1.1220 +**       <th>Output Change
  1.1221 +**   <tr><td>INSERT <td>INSERT <td>
  1.1222 +**       The new change is ignored. This case does not occur if the new
  1.1223 +**       changeset was recorded immediately after the changesets already
  1.1224 +**       added to the changegroup.
  1.1225 +**   <tr><td>INSERT <td>UPDATE <td>
  1.1226 +**       The INSERT change remains in the changegroup. The values in the 
  1.1227 +**       INSERT change are modified as if the row was inserted by the
  1.1228 +**       existing change and then updated according to the new change.
  1.1229 +**   <tr><td>INSERT <td>DELETE <td>
  1.1230 +**       The existing INSERT is removed from the changegroup. The DELETE is
  1.1231 +**       not added.
  1.1232 +**   <tr><td>UPDATE <td>INSERT <td>
  1.1233 +**       The new change is ignored. This case does not occur if the new
  1.1234 +**       changeset was recorded immediately after the changesets already
  1.1235 +**       added to the changegroup.
  1.1236 +**   <tr><td>UPDATE <td>UPDATE <td>
  1.1237 +**       The existing UPDATE remains within the changegroup. It is amended 
  1.1238 +**       so that the accompanying values are as if the row was updated once 
  1.1239 +**       by the existing change and then again by the new change.
  1.1240 +**   <tr><td>UPDATE <td>DELETE <td>
  1.1241 +**       The existing UPDATE is replaced by the new DELETE within the
  1.1242 +**       changegroup.
  1.1243 +**   <tr><td>DELETE <td>INSERT <td>
  1.1244 +**       If one or more of the column values in the row inserted by the
  1.1245 +**       new change differ from those in the row deleted by the existing 
  1.1246 +**       change, the existing DELETE is replaced by an UPDATE within the
  1.1247 +**       changegroup. Otherwise, if the inserted row is exactly the same 
  1.1248 +**       as the deleted row, the existing DELETE is simply discarded.
  1.1249 +**   <tr><td>DELETE <td>UPDATE <td>
  1.1250 +**       The new change is ignored. This case does not occur if the new
  1.1251 +**       changeset was recorded immediately after the changesets already
  1.1252 +**       added to the changegroup.
  1.1253 +**   <tr><td>DELETE <td>DELETE <td>
  1.1254 +**       The new change is ignored. This case does not occur if the new
  1.1255 +**       changeset was recorded immediately after the changesets already
  1.1256 +**       added to the changegroup.
  1.1257 +** </table>
  1.1258 +**
  1.1259 +** If the new changeset contains changes to a table that is already present
  1.1260 +** in the changegroup, then the number of columns and the position of the
  1.1261 +** primary key columns for the table must be consistent. If this is not the
  1.1262 +** case, this function fails with SQLITE_SCHEMA. If the input changeset
  1.1263 +** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
  1.1264 +** returned. Or, if an out-of-memory condition occurs during processing, this
  1.1265 +** function returns SQLITE_NOMEM. In all cases, if an error occurs the
  1.1266 +** final contents of the changegroup is undefined.
  1.1267 +**
  1.1268 +** If no error occurs, SQLITE_OK is returned.
  1.1269 +*/
  1.1270 +int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
  1.1271 +
  1.1272 +/*
  1.1273 +** Obtain a buffer containing a changeset (or patchset) representing the
  1.1274 +** current contents of the changegroup. If the inputs to the changegroup
  1.1275 +** were themselves changesets, the output is a changeset. Or, if the
  1.1276 +** inputs were patchsets, the output is also a patchset.
  1.1277 +**
  1.1278 +** As with the output of the sqlite3session_changeset() and
  1.1279 +** sqlite3session_patchset() functions, all changes related to a single
  1.1280 +** table are grouped together in the output of this function. Tables appear
  1.1281 +** in the same order as for the very first changeset added to the changegroup.
  1.1282 +** If the second or subsequent changesets added to the changegroup contain
  1.1283 +** changes for tables that do not appear in the first changeset, they are
  1.1284 +** appended onto the end of the output changeset, again in the order in
  1.1285 +** which they are first encountered.
  1.1286 +**
  1.1287 +** If an error occurs, an SQLite error code is returned and the output
  1.1288 +** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
  1.1289 +** is returned and the output variables are set to the size of and a 
  1.1290 +** pointer to the output buffer, respectively. In this case it is the
  1.1291 +** responsibility of the caller to eventually free the buffer using a
  1.1292 +** call to sqlite3_free().
  1.1293 +*/
  1.1294 +int sqlite3changegroup_output(
  1.1295 +  sqlite3_changegroup*,
  1.1296 +  int *pnData,                    /* OUT: Size of output buffer in bytes */
  1.1297 +  void **ppData                   /* OUT: Pointer to output buffer */
  1.1298 +);
  1.1299 +
  1.1300 +/*
  1.1301 +** Delete a changegroup object.
  1.1302 +*/
  1.1303 +void sqlite3changegroup_delete(sqlite3_changegroup*);
  1.1304 +
  1.1305 +/*
  1.1306 +** CAPI3REF: Apply A Changeset To A Database
  1.1307 +**
  1.1308 +** Apply a changeset to a database. This function attempts to update the
  1.1309 +** "main" database attached to handle db with the changes found in the
  1.1310 +** changeset passed via the second and third arguments.
  1.1311 +**
  1.1312 +** The fourth argument (xFilter) passed to this function is the "filter
  1.1313 +** callback". If it is not NULL, then for each table affected by at least one
  1.1314 +** change in the changeset, the filter callback is invoked with
  1.1315 +** the table name as the second argument, and a copy of the context pointer
  1.1316 +** passed as the sixth argument to this function as the first. If the "filter
  1.1317 +** callback" returns zero, then no attempt is made to apply any changes to 
  1.1318 +** the table. Otherwise, if the return value is non-zero or the xFilter
  1.1319 +** argument to this function is NULL, all changes related to the table are
  1.1320 +** attempted.
  1.1321 +**
  1.1322 +** For each table that is not excluded by the filter callback, this function 
  1.1323 +** tests that the target database contains a compatible table. A table is 
  1.1324 +** considered compatible if all of the following are true:
  1.1325 +**
  1.1326 +** <ul>
  1.1327 +**   <li> The table has the same name as the name recorded in the 
  1.1328 +**        changeset, and
  1.1329 +**   <li> The table has the same number of columns as recorded in the 
  1.1330 +**        changeset, and
  1.1331 +**   <li> The table has primary key columns in the same position as 
  1.1332 +**        recorded in the changeset.
  1.1333 +** </ul>
  1.1334 +**
  1.1335 +** If there is no compatible table, it is not an error, but none of the
  1.1336 +** changes associated with the table are applied. A warning message is issued
  1.1337 +** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
  1.1338 +** one such warning is issued for each table in the changeset.
  1.1339 +**
  1.1340 +** For each change for which there is a compatible table, an attempt is made 
  1.1341 +** to modify the table contents according to the UPDATE, INSERT or DELETE 
  1.1342 +** change. If a change cannot be applied cleanly, the conflict handler 
  1.1343 +** function passed as the fifth argument to sqlite3changeset_apply() may be 
  1.1344 +** invoked. A description of exactly when the conflict handler is invoked for 
  1.1345 +** each type of change is below.
  1.1346 +**
  1.1347 +** Unlike the xFilter argument, xConflict may not be passed NULL. The results
  1.1348 +** of passing anything other than a valid function pointer as the xConflict
  1.1349 +** argument are undefined.
  1.1350 +**
  1.1351 +** Each time the conflict handler function is invoked, it must return one
  1.1352 +** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or 
  1.1353 +** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
  1.1354 +** if the second argument passed to the conflict handler is either
  1.1355 +** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
  1.1356 +** returns an illegal value, any changes already made are rolled back and
  1.1357 +** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different 
  1.1358 +** actions are taken by sqlite3changeset_apply() depending on the value
  1.1359 +** returned by each invocation of the conflict-handler function. Refer to
  1.1360 +** the documentation for the three 
  1.1361 +** [SQLITE_CHANGESET_OMIT|available return values] for details.
  1.1362 +**
  1.1363 +** <dl>
  1.1364 +** <dt>DELETE Changes<dd>
  1.1365 +**   For each DELETE change, this function checks if the target database 
  1.1366 +**   contains a row with the same primary key value (or values) as the 
  1.1367 +**   original row values stored in the changeset. If it does, and the values 
  1.1368 +**   stored in all non-primary key columns also match the values stored in 
  1.1369 +**   the changeset the row is deleted from the target database.
  1.1370 +**
  1.1371 +**   If a row with matching primary key values is found, but one or more of
  1.1372 +**   the non-primary key fields contains a value different from the original
  1.1373 +**   row value stored in the changeset, the conflict-handler function is
  1.1374 +**   invoked with [SQLITE_CHANGESET_DATA] as the second argument.
  1.1375 +**
  1.1376 +**   If no row with matching primary key values is found in the database,
  1.1377 +**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  1.1378 +**   passed as the second argument.
  1.1379 +**
  1.1380 +**   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
  1.1381 +**   (which can only happen if a foreign key constraint is violated), the
  1.1382 +**   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
  1.1383 +**   passed as the second argument. This includes the case where the DELETE
  1.1384 +**   operation is attempted because an earlier call to the conflict handler
  1.1385 +**   function returned [SQLITE_CHANGESET_REPLACE].
  1.1386 +**
  1.1387 +** <dt>INSERT Changes<dd>
  1.1388 +**   For each INSERT change, an attempt is made to insert the new row into
  1.1389 +**   the database.
  1.1390 +**
  1.1391 +**   If the attempt to insert the row fails because the database already 
  1.1392 +**   contains a row with the same primary key values, the conflict handler
  1.1393 +**   function is invoked with the second argument set to 
  1.1394 +**   [SQLITE_CHANGESET_CONFLICT].
  1.1395 +**
  1.1396 +**   If the attempt to insert the row fails because of some other constraint
  1.1397 +**   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 
  1.1398 +**   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
  1.1399 +**   This includes the case where the INSERT operation is re-attempted because 
  1.1400 +**   an earlier call to the conflict handler function returned 
  1.1401 +**   [SQLITE_CHANGESET_REPLACE].
  1.1402 +**
  1.1403 +** <dt>UPDATE Changes<dd>
  1.1404 +**   For each UPDATE change, this function checks if the target database 
  1.1405 +**   contains a row with the same primary key value (or values) as the 
  1.1406 +**   original row values stored in the changeset. If it does, and the values 
  1.1407 +**   stored in all non-primary key columns also match the values stored in 
  1.1408 +**   the changeset the row is updated within the target database.
  1.1409 +**
  1.1410 +**   If a row with matching primary key values is found, but one or more of
  1.1411 +**   the non-primary key fields contains a value different from an original
  1.1412 +**   row value stored in the changeset, the conflict-handler function is
  1.1413 +**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
  1.1414 +**   UPDATE changes only contain values for non-primary key fields that are
  1.1415 +**   to be modified, only those fields need to match the original values to
  1.1416 +**   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
  1.1417 +**
  1.1418 +**   If no row with matching primary key values is found in the database,
  1.1419 +**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  1.1420 +**   passed as the second argument.
  1.1421 +**
  1.1422 +**   If the UPDATE operation is attempted, but SQLite returns 
  1.1423 +**   SQLITE_CONSTRAINT, the conflict-handler function is invoked with 
  1.1424 +**   [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
  1.1425 +**   This includes the case where the UPDATE operation is attempted after 
  1.1426 +**   an earlier call to the conflict handler function returned
  1.1427 +**   [SQLITE_CHANGESET_REPLACE].  
  1.1428 +** </dl>
  1.1429 +**
  1.1430 +** It is safe to execute SQL statements, including those that write to the
  1.1431 +** table that the callback related to, from within the xConflict callback.
  1.1432 +** This can be used to further customize the applications conflict
  1.1433 +** resolution strategy.
  1.1434 +**
  1.1435 +** All changes made by this function are enclosed in a savepoint transaction.
  1.1436 +** If any other error (aside from a constraint failure when attempting to
  1.1437 +** write to the target database) occurs, then the savepoint transaction is
  1.1438 +** rolled back, restoring the target database to its original state, and an 
  1.1439 +** SQLite error code returned.
  1.1440 +*/
  1.1441 +int sqlite3changeset_apply(
  1.1442 +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
  1.1443 +  int nChangeset,                 /* Size of changeset in bytes */
  1.1444 +  void *pChangeset,               /* Changeset blob */
  1.1445 +  int(*xFilter)(
  1.1446 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
  1.1447 +    const char *zTab              /* Table name */
  1.1448 +  ),
  1.1449 +  int(*xConflict)(
  1.1450 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
  1.1451 +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
  1.1452 +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
  1.1453 +  ),
  1.1454 +  void *pCtx                      /* First argument passed to xConflict */
  1.1455 +);
  1.1456 +
  1.1457 +/* 
  1.1458 +** CAPI3REF: Constants Passed To The Conflict Handler
  1.1459 +**
  1.1460 +** Values that may be passed as the second argument to a conflict-handler.
  1.1461 +**
  1.1462 +** <dl>
  1.1463 +** <dt>SQLITE_CHANGESET_DATA<dd>
  1.1464 +**   The conflict handler is invoked with CHANGESET_DATA as the second argument
  1.1465 +**   when processing a DELETE or UPDATE change if a row with the required
  1.1466 +**   PRIMARY KEY fields is present in the database, but one or more other 
  1.1467 +**   (non primary-key) fields modified by the update do not contain the 
  1.1468 +**   expected "before" values.
  1.1469 +** 
  1.1470 +**   The conflicting row, in this case, is the database row with the matching
  1.1471 +**   primary key.
  1.1472 +** 
  1.1473 +** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
  1.1474 +**   The conflict handler is invoked with CHANGESET_NOTFOUND as the second
  1.1475 +**   argument when processing a DELETE or UPDATE change if a row with the
  1.1476 +**   required PRIMARY KEY fields is not present in the database.
  1.1477 +** 
  1.1478 +**   There is no conflicting row in this case. The results of invoking the
  1.1479 +**   sqlite3changeset_conflict() API are undefined.
  1.1480 +** 
  1.1481 +** <dt>SQLITE_CHANGESET_CONFLICT<dd>
  1.1482 +**   CHANGESET_CONFLICT is passed as the second argument to the conflict
  1.1483 +**   handler while processing an INSERT change if the operation would result 
  1.1484 +**   in duplicate primary key values.
  1.1485 +** 
  1.1486 +**   The conflicting row in this case is the database row with the matching
  1.1487 +**   primary key.
  1.1488 +**
  1.1489 +** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
  1.1490 +**   If foreign key handling is enabled, and applying a changeset leaves the
  1.1491 +**   database in a state containing foreign key violations, the conflict 
  1.1492 +**   handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
  1.1493 +**   exactly once before the changeset is committed. If the conflict handler
  1.1494 +**   returns CHANGESET_OMIT, the changes, including those that caused the
  1.1495 +**   foreign key constraint violation, are committed. Or, if it returns
  1.1496 +**   CHANGESET_ABORT, the changeset is rolled back.
  1.1497 +**
  1.1498 +**   No current or conflicting row information is provided. The only function
  1.1499 +**   it is possible to call on the supplied sqlite3_changeset_iter handle
  1.1500 +**   is sqlite3changeset_fk_conflicts().
  1.1501 +** 
  1.1502 +** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
  1.1503 +**   If any other constraint violation occurs while applying a change (i.e. 
  1.1504 +**   a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is 
  1.1505 +**   invoked with CHANGESET_CONSTRAINT as the second argument.
  1.1506 +** 
  1.1507 +**   There is no conflicting row in this case. The results of invoking the
  1.1508 +**   sqlite3changeset_conflict() API are undefined.
  1.1509 +**
  1.1510 +** </dl>
  1.1511 +*/
  1.1512 +#define SQLITE_CHANGESET_DATA        1
  1.1513 +#define SQLITE_CHANGESET_NOTFOUND    2
  1.1514 +#define SQLITE_CHANGESET_CONFLICT    3
  1.1515 +#define SQLITE_CHANGESET_CONSTRAINT  4
  1.1516 +#define SQLITE_CHANGESET_FOREIGN_KEY 5
  1.1517 +
  1.1518 +/* 
  1.1519 +** CAPI3REF: Constants Returned By The Conflict Handler
  1.1520 +**
  1.1521 +** A conflict handler callback must return one of the following three values.
  1.1522 +**
  1.1523 +** <dl>
  1.1524 +** <dt>SQLITE_CHANGESET_OMIT<dd>
  1.1525 +**   If a conflict handler returns this value no special action is taken. The
  1.1526 +**   change that caused the conflict is not applied. The session module 
  1.1527 +**   continues to the next change in the changeset.
  1.1528 +**
  1.1529 +** <dt>SQLITE_CHANGESET_REPLACE<dd>
  1.1530 +**   This value may only be returned if the second argument to the conflict
  1.1531 +**   handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
  1.1532 +**   is not the case, any changes applied so far are rolled back and the 
  1.1533 +**   call to sqlite3changeset_apply() returns SQLITE_MISUSE.
  1.1534 +**
  1.1535 +**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
  1.1536 +**   handler, then the conflicting row is either updated or deleted, depending
  1.1537 +**   on the type of change.
  1.1538 +**
  1.1539 +**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
  1.1540 +**   handler, then the conflicting row is removed from the database and a
  1.1541 +**   second attempt to apply the change is made. If this second attempt fails,
  1.1542 +**   the original row is restored to the database before continuing.
  1.1543 +**
  1.1544 +** <dt>SQLITE_CHANGESET_ABORT<dd>
  1.1545 +**   If this value is returned, any changes applied so far are rolled back 
  1.1546 +**   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
  1.1547 +** </dl>
  1.1548 +*/
  1.1549 +#define SQLITE_CHANGESET_OMIT       0
  1.1550 +#define SQLITE_CHANGESET_REPLACE    1
  1.1551 +#define SQLITE_CHANGESET_ABORT      2
  1.1552 +
  1.1553 +/*
  1.1554 +** CAPI3REF: Streaming Versions of API functions.
  1.1555 +**
  1.1556 +** The six streaming API xxx_strm() functions serve similar purposes to the 
  1.1557 +** corresponding non-streaming API functions:
  1.1558 +**
  1.1559 +** <table border=1 style="margin-left:8ex;margin-right:8ex">
  1.1560 +**   <tr><th>Streaming function<th>Non-streaming equivalent</th>
  1.1561 +**   <tr><td>sqlite3changeset_apply_str<td>[sqlite3changeset_apply] 
  1.1562 +**   <tr><td>sqlite3changeset_concat_str<td>[sqlite3changeset_concat] 
  1.1563 +**   <tr><td>sqlite3changeset_invert_str<td>[sqlite3changeset_invert] 
  1.1564 +**   <tr><td>sqlite3changeset_start_str<td>[sqlite3changeset_start] 
  1.1565 +**   <tr><td>sqlite3session_changeset_str<td>[sqlite3session_changeset] 
  1.1566 +**   <tr><td>sqlite3session_patchset_str<td>[sqlite3session_patchset] 
  1.1567 +** </table>
  1.1568 +**
  1.1569 +** Non-streaming functions that accept changesets (or patchsets) as input
  1.1570 +** require that the entire changeset be stored in a single buffer in memory. 
  1.1571 +** Similarly, those that return a changeset or patchset do so by returning 
  1.1572 +** a pointer to a single large buffer allocated using sqlite3_malloc(). 
  1.1573 +** Normally this is convenient. However, if an application running in a 
  1.1574 +** low-memory environment is required to handle very large changesets, the
  1.1575 +** large contiguous memory allocations required can become onerous.
  1.1576 +**
  1.1577 +** In order to avoid this problem, instead of a single large buffer, input
  1.1578 +** is passed to a streaming API functions by way of a callback function that
  1.1579 +** the sessions module invokes to incrementally request input data as it is
  1.1580 +** required. In all cases, a pair of API function parameters such as
  1.1581 +**
  1.1582 +**  <pre>
  1.1583 +**  &nbsp;     int nChangeset,
  1.1584 +**  &nbsp;     void *pChangeset,
  1.1585 +**  </pre>
  1.1586 +**
  1.1587 +** Is replaced by:
  1.1588 +**
  1.1589 +**  <pre>
  1.1590 +**  &nbsp;     int (*xInput)(void *pIn, void *pData, int *pnData),
  1.1591 +**  &nbsp;     void *pIn,
  1.1592 +**  </pre>
  1.1593 +**
  1.1594 +** Each time the xInput callback is invoked by the sessions module, the first
  1.1595 +** argument passed is a copy of the supplied pIn context pointer. The second 
  1.1596 +** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no 
  1.1597 +** error occurs the xInput method should copy up to (*pnData) bytes of data 
  1.1598 +** into the buffer and set (*pnData) to the actual number of bytes copied 
  1.1599 +** before returning SQLITE_OK. If the input is completely exhausted, (*pnData) 
  1.1600 +** should be set to zero to indicate this. Or, if an error occurs, an SQLite 
  1.1601 +** error code should be returned. In all cases, if an xInput callback returns
  1.1602 +** an error, all processing is abandoned and the streaming API function
  1.1603 +** returns a copy of the error code to the caller.
  1.1604 +**
  1.1605 +** In the case of sqlite3changeset_start_strm(), the xInput callback may be
  1.1606 +** invoked by the sessions module at any point during the lifetime of the
  1.1607 +** iterator. If such an xInput callback returns an error, the iterator enters
  1.1608 +** an error state, whereby all subsequent calls to iterator functions 
  1.1609 +** immediately fail with the same error code as returned by xInput.
  1.1610 +**
  1.1611 +** Similarly, streaming API functions that return changesets (or patchsets)
  1.1612 +** return them in chunks by way of a callback function instead of via a
  1.1613 +** pointer to a single large buffer. In this case, a pair of parameters such
  1.1614 +** as:
  1.1615 +**
  1.1616 +**  <pre>
  1.1617 +**  &nbsp;     int *pnChangeset,
  1.1618 +**  &nbsp;     void **ppChangeset,
  1.1619 +**  </pre>
  1.1620 +**
  1.1621 +** Is replaced by:
  1.1622 +**
  1.1623 +**  <pre>
  1.1624 +**  &nbsp;     int (*xOutput)(void *pOut, const void *pData, int nData),
  1.1625 +**  &nbsp;     void *pOut
  1.1626 +**  </pre>
  1.1627 +**
  1.1628 +** The xOutput callback is invoked zero or more times to return data to
  1.1629 +** the application. The first parameter passed to each call is a copy of the
  1.1630 +** pOut pointer supplied by the application. The second parameter, pData,
  1.1631 +** points to a buffer nData bytes in size containing the chunk of output
  1.1632 +** data being returned. If the xOutput callback successfully processes the
  1.1633 +** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
  1.1634 +** it should return some other SQLite error code. In this case processing
  1.1635 +** is immediately abandoned and the streaming API function returns a copy
  1.1636 +** of the xOutput error code to the application.
  1.1637 +**
  1.1638 +** The sessions module never invokes an xOutput callback with the third 
  1.1639 +** parameter set to a value less than or equal to zero. Other than this,
  1.1640 +** no guarantees are made as to the size of the chunks of data returned.
  1.1641 +*/
  1.1642 +int sqlite3changeset_apply_strm(
  1.1643 +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
  1.1644 +  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
  1.1645 +  void *pIn,                                          /* First arg for xInput */
  1.1646 +  int(*xFilter)(
  1.1647 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
  1.1648 +    const char *zTab              /* Table name */
  1.1649 +  ),
  1.1650 +  int(*xConflict)(
  1.1651 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
  1.1652 +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
  1.1653 +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
  1.1654 +  ),
  1.1655 +  void *pCtx                      /* First argument passed to xConflict */
  1.1656 +);
  1.1657 +int sqlite3changeset_concat_strm(
  1.1658 +  int (*xInputA)(void *pIn, void *pData, int *pnData),
  1.1659 +  void *pInA,
  1.1660 +  int (*xInputB)(void *pIn, void *pData, int *pnData),
  1.1661 +  void *pInB,
  1.1662 +  int (*xOutput)(void *pOut, const void *pData, int nData),
  1.1663 +  void *pOut
  1.1664 +);
  1.1665 +int sqlite3changeset_invert_strm(
  1.1666 +  int (*xInput)(void *pIn, void *pData, int *pnData),
  1.1667 +  void *pIn,
  1.1668 +  int (*xOutput)(void *pOut, const void *pData, int nData),
  1.1669 +  void *pOut
  1.1670 +);
  1.1671 +int sqlite3changeset_start_strm(
  1.1672 +  sqlite3_changeset_iter **pp,
  1.1673 +  int (*xInput)(void *pIn, void *pData, int *pnData),
  1.1674 +  void *pIn
  1.1675 +);
  1.1676 +int sqlite3session_changeset_strm(
  1.1677 +  sqlite3_session *pSession,
  1.1678 +  int (*xOutput)(void *pOut, const void *pData, int nData),
  1.1679 +  void *pOut
  1.1680 +);
  1.1681 +int sqlite3session_patchset_strm(
  1.1682 +  sqlite3_session *pSession,
  1.1683 +  int (*xOutput)(void *pOut, const void *pData, int nData),
  1.1684 +  void *pOut
  1.1685 +);
  1.1686 +int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
  1.1687 +    int (*xInput)(void *pIn, void *pData, int *pnData),
  1.1688 +    void *pIn
  1.1689 +);
  1.1690 +int sqlite3changegroup_output_strm(sqlite3_changegroup*,
  1.1691 +    int (*xOutput)(void *pOut, const void *pData, int nData), 
  1.1692 +    void *pOut
  1.1693 +);
  1.1694 +
  1.1695 +
  1.1696 +/*
  1.1697 +** Make sure we can call this stuff from C++.
  1.1698 +*/
  1.1699 +#if 0
  1.1700 +}
  1.1701 +#endif
  1.1702 +
  1.1703 +#endif  /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
  1.1704 +
  1.1705 +/******** End of sqlite3session.h *********/
  1.1706 +/******** Begin file fts5.h *********/
  1.1707  /*
  1.1708  ** 2014 May 31
  1.1709  **
  1.1710 @@ -8469,11 +10001,13 @@
  1.1711  **       ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
  1.1712  **
  1.1713  **   with $p set to a phrase equivalent to the phrase iPhrase of the
  1.1714 -**   current query is executed. For each row visited, the callback function
  1.1715 -**   passed as the fourth argument is invoked. The context and API objects 
  1.1716 -**   passed to the callback function may be used to access the properties of
  1.1717 -**   each matched row. Invoking Api.xUserData() returns a copy of the pointer
  1.1718 -**   passed as the third argument to pUserData.
  1.1719 +**   current query is executed. Any column filter that applies to
  1.1720 +**   phrase iPhrase of the current query is included in $p. For each 
  1.1721 +**   row visited, the callback function passed as the fourth argument 
  1.1722 +**   is invoked. The context and API objects passed to the callback 
  1.1723 +**   function may be used to access the properties of each matched row.
  1.1724 +**   Invoking Api.xUserData() returns a copy of the pointer passed as 
  1.1725 +**   the third argument to pUserData.
  1.1726  **
  1.1727  **   If the callback function returns any value other than SQLITE_OK, the
  1.1728  **   query is abandoned and the xQueryPhrase function returns immediately.
  1.1729 @@ -8903,6 +10437,7 @@
  1.1730  #endif /* _FTS5_H */
  1.1731  
  1.1732  
  1.1733 +/******** End of fts5.h *********/
  1.1734  
  1.1735  /************** End of sqlite3.h *********************************************/
  1.1736  /************** Continuing where we left off in sqliteInt.h ******************/
  1.1737 @@ -9020,13 +10555,13 @@
  1.1738  ** The suggested maximum number of in-memory pages to use for
  1.1739  ** the main database table and for temporary tables.
  1.1740  **
  1.1741 -** IMPLEMENTATION-OF: R-31093-59126 The default suggested cache size
  1.1742 -** is 2000 pages.
  1.1743 +** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
  1.1744 +** which means the cache size is limited to 2048000 bytes of memory.
  1.1745  ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
  1.1746  ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
  1.1747  */
  1.1748  #ifndef SQLITE_DEFAULT_CACHE_SIZE
  1.1749 -# define SQLITE_DEFAULT_CACHE_SIZE  2000
  1.1750 +# define SQLITE_DEFAULT_CACHE_SIZE  -2000
  1.1751  #endif
  1.1752  
  1.1753  /*
  1.1754 @@ -9039,8 +10574,9 @@
  1.1755  
  1.1756  /*
  1.1757  ** The maximum number of attached databases.  This must be between 0
  1.1758 -** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
  1.1759 -** is used internally to track attached databases.
  1.1760 +** and 125.  The upper bound of 125 is because the attached databases are
  1.1761 +** counted using a signed 8-bit integer which has a maximum value of 127
  1.1762 +** and we have to allow 2 extra counts for the "main" and "temp" databases.
  1.1763  */
  1.1764  #ifndef SQLITE_MAX_ATTACHED
  1.1765  # define SQLITE_MAX_ATTACHED 10
  1.1766 @@ -9075,7 +10611,7 @@
  1.1767  ** The default size of a database page.
  1.1768  */
  1.1769  #ifndef SQLITE_DEFAULT_PAGE_SIZE
  1.1770 -# define SQLITE_DEFAULT_PAGE_SIZE 1024
  1.1771 +# define SQLITE_DEFAULT_PAGE_SIZE 4096
  1.1772  #endif
  1.1773  #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
  1.1774  # undef SQLITE_DEFAULT_PAGE_SIZE
  1.1775 @@ -9156,7 +10692,7 @@
  1.1776  ** to the next, so we have developed the following set of #if statements
  1.1777  ** to generate appropriate macros for a wide range of compilers.
  1.1778  **
  1.1779 -** The correct "ANSI" way to do this is to use the intptr_t type. 
  1.1780 +** The correct "ANSI" way to do this is to use the intptr_t type.
  1.1781  ** Unfortunately, that typedef is not available on all compilers, or
  1.1782  ** if it is available, it requires an #include of specific headers
  1.1783  ** that vary from one machine to the next.
  1.1784 @@ -9181,21 +10717,6 @@
  1.1785  #endif
  1.1786  
  1.1787  /*
  1.1788 -** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
  1.1789 -** something between S (inclusive) and E (exclusive).
  1.1790 -**
  1.1791 -** In other words, S is a buffer and E is a pointer to the first byte after
  1.1792 -** the end of buffer S.  This macro returns true if P points to something
  1.1793 -** contained within the buffer S.
  1.1794 -*/
  1.1795 -#if defined(HAVE_STDINT_H)
  1.1796 -# define SQLITE_WITHIN(P,S,E) \
  1.1797 -    ((uintptr_t)(P)>=(uintptr_t)(S) && (uintptr_t)(P)<(uintptr_t)(E))
  1.1798 -#else
  1.1799 -# define SQLITE_WITHIN(P,S,E) ((P)>=(S) && (P)<(E))
  1.1800 -#endif
  1.1801 -
  1.1802 -/*
  1.1803  ** A macro to hint to the compiler that a function should not be
  1.1804  ** inlined.
  1.1805  */
  1.1806 @@ -9323,7 +10844,7 @@
  1.1807  ** is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
  1.1808  ** feature.
  1.1809  */
  1.1810 -#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
  1.1811 +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
  1.1812  # define NDEBUG 1
  1.1813  #endif
  1.1814  #if defined(NDEBUG) && defined(SQLITE_DEBUG)
  1.1815 @@ -9338,7 +10859,7 @@
  1.1816  #endif
  1.1817  
  1.1818  /*
  1.1819 -** The testcase() macro is used to aid in coverage testing.  When 
  1.1820 +** The testcase() macro is used to aid in coverage testing.  When
  1.1821  ** doing coverage testing, the condition inside the argument to
  1.1822  ** testcase() must be evaluated both true and false in order to
  1.1823  ** get full branch coverage.  The testcase() macro is inserted
  1.1824 @@ -9384,7 +10905,7 @@
  1.1825  #endif
  1.1826  
  1.1827  /*
  1.1828 -** The ALWAYS and NEVER macros surround boolean expressions which 
  1.1829 +** The ALWAYS and NEVER macros surround boolean expressions which
  1.1830  ** are intended to always be true or false, respectively.  Such
  1.1831  ** expressions could be omitted from the code completely.  But they
  1.1832  ** are included in a few cases in order to enhance the resilience
  1.1833 @@ -9398,7 +10919,7 @@
  1.1834  ** be true and false so that the unreachable code they specify will
  1.1835  ** not be counted as untested code.
  1.1836  */
  1.1837 -#if defined(SQLITE_COVERAGE_TEST)
  1.1838 +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
  1.1839  # define ALWAYS(X)      (1)
  1.1840  # define NEVER(X)       (0)
  1.1841  #elif !defined(NDEBUG)
  1.1842 @@ -9451,6 +10972,13 @@
  1.1843  #endif
  1.1844  
  1.1845  /*
  1.1846 +** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
  1.1847 +*/
  1.1848 +#ifdef SQLITE_OMIT_EXPLAIN
  1.1849 +# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
  1.1850 +#endif
  1.1851 +
  1.1852 +/*
  1.1853  ** Return true (non-zero) if the input is an integer that is too large
  1.1854  ** to fit in 32-bits.  This macro is used inside of various testcase()
  1.1855  ** macros to verify that we have tested SQLite for large-file support.
  1.1856 @@ -9596,76 +11124,76 @@
  1.1857  #define TK_AS                              24
  1.1858  #define TK_WITHOUT                         25
  1.1859  #define TK_COMMA                           26
  1.1860 -#define TK_ID                              27
  1.1861 -#define TK_INDEXED                         28
  1.1862 -#define TK_ABORT                           29
  1.1863 -#define TK_ACTION                          30
  1.1864 -#define TK_AFTER                           31
  1.1865 -#define TK_ANALYZE                         32
  1.1866 -#define TK_ASC                             33
  1.1867 -#define TK_ATTACH                          34
  1.1868 -#define TK_BEFORE                          35
  1.1869 -#define TK_BY                              36
  1.1870 -#define TK_CASCADE                         37
  1.1871 -#define TK_CAST                            38
  1.1872 -#define TK_COLUMNKW                        39
  1.1873 -#define TK_CONFLICT                        40
  1.1874 -#define TK_DATABASE                        41
  1.1875 -#define TK_DESC                            42
  1.1876 -#define TK_DETACH                          43
  1.1877 -#define TK_EACH                            44
  1.1878 -#define TK_FAIL                            45
  1.1879 -#define TK_FOR                             46
  1.1880 -#define TK_IGNORE                          47
  1.1881 -#define TK_INITIALLY                       48
  1.1882 -#define TK_INSTEAD                         49
  1.1883 -#define TK_LIKE_KW                         50
  1.1884 -#define TK_MATCH                           51
  1.1885 -#define TK_NO                              52
  1.1886 -#define TK_KEY                             53
  1.1887 -#define TK_OF                              54
  1.1888 -#define TK_OFFSET                          55
  1.1889 -#define TK_PRAGMA                          56
  1.1890 -#define TK_RAISE                           57
  1.1891 -#define TK_RECURSIVE                       58
  1.1892 -#define TK_REPLACE                         59
  1.1893 -#define TK_RESTRICT                        60
  1.1894 -#define TK_ROW                             61
  1.1895 -#define TK_TRIGGER                         62
  1.1896 -#define TK_VACUUM                          63
  1.1897 -#define TK_VIEW                            64
  1.1898 -#define TK_VIRTUAL                         65
  1.1899 -#define TK_WITH                            66
  1.1900 -#define TK_REINDEX                         67
  1.1901 -#define TK_RENAME                          68
  1.1902 -#define TK_CTIME_KW                        69
  1.1903 -#define TK_ANY                             70
  1.1904 -#define TK_OR                              71
  1.1905 -#define TK_AND                             72
  1.1906 -#define TK_IS                              73
  1.1907 -#define TK_BETWEEN                         74
  1.1908 -#define TK_IN                              75
  1.1909 -#define TK_ISNULL                          76
  1.1910 -#define TK_NOTNULL                         77
  1.1911 -#define TK_NE                              78
  1.1912 -#define TK_EQ                              79
  1.1913 -#define TK_GT                              80
  1.1914 -#define TK_LE                              81
  1.1915 -#define TK_LT                              82
  1.1916 -#define TK_GE                              83
  1.1917 -#define TK_ESCAPE                          84
  1.1918 -#define TK_BITAND                          85
  1.1919 -#define TK_BITOR                           86
  1.1920 -#define TK_LSHIFT                          87
  1.1921 -#define TK_RSHIFT                          88
  1.1922 -#define TK_PLUS                            89
  1.1923 -#define TK_MINUS                           90
  1.1924 -#define TK_STAR                            91
  1.1925 -#define TK_SLASH                           92
  1.1926 -#define TK_REM                             93
  1.1927 -#define TK_CONCAT                          94
  1.1928 -#define TK_COLLATE                         95
  1.1929 -#define TK_BITNOT                          96
  1.1930 +#define TK_OR                              27
  1.1931 +#define TK_AND                             28
  1.1932 +#define TK_IS                              29
  1.1933 +#define TK_MATCH                           30
  1.1934 +#define TK_LIKE_KW                         31
  1.1935 +#define TK_BETWEEN                         32
  1.1936 +#define TK_IN                              33
  1.1937 +#define TK_ISNULL                          34
  1.1938 +#define TK_NOTNULL                         35
  1.1939 +#define TK_NE                              36
  1.1940 +#define TK_EQ                              37
  1.1941 +#define TK_GT                              38
  1.1942 +#define TK_LE                              39
  1.1943 +#define TK_LT                              40
  1.1944 +#define TK_GE                              41
  1.1945 +#define TK_ESCAPE                          42
  1.1946 +#define TK_BITAND                          43
  1.1947 +#define TK_BITOR                           44
  1.1948 +#define TK_LSHIFT                          45
  1.1949 +#define TK_RSHIFT                          46
  1.1950 +#define TK_PLUS                            47
  1.1951 +#define TK_MINUS                           48
  1.1952 +#define TK_STAR                            49
  1.1953 +#define TK_SLASH                           50
  1.1954 +#define TK_REM                             51
  1.1955 +#define TK_CONCAT                          52
  1.1956 +#define TK_COLLATE                         53
  1.1957 +#define TK_BITNOT                          54
  1.1958 +#define TK_ID                              55
  1.1959 +#define TK_INDEXED                         56
  1.1960 +#define TK_ABORT                           57
  1.1961 +#define TK_ACTION                          58
  1.1962 +#define TK_AFTER                           59
  1.1963 +#define TK_ANALYZE                         60
  1.1964 +#define TK_ASC                             61
  1.1965 +#define TK_ATTACH                          62
  1.1966 +#define TK_BEFORE                          63
  1.1967 +#define TK_BY                              64
  1.1968 +#define TK_CASCADE                         65
  1.1969 +#define TK_CAST                            66
  1.1970 +#define TK_COLUMNKW                        67
  1.1971 +#define TK_CONFLICT                        68
  1.1972 +#define TK_DATABASE                        69
  1.1973 +#define TK_DESC                            70
  1.1974 +#define TK_DETACH                          71
  1.1975 +#define TK_EACH                            72
  1.1976 +#define TK_FAIL                            73
  1.1977 +#define TK_FOR                             74
  1.1978 +#define TK_IGNORE                          75
  1.1979 +#define TK_INITIALLY                       76
  1.1980 +#define TK_INSTEAD                         77
  1.1981 +#define TK_NO                              78
  1.1982 +#define TK_KEY                             79
  1.1983 +#define TK_OF                              80
  1.1984 +#define TK_OFFSET                          81
  1.1985 +#define TK_PRAGMA                          82
  1.1986 +#define TK_RAISE                           83
  1.1987 +#define TK_RECURSIVE                       84
  1.1988 +#define TK_REPLACE                         85
  1.1989 +#define TK_RESTRICT                        86
  1.1990 +#define TK_ROW                             87
  1.1991 +#define TK_TRIGGER                         88
  1.1992 +#define TK_VACUUM                          89
  1.1993 +#define TK_VIEW                            90
  1.1994 +#define TK_VIRTUAL                         91
  1.1995 +#define TK_WITH                            92
  1.1996 +#define TK_REINDEX                         93
  1.1997 +#define TK_RENAME                          94
  1.1998 +#define TK_CTIME_KW                        95
  1.1999 +#define TK_ANY                             96
  1.2000  #define TK_STRING                          97
  1.2001  #define TK_JOIN_KW                         98
  1.2002  #define TK_CONSTRAINT                      99
  1.2003 @@ -9728,8 +11256,9 @@
  1.2004  #define TK_UPLUS                          156
  1.2005  #define TK_REGISTER                       157
  1.2006  #define TK_ASTERISK                       158
  1.2007 -#define TK_SPACE                          159
  1.2008 -#define TK_ILLEGAL                        160
  1.2009 +#define TK_SPAN                           159
  1.2010 +#define TK_SPACE                          160
  1.2011 +#define TK_ILLEGAL                        161
  1.2012  
  1.2013  /* The token codes above must all fit in 8 bits */
  1.2014  #define TKFLG_MASK           0xff  
  1.2015 @@ -9768,7 +11297,7 @@
  1.2016  
  1.2017  /*
  1.2018  ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
  1.2019 -** afterward. Having this macro allows us to cause the C compiler 
  1.2020 +** afterward. Having this macro allows us to cause the C compiler
  1.2021  ** to omit code used by TEMP tables without messy #ifndef statements.
  1.2022  */
  1.2023  #ifdef SQLITE_OMIT_TEMPDB
  1.2024 @@ -9807,7 +11336,7 @@
  1.2025  
  1.2026  /*
  1.2027  ** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
  1.2028 -** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it 
  1.2029 +** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
  1.2030  ** to zero.
  1.2031  */
  1.2032  #if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
  1.2033 @@ -9846,8 +11375,12 @@
  1.2034  /*
  1.2035  ** Macros to compute minimum and maximum of two numbers.
  1.2036  */
  1.2037 -#define MIN(A,B) ((A)<(B)?(A):(B))
  1.2038 -#define MAX(A,B) ((A)>(B)?(A):(B))
  1.2039 +#ifndef MIN
  1.2040 +# define MIN(A,B) ((A)<(B)?(A):(B))
  1.2041 +#endif
  1.2042 +#ifndef MAX
  1.2043 +# define MAX(A,B) ((A)>(B)?(A):(B))
  1.2044 +#endif
  1.2045  
  1.2046  /*
  1.2047  ** Swap two objects of type TYPE.
  1.2048 @@ -9955,7 +11488,7 @@
  1.2049  **      4 -> 20           1000 -> 99        1048576 -> 200
  1.2050  **     10 -> 33           1024 -> 100    4294967296 -> 320
  1.2051  **
  1.2052 -** The LogEst can be negative to indicate fractional values. 
  1.2053 +** The LogEst can be negative to indicate fractional values.
  1.2054  ** Examples:
  1.2055  **
  1.2056  **    0.5 -> -10           0.1 -> -33        0.0625 -> -40
  1.2057 @@ -9976,6 +11509,27 @@
  1.2058  # endif
  1.2059  #endif
  1.2060  
  1.2061 +/* The uptr type is an unsigned integer large enough to hold a pointer
  1.2062 +*/
  1.2063 +#if defined(HAVE_STDINT_H)
  1.2064 +  typedef uintptr_t uptr;
  1.2065 +#elif SQLITE_PTRSIZE==4
  1.2066 +  typedef u32 uptr;
  1.2067 +#else
  1.2068 +  typedef u64 uptr;
  1.2069 +#endif
  1.2070 +
  1.2071 +/*
  1.2072 +** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
  1.2073 +** something between S (inclusive) and E (exclusive).
  1.2074 +**
  1.2075 +** In other words, S is a buffer and E is a pointer to the first byte after
  1.2076 +** the end of buffer S.  This macro returns true if P points to something
  1.2077 +** contained within the buffer S.
  1.2078 +*/
  1.2079 +#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
  1.2080 +
  1.2081 +
  1.2082  /*
  1.2083  ** Macros to determine whether the machine is big or little endian,
  1.2084  ** and whether or not that determination is run-time or compile-time.
  1.2085 @@ -10021,7 +11575,7 @@
  1.2086  #define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
  1.2087  #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
  1.2088  
  1.2089 -/* 
  1.2090 +/*
  1.2091  ** Round up a number to the next larger multiple of 8.  This is used
  1.2092  ** to force 8-byte alignment on 64-bit architectures.
  1.2093  */
  1.2094 @@ -10115,7 +11669,7 @@
  1.2095  
  1.2096  /*
  1.2097  ** An instance of the following structure is used to store the busy-handler
  1.2098 -** callback for a given sqlite handle. 
  1.2099 +** callback for a given sqlite handle.
  1.2100  **
  1.2101  ** The sqlite.busyHandler member of the sqlite struct contains the busy
  1.2102  ** callback for the database handle. Each pager opened via the sqlite
  1.2103 @@ -10160,9 +11714,9 @@
  1.2104  
  1.2105  /*
  1.2106  ** The following value as a destructor means to use sqlite3DbFree().
  1.2107 -** The sqlite3DbFree() routine requires two parameters instead of the 
  1.2108 -** one parameter that destructors normally want.  So we have to introduce 
  1.2109 -** this magic value that the code knows to handle differently.  Any 
  1.2110 +** The sqlite3DbFree() routine requires two parameters instead of the
  1.2111 +** one parameter that destructors normally want.  So we have to introduce
  1.2112 +** this magic value that the code knows to handle differently.  Any
  1.2113  ** pointer will work here as long as it is distinct from SQLITE_STATIC
  1.2114  ** and SQLITE_TRANSIENT.
  1.2115  */
  1.2116 @@ -10189,16 +11743,16 @@
  1.2117  SQLITE_API int SQLITE_STDCALL sqlite3_wsd_init(int N, int J);
  1.2118  SQLITE_API void *SQLITE_STDCALL sqlite3_wsd_find(void *K, int L);
  1.2119  #else
  1.2120 -  #define SQLITE_WSD 
  1.2121 +  #define SQLITE_WSD
  1.2122    #define GLOBAL(t,v) v
  1.2123    #define sqlite3GlobalConfig sqlite3Config
  1.2124  #endif
  1.2125  
  1.2126  /*
  1.2127  ** The following macros are used to suppress compiler warnings and to
  1.2128 -** make it clear to human readers when a function parameter is deliberately 
  1.2129 +** make it clear to human readers when a function parameter is deliberately
  1.2130  ** left unused within the body of a function. This usually happens when
  1.2131 -** a function is called via a function pointer. For example the 
  1.2132 +** a function is called via a function pointer. For example the
  1.2133  ** implementation of an SQL aggregate step callback may not use the
  1.2134  ** parameter indicating the number of arguments passed to the aggregate,
  1.2135  ** if it knows that this is enforced elsewhere.
  1.2136 @@ -10241,6 +11795,7 @@
  1.2137  typedef struct Module Module;
  1.2138  typedef struct NameContext NameContext;
  1.2139  typedef struct Parse Parse;
  1.2140 +typedef struct PreUpdate PreUpdate;
  1.2141  typedef struct PrintfArguments PrintfArguments;
  1.2142  typedef struct RowSet RowSet;
  1.2143  typedef struct Savepoint Savepoint;
  1.2144 @@ -10264,7 +11819,7 @@
  1.2145  typedef struct With With;
  1.2146  
  1.2147  /*
  1.2148 -** Defer sourcing vdbe.h and btree.h until after the "u8" and 
  1.2149 +** Defer sourcing vdbe.h and btree.h until after the "u8" and
  1.2150  ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
  1.2151  ** pointer types (i.e. FuncDef) defined above.
  1.2152  */
  1.2153 @@ -10340,7 +11895,6 @@
  1.2154  SQLITE_PRIVATE   int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
  1.2155  #endif
  1.2156  SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
  1.2157 -SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
  1.2158  SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  1.2159  SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
  1.2160  SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
  1.2161 @@ -10654,7 +12208,7 @@
  1.2162  struct VdbeOp {
  1.2163    u8 opcode;          /* What operation to perform */
  1.2164    signed char p4type; /* One of the P4_xxx constants for p4 */
  1.2165 -  u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
  1.2166 +  u8 notUsed1;
  1.2167    u8 p5;              /* Fifth parameter is an unsigned character */
  1.2168    int p1;             /* First operand */
  1.2169    int p2;             /* Second parameter (often the jump destination) */
  1.2170 @@ -10673,6 +12227,7 @@
  1.2171      KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
  1.2172      int *ai;               /* Used when p4type is P4_INTARRAY */
  1.2173      SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
  1.2174 +    Table *pTab;           /* Used when p4type is P4_TABLE */
  1.2175  #ifdef SQLITE_ENABLE_CURSOR_HINTS
  1.2176      Expr *pExpr;           /* Used when p4type is P4_EXPR */
  1.2177  #endif
  1.2178 @@ -10737,7 +12292,8 @@
  1.2179  #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
  1.2180  #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
  1.2181  #define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
  1.2182 -#define P4_FUNCCTX  (-20) /* P4 is a pointer to an sqlite3_context object */
  1.2183 +#define P4_TABLE    (-20) /* P4 is a pointer to a Table structure */
  1.2184 +#define P4_FUNCCTX  (-21) /* P4 is a pointer to an sqlite3_context object */
  1.2185  
  1.2186  /* Error message codes for OP_Halt */
  1.2187  #define P5_ConstraintNotNull 1
  1.2188 @@ -10795,153 +12351,152 @@
  1.2189  #define OP_VUpdate        12 /* synopsis: data=r[P3@P2]                    */
  1.2190  #define OP_Goto           13
  1.2191  #define OP_Gosub          14
  1.2192 -#define OP_Return         15
  1.2193 -#define OP_InitCoroutine  16
  1.2194 -#define OP_EndCoroutine   17
  1.2195 -#define OP_Yield          18
  1.2196 +#define OP_InitCoroutine  15
  1.2197 +#define OP_Yield          16
  1.2198 +#define OP_MustBeInt      17
  1.2199 +#define OP_Jump           18
  1.2200  #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
  1.2201 -#define OP_HaltIfNull     20 /* synopsis: if r[P3]=null halt               */
  1.2202 -#define OP_Halt           21
  1.2203 -#define OP_Integer        22 /* synopsis: r[P2]=P1                         */
  1.2204 -#define OP_Int64          23 /* synopsis: r[P2]=P4                         */
  1.2205 -#define OP_String         24 /* synopsis: r[P2]='P4' (len=P1)              */
  1.2206 -#define OP_Null           25 /* synopsis: r[P2..P3]=NULL                   */
  1.2207 -#define OP_SoftNull       26 /* synopsis: r[P1]=NULL                       */
  1.2208 -#define OP_Blob           27 /* synopsis: r[P2]=P4 (len=P1)                */
  1.2209 -#define OP_Variable       28 /* synopsis: r[P2]=parameter(P1,P4)           */
  1.2210 -#define OP_Move           29 /* synopsis: r[P2@P3]=r[P1@P3]                */
  1.2211 -#define OP_Copy           30 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
  1.2212 -#define OP_SCopy          31 /* synopsis: r[P2]=r[P1]                      */
  1.2213 -#define OP_IntCopy        32 /* synopsis: r[P2]=r[P1]                      */
  1.2214 -#define OP_ResultRow      33 /* synopsis: output=r[P1@P2]                  */
  1.2215 -#define OP_CollSeq        34
  1.2216 -#define OP_Function0      35 /* synopsis: r[P3]=func(r[P2@P5])             */
  1.2217 -#define OP_Function       36 /* synopsis: r[P3]=func(r[P2@P5])             */
  1.2218 -#define OP_AddImm         37 /* synopsis: r[P1]=r[P1]+P2                   */
  1.2219 -#define OP_MustBeInt      38
  1.2220 -#define OP_RealAffinity   39
  1.2221 -#define OP_Cast           40 /* synopsis: affinity(r[P1])                  */
  1.2222 -#define OP_Permutation    41
  1.2223 -#define OP_Compare        42 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
  1.2224 -#define OP_Jump           43
  1.2225 -#define OP_Once           44
  1.2226 -#define OP_If             45
  1.2227 -#define OP_IfNot          46
  1.2228 -#define OP_Column         47 /* synopsis: r[P3]=PX                         */
  1.2229 -#define OP_Affinity       48 /* synopsis: affinity(r[P1@P2])               */
  1.2230 -#define OP_MakeRecord     49 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
  1.2231 -#define OP_Count          50 /* synopsis: r[P2]=count()                    */
  1.2232 -#define OP_ReadCookie     51
  1.2233 -#define OP_SetCookie      52
  1.2234 -#define OP_ReopenIdx      53 /* synopsis: root=P2 iDb=P3                   */
  1.2235 -#define OP_OpenRead       54 /* synopsis: root=P2 iDb=P3                   */
  1.2236 -#define OP_OpenWrite      55 /* synopsis: root=P2 iDb=P3                   */
  1.2237 -#define OP_OpenAutoindex  56 /* synopsis: nColumn=P2                       */
  1.2238 -#define OP_OpenEphemeral  57 /* synopsis: nColumn=P2                       */
  1.2239 -#define OP_SorterOpen     58
  1.2240 -#define OP_SequenceTest   59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
  1.2241 -#define OP_OpenPseudo     60 /* synopsis: P3 columns in r[P2]              */
  1.2242 -#define OP_Close          61
  1.2243 -#define OP_ColumnsUsed    62
  1.2244 -#define OP_SeekLT         63 /* synopsis: key=r[P3@P4]                     */
  1.2245 -#define OP_SeekLE         64 /* synopsis: key=r[P3@P4]                     */
  1.2246 -#define OP_SeekGE         65 /* synopsis: key=r[P3@P4]                     */
  1.2247 -#define OP_SeekGT         66 /* synopsis: key=r[P3@P4]                     */
  1.2248 -#define OP_NoConflict     67 /* synopsis: key=r[P3@P4]                     */
  1.2249 -#define OP_NotFound       68 /* synopsis: key=r[P3@P4]                     */
  1.2250 -#define OP_Found          69 /* synopsis: key=r[P3@P4]                     */
  1.2251 -#define OP_NotExists      70 /* synopsis: intkey=r[P3]                     */
  1.2252 -#define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  1.2253 -#define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  1.2254 -#define OP_Sequence       73 /* synopsis: r[P2]=cursor[P1].ctr++           */
  1.2255 -#define OP_NewRowid       74 /* synopsis: r[P2]=rowid                      */
  1.2256 -#define OP_Insert         75 /* synopsis: intkey=r[P3] data=r[P2]          */
  1.2257 -#define OP_IsNull         76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
  1.2258 -#define OP_NotNull        77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
  1.2259 -#define OP_Ne             78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
  1.2260 -#define OP_Eq             79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
  1.2261 -#define OP_Gt             80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
  1.2262 -#define OP_Le             81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
  1.2263 -#define OP_Lt             82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
  1.2264 -#define OP_Ge             83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
  1.2265 -#define OP_InsertInt      84 /* synopsis: intkey=P3 data=r[P2]             */
  1.2266 -#define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
  1.2267 -#define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
  1.2268 -#define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
  1.2269 -#define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
  1.2270 -#define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
  1.2271 -#define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
  1.2272 -#define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
  1.2273 -#define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
  1.2274 -#define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
  1.2275 -#define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
  1.2276 -#define OP_Delete         95
  1.2277 -#define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
  1.2278 +#define OP_Once           20
  1.2279 +#define OP_If             21
  1.2280 +#define OP_IfNot          22
  1.2281 +#define OP_SeekLT         23 /* synopsis: key=r[P3@P4]                     */
  1.2282 +#define OP_SeekLE         24 /* synopsis: key=r[P3@P4]                     */
  1.2283 +#define OP_SeekGE         25 /* synopsis: key=r[P3@P4]                     */
  1.2284 +#define OP_SeekGT         26 /* synopsis: key=r[P3@P4]                     */
  1.2285 +#define OP_Or             27 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  1.2286 +#define OP_And            28 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  1.2287 +#define OP_NoConflict     29 /* synopsis: key=r[P3@P4]                     */
  1.2288 +#define OP_NotFound       30 /* synopsis: key=r[P3@P4]                     */
  1.2289 +#define OP_Found          31 /* synopsis: key=r[P3@P4]                     */
  1.2290 +#define OP_NotExists      32 /* synopsis: intkey=r[P3]                     */
  1.2291 +#define OP_Last           33
  1.2292 +#define OP_IsNull         34 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
  1.2293 +#define OP_NotNull        35 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
  1.2294 +#define OP_Ne             36 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
  1.2295 +#define OP_Eq             37 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
  1.2296 +#define OP_Gt             38 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
  1.2297 +#define OP_Le             39 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
  1.2298 +#define OP_Lt             40 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
  1.2299 +#define OP_Ge             41 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
  1.2300 +#define OP_SorterSort     42
  1.2301 +#define OP_BitAnd         43 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
  1.2302 +#define OP_BitOr          44 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
  1.2303 +#define OP_ShiftLeft      45 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
  1.2304 +#define OP_ShiftRight     46 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
  1.2305 +#define OP_Add            47 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
  1.2306 +#define OP_Subtract       48 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
  1.2307 +#define OP_Multiply       49 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
  1.2308 +#define OP_Divide         50 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
  1.2309 +#define OP_Remainder      51 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
  1.2310 +#define OP_Concat         52 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
  1.2311 +#define OP_Sort           53
  1.2312 +#define OP_BitNot         54 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
  1.2313 +#define OP_Rewind         55
  1.2314 +#define OP_IdxLE          56 /* synopsis: key=r[P3@P4]                     */
  1.2315 +#define OP_IdxGT          57 /* synopsis: key=r[P3@P4]                     */
  1.2316 +#define OP_IdxLT          58 /* synopsis: key=r[P3@P4]                     */
  1.2317 +#define OP_IdxGE          59 /* synopsis: key=r[P3@P4]                     */
  1.2318 +#define OP_RowSetRead     60 /* synopsis: r[P3]=rowset(P1)                 */
  1.2319 +#define OP_RowSetTest     61 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  1.2320 +#define OP_Program        62
  1.2321 +#define OP_FkIfZero       63 /* synopsis: if fkctr[P1]==0 goto P2          */
  1.2322 +#define OP_IfPos          64 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
  1.2323 +#define OP_IfNotZero      65 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
  1.2324 +#define OP_DecrJumpZero   66 /* synopsis: if (--r[P1])==0 goto P2          */
  1.2325 +#define OP_IncrVacuum     67
  1.2326 +#define OP_VNext          68
  1.2327 +#define OP_Init           69 /* synopsis: Start at P2                      */
  1.2328 +#define OP_Return         70
  1.2329 +#define OP_EndCoroutine   71
  1.2330 +#define OP_HaltIfNull     72 /* synopsis: if r[P3]=null halt               */
  1.2331 +#define OP_Halt           73
  1.2332 +#define OP_Integer        74 /* synopsis: r[P2]=P1                         */
  1.2333 +#define OP_Int64          75 /* synopsis: r[P2]=P4                         */
  1.2334 +#define OP_String         76 /* synopsis: r[P2]='P4' (len=P1)              */
  1.2335 +#define OP_Null           77 /* synopsis: r[P2..P3]=NULL                   */
  1.2336 +#define OP_SoftNull       78 /* synopsis: r[P1]=NULL                       */
  1.2337 +#define OP_Blob           79 /* synopsis: r[P2]=P4 (len=P1)                */
  1.2338 +#define OP_Variable       80 /* synopsis: r[P2]=parameter(P1,P4)           */
  1.2339 +#define OP_Move           81 /* synopsis: r[P2@P3]=r[P1@P3]                */
  1.2340 +#define OP_Copy           82 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
  1.2341 +#define OP_SCopy          83 /* synopsis: r[P2]=r[P1]                      */
  1.2342 +#define OP_IntCopy        84 /* synopsis: r[P2]=r[P1]                      */
  1.2343 +#define OP_ResultRow      85 /* synopsis: output=r[P1@P2]                  */
  1.2344 +#define OP_CollSeq        86
  1.2345 +#define OP_Function0      87 /* synopsis: r[P3]=func(r[P2@P5])             */
  1.2346 +#define OP_Function       88 /* synopsis: r[P3]=func(r[P2@P5])             */
  1.2347 +#define OP_AddImm         89 /* synopsis: r[P1]=r[P1]+P2                   */
  1.2348 +#define OP_RealAffinity   90
  1.2349 +#define OP_Cast           91 /* synopsis: affinity(r[P1])                  */
  1.2350 +#define OP_Permutation    92
  1.2351 +#define OP_Compare        93 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
  1.2352 +#define OP_Column         94 /* synopsis: r[P3]=PX                         */
  1.2353 +#define OP_Affinity       95 /* synopsis: affinity(r[P1@P2])               */
  1.2354 +#define OP_MakeRecord     96 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
  1.2355  #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
  1.2356 -#define OP_ResetCount     98
  1.2357 -#define OP_SorterCompare  99 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
  1.2358 -#define OP_SorterData    100 /* synopsis: r[P2]=data                       */
  1.2359 -#define OP_RowKey        101 /* synopsis: r[P2]=key                        */
  1.2360 -#define OP_RowData       102 /* synopsis: r[P2]=data                       */
  1.2361 -#define OP_Rowid         103 /* synopsis: r[P2]=rowid                      */
  1.2362 -#define OP_NullRow       104
  1.2363 -#define OP_Last          105
  1.2364 -#define OP_SorterSort    106
  1.2365 -#define OP_Sort          107
  1.2366 -#define OP_Rewind        108
  1.2367 -#define OP_SorterInsert  109
  1.2368 -#define OP_IdxInsert     110 /* synopsis: key=r[P2]                        */
  1.2369 -#define OP_IdxDelete     111 /* synopsis: key=r[P2@P3]                     */
  1.2370 -#define OP_Seek          112 /* synopsis: Move P3 to P1.rowid              */
  1.2371 -#define OP_IdxRowid      113 /* synopsis: r[P2]=rowid                      */
  1.2372 -#define OP_IdxLE         114 /* synopsis: key=r[P3@P4]                     */
  1.2373 -#define OP_IdxGT         115 /* synopsis: key=r[P3@P4]                     */
  1.2374 -#define OP_IdxLT         116 /* synopsis: key=r[P3@P4]                     */
  1.2375 -#define OP_IdxGE         117 /* synopsis: key=r[P3@P4]                     */
  1.2376 -#define OP_Destroy       118
  1.2377 -#define OP_Clear         119
  1.2378 -#define OP_ResetSorter   120
  1.2379 -#define OP_CreateIndex   121 /* synopsis: r[P2]=root iDb=P1                */
  1.2380 -#define OP_CreateTable   122 /* synopsis: r[P2]=root iDb=P1                */
  1.2381 -#define OP_ParseSchema   123
  1.2382 -#define OP_LoadAnalysis  124
  1.2383 -#define OP_DropTable     125
  1.2384 -#define OP_DropIndex     126
  1.2385 -#define OP_DropTrigger   127
  1.2386 -#define OP_IntegrityCk   128
  1.2387 -#define OP_RowSetAdd     129 /* synopsis: rowset(P1)=r[P2]                 */
  1.2388 -#define OP_RowSetRead    130 /* synopsis: r[P3]=rowset(P1)                 */
  1.2389 -#define OP_RowSetTest    131 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  1.2390 -#define OP_Program       132
  1.2391 +#define OP_Count          98 /* synopsis: r[P2]=count()                    */
  1.2392 +#define OP_ReadCookie     99
  1.2393 +#define OP_SetCookie     100
  1.2394 +#define OP_ReopenIdx     101 /* synopsis: root=P2 iDb=P3                   */
  1.2395 +#define OP_OpenRead      102 /* synopsis: root=P2 iDb=P3                   */
  1.2396 +#define OP_OpenWrite     103 /* synopsis: root=P2 iDb=P3                   */
  1.2397 +#define OP_OpenAutoindex 104 /* synopsis: nColumn=P2                       */
  1.2398 +#define OP_OpenEphemeral 105 /* synopsis: nColumn=P2                       */
  1.2399 +#define OP_SorterOpen    106
  1.2400 +#define OP_SequenceTest  107 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
  1.2401 +#define OP_OpenPseudo    108 /* synopsis: P3 columns in r[P2]              */
  1.2402 +#define OP_Close         109
  1.2403 +#define OP_ColumnsUsed   110
  1.2404 +#define OP_Sequence      111 /* synopsis: r[P2]=cursor[P1].ctr++           */
  1.2405 +#define OP_NewRowid      112 /* synopsis: r[P2]=rowid                      */
  1.2406 +#define OP_Insert        113 /* synopsis: intkey=r[P3] data=r[P2]          */
  1.2407 +#define OP_InsertInt     114 /* synopsis: intkey=P3 data=r[P2]             */
  1.2408 +#define OP_Delete        115
  1.2409 +#define OP_ResetCount    116
  1.2410 +#define OP_SorterCompare 117 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
  1.2411 +#define OP_SorterData    118 /* synopsis: r[P2]=data                       */
  1.2412 +#define OP_RowKey        119 /* synopsis: r[P2]=key                        */
  1.2413 +#define OP_RowData       120 /* synopsis: r[P2]=data                       */
  1.2414 +#define OP_Rowid         121 /* synopsis: r[P2]=rowid                      */
  1.2415 +#define OP_NullRow       122
  1.2416 +#define OP_SorterInsert  123
  1.2417 +#define OP_IdxInsert     124 /* synopsis: key=r[P2]                        */
  1.2418 +#define OP_IdxDelete     125 /* synopsis: key=r[P2@P3]                     */
  1.2419 +#define OP_Seek          126 /* synopsis: Move P3 to P1.rowid              */
  1.2420 +#define OP_IdxRowid      127 /* synopsis: r[P2]=rowid                      */
  1.2421 +#define OP_Destroy       128
  1.2422 +#define OP_Clear         129
  1.2423 +#define OP_ResetSorter   130
  1.2424 +#define OP_CreateIndex   131 /* synopsis: r[P2]=root iDb=P1                */
  1.2425 +#define OP_CreateTable   132 /* synopsis: r[P2]=root iDb=P1                */
  1.2426  #define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
  1.2427 -#define OP_Param         134
  1.2428 -#define OP_FkCounter     135 /* synopsis: fkctr[P1]+=P2                    */
  1.2429 -#define OP_FkIfZero      136 /* synopsis: if fkctr[P1]==0 goto P2          */
  1.2430 -#define OP_MemMax        137 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  1.2431 -#define OP_IfPos         138 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
  1.2432 -#define OP_OffsetLimit   139 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
  1.2433 -#define OP_IfNotZero     140 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
  1.2434 -#define OP_DecrJumpZero  141 /* synopsis: if (--r[P1])==0 goto P2          */
  1.2435 -#define OP_JumpZeroIncr  142 /* synopsis: if (r[P1]++)==0 ) goto P2        */
  1.2436 -#define OP_AggStep0      143 /* synopsis: accum=r[P3] step(r[P2@P5])       */
  1.2437 -#define OP_AggStep       144 /* synopsis: accum=r[P3] step(r[P2@P5])       */
  1.2438 -#define OP_AggFinal      145 /* synopsis: accum=r[P1] N=P2                 */
  1.2439 -#define OP_IncrVacuum    146
  1.2440 -#define OP_Expire        147
  1.2441 -#define OP_TableLock     148 /* synopsis: iDb=P1 root=P2 write=P3          */
  1.2442 -#define OP_VBegin        149
  1.2443 -#define OP_VCreate       150
  1.2444 -#define OP_VDestroy      151
  1.2445 -#define OP_VOpen         152
  1.2446 -#define OP_VColumn       153 /* synopsis: r[P3]=vcolumn(P2)                */
  1.2447 -#define OP_VNext         154
  1.2448 +#define OP_ParseSchema   134
  1.2449 +#define OP_LoadAnalysis  135
  1.2450 +#define OP_DropTable     136
  1.2451 +#define OP_DropIndex     137
  1.2452 +#define OP_DropTrigger   138
  1.2453 +#define OP_IntegrityCk   139
  1.2454 +#define OP_RowSetAdd     140 /* synopsis: rowset(P1)=r[P2]                 */
  1.2455 +#define OP_Param         141
  1.2456 +#define OP_FkCounter     142 /* synopsis: fkctr[P1]+=P2                    */
  1.2457 +#define OP_MemMax        143 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  1.2458 +#define OP_OffsetLimit   144 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
  1.2459 +#define OP_AggStep0      145 /* synopsis: accum=r[P3] step(r[P2@P5])       */
  1.2460 +#define OP_AggStep       146 /* synopsis: accum=r[P3] step(r[P2@P5])       */
  1.2461 +#define OP_AggFinal      147 /* synopsis: accum=r[P1] N=P2                 */
  1.2462 +#define OP_Expire        148
  1.2463 +#define OP_TableLock     149 /* synopsis: iDb=P1 root=P2 write=P3          */
  1.2464 +#define OP_VBegin        150
  1.2465 +#define OP_VCreate       151
  1.2466 +#define OP_VDestroy      152
  1.2467 +#define OP_VOpen         153
  1.2468 +#define OP_VColumn       154 /* synopsis: r[P3]=vcolumn(P2)                */
  1.2469  #define OP_VRename       155
  1.2470  #define OP_Pagecount     156
  1.2471  #define OP_MaxPgcnt      157
  1.2472 -#define OP_Init          158 /* synopsis: Start at P2                      */
  1.2473 -#define OP_CursorHint    159
  1.2474 -#define OP_Noop          160
  1.2475 -#define OP_Explain       161
  1.2476 +#define OP_CursorHint    158
  1.2477 +#define OP_Noop          159
  1.2478 +#define OP_Explain       160
  1.2479  
  1.2480  /* Properties such as "out2" or "jump" that are specified in
  1.2481  ** comments following the "case" for each opcode in the vdbe.c
  1.2482 @@ -10955,26 +12510,34 @@
  1.2483  #define OPFLG_OUT3        0x20  /* out3:  P3 is an output */
  1.2484  #define OPFLG_INITIALIZER {\
  1.2485  /*   0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,\
  1.2486 -/*   8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x02,\
  1.2487 -/*  16 */ 0x01, 0x02, 0x03, 0x12, 0x08, 0x00, 0x10, 0x10,\
  1.2488 -/*  24 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
  1.2489 -/*  32 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x02,\
  1.2490 -/*  40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\
  1.2491 -/*  48 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
  1.2492 -/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,\
  1.2493 -/*  64 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x26,\
  1.2494 -/*  72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\
  1.2495 -/*  80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\
  1.2496 -/*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
  1.2497 -/*  96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
  1.2498 -/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\
  1.2499 -/* 112 */ 0x00, 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00,\
  1.2500 -/* 120 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
  1.2501 -/* 128 */ 0x00, 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00,\
  1.2502 -/* 136 */ 0x01, 0x04, 0x03, 0x1a, 0x03, 0x03, 0x03, 0x00,\
  1.2503 -/* 144 */ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\
  1.2504 -/* 152 */ 0x00, 0x00, 0x01, 0x00, 0x10, 0x10, 0x01, 0x00,\
  1.2505 -/* 160 */ 0x00, 0x00,}
  1.2506 +/*   8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01,\
  1.2507 +/*  16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x03, 0x03, 0x09,\
  1.2508 +/*  24 */ 0x09, 0x09, 0x09, 0x26, 0x26, 0x09, 0x09, 0x09,\
  1.2509 +/*  32 */ 0x09, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
  1.2510 +/*  40 */ 0x0b, 0x0b, 0x01, 0x26, 0x26, 0x26, 0x26, 0x26,\
  1.2511 +/*  48 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x01, 0x12, 0x01,\
  1.2512 +/*  56 */ 0x01, 0x01, 0x01, 0x01, 0x23, 0x0b, 0x01, 0x01,\
  1.2513 +/*  64 */ 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x02, 0x02,\
  1.2514 +/*  72 */ 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10,\
  1.2515 +/*  80 */ 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\
  1.2516 +/*  88 */ 0x00, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,\
  1.2517 +/*  96 */ 0x00, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
  1.2518 +/* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
  1.2519 +/* 112 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  1.2520 +/* 120 */ 0x00, 0x10, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,\
  1.2521 +/* 128 */ 0x10, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\
  1.2522 +/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
  1.2523 +/* 144 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  1.2524 +/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
  1.2525 +/* 160 */ 0x00,}
  1.2526 +
  1.2527 +/* The sqlite3P2Values() routine is able to run faster if it knows
  1.2528 +** the value of the largest JUMP opcode.  The smaller the maximum
  1.2529 +** JUMP opcode the better, so the mkopcodeh.tcl script that
  1.2530 +** generated this include file strives to group all JUMP opcodes
  1.2531 +** together near the beginning of the list.
  1.2532 +*/
  1.2533 +#define SQLITE_MX_JUMP_OPCODE  69  /* Maximum JUMP opcode */
  1.2534  
  1.2535  /************** End of opcodes.h *********************************************/
  1.2536  /************** Continuing where we left off in vdbe.h ***********************/
  1.2537 @@ -11016,6 +12579,7 @@
  1.2538  SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
  1.2539  SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
  1.2540  SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
  1.2541 +SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
  1.2542  SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
  1.2543  SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
  1.2544  SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
  1.2545 @@ -11196,7 +12760,11 @@
  1.2546  #define PAGER_LOCKINGMODE_EXCLUSIVE   1
  1.2547  
  1.2548  /*
  1.2549 -** Numeric constants that encode the journalmode.  
  1.2550 +** Numeric constants that encode the journalmode.
  1.2551 +**
  1.2552 +** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
  1.2553 +** are exposed in the API via the "PRAGMA journal_mode" command and
  1.2554 +** therefore cannot be changed without a compatibility break.
  1.2555  */
  1.2556  #define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
  1.2557  #define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
  1.2558 @@ -11214,6 +12782,11 @@
  1.2559  
  1.2560  /*
  1.2561  ** Flags for sqlite3PagerSetFlags()
  1.2562 +**
  1.2563 +** Value constraints (enforced via assert()):
  1.2564 +**    PAGER_FULLFSYNC      == SQLITE_FullFSync
  1.2565 +**    PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
  1.2566 +**    PAGER_CACHE_SPILL    == SQLITE_CacheSpill
  1.2567  */
  1.2568  #define PAGER_SYNCHRONOUS_OFF       0x01  /* PRAGMA synchronous=OFF */
  1.2569  #define PAGER_SYNCHRONOUS_NORMAL    0x02  /* PRAGMA synchronous=NORMAL */
  1.2570 @@ -11319,7 +12892,6 @@
  1.2571  SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
  1.2572  SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
  1.2573  SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
  1.2574 -SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
  1.2575  SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
  1.2576  SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
  1.2577  SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
  1.2578 @@ -11384,7 +12956,7 @@
  1.2579    sqlite3_pcache_page *pPage;    /* Pcache object page handle */
  1.2580    void *pData;                   /* Page data */
  1.2581    void *pExtra;                  /* Extra content */
  1.2582 -  PgHdr *pDirty;                 /* Transient list of dirty pages */
  1.2583 +  PgHdr *pDirty;                 /* Transient list of dirty sorted by pgno */
  1.2584    Pager *pPager;                 /* The pager this page is part of */
  1.2585    Pgno pgno;                     /* Page number for this page */
  1.2586  #ifdef SQLITE_CHECK_PAGES
  1.2587 @@ -11409,11 +12981,10 @@
  1.2588  #define PGHDR_WRITEABLE       0x004  /* Journaled and ready to modify */
  1.2589  #define PGHDR_NEED_SYNC       0x008  /* Fsync the rollback journal before
  1.2590                                       ** writing this page to the database */
  1.2591 -#define PGHDR_NEED_READ       0x010  /* Content is unread */
  1.2592 -#define PGHDR_DONT_WRITE      0x020  /* Do not write content to disk */
  1.2593 -#define PGHDR_MMAP            0x040  /* This is an mmap page object */
  1.2594 -
  1.2595 -#define PGHDR_WAL_APPEND      0x080  /* Appended to wal file */
  1.2596 +#define PGHDR_DONT_WRITE      0x010  /* Do not write content to disk */
  1.2597 +#define PGHDR_MMAP            0x020  /* This is an mmap page object */
  1.2598 +
  1.2599 +#define PGHDR_WAL_APPEND      0x040  /* Appended to wal file */
  1.2600  
  1.2601  /* Initialize and shutdown the page cache subsystem */
  1.2602  SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
  1.2603 @@ -11457,6 +13028,7 @@
  1.2604  SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
  1.2605  SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
  1.2606  SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
  1.2607 +SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache*);
  1.2608  
  1.2609  /* Change a page number.  Used by incr-vacuum. */
  1.2610  SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
  1.2611 @@ -11495,6 +13067,11 @@
  1.2612  SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
  1.2613  #endif
  1.2614  
  1.2615 +#if defined(SQLITE_DEBUG)
  1.2616 +/* Check invariants on a PgHdr object */
  1.2617 +SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*);
  1.2618 +#endif
  1.2619 +
  1.2620  /* Set and get the suggested cache-size for the specified pager-cache.
  1.2621  **
  1.2622  ** If no global maximum is configured, then the system attempts to limit
  1.2623 @@ -11531,11 +13108,13 @@
  1.2624  SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
  1.2625  SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
  1.2626  
  1.2627 +/* Number of dirty pages as a percentage of the configured cache size */
  1.2628 +SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
  1.2629 +
  1.2630  #endif /* _PCACHE_H_ */
  1.2631  
  1.2632  /************** End of pcache.h **********************************************/
  1.2633  /************** Continuing where we left off in sqliteInt.h ******************/
  1.2634 -
  1.2635  /************** Include os.h in the middle of sqliteInt.h ********************/
  1.2636  /************** Begin file os.h **********************************************/
  1.2637  /*
  1.2638 @@ -11761,7 +13340,7 @@
  1.2639  /* 
  1.2640  ** Functions for accessing sqlite3_file methods 
  1.2641  */
  1.2642 -SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
  1.2643 +SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
  1.2644  SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
  1.2645  SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
  1.2646  SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
  1.2647 @@ -11798,6 +13377,7 @@
  1.2648  #endif /* SQLITE_OMIT_LOAD_EXTENSION */
  1.2649  SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
  1.2650  SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
  1.2651 +SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
  1.2652  SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
  1.2653  
  1.2654  /*
  1.2655 @@ -11805,7 +13385,7 @@
  1.2656  ** sqlite3_malloc() to obtain space for the file-handle structure.
  1.2657  */
  1.2658  SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
  1.2659 -SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
  1.2660 +SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
  1.2661  
  1.2662  #endif /* _SQLITE_OS_H_ */
  1.2663  
  1.2664 @@ -11887,6 +13467,36 @@
  1.2665  /************** End of mutex.h ***********************************************/
  1.2666  /************** Continuing where we left off in sqliteInt.h ******************/
  1.2667  
  1.2668 +/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
  1.2669 +** synchronous setting to EXTRA.  It is no longer supported.
  1.2670 +*/
  1.2671 +#ifdef SQLITE_EXTRA_DURABLE
  1.2672 +# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
  1.2673 +# define SQLITE_DEFAULT_SYNCHRONOUS 3
  1.2674 +#endif
  1.2675 +
  1.2676 +/*
  1.2677 +** Default synchronous levels.
  1.2678 +**
  1.2679 +** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
  1.2680 +** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
  1.2681 +**
  1.2682 +**           PAGER_SYNCHRONOUS       DEFAULT_SYNCHRONOUS
  1.2683 +**   OFF           1                         0
  1.2684 +**   NORMAL        2                         1
  1.2685 +**   FULL          3                         2
  1.2686 +**   EXTRA         4                         3
  1.2687 +**
  1.2688 +** The "PRAGMA synchronous" statement also uses the zero-based numbers.
  1.2689 +** In other words, the zero-based numbers are used for all external interfaces
  1.2690 +** and the one-based values are used internally.
  1.2691 +*/
  1.2692 +#ifndef SQLITE_DEFAULT_SYNCHRONOUS
  1.2693 +# define SQLITE_DEFAULT_SYNCHRONOUS (PAGER_SYNCHRONOUS_FULL-1)
  1.2694 +#endif
  1.2695 +#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
  1.2696 +# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
  1.2697 +#endif
  1.2698  
  1.2699  /*
  1.2700  ** Each database file to be accessed by the system is an instance
  1.2701 @@ -11899,6 +13509,7 @@
  1.2702    char *zName;         /* Name of this database */
  1.2703    Btree *pBt;          /* The B*Tree structure for this database file */
  1.2704    u8 safety_level;     /* How aggressive at syncing data to disk */
  1.2705 +  u8 bSyncSet;         /* True if "PRAGMA synchronous=N" has been run */
  1.2706    Schema *pSchema;     /* Pointer to database schema (possibly shared) */
  1.2707  };
  1.2708  
  1.2709 @@ -11909,7 +13520,7 @@
  1.2710  ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
  1.2711  ** In shared cache mode, a single Schema object can be shared by multiple
  1.2712  ** Btrees that refer to the same underlying BtShared object.
  1.2713 -** 
  1.2714 +**
  1.2715  ** Schema objects are automatically deallocated when the last Btree that
  1.2716  ** references them is destroyed.   The TEMP Schema is manually freed by
  1.2717  ** sqlite3_close().
  1.2718 @@ -11934,7 +13545,7 @@
  1.2719  };
  1.2720  
  1.2721  /*
  1.2722 -** These macros can be used to test, set, or clear bits in the 
  1.2723 +** These macros can be used to test, set, or clear bits in the
  1.2724  ** Db.pSchema->flags field.
  1.2725  */
  1.2726  #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
  1.2727 @@ -11998,13 +13609,15 @@
  1.2728  };
  1.2729  
  1.2730  /*
  1.2731 -** A hash table for function definitions.
  1.2732 +** A hash table for built-in function definitions.  (Application-defined
  1.2733 +** functions use a regular table table from hash.h.)
  1.2734  **
  1.2735  ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
  1.2736 -** Collisions are on the FuncDef.pHash chain.
  1.2737 -*/
  1.2738 +** Collisions are on the FuncDef.u.pHash chain.
  1.2739 +*/
  1.2740 +#define SQLITE_FUNC_HASH_SZ 23
  1.2741  struct FuncDefHash {
  1.2742 -  FuncDef *a[23];       /* Hash table for functions */
  1.2743 +  FuncDef *a[SQLITE_FUNC_HASH_SZ];       /* Hash table for functions */
  1.2744  };
  1.2745  
  1.2746  #ifdef SQLITE_USER_AUTHENTICATION
  1.2747 @@ -12062,6 +13675,7 @@
  1.2748    unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
  1.2749    int errCode;                  /* Most recent error code (SQLITE_*) */
  1.2750    int errMask;                  /* & result codes with this before returning */
  1.2751 +  int iSysErrno;                /* Errno value from last system error */
  1.2752    u16 dbOptFlags;               /* Flags to enable/disable optimizations */
  1.2753    u8 enc;                       /* Text encoding */
  1.2754    u8 autoCommit;                /* The auto-commit flag. */
  1.2755 @@ -12097,12 +13711,19 @@
  1.2756    void *pTraceArg;                          /* Argument to the trace function */
  1.2757    void (*xProfile)(void*,const char*,u64);  /* Profiling function */
  1.2758    void *pProfileArg;                        /* Argument to profile function */
  1.2759 -  void *pCommitArg;                 /* Argument to xCommitCallback() */   
  1.2760 +  void *pCommitArg;                 /* Argument to xCommitCallback() */
  1.2761    int (*xCommitCallback)(void*);    /* Invoked at every commit. */
  1.2762 -  void *pRollbackArg;               /* Argument to xRollbackCallback() */   
  1.2763 +  void *pRollbackArg;               /* Argument to xRollbackCallback() */
  1.2764    void (*xRollbackCallback)(void*); /* Invoked at every commit. */
  1.2765    void *pUpdateArg;
  1.2766    void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
  1.2767 +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  1.2768 +  void *pPreUpdateArg;          /* First argument to xPreUpdateCallback */
  1.2769 +  void (*xPreUpdateCallback)(   /* Registered using sqlite3_preupdate_hook() */
  1.2770 +    void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
  1.2771 +  );
  1.2772 +  PreUpdate *pPreUpdate;        /* Context for active pre-update callback */
  1.2773 +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
  1.2774  #ifndef SQLITE_OMIT_WAL
  1.2775    int (*xWalCallback)(void *, sqlite3 *, const char *, int);
  1.2776    void *pWalArg;
  1.2777 @@ -12132,7 +13753,7 @@
  1.2778    VTable **aVTrans;             /* Virtual tables with open transactions */
  1.2779    VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
  1.2780  #endif
  1.2781 -  FuncDefHash aFunc;            /* Hash table of connection functions */
  1.2782 +  Hash aFunc;                   /* Hash table of connection functions */
  1.2783    Hash aCollSeq;                /* All collating sequences */
  1.2784    BusyHandler busyHandler;      /* Busy callback */
  1.2785    Db aDbStatic[2];              /* Static space for the 2 default backends */
  1.2786 @@ -12144,8 +13765,8 @@
  1.2787    i64 nDeferredImmCons;         /* Net deferred immediate constraints */
  1.2788    int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
  1.2789  #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  1.2790 -  /* The following variables are all protected by the STATIC_MASTER 
  1.2791 -  ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
  1.2792 +  /* The following variables are all protected by the STATIC_MASTER
  1.2793 +  ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
  1.2794    **
  1.2795    ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
  1.2796    ** unlock so that it can proceed.
  1.2797 @@ -12173,6 +13794,11 @@
  1.2798  
  1.2799  /*
  1.2800  ** Possible values for the sqlite3.flags.
  1.2801 +**
  1.2802 +** Value constraints (enforced via assert()):
  1.2803 +**      SQLITE_FullFSync     == PAGER_FULLFSYNC
  1.2804 +**      SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
  1.2805 +**      SQLITE_CacheSpill    == PAGER_CACHE_SPILL
  1.2806  */
  1.2807  #define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
  1.2808  #define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
  1.2809 @@ -12200,12 +13826,14 @@
  1.2810  #define SQLITE_AutoIndex      0x00100000  /* Enable automatic indexes */
  1.2811  #define SQLITE_PreferBuiltin  0x00200000  /* Preference to built-in funcs */
  1.2812  #define SQLITE_LoadExtension  0x00400000  /* Enable load_extension */
  1.2813 -#define SQLITE_EnableTrigger  0x00800000  /* True to enable triggers */
  1.2814 -#define SQLITE_DeferFKs       0x01000000  /* Defer all FK constraints */
  1.2815 -#define SQLITE_QueryOnly      0x02000000  /* Disable database changes */
  1.2816 -#define SQLITE_VdbeEQP        0x04000000  /* Debug EXPLAIN QUERY PLAN */
  1.2817 -#define SQLITE_Vacuum         0x08000000  /* Currently in a VACUUM */
  1.2818 -#define SQLITE_CellSizeCk     0x10000000  /* Check btree cell sizes on load */
  1.2819 +#define SQLITE_LoadExtFunc    0x00800000  /* Enable load_extension() SQL func */
  1.2820 +#define SQLITE_EnableTrigger  0x01000000  /* True to enable triggers */
  1.2821 +#define SQLITE_DeferFKs       0x02000000  /* Defer all FK constraints */
  1.2822 +#define SQLITE_QueryOnly      0x04000000  /* Disable database changes */
  1.2823 +#define SQLITE_VdbeEQP        0x08000000  /* Debug EXPLAIN QUERY PLAN */
  1.2824 +#define SQLITE_Vacuum         0x10000000  /* Currently in a VACUUM */
  1.2825 +#define SQLITE_CellSizeCk     0x20000000  /* Check btree cell sizes on load */
  1.2826 +#define SQLITE_Fts3Tokenizer  0x40000000  /* Enable fts3_tokenizer(2) */
  1.2827  
  1.2828  
  1.2829  /*
  1.2830 @@ -12259,27 +13887,33 @@
  1.2831  
  1.2832  /*
  1.2833  ** Each SQL function is defined by an instance of the following
  1.2834 -** structure.  A pointer to this structure is stored in the sqlite.aFunc
  1.2835 -** hash table.  When multiple functions have the same name, the hash table
  1.2836 -** points to a linked list of these structures.
  1.2837 +** structure.  For global built-in functions (ex: substr(), max(), count())
  1.2838 +** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
  1.2839 +** For per-connection application-defined functions, a pointer to this
  1.2840 +** structure is held in the db->aHash hash table.
  1.2841 +**
  1.2842 +** The u.pHash field is used by the global built-ins.  The u.pDestructor
  1.2843 +** field is used by per-connection app-def functions.
  1.2844  */
  1.2845  struct FuncDef {
  1.2846 -  i16 nArg;            /* Number of arguments.  -1 means unlimited */
  1.2847 +  i8 nArg;             /* Number of arguments.  -1 means unlimited */
  1.2848    u16 funcFlags;       /* Some combination of SQLITE_FUNC_* */
  1.2849    void *pUserData;     /* User data parameter */
  1.2850    FuncDef *pNext;      /* Next function with same name */
  1.2851    void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
  1.2852    void (*xFinalize)(sqlite3_context*);                  /* Agg finalizer */
  1.2853 -  char *zName;         /* SQL name of the function. */
  1.2854 -  FuncDef *pHash;      /* Next with a different name but the same hash */
  1.2855 -  FuncDestructor *pDestructor;   /* Reference counted destructor function */
  1.2856 +  const char *zName;   /* SQL name of the function. */
  1.2857 +  union {
  1.2858 +    FuncDef *pHash;      /* Next with a different name but the same hash */
  1.2859 +    FuncDestructor *pDestructor;   /* Reference counted destructor function */
  1.2860 +  } u;
  1.2861  };
  1.2862  
  1.2863  /*
  1.2864  ** This structure encapsulates a user-function destructor callback (as
  1.2865  ** configured using create_function_v2()) and a reference counter. When
  1.2866  ** create_function_v2() is called to create a function with a destructor,
  1.2867 -** a single object of this type is allocated. FuncDestructor.nRef is set to 
  1.2868 +** a single object of this type is allocated. FuncDestructor.nRef is set to
  1.2869  ** the number of FuncDef objects created (either 1 or 3, depending on whether
  1.2870  ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
  1.2871  ** member of each of the new FuncDef objects is set to point to the allocated
  1.2872 @@ -12300,6 +13934,13 @@
  1.2873  ** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  And
  1.2874  ** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC.  There
  1.2875  ** are assert() statements in the code to verify this.
  1.2876 +**
  1.2877 +** Value constraints (enforced via assert()):
  1.2878 +**     SQLITE_FUNC_MINMAX    ==  NC_MinMaxAgg      == SF_MinMaxAgg
  1.2879 +**     SQLITE_FUNC_LENGTH    ==  OPFLAG_LENGTHARG
  1.2880 +**     SQLITE_FUNC_TYPEOF    ==  OPFLAG_TYPEOFARG
  1.2881 +**     SQLITE_FUNC_CONSTANT  ==  SQLITE_DETERMINISTIC from the API
  1.2882 +**     SQLITE_FUNC_ENCMASK   depends on SQLITE_UTF* macros in the API
  1.2883  */
  1.2884  #define SQLITE_FUNC_ENCMASK  0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
  1.2885  #define SQLITE_FUNC_LIKE     0x0004 /* Candidate for the LIKE optimization */
  1.2886 @@ -12321,10 +13962,10 @@
  1.2887  ** used to create the initializers for the FuncDef structures.
  1.2888  **
  1.2889  **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
  1.2890 -**     Used to create a scalar function definition of a function zName 
  1.2891 +**     Used to create a scalar function definition of a function zName
  1.2892  **     implemented by C function xFunc that accepts nArg arguments. The
  1.2893  **     value passed as iArg is cast to a (void*) and made available
  1.2894 -**     as the user-data (sqlite3_user_data()) for the function. If 
  1.2895 +**     as the user-data (sqlite3_user_data()) for the function. If
  1.2896  **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
  1.2897  **
  1.2898  **   VFUNCTION(zName, nArg, iArg, bNC, xFunc)
  1.2899 @@ -12343,8 +13984,8 @@
  1.2900  **     FUNCTION().
  1.2901  **
  1.2902  **   LIKEFUNC(zName, nArg, pArg, flags)
  1.2903 -**     Used to create a scalar function definition of a function zName 
  1.2904 -**     that accepts nArg arguments and is implemented by a call to C 
  1.2905 +**     Used to create a scalar function definition of a function zName
  1.2906 +**     that accepts nArg arguments and is implemented by a call to C
  1.2907  **     function likeFunc. Argument pArg is cast to a (void *) and made
  1.2908  **     available as the function user-data (sqlite3_user_data()). The
  1.2909  **     FuncDef.flags variable is set to the value passed as the flags
  1.2910 @@ -12352,28 +13993,28 @@
  1.2911  */
  1.2912  #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
  1.2913    {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  1.2914 -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
  1.2915 +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} }
  1.2916  #define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
  1.2917    {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  1.2918 -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
  1.2919 +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} }
  1.2920  #define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
  1.2921    {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  1.2922 -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
  1.2923 +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} }
  1.2924  #define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
  1.2925    {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
  1.2926 -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
  1.2927 +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} }
  1.2928  #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
  1.2929    {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  1.2930 -   pArg, 0, xFunc, 0, #zName, 0, 0}
  1.2931 +   pArg, 0, xFunc, 0, #zName, }
  1.2932  #define LIKEFUNC(zName, nArg, arg, flags) \
  1.2933    {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
  1.2934 -   (void *)arg, 0, likeFunc, 0, #zName, 0, 0}
  1.2935 +   (void *)arg, 0, likeFunc, 0, #zName, {0} }
  1.2936  #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
  1.2937    {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
  1.2938 -   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName,0,0}
  1.2939 +   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName, {0}}
  1.2940  #define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \
  1.2941    {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
  1.2942 -   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName,0,0}
  1.2943 +   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName, {0}}
  1.2944  
  1.2945  /*
  1.2946  ** All current savepoints are stored in a linked list starting at
  1.2947 @@ -12415,10 +14056,8 @@
  1.2948  ** of this structure.
  1.2949  */
  1.2950  struct Column {
  1.2951 -  char *zName;     /* Name of this column */
  1.2952 +  char *zName;     /* Name of this column, \000, then the type */
  1.2953    Expr *pDflt;     /* Default value of this column */
  1.2954 -  char *zDflt;     /* Original text of the default value */
  1.2955 -  char *zType;     /* Data type for this column */
  1.2956    char *zColl;     /* Collating sequence.  If NULL, use the default */
  1.2957    u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
  1.2958    char affinity;   /* One of the SQLITE_AFF_... values */
  1.2959 @@ -12430,6 +14069,7 @@
  1.2960  */
  1.2961  #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
  1.2962  #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
  1.2963 +#define COLFLAG_HASTYPE  0x0004    /* Type name follows column name */
  1.2964  
  1.2965  /*
  1.2966  ** A "Collating Sequence" is defined by an instance of the following
  1.2967 @@ -12460,7 +14100,7 @@
  1.2968  **
  1.2969  ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
  1.2970  ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
  1.2971 -** the speed a little by numbering the values consecutively.  
  1.2972 +** the speed a little by numbering the values consecutively.
  1.2973  **
  1.2974  ** But rather than start with 0 or 1, we begin with 'A'.  That way,
  1.2975  ** when multiple affinity types are concatenated into a string and
  1.2976 @@ -12479,7 +14119,7 @@
  1.2977  
  1.2978  /*
  1.2979  ** The SQLITE_AFF_MASK values masks off the significant bits of an
  1.2980 -** affinity value. 
  1.2981 +** affinity value.
  1.2982  */
  1.2983  #define SQLITE_AFF_MASK     0x47
  1.2984  
  1.2985 @@ -12499,20 +14139,20 @@
  1.2986  
  1.2987  /*
  1.2988  ** An object of this type is created for each virtual table present in
  1.2989 -** the database schema. 
  1.2990 +** the database schema.
  1.2991  **
  1.2992  ** If the database schema is shared, then there is one instance of this
  1.2993  ** structure for each database connection (sqlite3*) that uses the shared
  1.2994  ** schema. This is because each database connection requires its own unique
  1.2995 -** instance of the sqlite3_vtab* handle used to access the virtual table 
  1.2996 -** implementation. sqlite3_vtab* handles can not be shared between 
  1.2997 -** database connections, even when the rest of the in-memory database 
  1.2998 +** instance of the sqlite3_vtab* handle used to access the virtual table
  1.2999 +** implementation. sqlite3_vtab* handles can not be shared between
  1.3000 +** database connections, even when the rest of the in-memory database
  1.3001  ** schema is shared, as the implementation often stores the database
  1.3002  ** connection handle passed to it via the xConnect() or xCreate() method
  1.3003  ** during initialization internally. This database connection handle may
  1.3004 -** then be used by the virtual table implementation to access real tables 
  1.3005 -** within the database. So that they appear as part of the callers 
  1.3006 -** transaction, these accesses need to be made via the same database 
  1.3007 +** then be used by the virtual table implementation to access real tables
  1.3008 +** within the database. So that they appear as part of the callers
  1.3009 +** transaction, these accesses need to be made via the same database
  1.3010  ** connection as that used to execute SQL operations on the virtual table.
  1.3011  **
  1.3012  ** All VTable objects that correspond to a single table in a shared
  1.3013 @@ -12524,19 +14164,19 @@
  1.3014  ** sqlite3_vtab* handle in the compiled query.
  1.3015  **
  1.3016  ** When an in-memory Table object is deleted (for example when the
  1.3017 -** schema is being reloaded for some reason), the VTable objects are not 
  1.3018 -** deleted and the sqlite3_vtab* handles are not xDisconnect()ed 
  1.3019 +** schema is being reloaded for some reason), the VTable objects are not
  1.3020 +** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
  1.3021  ** immediately. Instead, they are moved from the Table.pVTable list to
  1.3022  ** another linked list headed by the sqlite3.pDisconnect member of the
  1.3023 -** corresponding sqlite3 structure. They are then deleted/xDisconnected 
  1.3024 +** corresponding sqlite3 structure. They are then deleted/xDisconnected
  1.3025  ** next time a statement is prepared using said sqlite3*. This is done
  1.3026  ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
  1.3027  ** Refer to comments above function sqlite3VtabUnlockList() for an
  1.3028  ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
  1.3029  ** list without holding the corresponding sqlite3.mutex mutex.
  1.3030  **
  1.3031 -** The memory for objects of this type is always allocated by 
  1.3032 -** sqlite3DbMalloc(), using the connection handle stored in VTable.db as 
  1.3033 +** The memory for objects of this type is always allocated by
  1.3034 +** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
  1.3035  ** the first argument.
  1.3036  */
  1.3037  struct VTable {
  1.3038 @@ -12704,7 +14344,7 @@
  1.3039  ** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
  1.3040  ** referenced table row is propagated into the row that holds the
  1.3041  ** foreign key.
  1.3042 -** 
  1.3043 +**
  1.3044  ** The following symbolic values are used to record which type
  1.3045  ** of action to take.
  1.3046  */
  1.3047 @@ -12725,7 +14365,7 @@
  1.3048  
  1.3049  /*
  1.3050  ** An instance of the following structure is passed as the first
  1.3051 -** argument to sqlite3VdbeKeyCompare and is used to control the 
  1.3052 +** argument to sqlite3VdbeKeyCompare and is used to control the
  1.3053  ** comparison of the two index keys.
  1.3054  **
  1.3055  ** Note that aSortOrder[] and aColl[] have nField+1 slots.  There
  1.3056 @@ -12766,7 +14406,7 @@
  1.3057  ** The key comparison functions actually return default_rc when they find
  1.3058  ** an equals comparison.  default_rc can be -1, 0, or +1.  If there are
  1.3059  ** multiple entries in the b-tree with the same key (when only looking
  1.3060 -** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to 
  1.3061 +** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
  1.3062  ** cause the search to find the last match, or +1 to cause the search to
  1.3063  ** find the first match.
  1.3064  **
  1.3065 @@ -12803,7 +14443,7 @@
  1.3066  ** In the Table structure describing Ex1, nCol==3 because there are
  1.3067  ** three columns in the table.  In the Index structure describing
  1.3068  ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
  1.3069 -** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the 
  1.3070 +** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the
  1.3071  ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
  1.3072  ** The second column to be indexed (c1) has an index of 0 in
  1.3073  ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
  1.3074 @@ -12811,7 +14451,7 @@
  1.3075  ** The Index.onError field determines whether or not the indexed columns
  1.3076  ** must be unique and what to do if they are not.  When Index.onError=OE_None,
  1.3077  ** it means this is not a unique index.  Otherwise it is a unique index
  1.3078 -** and the value of Index.onError indicate the which conflict resolution 
  1.3079 +** and the value of Index.onError indicate the which conflict resolution
  1.3080  ** algorithm to employ whenever an attempt is made to insert a non-unique
  1.3081  ** element.
  1.3082  **
  1.3083 @@ -12876,7 +14516,7 @@
  1.3084  #define XN_EXPR      (-2)     /* Indexed column is an expression */
  1.3085  
  1.3086  /*
  1.3087 -** Each sample stored in the sqlite_stat3 table is represented in memory 
  1.3088 +** Each sample stored in the sqlite_stat3 table is represented in memory
  1.3089  ** using a structure of this type.  See documentation at the top of the
  1.3090  ** analyze.c source file for additional information.
  1.3091  */
  1.3092 @@ -12971,9 +14611,9 @@
  1.3093  ** to represent the greater-than-or-equal-to operator in the expression
  1.3094  ** tree.
  1.3095  **
  1.3096 -** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, 
  1.3097 +** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
  1.3098  ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
  1.3099 -** the expression is a variable (TK_VARIABLE), then Expr.token contains the 
  1.3100 +** the expression is a variable (TK_VARIABLE), then Expr.token contains the
  1.3101  ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
  1.3102  ** then Expr.token contains the name of the function.
  1.3103  **
  1.3104 @@ -12984,7 +14624,7 @@
  1.3105  ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
  1.3106  ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
  1.3107  ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
  1.3108 -** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is 
  1.3109 +** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
  1.3110  ** valid.
  1.3111  **
  1.3112  ** An expression of the form ID or ID.ID refers to a column in a table.
  1.3113 @@ -12995,8 +14635,8 @@
  1.3114  ** value is also stored in the Expr.iAgg column in the aggregate so that
  1.3115  ** it can be accessed after all aggregates are computed.
  1.3116  **
  1.3117 -** If the expression is an unbound variable marker (a question mark 
  1.3118 -** character '?' in the original SQL) then the Expr.iTable holds the index 
  1.3119 +** If the expression is an unbound variable marker (a question mark
  1.3120 +** character '?' in the original SQL) then the Expr.iTable holds the index
  1.3121  ** number for that variable.
  1.3122  **
  1.3123  ** If the expression is a subquery then Expr.iColumn holds an integer
  1.3124 @@ -13035,7 +14675,7 @@
  1.3125  
  1.3126    /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
  1.3127    ** space is allocated for the fields below this point. An attempt to
  1.3128 -  ** access them will result in a segfault or malfunction. 
  1.3129 +  ** access them will result in a segfault or malfunction.
  1.3130    *********************************************************************/
  1.3131  
  1.3132    Expr *pLeft;           /* Left subnode */
  1.3133 @@ -13101,7 +14741,7 @@
  1.3134  #define EP_Propagate (EP_Collate|EP_Subquery) /* Propagate these bits up tree */
  1.3135  
  1.3136  /*
  1.3137 -** These macros can be used to test, set, or clear bits in the 
  1.3138 +** These macros can be used to test, set, or clear bits in the
  1.3139  ** Expr.flags field.
  1.3140  */
  1.3141  #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
  1.3142 @@ -13120,8 +14760,8 @@
  1.3143  #endif
  1.3144  
  1.3145  /*
  1.3146 -** Macros to determine the number of bytes required by a normal Expr 
  1.3147 -** struct, an Expr struct with the EP_Reduced flag set in Expr.flags 
  1.3148 +** Macros to determine the number of bytes required by a normal Expr
  1.3149 +** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
  1.3150  ** and an Expr struct with the EP_TokenOnly flag set.
  1.3151  */
  1.3152  #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
  1.3153 @@ -13129,7 +14769,7 @@
  1.3154  #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
  1.3155  
  1.3156  /*
  1.3157 -** Flags passed to the sqlite3ExprDup() function. See the header comment 
  1.3158 +** Flags passed to the sqlite3ExprDup() function. See the header comment
  1.3159  ** above sqlite3ExprDup() for details.
  1.3160  */
  1.3161  #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
  1.3162 @@ -13211,7 +14851,11 @@
  1.3163  ** tables in a join to 32 instead of 64.  But it also reduces the size
  1.3164  ** of the library by 738 bytes on ix86.
  1.3165  */
  1.3166 -typedef u64 Bitmask;
  1.3167 +#ifdef SQLITE_BITMASK_TYPE
  1.3168 +  typedef SQLITE_BITMASK_TYPE Bitmask;
  1.3169 +#else
  1.3170 +  typedef u64 Bitmask;
  1.3171 +#endif
  1.3172  
  1.3173  /*
  1.3174  ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
  1.3175 @@ -13223,6 +14867,7 @@
  1.3176  */
  1.3177  #define MASKBIT(n)   (((Bitmask)1)<<(n))
  1.3178  #define MASKBIT32(n) (((unsigned int)1)<<(n))
  1.3179 +#define ALLBITS      ((Bitmask)-1)
  1.3180  
  1.3181  /*
  1.3182  ** The following structure describes the FROM clause of a SELECT statement.
  1.3183 @@ -13295,6 +14940,9 @@
  1.3184  /*
  1.3185  ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
  1.3186  ** and the WhereInfo.wctrlFlags member.
  1.3187 +**
  1.3188 +** Value constraints (enforced via assert()):
  1.3189 +**     WHERE_USE_LIMIT  == SF_FixedLimit
  1.3190  */
  1.3191  #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
  1.3192  #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
  1.3193 @@ -13311,6 +14959,8 @@
  1.3194  #define WHERE_SORTBYGROUP      0x0800 /* Support sqlite3WhereIsSorted() */
  1.3195  #define WHERE_REOPEN_IDX       0x1000 /* Try to use OP_ReopenIdx */
  1.3196  #define WHERE_ONEPASS_MULTIROW 0x2000 /* ONEPASS is ok with multiple rows */
  1.3197 +#define WHERE_USE_LIMIT        0x4000 /* There is a constant LIMIT clause */
  1.3198 +#define WHERE_SEEK_TABLE       0x8000 /* Do not defer seeks on main table */
  1.3199  
  1.3200  /* Allowed return values from sqlite3WhereIsDistinct()
  1.3201  */
  1.3202 @@ -13328,12 +14978,12 @@
  1.3203  ** pEList corresponds to the result set of a SELECT and is NULL for
  1.3204  ** other statements.
  1.3205  **
  1.3206 -** NameContexts can be nested.  When resolving names, the inner-most 
  1.3207 +** NameContexts can be nested.  When resolving names, the inner-most
  1.3208  ** context is searched first.  If no match is found, the next outer
  1.3209  ** context is checked.  If there is still no match, the next context
  1.3210  ** is checked.  This process continues until either a match is found
  1.3211  ** or all contexts are check.  When a match is found, the nRef member of
  1.3212 -** the context containing the match is incremented. 
  1.3213 +** the context containing the match is incremented.
  1.3214  **
  1.3215  ** Each subquery gets a new NameContext.  The pNext field points to the
  1.3216  ** NameContext in the parent query.  Thus the process of scanning the
  1.3217 @@ -13354,16 +15004,18 @@
  1.3218  /*
  1.3219  ** Allowed values for the NameContext, ncFlags field.
  1.3220  **
  1.3221 -** Note:  NC_MinMaxAgg must have the same value as SF_MinMaxAgg and
  1.3222 -** SQLITE_FUNC_MINMAX.
  1.3223 -** 
  1.3224 +** Value constraints (all checked via assert()):
  1.3225 +**    NC_HasAgg    == SF_HasAgg
  1.3226 +**    NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
  1.3227 +**
  1.3228  */
  1.3229  #define NC_AllowAgg  0x0001  /* Aggregate functions are allowed here */
  1.3230 -#define NC_HasAgg    0x0002  /* One or more aggregate functions seen */
  1.3231 +#define NC_PartIdx   0x0002  /* True if resolving a partial index WHERE */
  1.3232  #define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
  1.3233  #define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
  1.3234 -#define NC_PartIdx   0x0010  /* True if resolving a partial index WHERE */
  1.3235 +#define NC_HasAgg    0x0010  /* One or more aggregate functions seen */
  1.3236  #define NC_IdxExpr   0x0020  /* True if resolving columns of CREATE INDEX */
  1.3237 +#define NC_VarSelect 0x0040  /* A correlated subquery has been seen */
  1.3238  #define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
  1.3239  
  1.3240  /*
  1.3241 @@ -13389,13 +15041,13 @@
  1.3242  struct Select {
  1.3243    ExprList *pEList;      /* The fields of the result */
  1.3244    u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
  1.3245 -  u16 selFlags;          /* Various SF_* values */
  1.3246 +  LogEst nSelectRow;     /* Estimated number of result rows */
  1.3247 +  u32 selFlags;          /* Various SF_* values */
  1.3248    int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
  1.3249  #if SELECTTRACE_ENABLED
  1.3250    char zSelName[12];     /* Symbolic name of this SELECT use for debugging */
  1.3251  #endif
  1.3252    int addrOpenEphm[2];   /* OP_OpenEphem opcodes related to this select */
  1.3253 -  u64 nSelectRow;        /* Estimated number of result rows */
  1.3254    SrcList *pSrc;         /* The FROM clause */
  1.3255    Expr *pWhere;          /* The WHERE clause */
  1.3256    ExprList *pGroupBy;    /* The GROUP BY clause */
  1.3257 @@ -13411,23 +15063,30 @@
  1.3258  /*
  1.3259  ** Allowed values for Select.selFlags.  The "SF" prefix stands for
  1.3260  ** "Select Flag".
  1.3261 -*/
  1.3262 -#define SF_Distinct        0x0001  /* Output should be DISTINCT */
  1.3263 -#define SF_All             0x0002  /* Includes the ALL keyword */
  1.3264 -#define SF_Resolved        0x0004  /* Identifiers have been resolved */
  1.3265 -#define SF_Aggregate       0x0008  /* Contains aggregate functions */
  1.3266 -#define SF_UsesEphemeral   0x0010  /* Uses the OpenEphemeral opcode */
  1.3267 -#define SF_Expanded        0x0020  /* sqlite3SelectExpand() called on this */
  1.3268 -#define SF_HasTypeInfo     0x0040  /* FROM subqueries have Table metadata */
  1.3269 -#define SF_Compound        0x0080  /* Part of a compound query */
  1.3270 -#define SF_Values          0x0100  /* Synthesized from VALUES clause */
  1.3271 -#define SF_MultiValue      0x0200  /* Single VALUES term with multiple rows */
  1.3272 -#define SF_NestedFrom      0x0400  /* Part of a parenthesized FROM clause */
  1.3273 -#define SF_MaybeConvert    0x0800  /* Need convertCompoundSelectToSubquery() */
  1.3274 -#define SF_MinMaxAgg       0x1000  /* Aggregate containing min() or max() */
  1.3275 -#define SF_Recursive       0x2000  /* The recursive part of a recursive CTE */
  1.3276 -#define SF_Converted       0x4000  /* By convertCompoundSelectToSubquery() */
  1.3277 -#define SF_IncludeHidden   0x8000  /* Include hidden columns in output */
  1.3278 +**
  1.3279 +** Value constraints (all checked via assert())
  1.3280 +**     SF_HasAgg     == NC_HasAgg
  1.3281 +**     SF_MinMaxAgg  == NC_MinMaxAgg     == SQLITE_FUNC_MINMAX
  1.3282 +**     SF_FixedLimit == WHERE_USE_LIMIT
  1.3283 +*/
  1.3284 +#define SF_Distinct       0x00001  /* Output should be DISTINCT */
  1.3285 +#define SF_All            0x00002  /* Includes the ALL keyword */
  1.3286 +#define SF_Resolved       0x00004  /* Identifiers have been resolved */
  1.3287 +#define SF_Aggregate      0x00008  /* Contains agg functions or a GROUP BY */
  1.3288 +#define SF_HasAgg         0x00010  /* Contains aggregate functions */
  1.3289 +#define SF_UsesEphemeral  0x00020  /* Uses the OpenEphemeral opcode */
  1.3290 +#define SF_Expanded       0x00040  /* sqlite3SelectExpand() called on this */
  1.3291 +#define SF_HasTypeInfo    0x00080  /* FROM subqueries have Table metadata */
  1.3292 +#define SF_Compound       0x00100  /* Part of a compound query */
  1.3293 +#define SF_Values         0x00200  /* Synthesized from VALUES clause */
  1.3294 +#define SF_MultiValue     0x00400  /* Single VALUES term with multiple rows */
  1.3295 +#define SF_NestedFrom     0x00800  /* Part of a parenthesized FROM clause */
  1.3296 +#define SF_MinMaxAgg      0x01000  /* Aggregate containing min() or max() */
  1.3297 +#define SF_Recursive      0x02000  /* The recursive part of a recursive CTE */
  1.3298 +#define SF_FixedLimit     0x04000  /* nSelectRow set by a constant LIMIT */
  1.3299 +#define SF_MaybeConvert   0x08000  /* Need convertCompoundSelectToSubquery() */
  1.3300 +#define SF_Converted      0x10000  /* By convertCompoundSelectToSubquery() */
  1.3301 +#define SF_IncludeHidden  0x20000  /* Include hidden columns in output */
  1.3302  
  1.3303  
  1.3304  /*
  1.3305 @@ -13435,7 +15094,7 @@
  1.3306  ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  1.3307  ** Type".
  1.3308  **
  1.3309 -**     SRT_Union       Store results as a key in a temporary index 
  1.3310 +**     SRT_Union       Store results as a key in a temporary index
  1.3311  **                     identified by pDest->iSDParm.
  1.3312  **
  1.3313  **     SRT_Except      Remove results from the temporary index pDest->iSDParm.
  1.3314 @@ -13459,7 +15118,7 @@
  1.3315  **                     of the query.  This destination implies "LIMIT 1".
  1.3316  **
  1.3317  **     SRT_Set         The result must be a single column.  Store each
  1.3318 -**                     row of result as the key in table pDest->iSDParm. 
  1.3319 +**                     row of result as the key in table pDest->iSDParm.
  1.3320  **                     Apply the affinity pDest->affSdst before storing
  1.3321  **                     results.  Used to implement "IN (SELECT ...)".
  1.3322  **
  1.3323 @@ -13527,7 +15186,7 @@
  1.3324  };
  1.3325  
  1.3326  /*
  1.3327 -** During code generation of statements that do inserts into AUTOINCREMENT 
  1.3328 +** During code generation of statements that do inserts into AUTOINCREMENT
  1.3329  ** tables, the following information is attached to the Table.u.autoInc.p
  1.3330  ** pointer of each autoincrement table to record some side information that
  1.3331  ** the code generator needs.  We have to keep per-table autoincrement
  1.3332 @@ -13550,7 +15209,7 @@
  1.3333  #endif
  1.3334  
  1.3335  /*
  1.3336 -** At least one instance of the following structure is created for each 
  1.3337 +** At least one instance of the following structure is created for each
  1.3338  ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
  1.3339  ** statement. All such objects are stored in the linked list headed at
  1.3340  ** Parse.pTriggerPrg and deleted once statement compilation has been
  1.3341 @@ -13563,7 +15222,7 @@
  1.3342  ** values for both pTrigger and orconf.
  1.3343  **
  1.3344  ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
  1.3345 -** accessed (or set to 0 for triggers fired as a result of INSERT 
  1.3346 +** accessed (or set to 0 for triggers fired as a result of INSERT
  1.3347  ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
  1.3348  ** a mask of new.* columns used by the program.
  1.3349  */
  1.3350 @@ -13604,7 +15263,7 @@
  1.3351  ** is constant but the second part is reset at the beginning and end of
  1.3352  ** each recursion.
  1.3353  **
  1.3354 -** The nTableLock and aTableLock variables are only used if the shared-cache 
  1.3355 +** The nTableLock and aTableLock variables are only used if the shared-cache
  1.3356  ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
  1.3357  ** used to store the set of table-locks required by the statement being
  1.3358  ** compiled. Function sqlite3TableLock() is used to add entries to the
  1.3359 @@ -13624,6 +15283,7 @@
  1.3360    u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
  1.3361    u8 okConstFactor;    /* OK to factor out constants */
  1.3362    u8 disableLookaside; /* Number of times lookaside has been disabled */
  1.3363 +  u8 nColCache;        /* Number of entries in aColCache[] */
  1.3364    int aTempReg[8];     /* Holding area for temporary registers */
  1.3365    int nRangeReg;       /* Size of the temporary register block */
  1.3366    int iRangeReg;       /* First register in temporary register block */
  1.3367 @@ -13737,6 +15397,15 @@
  1.3368  
  1.3369  /*
  1.3370  ** Bitfield flags for P5 value in various opcodes.
  1.3371 +**
  1.3372 +** Value constraints (enforced via assert()):
  1.3373 +**    OPFLAG_LENGTHARG    == SQLITE_FUNC_LENGTH
  1.3374 +**    OPFLAG_TYPEOFARG    == SQLITE_FUNC_TYPEOF
  1.3375 +**    OPFLAG_BULKCSR      == BTREE_BULKLOAD
  1.3376 +**    OPFLAG_SEEKEQ       == BTREE_SEEK_EQ
  1.3377 +**    OPFLAG_FORDELETE    == BTREE_FORDELETE
  1.3378 +**    OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
  1.3379 +**    OPFLAG_AUXDELETE    == BTREE_AUXDELETE
  1.3380  */
  1.3381  #define OPFLAG_NCHANGE       0x01    /* OP_Insert: Set to update db->nChange */
  1.3382                                       /* Also used in P2 (not P5) of OP_Delete */
  1.3383 @@ -13745,6 +15414,9 @@
  1.3384  #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
  1.3385  #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
  1.3386  #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
  1.3387 +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  1.3388 +#define OPFLAG_ISNOOP        0x40    /* OP_Delete does pre-update-hook only */
  1.3389 +#endif
  1.3390  #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
  1.3391  #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
  1.3392  #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
  1.3393 @@ -13757,10 +15429,10 @@
  1.3394  
  1.3395  /*
  1.3396   * Each trigger present in the database schema is stored as an instance of
  1.3397 - * struct Trigger. 
  1.3398 + * struct Trigger.
  1.3399   *
  1.3400   * Pointers to instances of struct Trigger are stored in two ways.
  1.3401 - * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
  1.3402 + * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
  1.3403   *    database). This allows Trigger structures to be retrieved by name.
  1.3404   * 2. All triggers associated with a single table form a linked list, using the
  1.3405   *    pNext member of struct Trigger. A pointer to the first element of the
  1.3406 @@ -13786,7 +15458,7 @@
  1.3407  
  1.3408  /*
  1.3409  ** A trigger is either a BEFORE or an AFTER trigger.  The following constants
  1.3410 -** determine which. 
  1.3411 +** determine which.
  1.3412  **
  1.3413  ** If there are multiple triggers, you might of some BEFORE and some AFTER.
  1.3414  ** In that cases, the constants below can be ORed together.
  1.3415 @@ -13796,15 +15468,15 @@
  1.3416  
  1.3417  /*
  1.3418   * An instance of struct TriggerStep is used to store a single SQL statement
  1.3419 - * that is a part of a trigger-program. 
  1.3420 + * that is a part of a trigger-program.
  1.3421   *
  1.3422   * Instances of struct TriggerStep are stored in a singly linked list (linked
  1.3423 - * using the "pNext" member) referenced by the "step_list" member of the 
  1.3424 + * using the "pNext" member) referenced by the "step_list" member of the
  1.3425   * associated struct Trigger instance. The first element of the linked list is
  1.3426   * the first step of the trigger-program.
  1.3427 - * 
  1.3428 + *
  1.3429   * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
  1.3430 - * "SELECT" statement. The meanings of the other members is determined by the 
  1.3431 + * "SELECT" statement. The meanings of the other members is determined by the
  1.3432   * value of "op" as follows:
  1.3433   *
  1.3434   * (op == TK_INSERT)
  1.3435 @@ -13814,7 +15486,7 @@
  1.3436   * zTarget   -> Dequoted name of the table to insert into.
  1.3437   * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
  1.3438   *              this stores values to be inserted. Otherwise NULL.
  1.3439 - * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ... 
  1.3440 + * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ...
  1.3441   *              statement, then this stores the column-names to be
  1.3442   *              inserted into.
  1.3443   *
  1.3444 @@ -13822,7 +15494,7 @@
  1.3445   * zTarget   -> Dequoted name of the table to delete from.
  1.3446   * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
  1.3447   *              Otherwise NULL.
  1.3448 - * 
  1.3449 + *
  1.3450   * (op == TK_UPDATE)
  1.3451   * zTarget   -> Dequoted name of the table to update.
  1.3452   * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
  1.3453 @@ -13830,7 +15502,7 @@
  1.3454   * pExprList -> A list of the columns to update and the expressions to update
  1.3455   *              them to. See sqlite3Update() documentation of "pChanges"
  1.3456   *              argument.
  1.3457 - * 
  1.3458 + *
  1.3459   */
  1.3460  struct TriggerStep {
  1.3461    u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
  1.3462 @@ -13848,7 +15520,7 @@
  1.3463  /*
  1.3464  ** The following structure contains information used by the sqliteFix...
  1.3465  ** routines as they walk the parse tree to make database references
  1.3466 -** explicit.  
  1.3467 +** explicit.
  1.3468  */
  1.3469  typedef struct DbFixer DbFixer;
  1.3470  struct DbFixer {
  1.3471 @@ -13909,6 +15581,7 @@
  1.3472    int neverCorrupt;                 /* Database is always well-formed */
  1.3473    int szLookaside;                  /* Default lookaside buffer size */
  1.3474    int nLookaside;                   /* Default lookaside buffer count */
  1.3475 +  int nStmtSpill;                   /* Stmt-journal spill-to-disk threshold */
  1.3476    sqlite3_mem_methods m;            /* Low-level memory allocation interface */
  1.3477    sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
  1.3478    sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
  1.3479 @@ -14058,6 +15731,15 @@
  1.3480  #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
  1.3481  #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
  1.3482  #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
  1.3483 +#ifdef SQLITE_DEBUG
  1.3484 +SQLITE_PRIVATE   int sqlite3NomemError(int);
  1.3485 +SQLITE_PRIVATE   int sqlite3IoerrnomemError(int);
  1.3486 +# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
  1.3487 +# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
  1.3488 +#else
  1.3489 +# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
  1.3490 +# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
  1.3491 +#endif
  1.3492  
  1.3493  /*
  1.3494  ** FTS3 and FTS4 both require virtual table support
  1.3495 @@ -14098,6 +15780,7 @@
  1.3496  # define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
  1.3497  # define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
  1.3498  # define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
  1.3499 +# define sqlite3Isquote(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
  1.3500  #else
  1.3501  # define sqlite3Toupper(x)   toupper((unsigned char)(x))
  1.3502  # define sqlite3Isspace(x)   isspace((unsigned char)(x))
  1.3503 @@ -14106,6 +15789,7 @@
  1.3504  # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
  1.3505  # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
  1.3506  # define sqlite3Tolower(x)   tolower((unsigned char)(x))
  1.3507 +# define sqlite3Isquote(x)   ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
  1.3508  #endif
  1.3509  #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  1.3510  SQLITE_PRIVATE int sqlite3IsIdChar(u8);
  1.3511 @@ -14114,8 +15798,9 @@
  1.3512  /*
  1.3513  ** Internal function prototypes
  1.3514  */
  1.3515 -#define sqlite3StrICmp sqlite3_stricmp
  1.3516 +SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
  1.3517  SQLITE_PRIVATE int sqlite3Strlen30(const char*);
  1.3518 +SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
  1.3519  #define sqlite3StrNICmp sqlite3_strnicmp
  1.3520  
  1.3521  SQLITE_PRIVATE int sqlite3MallocInit(void);
  1.3522 @@ -14154,7 +15839,7 @@
  1.3523  #ifdef SQLITE_USE_ALLOCA
  1.3524  # define sqlite3StackAllocRaw(D,N)   alloca(N)
  1.3525  # define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
  1.3526 -# define sqlite3StackFree(D,P)       
  1.3527 +# define sqlite3StackFree(D,P)
  1.3528  #else
  1.3529  # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
  1.3530  # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
  1.3531 @@ -14228,7 +15913,7 @@
  1.3532  
  1.3533  SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
  1.3534  SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
  1.3535 -SQLITE_PRIVATE int sqlite3Dequote(char*);
  1.3536 +SQLITE_PRIVATE void sqlite3Dequote(char*);
  1.3537  SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
  1.3538  SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
  1.3539  SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
  1.3540 @@ -14238,10 +15923,14 @@
  1.3541  SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
  1.3542  SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
  1.3543  SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
  1.3544 +#ifdef SQLITE_DEBUG
  1.3545 +SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int);
  1.3546 +#endif
  1.3547  SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
  1.3548  SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
  1.3549  SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
  1.3550  SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
  1.3551 +SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
  1.3552  SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
  1.3553  SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
  1.3554  SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
  1.3555 @@ -14261,6 +15950,7 @@
  1.3556  SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
  1.3557  SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*);
  1.3558  SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
  1.3559 +SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*);
  1.3560  SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
  1.3561  SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
  1.3562  SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
  1.3563 @@ -14271,11 +15961,10 @@
  1.3564  #else
  1.3565  # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
  1.3566  #endif
  1.3567 -SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
  1.3568 +SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*,Token*);
  1.3569  SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
  1.3570  SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
  1.3571  SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
  1.3572 -SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
  1.3573  SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
  1.3574  SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
  1.3575  SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
  1.3576 @@ -14349,7 +16038,7 @@
  1.3577  SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
  1.3578  SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
  1.3579  SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
  1.3580 -                         Expr*,ExprList*,u16,Expr*,Expr*);
  1.3581 +                         Expr*,ExprList*,u32,Expr*,Expr*);
  1.3582  SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
  1.3583  SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
  1.3584  SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
  1.3585 @@ -14361,7 +16050,7 @@
  1.3586  SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
  1.3587  SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
  1.3588  SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
  1.3589 -SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
  1.3590 +SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
  1.3591  SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
  1.3592  SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
  1.3593  SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
  1.3594 @@ -14461,11 +16150,11 @@
  1.3595  #else
  1.3596  # define sqlite3SelectSetName(A,B)
  1.3597  #endif
  1.3598 -SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
  1.3599 -SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
  1.3600 -SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
  1.3601 +SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
  1.3602 +SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
  1.3603 +SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
  1.3604  SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
  1.3605 -SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
  1.3606 +SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
  1.3607  SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
  1.3608  SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
  1.3609  SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
  1.3610 @@ -14544,7 +16233,11 @@
  1.3611  #ifndef SQLITE_OMIT_VIRTUALTABLE
  1.3612  SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
  1.3613  #endif
  1.3614 +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  1.3615 +    defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
  1.3616 +    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  1.3617  SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
  1.3618 +#endif
  1.3619  
  1.3620  /*
  1.3621  ** Routines to read and write variable-length integers.  These used to
  1.3622 @@ -14579,6 +16272,7 @@
  1.3623  SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
  1.3624  SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
  1.3625  SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
  1.3626 +SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
  1.3627  SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  1.3628  SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
  1.3629  SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  1.3630 @@ -14611,7 +16305,7 @@
  1.3631  
  1.3632  SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
  1.3633  SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
  1.3634 -SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
  1.3635 +SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
  1.3636                          void(*)(void*));
  1.3637  SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
  1.3638  SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
  1.3639 @@ -14626,7 +16320,7 @@
  1.3640  SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
  1.3641  SQLITE_PRIVATE const Token sqlite3IntTokens[];
  1.3642  SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
  1.3643 -SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
  1.3644 +SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
  1.3645  #ifndef SQLITE_OMIT_WSD
  1.3646  SQLITE_PRIVATE int sqlite3PendingByte;
  1.3647  #endif
  1.3648 @@ -14671,7 +16365,7 @@
  1.3649  #ifdef SQLITE_DEBUG
  1.3650  SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
  1.3651  #endif
  1.3652 -SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
  1.3653 +SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
  1.3654    void (*)(sqlite3_context*,int,sqlite3_value **),
  1.3655    void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
  1.3656    FuncDestructor *pDestructor
  1.3657 @@ -14734,7 +16428,7 @@
  1.3658  #  define sqlite3VtabRollback(X)
  1.3659  #  define sqlite3VtabCommit(X)
  1.3660  #  define sqlite3VtabInSync(db) 0
  1.3661 -#  define sqlite3VtabLock(X) 
  1.3662 +#  define sqlite3VtabLock(X)
  1.3663  #  define sqlite3VtabUnlock(X)
  1.3664  #  define sqlite3VtabUnlockList(X)
  1.3665  #  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
  1.3666 @@ -14792,7 +16486,7 @@
  1.3667  ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
  1.3668  ** key functionality is available. If OMIT_TRIGGER is defined but
  1.3669  ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
  1.3670 -** this case foreign keys are parsed, but no other functionality is 
  1.3671 +** this case foreign keys are parsed, but no other functionality is
  1.3672  ** provided (enforcement of FK constraints requires the triggers sub-system).
  1.3673  */
  1.3674  #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
  1.3675 @@ -14853,19 +16547,14 @@
  1.3676  #define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
  1.3677  SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
  1.3678  
  1.3679 +SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
  1.3680 +SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
  1.3681  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  1.3682 -SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
  1.3683 -SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
  1.3684  SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
  1.3685 -SQLITE_PRIVATE   int sqlite3JournalExists(sqlite3_file *p);
  1.3686 -#else
  1.3687 -  #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
  1.3688 -  #define sqlite3JournalExists(p) 1
  1.3689 -#endif
  1.3690 -
  1.3691 +#endif
  1.3692 +
  1.3693 +SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p);
  1.3694  SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
  1.3695 -SQLITE_PRIVATE int sqlite3MemJournalSize(void);
  1.3696 -SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
  1.3697  
  1.3698  SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
  1.3699  #if SQLITE_MAX_EXPR_DEPTH>0
  1.3700 @@ -14896,7 +16585,7 @@
  1.3701  /*
  1.3702  ** If the SQLITE_ENABLE IOTRACE exists then the global variable
  1.3703  ** sqlite3IoTrace is a pointer to a printf-like routine used to
  1.3704 -** print I/O tracing messages. 
  1.3705 +** print I/O tracing messages.
  1.3706  */
  1.3707  #ifdef SQLITE_ENABLE_IOTRACE
  1.3708  # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
  1.3709 @@ -14930,7 +16619,7 @@
  1.3710  ** that allocations that might have been satisfied by lookaside are not
  1.3711  ** passed back to non-lookaside free() routines.  Asserts such as the
  1.3712  ** example above are placed on the non-lookaside free() routines to verify
  1.3713 -** this constraint. 
  1.3714 +** this constraint.
  1.3715  **
  1.3716  ** All of this is no-op for a production build.  It only comes into
  1.3717  ** play when the SQLITE_MEMDEBUG compile-time option is used.
  1.3718 @@ -15037,6 +16726,7 @@
  1.3719  **   isxdigit()                       0x08
  1.3720  **   toupper()                        0x20
  1.3721  **   SQLite identifier character      0x40
  1.3722 +**   Quote character                  0x80
  1.3723  **
  1.3724  ** Bit 0x20 is set if the mapped character requires translation to upper
  1.3725  ** case. i.e. if the character is a lower-case ASCII character.
  1.3726 @@ -15062,7 +16752,7 @@
  1.3727    0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
  1.3728    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
  1.3729    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
  1.3730 -  0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
  1.3731 +  0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80,  /* 20..27     !"#$%&' */
  1.3732    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
  1.3733    0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
  1.3734    0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
  1.3735 @@ -15070,8 +16760,8 @@
  1.3736    0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
  1.3737    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
  1.3738    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
  1.3739 -  0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
  1.3740 -  0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
  1.3741 +  0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
  1.3742 +  0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
  1.3743    0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
  1.3744    0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
  1.3745    0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
  1.3746 @@ -15126,6 +16816,18 @@
  1.3747  # define SQLITE_SORTER_PMASZ 250
  1.3748  #endif
  1.3749  
  1.3750 +/* Statement journals spill to disk when their size exceeds the following
  1.3751 +** threashold (in bytes). 0 means that statement journals are created and
  1.3752 +** written to disk immediately (the default behavior for SQLite versions
  1.3753 +** before 3.12.0).  -1 means always keep the entire statement journal in
  1.3754 +** memory.  (The statement journal is also always held entirely in memory
  1.3755 +** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
  1.3756 +** setting.)
  1.3757 +*/
  1.3758 +#ifndef SQLITE_STMTJRNL_SPILL 
  1.3759 +# define SQLITE_STMTJRNL_SPILL (64*1024)
  1.3760 +#endif
  1.3761 +
  1.3762  /*
  1.3763  ** The following singleton contains the global configuration for
  1.3764  ** the SQLite library.
  1.3765 @@ -15140,6 +16842,7 @@
  1.3766     0,                         /* neverCorrupt */
  1.3767     128,                       /* szLookaside */
  1.3768     500,                       /* nLookaside */
  1.3769 +   SQLITE_STMTJRNL_SPILL,     /* nStmtSpill */
  1.3770     {0,0,0,0,0,0,0,0},         /* m */
  1.3771     {0,0,0,0,0,0,0,0,0},       /* mutex */
  1.3772     {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
  1.3773 @@ -15186,7 +16889,7 @@
  1.3774  ** database connections.  After initialization, this table is
  1.3775  ** read-only.
  1.3776  */
  1.3777 -SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
  1.3778 +SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
  1.3779  
  1.3780  /*
  1.3781  ** Constant tokens for values 0 and 1.
  1.3782 @@ -15787,7 +17490,7 @@
  1.3783  #endif
  1.3784    Bool isEphemeral:1;   /* True for an ephemeral table */
  1.3785    Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
  1.3786 -  Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
  1.3787 +  Bool isOrdered:1;     /* True if the table is not BTREE_UNORDERED */
  1.3788    Pgno pgnoRoot;        /* Root page of the open btree cursor */
  1.3789    i16 nField;           /* Number of fields in the header */
  1.3790    u16 nHdrParsed;       /* Number of header fields parsed so far */
  1.3791 @@ -15861,6 +17564,7 @@
  1.3792    VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
  1.3793    void *token;            /* Copy of SubProgram.token */
  1.3794    i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
  1.3795 +  AuxData *pAuxData;      /* Linked list of auxdata allocations */
  1.3796    int nCursor;            /* Number of entries in apCsr */
  1.3797    int pc;                 /* Program Counter in parent (calling) frame */
  1.3798    int nOp;                /* Size of aOp array */
  1.3799 @@ -16080,16 +17784,16 @@
  1.3800  #endif
  1.3801    u16 nResColumn;         /* Number of columns in one row of the result set */
  1.3802    u8 errorAction;         /* Recovery action to do in case of an error */
  1.3803 +  bft expired:1;          /* True if the VM needs to be recompiled */
  1.3804 +  bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
  1.3805    u8 minWriteFileFormat;  /* Minimum file format for writable database files */
  1.3806    bft explain:2;          /* True if EXPLAIN present on SQL command */
  1.3807    bft changeCntOn:1;      /* True to update the change-counter */
  1.3808 -  bft expired:1;          /* True if the VM needs to be recompiled */
  1.3809    bft runOnlyOnce:1;      /* Automatically expire on reset */
  1.3810    bft usesStmtJournal:1;  /* True if uses a statement journal */
  1.3811    bft readOnly:1;         /* True for statements that do not write */
  1.3812    bft bIsReader:1;        /* True for statements that read */
  1.3813    bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
  1.3814 -  bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
  1.3815    int nChange;            /* Number of db changes made since last reset */
  1.3816    yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
  1.3817    yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
  1.3818 @@ -16128,6 +17832,25 @@
  1.3819  #define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
  1.3820  
  1.3821  /*
  1.3822 +** Structure used to store the context required by the 
  1.3823 +** sqlite3_preupdate_*() API functions.
  1.3824 +*/
  1.3825 +struct PreUpdate {
  1.3826 +  Vdbe *v;
  1.3827 +  VdbeCursor *pCsr;               /* Cursor to read old values from */
  1.3828 +  int op;                         /* One of SQLITE_INSERT, UPDATE, DELETE */
  1.3829 +  u8 *aRecord;                    /* old.* database record */
  1.3830 +  KeyInfo keyinfo;
  1.3831 +  UnpackedRecord *pUnpacked;      /* Unpacked version of aRecord[] */
  1.3832 +  UnpackedRecord *pNewUnpacked;   /* Unpacked version of new.* record */
  1.3833 +  int iNewReg;                    /* Register for new.* values */
  1.3834 +  i64 iKey1;                      /* First key value passed to hook */
  1.3835 +  i64 iKey2;                      /* Second key value passed to hook */
  1.3836 +  int iPKey;                      /* If not negative index of IPK column */
  1.3837 +  Mem *aNew;                      /* Array of new.* values */
  1.3838 +};
  1.3839 +
  1.3840 +/*
  1.3841  ** Function prototypes
  1.3842  */
  1.3843  SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
  1.3844 @@ -16143,7 +17866,7 @@
  1.3845  SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int, u32*);
  1.3846  SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
  1.3847  SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
  1.3848 -SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
  1.3849 +SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
  1.3850  
  1.3851  int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
  1.3852  SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
  1.3853 @@ -16186,6 +17909,9 @@
  1.3854  SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
  1.3855  SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
  1.3856  SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
  1.3857 +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  1.3858 +SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int);
  1.3859 +#endif
  1.3860  SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
  1.3861  
  1.3862  SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
  1.3863 @@ -16623,6 +18349,15 @@
  1.3864  
  1.3865  #ifndef SQLITE_OMIT_DATETIME_FUNCS
  1.3866  
  1.3867 +/*
  1.3868 +** The MSVC CRT on Windows CE may not have a localtime() function.
  1.3869 +** So declare a substitute.  The substitute function itself is
  1.3870 +** defined in "os_win.c".
  1.3871 +*/
  1.3872 +#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
  1.3873 +    (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
  1.3874 +struct tm *__cdecl localtime(const time_t *);
  1.3875 +#endif
  1.3876  
  1.3877  /*
  1.3878  ** A structure for holding a single date and time.
  1.3879 @@ -16991,6 +18726,7 @@
  1.3880    p->validTZ = 0;
  1.3881  }
  1.3882  
  1.3883 +#ifndef SQLITE_OMIT_LOCALTIME
  1.3884  /*
  1.3885  ** On recent Windows platforms, the localtime_s() function is available
  1.3886  ** as part of the "Secure CRT". It is essentially equivalent to 
  1.3887 @@ -17009,7 +18745,6 @@
  1.3888  #define HAVE_LOCALTIME_S 1
  1.3889  #endif
  1.3890  
  1.3891 -#ifndef SQLITE_OMIT_LOCALTIME
  1.3892  /*
  1.3893  ** The following routine implements the rough equivalent of localtime_r()
  1.3894  ** using whatever operating-system specific localtime facility that
  1.3895 @@ -17709,7 +19444,7 @@
  1.3896  ** external linkage.
  1.3897  */
  1.3898  SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
  1.3899 -  static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
  1.3900 +  static FuncDef aDateTimeFuncs[] = {
  1.3901  #ifndef SQLITE_OMIT_DATETIME_FUNCS
  1.3902      DFUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
  1.3903      DFUNCTION(date,             -1, 0, 0, dateFunc      ),
  1.3904 @@ -17725,13 +19460,7 @@
  1.3905      STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
  1.3906  #endif
  1.3907    };
  1.3908 -  int i;
  1.3909 -  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
  1.3910 -  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
  1.3911 -
  1.3912 -  for(i=0; i<ArraySize(aDateTimeFuncs); i++){
  1.3913 -    sqlite3FuncDefInsert(pHash, &aFunc[i]);
  1.3914 -  }
  1.3915 +  sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs));
  1.3916  }
  1.3917  
  1.3918  /************** End of date.c ************************************************/
  1.3919 @@ -17804,9 +19533,9 @@
  1.3920  #if defined(SQLITE_TEST)
  1.3921  SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
  1.3922    #define DO_OS_MALLOC_TEST(x)                                       \
  1.3923 -  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
  1.3924 +  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
  1.3925      void *pTstAlloc = sqlite3Malloc(10);                             \
  1.3926 -    if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
  1.3927 +    if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT;                  \
  1.3928      sqlite3_free(pTstAlloc);                                         \
  1.3929    }
  1.3930  #else
  1.3931 @@ -17819,13 +19548,11 @@
  1.3932  ** of this would be completely automatic if SQLite were coded using
  1.3933  ** C++ instead of plain old C.
  1.3934  */
  1.3935 -SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
  1.3936 -  int rc = SQLITE_OK;
  1.3937 +SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file *pId){
  1.3938    if( pId->pMethods ){
  1.3939 -    rc = pId->pMethods->xClose(pId);
  1.3940 +    pId->pMethods->xClose(pId);
  1.3941      pId->pMethods = 0;
  1.3942    }
  1.3943 -  return rc;
  1.3944  }
  1.3945  SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
  1.3946    DO_OS_MALLOC_TEST(id);
  1.3947 @@ -18000,6 +19727,9 @@
  1.3948  SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
  1.3949    return pVfs->xSleep(pVfs, nMicro);
  1.3950  }
  1.3951 +SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
  1.3952 +  return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
  1.3953 +}
  1.3954  SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
  1.3955    int rc;
  1.3956    /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
  1.3957 @@ -18025,7 +19755,7 @@
  1.3958    int flags,
  1.3959    int *pOutFlags
  1.3960  ){
  1.3961 -  int rc = SQLITE_NOMEM;
  1.3962 +  int rc;
  1.3963    sqlite3_file *pFile;
  1.3964    pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
  1.3965    if( pFile ){
  1.3966 @@ -18035,15 +19765,15 @@
  1.3967      }else{
  1.3968        *ppFile = pFile;
  1.3969      }
  1.3970 -  }
  1.3971 -  return rc;
  1.3972 -}
  1.3973 -SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
  1.3974 -  int rc = SQLITE_OK;
  1.3975 +  }else{
  1.3976 +    rc = SQLITE_NOMEM_BKPT;
  1.3977 +  }
  1.3978 +  return rc;
  1.3979 +}
  1.3980 +SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){
  1.3981    assert( pFile );
  1.3982 -  rc = sqlite3OsClose(pFile);
  1.3983 +  sqlite3OsClose(pFile);
  1.3984    sqlite3_free(pFile);
  1.3985 -  return rc;
  1.3986  }
  1.3987  
  1.3988  /*
  1.3989 @@ -18054,7 +19784,7 @@
  1.3990  */
  1.3991  SQLITE_PRIVATE int sqlite3OsInit(void){
  1.3992    void *p = sqlite3_malloc(10);
  1.3993 -  if( p==0 ) return SQLITE_NOMEM;
  1.3994 +  if( p==0 ) return SQLITE_NOMEM_BKPT;
  1.3995    sqlite3_free(p);
  1.3996    return sqlite3_os_init();
  1.3997  }
  1.3998 @@ -22676,7 +24406,7 @@
  1.3999  static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
  1.4000    sqlite3OomClear(db);
  1.4001    sqlite3Error(db, SQLITE_NOMEM);
  1.4002 -  return SQLITE_NOMEM;
  1.4003 +  return SQLITE_NOMEM_BKPT;
  1.4004  }
  1.4005  
  1.4006  /*
  1.4007 @@ -22723,26 +24453,26 @@
  1.4008  ** Conversion types fall into various categories as defined by the
  1.4009  ** following enumeration.
  1.4010  */
  1.4011 -#define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
  1.4012 -#define etFLOAT       2 /* Floating point.  %f */
  1.4013 -#define etEXP         3 /* Exponentional notation. %e and %E */
  1.4014 -#define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
  1.4015 -#define etSIZE        5 /* Return number of characters processed so far. %n */
  1.4016 -#define etSTRING      6 /* Strings. %s */
  1.4017 -#define etDYNSTRING   7 /* Dynamically allocated strings. %z */
  1.4018 -#define etPERCENT     8 /* Percent symbol. %% */
  1.4019 -#define etCHARX       9 /* Characters. %c */
  1.4020 +#define etRADIX       0 /* Integer types.  %d, %x, %o, and so forth */
  1.4021 +#define etFLOAT       1 /* Floating point.  %f */
  1.4022 +#define etEXP         2 /* Exponentional notation. %e and %E */
  1.4023 +#define etGENERIC     3 /* Floating or exponential, depending on exponent. %g */
  1.4024 +#define etSIZE        4 /* Return number of characters processed so far. %n */
  1.4025 +#define etSTRING      5 /* Strings. %s */
  1.4026 +#define etDYNSTRING   6 /* Dynamically allocated strings. %z */
  1.4027 +#define etPERCENT     7 /* Percent symbol. %% */
  1.4028 +#define etCHARX       8 /* Characters. %c */
  1.4029  /* The rest are extensions, not normally found in printf() */
  1.4030 -#define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
  1.4031 -#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
  1.4032 +#define etSQLESCAPE   9 /* Strings with '\'' doubled.  %q */
  1.4033 +#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
  1.4034                            NULL pointers replaced by SQL NULL.  %Q */
  1.4035 -#define etTOKEN      12 /* a pointer to a Token structure */
  1.4036 -#define etSRCLIST    13 /* a pointer to a SrcList */
  1.4037 -#define etPOINTER    14 /* The %p conversion */
  1.4038 -#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
  1.4039 -#define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
  1.4040 -
  1.4041 -#define etINVALID     0 /* Any unrecognized conversion type */
  1.4042 +#define etTOKEN      11 /* a pointer to a Token structure */
  1.4043 +#define etSRCLIST    12 /* a pointer to a SrcList */
  1.4044 +#define etPOINTER    13 /* The %p conversion */
  1.4045 +#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
  1.4046 +#define etORDINAL    15 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
  1.4047 +
  1.4048 +#define etINVALID    16 /* Any unrecognized conversion type */
  1.4049  
  1.4050  
  1.4051  /*
  1.4052 @@ -22897,7 +24627,7 @@
  1.4053    etByte flag_long;          /* True if "l" flag is present */
  1.4054    etByte flag_longlong;      /* True if the "ll" flag is present */
  1.4055    etByte done;               /* Loop termination flag */
  1.4056 -  etByte xtype = 0;          /* Conversion paradigm */
  1.4057 +  etByte xtype = etINVALID;  /* Conversion paradigm */
  1.4058    u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
  1.4059    u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
  1.4060    char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
  1.4061 @@ -23935,9 +25665,10 @@
  1.4062      sqlite3TreeViewPush(pView, 1);
  1.4063    }
  1.4064    do{
  1.4065 -    sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
  1.4066 +    sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
  1.4067        ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
  1.4068 -      ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
  1.4069 +      ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags,
  1.4070 +      (int)p->nSelectRow
  1.4071      );
  1.4072      if( cnt++ ) sqlite3TreeViewPop(pView);
  1.4073      if( p->pPrior ){
  1.4074 @@ -24141,6 +25872,12 @@
  1.4075      case TK_ISNULL:  zUniOp = "ISNULL"; break;
  1.4076      case TK_NOTNULL: zUniOp = "NOTNULL"; break;
  1.4077  
  1.4078 +    case TK_SPAN: {
  1.4079 +      sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
  1.4080 +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  1.4081 +      break;
  1.4082 +    }
  1.4083 +
  1.4084      case TK_COLLATE: {
  1.4085        sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
  1.4086        sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  1.4087 @@ -24492,7 +26229,7 @@
  1.4088  
  1.4089    *ppThread = 0;
  1.4090    p = sqlite3Malloc(sizeof(*p));
  1.4091 -  if( p==0 ) return SQLITE_NOMEM;
  1.4092 +  if( p==0 ) return SQLITE_NOMEM_BKPT;
  1.4093    memset(p, 0, sizeof(*p));
  1.4094    p->xTask = xTask;
  1.4095    p->pIn = pIn;
  1.4096 @@ -24518,7 +26255,7 @@
  1.4097    int rc;
  1.4098  
  1.4099    assert( ppOut!=0 );
  1.4100 -  if( NEVER(p==0) ) return SQLITE_NOMEM;
  1.4101 +  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
  1.4102    if( p->done ){
  1.4103      *ppOut = p->pOut;
  1.4104      rc = SQLITE_OK;
  1.4105 @@ -24583,7 +26320,7 @@
  1.4106    assert( xTask!=0 );
  1.4107    *ppThread = 0;
  1.4108    p = sqlite3Malloc(sizeof(*p));
  1.4109 -  if( p==0 ) return SQLITE_NOMEM;
  1.4110 +  if( p==0 ) return SQLITE_NOMEM_BKPT;
  1.4111    /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a 
  1.4112    ** function that returns SQLITE_ERROR when passed the argument 200, that
  1.4113    ** forces worker threads to run sequentially and deterministically 
  1.4114 @@ -24615,7 +26352,7 @@
  1.4115    BOOL bRc;
  1.4116  
  1.4117    assert( ppOut!=0 );
  1.4118 -  if( NEVER(p==0) ) return SQLITE_NOMEM;
  1.4119 +  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
  1.4120    if( p->xTask==0 ){
  1.4121      /* assert( p->id==GetCurrentThreadId() ); */
  1.4122      rc = WAIT_OBJECT_0;
  1.4123 @@ -24663,7 +26400,7 @@
  1.4124    assert( xTask!=0 );
  1.4125    *ppThread = 0;
  1.4126    p = sqlite3Malloc(sizeof(*p));
  1.4127 -  if( p==0 ) return SQLITE_NOMEM;
  1.4128 +  if( p==0 ) return SQLITE_NOMEM_BKPT;
  1.4129    if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
  1.4130      p->xTask = xTask;
  1.4131      p->pIn = pIn;
  1.4132 @@ -24679,7 +26416,7 @@
  1.4133  SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
  1.4134  
  1.4135    assert( ppOut!=0 );
  1.4136 -  if( NEVER(p==0) ) return SQLITE_NOMEM;
  1.4137 +  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
  1.4138    if( p->xTask ){
  1.4139      *ppOut = p->xTask(p->pIn);
  1.4140    }else{
  1.4141 @@ -24690,7 +26427,7 @@
  1.4142  #if defined(SQLITE_TEST)
  1.4143    {
  1.4144      void *pTstAlloc = sqlite3Malloc(10);
  1.4145 -    if (!pTstAlloc) return SQLITE_NOMEM;
  1.4146 +    if (!pTstAlloc) return SQLITE_NOMEM_BKPT;
  1.4147      sqlite3_free(pTstAlloc);
  1.4148    }
  1.4149  #endif
  1.4150 @@ -24937,7 +26674,7 @@
  1.4151      rc = sqlite3VdbeMemMakeWriteable(pMem);
  1.4152      if( rc!=SQLITE_OK ){
  1.4153        assert( rc==SQLITE_NOMEM );
  1.4154 -      return SQLITE_NOMEM;
  1.4155 +      return SQLITE_NOMEM_BKPT;
  1.4156      }
  1.4157      zIn = (u8*)pMem->z;
  1.4158      zTerm = &zIn[pMem->n&~1];
  1.4159 @@ -24979,7 +26716,7 @@
  1.4160    zTerm = &zIn[pMem->n];
  1.4161    zOut = sqlite3DbMallocRaw(pMem->db, len);
  1.4162    if( !zOut ){
  1.4163 -    return SQLITE_NOMEM;
  1.4164 +    return SQLITE_NOMEM_BKPT;
  1.4165    }
  1.4166    z = zOut;
  1.4167  
  1.4168 @@ -25349,12 +27086,48 @@
  1.4169  }
  1.4170  
  1.4171  /*
  1.4172 +** Return the declared type of a column.  Or return zDflt if the column 
  1.4173 +** has no declared type.
  1.4174 +**
  1.4175 +** The column type is an extra string stored after the zero-terminator on
  1.4176 +** the column name if and only if the COLFLAG_HASTYPE flag is set.
  1.4177 +*/
  1.4178 +SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){
  1.4179 +  if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt;
  1.4180 +  return pCol->zName + strlen(pCol->zName) + 1;
  1.4181 +}
  1.4182 +
  1.4183 +/*
  1.4184 +** Helper function for sqlite3Error() - called rarely.  Broken out into
  1.4185 +** a separate routine to avoid unnecessary register saves on entry to
  1.4186 +** sqlite3Error().
  1.4187 +*/
  1.4188 +static SQLITE_NOINLINE void  sqlite3ErrorFinish(sqlite3 *db, int err_code){
  1.4189 +  if( db->pErr ) sqlite3ValueSetNull(db->pErr);
  1.4190 +  sqlite3SystemError(db, err_code);
  1.4191 +}
  1.4192 +
  1.4193 +/*
  1.4194  ** Set the current error code to err_code and clear any prior error message.
  1.4195 +** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
  1.4196 +** that would be appropriate.
  1.4197  */
  1.4198  SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
  1.4199    assert( db!=0 );
  1.4200    db->errCode = err_code;
  1.4201 -  if( db->pErr ) sqlite3ValueSetNull(db->pErr);
  1.4202 +  if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
  1.4203 +}
  1.4204 +
  1.4205 +/*
  1.4206 +** Load the sqlite3.iSysErrno field if that is an appropriate thing
  1.4207 +** to do based on the SQLite error code in rc.
  1.4208 +*/
  1.4209 +SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
  1.4210 +  if( rc==SQLITE_IOERR_NOMEM ) return;
  1.4211 +  rc &= 0xff;
  1.4212 +  if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
  1.4213 +    db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
  1.4214 +  }
  1.4215  }
  1.4216  
  1.4217  /*
  1.4218 @@ -25381,6 +27154,7 @@
  1.4219  SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
  1.4220    assert( db!=0 );
  1.4221    db->errCode = err_code;
  1.4222 +  sqlite3SystemError(db, err_code);
  1.4223    if( zFormat==0 ){
  1.4224      sqlite3Error(db, err_code);
  1.4225    }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
  1.4226 @@ -25444,18 +27218,13 @@
  1.4227  ** brackets from around identifiers.  For example:  "[a-b-c]" becomes
  1.4228  ** "a-b-c".
  1.4229  */
  1.4230 -SQLITE_PRIVATE int sqlite3Dequote(char *z){
  1.4231 +SQLITE_PRIVATE void sqlite3Dequote(char *z){
  1.4232    char quote;
  1.4233    int i, j;
  1.4234 -  if( z==0 ) return -1;
  1.4235 +  if( z==0 ) return;
  1.4236    quote = z[0];
  1.4237 -  switch( quote ){
  1.4238 -    case '\'':  break;
  1.4239 -    case '"':   break;
  1.4240 -    case '`':   break;                /* For MySQL compatibility */
  1.4241 -    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
  1.4242 -    default:    return -1;
  1.4243 -  }
  1.4244 +  if( !sqlite3Isquote(quote) ) return;
  1.4245 +  if( quote=='[' ) quote = ']';
  1.4246    for(i=1, j=0;; i++){
  1.4247      assert( z[i] );
  1.4248      if( z[i]==quote ){
  1.4249 @@ -25470,7 +27239,6 @@
  1.4250      }
  1.4251    }
  1.4252    z[j] = 0;
  1.4253 -  return j;
  1.4254  }
  1.4255  
  1.4256  /*
  1.4257 @@ -25495,16 +27263,25 @@
  1.4258  ** independence" that SQLite uses internally when comparing identifiers.
  1.4259  */
  1.4260  SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *zLeft, const char *zRight){
  1.4261 -  register unsigned char *a, *b;
  1.4262    if( zLeft==0 ){
  1.4263      return zRight ? -1 : 0;
  1.4264    }else if( zRight==0 ){
  1.4265      return 1;
  1.4266    }
  1.4267 +  return sqlite3StrICmp(zLeft, zRight);
  1.4268 +}
  1.4269 +SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
  1.4270 +  unsigned char *a, *b;
  1.4271 +  int c;
  1.4272    a = (unsigned char *)zLeft;
  1.4273    b = (unsigned char *)zRight;
  1.4274 -  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
  1.4275 -  return UpperToLower[*a] - UpperToLower[*b];
  1.4276 +  for(;;){
  1.4277 +    c = (int)UpperToLower[*a] - (int)UpperToLower[*b];
  1.4278 +    if( c || *a==0 ) break;
  1.4279 +    a++;
  1.4280 +    b++;
  1.4281 +  }
  1.4282 +  return c;
  1.4283  }
  1.4284  SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
  1.4285    register unsigned char *a, *b;
  1.4286 @@ -25554,7 +27331,7 @@
  1.4287    int eValid = 1;  /* True exponent is either not used or is well-formed */
  1.4288    double result;
  1.4289    int nDigits = 0;
  1.4290 -  int nonNum = 0;
  1.4291 +  int nonNum = 0;  /* True if input contains UTF16 with high byte non-zero */
  1.4292  
  1.4293    assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
  1.4294    *pResult = 0.0;   /* Default return value, in case of an error */
  1.4295 @@ -25567,7 +27344,7 @@
  1.4296      assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
  1.4297      for(i=3-enc; i<length && z[i]==0; i+=2){}
  1.4298      nonNum = i<length;
  1.4299 -    zEnd = z+i+enc-3;
  1.4300 +    zEnd = &z[i^1];
  1.4301      z += (enc&1);
  1.4302    }
  1.4303  
  1.4304 @@ -25583,9 +27360,6 @@
  1.4305      z+=incr;
  1.4306    }
  1.4307  
  1.4308 -  /* skip leading zeroes */
  1.4309 -  while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
  1.4310 -
  1.4311    /* copy max significant digits to significand */
  1.4312    while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
  1.4313      s = s*10 + (*z - '0');
  1.4314 @@ -25602,12 +27376,13 @@
  1.4315      z+=incr;
  1.4316      /* copy digits from after decimal to significand
  1.4317      ** (decrease exponent by d to shift decimal right) */
  1.4318 -    while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
  1.4319 -      s = s*10 + (*z - '0');
  1.4320 -      z+=incr, nDigits++, d--;
  1.4321 -    }
  1.4322 -    /* skip non-significant digits */
  1.4323 -    while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
  1.4324 +    while( z<zEnd && sqlite3Isdigit(*z) ){
  1.4325 +      if( s<((LARGEST_INT64-9)/10) ){
  1.4326 +        s = s*10 + (*z - '0');
  1.4327 +        d--;
  1.4328 +      }
  1.4329 +      z+=incr, nDigits++;
  1.4330 +    }
  1.4331    }
  1.4332    if( z>=zEnd ) goto do_atof_calc;
  1.4333  
  1.4334 @@ -25615,7 +27390,12 @@
  1.4335    if( *z=='e' || *z=='E' ){
  1.4336      z+=incr;
  1.4337      eValid = 0;
  1.4338 -    if( z>=zEnd ) goto do_atof_calc;
  1.4339 +
  1.4340 +    /* This branch is needed to avoid a (harmless) buffer overread.  The 
  1.4341 +    ** special comment alerts the mutation tester that the correct answer
  1.4342 +    ** is obtained even if the branch is omitted */
  1.4343 +    if( z>=zEnd ) goto do_atof_calc;              /*PREVENTS-HARMLESS-OVERREAD*/
  1.4344 +
  1.4345      /* get sign of exponent */
  1.4346      if( *z=='-' ){
  1.4347        esign = -1;
  1.4348 @@ -25632,9 +27412,7 @@
  1.4349    }
  1.4350  
  1.4351    /* skip trailing spaces */
  1.4352 -  if( nDigits && eValid ){
  1.4353 -    while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
  1.4354 -  }
  1.4355 +  while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
  1.4356  
  1.4357  do_atof_calc:
  1.4358    /* adjust exponent by d, and update sign */
  1.4359 @@ -25646,41 +27424,51 @@
  1.4360      esign = 1;
  1.4361    }
  1.4362  
  1.4363 -  /* if 0 significand */
  1.4364 -  if( !s ) {
  1.4365 -    /* In the IEEE 754 standard, zero is signed.
  1.4366 -    ** Add the sign if we've seen at least one digit */
  1.4367 -    result = (sign<0 && nDigits) ? -(double)0 : (double)0;
  1.4368 +  if( s==0 ) {
  1.4369 +    /* In the IEEE 754 standard, zero is signed. */
  1.4370 +    result = sign<0 ? -(double)0 : (double)0;
  1.4371    } else {
  1.4372 -    /* attempt to reduce exponent */
  1.4373 -    if( esign>0 ){
  1.4374 -      while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
  1.4375 -    }else{
  1.4376 -      while( !(s%10) && e>0 ) e--,s/=10;
  1.4377 +    /* Attempt to reduce exponent.
  1.4378 +    **
  1.4379 +    ** Branches that are not required for the correct answer but which only
  1.4380 +    ** help to obtain the correct answer faster are marked with special
  1.4381 +    ** comments, as a hint to the mutation tester.
  1.4382 +    */
  1.4383 +    while( e>0 ){                                       /*OPTIMIZATION-IF-TRUE*/
  1.4384 +      if( esign>0 ){
  1.4385 +        if( s>=(LARGEST_INT64/10) ) break;             /*OPTIMIZATION-IF-FALSE*/
  1.4386 +        s *= 10;
  1.4387 +      }else{
  1.4388 +        if( s%10!=0 ) break;                           /*OPTIMIZATION-IF-FALSE*/
  1.4389 +        s /= 10;
  1.4390 +      }
  1.4391 +      e--;
  1.4392      }
  1.4393  
  1.4394      /* adjust the sign of significand */
  1.4395      s = sign<0 ? -s : s;
  1.4396  
  1.4397 -    /* if exponent, scale significand as appropriate
  1.4398 -    ** and store in result. */
  1.4399 -    if( e ){
  1.4400 +    if( e==0 ){                                         /*OPTIMIZATION-IF-TRUE*/
  1.4401 +      result = (double)s;
  1.4402 +    }else{
  1.4403        LONGDOUBLE_TYPE scale = 1.0;
  1.4404        /* attempt to handle extremely small/large numbers better */
  1.4405 -      if( e>307 && e<342 ){
  1.4406 -        while( e%308 ) { scale *= 1.0e+1; e -= 1; }
  1.4407 -        if( esign<0 ){
  1.4408 -          result = s / scale;
  1.4409 -          result /= 1.0e+308;
  1.4410 -        }else{
  1.4411 -          result = s * scale;
  1.4412 -          result *= 1.0e+308;
  1.4413 -        }
  1.4414 -      }else if( e>=342 ){
  1.4415 -        if( esign<0 ){
  1.4416 -          result = 0.0*s;
  1.4417 -        }else{
  1.4418 -          result = 1e308*1e308*s;  /* Infinity */
  1.4419 +      if( e>307 ){                                      /*OPTIMIZATION-IF-TRUE*/
  1.4420 +        if( e<342 ){                                    /*OPTIMIZATION-IF-TRUE*/
  1.4421 +          while( e%308 ) { scale *= 1.0e+1; e -= 1; }
  1.4422 +          if( esign<0 ){
  1.4423 +            result = s / scale;
  1.4424 +            result /= 1.0e+308;
  1.4425 +          }else{
  1.4426 +            result = s * scale;
  1.4427 +            result *= 1.0e+308;
  1.4428 +          }
  1.4429 +        }else{ assert( e>=342 );
  1.4430 +          if( esign<0 ){
  1.4431 +            result = 0.0*s;
  1.4432 +          }else{
  1.4433 +            result = 1e308*1e308*s;  /* Infinity */
  1.4434 +          }
  1.4435          }
  1.4436        }else{
  1.4437          /* 1.0e+22 is the largest power of 10 than can be 
  1.4438 @@ -25693,8 +27481,6 @@
  1.4439            result = s * scale;
  1.4440          }
  1.4441        }
  1.4442 -    } else {
  1.4443 -      result = (double)s;
  1.4444      }
  1.4445    }
  1.4446  
  1.4447 @@ -25702,7 +27488,7 @@
  1.4448    *pResult = result;
  1.4449  
  1.4450    /* return true if number and no extra non-whitespace chracters after */
  1.4451 -  return z>=zEnd && nDigits>0 && eValid && nonNum==0;
  1.4452 +  return z==zEnd && nDigits>0 && eValid && nonNum==0;
  1.4453  #else
  1.4454    return !sqlite3Atoi64(z, pResult, length, enc);
  1.4455  #endif /* SQLITE_OMIT_FLOATING_POINT */
  1.4456 @@ -25764,7 +27550,7 @@
  1.4457    int neg = 0; /* assume positive */
  1.4458    int i;
  1.4459    int c = 0;
  1.4460 -  int nonNum = 0;
  1.4461 +  int nonNum = 0;  /* True if input contains UTF16 with high byte non-zero */
  1.4462    const char *zStart;
  1.4463    const char *zEnd = zNum + length;
  1.4464    assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
  1.4465 @@ -25775,7 +27561,7 @@
  1.4466      assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
  1.4467      for(i=3-enc; i<length && zNum[i]==0; i+=2){}
  1.4468      nonNum = i<length;
  1.4469 -    zEnd = zNum+i+enc-3;
  1.4470 +    zEnd = &zNum[i^1];
  1.4471      zNum += (enc&1);
  1.4472    }
  1.4473    while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
  1.4474 @@ -25802,8 +27588,11 @@
  1.4475    testcase( i==18 );
  1.4476    testcase( i==19 );
  1.4477    testcase( i==20 );
  1.4478 -  if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum)
  1.4479 -       || i>19*incr || nonNum ){
  1.4480 +  if( &zNum[i]<zEnd              /* Extra bytes at the end */
  1.4481 +   || (i==0 && zStart==zNum)     /* No digits */
  1.4482 +   || i>19*incr                  /* Too many digits */
  1.4483 +   || nonNum                     /* UTF16 with high-order bytes non-zero */
  1.4484 +  ){
  1.4485      /* zNum is empty or contains non-numeric text or is longer
  1.4486      ** than 19 digits (thus guaranteeing that it is too large) */
  1.4487      return 1;
  1.4488 @@ -25845,7 +27634,6 @@
  1.4489  #ifndef SQLITE_OMIT_HEX_INTEGER
  1.4490    if( z[0]=='0'
  1.4491     && (z[1]=='x' || z[1]=='X')
  1.4492 -   && sqlite3Isxdigit(z[2])
  1.4493    ){
  1.4494      u64 u = 0;
  1.4495      int i, k;
  1.4496 @@ -26315,7 +28103,7 @@
  1.4497  */
  1.4498  SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
  1.4499    int i;
  1.4500 -  for(i=1; (v >>= 7)!=0; i++){ assert( i<9 ); }
  1.4501 +  for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
  1.4502    return i;
  1.4503  }
  1.4504  
  1.4505 @@ -26346,10 +28134,12 @@
  1.4506  SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
  1.4507  #if SQLITE_BYTEORDER==4321
  1.4508    memcpy(p,&v,4);
  1.4509 -#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000
  1.4510 +#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
  1.4511 +    && defined(__GNUC__) && GCC_VERSION>=4003000
  1.4512    u32 x = __builtin_bswap32(v);
  1.4513    memcpy(p,&x,4);
  1.4514 -#elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300
  1.4515 +#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
  1.4516 +    && defined(_MSC_VER) && _MSC_VER>=1300
  1.4517    u32 x = _byteswap_ulong(v);
  1.4518    memcpy(p,&x,4);
  1.4519  #else
  1.4520 @@ -26605,7 +28395,7 @@
  1.4521      if( x<2 ) return 0;
  1.4522      while( x<8 ){  y -= 10; x <<= 1; }
  1.4523    }else{
  1.4524 -    while( x>255 ){ y += 40; x >>= 4; }
  1.4525 +    while( x>255 ){ y += 40; x >>= 4; }  /*OPTIMIZATION-IF-TRUE*/
  1.4526      while( x>15 ){  y += 10; x >>= 1; }
  1.4527    }
  1.4528    return a[x&7] + y - 10;
  1.4529 @@ -26628,21 +28418,32 @@
  1.4530  }
  1.4531  #endif /* SQLITE_OMIT_VIRTUALTABLE */
  1.4532  
  1.4533 +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  1.4534 +    defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
  1.4535 +    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  1.4536  /*
  1.4537  ** Convert a LogEst into an integer.
  1.4538 +**
  1.4539 +** Note that this routine is only used when one or more of various
  1.4540 +** non-standard compile-time options is enabled.
  1.4541  */
  1.4542  SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
  1.4543    u64 n;
  1.4544 -  if( x<10 ) return 1;
  1.4545    n = x%10;
  1.4546    x /= 10;
  1.4547    if( n>=5 ) n -= 2;
  1.4548    else if( n>=1 ) n -= 1;
  1.4549 -  if( x>=3 ){
  1.4550 -    return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3);
  1.4551 -  }
  1.4552 -  return (n+8)>>(3-x);
  1.4553 -}
  1.4554 +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  1.4555 +    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  1.4556 +  if( x>60 ) return (u64)LARGEST_INT64;
  1.4557 +#else
  1.4558 +  /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input
  1.4559 +  ** possible to this routine is 310, resulting in a maximum x of 31 */
  1.4560 +  assert( x<=60 );
  1.4561 +#endif
  1.4562 +  return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
  1.4563 +}
  1.4564 +#endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
  1.4565  
  1.4566  /************** End of util.c ************************************************/
  1.4567  /************** Begin file hash.c ********************************************/
  1.4568 @@ -26703,7 +28504,7 @@
  1.4569  static unsigned int strHash(const char *z){
  1.4570    unsigned int h = 0;
  1.4571    unsigned char c;
  1.4572 -  while( (c = (unsigned char)*z++)!=0 ){
  1.4573 +  while( (c = (unsigned char)*z++)!=0 ){     /*OPTIMIZATION-IF-TRUE*/
  1.4574      h = (h<<3) ^ h ^ sqlite3UpperToLower[c];
  1.4575    }
  1.4576    return h;
  1.4577 @@ -26796,7 +28597,7 @@
  1.4578    int count;                     /* Number of elements left to test */
  1.4579    unsigned int h;                /* The computed hash */
  1.4580  
  1.4581 -  if( pH->ht ){
  1.4582 +  if( pH->ht ){   /*OPTIMIZATION-IF-TRUE*/
  1.4583      struct _ht *pEntry;
  1.4584      h = strHash(pKey) % pH->htsize;
  1.4585      pEntry = &pH->ht[h];
  1.4586 @@ -26943,153 +28744,152 @@
  1.4587      /*  12 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
  1.4588      /*  13 */ "Goto"             OpHelp(""),
  1.4589      /*  14 */ "Gosub"            OpHelp(""),
  1.4590 -    /*  15 */ "Return"           OpHelp(""),
  1.4591 -    /*  16 */ "InitCoroutine"    OpHelp(""),
  1.4592 -    /*  17 */ "EndCoroutine"     OpHelp(""),
  1.4593 -    /*  18 */ "Yield"            OpHelp(""),
  1.4594 +    /*  15 */ "InitCoroutine"    OpHelp(""),
  1.4595 +    /*  16 */ "Yield"            OpHelp(""),
  1.4596 +    /*  17 */ "MustBeInt"        OpHelp(""),
  1.4597 +    /*  18 */ "Jump"             OpHelp(""),
  1.4598      /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
  1.4599 -    /*  20 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
  1.4600 -    /*  21 */ "Halt"             OpHelp(""),
  1.4601 -    /*  22 */ "Integer"          OpHelp("r[P2]=P1"),
  1.4602 -    /*  23 */ "Int64"            OpHelp("r[P2]=P4"),
  1.4603 -    /*  24 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
  1.4604 -    /*  25 */ "Null"             OpHelp("r[P2..P3]=NULL"),
  1.4605 -    /*  26 */ "SoftNull"         OpHelp("r[P1]=NULL"),
  1.4606 -    /*  27 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
  1.4607 -    /*  28 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
  1.4608 -    /*  29 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
  1.4609 -    /*  30 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
  1.4610 -    /*  31 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
  1.4611 -    /*  32 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
  1.4612 -    /*  33 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
  1.4613 -    /*  34 */ "CollSeq"          OpHelp(""),
  1.4614 -    /*  35 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
  1.4615 -    /*  36 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
  1.4616 -    /*  37 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
  1.4617 -    /*  38 */ "MustBeInt"        OpHelp(""),
  1.4618 -    /*  39 */ "RealAffinity"     OpHelp(""),
  1.4619 -    /*  40 */ "Cast"             OpHelp("affinity(r[P1])"),
  1.4620 -    /*  41 */ "Permutation"      OpHelp(""),
  1.4621 -    /*  42 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
  1.4622 -    /*  43 */ "Jump"             OpHelp(""),
  1.4623 -    /*  44 */ "Once"             OpHelp(""),
  1.4624 -    /*  45 */ "If"               OpHelp(""),
  1.4625 -    /*  46 */ "IfNot"            OpHelp(""),
  1.4626 -    /*  47 */ "Column"           OpHelp("r[P3]=PX"),
  1.4627 -    /*  48 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
  1.4628 -    /*  49 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
  1.4629 -    /*  50 */ "Count"            OpHelp("r[P2]=count()"),
  1.4630 -    /*  51 */ "ReadCookie"       OpHelp(""),
  1.4631 -    /*  52 */ "SetCookie"        OpHelp(""),
  1.4632 -    /*  53 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
  1.4633 -    /*  54 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
  1.4634 -    /*  55 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
  1.4635 -    /*  56 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
  1.4636 -    /*  57 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
  1.4637 -    /*  58 */ "SorterOpen"       OpHelp(""),
  1.4638 -    /*  59 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
  1.4639 -    /*  60 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
  1.4640 -    /*  61 */ "Close"            OpHelp(""),
  1.4641 -    /*  62 */ "ColumnsUsed"      OpHelp(""),
  1.4642 -    /*  63 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
  1.4643 -    /*  64 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
  1.4644 -    /*  65 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
  1.4645 -    /*  66 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
  1.4646 -    /*  67 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
  1.4647 -    /*  68 */ "NotFound"         OpHelp("key=r[P3@P4]"),
  1.4648 -    /*  69 */ "Found"            OpHelp("key=r[P3@P4]"),
  1.4649 -    /*  70 */ "NotExists"        OpHelp("intkey=r[P3]"),
  1.4650 -    /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
  1.4651 -    /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
  1.4652 -    /*  73 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
  1.4653 -    /*  74 */ "NewRowid"         OpHelp("r[P2]=rowid"),
  1.4654 -    /*  75 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
  1.4655 -    /*  76 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
  1.4656 -    /*  77 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
  1.4657 -    /*  78 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
  1.4658 -    /*  79 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
  1.4659 -    /*  80 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
  1.4660 -    /*  81 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
  1.4661 -    /*  82 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
  1.4662 -    /*  83 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
  1.4663 -    /*  84 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
  1.4664 -    /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
  1.4665 -    /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
  1.4666 -    /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
  1.4667 -    /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
  1.4668 -    /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
  1.4669 -    /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
  1.4670 -    /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
  1.4671 -    /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
  1.4672 -    /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
  1.4673 -    /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
  1.4674 -    /*  95 */ "Delete"           OpHelp(""),
  1.4675 -    /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
  1.4676 +    /*  20 */ "Once"             OpHelp(""),
  1.4677 +    /*  21 */ "If"               OpHelp(""),
  1.4678 +    /*  22 */ "IfNot"            OpHelp(""),
  1.4679 +    /*  23 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
  1.4680 +    /*  24 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
  1.4681 +    /*  25 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
  1.4682 +    /*  26 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
  1.4683 +    /*  27 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
  1.4684 +    /*  28 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
  1.4685 +    /*  29 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
  1.4686 +    /*  30 */ "NotFound"         OpHelp("key=r[P3@P4]"),
  1.4687 +    /*  31 */ "Found"            OpHelp("key=r[P3@P4]"),
  1.4688 +    /*  32 */ "NotExists"        OpHelp("intkey=r[P3]"),
  1.4689 +    /*  33 */ "Last"             OpHelp(""),
  1.4690 +    /*  34 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
  1.4691 +    /*  35 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
  1.4692 +    /*  36 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
  1.4693 +    /*  37 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
  1.4694 +    /*  38 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
  1.4695 +    /*  39 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
  1.4696 +    /*  40 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
  1.4697 +    /*  41 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
  1.4698 +    /*  42 */ "SorterSort"       OpHelp(""),
  1.4699 +    /*  43 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
  1.4700 +    /*  44 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
  1.4701 +    /*  45 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
  1.4702 +    /*  46 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
  1.4703 +    /*  47 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
  1.4704 +    /*  48 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
  1.4705 +    /*  49 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
  1.4706 +    /*  50 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
  1.4707 +    /*  51 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
  1.4708 +    /*  52 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
  1.4709 +    /*  53 */ "Sort"             OpHelp(""),
  1.4710 +    /*  54 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
  1.4711 +    /*  55 */ "Rewind"           OpHelp(""),
  1.4712 +    /*  56 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
  1.4713 +    /*  57 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
  1.4714 +    /*  58 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
  1.4715 +    /*  59 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
  1.4716 +    /*  60 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
  1.4717 +    /*  61 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
  1.4718 +    /*  62 */ "Program"          OpHelp(""),
  1.4719 +    /*  63 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
  1.4720 +    /*  64 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
  1.4721 +    /*  65 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
  1.4722 +    /*  66 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
  1.4723 +    /*  67 */ "IncrVacuum"       OpHelp(""),
  1.4724 +    /*  68 */ "VNext"            OpHelp(""),
  1.4725 +    /*  69 */ "Init"             OpHelp("Start at P2"),
  1.4726 +    /*  70 */ "Return"           OpHelp(""),
  1.4727 +    /*  71 */ "EndCoroutine"     OpHelp(""),
  1.4728 +    /*  72 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
  1.4729 +    /*  73 */ "Halt"             OpHelp(""),
  1.4730 +    /*  74 */ "Integer"          OpHelp("r[P2]=P1"),
  1.4731 +    /*  75 */ "Int64"            OpHelp("r[P2]=P4"),
  1.4732 +    /*  76 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
  1.4733 +    /*  77 */ "Null"             OpHelp("r[P2..P3]=NULL"),
  1.4734 +    /*  78 */ "SoftNull"         OpHelp("r[P1]=NULL"),
  1.4735 +    /*  79 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
  1.4736 +    /*  80 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
  1.4737 +    /*  81 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
  1.4738 +    /*  82 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
  1.4739 +    /*  83 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
  1.4740 +    /*  84 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
  1.4741 +    /*  85 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
  1.4742 +    /*  86 */ "CollSeq"          OpHelp(""),
  1.4743 +    /*  87 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
  1.4744 +    /*  88 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
  1.4745 +    /*  89 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
  1.4746 +    /*  90 */ "RealAffinity"     OpHelp(""),
  1.4747 +    /*  91 */ "Cast"             OpHelp("affinity(r[P1])"),
  1.4748 +    /*  92 */ "Permutation"      OpHelp(""),
  1.4749 +    /*  93 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
  1.4750 +    /*  94 */ "Column"           OpHelp("r[P3]=PX"),
  1.4751 +    /*  95 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
  1.4752 +    /*  96 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
  1.4753      /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
  1.4754 -    /*  98 */ "ResetCount"       OpHelp(""),
  1.4755 -    /*  99 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
  1.4756 -    /* 100 */ "SorterData"       OpHelp("r[P2]=data"),
  1.4757 -    /* 101 */ "RowKey"           OpHelp("r[P2]=key"),
  1.4758 -    /* 102 */ "RowData"          OpHelp("r[P2]=data"),
  1.4759 -    /* 103 */ "Rowid"            OpHelp("r[P2]=rowid"),
  1.4760 -    /* 104 */ "NullRow"          OpHelp(""),
  1.4761 -    /* 105 */ "Last"             OpHelp(""),
  1.4762 -    /* 106 */ "SorterSort"       OpHelp(""),
  1.4763 -    /* 107 */ "Sort"             OpHelp(""),
  1.4764 -    /* 108 */ "Rewind"           OpHelp(""),
  1.4765 -    /* 109 */ "SorterInsert"     OpHelp(""),
  1.4766 -    /* 110 */ "IdxInsert"        OpHelp("key=r[P2]"),
  1.4767 -    /* 111 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
  1.4768 -    /* 112 */ "Seek"             OpHelp("Move P3 to P1.rowid"),
  1.4769 -    /* 113 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
  1.4770 -    /* 114 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
  1.4771 -    /* 115 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
  1.4772 -    /* 116 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
  1.4773 -    /* 117 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
  1.4774 -    /* 118 */ "Destroy"          OpHelp(""),
  1.4775 -    /* 119 */ "Clear"            OpHelp(""),
  1.4776 -    /* 120 */ "ResetSorter"      OpHelp(""),
  1.4777 -    /* 121 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
  1.4778 -    /* 122 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
  1.4779 -    /* 123 */ "ParseSchema"      OpHelp(""),
  1.4780 -    /* 124 */ "LoadAnalysis"     OpHelp(""),
  1.4781 -    /* 125 */ "DropTable"        OpHelp(""),
  1.4782 -    /* 126 */ "DropIndex"        OpHelp(""),
  1.4783 -    /* 127 */ "DropTrigger"      OpHelp(""),
  1.4784 -    /* 128 */ "IntegrityCk"      OpHelp(""),
  1.4785 -    /* 129 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
  1.4786 -    /* 130 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
  1.4787 -    /* 131 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
  1.4788 -    /* 132 */ "Program"          OpHelp(""),
  1.4789 +    /*  98 */ "Count"            OpHelp("r[P2]=count()"),
  1.4790 +    /*  99 */ "ReadCookie"       OpHelp(""),
  1.4791 +    /* 100 */ "SetCookie"        OpHelp(""),
  1.4792 +    /* 101 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
  1.4793 +    /* 102 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
  1.4794 +    /* 103 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
  1.4795 +    /* 104 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
  1.4796 +    /* 105 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
  1.4797 +    /* 106 */ "SorterOpen"       OpHelp(""),
  1.4798 +    /* 107 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
  1.4799 +    /* 108 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
  1.4800 +    /* 109 */ "Close"            OpHelp(""),
  1.4801 +    /* 110 */ "ColumnsUsed"      OpHelp(""),
  1.4802 +    /* 111 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
  1.4803 +    /* 112 */ "NewRowid"         OpHelp("r[P2]=rowid"),
  1.4804 +    /* 113 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
  1.4805 +    /* 114 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
  1.4806 +    /* 115 */ "Delete"           OpHelp(""),
  1.4807 +    /* 116 */ "ResetCount"       OpHelp(""),
  1.4808 +    /* 117 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
  1.4809 +    /* 118 */ "SorterData"       OpHelp("r[P2]=data"),
  1.4810 +    /* 119 */ "RowKey"           OpHelp("r[P2]=key"),
  1.4811 +    /* 120 */ "RowData"          OpHelp("r[P2]=data"),
  1.4812 +    /* 121 */ "Rowid"            OpHelp("r[P2]=rowid"),
  1.4813 +    /* 122 */ "NullRow"          OpHelp(""),
  1.4814 +    /* 123 */ "SorterInsert"     OpHelp(""),
  1.4815 +    /* 124 */ "IdxInsert"        OpHelp("key=r[P2]"),
  1.4816 +    /* 125 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
  1.4817 +    /* 126 */ "Seek"             OpHelp("Move P3 to P1.rowid"),
  1.4818 +    /* 127 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
  1.4819 +    /* 128 */ "Destroy"          OpHelp(""),
  1.4820 +    /* 129 */ "Clear"            OpHelp(""),
  1.4821 +    /* 130 */ "ResetSorter"      OpHelp(""),
  1.4822 +    /* 131 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
  1.4823 +    /* 132 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
  1.4824      /* 133 */ "Real"             OpHelp("r[P2]=P4"),
  1.4825 -    /* 134 */ "Param"            OpHelp(""),
  1.4826 -    /* 135 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
  1.4827 -    /* 136 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
  1.4828 -    /* 137 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
  1.4829 -    /* 138 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
  1.4830 -    /* 139 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
  1.4831 -    /* 140 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
  1.4832 -    /* 141 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
  1.4833 -    /* 142 */ "JumpZeroIncr"     OpHelp("if (r[P1]++)==0 ) goto P2"),
  1.4834 -    /* 143 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
  1.4835 -    /* 144 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
  1.4836 -    /* 145 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
  1.4837 -    /* 146 */ "IncrVacuum"       OpHelp(""),
  1.4838 -    /* 147 */ "Expire"           OpHelp(""),
  1.4839 -    /* 148 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
  1.4840 -    /* 149 */ "VBegin"           OpHelp(""),
  1.4841 -    /* 150 */ "VCreate"          OpHelp(""),
  1.4842 -    /* 151 */ "VDestroy"         OpHelp(""),
  1.4843 -    /* 152 */ "VOpen"            OpHelp(""),
  1.4844 -    /* 153 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
  1.4845 -    /* 154 */ "VNext"            OpHelp(""),
  1.4846 +    /* 134 */ "ParseSchema"      OpHelp(""),
  1.4847 +    /* 135 */ "LoadAnalysis"     OpHelp(""),
  1.4848 +    /* 136 */ "DropTable"        OpHelp(""),
  1.4849 +    /* 137 */ "DropIndex"        OpHelp(""),
  1.4850 +    /* 138 */ "DropTrigger"      OpHelp(""),
  1.4851 +    /* 139 */ "IntegrityCk"      OpHelp(""),
  1.4852 +    /* 140 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
  1.4853 +    /* 141 */ "Param"            OpHelp(""),
  1.4854 +    /* 142 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
  1.4855 +    /* 143 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
  1.4856 +    /* 144 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
  1.4857 +    /* 145 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
  1.4858 +    /* 146 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
  1.4859 +    /* 147 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
  1.4860 +    /* 148 */ "Expire"           OpHelp(""),
  1.4861 +    /* 149 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
  1.4862 +    /* 150 */ "VBegin"           OpHelp(""),
  1.4863 +    /* 151 */ "VCreate"          OpHelp(""),
  1.4864 +    /* 152 */ "VDestroy"         OpHelp(""),
  1.4865 +    /* 153 */ "VOpen"            OpHelp(""),
  1.4866 +    /* 154 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
  1.4867      /* 155 */ "VRename"          OpHelp(""),
  1.4868      /* 156 */ "Pagecount"        OpHelp(""),
  1.4869      /* 157 */ "MaxPgcnt"         OpHelp(""),
  1.4870 -    /* 158 */ "Init"             OpHelp("Start at P2"),
  1.4871 -    /* 159 */ "CursorHint"       OpHelp(""),
  1.4872 -    /* 160 */ "Noop"             OpHelp(""),
  1.4873 -    /* 161 */ "Explain"          OpHelp(""),
  1.4874 +    /* 158 */ "CursorHint"       OpHelp(""),
  1.4875 +    /* 159 */ "Noop"             OpHelp(""),
  1.4876 +    /* 160 */ "Explain"          OpHelp(""),
  1.4877    };
  1.4878    return azName[i];
  1.4879  }
  1.4880 @@ -27170,6 +28970,19 @@
  1.4881  #  endif
  1.4882  #endif
  1.4883  
  1.4884 +/* Use pread() and pwrite() if they are available */
  1.4885 +#if defined(__APPLE__)
  1.4886 +# define HAVE_PREAD 1
  1.4887 +# define HAVE_PWRITE 1
  1.4888 +#endif
  1.4889 +#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
  1.4890 +# undef USE_PREAD
  1.4891 +# define USE_PREAD64 1
  1.4892 +#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
  1.4893 +# undef USE_PREAD64
  1.4894 +# define USE_PREAD 1
  1.4895 +#endif
  1.4896 +
  1.4897  /*
  1.4898  ** standard include files.
  1.4899  */
  1.4900 @@ -27689,7 +29502,7 @@
  1.4901  #else
  1.4902    { "pread64",      (sqlite3_syscall_ptr)0,          0  },
  1.4903  #endif
  1.4904 -#define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
  1.4905 +#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
  1.4906  
  1.4907    { "write",        (sqlite3_syscall_ptr)write,      0  },
  1.4908  #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
  1.4909 @@ -27707,7 +29520,7 @@
  1.4910  #else
  1.4911    { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
  1.4912  #endif
  1.4913 -#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
  1.4914 +#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off64_t))\
  1.4915                      aSyscall[13].pCurrent)
  1.4916  
  1.4917    { "fchmod",       (sqlite3_syscall_ptr)fchmod,          0  },
  1.4918 @@ -28599,7 +30412,7 @@
  1.4919    if( pInode==0 ){
  1.4920      pInode = sqlite3_malloc64( sizeof(*pInode) );
  1.4921      if( pInode==0 ){
  1.4922 -      return SQLITE_NOMEM;
  1.4923 +      return SQLITE_NOMEM_BKPT;
  1.4924      }
  1.4925      memset(pInode, 0, sizeof(*pInode));
  1.4926      memcpy(&pInode->fileId, &fileId, sizeof(fileId));
  1.4927 @@ -28641,12 +30454,16 @@
  1.4928  static void verifyDbFile(unixFile *pFile){
  1.4929    struct stat buf;
  1.4930    int rc;
  1.4931 +
  1.4932 +  /* These verifications occurs for the main database only */
  1.4933 +  if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
  1.4934 +
  1.4935    rc = osFstat(pFile->h, &buf);
  1.4936    if( rc!=0 ){
  1.4937      sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
  1.4938      return;
  1.4939    }
  1.4940 -  if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
  1.4941 +  if( buf.st_nlink==0 ){
  1.4942      sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
  1.4943      return;
  1.4944    }
  1.4945 @@ -28782,7 +30599,7 @@
  1.4946    ** lock transitions in terms of the POSIX advisory shared and exclusive
  1.4947    ** lock primitives (called read-locks and write-locks below, to avoid
  1.4948    ** confusion with SQLite lock names). The algorithms are complicated
  1.4949 -  ** slightly in order to be compatible with windows systems simultaneously
  1.4950 +  ** slightly in order to be compatible with Windows95 systems simultaneously
  1.4951    ** accessing the same database file, in case that is ever required.
  1.4952    **
  1.4953    ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
  1.4954 @@ -28790,8 +30607,14 @@
  1.4955    ** range', a range of 510 bytes at a well known offset.
  1.4956    **
  1.4957    ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
  1.4958 -  ** byte'.  If this is successful, a random byte from the 'shared byte
  1.4959 -  ** range' is read-locked and the lock on the 'pending byte' released.
  1.4960 +  ** byte'.  If this is successful, 'shared byte range' is read-locked
  1.4961 +  ** and the lock on the 'pending byte' released.  (Legacy note:  When
  1.4962 +  ** SQLite was first developed, Windows95 systems were still very common,
  1.4963 +  ** and Widnows95 lacks a shared-lock capability.  So on Windows95, a
  1.4964 +  ** single randomly selected by from the 'shared byte range' is locked.
  1.4965 +  ** Windows95 is now pretty much extinct, but this work-around for the
  1.4966 +  ** lack of shared-locks on Windows95 lives on, for backwards
  1.4967 +  ** compatibility.)
  1.4968    **
  1.4969    ** A process may only obtain a RESERVED lock after it has a SHARED lock.
  1.4970    ** A RESERVED lock is implemented by grabbing a write-lock on the
  1.4971 @@ -28810,11 +30633,6 @@
  1.4972    ** range'. Since all other locks require a read-lock on one of the bytes
  1.4973    ** within this range, this ensures that no other locks are held on the
  1.4974    ** database. 
  1.4975 -  **
  1.4976 -  ** The reason a single byte cannot be used instead of the 'shared byte
  1.4977 -  ** range' is that some versions of windows do not support read-locks. By
  1.4978 -  ** locking a random byte from a range, concurrent SHARED locks may exist
  1.4979 -  ** even if the locking primitive used is always a write-lock.
  1.4980    */
  1.4981    int rc = SQLITE_OK;
  1.4982    unixFile *pFile = (unixFile*)id;
  1.4983 @@ -31519,7 +33337,7 @@
  1.4984  
  1.4985    /* Allocate space for the new unixShm object. */
  1.4986    p = sqlite3_malloc64( sizeof(*p) );
  1.4987 -  if( p==0 ) return SQLITE_NOMEM;
  1.4988 +  if( p==0 ) return SQLITE_NOMEM_BKPT;
  1.4989    memset(p, 0, sizeof(*p));
  1.4990    assert( pDbFd->pShm==0 );
  1.4991  
  1.4992 @@ -31551,7 +33369,7 @@
  1.4993  #endif
  1.4994      pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
  1.4995      if( pShmNode==0 ){
  1.4996 -      rc = SQLITE_NOMEM;
  1.4997 +      rc = SQLITE_NOMEM_BKPT;
  1.4998        goto shm_open_err;
  1.4999      }
  1.5000      memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
  1.5001 @@ -31567,10 +33385,12 @@
  1.5002      pShmNode->h = -1;
  1.5003      pDbFd->pInode->pShmNode = pShmNode;
  1.5004      pShmNode->pInode = pDbFd->pInode;
  1.5005 -    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  1.5006 -    if( pShmNode->mutex==0 ){
  1.5007 -      rc = SQLITE_NOMEM;
  1.5008 -      goto shm_open_err;
  1.5009 +    if( sqlite3GlobalConfig.bCoreMutex ){
  1.5010 +      pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  1.5011 +      if( pShmNode->mutex==0 ){
  1.5012 +        rc = SQLITE_NOMEM_BKPT;
  1.5013 +        goto shm_open_err;
  1.5014 +      }
  1.5015      }
  1.5016  
  1.5017      if( pInode->bProcessLock==0 ){
  1.5018 @@ -31742,7 +33562,7 @@
  1.5019          pShmNode->apRegion, nReqRegion*sizeof(char *)
  1.5020      );
  1.5021      if( !apNew ){
  1.5022 -      rc = SQLITE_IOERR_NOMEM;
  1.5023 +      rc = SQLITE_IOERR_NOMEM_BKPT;
  1.5024        goto shmpage_out;
  1.5025      }
  1.5026      pShmNode->apRegion = apNew;
  1.5027 @@ -31762,7 +33582,7 @@
  1.5028        }else{
  1.5029          pMem = sqlite3_malloc64(szRegion);
  1.5030          if( pMem==0 ){
  1.5031 -          rc = SQLITE_NOMEM;
  1.5032 +          rc = SQLITE_NOMEM_BKPT;
  1.5033            goto shmpage_out;
  1.5034          }
  1.5035          memset(pMem, 0, szRegion);
  1.5036 @@ -32540,7 +34360,7 @@
  1.5037    pNew->pId = vxworksFindFileId(zFilename);
  1.5038    if( pNew->pId==0 ){
  1.5039      ctrlFlags |= UNIXFILE_NOLOCK;
  1.5040 -    rc = SQLITE_NOMEM;
  1.5041 +    rc = SQLITE_NOMEM_BKPT;
  1.5042    }
  1.5043  #endif
  1.5044  
  1.5045 @@ -32596,7 +34416,7 @@
  1.5046      afpLockingContext *pCtx;
  1.5047      pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
  1.5048      if( pCtx==0 ){
  1.5049 -      rc = SQLITE_NOMEM;
  1.5050 +      rc = SQLITE_NOMEM_BKPT;
  1.5051      }else{
  1.5052        /* NB: zFilename exists and remains valid until the file is closed
  1.5053        ** according to requirement F11141.  So we do not need to make a
  1.5054 @@ -32626,7 +34446,7 @@
  1.5055      nFilename = (int)strlen(zFilename) + 6;
  1.5056      zLockFile = (char *)sqlite3_malloc64(nFilename);
  1.5057      if( zLockFile==0 ){
  1.5058 -      rc = SQLITE_NOMEM;
  1.5059 +      rc = SQLITE_NOMEM_BKPT;
  1.5060      }else{
  1.5061        sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
  1.5062      }
  1.5063 @@ -32649,7 +34469,7 @@
  1.5064          if( zSemName[n]=='/' ) zSemName[n] = '_';
  1.5065        pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
  1.5066        if( pNew->pInode->pSem == SEM_FAILED ){
  1.5067 -        rc = SQLITE_NOMEM;
  1.5068 +        rc = SQLITE_NOMEM_BKPT;
  1.5069          pNew->pInode->aSemName[0] = '\0';
  1.5070        }
  1.5071      }
  1.5072 @@ -32689,20 +34509,24 @@
  1.5073       "/tmp",
  1.5074       "."
  1.5075    };
  1.5076 -  unsigned int i;
  1.5077 +  unsigned int i = 0;
  1.5078    struct stat buf;
  1.5079    const char *zDir = sqlite3_temp_directory;
  1.5080  
  1.5081    if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
  1.5082    if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
  1.5083 -  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
  1.5084 -    if( zDir==0 ) continue;
  1.5085 -    if( osStat(zDir, &buf) ) continue;
  1.5086 -    if( !S_ISDIR(buf.st_mode) ) continue;
  1.5087 -    if( osAccess(zDir, 07) ) continue;
  1.5088 -    break;
  1.5089 -  }
  1.5090 -  return zDir;
  1.5091 +  while(1){
  1.5092 +    if( zDir!=0
  1.5093 +     && osStat(zDir, &buf)==0
  1.5094 +     && S_ISDIR(buf.st_mode)
  1.5095 +     && osAccess(zDir, 03)==0
  1.5096 +    ){
  1.5097 +      return zDir;
  1.5098 +    }
  1.5099 +    if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
  1.5100 +    zDir = azDirs[i++];
  1.5101 +  }
  1.5102 +  return 0;
  1.5103  }
  1.5104  
  1.5105  /*
  1.5106 @@ -32718,9 +34542,11 @@
  1.5107    ** using the io-error infrastructure to test that SQLite handles this
  1.5108    ** function failing. 
  1.5109    */
  1.5110 +  zBuf[0] = 0;
  1.5111    SimulateIOError( return SQLITE_IOERR );
  1.5112  
  1.5113    zDir = unixTempFileDir();
  1.5114 +  if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
  1.5115    do{
  1.5116      u64 r;
  1.5117      sqlite3_randomness(sizeof(r), &r);
  1.5118 @@ -32990,7 +34816,7 @@
  1.5119      }else{
  1.5120        pUnused = sqlite3_malloc64(sizeof(*pUnused));
  1.5121        if( !pUnused ){
  1.5122 -        return SQLITE_NOMEM;
  1.5123 +        return SQLITE_NOMEM_BKPT;
  1.5124        }
  1.5125      }
  1.5126      p->pUnused = pUnused;
  1.5127 @@ -33076,7 +34902,7 @@
  1.5128      zPath = sqlite3_mprintf("%s", zName);
  1.5129      if( zPath==0 ){
  1.5130        robust_close(p, fd, __LINE__);
  1.5131 -      return SQLITE_NOMEM;
  1.5132 +      return SQLITE_NOMEM_BKPT;
  1.5133      }
  1.5134  #else
  1.5135      osUnlink(zName);
  1.5136 @@ -33087,9 +34913,6 @@
  1.5137      p->openFlags = openFlags;
  1.5138    }
  1.5139  #endif
  1.5140 -
  1.5141 -  noLock = eType!=SQLITE_OPEN_MAIN_DB;
  1.5142 -
  1.5143    
  1.5144  #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
  1.5145    if( fstatfs(fd, &fsInfo) == -1 ){
  1.5146 @@ -33108,6 +34931,7 @@
  1.5147    /* Set up appropriate ctrlFlags */
  1.5148    if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
  1.5149    if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
  1.5150 +  noLock = eType!=SQLITE_OPEN_MAIN_DB;
  1.5151    if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
  1.5152    if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
  1.5153    if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
  1.5154 @@ -33308,7 +35132,7 @@
  1.5155      if( bLink ){
  1.5156        if( zDel==0 ){
  1.5157          zDel = sqlite3_malloc(nOut);
  1.5158 -        if( zDel==0 ) rc = SQLITE_NOMEM;
  1.5159 +        if( zDel==0 ) rc = SQLITE_NOMEM_BKPT;
  1.5160        }else if( ++nLink>SQLITE_MAX_SYMLINKS ){
  1.5161          rc = SQLITE_CANTOPEN_BKPT;
  1.5162        }
  1.5163 @@ -33546,23 +35370,18 @@
  1.5164  # define unixCurrentTime 0
  1.5165  #endif
  1.5166  
  1.5167 -#ifndef SQLITE_OMIT_DEPRECATED
  1.5168 -/*
  1.5169 -** We added the xGetLastError() method with the intention of providing
  1.5170 -** better low-level error messages when operating-system problems come up
  1.5171 -** during SQLite operation.  But so far, none of that has been implemented
  1.5172 -** in the core.  So this routine is never called.  For now, it is merely
  1.5173 -** a place-holder.
  1.5174 +/*
  1.5175 +** The xGetLastError() method is designed to return a better
  1.5176 +** low-level error message when operating-system problems come up
  1.5177 +** during SQLite operation.  Only the integer return code is currently
  1.5178 +** used.
  1.5179  */
  1.5180  static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
  1.5181    UNUSED_PARAMETER(NotUsed);
  1.5182    UNUSED_PARAMETER(NotUsed2);
  1.5183    UNUSED_PARAMETER(NotUsed3);
  1.5184 -  return 0;
  1.5185 -}
  1.5186 -#else
  1.5187 -# define unixGetLastError 0
  1.5188 -#endif
  1.5189 +  return errno;
  1.5190 +}
  1.5191  
  1.5192  
  1.5193  /*
  1.5194 @@ -33852,7 +35671,7 @@
  1.5195    }else{
  1.5196      pUnused = sqlite3_malloc64(sizeof(*pUnused));
  1.5197      if( !pUnused ){
  1.5198 -      return SQLITE_NOMEM;
  1.5199 +      return SQLITE_NOMEM_BKPT;
  1.5200      }
  1.5201    }
  1.5202    if( fd<0 ){
  1.5203 @@ -33885,7 +35704,7 @@
  1.5204    
  1.5205    pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
  1.5206    if( pNew==NULL ){
  1.5207 -    rc = SQLITE_NOMEM;
  1.5208 +    rc = SQLITE_NOMEM_BKPT;
  1.5209      goto end_create_proxy;
  1.5210    }
  1.5211    memset(pNew, 0, sizeof(unixFile));
  1.5212 @@ -34298,7 +36117,7 @@
  1.5213          if( tempLockPath ){
  1.5214            pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
  1.5215            if( !pCtx->lockProxyPath ){
  1.5216 -            rc = SQLITE_NOMEM;
  1.5217 +            rc = SQLITE_NOMEM_BKPT;
  1.5218            }
  1.5219          }
  1.5220        }
  1.5221 @@ -34363,7 +36182,7 @@
  1.5222    ** the name of the original database file. */  
  1.5223    *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
  1.5224    if( conchPath==0 ){
  1.5225 -    return SQLITE_NOMEM;
  1.5226 +    return SQLITE_NOMEM_BKPT;
  1.5227    }
  1.5228    memcpy(conchPath, dbPath, len+1);
  1.5229    
  1.5230 @@ -34479,7 +36298,7 @@
  1.5231  
  1.5232    pCtx = sqlite3_malloc64( sizeof(*pCtx) );
  1.5233    if( pCtx==0 ){
  1.5234 -    return SQLITE_NOMEM;
  1.5235 +    return SQLITE_NOMEM_BKPT;
  1.5236    }
  1.5237    memset(pCtx, 0, sizeof(*pCtx));
  1.5238  
  1.5239 @@ -34515,7 +36334,7 @@
  1.5240    if( rc==SQLITE_OK ){
  1.5241      pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
  1.5242      if( pCtx->dbPath==NULL ){
  1.5243 -      rc = SQLITE_NOMEM;
  1.5244 +      rc = SQLITE_NOMEM_BKPT;
  1.5245      }
  1.5246    }
  1.5247    if( rc==SQLITE_OK ){
  1.5248 @@ -35414,10 +37233,22 @@
  1.5249  #endif
  1.5250  
  1.5251  /*
  1.5252 + * This is cache size used in the calculation of the initial size of the
  1.5253 + * Win32-specific heap.  It cannot be negative.
  1.5254 + */
  1.5255 +#ifndef SQLITE_WIN32_CACHE_SIZE
  1.5256 +#  if SQLITE_DEFAULT_CACHE_SIZE>=0
  1.5257 +#    define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
  1.5258 +#  else
  1.5259 +#    define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
  1.5260 +#  endif
  1.5261 +#endif
  1.5262 +
  1.5263 +/*
  1.5264   * The initial size of the Win32-specific heap.  This value may be zero.
  1.5265   */
  1.5266  #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
  1.5267 -#  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
  1.5268 +#  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
  1.5269                                         (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
  1.5270  #endif
  1.5271  
  1.5272 @@ -36291,7 +38122,7 @@
  1.5273      if( lastErrno==NO_ERROR ){
  1.5274        sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
  1.5275                    (void*)hHeap);
  1.5276 -      rc = SQLITE_NOMEM;
  1.5277 +      rc = SQLITE_NOMEM_BKPT;
  1.5278      }else{
  1.5279        sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
  1.5280                    osGetLastError(), (void*)hHeap);
  1.5281 @@ -36317,8 +38148,8 @@
  1.5282    int rc;
  1.5283    MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
  1.5284    MUTEX_LOGIC( sqlite3_mutex *pMem; )    /* The memsys static mutex */
  1.5285 -  MUTEX_LOGIC( pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); )
  1.5286 -  MUTEX_LOGIC( pMem = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); )
  1.5287 +  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
  1.5288 +  MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
  1.5289    sqlite3_mutex_enter(pMaster);
  1.5290    sqlite3_mutex_enter(pMem);
  1.5291    winMemAssertMagic();
  1.5292 @@ -36363,6 +38194,12 @@
  1.5293    int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
  1.5294    if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
  1.5295    assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
  1.5296 +#ifdef SQLITE_ENABLE_API_ARMOR
  1.5297 +  if( !zBuf ){
  1.5298 +    (void)SQLITE_MISUSE_BKPT;
  1.5299 +    return;
  1.5300 +  }
  1.5301 +#endif
  1.5302  #if defined(SQLITE_WIN32_HAS_ANSI)
  1.5303    if( nMin>0 ){
  1.5304      memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
  1.5305 @@ -36611,7 +38448,7 @@
  1.5306            "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
  1.5307            osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
  1.5308            dwMaximumSize);
  1.5309 -      return SQLITE_NOMEM;
  1.5310 +      return SQLITE_NOMEM_BKPT;
  1.5311      }
  1.5312      pWinMemData->bOwned = TRUE;
  1.5313      assert( pWinMemData->bOwned );
  1.5314 @@ -36621,7 +38458,7 @@
  1.5315    if( !pWinMemData->hHeap ){
  1.5316      sqlite3_log(SQLITE_NOMEM,
  1.5317          "failed to GetProcessHeap (%lu)", osGetLastError());
  1.5318 -    return SQLITE_NOMEM;
  1.5319 +    return SQLITE_NOMEM_BKPT;
  1.5320    }
  1.5321    pWinMemData->bOwned = FALSE;
  1.5322    assert( !pWinMemData->bOwned );
  1.5323 @@ -36688,147 +38525,244 @@
  1.5324  #endif /* SQLITE_WIN32_MALLOC */
  1.5325  
  1.5326  /*
  1.5327 -** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
  1.5328 -**
  1.5329 -** Space to hold the returned string is obtained from malloc.
  1.5330 -*/
  1.5331 -static LPWSTR winUtf8ToUnicode(const char *zFilename){
  1.5332 +** Convert a UTF-8 string to Microsoft Unicode.
  1.5333 +**
  1.5334 +** Space to hold the returned string is obtained from sqlite3_malloc().
  1.5335 +*/
  1.5336 +static LPWSTR winUtf8ToUnicode(const char *zText){
  1.5337    int nChar;
  1.5338 -  LPWSTR zWideFilename;
  1.5339 -
  1.5340 -  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
  1.5341 +  LPWSTR zWideText;
  1.5342 +
  1.5343 +  nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
  1.5344    if( nChar==0 ){
  1.5345      return 0;
  1.5346    }
  1.5347 -  zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
  1.5348 -  if( zWideFilename==0 ){
  1.5349 +  zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
  1.5350 +  if( zWideText==0 ){
  1.5351      return 0;
  1.5352    }
  1.5353 -  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
  1.5354 +  nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
  1.5355                                  nChar);
  1.5356    if( nChar==0 ){
  1.5357 -    sqlite3_free(zWideFilename);
  1.5358 -    zWideFilename = 0;
  1.5359 -  }
  1.5360 -  return zWideFilename;
  1.5361 -}
  1.5362 -
  1.5363 -/*
  1.5364 -** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
  1.5365 -** obtained from sqlite3_malloc().
  1.5366 -*/
  1.5367 -static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
  1.5368 +    sqlite3_free(zWideText);
  1.5369 +    zWideText = 0;
  1.5370 +  }
  1.5371 +  return zWideText;
  1.5372 +}
  1.5373 +
  1.5374 +/*
  1.5375 +** Convert a Microsoft Unicode string to UTF-8.
  1.5376 +**
  1.5377 +** Space to hold the returned string is obtained from sqlite3_malloc().
  1.5378 +*/
  1.5379 +static char *winUnicodeToUtf8(LPCWSTR zWideText){
  1.5380    int nByte;
  1.5381 -  char *zFilename;
  1.5382 -
  1.5383 -  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
  1.5384 +  char *zText;
  1.5385 +
  1.5386 +  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
  1.5387    if( nByte == 0 ){
  1.5388      return 0;
  1.5389    }
  1.5390 -  zFilename = sqlite3MallocZero( nByte );
  1.5391 -  if( zFilename==0 ){
  1.5392 +  zText = sqlite3MallocZero( nByte );
  1.5393 +  if( zText==0 ){
  1.5394      return 0;
  1.5395    }
  1.5396 -  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
  1.5397 +  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
  1.5398                                  0, 0);
  1.5399    if( nByte == 0 ){
  1.5400 -    sqlite3_free(zFilename);
  1.5401 -    zFilename = 0;
  1.5402 -  }
  1.5403 -  return zFilename;
  1.5404 -}
  1.5405 -
  1.5406 -/*
  1.5407 -** Convert an ANSI string to Microsoft Unicode, based on the
  1.5408 -** current codepage settings for file apis.
  1.5409 -**
  1.5410 -** Space to hold the returned string is obtained
  1.5411 -** from sqlite3_malloc.
  1.5412 -*/
  1.5413 -static LPWSTR winMbcsToUnicode(const char *zFilename){
  1.5414 +    sqlite3_free(zText);
  1.5415 +    zText = 0;
  1.5416 +  }
  1.5417 +  return zText;
  1.5418 +}
  1.5419 +
  1.5420 +/*
  1.5421 +** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
  1.5422 +** code page.
  1.5423 +**
  1.5424 +** Space to hold the returned string is obtained from sqlite3_malloc().
  1.5425 +*/
  1.5426 +static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
  1.5427    int nByte;
  1.5428 -  LPWSTR zMbcsFilename;
  1.5429 -  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
  1.5430 -
  1.5431 -  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
  1.5432 +  LPWSTR zMbcsText;
  1.5433 +  int codepage = useAnsi ? CP_ACP : CP_OEMCP;
  1.5434 +
  1.5435 +  nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
  1.5436                                  0)*sizeof(WCHAR);
  1.5437    if( nByte==0 ){
  1.5438      return 0;
  1.5439    }
  1.5440 -  zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
  1.5441 -  if( zMbcsFilename==0 ){
  1.5442 +  zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
  1.5443 +  if( zMbcsText==0 ){
  1.5444      return 0;
  1.5445    }
  1.5446 -  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
  1.5447 +  nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
  1.5448                                  nByte);
  1.5449    if( nByte==0 ){
  1.5450 -    sqlite3_free(zMbcsFilename);
  1.5451 -    zMbcsFilename = 0;
  1.5452 -  }
  1.5453 -  return zMbcsFilename;
  1.5454 -}
  1.5455 -
  1.5456 -/*
  1.5457 -** Convert Microsoft Unicode to multi-byte character string, based on the
  1.5458 -** user's ANSI codepage.
  1.5459 -**
  1.5460 -** Space to hold the returned string is obtained from
  1.5461 -** sqlite3_malloc().
  1.5462 -*/
  1.5463 -static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
  1.5464 +    sqlite3_free(zMbcsText);
  1.5465 +    zMbcsText = 0;
  1.5466 +  }
  1.5467 +  return zMbcsText;
  1.5468 +}
  1.5469 +
  1.5470 +/*
  1.5471 +** Convert a Microsoft Unicode string to a multi-byte character string,
  1.5472 +** using the ANSI or OEM code page.
  1.5473 +**
  1.5474 +** Space to hold the returned string is obtained from sqlite3_malloc().
  1.5475 +*/
  1.5476 +static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
  1.5477    int nByte;
  1.5478 -  char *zFilename;
  1.5479 -  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
  1.5480 -
  1.5481 -  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
  1.5482 +  char *zText;
  1.5483 +  int codepage = useAnsi ? CP_ACP : CP_OEMCP;
  1.5484 +
  1.5485 +  nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
  1.5486    if( nByte == 0 ){
  1.5487      return 0;
  1.5488    }
  1.5489 -  zFilename = sqlite3MallocZero( nByte );
  1.5490 -  if( zFilename==0 ){
  1.5491 +  zText = sqlite3MallocZero( nByte );
  1.5492 +  if( zText==0 ){
  1.5493      return 0;
  1.5494    }
  1.5495 -  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
  1.5496 +  nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
  1.5497                                  nByte, 0, 0);
  1.5498    if( nByte == 0 ){
  1.5499 -    sqlite3_free(zFilename);
  1.5500 -    zFilename = 0;
  1.5501 -  }
  1.5502 -  return zFilename;
  1.5503 -}
  1.5504 -
  1.5505 -/*
  1.5506 -** Convert multibyte character string to UTF-8.  Space to hold the
  1.5507 -** returned string is obtained from sqlite3_malloc().
  1.5508 -*/
  1.5509 -SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8(const char *zFilename){
  1.5510 -  char *zFilenameUtf8;
  1.5511 +    sqlite3_free(zText);
  1.5512 +    zText = 0;
  1.5513 +  }
  1.5514 +  return zText;
  1.5515 +}
  1.5516 +
  1.5517 +/*
  1.5518 +** Convert a multi-byte character string to UTF-8.
  1.5519 +**
  1.5520 +** Space to hold the returned string is obtained from sqlite3_malloc().
  1.5521 +*/
  1.5522 +static char *winMbcsToUtf8(const char *zText, int useAnsi){
  1.5523 +  char *zTextUtf8;
  1.5524    LPWSTR zTmpWide;
  1.5525  
  1.5526 -  zTmpWide = winMbcsToUnicode(zFilename);
  1.5527 +  zTmpWide = winMbcsToUnicode(zText, useAnsi);
  1.5528    if( zTmpWide==0 ){
  1.5529      return 0;
  1.5530    }
  1.5531 -  zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
  1.5532 +  zTextUtf8 = winUnicodeToUtf8(zTmpWide);
  1.5533    sqlite3_free(zTmpWide);
  1.5534 -  return zFilenameUtf8;
  1.5535 -}
  1.5536 -
  1.5537 -/*
  1.5538 -** Convert UTF-8 to multibyte character string.  Space to hold the
  1.5539 -** returned string is obtained from sqlite3_malloc().
  1.5540 -*/
  1.5541 -SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs(const char *zFilename){
  1.5542 -  char *zFilenameMbcs;
  1.5543 +  return zTextUtf8;
  1.5544 +}
  1.5545 +
  1.5546 +/*
  1.5547 +** Convert a UTF-8 string to a multi-byte character string.
  1.5548 +**
  1.5549 +** Space to hold the returned string is obtained from sqlite3_malloc().
  1.5550 +*/
  1.5551 +static char *winUtf8ToMbcs(const char *zText, int useAnsi){
  1.5552 +  char *zTextMbcs;
  1.5553    LPWSTR zTmpWide;
  1.5554  
  1.5555 -  zTmpWide = winUtf8ToUnicode(zFilename);
  1.5556 +  zTmpWide = winUtf8ToUnicode(zText);
  1.5557    if( zTmpWide==0 ){
  1.5558      return 0;
  1.5559    }
  1.5560 -  zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
  1.5561 +  zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
  1.5562    sqlite3_free(zTmpWide);
  1.5563 -  return zFilenameMbcs;
  1.5564 +  return zTextMbcs;
  1.5565 +}
  1.5566 +
  1.5567 +/*
  1.5568 +** This is a public wrapper for the winUtf8ToUnicode() function.
  1.5569 +*/
  1.5570 +SQLITE_API LPWSTR SQLITE_STDCALL sqlite3_win32_utf8_to_unicode(const char *zText){
  1.5571 +#ifdef SQLITE_ENABLE_API_ARMOR
  1.5572 +  if( !zText ){
  1.5573 +    (void)SQLITE_MISUSE_BKPT;
  1.5574 +    return 0;
  1.5575 +  }
  1.5576 +#endif
  1.5577 +#ifndef SQLITE_OMIT_AUTOINIT
  1.5578 +  if( sqlite3_initialize() ) return 0;
  1.5579 +#endif
  1.5580 +  return winUtf8ToUnicode(zText);
  1.5581 +}
  1.5582 +
  1.5583 +/*
  1.5584 +** This is a public wrapper for the winUnicodeToUtf8() function.
  1.5585 +*/
  1.5586 +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
  1.5587 +#ifdef SQLITE_ENABLE_API_ARMOR
  1.5588 +  if( !zWideText ){
  1.5589 +    (void)SQLITE_MISUSE_BKPT;
  1.5590 +    return 0;
  1.5591 +  }
  1.5592 +#endif
  1.5593 +#ifndef SQLITE_OMIT_AUTOINIT
  1.5594 +  if( sqlite3_initialize() ) return 0;
  1.5595 +#endif
  1.5596 +  return winUnicodeToUtf8(zWideText);
  1.5597 +}
  1.5598 +
  1.5599 +/*
  1.5600 +** This is a public wrapper for the winMbcsToUtf8() function.
  1.5601 +*/
  1.5602 +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8(const char *zText){
  1.5603 +#ifdef SQLITE_ENABLE_API_ARMOR
  1.5604 +  if( !zText ){
  1.5605 +    (void)SQLITE_MISUSE_BKPT;
  1.5606 +    return 0;
  1.5607 +  }
  1.5608 +#endif
  1.5609 +#ifndef SQLITE_OMIT_AUTOINIT
  1.5610 +  if( sqlite3_initialize() ) return 0;
  1.5611 +#endif
  1.5612 +  return winMbcsToUtf8(zText, osAreFileApisANSI());
  1.5613 +}
  1.5614 +
  1.5615 +/*
  1.5616 +** This is a public wrapper for the winMbcsToUtf8() function.
  1.5617 +*/
  1.5618 +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
  1.5619 +#ifdef SQLITE_ENABLE_API_ARMOR
  1.5620 +  if( !zText ){
  1.5621 +    (void)SQLITE_MISUSE_BKPT;
  1.5622 +    return 0;
  1.5623 +  }
  1.5624 +#endif
  1.5625 +#ifndef SQLITE_OMIT_AUTOINIT
  1.5626 +  if( sqlite3_initialize() ) return 0;
  1.5627 +#endif
  1.5628 +  return winMbcsToUtf8(zText, useAnsi);
  1.5629 +}
  1.5630 +
  1.5631 +/*
  1.5632 +** This is a public wrapper for the winUtf8ToMbcs() function.
  1.5633 +*/
  1.5634 +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs(const char *zText){
  1.5635 +#ifdef SQLITE_ENABLE_API_ARMOR
  1.5636 +  if( !zText ){
  1.5637 +    (void)SQLITE_MISUSE_BKPT;
  1.5638 +    return 0;
  1.5639 +  }
  1.5640 +#endif
  1.5641 +#ifndef SQLITE_OMIT_AUTOINIT
  1.5642 +  if( sqlite3_initialize() ) return 0;
  1.5643 +#endif
  1.5644 +  return winUtf8ToMbcs(zText, osAreFileApisANSI());
  1.5645 +}
  1.5646 +
  1.5647 +/*
  1.5648 +** This is a public wrapper for the winUtf8ToMbcs() function.
  1.5649 +*/
  1.5650 +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
  1.5651 +#ifdef SQLITE_ENABLE_API_ARMOR
  1.5652 +  if( !zText ){
  1.5653 +    (void)SQLITE_MISUSE_BKPT;
  1.5654 +    return 0;
  1.5655 +  }
  1.5656 +#endif
  1.5657 +#ifndef SQLITE_OMIT_AUTOINIT
  1.5658 +  if( sqlite3_initialize() ) return 0;
  1.5659 +#endif
  1.5660 +  return winUtf8ToMbcs(zText, useAnsi);
  1.5661  }
  1.5662  
  1.5663  /*
  1.5664 @@ -36858,7 +38792,7 @@
  1.5665      if( zValue && zValue[0] ){
  1.5666        zValueUtf8 = winUnicodeToUtf8(zValue);
  1.5667        if ( zValueUtf8==0 ){
  1.5668 -        return SQLITE_NOMEM;
  1.5669 +        return SQLITE_NOMEM_BKPT;
  1.5670        }
  1.5671      }
  1.5672      sqlite3_free(*ppDirectory);
  1.5673 @@ -36930,7 +38864,7 @@
  1.5674      if( dwLen > 0 ){
  1.5675        /* allocate a buffer and convert to UTF8 */
  1.5676        sqlite3BeginBenignMalloc();
  1.5677 -      zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
  1.5678 +      zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
  1.5679        sqlite3EndBenignMalloc();
  1.5680        /* free the system buffer allocated by FormatMessage */
  1.5681        osLocalFree(zTemp);
  1.5682 @@ -37072,16 +39006,17 @@
  1.5683    }
  1.5684  }
  1.5685  
  1.5686 -#if SQLITE_OS_WINCE
  1.5687 -/*************************************************************************
  1.5688 -** This section contains code for WinCE only.
  1.5689 -*/
  1.5690 -#if !defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API
  1.5691 -/*
  1.5692 -** The MSVC CRT on Windows CE may not have a localtime() function.  So
  1.5693 -** create a substitute.
  1.5694 -*/
  1.5695 -/* #include <time.h> */
  1.5696 +/*
  1.5697 +** This #if does not rely on the SQLITE_OS_WINCE define because the
  1.5698 +** corresponding section in "date.c" cannot use it.
  1.5699 +*/
  1.5700 +#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
  1.5701 +    (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
  1.5702 +/*
  1.5703 +** The MSVC CRT on Windows CE may not have a localtime() function.
  1.5704 +** So define a substitute.
  1.5705 +*/
  1.5706 +/* #  include <time.h> */
  1.5707  struct tm *__cdecl localtime(const time_t *t)
  1.5708  {
  1.5709    static struct tm y;
  1.5710 @@ -37105,6 +39040,10 @@
  1.5711  }
  1.5712  #endif
  1.5713  
  1.5714 +#if SQLITE_OS_WINCE
  1.5715 +/*************************************************************************
  1.5716 +** This section contains code for WinCE only.
  1.5717 +*/
  1.5718  #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
  1.5719  
  1.5720  /*
  1.5721 @@ -37135,7 +39074,7 @@
  1.5722    zName = winUtf8ToUnicode(zFilename);
  1.5723    if( zName==0 ){
  1.5724      /* out of memory */
  1.5725 -    return SQLITE_IOERR_NOMEM;
  1.5726 +    return SQLITE_IOERR_NOMEM_BKPT;
  1.5727    }
  1.5728  
  1.5729    /* Initialize the local lockdata */
  1.5730 @@ -38118,9 +40057,8 @@
  1.5731    ** the PENDING_LOCK byte is temporary.
  1.5732    */
  1.5733    newLocktype = pFile->locktype;
  1.5734 -  if(   (pFile->locktype==NO_LOCK)
  1.5735 -     || (   (locktype==EXCLUSIVE_LOCK)
  1.5736 -         && (pFile->locktype==RESERVED_LOCK))
  1.5737 +  if( pFile->locktype==NO_LOCK
  1.5738 +   || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
  1.5739    ){
  1.5740      int cnt = 3;
  1.5741      while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
  1.5742 @@ -38326,7 +40264,7 @@
  1.5743        OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  1.5744        return SQLITE_OK;
  1.5745      }
  1.5746 -    case SQLITE_LAST_ERRNO: {
  1.5747 +    case SQLITE_FCNTL_LAST_ERRNO: {
  1.5748        *(int*)pArg = (int)pFile->lastErrno;
  1.5749        OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  1.5750        return SQLITE_OK;
  1.5751 @@ -38684,12 +40622,12 @@
  1.5752    ** allocate space for a new winShmNode and filename.
  1.5753    */
  1.5754    p = sqlite3MallocZero( sizeof(*p) );
  1.5755 -  if( p==0 ) return SQLITE_IOERR_NOMEM;
  1.5756 +  if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
  1.5757    nName = sqlite3Strlen30(pDbFd->zPath);
  1.5758    pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
  1.5759    if( pNew==0 ){
  1.5760      sqlite3_free(p);
  1.5761 -    return SQLITE_IOERR_NOMEM;
  1.5762 +    return SQLITE_IOERR_NOMEM_BKPT;
  1.5763    }
  1.5764    pNew->zFilename = (char*)&pNew[1];
  1.5765    sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
  1.5766 @@ -38714,10 +40652,12 @@
  1.5767      pShmNode->pNext = winShmNodeList;
  1.5768      winShmNodeList = pShmNode;
  1.5769  
  1.5770 -    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  1.5771 -    if( pShmNode->mutex==0 ){
  1.5772 -      rc = SQLITE_IOERR_NOMEM;
  1.5773 -      goto shm_open_err;
  1.5774 +    if( sqlite3GlobalConfig.bCoreMutex ){
  1.5775 +      pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  1.5776 +      if( pShmNode->mutex==0 ){
  1.5777 +        rc = SQLITE_IOERR_NOMEM_BKPT;
  1.5778 +        goto shm_open_err;
  1.5779 +      }
  1.5780      }
  1.5781  
  1.5782      rc = winOpen(pDbFd->pVfs,
  1.5783 @@ -39021,7 +40961,7 @@
  1.5784          pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
  1.5785      );
  1.5786      if( !apNew ){
  1.5787 -      rc = SQLITE_IOERR_NOMEM;
  1.5788 +      rc = SQLITE_IOERR_NOMEM_BKPT;
  1.5789        goto shmpage_out;
  1.5790      }
  1.5791      pShmNode->aRegion = apNew;
  1.5792 @@ -39375,7 +41315,7 @@
  1.5793    }
  1.5794  #ifdef SQLITE_WIN32_HAS_ANSI
  1.5795    else{
  1.5796 -    zConverted = sqlite3_win32_mbcs_to_utf8(zFilename);
  1.5797 +    zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
  1.5798    }
  1.5799  #endif
  1.5800    /* caller will handle out of memory */
  1.5801 @@ -39396,7 +41336,7 @@
  1.5802    }
  1.5803  #ifdef SQLITE_WIN32_HAS_ANSI
  1.5804    else{
  1.5805 -    zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
  1.5806 +    zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
  1.5807    }
  1.5808  #endif
  1.5809    /* caller will handle out of memory */
  1.5810 @@ -39451,7 +41391,7 @@
  1.5811    zBuf = sqlite3MallocZero( nBuf );
  1.5812    if( !zBuf ){
  1.5813      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  1.5814 -    return SQLITE_IOERR_NOMEM;
  1.5815 +    return SQLITE_IOERR_NOMEM_BKPT;
  1.5816    }
  1.5817  
  1.5818    /* Figure out the effective temporary directory.  First, check if one
  1.5819 @@ -39509,7 +41449,7 @@
  1.5820          if( !zConverted ){
  1.5821            sqlite3_free(zBuf);
  1.5822            OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  1.5823 -          return SQLITE_IOERR_NOMEM;
  1.5824 +          return SQLITE_IOERR_NOMEM_BKPT;
  1.5825          }
  1.5826          if( winIsDir(zConverted) ){
  1.5827            sqlite3_snprintf(nMax, zBuf, "%s", zDir);
  1.5828 @@ -39522,7 +41462,7 @@
  1.5829          if( !zConverted ){
  1.5830            sqlite3_free(zBuf);
  1.5831            OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  1.5832 -          return SQLITE_IOERR_NOMEM;
  1.5833 +          return SQLITE_IOERR_NOMEM_BKPT;
  1.5834          }
  1.5835          if( cygwin_conv_path(
  1.5836                  osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
  1.5837 @@ -39543,7 +41483,7 @@
  1.5838              sqlite3_free(zConverted);
  1.5839              sqlite3_free(zBuf);
  1.5840              OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  1.5841 -            return SQLITE_IOERR_NOMEM;
  1.5842 +            return SQLITE_IOERR_NOMEM_BKPT;
  1.5843            }
  1.5844            sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
  1.5845            sqlite3_free(zUtf8);
  1.5846 @@ -39561,7 +41501,7 @@
  1.5847      if( !zWidePath ){
  1.5848        sqlite3_free(zBuf);
  1.5849        OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  1.5850 -      return SQLITE_IOERR_NOMEM;
  1.5851 +      return SQLITE_IOERR_NOMEM_BKPT;
  1.5852      }
  1.5853      if( osGetTempPathW(nMax, zWidePath)==0 ){
  1.5854        sqlite3_free(zWidePath);
  1.5855 @@ -39579,7 +41519,7 @@
  1.5856        sqlite3_free(zWidePath);
  1.5857        sqlite3_free(zBuf);
  1.5858        OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  1.5859 -      return SQLITE_IOERR_NOMEM;
  1.5860 +      return SQLITE_IOERR_NOMEM_BKPT;
  1.5861      }
  1.5862    }
  1.5863  #ifdef SQLITE_WIN32_HAS_ANSI
  1.5864 @@ -39589,7 +41529,7 @@
  1.5865      if( !zMbcsPath ){
  1.5866        sqlite3_free(zBuf);
  1.5867        OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  1.5868 -      return SQLITE_IOERR_NOMEM;
  1.5869 +      return SQLITE_IOERR_NOMEM_BKPT;
  1.5870      }
  1.5871      if( osGetTempPathA(nMax, zMbcsPath)==0 ){
  1.5872        sqlite3_free(zBuf);
  1.5873 @@ -39597,14 +41537,14 @@
  1.5874        return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
  1.5875                           "winGetTempname3", 0);
  1.5876      }
  1.5877 -    zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
  1.5878 +    zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
  1.5879      if( zUtf8 ){
  1.5880        sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
  1.5881        sqlite3_free(zUtf8);
  1.5882      }else{
  1.5883        sqlite3_free(zBuf);
  1.5884        OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  1.5885 -      return SQLITE_IOERR_NOMEM;
  1.5886 +      return SQLITE_IOERR_NOMEM_BKPT;
  1.5887      }
  1.5888    }
  1.5889  #endif /* SQLITE_WIN32_HAS_ANSI */
  1.5890 @@ -39796,7 +41736,7 @@
  1.5891    if( zConverted==0 ){
  1.5892      sqlite3_free(zTmpname);
  1.5893      OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
  1.5894 -    return SQLITE_IOERR_NOMEM;
  1.5895 +    return SQLITE_IOERR_NOMEM_BKPT;
  1.5896    }
  1.5897  
  1.5898    if( winIsDir(zConverted) ){
  1.5899 @@ -39996,7 +41936,7 @@
  1.5900    zConverted = winConvertFromUtf8Filename(zFilename);
  1.5901    if( zConverted==0 ){
  1.5902      OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
  1.5903 -    return SQLITE_IOERR_NOMEM;
  1.5904 +    return SQLITE_IOERR_NOMEM_BKPT;
  1.5905    }
  1.5906    if( osIsNT() ){
  1.5907      do {
  1.5908 @@ -40104,7 +42044,7 @@
  1.5909    zConverted = winConvertFromUtf8Filename(zFilename);
  1.5910    if( zConverted==0 ){
  1.5911      OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
  1.5912 -    return SQLITE_IOERR_NOMEM;
  1.5913 +    return SQLITE_IOERR_NOMEM_BKPT;
  1.5914    }
  1.5915    if( osIsNT() ){
  1.5916      int cnt = 0;
  1.5917 @@ -40231,7 +42171,7 @@
  1.5918      */
  1.5919      char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
  1.5920      if( !zOut ){
  1.5921 -      return SQLITE_IOERR_NOMEM;
  1.5922 +      return SQLITE_IOERR_NOMEM_BKPT;
  1.5923      }
  1.5924      if( cygwin_conv_path(
  1.5925              (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
  1.5926 @@ -40243,7 +42183,7 @@
  1.5927        char *zUtf8 = winConvertToUtf8Filename(zOut);
  1.5928        if( !zUtf8 ){
  1.5929          sqlite3_free(zOut);
  1.5930 -        return SQLITE_IOERR_NOMEM;
  1.5931 +        return SQLITE_IOERR_NOMEM_BKPT;
  1.5932        }
  1.5933        sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
  1.5934                         sqlite3_data_directory, winGetDirSep(), zUtf8);
  1.5935 @@ -40253,7 +42193,7 @@
  1.5936    }else{
  1.5937      char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
  1.5938      if( !zOut ){
  1.5939 -      return SQLITE_IOERR_NOMEM;
  1.5940 +      return SQLITE_IOERR_NOMEM_BKPT;
  1.5941      }
  1.5942      if( cygwin_conv_path(
  1.5943              (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
  1.5944 @@ -40265,7 +42205,7 @@
  1.5945        char *zUtf8 = winConvertToUtf8Filename(zOut);
  1.5946        if( !zUtf8 ){
  1.5947          sqlite3_free(zOut);
  1.5948 -        return SQLITE_IOERR_NOMEM;
  1.5949 +        return SQLITE_IOERR_NOMEM_BKPT;
  1.5950        }
  1.5951        sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
  1.5952        sqlite3_free(zUtf8);
  1.5953 @@ -40325,7 +42265,7 @@
  1.5954    }
  1.5955    zConverted = winConvertFromUtf8Filename(zRelative);
  1.5956    if( zConverted==0 ){
  1.5957 -    return SQLITE_IOERR_NOMEM;
  1.5958 +    return SQLITE_IOERR_NOMEM_BKPT;
  1.5959    }
  1.5960    if( osIsNT() ){
  1.5961      LPWSTR zTemp;
  1.5962 @@ -40339,7 +42279,7 @@
  1.5963      zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
  1.5964      if( zTemp==0 ){
  1.5965        sqlite3_free(zConverted);
  1.5966 -      return SQLITE_IOERR_NOMEM;
  1.5967 +      return SQLITE_IOERR_NOMEM_BKPT;
  1.5968      }
  1.5969      nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
  1.5970      if( nByte==0 ){
  1.5971 @@ -40365,7 +42305,7 @@
  1.5972      zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
  1.5973      if( zTemp==0 ){
  1.5974        sqlite3_free(zConverted);
  1.5975 -      return SQLITE_IOERR_NOMEM;
  1.5976 +      return SQLITE_IOERR_NOMEM_BKPT;
  1.5977      }
  1.5978      nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
  1.5979      if( nByte==0 ){
  1.5980 @@ -40375,7 +42315,7 @@
  1.5981                           "winFullPathname4", zRelative);
  1.5982      }
  1.5983      sqlite3_free(zConverted);
  1.5984 -    zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
  1.5985 +    zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
  1.5986      sqlite3_free(zTemp);
  1.5987    }
  1.5988  #endif
  1.5989 @@ -40384,7 +42324,7 @@
  1.5990      sqlite3_free(zOut);
  1.5991      return SQLITE_OK;
  1.5992    }else{
  1.5993 -    return SQLITE_IOERR_NOMEM;
  1.5994 +    return SQLITE_IOERR_NOMEM_BKPT;
  1.5995    }
  1.5996  #endif
  1.5997  }
  1.5998 @@ -40459,65 +42399,85 @@
  1.5999    #define winDlClose 0
  1.6000  #endif
  1.6001  
  1.6002 +/* State information for the randomness gatherer. */
  1.6003 +typedef struct EntropyGatherer EntropyGatherer;
  1.6004 +struct EntropyGatherer {
  1.6005 +  unsigned char *a;   /* Gather entropy into this buffer */
  1.6006 +  int na;             /* Size of a[] in bytes */
  1.6007 +  int i;              /* XOR next input into a[i] */
  1.6008 +  int nXor;           /* Number of XOR operations done */
  1.6009 +};
  1.6010 +
  1.6011 +#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
  1.6012 +/* Mix sz bytes of entropy into p. */
  1.6013 +static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
  1.6014 +  int j, k;
  1.6015 +  for(j=0, k=p->i; j<sz; j++){
  1.6016 +    p->a[k++] ^= x[j];
  1.6017 +    if( k>=p->na ) k = 0;
  1.6018 +  }
  1.6019 +  p->i = k;
  1.6020 +  p->nXor += sz;
  1.6021 +}
  1.6022 +#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
  1.6023  
  1.6024  /*
  1.6025  ** Write up to nBuf bytes of randomness into zBuf.
  1.6026  */
  1.6027  static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  1.6028 -  int n = 0;
  1.6029 +#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
  1.6030    UNUSED_PARAMETER(pVfs);
  1.6031 -#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
  1.6032 -  n = nBuf;
  1.6033    memset(zBuf, 0, nBuf);
  1.6034 -#else
  1.6035 -  if( sizeof(SYSTEMTIME)<=nBuf-n ){
  1.6036 +  return nBuf;
  1.6037 +#else
  1.6038 +  EntropyGatherer e;
  1.6039 +  UNUSED_PARAMETER(pVfs);
  1.6040 +  memset(zBuf, 0, nBuf);
  1.6041 +#if defined(_MSC_VER) && _MSC_VER>=1400 && !SQLITE_OS_WINCE
  1.6042 +  rand_s((unsigned int*)zBuf); /* rand_s() is not available with MinGW */
  1.6043 +#endif /* defined(_MSC_VER) && _MSC_VER>=1400 */
  1.6044 +  e.a = (unsigned char*)zBuf;
  1.6045 +  e.na = nBuf;
  1.6046 +  e.nXor = 0;
  1.6047 +  e.i = 0;
  1.6048 +  {
  1.6049      SYSTEMTIME x;
  1.6050      osGetSystemTime(&x);
  1.6051 -    memcpy(&zBuf[n], &x, sizeof(x));
  1.6052 -    n += sizeof(x);
  1.6053 -  }
  1.6054 -  if( sizeof(DWORD)<=nBuf-n ){
  1.6055 +    xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
  1.6056 +  }
  1.6057 +  {
  1.6058      DWORD pid = osGetCurrentProcessId();
  1.6059 -    memcpy(&zBuf[n], &pid, sizeof(pid));
  1.6060 -    n += sizeof(pid);
  1.6061 +    xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
  1.6062    }
  1.6063  #if SQLITE_OS_WINRT
  1.6064 -  if( sizeof(ULONGLONG)<=nBuf-n ){
  1.6065 +  {
  1.6066      ULONGLONG cnt = osGetTickCount64();
  1.6067 -    memcpy(&zBuf[n], &cnt, sizeof(cnt));
  1.6068 -    n += sizeof(cnt);
  1.6069 -  }
  1.6070 -#else
  1.6071 -  if( sizeof(DWORD)<=nBuf-n ){
  1.6072 +    xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
  1.6073 +  }
  1.6074 +#else
  1.6075 +  {
  1.6076      DWORD cnt = osGetTickCount();
  1.6077 -    memcpy(&zBuf[n], &cnt, sizeof(cnt));
  1.6078 -    n += sizeof(cnt);
  1.6079 -  }
  1.6080 -#endif
  1.6081 -  if( sizeof(LARGE_INTEGER)<=nBuf-n ){
  1.6082 +    xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
  1.6083 +  }
  1.6084 +#endif /* SQLITE_OS_WINRT */
  1.6085 +  {
  1.6086      LARGE_INTEGER i;
  1.6087      osQueryPerformanceCounter(&i);
  1.6088 -    memcpy(&zBuf[n], &i, sizeof(i));
  1.6089 -    n += sizeof(i);
  1.6090 +    xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
  1.6091    }
  1.6092  #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
  1.6093 -  if( sizeof(UUID)<=nBuf-n ){
  1.6094 +  {
  1.6095      UUID id;
  1.6096      memset(&id, 0, sizeof(UUID));
  1.6097      osUuidCreate(&id);
  1.6098 -    memcpy(&zBuf[n], &id, sizeof(UUID));
  1.6099 -    n += sizeof(UUID);
  1.6100 -  }
  1.6101 -  if( sizeof(UUID)<=nBuf-n ){
  1.6102 -    UUID id;
  1.6103 +    xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
  1.6104      memset(&id, 0, sizeof(UUID));
  1.6105      osUuidCreateSequential(&id);
  1.6106 -    memcpy(&zBuf[n], &id, sizeof(UUID));
  1.6107 -    n += sizeof(UUID);
  1.6108 -  }
  1.6109 -#endif
  1.6110 -#endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */
  1.6111 -  return n;
  1.6112 +    xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
  1.6113 +  }
  1.6114 +#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
  1.6115 +  return e.nXor>nBuf ? nBuf : e.nXor;
  1.6116 +#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
  1.6117  }
  1.6118  
  1.6119  
  1.6120 @@ -40633,8 +42593,10 @@
  1.6121  ** sqlite3_errmsg(), possibly making IO errors easier to debug.
  1.6122  */
  1.6123  static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  1.6124 +  DWORD e = osGetLastError();
  1.6125    UNUSED_PARAMETER(pVfs);
  1.6126 -  return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
  1.6127 +  if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
  1.6128 +  return e;
  1.6129  }
  1.6130  
  1.6131  /*
  1.6132 @@ -40908,7 +42870,7 @@
  1.6133      i = i%p->iDivisor;
  1.6134      if( p->u.apSub[bin]==0 ){
  1.6135        p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
  1.6136 -      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
  1.6137 +      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
  1.6138      }
  1.6139      p = p->u.apSub[bin];
  1.6140    }
  1.6141 @@ -40943,7 +42905,7 @@
  1.6142      int rc;
  1.6143      u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
  1.6144      if( aiValues==0 ){
  1.6145 -      return SQLITE_NOMEM;
  1.6146 +      return SQLITE_NOMEM_BKPT;
  1.6147      }else{
  1.6148        memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
  1.6149        memset(p->u.apSub, 0, sizeof(p->u.apSub));
  1.6150 @@ -41159,7 +43121,29 @@
  1.6151  /* #include "sqliteInt.h" */
  1.6152  
  1.6153  /*
  1.6154 -** A complete page cache is an instance of this structure.
  1.6155 +** A complete page cache is an instance of this structure.  Every
  1.6156 +** entry in the cache holds a single page of the database file.  The
  1.6157 +** btree layer only operates on the cached copy of the database pages.
  1.6158 +**
  1.6159 +** A page cache entry is "clean" if it exactly matches what is currently
  1.6160 +** on disk.  A page is "dirty" if it has been modified and needs to be
  1.6161 +** persisted to disk.
  1.6162 +**
  1.6163 +** pDirty, pDirtyTail, pSynced:
  1.6164 +**   All dirty pages are linked into the doubly linked list using
  1.6165 +**   PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
  1.6166 +**   such that p was added to the list more recently than p->pDirtyNext.
  1.6167 +**   PCache.pDirty points to the first (newest) element in the list and
  1.6168 +**   pDirtyTail to the last (oldest).
  1.6169 +**
  1.6170 +**   The PCache.pSynced variable is used to optimize searching for a dirty
  1.6171 +**   page to eject from the cache mid-transaction. It is better to eject
  1.6172 +**   a page that does not require a journal sync than one that does. 
  1.6173 +**   Therefore, pSynced is maintained to that it *almost* always points
  1.6174 +**   to either the oldest page in the pDirty/pDirtyTail list that has a
  1.6175 +**   clear PGHDR_NEED_SYNC flag or to a page that is older than this one
  1.6176 +**   (so that the right page to eject can be found by following pDirtyPrev
  1.6177 +**   pointers).
  1.6178  */
  1.6179  struct PCache {
  1.6180    PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
  1.6181 @@ -41176,6 +43160,95 @@
  1.6182    sqlite3_pcache *pCache;             /* Pluggable cache module */
  1.6183  };
  1.6184  
  1.6185 +/********************************** Test and Debug Logic **********************/
  1.6186 +/*
  1.6187 +** Debug tracing macros.  Enable by by changing the "0" to "1" and
  1.6188 +** recompiling.
  1.6189 +**
  1.6190 +** When sqlite3PcacheTrace is 1, single line trace messages are issued.
  1.6191 +** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
  1.6192 +** is displayed for many operations, resulting in a lot of output.
  1.6193 +*/
  1.6194 +#if defined(SQLITE_DEBUG) && 0
  1.6195 +  int sqlite3PcacheTrace = 2;       /* 0: off  1: simple  2: cache dumps */
  1.6196 +  int sqlite3PcacheMxDump = 9999;   /* Max cache entries for pcacheDump() */
  1.6197 +# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
  1.6198 +  void pcacheDump(PCache *pCache){
  1.6199 +    int N;
  1.6200 +    int i, j;
  1.6201 +    sqlite3_pcache_page *pLower;
  1.6202 +    PgHdr *pPg;
  1.6203 +    unsigned char *a;
  1.6204 +  
  1.6205 +    if( sqlite3PcacheTrace<2 ) return;
  1.6206 +    if( pCache->pCache==0 ) return;
  1.6207 +    N = sqlite3PcachePagecount(pCache);
  1.6208 +    if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
  1.6209 +    for(i=1; i<=N; i++){
  1.6210 +       pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
  1.6211 +       if( pLower==0 ) continue;
  1.6212 +       pPg = (PgHdr*)pLower->pExtra;
  1.6213 +       printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
  1.6214 +       a = (unsigned char *)pLower->pBuf;
  1.6215 +       for(j=0; j<12; j++) printf("%02x", a[j]);
  1.6216 +       printf("\n");
  1.6217 +       if( pPg->pPage==0 ){
  1.6218 +         sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
  1.6219 +       }
  1.6220 +    }
  1.6221 +  }
  1.6222 +  #else
  1.6223 +# define pcacheTrace(X)
  1.6224 +# define pcacheDump(X)
  1.6225 +#endif
  1.6226 +
  1.6227 +/*
  1.6228 +** Check invariants on a PgHdr entry.  Return true if everything is OK.
  1.6229 +** Return false if any invariant is violated.
  1.6230 +**
  1.6231 +** This routine is for use inside of assert() statements only.  For
  1.6232 +** example:
  1.6233 +**
  1.6234 +**          assert( sqlite3PcachePageSanity(pPg) );
  1.6235 +*/
  1.6236 +#if SQLITE_DEBUG
  1.6237 +SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
  1.6238 +  PCache *pCache;
  1.6239 +  assert( pPg!=0 );
  1.6240 +  assert( pPg->pgno>0 );    /* Page number is 1 or more */
  1.6241 +  pCache = pPg->pCache;
  1.6242 +  assert( pCache!=0 );      /* Every page has an associated PCache */
  1.6243 +  if( pPg->flags & PGHDR_CLEAN ){
  1.6244 +    assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
  1.6245 +    assert( pCache->pDirty!=pPg );          /* CLEAN pages not on dirty list */
  1.6246 +    assert( pCache->pDirtyTail!=pPg );
  1.6247 +  }
  1.6248 +  /* WRITEABLE pages must also be DIRTY */
  1.6249 +  if( pPg->flags & PGHDR_WRITEABLE ){
  1.6250 +    assert( pPg->flags & PGHDR_DIRTY );     /* WRITEABLE implies DIRTY */
  1.6251 +  }
  1.6252 +  /* NEED_SYNC can be set independently of WRITEABLE.  This can happen,
  1.6253 +  ** for example, when using the sqlite3PagerDontWrite() optimization:
  1.6254 +  **    (1)  Page X is journalled, and gets WRITEABLE and NEED_SEEK.
  1.6255 +  **    (2)  Page X moved to freelist, WRITEABLE is cleared
  1.6256 +  **    (3)  Page X reused, WRITEABLE is set again
  1.6257 +  ** If NEED_SYNC had been cleared in step 2, then it would not be reset
  1.6258 +  ** in step 3, and page might be written into the database without first
  1.6259 +  ** syncing the rollback journal, which might cause corruption on a power
  1.6260 +  ** loss.
  1.6261 +  **
  1.6262 +  ** Another example is when the database page size is smaller than the
  1.6263 +  ** disk sector size.  When any page of a sector is journalled, all pages
  1.6264 +  ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
  1.6265 +  ** in case they are later modified, since all pages in the same sector
  1.6266 +  ** must be journalled and synced before any of those pages can be safely
  1.6267 +  ** written.
  1.6268 +  */
  1.6269 +  return 1;
  1.6270 +}
  1.6271 +#endif /* SQLITE_DEBUG */
  1.6272 +
  1.6273 +
  1.6274  /********************************** Linked List Management ********************/
  1.6275  
  1.6276  /* Allowed values for second argument to pcacheManageDirtyList() */
  1.6277 @@ -41192,17 +43265,16 @@
  1.6278  static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
  1.6279    PCache *p = pPage->pCache;
  1.6280  
  1.6281 +  pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
  1.6282 +                addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
  1.6283 +                pPage->pgno));
  1.6284    if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
  1.6285      assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
  1.6286      assert( pPage->pDirtyPrev || pPage==p->pDirty );
  1.6287    
  1.6288      /* Update the PCache1.pSynced variable if necessary. */
  1.6289      if( p->pSynced==pPage ){
  1.6290 -      PgHdr *pSynced = pPage->pDirtyPrev;
  1.6291 -      while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
  1.6292 -        pSynced = pSynced->pDirtyPrev;
  1.6293 -      }
  1.6294 -      p->pSynced = pSynced;
  1.6295 +      p->pSynced = pPage->pDirtyPrev;
  1.6296      }
  1.6297    
  1.6298      if( pPage->pDirtyNext ){
  1.6299 @@ -41214,10 +43286,15 @@
  1.6300      if( pPage->pDirtyPrev ){
  1.6301        pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
  1.6302      }else{
  1.6303 +      /* If there are now no dirty pages in the cache, set eCreate to 2. 
  1.6304 +      ** This is an optimization that allows sqlite3PcacheFetch() to skip
  1.6305 +      ** searching for a dirty page to eject from the cache when it might
  1.6306 +      ** otherwise have to.  */
  1.6307        assert( pPage==p->pDirty );
  1.6308        p->pDirty = pPage->pDirtyNext;
  1.6309 -      if( p->pDirty==0 && p->bPurgeable ){
  1.6310 -        assert( p->eCreate==1 );
  1.6311 +      assert( p->bPurgeable || p->eCreate==2 );
  1.6312 +      if( p->pDirty==0 ){         /*OPTIMIZATION-IF-TRUE*/
  1.6313 +        assert( p->bPurgeable==0 || p->eCreate==1 );
  1.6314          p->eCreate = 2;
  1.6315        }
  1.6316      }
  1.6317 @@ -41239,10 +43316,19 @@
  1.6318        }
  1.6319      }
  1.6320      p->pDirty = pPage;
  1.6321 -    if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
  1.6322 +
  1.6323 +    /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
  1.6324 +    ** pSynced to point to it. Checking the NEED_SYNC flag is an 
  1.6325 +    ** optimization, as if pSynced points to a page with the NEED_SYNC
  1.6326 +    ** flag set sqlite3PcacheFetchStress() searches through all newer 
  1.6327 +    ** entries of the dirty-list for a page with NEED_SYNC clear anyway.  */
  1.6328 +    if( !p->pSynced 
  1.6329 +     && 0==(pPage->flags&PGHDR_NEED_SYNC)   /*OPTIMIZATION-IF-FALSE*/
  1.6330 +    ){
  1.6331        p->pSynced = pPage;
  1.6332      }
  1.6333    }
  1.6334 +  pcacheDump(p);
  1.6335  }
  1.6336  
  1.6337  /*
  1.6338 @@ -41251,7 +43337,9 @@
  1.6339  */
  1.6340  static void pcacheUnpin(PgHdr *p){
  1.6341    if( p->pCache->bPurgeable ){
  1.6342 +    pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
  1.6343      sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
  1.6344 +    pcacheDump(p->pCache);
  1.6345    }
  1.6346  }
  1.6347  
  1.6348 @@ -41321,6 +43409,7 @@
  1.6349    p->pStress = pStress;
  1.6350    p->szCache = 100;
  1.6351    p->szSpill = 1;
  1.6352 +  pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
  1.6353    return sqlite3PcacheSetPageSize(p, szPage);
  1.6354  }
  1.6355  
  1.6356 @@ -41336,13 +43425,14 @@
  1.6357                  szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
  1.6358                  pCache->bPurgeable
  1.6359      );
  1.6360 -    if( pNew==0 ) return SQLITE_NOMEM;
  1.6361 +    if( pNew==0 ) return SQLITE_NOMEM_BKPT;
  1.6362      sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
  1.6363      if( pCache->pCache ){
  1.6364        sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
  1.6365      }
  1.6366      pCache->pCache = pNew;
  1.6367      pCache->szPage = szPage;
  1.6368 +    pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
  1.6369    }
  1.6370    return SQLITE_OK;
  1.6371  }
  1.6372 @@ -41377,11 +43467,13 @@
  1.6373    int createFlag        /* If true, create page if it does not exist already */
  1.6374  ){
  1.6375    int eCreate;
  1.6376 +  sqlite3_pcache_page *pRes;
  1.6377  
  1.6378    assert( pCache!=0 );
  1.6379    assert( pCache->pCache!=0 );
  1.6380    assert( createFlag==3 || createFlag==0 );
  1.6381    assert( pgno>0 );
  1.6382 +  assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
  1.6383  
  1.6384    /* eCreate defines what to do if the page does not exist.
  1.6385    **    0     Do not allocate a new page.  (createFlag==0)
  1.6386 @@ -41394,12 +43486,15 @@
  1.6387    assert( eCreate==0 || eCreate==1 || eCreate==2 );
  1.6388    assert( createFlag==0 || pCache->eCreate==eCreate );
  1.6389    assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
  1.6390 -  return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
  1.6391 +  pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
  1.6392 +  pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
  1.6393 +               createFlag?" create":"",pRes));
  1.6394 +  return pRes;
  1.6395  }
  1.6396  
  1.6397  /*
  1.6398  ** If the sqlite3PcacheFetch() routine is unable to allocate a new
  1.6399 -** page because new clean pages are available for reuse and the cache
  1.6400 +** page because no clean pages are available for reuse and the cache
  1.6401  ** size limit has been reached, then this routine can be invoked to 
  1.6402  ** try harder to allocate a page.  This routine might invoke the stress
  1.6403  ** callback to spill dirty pages to the journal.  It will then try to
  1.6404 @@ -41421,7 +43516,11 @@
  1.6405      ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
  1.6406      ** cleared), but if that is not possible settle for any other 
  1.6407      ** unreferenced dirty page.
  1.6408 -    */
  1.6409 +    **
  1.6410 +    ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
  1.6411 +    ** flag is currently referenced, then the following may leave pSynced
  1.6412 +    ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
  1.6413 +    ** cleared). This is Ok, as pSynced is just an optimization.  */
  1.6414      for(pPg=pCache->pSynced; 
  1.6415          pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
  1.6416          pPg=pPg->pDirtyPrev
  1.6417 @@ -41439,14 +43538,16 @@
  1.6418                    sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
  1.6419                  numberOfCachePages(pCache));
  1.6420  #endif
  1.6421 +      pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
  1.6422        rc = pCache->xStress(pCache->pStress, pPg);
  1.6423 +      pcacheDump(pCache);
  1.6424        if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
  1.6425          return rc;
  1.6426        }
  1.6427      }
  1.6428    }
  1.6429    *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
  1.6430 -  return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK;
  1.6431 +  return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
  1.6432  }
  1.6433  
  1.6434  /*
  1.6435 @@ -41499,6 +43600,7 @@
  1.6436    }
  1.6437    pCache->nRefSum++;
  1.6438    pPgHdr->nRef++;
  1.6439 +  assert( sqlite3PcachePageSanity(pPgHdr) );
  1.6440    return pPgHdr;
  1.6441  }
  1.6442  
  1.6443 @@ -41512,8 +43614,11 @@
  1.6444    if( (--p->nRef)==0 ){
  1.6445      if( p->flags&PGHDR_CLEAN ){
  1.6446        pcacheUnpin(p);
  1.6447 -    }else if( p->pDirtyPrev!=0 ){
  1.6448 -      /* Move the page to the head of the dirty list. */
  1.6449 +    }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/
  1.6450 +      /* Move the page to the head of the dirty list. If p->pDirtyPrev==0,
  1.6451 +      ** then page p is already at the head of the dirty list and the
  1.6452 +      ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE
  1.6453 +      ** tag above.  */
  1.6454        pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
  1.6455      }
  1.6456    }
  1.6457 @@ -41524,6 +43629,7 @@
  1.6458  */
  1.6459  SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
  1.6460    assert(p->nRef>0);
  1.6461 +  assert( sqlite3PcachePageSanity(p) );
  1.6462    p->nRef++;
  1.6463    p->pCache->nRefSum++;
  1.6464  }
  1.6465 @@ -41535,6 +43641,7 @@
  1.6466  */
  1.6467  SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
  1.6468    assert( p->nRef==1 );
  1.6469 +  assert( sqlite3PcachePageSanity(p) );
  1.6470    if( p->flags&PGHDR_DIRTY ){
  1.6471      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
  1.6472    }
  1.6473 @@ -41548,13 +43655,16 @@
  1.6474  */
  1.6475  SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
  1.6476    assert( p->nRef>0 );
  1.6477 -  if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){
  1.6478 +  assert( sqlite3PcachePageSanity(p) );
  1.6479 +  if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){    /*OPTIMIZATION-IF-FALSE*/
  1.6480      p->flags &= ~PGHDR_DONT_WRITE;
  1.6481      if( p->flags & PGHDR_CLEAN ){
  1.6482        p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
  1.6483 +      pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
  1.6484        assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
  1.6485        pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
  1.6486      }
  1.6487 +    assert( sqlite3PcachePageSanity(p) );
  1.6488    }
  1.6489  }
  1.6490  
  1.6491 @@ -41563,11 +43673,14 @@
  1.6492  ** make it so.
  1.6493  */
  1.6494  SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
  1.6495 -  if( (p->flags & PGHDR_DIRTY) ){
  1.6496 +  assert( sqlite3PcachePageSanity(p) );
  1.6497 +  if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){
  1.6498      assert( (p->flags & PGHDR_CLEAN)==0 );
  1.6499      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
  1.6500      p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
  1.6501      p->flags |= PGHDR_CLEAN;
  1.6502 +    pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
  1.6503 +    assert( sqlite3PcachePageSanity(p) );
  1.6504      if( p->nRef==0 ){
  1.6505        pcacheUnpin(p);
  1.6506      }
  1.6507 @@ -41579,12 +43692,25 @@
  1.6508  */
  1.6509  SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
  1.6510    PgHdr *p;
  1.6511 +  pcacheTrace(("%p.CLEAN-ALL\n",pCache));
  1.6512    while( (p = pCache->pDirty)!=0 ){
  1.6513      sqlite3PcacheMakeClean(p);
  1.6514    }
  1.6515  }
  1.6516  
  1.6517  /*
  1.6518 +** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
  1.6519 +*/
  1.6520 +SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){
  1.6521 +  PgHdr *p;
  1.6522 +  pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
  1.6523 +  for(p=pCache->pDirty; p; p=p->pDirtyNext){
  1.6524 +    p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
  1.6525 +  }
  1.6526 +  pCache->pSynced = pCache->pDirtyTail;
  1.6527 +}
  1.6528 +
  1.6529 +/*
  1.6530  ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
  1.6531  */
  1.6532  SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
  1.6533 @@ -41602,6 +43728,8 @@
  1.6534    PCache *pCache = p->pCache;
  1.6535    assert( p->nRef>0 );
  1.6536    assert( newPgno>0 );
  1.6537 +  assert( sqlite3PcachePageSanity(p) );
  1.6538 +  pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
  1.6539    sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
  1.6540    p->pgno = newPgno;
  1.6541    if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
  1.6542 @@ -41622,6 +43750,7 @@
  1.6543    if( pCache->pCache ){
  1.6544      PgHdr *p;
  1.6545      PgHdr *pNext;
  1.6546 +    pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
  1.6547      for(p=pCache->pDirty; p; p=pNext){
  1.6548        pNext = p->pDirtyNext;
  1.6549        /* This routine never gets call with a positive pgno except right
  1.6550 @@ -41629,7 +43758,7 @@
  1.6551        ** it must be that pgno==0.
  1.6552        */
  1.6553        assert( p->pgno>0 );
  1.6554 -      if( ALWAYS(p->pgno>pgno) ){
  1.6555 +      if( p->pgno>pgno ){
  1.6556          assert( p->flags&PGHDR_DIRTY );
  1.6557          sqlite3PcacheMakeClean(p);
  1.6558        }
  1.6559 @@ -41652,6 +43781,7 @@
  1.6560  */
  1.6561  SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
  1.6562    assert( pCache->pCache!=0 );
  1.6563 +  pcacheTrace(("%p.CLOSE\n",pCache));
  1.6564    sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
  1.6565  }
  1.6566  
  1.6567 @@ -41820,6 +43950,17 @@
  1.6568  */
  1.6569  SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
  1.6570  
  1.6571 +/*
  1.6572 +** Return the number of dirty pages currently in the cache, as a percentage
  1.6573 +** of the configured cache size.
  1.6574 +*/
  1.6575 +SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
  1.6576 +  PgHdr *pDirty;
  1.6577 +  int nDirty = 0;
  1.6578 +  int nCache = numberOfCachePages(pCache);
  1.6579 +  for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
  1.6580 +  return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
  1.6581 +}
  1.6582  
  1.6583  #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
  1.6584  /*
  1.6585 @@ -42187,7 +44328,6 @@
  1.6586  ** Free an allocated buffer obtained from pcache1Alloc().
  1.6587  */
  1.6588  static void pcache1Free(void *p){
  1.6589 -  int nFreed = 0;
  1.6590    if( p==0 ) return;
  1.6591    if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
  1.6592      PgFreeslot *pSlot;
  1.6593 @@ -42204,10 +44344,13 @@
  1.6594      assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
  1.6595      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
  1.6596  #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
  1.6597 -    nFreed = sqlite3MallocSize(p);
  1.6598 -    sqlite3_mutex_enter(pcache1.mutex);
  1.6599 -    sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
  1.6600 -    sqlite3_mutex_leave(pcache1.mutex);
  1.6601 +    {
  1.6602 +      int nFreed = 0;
  1.6603 +      nFreed = sqlite3MallocSize(p);
  1.6604 +      sqlite3_mutex_enter(pcache1.mutex);
  1.6605 +      sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
  1.6606 +      sqlite3_mutex_leave(pcache1.mutex);
  1.6607 +    }
  1.6608  #endif
  1.6609      sqlite3_free(p);
  1.6610    }
  1.6611 @@ -42527,8 +44670,8 @@
  1.6612  
  1.6613  #if SQLITE_THREADSAFE
  1.6614    if( sqlite3GlobalConfig.bCoreMutex ){
  1.6615 -    pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
  1.6616 -    pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
  1.6617 +    pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
  1.6618 +    pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
  1.6619    }
  1.6620  #endif
  1.6621    if( pcache1.separateCache
  1.6622 @@ -43134,8 +45277,9 @@
  1.6623  ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
  1.6624  ** primitives are constant time.  The cost of DESTROY is O(N).
  1.6625  **
  1.6626 -** There is an added cost of O(N) when switching between TEST and
  1.6627 -** SMALLEST primitives.
  1.6628 +** TEST and SMALLEST may not be used by the same RowSet.  This used to
  1.6629 +** be possible, but the feature was not used, so it was removed in order
  1.6630 +** to simplify the code.
  1.6631  */
  1.6632  /* #include "sqliteInt.h" */
  1.6633  
  1.6634 @@ -43256,7 +45400,9 @@
  1.6635  */
  1.6636  static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
  1.6637    assert( p!=0 );
  1.6638 -  if( p->nFresh==0 ){
  1.6639 +  if( p->nFresh==0 ){  /*OPTIMIZATION-IF-FALSE*/
  1.6640 +    /* We could allocate a fresh RowSetEntry each time one is needed, but it
  1.6641 +    ** is more efficient to pull a preallocated entry from the pool */
  1.6642      struct RowSetChunk *pNew;
  1.6643      pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
  1.6644      if( pNew==0 ){
  1.6645 @@ -43290,7 +45436,9 @@
  1.6646    pEntry->pRight = 0;
  1.6647    pLast = p->pLast;
  1.6648    if( pLast ){
  1.6649 -    if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
  1.6650 +    if( rowid<=pLast->v ){  /*OPTIMIZATION-IF-FALSE*/
  1.6651 +      /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
  1.6652 +      ** where possible */
  1.6653        p->rsFlags &= ~ROWSET_SORTED;
  1.6654      }
  1.6655      pLast->pRight = pEntry;
  1.6656 @@ -43412,23 +45560,29 @@
  1.6657  ){
  1.6658    struct RowSetEntry *p;         /* Root of the new tree */
  1.6659    struct RowSetEntry *pLeft;     /* Left subtree */
  1.6660 -  if( *ppList==0 ){
  1.6661 -    return 0;
  1.6662 -  }
  1.6663 -  if( iDepth==1 ){
  1.6664 +  if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
  1.6665 +    /* Prevent unnecessary deep recursion when we run out of entries */
  1.6666 +    return 0; 
  1.6667 +  }
  1.6668 +  if( iDepth>1 ){   /*OPTIMIZATION-IF-TRUE*/
  1.6669 +    /* This branch causes a *balanced* tree to be generated.  A valid tree
  1.6670 +    ** is still generated without this branch, but the tree is wildly
  1.6671 +    ** unbalanced and inefficient. */
  1.6672 +    pLeft = rowSetNDeepTree(ppList, iDepth-1);
  1.6673 +    p = *ppList;
  1.6674 +    if( p==0 ){     /*OPTIMIZATION-IF-FALSE*/
  1.6675 +      /* It is safe to always return here, but the resulting tree
  1.6676 +      ** would be unbalanced */
  1.6677 +      return pLeft;
  1.6678 +    }
  1.6679 +    p->pLeft = pLeft;
  1.6680 +    *ppList = p->pRight;
  1.6681 +    p->pRight = rowSetNDeepTree(ppList, iDepth-1);
  1.6682 +  }else{
  1.6683      p = *ppList;
  1.6684      *ppList = p->pRight;
  1.6685      p->pLeft = p->pRight = 0;
  1.6686 -    return p;
  1.6687 -  }
  1.6688 -  pLeft = rowSetNDeepTree(ppList, iDepth-1);
  1.6689 -  p = *ppList;
  1.6690 -  if( p==0 ){
  1.6691 -    return pLeft;
  1.6692 -  }
  1.6693 -  p->pLeft = pLeft;
  1.6694 -  *ppList = p->pRight;
  1.6695 -  p->pRight = rowSetNDeepTree(ppList, iDepth-1);
  1.6696 +  }
  1.6697    return p;
  1.6698  }
  1.6699  
  1.6700 @@ -43456,58 +45610,36 @@
  1.6701  }
  1.6702  
  1.6703  /*
  1.6704 -** Take all the entries on p->pEntry and on the trees in p->pForest and
  1.6705 -** sort them all together into one big ordered list on p->pEntry.
  1.6706 -**
  1.6707 -** This routine should only be called once in the life of a RowSet.
  1.6708 -*/
  1.6709 -static void rowSetToList(RowSet *p){
  1.6710 -
  1.6711 -  /* This routine is called only once */
  1.6712 -  assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
  1.6713 -
  1.6714 -  if( (p->rsFlags & ROWSET_SORTED)==0 ){
  1.6715 -    p->pEntry = rowSetEntrySort(p->pEntry);
  1.6716 -  }
  1.6717 -
  1.6718 -  /* While this module could theoretically support it, sqlite3RowSetNext()
  1.6719 -  ** is never called after sqlite3RowSetText() for the same RowSet.  So
  1.6720 -  ** there is never a forest to deal with.  Should this change, simply
  1.6721 -  ** remove the assert() and the #if 0. */
  1.6722 -  assert( p->pForest==0 );
  1.6723 -#if 0
  1.6724 -  while( p->pForest ){
  1.6725 -    struct RowSetEntry *pTree = p->pForest->pLeft;
  1.6726 -    if( pTree ){
  1.6727 -      struct RowSetEntry *pHead, *pTail;
  1.6728 -      rowSetTreeToList(pTree, &pHead, &pTail);
  1.6729 -      p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
  1.6730 -    }
  1.6731 -    p->pForest = p->pForest->pRight;
  1.6732 -  }
  1.6733 -#endif
  1.6734 -  p->rsFlags |= ROWSET_NEXT;  /* Verify this routine is never called again */
  1.6735 -}
  1.6736 -
  1.6737 -/*
  1.6738  ** Extract the smallest element from the RowSet.
  1.6739  ** Write the element into *pRowid.  Return 1 on success.  Return
  1.6740  ** 0 if the RowSet is already empty.
  1.6741  **
  1.6742  ** After this routine has been called, the sqlite3RowSetInsert()
  1.6743 -** routine may not be called again.  
  1.6744 +** routine may not be called again.
  1.6745 +**
  1.6746 +** This routine may not be called after sqlite3RowSetTest() has
  1.6747 +** been used.  Older versions of RowSet allowed that, but as the
  1.6748 +** capability was not used by the code generator, it was removed
  1.6749 +** for code economy.
  1.6750  */
  1.6751  SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
  1.6752    assert( p!=0 );
  1.6753 +  assert( p->pForest==0 );  /* Cannot be used with sqlite3RowSetText() */
  1.6754  
  1.6755    /* Merge the forest into a single sorted list on first call */
  1.6756 -  if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
  1.6757 +  if( (p->rsFlags & ROWSET_NEXT)==0 ){  /*OPTIMIZATION-IF-FALSE*/
  1.6758 +    if( (p->rsFlags & ROWSET_SORTED)==0 ){  /*OPTIMIZATION-IF-FALSE*/
  1.6759 +      p->pEntry = rowSetEntrySort(p->pEntry);
  1.6760 +    }
  1.6761 +    p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
  1.6762 +  }
  1.6763  
  1.6764    /* Return the next entry on the list */
  1.6765    if( p->pEntry ){
  1.6766      *pRowid = p->pEntry->v;
  1.6767      p->pEntry = p->pEntry->pRight;
  1.6768 -    if( p->pEntry==0 ){
  1.6769 +    if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
  1.6770 +      /* Free memory immediately, rather than waiting on sqlite3_finalize() */
  1.6771        sqlite3RowSetClear(p);
  1.6772      }
  1.6773      return 1;
  1.6774 @@ -43530,13 +45662,15 @@
  1.6775    /* This routine is never called after sqlite3RowSetNext() */
  1.6776    assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
  1.6777  
  1.6778 -  /* Sort entries into the forest on the first test of a new batch 
  1.6779 -  */
  1.6780 -  if( iBatch!=pRowSet->iBatch ){
  1.6781 +  /* Sort entries into the forest on the first test of a new batch.
  1.6782 +  ** To save unnecessary work, only do this when the batch number changes.
  1.6783 +  */
  1.6784 +  if( iBatch!=pRowSet->iBatch ){  /*OPTIMIZATION-IF-FALSE*/
  1.6785      p = pRowSet->pEntry;
  1.6786      if( p ){
  1.6787        struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
  1.6788 -      if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
  1.6789 +      if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
  1.6790 +        /* Only sort the current set of entiries if they need it */
  1.6791          p = rowSetEntrySort(p);
  1.6792        }
  1.6793        for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
  1.6794 @@ -44166,19 +46300,6 @@
  1.6795  */
  1.6796  #define MAX_SECTOR_SIZE 0x10000
  1.6797  
  1.6798 -/*
  1.6799 -** If the option SQLITE_EXTRA_DURABLE option is set at compile-time, then
  1.6800 -** SQLite will do extra fsync() operations when synchronous==FULL to help
  1.6801 -** ensure that transactions are durable across a power failure.  Most
  1.6802 -** applications are happy as long as transactions are consistent across
  1.6803 -** a power failure, and are perfectly willing to lose the last transaction
  1.6804 -** in exchange for the extra performance of avoiding directory syncs.
  1.6805 -** And so the default SQLITE_EXTRA_DURABLE setting is off.
  1.6806 -*/
  1.6807 -#ifndef SQLITE_EXTRA_DURABLE
  1.6808 -# define SQLITE_EXTRA_DURABLE 0
  1.6809 -#endif
  1.6810 -
  1.6811  
  1.6812  /*
  1.6813  ** An instance of the following structure is allocated for each active
  1.6814 @@ -44623,6 +46744,7 @@
  1.6815    ** state.
  1.6816    */
  1.6817    if( MEMDB ){
  1.6818 +    assert( !isOpen(p->fd) );
  1.6819      assert( p->noSync );
  1.6820      assert( p->journalMode==PAGER_JOURNALMODE_OFF 
  1.6821           || p->journalMode==PAGER_JOURNALMODE_MEMORY 
  1.6822 @@ -44709,7 +46831,7 @@
  1.6823        ** back to OPEN state.
  1.6824        */
  1.6825        assert( pPager->errCode!=SQLITE_OK );
  1.6826 -      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
  1.6827 +      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
  1.6828        break;
  1.6829    }
  1.6830  
  1.6831 @@ -44921,6 +47043,8 @@
  1.6832  
  1.6833    return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
  1.6834  }
  1.6835 +#else
  1.6836 +# define jrnlBufferSize(x) 0
  1.6837  #endif
  1.6838  
  1.6839  /*
  1.6840 @@ -45081,6 +47205,7 @@
  1.6841  static int zeroJournalHdr(Pager *pPager, int doTruncate){
  1.6842    int rc = SQLITE_OK;                               /* Return code */
  1.6843    assert( isOpen(pPager->jfd) );
  1.6844 +  assert( !sqlite3JournalIsInMemory(pPager->jfd) );
  1.6845    if( pPager->journalOff ){
  1.6846      const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
  1.6847  
  1.6848 @@ -45462,7 +47587,7 @@
  1.6849    for(ii=0; ii<pPager->nSavepoint; ii++){
  1.6850      sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
  1.6851    }
  1.6852 -  if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
  1.6853 +  if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
  1.6854      sqlite3OsClose(pPager->sjfd);
  1.6855    }
  1.6856    sqlite3_free(pPager->aSavepoint);
  1.6857 @@ -45568,13 +47693,17 @@
  1.6858    ** it can safely move back to PAGER_OPEN state. This happens in both
  1.6859    ** normal and exclusive-locking mode.
  1.6860    */
  1.6861 +  assert( pPager->errCode==SQLITE_OK || !MEMDB );
  1.6862    if( pPager->errCode ){
  1.6863 -    assert( !MEMDB );
  1.6864 -    pager_reset(pPager);
  1.6865 -    pPager->changeCountDone = pPager->tempFile;
  1.6866 -    pPager->eState = PAGER_OPEN;
  1.6867 +    if( pPager->tempFile==0 ){
  1.6868 +      pager_reset(pPager);
  1.6869 +      pPager->changeCountDone = 0;
  1.6870 +      pPager->eState = PAGER_OPEN;
  1.6871 +    }else{
  1.6872 +      pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
  1.6873 +    }
  1.6874 +    if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
  1.6875      pPager->errCode = SQLITE_OK;
  1.6876 -    if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
  1.6877    }
  1.6878  
  1.6879    pPager->journalOff = 0;
  1.6880 @@ -45619,6 +47748,29 @@
  1.6881  static int pager_truncate(Pager *pPager, Pgno nPage);
  1.6882  
  1.6883  /*
  1.6884 +** The write transaction open on pPager is being committed (bCommit==1)
  1.6885 +** or rolled back (bCommit==0).
  1.6886 +**
  1.6887 +** Return TRUE if and only if all dirty pages should be flushed to disk.
  1.6888 +**
  1.6889 +** Rules:
  1.6890 +**
  1.6891 +**   *  For non-TEMP databases, always sync to disk.  This is necessary
  1.6892 +**      for transactions to be durable.
  1.6893 +**
  1.6894 +**   *  Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
  1.6895 +**      file has been created already (via a spill on pagerStress()) and
  1.6896 +**      when the number of dirty pages in memory exceeds 25% of the total
  1.6897 +**      cache size.
  1.6898 +*/
  1.6899 +static int pagerFlushOnCommit(Pager *pPager, int bCommit){
  1.6900 +  if( pPager->tempFile==0 ) return 1;
  1.6901 +  if( !bCommit ) return 0;
  1.6902 +  if( !isOpen(pPager->fd) ) return 0;
  1.6903 +  return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
  1.6904 +}
  1.6905 +
  1.6906 +/*
  1.6907  ** This routine ends a transaction. A transaction is usually ended by 
  1.6908  ** either a COMMIT or a ROLLBACK operation. This routine may be called 
  1.6909  ** after rollback of a hot-journal, or if an error occurs while opening
  1.6910 @@ -45700,8 +47852,8 @@
  1.6911      assert( !pagerUseWal(pPager) );
  1.6912  
  1.6913      /* Finalize the journal file. */
  1.6914 -    if( sqlite3IsMemJournal(pPager->jfd) ){
  1.6915 -      assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
  1.6916 +    if( sqlite3JournalIsInMemory(pPager->jfd) ){
  1.6917 +      /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
  1.6918        sqlite3OsClose(pPager->jfd);
  1.6919      }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
  1.6920        if( pPager->journalOff==0 ){
  1.6921 @@ -45721,15 +47873,16 @@
  1.6922      }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
  1.6923        || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
  1.6924      ){
  1.6925 -      rc = zeroJournalHdr(pPager, hasMaster);
  1.6926 +      rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile);
  1.6927        pPager->journalOff = 0;
  1.6928      }else{
  1.6929        /* This branch may be executed with Pager.journalMode==MEMORY if
  1.6930        ** a hot-journal was just rolled back. In this case the journal
  1.6931        ** file should be closed and deleted. If this connection writes to
  1.6932 -      ** the database file, it will do so using an in-memory journal. 
  1.6933 +      ** the database file, it will do so using an in-memory journal.
  1.6934        */
  1.6935 -      int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
  1.6936 +      int bDelete = !pPager->tempFile;
  1.6937 +      assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
  1.6938        assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 
  1.6939             || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
  1.6940             || pPager->journalMode==PAGER_JOURNALMODE_WAL 
  1.6941 @@ -45755,8 +47908,14 @@
  1.6942    sqlite3BitvecDestroy(pPager->pInJournal);
  1.6943    pPager->pInJournal = 0;
  1.6944    pPager->nRec = 0;
  1.6945 -  sqlite3PcacheCleanAll(pPager->pPCache);
  1.6946 -  sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
  1.6947 +  if( rc==SQLITE_OK ){
  1.6948 +    if( pagerFlushOnCommit(pPager, bCommit) ){
  1.6949 +      sqlite3PcacheCleanAll(pPager->pPCache);
  1.6950 +    }else{
  1.6951 +      sqlite3PcacheClearWritable(pPager->pPCache);
  1.6952 +    }
  1.6953 +    sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
  1.6954 +  }
  1.6955  
  1.6956    if( pagerUseWal(pPager) ){
  1.6957      /* Drop the WAL write-lock, if any. Also, if the connection was in 
  1.6958 @@ -46040,7 +48199,7 @@
  1.6959      pPg = sqlite3PagerLookup(pPager, pgno);
  1.6960    }
  1.6961    assert( pPg || !MEMDB );
  1.6962 -  assert( pPager->eState!=PAGER_OPEN || pPg==0 );
  1.6963 +  assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
  1.6964    PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
  1.6965             PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
  1.6966             (isMainJrnl?"main-journal":"sub-journal")
  1.6967 @@ -46062,9 +48221,9 @@
  1.6968        pPager->dbFileSize = pgno;
  1.6969      }
  1.6970      if( pPager->pBackup ){
  1.6971 -      CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
  1.6972 +      CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT);
  1.6973        sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
  1.6974 -      CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
  1.6975 +      CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT, aData);
  1.6976      }
  1.6977    }else if( !isMainJrnl && pPg==0 ){
  1.6978      /* If this is a rollback of a savepoint and data was not written to
  1.6979 @@ -46090,7 +48249,6 @@
  1.6980      assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
  1.6981      pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
  1.6982      if( rc!=SQLITE_OK ) return rc;
  1.6983 -    pPg->flags &= ~PGHDR_NEED_READ;
  1.6984      sqlite3PcacheMakeDirty(pPg);
  1.6985    }
  1.6986    if( pPg ){
  1.6987 @@ -46104,29 +48262,10 @@
  1.6988      pData = pPg->pData;
  1.6989      memcpy(pData, (u8*)aData, pPager->pageSize);
  1.6990      pPager->xReiniter(pPg);
  1.6991 -    if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
  1.6992 -      /* If the contents of this page were just restored from the main 
  1.6993 -      ** journal file, then its content must be as they were when the 
  1.6994 -      ** transaction was first opened. In this case we can mark the page
  1.6995 -      ** as clean, since there will be no need to write it out to the
  1.6996 -      ** database.
  1.6997 -      **
  1.6998 -      ** There is one exception to this rule. If the page is being rolled
  1.6999 -      ** back as part of a savepoint (or statement) rollback from an 
  1.7000 -      ** unsynced portion of the main journal file, then it is not safe
  1.7001 -      ** to mark the page as clean. This is because marking the page as
  1.7002 -      ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
  1.7003 -      ** already in the journal file (recorded in Pager.pInJournal) and
  1.7004 -      ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
  1.7005 -      ** again within this transaction, it will be marked as dirty but
  1.7006 -      ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
  1.7007 -      ** be written out into the database file before its journal file
  1.7008 -      ** segment is synced. If a crash occurs during or following this,
  1.7009 -      ** database corruption may ensue.
  1.7010 -      */
  1.7011 -      assert( !pagerUseWal(pPager) );
  1.7012 -      sqlite3PcacheMakeClean(pPg);
  1.7013 -    }
  1.7014 +    /* It used to be that sqlite3PcacheMakeClean(pPg) was called here.  But
  1.7015 +    ** that call was dangerous and had no detectable benefit since the cache
  1.7016 +    ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
  1.7017 +    ** has been removed. */
  1.7018      pager_set_pagehash(pPg);
  1.7019  
  1.7020      /* If this was page 1, then restore the value of Pager.dbFileVers.
  1.7021 @@ -46136,7 +48275,7 @@
  1.7022      }
  1.7023  
  1.7024      /* Decode the page just read from disk */
  1.7025 -    CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
  1.7026 +    CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT);
  1.7027      sqlite3PcacheRelease(pPg);
  1.7028    }
  1.7029    return rc;
  1.7030 @@ -46202,7 +48341,7 @@
  1.7031    pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
  1.7032    pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
  1.7033    if( !pMaster ){
  1.7034 -    rc = SQLITE_NOMEM;
  1.7035 +    rc = SQLITE_NOMEM_BKPT;
  1.7036    }else{
  1.7037      const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
  1.7038      rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
  1.7039 @@ -46219,7 +48358,7 @@
  1.7040    nMasterPtr = pVfs->mxPathname+1;
  1.7041    zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1);
  1.7042    if( !zMasterJournal ){
  1.7043 -    rc = SQLITE_NOMEM;
  1.7044 +    rc = SQLITE_NOMEM_BKPT;
  1.7045      goto delmaster_out;
  1.7046    }
  1.7047    zMasterPtr = &zMasterJournal[nMasterJournal+1];
  1.7048 @@ -46467,7 +48606,7 @@
  1.7049    ** TODO: Technically the following is an error because it assumes that
  1.7050    ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
  1.7051    ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
  1.7052 -  **  mxPathname is 512, which is the same as the minimum allowable value
  1.7053 +  ** mxPathname is 512, which is the same as the minimum allowable value
  1.7054    ** for pageSize.
  1.7055    */
  1.7056    zMaster = pPager->pTmpSpace;
  1.7057 @@ -46689,7 +48828,7 @@
  1.7058        memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
  1.7059      }
  1.7060    }
  1.7061 -  CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
  1.7062 +  CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM_BKPT);
  1.7063  
  1.7064    PAGER_INCR(sqlite3_pager_readdb_count);
  1.7065    PAGER_INCR(pPager->nRead);
  1.7066 @@ -46917,6 +49056,8 @@
  1.7067    */
  1.7068    assert( pPager->eState==PAGER_OPEN );
  1.7069    assert( pPager->eLock>=SHARED_LOCK );
  1.7070 +  assert( isOpen(pPager->fd) );
  1.7071 +  assert( pPager->tempFile==0 );
  1.7072    nPage = sqlite3WalDbsize(pPager->pWal);
  1.7073  
  1.7074    /* If the number of pages in the database is not available from the
  1.7075 @@ -46924,14 +49065,11 @@
  1.7076    ** the database file.  If the size of the database file is not an
  1.7077    ** integer multiple of the page-size, round up the result.
  1.7078    */
  1.7079 -  if( nPage==0 ){
  1.7080 +  if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
  1.7081      i64 n = 0;                    /* Size of db file in bytes */
  1.7082 -    assert( isOpen(pPager->fd) || pPager->tempFile );
  1.7083 -    if( isOpen(pPager->fd) ){
  1.7084 -      int rc = sqlite3OsFileSize(pPager->fd, &n);
  1.7085 -      if( rc!=SQLITE_OK ){
  1.7086 -        return rc;
  1.7087 -      }
  1.7088 +    int rc = sqlite3OsFileSize(pPager->fd, &n);
  1.7089 +    if( rc!=SQLITE_OK ){
  1.7090 +      return rc;
  1.7091      }
  1.7092      nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
  1.7093    }
  1.7094 @@ -47049,7 +49187,7 @@
  1.7095    if( pSavepoint ){
  1.7096      pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
  1.7097      if( !pDone ){
  1.7098 -      return SQLITE_NOMEM;
  1.7099 +      return SQLITE_NOMEM_BKPT;
  1.7100      }
  1.7101    }
  1.7102  
  1.7103 @@ -47196,7 +49334,7 @@
  1.7104  ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
  1.7105  ** of the database to damage due to OS crashes or power failures by
  1.7106  ** changing the number of syncs()s when writing the journals.
  1.7107 -** There are three levels:
  1.7108 +** There are four levels:
  1.7109  **
  1.7110  **    OFF       sqlite3OsSync() is never called.  This is the default
  1.7111  **              for temporary and transient files.
  1.7112 @@ -47216,6 +49354,10 @@
  1.7113  **              assurance that the journal will not be corrupted to the
  1.7114  **              point of causing damage to the database during rollback.
  1.7115  **
  1.7116 +**    EXTRA     This is like FULL except that is also syncs the directory
  1.7117 +**              that contains the rollback journal after the rollback
  1.7118 +**              journal is unlinked.
  1.7119 +**
  1.7120  ** The above is for a rollback-journal mode.  For WAL mode, OFF continues
  1.7121  ** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
  1.7122  ** prior to the start of checkpoint and that the database file is synced
  1.7123 @@ -47223,7 +49365,8 @@
  1.7124  ** was written back into the database.  But no sync operations occur for
  1.7125  ** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
  1.7126  ** file is synced following each commit operation, in addition to the
  1.7127 -** syncs associated with NORMAL.
  1.7128 +** syncs associated with NORMAL.  There is no difference between FULL
  1.7129 +** and EXTRA for WAL mode.
  1.7130  **
  1.7131  ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
  1.7132  ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
  1.7133 @@ -47412,7 +49555,7 @@
  1.7134      }
  1.7135      if( rc==SQLITE_OK ){
  1.7136        pNew = (char *)sqlite3PageMalloc(pageSize);
  1.7137 -      if( !pNew ) rc = SQLITE_NOMEM;
  1.7138 +      if( !pNew ) rc = SQLITE_NOMEM_BKPT;
  1.7139      }
  1.7140  
  1.7141      if( rc==SQLITE_OK ){
  1.7142 @@ -47688,7 +49831,7 @@
  1.7143      *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
  1.7144      if( p==0 ){
  1.7145        sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
  1.7146 -      return SQLITE_NOMEM;
  1.7147 +      return SQLITE_NOMEM_BKPT;
  1.7148      }
  1.7149      p->pExtra = (void *)&p[1];
  1.7150      p->flags = PGHDR_MMAP;
  1.7151 @@ -48002,8 +50145,9 @@
  1.7152  
  1.7153    /* This function is only called for rollback pagers in WRITER_DBMOD state. */
  1.7154    assert( !pagerUseWal(pPager) );
  1.7155 -  assert( pPager->eState==PAGER_WRITER_DBMOD );
  1.7156 +  assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
  1.7157    assert( pPager->eLock==EXCLUSIVE_LOCK );
  1.7158 +  assert( isOpen(pPager->fd) || pList->pDirty==0 );
  1.7159  
  1.7160    /* If the file is a temp-file has not yet been opened, open it now. It
  1.7161    ** is not possible for rc to be other than SQLITE_OK if this branch
  1.7162 @@ -48046,7 +50190,7 @@
  1.7163        if( pList->pgno==1 ) pager_write_changecounter(pList);
  1.7164  
  1.7165        /* Encode the database */
  1.7166 -      CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
  1.7167 +      CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData);
  1.7168  
  1.7169        /* Write out the page data. */
  1.7170        rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
  1.7171 @@ -48091,11 +50235,14 @@
  1.7172  static int openSubJournal(Pager *pPager){
  1.7173    int rc = SQLITE_OK;
  1.7174    if( !isOpen(pPager->sjfd) ){
  1.7175 +    const int flags =  SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE 
  1.7176 +      | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE 
  1.7177 +      | SQLITE_OPEN_DELETEONCLOSE;
  1.7178 +    int nStmtSpill = sqlite3Config.nStmtSpill;
  1.7179      if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
  1.7180 -      sqlite3MemJournalOpen(pPager->sjfd);
  1.7181 -    }else{
  1.7182 -      rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
  1.7183 -    }
  1.7184 +      nStmtSpill = -1;
  1.7185 +    }
  1.7186 +    rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
  1.7187    }
  1.7188    return rc;
  1.7189  }
  1.7190 @@ -48133,7 +50280,7 @@
  1.7191        i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
  1.7192        char *pData2;
  1.7193    
  1.7194 -      CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
  1.7195 +      CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
  1.7196        PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
  1.7197        rc = write32bits(pPager->sjfd, offset, pPg->pgno);
  1.7198        if( rc==SQLITE_OK ){
  1.7199 @@ -48316,18 +50463,8 @@
  1.7200    int nUri = 0;            /* Number of bytes of URI args at *zUri */
  1.7201  
  1.7202    /* Figure out how much space is required for each journal file-handle
  1.7203 -  ** (there are two of them, the main journal and the sub-journal). This
  1.7204 -  ** is the maximum space required for an in-memory journal file handle 
  1.7205 -  ** and a regular journal file-handle. Note that a "regular journal-handle"
  1.7206 -  ** may be a wrapper capable of caching the first portion of the journal
  1.7207 -  ** file in memory to implement the atomic-write optimization (see 
  1.7208 -  ** source file journal.c).
  1.7209 -  */
  1.7210 -  if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
  1.7211 -    journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
  1.7212 -  }else{
  1.7213 -    journalFileSize = ROUND8(sqlite3MemJournalSize());
  1.7214 -  }
  1.7215 +  ** (there are two of them, the main journal and the sub-journal).  */
  1.7216 +  journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
  1.7217  
  1.7218    /* Set the output variable to NULL in case an error occurs. */
  1.7219    *ppPager = 0;
  1.7220 @@ -48337,7 +50474,7 @@
  1.7221      memDb = 1;
  1.7222      if( zFilename && zFilename[0] ){
  1.7223        zPathname = sqlite3DbStrDup(0, zFilename);
  1.7224 -      if( zPathname==0  ) return SQLITE_NOMEM;
  1.7225 +      if( zPathname==0  ) return SQLITE_NOMEM_BKPT;
  1.7226        nPathname = sqlite3Strlen30(zPathname);
  1.7227        zFilename = 0;
  1.7228      }
  1.7229 @@ -48353,7 +50490,7 @@
  1.7230      nPathname = pVfs->mxPathname+1;
  1.7231      zPathname = sqlite3DbMallocRaw(0, nPathname*2);
  1.7232      if( zPathname==0 ){
  1.7233 -      return SQLITE_NOMEM;
  1.7234 +      return SQLITE_NOMEM_BKPT;
  1.7235      }
  1.7236      zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
  1.7237      rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
  1.7238 @@ -48406,7 +50543,7 @@
  1.7239    assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
  1.7240    if( !pPtr ){
  1.7241      sqlite3DbFree(0, zPathname);
  1.7242 -    return SQLITE_NOMEM;
  1.7243 +    return SQLITE_NOMEM_BKPT;
  1.7244    }
  1.7245    pPager =              (Pager*)(pPtr);
  1.7246    pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
  1.7247 @@ -48561,11 +50698,7 @@
  1.7248      assert( pPager->ckptSyncFlags==0 );
  1.7249    }else{
  1.7250      pPager->fullSync = 1;
  1.7251 -#if SQLITE_EXTRA_DURABLE
  1.7252 -    pPager->extraSync = 1;
  1.7253 -#else
  1.7254      pPager->extraSync = 0;
  1.7255 -#endif
  1.7256      pPager->syncFlags = SQLITE_SYNC_NORMAL;
  1.7257      pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
  1.7258      pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
  1.7259 @@ -48682,6 +50815,7 @@
  1.7260      if( rc==SQLITE_OK && !locked ){
  1.7261        Pgno nPage;                 /* Number of pages in database file */
  1.7262  
  1.7263 +      assert( pPager->tempFile==0 );
  1.7264        rc = pagerPagecount(pPager, &nPage);
  1.7265        if( rc==SQLITE_OK ){
  1.7266          /* If the database is zero pages in size, that means that either (1) the
  1.7267 @@ -48774,17 +50908,17 @@
  1.7268    /* This routine is only called from b-tree and only when there are no
  1.7269    ** outstanding pages. This implies that the pager state should either
  1.7270    ** be OPEN or READER. READER is only possible if the pager is or was in 
  1.7271 -  ** exclusive access mode.
  1.7272 -  */
  1.7273 +  ** exclusive access mode.  */
  1.7274    assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
  1.7275    assert( assert_pager_state(pPager) );
  1.7276    assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
  1.7277 -  if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
  1.7278 +  assert( pPager->errCode==SQLITE_OK );
  1.7279  
  1.7280    if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
  1.7281      int bHotJournal = 1;          /* True if there exists a hot journal-file */
  1.7282  
  1.7283      assert( !MEMDB );
  1.7284 +    assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
  1.7285  
  1.7286      rc = pager_wait_on_lock(pPager, SHARED_LOCK);
  1.7287      if( rc!=SQLITE_OK ){
  1.7288 @@ -48870,7 +51004,7 @@
  1.7289          assert( rc==SQLITE_OK );
  1.7290          rc = pagerSyncHotJournal(pPager);
  1.7291          if( rc==SQLITE_OK ){
  1.7292 -          rc = pager_playback(pPager, 1);
  1.7293 +          rc = pager_playback(pPager, !pPager->tempFile);
  1.7294            pPager->eState = PAGER_OPEN;
  1.7295          }
  1.7296        }else if( !pPager->exclusiveMode ){
  1.7297 @@ -48966,7 +51100,7 @@
  1.7298      rc = pagerBeginReadTransaction(pPager);
  1.7299    }
  1.7300  
  1.7301 -  if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
  1.7302 +  if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
  1.7303      rc = pagerPagecount(pPager, &pPager->dbSize);
  1.7304    }
  1.7305  
  1.7306 @@ -49099,7 +51233,7 @@
  1.7307        );
  1.7308  
  1.7309        if( rc==SQLITE_OK && pData ){
  1.7310 -        if( pPager->eState>PAGER_READER ){
  1.7311 +        if( pPager->eState>PAGER_READER || pPager->tempFile ){
  1.7312            pPg = sqlite3PagerLookup(pPager, pgno);
  1.7313          }
  1.7314          if( pPg==0 ){
  1.7315 @@ -49126,7 +51260,7 @@
  1.7316          if( rc!=SQLITE_OK ) goto pager_acquire_err;
  1.7317          if( pBase==0 ){
  1.7318            pPg = *ppPage = 0;
  1.7319 -          rc = SQLITE_NOMEM;
  1.7320 +          rc = SQLITE_NOMEM_BKPT;
  1.7321            goto pager_acquire_err;
  1.7322          }
  1.7323        }
  1.7324 @@ -49166,7 +51300,8 @@
  1.7325        goto pager_acquire_err;
  1.7326      }
  1.7327  
  1.7328 -    if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
  1.7329 +    assert( !isOpen(pPager->fd) || !MEMDB );
  1.7330 +    if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
  1.7331        if( pgno>pPager->mxPgno ){
  1.7332          rc = SQLITE_FULL;
  1.7333          goto pager_acquire_err;
  1.7334 @@ -49300,7 +51435,7 @@
  1.7335    if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
  1.7336      pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
  1.7337      if( pPager->pInJournal==0 ){
  1.7338 -      return SQLITE_NOMEM;
  1.7339 +      return SQLITE_NOMEM_BKPT;
  1.7340      }
  1.7341    
  1.7342      /* Open the journal file if it is not already open. */
  1.7343 @@ -49308,24 +51443,24 @@
  1.7344        if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
  1.7345          sqlite3MemJournalOpen(pPager->jfd);
  1.7346        }else{
  1.7347 -        const int flags =                   /* VFS flags to open journal file */
  1.7348 -          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
  1.7349 -          (pPager->tempFile ? 
  1.7350 -            (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
  1.7351 -            (SQLITE_OPEN_MAIN_JOURNAL)
  1.7352 -          );
  1.7353 -
  1.7354 +        int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
  1.7355 +        int nSpill;
  1.7356 +
  1.7357 +        if( pPager->tempFile ){
  1.7358 +          flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
  1.7359 +          nSpill = sqlite3Config.nStmtSpill;
  1.7360 +        }else{
  1.7361 +          flags |= SQLITE_OPEN_MAIN_JOURNAL;
  1.7362 +          nSpill = jrnlBufferSize(pPager);
  1.7363 +        }
  1.7364 +          
  1.7365          /* Verify that the database still has the same name as it did when
  1.7366          ** it was originally opened. */
  1.7367          rc = databaseIsUnmoved(pPager);
  1.7368          if( rc==SQLITE_OK ){
  1.7369 -#ifdef SQLITE_ENABLE_ATOMIC_WRITE
  1.7370 -          rc = sqlite3JournalOpen(
  1.7371 -              pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
  1.7372 +          rc = sqlite3JournalOpen (
  1.7373 +              pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
  1.7374            );
  1.7375 -#else
  1.7376 -          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
  1.7377 -#endif
  1.7378          }
  1.7379        }
  1.7380        assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
  1.7381 @@ -49455,7 +51590,7 @@
  1.7382    assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
  1.7383  
  1.7384    assert( pPager->journalHdr<=pPager->journalOff );
  1.7385 -  CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
  1.7386 +  CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
  1.7387    cksum = pager_cksum(pPager, (u8*)pData2);
  1.7388  
  1.7389    /* Even if an IO or diskfull error occurs while journalling the
  1.7390 @@ -49696,6 +51831,7 @@
  1.7391      if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
  1.7392      return SQLITE_OK;
  1.7393    }else if( pPager->sectorSize > (u32)pPager->pageSize ){
  1.7394 +    assert( pPager->tempFile==0 );
  1.7395      return pagerWriteLargeSector(pPg);
  1.7396    }else{
  1.7397      return pager_write(pPg);
  1.7398 @@ -49726,14 +51862,21 @@
  1.7399  **
  1.7400  ** Tests show that this optimization can quadruple the speed of large 
  1.7401  ** DELETE operations.
  1.7402 +**
  1.7403 +** This optimization cannot be used with a temp-file, as the page may
  1.7404 +** have been dirty at the start of the transaction. In that case, if
  1.7405 +** memory pressure forces page pPg out of the cache, the data does need 
  1.7406 +** to be written out to disk so that it may be read back in if the 
  1.7407 +** current transaction is rolled back.
  1.7408  */
  1.7409  SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
  1.7410    Pager *pPager = pPg->pPager;
  1.7411 -  if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
  1.7412 +  if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
  1.7413      PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
  1.7414      IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
  1.7415      pPg->flags |= PGHDR_DONT_WRITE;
  1.7416      pPg->flags &= ~PGHDR_WRITEABLE;
  1.7417 +    testcase( pPg->flags & PGHDR_NEED_SYNC );
  1.7418      pager_set_pagehash(pPg);
  1.7419    }
  1.7420  }
  1.7421 @@ -49812,7 +51955,7 @@
  1.7422        if( DIRECT_MODE ){
  1.7423          const void *zBuf;
  1.7424          assert( pPager->dbFileSize>0 );
  1.7425 -        CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
  1.7426 +        CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf);
  1.7427          if( rc==SQLITE_OK ){
  1.7428            rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
  1.7429            pPager->aStat[PAGER_STAT_WRITE]++;
  1.7430 @@ -49928,17 +52071,21 @@
  1.7431    /* If a prior error occurred, report that error again. */
  1.7432    if( NEVER(pPager->errCode) ) return pPager->errCode;
  1.7433  
  1.7434 +  /* Provide the ability to easily simulate an I/O error during testing */
  1.7435 +  if( sqlite3FaultSim(400) ) return SQLITE_IOERR;
  1.7436 +
  1.7437    PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
  1.7438        pPager->zFilename, zMaster, pPager->dbSize));
  1.7439  
  1.7440    /* If no database changes have been made, return early. */
  1.7441    if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
  1.7442  
  1.7443 -  if( MEMDB ){
  1.7444 +  assert( MEMDB==0 || pPager->tempFile );
  1.7445 +  assert( isOpen(pPager->fd) || pPager->tempFile );
  1.7446 +  if( 0==pagerFlushOnCommit(pPager, 1) ){
  1.7447      /* If this is an in-memory db, or no pages have been written to, or this
  1.7448      ** function has already been called, it is mostly a no-op.  However, any
  1.7449 -    ** backup in progress needs to be restarted.
  1.7450 -    */
  1.7451 +    ** backup in progress needs to be restarted.  */
  1.7452      sqlite3BackupRestart(pPager->pBackup);
  1.7453    }else{
  1.7454      if( pagerUseWal(pPager) ){
  1.7455 @@ -50277,10 +52424,10 @@
  1.7456  }
  1.7457  
  1.7458  /*
  1.7459 -** Return true if this is an in-memory pager.
  1.7460 +** Return true if this is an in-memory or temp-file backed pager.
  1.7461  */
  1.7462  SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
  1.7463 -  return MEMDB;
  1.7464 +  return pPager->tempFile;
  1.7465  }
  1.7466  
  1.7467  /*
  1.7468 @@ -50311,7 +52458,7 @@
  1.7469        pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
  1.7470    );
  1.7471    if( !aNew ){
  1.7472 -    return SQLITE_NOMEM;
  1.7473 +    return SQLITE_NOMEM_BKPT;
  1.7474    }
  1.7475    memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
  1.7476    pPager->aSavepoint = aNew;
  1.7477 @@ -50327,7 +52474,7 @@
  1.7478      aNew[ii].iSubRec = pPager->nSubRec;
  1.7479      aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
  1.7480      if( !aNew[ii].pInSavepoint ){
  1.7481 -      return SQLITE_NOMEM;
  1.7482 +      return SQLITE_NOMEM_BKPT;
  1.7483      }
  1.7484      if( pagerUseWal(pPager) ){
  1.7485        sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
  1.7486 @@ -50405,7 +52552,7 @@
  1.7487      if( op==SAVEPOINT_RELEASE ){
  1.7488        if( nNew==0 && isOpen(pPager->sjfd) ){
  1.7489          /* Only truncate if it is an in-memory sub-journal. */
  1.7490 -        if( sqlite3IsMemJournal(pPager->sjfd) ){
  1.7491 +        if( sqlite3JournalIsInMemory(pPager->sjfd) ){
  1.7492            rc = sqlite3OsTruncate(pPager->sjfd, 0);
  1.7493            assert( rc==SQLITE_OK );
  1.7494          }
  1.7495 @@ -50476,14 +52623,6 @@
  1.7496    return pPager->zJournal;
  1.7497  }
  1.7498  
  1.7499 -/*
  1.7500 -** Return true if fsync() calls are disabled for this pager.  Return FALSE
  1.7501 -** if fsync()s are executed normally.
  1.7502 -*/
  1.7503 -SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
  1.7504 -  return pPager->noSync;
  1.7505 -}
  1.7506 -
  1.7507  #ifdef SQLITE_HAS_CODEC
  1.7508  /*
  1.7509  ** Set or retrieve the codec for this pager
  1.7510 @@ -50568,7 +52707,8 @@
  1.7511    /* In order to be able to rollback, an in-memory database must journal
  1.7512    ** the page we are moving from.
  1.7513    */
  1.7514 -  if( MEMDB ){
  1.7515 +  assert( pPager->tempFile || !MEMDB );
  1.7516 +  if( pPager->tempFile ){
  1.7517      rc = sqlite3PagerWrite(pPg);
  1.7518      if( rc ) return rc;
  1.7519    }
  1.7520 @@ -50625,7 +52765,7 @@
  1.7521    assert( !pPgOld || pPgOld->nRef==1 );
  1.7522    if( pPgOld ){
  1.7523      pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
  1.7524 -    if( MEMDB ){
  1.7525 +    if( pPager->tempFile ){
  1.7526        /* Do not discard pages from an in-memory database since we might
  1.7527        ** need to rollback later.  Just move the page out of the way. */
  1.7528        sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
  1.7529 @@ -50642,8 +52782,7 @@
  1.7530    ** to exist, in case the transaction needs to roll back.  Use pPgOld
  1.7531    ** as the original page since it has already been allocated.
  1.7532    */
  1.7533 -  if( MEMDB ){
  1.7534 -    assert( pPgOld );
  1.7535 +  if( pPager->tempFile && pPgOld ){
  1.7536      sqlite3PcacheMove(pPgOld, origPgno);
  1.7537      sqlite3PagerUnrefNotNull(pPgOld);
  1.7538    }
  1.7539 @@ -50895,7 +53034,8 @@
  1.7540  ** Unless this is an in-memory or temporary database, clear the pager cache.
  1.7541  */
  1.7542  SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
  1.7543 -  if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
  1.7544 +  assert( MEMDB==0 || pPager->tempFile );
  1.7545 +  if( pPager->tempFile==0 ) pager_reset(pPager);
  1.7546  }
  1.7547  #endif
  1.7548  
  1.7549 @@ -50930,6 +53070,7 @@
  1.7550  */
  1.7551  SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
  1.7552    const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
  1.7553 +  if( pPager->noLock ) return 0;
  1.7554    return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
  1.7555  }
  1.7556  
  1.7557 @@ -51073,6 +53214,7 @@
  1.7558                             pPager->pageSize, (u8*)pPager->pTmpSpace);
  1.7559        pPager->pWal = 0;
  1.7560        pagerFixMaplimit(pPager);
  1.7561 +      if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
  1.7562      }
  1.7563    }
  1.7564    return rc;
  1.7565 @@ -51675,7 +53817,7 @@
  1.7566      apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
  1.7567      if( !apNew ){
  1.7568        *ppPage = 0;
  1.7569 -      return SQLITE_NOMEM;
  1.7570 +      return SQLITE_NOMEM_BKPT;
  1.7571      }
  1.7572      memset((void*)&apNew[pWal->nWiData], 0,
  1.7573             sizeof(u32*)*(iPage+1-pWal->nWiData));
  1.7574 @@ -51687,7 +53829,7 @@
  1.7575    if( pWal->apWiData[iPage]==0 ){
  1.7576      if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
  1.7577        pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
  1.7578 -      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
  1.7579 +      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
  1.7580      }else{
  1.7581        rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
  1.7582            pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
  1.7583 @@ -52302,7 +54444,7 @@
  1.7584      szFrame = szPage + WAL_FRAME_HDRSIZE;
  1.7585      aFrame = (u8 *)sqlite3_malloc64(szFrame);
  1.7586      if( !aFrame ){
  1.7587 -      rc = SQLITE_NOMEM;
  1.7588 +      rc = SQLITE_NOMEM_BKPT;
  1.7589        goto recovery_error;
  1.7590      }
  1.7591      aData = &aFrame[WAL_FRAME_HDRSIZE];
  1.7592 @@ -52440,7 +54582,7 @@
  1.7593    *ppWal = 0;
  1.7594    pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
  1.7595    if( !pRet ){
  1.7596 -    return SQLITE_NOMEM;
  1.7597 +    return SQLITE_NOMEM_BKPT;
  1.7598    }
  1.7599  
  1.7600    pRet->pVfs = pVfs;
  1.7601 @@ -52704,7 +54846,7 @@
  1.7602          + iLast*sizeof(ht_slot);
  1.7603    p = (WalIterator *)sqlite3_malloc64(nByte);
  1.7604    if( !p ){
  1.7605 -    return SQLITE_NOMEM;
  1.7606 +    return SQLITE_NOMEM_BKPT;
  1.7607    }
  1.7608    memset(p, 0, nByte);
  1.7609    p->nSegment = nSegment;
  1.7610 @@ -52716,7 +54858,7 @@
  1.7611        sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
  1.7612    );
  1.7613    if( !aTmp ){
  1.7614 -    rc = SQLITE_NOMEM;
  1.7615 +    rc = SQLITE_NOMEM_BKPT;
  1.7616    }
  1.7617  
  1.7618    for(i=0; rc==SQLITE_OK && i<nSegment; i++){
  1.7619 @@ -54009,7 +56151,7 @@
  1.7620    void *pData;                    /* Data actually written */
  1.7621    u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
  1.7622  #if defined(SQLITE_HAS_CODEC)
  1.7623 -  if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
  1.7624 +  if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT;
  1.7625  #else
  1.7626    pData = pPage->pData;
  1.7627  #endif
  1.7628 @@ -54038,7 +56180,7 @@
  1.7629    i64 iCksumOff;
  1.7630  
  1.7631    aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE);
  1.7632 -  if( aBuf==0 ) return SQLITE_NOMEM;
  1.7633 +  if( aBuf==0 ) return SQLITE_NOMEM_BKPT;
  1.7634  
  1.7635    /* Find the checksum values to use as input for the recalculating the
  1.7636    ** first checksum. If the first frame is frame 1 (implying that the current
  1.7637 @@ -54514,7 +56656,7 @@
  1.7638  
  1.7639    pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
  1.7640    if( pRet==0 ){
  1.7641 -    rc = SQLITE_NOMEM;
  1.7642 +    rc = SQLITE_NOMEM_BKPT;
  1.7643    }else{
  1.7644      memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
  1.7645      *ppSnapshot = (sqlite3_snapshot*)pRet;
  1.7646 @@ -54528,6 +56670,23 @@
  1.7647  SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){
  1.7648    pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
  1.7649  }
  1.7650 +
  1.7651 +/* 
  1.7652 +** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
  1.7653 +** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
  1.7654 +*/
  1.7655 +SQLITE_API int SQLITE_STDCALL sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
  1.7656 +  WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
  1.7657 +  WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
  1.7658 +
  1.7659 +  /* aSalt[0] is a copy of the value stored in the wal file header. It
  1.7660 +  ** is incremented each time the wal file is restarted.  */
  1.7661 +  if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
  1.7662 +  if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
  1.7663 +  if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
  1.7664 +  if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
  1.7665 +  return 0;
  1.7666 +}
  1.7667  #endif /* SQLITE_ENABLE_SNAPSHOT */
  1.7668  
  1.7669  #ifdef SQLITE_ENABLE_ZIPVFS
  1.7670 @@ -55917,7 +58076,7 @@
  1.7671    if( !pLock ){
  1.7672      pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
  1.7673      if( !pLock ){
  1.7674 -      return SQLITE_NOMEM;
  1.7675 +      return SQLITE_NOMEM_BKPT;
  1.7676      }
  1.7677      pLock->iTable = iTable;
  1.7678      pLock->pBtree = p;
  1.7679 @@ -56120,7 +58279,7 @@
  1.7680      assert( pgno<=pBt->nPage );
  1.7681      pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
  1.7682      if( !pBt->pHasContent ){
  1.7683 -      rc = SQLITE_NOMEM;
  1.7684 +      rc = SQLITE_NOMEM_BKPT;
  1.7685      }
  1.7686    }
  1.7687    if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
  1.7688 @@ -56199,7 +58358,7 @@
  1.7689          sqlite3_free(pKey);
  1.7690        }
  1.7691      }else{
  1.7692 -      rc = SQLITE_NOMEM;
  1.7693 +      rc = SQLITE_NOMEM_BKPT;
  1.7694      }
  1.7695    }
  1.7696    assert( !pCur->curIntKey || !pCur->pKey );
  1.7697 @@ -56331,7 +58490,7 @@
  1.7698      pIdxKey = sqlite3VdbeAllocUnpackedRecord(
  1.7699          pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
  1.7700      );
  1.7701 -    if( pIdxKey==0 ) return SQLITE_NOMEM;
  1.7702 +    if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
  1.7703      sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
  1.7704      if( pIdxKey->nField==0 ){
  1.7705        sqlite3DbFree(pCur->pKeyInfo->db, pFree);
  1.7706 @@ -57243,11 +59402,11 @@
  1.7707    pPage->xCellSize = cellSizePtr;
  1.7708    pBt = pPage->pBt;
  1.7709    if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
  1.7710 -    /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior
  1.7711 -    ** table b-tree page. */
  1.7712 +    /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
  1.7713 +    ** interior table b-tree page. */
  1.7714      assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
  1.7715 -    /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf
  1.7716 -    ** table b-tree page. */
  1.7717 +    /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
  1.7718 +    ** leaf table b-tree page. */
  1.7719      assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
  1.7720      pPage->intKey = 1;
  1.7721      if( pPage->leaf ){
  1.7722 @@ -57261,11 +59420,11 @@
  1.7723      pPage->maxLocal = pBt->maxLeaf;
  1.7724      pPage->minLocal = pBt->minLeaf;
  1.7725    }else if( flagByte==PTF_ZERODATA ){
  1.7726 -    /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior
  1.7727 -    ** index b-tree page. */
  1.7728 +    /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
  1.7729 +    ** interior index b-tree page. */
  1.7730      assert( (PTF_ZERODATA)==2 );
  1.7731 -    /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf
  1.7732 -    ** index b-tree page. */
  1.7733 +    /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
  1.7734 +    ** leaf index b-tree page. */
  1.7735      assert( (PTF_ZERODATA|PTF_LEAF)==10 );
  1.7736      pPage->intKey = 0;
  1.7737      pPage->intKeyLeaf = 0;
  1.7738 @@ -57743,7 +59902,7 @@
  1.7739    }
  1.7740    p = sqlite3MallocZero(sizeof(Btree));
  1.7741    if( !p ){
  1.7742 -    return SQLITE_NOMEM;
  1.7743 +    return SQLITE_NOMEM_BKPT;
  1.7744    }
  1.7745    p->inTrans = TRANS_NONE;
  1.7746    p->db = db;
  1.7747 @@ -57767,7 +59926,7 @@
  1.7748        p->sharable = 1;
  1.7749        if( !zFullPathname ){
  1.7750          sqlite3_free(p);
  1.7751 -        return SQLITE_NOMEM;
  1.7752 +        return SQLITE_NOMEM_BKPT;
  1.7753        }
  1.7754        if( isMemdb ){
  1.7755          memcpy(zFullPathname, zFilename, nFilename);
  1.7756 @@ -57835,7 +59994,7 @@
  1.7757    
  1.7758      pBt = sqlite3MallocZero( sizeof(*pBt) );
  1.7759      if( pBt==0 ){
  1.7760 -      rc = SQLITE_NOMEM;
  1.7761 +      rc = SQLITE_NOMEM_BKPT;
  1.7762        goto btree_open_out;
  1.7763      }
  1.7764      rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
  1.7765 @@ -57904,7 +60063,7 @@
  1.7766        if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
  1.7767          pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
  1.7768          if( pBt->mutex==0 ){
  1.7769 -          rc = SQLITE_NOMEM;
  1.7770 +          rc = SQLITE_NOMEM_BKPT;
  1.7771            goto btree_open_out;
  1.7772          }
  1.7773        }
  1.7774 @@ -57927,12 +60086,12 @@
  1.7775      for(i=0; i<db->nDb; i++){
  1.7776        if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
  1.7777          while( pSib->pPrev ){ pSib = pSib->pPrev; }
  1.7778 -        if( p->pBt<pSib->pBt ){
  1.7779 +        if( (uptr)p->pBt<(uptr)pSib->pBt ){
  1.7780            p->pNext = pSib;
  1.7781            p->pPrev = 0;
  1.7782            pSib->pPrev = p;
  1.7783          }else{
  1.7784 -          while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
  1.7785 +          while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
  1.7786              pSib = pSib->pNext;
  1.7787            }
  1.7788            p->pNext = pSib->pNext;
  1.7789 @@ -58187,21 +60346,6 @@
  1.7790  #endif
  1.7791  
  1.7792  /*
  1.7793 -** Return TRUE if the given btree is set to safety level 1.  In other
  1.7794 -** words, return TRUE if no sync() occurs on the disk files.
  1.7795 -*/
  1.7796 -SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
  1.7797 -  BtShared *pBt = p->pBt;
  1.7798 -  int rc;
  1.7799 -  assert( sqlite3_mutex_held(p->db->mutex) );  
  1.7800 -  sqlite3BtreeEnter(p);
  1.7801 -  assert( pBt && pBt->pPager );
  1.7802 -  rc = sqlite3PagerNosync(pBt->pPager);
  1.7803 -  sqlite3BtreeLeave(p);
  1.7804 -  return rc;
  1.7805 -}
  1.7806 -
  1.7807 -/*
  1.7808  ** Change the default pages size and the number of reserved bytes per page.
  1.7809  ** Or, if the page size has already been fixed, return SQLITE_READONLY 
  1.7810  ** without changing anything.
  1.7811 @@ -58446,9 +60590,25 @@
  1.7812        rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
  1.7813        if( rc!=SQLITE_OK ){
  1.7814          goto page1_init_failed;
  1.7815 -      }else if( isOpen==0 ){
  1.7816 -        releasePage(pPage1);
  1.7817 -        return SQLITE_OK;
  1.7818 +      }else{
  1.7819 +#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS
  1.7820 +        sqlite3 *db;
  1.7821 +        Db *pDb;
  1.7822 +        if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
  1.7823 +          while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
  1.7824 +          if( pDb->bSyncSet==0
  1.7825 +           && pDb->safety_level==SQLITE_DEFAULT_SYNCHRONOUS+1
  1.7826 +          ){
  1.7827 +            pDb->safety_level = SQLITE_DEFAULT_WAL_SYNCHRONOUS+1;
  1.7828 +            sqlite3PagerSetFlags(pBt->pPager,
  1.7829 +               pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
  1.7830 +          }
  1.7831 +        }
  1.7832 +#endif
  1.7833 +        if( isOpen==0 ){
  1.7834 +          releasePage(pPage1);
  1.7835 +          return SQLITE_OK;
  1.7836 +        }
  1.7837        }
  1.7838        rc = SQLITE_NOTADB;
  1.7839      }
  1.7840 @@ -59681,7 +61841,7 @@
  1.7841  
  1.7842    if( wrFlag ){
  1.7843      allocateTempSpace(pBt);
  1.7844 -    if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM;
  1.7845 +    if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
  1.7846    }
  1.7847    if( iTable==1 && btreePagecount(pBt)==0 ){
  1.7848      assert( wrFlag==0 );
  1.7849 @@ -60038,8 +62198,13 @@
  1.7850  #endif
  1.7851    assert( offset+amt <= pCur->info.nPayload );
  1.7852  
  1.7853 -  if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){
  1.7854 -    /* Trying to read or write past the end of the data is an error */
  1.7855 +  assert( aPayload > pPage->aData );
  1.7856 +  if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
  1.7857 +    /* Trying to read or write past the end of the data is an error.  The
  1.7858 +    ** conditional above is really:
  1.7859 +    **    &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
  1.7860 +    ** but is recast into its current form to avoid integer overflow problems
  1.7861 +    */
  1.7862      return SQLITE_CORRUPT_BKPT;
  1.7863    }
  1.7864  
  1.7865 @@ -60079,7 +62244,7 @@
  1.7866              pCur->aOverflow, nOvfl*2*sizeof(Pgno)
  1.7867          );
  1.7868          if( aNew==0 ){
  1.7869 -          rc = SQLITE_NOMEM;
  1.7870 +          rc = SQLITE_NOMEM_BKPT;
  1.7871          }else{
  1.7872            pCur->nOvflAlloc = nOvfl*2;
  1.7873            pCur->aOverflow = aNew;
  1.7874 @@ -60784,7 +62949,7 @@
  1.7875            }
  1.7876            pCellKey = sqlite3Malloc( nCell+18 );
  1.7877            if( pCellKey==0 ){
  1.7878 -            rc = SQLITE_NOMEM;
  1.7879 +            rc = SQLITE_NOMEM_BKPT;
  1.7880              goto moveto_finish;
  1.7881            }
  1.7882            pCur->aiIdx[pCur->iPage] = (u16)idx;
  1.7883 @@ -62106,8 +64271,8 @@
  1.7884      u8 *pSlot;
  1.7885      sz = cachedCellSize(pCArray, i);
  1.7886      if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
  1.7887 +      if( (pData - pBegin)<sz ) return 1;
  1.7888        pData -= sz;
  1.7889 -      if( pData<pBegin ) return 1;
  1.7890        pSlot = pData;
  1.7891      }
  1.7892      /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
  1.7893 @@ -62269,7 +64434,7 @@
  1.7894    for(i=0; i<nNew && !CORRUPT_DB; i++){
  1.7895      u8 *pCell = pCArray->apCell[i+iNew];
  1.7896      int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
  1.7897 -    if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){
  1.7898 +    if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
  1.7899        pCell = &pTmp[pCell - aData];
  1.7900      }
  1.7901      assert( 0==memcmp(pCell, &aData[iOff],
  1.7902 @@ -62603,7 +64768,7 @@
  1.7903    assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
  1.7904  
  1.7905    if( !aOvflSpace ){
  1.7906 -    return SQLITE_NOMEM;
  1.7907 +    return SQLITE_NOMEM_BKPT;
  1.7908    }
  1.7909  
  1.7910    /* Find the sibling pages to balance. Also locate the cells in pParent 
  1.7911 @@ -62703,7 +64868,7 @@
  1.7912    assert( szScratch<=6*(int)pBt->pageSize );
  1.7913    b.apCell = sqlite3ScratchMalloc( szScratch ); 
  1.7914    if( b.apCell==0 ){
  1.7915 -    rc = SQLITE_NOMEM;
  1.7916 +    rc = SQLITE_NOMEM_BKPT;
  1.7917      goto balance_cleanup;
  1.7918    }
  1.7919    b.szCell = (u16*)&b.apCell[nMaxCells];
  1.7920 @@ -63138,9 +65303,9 @@
  1.7921        ** any cell). But it is important to pass the correct size to 
  1.7922        ** insertCell(), so reparse the cell now.
  1.7923        **
  1.7924 -      ** Note that this can never happen in an SQLite data file, as all
  1.7925 -      ** cells are at least 4 bytes. It only happens in b-trees used
  1.7926 -      ** to evaluate "IN (SELECT ...)" and similar clauses.
  1.7927 +      ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
  1.7928 +      ** and WITHOUT ROWID tables with exactly one column which is the
  1.7929 +      ** primary key.
  1.7930        */
  1.7931        if( b.szCell[j]==4 ){
  1.7932          assert(leafCorrection==4);
  1.7933 @@ -63701,6 +65866,28 @@
  1.7934    pPage = pCur->apPage[iCellDepth];
  1.7935    pCell = findCell(pPage, iCellIdx);
  1.7936  
  1.7937 +  /* If the bPreserve flag is set to true, then the cursor position must
  1.7938 +  ** be preserved following this delete operation. If the current delete
  1.7939 +  ** will cause a b-tree rebalance, then this is done by saving the cursor
  1.7940 +  ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
  1.7941 +  ** returning. 
  1.7942 +  **
  1.7943 +  ** Or, if the current delete will not cause a rebalance, then the cursor
  1.7944 +  ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
  1.7945 +  ** before or after the deleted entry. In this case set bSkipnext to true.  */
  1.7946 +  if( bPreserve ){
  1.7947 +    if( !pPage->leaf 
  1.7948 +     || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
  1.7949 +    ){
  1.7950 +      /* A b-tree rebalance will be required after deleting this entry.
  1.7951 +      ** Save the cursor key.  */
  1.7952 +      rc = saveCursorKey(pCur);
  1.7953 +      if( rc ) return rc;
  1.7954 +    }else{
  1.7955 +      bSkipnext = 1;
  1.7956 +    }
  1.7957 +  }
  1.7958 +
  1.7959    /* If the page containing the entry to delete is not a leaf page, move
  1.7960    ** the cursor to the largest entry in the tree that is smaller than
  1.7961    ** the entry being deleted. This cell will replace the cell being deleted
  1.7962 @@ -63727,28 +65914,6 @@
  1.7963      invalidateIncrblobCursors(p, pCur->info.nKey, 0);
  1.7964    }
  1.7965  
  1.7966 -  /* If the bPreserve flag is set to true, then the cursor position must
  1.7967 -  ** be preserved following this delete operation. If the current delete
  1.7968 -  ** will cause a b-tree rebalance, then this is done by saving the cursor
  1.7969 -  ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
  1.7970 -  ** returning. 
  1.7971 -  **
  1.7972 -  ** Or, if the current delete will not cause a rebalance, then the cursor
  1.7973 -  ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
  1.7974 -  ** before or after the deleted entry. In this case set bSkipnext to true.  */
  1.7975 -  if( bPreserve ){
  1.7976 -    if( !pPage->leaf 
  1.7977 -     || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
  1.7978 -    ){
  1.7979 -      /* A b-tree rebalance will be required after deleting this entry.
  1.7980 -      ** Save the cursor key.  */
  1.7981 -      rc = saveCursorKey(pCur);
  1.7982 -      if( rc ) return rc;
  1.7983 -    }else{
  1.7984 -      bSkipnext = 1;
  1.7985 -    }
  1.7986 -  }
  1.7987 -
  1.7988    /* Make the page containing the entry to be deleted writable. Then free any
  1.7989    ** overflow pages associated with the entry and finally remove the cell
  1.7990    ** itself from within the page.  */
  1.7991 @@ -65359,7 +67524,7 @@
  1.7992      pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
  1.7993      if( pParse==0 ){
  1.7994        sqlite3ErrorWithMsg(pErrorDb, SQLITE_NOMEM, "out of memory");
  1.7995 -      rc = SQLITE_NOMEM;
  1.7996 +      rc = SQLITE_NOMEM_BKPT;
  1.7997      }else{
  1.7998        pParse->db = pDb;
  1.7999        if( sqlite3OpenTempDatabase(pParse) ){
  1.8000 @@ -65453,7 +67618,7 @@
  1.8001      ** sqlite3_backup_finish(). */
  1.8002      p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
  1.8003      if( !p ){
  1.8004 -      sqlite3Error(pDestDb, SQLITE_NOMEM);
  1.8005 +      sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT);
  1.8006      }
  1.8007    }
  1.8008  
  1.8009 @@ -65852,7 +68017,7 @@
  1.8010      }