new sqlite3
authorVolker Birk <vb@pep-project.org>
Mon, 06 Jun 2016 17:35:13 +0200
changeset 716498156bf9993
parent 714 aa1c9737adf7
child 717 4307c2cf21d4
new sqlite3
src/Makefile
src/sqlite3.c
src/sqlite3.h
     1.1 --- a/src/Makefile	Mon Jun 06 16:36:22 2016 +0200
     1.2 +++ b/src/Makefile	Mon Jun 06 17:35:13 2016 +0200
     1.3 @@ -30,7 +30,7 @@
     1.4  CFLAGS?=-I$(GPGME_IN)/include $(OPTIMIZE) -fPIC -pedantic \
     1.5  	-DSYSTEM_DB=\"$(SYSTEM_DB)\" -DLIBGPGME=\"$(LIBGPGME)\" \
     1.6  	-DSQLITE_THREADSAFE=1 -D_GNU_SOURCE -I../asn.1
     1.7 -LDFLAGS?=-L$(GPGME_IN)/lib -shared -lc -ldl -letpan -lpthread -L../asn.1 -lasn1
     1.8 +LDFLAGS?=-L$(GPGME_IN)/lib -shared -lc -ldl -letpan -lsqlite3 -lpthread -L../asn.1 -lasn1
     1.9  
    1.10  else
    1.11  $(error don't know how to make for $(BUILD_FOR) on $(BUILD_ON))
     2.1 --- a/src/sqlite3.c	Mon Jun 06 16:36:22 2016 +0200
     2.2 +++ b/src/sqlite3.c	Mon Jun 06 17:35:13 2016 +0200
     2.3 @@ -1,6 +1,6 @@
     2.4  /******************************************************************************
     2.5  ** This file is an amalgamation of many separate C source files from SQLite
     2.6 -** version 3.11.0.  By combining all the individual C code files into this 
     2.7 +** version 3.13.0.  By combining all the individual C code files into this 
     2.8  ** single large file, the entire code can be compiled as a single translation
     2.9  ** unit.  This allows many compilers to do optimizations that would not be
    2.10  ** possible if the files were compiled separately.  Performance improvements
    2.11 @@ -40,6 +40,41 @@
    2.12  #ifndef _SQLITEINT_H_
    2.13  #define _SQLITEINT_H_
    2.14  
    2.15 +/* Special Comments:
    2.16 +**
    2.17 +** Some comments have special meaning to the tools that measure test
    2.18 +** coverage:
    2.19 +**
    2.20 +**    NO_TEST                     - The branches on this line are not
    2.21 +**                                  measured by branch coverage.  This is
    2.22 +**                                  used on lines of code that actually
    2.23 +**                                  implement parts of coverage testing.
    2.24 +**
    2.25 +**    OPTIMIZATION-IF-TRUE        - This branch is allowed to alway be false
    2.26 +**                                  and the correct answer is still obtained,
    2.27 +**                                  though perhaps more slowly.
    2.28 +**
    2.29 +**    OPTIMIZATION-IF-FALSE       - This branch is allowed to alway be true
    2.30 +**                                  and the correct answer is still obtained,
    2.31 +**                                  though perhaps more slowly.
    2.32 +**
    2.33 +**    PREVENTS-HARMLESS-OVERREAD  - This branch prevents a buffer overread
    2.34 +**                                  that would be harmless and undetectable
    2.35 +**                                  if it did occur.  
    2.36 +**
    2.37 +** In all cases, the special comment must be enclosed in the usual
    2.38 +** slash-asterisk...asterisk-slash comment marks, with no spaces between the 
    2.39 +** asterisks and the comment text.
    2.40 +*/
    2.41 +
    2.42 +/*
    2.43 +** Make sure that rand_s() is available on Windows systems with MSVC 2005
    2.44 +** or higher.
    2.45 +*/
    2.46 +#if defined(_MSC_VER) && _MSC_VER>=1400
    2.47 +#  define _CRT_RAND_S
    2.48 +#endif
    2.49 +
    2.50  /*
    2.51  ** Include the header file used to customize the compiler options for MSVC.
    2.52  ** This should be done first so that it can successfully prevent spurious
    2.53 @@ -328,9 +363,9 @@
    2.54  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
    2.55  ** [sqlite_version()] and [sqlite_source_id()].
    2.56  */
    2.57 -#define SQLITE_VERSION        "3.11.0"
    2.58 -#define SQLITE_VERSION_NUMBER 3011000
    2.59 -#define SQLITE_SOURCE_ID      "2016-02-15 17:29:24 3d862f207e3adc00f78066799ac5a8c282430a5f"
    2.60 +#define SQLITE_VERSION        "3.13.0"
    2.61 +#define SQLITE_VERSION_NUMBER 3013000
    2.62 +#define SQLITE_SOURCE_ID      "2016-05-18 10:57:30 fc49f556e48970561d7ab6a2f24fdd7d9eb81ff2"
    2.63  
    2.64  /*
    2.65  ** CAPI3REF: Run-Time Library Version Numbers
    2.66 @@ -1445,7 +1480,7 @@
    2.67    const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
    2.68    /*
    2.69    ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
    2.70 -  ** New fields may be appended in figure versions.  The iVersion
    2.71 +  ** New fields may be appended in future versions.  The iVersion
    2.72    ** value will increment whenever this happens. 
    2.73    */
    2.74  };
    2.75 @@ -2037,6 +2072,20 @@
    2.76  ** is enabled (using the [PRAGMA threads] command) and the amount of content
    2.77  ** to be sorted exceeds the page size times the minimum of the
    2.78  ** [PRAGMA cache_size] setting and this value.
    2.79 +**
    2.80 +** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
    2.81 +** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
    2.82 +** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
    2.83 +** becomes the [statement journal] spill-to-disk threshold.  
    2.84 +** [Statement journals] are held in memory until their size (in bytes)
    2.85 +** exceeds this threshold, at which point they are written to disk.
    2.86 +** Or if the threshold is -1, statement journals are always held
    2.87 +** exclusively in memory.
    2.88 +** Since many statement journals never become large, setting the spill
    2.89 +** threshold to a value such as 64KiB can greatly reduce the amount of
    2.90 +** I/O required to support statement rollback.
    2.91 +** The default value for this setting is controlled by the
    2.92 +** [SQLITE_STMTJRNL_SPILL] compile-time option.
    2.93  ** </dl>
    2.94  */
    2.95  #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
    2.96 @@ -2064,6 +2113,7 @@
    2.97  #define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
    2.98  #define SQLITE_CONFIG_PCACHE_HDRSZ        24  /* int *psz */
    2.99  #define SQLITE_CONFIG_PMASZ               25  /* unsigned int szPma */
   2.100 +#define SQLITE_CONFIG_STMTJRNL_SPILL      26  /* int nByte */
   2.101  
   2.102  /*
   2.103  ** CAPI3REF: Database Connection Configuration Options
   2.104 @@ -2121,11 +2171,43 @@
   2.105  ** following this call.  The second parameter may be a NULL pointer, in
   2.106  ** which case the trigger setting is not reported back. </dd>
   2.107  **
   2.108 +** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
   2.109 +** <dd> ^This option is used to enable or disable the two-argument
   2.110 +** version of the [fts3_tokenizer()] function which is part of the
   2.111 +** [FTS3] full-text search engine extension.
   2.112 +** There should be two additional arguments.
   2.113 +** The first argument is an integer which is 0 to disable fts3_tokenizer() or
   2.114 +** positive to enable fts3_tokenizer() or negative to leave the setting
   2.115 +** unchanged.
   2.116 +** The second parameter is a pointer to an integer into which
   2.117 +** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
   2.118 +** following this call.  The second parameter may be a NULL pointer, in
   2.119 +** which case the new setting is not reported back. </dd>
   2.120 +**
   2.121 +** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
   2.122 +** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
   2.123 +** interface independently of the [load_extension()] SQL function.
   2.124 +** The [sqlite3_enable_load_extension()] API enables or disables both the
   2.125 +** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
   2.126 +** There should be two additional arguments.
   2.127 +** When the first argument to this interface is 1, then only the C-API is
   2.128 +** enabled and the SQL function remains disabled.  If the first argment to
   2.129 +** this interface is 0, then both the C-API and the SQL function are disabled.
   2.130 +** If the first argument is -1, then no changes are made to state of either the
   2.131 +** C-API or the SQL function.
   2.132 +** The second parameter is a pointer to an integer into which
   2.133 +** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
   2.134 +** is disabled or enabled following this call.  The second parameter may
   2.135 +** be a NULL pointer, in which case the new setting is not reported back.
   2.136 +** </dd>
   2.137 +**
   2.138  ** </dl>
   2.139  */
   2.140 -#define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
   2.141 -#define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
   2.142 -#define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
   2.143 +#define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
   2.144 +#define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
   2.145 +#define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
   2.146 +#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
   2.147 +#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
   2.148  
   2.149  
   2.150  /*
   2.151 @@ -5375,7 +5457,7 @@
   2.152  ** ^The sqlite3_update_hook() interface registers a callback function
   2.153  ** with the [database connection] identified by the first argument
   2.154  ** to be invoked whenever a row is updated, inserted or deleted in
   2.155 -** a rowid table.
   2.156 +** a [rowid table].
   2.157  ** ^Any callback set by a previous call to this function
   2.158  ** for the same database connection is overridden.
   2.159  **
   2.160 @@ -5414,8 +5496,8 @@
   2.161  ** on the same [database connection] D, or NULL for
   2.162  ** the first call on D.
   2.163  **
   2.164 -** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
   2.165 -** interfaces.
   2.166 +** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
   2.167 +** and [sqlite3_preupdate_hook()] interfaces.
   2.168  */
   2.169  SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
   2.170    sqlite3*, 
   2.171 @@ -5662,9 +5744,18 @@
   2.172  ** should free this memory by calling [sqlite3_free()].
   2.173  **
   2.174  ** ^Extension loading must be enabled using
   2.175 -** [sqlite3_enable_load_extension()] prior to calling this API,
   2.176 +** [sqlite3_enable_load_extension()] or
   2.177 +** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
   2.178 +** prior to calling this API,
   2.179  ** otherwise an error will be returned.
   2.180  **
   2.181 +** <b>Security warning:</b> It is recommended that the 
   2.182 +** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
   2.183 +** interface.  The use of the [sqlite3_enable_load_extension()] interface
   2.184 +** should be avoided.  This will keep the SQL function [load_extension()]
   2.185 +** disabled and prevent SQL injections from giving attackers
   2.186 +** access to extension loading capabilities.
   2.187 +**
   2.188  ** See also the [load_extension() SQL function].
   2.189  */
   2.190  SQLITE_API int SQLITE_STDCALL sqlite3_load_extension(
   2.191 @@ -5687,6 +5778,17 @@
   2.192  ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
   2.193  ** to turn extension loading on and call it with onoff==0 to turn
   2.194  ** it back off again.
   2.195 +**
   2.196 +** ^This interface enables or disables both the C-API
   2.197 +** [sqlite3_load_extension()] and the SQL function [load_extension()].
   2.198 +** Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
   2.199 +** to enable or disable only the C-API.
   2.200 +**
   2.201 +** <b>Security warning:</b> It is recommended that extension loading
   2.202 +** be disabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
   2.203 +** rather than this interface, so the [load_extension()] SQL function
   2.204 +** remains disabled. This will prevent SQL injections from giving attackers
   2.205 +** access to extension loading capabilities.
   2.206  */
   2.207  SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int onoff);
   2.208  
   2.209 @@ -7325,7 +7427,7 @@
   2.210  ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
   2.211  ** an error.
   2.212  **
   2.213 -** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if 
   2.214 +** ^A call to sqlite3_backup_init() will fail, returning NULL, if 
   2.215  ** there is already a read or read-write transaction open on the 
   2.216  ** destination database.
   2.217  **
   2.218 @@ -7706,7 +7808,7 @@
   2.219  ** previously registered write-ahead log callback. ^Note that the
   2.220  ** [sqlite3_wal_autocheckpoint()] interface and the
   2.221  ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
   2.222 -** those overwrite any prior [sqlite3_wal_hook()] settings.
   2.223 +** overwrite any prior [sqlite3_wal_hook()] settings.
   2.224  */
   2.225  SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook(
   2.226    sqlite3*, 
   2.227 @@ -8104,6 +8206,114 @@
   2.228  SQLITE_API int SQLITE_STDCALL sqlite3_db_cacheflush(sqlite3*);
   2.229  
   2.230  /*
   2.231 +** CAPI3REF: The pre-update hook.
   2.232 +**
   2.233 +** ^These interfaces are only available if SQLite is compiled using the
   2.234 +** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
   2.235 +**
   2.236 +** ^The [sqlite3_preupdate_hook()] interface registers a callback function
   2.237 +** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
   2.238 +** on a [rowid table].
   2.239 +** ^At most one preupdate hook may be registered at a time on a single
   2.240 +** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
   2.241 +** the previous setting.
   2.242 +** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
   2.243 +** with a NULL pointer as the second parameter.
   2.244 +** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
   2.245 +** the first parameter to callbacks.
   2.246 +**
   2.247 +** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate
   2.248 +** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID]
   2.249 +** tables.
   2.250 +**
   2.251 +** ^The second parameter to the preupdate callback is a pointer to
   2.252 +** the [database connection] that registered the preupdate hook.
   2.253 +** ^The third parameter to the preupdate callback is one of the constants
   2.254 +** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to indentify the
   2.255 +** kind of update operation that is about to occur.
   2.256 +** ^(The fourth parameter to the preupdate callback is the name of the
   2.257 +** database within the database connection that is being modified.  This
   2.258 +** will be "main" for the main database or "temp" for TEMP tables or 
   2.259 +** the name given after the AS keyword in the [ATTACH] statement for attached
   2.260 +** databases.)^
   2.261 +** ^The fifth parameter to the preupdate callback is the name of the
   2.262 +** table that is being modified.
   2.263 +** ^The sixth parameter to the preupdate callback is the initial [rowid] of the
   2.264 +** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is
   2.265 +** undefined for SQLITE_INSERT changes.
   2.266 +** ^The seventh parameter to the preupdate callback is the final [rowid] of
   2.267 +** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is
   2.268 +** undefined for SQLITE_DELETE changes.
   2.269 +**
   2.270 +** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
   2.271 +** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
   2.272 +** provide additional information about a preupdate event. These routines
   2.273 +** may only be called from within a preupdate callback.  Invoking any of
   2.274 +** these routines from outside of a preupdate callback or with a
   2.275 +** [database connection] pointer that is different from the one supplied
   2.276 +** to the preupdate callback results in undefined and probably undesirable
   2.277 +** behavior.
   2.278 +**
   2.279 +** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
   2.280 +** in the row that is being inserted, updated, or deleted.
   2.281 +**
   2.282 +** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
   2.283 +** a [protected sqlite3_value] that contains the value of the Nth column of
   2.284 +** the table row before it is updated.  The N parameter must be between 0
   2.285 +** and one less than the number of columns or the behavior will be
   2.286 +** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
   2.287 +** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
   2.288 +** behavior is undefined.  The [sqlite3_value] that P points to
   2.289 +** will be destroyed when the preupdate callback returns.
   2.290 +**
   2.291 +** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
   2.292 +** a [protected sqlite3_value] that contains the value of the Nth column of
   2.293 +** the table row after it is updated.  The N parameter must be between 0
   2.294 +** and one less than the number of columns or the behavior will be
   2.295 +** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
   2.296 +** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
   2.297 +** behavior is undefined.  The [sqlite3_value] that P points to
   2.298 +** will be destroyed when the preupdate callback returns.
   2.299 +**
   2.300 +** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
   2.301 +** callback was invoked as a result of a direct insert, update, or delete
   2.302 +** operation; or 1 for inserts, updates, or deletes invoked by top-level 
   2.303 +** triggers; or 2 for changes resulting from triggers called by top-level
   2.304 +** triggers; and so forth.
   2.305 +**
   2.306 +** See also:  [sqlite3_update_hook()]
   2.307 +*/
   2.308 +SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_preupdate_hook(
   2.309 +  sqlite3 *db,
   2.310 +  void(*xPreUpdate)(
   2.311 +    void *pCtx,                   /* Copy of third arg to preupdate_hook() */
   2.312 +    sqlite3 *db,                  /* Database handle */
   2.313 +    int op,                       /* SQLITE_UPDATE, DELETE or INSERT */
   2.314 +    char const *zDb,              /* Database name */
   2.315 +    char const *zName,            /* Table name */
   2.316 +    sqlite3_int64 iKey1,          /* Rowid of row about to be deleted/updated */
   2.317 +    sqlite3_int64 iKey2           /* New rowid value (for a rowid UPDATE) */
   2.318 +  ),
   2.319 +  void*
   2.320 +);
   2.321 +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
   2.322 +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_count(sqlite3 *);
   2.323 +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_depth(sqlite3 *);
   2.324 +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
   2.325 +
   2.326 +/*
   2.327 +** CAPI3REF: Low-level system error code
   2.328 +**
   2.329 +** ^Attempt to return the underlying operating system error code or error
   2.330 +** number that caused the most recent I/O error or failure to open a file.
   2.331 +** The return value is OS-dependent.  For example, on unix systems, after
   2.332 +** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
   2.333 +** called to get back the underlying "errno" that caused the problem, such
   2.334 +** as ENOSPC, EAUTH, EISDIR, and so forth.  
   2.335 +*/
   2.336 +SQLITE_API int SQLITE_STDCALL sqlite3_system_errno(sqlite3*);
   2.337 +
   2.338 +/*
   2.339  ** CAPI3REF: Database Snapshot
   2.340  ** KEYWORDS: {snapshot}
   2.341  ** EXPERIMENTAL
   2.342 @@ -8161,17 +8371,30 @@
   2.343  ** CAPI3REF: Start a read transaction on an historical snapshot
   2.344  ** EXPERIMENTAL
   2.345  **
   2.346 -** ^The [sqlite3_snapshot_open(D,S,P)] interface attempts to move the
   2.347 -** read transaction that is currently open on schema S of
   2.348 -** [database connection] D so that it refers to historical [snapshot] P.
   2.349 +** ^The [sqlite3_snapshot_open(D,S,P)] interface starts a
   2.350 +** read transaction for schema S of
   2.351 +** [database connection] D such that the read transaction
   2.352 +** refers to historical [snapshot] P, rather than the most
   2.353 +** recent change to the database.
   2.354  ** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success
   2.355  ** or an appropriate [error code] if it fails.
   2.356  **
   2.357  ** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be
   2.358 -** the first operation, apart from other sqlite3_snapshot_open() calls,
   2.359 -** following the [BEGIN] that starts a new read transaction.
   2.360 -** ^A [snapshot] will fail to open if it has been overwritten by a 
   2.361 -** [checkpoint].  
   2.362 +** the first operation following the [BEGIN] that takes the schema S
   2.363 +** out of [autocommit mode].
   2.364 +** ^In other words, schema S must not currently be in
   2.365 +** a transaction for [sqlite3_snapshot_open(D,S,P)] to work, but the
   2.366 +** database connection D must be out of [autocommit mode].
   2.367 +** ^A [snapshot] will fail to open if it has been overwritten by a
   2.368 +** [checkpoint].
   2.369 +** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
   2.370 +** database connection D does not know that the database file for
   2.371 +** schema S is in [WAL mode].  A database connection might not know
   2.372 +** that the database file is in [WAL mode] if there has been no prior
   2.373 +** I/O on that database connection, or if the database entered [WAL mode] 
   2.374 +** after the most recent I/O on the database connection.)^
   2.375 +** (Hint: Run "[PRAGMA application_id]" against a newly opened
   2.376 +** database connection in order to make it ready to use snapshots.)
   2.377  **
   2.378  ** The [sqlite3_snapshot_open()] interface is only available when the
   2.379  ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
   2.380 @@ -8196,6 +8419,33 @@
   2.381  SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_snapshot_free(sqlite3_snapshot*);
   2.382  
   2.383  /*
   2.384 +** CAPI3REF: Compare the ages of two snapshot handles.
   2.385 +** EXPERIMENTAL
   2.386 +**
   2.387 +** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
   2.388 +** of two valid snapshot handles. 
   2.389 +**
   2.390 +** If the two snapshot handles are not associated with the same database 
   2.391 +** file, the result of the comparison is undefined. 
   2.392 +**
   2.393 +** Additionally, the result of the comparison is only valid if both of the
   2.394 +** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
   2.395 +** last time the wal file was deleted. The wal file is deleted when the
   2.396 +** database is changed back to rollback mode or when the number of database
   2.397 +** clients drops to zero. If either snapshot handle was obtained before the 
   2.398 +** wal file was last deleted, the value returned by this function 
   2.399 +** is undefined.
   2.400 +**
   2.401 +** Otherwise, this API returns a negative value if P1 refers to an older
   2.402 +** snapshot than P2, zero if the two handles refer to the same database
   2.403 +** snapshot, and a positive value if P1 is a newer snapshot than P2.
   2.404 +*/
   2.405 +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_cmp(
   2.406 +  sqlite3_snapshot *p1,
   2.407 +  sqlite3_snapshot *p2
   2.408 +);
   2.409 +
   2.410 +/*
   2.411  ** Undo the hack that converts floating point types to integer for
   2.412  ** builds on processors without floating point support.
   2.413  */
   2.414 @@ -8208,6 +8458,7 @@
   2.415  #endif
   2.416  #endif /* _SQLITE3_H_ */
   2.417  
   2.418 +/******** Begin file sqlite3rtree.h *********/
   2.419  /*
   2.420  ** 2010 August 30
   2.421  **
   2.422 @@ -8325,6 +8576,1287 @@
   2.423  
   2.424  #endif  /* ifndef _SQLITE3RTREE_H_ */
   2.425  
   2.426 +/******** End of sqlite3rtree.h *********/
   2.427 +/******** Begin file sqlite3session.h *********/
   2.428 +
   2.429 +#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
   2.430 +#define __SQLITESESSION_H_ 1
   2.431 +
   2.432 +/*
   2.433 +** Make sure we can call this stuff from C++.
   2.434 +*/
   2.435 +#if 0
   2.436 +extern "C" {
   2.437 +#endif
   2.438 +
   2.439 +
   2.440 +/*
   2.441 +** CAPI3REF: Session Object Handle
   2.442 +*/
   2.443 +typedef struct sqlite3_session sqlite3_session;
   2.444 +
   2.445 +/*
   2.446 +** CAPI3REF: Changeset Iterator Handle
   2.447 +*/
   2.448 +typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
   2.449 +
   2.450 +/*
   2.451 +** CAPI3REF: Create A New Session Object
   2.452 +**
   2.453 +** Create a new session object attached to database handle db. If successful,
   2.454 +** a pointer to the new object is written to *ppSession and SQLITE_OK is
   2.455 +** returned. If an error occurs, *ppSession is set to NULL and an SQLite
   2.456 +** error code (e.g. SQLITE_NOMEM) is returned.
   2.457 +**
   2.458 +** It is possible to create multiple session objects attached to a single
   2.459 +** database handle.
   2.460 +**
   2.461 +** Session objects created using this function should be deleted using the
   2.462 +** [sqlite3session_delete()] function before the database handle that they
   2.463 +** are attached to is itself closed. If the database handle is closed before
   2.464 +** the session object is deleted, then the results of calling any session
   2.465 +** module function, including [sqlite3session_delete()] on the session object
   2.466 +** are undefined.
   2.467 +**
   2.468 +** Because the session module uses the [sqlite3_preupdate_hook()] API, it
   2.469 +** is not possible for an application to register a pre-update hook on a
   2.470 +** database handle that has one or more session objects attached. Nor is
   2.471 +** it possible to create a session object attached to a database handle for
   2.472 +** which a pre-update hook is already defined. The results of attempting 
   2.473 +** either of these things are undefined.
   2.474 +**
   2.475 +** The session object will be used to create changesets for tables in
   2.476 +** database zDb, where zDb is either "main", or "temp", or the name of an
   2.477 +** attached database. It is not an error if database zDb is not attached
   2.478 +** to the database when the session object is created.
   2.479 +*/
   2.480 +int sqlite3session_create(
   2.481 +  sqlite3 *db,                    /* Database handle */
   2.482 +  const char *zDb,                /* Name of db (e.g. "main") */
   2.483 +  sqlite3_session **ppSession     /* OUT: New session object */
   2.484 +);
   2.485 +
   2.486 +/*
   2.487 +** CAPI3REF: Delete A Session Object
   2.488 +**
   2.489 +** Delete a session object previously allocated using 
   2.490 +** [sqlite3session_create()]. Once a session object has been deleted, the
   2.491 +** results of attempting to use pSession with any other session module
   2.492 +** function are undefined.
   2.493 +**
   2.494 +** Session objects must be deleted before the database handle to which they
   2.495 +** are attached is closed. Refer to the documentation for 
   2.496 +** [sqlite3session_create()] for details.
   2.497 +*/
   2.498 +void sqlite3session_delete(sqlite3_session *pSession);
   2.499 +
   2.500 +
   2.501 +/*
   2.502 +** CAPI3REF: Enable Or Disable A Session Object
   2.503 +**
   2.504 +** Enable or disable the recording of changes by a session object. When
   2.505 +** enabled, a session object records changes made to the database. When
   2.506 +** disabled - it does not. A newly created session object is enabled.
   2.507 +** Refer to the documentation for [sqlite3session_changeset()] for further
   2.508 +** details regarding how enabling and disabling a session object affects
   2.509 +** the eventual changesets.
   2.510 +**
   2.511 +** Passing zero to this function disables the session. Passing a value
   2.512 +** greater than zero enables it. Passing a value less than zero is a 
   2.513 +** no-op, and may be used to query the current state of the session.
   2.514 +**
   2.515 +** The return value indicates the final state of the session object: 0 if 
   2.516 +** the session is disabled, or 1 if it is enabled.
   2.517 +*/
   2.518 +int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
   2.519 +
   2.520 +/*
   2.521 +** CAPI3REF: Set Or Clear the Indirect Change Flag
   2.522 +**
   2.523 +** Each change recorded by a session object is marked as either direct or
   2.524 +** indirect. A change is marked as indirect if either:
   2.525 +**
   2.526 +** <ul>
   2.527 +**   <li> The session object "indirect" flag is set when the change is
   2.528 +**        made, or
   2.529 +**   <li> The change is made by an SQL trigger or foreign key action 
   2.530 +**        instead of directly as a result of a users SQL statement.
   2.531 +** </ul>
   2.532 +**
   2.533 +** If a single row is affected by more than one operation within a session,
   2.534 +** then the change is considered indirect if all operations meet the criteria
   2.535 +** for an indirect change above, or direct otherwise.
   2.536 +**
   2.537 +** This function is used to set, clear or query the session object indirect
   2.538 +** flag.  If the second argument passed to this function is zero, then the
   2.539 +** indirect flag is cleared. If it is greater than zero, the indirect flag
   2.540 +** is set. Passing a value less than zero does not modify the current value
   2.541 +** of the indirect flag, and may be used to query the current state of the 
   2.542 +** indirect flag for the specified session object.
   2.543 +**
   2.544 +** The return value indicates the final state of the indirect flag: 0 if 
   2.545 +** it is clear, or 1 if it is set.
   2.546 +*/
   2.547 +int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
   2.548 +
   2.549 +/*
   2.550 +** CAPI3REF: Attach A Table To A Session Object
   2.551 +**
   2.552 +** If argument zTab is not NULL, then it is the name of a table to attach
   2.553 +** to the session object passed as the first argument. All subsequent changes 
   2.554 +** made to the table while the session object is enabled will be recorded. See 
   2.555 +** documentation for [sqlite3session_changeset()] for further details.
   2.556 +**
   2.557 +** Or, if argument zTab is NULL, then changes are recorded for all tables
   2.558 +** in the database. If additional tables are added to the database (by 
   2.559 +** executing "CREATE TABLE" statements) after this call is made, changes for 
   2.560 +** the new tables are also recorded.
   2.561 +**
   2.562 +** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
   2.563 +** defined as part of their CREATE TABLE statement. It does not matter if the 
   2.564 +** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
   2.565 +** KEY may consist of a single column, or may be a composite key.
   2.566 +** 
   2.567 +** It is not an error if the named table does not exist in the database. Nor
   2.568 +** is it an error if the named table does not have a PRIMARY KEY. However,
   2.569 +** no changes will be recorded in either of these scenarios.
   2.570 +**
   2.571 +** Changes are not recorded for individual rows that have NULL values stored
   2.572 +** in one or more of their PRIMARY KEY columns.
   2.573 +**
   2.574 +** SQLITE_OK is returned if the call completes without error. Or, if an error 
   2.575 +** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
   2.576 +*/
   2.577 +int sqlite3session_attach(
   2.578 +  sqlite3_session *pSession,      /* Session object */
   2.579 +  const char *zTab                /* Table name */
   2.580 +);
   2.581 +
   2.582 +/*
   2.583 +** CAPI3REF: Set a table filter on a Session Object.
   2.584 +**
   2.585 +** The second argument (xFilter) is the "filter callback". For changes to rows 
   2.586 +** in tables that are not attached to the Session oject, the filter is called
   2.587 +** to determine whether changes to the table's rows should be tracked or not. 
   2.588 +** If xFilter returns 0, changes is not tracked. Note that once a table is 
   2.589 +** attached, xFilter will not be called again.
   2.590 +*/
   2.591 +void sqlite3session_table_filter(
   2.592 +  sqlite3_session *pSession,      /* Session object */
   2.593 +  int(*xFilter)(
   2.594 +    void *pCtx,                   /* Copy of third arg to _filter_table() */
   2.595 +    const char *zTab              /* Table name */
   2.596 +  ),
   2.597 +  void *pCtx                      /* First argument passed to xFilter */
   2.598 +);
   2.599 +
   2.600 +/*
   2.601 +** CAPI3REF: Generate A Changeset From A Session Object
   2.602 +**
   2.603 +** Obtain a changeset containing changes to the tables attached to the 
   2.604 +** session object passed as the first argument. If successful, 
   2.605 +** set *ppChangeset to point to a buffer containing the changeset 
   2.606 +** and *pnChangeset to the size of the changeset in bytes before returning
   2.607 +** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
   2.608 +** zero and return an SQLite error code.
   2.609 +**
   2.610 +** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
   2.611 +** each representing a change to a single row of an attached table. An INSERT
   2.612 +** change contains the values of each field of a new database row. A DELETE
   2.613 +** contains the original values of each field of a deleted database row. An
   2.614 +** UPDATE change contains the original values of each field of an updated
   2.615 +** database row along with the updated values for each updated non-primary-key
   2.616 +** column. It is not possible for an UPDATE change to represent a change that
   2.617 +** modifies the values of primary key columns. If such a change is made, it
   2.618 +** is represented in a changeset as a DELETE followed by an INSERT.
   2.619 +**
   2.620 +** Changes are not recorded for rows that have NULL values stored in one or 
   2.621 +** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
   2.622 +** no corresponding change is present in the changesets returned by this
   2.623 +** function. If an existing row with one or more NULL values stored in
   2.624 +** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
   2.625 +** only an INSERT is appears in the changeset. Similarly, if an existing row
   2.626 +** with non-NULL PRIMARY KEY values is updated so that one or more of its
   2.627 +** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
   2.628 +** DELETE change only.
   2.629 +**
   2.630 +** The contents of a changeset may be traversed using an iterator created
   2.631 +** using the [sqlite3changeset_start()] API. A changeset may be applied to
   2.632 +** a database with a compatible schema using the [sqlite3changeset_apply()]
   2.633 +** API.
   2.634 +**
   2.635 +** Within a changeset generated by this function, all changes related to a
   2.636 +** single table are grouped together. In other words, when iterating through
   2.637 +** a changeset or when applying a changeset to a database, all changes related
   2.638 +** to a single table are processed before moving on to the next table. Tables
   2.639 +** are sorted in the same order in which they were attached (or auto-attached)
   2.640 +** to the sqlite3_session object. The order in which the changes related to
   2.641 +** a single table are stored is undefined.
   2.642 +**
   2.643 +** Following a successful call to this function, it is the responsibility of
   2.644 +** the caller to eventually free the buffer that *ppChangeset points to using
   2.645 +** [sqlite3_free()].
   2.646 +**
   2.647 +** <h3>Changeset Generation</h3>
   2.648 +**
   2.649 +** Once a table has been attached to a session object, the session object
   2.650 +** records the primary key values of all new rows inserted into the table.
   2.651 +** It also records the original primary key and other column values of any
   2.652 +** deleted or updated rows. For each unique primary key value, data is only
   2.653 +** recorded once - the first time a row with said primary key is inserted,
   2.654 +** updated or deleted in the lifetime of the session.
   2.655 +**
   2.656 +** There is one exception to the previous paragraph: when a row is inserted,
   2.657 +** updated or deleted, if one or more of its primary key columns contain a
   2.658 +** NULL value, no record of the change is made.
   2.659 +**
   2.660 +** The session object therefore accumulates two types of records - those
   2.661 +** that consist of primary key values only (created when the user inserts
   2.662 +** a new record) and those that consist of the primary key values and the
   2.663 +** original values of other table columns (created when the users deletes
   2.664 +** or updates a record).
   2.665 +**
   2.666 +** When this function is called, the requested changeset is created using
   2.667 +** both the accumulated records and the current contents of the database
   2.668 +** file. Specifically:
   2.669 +**
   2.670 +** <ul>
   2.671 +**   <li> For each record generated by an insert, the database is queried
   2.672 +**        for a row with a matching primary key. If one is found, an INSERT
   2.673 +**        change is added to the changeset. If no such row is found, no change 
   2.674 +**        is added to the changeset.
   2.675 +**
   2.676 +**   <li> For each record generated by an update or delete, the database is 
   2.677 +**        queried for a row with a matching primary key. If such a row is
   2.678 +**        found and one or more of the non-primary key fields have been
   2.679 +**        modified from their original values, an UPDATE change is added to 
   2.680 +**        the changeset. Or, if no such row is found in the table, a DELETE 
   2.681 +**        change is added to the changeset. If there is a row with a matching
   2.682 +**        primary key in the database, but all fields contain their original
   2.683 +**        values, no change is added to the changeset.
   2.684 +** </ul>
   2.685 +**
   2.686 +** This means, amongst other things, that if a row is inserted and then later
   2.687 +** deleted while a session object is active, neither the insert nor the delete
   2.688 +** will be present in the changeset. Or if a row is deleted and then later a 
   2.689 +** row with the same primary key values inserted while a session object is
   2.690 +** active, the resulting changeset will contain an UPDATE change instead of
   2.691 +** a DELETE and an INSERT.
   2.692 +**
   2.693 +** When a session object is disabled (see the [sqlite3session_enable()] API),
   2.694 +** it does not accumulate records when rows are inserted, updated or deleted.
   2.695 +** This may appear to have some counter-intuitive effects if a single row
   2.696 +** is written to more than once during a session. For example, if a row
   2.697 +** is inserted while a session object is enabled, then later deleted while 
   2.698 +** the same session object is disabled, no INSERT record will appear in the
   2.699 +** changeset, even though the delete took place while the session was disabled.
   2.700 +** Or, if one field of a row is updated while a session is disabled, and 
   2.701 +** another field of the same row is updated while the session is enabled, the
   2.702 +** resulting changeset will contain an UPDATE change that updates both fields.
   2.703 +*/
   2.704 +int sqlite3session_changeset(
   2.705 +  sqlite3_session *pSession,      /* Session object */
   2.706 +  int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
   2.707 +  void **ppChangeset              /* OUT: Buffer containing changeset */
   2.708 +);
   2.709 +
   2.710 +/*
   2.711 +** CAPI3REF: Load The Difference Between Tables Into A Session 
   2.712 +**
   2.713 +** If it is not already attached to the session object passed as the first
   2.714 +** argument, this function attaches table zTbl in the same manner as the
   2.715 +** [sqlite3session_attach()] function. If zTbl does not exist, or if it
   2.716 +** does not have a primary key, this function is a no-op (but does not return
   2.717 +** an error).
   2.718 +**
   2.719 +** Argument zFromDb must be the name of a database ("main", "temp" etc.)
   2.720 +** attached to the same database handle as the session object that contains 
   2.721 +** a table compatible with the table attached to the session by this function.
   2.722 +** A table is considered compatible if it:
   2.723 +**
   2.724 +** <ul>
   2.725 +**   <li> Has the same name,
   2.726 +**   <li> Has the same set of columns declared in the same order, and
   2.727 +**   <li> Has the same PRIMARY KEY definition.
   2.728 +** </ul>
   2.729 +**
   2.730 +** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
   2.731 +** are compatible but do not have any PRIMARY KEY columns, it is not an error
   2.732 +** but no changes are added to the session object. As with other session
   2.733 +** APIs, tables without PRIMARY KEYs are simply ignored.
   2.734 +**
   2.735 +** This function adds a set of changes to the session object that could be
   2.736 +** used to update the table in database zFrom (call this the "from-table") 
   2.737 +** so that its content is the same as the table attached to the session 
   2.738 +** object (call this the "to-table"). Specifically:
   2.739 +**
   2.740 +** <ul>
   2.741 +**   <li> For each row (primary key) that exists in the to-table but not in 
   2.742 +**     the from-table, an INSERT record is added to the session object.
   2.743 +**
   2.744 +**   <li> For each row (primary key) that exists in the to-table but not in 
   2.745 +**     the from-table, a DELETE record is added to the session object.
   2.746 +**
   2.747 +**   <li> For each row (primary key) that exists in both tables, but features 
   2.748 +**     different in each, an UPDATE record is added to the session.
   2.749 +** </ul>
   2.750 +**
   2.751 +** To clarify, if this function is called and then a changeset constructed
   2.752 +** using [sqlite3session_changeset()], then after applying that changeset to 
   2.753 +** database zFrom the contents of the two compatible tables would be 
   2.754 +** identical.
   2.755 +**
   2.756 +** It an error if database zFrom does not exist or does not contain the
   2.757 +** required compatible table.
   2.758 +**
   2.759 +** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
   2.760 +** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
   2.761 +** may be set to point to a buffer containing an English language error 
   2.762 +** message. It is the responsibility of the caller to free this buffer using
   2.763 +** sqlite3_free().
   2.764 +*/
   2.765 +int sqlite3session_diff(
   2.766 +  sqlite3_session *pSession,
   2.767 +  const char *zFromDb,
   2.768 +  const char *zTbl,
   2.769 +  char **pzErrMsg
   2.770 +);
   2.771 +
   2.772 +
   2.773 +/*
   2.774 +** CAPI3REF: Generate A Patchset From A Session Object
   2.775 +**
   2.776 +** The differences between a patchset and a changeset are that:
   2.777 +**
   2.778 +** <ul>
   2.779 +**   <li> DELETE records consist of the primary key fields only. The 
   2.780 +**        original values of other fields are omitted.
   2.781 +**   <li> The original values of any modified fields are omitted from 
   2.782 +**        UPDATE records.
   2.783 +** </ul>
   2.784 +**
   2.785 +** A patchset blob may be used with up to date versions of all 
   2.786 +** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(), 
   2.787 +** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
   2.788 +** attempting to use a patchset blob with old versions of the
   2.789 +** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error. 
   2.790 +**
   2.791 +** Because the non-primary key "old.*" fields are omitted, no 
   2.792 +** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
   2.793 +** is passed to the sqlite3changeset_apply() API. Other conflict types work
   2.794 +** in the same way as for changesets.
   2.795 +**
   2.796 +** Changes within a patchset are ordered in the same way as for changesets
   2.797 +** generated by the sqlite3session_changeset() function (i.e. all changes for
   2.798 +** a single table are grouped together, tables appear in the order in which
   2.799 +** they were attached to the session object).
   2.800 +*/
   2.801 +int sqlite3session_patchset(
   2.802 +  sqlite3_session *pSession,      /* Session object */
   2.803 +  int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
   2.804 +  void **ppPatchset               /* OUT: Buffer containing changeset */
   2.805 +);
   2.806 +
   2.807 +/*
   2.808 +** CAPI3REF: Test if a changeset has recorded any changes.
   2.809 +**
   2.810 +** Return non-zero if no changes to attached tables have been recorded by 
   2.811 +** the session object passed as the first argument. Otherwise, if one or 
   2.812 +** more changes have been recorded, return zero.
   2.813 +**
   2.814 +** Even if this function returns zero, it is possible that calling
   2.815 +** [sqlite3session_changeset()] on the session handle may still return a
   2.816 +** changeset that contains no changes. This can happen when a row in 
   2.817 +** an attached table is modified and then later on the original values 
   2.818 +** are restored. However, if this function returns non-zero, then it is
   2.819 +** guaranteed that a call to sqlite3session_changeset() will return a 
   2.820 +** changeset containing zero changes.
   2.821 +*/
   2.822 +int sqlite3session_isempty(sqlite3_session *pSession);
   2.823 +
   2.824 +/*
   2.825 +** CAPI3REF: Create An Iterator To Traverse A Changeset 
   2.826 +**
   2.827 +** Create an iterator used to iterate through the contents of a changeset.
   2.828 +** If successful, *pp is set to point to the iterator handle and SQLITE_OK
   2.829 +** is returned. Otherwise, if an error occurs, *pp is set to zero and an
   2.830 +** SQLite error code is returned.
   2.831 +**
   2.832 +** The following functions can be used to advance and query a changeset 
   2.833 +** iterator created by this function:
   2.834 +**
   2.835 +** <ul>
   2.836 +**   <li> [sqlite3changeset_next()]
   2.837 +**   <li> [sqlite3changeset_op()]
   2.838 +**   <li> [sqlite3changeset_new()]
   2.839 +**   <li> [sqlite3changeset_old()]
   2.840 +** </ul>
   2.841 +**
   2.842 +** It is the responsibility of the caller to eventually destroy the iterator
   2.843 +** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
   2.844 +** changeset (pChangeset) must remain valid until after the iterator is
   2.845 +** destroyed.
   2.846 +**
   2.847 +** Assuming the changeset blob was created by one of the
   2.848 +** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
   2.849 +** [sqlite3changeset_invert()] functions, all changes within the changeset 
   2.850 +** that apply to a single table are grouped together. This means that when 
   2.851 +** an application iterates through a changeset using an iterator created by 
   2.852 +** this function, all changes that relate to a single table are visted 
   2.853 +** consecutively. There is no chance that the iterator will visit a change 
   2.854 +** the applies to table X, then one for table Y, and then later on visit 
   2.855 +** another change for table X.
   2.856 +*/
   2.857 +int sqlite3changeset_start(
   2.858 +  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
   2.859 +  int nChangeset,                 /* Size of changeset blob in bytes */
   2.860 +  void *pChangeset                /* Pointer to blob containing changeset */
   2.861 +);
   2.862 +
   2.863 +
   2.864 +/*
   2.865 +** CAPI3REF: Advance A Changeset Iterator
   2.866 +**
   2.867 +** This function may only be used with iterators created by function
   2.868 +** [sqlite3changeset_start()]. If it is called on an iterator passed to
   2.869 +** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
   2.870 +** is returned and the call has no effect.
   2.871 +**
   2.872 +** Immediately after an iterator is created by sqlite3changeset_start(), it
   2.873 +** does not point to any change in the changeset. Assuming the changeset
   2.874 +** is not empty, the first call to this function advances the iterator to
   2.875 +** point to the first change in the changeset. Each subsequent call advances
   2.876 +** the iterator to point to the next change in the changeset (if any). If
   2.877 +** no error occurs and the iterator points to a valid change after a call
   2.878 +** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned. 
   2.879 +** Otherwise, if all changes in the changeset have already been visited,
   2.880 +** SQLITE_DONE is returned.
   2.881 +**
   2.882 +** If an error occurs, an SQLite error code is returned. Possible error 
   2.883 +** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
   2.884 +** SQLITE_NOMEM.
   2.885 +*/
   2.886 +int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
   2.887 +
   2.888 +/*
   2.889 +** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
   2.890 +**
   2.891 +** The pIter argument passed to this function may either be an iterator
   2.892 +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
   2.893 +** created by [sqlite3changeset_start()]. In the latter case, the most recent
   2.894 +** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
   2.895 +** is not the case, this function returns [SQLITE_MISUSE].
   2.896 +**
   2.897 +** If argument pzTab is not NULL, then *pzTab is set to point to a
   2.898 +** nul-terminated utf-8 encoded string containing the name of the table
   2.899 +** affected by the current change. The buffer remains valid until either
   2.900 +** sqlite3changeset_next() is called on the iterator or until the 
   2.901 +** conflict-handler function returns. If pnCol is not NULL, then *pnCol is 
   2.902 +** set to the number of columns in the table affected by the change. If
   2.903 +** pbIncorrect is not NULL, then *pbIndirect is set to true (1) if the change
   2.904 +** is an indirect change, or false (0) otherwise. See the documentation for
   2.905 +** [sqlite3session_indirect()] for a description of direct and indirect
   2.906 +** changes. Finally, if pOp is not NULL, then *pOp is set to one of 
   2.907 +** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the 
   2.908 +** type of change that the iterator currently points to.
   2.909 +**
   2.910 +** If no error occurs, SQLITE_OK is returned. If an error does occur, an
   2.911 +** SQLite error code is returned. The values of the output variables may not
   2.912 +** be trusted in this case.
   2.913 +*/
   2.914 +int sqlite3changeset_op(
   2.915 +  sqlite3_changeset_iter *pIter,  /* Iterator object */
   2.916 +  const char **pzTab,             /* OUT: Pointer to table name */
   2.917 +  int *pnCol,                     /* OUT: Number of columns in table */
   2.918 +  int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
   2.919 +  int *pbIndirect                 /* OUT: True for an 'indirect' change */
   2.920 +);
   2.921 +
   2.922 +/*
   2.923 +** CAPI3REF: Obtain The Primary Key Definition Of A Table
   2.924 +**
   2.925 +** For each modified table, a changeset includes the following:
   2.926 +**
   2.927 +** <ul>
   2.928 +**   <li> The number of columns in the table, and
   2.929 +**   <li> Which of those columns make up the tables PRIMARY KEY.
   2.930 +** </ul>
   2.931 +**
   2.932 +** This function is used to find which columns comprise the PRIMARY KEY of
   2.933 +** the table modified by the change that iterator pIter currently points to.
   2.934 +** If successful, *pabPK is set to point to an array of nCol entries, where
   2.935 +** nCol is the number of columns in the table. Elements of *pabPK are set to
   2.936 +** 0x01 if the corresponding column is part of the tables primary key, or
   2.937 +** 0x00 if it is not.
   2.938 +**
   2.939 +** If argumet pnCol is not NULL, then *pnCol is set to the number of columns
   2.940 +** in the table.
   2.941 +**
   2.942 +** If this function is called when the iterator does not point to a valid
   2.943 +** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
   2.944 +** SQLITE_OK is returned and the output variables populated as described
   2.945 +** above.
   2.946 +*/
   2.947 +int sqlite3changeset_pk(
   2.948 +  sqlite3_changeset_iter *pIter,  /* Iterator object */
   2.949 +  unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
   2.950 +  int *pnCol                      /* OUT: Number of entries in output array */
   2.951 +);
   2.952 +
   2.953 +/*
   2.954 +** CAPI3REF: Obtain old.* Values From A Changeset Iterator
   2.955 +**
   2.956 +** The pIter argument passed to this function may either be an iterator
   2.957 +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
   2.958 +** created by [sqlite3changeset_start()]. In the latter case, the most recent
   2.959 +** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
   2.960 +** Furthermore, it may only be called if the type of change that the iterator
   2.961 +** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
   2.962 +** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
   2.963 +**
   2.964 +** Argument iVal must be greater than or equal to 0, and less than the number
   2.965 +** of columns in the table affected by the current change. Otherwise,
   2.966 +** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
   2.967 +**
   2.968 +** If successful, this function sets *ppValue to point to a protected
   2.969 +** sqlite3_value object containing the iVal'th value from the vector of 
   2.970 +** original row values stored as part of the UPDATE or DELETE change and
   2.971 +** returns SQLITE_OK. The name of the function comes from the fact that this 
   2.972 +** is similar to the "old.*" columns available to update or delete triggers.
   2.973 +**
   2.974 +** If some other error occurs (e.g. an OOM condition), an SQLite error code
   2.975 +** is returned and *ppValue is set to NULL.
   2.976 +*/
   2.977 +int sqlite3changeset_old(
   2.978 +  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
   2.979 +  int iVal,                       /* Column number */
   2.980 +  sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
   2.981 +);
   2.982 +
   2.983 +/*
   2.984 +** CAPI3REF: Obtain new.* Values From A Changeset Iterator
   2.985 +**
   2.986 +** The pIter argument passed to this function may either be an iterator
   2.987 +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
   2.988 +** created by [sqlite3changeset_start()]. In the latter case, the most recent
   2.989 +** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
   2.990 +** Furthermore, it may only be called if the type of change that the iterator
   2.991 +** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
   2.992 +** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
   2.993 +**
   2.994 +** Argument iVal must be greater than or equal to 0, and less than the number
   2.995 +** of columns in the table affected by the current change. Otherwise,
   2.996 +** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
   2.997 +**
   2.998 +** If successful, this function sets *ppValue to point to a protected
   2.999 +** sqlite3_value object containing the iVal'th value from the vector of 
  2.1000 +** new row values stored as part of the UPDATE or INSERT change and
  2.1001 +** returns SQLITE_OK. If the change is an UPDATE and does not include
  2.1002 +** a new value for the requested column, *ppValue is set to NULL and 
  2.1003 +** SQLITE_OK returned. The name of the function comes from the fact that 
  2.1004 +** this is similar to the "new.*" columns available to update or delete 
  2.1005 +** triggers.
  2.1006 +**
  2.1007 +** If some other error occurs (e.g. an OOM condition), an SQLite error code
  2.1008 +** is returned and *ppValue is set to NULL.
  2.1009 +*/
  2.1010 +int sqlite3changeset_new(
  2.1011 +  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  2.1012 +  int iVal,                       /* Column number */
  2.1013 +  sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
  2.1014 +);
  2.1015 +
  2.1016 +/*
  2.1017 +** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
  2.1018 +**
  2.1019 +** This function should only be used with iterator objects passed to a
  2.1020 +** conflict-handler callback by [sqlite3changeset_apply()] with either
  2.1021 +** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
  2.1022 +** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
  2.1023 +** is set to NULL.
  2.1024 +**
  2.1025 +** Argument iVal must be greater than or equal to 0, and less than the number
  2.1026 +** of columns in the table affected by the current change. Otherwise,
  2.1027 +** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
  2.1028 +**
  2.1029 +** If successful, this function sets *ppValue to point to a protected
  2.1030 +** sqlite3_value object containing the iVal'th value from the 
  2.1031 +** "conflicting row" associated with the current conflict-handler callback
  2.1032 +** and returns SQLITE_OK.
  2.1033 +**
  2.1034 +** If some other error occurs (e.g. an OOM condition), an SQLite error code
  2.1035 +** is returned and *ppValue is set to NULL.
  2.1036 +*/
  2.1037 +int sqlite3changeset_conflict(
  2.1038 +  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  2.1039 +  int iVal,                       /* Column number */
  2.1040 +  sqlite3_value **ppValue         /* OUT: Value from conflicting row */
  2.1041 +);
  2.1042 +
  2.1043 +/*
  2.1044 +** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
  2.1045 +**
  2.1046 +** This function may only be called with an iterator passed to an
  2.1047 +** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
  2.1048 +** it sets the output variable to the total number of known foreign key
  2.1049 +** violations in the destination database and returns SQLITE_OK.
  2.1050 +**
  2.1051 +** In all other cases this function returns SQLITE_MISUSE.
  2.1052 +*/
  2.1053 +int sqlite3changeset_fk_conflicts(
  2.1054 +  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  2.1055 +  int *pnOut                      /* OUT: Number of FK violations */
  2.1056 +);
  2.1057 +
  2.1058 +
  2.1059 +/*
  2.1060 +** CAPI3REF: Finalize A Changeset Iterator
  2.1061 +**
  2.1062 +** This function is used to finalize an iterator allocated with
  2.1063 +** [sqlite3changeset_start()].
  2.1064 +**
  2.1065 +** This function should only be called on iterators created using the
  2.1066 +** [sqlite3changeset_start()] function. If an application calls this
  2.1067 +** function with an iterator passed to a conflict-handler by
  2.1068 +** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
  2.1069 +** call has no effect.
  2.1070 +**
  2.1071 +** If an error was encountered within a call to an sqlite3changeset_xxx()
  2.1072 +** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
  2.1073 +** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
  2.1074 +** to that error is returned by this function. Otherwise, SQLITE_OK is
  2.1075 +** returned. This is to allow the following pattern (pseudo-code):
  2.1076 +**
  2.1077 +**   sqlite3changeset_start();
  2.1078 +**   while( SQLITE_ROW==sqlite3changeset_next() ){
  2.1079 +**     // Do something with change.
  2.1080 +**   }
  2.1081 +**   rc = sqlite3changeset_finalize();
  2.1082 +**   if( rc!=SQLITE_OK ){
  2.1083 +**     // An error has occurred 
  2.1084 +**   }
  2.1085 +*/
  2.1086 +int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
  2.1087 +
  2.1088 +/*
  2.1089 +** CAPI3REF: Invert A Changeset
  2.1090 +**
  2.1091 +** This function is used to "invert" a changeset object. Applying an inverted
  2.1092 +** changeset to a database reverses the effects of applying the uninverted
  2.1093 +** changeset. Specifically:
  2.1094 +**
  2.1095 +** <ul>
  2.1096 +**   <li> Each DELETE change is changed to an INSERT, and
  2.1097 +**   <li> Each INSERT change is changed to a DELETE, and
  2.1098 +**   <li> For each UPDATE change, the old.* and new.* values are exchanged.
  2.1099 +** </ul>
  2.1100 +**
  2.1101 +** This function does not change the order in which changes appear within
  2.1102 +** the changeset. It merely reverses the sense of each individual change.
  2.1103 +**
  2.1104 +** If successful, a pointer to a buffer containing the inverted changeset
  2.1105 +** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
  2.1106 +** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
  2.1107 +** zeroed and an SQLite error code returned.
  2.1108 +**
  2.1109 +** It is the responsibility of the caller to eventually call sqlite3_free()
  2.1110 +** on the *ppOut pointer to free the buffer allocation following a successful 
  2.1111 +** call to this function.
  2.1112 +**
  2.1113 +** WARNING/TODO: This function currently assumes that the input is a valid
  2.1114 +** changeset. If it is not, the results are undefined.
  2.1115 +*/
  2.1116 +int sqlite3changeset_invert(
  2.1117 +  int nIn, const void *pIn,       /* Input changeset */
  2.1118 +  int *pnOut, void **ppOut        /* OUT: Inverse of input */
  2.1119 +);
  2.1120 +
  2.1121 +/*
  2.1122 +** CAPI3REF: Concatenate Two Changeset Objects
  2.1123 +**
  2.1124 +** This function is used to concatenate two changesets, A and B, into a 
  2.1125 +** single changeset. The result is a changeset equivalent to applying
  2.1126 +** changeset A followed by changeset B. 
  2.1127 +**
  2.1128 +** This function combines the two input changesets using an 
  2.1129 +** sqlite3_changegroup object. Calling it produces similar results as the
  2.1130 +** following code fragment:
  2.1131 +**
  2.1132 +**   sqlite3_changegroup *pGrp;
  2.1133 +**   rc = sqlite3_changegroup_new(&pGrp);
  2.1134 +**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
  2.1135 +**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
  2.1136 +**   if( rc==SQLITE_OK ){
  2.1137 +**     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
  2.1138 +**   }else{
  2.1139 +**     *ppOut = 0;
  2.1140 +**     *pnOut = 0;
  2.1141 +**   }
  2.1142 +**
  2.1143 +** Refer to the sqlite3_changegroup documentation below for details.
  2.1144 +*/
  2.1145 +int sqlite3changeset_concat(
  2.1146 +  int nA,                         /* Number of bytes in buffer pA */
  2.1147 +  void *pA,                       /* Pointer to buffer containing changeset A */
  2.1148 +  int nB,                         /* Number of bytes in buffer pB */
  2.1149 +  void *pB,                       /* Pointer to buffer containing changeset B */
  2.1150 +  int *pnOut,                     /* OUT: Number of bytes in output changeset */
  2.1151 +  void **ppOut                    /* OUT: Buffer containing output changeset */
  2.1152 +);
  2.1153 +
  2.1154 +
  2.1155 +/*
  2.1156 +** Changegroup handle.
  2.1157 +*/
  2.1158 +typedef struct sqlite3_changegroup sqlite3_changegroup;
  2.1159 +
  2.1160 +/*
  2.1161 +** CAPI3REF: Combine two or more changesets into a single changeset.
  2.1162 +**
  2.1163 +** An sqlite3_changegroup object is used to combine two or more changesets
  2.1164 +** (or patchsets) into a single changeset (or patchset). A single changegroup
  2.1165 +** object may combine changesets or patchsets, but not both. The output is
  2.1166 +** always in the same format as the input.
  2.1167 +**
  2.1168 +** If successful, this function returns SQLITE_OK and populates (*pp) with
  2.1169 +** a pointer to a new sqlite3_changegroup object before returning. The caller
  2.1170 +** should eventually free the returned object using a call to 
  2.1171 +** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
  2.1172 +** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
  2.1173 +**
  2.1174 +** The usual usage pattern for an sqlite3_changegroup object is as follows:
  2.1175 +**
  2.1176 +** <ul>
  2.1177 +**   <li> It is created using a call to sqlite3changegroup_new().
  2.1178 +**
  2.1179 +**   <li> Zero or more changesets (or patchsets) are added to the object
  2.1180 +**        by calling sqlite3changegroup_add().
  2.1181 +**
  2.1182 +**   <li> The result of combining all input changesets together is obtained 
  2.1183 +**        by the application via a call to sqlite3changegroup_output().
  2.1184 +**
  2.1185 +**   <li> The object is deleted using a call to sqlite3changegroup_delete().
  2.1186 +** </ul>
  2.1187 +**
  2.1188 +** Any number of calls to add() and output() may be made between the calls to
  2.1189 +** new() and delete(), and in any order.
  2.1190 +**
  2.1191 +** As well as the regular sqlite3changegroup_add() and 
  2.1192 +** sqlite3changegroup_output() functions, also available are the streaming
  2.1193 +** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
  2.1194 +*/
  2.1195 +int sqlite3changegroup_new(sqlite3_changegroup **pp);
  2.1196 +
  2.1197 +/*
  2.1198 +** Add all changes within the changeset (or patchset) in buffer pData (size
  2.1199 +** nData bytes) to the changegroup. 
  2.1200 +**
  2.1201 +** If the buffer contains a patchset, then all prior calls to this function
  2.1202 +** on the same changegroup object must also have specified patchsets. Or, if
  2.1203 +** the buffer contains a changeset, so must have the earlier calls to this
  2.1204 +** function. Otherwise, SQLITE_ERROR is returned and no changes are added
  2.1205 +** to the changegroup.
  2.1206 +**
  2.1207 +** Rows within the changeset and changegroup are identified by the values in
  2.1208 +** their PRIMARY KEY columns. A change in the changeset is considered to
  2.1209 +** apply to the same row as a change already present in the changegroup if
  2.1210 +** the two rows have the same primary key.
  2.1211 +**
  2.1212 +** Changes to rows that that do not already appear in the changegroup are
  2.1213 +** simply copied into it. Or, if both the new changeset and the changegroup
  2.1214 +** contain changes that apply to a single row, the final contents of the
  2.1215 +** changegroup depends on the type of each change, as follows:
  2.1216 +**
  2.1217 +** <table border=1 style="margin-left:8ex;margin-right:8ex">
  2.1218 +**   <tr><th style="white-space:pre">Existing Change  </th>
  2.1219 +**       <th style="white-space:pre">New Change       </th>
  2.1220 +**       <th>Output Change
  2.1221 +**   <tr><td>INSERT <td>INSERT <td>
  2.1222 +**       The new change is ignored. This case does not occur if the new
  2.1223 +**       changeset was recorded immediately after the changesets already
  2.1224 +**       added to the changegroup.
  2.1225 +**   <tr><td>INSERT <td>UPDATE <td>
  2.1226 +**       The INSERT change remains in the changegroup. The values in the 
  2.1227 +**       INSERT change are modified as if the row was inserted by the
  2.1228 +**       existing change and then updated according to the new change.
  2.1229 +**   <tr><td>INSERT <td>DELETE <td>
  2.1230 +**       The existing INSERT is removed from the changegroup. The DELETE is
  2.1231 +**       not added.
  2.1232 +**   <tr><td>UPDATE <td>INSERT <td>
  2.1233 +**       The new change is ignored. This case does not occur if the new
  2.1234 +**       changeset was recorded immediately after the changesets already
  2.1235 +**       added to the changegroup.
  2.1236 +**   <tr><td>UPDATE <td>UPDATE <td>
  2.1237 +**       The existing UPDATE remains within the changegroup. It is amended 
  2.1238 +**       so that the accompanying values are as if the row was updated once 
  2.1239 +**       by the existing change and then again by the new change.
  2.1240 +**   <tr><td>UPDATE <td>DELETE <td>
  2.1241 +**       The existing UPDATE is replaced by the new DELETE within the
  2.1242 +**       changegroup.
  2.1243 +**   <tr><td>DELETE <td>INSERT <td>
  2.1244 +**       If one or more of the column values in the row inserted by the
  2.1245 +**       new change differ from those in the row deleted by the existing 
  2.1246 +**       change, the existing DELETE is replaced by an UPDATE within the
  2.1247 +**       changegroup. Otherwise, if the inserted row is exactly the same 
  2.1248 +**       as the deleted row, the existing DELETE is simply discarded.
  2.1249 +**   <tr><td>DELETE <td>UPDATE <td>
  2.1250 +**       The new change is ignored. This case does not occur if the new
  2.1251 +**       changeset was recorded immediately after the changesets already
  2.1252 +**       added to the changegroup.
  2.1253 +**   <tr><td>DELETE <td>DELETE <td>
  2.1254 +**       The new change is ignored. This case does not occur if the new
  2.1255 +**       changeset was recorded immediately after the changesets already
  2.1256 +**       added to the changegroup.
  2.1257 +** </table>
  2.1258 +**
  2.1259 +** If the new changeset contains changes to a table that is already present
  2.1260 +** in the changegroup, then the number of columns and the position of the
  2.1261 +** primary key columns for the table must be consistent. If this is not the
  2.1262 +** case, this function fails with SQLITE_SCHEMA. If the input changeset
  2.1263 +** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
  2.1264 +** returned. Or, if an out-of-memory condition occurs during processing, this
  2.1265 +** function returns SQLITE_NOMEM. In all cases, if an error occurs the
  2.1266 +** final contents of the changegroup is undefined.
  2.1267 +**
  2.1268 +** If no error occurs, SQLITE_OK is returned.
  2.1269 +*/
  2.1270 +int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
  2.1271 +
  2.1272 +/*
  2.1273 +** Obtain a buffer containing a changeset (or patchset) representing the
  2.1274 +** current contents of the changegroup. If the inputs to the changegroup
  2.1275 +** were themselves changesets, the output is a changeset. Or, if the
  2.1276 +** inputs were patchsets, the output is also a patchset.
  2.1277 +**
  2.1278 +** As with the output of the sqlite3session_changeset() and
  2.1279 +** sqlite3session_patchset() functions, all changes related to a single
  2.1280 +** table are grouped together in the output of this function. Tables appear
  2.1281 +** in the same order as for the very first changeset added to the changegroup.
  2.1282 +** If the second or subsequent changesets added to the changegroup contain
  2.1283 +** changes for tables that do not appear in the first changeset, they are
  2.1284 +** appended onto the end of the output changeset, again in the order in
  2.1285 +** which they are first encountered.
  2.1286 +**
  2.1287 +** If an error occurs, an SQLite error code is returned and the output
  2.1288 +** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
  2.1289 +** is returned and the output variables are set to the size of and a 
  2.1290 +** pointer to the output buffer, respectively. In this case it is the
  2.1291 +** responsibility of the caller to eventually free the buffer using a
  2.1292 +** call to sqlite3_free().
  2.1293 +*/
  2.1294 +int sqlite3changegroup_output(
  2.1295 +  sqlite3_changegroup*,
  2.1296 +  int *pnData,                    /* OUT: Size of output buffer in bytes */
  2.1297 +  void **ppData                   /* OUT: Pointer to output buffer */
  2.1298 +);
  2.1299 +
  2.1300 +/*
  2.1301 +** Delete a changegroup object.
  2.1302 +*/
  2.1303 +void sqlite3changegroup_delete(sqlite3_changegroup*);
  2.1304 +
  2.1305 +/*
  2.1306 +** CAPI3REF: Apply A Changeset To A Database
  2.1307 +**
  2.1308 +** Apply a changeset to a database. This function attempts to update the
  2.1309 +** "main" database attached to handle db with the changes found in the
  2.1310 +** changeset passed via the second and third arguments.
  2.1311 +**
  2.1312 +** The fourth argument (xFilter) passed to this function is the "filter
  2.1313 +** callback". If it is not NULL, then for each table affected by at least one
  2.1314 +** change in the changeset, the filter callback is invoked with
  2.1315 +** the table name as the second argument, and a copy of the context pointer
  2.1316 +** passed as the sixth argument to this function as the first. If the "filter
  2.1317 +** callback" returns zero, then no attempt is made to apply any changes to 
  2.1318 +** the table. Otherwise, if the return value is non-zero or the xFilter
  2.1319 +** argument to this function is NULL, all changes related to the table are
  2.1320 +** attempted.
  2.1321 +**
  2.1322 +** For each table that is not excluded by the filter callback, this function 
  2.1323 +** tests that the target database contains a compatible table. A table is 
  2.1324 +** considered compatible if all of the following are true:
  2.1325 +**
  2.1326 +** <ul>
  2.1327 +**   <li> The table has the same name as the name recorded in the 
  2.1328 +**        changeset, and
  2.1329 +**   <li> The table has the same number of columns as recorded in the 
  2.1330 +**        changeset, and
  2.1331 +**   <li> The table has primary key columns in the same position as 
  2.1332 +**        recorded in the changeset.
  2.1333 +** </ul>
  2.1334 +**
  2.1335 +** If there is no compatible table, it is not an error, but none of the
  2.1336 +** changes associated with the table are applied. A warning message is issued
  2.1337 +** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
  2.1338 +** one such warning is issued for each table in the changeset.
  2.1339 +**
  2.1340 +** For each change for which there is a compatible table, an attempt is made 
  2.1341 +** to modify the table contents according to the UPDATE, INSERT or DELETE 
  2.1342 +** change. If a change cannot be applied cleanly, the conflict handler 
  2.1343 +** function passed as the fifth argument to sqlite3changeset_apply() may be 
  2.1344 +** invoked. A description of exactly when the conflict handler is invoked for 
  2.1345 +** each type of change is below.
  2.1346 +**
  2.1347 +** Unlike the xFilter argument, xConflict may not be passed NULL. The results
  2.1348 +** of passing anything other than a valid function pointer as the xConflict
  2.1349 +** argument are undefined.
  2.1350 +**
  2.1351 +** Each time the conflict handler function is invoked, it must return one
  2.1352 +** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or 
  2.1353 +** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
  2.1354 +** if the second argument passed to the conflict handler is either
  2.1355 +** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
  2.1356 +** returns an illegal value, any changes already made are rolled back and
  2.1357 +** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different 
  2.1358 +** actions are taken by sqlite3changeset_apply() depending on the value
  2.1359 +** returned by each invocation of the conflict-handler function. Refer to
  2.1360 +** the documentation for the three 
  2.1361 +** [SQLITE_CHANGESET_OMIT|available return values] for details.
  2.1362 +**
  2.1363 +** <dl>
  2.1364 +** <dt>DELETE Changes<dd>
  2.1365 +**   For each DELETE change, this function checks if the target database 
  2.1366 +**   contains a row with the same primary key value (or values) as the 
  2.1367 +**   original row values stored in the changeset. If it does, and the values 
  2.1368 +**   stored in all non-primary key columns also match the values stored in 
  2.1369 +**   the changeset the row is deleted from the target database.
  2.1370 +**
  2.1371 +**   If a row with matching primary key values is found, but one or more of
  2.1372 +**   the non-primary key fields contains a value different from the original
  2.1373 +**   row value stored in the changeset, the conflict-handler function is
  2.1374 +**   invoked with [SQLITE_CHANGESET_DATA] as the second argument.
  2.1375 +**
  2.1376 +**   If no row with matching primary key values is found in the database,
  2.1377 +**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  2.1378 +**   passed as the second argument.
  2.1379 +**
  2.1380 +**   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
  2.1381 +**   (which can only happen if a foreign key constraint is violated), the
  2.1382 +**   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
  2.1383 +**   passed as the second argument. This includes the case where the DELETE
  2.1384 +**   operation is attempted because an earlier call to the conflict handler
  2.1385 +**   function returned [SQLITE_CHANGESET_REPLACE].
  2.1386 +**
  2.1387 +** <dt>INSERT Changes<dd>
  2.1388 +**   For each INSERT change, an attempt is made to insert the new row into
  2.1389 +**   the database.
  2.1390 +**
  2.1391 +**   If the attempt to insert the row fails because the database already 
  2.1392 +**   contains a row with the same primary key values, the conflict handler
  2.1393 +**   function is invoked with the second argument set to 
  2.1394 +**   [SQLITE_CHANGESET_CONFLICT].
  2.1395 +**
  2.1396 +**   If the attempt to insert the row fails because of some other constraint
  2.1397 +**   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 
  2.1398 +**   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
  2.1399 +**   This includes the case where the INSERT operation is re-attempted because 
  2.1400 +**   an earlier call to the conflict handler function returned 
  2.1401 +**   [SQLITE_CHANGESET_REPLACE].
  2.1402 +**
  2.1403 +** <dt>UPDATE Changes<dd>
  2.1404 +**   For each UPDATE change, this function checks if the target database 
  2.1405 +**   contains a row with the same primary key value (or values) as the 
  2.1406 +**   original row values stored in the changeset. If it does, and the values 
  2.1407 +**   stored in all non-primary key columns also match the values stored in 
  2.1408 +**   the changeset the row is updated within the target database.
  2.1409 +**
  2.1410 +**   If a row with matching primary key values is found, but one or more of
  2.1411 +**   the non-primary key fields contains a value different from an original
  2.1412 +**   row value stored in the changeset, the conflict-handler function is
  2.1413 +**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
  2.1414 +**   UPDATE changes only contain values for non-primary key fields that are
  2.1415 +**   to be modified, only those fields need to match the original values to
  2.1416 +**   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
  2.1417 +**
  2.1418 +**   If no row with matching primary key values is found in the database,
  2.1419 +**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  2.1420 +**   passed as the second argument.
  2.1421 +**
  2.1422 +**   If the UPDATE operation is attempted, but SQLite returns 
  2.1423 +**   SQLITE_CONSTRAINT, the conflict-handler function is invoked with 
  2.1424 +**   [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
  2.1425 +**   This includes the case where the UPDATE operation is attempted after 
  2.1426 +**   an earlier call to the conflict handler function returned
  2.1427 +**   [SQLITE_CHANGESET_REPLACE].  
  2.1428 +** </dl>
  2.1429 +**
  2.1430 +** It is safe to execute SQL statements, including those that write to the
  2.1431 +** table that the callback related to, from within the xConflict callback.
  2.1432 +** This can be used to further customize the applications conflict
  2.1433 +** resolution strategy.
  2.1434 +**
  2.1435 +** All changes made by this function are enclosed in a savepoint transaction.
  2.1436 +** If any other error (aside from a constraint failure when attempting to
  2.1437 +** write to the target database) occurs, then the savepoint transaction is
  2.1438 +** rolled back, restoring the target database to its original state, and an 
  2.1439 +** SQLite error code returned.
  2.1440 +*/
  2.1441 +int sqlite3changeset_apply(
  2.1442 +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
  2.1443 +  int nChangeset,                 /* Size of changeset in bytes */
  2.1444 +  void *pChangeset,               /* Changeset blob */
  2.1445 +  int(*xFilter)(
  2.1446 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
  2.1447 +    const char *zTab              /* Table name */
  2.1448 +  ),
  2.1449 +  int(*xConflict)(
  2.1450 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
  2.1451 +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
  2.1452 +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
  2.1453 +  ),
  2.1454 +  void *pCtx                      /* First argument passed to xConflict */
  2.1455 +);
  2.1456 +
  2.1457 +/* 
  2.1458 +** CAPI3REF: Constants Passed To The Conflict Handler
  2.1459 +**
  2.1460 +** Values that may be passed as the second argument to a conflict-handler.
  2.1461 +**
  2.1462 +** <dl>
  2.1463 +** <dt>SQLITE_CHANGESET_DATA<dd>
  2.1464 +**   The conflict handler is invoked with CHANGESET_DATA as the second argument
  2.1465 +**   when processing a DELETE or UPDATE change if a row with the required
  2.1466 +**   PRIMARY KEY fields is present in the database, but one or more other 
  2.1467 +**   (non primary-key) fields modified by the update do not contain the 
  2.1468 +**   expected "before" values.
  2.1469 +** 
  2.1470 +**   The conflicting row, in this case, is the database row with the matching
  2.1471 +**   primary key.
  2.1472 +** 
  2.1473 +** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
  2.1474 +**   The conflict handler is invoked with CHANGESET_NOTFOUND as the second
  2.1475 +**   argument when processing a DELETE or UPDATE change if a row with the
  2.1476 +**   required PRIMARY KEY fields is not present in the database.
  2.1477 +** 
  2.1478 +**   There is no conflicting row in this case. The results of invoking the
  2.1479 +**   sqlite3changeset_conflict() API are undefined.
  2.1480 +** 
  2.1481 +** <dt>SQLITE_CHANGESET_CONFLICT<dd>
  2.1482 +**   CHANGESET_CONFLICT is passed as the second argument to the conflict
  2.1483 +**   handler while processing an INSERT change if the operation would result 
  2.1484 +**   in duplicate primary key values.
  2.1485 +** 
  2.1486 +**   The conflicting row in this case is the database row with the matching
  2.1487 +**   primary key.
  2.1488 +**
  2.1489 +** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
  2.1490 +**   If foreign key handling is enabled, and applying a changeset leaves the
  2.1491 +**   database in a state containing foreign key violations, the conflict 
  2.1492 +**   handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
  2.1493 +**   exactly once before the changeset is committed. If the conflict handler
  2.1494 +**   returns CHANGESET_OMIT, the changes, including those that caused the
  2.1495 +**   foreign key constraint violation, are committed. Or, if it returns
  2.1496 +**   CHANGESET_ABORT, the changeset is rolled back.
  2.1497 +**
  2.1498 +**   No current or conflicting row information is provided. The only function
  2.1499 +**   it is possible to call on the supplied sqlite3_changeset_iter handle
  2.1500 +**   is sqlite3changeset_fk_conflicts().
  2.1501 +** 
  2.1502 +** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
  2.1503 +**   If any other constraint violation occurs while applying a change (i.e. 
  2.1504 +**   a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is 
  2.1505 +**   invoked with CHANGESET_CONSTRAINT as the second argument.
  2.1506 +** 
  2.1507 +**   There is no conflicting row in this case. The results of invoking the
  2.1508 +**   sqlite3changeset_conflict() API are undefined.
  2.1509 +**
  2.1510 +** </dl>
  2.1511 +*/
  2.1512 +#define SQLITE_CHANGESET_DATA        1
  2.1513 +#define SQLITE_CHANGESET_NOTFOUND    2
  2.1514 +#define SQLITE_CHANGESET_CONFLICT    3
  2.1515 +#define SQLITE_CHANGESET_CONSTRAINT  4
  2.1516 +#define SQLITE_CHANGESET_FOREIGN_KEY 5
  2.1517 +
  2.1518 +/* 
  2.1519 +** CAPI3REF: Constants Returned By The Conflict Handler
  2.1520 +**
  2.1521 +** A conflict handler callback must return one of the following three values.
  2.1522 +**
  2.1523 +** <dl>
  2.1524 +** <dt>SQLITE_CHANGESET_OMIT<dd>
  2.1525 +**   If a conflict handler returns this value no special action is taken. The
  2.1526 +**   change that caused the conflict is not applied. The session module 
  2.1527 +**   continues to the next change in the changeset.
  2.1528 +**
  2.1529 +** <dt>SQLITE_CHANGESET_REPLACE<dd>
  2.1530 +**   This value may only be returned if the second argument to the conflict
  2.1531 +**   handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
  2.1532 +**   is not the case, any changes applied so far are rolled back and the 
  2.1533 +**   call to sqlite3changeset_apply() returns SQLITE_MISUSE.
  2.1534 +**
  2.1535 +**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
  2.1536 +**   handler, then the conflicting row is either updated or deleted, depending
  2.1537 +**   on the type of change.
  2.1538 +**
  2.1539 +**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
  2.1540 +**   handler, then the conflicting row is removed from the database and a
  2.1541 +**   second attempt to apply the change is made. If this second attempt fails,
  2.1542 +**   the original row is restored to the database before continuing.
  2.1543 +**
  2.1544 +** <dt>SQLITE_CHANGESET_ABORT<dd>
  2.1545 +**   If this value is returned, any changes applied so far are rolled back 
  2.1546 +**   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
  2.1547 +** </dl>
  2.1548 +*/
  2.1549 +#define SQLITE_CHANGESET_OMIT       0
  2.1550 +#define SQLITE_CHANGESET_REPLACE    1
  2.1551 +#define SQLITE_CHANGESET_ABORT      2
  2.1552 +
  2.1553 +/*
  2.1554 +** CAPI3REF: Streaming Versions of API functions.
  2.1555 +**
  2.1556 +** The six streaming API xxx_strm() functions serve similar purposes to the 
  2.1557 +** corresponding non-streaming API functions:
  2.1558 +**
  2.1559 +** <table border=1 style="margin-left:8ex;margin-right:8ex">
  2.1560 +**   <tr><th>Streaming function<th>Non-streaming equivalent</th>
  2.1561 +**   <tr><td>sqlite3changeset_apply_str<td>[sqlite3changeset_apply] 
  2.1562 +**   <tr><td>sqlite3changeset_concat_str<td>[sqlite3changeset_concat] 
  2.1563 +**   <tr><td>sqlite3changeset_invert_str<td>[sqlite3changeset_invert] 
  2.1564 +**   <tr><td>sqlite3changeset_start_str<td>[sqlite3changeset_start] 
  2.1565 +**   <tr><td>sqlite3session_changeset_str<td>[sqlite3session_changeset] 
  2.1566 +**   <tr><td>sqlite3session_patchset_str<td>[sqlite3session_patchset] 
  2.1567 +** </table>
  2.1568 +**
  2.1569 +** Non-streaming functions that accept changesets (or patchsets) as input
  2.1570 +** require that the entire changeset be stored in a single buffer in memory. 
  2.1571 +** Similarly, those that return a changeset or patchset do so by returning 
  2.1572 +** a pointer to a single large buffer allocated using sqlite3_malloc(). 
  2.1573 +** Normally this is convenient. However, if an application running in a 
  2.1574 +** low-memory environment is required to handle very large changesets, the
  2.1575 +** large contiguous memory allocations required can become onerous.
  2.1576 +**
  2.1577 +** In order to avoid this problem, instead of a single large buffer, input
  2.1578 +** is passed to a streaming API functions by way of a callback function that
  2.1579 +** the sessions module invokes to incrementally request input data as it is
  2.1580 +** required. In all cases, a pair of API function parameters such as
  2.1581 +**
  2.1582 +**  <pre>
  2.1583 +**  &nbsp;     int nChangeset,
  2.1584 +**  &nbsp;     void *pChangeset,
  2.1585 +**  </pre>
  2.1586 +**
  2.1587 +** Is replaced by:
  2.1588 +**
  2.1589 +**  <pre>
  2.1590 +**  &nbsp;     int (*xInput)(void *pIn, void *pData, int *pnData),
  2.1591 +**  &nbsp;     void *pIn,
  2.1592 +**  </pre>
  2.1593 +**
  2.1594 +** Each time the xInput callback is invoked by the sessions module, the first
  2.1595 +** argument passed is a copy of the supplied pIn context pointer. The second 
  2.1596 +** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no 
  2.1597 +** error occurs the xInput method should copy up to (*pnData) bytes of data 
  2.1598 +** into the buffer and set (*pnData) to the actual number of bytes copied 
  2.1599 +** before returning SQLITE_OK. If the input is completely exhausted, (*pnData) 
  2.1600 +** should be set to zero to indicate this. Or, if an error occurs, an SQLite 
  2.1601 +** error code should be returned. In all cases, if an xInput callback returns
  2.1602 +** an error, all processing is abandoned and the streaming API function
  2.1603 +** returns a copy of the error code to the caller.
  2.1604 +**
  2.1605 +** In the case of sqlite3changeset_start_strm(), the xInput callback may be
  2.1606 +** invoked by the sessions module at any point during the lifetime of the
  2.1607 +** iterator. If such an xInput callback returns an error, the iterator enters
  2.1608 +** an error state, whereby all subsequent calls to iterator functions 
  2.1609 +** immediately fail with the same error code as returned by xInput.
  2.1610 +**
  2.1611 +** Similarly, streaming API functions that return changesets (or patchsets)
  2.1612 +** return them in chunks by way of a callback function instead of via a
  2.1613 +** pointer to a single large buffer. In this case, a pair of parameters such
  2.1614 +** as:
  2.1615 +**
  2.1616 +**  <pre>
  2.1617 +**  &nbsp;     int *pnChangeset,
  2.1618 +**  &nbsp;     void **ppChangeset,
  2.1619 +**  </pre>
  2.1620 +**
  2.1621 +** Is replaced by:
  2.1622 +**
  2.1623 +**  <pre>
  2.1624 +**  &nbsp;     int (*xOutput)(void *pOut, const void *pData, int nData),
  2.1625 +**  &nbsp;     void *pOut
  2.1626 +**  </pre>
  2.1627 +**
  2.1628 +** The xOutput callback is invoked zero or more times to return data to
  2.1629 +** the application. The first parameter passed to each call is a copy of the
  2.1630 +** pOut pointer supplied by the application. The second parameter, pData,
  2.1631 +** points to a buffer nData bytes in size containing the chunk of output
  2.1632 +** data being returned. If the xOutput callback successfully processes the
  2.1633 +** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
  2.1634 +** it should return some other SQLite error code. In this case processing
  2.1635 +** is immediately abandoned and the streaming API function returns a copy
  2.1636 +** of the xOutput error code to the application.
  2.1637 +**
  2.1638 +** The sessions module never invokes an xOutput callback with the third 
  2.1639 +** parameter set to a value less than or equal to zero. Other than this,
  2.1640 +** no guarantees are made as to the size of the chunks of data returned.
  2.1641 +*/
  2.1642 +int sqlite3changeset_apply_strm(
  2.1643 +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
  2.1644 +  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
  2.1645 +  void *pIn,                                          /* First arg for xInput */
  2.1646 +  int(*xFilter)(
  2.1647 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
  2.1648 +    const char *zTab              /* Table name */
  2.1649 +  ),
  2.1650 +  int(*xConflict)(
  2.1651 +    void *pCtx,                   /* Copy of sixth arg to _apply() */
  2.1652 +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
  2.1653 +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
  2.1654 +  ),
  2.1655 +  void *pCtx                      /* First argument passed to xConflict */
  2.1656 +);
  2.1657 +int sqlite3changeset_concat_strm(
  2.1658 +  int (*xInputA)(void *pIn, void *pData, int *pnData),
  2.1659 +  void *pInA,
  2.1660 +  int (*xInputB)(void *pIn, void *pData, int *pnData),
  2.1661 +  void *pInB,
  2.1662 +  int (*xOutput)(void *pOut, const void *pData, int nData),
  2.1663 +  void *pOut
  2.1664 +);
  2.1665 +int sqlite3changeset_invert_strm(
  2.1666 +  int (*xInput)(void *pIn, void *pData, int *pnData),
  2.1667 +  void *pIn,
  2.1668 +  int (*xOutput)(void *pOut, const void *pData, int nData),
  2.1669 +  void *pOut
  2.1670 +);
  2.1671 +int sqlite3changeset_start_strm(
  2.1672 +  sqlite3_changeset_iter **pp,
  2.1673 +  int (*xInput)(void *pIn, void *pData, int *pnData),
  2.1674 +  void *pIn
  2.1675 +);
  2.1676 +int sqlite3session_changeset_strm(
  2.1677 +  sqlite3_session *pSession,
  2.1678 +  int (*xOutput)(void *pOut, const void *pData, int nData),
  2.1679 +  void *pOut
  2.1680 +);
  2.1681 +int sqlite3session_patchset_strm(
  2.1682 +  sqlite3_session *pSession,
  2.1683 +  int (*xOutput)(void *pOut, const void *pData, int nData),
  2.1684 +  void *pOut
  2.1685 +);
  2.1686 +int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
  2.1687 +    int (*xInput)(void *pIn, void *pData, int *pnData),
  2.1688 +    void *pIn
  2.1689 +);
  2.1690 +int sqlite3changegroup_output_strm(sqlite3_changegroup*,
  2.1691 +    int (*xOutput)(void *pOut, const void *pData, int nData), 
  2.1692 +    void *pOut
  2.1693 +);
  2.1694 +
  2.1695 +
  2.1696 +/*
  2.1697 +** Make sure we can call this stuff from C++.
  2.1698 +*/
  2.1699 +#if 0
  2.1700 +}
  2.1701 +#endif
  2.1702 +
  2.1703 +#endif  /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
  2.1704 +
  2.1705 +/******** End of sqlite3session.h *********/
  2.1706 +/******** Begin file fts5.h *********/
  2.1707  /*
  2.1708  ** 2014 May 31
  2.1709  **
  2.1710 @@ -8469,11 +10001,13 @@
  2.1711  **       ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
  2.1712  **
  2.1713  **   with $p set to a phrase equivalent to the phrase iPhrase of the
  2.1714 -**   current query is executed. For each row visited, the callback function
  2.1715 -**   passed as the fourth argument is invoked. The context and API objects 
  2.1716 -**   passed to the callback function may be used to access the properties of
  2.1717 -**   each matched row. Invoking Api.xUserData() returns a copy of the pointer
  2.1718 -**   passed as the third argument to pUserData.
  2.1719 +**   current query is executed. Any column filter that applies to
  2.1720 +**   phrase iPhrase of the current query is included in $p. For each 
  2.1721 +**   row visited, the callback function passed as the fourth argument 
  2.1722 +**   is invoked. The context and API objects passed to the callback 
  2.1723 +**   function may be used to access the properties of each matched row.
  2.1724 +**   Invoking Api.xUserData() returns a copy of the pointer passed as 
  2.1725 +**   the third argument to pUserData.
  2.1726  **
  2.1727  **   If the callback function returns any value other than SQLITE_OK, the
  2.1728  **   query is abandoned and the xQueryPhrase function returns immediately.
  2.1729 @@ -8903,6 +10437,7 @@
  2.1730  #endif /* _FTS5_H */
  2.1731  
  2.1732  
  2.1733 +/******** End of fts5.h *********/
  2.1734  
  2.1735  /************** End of sqlite3.h *********************************************/
  2.1736  /************** Continuing where we left off in sqliteInt.h ******************/
  2.1737 @@ -9020,13 +10555,13 @@
  2.1738  ** The suggested maximum number of in-memory pages to use for
  2.1739  ** the main database table and for temporary tables.
  2.1740  **
  2.1741 -** IMPLEMENTATION-OF: R-31093-59126 The default suggested cache size
  2.1742 -** is 2000 pages.
  2.1743 +** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
  2.1744 +** which means the cache size is limited to 2048000 bytes of memory.
  2.1745  ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
  2.1746  ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
  2.1747  */
  2.1748  #ifndef SQLITE_DEFAULT_CACHE_SIZE
  2.1749 -# define SQLITE_DEFAULT_CACHE_SIZE  2000
  2.1750 +# define SQLITE_DEFAULT_CACHE_SIZE  -2000
  2.1751  #endif
  2.1752  
  2.1753  /*
  2.1754 @@ -9039,8 +10574,9 @@
  2.1755  
  2.1756  /*
  2.1757  ** The maximum number of attached databases.  This must be between 0
  2.1758 -** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
  2.1759 -** is used internally to track attached databases.
  2.1760 +** and 125.  The upper bound of 125 is because the attached databases are
  2.1761 +** counted using a signed 8-bit integer which has a maximum value of 127
  2.1762 +** and we have to allow 2 extra counts for the "main" and "temp" databases.
  2.1763  */
  2.1764  #ifndef SQLITE_MAX_ATTACHED
  2.1765  # define SQLITE_MAX_ATTACHED 10
  2.1766 @@ -9075,7 +10611,7 @@
  2.1767  ** The default size of a database page.
  2.1768  */
  2.1769  #ifndef SQLITE_DEFAULT_PAGE_SIZE
  2.1770 -# define SQLITE_DEFAULT_PAGE_SIZE 1024
  2.1771 +# define SQLITE_DEFAULT_PAGE_SIZE 4096
  2.1772  #endif
  2.1773  #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
  2.1774  # undef SQLITE_DEFAULT_PAGE_SIZE
  2.1775 @@ -9156,7 +10692,7 @@
  2.1776  ** to the next, so we have developed the following set of #if statements
  2.1777  ** to generate appropriate macros for a wide range of compilers.
  2.1778  **
  2.1779 -** The correct "ANSI" way to do this is to use the intptr_t type. 
  2.1780 +** The correct "ANSI" way to do this is to use the intptr_t type.
  2.1781  ** Unfortunately, that typedef is not available on all compilers, or
  2.1782  ** if it is available, it requires an #include of specific headers
  2.1783  ** that vary from one machine to the next.
  2.1784 @@ -9181,21 +10717,6 @@
  2.1785  #endif
  2.1786  
  2.1787  /*
  2.1788 -** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
  2.1789 -** something between S (inclusive) and E (exclusive).
  2.1790 -**
  2.1791 -** In other words, S is a buffer and E is a pointer to the first byte after
  2.1792 -** the end of buffer S.  This macro returns true if P points to something
  2.1793 -** contained within the buffer S.
  2.1794 -*/
  2.1795 -#if defined(HAVE_STDINT_H)
  2.1796 -# define SQLITE_WITHIN(P,S,E) \
  2.1797 -    ((uintptr_t)(P)>=(uintptr_t)(S) && (uintptr_t)(P)<(uintptr_t)(E))
  2.1798 -#else
  2.1799 -# define SQLITE_WITHIN(P,S,E) ((P)>=(S) && (P)<(E))
  2.1800 -#endif
  2.1801 -
  2.1802 -/*
  2.1803  ** A macro to hint to the compiler that a function should not be
  2.1804  ** inlined.
  2.1805  */
  2.1806 @@ -9323,7 +10844,7 @@
  2.1807  ** is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
  2.1808  ** feature.
  2.1809  */
  2.1810 -#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
  2.1811 +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
  2.1812  # define NDEBUG 1
  2.1813  #endif
  2.1814  #if defined(NDEBUG) && defined(SQLITE_DEBUG)
  2.1815 @@ -9338,7 +10859,7 @@
  2.1816  #endif
  2.1817  
  2.1818  /*
  2.1819 -** The testcase() macro is used to aid in coverage testing.  When 
  2.1820 +** The testcase() macro is used to aid in coverage testing.  When
  2.1821  ** doing coverage testing, the condition inside the argument to
  2.1822  ** testcase() must be evaluated both true and false in order to
  2.1823  ** get full branch coverage.  The testcase() macro is inserted
  2.1824 @@ -9384,7 +10905,7 @@
  2.1825  #endif
  2.1826  
  2.1827  /*
  2.1828 -** The ALWAYS and NEVER macros surround boolean expressions which 
  2.1829 +** The ALWAYS and NEVER macros surround boolean expressions which
  2.1830  ** are intended to always be true or false, respectively.  Such
  2.1831  ** expressions could be omitted from the code completely.  But they
  2.1832  ** are included in a few cases in order to enhance the resilience
  2.1833 @@ -9398,7 +10919,7 @@
  2.1834  ** be true and false so that the unreachable code they specify will
  2.1835  ** not be counted as untested code.
  2.1836  */
  2.1837 -#if defined(SQLITE_COVERAGE_TEST)
  2.1838 +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
  2.1839  # define ALWAYS(X)      (1)
  2.1840  # define NEVER(X)       (0)
  2.1841  #elif !defined(NDEBUG)
  2.1842 @@ -9451,6 +10972,13 @@
  2.1843  #endif
  2.1844  
  2.1845  /*
  2.1846 +** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
  2.1847 +*/
  2.1848 +#ifdef SQLITE_OMIT_EXPLAIN
  2.1849 +# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
  2.1850 +#endif
  2.1851 +
  2.1852 +/*
  2.1853  ** Return true (non-zero) if the input is an integer that is too large
  2.1854  ** to fit in 32-bits.  This macro is used inside of various testcase()
  2.1855  ** macros to verify that we have tested SQLite for large-file support.
  2.1856 @@ -9596,76 +11124,76 @@
  2.1857  #define TK_AS                              24
  2.1858  #define TK_WITHOUT                         25
  2.1859  #define TK_COMMA                           26
  2.1860 -#define TK_ID                              27
  2.1861 -#define TK_INDEXED                         28
  2.1862 -#define TK_ABORT                           29
  2.1863 -#define TK_ACTION                          30
  2.1864 -#define TK_AFTER                           31
  2.1865 -#define TK_ANALYZE                         32
  2.1866 -#define TK_ASC                             33
  2.1867 -#define TK_ATTACH                          34
  2.1868 -#define TK_BEFORE                          35
  2.1869 -#define TK_BY                              36
  2.1870 -#define TK_CASCADE                         37
  2.1871 -#define TK_CAST                            38
  2.1872 -#define TK_COLUMNKW                        39
  2.1873 -#define TK_CONFLICT                        40
  2.1874 -#define TK_DATABASE                        41
  2.1875 -#define TK_DESC                            42
  2.1876 -#define TK_DETACH                          43
  2.1877 -#define TK_EACH                            44
  2.1878 -#define TK_FAIL                            45
  2.1879 -#define TK_FOR                             46
  2.1880 -#define TK_IGNORE                          47
  2.1881 -#define TK_INITIALLY                       48
  2.1882 -#define TK_INSTEAD                         49
  2.1883 -#define TK_LIKE_KW                         50
  2.1884 -#define TK_MATCH                           51
  2.1885 -#define TK_NO                              52
  2.1886 -#define TK_KEY                             53
  2.1887 -#define TK_OF                              54
  2.1888 -#define TK_OFFSET                          55
  2.1889 -#define TK_PRAGMA                          56
  2.1890 -#define TK_RAISE                           57
  2.1891 -#define TK_RECURSIVE                       58
  2.1892 -#define TK_REPLACE                         59
  2.1893 -#define TK_RESTRICT                        60
  2.1894 -#define TK_ROW                             61
  2.1895 -#define TK_TRIGGER                         62
  2.1896 -#define TK_VACUUM                          63
  2.1897 -#define TK_VIEW                            64
  2.1898 -#define TK_VIRTUAL                         65
  2.1899 -#define TK_WITH                            66
  2.1900 -#define TK_REINDEX                         67
  2.1901 -#define TK_RENAME                          68
  2.1902 -#define TK_CTIME_KW                        69
  2.1903 -#define TK_ANY                             70
  2.1904 -#define TK_OR                              71
  2.1905 -#define TK_AND                             72
  2.1906 -#define TK_IS                              73
  2.1907 -#define TK_BETWEEN                         74
  2.1908 -#define TK_IN                              75
  2.1909 -#define TK_ISNULL                          76
  2.1910 -#define TK_NOTNULL                         77
  2.1911 -#define TK_NE                              78
  2.1912 -#define TK_EQ                              79
  2.1913 -#define TK_GT                              80
  2.1914 -#define TK_LE                              81
  2.1915 -#define TK_LT                              82
  2.1916 -#define TK_GE                              83
  2.1917 -#define TK_ESCAPE                          84
  2.1918 -#define TK_BITAND                          85
  2.1919 -#define TK_BITOR                           86
  2.1920 -#define TK_LSHIFT                          87
  2.1921 -#define TK_RSHIFT                          88
  2.1922 -#define TK_PLUS                            89
  2.1923 -#define TK_MINUS                           90
  2.1924 -#define TK_STAR                            91
  2.1925 -#define TK_SLASH                           92
  2.1926 -#define TK_REM                             93
  2.1927 -#define TK_CONCAT                          94
  2.1928 -#define TK_COLLATE                         95
  2.1929 -#define TK_BITNOT                          96
  2.1930 +#define TK_OR                              27
  2.1931 +#define TK_AND                             28
  2.1932 +#define TK_IS                              29
  2.1933 +#define TK_MATCH                           30
  2.1934 +#define TK_LIKE_KW                         31
  2.1935 +#define TK_BETWEEN                         32
  2.1936 +#define TK_IN                              33
  2.1937 +#define TK_ISNULL                          34
  2.1938 +#define TK_NOTNULL                         35
  2.1939 +#define TK_NE                              36
  2.1940 +#define TK_EQ                              37
  2.1941 +#define TK_GT                              38
  2.1942 +#define TK_LE                              39
  2.1943 +#define TK_LT                              40
  2.1944 +#define TK_GE                              41
  2.1945 +#define TK_ESCAPE                          42
  2.1946 +#define TK_BITAND                          43
  2.1947 +#define TK_BITOR                           44
  2.1948 +#define TK_LSHIFT                          45
  2.1949 +#define TK_RSHIFT                          46
  2.1950 +#define TK_PLUS                            47
  2.1951 +#define TK_MINUS                           48
  2.1952 +#define TK_STAR                            49
  2.1953 +#define TK_SLASH                           50
  2.1954 +#define TK_REM                             51
  2.1955 +#define TK_CONCAT                          52
  2.1956 +#define TK_COLLATE                         53
  2.1957 +#define TK_BITNOT                          54
  2.1958 +#define TK_ID                              55
  2.1959 +#define TK_INDEXED                         56
  2.1960 +#define TK_ABORT                           57
  2.1961 +#define TK_ACTION                          58
  2.1962 +#define TK_AFTER                           59
  2.1963 +#define TK_ANALYZE                         60
  2.1964 +#define TK_ASC                             61
  2.1965 +#define TK_ATTACH                          62
  2.1966 +#define TK_BEFORE                          63
  2.1967 +#define TK_BY                              64
  2.1968 +#define TK_CASCADE                         65
  2.1969 +#define TK_CAST                            66
  2.1970 +#define TK_COLUMNKW                        67
  2.1971 +#define TK_CONFLICT                        68
  2.1972 +#define TK_DATABASE                        69
  2.1973 +#define TK_DESC                            70
  2.1974 +#define TK_DETACH                          71
  2.1975 +#define TK_EACH                            72
  2.1976 +#define TK_FAIL                            73
  2.1977 +#define TK_FOR                             74
  2.1978 +#define TK_IGNORE                          75
  2.1979 +#define TK_INITIALLY                       76
  2.1980 +#define TK_INSTEAD                         77
  2.1981 +#define TK_NO                              78
  2.1982 +#define TK_KEY                             79
  2.1983 +#define TK_OF                              80
  2.1984 +#define TK_OFFSET                          81
  2.1985 +#define TK_PRAGMA                          82
  2.1986 +#define TK_RAISE                           83
  2.1987 +#define TK_RECURSIVE                       84
  2.1988 +#define TK_REPLACE                         85
  2.1989 +#define TK_RESTRICT                        86
  2.1990 +#define TK_ROW                             87
  2.1991 +#define TK_TRIGGER                         88
  2.1992 +#define TK_VACUUM                          89
  2.1993 +#define TK_VIEW                            90
  2.1994 +#define TK_VIRTUAL                         91
  2.1995 +#define TK_WITH                            92
  2.1996 +#define TK_REINDEX                         93
  2.1997 +#define TK_RENAME                          94
  2.1998 +#define TK_CTIME_KW                        95
  2.1999 +#define TK_ANY                             96
  2.2000  #define TK_STRING                          97
  2.2001  #define TK_JOIN_KW                         98
  2.2002  #define TK_CONSTRAINT                      99
  2.2003 @@ -9728,8 +11256,9 @@
  2.2004  #define TK_UPLUS                          156
  2.2005  #define TK_REGISTER                       157
  2.2006  #define TK_ASTERISK                       158
  2.2007 -#define TK_SPACE                          159
  2.2008 -#define TK_ILLEGAL                        160
  2.2009 +#define TK_SPAN                           159
  2.2010 +#define TK_SPACE                          160
  2.2011 +#define TK_ILLEGAL                        161
  2.2012  
  2.2013  /* The token codes above must all fit in 8 bits */
  2.2014  #define TKFLG_MASK           0xff  
  2.2015 @@ -9768,7 +11297,7 @@
  2.2016  
  2.2017  /*
  2.2018  ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
  2.2019 -** afterward. Having this macro allows us to cause the C compiler 
  2.2020 +** afterward. Having this macro allows us to cause the C compiler
  2.2021  ** to omit code used by TEMP tables without messy #ifndef statements.
  2.2022  */
  2.2023  #ifdef SQLITE_OMIT_TEMPDB
  2.2024 @@ -9807,7 +11336,7 @@
  2.2025  
  2.2026  /*
  2.2027  ** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
  2.2028 -** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it 
  2.2029 +** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
  2.2030  ** to zero.
  2.2031  */
  2.2032  #if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
  2.2033 @@ -9846,8 +11375,12 @@
  2.2034  /*
  2.2035  ** Macros to compute minimum and maximum of two numbers.
  2.2036  */
  2.2037 -#define MIN(A,B) ((A)<(B)?(A):(B))
  2.2038 -#define MAX(A,B) ((A)>(B)?(A):(B))
  2.2039 +#ifndef MIN
  2.2040 +# define MIN(A,B) ((A)<(B)?(A):(B))
  2.2041 +#endif
  2.2042 +#ifndef MAX
  2.2043 +# define MAX(A,B) ((A)>(B)?(A):(B))
  2.2044 +#endif
  2.2045  
  2.2046  /*
  2.2047  ** Swap two objects of type TYPE.
  2.2048 @@ -9955,7 +11488,7 @@
  2.2049  **      4 -> 20           1000 -> 99        1048576 -> 200
  2.2050  **     10 -> 33           1024 -> 100    4294967296 -> 320
  2.2051  **
  2.2052 -** The LogEst can be negative to indicate fractional values. 
  2.2053 +** The LogEst can be negative to indicate fractional values.
  2.2054  ** Examples:
  2.2055  **
  2.2056  **    0.5 -> -10           0.1 -> -33        0.0625 -> -40
  2.2057 @@ -9976,6 +11509,27 @@
  2.2058  # endif
  2.2059  #endif
  2.2060  
  2.2061 +/* The uptr type is an unsigned integer large enough to hold a pointer
  2.2062 +*/
  2.2063 +#if defined(HAVE_STDINT_H)
  2.2064 +  typedef uintptr_t uptr;
  2.2065 +#elif SQLITE_PTRSIZE==4
  2.2066 +  typedef u32 uptr;
  2.2067 +#else
  2.2068 +  typedef u64 uptr;
  2.2069 +#endif
  2.2070 +
  2.2071 +/*
  2.2072 +** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
  2.2073 +** something between S (inclusive) and E (exclusive).
  2.2074 +**
  2.2075 +** In other words, S is a buffer and E is a pointer to the first byte after
  2.2076 +** the end of buffer S.  This macro returns true if P points to something
  2.2077 +** contained within the buffer S.
  2.2078 +*/
  2.2079 +#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
  2.2080 +
  2.2081 +
  2.2082  /*
  2.2083  ** Macros to determine whether the machine is big or little endian,
  2.2084  ** and whether or not that determination is run-time or compile-time.
  2.2085 @@ -10021,7 +11575,7 @@
  2.2086  #define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
  2.2087  #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
  2.2088  
  2.2089 -/* 
  2.2090 +/*
  2.2091  ** Round up a number to the next larger multiple of 8.  This is used
  2.2092  ** to force 8-byte alignment on 64-bit architectures.
  2.2093  */
  2.2094 @@ -10115,7 +11669,7 @@
  2.2095  
  2.2096  /*
  2.2097  ** An instance of the following structure is used to store the busy-handler
  2.2098 -** callback for a given sqlite handle. 
  2.2099 +** callback for a given sqlite handle.
  2.2100  **
  2.2101  ** The sqlite.busyHandler member of the sqlite struct contains the busy
  2.2102  ** callback for the database handle. Each pager opened via the sqlite
  2.2103 @@ -10160,9 +11714,9 @@
  2.2104  
  2.2105  /*
  2.2106  ** The following value as a destructor means to use sqlite3DbFree().
  2.2107 -** The sqlite3DbFree() routine requires two parameters instead of the 
  2.2108 -** one parameter that destructors normally want.  So we have to introduce 
  2.2109 -** this magic value that the code knows to handle differently.  Any 
  2.2110 +** The sqlite3DbFree() routine requires two parameters instead of the
  2.2111 +** one parameter that destructors normally want.  So we have to introduce
  2.2112 +** this magic value that the code knows to handle differently.  Any
  2.2113  ** pointer will work here as long as it is distinct from SQLITE_STATIC
  2.2114  ** and SQLITE_TRANSIENT.
  2.2115  */
  2.2116 @@ -10189,16 +11743,16 @@
  2.2117  SQLITE_API int SQLITE_STDCALL sqlite3_wsd_init(int N, int J);
  2.2118  SQLITE_API void *SQLITE_STDCALL sqlite3_wsd_find(void *K, int L);
  2.2119  #else
  2.2120 -  #define SQLITE_WSD 
  2.2121 +  #define SQLITE_WSD
  2.2122    #define GLOBAL(t,v) v
  2.2123    #define sqlite3GlobalConfig sqlite3Config
  2.2124  #endif
  2.2125  
  2.2126  /*
  2.2127  ** The following macros are used to suppress compiler warnings and to
  2.2128 -** make it clear to human readers when a function parameter is deliberately 
  2.2129 +** make it clear to human readers when a function parameter is deliberately
  2.2130  ** left unused within the body of a function. This usually happens when
  2.2131 -** a function is called via a function pointer. For example the 
  2.2132 +** a function is called via a function pointer. For example the
  2.2133  ** implementation of an SQL aggregate step callback may not use the
  2.2134  ** parameter indicating the number of arguments passed to the aggregate,
  2.2135  ** if it knows that this is enforced elsewhere.
  2.2136 @@ -10241,6 +11795,7 @@
  2.2137  typedef struct Module Module;
  2.2138  typedef struct NameContext NameContext;
  2.2139  typedef struct Parse Parse;
  2.2140 +typedef struct PreUpdate PreUpdate;
  2.2141  typedef struct PrintfArguments PrintfArguments;
  2.2142  typedef struct RowSet RowSet;
  2.2143  typedef struct Savepoint Savepoint;
  2.2144 @@ -10264,7 +11819,7 @@
  2.2145  typedef struct With With;
  2.2146  
  2.2147  /*
  2.2148 -** Defer sourcing vdbe.h and btree.h until after the "u8" and 
  2.2149 +** Defer sourcing vdbe.h and btree.h until after the "u8" and
  2.2150  ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
  2.2151  ** pointer types (i.e. FuncDef) defined above.
  2.2152  */
  2.2153 @@ -10340,7 +11895,6 @@
  2.2154  SQLITE_PRIVATE   int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
  2.2155  #endif
  2.2156  SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
  2.2157 -SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
  2.2158  SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  2.2159  SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
  2.2160  SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
  2.2161 @@ -10654,7 +12208,7 @@
  2.2162  struct VdbeOp {
  2.2163    u8 opcode;          /* What operation to perform */
  2.2164    signed char p4type; /* One of the P4_xxx constants for p4 */
  2.2165 -  u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
  2.2166 +  u8 notUsed1;
  2.2167    u8 p5;              /* Fifth parameter is an unsigned character */
  2.2168    int p1;             /* First operand */
  2.2169    int p2;             /* Second parameter (often the jump destination) */
  2.2170 @@ -10673,6 +12227,7 @@
  2.2171      KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
  2.2172      int *ai;               /* Used when p4type is P4_INTARRAY */
  2.2173      SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
  2.2174 +    Table *pTab;           /* Used when p4type is P4_TABLE */
  2.2175  #ifdef SQLITE_ENABLE_CURSOR_HINTS
  2.2176      Expr *pExpr;           /* Used when p4type is P4_EXPR */
  2.2177  #endif
  2.2178 @@ -10737,7 +12292,8 @@
  2.2179  #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
  2.2180  #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
  2.2181  #define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
  2.2182 -#define P4_FUNCCTX  (-20) /* P4 is a pointer to an sqlite3_context object */
  2.2183 +#define P4_TABLE    (-20) /* P4 is a pointer to a Table structure */
  2.2184 +#define P4_FUNCCTX  (-21) /* P4 is a pointer to an sqlite3_context object */
  2.2185  
  2.2186  /* Error message codes for OP_Halt */
  2.2187  #define P5_ConstraintNotNull 1
  2.2188 @@ -10795,153 +12351,152 @@
  2.2189  #define OP_VUpdate        12 /* synopsis: data=r[P3@P2]                    */
  2.2190  #define OP_Goto           13
  2.2191  #define OP_Gosub          14
  2.2192 -#define OP_Return         15
  2.2193 -#define OP_InitCoroutine  16
  2.2194 -#define OP_EndCoroutine   17
  2.2195 -#define OP_Yield          18
  2.2196 +#define OP_InitCoroutine  15
  2.2197 +#define OP_Yield          16
  2.2198 +#define OP_MustBeInt      17
  2.2199 +#define OP_Jump           18
  2.2200  #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
  2.2201 -#define OP_HaltIfNull     20 /* synopsis: if r[P3]=null halt               */
  2.2202 -#define OP_Halt           21
  2.2203 -#define OP_Integer        22 /* synopsis: r[P2]=P1                         */
  2.2204 -#define OP_Int64          23 /* synopsis: r[P2]=P4                         */
  2.2205 -#define OP_String         24 /* synopsis: r[P2]='P4' (len=P1)              */
  2.2206 -#define OP_Null           25 /* synopsis: r[P2..P3]=NULL                   */
  2.2207 -#define OP_SoftNull       26 /* synopsis: r[P1]=NULL                       */
  2.2208 -#define OP_Blob           27 /* synopsis: r[P2]=P4 (len=P1)                */
  2.2209 -#define OP_Variable       28 /* synopsis: r[P2]=parameter(P1,P4)           */
  2.2210 -#define OP_Move           29 /* synopsis: r[P2@P3]=r[P1@P3]                */
  2.2211 -#define OP_Copy           30 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
  2.2212 -#define OP_SCopy          31 /* synopsis: r[P2]=r[P1]                      */
  2.2213 -#define OP_IntCopy        32 /* synopsis: r[P2]=r[P1]                      */
  2.2214 -#define OP_ResultRow      33 /* synopsis: output=r[P1@P2]                  */
  2.2215 -#define OP_CollSeq        34
  2.2216 -#define OP_Function0      35 /* synopsis: r[P3]=func(r[P2@P5])             */
  2.2217 -#define OP_Function       36 /* synopsis: r[P3]=func(r[P2@P5])             */
  2.2218 -#define OP_AddImm         37 /* synopsis: r[P1]=r[P1]+P2                   */
  2.2219 -#define OP_MustBeInt      38
  2.2220 -#define OP_RealAffinity   39
  2.2221 -#define OP_Cast           40 /* synopsis: affinity(r[P1])                  */
  2.2222 -#define OP_Permutation    41
  2.2223 -#define OP_Compare        42 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
  2.2224 -#define OP_Jump           43
  2.2225 -#define OP_Once           44
  2.2226 -#define OP_If             45
  2.2227 -#define OP_IfNot          46
  2.2228 -#define OP_Column         47 /* synopsis: r[P3]=PX                         */
  2.2229 -#define OP_Affinity       48 /* synopsis: affinity(r[P1@P2])               */
  2.2230 -#define OP_MakeRecord     49 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
  2.2231 -#define OP_Count          50 /* synopsis: r[P2]=count()                    */
  2.2232 -#define OP_ReadCookie     51
  2.2233 -#define OP_SetCookie      52
  2.2234 -#define OP_ReopenIdx      53 /* synopsis: root=P2 iDb=P3                   */
  2.2235 -#define OP_OpenRead       54 /* synopsis: root=P2 iDb=P3                   */
  2.2236 -#define OP_OpenWrite      55 /* synopsis: root=P2 iDb=P3                   */
  2.2237 -#define OP_OpenAutoindex  56 /* synopsis: nColumn=P2                       */
  2.2238 -#define OP_OpenEphemeral  57 /* synopsis: nColumn=P2                       */
  2.2239 -#define OP_SorterOpen     58
  2.2240 -#define OP_SequenceTest   59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
  2.2241 -#define OP_OpenPseudo     60 /* synopsis: P3 columns in r[P2]              */
  2.2242 -#define OP_Close          61
  2.2243 -#define OP_ColumnsUsed    62
  2.2244 -#define OP_SeekLT         63 /* synopsis: key=r[P3@P4]                     */
  2.2245 -#define OP_SeekLE         64 /* synopsis: key=r[P3@P4]                     */
  2.2246 -#define OP_SeekGE         65 /* synopsis: key=r[P3@P4]                     */
  2.2247 -#define OP_SeekGT         66 /* synopsis: key=r[P3@P4]                     */
  2.2248 -#define OP_NoConflict     67 /* synopsis: key=r[P3@P4]                     */
  2.2249 -#define OP_NotFound       68 /* synopsis: key=r[P3@P4]                     */
  2.2250 -#define OP_Found          69 /* synopsis: key=r[P3@P4]                     */
  2.2251 -#define OP_NotExists      70 /* synopsis: intkey=r[P3]                     */
  2.2252 -#define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  2.2253 -#define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  2.2254 -#define OP_Sequence       73 /* synopsis: r[P2]=cursor[P1].ctr++           */
  2.2255 -#define OP_NewRowid       74 /* synopsis: r[P2]=rowid                      */
  2.2256 -#define OP_Insert         75 /* synopsis: intkey=r[P3] data=r[P2]          */
  2.2257 -#define OP_IsNull         76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
  2.2258 -#define OP_NotNull        77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
  2.2259 -#define OP_Ne             78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
  2.2260 -#define OP_Eq             79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
  2.2261 -#define OP_Gt             80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
  2.2262 -#define OP_Le             81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
  2.2263 -#define OP_Lt             82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
  2.2264 -#define OP_Ge             83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
  2.2265 -#define OP_InsertInt      84 /* synopsis: intkey=P3 data=r[P2]             */
  2.2266 -#define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
  2.2267 -#define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
  2.2268 -#define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
  2.2269 -#define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
  2.2270 -#define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
  2.2271 -#define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
  2.2272 -#define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
  2.2273 -#define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
  2.2274 -#define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
  2.2275 -#define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
  2.2276 -#define OP_Delete         95
  2.2277 -#define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
  2.2278 +#define OP_Once           20
  2.2279 +#define OP_If             21
  2.2280 +#define OP_IfNot          22
  2.2281 +#define OP_SeekLT         23 /* synopsis: key=r[P3@P4]                     */
  2.2282 +#define OP_SeekLE         24 /* synopsis: key=r[P3@P4]                     */
  2.2283 +#define OP_SeekGE         25 /* synopsis: key=r[P3@P4]                     */
  2.2284 +#define OP_SeekGT         26 /* synopsis: key=r[P3@P4]                     */
  2.2285 +#define OP_Or             27 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  2.2286 +#define OP_And            28 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  2.2287 +#define OP_NoConflict     29 /* synopsis: key=r[P3@P4]                     */
  2.2288 +#define OP_NotFound       30 /* synopsis: key=r[P3@P4]                     */
  2.2289 +#define OP_Found          31 /* synopsis: key=r[P3@P4]                     */
  2.2290 +#define OP_NotExists      32 /* synopsis: intkey=r[P3]                     */
  2.2291 +#define OP_Last           33
  2.2292 +#define OP_IsNull         34 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
  2.2293 +#define OP_NotNull        35 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
  2.2294 +#define OP_Ne             36 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
  2.2295 +#define OP_Eq             37 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
  2.2296 +#define OP_Gt             38 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
  2.2297 +#define OP_Le             39 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
  2.2298 +#define OP_Lt             40 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
  2.2299 +#define OP_Ge             41 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
  2.2300 +#define OP_SorterSort     42
  2.2301 +#define OP_BitAnd         43 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
  2.2302 +#define OP_BitOr          44 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
  2.2303 +#define OP_ShiftLeft      45 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
  2.2304 +#define OP_ShiftRight     46 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
  2.2305 +#define OP_Add            47 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
  2.2306 +#define OP_Subtract       48 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
  2.2307 +#define OP_Multiply       49 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
  2.2308 +#define OP_Divide         50 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
  2.2309 +#define OP_Remainder      51 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
  2.2310 +#define OP_Concat         52 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
  2.2311 +#define OP_Sort           53
  2.2312 +#define OP_BitNot         54 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
  2.2313 +#define OP_Rewind         55
  2.2314 +#define OP_IdxLE          56 /* synopsis: key=r[P3@P4]                     */
  2.2315 +#define OP_IdxGT          57 /* synopsis: key=r[P3@P4]                     */
  2.2316 +#define OP_IdxLT          58 /* synopsis: key=r[P3@P4]                     */
  2.2317 +#define OP_IdxGE          59 /* synopsis: key=r[P3@P4]                     */
  2.2318 +#define OP_RowSetRead     60 /* synopsis: r[P3]=rowset(P1)                 */
  2.2319 +#define OP_RowSetTest     61 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  2.2320 +#define OP_Program        62
  2.2321 +#define OP_FkIfZero       63 /* synopsis: if fkctr[P1]==0 goto P2          */
  2.2322 +#define OP_IfPos          64 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
  2.2323 +#define OP_IfNotZero      65 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
  2.2324 +#define OP_DecrJumpZero   66 /* synopsis: if (--r[P1])==0 goto P2          */
  2.2325 +#define OP_IncrVacuum     67
  2.2326 +#define OP_VNext          68
  2.2327 +#define OP_Init           69 /* synopsis: Start at P2                      */
  2.2328 +#define OP_Return         70
  2.2329 +#define OP_EndCoroutine   71
  2.2330 +#define OP_HaltIfNull     72 /* synopsis: if r[P3]=null halt               */
  2.2331 +#define OP_Halt           73
  2.2332 +#define OP_Integer        74 /* synopsis: r[P2]=P1                         */
  2.2333 +#define OP_Int64          75 /* synopsis: r[P2]=P4                         */
  2.2334 +#define OP_String         76 /* synopsis: r[P2]='P4' (len=P1)              */
  2.2335 +#define OP_Null           77 /* synopsis: r[P2..P3]=NULL                   */
  2.2336 +#define OP_SoftNull       78 /* synopsis: r[P1]=NULL                       */
  2.2337 +#define OP_Blob           79 /* synopsis: r[P2]=P4 (len=P1)                */
  2.2338 +#define OP_Variable       80 /* synopsis: r[P2]=parameter(P1,P4)           */
  2.2339 +#define OP_Move           81 /* synopsis: r[P2@P3]=r[P1@P3]                */
  2.2340 +#define OP_Copy           82 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
  2.2341 +#define OP_SCopy          83 /* synopsis: r[P2]=r[P1]                      */
  2.2342 +#define OP_IntCopy        84 /* synopsis: r[P2]=r[P1]                      */
  2.2343 +#define OP_ResultRow      85 /* synopsis: output=r[P1@P2]                  */
  2.2344 +#define OP_CollSeq        86
  2.2345 +#define OP_Function0      87 /* synopsis: r[P3]=func(r[P2@P5])             */
  2.2346 +#define OP_Function       88 /* synopsis: r[P3]=func(r[P2@P5])             */
  2.2347 +#define OP_AddImm         89 /* synopsis: r[P1]=r[P1]+P2                   */
  2.2348 +#define OP_RealAffinity   90
  2.2349 +#define OP_Cast           91 /* synopsis: affinity(r[P1])                  */
  2.2350 +#define OP_Permutation    92
  2.2351 +#define OP_Compare        93 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
  2.2352 +#define OP_Column         94 /* synopsis: r[P3]=PX                         */
  2.2353 +#define OP_Affinity       95 /* synopsis: affinity(r[P1@P2])               */
  2.2354 +#define OP_MakeRecord     96 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
  2.2355  #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
  2.2356 -#define OP_ResetCount     98
  2.2357 -#define OP_SorterCompare  99 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
  2.2358 -#define OP_SorterData    100 /* synopsis: r[P2]=data                       */
  2.2359 -#define OP_RowKey        101 /* synopsis: r[P2]=key                        */
  2.2360 -#define OP_RowData       102 /* synopsis: r[P2]=data                       */
  2.2361 -#define OP_Rowid         103 /* synopsis: r[P2]=rowid                      */
  2.2362 -#define OP_NullRow       104
  2.2363 -#define OP_Last          105
  2.2364 -#define OP_SorterSort    106
  2.2365 -#define OP_Sort          107
  2.2366 -#define OP_Rewind        108
  2.2367 -#define OP_SorterInsert  109
  2.2368 -#define OP_IdxInsert     110 /* synopsis: key=r[P2]                        */
  2.2369 -#define OP_IdxDelete     111 /* synopsis: key=r[P2@P3]                     */
  2.2370 -#define OP_Seek          112 /* synopsis: Move P3 to P1.rowid              */
  2.2371 -#define OP_IdxRowid      113 /* synopsis: r[P2]=rowid                      */
  2.2372 -#define OP_IdxLE         114 /* synopsis: key=r[P3@P4]                     */
  2.2373 -#define OP_IdxGT         115 /* synopsis: key=r[P3@P4]                     */
  2.2374 -#define OP_IdxLT         116 /* synopsis: key=r[P3@P4]                     */
  2.2375 -#define OP_IdxGE         117 /* synopsis: key=r[P3@P4]                     */
  2.2376 -#define OP_Destroy       118
  2.2377 -#define OP_Clear         119
  2.2378 -#define OP_ResetSorter   120
  2.2379 -#define OP_CreateIndex   121 /* synopsis: r[P2]=root iDb=P1                */
  2.2380 -#define OP_CreateTable   122 /* synopsis: r[P2]=root iDb=P1                */
  2.2381 -#define OP_ParseSchema   123
  2.2382 -#define OP_LoadAnalysis  124
  2.2383 -#define OP_DropTable     125
  2.2384 -#define OP_DropIndex     126
  2.2385 -#define OP_DropTrigger   127
  2.2386 -#define OP_IntegrityCk   128
  2.2387 -#define OP_RowSetAdd     129 /* synopsis: rowset(P1)=r[P2]                 */
  2.2388 -#define OP_RowSetRead    130 /* synopsis: r[P3]=rowset(P1)                 */
  2.2389 -#define OP_RowSetTest    131 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  2.2390 -#define OP_Program       132
  2.2391 +#define OP_Count          98 /* synopsis: r[P2]=count()                    */
  2.2392 +#define OP_ReadCookie     99
  2.2393 +#define OP_SetCookie     100
  2.2394 +#define OP_ReopenIdx     101 /* synopsis: root=P2 iDb=P3                   */
  2.2395 +#define OP_OpenRead      102 /* synopsis: root=P2 iDb=P3                   */
  2.2396 +#define OP_OpenWrite     103 /* synopsis: root=P2 iDb=P3                   */
  2.2397 +#define OP_OpenAutoindex 104 /* synopsis: nColumn=P2                       */
  2.2398 +#define OP_OpenEphemeral 105 /* synopsis: nColumn=P2                       */
  2.2399 +#define OP_SorterOpen    106
  2.2400 +#define OP_SequenceTest  107 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
  2.2401 +#define OP_OpenPseudo    108 /* synopsis: P3 columns in r[P2]              */
  2.2402 +#define OP_Close         109
  2.2403 +#define OP_ColumnsUsed   110
  2.2404 +#define OP_Sequence      111 /* synopsis: r[P2]=cursor[P1].ctr++           */
  2.2405 +#define OP_NewRowid      112 /* synopsis: r[P2]=rowid                      */
  2.2406 +#define OP_Insert        113 /* synopsis: intkey=r[P3] data=r[P2]          */
  2.2407 +#define OP_InsertInt     114 /* synopsis: intkey=P3 data=r[P2]             */
  2.2408 +#define OP_Delete        115
  2.2409 +#define OP_ResetCount    116
  2.2410 +#define OP_SorterCompare 117 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
  2.2411 +#define OP_SorterData    118 /* synopsis: r[P2]=data                       */
  2.2412 +#define OP_RowKey        119 /* synopsis: r[P2]=key                        */
  2.2413 +#define OP_RowData       120 /* synopsis: r[P2]=data                       */
  2.2414 +#define OP_Rowid         121 /* synopsis: r[P2]=rowid                      */
  2.2415 +#define OP_NullRow       122
  2.2416 +#define OP_SorterInsert  123
  2.2417 +#define OP_IdxInsert     124 /* synopsis: key=r[P2]                        */
  2.2418 +#define OP_IdxDelete     125 /* synopsis: key=r[P2@P3]                     */
  2.2419 +#define OP_Seek          126 /* synopsis: Move P3 to P1.rowid              */
  2.2420 +#define OP_IdxRowid      127 /* synopsis: r[P2]=rowid                      */
  2.2421 +#define OP_Destroy       128
  2.2422 +#define OP_Clear         129
  2.2423 +#define OP_ResetSorter   130
  2.2424 +#define OP_CreateIndex   131 /* synopsis: r[P2]=root iDb=P1                */
  2.2425 +#define OP_CreateTable   132 /* synopsis: r[P2]=root iDb=P1                */
  2.2426  #define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
  2.2427 -#define OP_Param         134
  2.2428 -#define OP_FkCounter     135 /* synopsis: fkctr[P1]+=P2                    */
  2.2429 -#define OP_FkIfZero      136 /* synopsis: if fkctr[P1]==0 goto P2          */
  2.2430 -#define OP_MemMax        137 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  2.2431 -#define OP_IfPos         138 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
  2.2432 -#define OP_OffsetLimit   139 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
  2.2433 -#define OP_IfNotZero     140 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
  2.2434 -#define OP_DecrJumpZero  141 /* synopsis: if (--r[P1])==0 goto P2          */
  2.2435 -#define OP_JumpZeroIncr  142 /* synopsis: if (r[P1]++)==0 ) goto P2        */
  2.2436 -#define OP_AggStep0      143 /* synopsis: accum=r[P3] step(r[P2@P5])       */
  2.2437 -#define OP_AggStep       144 /* synopsis: accum=r[P3] step(r[P2@P5])       */
  2.2438 -#define OP_AggFinal      145 /* synopsis: accum=r[P1] N=P2                 */
  2.2439 -#define OP_IncrVacuum    146
  2.2440 -#define OP_Expire        147
  2.2441 -#define OP_TableLock     148 /* synopsis: iDb=P1 root=P2 write=P3          */
  2.2442 -#define OP_VBegin        149
  2.2443 -#define OP_VCreate       150
  2.2444 -#define OP_VDestroy      151
  2.2445 -#define OP_VOpen         152
  2.2446 -#define OP_VColumn       153 /* synopsis: r[P3]=vcolumn(P2)                */
  2.2447 -#define OP_VNext         154
  2.2448 +#define OP_ParseSchema   134
  2.2449 +#define OP_LoadAnalysis  135
  2.2450 +#define OP_DropTable     136
  2.2451 +#define OP_DropIndex     137
  2.2452 +#define OP_DropTrigger   138
  2.2453 +#define OP_IntegrityCk   139
  2.2454 +#define OP_RowSetAdd     140 /* synopsis: rowset(P1)=r[P2]                 */
  2.2455 +#define OP_Param         141
  2.2456 +#define OP_FkCounter     142 /* synopsis: fkctr[P1]+=P2                    */
  2.2457 +#define OP_MemMax        143 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  2.2458 +#define OP_OffsetLimit   144 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
  2.2459 +#define OP_AggStep0      145 /* synopsis: accum=r[P3] step(r[P2@P5])       */
  2.2460 +#define OP_AggStep       146 /* synopsis: accum=r[P3] step(r[P2@P5])       */
  2.2461 +#define OP_AggFinal      147 /* synopsis: accum=r[P1] N=P2                 */
  2.2462 +#define OP_Expire        148
  2.2463 +#define OP_TableLock     149 /* synopsis: iDb=P1 root=P2 write=P3          */
  2.2464 +#define OP_VBegin        150
  2.2465 +#define OP_VCreate       151
  2.2466 +#define OP_VDestroy      152
  2.2467 +#define OP_VOpen         153
  2.2468 +#define OP_VColumn       154 /* synopsis: r[P3]=vcolumn(P2)                */
  2.2469  #define OP_VRename       155
  2.2470  #define OP_Pagecount     156
  2.2471  #define OP_MaxPgcnt      157
  2.2472 -#define OP_Init          158 /* synopsis: Start at P2                      */
  2.2473 -#define OP_CursorHint    159
  2.2474 -#define OP_Noop          160
  2.2475 -#define OP_Explain       161
  2.2476 +#define OP_CursorHint    158
  2.2477 +#define OP_Noop          159
  2.2478 +#define OP_Explain       160
  2.2479  
  2.2480  /* Properties such as "out2" or "jump" that are specified in
  2.2481  ** comments following the "case" for each opcode in the vdbe.c
  2.2482 @@ -10955,26 +12510,34 @@
  2.2483  #define OPFLG_OUT3        0x20  /* out3:  P3 is an output */
  2.2484  #define OPFLG_INITIALIZER {\
  2.2485  /*   0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,\
  2.2486 -/*   8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x02,\
  2.2487 -/*  16 */ 0x01, 0x02, 0x03, 0x12, 0x08, 0x00, 0x10, 0x10,\
  2.2488 -/*  24 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
  2.2489 -/*  32 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x02,\
  2.2490 -/*  40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\
  2.2491 -/*  48 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
  2.2492 -/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,\
  2.2493 -/*  64 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x26,\
  2.2494 -/*  72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\
  2.2495 -/*  80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\
  2.2496 -/*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
  2.2497 -/*  96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
  2.2498 -/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\
  2.2499 -/* 112 */ 0x00, 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00,\
  2.2500 -/* 120 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
  2.2501 -/* 128 */ 0x00, 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00,\
  2.2502 -/* 136 */ 0x01, 0x04, 0x03, 0x1a, 0x03, 0x03, 0x03, 0x00,\
  2.2503 -/* 144 */ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\
  2.2504 -/* 152 */ 0x00, 0x00, 0x01, 0x00, 0x10, 0x10, 0x01, 0x00,\
  2.2505 -/* 160 */ 0x00, 0x00,}
  2.2506 +/*   8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01,\
  2.2507 +/*  16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x03, 0x03, 0x09,\
  2.2508 +/*  24 */ 0x09, 0x09, 0x09, 0x26, 0x26, 0x09, 0x09, 0x09,\
  2.2509 +/*  32 */ 0x09, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
  2.2510 +/*  40 */ 0x0b, 0x0b, 0x01, 0x26, 0x26, 0x26, 0x26, 0x26,\
  2.2511 +/*  48 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x01, 0x12, 0x01,\
  2.2512 +/*  56 */ 0x01, 0x01, 0x01, 0x01, 0x23, 0x0b, 0x01, 0x01,\
  2.2513 +/*  64 */ 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x02, 0x02,\
  2.2514 +/*  72 */ 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10,\
  2.2515 +/*  80 */ 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\
  2.2516 +/*  88 */ 0x00, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,\
  2.2517 +/*  96 */ 0x00, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
  2.2518 +/* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
  2.2519 +/* 112 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  2.2520 +/* 120 */ 0x00, 0x10, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,\
  2.2521 +/* 128 */ 0x10, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\
  2.2522 +/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
  2.2523 +/* 144 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  2.2524 +/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
  2.2525 +/* 160 */ 0x00,}
  2.2526 +
  2.2527 +/* The sqlite3P2Values() routine is able to run faster if it knows
  2.2528 +** the value of the largest JUMP opcode.  The smaller the maximum
  2.2529 +** JUMP opcode the better, so the mkopcodeh.tcl script that
  2.2530 +** generated this include file strives to group all JUMP opcodes
  2.2531 +** together near the beginning of the list.
  2.2532 +*/
  2.2533 +#define SQLITE_MX_JUMP_OPCODE  69  /* Maximum JUMP opcode */
  2.2534  
  2.2535  /************** End of opcodes.h *********************************************/
  2.2536  /************** Continuing where we left off in vdbe.h ***********************/
  2.2537 @@ -11016,6 +12579,7 @@
  2.2538  SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
  2.2539  SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
  2.2540  SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
  2.2541 +SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
  2.2542  SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
  2.2543  SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
  2.2544  SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
  2.2545 @@ -11196,7 +12760,11 @@
  2.2546  #define PAGER_LOCKINGMODE_EXCLUSIVE   1
  2.2547  
  2.2548  /*
  2.2549 -** Numeric constants that encode the journalmode.  
  2.2550 +** Numeric constants that encode the journalmode.
  2.2551 +**
  2.2552 +** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
  2.2553 +** are exposed in the API via the "PRAGMA journal_mode" command and
  2.2554 +** therefore cannot be changed without a compatibility break.
  2.2555  */
  2.2556  #define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
  2.2557  #define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
  2.2558 @@ -11214,6 +12782,11 @@
  2.2559  
  2.2560  /*
  2.2561  ** Flags for sqlite3PagerSetFlags()
  2.2562 +**
  2.2563 +** Value constraints (enforced via assert()):
  2.2564 +**    PAGER_FULLFSYNC      == SQLITE_FullFSync
  2.2565 +**    PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
  2.2566 +**    PAGER_CACHE_SPILL    == SQLITE_CacheSpill
  2.2567  */
  2.2568  #define PAGER_SYNCHRONOUS_OFF       0x01  /* PRAGMA synchronous=OFF */
  2.2569  #define PAGER_SYNCHRONOUS_NORMAL    0x02  /* PRAGMA synchronous=NORMAL */
  2.2570 @@ -11319,7 +12892,6 @@
  2.2571  SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
  2.2572  SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
  2.2573  SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
  2.2574 -SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
  2.2575  SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
  2.2576  SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
  2.2577  SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
  2.2578 @@ -11384,7 +12956,7 @@
  2.2579    sqlite3_pcache_page *pPage;    /* Pcache object page handle */
  2.2580    void *pData;                   /* Page data */
  2.2581    void *pExtra;                  /* Extra content */
  2.2582 -  PgHdr *pDirty;                 /* Transient list of dirty pages */
  2.2583 +  PgHdr *pDirty;                 /* Transient list of dirty sorted by pgno */
  2.2584    Pager *pPager;                 /* The pager this page is part of */
  2.2585    Pgno pgno;                     /* Page number for this page */
  2.2586  #ifdef SQLITE_CHECK_PAGES
  2.2587 @@ -11409,11 +12981,10 @@
  2.2588  #define PGHDR_WRITEABLE       0x004  /* Journaled and ready to modify */
  2.2589  #define PGHDR_NEED_SYNC       0x008  /* Fsync the rollback journal before
  2.2590                                       ** writing this page to the database */
  2.2591 -#define PGHDR_NEED_READ       0x010  /* Content is unread */
  2.2592 -#define PGHDR_DONT_WRITE      0x020  /* Do not write content to disk */
  2.2593 -#define PGHDR_MMAP            0x040  /* This is an mmap page object */
  2.2594 -
  2.2595 -#define PGHDR_WAL_APPEND      0x080  /* Appended to wal file */
  2.2596 +#define PGHDR_DONT_WRITE      0x010  /* Do not write content to disk */
  2.2597 +#define PGHDR_MMAP            0x020  /* This is an mmap page object */
  2.2598 +
  2.2599 +#define PGHDR_WAL_APPEND      0x040  /* Appended to wal file */
  2.2600  
  2.2601  /* Initialize and shutdown the page cache subsystem */
  2.2602  SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
  2.2603 @@ -11457,6 +13028,7 @@
  2.2604  SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
  2.2605  SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
  2.2606  SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
  2.2607 +SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache*);
  2.2608  
  2.2609  /* Change a page number.  Used by incr-vacuum. */
  2.2610  SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
  2.2611 @@ -11495,6 +13067,11 @@
  2.2612  SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
  2.2613  #endif
  2.2614  
  2.2615 +#if defined(SQLITE_DEBUG)
  2.2616 +/* Check invariants on a PgHdr object */
  2.2617 +SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*);
  2.2618 +#endif
  2.2619 +
  2.2620  /* Set and get the suggested cache-size for the specified pager-cache.
  2.2621  **
  2.2622  ** If no global maximum is configured, then the system attempts to limit
  2.2623 @@ -11531,11 +13108,13 @@
  2.2624  SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
  2.2625  SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
  2.2626  
  2.2627 +/* Number of dirty pages as a percentage of the configured cache size */
  2.2628 +SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
  2.2629 +
  2.2630  #endif /* _PCACHE_H_ */
  2.2631  
  2.2632  /************** End of pcache.h **********************************************/
  2.2633  /************** Continuing where we left off in sqliteInt.h ******************/
  2.2634 -
  2.2635  /************** Include os.h in the middle of sqliteInt.h ********************/
  2.2636  /************** Begin file os.h **********************************************/
  2.2637  /*
  2.2638 @@ -11761,7 +13340,7 @@
  2.2639  /* 
  2.2640  ** Functions for accessing sqlite3_file methods 
  2.2641  */
  2.2642 -SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
  2.2643 +SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
  2.2644  SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
  2.2645  SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
  2.2646  SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
  2.2647 @@ -11798,6 +13377,7 @@
  2.2648  #endif /* SQLITE_OMIT_LOAD_EXTENSION */
  2.2649  SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
  2.2650  SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
  2.2651 +SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
  2.2652  SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
  2.2653  
  2.2654  /*
  2.2655 @@ -11805,7 +13385,7 @@
  2.2656  ** sqlite3_malloc() to obtain space for the file-handle structure.
  2.2657  */
  2.2658  SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
  2.2659 -SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
  2.2660 +SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
  2.2661  
  2.2662  #endif /* _SQLITE_OS_H_ */
  2.2663  
  2.2664 @@ -11887,6 +13467,36 @@
  2.2665  /************** End of mutex.h ***********************************************/
  2.2666  /************** Continuing where we left off in sqliteInt.h ******************/
  2.2667  
  2.2668 +/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
  2.2669 +** synchronous setting to EXTRA.  It is no longer supported.
  2.2670 +*/
  2.2671 +#ifdef SQLITE_EXTRA_DURABLE
  2.2672 +# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
  2.2673 +# define SQLITE_DEFAULT_SYNCHRONOUS 3
  2.2674 +#endif
  2.2675 +
  2.2676 +/*
  2.2677 +** Default synchronous levels.
  2.2678 +**
  2.2679 +** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
  2.2680 +** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
  2.2681 +**
  2.2682 +**           PAGER_SYNCHRONOUS       DEFAULT_SYNCHRONOUS
  2.2683 +**   OFF           1                         0
  2.2684 +**   NORMAL        2                         1
  2.2685 +**   FULL          3                         2
  2.2686 +**   EXTRA         4                         3
  2.2687 +**
  2.2688 +** The "PRAGMA synchronous" statement also uses the zero-based numbers.
  2.2689 +** In other words, the zero-based numbers are used for all external interfaces
  2.2690 +** and the one-based values are used internally.
  2.2691 +*/
  2.2692 +#ifndef SQLITE_DEFAULT_SYNCHRONOUS
  2.2693 +# define SQLITE_DEFAULT_SYNCHRONOUS (PAGER_SYNCHRONOUS_FULL-1)
  2.2694 +#endif
  2.2695 +#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
  2.2696 +# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
  2.2697 +#endif
  2.2698  
  2.2699  /*
  2.2700  ** Each database file to be accessed by the system is an instance
  2.2701 @@ -11899,6 +13509,7 @@
  2.2702    char *zName;         /* Name of this database */
  2.2703    Btree *pBt;          /* The B*Tree structure for this database file */
  2.2704    u8 safety_level;     /* How aggressive at syncing data to disk */
  2.2705 +  u8 bSyncSet;         /* True if "PRAGMA synchronous=N" has been run */
  2.2706    Schema *pSchema;     /* Pointer to database schema (possibly shared) */
  2.2707  };
  2.2708  
  2.2709 @@ -11909,7 +13520,7 @@
  2.2710  ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
  2.2711  ** In shared cache mode, a single Schema object can be shared by multiple
  2.2712  ** Btrees that refer to the same underlying BtShared object.
  2.2713 -** 
  2.2714 +**
  2.2715  ** Schema objects are automatically deallocated when the last Btree that
  2.2716  ** references them is destroyed.   The TEMP Schema is manually freed by
  2.2717  ** sqlite3_close().
  2.2718 @@ -11934,7 +13545,7 @@
  2.2719  };
  2.2720  
  2.2721  /*
  2.2722 -** These macros can be used to test, set, or clear bits in the 
  2.2723 +** These macros can be used to test, set, or clear bits in the
  2.2724  ** Db.pSchema->flags field.
  2.2725  */
  2.2726  #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
  2.2727 @@ -11998,13 +13609,15 @@
  2.2728  };
  2.2729  
  2.2730  /*
  2.2731 -** A hash table for function definitions.
  2.2732 +** A hash table for built-in function definitions.  (Application-defined
  2.2733 +** functions use a regular table table from hash.h.)
  2.2734  **
  2.2735  ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
  2.2736 -** Collisions are on the FuncDef.pHash chain.
  2.2737 -*/
  2.2738 +** Collisions are on the FuncDef.u.pHash chain.
  2.2739 +*/
  2.2740 +#define SQLITE_FUNC_HASH_SZ 23
  2.2741  struct FuncDefHash {
  2.2742 -  FuncDef *a[23];       /* Hash table for functions */
  2.2743 +  FuncDef *a[SQLITE_FUNC_HASH_SZ];       /* Hash table for functions */
  2.2744  };
  2.2745  
  2.2746  #ifdef SQLITE_USER_AUTHENTICATION
  2.2747 @@ -12062,6 +13675,7 @@
  2.2748    unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
  2.2749    int errCode;                  /* Most recent error code (SQLITE_*) */
  2.2750    int errMask;                  /* & result codes with this before returning */
  2.2751 +  int iSysErrno;                /* Errno value from last system error */
  2.2752    u16 dbOptFlags;               /* Flags to enable/disable optimizations */
  2.2753    u8 enc;                       /* Text encoding */
  2.2754    u8 autoCommit;                /* The auto-commit flag. */
  2.2755 @@ -12097,12 +13711,19 @@
  2.2756    void *pTraceArg;                          /* Argument to the trace function */
  2.2757    void (*xProfile)(void*,const char*,u64);  /* Profiling function */
  2.2758    void *pProfileArg;                        /* Argument to profile function */
  2.2759 -  void *pCommitArg;                 /* Argument to xCommitCallback() */   
  2.2760 +  void *pCommitArg;                 /* Argument to xCommitCallback() */
  2.2761    int (*xCommitCallback)(void*);    /* Invoked at every commit. */
  2.2762 -  void *pRollbackArg;               /* Argument to xRollbackCallback() */   
  2.2763 +  void *pRollbackArg;               /* Argument to xRollbackCallback() */
  2.2764    void (*xRollbackCallback)(void*); /* Invoked at every commit. */
  2.2765    void *pUpdateArg;
  2.2766    void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
  2.2767 +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  2.2768 +  void *pPreUpdateArg;          /* First argument to xPreUpdateCallback */
  2.2769 +  void (*xPreUpdateCallback)(   /* Registered using sqlite3_preupdate_hook() */
  2.2770 +    void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
  2.2771 +  );
  2.2772 +  PreUpdate *pPreUpdate;        /* Context for active pre-update callback */
  2.2773 +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
  2.2774  #ifndef SQLITE_OMIT_WAL
  2.2775    int (*xWalCallback)(void *, sqlite3 *, const char *, int);
  2.2776    void *pWalArg;
  2.2777 @@ -12132,7 +13753,7 @@
  2.2778    VTable **aVTrans;             /* Virtual tables with open transactions */
  2.2779    VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
  2.2780  #endif
  2.2781 -  FuncDefHash aFunc;            /* Hash table of connection functions */
  2.2782 +  Hash aFunc;                   /* Hash table of connection functions */
  2.2783    Hash aCollSeq;                /* All collating sequences */
  2.2784    BusyHandler busyHandler;      /* Busy callback */
  2.2785    Db aDbStatic[2];              /* Static space for the 2 default backends */
  2.2786 @@ -12144,8 +13765,8 @@
  2.2787    i64 nDeferredImmCons;         /* Net deferred immediate constraints */
  2.2788    int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
  2.2789  #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  2.2790 -  /* The following variables are all protected by the STATIC_MASTER 
  2.2791 -  ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
  2.2792 +  /* The following variables are all protected by the STATIC_MASTER
  2.2793 +  ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
  2.2794    **
  2.2795    ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
  2.2796    ** unlock so that it can proceed.
  2.2797 @@ -12173,6 +13794,11 @@
  2.2798  
  2.2799  /*
  2.2800  ** Possible values for the sqlite3.flags.
  2.2801 +**
  2.2802 +** Value constraints (enforced via assert()):
  2.2803 +**      SQLITE_FullFSync     == PAGER_FULLFSYNC
  2.2804 +**      SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
  2.2805 +**      SQLITE_CacheSpill    == PAGER_CACHE_SPILL
  2.2806  */
  2.2807  #define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
  2.2808  #define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
  2.2809 @@ -12200,12 +13826,14 @@
  2.2810  #define SQLITE_AutoIndex      0x00100000  /* Enable automatic indexes */
  2.2811  #define SQLITE_PreferBuiltin  0x00200000  /* Preference to built-in funcs */
  2.2812  #define SQLITE_LoadExtension  0x00400000  /* Enable load_extension */
  2.2813 -#define SQLITE_EnableTrigger  0x00800000  /* True to enable triggers */
  2.2814 -#define SQLITE_DeferFKs       0x01000000  /* Defer all FK constraints */
  2.2815 -#define SQLITE_QueryOnly      0x02000000  /* Disable database changes */
  2.2816 -#define SQLITE_VdbeEQP        0x04000000  /* Debug EXPLAIN QUERY PLAN */
  2.2817 -#define SQLITE_Vacuum         0x08000000  /* Currently in a VACUUM */
  2.2818 -#define SQLITE_CellSizeCk     0x10000000  /* Check btree cell sizes on load */
  2.2819 +#define SQLITE_LoadExtFunc    0x00800000  /* Enable load_extension() SQL func */
  2.2820 +#define SQLITE_EnableTrigger  0x01000000  /* True to enable triggers */
  2.2821 +#define SQLITE_DeferFKs       0x02000000  /* Defer all FK constraints */
  2.2822 +#define SQLITE_QueryOnly      0x04000000  /* Disable database changes */
  2.2823 +#define SQLITE_VdbeEQP        0x08000000  /* Debug EXPLAIN QUERY PLAN */
  2.2824 +#define SQLITE_Vacuum         0x10000000  /* Currently in a VACUUM */
  2.2825 +#define SQLITE_CellSizeCk     0x20000000  /* Check btree cell sizes on load */
  2.2826 +#define SQLITE_Fts3Tokenizer  0x40000000  /* Enable fts3_tokenizer(2) */
  2.2827  
  2.2828  
  2.2829  /*
  2.2830 @@ -12259,27 +13887,33 @@
  2.2831  
  2.2832  /*
  2.2833  ** Each SQL function is defined by an instance of the following
  2.2834 -** structure.  A pointer to this structure is stored in the sqlite.aFunc
  2.2835 -** hash table.  When multiple functions have the same name, the hash table
  2.2836 -** points to a linked list of these structures.
  2.2837 +** structure.  For global built-in functions (ex: substr(), max(), count())
  2.2838 +** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
  2.2839 +** For per-connection application-defined functions, a pointer to this
  2.2840 +** structure is held in the db->aHash hash table.
  2.2841 +**
  2.2842 +** The u.pHash field is used by the global built-ins.  The u.pDestructor
  2.2843 +** field is used by per-connection app-def functions.
  2.2844  */
  2.2845  struct FuncDef {
  2.2846 -  i16 nArg;            /* Number of arguments.  -1 means unlimited */
  2.2847 +  i8 nArg;             /* Number of arguments.  -1 means unlimited */
  2.2848    u16 funcFlags;       /* Some combination of SQLITE_FUNC_* */
  2.2849    void *pUserData;     /* User data parameter */
  2.2850    FuncDef *pNext;      /* Next function with same name */
  2.2851    void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
  2.2852    void (*xFinalize)(sqlite3_context*);                  /* Agg finalizer */
  2.2853 -  char *zName;         /* SQL name of the function. */
  2.2854 -  FuncDef *pHash;      /* Next with a different name but the same hash */
  2.2855 -  FuncDestructor *pDestructor;   /* Reference counted destructor function */
  2.2856 +  const char *zName;   /* SQL name of the function. */
  2.2857 +  union {
  2.2858 +    FuncDef *pHash;      /* Next with a different name but the same hash */
  2.2859 +    FuncDestructor *pDestructor;   /* Reference counted destructor function */
  2.2860 +  } u;
  2.2861  };
  2.2862  
  2.2863  /*
  2.2864  ** This structure encapsulates a user-function destructor callback (as
  2.2865  ** configured using create_function_v2()) and a reference counter. When
  2.2866  ** create_function_v2() is called to create a function with a destructor,
  2.2867 -** a single object of this type is allocated. FuncDestructor.nRef is set to 
  2.2868 +** a single object of this type is allocated. FuncDestructor.nRef is set to
  2.2869  ** the number of FuncDef objects created (either 1 or 3, depending on whether
  2.2870  ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
  2.2871  ** member of each of the new FuncDef objects is set to point to the allocated
  2.2872 @@ -12300,6 +13934,13 @@
  2.2873  ** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  And
  2.2874  ** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC.  There
  2.2875  ** are assert() statements in the code to verify this.
  2.2876 +**
  2.2877 +** Value constraints (enforced via assert()):
  2.2878 +**     SQLITE_FUNC_MINMAX    ==  NC_MinMaxAgg      == SF_MinMaxAgg
  2.2879 +**     SQLITE_FUNC_LENGTH    ==  OPFLAG_LENGTHARG
  2.2880 +**     SQLITE_FUNC_TYPEOF    ==  OPFLAG_TYPEOFARG
  2.2881 +**     SQLITE_FUNC_CONSTANT  ==  SQLITE_DETERMINISTIC from the API
  2.2882 +**     SQLITE_FUNC_ENCMASK   depends on SQLITE_UTF* macros in the API
  2.2883  */
  2.2884  #define SQLITE_FUNC_ENCMASK  0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
  2.2885  #define SQLITE_FUNC_LIKE     0x0004 /* Candidate for the LIKE optimization */
  2.2886 @@ -12321,10 +13962,10 @@
  2.2887  ** used to create the initializers for the FuncDef structures.
  2.2888  **
  2.2889  **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
  2.2890 -**     Used to create a scalar function definition of a function zName 
  2.2891 +**     Used to create a scalar function definition of a function zName
  2.2892  **     implemented by C function xFunc that accepts nArg arguments. The
  2.2893  **     value passed as iArg is cast to a (void*) and made available
  2.2894 -**     as the user-data (sqlite3_user_data()) for the function. If 
  2.2895 +**     as the user-data (sqlite3_user_data()) for the function. If
  2.2896  **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
  2.2897  **
  2.2898  **   VFUNCTION(zName, nArg, iArg, bNC, xFunc)
  2.2899 @@ -12343,8 +13984,8 @@
  2.2900  **     FUNCTION().
  2.2901  **
  2.2902  **   LIKEFUNC(zName, nArg, pArg, flags)
  2.2903 -**     Used to create a scalar function definition of a function zName 
  2.2904 -**     that accepts nArg arguments and is implemented by a call to C 
  2.2905 +**     Used to create a scalar function definition of a function zName
  2.2906 +**     that accepts nArg arguments and is implemented by a call to C
  2.2907  **     function likeFunc. Argument pArg is cast to a (void *) and made
  2.2908  **     available as the function user-data (sqlite3_user_data()). The
  2.2909  **     FuncDef.flags variable is set to the value passed as the flags
  2.2910 @@ -12352,28 +13993,28 @@
  2.2911  */
  2.2912  #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
  2.2913    {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  2.2914 -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
  2.2915 +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} }
  2.2916  #define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
  2.2917    {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  2.2918 -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
  2.2919 +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} }
  2.2920  #define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
  2.2921    {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  2.2922 -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
  2.2923 +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} }
  2.2924  #define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
  2.2925    {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
  2.2926 -   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0}
  2.2927 +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, {0} }
  2.2928  #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
  2.2929    {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  2.2930 -   pArg, 0, xFunc, 0, #zName, 0, 0}
  2.2931 +   pArg, 0, xFunc, 0, #zName, }
  2.2932  #define LIKEFUNC(zName, nArg, arg, flags) \
  2.2933    {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
  2.2934 -   (void *)arg, 0, likeFunc, 0, #zName, 0, 0}
  2.2935 +   (void *)arg, 0, likeFunc, 0, #zName, {0} }
  2.2936  #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
  2.2937    {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
  2.2938 -   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName,0,0}
  2.2939 +   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName, {0}}
  2.2940  #define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \
  2.2941    {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
  2.2942 -   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName,0,0}
  2.2943 +   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName, {0}}
  2.2944  
  2.2945  /*
  2.2946  ** All current savepoints are stored in a linked list starting at
  2.2947 @@ -12415,10 +14056,8 @@
  2.2948  ** of this structure.
  2.2949  */
  2.2950  struct Column {
  2.2951 -  char *zName;     /* Name of this column */
  2.2952 +  char *zName;     /* Name of this column, \000, then the type */
  2.2953    Expr *pDflt;     /* Default value of this column */
  2.2954 -  char *zDflt;     /* Original text of the default value */
  2.2955 -  char *zType;     /* Data type for this column */
  2.2956    char *zColl;     /* Collating sequence.  If NULL, use the default */
  2.2957    u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
  2.2958    char affinity;   /* One of the SQLITE_AFF_... values */
  2.2959 @@ -12430,6 +14069,7 @@
  2.2960  */
  2.2961  #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
  2.2962  #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
  2.2963 +#define COLFLAG_HASTYPE  0x0004    /* Type name follows column name */
  2.2964  
  2.2965  /*
  2.2966  ** A "Collating Sequence" is defined by an instance of the following
  2.2967 @@ -12460,7 +14100,7 @@
  2.2968  **
  2.2969  ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
  2.2970  ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
  2.2971 -** the speed a little by numbering the values consecutively.  
  2.2972 +** the speed a little by numbering the values consecutively.
  2.2973  **
  2.2974  ** But rather than start with 0 or 1, we begin with 'A'.  That way,
  2.2975  ** when multiple affinity types are concatenated into a string and
  2.2976 @@ -12479,7 +14119,7 @@
  2.2977  
  2.2978  /*
  2.2979  ** The SQLITE_AFF_MASK values masks off the significant bits of an
  2.2980 -** affinity value. 
  2.2981 +** affinity value.
  2.2982  */
  2.2983  #define SQLITE_AFF_MASK     0x47
  2.2984  
  2.2985 @@ -12499,20 +14139,20 @@
  2.2986  
  2.2987  /*
  2.2988  ** An object of this type is created for each virtual table present in
  2.2989 -** the database schema. 
  2.2990 +** the database schema.
  2.2991  **
  2.2992  ** If the database schema is shared, then there is one instance of this
  2.2993  ** structure for each database connection (sqlite3*) that uses the shared
  2.2994  ** schema. This is because each database connection requires its own unique
  2.2995 -** instance of the sqlite3_vtab* handle used to access the virtual table 
  2.2996 -** implementation. sqlite3_vtab* handles can not be shared between 
  2.2997 -** database connections, even when the rest of the in-memory database 
  2.2998 +** instance of the sqlite3_vtab* handle used to access the virtual table
  2.2999 +** implementation. sqlite3_vtab* handles can not be shared between
  2.3000 +** database connections, even when the rest of the in-memory database
  2.3001  ** schema is shared, as the implementation often stores the database
  2.3002  ** connection handle passed to it via the xConnect() or xCreate() method
  2.3003  ** during initialization internally. This database connection handle may
  2.3004 -** then be used by the virtual table implementation to access real tables 
  2.3005 -** within the database. So that they appear as part of the callers 
  2.3006 -** transaction, these accesses need to be made via the same database 
  2.3007 +** then be used by the virtual table implementation to access real tables
  2.3008 +** within the database. So that they appear as part of the callers
  2.3009 +** transaction, these accesses need to be made via the same database
  2.3010  ** connection as that used to execute SQL operations on the virtual table.
  2.3011  **
  2.3012  ** All VTable objects that correspond to a single table in a shared
  2.3013 @@ -12524,19 +14164,19 @@
  2.3014  ** sqlite3_vtab* handle in the compiled query.
  2.3015  **
  2.3016  ** When an in-memory Table object is deleted (for example when the
  2.3017 -** schema is being reloaded for some reason), the VTable objects are not 
  2.3018 -** deleted and the sqlite3_vtab* handles are not xDisconnect()ed 
  2.3019 +** schema is being reloaded for some reason), the VTable objects are not
  2.3020 +** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
  2.3021  ** immediately. Instead, they are moved from the Table.pVTable list to
  2.3022  ** another linked list headed by the sqlite3.pDisconnect member of the
  2.3023 -** corresponding sqlite3 structure. They are then deleted/xDisconnected 
  2.3024 +** corresponding sqlite3 structure. They are then deleted/xDisconnected
  2.3025  ** next time a statement is prepared using said sqlite3*. This is done
  2.3026  ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
  2.3027  ** Refer to comments above function sqlite3VtabUnlockList() for an
  2.3028  ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
  2.3029  ** list without holding the corresponding sqlite3.mutex mutex.
  2.3030  **
  2.3031 -** The memory for objects of this type is always allocated by 
  2.3032 -** sqlite3DbMalloc(), using the connection handle stored in VTable.db as 
  2.3033 +** The memory for objects of this type is always allocated by
  2.3034 +** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
  2.3035  ** the first argument.
  2.3036  */
  2.3037  struct VTable {
  2.3038 @@ -12704,7 +14344,7 @@
  2.3039  ** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
  2.3040  ** referenced table row is propagated into the row that holds the
  2.3041  ** foreign key.
  2.3042 -** 
  2.3043 +**
  2.3044  ** The following symbolic values are used to record which type
  2.3045  ** of action to take.
  2.3046  */
  2.3047 @@ -12725,7 +14365,7 @@
  2.3048  
  2.3049  /*
  2.3050  ** An instance of the following structure is passed as the first
  2.3051 -** argument to sqlite3VdbeKeyCompare and is used to control the 
  2.3052 +** argument to sqlite3VdbeKeyCompare and is used to control the
  2.3053  ** comparison of the two index keys.
  2.3054  **
  2.3055  ** Note that aSortOrder[] and aColl[] have nField+1 slots.  There
  2.3056 @@ -12766,7 +14406,7 @@
  2.3057  ** The key comparison functions actually return default_rc when they find
  2.3058  ** an equals comparison.  default_rc can be -1, 0, or +1.  If there are
  2.3059  ** multiple entries in the b-tree with the same key (when only looking
  2.3060 -** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to 
  2.3061 +** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
  2.3062  ** cause the search to find the last match, or +1 to cause the search to
  2.3063  ** find the first match.
  2.3064  **
  2.3065 @@ -12803,7 +14443,7 @@
  2.3066  ** In the Table structure describing Ex1, nCol==3 because there are
  2.3067  ** three columns in the table.  In the Index structure describing
  2.3068  ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
  2.3069 -** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the 
  2.3070 +** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the
  2.3071  ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
  2.3072  ** The second column to be indexed (c1) has an index of 0 in
  2.3073  ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
  2.3074 @@ -12811,7 +14451,7 @@
  2.3075  ** The Index.onError field determines whether or not the indexed columns
  2.3076  ** must be unique and what to do if they are not.  When Index.onError=OE_None,
  2.3077  ** it means this is not a unique index.  Otherwise it is a unique index
  2.3078 -** and the value of Index.onError indicate the which conflict resolution 
  2.3079 +** and the value of Index.onError indicate the which conflict resolution
  2.3080  ** algorithm to employ whenever an attempt is made to insert a non-unique
  2.3081  ** element.
  2.3082  **
  2.3083 @@ -12876,7 +14516,7 @@
  2.3084  #define XN_EXPR      (-2)     /* Indexed column is an expression */
  2.3085  
  2.3086  /*
  2.3087 -** Each sample stored in the sqlite_stat3 table is represented in memory 
  2.3088 +** Each sample stored in the sqlite_stat3 table is represented in memory
  2.3089  ** using a structure of this type.  See documentation at the top of the
  2.3090  ** analyze.c source file for additional information.
  2.3091  */
  2.3092 @@ -12971,9 +14611,9 @@
  2.3093  ** to represent the greater-than-or-equal-to operator in the expression
  2.3094  ** tree.
  2.3095  **
  2.3096 -** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, 
  2.3097 +** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
  2.3098  ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
  2.3099 -** the expression is a variable (TK_VARIABLE), then Expr.token contains the 
  2.3100 +** the expression is a variable (TK_VARIABLE), then Expr.token contains the
  2.3101  ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
  2.3102  ** then Expr.token contains the name of the function.
  2.3103  **
  2.3104 @@ -12984,7 +14624,7 @@
  2.3105  ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
  2.3106  ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
  2.3107  ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
  2.3108 -** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is 
  2.3109 +** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
  2.3110  ** valid.
  2.3111  **
  2.3112  ** An expression of the form ID or ID.ID refers to a column in a table.
  2.3113 @@ -12995,8 +14635,8 @@
  2.3114  ** value is also stored in the Expr.iAgg column in the aggregate so that
  2.3115  ** it can be accessed after all aggregates are computed.
  2.3116  **
  2.3117 -** If the expression is an unbound variable marker (a question mark 
  2.3118 -** character '?' in the original SQL) then the Expr.iTable holds the index 
  2.3119 +** If the expression is an unbound variable marker (a question mark
  2.3120 +** character '?' in the original SQL) then the Expr.iTable holds the index
  2.3121  ** number for that variable.
  2.3122  **
  2.3123  ** If the expression is a subquery then Expr.iColumn holds an integer
  2.3124 @@ -13035,7 +14675,7 @@
  2.3125  
  2.3126    /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
  2.3127    ** space is allocated for the fields below this point. An attempt to
  2.3128 -  ** access them will result in a segfault or malfunction. 
  2.3129 +  ** access them will result in a segfault or malfunction.
  2.3130    *********************************************************************/
  2.3131  
  2.3132    Expr *pLeft;           /* Left subnode */
  2.3133 @@ -13101,7 +14741,7 @@
  2.3134  #define EP_Propagate (EP_Collate|EP_Subquery) /* Propagate these bits up tree */
  2.3135  
  2.3136  /*
  2.3137 -** These macros can be used to test, set, or clear bits in the 
  2.3138 +** These macros can be used to test, set, or clear bits in the
  2.3139  ** Expr.flags field.
  2.3140  */
  2.3141  #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
  2.3142 @@ -13120,8 +14760,8 @@
  2.3143  #endif
  2.3144  
  2.3145  /*
  2.3146 -** Macros to determine the number of bytes required by a normal Expr 
  2.3147 -** struct, an Expr struct with the EP_Reduced flag set in Expr.flags 
  2.3148 +** Macros to determine the number of bytes required by a normal Expr
  2.3149 +** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
  2.3150  ** and an Expr struct with the EP_TokenOnly flag set.
  2.3151  */
  2.3152  #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
  2.3153 @@ -13129,7 +14769,7 @@
  2.3154  #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
  2.3155  
  2.3156  /*
  2.3157 -** Flags passed to the sqlite3ExprDup() function. See the header comment 
  2.3158 +** Flags passed to the sqlite3ExprDup() function. See the header comment
  2.3159  ** above sqlite3ExprDup() for details.
  2.3160  */
  2.3161  #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
  2.3162 @@ -13211,7 +14851,11 @@
  2.3163  ** tables in a join to 32 instead of 64.  But it also reduces the size
  2.3164  ** of the library by 738 bytes on ix86.
  2.3165  */
  2.3166 -typedef u64 Bitmask;
  2.3167 +#ifdef SQLITE_BITMASK_TYPE
  2.3168 +  typedef SQLITE_BITMASK_TYPE Bitmask;
  2.3169 +#else
  2.3170 +  typedef u64 Bitmask;
  2.3171 +#endif
  2.3172  
  2.3173  /*
  2.3174  ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
  2.3175 @@ -13223,6 +14867,7 @@
  2.3176  */
  2.3177  #define MASKBIT(n)   (((Bitmask)1)<<(n))
  2.3178  #define MASKBIT32(n) (((unsigned int)1)<<(n))
  2.3179 +#define ALLBITS      ((Bitmask)-1)
  2.3180  
  2.3181  /*
  2.3182  ** The following structure describes the FROM clause of a SELECT statement.
  2.3183 @@ -13295,6 +14940,9 @@
  2.3184  /*
  2.3185  ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
  2.3186  ** and the WhereInfo.wctrlFlags member.
  2.3187 +**
  2.3188 +** Value constraints (enforced via assert()):
  2.3189 +**     WHERE_USE_LIMIT  == SF_FixedLimit
  2.3190  */
  2.3191  #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
  2.3192  #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
  2.3193 @@ -13311,6 +14959,8 @@
  2.3194  #define WHERE_SORTBYGROUP      0x0800 /* Support sqlite3WhereIsSorted() */
  2.3195  #define WHERE_REOPEN_IDX       0x1000 /* Try to use OP_ReopenIdx */
  2.3196  #define WHERE_ONEPASS_MULTIROW 0x2000 /* ONEPASS is ok with multiple rows */
  2.3197 +#define WHERE_USE_LIMIT        0x4000 /* There is a constant LIMIT clause */
  2.3198 +#define WHERE_SEEK_TABLE       0x8000 /* Do not defer seeks on main table */
  2.3199  
  2.3200  /* Allowed return values from sqlite3WhereIsDistinct()
  2.3201  */
  2.3202 @@ -13328,12 +14978,12 @@
  2.3203  ** pEList corresponds to the result set of a SELECT and is NULL for
  2.3204  ** other statements.
  2.3205  **
  2.3206 -** NameContexts can be nested.  When resolving names, the inner-most 
  2.3207 +** NameContexts can be nested.  When resolving names, the inner-most
  2.3208  ** context is searched first.  If no match is found, the next outer
  2.3209  ** context is checked.  If there is still no match, the next context
  2.3210  ** is checked.  This process continues until either a match is found
  2.3211  ** or all contexts are check.  When a match is found, the nRef member of
  2.3212 -** the context containing the match is incremented. 
  2.3213 +** the context containing the match is incremented.
  2.3214  **
  2.3215  ** Each subquery gets a new NameContext.  The pNext field points to the
  2.3216  ** NameContext in the parent query.  Thus the process of scanning the
  2.3217 @@ -13354,16 +15004,18 @@
  2.3218  /*
  2.3219  ** Allowed values for the NameContext, ncFlags field.
  2.3220  **
  2.3221 -** Note:  NC_MinMaxAgg must have the same value as SF_MinMaxAgg and
  2.3222 -** SQLITE_FUNC_MINMAX.
  2.3223 -** 
  2.3224 +** Value constraints (all checked via assert()):
  2.3225 +**    NC_HasAgg    == SF_HasAgg
  2.3226 +**    NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
  2.3227 +**
  2.3228  */
  2.3229  #define NC_AllowAgg  0x0001  /* Aggregate functions are allowed here */
  2.3230 -#define NC_HasAgg    0x0002  /* One or more aggregate functions seen */
  2.3231 +#define NC_PartIdx   0x0002  /* True if resolving a partial index WHERE */
  2.3232  #define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
  2.3233  #define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
  2.3234 -#define NC_PartIdx   0x0010  /* True if resolving a partial index WHERE */
  2.3235 +#define NC_HasAgg    0x0010  /* One or more aggregate functions seen */
  2.3236  #define NC_IdxExpr   0x0020  /* True if resolving columns of CREATE INDEX */
  2.3237 +#define NC_VarSelect 0x0040  /* A correlated subquery has been seen */
  2.3238  #define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
  2.3239  
  2.3240  /*
  2.3241 @@ -13389,13 +15041,13 @@
  2.3242  struct Select {
  2.3243    ExprList *pEList;      /* The fields of the result */
  2.3244    u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
  2.3245 -  u16 selFlags;          /* Various SF_* values */
  2.3246 +  LogEst nSelectRow;     /* Estimated number of result rows */
  2.3247 +  u32 selFlags;          /* Various SF_* values */
  2.3248    int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
  2.3249  #if SELECTTRACE_ENABLED
  2.3250    char zSelName[12];     /* Symbolic name of this SELECT use for debugging */
  2.3251  #endif
  2.3252    int addrOpenEphm[2];   /* OP_OpenEphem opcodes related to this select */
  2.3253 -  u64 nSelectRow;        /* Estimated number of result rows */
  2.3254    SrcList *pSrc;         /* The FROM clause */
  2.3255    Expr *pWhere;          /* The WHERE clause */
  2.3256    ExprList *pGroupBy;    /* The GROUP BY clause */
  2.3257 @@ -13411,23 +15063,30 @@
  2.3258  /*
  2.3259  ** Allowed values for Select.selFlags.  The "SF" prefix stands for
  2.3260  ** "Select Flag".
  2.3261 -*/
  2.3262 -#define SF_Distinct        0x0001  /* Output should be DISTINCT */
  2.3263 -#define SF_All             0x0002  /* Includes the ALL keyword */
  2.3264 -#define SF_Resolved        0x0004  /* Identifiers have been resolved */
  2.3265 -#define SF_Aggregate       0x0008  /* Contains aggregate functions */
  2.3266 -#define SF_UsesEphemeral   0x0010  /* Uses the OpenEphemeral opcode */
  2.3267 -#define SF_Expanded        0x0020  /* sqlite3SelectExpand() called on this */
  2.3268 -#define SF_HasTypeInfo     0x0040  /* FROM subqueries have Table metadata */
  2.3269 -#define SF_Compound        0x0080  /* Part of a compound query */
  2.3270 -#define SF_Values          0x0100  /* Synthesized from VALUES clause */
  2.3271 -#define SF_MultiValue      0x0200  /* Single VALUES term with multiple rows */
  2.3272 -#define SF_NestedFrom      0x0400  /* Part of a parenthesized FROM clause */
  2.3273 -#define SF_MaybeConvert    0x0800  /* Need convertCompoundSelectToSubquery() */
  2.3274 -#define SF_MinMaxAgg       0x1000  /* Aggregate containing min() or max() */
  2.3275 -#define SF_Recursive       0x2000  /* The recursive part of a recursive CTE */
  2.3276 -#define SF_Converted       0x4000  /* By convertCompoundSelectToSubquery() */
  2.3277 -#define SF_IncludeHidden   0x8000  /* Include hidden columns in output */
  2.3278 +**
  2.3279 +** Value constraints (all checked via assert())
  2.3280 +**     SF_HasAgg     == NC_HasAgg
  2.3281 +**     SF_MinMaxAgg  == NC_MinMaxAgg     == SQLITE_FUNC_MINMAX
  2.3282 +**     SF_FixedLimit == WHERE_USE_LIMIT
  2.3283 +*/
  2.3284 +#define SF_Distinct       0x00001  /* Output should be DISTINCT */
  2.3285 +#define SF_All            0x00002  /* Includes the ALL keyword */
  2.3286 +#define SF_Resolved       0x00004  /* Identifiers have been resolved */
  2.3287 +#define SF_Aggregate      0x00008  /* Contains agg functions or a GROUP BY */
  2.3288 +#define SF_HasAgg         0x00010  /* Contains aggregate functions */
  2.3289 +#define SF_UsesEphemeral  0x00020  /* Uses the OpenEphemeral opcode */
  2.3290 +#define SF_Expanded       0x00040  /* sqlite3SelectExpand() called on this */
  2.3291 +#define SF_HasTypeInfo    0x00080  /* FROM subqueries have Table metadata */
  2.3292 +#define SF_Compound       0x00100  /* Part of a compound query */
  2.3293 +#define SF_Values         0x00200  /* Synthesized from VALUES clause */
  2.3294 +#define SF_MultiValue     0x00400  /* Single VALUES term with multiple rows */
  2.3295 +#define SF_NestedFrom     0x00800  /* Part of a parenthesized FROM clause */
  2.3296 +#define SF_MinMaxAgg      0x01000  /* Aggregate containing min() or max() */
  2.3297 +#define SF_Recursive      0x02000  /* The recursive part of a recursive CTE */
  2.3298 +#define SF_FixedLimit     0x04000  /* nSelectRow set by a constant LIMIT */
  2.3299 +#define SF_MaybeConvert   0x08000  /* Need convertCompoundSelectToSubquery() */
  2.3300 +#define SF_Converted      0x10000  /* By convertCompoundSelectToSubquery() */
  2.3301 +#define SF_IncludeHidden  0x20000  /* Include hidden columns in output */
  2.3302  
  2.3303  
  2.3304  /*
  2.3305 @@ -13435,7 +15094,7 @@
  2.3306  ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  2.3307  ** Type".
  2.3308  **
  2.3309 -**     SRT_Union       Store results as a key in a temporary index 
  2.3310 +**     SRT_Union       Store results as a key in a temporary index
  2.3311  **                     identified by pDest->iSDParm.
  2.3312  **
  2.3313  **     SRT_Except      Remove results from the temporary index pDest->iSDParm.
  2.3314 @@ -13459,7 +15118,7 @@
  2.3315  **                     of the query.  This destination implies "LIMIT 1".
  2.3316  **
  2.3317  **     SRT_Set         The result must be a single column.  Store each
  2.3318 -**                     row of result as the key in table pDest->iSDParm. 
  2.3319 +**                     row of result as the key in table pDest->iSDParm.
  2.3320  **                     Apply the affinity pDest->affSdst before storing
  2.3321  **                     results.  Used to implement "IN (SELECT ...)".
  2.3322  **
  2.3323 @@ -13527,7 +15186,7 @@
  2.3324  };
  2.3325  
  2.3326  /*
  2.3327 -** During code generation of statements that do inserts into AUTOINCREMENT 
  2.3328 +** During code generation of statements that do inserts into AUTOINCREMENT
  2.3329  ** tables, the following information is attached to the Table.u.autoInc.p
  2.3330  ** pointer of each autoincrement table to record some side information that
  2.3331  ** the code generator needs.  We have to keep per-table autoincrement
  2.3332 @@ -13550,7 +15209,7 @@
  2.3333  #endif
  2.3334  
  2.3335  /*
  2.3336 -** At least one instance of the following structure is created for each 
  2.3337 +** At least one instance of the following structure is created for each
  2.3338  ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
  2.3339  ** statement. All such objects are stored in the linked list headed at
  2.3340  ** Parse.pTriggerPrg and deleted once statement compilation has been
  2.3341 @@ -13563,7 +15222,7 @@
  2.3342  ** values for both pTrigger and orconf.
  2.3343  **
  2.3344  ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
  2.3345 -** accessed (or set to 0 for triggers fired as a result of INSERT 
  2.3346 +** accessed (or set to 0 for triggers fired as a result of INSERT
  2.3347  ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
  2.3348  ** a mask of new.* columns used by the program.
  2.3349  */
  2.3350 @@ -13604,7 +15263,7 @@
  2.3351  ** is constant but the second part is reset at the beginning and end of
  2.3352  ** each recursion.
  2.3353  **
  2.3354 -** The nTableLock and aTableLock variables are only used if the shared-cache 
  2.3355 +** The nTableLock and aTableLock variables are only used if the shared-cache
  2.3356  ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
  2.3357  ** used to store the set of table-locks required by the statement being
  2.3358  ** compiled. Function sqlite3TableLock() is used to add entries to the
  2.3359 @@ -13624,6 +15283,7 @@
  2.3360    u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
  2.3361    u8 okConstFactor;    /* OK to factor out constants */
  2.3362    u8 disableLookaside; /* Number of times lookaside has been disabled */
  2.3363 +  u8 nColCache;        /* Number of entries in aColCache[] */
  2.3364    int aTempReg[8];     /* Holding area for temporary registers */
  2.3365    int nRangeReg;       /* Size of the temporary register block */
  2.3366    int iRangeReg;       /* First register in temporary register block */
  2.3367 @@ -13737,6 +15397,15 @@
  2.3368  
  2.3369  /*
  2.3370  ** Bitfield flags for P5 value in various opcodes.
  2.3371 +**
  2.3372 +** Value constraints (enforced via assert()):
  2.3373 +**    OPFLAG_LENGTHARG    == SQLITE_FUNC_LENGTH
  2.3374 +**    OPFLAG_TYPEOFARG    == SQLITE_FUNC_TYPEOF
  2.3375 +**    OPFLAG_BULKCSR      == BTREE_BULKLOAD
  2.3376 +**    OPFLAG_SEEKEQ       == BTREE_SEEK_EQ
  2.3377 +**    OPFLAG_FORDELETE    == BTREE_FORDELETE
  2.3378 +**    OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
  2.3379 +**    OPFLAG_AUXDELETE    == BTREE_AUXDELETE
  2.3380  */
  2.3381  #define OPFLAG_NCHANGE       0x01    /* OP_Insert: Set to update db->nChange */
  2.3382                                       /* Also used in P2 (not P5) of OP_Delete */
  2.3383 @@ -13745,6 +15414,9 @@
  2.3384  #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
  2.3385  #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
  2.3386  #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
  2.3387 +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  2.3388 +#define OPFLAG_ISNOOP        0x40    /* OP_Delete does pre-update-hook only */
  2.3389 +#endif
  2.3390  #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
  2.3391  #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
  2.3392  #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
  2.3393 @@ -13757,10 +15429,10 @@
  2.3394  
  2.3395  /*
  2.3396   * Each trigger present in the database schema is stored as an instance of
  2.3397 - * struct Trigger. 
  2.3398 + * struct Trigger.
  2.3399   *
  2.3400   * Pointers to instances of struct Trigger are stored in two ways.
  2.3401 - * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
  2.3402 + * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
  2.3403   *    database). This allows Trigger structures to be retrieved by name.
  2.3404   * 2. All triggers associated with a single table form a linked list, using the
  2.3405   *    pNext member of struct Trigger. A pointer to the first element of the
  2.3406 @@ -13786,7 +15458,7 @@
  2.3407  
  2.3408  /*
  2.3409  ** A trigger is either a BEFORE or an AFTER trigger.  The following constants
  2.3410 -** determine which. 
  2.3411 +** determine which.
  2.3412  **
  2.3413  ** If there are multiple triggers, you might of some BEFORE and some AFTER.
  2.3414  ** In that cases, the constants below can be ORed together.
  2.3415 @@ -13796,15 +15468,15 @@
  2.3416  
  2.3417  /*
  2.3418   * An instance of struct TriggerStep is used to store a single SQL statement
  2.3419 - * that is a part of a trigger-program. 
  2.3420 + * that is a part of a trigger-program.
  2.3421   *
  2.3422   * Instances of struct TriggerStep are stored in a singly linked list (linked
  2.3423 - * using the "pNext" member) referenced by the "step_list" member of the 
  2.3424 + * using the "pNext" member) referenced by the "step_list" member of the
  2.3425   * associated struct Trigger instance. The first element of the linked list is
  2.3426   * the first step of the trigger-program.
  2.3427 - * 
  2.3428 + *
  2.3429   * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
  2.3430 - * "SELECT" statement. The meanings of the other members is determined by the 
  2.3431 + * "SELECT" statement. The meanings of the other members is determined by the
  2.3432   * value of "op" as follows:
  2.3433   *
  2.3434   * (op == TK_INSERT)
  2.3435 @@ -13814,7 +15486,7 @@
  2.3436   * zTarget   -> Dequoted name of the table to insert into.
  2.3437   * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
  2.3438   *              this stores values to be inserted. Otherwise NULL.
  2.3439 - * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ... 
  2.3440 + * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ...
  2.3441   *              statement, then this stores the column-names to be
  2.3442   *              inserted into.
  2.3443   *
  2.3444 @@ -13822,7 +15494,7 @@
  2.3445   * zTarget   -> Dequoted name of the table to delete from.
  2.3446   * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
  2.3447   *              Otherwise NULL.
  2.3448 - * 
  2.3449 + *
  2.3450   * (op == TK_UPDATE)
  2.3451   * zTarget   -> Dequoted name of the table to update.
  2.3452   * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
  2.3453 @@ -13830,7 +15502,7 @@
  2.3454   * pExprList -> A list of the columns to update and the expressions to update
  2.3455   *              them to. See sqlite3Update() documentation of "pChanges"
  2.3456   *              argument.
  2.3457 - * 
  2.3458 + *
  2.3459   */
  2.3460  struct TriggerStep {
  2.3461    u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
  2.3462 @@ -13848,7 +15520,7 @@
  2.3463  /*
  2.3464  ** The following structure contains information used by the sqliteFix...
  2.3465  ** routines as they walk the parse tree to make database references
  2.3466 -** explicit.  
  2.3467 +** explicit.
  2.3468  */
  2.3469  typedef struct DbFixer DbFixer;
  2.3470  struct DbFixer {
  2.3471 @@ -13909,6 +15581,7 @@
  2.3472    int neverCorrupt;                 /* Database is always well-formed */
  2.3473    int szLookaside;                  /* Default lookaside buffer size */
  2.3474    int nLookaside;                   /* Default lookaside buffer count */
  2.3475 +  int nStmtSpill;                   /* Stmt-journal spill-to-disk threshold */
  2.3476    sqlite3_mem_methods m;            /* Low-level memory allocation interface */
  2.3477    sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
  2.3478    sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
  2.3479 @@ -14058,6 +15731,15 @@
  2.3480  #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
  2.3481  #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
  2.3482  #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
  2.3483 +#ifdef SQLITE_DEBUG
  2.3484 +SQLITE_PRIVATE   int sqlite3NomemError(int);
  2.3485 +SQLITE_PRIVATE   int sqlite3IoerrnomemError(int);
  2.3486 +# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
  2.3487 +# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
  2.3488 +#else
  2.3489 +# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
  2.3490 +# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
  2.3491 +#endif
  2.3492  
  2.3493  /*
  2.3494  ** FTS3 and FTS4 both require virtual table support
  2.3495 @@ -14098,6 +15780,7 @@
  2.3496  # define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
  2.3497  # define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
  2.3498  # define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
  2.3499 +# define sqlite3Isquote(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
  2.3500  #else
  2.3501  # define sqlite3Toupper(x)   toupper((unsigned char)(x))
  2.3502  # define sqlite3Isspace(x)   isspace((unsigned char)(x))
  2.3503 @@ -14106,6 +15789,7 @@
  2.3504  # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
  2.3505  # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
  2.3506  # define sqlite3Tolower(x)   tolower((unsigned char)(x))
  2.3507 +# define sqlite3Isquote(x)   ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
  2.3508  #endif
  2.3509  #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  2.3510  SQLITE_PRIVATE int sqlite3IsIdChar(u8);
  2.3511 @@ -14114,8 +15798,9 @@
  2.3512  /*
  2.3513  ** Internal function prototypes
  2.3514  */
  2.3515 -#define sqlite3StrICmp sqlite3_stricmp
  2.3516 +SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
  2.3517  SQLITE_PRIVATE int sqlite3Strlen30(const char*);
  2.3518 +SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
  2.3519  #define sqlite3StrNICmp sqlite3_strnicmp
  2.3520  
  2.3521  SQLITE_PRIVATE int sqlite3MallocInit(void);
  2.3522 @@ -14154,7 +15839,7 @@
  2.3523  #ifdef SQLITE_USE_ALLOCA
  2.3524  # define sqlite3StackAllocRaw(D,N)   alloca(N)
  2.3525  # define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
  2.3526 -# define sqlite3StackFree(D,P)       
  2.3527 +# define sqlite3StackFree(D,P)
  2.3528  #else
  2.3529  # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
  2.3530  # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
  2.3531 @@ -14228,7 +15913,7 @@
  2.3532  
  2.3533  SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
  2.3534  SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
  2.3535 -SQLITE_PRIVATE int sqlite3Dequote(char*);
  2.3536 +SQLITE_PRIVATE void sqlite3Dequote(char*);
  2.3537  SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
  2.3538  SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
  2.3539  SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
  2.3540 @@ -14238,10 +15923,14 @@
  2.3541  SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
  2.3542  SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
  2.3543  SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
  2.3544 +#ifdef SQLITE_DEBUG
  2.3545 +SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int);
  2.3546 +#endif
  2.3547  SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
  2.3548  SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
  2.3549  SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
  2.3550  SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
  2.3551 +SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
  2.3552  SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
  2.3553  SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
  2.3554  SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
  2.3555 @@ -14261,6 +15950,7 @@
  2.3556  SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
  2.3557  SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*);
  2.3558  SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
  2.3559 +SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*);
  2.3560  SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
  2.3561  SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
  2.3562  SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
  2.3563 @@ -14271,11 +15961,10 @@
  2.3564  #else
  2.3565  # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
  2.3566  #endif
  2.3567 -SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
  2.3568 +SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*,Token*);
  2.3569  SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
  2.3570  SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
  2.3571  SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
  2.3572 -SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
  2.3573  SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
  2.3574  SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
  2.3575  SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
  2.3576 @@ -14349,7 +16038,7 @@
  2.3577  SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
  2.3578  SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
  2.3579  SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
  2.3580 -                         Expr*,ExprList*,u16,Expr*,Expr*);
  2.3581 +                         Expr*,ExprList*,u32,Expr*,Expr*);
  2.3582  SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
  2.3583  SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
  2.3584  SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
  2.3585 @@ -14361,7 +16050,7 @@
  2.3586  SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
  2.3587  SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
  2.3588  SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
  2.3589 -SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
  2.3590 +SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
  2.3591  SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
  2.3592  SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
  2.3593  SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
  2.3594 @@ -14461,11 +16150,11 @@
  2.3595  #else
  2.3596  # define sqlite3SelectSetName(A,B)
  2.3597  #endif
  2.3598 -SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
  2.3599 -SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
  2.3600 -SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
  2.3601 +SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
  2.3602 +SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
  2.3603 +SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
  2.3604  SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
  2.3605 -SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
  2.3606 +SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
  2.3607  SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
  2.3608  SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
  2.3609  SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
  2.3610 @@ -14544,7 +16233,11 @@
  2.3611  #ifndef SQLITE_OMIT_VIRTUALTABLE
  2.3612  SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
  2.3613  #endif
  2.3614 +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  2.3615 +    defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
  2.3616 +    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  2.3617  SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
  2.3618 +#endif
  2.3619  
  2.3620  /*
  2.3621  ** Routines to read and write variable-length integers.  These used to
  2.3622 @@ -14579,6 +16272,7 @@
  2.3623  SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
  2.3624  SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
  2.3625  SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
  2.3626 +SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
  2.3627  SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  2.3628  SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
  2.3629  SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  2.3630 @@ -14611,7 +16305,7 @@
  2.3631  
  2.3632  SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
  2.3633  SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
  2.3634 -SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
  2.3635 +SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
  2.3636                          void(*)(void*));
  2.3637  SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
  2.3638  SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
  2.3639 @@ -14626,7 +16320,7 @@
  2.3640  SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
  2.3641  SQLITE_PRIVATE const Token sqlite3IntTokens[];
  2.3642  SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
  2.3643 -SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
  2.3644 +SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
  2.3645  #ifndef SQLITE_OMIT_WSD
  2.3646  SQLITE_PRIVATE int sqlite3PendingByte;
  2.3647  #endif
  2.3648 @@ -14671,7 +16365,7 @@
  2.3649  #ifdef SQLITE_DEBUG
  2.3650  SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
  2.3651  #endif
  2.3652 -SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
  2.3653 +SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
  2.3654    void (*)(sqlite3_context*,int,sqlite3_value **),
  2.3655    void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
  2.3656    FuncDestructor *pDestructor
  2.3657 @@ -14734,7 +16428,7 @@
  2.3658  #  define sqlite3VtabRollback(X)
  2.3659  #  define sqlite3VtabCommit(X)
  2.3660  #  define sqlite3VtabInSync(db) 0
  2.3661 -#  define sqlite3VtabLock(X) 
  2.3662 +#  define sqlite3VtabLock(X)
  2.3663  #  define sqlite3VtabUnlock(X)
  2.3664  #  define sqlite3VtabUnlockList(X)
  2.3665  #  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
  2.3666 @@ -14792,7 +16486,7 @@
  2.3667  ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
  2.3668  ** key functionality is available. If OMIT_TRIGGER is defined but
  2.3669  ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
  2.3670 -** this case foreign keys are parsed, but no other functionality is 
  2.3671 +** this case foreign keys are parsed, but no other functionality is
  2.3672  ** provided (enforcement of FK constraints requires the triggers sub-system).
  2.3673  */
  2.3674  #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
  2.3675 @@ -14853,19 +16547,14 @@
  2.3676  #define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
  2.3677  SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
  2.3678  
  2.3679 +SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
  2.3680 +SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
  2.3681  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  2.3682 -SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
  2.3683 -SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
  2.3684  SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
  2.3685 -SQLITE_PRIVATE   int sqlite3JournalExists(sqlite3_file *p);
  2.3686 -#else
  2.3687 -  #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
  2.3688 -  #define sqlite3JournalExists(p) 1
  2.3689 -#endif
  2.3690 -
  2.3691 +#endif
  2.3692 +
  2.3693 +SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p);
  2.3694  SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
  2.3695 -SQLITE_PRIVATE int sqlite3MemJournalSize(void);
  2.3696 -SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
  2.3697  
  2.3698  SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
  2.3699  #if SQLITE_MAX_EXPR_DEPTH>0
  2.3700 @@ -14896,7 +16585,7 @@
  2.3701  /*
  2.3702  ** If the SQLITE_ENABLE IOTRACE exists then the global variable
  2.3703  ** sqlite3IoTrace is a pointer to a printf-like routine used to
  2.3704 -** print I/O tracing messages. 
  2.3705 +** print I/O tracing messages.
  2.3706  */
  2.3707  #ifdef SQLITE_ENABLE_IOTRACE
  2.3708  # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
  2.3709 @@ -14930,7 +16619,7 @@
  2.3710  ** that allocations that might have been satisfied by lookaside are not
  2.3711  ** passed back to non-lookaside free() routines.  Asserts such as the
  2.3712  ** example above are placed on the non-lookaside free() routines to verify
  2.3713 -** this constraint. 
  2.3714 +** this constraint.
  2.3715  **
  2.3716  ** All of this is no-op for a production build.  It only comes into
  2.3717  ** play when the SQLITE_MEMDEBUG compile-time option is used.
  2.3718 @@ -15037,6 +16726,7 @@
  2.3719  **   isxdigit()                       0x08
  2.3720  **   toupper()                        0x20
  2.3721  **   SQLite identifier character      0x40
  2.3722 +**   Quote character                  0x80
  2.3723  **
  2.3724  ** Bit 0x20 is set if the mapped character requires translation to upper
  2.3725  ** case. i.e. if the character is a lower-case ASCII character.
  2.3726 @@ -15062,7 +16752,7 @@
  2.3727    0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
  2.3728    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
  2.3729    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
  2.3730 -  0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
  2.3731 +  0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80,  /* 20..27     !"#$%&' */
  2.3732    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
  2.3733    0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
  2.3734    0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
  2.3735 @@ -15070,8 +16760,8 @@
  2.3736    0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
  2.3737    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
  2.3738    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
  2.3739 -  0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
  2.3740 -  0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
  2.3741 +  0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
  2.3742 +  0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
  2.3743    0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
  2.3744    0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
  2.3745    0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
  2.3746 @@ -15126,6 +16816,18 @@
  2.3747  # define SQLITE_SORTER_PMASZ 250
  2.3748  #endif
  2.3749  
  2.3750 +/* Statement journals spill to disk when their size exceeds the following
  2.3751 +** threashold (in bytes). 0 means that statement journals are created and
  2.3752 +** written to disk immediately (the default behavior for SQLite versions
  2.3753 +** before 3.12.0).  -1 means always keep the entire statement journal in
  2.3754 +** memory.  (The statement journal is also always held entirely in memory
  2.3755 +** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
  2.3756 +** setting.)
  2.3757 +*/
  2.3758 +#ifndef SQLITE_STMTJRNL_SPILL 
  2.3759 +# define SQLITE_STMTJRNL_SPILL (64*1024)
  2.3760 +#endif
  2.3761 +
  2.3762  /*
  2.3763  ** The following singleton contains the global configuration for
  2.3764  ** the SQLite library.
  2.3765 @@ -15140,6 +16842,7 @@
  2.3766     0,                         /* neverCorrupt */
  2.3767     128,                       /* szLookaside */
  2.3768     500,                       /* nLookaside */
  2.3769 +   SQLITE_STMTJRNL_SPILL,     /* nStmtSpill */
  2.3770     {0,0,0,0,0,0,0,0},         /* m */
  2.3771     {0,0,0,0,0,0,0,0,0},       /* mutex */
  2.3772     {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
  2.3773 @@ -15186,7 +16889,7 @@
  2.3774  ** database connections.  After initialization, this table is
  2.3775  ** read-only.
  2.3776  */
  2.3777 -SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
  2.3778 +SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
  2.3779  
  2.3780  /*
  2.3781  ** Constant tokens for values 0 and 1.
  2.3782 @@ -15787,7 +17490,7 @@
  2.3783  #endif
  2.3784    Bool isEphemeral:1;   /* True for an ephemeral table */
  2.3785    Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
  2.3786 -  Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
  2.3787 +  Bool isOrdered:1;     /* True if the table is not BTREE_UNORDERED */
  2.3788    Pgno pgnoRoot;        /* Root page of the open btree cursor */
  2.3789    i16 nField;           /* Number of fields in the header */
  2.3790    u16 nHdrParsed;       /* Number of header fields parsed so far */
  2.3791 @@ -15861,6 +17564,7 @@
  2.3792    VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
  2.3793    void *token;            /* Copy of SubProgram.token */
  2.3794    i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
  2.3795 +  AuxData *pAuxData;      /* Linked list of auxdata allocations */
  2.3796    int nCursor;            /* Number of entries in apCsr */
  2.3797    int pc;                 /* Program Counter in parent (calling) frame */
  2.3798    int nOp;                /* Size of aOp array */
  2.3799 @@ -16080,16 +17784,16 @@
  2.3800  #endif
  2.3801    u16 nResColumn;         /* Number of columns in one row of the result set */
  2.3802    u8 errorAction;         /* Recovery action to do in case of an error */
  2.3803 +  bft expired:1;          /* True if the VM needs to be recompiled */
  2.3804 +  bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
  2.3805    u8 minWriteFileFormat;  /* Minimum file format for writable database files */
  2.3806    bft explain:2;          /* True if EXPLAIN present on SQL command */
  2.3807    bft changeCntOn:1;      /* True to update the change-counter */
  2.3808 -  bft expired:1;          /* True if the VM needs to be recompiled */
  2.3809    bft runOnlyOnce:1;      /* Automatically expire on reset */
  2.3810    bft usesStmtJournal:1;  /* True if uses a statement journal */
  2.3811    bft readOnly:1;         /* True for statements that do not write */
  2.3812    bft bIsReader:1;        /* True for statements that read */
  2.3813    bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
  2.3814 -  bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
  2.3815    int nChange;            /* Number of db changes made since last reset */
  2.3816    yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
  2.3817    yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
  2.3818 @@ -16128,6 +17832,25 @@
  2.3819  #define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
  2.3820  
  2.3821  /*
  2.3822 +** Structure used to store the context required by the 
  2.3823 +** sqlite3_preupdate_*() API functions.
  2.3824 +*/
  2.3825 +struct PreUpdate {
  2.3826 +  Vdbe *v;
  2.3827 +  VdbeCursor *pCsr;               /* Cursor to read old values from */
  2.3828 +  int op;                         /* One of SQLITE_INSERT, UPDATE, DELETE */
  2.3829 +  u8 *aRecord;                    /* old.* database record */
  2.3830 +  KeyInfo keyinfo;
  2.3831 +  UnpackedRecord *pUnpacked;      /* Unpacked version of aRecord[] */
  2.3832 +  UnpackedRecord *pNewUnpacked;   /* Unpacked version of new.* record */
  2.3833 +  int iNewReg;                    /* Register for new.* values */
  2.3834 +  i64 iKey1;                      /* First key value passed to hook */
  2.3835 +  i64 iKey2;                      /* Second key value passed to hook */
  2.3836 +  int iPKey;                      /* If not negative index of IPK column */
  2.3837 +  Mem *aNew;                      /* Array of new.* values */
  2.3838 +};
  2.3839 +
  2.3840 +/*
  2.3841  ** Function prototypes
  2.3842  */
  2.3843  SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
  2.3844 @@ -16143,7 +17866,7 @@
  2.3845  SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int, u32*);
  2.3846  SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
  2.3847  SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
  2.3848 -SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
  2.3849 +SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
  2.3850  
  2.3851  int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
  2.3852  SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
  2.3853 @@ -16186,6 +17909,9 @@
  2.3854  SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
  2.3855  SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
  2.3856  SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
  2.3857 +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  2.3858 +SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int);
  2.3859 +#endif
  2.3860  SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
  2.3861  
  2.3862  SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
  2.3863 @@ -16623,6 +18349,15 @@
  2.3864  
  2.3865  #ifndef SQLITE_OMIT_DATETIME_FUNCS
  2.3866  
  2.3867 +/*
  2.3868 +** The MSVC CRT on Windows CE may not have a localtime() function.
  2.3869 +** So declare a substitute.  The substitute function itself is
  2.3870 +** defined in "os_win.c".
  2.3871 +*/
  2.3872 +#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
  2.3873 +    (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
  2.3874 +struct tm *__cdecl localtime(const time_t *);
  2.3875 +#endif
  2.3876  
  2.3877  /*
  2.3878  ** A structure for holding a single date and time.
  2.3879 @@ -16991,6 +18726,7 @@
  2.3880    p->validTZ = 0;
  2.3881  }
  2.3882  
  2.3883 +#ifndef SQLITE_OMIT_LOCALTIME
  2.3884  /*
  2.3885  ** On recent Windows platforms, the localtime_s() function is available
  2.3886  ** as part of the "Secure CRT". It is essentially equivalent to 
  2.3887 @@ -17009,7 +18745,6 @@
  2.3888  #define HAVE_LOCALTIME_S 1
  2.3889  #endif
  2.3890  
  2.3891 -#ifndef SQLITE_OMIT_LOCALTIME
  2.3892  /*
  2.3893  ** The following routine implements the rough equivalent of localtime_r()
  2.3894  ** using whatever operating-system specific localtime facility that
  2.3895 @@ -17709,7 +19444,7 @@
  2.3896  ** external linkage.
  2.3897  */
  2.3898  SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
  2.3899 -  static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
  2.3900 +  static FuncDef aDateTimeFuncs[] = {
  2.3901  #ifndef SQLITE_OMIT_DATETIME_FUNCS
  2.3902      DFUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
  2.3903      DFUNCTION(date,             -1, 0, 0, dateFunc      ),
  2.3904 @@ -17725,13 +19460,7 @@
  2.3905      STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
  2.3906  #endif
  2.3907    };
  2.3908 -  int i;
  2.3909 -  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
  2.3910 -  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
  2.3911 -
  2.3912 -  for(i=0; i<ArraySize(aDateTimeFuncs); i++){
  2.3913 -    sqlite3FuncDefInsert(pHash, &aFunc[i]);
  2.3914 -  }
  2.3915 +  sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs));
  2.3916  }
  2.3917  
  2.3918  /************** End of date.c ************************************************/
  2.3919 @@ -17804,9 +19533,9 @@
  2.3920  #if defined(SQLITE_TEST)
  2.3921  SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
  2.3922    #define DO_OS_MALLOC_TEST(x)                                       \
  2.3923 -  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
  2.3924 +  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
  2.3925      void *pTstAlloc = sqlite3Malloc(10);                             \
  2.3926 -    if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
  2.3927 +    if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT;                  \
  2.3928      sqlite3_free(pTstAlloc);                                         \
  2.3929    }
  2.3930  #else
  2.3931 @@ -17819,13 +19548,11 @@
  2.3932  ** of this would be completely automatic if SQLite were coded using
  2.3933  ** C++ instead of plain old C.
  2.3934  */
  2.3935 -SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
  2.3936 -  int rc = SQLITE_OK;
  2.3937 +SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file *pId){
  2.3938    if( pId->pMethods ){
  2.3939 -    rc = pId->pMethods->xClose(pId);
  2.3940 +    pId->pMethods->xClose(pId);
  2.3941      pId->pMethods = 0;
  2.3942    }
  2.3943 -  return rc;
  2.3944  }
  2.3945  SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
  2.3946    DO_OS_MALLOC_TEST(id);
  2.3947 @@ -18000,6 +19727,9 @@
  2.3948  SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
  2.3949    return pVfs->xSleep(pVfs, nMicro);
  2.3950  }
  2.3951 +SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
  2.3952 +  return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
  2.3953 +}
  2.3954  SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
  2.3955    int rc;
  2.3956    /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
  2.3957 @@ -18025,7 +19755,7 @@
  2.3958    int flags,
  2.3959    int *pOutFlags
  2.3960  ){
  2.3961 -  int rc = SQLITE_NOMEM;
  2.3962 +  int rc;
  2.3963    sqlite3_file *pFile;
  2.3964    pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
  2.3965    if( pFile ){
  2.3966 @@ -18035,15 +19765,15 @@
  2.3967      }else{
  2.3968        *ppFile = pFile;
  2.3969      }
  2.3970 -  }
  2.3971 -  return rc;
  2.3972 -}
  2.3973 -SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
  2.3974 -  int rc = SQLITE_OK;
  2.3975 +  }else{
  2.3976 +    rc = SQLITE_NOMEM_BKPT;
  2.3977 +  }
  2.3978 +  return rc;
  2.3979 +}
  2.3980 +SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){
  2.3981    assert( pFile );
  2.3982 -  rc = sqlite3OsClose(pFile);
  2.3983 +  sqlite3OsClose(pFile);
  2.3984    sqlite3_free(pFile);
  2.3985 -  return rc;
  2.3986  }
  2.3987  
  2.3988  /*
  2.3989 @@ -18054,7 +19784,7 @@
  2.3990  */
  2.3991  SQLITE_PRIVATE int sqlite3OsInit(void){
  2.3992    void *p = sqlite3_malloc(10);
  2.3993 -  if( p==0 ) return SQLITE_NOMEM;
  2.3994 +  if( p==0 ) return SQLITE_NOMEM_BKPT;
  2.3995    sqlite3_free(p);
  2.3996    return sqlite3_os_init();
  2.3997  }
  2.3998 @@ -22676,7 +24406,7 @@
  2.3999  static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
  2.4000    sqlite3OomClear(db);
  2.4001    sqlite3Error(db, SQLITE_NOMEM);
  2.4002 -  return SQLITE_NOMEM;
  2.4003 +  return SQLITE_NOMEM_BKPT;
  2.4004  }
  2.4005  
  2.4006  /*
  2.4007 @@ -22723,26 +24453,26 @@
  2.4008  ** Conversion types fall into various categories as defined by the
  2.4009  ** following enumeration.
  2.4010  */
  2.4011 -#define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
  2.4012 -#define etFLOAT       2 /* Floating point.  %f */
  2.4013 -#define etEXP         3 /* Exponentional notation. %e and %E */
  2.4014 -#define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
  2.4015 -#define etSIZE        5 /* Return number of characters processed so far. %n */
  2.4016 -#define etSTRING      6 /* Strings. %s */
  2.4017 -#define etDYNSTRING   7 /* Dynamically allocated strings. %z */
  2.4018 -#define etPERCENT     8 /* Percent symbol. %% */
  2.4019 -#define etCHARX       9 /* Characters. %c */
  2.4020 +#define etRADIX       0 /* Integer types.  %d, %x, %o, and so forth */
  2.4021 +#define etFLOAT       1 /* Floating point.  %f */
  2.4022 +#define etEXP         2 /* Exponentional notation. %e and %E */
  2.4023 +#define etGENERIC     3 /* Floating or exponential, depending on exponent. %g */
  2.4024 +#define etSIZE        4 /* Return number of characters processed so far. %n */
  2.4025 +#define etSTRING      5 /* Strings. %s */
  2.4026 +#define etDYNSTRING   6 /* Dynamically allocated strings. %z */
  2.4027 +#define etPERCENT     7 /* Percent symbol. %% */
  2.4028 +#define etCHARX       8 /* Characters. %c */
  2.4029  /* The rest are extensions, not normally found in printf() */
  2.4030 -#define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
  2.4031 -#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
  2.4032 +#define etSQLESCAPE   9 /* Strings with '\'' doubled.  %q */
  2.4033 +#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
  2.4034                            NULL pointers replaced by SQL NULL.  %Q */
  2.4035 -#define etTOKEN      12 /* a pointer to a Token structure */
  2.4036 -#define etSRCLIST    13 /* a pointer to a SrcList */
  2.4037 -#define etPOINTER    14 /* The %p conversion */
  2.4038 -#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
  2.4039 -#define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
  2.4040 -
  2.4041 -#define etINVALID     0 /* Any unrecognized conversion type */
  2.4042 +#define etTOKEN      11 /* a pointer to a Token structure */
  2.4043 +#define etSRCLIST    12 /* a pointer to a SrcList */
  2.4044 +#define etPOINTER    13 /* The %p conversion */
  2.4045 +#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
  2.4046 +#define etORDINAL    15 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
  2.4047 +
  2.4048 +#define etINVALID    16 /* Any unrecognized conversion type */
  2.4049  
  2.4050  
  2.4051  /*
  2.4052 @@ -22897,7 +24627,7 @@
  2.4053    etByte flag_long;          /* True if "l" flag is present */
  2.4054    etByte flag_longlong;      /* True if the "ll" flag is present */
  2.4055    etByte done;               /* Loop termination flag */
  2.4056 -  etByte xtype = 0;          /* Conversion paradigm */
  2.4057 +  etByte xtype = etINVALID;  /* Conversion paradigm */
  2.4058    u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
  2.4059    u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
  2.4060    char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
  2.4061 @@ -23935,9 +25665,10 @@
  2.4062      sqlite3TreeViewPush(pView, 1);
  2.4063    }
  2.4064    do{
  2.4065 -    sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
  2.4066 +    sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
  2.4067        ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
  2.4068 -      ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
  2.4069 +      ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags,
  2.4070 +      (int)p->nSelectRow
  2.4071      );
  2.4072      if( cnt++ ) sqlite3TreeViewPop(pView);
  2.4073      if( p->pPrior ){
  2.4074 @@ -24141,6 +25872,12 @@
  2.4075      case TK_ISNULL:  zUniOp = "ISNULL"; break;
  2.4076      case TK_NOTNULL: zUniOp = "NOTNULL"; break;
  2.4077  
  2.4078 +    case TK_SPAN: {
  2.4079 +      sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
  2.4080 +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  2.4081 +      break;
  2.4082 +    }
  2.4083 +
  2.4084      case TK_COLLATE: {
  2.4085        sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
  2.4086        sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  2.4087 @@ -24492,7 +26229,7 @@
  2.4088  
  2.4089    *ppThread = 0;
  2.4090    p = sqlite3Malloc(sizeof(*p));
  2.4091 -  if( p==0 ) return SQLITE_NOMEM;
  2.4092 +  if( p==0 ) return SQLITE_NOMEM_BKPT;
  2.4093    memset(p, 0, sizeof(*p));
  2.4094    p->xTask = xTask;
  2.4095    p->pIn = pIn;
  2.4096 @@ -24518,7 +26255,7 @@
  2.4097    int rc;
  2.4098  
  2.4099    assert( ppOut!=0 );
  2.4100 -  if( NEVER(p==0) ) return SQLITE_NOMEM;
  2.4101 +  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
  2.4102    if( p->done ){
  2.4103      *ppOut = p->pOut;
  2.4104      rc = SQLITE_OK;
  2.4105 @@ -24583,7 +26320,7 @@
  2.4106    assert( xTask!=0 );
  2.4107    *ppThread = 0;
  2.4108    p = sqlite3Malloc(sizeof(*p));
  2.4109 -  if( p==0 ) return SQLITE_NOMEM;
  2.4110 +  if( p==0 ) return SQLITE_NOMEM_BKPT;
  2.4111    /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a 
  2.4112    ** function that returns SQLITE_ERROR when passed the argument 200, that
  2.4113    ** forces worker threads to run sequentially and deterministically 
  2.4114 @@ -24615,7 +26352,7 @@
  2.4115    BOOL bRc;
  2.4116  
  2.4117    assert( ppOut!=0 );
  2.4118 -  if( NEVER(p==0) ) return SQLITE_NOMEM;
  2.4119 +  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
  2.4120    if( p->xTask==0 ){
  2.4121      /* assert( p->id==GetCurrentThreadId() ); */
  2.4122      rc = WAIT_OBJECT_0;
  2.4123 @@ -24663,7 +26400,7 @@
  2.4124    assert( xTask!=0 );
  2.4125    *ppThread = 0;
  2.4126    p = sqlite3Malloc(sizeof(*p));
  2.4127 -  if( p==0 ) return SQLITE_NOMEM;
  2.4128 +  if( p==0 ) return SQLITE_NOMEM_BKPT;
  2.4129    if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
  2.4130      p->xTask = xTask;
  2.4131      p->pIn = pIn;
  2.4132 @@ -24679,7 +26416,7 @@
  2.4133  SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
  2.4134  
  2.4135    assert( ppOut!=0 );
  2.4136 -  if( NEVER(p==0) ) return SQLITE_NOMEM;
  2.4137 +  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
  2.4138    if( p->xTask ){
  2.4139      *ppOut = p->xTask(p->pIn);
  2.4140    }else{
  2.4141 @@ -24690,7 +26427,7 @@
  2.4142  #if defined(SQLITE_TEST)
  2.4143    {
  2.4144      void *pTstAlloc = sqlite3Malloc(10);
  2.4145 -    if (!pTstAlloc) return SQLITE_NOMEM;
  2.4146 +    if (!pTstAlloc) return SQLITE_NOMEM_BKPT;
  2.4147      sqlite3_free(pTstAlloc);
  2.4148    }
  2.4149  #endif
  2.4150 @@ -24937,7 +26674,7 @@
  2.4151      rc = sqlite3VdbeMemMakeWriteable(pMem);
  2.4152      if( rc!=SQLITE_OK ){
  2.4153        assert( rc==SQLITE_NOMEM );
  2.4154 -      return SQLITE_NOMEM;
  2.4155 +      return SQLITE_NOMEM_BKPT;
  2.4156      }
  2.4157      zIn = (u8*)pMem->z;
  2.4158      zTerm = &zIn[pMem->n&~1];
  2.4159 @@ -24979,7 +26716,7 @@
  2.4160    zTerm = &zIn[pMem->n];
  2.4161    zOut = sqlite3DbMallocRaw(pMem->db, len);
  2.4162    if( !zOut ){
  2.4163 -    return SQLITE_NOMEM;
  2.4164 +    return SQLITE_NOMEM_BKPT;
  2.4165    }
  2.4166    z = zOut;
  2.4167  
  2.4168 @@ -25349,12 +27086,48 @@
  2.4169  }
  2.4170  
  2.4171  /*
  2.4172 +** Return the declared type of a column.  Or return zDflt if the column 
  2.4173 +** has no declared type.
  2.4174 +**
  2.4175 +** The column type is an extra string stored after the zero-terminator on
  2.4176 +** the column name if and only if the COLFLAG_HASTYPE flag is set.
  2.4177 +*/
  2.4178 +SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){
  2.4179 +  if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt;
  2.4180 +  return pCol->zName + strlen(pCol->zName) + 1;
  2.4181 +}
  2.4182 +
  2.4183 +/*
  2.4184 +** Helper function for sqlite3Error() - called rarely.  Broken out into
  2.4185 +** a separate routine to avoid unnecessary register saves on entry to
  2.4186 +** sqlite3Error().
  2.4187 +*/
  2.4188 +static SQLITE_NOINLINE void  sqlite3ErrorFinish(sqlite3 *db, int err_code){
  2.4189 +  if( db->pErr ) sqlite3ValueSetNull(db->pErr);
  2.4190 +  sqlite3SystemError(db, err_code);
  2.4191 +}
  2.4192 +
  2.4193 +/*
  2.4194  ** Set the current error code to err_code and clear any prior error message.
  2.4195 +** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
  2.4196 +** that would be appropriate.
  2.4197  */
  2.4198  SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
  2.4199    assert( db!=0 );
  2.4200    db->errCode = err_code;
  2.4201 -  if( db->pErr ) sqlite3ValueSetNull(db->pErr);
  2.4202 +  if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
  2.4203 +}
  2.4204 +
  2.4205 +/*
  2.4206 +** Load the sqlite3.iSysErrno field if that is an appropriate thing
  2.4207 +** to do based on the SQLite error code in rc.
  2.4208 +*/
  2.4209 +SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
  2.4210 +  if( rc==SQLITE_IOERR_NOMEM ) return;
  2.4211 +  rc &= 0xff;
  2.4212 +  if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
  2.4213 +    db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
  2.4214 +  }
  2.4215  }
  2.4216  
  2.4217  /*
  2.4218 @@ -25381,6 +27154,7 @@
  2.4219  SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
  2.4220    assert( db!=0 );
  2.4221    db->errCode = err_code;
  2.4222 +  sqlite3SystemError(db, err_code);
  2.4223    if( zFormat==0 ){
  2.4224      sqlite3Error(db, err_code);
  2.4225    }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
  2.4226 @@ -25444,18 +27218,13 @@
  2.4227  ** brackets from around identifiers.  For example:  "[a-b-c]" becomes
  2.4228  ** "a-b-c".
  2.4229  */
  2.4230 -SQLITE_PRIVATE int sqlite3Dequote(char *z){
  2.4231 +SQLITE_PRIVATE void sqlite3Dequote(char *z){
  2.4232    char quote;
  2.4233    int i, j;
  2.4234 -  if( z==0 ) return -1;
  2.4235 +  if( z==0 ) return;
  2.4236    quote = z[0];
  2.4237 -  switch( quote ){
  2.4238 -    case '\'':  break;
  2.4239 -    case '"':   break;
  2.4240 -    case '`':   break;                /* For MySQL compatibility */
  2.4241 -    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
  2.4242 -    default:    return -1;
  2.4243 -  }
  2.4244 +  if( !sqlite3Isquote(quote) ) return;
  2.4245 +  if( quote=='[' ) quote = ']';
  2.4246    for(i=1, j=0;; i++){
  2.4247      assert( z[i] );
  2.4248      if( z[i]==quote ){
  2.4249 @@ -25470,7 +27239,6 @@
  2.4250      }
  2.4251    }
  2.4252    z[j] = 0;
  2.4253 -  return j;
  2.4254  }
  2.4255  
  2.4256  /*
  2.4257 @@ -25495,16 +27263,25 @@
  2.4258  ** independence" that SQLite uses internally when comparing identifiers.
  2.4259  */
  2.4260  SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *zLeft, const char *zRight){
  2.4261 -  register unsigned char *a, *b;
  2.4262    if( zLeft==0 ){
  2.4263      return zRight ? -1 : 0;
  2.4264    }else if( zRight==0 ){
  2.4265      return 1;
  2.4266    }
  2.4267 +  return sqlite3StrICmp(zLeft, zRight);
  2.4268 +}
  2.4269 +SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
  2.4270 +  unsigned char *a, *b;
  2.4271 +  int c;
  2.4272    a = (unsigned char *)zLeft;
  2.4273    b = (unsigned char *)zRight;
  2.4274 -  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
  2.4275 -  return UpperToLower[*a] - UpperToLower[*b];
  2.4276 +  for(;;){
  2.4277 +    c = (int)UpperToLower[*a] - (int)UpperToLower[*b];
  2.4278 +    if( c || *a==0 ) break;
  2.4279 +    a++;
  2.4280 +    b++;
  2.4281 +  }
  2.4282 +  return c;
  2.4283  }
  2.4284  SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
  2.4285    register unsigned char *a, *b;
  2.4286 @@ -25554,7 +27331,7 @@
  2.4287    int eValid = 1;  /* True exponent is either not used or is well-formed */
  2.4288    double result;
  2.4289    int nDigits = 0;
  2.4290 -  int nonNum = 0;
  2.4291 +  int nonNum = 0;  /* True if input contains UTF16 with high byte non-zero */
  2.4292  
  2.4293    assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
  2.4294    *pResult = 0.0;   /* Default return value, in case of an error */
  2.4295 @@ -25567,7 +27344,7 @@
  2.4296      assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
  2.4297      for(i=3-enc; i<length && z[i]==0; i+=2){}
  2.4298      nonNum = i<length;
  2.4299 -    zEnd = z+i+enc-3;
  2.4300 +    zEnd = &z[i^1];
  2.4301      z += (enc&1);
  2.4302    }
  2.4303  
  2.4304 @@ -25583,9 +27360,6 @@
  2.4305      z+=incr;
  2.4306    }
  2.4307  
  2.4308 -  /* skip leading zeroes */
  2.4309 -  while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
  2.4310 -
  2.4311    /* copy max significant digits to significand */
  2.4312    while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
  2.4313      s = s*10 + (*z - '0');
  2.4314 @@ -25602,12 +27376,13 @@
  2.4315      z+=incr;
  2.4316      /* copy digits from after decimal to significand
  2.4317      ** (decrease exponent by d to shift decimal right) */
  2.4318 -    while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
  2.4319 -      s = s*10 + (*z - '0');
  2.4320 -      z+=incr, nDigits++, d--;
  2.4321 -    }
  2.4322 -    /* skip non-significant digits */
  2.4323 -    while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
  2.4324 +    while( z<zEnd && sqlite3Isdigit(*z) ){
  2.4325 +      if( s<((LARGEST_INT64-9)/10) ){
  2.4326 +        s = s*10 + (*z - '0');
  2.4327 +        d--;
  2.4328 +      }
  2.4329 +      z+=incr, nDigits++;
  2.4330 +    }
  2.4331    }
  2.4332    if( z>=zEnd ) goto do_atof_calc;
  2.4333  
  2.4334 @@ -25615,7 +27390,12 @@
  2.4335    if( *z=='e' || *z=='E' ){
  2.4336      z+=incr;
  2.4337      eValid = 0;
  2.4338 -    if( z>=zEnd ) goto do_atof_calc;
  2.4339 +
  2.4340 +    /* This branch is needed to avoid a (harmless) buffer overread.  The 
  2.4341 +    ** special comment alerts the mutation tester that the correct answer
  2.4342 +    ** is obtained even if the branch is omitted */
  2.4343 +    if( z>=zEnd ) goto do_atof_calc;              /*PREVENTS-HARMLESS-OVERREAD*/
  2.4344 +
  2.4345      /* get sign of exponent */
  2.4346      if( *z=='-' ){
  2.4347        esign = -1;
  2.4348 @@ -25632,9 +27412,7 @@
  2.4349    }
  2.4350  
  2.4351    /* skip trailing spaces */
  2.4352 -  if( nDigits && eValid ){
  2.4353 -    while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
  2.4354 -  }
  2.4355 +  while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
  2.4356  
  2.4357  do_atof_calc:
  2.4358    /* adjust exponent by d, and update sign */
  2.4359 @@ -25646,41 +27424,51 @@
  2.4360      esign = 1;
  2.4361    }
  2.4362  
  2.4363 -  /* if 0 significand */
  2.4364 -  if( !s ) {
  2.4365 -    /* In the IEEE 754 standard, zero is signed.
  2.4366 -    ** Add the sign if we've seen at least one digit */
  2.4367 -    result = (sign<0 && nDigits) ? -(double)0 : (double)0;
  2.4368 +  if( s==0 ) {
  2.4369 +    /* In the IEEE 754 standard, zero is signed. */
  2.4370 +    result = sign<0 ? -(double)0 : (double)0;
  2.4371    } else {
  2.4372 -    /* attempt to reduce exponent */
  2.4373 -    if( esign>0 ){
  2.4374 -      while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
  2.4375 -    }else{
  2.4376 -      while( !(s%10) && e>0 ) e--,s/=10;
  2.4377 +    /* Attempt to reduce exponent.
  2.4378 +    **
  2.4379 +    ** Branches that are not required for the correct answer but which only
  2.4380 +    ** help to obtain the correct answer faster are marked with special
  2.4381 +    ** comments, as a hint to the mutation tester.
  2.4382 +    */
  2.4383 +    while( e>0 ){                                       /*OPTIMIZATION-IF-TRUE*/
  2.4384 +      if( esign>0 ){
  2.4385 +        if( s>=(LARGEST_INT64/10) ) break;             /*OPTIMIZATION-IF-FALSE*/
  2.4386 +        s *= 10;
  2.4387 +      }else{
  2.4388 +        if( s%10!=0 ) break;                           /*OPTIMIZATION-IF-FALSE*/
  2.4389 +        s /= 10;
  2.4390 +      }
  2.4391 +      e--;
  2.4392      }
  2.4393  
  2.4394      /* adjust the sign of significand */
  2.4395      s = sign<0 ? -s : s;
  2.4396  
  2.4397 -    /* if exponent, scale significand as appropriate
  2.4398 -    ** and store in result. */
  2.4399 -    if( e ){
  2.4400 +    if( e==0 ){                                         /*OPTIMIZATION-IF-TRUE*/
  2.4401 +      result = (double)s;
  2.4402 +    }else{
  2.4403        LONGDOUBLE_TYPE scale = 1.0;
  2.4404        /* attempt to handle extremely small/large numbers better */
  2.4405 -      if( e>307 && e<342 ){
  2.4406 -        while( e%308 ) { scale *= 1.0e+1; e -= 1; }
  2.4407 -        if( esign<0 ){
  2.4408 -          result = s / scale;
  2.4409 -          result /= 1.0e+308;
  2.4410 -        }else{
  2.4411 -          result = s * scale;
  2.4412 -          result *= 1.0e+308;
  2.4413 -        }
  2.4414 -      }else if( e>=342 ){
  2.4415 -        if( esign<0 ){
  2.4416 -          result = 0.0*s;
  2.4417 -        }else{
  2.4418 -          result = 1e308*1e308*s;  /* Infinity */
  2.4419 +      if( e>307 ){                                      /*OPTIMIZATION-IF-TRUE*/
  2.4420 +        if( e<342 ){                                    /*OPTIMIZATION-IF-TRUE*/
  2.4421 +          while( e%308 ) { scale *= 1.0e+1; e -= 1; }
  2.4422 +          if( esign<0 ){
  2.4423 +            result = s / scale;
  2.4424 +            result /= 1.0e+308;
  2.4425 +          }else{
  2.4426 +            result = s * scale;
  2.4427 +            result *= 1.0e+308;
  2.4428 +          }
  2.4429 +        }else{ assert( e>=342 );
  2.4430 +          if( esign<0 ){
  2.4431 +            result = 0.0*s;
  2.4432 +          }else{
  2.4433 +            result = 1e308*1e308*s;  /* Infinity */
  2.4434 +          }
  2.4435          }
  2.4436        }else{
  2.4437          /* 1.0e+22 is the largest power of 10 than can be 
  2.4438 @@ -25693,8 +27481,6 @@
  2.4439            result = s * scale;
  2.4440          }
  2.4441        }
  2.4442 -    } else {
  2.4443 -      result = (double)s;
  2.4444      }
  2.4445    }
  2.4446  
  2.4447 @@ -25702,7 +27488,7 @@
  2.4448    *pResult = result;
  2.4449  
  2.4450    /* return true if number and no extra non-whitespace chracters after */
  2.4451 -  return z>=zEnd && nDigits>0 && eValid && nonNum==0;
  2.4452 +  return z==zEnd && nDigits>0 && eValid && nonNum==0;
  2.4453  #else
  2.4454    return !sqlite3Atoi64(z, pResult, length, enc);
  2.4455  #endif /* SQLITE_OMIT_FLOATING_POINT */
  2.4456 @@ -25764,7 +27550,7 @@
  2.4457    int neg = 0; /* assume positive */
  2.4458    int i;
  2.4459    int c = 0;
  2.4460 -  int nonNum = 0;
  2.4461 +  int nonNum = 0;  /* True if input contains UTF16 with high byte non-zero */
  2.4462    const char *zStart;
  2.4463    const char *zEnd = zNum + length;
  2.4464    assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
  2.4465 @@ -25775,7 +27561,7 @@
  2.4466      assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
  2.4467      for(i=3-enc; i<length && zNum[i]==0; i+=2){}
  2.4468      nonNum = i<length;
  2.4469 -    zEnd = zNum+i+enc-3;
  2.4470 +    zEnd = &zNum[i^1];
  2.4471      zNum += (enc&1);
  2.4472    }
  2.4473    while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
  2.4474 @@ -25802,8 +27588,11 @@
  2.4475    testcase( i==18 );
  2.4476    testcase( i==19 );
  2.4477    testcase( i==20 );
  2.4478 -  if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum)
  2.4479 -       || i>19*incr || nonNum ){
  2.4480 +  if( &zNum[i]<zEnd              /* Extra bytes at the end */
  2.4481 +   || (i==0 && zStart==zNum)     /* No digits */
  2.4482 +   || i>19*incr                  /* Too many digits */
  2.4483 +   || nonNum                     /* UTF16 with high-order bytes non-zero */
  2.4484 +  ){
  2.4485      /* zNum is empty or contains non-numeric text or is longer
  2.4486      ** than 19 digits (thus guaranteeing that it is too large) */
  2.4487      return 1;
  2.4488 @@ -25845,7 +27634,6 @@
  2.4489  #ifndef SQLITE_OMIT_HEX_INTEGER
  2.4490    if( z[0]=='0'
  2.4491     && (z[1]=='x' || z[1]=='X')
  2.4492 -   && sqlite3Isxdigit(z[2])
  2.4493    ){
  2.4494      u64 u = 0;
  2.4495      int i, k;
  2.4496 @@ -26315,7 +28103,7 @@
  2.4497  */
  2.4498  SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
  2.4499    int i;
  2.4500 -  for(i=1; (v >>= 7)!=0; i++){ assert( i<9 ); }
  2.4501 +  for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
  2.4502    return i;
  2.4503  }
  2.4504  
  2.4505 @@ -26346,10 +28134,12 @@
  2.4506  SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
  2.4507  #if SQLITE_BYTEORDER==4321
  2.4508    memcpy(p,&v,4);
  2.4509 -#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000
  2.4510 +#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
  2.4511 +    && defined(__GNUC__) && GCC_VERSION>=4003000
  2.4512    u32 x = __builtin_bswap32(v);
  2.4513    memcpy(p,&x,4);
  2.4514 -#elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300
  2.4515 +#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
  2.4516 +    && defined(_MSC_VER) && _MSC_VER>=1300
  2.4517    u32 x = _byteswap_ulong(v);
  2.4518    memcpy(p,&x,4);
  2.4519  #else
  2.4520 @@ -26605,7 +28395,7 @@
  2.4521      if( x<2 ) return 0;
  2.4522      while( x<8 ){  y -= 10; x <<= 1; }
  2.4523    }else{
  2.4524 -    while( x>255 ){ y += 40; x >>= 4; }
  2.4525 +    while( x>255 ){ y += 40; x >>= 4; }  /*OPTIMIZATION-IF-TRUE*/
  2.4526      while( x>15 ){  y += 10; x >>= 1; }
  2.4527    }
  2.4528    return a[x&7] + y - 10;
  2.4529 @@ -26628,21 +28418,32 @@
  2.4530  }
  2.4531  #endif /* SQLITE_OMIT_VIRTUALTABLE */
  2.4532  
  2.4533 +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  2.4534 +    defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
  2.4535 +    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  2.4536  /*
  2.4537  ** Convert a LogEst into an integer.
  2.4538 +**
  2.4539 +** Note that this routine is only used when one or more of various
  2.4540 +** non-standard compile-time options is enabled.
  2.4541  */
  2.4542  SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
  2.4543    u64 n;
  2.4544 -  if( x<10 ) return 1;
  2.4545    n = x%10;
  2.4546    x /= 10;
  2.4547    if( n>=5 ) n -= 2;
  2.4548    else if( n>=1 ) n -= 1;
  2.4549 -  if( x>=3 ){
  2.4550 -    return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3);
  2.4551 -  }
  2.4552 -  return (n+8)>>(3-x);
  2.4553 -}
  2.4554 +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  2.4555 +    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  2.4556 +  if( x>60 ) return (u64)LARGEST_INT64;
  2.4557 +#else
  2.4558 +  /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input
  2.4559 +  ** possible to this routine is 310, resulting in a maximum x of 31 */
  2.4560 +  assert( x<=60 );
  2.4561 +#endif
  2.4562 +  return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
  2.4563 +}
  2.4564 +#endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
  2.4565  
  2.4566  /************** End of util.c ************************************************/
  2.4567  /************** Begin file hash.c ********************************************/
  2.4568 @@ -26703,7 +28504,7 @@
  2.4569  static unsigned int strHash(const char *z){
  2.4570    unsigned int h = 0;
  2.4571    unsigned char c;
  2.4572 -  while( (c = (unsigned char)*z++)!=0 ){
  2.4573 +  while( (c = (unsigned char)*z++)!=0 ){     /*OPTIMIZATION-IF-TRUE*/
  2.4574      h = (h<<3) ^ h ^ sqlite3UpperToLower[c];
  2.4575    }
  2.4576    return h;
  2.4577 @@ -26796,7 +28597,7 @@
  2.4578    int count;                     /* Number of elements left to test */
  2.4579    unsigned int h;                /* The computed hash */
  2.4580  
  2.4581 -  if( pH->ht ){
  2.4582 +  if( pH->ht ){   /*OPTIMIZATION-IF-TRUE*/
  2.4583      struct _ht *pEntry;
  2.4584      h = strHash(pKey) % pH->htsize;
  2.4585      pEntry = &pH->ht[h];
  2.4586 @@ -26943,153 +28744,152 @@
  2.4587      /*  12 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
  2.4588      /*  13 */ "Goto"             OpHelp(""),
  2.4589      /*  14 */ "Gosub"            OpHelp(""),
  2.4590 -    /*  15 */ "Return"           OpHelp(""),
  2.4591 -    /*  16 */ "InitCoroutine"    OpHelp(""),
  2.4592 -    /*  17 */ "EndCoroutine"     OpHelp(""),
  2.4593 -    /*  18 */ "Yield"            OpHelp(""),
  2.4594 +    /*  15 */ "InitCoroutine"    OpHelp(""),
  2.4595 +    /*  16 */ "Yield"            OpHelp(""),
  2.4596 +    /*  17 */ "MustBeInt"        OpHelp(""),
  2.4597 +    /*  18 */ "Jump"             OpHelp(""),
  2.4598      /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
  2.4599 -    /*  20 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
  2.4600 -    /*  21 */ "Halt"             OpHelp(""),
  2.4601 -    /*  22 */ "Integer"          OpHelp("r[P2]=P1"),
  2.4602 -    /*  23 */ "Int64"            OpHelp("r[P2]=P4"),
  2.4603 -    /*  24 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
  2.4604 -    /*  25 */ "Null"             OpHelp("r[P2..P3]=NULL"),
  2.4605 -    /*  26 */ "SoftNull"         OpHelp("r[P1]=NULL"),
  2.4606 -    /*  27 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
  2.4607 -    /*  28 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
  2.4608 -    /*  29 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
  2.4609 -    /*  30 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
  2.4610 -    /*  31 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
  2.4611 -    /*  32 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
  2.4612 -    /*  33 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
  2.4613 -    /*  34 */ "CollSeq"          OpHelp(""),
  2.4614 -    /*  35 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
  2.4615 -    /*  36 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
  2.4616 -    /*  37 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
  2.4617 -    /*  38 */ "MustBeInt"        OpHelp(""),
  2.4618 -    /*  39 */ "RealAffinity"     OpHelp(""),
  2.4619 -    /*  40 */ "Cast"             OpHelp("affinity(r[P1])"),
  2.4620 -    /*  41 */ "Permutation"      OpHelp(""),
  2.4621 -    /*  42 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
  2.4622 -    /*  43 */ "Jump"             OpHelp(""),
  2.4623 -    /*  44 */ "Once"             OpHelp(""),
  2.4624 -    /*  45 */ "If"               OpHelp(""),
  2.4625 -    /*  46 */ "IfNot"            OpHelp(""),
  2.4626 -    /*  47 */ "Column"           OpHelp("r[P3]=PX"),
  2.4627 -    /*  48 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
  2.4628 -    /*  49 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
  2.4629 -    /*  50 */ "Count"            OpHelp("r[P2]=count()"),
  2.4630 -    /*  51 */ "ReadCookie"       OpHelp(""),
  2.4631 -    /*  52 */ "SetCookie"        OpHelp(""),
  2.4632 -    /*  53 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
  2.4633 -    /*  54 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
  2.4634 -    /*  55 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
  2.4635 -    /*  56 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
  2.4636 -    /*  57 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
  2.4637 -    /*  58 */ "SorterOpen"       OpHelp(""),
  2.4638 -    /*  59 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
  2.4639 -    /*  60 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
  2.4640 -    /*  61 */ "Close"            OpHelp(""),
  2.4641 -    /*  62 */ "ColumnsUsed"      OpHelp(""),
  2.4642 -    /*  63 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
  2.4643 -    /*  64 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
  2.4644 -    /*  65 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
  2.4645 -    /*  66 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
  2.4646 -    /*  67 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
  2.4647 -    /*  68 */ "NotFound"         OpHelp("key=r[P3@P4]"),
  2.4648 -    /*  69 */ "Found"            OpHelp("key=r[P3@P4]"),
  2.4649 -    /*  70 */ "NotExists"        OpHelp("intkey=r[P3]"),
  2.4650 -    /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
  2.4651 -    /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
  2.4652 -    /*  73 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
  2.4653 -    /*  74 */ "NewRowid"         OpHelp("r[P2]=rowid"),
  2.4654 -    /*  75 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
  2.4655 -    /*  76 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
  2.4656 -    /*  77 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
  2.4657 -    /*  78 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
  2.4658 -    /*  79 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
  2.4659 -    /*  80 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
  2.4660 -    /*  81 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
  2.4661 -    /*  82 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
  2.4662 -    /*  83 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
  2.4663 -    /*  84 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
  2.4664 -    /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
  2.4665 -    /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
  2.4666 -    /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
  2.4667 -    /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
  2.4668 -    /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
  2.4669 -    /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
  2.4670 -    /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
  2.4671 -    /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
  2.4672 -    /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
  2.4673 -    /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
  2.4674 -    /*  95 */ "Delete"           OpHelp(""),
  2.4675 -    /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
  2.4676 +    /*  20 */ "Once"             OpHelp(""),
  2.4677 +    /*  21 */ "If"               OpHelp(""),
  2.4678 +    /*  22 */ "IfNot"            OpHelp(""),
  2.4679 +    /*  23 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
  2.4680 +    /*  24 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
  2.4681 +    /*  25 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
  2.4682 +    /*  26 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
  2.4683 +    /*  27 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
  2.4684 +    /*  28 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
  2.4685 +    /*  29 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
  2.4686 +    /*  30 */ "NotFound"         OpHelp("key=r[P3@P4]"),
  2.4687 +    /*  31 */ "Found"            OpHelp("key=r[P3@P4]"),
  2.4688 +    /*  32 */ "NotExists"        OpHelp("intkey=r[P3]"),
  2.4689 +    /*  33 */ "Last"             OpHelp(""),
  2.4690 +    /*  34 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
  2.4691 +    /*  35 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
  2.4692 +    /*  36 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
  2.4693 +    /*  37 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
  2.4694 +    /*  38 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
  2.4695 +    /*  39 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
  2.4696 +    /*  40 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
  2.4697 +    /*  41 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
  2.4698 +    /*  42 */ "SorterSort"       OpHelp(""),
  2.4699 +    /*  43 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
  2.4700 +    /*  44 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
  2.4701 +    /*  45 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
  2.4702 +    /*  46 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
  2.4703 +    /*  47 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
  2.4704 +    /*  48 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
  2.4705 +    /*  49 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
  2.4706 +    /*  50 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
  2.4707 +    /*  51 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
  2.4708 +    /*  52 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
  2.4709 +    /*  53 */ "Sort"             OpHelp(""),
  2.4710 +    /*  54 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
  2.4711 +    /*  55 */ "Rewind"           OpHelp(""),
  2.4712 +    /*  56 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
  2.4713 +    /*  57 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
  2.4714 +    /*  58 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
  2.4715 +    /*  59 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
  2.4716 +    /*  60 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
  2.4717 +    /*  61 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
  2.4718 +    /*  62 */ "Program"          OpHelp(""),
  2.4719 +    /*  63 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
  2.4720 +    /*  64 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
  2.4721 +    /*  65 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
  2.4722 +    /*  66 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
  2.4723 +    /*  67 */ "IncrVacuum"       OpHelp(""),
  2.4724 +    /*  68 */ "VNext"            OpHelp(""),
  2.4725 +    /*  69 */ "Init"             OpHelp("Start at P2"),
  2.4726 +    /*  70 */ "Return"           OpHelp(""),
  2.4727 +    /*  71 */ "EndCoroutine"     OpHelp(""),
  2.4728 +    /*  72 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
  2.4729 +    /*  73 */ "Halt"             OpHelp(""),
  2.4730 +    /*  74 */ "Integer"          OpHelp("r[P2]=P1"),
  2.4731 +    /*  75 */ "Int64"            OpHelp("r[P2]=P4"),
  2.4732 +    /*  76 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
  2.4733 +    /*  77 */ "Null"             OpHelp("r[P2..P3]=NULL"),
  2.4734 +    /*  78 */ "SoftNull"         OpHelp("r[P1]=NULL"),
  2.4735 +    /*  79 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
  2.4736 +    /*  80 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
  2.4737 +    /*  81 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
  2.4738 +    /*  82 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
  2.4739 +    /*  83 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
  2.4740 +    /*  84 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
  2.4741 +    /*  85 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
  2.4742 +    /*  86 */ "CollSeq"          OpHelp(""),
  2.4743 +    /*  87 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
  2.4744 +    /*  88 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
  2.4745 +    /*  89 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
  2.4746 +    /*  90 */ "RealAffinity"     OpHelp(""),
  2.4747 +    /*  91 */ "Cast"             OpHelp("affinity(r[P1])"),
  2.4748 +    /*  92 */ "Permutation"      OpHelp(""),
  2.4749 +    /*  93 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
  2.4750 +    /*  94 */ "Column"           OpHelp("r[P3]=PX"),
  2.4751 +    /*  95 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
  2.4752 +    /*  96 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
  2.4753      /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
  2.4754 -    /*  98 */ "ResetCount"       OpHelp(""),
  2.4755 -    /*  99 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
  2.4756 -    /* 100 */ "SorterData"       OpHelp("r[P2]=data"),
  2.4757 -    /* 101 */ "RowKey"           OpHelp("r[P2]=key"),
  2.4758 -    /* 102 */ "RowData"          OpHelp("r[P2]=data"),
  2.4759 -    /* 103 */ "Rowid"            OpHelp("r[P2]=rowid"),
  2.4760 -    /* 104 */ "NullRow"          OpHelp(""),
  2.4761 -    /* 105 */ "Last"             OpHelp(""),
  2.4762 -    /* 106 */ "SorterSort"       OpHelp(""),
  2.4763 -    /* 107 */ "Sort"             OpHelp(""),
  2.4764 -    /* 108 */ "Rewind"           OpHelp(""),
  2.4765 -    /* 109 */ "SorterInsert"     OpHelp(""),
  2.4766 -    /* 110 */ "IdxInsert"        OpHelp("key=r[P2]"),
  2.4767 -    /* 111 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
  2.4768 -    /* 112 */ "Seek"             OpHelp("Move P3 to P1.rowid"),
  2.4769 -    /* 113 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
  2.4770 -    /* 114 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
  2.4771 -    /* 115 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
  2.4772 -    /* 116 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
  2.4773 -    /* 117 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
  2.4774 -    /* 118 */ "Destroy"          OpHelp(""),
  2.4775 -    /* 119 */ "Clear"            OpHelp(""),
  2.4776 -    /* 120 */ "ResetSorter"      OpHelp(""),
  2.4777 -    /* 121 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
  2.4778 -    /* 122 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
  2.4779 -    /* 123 */ "ParseSchema"      OpHelp(""),
  2.4780 -    /* 124 */ "LoadAnalysis"     OpHelp(""),
  2.4781 -    /* 125 */ "DropTable"        OpHelp(""),
  2.4782 -    /* 126 */ "DropIndex"        OpHelp(""),
  2.4783 -    /* 127 */ "DropTrigger"      OpHelp(""),
  2.4784 -    /* 128 */ "IntegrityCk"      OpHelp(""),
  2.4785 -    /* 129 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
  2.4786 -    /* 130 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
  2.4787 -    /* 131 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
  2.4788 -    /* 132 */ "Program"          OpHelp(""),
  2.4789 +    /*  98 */ "Count"            OpHelp("r[P2]=count()"),
  2.4790 +    /*  99 */ "ReadCookie"       OpHelp(""),
  2.4791 +    /* 100 */ "SetCookie"        OpHelp(""),
  2.4792 +    /* 101 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
  2.4793 +    /* 102 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
  2.4794 +    /* 103 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
  2.4795 +    /* 104 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
  2.4796 +    /* 105 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
  2.4797 +    /* 106 */ "SorterOpen"       OpHelp(""),
  2.4798 +    /* 107 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
  2.4799 +    /* 108 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
  2.4800 +    /* 109 */ "Close"            OpHelp(""),
  2.4801 +    /* 110 */ "ColumnsUsed"      OpHelp(""),
  2.4802 +    /* 111 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
  2.4803 +    /* 112 */ "NewRowid"         OpHelp("r[P2]=rowid"),
  2.4804 +    /* 113 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
  2.4805 +    /* 114 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
  2.4806 +    /* 115 */ "Delete"           OpHelp(""),
  2.4807 +    /* 116 */ "ResetCount"       OpHelp(""),
  2.4808 +    /* 117 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
  2.4809 +    /* 118 */ "SorterData"       OpHelp("r[P2]=data"),
  2.4810 +    /* 119 */ "RowKey"           OpHelp("r[P2]=key"),
  2.4811 +    /* 120 */ "RowData"          OpHelp("r[P2]=data"),
  2.4812 +    /* 121 */ "Rowid"            OpHelp("r[P2]=rowid"),
  2.4813 +    /* 122 */ "NullRow"          OpHelp(""),
  2.4814 +    /* 123 */ "SorterInsert"     OpHelp(""),
  2.4815 +    /* 124 */ "IdxInsert"        OpHelp("key=r[P2]"),
  2.4816 +    /* 125 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
  2.4817 +    /* 126 */ "Seek"             OpHelp("Move P3 to P1.rowid"),
  2.4818 +    /* 127 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
  2.4819 +    /* 128 */ "Destroy"          OpHelp(""),
  2.4820 +    /* 129 */ "Clear"            OpHelp(""),
  2.4821 +    /* 130 */ "ResetSorter"      OpHelp(""),
  2.4822 +    /* 131 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
  2.4823 +    /* 132 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
  2.4824      /* 133 */ "Real"             OpHelp("r[P2]=P4"),
  2.4825 -    /* 134 */ "Param"            OpHelp(""),
  2.4826 -    /* 135 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
  2.4827 -    /* 136 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
  2.4828 -    /* 137 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
  2.4829 -    /* 138 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
  2.4830 -    /* 139 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
  2.4831 -    /* 140 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
  2.4832 -    /* 141 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
  2.4833 -    /* 142 */ "JumpZeroIncr"     OpHelp("if (r[P1]++)==0 ) goto P2"),
  2.4834 -    /* 143 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
  2.4835 -    /* 144 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
  2.4836 -    /* 145 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
  2.4837 -    /* 146 */ "IncrVacuum"       OpHelp(""),
  2.4838 -    /* 147 */ "Expire"           OpHelp(""),
  2.4839 -    /* 148 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
  2.4840 -    /* 149 */ "VBegin"           OpHelp(""),
  2.4841 -    /* 150 */ "VCreate"          OpHelp(""),
  2.4842 -    /* 151 */ "VDestroy"         OpHelp(""),
  2.4843 -    /* 152 */ "VOpen"            OpHelp(""),
  2.4844 -    /* 153 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
  2.4845 -    /* 154 */ "VNext"            OpHelp(""),
  2.4846 +    /* 134 */ "ParseSchema"      OpHelp(""),
  2.4847 +    /* 135 */ "LoadAnalysis"     OpHelp(""),
  2.4848 +    /* 136 */ "DropTable"        OpHelp(""),
  2.4849 +    /* 137 */ "DropIndex"        OpHelp(""),
  2.4850 +    /* 138 */ "DropTrigger"      OpHelp(""),
  2.4851 +    /* 139 */ "IntegrityCk"      OpHelp(""),
  2.4852 +    /* 140 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
  2.4853 +    /* 141 */ "Param"            OpHelp(""),
  2.4854 +    /* 142 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
  2.4855 +    /* 143 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
  2.4856 +    /* 144 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
  2.4857 +    /* 145 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
  2.4858 +    /* 146 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
  2.4859 +    /* 147 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
  2.4860 +    /* 148 */ "Expire"           OpHelp(""),
  2.4861 +    /* 149 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
  2.4862 +    /* 150 */ "VBegin"           OpHelp(""),
  2.4863 +    /* 151 */ "VCreate"          OpHelp(""),
  2.4864 +    /* 152 */ "VDestroy"         OpHelp(""),
  2.4865 +    /* 153 */ "VOpen"            OpHelp(""),
  2.4866 +    /* 154 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
  2.4867      /* 155 */ "VRename"          OpHelp(""),
  2.4868      /* 156 */ "Pagecount"        OpHelp(""),
  2.4869      /* 157 */ "MaxPgcnt"         OpHelp(""),
  2.4870 -    /* 158 */ "Init"             OpHelp("Start at P2"),
  2.4871 -    /* 159 */ "CursorHint"       OpHelp(""),
  2.4872 -    /* 160 */ "Noop"             OpHelp(""),
  2.4873 -    /* 161 */ "Explain"          OpHelp(""),
  2.4874 +    /* 158 */ "CursorHint"       OpHelp(""),
  2.4875 +    /* 159 */ "Noop"             OpHelp(""),
  2.4876 +    /* 160 */ "Explain"          OpHelp(""),
  2.4877    };
  2.4878    return azName[i];
  2.4879  }
  2.4880 @@ -27170,6 +28970,19 @@
  2.4881  #  endif
  2.4882  #endif
  2.4883  
  2.4884 +/* Use pread() and pwrite() if they are available */
  2.4885 +#if defined(__APPLE__)
  2.4886 +# define HAVE_PREAD 1
  2.4887 +# define HAVE_PWRITE 1
  2.4888 +#endif
  2.4889 +#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
  2.4890 +# undef USE_PREAD
  2.4891 +# define USE_PREAD64 1
  2.4892 +#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
  2.4893 +# undef USE_PREAD64
  2.4894 +# define USE_PREAD 1
  2.4895 +#endif
  2.4896 +
  2.4897  /*
  2.4898  ** standard include files.
  2.4899  */
  2.4900 @@ -27689,7 +29502,7 @@
  2.4901  #else
  2.4902    { "pread64",      (sqlite3_syscall_ptr)0,          0  },
  2.4903  #endif
  2.4904 -#define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
  2.4905 +#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
  2.4906  
  2.4907    { "write",        (sqlite3_syscall_ptr)write,      0  },
  2.4908  #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
  2.4909 @@ -27707,7 +29520,7 @@
  2.4910  #else
  2.4911    { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
  2.4912  #endif
  2.4913 -#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
  2.4914 +#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off64_t))\
  2.4915                      aSyscall[13].pCurrent)
  2.4916  
  2.4917    { "fchmod",       (sqlite3_syscall_ptr)fchmod,          0  },
  2.4918 @@ -28599,7 +30412,7 @@
  2.4919    if( pInode==0 ){
  2.4920      pInode = sqlite3_malloc64( sizeof(*pInode) );
  2.4921      if( pInode==0 ){
  2.4922 -      return SQLITE_NOMEM;
  2.4923 +      return SQLITE_NOMEM_BKPT;
  2.4924      }
  2.4925      memset(pInode, 0, sizeof(*pInode));
  2.4926      memcpy(&pInode->fileId, &fileId, sizeof(fileId));
  2.4927 @@ -28641,12 +30454,16 @@
  2.4928  static void verifyDbFile(unixFile *pFile){
  2.4929    struct stat buf;
  2.4930    int rc;
  2.4931 +
  2.4932 +  /* These verifications occurs for the main database only */
  2.4933 +  if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
  2.4934 +
  2.4935    rc = osFstat(pFile->h, &buf);
  2.4936    if( rc!=0 ){
  2.4937      sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
  2.4938      return;
  2.4939    }
  2.4940 -  if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
  2.4941 +  if( buf.st_nlink==0 ){
  2.4942      sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
  2.4943      return;
  2.4944    }
  2.4945 @@ -28782,7 +30599,7 @@
  2.4946    ** lock transitions in terms of the POSIX advisory shared and exclusive
  2.4947    ** lock primitives (called read-locks and write-locks below, to avoid
  2.4948    ** confusion with SQLite lock names). The algorithms are complicated
  2.4949 -  ** slightly in order to be compatible with windows systems simultaneously
  2.4950 +  ** slightly in order to be compatible with Windows95 systems simultaneously
  2.4951    ** accessing the same database file, in case that is ever required.
  2.4952    **
  2.4953    ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
  2.4954 @@ -28790,8 +30607,14 @@
  2.4955    ** range', a range of 510 bytes at a well known offset.
  2.4956    **
  2.4957    ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
  2.4958 -  ** byte'.  If this is successful, a random byte from the 'shared byte
  2.4959 -  ** range' is read-locked and the lock on the 'pending byte' released.
  2.4960 +  ** byte'.  If this is successful, 'shared byte range' is read-locked
  2.4961 +  ** and the lock on the 'pending byte' released.  (Legacy note:  When
  2.4962 +  ** SQLite was first developed, Windows95 systems were still very common,
  2.4963 +  ** and Widnows95 lacks a shared-lock capability.  So on Windows95, a
  2.4964 +  ** single randomly selected by from the 'shared byte range' is locked.
  2.4965 +  ** Windows95 is now pretty much extinct, but this work-around for the
  2.4966 +  ** lack of shared-locks on Windows95 lives on, for backwards
  2.4967 +  ** compatibility.)
  2.4968    **
  2.4969    ** A process may only obtain a RESERVED lock after it has a SHARED lock.
  2.4970    ** A RESERVED lock is implemented by grabbing a write-lock on the
  2.4971 @@ -28810,11 +30633,6 @@
  2.4972    ** range'. Since all other locks require a read-lock on one of the bytes
  2.4973    ** within this range, this ensures that no other locks are held on the
  2.4974    ** database. 
  2.4975 -  **
  2.4976 -  ** The reason a single byte cannot be used instead of the 'shared byte
  2.4977 -  ** range' is that some versions of windows do not support read-locks. By
  2.4978 -  ** locking a random byte from a range, concurrent SHARED locks may exist
  2.4979 -  ** even if the locking primitive used is always a write-lock.
  2.4980    */
  2.4981    int rc = SQLITE_OK;
  2.4982    unixFile *pFile = (unixFile*)id;
  2.4983 @@ -31519,7 +33337,7 @@
  2.4984  
  2.4985    /* Allocate space for the new unixShm object. */
  2.4986    p = sqlite3_malloc64( sizeof(*p) );
  2.4987 -  if( p==0 ) return SQLITE_NOMEM;
  2.4988 +  if( p==0 ) return SQLITE_NOMEM_BKPT;
  2.4989    memset(p, 0, sizeof(*p));
  2.4990    assert( pDbFd->pShm==0 );
  2.4991  
  2.4992 @@ -31551,7 +33369,7 @@
  2.4993  #endif
  2.4994      pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
  2.4995      if( pShmNode==0 ){
  2.4996 -      rc = SQLITE_NOMEM;
  2.4997 +      rc = SQLITE_NOMEM_BKPT;
  2.4998        goto shm_open_err;
  2.4999      }
  2.5000      memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
  2.5001 @@ -31567,10 +33385,12 @@
  2.5002      pShmNode->h = -1;
  2.5003      pDbFd->pInode->pShmNode = pShmNode;
  2.5004      pShmNode->pInode = pDbFd->pInode;
  2.5005 -    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  2.5006 -    if( pShmNode->mutex==0 ){
  2.5007 -      rc = SQLITE_NOMEM;
  2.5008 -      goto shm_open_err;
  2.5009 +    if( sqlite3GlobalConfig.bCoreMutex ){
  2.5010 +      pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  2.5011 +      if( pShmNode->mutex==0 ){
  2.5012 +        rc = SQLITE_NOMEM_BKPT;
  2.5013 +        goto shm_open_err;
  2.5014 +      }
  2.5015      }
  2.5016  
  2.5017      if( pInode->bProcessLock==0 ){
  2.5018 @@ -31742,7 +33562,7 @@
  2.5019          pShmNode->apRegion, nReqRegion*sizeof(char *)
  2.5020      );
  2.5021      if( !apNew ){
  2.5022 -      rc = SQLITE_IOERR_NOMEM;
  2.5023 +      rc = SQLITE_IOERR_NOMEM_BKPT;
  2.5024        goto shmpage_out;
  2.5025      }
  2.5026      pShmNode->apRegion = apNew;
  2.5027 @@ -31762,7 +33582,7 @@
  2.5028        }else{
  2.5029          pMem = sqlite3_malloc64(szRegion);
  2.5030          if( pMem==0 ){
  2.5031 -          rc = SQLITE_NOMEM;
  2.5032 +          rc = SQLITE_NOMEM_BKPT;
  2.5033            goto shmpage_out;
  2.5034          }
  2.5035          memset(pMem, 0, szRegion);
  2.5036 @@ -32540,7 +34360,7 @@
  2.5037    pNew->pId = vxworksFindFileId(zFilename);
  2.5038    if( pNew->pId==0 ){
  2.5039      ctrlFlags |= UNIXFILE_NOLOCK;
  2.5040 -    rc = SQLITE_NOMEM;
  2.5041 +    rc = SQLITE_NOMEM_BKPT;
  2.5042    }
  2.5043  #endif
  2.5044  
  2.5045 @@ -32596,7 +34416,7 @@
  2.5046      afpLockingContext *pCtx;
  2.5047      pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
  2.5048      if( pCtx==0 ){
  2.5049 -      rc = SQLITE_NOMEM;
  2.5050 +      rc = SQLITE_NOMEM_BKPT;
  2.5051      }else{
  2.5052        /* NB: zFilename exists and remains valid until the file is closed
  2.5053        ** according to requirement F11141.  So we do not need to make a
  2.5054 @@ -32626,7 +34446,7 @@
  2.5055      nFilename = (int)strlen(zFilename) + 6;
  2.5056      zLockFile = (char *)sqlite3_malloc64(nFilename);
  2.5057      if( zLockFile==0 ){
  2.5058 -      rc = SQLITE_NOMEM;
  2.5059 +      rc = SQLITE_NOMEM_BKPT;
  2.5060      }else{
  2.5061        sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
  2.5062      }
  2.5063 @@ -32649,7 +34469,7 @@
  2.5064          if( zSemName[n]=='/' ) zSemName[n] = '_';
  2.5065        pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
  2.5066        if( pNew->pInode->pSem == SEM_FAILED ){
  2.5067 -        rc = SQLITE_NOMEM;
  2.5068 +        rc = SQLITE_NOMEM_BKPT;
  2.5069          pNew->pInode->aSemName[0] = '\0';
  2.5070        }
  2.5071      }
  2.5072 @@ -32689,20 +34509,24 @@
  2.5073       "/tmp",
  2.5074       "."
  2.5075    };
  2.5076 -  unsigned int i;
  2.5077 +  unsigned int i = 0;
  2.5078    struct stat buf;
  2.5079    const char *zDir = sqlite3_temp_directory;
  2.5080  
  2.5081    if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
  2.5082    if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
  2.5083 -  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
  2.5084 -    if( zDir==0 ) continue;
  2.5085 -    if( osStat(zDir, &buf) ) continue;
  2.5086 -    if( !S_ISDIR(buf.st_mode) ) continue;
  2.5087 -    if( osAccess(zDir, 07) ) continue;
  2.5088 -    break;
  2.5089 -  }
  2.5090 -  return zDir;
  2.5091 +  while(1){
  2.5092 +    if( zDir!=0
  2.5093 +     && osStat(zDir, &buf)==0
  2.5094 +     && S_ISDIR(buf.st_mode)
  2.5095 +     && osAccess(zDir, 03)==0
  2.5096 +    ){
  2.5097 +      return zDir;
  2.5098 +    }
  2.5099 +    if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
  2.5100 +    zDir = azDirs[i++];
  2.5101 +  }
  2.5102 +  return 0;
  2.5103  }
  2.5104  
  2.5105  /*
  2.5106 @@ -32718,9 +34542,11 @@
  2.5107    ** using the io-error infrastructure to test that SQLite handles this
  2.5108    ** function failing. 
  2.5109    */
  2.5110 +  zBuf[0] = 0;
  2.5111    SimulateIOError( return SQLITE_IOERR );
  2.5112  
  2.5113    zDir = unixTempFileDir();
  2.5114 +  if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
  2.5115    do{
  2.5116      u64 r;
  2.5117      sqlite3_randomness(sizeof(r), &r);
  2.5118 @@ -32990,7 +34816,7 @@
  2.5119      }else{
  2.5120        pUnused = sqlite3_malloc64(sizeof(*pUnused));
  2.5121        if( !pUnused ){
  2.5122 -        return SQLITE_NOMEM;
  2.5123 +        return SQLITE_NOMEM_BKPT;
  2.5124        }
  2.5125      }
  2.5126      p->pUnused = pUnused;
  2.5127 @@ -33076,7 +34902,7 @@
  2.5128      zPath = sqlite3_mprintf("%s", zName);
  2.5129      if( zPath==0 ){
  2.5130        robust_close(p, fd, __LINE__);
  2.5131 -      return SQLITE_NOMEM;
  2.5132 +      return SQLITE_NOMEM_BKPT;
  2.5133      }
  2.5134  #else
  2.5135      osUnlink(zName);
  2.5136 @@ -33087,9 +34913,6 @@
  2.5137      p->openFlags = openFlags;
  2.5138    }
  2.5139  #endif
  2.5140 -
  2.5141 -  noLock = eType!=SQLITE_OPEN_MAIN_DB;
  2.5142 -
  2.5143    
  2.5144  #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
  2.5145    if( fstatfs(fd, &fsInfo) == -1 ){
  2.5146 @@ -33108,6 +34931,7 @@
  2.5147    /* Set up appropriate ctrlFlags */
  2.5148    if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
  2.5149    if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
  2.5150 +  noLock = eType!=SQLITE_OPEN_MAIN_DB;
  2.5151    if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
  2.5152    if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
  2.5153    if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
  2.5154 @@ -33308,7 +35132,7 @@
  2.5155      if( bLink ){
  2.5156        if( zDel==0 ){
  2.5157          zDel = sqlite3_malloc(nOut);
  2.5158 -        if( zDel==0 ) rc = SQLITE_NOMEM;
  2.5159 +        if( zDel==0 ) rc = SQLITE_NOMEM_BKPT;
  2.5160        }else if( ++nLink>SQLITE_MAX_SYMLINKS ){
  2.5161          rc = SQLITE_CANTOPEN_BKPT;
  2.5162        }
  2.5163 @@ -33546,23 +35370,18 @@
  2.5164  # define unixCurrentTime 0
  2.5165  #endif
  2.5166  
  2.5167 -#ifndef SQLITE_OMIT_DEPRECATED
  2.5168 -/*
  2.5169 -** We added the xGetLastError() method with the intention of providing
  2.5170 -** better low-level error messages when operating-system problems come up
  2.5171 -** during SQLite operation.  But so far, none of that has been implemented
  2.5172 -** in the core.  So this routine is never called.  For now, it is merely
  2.5173 -** a place-holder.
  2.5174 +/*
  2.5175 +** The xGetLastError() method is designed to return a better
  2.5176 +** low-level error message when operating-system problems come up
  2.5177 +** during SQLite operation.  Only the integer return code is currently
  2.5178 +** used.
  2.5179  */
  2.5180  static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
  2.5181    UNUSED_PARAMETER(NotUsed);
  2.5182    UNUSED_PARAMETER(NotUsed2);
  2.5183    UNUSED_PARAMETER(NotUsed3);
  2.5184 -  return 0;
  2.5185 -}
  2.5186 -#else
  2.5187 -# define unixGetLastError 0
  2.5188 -#endif
  2.5189 +  return errno;
  2.5190 +}
  2.5191  
  2.5192  
  2.5193  /*
  2.5194 @@ -33852,7 +35671,7 @@
  2.5195    }else{
  2.5196      pUnused = sqlite3_malloc64(sizeof(*pUnused));
  2.5197      if( !pUnused ){
  2.5198 -      return SQLITE_NOMEM;
  2.5199 +      return SQLITE_NOMEM_BKPT;
  2.5200      }
  2.5201    }
  2.5202    if( fd<0 ){
  2.5203 @@ -33885,7 +35704,7 @@
  2.5204    
  2.5205    pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
  2.5206    if( pNew==NULL ){
  2.5207 -    rc = SQLITE_NOMEM;
  2.5208 +    rc = SQLITE_NOMEM_BKPT;
  2.5209      goto end_create_proxy;
  2.5210    }
  2.5211    memset(pNew, 0, sizeof(unixFile));
  2.5212 @@ -34298,7 +36117,7 @@
  2.5213          if( tempLockPath ){
  2.5214            pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
  2.5215            if( !pCtx->lockProxyPath ){
  2.5216 -            rc = SQLITE_NOMEM;
  2.5217 +            rc = SQLITE_NOMEM_BKPT;
  2.5218            }
  2.5219          }
  2.5220        }
  2.5221 @@ -34363,7 +36182,7 @@
  2.5222    ** the name of the original database file. */  
  2.5223    *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
  2.5224    if( conchPath==0 ){
  2.5225 -    return SQLITE_NOMEM;
  2.5226 +    return SQLITE_NOMEM_BKPT;
  2.5227    }
  2.5228    memcpy(conchPath, dbPath, len+1);
  2.5229    
  2.5230 @@ -34479,7 +36298,7 @@
  2.5231  
  2.5232    pCtx = sqlite3_malloc64( sizeof(*pCtx) );
  2.5233    if( pCtx==0 ){
  2.5234 -    return SQLITE_NOMEM;
  2.5235 +    return SQLITE_NOMEM_BKPT;
  2.5236    }
  2.5237    memset(pCtx, 0, sizeof(*pCtx));
  2.5238  
  2.5239 @@ -34515,7 +36334,7 @@
  2.5240    if( rc==SQLITE_OK ){
  2.5241      pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
  2.5242      if( pCtx->dbPath==NULL ){
  2.5243 -      rc = SQLITE_NOMEM;
  2.5244 +      rc = SQLITE_NOMEM_BKPT;
  2.5245      }
  2.5246    }
  2.5247    if( rc==SQLITE_OK ){
  2.5248 @@ -35414,10 +37233,22 @@
  2.5249  #endif
  2.5250  
  2.5251  /*
  2.5252 + * This is cache size used in the calculation of the initial size of the
  2.5253 + * Win32-specific heap.  It cannot be negative.
  2.5254 + */
  2.5255 +#ifndef SQLITE_WIN32_CACHE_SIZE
  2.5256 +#  if SQLITE_DEFAULT_CACHE_SIZE>=0
  2.5257 +#    define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
  2.5258 +#  else
  2.5259 +#    define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
  2.5260 +#  endif
  2.5261 +#endif
  2.5262 +
  2.5263 +/*
  2.5264   * The initial size of the Win32-specific heap.  This value may be zero.
  2.5265   */
  2.5266  #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
  2.5267 -#  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
  2.5268 +#  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
  2.5269                                         (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
  2.5270  #endif
  2.5271  
  2.5272 @@ -36291,7 +38122,7 @@
  2.5273      if( lastErrno==NO_ERROR ){
  2.5274        sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
  2.5275                    (void*)hHeap);
  2.5276 -      rc = SQLITE_NOMEM;
  2.5277 +      rc = SQLITE_NOMEM_BKPT;
  2.5278      }else{
  2.5279        sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
  2.5280                    osGetLastError(), (void*)hHeap);
  2.5281 @@ -36317,8 +38148,8 @@
  2.5282    int rc;
  2.5283    MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
  2.5284    MUTEX_LOGIC( sqlite3_mutex *pMem; )    /* The memsys static mutex */
  2.5285 -  MUTEX_LOGIC( pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); )
  2.5286 -  MUTEX_LOGIC( pMem = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); )
  2.5287 +  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
  2.5288 +  MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
  2.5289    sqlite3_mutex_enter(pMaster);
  2.5290    sqlite3_mutex_enter(pMem);
  2.5291    winMemAssertMagic();
  2.5292 @@ -36363,6 +38194,12 @@
  2.5293    int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
  2.5294    if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
  2.5295    assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
  2.5296 +#ifdef SQLITE_ENABLE_API_ARMOR
  2.5297 +  if( !zBuf ){
  2.5298 +    (void)SQLITE_MISUSE_BKPT;
  2.5299 +    return;
  2.5300 +  }
  2.5301 +#endif
  2.5302  #if defined(SQLITE_WIN32_HAS_ANSI)
  2.5303    if( nMin>0 ){
  2.5304      memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
  2.5305 @@ -36611,7 +38448,7 @@
  2.5306            "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
  2.5307            osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
  2.5308            dwMaximumSize);
  2.5309 -      return SQLITE_NOMEM;
  2.5310 +      return SQLITE_NOMEM_BKPT;
  2.5311      }
  2.5312      pWinMemData->bOwned = TRUE;
  2.5313      assert( pWinMemData->bOwned );
  2.5314 @@ -36621,7 +38458,7 @@
  2.5315    if( !pWinMemData->hHeap ){
  2.5316      sqlite3_log(SQLITE_NOMEM,
  2.5317          "failed to GetProcessHeap (%lu)", osGetLastError());
  2.5318 -    return SQLITE_NOMEM;
  2.5319 +    return SQLITE_NOMEM_BKPT;
  2.5320    }
  2.5321    pWinMemData->bOwned = FALSE;
  2.5322    assert( !pWinMemData->bOwned );
  2.5323 @@ -36688,147 +38525,244 @@
  2.5324  #endif /* SQLITE_WIN32_MALLOC */
  2.5325  
  2.5326  /*
  2.5327 -** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
  2.5328 -**
  2.5329 -** Space to hold the returned string is obtained from malloc.
  2.5330 -*/
  2.5331 -static LPWSTR winUtf8ToUnicode(const char *zFilename){
  2.5332 +** Convert a UTF-8 string to Microsoft Unicode.
  2.5333 +**
  2.5334 +** Space to hold the returned string is obtained from sqlite3_malloc().
  2.5335 +*/
  2.5336 +static LPWSTR winUtf8ToUnicode(const char *zText){
  2.5337    int nChar;
  2.5338 -  LPWSTR zWideFilename;
  2.5339 -
  2.5340 -  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
  2.5341 +  LPWSTR zWideText;
  2.5342 +
  2.5343 +  nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
  2.5344    if( nChar==0 ){
  2.5345      return 0;
  2.5346    }
  2.5347 -  zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
  2.5348 -  if( zWideFilename==0 ){
  2.5349 +  zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
  2.5350 +  if( zWideText==0 ){
  2.5351      return 0;
  2.5352    }
  2.5353 -  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
  2.5354 +  nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
  2.5355                                  nChar);
  2.5356    if( nChar==0 ){
  2.5357 -    sqlite3_free(zWideFilename);
  2.5358 -    zWideFilename = 0;
  2.5359 -  }
  2.5360 -  return zWideFilename;
  2.5361 -}
  2.5362 -
  2.5363 -/*
  2.5364 -** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
  2.5365 -** obtained from sqlite3_malloc().
  2.5366 -*/
  2.5367 -static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
  2.5368 +    sqlite3_free(zWideText);
  2.5369 +    zWideText = 0;
  2.5370 +  }
  2.5371 +  return zWideText;
  2.5372 +}
  2.5373 +
  2.5374 +/*
  2.5375 +** Convert a Microsoft Unicode string to UTF-8.
  2.5376 +**
  2.5377 +** Space to hold the returned string is obtained from sqlite3_malloc().
  2.5378 +*/
  2.5379 +static char *winUnicodeToUtf8(LPCWSTR zWideText){
  2.5380    int nByte;
  2.5381 -  char *zFilename;
  2.5382 -
  2.5383 -  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
  2.5384 +  char *zText;
  2.5385 +
  2.5386 +  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
  2.5387    if( nByte == 0 ){
  2.5388      return 0;
  2.5389    }
  2.5390 -  zFilename = sqlite3MallocZero( nByte );
  2.5391 -  if( zFilename==0 ){
  2.5392 +  zText = sqlite3MallocZero( nByte );
  2.5393 +  if( zText==0 ){
  2.5394      return 0;
  2.5395    }
  2.5396 -  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
  2.5397 +  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
  2.5398                                  0, 0);
  2.5399    if( nByte == 0 ){
  2.5400 -    sqlite3_free(zFilename);
  2.5401 -    zFilename = 0;
  2.5402 -  }
  2.5403 -  return zFilename;
  2.5404 -}
  2.5405 -
  2.5406 -/*
  2.5407 -** Convert an ANSI string to Microsoft Unicode, based on the
  2.5408 -** current codepage settings for file apis.
  2.5409 -**
  2.5410 -** Space to hold the returned string is obtained
  2.5411 -** from sqlite3_malloc.
  2.5412 -*/
  2.5413 -static LPWSTR winMbcsToUnicode(const char *zFilename){
  2.5414 +    sqlite3_free(zText);
  2.5415 +    zText = 0;
  2.5416 +  }
  2.5417 +  return zText;
  2.5418 +}
  2.5419 +
  2.5420 +/*
  2.5421 +** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
  2.5422 +** code page.
  2.5423 +**
  2.5424 +** Space to hold the returned string is obtained from sqlite3_malloc().
  2.5425 +*/
  2.5426 +static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
  2.5427    int nByte;
  2.5428 -  LPWSTR zMbcsFilename;
  2.5429 -  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
  2.5430 -
  2.5431 -  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
  2.5432 +  LPWSTR zMbcsText;
  2.5433 +  int codepage = useAnsi ? CP_ACP : CP_OEMCP;
  2.5434 +
  2.5435 +  nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
  2.5436                                  0)*sizeof(WCHAR);
  2.5437    if( nByte==0 ){
  2.5438      return 0;
  2.5439    }
  2.5440 -  zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
  2.5441 -  if( zMbcsFilename==0 ){
  2.5442 +  zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
  2.5443 +  if( zMbcsText==0 ){
  2.5444      return 0;
  2.5445    }
  2.5446 -  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
  2.5447 +  nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
  2.5448                                  nByte);
  2.5449    if( nByte==0 ){
  2.5450 -    sqlite3_free(zMbcsFilename);
  2.5451 -    zMbcsFilename = 0;
  2.5452 -  }
  2.5453 -  return zMbcsFilename;
  2.5454 -}
  2.5455 -
  2.5456 -/*
  2.5457 -** Convert Microsoft Unicode to multi-byte character string, based on the
  2.5458 -** user's ANSI codepage.
  2.5459 -**
  2.5460 -** Space to hold the returned string is obtained from
  2.5461 -** sqlite3_malloc().
  2.5462 -*/
  2.5463 -static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
  2.5464 +    sqlite3_free(zMbcsText);
  2.5465 +    zMbcsText = 0;
  2.5466 +  }
  2.5467 +  return zMbcsText;
  2.5468 +}
  2.5469 +
  2.5470 +/*
  2.5471 +** Convert a Microsoft Unicode string to a multi-byte character string,
  2.5472 +** using the ANSI or OEM code page.
  2.5473 +**
  2.5474 +** Space to hold the returned string is obtained from sqlite3_malloc().
  2.5475 +*/
  2.5476 +static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
  2.5477    int nByte;
  2.5478 -  char *zFilename;
  2.5479 -  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
  2.5480 -
  2.5481 -  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
  2.5482 +  char *zText;
  2.5483 +  int codepage = useAnsi ? CP_ACP : CP_OEMCP;
  2.5484 +
  2.5485 +  nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
  2.5486    if( nByte == 0 ){
  2.5487      return 0;
  2.5488    }
  2.5489 -  zFilename = sqlite3MallocZero( nByte );
  2.5490 -  if( zFilename==0 ){
  2.5491 +  zText = sqlite3MallocZero( nByte );
  2.5492 +  if( zText==0 ){
  2.5493      return 0;
  2.5494    }
  2.5495 -  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
  2.5496 +  nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
  2.5497                                  nByte, 0, 0);
  2.5498    if( nByte == 0 ){
  2.5499 -    sqlite3_free(zFilename);
  2.5500 -    zFilename = 0;
  2.5501 -  }
  2.5502 -  return zFilename;
  2.5503 -}
  2.5504 -
  2.5505 -/*
  2.5506 -** Convert multibyte character string to UTF-8.  Space to hold the
  2.5507 -** returned string is obtained from sqlite3_malloc().
  2.5508 -*/
  2.5509 -SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8(const char *zFilename){
  2.5510 -  char *zFilenameUtf8;
  2.5511 +    sqlite3_free(zText);
  2.5512 +    zText = 0;
  2.5513 +  }
  2.5514 +  return zText;
  2.5515 +}
  2.5516 +
  2.5517 +/*
  2.5518 +** Convert a multi-byte character string to UTF-8.
  2.5519 +**
  2.5520 +** Space to hold the returned string is obtained from sqlite3_malloc().
  2.5521 +*/
  2.5522 +static char *winMbcsToUtf8(const char *zText, int useAnsi){
  2.5523 +  char *zTextUtf8;
  2.5524    LPWSTR zTmpWide;
  2.5525  
  2.5526 -  zTmpWide = winMbcsToUnicode(zFilename);
  2.5527 +  zTmpWide = winMbcsToUnicode(zText, useAnsi);
  2.5528    if( zTmpWide==0 ){
  2.5529      return 0;
  2.5530    }
  2.5531 -  zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
  2.5532 +  zTextUtf8 = winUnicodeToUtf8(zTmpWide);
  2.5533    sqlite3_free(zTmpWide);
  2.5534 -  return zFilenameUtf8;
  2.5535 -}
  2.5536 -
  2.5537 -/*
  2.5538 -** Convert UTF-8 to multibyte character string.  Space to hold the
  2.5539 -** returned string is obtained from sqlite3_malloc().
  2.5540 -*/
  2.5541 -SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs(const char *zFilename){
  2.5542 -  char *zFilenameMbcs;
  2.5543 +  return zTextUtf8;
  2.5544 +}
  2.5545 +
  2.5546 +/*
  2.5547 +** Convert a UTF-8 string to a multi-byte character string.
  2.5548 +**
  2.5549 +** Space to hold the returned string is obtained from sqlite3_malloc().
  2.5550 +*/
  2.5551 +static char *winUtf8ToMbcs(const char *zText, int useAnsi){
  2.5552 +  char *zTextMbcs;
  2.5553    LPWSTR zTmpWide;
  2.5554  
  2.5555 -  zTmpWide = winUtf8ToUnicode(zFilename);
  2.5556 +  zTmpWide = winUtf8ToUnicode(zText);
  2.5557    if( zTmpWide==0 ){
  2.5558      return 0;
  2.5559    }
  2.5560 -  zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
  2.5561 +  zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
  2.5562    sqlite3_free(zTmpWide);
  2.5563 -  return zFilenameMbcs;
  2.5564 +  return zTextMbcs;
  2.5565 +}
  2.5566 +
  2.5567 +/*
  2.5568 +** This is a public wrapper for the winUtf8ToUnicode() function.
  2.5569 +*/
  2.5570 +SQLITE_API LPWSTR SQLITE_STDCALL sqlite3_win32_utf8_to_unicode(const char *zText){
  2.5571 +#ifdef SQLITE_ENABLE_API_ARMOR
  2.5572 +  if( !zText ){
  2.5573 +    (void)SQLITE_MISUSE_BKPT;
  2.5574 +    return 0;
  2.5575 +  }
  2.5576 +#endif
  2.5577 +#ifndef SQLITE_OMIT_AUTOINIT
  2.5578 +  if( sqlite3_initialize() ) return 0;
  2.5579 +#endif
  2.5580 +  return winUtf8ToUnicode(zText);
  2.5581 +}
  2.5582 +
  2.5583 +/*
  2.5584 +** This is a public wrapper for the winUnicodeToUtf8() function.
  2.5585 +*/
  2.5586 +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
  2.5587 +#ifdef SQLITE_ENABLE_API_ARMOR
  2.5588 +  if( !zWideText ){
  2.5589 +    (void)SQLITE_MISUSE_BKPT;
  2.5590 +    return 0;
  2.5591 +  }
  2.5592 +#endif
  2.5593 +#ifndef SQLITE_OMIT_AUTOINIT
  2.5594 +  if( sqlite3_initialize() ) return 0;
  2.5595 +#endif
  2.5596 +  return winUnicodeToUtf8(zWideText);
  2.5597 +}
  2.5598 +
  2.5599 +/*
  2.5600 +** This is a public wrapper for the winMbcsToUtf8() function.
  2.5601 +*/
  2.5602 +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8(const char *zText){
  2.5603 +#ifdef SQLITE_ENABLE_API_ARMOR
  2.5604 +  if( !zText ){
  2.5605 +    (void)SQLITE_MISUSE_BKPT;
  2.5606 +    return 0;
  2.5607 +  }
  2.5608 +#endif
  2.5609 +#ifndef SQLITE_OMIT_AUTOINIT
  2.5610 +  if( sqlite3_initialize() ) return 0;
  2.5611 +#endif
  2.5612 +  return winMbcsToUtf8(zText, osAreFileApisANSI());
  2.5613 +}
  2.5614 +
  2.5615 +/*
  2.5616 +** This is a public wrapper for the winMbcsToUtf8() function.
  2.5617 +*/
  2.5618 +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
  2.5619 +#ifdef SQLITE_ENABLE_API_ARMOR
  2.5620 +  if( !zText ){
  2.5621 +    (void)SQLITE_MISUSE_BKPT;
  2.5622 +    return 0;
  2.5623 +  }
  2.5624 +#endif
  2.5625 +#ifndef SQLITE_OMIT_AUTOINIT
  2.5626 +  if( sqlite3_initialize() ) return 0;
  2.5627 +#endif
  2.5628 +  return winMbcsToUtf8(zText, useAnsi);
  2.5629 +}
  2.5630 +
  2.5631 +/*
  2.5632 +** This is a public wrapper for the winUtf8ToMbcs() function.
  2.5633 +*/
  2.5634 +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs(const char *zText){
  2.5635 +#ifdef SQLITE_ENABLE_API_ARMOR
  2.5636 +  if( !zText ){
  2.5637 +    (void)SQLITE_MISUSE_BKPT;
  2.5638 +    return 0;
  2.5639 +  }
  2.5640 +#endif
  2.5641 +#ifndef SQLITE_OMIT_AUTOINIT
  2.5642 +  if( sqlite3_initialize() ) return 0;
  2.5643 +#endif
  2.5644 +  return winUtf8ToMbcs(zText, osAreFileApisANSI());
  2.5645 +}
  2.5646 +
  2.5647 +/*
  2.5648 +** This is a public wrapper for the winUtf8ToMbcs() function.
  2.5649 +*/
  2.5650 +SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
  2.5651 +#ifdef SQLITE_ENABLE_API_ARMOR
  2.5652 +  if( !zText ){
  2.5653 +    (void)SQLITE_MISUSE_BKPT;
  2.5654 +    return 0;
  2.5655 +  }
  2.5656 +#endif
  2.5657 +#ifndef SQLITE_OMIT_AUTOINIT
  2.5658 +  if( sqlite3_initialize() ) return 0;
  2.5659 +#endif
  2.5660 +  return winUtf8ToMbcs(zText, useAnsi);
  2.5661  }
  2.5662  
  2.5663  /*
  2.5664 @@ -36858,7 +38792,7 @@
  2.5665      if( zValue && zValue[0] ){
  2.5666        zValueUtf8 = winUnicodeToUtf8(zValue);
  2.5667        if ( zValueUtf8==0 ){
  2.5668 -        return SQLITE_NOMEM;
  2.5669 +        return SQLITE_NOMEM_BKPT;
  2.5670        }
  2.5671      }
  2.5672      sqlite3_free(*ppDirectory);
  2.5673 @@ -36930,7 +38864,7 @@
  2.5674      if( dwLen > 0 ){
  2.5675        /* allocate a buffer and convert to UTF8 */
  2.5676        sqlite3BeginBenignMalloc();
  2.5677 -      zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
  2.5678 +      zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
  2.5679        sqlite3EndBenignMalloc();
  2.5680        /* free the system buffer allocated by FormatMessage */
  2.5681        osLocalFree(zTemp);
  2.5682 @@ -37072,16 +39006,17 @@
  2.5683    }
  2.5684  }
  2.5685  
  2.5686 -#if SQLITE_OS_WINCE
  2.5687 -/*************************************************************************
  2.5688 -** This section contains code for WinCE only.
  2.5689 -*/
  2.5690 -#if !defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API
  2.5691 -/*
  2.5692 -** The MSVC CRT on Windows CE may not have a localtime() function.  So
  2.5693 -** create a substitute.
  2.5694 -*/
  2.5695 -/* #include <time.h> */
  2.5696 +/*
  2.5697 +** This #if does not rely on the SQLITE_OS_WINCE define because the
  2.5698 +** corresponding section in "date.c" cannot use it.
  2.5699 +*/
  2.5700 +#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
  2.5701 +    (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
  2.5702 +/*
  2.5703 +** The MSVC CRT on Windows CE may not have a localtime() function.
  2.5704 +** So define a substitute.
  2.5705 +*/
  2.5706 +/* #  include <time.h> */
  2.5707  struct tm *__cdecl localtime(const time_t *t)
  2.5708  {
  2.5709    static struct tm y;
  2.5710 @@ -37105,6 +39040,10 @@
  2.5711  }
  2.5712  #endif
  2.5713  
  2.5714 +#if SQLITE_OS_WINCE
  2.5715 +/*************************************************************************
  2.5716 +** This section contains code for WinCE only.
  2.5717 +*/
  2.5718  #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
  2.5719  
  2.5720  /*
  2.5721 @@ -37135,7 +39074,7 @@
  2.5722    zName = winUtf8ToUnicode(zFilename);
  2.5723    if( zName==0 ){
  2.5724      /* out of memory */
  2.5725 -    return SQLITE_IOERR_NOMEM;
  2.5726 +    return SQLITE_IOERR_NOMEM_BKPT;
  2.5727    }
  2.5728  
  2.5729    /* Initialize the local lockdata */
  2.5730 @@ -38118,9 +40057,8 @@
  2.5731    ** the PENDING_LOCK byte is temporary.
  2.5732    */
  2.5733    newLocktype = pFile->locktype;
  2.5734 -  if(   (pFile->locktype==NO_LOCK)
  2.5735 -     || (   (locktype==EXCLUSIVE_LOCK)
  2.5736 -         && (pFile->locktype==RESERVED_LOCK))
  2.5737 +  if( pFile->locktype==NO_LOCK
  2.5738 +   || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
  2.5739    ){
  2.5740      int cnt = 3;
  2.5741      while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
  2.5742 @@ -38326,7 +40264,7 @@
  2.5743        OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  2.5744        return SQLITE_OK;
  2.5745      }
  2.5746 -    case SQLITE_LAST_ERRNO: {
  2.5747 +    case SQLITE_FCNTL_LAST_ERRNO: {
  2.5748        *(int*)pArg = (int)pFile->lastErrno;
  2.5749        OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  2.5750        return SQLITE_OK;
  2.5751 @@ -38684,12 +40622,12 @@
  2.5752    ** allocate space for a new winShmNode and filename.
  2.5753    */
  2.5754    p = sqlite3MallocZero( sizeof(*p) );
  2.5755 -  if( p==0 ) return SQLITE_IOERR_NOMEM;
  2.5756 +  if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
  2.5757    nName = sqlite3Strlen30(pDbFd->zPath);
  2.5758    pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
  2.5759    if( pNew==0 ){
  2.5760      sqlite3_free(p);
  2.5761 -    return SQLITE_IOERR_NOMEM;
  2.5762 +    return SQLITE_IOERR_NOMEM_BKPT;
  2.5763    }
  2.5764    pNew->zFilename = (char*)&pNew[1];
  2.5765    sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
  2.5766 @@ -38714,10 +40652,12 @@
  2.5767      pShmNode->pNext = winShmNodeList;
  2.5768      winShmNodeList = pShmNode;
  2.5769  
  2.5770 -    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  2.5771 -    if( pShmNode->mutex==0 ){
  2.5772 -      rc = SQLITE_IOERR_NOMEM;
  2.5773 -      goto shm_open_err;
  2.5774 +    if( sqlite3GlobalConfig.bCoreMutex ){
  2.5775 +      pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  2.5776 +      if( pShmNode->mutex==0 ){
  2.5777 +        rc = SQLITE_IOERR_NOMEM_BKPT;
  2.5778 +        goto shm_open_err;
  2.5779 +      }
  2.5780      }
  2.5781  
  2.5782      rc = winOpen(pDbFd->pVfs,
  2.5783 @@ -39021,7 +40961,7 @@
  2.5784          pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
  2.5785      );
  2.5786      if( !apNew ){
  2.5787 -      rc = SQLITE_IOERR_NOMEM;
  2.5788 +      rc = SQLITE_IOERR_NOMEM_BKPT;
  2.5789        goto shmpage_out;
  2.5790      }
  2.5791      pShmNode->aRegion = apNew;
  2.5792 @@ -39375,7 +41315,7 @@
  2.5793    }
  2.5794  #ifdef SQLITE_WIN32_HAS_ANSI
  2.5795    else{
  2.5796 -    zConverted = sqlite3_win32_mbcs_to_utf8(zFilename);
  2.5797 +    zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
  2.5798    }
  2.5799  #endif
  2.5800    /* caller will handle out of memory */
  2.5801 @@ -39396,7 +41336,7 @@
  2.5802    }
  2.5803  #ifdef SQLITE_WIN32_HAS_ANSI
  2.5804    else{
  2.5805 -    zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
  2.5806 +    zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
  2.5807    }
  2.5808  #endif
  2.5809    /* caller will handle out of memory */
  2.5810 @@ -39451,7 +41391,7 @@
  2.5811    zBuf = sqlite3MallocZero( nBuf );
  2.5812    if( !zBuf ){
  2.5813      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  2.5814 -    return SQLITE_IOERR_NOMEM;
  2.5815 +    return SQLITE_IOERR_NOMEM_BKPT;
  2.5816    }
  2.5817  
  2.5818    /* Figure out the effective temporary directory.  First, check if one
  2.5819 @@ -39509,7 +41449,7 @@
  2.5820          if( !zConverted ){
  2.5821            sqlite3_free(zBuf);
  2.5822            OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  2.5823 -          return SQLITE_IOERR_NOMEM;
  2.5824 +          return SQLITE_IOERR_NOMEM_BKPT;
  2.5825          }
  2.5826          if( winIsDir(zConverted) ){
  2.5827            sqlite3_snprintf(nMax, zBuf, "%s", zDir);
  2.5828 @@ -39522,7 +41462,7 @@
  2.5829          if( !zConverted ){
  2.5830            sqlite3_free(zBuf);
  2.5831            OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  2.5832 -          return SQLITE_IOERR_NOMEM;
  2.5833 +          return SQLITE_IOERR_NOMEM_BKPT;
  2.5834          }
  2.5835          if( cygwin_conv_path(
  2.5836                  osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
  2.5837 @@ -39543,7 +41483,7 @@
  2.5838              sqlite3_free(zConverted);
  2.5839              sqlite3_free(zBuf);
  2.5840              OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  2.5841 -            return SQLITE_IOERR_NOMEM;
  2.5842 +            return SQLITE_IOERR_NOMEM_BKPT;
  2.5843            }
  2.5844            sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
  2.5845            sqlite3_free(zUtf8);
  2.5846 @@ -39561,7 +41501,7 @@
  2.5847      if( !zWidePath ){
  2.5848        sqlite3_free(zBuf);
  2.5849        OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  2.5850 -      return SQLITE_IOERR_NOMEM;
  2.5851 +      return SQLITE_IOERR_NOMEM_BKPT;
  2.5852      }
  2.5853      if( osGetTempPathW(nMax, zWidePath)==0 ){
  2.5854        sqlite3_free(zWidePath);
  2.5855 @@ -39579,7 +41519,7 @@
  2.5856        sqlite3_free(zWidePath);
  2.5857        sqlite3_free(zBuf);
  2.5858        OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  2.5859 -      return SQLITE_IOERR_NOMEM;
  2.5860 +      return SQLITE_IOERR_NOMEM_BKPT;
  2.5861      }
  2.5862    }
  2.5863  #ifdef SQLITE_WIN32_HAS_ANSI
  2.5864 @@ -39589,7 +41529,7 @@
  2.5865      if( !zMbcsPath ){
  2.5866        sqlite3_free(zBuf);
  2.5867        OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  2.5868 -      return SQLITE_IOERR_NOMEM;
  2.5869 +      return SQLITE_IOERR_NOMEM_BKPT;
  2.5870      }
  2.5871      if( osGetTempPathA(nMax, zMbcsPath)==0 ){
  2.5872        sqlite3_free(zBuf);
  2.5873 @@ -39597,14 +41537,14 @@
  2.5874        return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
  2.5875                           "winGetTempname3", 0);
  2.5876      }
  2.5877 -    zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
  2.5878 +    zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
  2.5879      if( zUtf8 ){
  2.5880        sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
  2.5881        sqlite3_free(zUtf8);
  2.5882      }else{
  2.5883        sqlite3_free(zBuf);
  2.5884        OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  2.5885 -      return SQLITE_IOERR_NOMEM;
  2.5886 +      return SQLITE_IOERR_NOMEM_BKPT;
  2.5887      }
  2.5888    }
  2.5889  #endif /* SQLITE_WIN32_HAS_ANSI */
  2.5890 @@ -39796,7 +41736,7 @@
  2.5891    if( zConverted==0 ){
  2.5892      sqlite3_free(zTmpname);
  2.5893      OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
  2.5894 -    return SQLITE_IOERR_NOMEM;
  2.5895 +    return SQLITE_IOERR_NOMEM_BKPT;
  2.5896    }
  2.5897  
  2.5898    if( winIsDir(zConverted) ){
  2.5899 @@ -39996,7 +41936,7 @@
  2.5900    zConverted = winConvertFromUtf8Filename(zFilename);
  2.5901    if( zConverted==0 ){
  2.5902      OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
  2.5903 -    return SQLITE_IOERR_NOMEM;
  2.5904 +    return SQLITE_IOERR_NOMEM_BKPT;
  2.5905    }
  2.5906    if( osIsNT() ){
  2.5907      do {
  2.5908 @@ -40104,7 +42044,7 @@
  2.5909    zConverted = winConvertFromUtf8Filename(zFilename);
  2.5910    if( zConverted==0 ){
  2.5911      OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
  2.5912 -    return SQLITE_IOERR_NOMEM;
  2.5913 +    return SQLITE_IOERR_NOMEM_BKPT;
  2.5914    }
  2.5915    if( osIsNT() ){
  2.5916      int cnt = 0;
  2.5917 @@ -40231,7 +42171,7 @@
  2.5918      */
  2.5919      char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
  2.5920      if( !zOut ){
  2.5921 -      return SQLITE_IOERR_NOMEM;
  2.5922 +      return SQLITE_IOERR_NOMEM_BKPT;
  2.5923      }
  2.5924      if( cygwin_conv_path(
  2.5925              (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
  2.5926 @@ -40243,7 +42183,7 @@
  2.5927        char *zUtf8 = winConvertToUtf8Filename(zOut);
  2.5928        if( !zUtf8 ){
  2.5929          sqlite3_free(zOut);
  2.5930 -        return SQLITE_IOERR_NOMEM;
  2.5931 +        return SQLITE_IOERR_NOMEM_BKPT;
  2.5932        }
  2.5933        sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
  2.5934                         sqlite3_data_directory, winGetDirSep(), zUtf8);
  2.5935 @@ -40253,7 +42193,7 @@
  2.5936    }else{
  2.5937      char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
  2.5938      if( !zOut ){
  2.5939 -      return SQLITE_IOERR_NOMEM;
  2.5940 +      return SQLITE_IOERR_NOMEM_BKPT;
  2.5941      }
  2.5942      if( cygwin_conv_path(
  2.5943              (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
  2.5944 @@ -40265,7 +42205,7 @@
  2.5945        char *zUtf8 = winConvertToUtf8Filename(zOut);
  2.5946        if( !zUtf8 ){
  2.5947          sqlite3_free(zOut);
  2.5948 -        return SQLITE_IOERR_NOMEM;
  2.5949 +        return SQLITE_IOERR_NOMEM_BKPT;
  2.5950        }
  2.5951        sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
  2.5952        sqlite3_free(zUtf8);
  2.5953 @@ -40325,7 +42265,7 @@
  2.5954    }
  2.5955    zConverted = winConvertFromUtf8Filename(zRelative);
  2.5956    if( zConverted==0 ){
  2.5957 -    return SQLITE_IOERR_NOMEM;
  2.5958 +    return SQLITE_IOERR_NOMEM_BKPT;
  2.5959    }
  2.5960    if( osIsNT() ){
  2.5961      LPWSTR zTemp;
  2.5962 @@ -40339,7 +42279,7 @@
  2.5963      zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
  2.5964      if( zTemp==0 ){
  2.5965        sqlite3_free(zConverted);
  2.5966 -      return SQLITE_IOERR_NOMEM;
  2.5967 +      return SQLITE_IOERR_NOMEM_BKPT;
  2.5968      }
  2.5969      nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
  2.5970      if( nByte==0 ){
  2.5971 @@ -40365,7 +42305,7 @@
  2.5972      zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
  2.5973      if( zTemp==0 ){
  2.5974        sqlite3_free(zConverted);
  2.5975 -      return SQLITE_IOERR_NOMEM;
  2.5976 +      return SQLITE_IOERR_NOMEM_BKPT;
  2.5977      }
  2.5978      nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
  2.5979      if( nByte==0 ){
  2.5980 @@ -40375,7 +42315,7 @@
  2.5981                           "winFullPathname4", zRelative);
  2.5982      }
  2.5983      sqlite3_free(zConverted);
  2.5984 -    zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
  2.5985 +    zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
  2.5986      sqlite3_free(zTemp);
  2.5987    }
  2.5988  #endif
  2.5989 @@ -40384,7 +42324,7 @@
  2.5990      sqlite3_free(zOut);
  2.5991      return SQLITE_OK;
  2.5992    }else{
  2.5993 -    return SQLITE_IOERR_NOMEM;
  2.5994 +    return SQLITE_IOERR_NOMEM_BKPT;
  2.5995    }
  2.5996  #endif
  2.5997  }
  2.5998 @@ -40459,65 +42399,85 @@
  2.5999    #define winDlClose 0
  2.6000  #endif
  2.6001  
  2.6002 +/* State information for the randomness gatherer. */
  2.6003 +typedef struct EntropyGatherer EntropyGatherer;
  2.6004 +struct EntropyGatherer {
  2.6005 +  unsigned char *a;   /* Gather entropy into this buffer */
  2.6006 +  int na;             /* Size of a[] in bytes */
  2.6007 +  int i;              /* XOR next input into a[i] */
  2.6008 +  int nXor;           /* Number of XOR operations done */
  2.6009 +};
  2.6010 +
  2.6011 +#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
  2.6012 +/* Mix sz bytes of entropy into p. */
  2.6013 +static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
  2.6014 +  int j, k;
  2.6015 +  for(j=0, k=p->i; j<sz; j++){
  2.6016 +    p->a[k++] ^= x[j];
  2.6017 +    if( k>=p->na ) k = 0;
  2.6018 +  }
  2.6019 +  p->i = k;
  2.6020 +  p->nXor += sz;
  2.6021 +}
  2.6022 +#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
  2.6023  
  2.6024  /*
  2.6025  ** Write up to nBuf bytes of randomness into zBuf.
  2.6026  */
  2.6027  static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  2.6028 -  int n = 0;
  2.6029 +#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
  2.6030    UNUSED_PARAMETER(pVfs);
  2.6031 -#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
  2.6032 -  n = nBuf;
  2.6033    memset(zBuf, 0, nBuf);
  2.6034 -#else
  2.6035 -  if( sizeof(SYSTEMTIME)<=nBuf-n ){
  2.6036 +  return nBuf;
  2.6037 +#else
  2.6038 +  EntropyGatherer e;
  2.6039 +  UNUSED_PARAMETER(pVfs);
  2.6040 +  memset(zBuf, 0, nBuf);
  2.6041 +#if defined(_MSC_VER) && _MSC_VER>=1400 && !SQLITE_OS_WINCE
  2.6042 +  rand_s((unsigned int*)zBuf); /* rand_s() is not available with MinGW */
  2.6043 +#endif /* defined(_MSC_VER) && _MSC_VER>=1400 */
  2.6044 +  e.a = (unsigned char*)zBuf;
  2.6045 +  e.na = nBuf;
  2.6046 +  e.nXor = 0;
  2.6047 +  e.i = 0;
  2.6048 +  {
  2.6049      SYSTEMTIME x;
  2.6050      osGetSystemTime(&x);
  2.6051 -    memcpy(&zBuf[n], &x, sizeof(x));
  2.6052 -    n += sizeof(x);
  2.6053 -  }
  2.6054 -  if( sizeof(DWORD)<=nBuf-n ){
  2.6055 +    xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
  2.6056 +  }
  2.6057 +  {
  2.6058      DWORD pid = osGetCurrentProcessId();
  2.6059 -    memcpy(&zBuf[n], &pid, sizeof(pid));
  2.6060 -    n += sizeof(pid);
  2.6061 +    xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
  2.6062    }
  2.6063  #if SQLITE_OS_WINRT
  2.6064 -  if( sizeof(ULONGLONG)<=nBuf-n ){
  2.6065 +  {
  2.6066      ULONGLONG cnt = osGetTickCount64();
  2.6067 -    memcpy(&zBuf[n], &cnt, sizeof(cnt));
  2.6068 -    n += sizeof(cnt);
  2.6069 -  }
  2.6070 -#else
  2.6071 -  if( sizeof(DWORD)<=nBuf-n ){
  2.6072 +    xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
  2.6073 +  }
  2.6074 +#else
  2.6075 +  {
  2.6076      DWORD cnt = osGetTickCount();
  2.6077 -    memcpy(&zBuf[n], &cnt, sizeof(cnt));
  2.6078 -    n += sizeof(cnt);
  2.6079 -  }
  2.6080 -#endif
  2.6081 -  if( sizeof(LARGE_INTEGER)<=nBuf-n ){
  2.6082 +    xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
  2.6083 +  }
  2.6084 +#endif /* SQLITE_OS_WINRT */
  2.6085 +  {
  2.6086      LARGE_INTEGER i;
  2.6087      osQueryPerformanceCounter(&i);
  2.6088 -    memcpy(&zBuf[n], &i, sizeof(i));
  2.6089 -    n += sizeof(i);
  2.6090 +    xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
  2.6091    }
  2.6092  #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
  2.6093 -  if( sizeof(UUID)<=nBuf-n ){
  2.6094 +  {
  2.6095      UUID id;
  2.6096      memset(&id, 0, sizeof(UUID));
  2.6097      osUuidCreate(&id);
  2.6098 -    memcpy(&zBuf[n], &id, sizeof(UUID));
  2.6099 -    n += sizeof(UUID);
  2.6100 -  }
  2.6101 -  if( sizeof(UUID)<=nBuf-n ){
  2.6102 -    UUID id;
  2.6103 +    xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
  2.6104      memset(&id, 0, sizeof(UUID));
  2.6105      osUuidCreateSequential(&id);
  2.6106 -    memcpy(&zBuf[n], &id, sizeof(UUID));
  2.6107 -    n += sizeof(UUID);
  2.6108 -  }
  2.6109 -#endif
  2.6110 -#endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */
  2.6111 -  return n;
  2.6112 +    xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
  2.6113 +  }
  2.6114 +#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
  2.6115 +  return e.nXor>nBuf ? nBuf : e.nXor;
  2.6116 +#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
  2.6117  }
  2.6118  
  2.6119  
  2.6120 @@ -40633,8 +42593,10 @@
  2.6121  ** sqlite3_errmsg(), possibly making IO errors easier to debug.
  2.6122  */
  2.6123  static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  2.6124 +  DWORD e = osGetLastError();
  2.6125    UNUSED_PARAMETER(pVfs);
  2.6126 -  return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
  2.6127 +  if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
  2.6128 +  return e;
  2.6129  }
  2.6130  
  2.6131  /*
  2.6132 @@ -40908,7 +42870,7 @@
  2.6133      i = i%p->iDivisor;
  2.6134      if( p->u.apSub[bin]==0 ){
  2.6135        p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
  2.6136 -      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
  2.6137 +      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
  2.6138      }
  2.6139      p = p->u.apSub[bin];
  2.6140    }
  2.6141 @@ -40943,7 +42905,7 @@
  2.6142      int rc;
  2.6143      u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
  2.6144      if( aiValues==0 ){
  2.6145 -      return SQLITE_NOMEM;
  2.6146 +      return SQLITE_NOMEM_BKPT;
  2.6147      }else{
  2.6148        memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
  2.6149        memset(p->u.apSub, 0, sizeof(p->u.apSub));
  2.6150 @@ -41159,7 +43121,29 @@
  2.6151  /* #include "sqliteInt.h" */
  2.6152  
  2.6153  /*
  2.6154 -** A complete page cache is an instance of this structure.
  2.6155 +** A complete page cache is an instance of this structure.  Every
  2.6156 +** entry in the cache holds a single page of the database file.  The
  2.6157 +** btree layer only operates on the cached copy of the database pages.
  2.6158 +**
  2.6159 +** A page cache entry is "clean" if it exactly matches what is currently
  2.6160 +** on disk.  A page is "dirty" if it has been modified and needs to be
  2.6161 +** persisted to disk.
  2.6162 +**
  2.6163 +** pDirty, pDirtyTail, pSynced:
  2.6164 +**   All dirty pages are linked into the doubly linked list using
  2.6165 +**   PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
  2.6166 +**   such that p was added to the list more recently than p->pDirtyNext.
  2.6167 +**   PCache.pDirty points to the first (newest) element in the list and
  2.6168 +**   pDirtyTail to the last (oldest).
  2.6169 +**
  2.6170 +**   The PCache.pSynced variable is used to optimize searching for a dirty
  2.6171 +**   page to eject from the cache mid-transaction. It is better to eject
  2.6172 +**   a page that does not require a journal sync than one that does. 
  2.6173 +**   Therefore, pSynced is maintained to that it *almost* always points
  2.6174 +**   to either the oldest page in the pDirty/pDirtyTail list that has a
  2.6175 +**   clear PGHDR_NEED_SYNC flag or to a page that is older than this one
  2.6176 +**   (so that the right page to eject can be found by following pDirtyPrev
  2.6177 +**   pointers).
  2.6178  */
  2.6179  struct PCache {
  2.6180    PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
  2.6181 @@ -41176,6 +43160,95 @@
  2.6182    sqlite3_pcache *pCache;             /* Pluggable cache module */
  2.6183  };
  2.6184  
  2.6185 +/********************************** Test and Debug Logic **********************/
  2.6186 +/*
  2.6187 +** Debug tracing macros.  Enable by by changing the "0" to "1" and
  2.6188 +** recompiling.
  2.6189 +**
  2.6190 +** When sqlite3PcacheTrace is 1, single line trace messages are issued.
  2.6191 +** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
  2.6192 +** is displayed for many operations, resulting in a lot of output.
  2.6193 +*/
  2.6194 +#if defined(SQLITE_DEBUG) && 0
  2.6195 +  int sqlite3PcacheTrace = 2;       /* 0: off  1: simple  2: cache dumps */
  2.6196 +  int sqlite3PcacheMxDump = 9999;   /* Max cache entries for pcacheDump() */
  2.6197 +# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
  2.6198 +  void pcacheDump(PCache *pCache){
  2.6199 +    int N;
  2.6200 +    int i, j;
  2.6201 +    sqlite3_pcache_page *pLower;
  2.6202 +    PgHdr *pPg;
  2.6203 +    unsigned char *a;
  2.6204 +  
  2.6205 +    if( sqlite3PcacheTrace<2 ) return;
  2.6206 +    if( pCache->pCache==0 ) return;
  2.6207 +    N = sqlite3PcachePagecount(pCache);
  2.6208 +    if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
  2.6209 +    for(i=1; i<=N; i++){
  2.6210 +       pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
  2.6211 +       if( pLower==0 ) continue;
  2.6212 +       pPg = (PgHdr*)pLower->pExtra;
  2.6213 +       printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
  2.6214 +       a = (unsigned char *)pLower->pBuf;
  2.6215 +       for(j=0; j<12; j++) printf("%02x", a[j]);
  2.6216 +       printf("\n");
  2.6217 +       if( pPg->pPage==0 ){
  2.6218 +         sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
  2.6219 +       }
  2.6220 +    }
  2.6221 +  }
  2.6222 +  #else
  2.6223 +# define pcacheTrace(X)
  2.6224 +# define pcacheDump(X)
  2.6225 +#endif
  2.6226 +
  2.6227 +/*
  2.6228 +** Check invariants on a PgHdr entry.  Return true if everything is OK.
  2.6229 +** Return false if any invariant is violated.
  2.6230 +**
  2.6231 +** This routine is for use inside of assert() statements only.  For
  2.6232 +** example:
  2.6233 +**
  2.6234 +**          assert( sqlite3PcachePageSanity(pPg) );
  2.6235 +*/
  2.6236 +#if SQLITE_DEBUG
  2.6237 +SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
  2.6238 +  PCache *pCache;
  2.6239 +  assert( pPg!=0 );
  2.6240 +  assert( pPg->pgno>0 );    /* Page number is 1 or more */
  2.6241 +  pCache = pPg->pCache;
  2.6242 +  assert( pCache!=0 );      /* Every page has an associated PCache */
  2.6243 +  if( pPg->flags & PGHDR_CLEAN ){
  2.6244 +    assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
  2.6245 +    assert( pCache->pDirty!=pPg );          /* CLEAN pages not on dirty list */
  2.6246 +    assert( pCache->pDirtyTail!=pPg );
  2.6247 +  }
  2.6248 +  /* WRITEABLE pages must also be DIRTY */
  2.6249 +  if( pPg->flags & PGHDR_WRITEABLE ){
  2.6250 +    assert( pPg->flags & PGHDR_DIRTY );     /* WRITEABLE implies DIRTY */
  2.6251 +  }
  2.6252 +  /* NEED_SYNC can be set independently of WRITEABLE.  This can happen,
  2.6253 +  ** for example, when using the sqlite3PagerDontWrite() optimization:
  2.6254 +  **    (1)  Page X is journalled, and gets WRITEABLE and NEED_SEEK.
  2.6255 +  **    (2)  Page X moved to freelist, WRITEABLE is cleared
  2.6256 +  **    (3)  Page X reused, WRITEABLE is set again
  2.6257 +  ** If NEED_SYNC had been cleared in step 2, then it would not be reset
  2.6258 +  ** in step 3, and page might be written into the database without first
  2.6259 +  ** syncing the rollback journal, which might cause corruption on a power
  2.6260 +  ** loss.
  2.6261 +  **
  2.6262 +  ** Another example is when the database page size is smaller than the
  2.6263 +  ** disk sector size.  When any page of a sector is journalled, all pages
  2.6264 +  ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
  2.6265 +  ** in case they are later modified, since all pages in the same sector
  2.6266 +  ** must be journalled and synced before any of those pages can be safely
  2.6267 +  ** written.
  2.6268 +  */
  2.6269 +  return 1;
  2.6270 +}
  2.6271 +#endif /* SQLITE_DEBUG */
  2.6272 +
  2.6273 +
  2.6274  /********************************** Linked List Management ********************/
  2.6275  
  2.6276  /* Allowed values for second argument to pcacheManageDirtyList() */
  2.6277 @@ -41192,17 +43265,16 @@
  2.6278  static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
  2.6279    PCache *p = pPage->pCache;
  2.6280  
  2.6281 +  pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
  2.6282 +                addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
  2.6283 +                pPage->pgno));
  2.6284    if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
  2.6285      assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
  2.6286      assert( pPage->pDirtyPrev || pPage==p->pDirty );
  2.6287    
  2.6288      /* Update the PCache1.pSynced variable if necessary. */
  2.6289      if( p->pSynced==pPage ){
  2.6290 -      PgHdr *pSynced = pPage->pDirtyPrev;
  2.6291 -      while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
  2.6292 -        pSynced = pSynced->pDirtyPrev;
  2.6293 -      }
  2.6294 -      p->pSynced = pSynced;
  2.6295 +      p->pSynced = pPage->pDirtyPrev;
  2.6296      }
  2.6297    
  2.6298      if( pPage->pDirtyNext ){
  2.6299 @@ -41214,10 +43286,15 @@
  2.6300      if( pPage->pDirtyPrev ){
  2.6301        pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
  2.6302      }else{
  2.6303 +      /* If there are now no dirty pages in the cache, set eCreate to 2. 
  2.6304 +      ** This is an optimization that allows sqlite3PcacheFetch() to skip
  2.6305 +      ** searching for a dirty page to eject from the cache when it might
  2.6306 +      ** otherwise have to.  */
  2.6307        assert( pPage==p->pDirty );
  2.6308        p->pDirty = pPage->pDirtyNext;
  2.6309 -      if( p->pDirty==0 && p->bPurgeable ){
  2.6310 -        assert( p->eCreate==1 );
  2.6311 +      assert( p->bPurgeable || p->eCreate==2 );
  2.6312 +      if( p->pDirty==0 ){         /*OPTIMIZATION-IF-TRUE*/
  2.6313 +        assert( p->bPurgeable==0 || p->eCreate==1 );
  2.6314          p->eCreate = 2;
  2.6315        }
  2.6316      }
  2.6317 @@ -41239,10 +43316,19 @@
  2.6318        }
  2.6319      }
  2.6320      p->pDirty = pPage;
  2.6321 -    if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
  2.6322 +
  2.6323 +    /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
  2.6324 +    ** pSynced to point to it. Checking the NEED_SYNC flag is an 
  2.6325 +    ** optimization, as if pSynced points to a page with the NEED_SYNC
  2.6326 +    ** flag set sqlite3PcacheFetchStress() searches through all newer 
  2.6327 +    ** entries of the dirty-list for a page with NEED_SYNC clear anyway.  */
  2.6328 +    if( !p->pSynced 
  2.6329 +     && 0==(pPage->flags&PGHDR_NEED_SYNC)   /*OPTIMIZATION-IF-FALSE*/
  2.6330 +    ){
  2.6331        p->pSynced = pPage;
  2.6332      }
  2.6333    }
  2.6334 +  pcacheDump(p);
  2.6335  }
  2.6336  
  2.6337  /*
  2.6338 @@ -41251,7 +43337,9 @@
  2.6339  */
  2.6340  static void pcacheUnpin(PgHdr *p){
  2.6341    if( p->pCache->bPurgeable ){
  2.6342 +    pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
  2.6343      sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
  2.6344 +    pcacheDump(p->pCache);
  2.6345    }
  2.6346  }
  2.6347  
  2.6348 @@ -41321,6 +43409,7 @@
  2.6349    p->pStress = pStress;
  2.6350    p->szCache = 100;
  2.6351    p->szSpill = 1;
  2.6352 +  pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
  2.6353    return sqlite3PcacheSetPageSize(p, szPage);
  2.6354  }
  2.6355  
  2.6356 @@ -41336,13 +43425,14 @@
  2.6357                  szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
  2.6358                  pCache->bPurgeable
  2.6359      );
  2.6360 -    if( pNew==0 ) return SQLITE_NOMEM;
  2.6361 +    if( pNew==0 ) return SQLITE_NOMEM_BKPT;
  2.6362      sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
  2.6363      if( pCache->pCache ){
  2.6364        sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
  2.6365      }
  2.6366      pCache->pCache = pNew;
  2.6367      pCache->szPage = szPage;
  2.6368 +    pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
  2.6369    }
  2.6370    return SQLITE_OK;
  2.6371  }
  2.6372 @@ -41377,11 +43467,13 @@
  2.6373    int createFlag        /* If true, create page if it does not exist already */
  2.6374  ){
  2.6375    int eCreate;
  2.6376 +  sqlite3_pcache_page *pRes;
  2.6377  
  2.6378    assert( pCache!=0 );
  2.6379    assert( pCache->pCache!=0 );
  2.6380    assert( createFlag==3 || createFlag==0 );
  2.6381    assert( pgno>0 );
  2.6382 +  assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
  2.6383  
  2.6384    /* eCreate defines what to do if the page does not exist.
  2.6385    **    0     Do not allocate a new page.  (createFlag==0)
  2.6386 @@ -41394,12 +43486,15 @@
  2.6387    assert( eCreate==0 || eCreate==1 || eCreate==2 );
  2.6388    assert( createFlag==0 || pCache->eCreate==eCreate );
  2.6389    assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
  2.6390 -  return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
  2.6391 +  pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
  2.6392 +  pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
  2.6393 +               createFlag?" create":"",pRes));
  2.6394 +  return pRes;
  2.6395  }
  2.6396  
  2.6397  /*
  2.6398  ** If the sqlite3PcacheFetch() routine is unable to allocate a new
  2.6399 -** page because new clean pages are available for reuse and the cache
  2.6400 +** page because no clean pages are available for reuse and the cache
  2.6401  ** size limit has been reached, then this routine can be invoked to 
  2.6402  ** try harder to allocate a page.  This routine might invoke the stress
  2.6403  ** callback to spill dirty pages to the journal.  It will then try to
  2.6404 @@ -41421,7 +43516,11 @@
  2.6405      ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
  2.6406      ** cleared), but if that is not possible settle for any other 
  2.6407      ** unreferenced dirty page.
  2.6408 -    */
  2.6409 +    **
  2.6410 +    ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
  2.6411 +    ** flag is currently referenced, then the following may leave pSynced
  2.6412 +    ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
  2.6413 +    ** cleared). This is Ok, as pSynced is just an optimization.  */
  2.6414      for(pPg=pCache->pSynced; 
  2.6415          pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
  2.6416          pPg=pPg->pDirtyPrev
  2.6417 @@ -41439,14 +43538,16 @@
  2.6418                    sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
  2.6419                  numberOfCachePages(pCache));
  2.6420  #endif
  2.6421 +      pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
  2.6422        rc = pCache->xStress(pCache->pStress, pPg);
  2.6423 +      pcacheDump(pCache);
  2.6424        if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
  2.6425          return rc;
  2.6426        }
  2.6427      }
  2.6428    }
  2.6429    *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
  2.6430 -  return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK;
  2.6431 +  return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
  2.6432  }
  2.6433  
  2.6434  /*
  2.6435 @@ -41499,6 +43600,7 @@
  2.6436    }
  2.6437    pCache->nRefSum++;
  2.6438    pPgHdr->nRef++;
  2.6439 +  assert( sqlite3PcachePageSanity(pPgHdr) );
  2.6440    return pPgHdr;
  2.6441  }
  2.6442  
  2.6443 @@ -41512,8 +43614,11 @@
  2.6444    if( (--p->nRef)==0 ){
  2.6445      if( p->flags&PGHDR_CLEAN ){
  2.6446        pcacheUnpin(p);
  2.6447 -    }else if( p->pDirtyPrev!=0 ){
  2.6448 -      /* Move the page to the head of the dirty list. */
  2.6449 +    }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/
  2.6450 +      /* Move the page to the head of the dirty list. If p->pDirtyPrev==0,
  2.6451 +      ** then page p is already at the head of the dirty list and the
  2.6452 +      ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE
  2.6453 +      ** tag above.  */
  2.6454        pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
  2.6455      }
  2.6456    }
  2.6457 @@ -41524,6 +43629,7 @@
  2.6458  */
  2.6459  SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
  2.6460    assert(p->nRef>0);
  2.6461 +  assert( sqlite3PcachePageSanity(p) );
  2.6462    p->nRef++;
  2.6463    p->pCache->nRefSum++;
  2.6464  }
  2.6465 @@ -41535,6 +43641,7 @@
  2.6466  */
  2.6467  SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
  2.6468    assert( p->nRef==1 );
  2.6469 +  assert( sqlite3PcachePageSanity(p) );
  2.6470    if( p->flags&PGHDR_DIRTY ){
  2.6471      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
  2.6472    }
  2.6473 @@ -41548,13 +43655,16 @@
  2.6474  */
  2.6475  SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
  2.6476    assert( p->nRef>0 );
  2.6477 -  if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){
  2.6478 +  assert( sqlite3PcachePageSanity(p) );
  2.6479 +  if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){    /*OPTIMIZATION-IF-FALSE*/
  2.6480      p->flags &= ~PGHDR_DONT_WRITE;
  2.6481      if( p->flags & PGHDR_CLEAN ){
  2.6482        p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
  2.6483 +      pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
  2.6484        assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
  2.6485        pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
  2.6486      }
  2.6487 +    assert( sqlite3PcachePageSanity(p) );
  2.6488    }
  2.6489  }
  2.6490  
  2.6491 @@ -41563,11 +43673,14 @@
  2.6492  ** make it so.
  2.6493  */
  2.6494  SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
  2.6495 -  if( (p->flags & PGHDR_DIRTY) ){
  2.6496 +  assert( sqlite3PcachePageSanity(p) );
  2.6497 +  if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){
  2.6498      assert( (p->flags & PGHDR_CLEAN)==0 );
  2.6499      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
  2.6500      p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
  2.6501      p->flags |= PGHDR_CLEAN;
  2.6502 +    pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
  2.6503 +    assert( sqlite3PcachePageSanity(p) );
  2.6504      if( p->nRef==0 ){
  2.6505        pcacheUnpin(p);
  2.6506      }
  2.6507 @@ -41579,12 +43692,25 @@
  2.6508  */
  2.6509  SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
  2.6510    PgHdr *p;
  2.6511 +  pcacheTrace(("%p.CLEAN-ALL\n",pCache));
  2.6512    while( (p = pCache->pDirty)!=0 ){
  2.6513      sqlite3PcacheMakeClean(p);
  2.6514    }
  2.6515  }
  2.6516  
  2.6517  /*
  2.6518 +** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
  2.6519 +*/
  2.6520 +SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){
  2.6521 +  PgHdr *p;
  2.6522 +  pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
  2.6523 +  for(p=pCache->pDirty; p; p=p->pDirtyNext){
  2.6524 +    p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
  2.6525 +  }
  2.6526 +  pCache->pSynced = pCache->pDirtyTail;
  2.6527 +}
  2.6528 +
  2.6529 +/*
  2.6530  ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
  2.6531  */
  2.6532  SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
  2.6533 @@ -41602,6 +43728,8 @@
  2.6534    PCache *pCache = p->pCache;
  2.6535    assert( p->nRef>0 );
  2.6536    assert( newPgno>0 );
  2.6537 +  assert( sqlite3PcachePageSanity(p) );
  2.6538 +  pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
  2.6539    sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
  2.6540    p->pgno = newPgno;
  2.6541    if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
  2.6542 @@ -41622,6 +43750,7 @@
  2.6543    if( pCache->pCache ){
  2.6544      PgHdr *p;
  2.6545      PgHdr *pNext;
  2.6546 +    pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
  2.6547      for(p=pCache->pDirty; p; p=pNext){
  2.6548        pNext = p->pDirtyNext;
  2.6549        /* This routine never gets call with a positive pgno except right
  2.6550 @@ -41629,7 +43758,7 @@
  2.6551        ** it must be that pgno==0.
  2.6552        */
  2.6553        assert( p->pgno>0 );
  2.6554 -      if( ALWAYS(p->pgno>pgno) ){
  2.6555 +      if( p->pgno>pgno ){
  2.6556          assert( p->flags&PGHDR_DIRTY );
  2.6557          sqlite3PcacheMakeClean(p);
  2.6558        }
  2.6559 @@ -41652,6 +43781,7 @@
  2.6560  */
  2.6561  SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
  2.6562    assert( pCache->pCache!=0 );
  2.6563 +  pcacheTrace(("%p.CLOSE\n",pCache));
  2.6564    sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
  2.6565  }
  2.6566  
  2.6567 @@ -41820,6 +43950,17 @@
  2.6568  */
  2.6569  SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
  2.6570  
  2.6571 +/*
  2.6572 +** Return the number of dirty pages currently in the cache, as a percentage
  2.6573 +** of the configured cache size.
  2.6574 +*/
  2.6575 +SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
  2.6576 +  PgHdr *pDirty;
  2.6577 +  int nDirty = 0;
  2.6578 +  int nCache = numberOfCachePages(pCache);
  2.6579 +  for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
  2.6580 +  return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
  2.6581 +}
  2.6582  
  2.6583  #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
  2.6584  /*
  2.6585 @@ -42187,7 +44328,6 @@
  2.6586  ** Free an allocated buffer obtained from pcache1Alloc().
  2.6587  */
  2.6588  static void pcache1Free(void *p){
  2.6589 -  int nFreed = 0;
  2.6590    if( p==0 ) return;
  2.6591    if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
  2.6592      PgFreeslot *pSlot;
  2.6593 @@ -42204,10 +44344,13 @@
  2.6594      assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
  2.6595      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
  2.6596  #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
  2.6597 -    nFreed = sqlite3MallocSize(p);
  2.6598 -    sqlite3_mutex_enter(pcache1.mutex);
  2.6599 -    sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
  2.6600 -    sqlite3_mutex_leave(pcache1.mutex);
  2.6601 +    {
  2.6602 +      int nFreed = 0;
  2.6603 +      nFreed = sqlite3MallocSize(p);
  2.6604 +      sqlite3_mutex_enter(pcache1.mutex);
  2.6605 +      sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
  2.6606 +      sqlite3_mutex_leave(pcache1.mutex);
  2.6607 +    }
  2.6608  #endif
  2.6609      sqlite3_free(p);
  2.6610    }
  2.6611 @@ -42527,8 +44670,8 @@
  2.6612  
  2.6613  #if SQLITE_THREADSAFE
  2.6614    if( sqlite3GlobalConfig.bCoreMutex ){
  2.6615 -    pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
  2.6616 -    pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
  2.6617 +    pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
  2.6618 +    pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
  2.6619    }
  2.6620  #endif
  2.6621    if( pcache1.separateCache
  2.6622 @@ -43134,8 +45277,9 @@
  2.6623  ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
  2.6624  ** primitives are constant time.  The cost of DESTROY is O(N).
  2.6625  **
  2.6626 -** There is an added cost of O(N) when switching between TEST and
  2.6627 -** SMALLEST primitives.
  2.6628 +** TEST and SMALLEST may not be used by the same RowSet.  This used to
  2.6629 +** be possible, but the feature was not used, so it was removed in order
  2.6630 +** to simplify the code.
  2.6631  */
  2.6632  /* #include "sqliteInt.h" */
  2.6633  
  2.6634 @@ -43256,7 +45400,9 @@
  2.6635  */
  2.6636  static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
  2.6637    assert( p!=0 );
  2.6638 -  if( p->nFresh==0 ){
  2.6639 +  if( p->nFresh==0 ){  /*OPTIMIZATION-IF-FALSE*/
  2.6640 +    /* We could allocate a fresh RowSetEntry each time one is needed, but it
  2.6641 +    ** is more efficient to pull a preallocated entry from the pool */
  2.6642      struct RowSetChunk *pNew;
  2.6643      pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
  2.6644      if( pNew==0 ){
  2.6645 @@ -43290,7 +45436,9 @@
  2.6646    pEntry->pRight = 0;
  2.6647    pLast = p->pLast;
  2.6648    if( pLast ){
  2.6649 -    if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
  2.6650 +    if( rowid<=pLast->v ){  /*OPTIMIZATION-IF-FALSE*/
  2.6651 +      /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
  2.6652 +      ** where possible */
  2.6653        p->rsFlags &= ~ROWSET_SORTED;
  2.6654      }
  2.6655      pLast->pRight = pEntry;
  2.6656 @@ -43412,23 +45560,29 @@
  2.6657  ){
  2.6658    struct RowSetEntry *p;         /* Root of the new tree */
  2.6659    struct RowSetEntry *pLeft;     /* Left subtree */
  2.6660 -  if( *ppList==0 ){
  2.6661 -    return 0;
  2.6662 -  }
  2.6663 -  if( iDepth==1 ){
  2.6664 +  if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
  2.6665 +    /* Prevent unnecessary deep recursion when we run out of entries */
  2.6666 +    return 0; 
  2.6667 +  }
  2.6668 +  if( iDepth>1 ){   /*OPTIMIZATION-IF-TRUE*/
  2.6669 +    /* This branch causes a *balanced* tree to be generated.  A valid tree
  2.6670 +    ** is still generated without this branch, but the tree is wildly
  2.6671 +    ** unbalanced and inefficient. */
  2.6672 +    pLeft = rowSetNDeepTree(ppList, iDepth-1);
  2.6673 +    p = *ppList;
  2.6674 +    if( p==0 ){     /*OPTIMIZATION-IF-FALSE*/
  2.6675 +      /* It is safe to always return here, but the resulting tree
  2.6676 +      ** would be unbalanced */
  2.6677 +      return pLeft;
  2.6678 +    }
  2.6679 +    p->pLeft = pLeft;
  2.6680 +    *ppList = p->pRight;
  2.6681 +    p->pRight = rowSetNDeepTree(ppList, iDepth-1);
  2.6682 +  }else{
  2.6683      p = *ppList;
  2.6684      *ppList = p->pRight;
  2.6685      p->pLeft = p->pRight = 0;
  2.6686 -    return p;
  2.6687 -  }
  2.6688 -  pLeft = rowSetNDeepTree(ppList, iDepth-1);
  2.6689 -  p = *ppList;
  2.6690 -  if( p==0 ){
  2.6691 -    return pLeft;
  2.6692 -  }
  2.6693 -  p->pLeft = pLeft;
  2.6694 -  *ppList = p->pRight;
  2.6695 -  p->pRight = rowSetNDeepTree(ppList, iDepth-1);
  2.6696 +  }
  2.6697    return p;
  2.6698  }
  2.6699  
  2.6700 @@ -43456,58 +45610,36 @@
  2.6701  }
  2.6702  
  2.6703  /*
  2.6704 -** Take all the entries on p->pEntry and on the trees in p->pForest and
  2.6705 -** sort them all together into one big ordered list on p->pEntry.
  2.6706 -**
  2.6707 -** This routine should only be called once in the life of a RowSet.
  2.6708 -*/
  2.6709 -static void rowSetToList(RowSet *p){
  2.6710 -
  2.6711 -  /* This routine is called only once */
  2.6712 -  assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
  2.6713 -
  2.6714 -  if( (p->rsFlags & ROWSET_SORTED)==0 ){
  2.6715 -    p->pEntry = rowSetEntrySort(p->pEntry);
  2.6716 -  }
  2.6717 -
  2.6718 -  /* While this module could theoretically support it, sqlite3RowSetNext()
  2.6719 -  ** is never called after sqlite3RowSetText() for the same RowSet.  So
  2.6720 -  ** there is never a forest to deal with.  Should this change, simply
  2.6721 -  ** remove the assert() and the #if 0. */
  2.6722 -  assert( p->pForest==0 );
  2.6723 -#if 0
  2.6724 -  while( p->pForest ){
  2.6725 -    struct RowSetEntry *pTree = p->pForest->pLeft;
  2.6726 -    if( pTree ){
  2.6727 -      struct RowSetEntry *pHead, *pTail;
  2.6728 -      rowSetTreeToList(pTree, &pHead, &pTail);
  2.6729 -      p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
  2.6730 -    }
  2.6731 -    p->pForest = p->pForest->pRight;
  2.6732 -  }
  2.6733 -#endif
  2.6734 -  p->rsFlags |= ROWSET_NEXT;  /* Verify this routine is never called again */
  2.6735 -}
  2.6736 -
  2.6737 -/*
  2.6738  ** Extract the smallest element from the RowSet.
  2.6739  ** Write the element into *pRowid.  Return 1 on success.  Return
  2.6740  ** 0 if the RowSet is already empty.
  2.6741  **
  2.6742  ** After this routine has been called, the sqlite3RowSetInsert()
  2.6743 -** routine may not be called again.  
  2.6744 +** routine may not be called again.
  2.6745 +**
  2.6746 +** This routine may not be called after sqlite3RowSetTest() has
  2.6747 +** been used.  Older versions of RowSet allowed that, but as the
  2.6748 +** capability was not used by the code generator, it was removed
  2.6749 +** for code economy.
  2.6750  */
  2.6751  SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
  2.6752    assert( p!=0 );
  2.6753 +  assert( p->pForest==0 );  /* Cannot be used with sqlite3RowSetText() */
  2.6754  
  2.6755    /* Merge the forest into a single sorted list on first call */
  2.6756 -  if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
  2.6757 +  if( (p->rsFlags & ROWSET_NEXT)==0 ){  /*OPTIMIZATION-IF-FALSE*/
  2.6758 +    if( (p->rsFlags & ROWSET_SORTED)==0 ){  /*OPTIMIZATION-IF-FALSE*/
  2.6759 +      p->pEntry = rowSetEntrySort(p->pEntry);
  2.6760 +    }
  2.6761 +    p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
  2.6762 +  }
  2.6763  
  2.6764    /* Return the next entry on the list */
  2.6765    if( p->pEntry ){
  2.6766      *pRowid = p->pEntry->v;
  2.6767      p->pEntry = p->pEntry->pRight;
  2.6768 -    if( p->pEntry==0 ){
  2.6769 +    if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
  2.6770 +      /* Free memory immediately, rather than waiting on sqlite3_finalize() */
  2.6771        sqlite3RowSetClear(p);
  2.6772      }
  2.6773      return 1;
  2.6774 @@ -43530,13 +45662,15 @@
  2.6775    /* This routine is never called after sqlite3RowSetNext() */
  2.6776    assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
  2.6777  
  2.6778 -  /* Sort entries into the forest on the first test of a new batch 
  2.6779 -  */
  2.6780 -  if( iBatch!=pRowSet->iBatch ){
  2.6781 +  /* Sort entries into the forest on the first test of a new batch.
  2.6782 +  ** To save unnecessary work, only do this when the batch number changes.
  2.6783 +  */
  2.6784 +  if( iBatch!=pRowSet->iBatch ){  /*OPTIMIZATION-IF-FALSE*/
  2.6785      p = pRowSet->pEntry;
  2.6786      if( p ){
  2.6787        struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
  2.6788 -      if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
  2.6789 +      if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
  2.6790 +        /* Only sort the current set of entiries if they need it */
  2.6791          p = rowSetEntrySort(p);
  2.6792        }
  2.6793        for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
  2.6794 @@ -44166,19 +46300,6 @@
  2.6795  */
  2.6796  #define MAX_SECTOR_SIZE 0x10000
  2.6797  
  2.6798 -/*
  2.6799 -** If the option SQLITE_EXTRA_DURABLE option is set at compile-time, then
  2.6800 -** SQLite will do extra fsync() operations when synchronous==FULL to help
  2.6801 -** ensure that transactions are durable across a power failure.  Most
  2.6802 -** applications are happy as long as transactions are consistent across
  2.6803 -** a power failure, and are perfectly willing to lose the last transaction
  2.6804 -** in exchange for the extra performance of avoiding directory syncs.
  2.6805 -** And so the default SQLITE_EXTRA_DURABLE setting is off.
  2.6806 -*/
  2.6807 -#ifndef SQLITE_EXTRA_DURABLE
  2.6808 -# define SQLITE_EXTRA_DURABLE 0
  2.6809 -#endif
  2.6810 -
  2.6811  
  2.6812  /*
  2.6813  ** An instance of the following structure is allocated for each active
  2.6814 @@ -44623,6 +46744,7 @@
  2.6815    ** state.
  2.6816    */
  2.6817    if( MEMDB ){
  2.6818 +    assert( !isOpen(p->fd) );
  2.6819      assert( p->noSync );
  2.6820      assert( p->journalMode==PAGER_JOURNALMODE_OFF 
  2.6821           || p->journalMode==PAGER_JOURNALMODE_MEMORY 
  2.6822 @@ -44709,7 +46831,7 @@
  2.6823        ** back to OPEN state.
  2.6824        */
  2.6825        assert( pPager->errCode!=SQLITE_OK );
  2.6826 -      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
  2.6827 +      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
  2.6828        break;
  2.6829    }
  2.6830  
  2.6831 @@ -44921,6 +47043,8 @@
  2.6832  
  2.6833    return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
  2.6834  }
  2.6835 +#else
  2.6836 +# define jrnlBufferSize(x) 0
  2.6837  #endif
  2.6838  
  2.6839  /*
  2.6840 @@ -45081,6 +47205,7 @@
  2.6841  static int zeroJournalHdr(Pager *pPager, int doTruncate){
  2.6842    int rc = SQLITE_OK;                               /* Return code */
  2.6843    assert( isOpen(pPager->jfd) );
  2.6844 +  assert( !sqlite3JournalIsInMemory(pPager->jfd) );
  2.6845    if( pPager->journalOff ){
  2.6846      const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
  2.6847  
  2.6848 @@ -45462,7 +47587,7 @@
  2.6849    for(ii=0; ii<pPager->nSavepoint; ii++){
  2.6850      sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
  2.6851    }
  2.6852 -  if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
  2.6853 +  if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
  2.6854      sqlite3OsClose(pPager->sjfd);
  2.6855    }
  2.6856    sqlite3_free(pPager->aSavepoint);
  2.6857 @@ -45568,13 +47693,17 @@
  2.6858    ** it can safely move back to PAGER_OPEN state. This happens in both
  2.6859    ** normal and exclusive-locking mode.
  2.6860    */
  2.6861 +  assert( pPager->errCode==SQLITE_OK || !MEMDB );
  2.6862    if( pPager->errCode ){
  2.6863 -    assert( !MEMDB );
  2.6864 -    pager_reset(pPager);
  2.6865 -    pPager->changeCountDone = pPager->tempFile;
  2.6866 -    pPager->eState = PAGER_OPEN;
  2.6867 +    if( pPager->tempFile==0 ){
  2.6868 +      pager_reset(pPager);
  2.6869 +      pPager->changeCountDone = 0;
  2.6870 +      pPager->eState = PAGER_OPEN;
  2.6871 +    }else{
  2.6872 +      pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
  2.6873 +    }
  2.6874 +    if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
  2.6875      pPager->errCode = SQLITE_OK;
  2.6876 -    if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
  2.6877    }
  2.6878  
  2.6879    pPager->journalOff = 0;
  2.6880 @@ -45619,6 +47748,29 @@
  2.6881  static int pager_truncate(Pager *pPager, Pgno nPage);
  2.6882  
  2.6883  /*
  2.6884 +** The write transaction open on pPager is being committed (bCommit==1)
  2.6885 +** or rolled back (bCommit==0).
  2.6886 +**
  2.6887 +** Return TRUE if and only if all dirty pages should be flushed to disk.
  2.6888 +**
  2.6889 +** Rules:
  2.6890 +**
  2.6891 +**   *  For non-TEMP databases, always sync to disk.  This is necessary
  2.6892 +**      for transactions to be durable.
  2.6893 +**
  2.6894 +**   *  Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
  2.6895 +**      file has been created already (via a spill on pagerStress()) and
  2.6896 +**      when the number of dirty pages in memory exceeds 25% of the total
  2.6897 +**      cache size.
  2.6898 +*/
  2.6899 +static int pagerFlushOnCommit(Pager *pPager, int bCommit){
  2.6900 +  if( pPager->tempFile==0 ) return 1;
  2.6901 +  if( !bCommit ) return 0;
  2.6902 +  if( !isOpen(pPager->fd) ) return 0;
  2.6903 +  return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
  2.6904 +}
  2.6905 +
  2.6906 +/*
  2.6907  ** This routine ends a transaction. A transaction is usually ended by 
  2.6908  ** either a COMMIT or a ROLLBACK operation. This routine may be called 
  2.6909  ** after rollback of a hot-journal, or if an error occurs while opening
  2.6910 @@ -45700,8 +47852,8 @@
  2.6911      assert( !pagerUseWal(pPager) );
  2.6912  
  2.6913      /* Finalize the journal file. */
  2.6914 -    if( sqlite3IsMemJournal(pPager->jfd) ){
  2.6915 -      assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
  2.6916 +    if( sqlite3JournalIsInMemory(pPager->jfd) ){
  2.6917 +      /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
  2.6918        sqlite3OsClose(pPager->jfd);
  2.6919      }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
  2.6920        if( pPager->journalOff==0 ){
  2.6921 @@ -45721,15 +47873,16 @@
  2.6922      }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
  2.6923        || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
  2.6924      ){
  2.6925 -      rc = zeroJournalHdr(pPager, hasMaster);
  2.6926 +      rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile);
  2.6927        pPager->journalOff = 0;
  2.6928      }else{
  2.6929        /* This branch may be executed with Pager.journalMode==MEMORY if
  2.6930        ** a hot-journal was just rolled back. In this case the journal
  2.6931        ** file should be closed and deleted. If this connection writes to
  2.6932 -      ** the database file, it will do so using an in-memory journal. 
  2.6933 +      ** the database file, it will do so using an in-memory journal.
  2.6934        */
  2.6935 -      int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
  2.6936 +      int bDelete = !pPager->tempFile;
  2.6937 +      assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
  2.6938        assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 
  2.6939             || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
  2.6940             || pPager->journalMode==PAGER_JOURNALMODE_WAL 
  2.6941 @@ -45755,8 +47908,14 @@
  2.6942    sqlite3BitvecDestroy(pPager->pInJournal);
  2.6943    pPager->pInJournal = 0;
  2.6944    pPager->nRec = 0;
  2.6945 -  sqlite3PcacheCleanAll(pPager->pPCache);
  2.6946 -  sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
  2.6947 +  if( rc==SQLITE_OK ){
  2.6948 +    if( pagerFlushOnCommit(pPager, bCommit) ){
  2.6949 +      sqlite3PcacheCleanAll(pPager->pPCache);
  2.6950 +    }else{
  2.6951 +      sqlite3PcacheClearWritable(pPager->pPCache);
  2.6952 +    }
  2.6953 +    sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
  2.6954 +  }
  2.6955  
  2.6956    if( pagerUseWal(pPager) ){
  2.6957      /* Drop the WAL write-lock, if any. Also, if the connection was in 
  2.6958 @@ -46040,7 +48199,7 @@
  2.6959      pPg = sqlite3PagerLookup(pPager, pgno);
  2.6960    }
  2.6961    assert( pPg || !MEMDB );
  2.6962 -  assert( pPager->eState!=PAGER_OPEN || pPg==0 );
  2.6963 +  assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
  2.6964    PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
  2.6965             PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
  2.6966             (isMainJrnl?"main-journal":"sub-journal")
  2.6967 @@ -46062,9 +48221,9 @@
  2.6968        pPager->dbFileSize = pgno;
  2.6969      }
  2.6970      if( pPager->pBackup ){
  2.6971 -      CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
  2.6972 +      CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT);
  2.6973        sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
  2.6974 -      CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
  2.6975 +      CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT, aData);
  2.6976      }
  2.6977    }else if( !isMainJrnl && pPg==0 ){
  2.6978      /* If this is a rollback of a savepoint and data was not written to
  2.6979 @@ -46090,7 +48249,6 @@
  2.6980      assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
  2.6981      pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
  2.6982      if( rc!=SQLITE_OK ) return rc;
  2.6983 -    pPg->flags &= ~PGHDR_NEED_READ;
  2.6984      sqlite3PcacheMakeDirty(pPg);
  2.6985    }
  2.6986    if( pPg ){
  2.6987 @@ -46104,29 +48262,10 @@
  2.6988      pData = pPg->pData;
  2.6989      memcpy(pData, (u8*)aData, pPager->pageSize);
  2.6990      pPager->xReiniter(pPg);
  2.6991 -    if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
  2.6992 -      /* If the contents of this page were just restored from the main 
  2.6993 -      ** journal file, then its content must be as they were when the 
  2.6994 -      ** transaction was first opened. In this case we can mark the page
  2.6995 -      ** as clean, since there will be no need to write it out to the
  2.6996 -      ** database.
  2.6997 -      **
  2.6998 -      ** There is one exception to this rule. If the page is being rolled
  2.6999 -      ** back as part of a savepoint (or statement) rollback from an 
  2.7000 -      ** unsynced portion of the main journal file, then it is not safe
  2.7001 -      ** to mark the page as clean. This is because marking the page as
  2.7002 -      ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
  2.7003 -      ** already in the journal file (recorded in Pager.pInJournal) and
  2.7004 -      ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
  2.7005 -      ** again within this transaction, it will be marked as dirty but
  2.7006 -      ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
  2.7007 -      ** be written out into the database file before its journal file
  2.7008 -      ** segment is synced. If a crash occurs during or following this,
  2.7009 -      ** database corruption may ensue.
  2.7010 -      */
  2.7011 -      assert( !pagerUseWal(pPager) );
  2.7012 -      sqlite3PcacheMakeClean(pPg);
  2.7013 -    }
  2.7014 +    /* It used to be that sqlite3PcacheMakeClean(pPg) was called here.  But
  2.7015 +    ** that call was dangerous and had no detectable benefit since the cache
  2.7016 +    ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
  2.7017 +    ** has been removed. */
  2.7018      pager_set_pagehash(pPg);
  2.7019  
  2.7020      /* If this was page 1, then restore the value of Pager.dbFileVers.
  2.7021 @@ -46136,7 +48275,7 @@
  2.7022      }
  2.7023  
  2.7024      /* Decode the page just read from disk */
  2.7025 -    CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
  2.7026 +    CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT);
  2.7027      sqlite3PcacheRelease(pPg);
  2.7028    }
  2.7029    return rc;
  2.7030 @@ -46202,7 +48341,7 @@
  2.7031    pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
  2.7032    pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
  2.7033    if( !pMaster ){
  2.7034 -    rc = SQLITE_NOMEM;
  2.7035 +    rc = SQLITE_NOMEM_BKPT;
  2.7036    }else{
  2.7037      const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
  2.7038      rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
  2.7039 @@ -46219,7 +48358,7 @@
  2.7040    nMasterPtr = pVfs->mxPathname+1;
  2.7041    zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1);
  2.7042    if( !zMasterJournal ){
  2.7043 -    rc = SQLITE_NOMEM;
  2.7044 +    rc = SQLITE_NOMEM_BKPT;
  2.7045      goto delmaster_out;
  2.7046    }
  2.7047    zMasterPtr = &zMasterJournal[nMasterJournal+1];
  2.7048 @@ -46467,7 +48606,7 @@
  2.7049    ** TODO: Technically the following is an error because it assumes that
  2.7050    ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
  2.7051    ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
  2.7052 -  **  mxPathname is 512, which is the same as the minimum allowable value
  2.7053 +  ** mxPathname is 512, which is the same as the minimum allowable value
  2.7054    ** for pageSize.
  2.7055    */
  2.7056    zMaster = pPager->pTmpSpace;
  2.7057 @@ -46689,7 +48828,7 @@
  2.7058        memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
  2.7059      }
  2.7060    }
  2.7061 -  CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
  2.7062 +  CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM_BKPT);
  2.7063  
  2.7064    PAGER_INCR(sqlite3_pager_readdb_count);
  2.7065    PAGER_INCR(pPager->nRead);
  2.7066 @@ -46917,6 +49056,8 @@
  2.7067    */
  2.7068    assert( pPager->eState==PAGER_OPEN );
  2.7069    assert( pPager->eLock>=SHARED_LOCK );
  2.7070 +  assert( isOpen(pPager->fd) );
  2.7071 +  assert( pPager->tempFile==0 );
  2.7072    nPage = sqlite3WalDbsize(pPager->pWal);
  2.7073  
  2.7074    /* If the number of pages in the database is not available from the
  2.7075 @@ -46924,14 +49065,11 @@
  2.7076    ** the database file.  If the size of the database file is not an
  2.7077    ** integer multiple of the page-size, round up the result.
  2.7078    */
  2.7079 -  if( nPage==0 ){
  2.7080 +  if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
  2.7081      i64 n = 0;                    /* Size of db file in bytes */
  2.7082 -    assert( isOpen(pPager->fd) || pPager->tempFile );
  2.7083 -    if( isOpen(pPager->fd) ){
  2.7084 -      int rc = sqlite3OsFileSize(pPager->fd, &n);
  2.7085 -      if( rc!=SQLITE_OK ){
  2.7086 -        return rc;
  2.7087 -      }
  2.7088 +    int rc = sqlite3OsFileSize(pPager->fd, &n);
  2.7089 +    if( rc!=SQLITE_OK ){
  2.7090 +      return rc;
  2.7091      }
  2.7092      nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
  2.7093    }
  2.7094 @@ -47049,7 +49187,7 @@
  2.7095    if( pSavepoint ){
  2.7096      pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
  2.7097      if( !pDone ){
  2.7098 -      return SQLITE_NOMEM;
  2.7099 +      return SQLITE_NOMEM_BKPT;
  2.7100      }
  2.7101    }
  2.7102  
  2.7103 @@ -47196,7 +49334,7 @@
  2.7104  ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
  2.7105  ** of the database to damage due to OS crashes or power failures by
  2.7106  ** changing the number of syncs()s when writing the journals.
  2.7107 -** There are three levels:
  2.7108 +** There are four levels:
  2.7109  **
  2.7110  **    OFF       sqlite3OsSync() is never called.  This is the default
  2.7111  **              for temporary and transient files.
  2.7112 @@ -47216,6 +49354,10 @@
  2.7113  **              assurance that the journal will not be corrupted to the
  2.7114  **              point of causing damage to the database during rollback.
  2.7115  **
  2.7116 +**    EXTRA     This is like FULL except that is also syncs the directory
  2.7117 +**              that contains the rollback journal after the rollback
  2.7118 +**              journal is unlinked.
  2.7119 +**
  2.7120  ** The above is for a rollback-journal mode.  For WAL mode, OFF continues
  2.7121  ** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
  2.7122  ** prior to the start of checkpoint and that the database file is synced
  2.7123 @@ -47223,7 +49365,8 @@
  2.7124  ** was written back into the database.  But no sync operations occur for
  2.7125  ** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
  2.7126  ** file is synced following each commit operation, in addition to the
  2.7127 -** syncs associated with NORMAL.
  2.7128 +** syncs associated with NORMAL.  There is no difference between FULL
  2.7129 +** and EXTRA for WAL mode.
  2.7130  **
  2.7131  ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
  2.7132  ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
  2.7133 @@ -47412,7 +49555,7 @@
  2.7134      }
  2.7135      if( rc==SQLITE_OK ){
  2.7136        pNew = (char *)sqlite3PageMalloc(pageSize);
  2.7137 -      if( !pNew ) rc = SQLITE_NOMEM;
  2.7138 +      if( !pNew ) rc = SQLITE_NOMEM_BKPT;
  2.7139      }
  2.7140  
  2.7141      if( rc==SQLITE_OK ){
  2.7142 @@ -47688,7 +49831,7 @@
  2.7143      *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
  2.7144      if( p==0 ){
  2.7145        sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
  2.7146 -      return SQLITE_NOMEM;
  2.7147 +      return SQLITE_NOMEM_BKPT;
  2.7148      }
  2.7149      p->pExtra = (void *)&p[1];
  2.7150      p->flags = PGHDR_MMAP;
  2.7151 @@ -48002,8 +50145,9 @@
  2.7152  
  2.7153    /* This function is only called for rollback pagers in WRITER_DBMOD state. */
  2.7154    assert( !pagerUseWal(pPager) );
  2.7155 -  assert( pPager->eState==PAGER_WRITER_DBMOD );
  2.7156 +  assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
  2.7157    assert( pPager->eLock==EXCLUSIVE_LOCK );
  2.7158 +  assert( isOpen(pPager->fd) || pList->pDirty==0 );
  2.7159  
  2.7160    /* If the file is a temp-file has not yet been opened, open it now. It
  2.7161    ** is not possible for rc to be other than SQLITE_OK if this branch
  2.7162 @@ -48046,7 +50190,7 @@
  2.7163        if( pList->pgno==1 ) pager_write_changecounter(pList);
  2.7164  
  2.7165        /* Encode the database */
  2.7166 -      CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
  2.7167 +      CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData);
  2.7168  
  2.7169        /* Write out the page data. */
  2.7170        rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
  2.7171 @@ -48091,11 +50235,14 @@
  2.7172  static int openSubJournal(Pager *pPager){
  2.7173    int rc = SQLITE_OK;
  2.7174    if( !isOpen(pPager->sjfd) ){
  2.7175 +    const int flags =  SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE 
  2.7176 +      | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE 
  2.7177 +      | SQLITE_OPEN_DELETEONCLOSE;
  2.7178 +    int nStmtSpill = sqlite3Config.nStmtSpill;
  2.7179      if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
  2.7180 -      sqlite3MemJournalOpen(pPager->sjfd);
  2.7181 -    }else{
  2.7182 -      rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
  2.7183 -    }
  2.7184 +      nStmtSpill = -1;
  2.7185 +    }
  2.7186 +    rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
  2.7187    }
  2.7188    return rc;
  2.7189  }
  2.7190 @@ -48133,7 +50280,7 @@
  2.7191        i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
  2.7192        char *pData2;
  2.7193    
  2.7194 -      CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
  2.7195 +      CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
  2.7196        PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
  2.7197        rc = write32bits(pPager->sjfd, offset, pPg->pgno);
  2.7198        if( rc==SQLITE_OK ){
  2.7199 @@ -48316,18 +50463,8 @@
  2.7200    int nUri = 0;            /* Number of bytes of URI args at *zUri */
  2.7201  
  2.7202    /* Figure out how much space is required for each journal file-handle
  2.7203 -  ** (there are two of them, the main journal and the sub-journal). This
  2.7204 -  ** is the maximum space required for an in-memory journal file handle 
  2.7205 -  ** and a regular journal file-handle. Note that a "regular journal-handle"
  2.7206 -  ** may be a wrapper capable of caching the first portion of the journal
  2.7207 -  ** file in memory to implement the atomic-write optimization (see 
  2.7208 -  ** source file journal.c).
  2.7209 -  */
  2.7210 -  if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
  2.7211 -    journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
  2.7212 -  }else{
  2.7213 -    journalFileSize = ROUND8(sqlite3MemJournalSize());
  2.7214 -  }
  2.7215 +  ** (there are two of them, the main journal and the sub-journal).  */
  2.7216 +  journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
  2.7217  
  2.7218    /* Set the output variable to NULL in case an error occurs. */
  2.7219    *ppPager = 0;
  2.7220 @@ -48337,7 +50474,7 @@
  2.7221      memDb = 1;
  2.7222      if( zFilename && zFilename[0] ){
  2.7223        zPathname = sqlite3DbStrDup(0, zFilename);
  2.7224 -      if( zPathname==0  ) return SQLITE_NOMEM;
  2.7225 +      if( zPathname==0  ) return SQLITE_NOMEM_BKPT;
  2.7226        nPathname = sqlite3Strlen30(zPathname);
  2.7227        zFilename = 0;
  2.7228      }
  2.7229 @@ -48353,7 +50490,7 @@
  2.7230      nPathname = pVfs->mxPathname+1;
  2.7231      zPathname = sqlite3DbMallocRaw(0, nPathname*2);
  2.7232      if( zPathname==0 ){
  2.7233 -      return SQLITE_NOMEM;
  2.7234 +      return SQLITE_NOMEM_BKPT;
  2.7235      }
  2.7236      zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
  2.7237      rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
  2.7238 @@ -48406,7 +50543,7 @@
  2.7239    assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
  2.7240    if( !pPtr ){
  2.7241      sqlite3DbFree(0, zPathname);
  2.7242 -    return SQLITE_NOMEM;
  2.7243 +    return SQLITE_NOMEM_BKPT;
  2.7244    }
  2.7245    pPager =              (Pager*)(pPtr);
  2.7246    pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
  2.7247 @@ -48561,11 +50698,7 @@
  2.7248      assert( pPager->ckptSyncFlags==0 );
  2.7249    }else{
  2.7250      pPager->fullSync = 1;
  2.7251 -#if SQLITE_EXTRA_DURABLE
  2.7252 -    pPager->extraSync = 1;
  2.7253 -#else
  2.7254      pPager->extraSync = 0;
  2.7255 -#endif
  2.7256      pPager->syncFlags = SQLITE_SYNC_NORMAL;
  2.7257      pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
  2.7258      pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
  2.7259 @@ -48682,6 +50815,7 @@
  2.7260      if( rc==SQLITE_OK && !locked ){
  2.7261        Pgno nPage;                 /* Number of pages in database file */
  2.7262  
  2.7263 +      assert( pPager->tempFile==0 );
  2.7264        rc = pagerPagecount(pPager, &nPage);
  2.7265        if( rc==SQLITE_OK ){
  2.7266          /* If the database is zero pages in size, that means that either (1) the
  2.7267 @@ -48774,17 +50908,17 @@
  2.7268    /* This routine is only called from b-tree and only when there are no
  2.7269    ** outstanding pages. This implies that the pager state should either
  2.7270    ** be OPEN or READER. READER is only possible if the pager is or was in 
  2.7271 -  ** exclusive access mode.
  2.7272 -  */
  2.7273 +  ** exclusive access mode.  */
  2.7274    assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
  2.7275    assert( assert_pager_state(pPager) );
  2.7276    assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
  2.7277 -  if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
  2.7278 +  assert( pPager->errCode==SQLITE_OK );
  2.7279  
  2.7280    if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
  2.7281      int bHotJournal = 1;          /* True if there exists a hot journal-file */
  2.7282  
  2.7283      assert( !MEMDB );
  2.7284 +    assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
  2.7285  
  2.7286      rc = pager_wait_on_lock(pPager, SHARED_LOCK);
  2.7287      if( rc!=SQLITE_OK ){
  2.7288 @@ -48870,7 +51004,7 @@
  2.7289          assert( rc==SQLITE_OK );
  2.7290          rc = pagerSyncHotJournal(pPager);
  2.7291          if( rc==SQLITE_OK ){
  2.7292 -          rc = pager_playback(pPager, 1);
  2.7293 +          rc = pager_playback(pPager, !pPager->tempFile);
  2.7294            pPager->eState = PAGER_OPEN;
  2.7295          }
  2.7296        }else if( !pPager->exclusiveMode ){
  2.7297 @@ -48966,7 +51100,7 @@
  2.7298      rc = pagerBeginReadTransaction(pPager);
  2.7299    }
  2.7300  
  2.7301 -  if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
  2.7302 +  if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
  2.7303      rc = pagerPagecount(pPager, &pPager->dbSize);
  2.7304    }
  2.7305  
  2.7306 @@ -49099,7 +51233,7 @@
  2.7307        );
  2.7308  
  2.7309        if( rc==SQLITE_OK && pData ){
  2.7310 -        if( pPager->eState>PAGER_READER ){
  2.7311 +        if( pPager->eState>PAGER_READER || pPager->tempFile ){
  2.7312            pPg = sqlite3PagerLookup(pPager, pgno);
  2.7313          }
  2.7314          if( pPg==0 ){
  2.7315 @@ -49126,7 +51260,7 @@
  2.7316          if( rc!=SQLITE_OK ) goto pager_acquire_err;
  2.7317          if( pBase==0 ){
  2.7318            pPg = *ppPage = 0;
  2.7319 -          rc = SQLITE_NOMEM;
  2.7320 +          rc = SQLITE_NOMEM_BKPT;
  2.7321            goto pager_acquire_err;
  2.7322          }
  2.7323        }
  2.7324 @@ -49166,7 +51300,8 @@
  2.7325        goto pager_acquire_err;
  2.7326      }
  2.7327  
  2.7328 -    if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
  2.7329 +    assert( !isOpen(pPager->fd) || !MEMDB );
  2.7330 +    if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
  2.7331        if( pgno>pPager->mxPgno ){
  2.7332          rc = SQLITE_FULL;
  2.7333          goto pager_acquire_err;
  2.7334 @@ -49300,7 +51435,7 @@
  2.7335    if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
  2.7336      pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
  2.7337      if( pPager->pInJournal==0 ){
  2.7338 -      return SQLITE_NOMEM;
  2.7339 +      return SQLITE_NOMEM_BKPT;
  2.7340      }
  2.7341    
  2.7342      /* Open the journal file if it is not already open. */
  2.7343 @@ -49308,24 +51443,24 @@
  2.7344        if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
  2.7345          sqlite3MemJournalOpen(pPager->jfd);
  2.7346        }else{
  2.7347 -        const int flags =                   /* VFS flags to open journal file */
  2.7348 -          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
  2.7349 -          (pPager->tempFile ? 
  2.7350 -            (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
  2.7351 -            (SQLITE_OPEN_MAIN_JOURNAL)
  2.7352 -          );
  2.7353 -
  2.7354 +        int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
  2.7355 +        int nSpill;
  2.7356 +
  2.7357 +        if( pPager->tempFile ){
  2.7358 +          flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
  2.7359 +          nSpill = sqlite3Config.nStmtSpill;
  2.7360 +        }else{
  2.7361 +          flags |= SQLITE_OPEN_MAIN_JOURNAL;
  2.7362 +          nSpill = jrnlBufferSize(pPager);
  2.7363 +        }
  2.7364 +          
  2.7365          /* Verify that the database still has the same name as it did when
  2.7366          ** it was originally opened. */
  2.7367          rc = databaseIsUnmoved(pPager);
  2.7368          if( rc==SQLITE_OK ){
  2.7369 -#ifdef SQLITE_ENABLE_ATOMIC_WRITE
  2.7370 -          rc = sqlite3JournalOpen(
  2.7371 -              pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
  2.7372 +          rc = sqlite3JournalOpen (
  2.7373 +              pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
  2.7374            );
  2.7375 -#else
  2.7376 -          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
  2.7377 -#endif
  2.7378          }
  2.7379        }
  2.7380        assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
  2.7381 @@ -49455,7 +51590,7 @@
  2.7382    assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
  2.7383  
  2.7384    assert( pPager->journalHdr<=pPager->journalOff );
  2.7385 -  CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
  2.7386 +  CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
  2.7387    cksum = pager_cksum(pPager, (u8*)pData2);
  2.7388  
  2.7389    /* Even if an IO or diskfull error occurs while journalling the
  2.7390 @@ -49696,6 +51831,7 @@
  2.7391      if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
  2.7392      return SQLITE_OK;
  2.7393    }else if( pPager->sectorSize > (u32)pPager->pageSize ){
  2.7394 +    assert( pPager->tempFile==0 );
  2.7395      return pagerWriteLargeSector(pPg);
  2.7396    }else{
  2.7397      return pager_write(pPg);
  2.7398 @@ -49726,14 +51862,21 @@
  2.7399  **
  2.7400  ** Tests show that this optimization can quadruple the speed of large 
  2.7401  ** DELETE operations.
  2.7402 +**
  2.7403 +** This optimization cannot be used with a temp-file, as the page may
  2.7404 +** have been dirty at the start of the transaction. In that case, if
  2.7405 +** memory pressure forces page pPg out of the cache, the data does need 
  2.7406 +** to be written out to disk so that it may be read back in if the 
  2.7407 +** current transaction is rolled back.
  2.7408  */
  2.7409  SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
  2.7410    Pager *pPager = pPg->pPager;
  2.7411 -  if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
  2.7412 +  if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
  2.7413      PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
  2.7414      IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
  2.7415      pPg->flags |= PGHDR_DONT_WRITE;
  2.7416      pPg->flags &= ~PGHDR_WRITEABLE;
  2.7417 +    testcase( pPg->flags & PGHDR_NEED_SYNC );
  2.7418      pager_set_pagehash(pPg);
  2.7419    }
  2.7420  }
  2.7421 @@ -49812,7 +51955,7 @@
  2.7422        if( DIRECT_MODE ){
  2.7423          const void *zBuf;
  2.7424          assert( pPager->dbFileSize>0 );
  2.7425 -        CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
  2.7426 +        CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf);
  2.7427          if( rc==SQLITE_OK ){
  2.7428            rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
  2.7429            pPager->aStat[PAGER_STAT_WRITE]++;
  2.7430 @@ -49928,17 +52071,21 @@
  2.7431    /* If a prior error occurred, report that error again. */
  2.7432    if( NEVER(pPager->errCode) ) return pPager->errCode;
  2.7433  
  2.7434 +  /* Provide the ability to easily simulate an I/O error during testing */
  2.7435 +  if( sqlite3FaultSim(400) ) return SQLITE_IOERR;
  2.7436 +
  2.7437    PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
  2.7438        pPager->zFilename, zMaster, pPager->dbSize));
  2.7439  
  2.7440    /* If no database changes have been made, return early. */
  2.7441    if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
  2.7442  
  2.7443 -  if( MEMDB ){
  2.7444 +  assert( MEMDB==0 || pPager->tempFile );
  2.7445 +  assert( isOpen(pPager->fd) || pPager->tempFile );
  2.7446 +  if( 0==pagerFlushOnCommit(pPager, 1) ){
  2.7447      /* If this is an in-memory db, or no pages have been written to, or this
  2.7448      ** function has already been called, it is mostly a no-op.  However, any
  2.7449 -    ** backup in progress needs to be restarted.
  2.7450 -    */
  2.7451 +    ** backup in progress needs to be restarted.  */
  2.7452      sqlite3BackupRestart(pPager->pBackup);
  2.7453    }else{
  2.7454      if( pagerUseWal(pPager) ){
  2.7455 @@ -50277,10 +52424,10 @@
  2.7456  }
  2.7457  
  2.7458  /*
  2.7459 -** Return true if this is an in-memory pager.
  2.7460 +** Return true if this is an in-memory or temp-file backed pager.
  2.7461  */
  2.7462  SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
  2.7463 -  return MEMDB;
  2.7464 +  return pPager->tempFile;
  2.7465  }
  2.7466  
  2.7467  /*
  2.7468 @@ -50311,7 +52458,7 @@
  2.7469        pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
  2.7470    );
  2.7471    if( !aNew ){
  2.7472 -    return SQLITE_NOMEM;
  2.7473 +    return SQLITE_NOMEM_BKPT;
  2.7474    }
  2.7475    memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
  2.7476    pPager->aSavepoint = aNew;
  2.7477 @@ -50327,7 +52474,7 @@
  2.7478      aNew[ii].iSubRec = pPager->nSubRec;
  2.7479      aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
  2.7480      if( !aNew[ii].pInSavepoint ){
  2.7481 -      return SQLITE_NOMEM;
  2.7482 +      return SQLITE_NOMEM_BKPT;
  2.7483      }
  2.7484      if( pagerUseWal(pPager) ){
  2.7485        sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
  2.7486 @@ -50405,7 +52552,7 @@
  2.7487      if( op==SAVEPOINT_RELEASE ){
  2.7488        if( nNew==0 && isOpen(pPager->sjfd) ){
  2.7489          /* Only truncate if it is an in-memory sub-journal. */
  2.7490 -        if( sqlite3IsMemJournal(pPager->sjfd) ){
  2.7491 +        if( sqlite3JournalIsInMemory(pPager->sjfd) ){
  2.7492            rc = sqlite3OsTruncate(pPager->sjfd, 0);
  2.7493            assert( rc==SQLITE_OK );
  2.7494          }
  2.7495 @@ -50476,14 +52623,6 @@
  2.7496    return pPager->zJournal;
  2.7497  }
  2.7498  
  2.7499 -/*
  2.7500 -** Return true if fsync() calls are disabled for this pager.  Return FALSE
  2.7501 -** if fsync()s are executed normally.
  2.7502 -*/
  2.7503 -SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
  2.7504 -  return pPager->noSync;
  2.7505 -}
  2.7506 -
  2.7507  #ifdef SQLITE_HAS_CODEC
  2.7508  /*
  2.7509  ** Set or retrieve the codec for this pager
  2.7510 @@ -50568,7 +52707,8 @@
  2.7511    /* In order to be able to rollback, an in-memory database must journal
  2.7512    ** the page we are moving from.
  2.7513    */
  2.7514 -  if( MEMDB ){
  2.7515 +  assert( pPager->tempFile || !MEMDB );
  2.7516 +  if( pPager->tempFile ){
  2.7517      rc = sqlite3PagerWrite(pPg);
  2.7518      if( rc ) return rc;
  2.7519    }
  2.7520 @@ -50625,7 +52765,7 @@
  2.7521    assert( !pPgOld || pPgOld->nRef==1 );
  2.7522    if( pPgOld ){
  2.7523      pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
  2.7524 -    if( MEMDB ){
  2.7525 +    if( pPager->tempFile ){
  2.7526        /* Do not discard pages from an in-memory database since we might
  2.7527        ** need to rollback later.  Just move the page out of the way. */
  2.7528        sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
  2.7529 @@ -50642,8 +52782,7 @@
  2.7530    ** to exist, in case the transaction needs to roll back.  Use pPgOld
  2.7531    ** as the original page since it has already been allocated.
  2.7532    */
  2.7533 -  if( MEMDB ){
  2.7534 -    assert( pPgOld );
  2.7535 +  if( pPager->tempFile && pPgOld ){
  2.7536      sqlite3PcacheMove(pPgOld, origPgno);
  2.7537      sqlite3PagerUnrefNotNull(pPgOld);
  2.7538    }
  2.7539 @@ -50895,7 +53034,8 @@
  2.7540  ** Unless this is an in-memory or temporary database, clear the pager cache.
  2.7541  */
  2.7542  SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
  2.7543 -  if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
  2.7544 +  assert( MEMDB==0 || pPager->tempFile );
  2.7545 +  if( pPager->tempFile==0 ) pager_reset(pPager);
  2.7546  }
  2.7547  #endif
  2.7548  
  2.7549 @@ -50930,6 +53070,7 @@
  2.7550  */
  2.7551  SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
  2.7552    const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
  2.7553 +  if( pPager->noLock ) return 0;
  2.7554    return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
  2.7555  }
  2.7556  
  2.7557 @@ -51073,6 +53214,7 @@
  2.7558                             pPager->pageSize, (u8*)pPager->pTmpSpace);
  2.7559        pPager->pWal = 0;
  2.7560        pagerFixMaplimit(pPager);
  2.7561 +      if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
  2.7562      }
  2.7563    }
  2.7564    return rc;
  2.7565 @@ -51675,7 +53817,7 @@
  2.7566      apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
  2.7567      if( !apNew ){
  2.7568        *ppPage = 0;
  2.7569 -      return SQLITE_NOMEM;
  2.7570 +      return SQLITE_NOMEM_BKPT;
  2.7571      }
  2.7572      memset((void*)&apNew[pWal->nWiData], 0,
  2.7573             sizeof(u32*)*(iPage+1-pWal->nWiData));
  2.7574 @@ -51687,7 +53829,7 @@
  2.7575    if( pWal->apWiData[iPage]==0 ){
  2.7576      if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
  2.7577        pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
  2.7578 -      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
  2.7579 +      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
  2.7580      }else{
  2.7581        rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
  2.7582            pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
  2.7583 @@ -52302,7 +54444,7 @@
  2.7584      szFrame = szPage + WAL_FRAME_HDRSIZE;
  2.7585      aFrame = (u8 *)sqlite3_malloc64(szFrame);
  2.7586      if( !aFrame ){
  2.7587 -      rc = SQLITE_NOMEM;
  2.7588 +      rc = SQLITE_NOMEM_BKPT;
  2.7589        goto recovery_error;
  2.7590      }
  2.7591      aData = &aFrame[WAL_FRAME_HDRSIZE];
  2.7592 @@ -52440,7 +54582,7 @@
  2.7593    *ppWal = 0;
  2.7594    pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
  2.7595    if( !pRet ){
  2.7596 -    return SQLITE_NOMEM;
  2.7597 +    return SQLITE_NOMEM_BKPT;
  2.7598    }
  2.7599  
  2.7600    pRet->pVfs = pVfs;
  2.7601 @@ -52704,7 +54846,7 @@
  2.7602          + iLast*sizeof(ht_slot);
  2.7603    p = (WalIterator *)sqlite3_malloc64(nByte);
  2.7604    if( !p ){
  2.7605 -    return SQLITE_NOMEM;
  2.7606 +    return SQLITE_NOMEM_BKPT;
  2.7607    }
  2.7608    memset(p, 0, nByte);
  2.7609    p->nSegment = nSegment;
  2.7610 @@ -52716,7 +54858,7 @@
  2.7611        sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
  2.7612    );
  2.7613    if( !aTmp ){
  2.7614 -    rc = SQLITE_NOMEM;
  2.7615 +    rc = SQLITE_NOMEM_BKPT;
  2.7616    }
  2.7617  
  2.7618    for(i=0; rc==SQLITE_OK && i<nSegment; i++){
  2.7619 @@ -54009,7 +56151,7 @@
  2.7620    void *pData;                    /* Data actually written */
  2.7621    u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
  2.7622  #if defined(SQLITE_HAS_CODEC)
  2.7623 -  if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
  2.7624 +  if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT;
  2.7625  #else
  2.7626    pData = pPage->pData;
  2.7627  #endif
  2.7628 @@ -54038,7 +56180,7 @@
  2.7629    i64 iCksumOff;
  2.7630  
  2.7631    aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE);
  2.7632 -  if( aBuf==0 ) return SQLITE_NOMEM;
  2.7633 +  if( aBuf==0 ) return SQLITE_NOMEM_BKPT;
  2.7634  
  2.7635    /* Find the checksum values to use as input for the recalculating the
  2.7636    ** first checksum. If the first frame is frame 1 (implying that the current
  2.7637 @@ -54514,7 +56656,7 @@
  2.7638  
  2.7639    pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
  2.7640    if( pRet==0 ){
  2.7641 -    rc = SQLITE_NOMEM;
  2.7642 +    rc = SQLITE_NOMEM_BKPT;
  2.7643    }else{
  2.7644      memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
  2.7645      *ppSnapshot = (sqlite3_snapshot*)pRet;
  2.7646 @@ -54528,6 +56670,23 @@
  2.7647  SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){
  2.7648    pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
  2.7649  }
  2.7650 +
  2.7651 +/* 
  2.7652 +** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
  2.7653 +** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
  2.7654 +*/
  2.7655 +SQLITE_API int SQLITE_STDCALL sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
  2.7656 +  WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
  2.7657 +  WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
  2.7658 +
  2.7659 +  /* aSalt[0] is a copy of the value stored in the wal file header. It
  2.7660 +  ** is incremented each time the wal file is restarted.  */
  2.7661 +  if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
  2.7662 +  if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
  2.7663 +  if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
  2.7664 +  if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
  2.7665 +  return 0;
  2.7666 +}
  2.7667  #endif /* SQLITE_ENABLE_SNAPSHOT */
  2.7668  
  2.7669  #ifdef SQLITE_ENABLE_ZIPVFS
  2.7670 @@ -55917,7 +58076,7 @@
  2.7671    if( !pLock ){
  2.7672      pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
  2.7673      if( !pLock ){
  2.7674 -      return SQLITE_NOMEM;
  2.7675 +      return SQLITE_NOMEM_BKPT;
  2.7676      }
  2.7677      pLock->iTable = iTable;
  2.7678      pLock->pBtree = p;
  2.7679 @@ -56120,7 +58279,7 @@
  2.7680      assert( pgno<=pBt->nPage );
  2.7681      pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
  2.7682      if( !pBt->pHasContent ){
  2.7683 -      rc = SQLITE_NOMEM;
  2.7684 +      rc = SQLITE_NOMEM_BKPT;
  2.7685      }
  2.7686    }
  2.7687    if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
  2.7688 @@ -56199,7 +58358,7 @@
  2.7689          sqlite3_free(pKey);
  2.7690        }
  2.7691      }else{
  2.7692 -      rc = SQLITE_NOMEM;
  2.7693 +      rc = SQLITE_NOMEM_BKPT;
  2.7694      }
  2.7695    }
  2.7696    assert( !pCur->curIntKey || !pCur->pKey );
  2.7697 @@ -56331,7 +58490,7 @@
  2.7698      pIdxKey = sqlite3VdbeAllocUnpackedRecord(
  2.7699          pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
  2.7700      );
  2.7701 -    if( pIdxKey==0 ) return SQLITE_NOMEM;
  2.7702 +    if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
  2.7703      sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
  2.7704      if( pIdxKey->nField==0 ){
  2.7705        sqlite3DbFree(pCur->pKeyInfo->db, pFree);
  2.7706 @@ -57243,11 +59402,11 @@
  2.7707    pPage->xCellSize = cellSizePtr;
  2.7708    pBt = pPage->pBt;
  2.7709    if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
  2.7710 -    /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior
  2.7711 -    ** table b-tree page. */
  2.7712 +    /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
  2.7713 +    ** interior table b-tree page. */
  2.7714      assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
  2.7715 -    /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf
  2.7716 -    ** table b-tree page. */
  2.7717 +    /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
  2.7718 +    ** leaf table b-tree page. */
  2.7719      assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
  2.7720      pPage->intKey = 1;
  2.7721      if( pPage->leaf ){
  2.7722 @@ -57261,11 +59420,11 @@
  2.7723      pPage->maxLocal = pBt->maxLeaf;
  2.7724      pPage->minLocal = pBt->minLeaf;
  2.7725    }else if( flagByte==PTF_ZERODATA ){
  2.7726 -    /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior
  2.7727 -    ** index b-tree page. */
  2.7728 +    /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
  2.7729 +    ** interior index b-tree page. */
  2.7730      assert( (PTF_ZERODATA)==2 );
  2.7731 -    /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf
  2.7732 -    ** index b-tree page. */
  2.7733 +    /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
  2.7734 +    ** leaf index b-tree page. */
  2.7735      assert( (PTF_ZERODATA|PTF_LEAF)==10 );
  2.7736      pPage->intKey = 0;
  2.7737      pPage->intKeyLeaf = 0;
  2.7738 @@ -57743,7 +59902,7 @@
  2.7739    }
  2.7740    p = sqlite3MallocZero(sizeof(Btree));
  2.7741    if( !p ){
  2.7742 -    return SQLITE_NOMEM;
  2.7743 +    return SQLITE_NOMEM_BKPT;
  2.7744    }
  2.7745    p->inTrans = TRANS_NONE;
  2.7746    p->db = db;
  2.7747 @@ -57767,7 +59926,7 @@
  2.7748        p->sharable = 1;
  2.7749        if( !zFullPathname ){
  2.7750          sqlite3_free(p);
  2.7751 -        return SQLITE_NOMEM;
  2.7752 +        return SQLITE_NOMEM_BKPT;
  2.7753        }
  2.7754        if( isMemdb ){
  2.7755          memcpy(zFullPathname, zFilename, nFilename);
  2.7756 @@ -57835,7 +59994,7 @@
  2.7757    
  2.7758      pBt = sqlite3MallocZero( sizeof(*pBt) );
  2.7759      if( pBt==0 ){
  2.7760 -      rc = SQLITE_NOMEM;
  2.7761 +      rc = SQLITE_NOMEM_BKPT;
  2.7762        goto btree_open_out;
  2.7763      }
  2.7764      rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
  2.7765 @@ -57904,7 +60063,7 @@
  2.7766        if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
  2.7767          pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
  2.7768          if( pBt->mutex==0 ){
  2.7769 -          rc = SQLITE_NOMEM;
  2.7770 +          rc = SQLITE_NOMEM_BKPT;
  2.7771            goto btree_open_out;
  2.7772          }
  2.7773        }
  2.7774 @@ -57927,12 +60086,12 @@
  2.7775      for(i=0; i<db->nDb; i++){
  2.7776        if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
  2.7777          while( pSib->pPrev ){ pSib = pSib->pPrev; }
  2.7778 -        if( p->pBt<pSib->pBt ){
  2.7779 +        if( (uptr)p->pBt<(uptr)pSib->pBt ){
  2.7780            p->pNext = pSib;
  2.7781            p->pPrev = 0;
  2.7782            pSib->pPrev = p;
  2.7783          }else{
  2.7784 -          while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
  2.7785 +          while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
  2.7786              pSib = pSib->pNext;
  2.7787            }
  2.7788            p->pNext = pSib->pNext;
  2.7789 @@ -58187,21 +60346,6 @@
  2.7790  #endif
  2.7791  
  2.7792  /*
  2.7793 -** Return TRUE if the given btree is set to safety level 1.  In other
  2.7794 -** words, return TRUE if no sync() occurs on the disk files.
  2.7795 -*/
  2.7796 -SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
  2.7797 -  BtShared *pBt = p->pBt;
  2.7798 -  int rc;
  2.7799 -  assert( sqlite3_mutex_held(p->db->mutex) );  
  2.7800 -  sqlite3BtreeEnter(p);
  2.7801 -  assert( pBt && pBt->pPager );
  2.7802 -  rc = sqlite3PagerNosync(pBt->pPager);
  2.7803 -  sqlite3BtreeLeave(p);
  2.7804 -  return rc;
  2.7805 -}
  2.7806 -
  2.7807 -/*
  2.7808  ** Change the default pages size and the number of reserved bytes per page.
  2.7809  ** Or, if the page size has already been fixed, return SQLITE_READONLY 
  2.7810  ** without changing anything.
  2.7811 @@ -58446,9 +60590,25 @@
  2.7812        rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
  2.7813        if( rc!=SQLITE_OK ){
  2.7814          goto page1_init_failed;
  2.7815 -      }else if( isOpen==0 ){
  2.7816 -        releasePage(pPage1);
  2.7817 -        return SQLITE_OK;
  2.7818 +      }else{
  2.7819 +#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS
  2.7820 +        sqlite3 *db;
  2.7821 +        Db *pDb;
  2.7822 +        if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
  2.7823 +          while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
  2.7824 +          if( pDb->bSyncSet==0
  2.7825 +           && pDb->safety_level==SQLITE_DEFAULT_SYNCHRONOUS+1
  2.7826 +          ){
  2.7827 +            pDb->safety_level = SQLITE_DEFAULT_WAL_SYNCHRONOUS+1;
  2.7828 +            sqlite3PagerSetFlags(pBt->pPager,
  2.7829 +               pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
  2.7830 +          }
  2.7831 +        }
  2.7832 +#endif
  2.7833 +        if( isOpen==0 ){
  2.7834 +          releasePage(pPage1);
  2.7835 +          return SQLITE_OK;
  2.7836 +        }
  2.7837        }
  2.7838        rc = SQLITE_NOTADB;
  2.7839      }
  2.7840 @@ -59681,7 +61841,7 @@
  2.7841  
  2.7842    if( wrFlag ){
  2.7843      allocateTempSpace(pBt);
  2.7844 -    if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM;
  2.7845 +    if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
  2.7846    }
  2.7847    if( iTable==1 && btreePagecount(pBt)==0 ){
  2.7848      assert( wrFlag==0 );
  2.7849 @@ -60038,8 +62198,13 @@
  2.7850  #endif
  2.7851    assert( offset+amt <= pCur->info.nPayload );
  2.7852  
  2.7853 -  if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){
  2.7854 -    /* Trying to read or write past the end of the data is an error */
  2.7855 +  assert( aPayload > pPage->aData );
  2.7856 +  if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
  2.7857 +    /* Trying to read or write past the end of the data is an error.  The
  2.7858 +    ** conditional above is really:
  2.7859 +    **    &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
  2.7860 +    ** but is recast into its current form to avoid integer overflow problems
  2.7861 +    */
  2.7862      return SQLITE_CORRUPT_BKPT;
  2.7863    }
  2.7864  
  2.7865 @@ -60079,7 +62244,7 @@
  2.7866              pCur->aOverflow, nOvfl*2*sizeof(Pgno)
  2.7867          );
  2.7868          if( aNew==0 ){
  2.7869 -          rc = SQLITE_NOMEM;
  2.7870 +          rc = SQLITE_NOMEM_BKPT;
  2.7871          }else{
  2.7872            pCur->nOvflAlloc = nOvfl*2;
  2.7873            pCur->aOverflow = aNew;
  2.7874 @@ -60784,7 +62949,7 @@
  2.7875            }
  2.7876            pCellKey = sqlite3Malloc( nCell+18 );
  2.7877            if( pCellKey==0 ){
  2.7878 -            rc = SQLITE_NOMEM;
  2.7879 +            rc = SQLITE_NOMEM_BKPT;
  2.7880              goto moveto_finish;
  2.7881            }
  2.7882            pCur->aiIdx[pCur->iPage] = (u16)idx;
  2.7883 @@ -62106,8 +64271,8 @@
  2.7884      u8 *pSlot;
  2.7885      sz = cachedCellSize(pCArray, i);
  2.7886      if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
  2.7887 +      if( (pData - pBegin)<sz ) return 1;
  2.7888        pData -= sz;
  2.7889 -      if( pData<pBegin ) return 1;
  2.7890        pSlot = pData;
  2.7891      }
  2.7892      /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
  2.7893 @@ -62269,7 +64434,7 @@
  2.7894    for(i=0; i<nNew && !CORRUPT_DB; i++){
  2.7895      u8 *pCell = pCArray->apCell[i+iNew];
  2.7896      int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
  2.7897 -    if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){
  2.7898 +    if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
  2.7899        pCell = &pTmp[pCell - aData];
  2.7900      }
  2.7901      assert( 0==memcmp(pCell, &aData[iOff],
  2.7902 @@ -62603,7 +64768,7 @@
  2.7903    assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
  2.7904  
  2.7905    if( !aOvflSpace ){
  2.7906 -    return SQLITE_NOMEM;
  2.7907 +    return SQLITE_NOMEM_BKPT;
  2.7908    }
  2.7909  
  2.7910    /* Find the sibling pages to balance. Also locate the cells in pParent 
  2.7911 @@ -62703,7 +64868,7 @@
  2.7912    assert( szScratch<=6*(int)pBt->pageSize );
  2.7913    b.apCell = sqlite3ScratchMalloc( szScratch ); 
  2.7914    if( b.apCell==0 ){
  2.7915 -    rc = SQLITE_NOMEM;
  2.7916 +    rc = SQLITE_NOMEM_BKPT;
  2.7917      goto balance_cleanup;
  2.7918    }
  2.7919    b.szCell = (u16*)&b.apCell[nMaxCells];
  2.7920 @@ -63138,9 +65303,9 @@
  2.7921        ** any cell). But it is important to pass the correct size to 
  2.7922        ** insertCell(), so reparse the cell now.
  2.7923        **
  2.7924 -      ** Note that this can never happen in an SQLite data file, as all
  2.7925 -      ** cells are at least 4 bytes. It only happens in b-trees used
  2.7926 -      ** to evaluate "IN (SELECT ...)" and similar clauses.
  2.7927 +      ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
  2.7928 +      ** and WITHOUT ROWID tables with exactly one column which is the
  2.7929 +      ** primary key.
  2.7930        */
  2.7931        if( b.szCell[j]==4 ){
  2.7932          assert(leafCorrection==4);
  2.7933 @@ -63701,6 +65866,28 @@
  2.7934    pPage = pCur->apPage[iCellDepth];
  2.7935    pCell = findCell(pPage, iCellIdx);
  2.7936  
  2.7937 +  /* If the bPreserve flag is set to true, then the cursor position must
  2.7938 +  ** be preserved following this delete operation. If the current delete
  2.7939 +  ** will cause a b-tree rebalance, then this is done by saving the cursor
  2.7940 +  ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
  2.7941 +  ** returning. 
  2.7942 +  **
  2.7943 +  ** Or, if the current delete will not cause a rebalance, then the cursor
  2.7944 +  ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
  2.7945 +  ** before or after the deleted entry. In this case set bSkipnext to true.  */
  2.7946 +  if( bPreserve ){
  2.7947 +    if( !pPage->leaf 
  2.7948 +     || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
  2.7949 +    ){
  2.7950 +      /* A b-tree rebalance will be required after deleting this entry.
  2.7951 +      ** Save the cursor key.  */
  2.7952 +      rc = saveCursorKey(pCur);
  2.7953 +      if( rc ) return rc;
  2.7954 +    }else{
  2.7955 +      bSkipnext = 1;
  2.7956 +    }
  2.7957 +  }
  2.7958 +
  2.7959    /* If the page containing the entry to delete is not a leaf page, move
  2.7960    ** the cursor to the largest entry in the tree that is smaller than
  2.7961    ** the entry being deleted. This cell will replace the cell being deleted
  2.7962 @@ -63727,28 +65914,6 @@
  2.7963      invalidateIncrblobCursors(p, pCur->info.nKey, 0);
  2.7964    }
  2.7965  
  2.7966 -  /* If the bPreserve flag is set to true, then the cursor position must
  2.7967 -  ** be preserved following this delete operation. If the current delete
  2.7968 -  ** will cause a b-tree rebalance, then this is done by saving the cursor
  2.7969 -  ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
  2.7970 -  ** returning. 
  2.7971 -  **
  2.7972 -  ** Or, if the current delete will not cause a rebalance, then the cursor
  2.7973 -  ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
  2.7974 -  ** before or after the deleted entry. In this case set bSkipnext to true.  */
  2.7975 -  if( bPreserve ){
  2.7976 -    if( !pPage->leaf 
  2.7977 -     || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
  2.7978 -    ){
  2.7979 -      /* A b-tree rebalance will be required after deleting this entry.
  2.7980 -      ** Save the cursor key.  */
  2.7981 -      rc = saveCursorKey(pCur);
  2.7982 -      if( rc ) return rc;
  2.7