SQLite Android Bindings
Check-in [b085f3f1fc]
Not logged in

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

Overview
Comment:Update this project to version 3.23.0.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b085f3f1fc9b59557eda127b6103be73991ff9b8
User & Date: dan 2018-04-02 11:10:57
Context
2018-04-10
18:04
Update this project to SQLite version 3.23.1. check-in: 1064adb2bb user: dan tags: trunk
2018-04-02
11:10
Update this project to version 3.23.0. check-in: b085f3f1fc user: dan tags: trunk
2018-01-22
19:26
Upgrade build.gradle to include new google() repository. check-in: 9e0de54b71 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.22.0.  By combining all the individual C code files into this
            3  +** version 3.23.0.  By combining all the individual C code files into this
     4      4   ** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
   209    209   #if SQLITE_ENABLE_ATOMIC_WRITE
   210    210     "ENABLE_ATOMIC_WRITE",
   211    211   #endif
   212    212   #if SQLITE_ENABLE_BATCH_ATOMIC_WRITE
   213    213     "ENABLE_BATCH_ATOMIC_WRITE",
   214    214   #endif
   215    215   #if SQLITE_ENABLE_CEROD
   216         -  "ENABLE_CEROD",
          216  +  "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD),
   217    217   #endif
   218    218   #if SQLITE_ENABLE_COLUMN_METADATA
   219    219     "ENABLE_COLUMN_METADATA",
   220    220   #endif
   221    221   #if SQLITE_ENABLE_COLUMN_USED_MASK
   222    222     "ENABLE_COLUMN_USED_MASK",
   223    223   #endif
................................................................................
  1143   1143   ** been edited in any way since it was last checked in, then the last
  1144   1144   ** four hexadecimal digits of the hash may be modified.
  1145   1145   **
  1146   1146   ** See also: [sqlite3_libversion()],
  1147   1147   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  1148   1148   ** [sqlite_version()] and [sqlite_source_id()].
  1149   1149   */
  1150         -#define SQLITE_VERSION        "3.22.0"
  1151         -#define SQLITE_VERSION_NUMBER 3022000
  1152         -#define SQLITE_SOURCE_ID      "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
         1150  +#define SQLITE_VERSION        "3.23.0"
         1151  +#define SQLITE_VERSION_NUMBER 3023000
         1152  +#define SQLITE_SOURCE_ID      "2018-04-02 11:04:16 736b53f57f70b23172c30880186dce7ad9baa3b74e3838cae5847cffb98f5cd2"
  1153   1153   
  1154   1154   /*
  1155   1155   ** CAPI3REF: Run-Time Library Version Numbers
  1156   1156   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1157   1157   **
  1158   1158   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1159   1159   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  2084   2084   ** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
  2085   2085   ** operations since the previous successful call to 
  2086   2086   ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
  2087   2087   ** ^This file control takes the file descriptor out of batch write mode
  2088   2088   ** so that all subsequent write operations are independent.
  2089   2089   ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
  2090   2090   ** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
         2091  +**
         2092  +** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
         2093  +** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
         2094  +** a file lock using the xLock or xShmLock methods of the VFS to wait
         2095  +** for up to M milliseconds before failing, where M is the single 
         2096  +** unsigned integer parameter.
  2091   2097   ** </ul>
  2092   2098   */
  2093   2099   #define SQLITE_FCNTL_LOCKSTATE               1
  2094   2100   #define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
  2095   2101   #define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
  2096   2102   #define SQLITE_FCNTL_LAST_ERRNO              4
  2097   2103   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
  2118   2124   #define SQLITE_FCNTL_VFS_POINTER            27
  2119   2125   #define SQLITE_FCNTL_JOURNAL_POINTER        28
  2120   2126   #define SQLITE_FCNTL_WIN32_GET_HANDLE       29
  2121   2127   #define SQLITE_FCNTL_PDB                    30
  2122   2128   #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
  2123   2129   #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
  2124   2130   #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
         2131  +#define SQLITE_FCNTL_LOCK_TIMEOUT           34
  2125   2132   
  2126   2133   /* deprecated names */
  2127   2134   #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
  2128   2135   #define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
  2129   2136   #define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
  2130   2137   
  2131   2138   
................................................................................
  3074   3081   **
  3075   3082   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  3076   3083   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  3077   3084   ** database handle, SQLite checks if this will mean that there are now no 
  3078   3085   ** connections at all to the database. If so, it performs a checkpoint 
  3079   3086   ** operation before closing the connection. This option may be used to
  3080   3087   ** override this behaviour. The first parameter passed to this operation
  3081         -** is an integer - non-zero to disable checkpoints-on-close, or zero (the
  3082         -** default) to enable them. The second parameter is a pointer to an integer
         3088  +** is an integer - positive to disable checkpoints-on-close, or zero (the
         3089  +** default) to enable them, and negative to leave the setting unchanged.
         3090  +** The second parameter is a pointer to an integer
  3083   3091   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  3084   3092   ** have been disabled - 0 if they are not disabled, 1 if they are.
  3085   3093   ** </dd>
         3094  +**
  3086   3095   ** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  3087   3096   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  3088   3097   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  3089   3098   ** a single SQL query statement will always use the same algorithm regardless
  3090   3099   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  3091   3100   ** that look at the values of bound parameters, which can make some queries
  3092   3101   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  3093   3102   ** the QPSG active, SQLite will always use the same query plan in the field as
  3094   3103   ** was used during testing in the lab.
         3104  +** The first argument to this setting is an integer which is 0 to disable 
         3105  +** the QPSG, positive to enable QPSG, or negative to leave the setting
         3106  +** unchanged. The second parameter is a pointer to an integer into which
         3107  +** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
         3108  +** following this call.
  3095   3109   ** </dd>
         3110  +**
  3096   3111   ** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  3097   3112   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  3098   3113   ** include output for any operations performed by trigger programs. This
  3099   3114   ** option is used to set or clear (the default) a flag that governs this
  3100   3115   ** behavior. The first parameter passed to this operation is an integer -
  3101         -** non-zero to enable output for trigger programs, or zero to disable it.
         3116  +** positive to enable output for trigger programs, or zero to disable it,
         3117  +** or negative to leave the setting unchanged.
  3102   3118   ** The second parameter is a pointer to an integer into which is written 
  3103   3119   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  3104   3120   ** it is not disabled, 1 if it is.  
  3105   3121   ** </dd>
  3106   3122   ** </dl>
  3107   3123   */
  3108   3124   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
................................................................................
  3516   3532   SQLITE_API void sqlite3_free_table(char **result);
  3517   3533   
  3518   3534   /*
  3519   3535   ** CAPI3REF: Formatted String Printing Functions
  3520   3536   **
  3521   3537   ** These routines are work-alikes of the "printf()" family of functions
  3522   3538   ** from the standard C library.
  3523         -** These routines understand most of the common K&R formatting options,
  3524         -** plus some additional non-standard formats, detailed below.
  3525         -** Note that some of the more obscure formatting options from recent
  3526         -** C-library standards are omitted from this implementation.
         3539  +** These routines understand most of the common formatting options from
         3540  +** the standard library printf() 
         3541  +** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
         3542  +** See the [built-in printf()] documentation for details.
  3527   3543   **
  3528   3544   ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  3529         -** results into memory obtained from [sqlite3_malloc()].
         3545  +** results into memory obtained from [sqlite3_malloc64()].
  3530   3546   ** The strings returned by these two routines should be
  3531   3547   ** released by [sqlite3_free()].  ^Both routines return a
  3532         -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
         3548  +** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
  3533   3549   ** memory to hold the resulting string.
  3534   3550   **
  3535   3551   ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  3536   3552   ** the standard C library.  The result is written into the
  3537   3553   ** buffer supplied as the second parameter whose size is given by
  3538   3554   ** the first parameter. Note that the order of the
  3539   3555   ** first two parameters is reversed from snprintf().)^  This is an
................................................................................
  3549   3565   ** guarantees that the buffer is always zero-terminated.  ^The first
  3550   3566   ** parameter "n" is the total size of the buffer, including space for
  3551   3567   ** the zero terminator.  So the longest string that can be completely
  3552   3568   ** written will be n-1 characters.
  3553   3569   **
  3554   3570   ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  3555   3571   **
  3556         -** These routines all implement some additional formatting
  3557         -** options that are useful for constructing SQL statements.
  3558         -** All of the usual printf() formatting options apply.  In addition, there
  3559         -** is are "%q", "%Q", "%w" and "%z" options.
  3560         -**
  3561         -** ^(The %q option works like %s in that it substitutes a nul-terminated
  3562         -** string from the argument list.  But %q also doubles every '\'' character.
  3563         -** %q is designed for use inside a string literal.)^  By doubling each '\''
  3564         -** character it escapes that character and allows it to be inserted into
  3565         -** the string.
  3566         -**
  3567         -** For example, assume the string variable zText contains text as follows:
  3568         -**
  3569         -** <blockquote><pre>
  3570         -**  char *zText = "It's a happy day!";
  3571         -** </pre></blockquote>
  3572         -**
  3573         -** One can use this text in an SQL statement as follows:
  3574         -**
  3575         -** <blockquote><pre>
  3576         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  3577         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  3578         -**  sqlite3_free(zSQL);
  3579         -** </pre></blockquote>
  3580         -**
  3581         -** Because the %q format string is used, the '\'' character in zText
  3582         -** is escaped and the SQL generated is as follows:
  3583         -**
  3584         -** <blockquote><pre>
  3585         -**  INSERT INTO table1 VALUES('It''s a happy day!')
  3586         -** </pre></blockquote>
  3587         -**
  3588         -** This is correct.  Had we used %s instead of %q, the generated SQL
  3589         -** would have looked like this:
  3590         -**
  3591         -** <blockquote><pre>
  3592         -**  INSERT INTO table1 VALUES('It's a happy day!');
  3593         -** </pre></blockquote>
  3594         -**
  3595         -** This second example is an SQL syntax error.  As a general rule you should
  3596         -** always use %q instead of %s when inserting text into a string literal.
  3597         -**
  3598         -** ^(The %Q option works like %q except it also adds single quotes around
  3599         -** the outside of the total string.  Additionally, if the parameter in the
  3600         -** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  3601         -** single quotes).)^  So, for example, one could say:
  3602         -**
  3603         -** <blockquote><pre>
  3604         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  3605         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  3606         -**  sqlite3_free(zSQL);
  3607         -** </pre></blockquote>
  3608         -**
  3609         -** The code above will render a correct SQL statement in the zSQL
  3610         -** variable even if the zText variable is a NULL pointer.
  3611         -**
  3612         -** ^(The "%w" formatting option is like "%q" except that it expects to
  3613         -** be contained within double-quotes instead of single quotes, and it
  3614         -** escapes the double-quote character instead of the single-quote
  3615         -** character.)^  The "%w" formatting option is intended for safely inserting
  3616         -** table and column names into a constructed SQL statement.
  3617         -**
  3618         -** ^(The "%z" formatting option works like "%s" but with the
  3619         -** addition that after the string has been read and copied into
  3620         -** the result, [sqlite3_free()] is called on the input string.)^
         3572  +** See also:  [built-in printf()], [printf() SQL function]
  3621   3573   */
  3622   3574   SQLITE_API char *sqlite3_mprintf(const char*,...);
  3623   3575   SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  3624   3576   SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  3625   3577   SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  3626   3578   
  3627   3579   /*
................................................................................
  4679   4631   ** a schema change, on the first  [sqlite3_step()] call following any change
  4680   4632   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  4681   4633   ** ^The specific value of WHERE-clause [parameter] might influence the 
  4682   4634   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  4683   4635   ** or [GLOB] operator or if the parameter is compared to an indexed column
  4684   4636   ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  4685   4637   ** </li>
         4638  +** </ol>
  4686   4639   **
  4687   4640   ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
  4688   4641   ** the extra prepFlags parameter, which is a bit array consisting of zero or
  4689   4642   ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
  4690   4643   ** sqlite3_prepare_v2() interface works exactly the same as
  4691   4644   ** sqlite3_prepare_v3() with a zero prepFlags parameter.
  4692         -** </ol>
  4693   4645   */
  4694   4646   SQLITE_API int sqlite3_prepare(
  4695   4647     sqlite3 *db,            /* Database handle */
  4696   4648     const char *zSql,       /* SQL statement, UTF-8 encoded */
  4697   4649     int nByte,              /* Maximum length of zSql in bytes. */
  4698   4650     sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  4699   4651     const char **pzTail     /* OUT: Pointer to unused portion of zSql */
................................................................................
  8313   8265   ** wal file in wal mode databases, or the number of pages written to the
  8314   8266   ** database file in rollback mode databases. Any pages written as part of
  8315   8267   ** transaction rollback or database recovery operations are not included.
  8316   8268   ** If an IO or other error occurs while writing a page to disk, the effect
  8317   8269   ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  8318   8270   ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  8319   8271   ** </dd>
         8272  +**
         8273  +** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
         8274  +** <dd>This parameter returns the number of dirty cache entries that have
         8275  +** been written to disk in the middle of a transaction due to the page
         8276  +** cache overflowing. Transactions are more efficient if they are written
         8277  +** to disk all at once. When pages spill mid-transaction, that introduces
         8278  +** additional overhead. This parameter can be used help identify
         8279  +** inefficiencies that can be resolve by increasing the cache size.
         8280  +** </dd>
  8320   8281   **
  8321   8282   ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  8322   8283   ** <dd>This parameter returns zero for the current value if and only if
  8323   8284   ** all foreign key constraints (deferred or immediate) have been
  8324   8285   ** resolved.)^  ^The highwater mark is always 0.
  8325   8286   ** </dd>
  8326   8287   ** </dl>
................................................................................
  8333   8294   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  8334   8295   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  8335   8296   #define SQLITE_DBSTATUS_CACHE_HIT            7
  8336   8297   #define SQLITE_DBSTATUS_CACHE_MISS           8
  8337   8298   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  8338   8299   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  8339   8300   #define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
  8340         -#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
         8301  +#define SQLITE_DBSTATUS_CACHE_SPILL         12
         8302  +#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
  8341   8303   
  8342   8304   
  8343   8305   /*
  8344   8306   ** CAPI3REF: Prepared Statement Status
  8345   8307   ** METHOD: sqlite3_stmt
  8346   8308   **
  8347   8309   ** ^(Each prepared statement maintains various
................................................................................
  9813   9775   ** transaction open on the database, or if the database is not a wal mode
  9814   9776   ** database.
  9815   9777   **
  9816   9778   ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  9817   9779   */
  9818   9780   SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
  9819   9781   
         9782  +/*
         9783  +** CAPI3REF: Serialize a database
         9784  +**
         9785  +** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
         9786  +** that is a serialization of the S database on [database connection] D.
         9787  +** If P is not a NULL pointer, then the size of the database in bytes
         9788  +** is written into *P.
         9789  +**
         9790  +** For an ordinary on-disk database file, the serialization is just a
         9791  +** copy of the disk file.  For an in-memory database or a "TEMP" database,
         9792  +** the serialization is the same sequence of bytes which would be written
         9793  +** to disk if that database where backed up to disk.
         9794  +**
         9795  +** The usual case is that sqlite3_serialize() copies the serialization of
         9796  +** the database into memory obtained from [sqlite3_malloc64()] and returns
         9797  +** a pointer to that memory.  The caller is responsible for freeing the
         9798  +** returned value to avoid a memory leak.  However, if the F argument
         9799  +** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
         9800  +** are made, and the sqlite3_serialize() function will return a pointer
         9801  +** to the contiguous memory representation of the database that SQLite
         9802  +** is currently using for that database, or NULL if the no such contiguous
         9803  +** memory representation of the database exists.  A contiguous memory
         9804  +** representation of the database will usually only exist if there has
         9805  +** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
         9806  +** values of D and S.
         9807  +** The size of the database is written into *P even if the 
         9808  +** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
         9809  +** of the database exists.
         9810  +**
         9811  +** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
         9812  +** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
         9813  +** allocation error occurs.
         9814  +**
         9815  +** This interface is only available if SQLite is compiled with the
         9816  +** [SQLITE_ENABLE_DESERIALIZE] option.
         9817  +*/
         9818  +SQLITE_API unsigned char *sqlite3_serialize(
         9819  +  sqlite3 *db,           /* The database connection */
         9820  +  const char *zSchema,   /* Which DB to serialize. ex: "main", "temp", ... */
         9821  +  sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
         9822  +  unsigned int mFlags    /* Zero or more SQLITE_SERIALIZE_* flags */
         9823  +);
         9824  +
         9825  +/*
         9826  +** CAPI3REF: Flags for sqlite3_serialize
         9827  +**
         9828  +** Zero or more of the following constants can be OR-ed together for
         9829  +** the F argument to [sqlite3_serialize(D,S,P,F)].
         9830  +**
         9831  +** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
         9832  +** a pointer to contiguous in-memory database that it is currently using,
         9833  +** without making a copy of the database.  If SQLite is not currently using
         9834  +** a contiguous in-memory database, then this option causes
         9835  +** [sqlite3_serialize()] to return a NULL pointer.  SQLite will only be
         9836  +** using a contiguous in-memory database if it has been initialized by a
         9837  +** prior call to [sqlite3_deserialize()].
         9838  +*/
         9839  +#define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
         9840  +
         9841  +/*
         9842  +** CAPI3REF: Deserialize a database
         9843  +**
         9844  +** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the 
         9845  +** [database connection] D to disconnect from database S and then
         9846  +** reopen S as an in-memory database based on the serialization contained
         9847  +** in P.  The serialized database P is N bytes in size.  M is the size of
         9848  +** the buffer P, which might be larger than N.  If M is larger than N, and
         9849  +** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
         9850  +** permitted to add content to the in-memory database as long as the total
         9851  +** size does not exceed M bytes.
         9852  +**
         9853  +** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
         9854  +** invoke sqlite3_free() on the serialization buffer when the database
         9855  +** connection closes.  If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
         9856  +** SQLite will try to increase the buffer size using sqlite3_realloc64()
         9857  +** if writes on the database cause it to grow larger than M bytes.
         9858  +**
         9859  +** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
         9860  +** database is currently in a read transaction or is involved in a backup
         9861  +** operation.
         9862  +**
         9863  +** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the 
         9864  +** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
         9865  +** [sqlite3_free()] is invoked on argument P prior to returning.
         9866  +**
         9867  +** This interface is only available if SQLite is compiled with the
         9868  +** [SQLITE_ENABLE_DESERIALIZE] option.
         9869  +*/
         9870  +SQLITE_API int sqlite3_deserialize(
         9871  +  sqlite3 *db,            /* The database connection */
         9872  +  const char *zSchema,    /* Which DB to reopen with the deserialization */
         9873  +  unsigned char *pData,   /* The serialized database content */
         9874  +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
         9875  +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
         9876  +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
         9877  +);
         9878  +
         9879  +/*
         9880  +** CAPI3REF: Flags for sqlite3_deserialize()
         9881  +**
         9882  +** The following are allowed values for 6th argument (the F argument) to
         9883  +** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
         9884  +**
         9885  +** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
         9886  +** in the P argument is held in memory obtained from [sqlite3_malloc64()]
         9887  +** and that SQLite should take ownership of this memory and automatically
         9888  +** free it when it has finished using it.  Without this flag, the caller
         9889  +** is resposible for freeing any dynamically allocated memory.
         9890  +**
         9891  +** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
         9892  +** grow the size of the database using calls to [sqlite3_realloc64()].  This
         9893  +** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
         9894  +** Without this flag, the deserialized database cannot increase in size beyond
         9895  +** the number of bytes specified by the M parameter.
         9896  +**
         9897  +** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
         9898  +** should be treated as read-only.
         9899  +*/
         9900  +#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
         9901  +#define SQLITE_DESERIALIZE_RESIZEABLE  2 /* Resize using sqlite3_realloc64() */
         9902  +#define SQLITE_DESERIALIZE_READONLY    4 /* Database is read-only */
         9903  +
  9820   9904   /*
  9821   9905   ** Undo the hack that converts floating point types to integer for
  9822   9906   ** builds on processors without floating point support.
  9823   9907   */
  9824   9908   #ifdef SQLITE_OMIT_FLOATING_POINT
  9825   9909   # undef double
  9826   9910   #endif
................................................................................
  9960  10044   #if 0
  9961  10045   extern "C" {
  9962  10046   #endif
  9963  10047   
  9964  10048   
  9965  10049   /*
  9966  10050   ** CAPI3REF: Session Object Handle
        10051  +**
        10052  +** An instance of this object is a [session] that can be used to
        10053  +** record changes to a database.
  9967  10054   */
  9968  10055   typedef struct sqlite3_session sqlite3_session;
  9969  10056   
  9970  10057   /*
  9971  10058   ** CAPI3REF: Changeset Iterator Handle
        10059  +**
        10060  +** An instance of this object acts as a cursor for iterating
        10061  +** over the elements of a [changeset] or [patchset].
  9972  10062   */
  9973  10063   typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
  9974  10064   
  9975  10065   /*
  9976  10066   ** CAPI3REF: Create A New Session Object
        10067  +** CONSTRUCTOR: sqlite3_session
  9977  10068   **
  9978  10069   ** Create a new session object attached to database handle db. If successful,
  9979  10070   ** a pointer to the new object is written to *ppSession and SQLITE_OK is
  9980  10071   ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
  9981  10072   ** error code (e.g. SQLITE_NOMEM) is returned.
  9982  10073   **
  9983  10074   ** It is possible to create multiple session objects attached to a single
................................................................................
 10006  10097     sqlite3 *db,                    /* Database handle */
 10007  10098     const char *zDb,                /* Name of db (e.g. "main") */
 10008  10099     sqlite3_session **ppSession     /* OUT: New session object */
 10009  10100   );
 10010  10101   
 10011  10102   /*
 10012  10103   ** CAPI3REF: Delete A Session Object
        10104  +** DESTRUCTOR: sqlite3_session
 10013  10105   **
 10014  10106   ** Delete a session object previously allocated using 
 10015  10107   ** [sqlite3session_create()]. Once a session object has been deleted, the
 10016  10108   ** results of attempting to use pSession with any other session module
 10017  10109   ** function are undefined.
 10018  10110   **
 10019  10111   ** Session objects must be deleted before the database handle to which they
................................................................................
 10021  10113   ** [sqlite3session_create()] for details.
 10022  10114   */
 10023  10115   SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
 10024  10116   
 10025  10117   
 10026  10118   /*
 10027  10119   ** CAPI3REF: Enable Or Disable A Session Object
        10120  +** METHOD: sqlite3_session
 10028  10121   **
 10029  10122   ** Enable or disable the recording of changes by a session object. When
 10030  10123   ** enabled, a session object records changes made to the database. When
 10031  10124   ** disabled - it does not. A newly created session object is enabled.
 10032  10125   ** Refer to the documentation for [sqlite3session_changeset()] for further
 10033  10126   ** details regarding how enabling and disabling a session object affects
 10034  10127   ** the eventual changesets.
................................................................................
 10040  10133   ** The return value indicates the final state of the session object: 0 if 
 10041  10134   ** the session is disabled, or 1 if it is enabled.
 10042  10135   */
 10043  10136   SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
 10044  10137   
 10045  10138   /*
 10046  10139   ** CAPI3REF: Set Or Clear the Indirect Change Flag
        10140  +** METHOD: sqlite3_session
 10047  10141   **
 10048  10142   ** Each change recorded by a session object is marked as either direct or
 10049  10143   ** indirect. A change is marked as indirect if either:
 10050  10144   **
 10051  10145   ** <ul>
 10052  10146   **   <li> The session object "indirect" flag is set when the change is
 10053  10147   **        made, or
................................................................................
 10069  10163   ** The return value indicates the final state of the indirect flag: 0 if 
 10070  10164   ** it is clear, or 1 if it is set.
 10071  10165   */
 10072  10166   SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
 10073  10167   
 10074  10168   /*
 10075  10169   ** CAPI3REF: Attach A Table To A Session Object
        10170  +** METHOD: sqlite3_session
 10076  10171   **
 10077  10172   ** If argument zTab is not NULL, then it is the name of a table to attach
 10078  10173   ** to the session object passed as the first argument. All subsequent changes 
 10079  10174   ** made to the table while the session object is enabled will be recorded. See 
 10080  10175   ** documentation for [sqlite3session_changeset()] for further details.
 10081  10176   **
 10082  10177   ** Or, if argument zTab is NULL, then changes are recorded for all tables
................................................................................
 10131  10226   SQLITE_API int sqlite3session_attach(
 10132  10227     sqlite3_session *pSession,      /* Session object */
 10133  10228     const char *zTab                /* Table name */
 10134  10229   );
 10135  10230   
 10136  10231   /*
 10137  10232   ** CAPI3REF: Set a table filter on a Session Object.
        10233  +** METHOD: sqlite3_session
 10138  10234   **
 10139  10235   ** The second argument (xFilter) is the "filter callback". For changes to rows 
 10140  10236   ** in tables that are not attached to the Session object, the filter is called
 10141  10237   ** to determine whether changes to the table's rows should be tracked or not. 
 10142  10238   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
 10143  10239   ** attached, xFilter will not be called again.
 10144  10240   */
................................................................................
 10149  10245       const char *zTab              /* Table name */
 10150  10246     ),
 10151  10247     void *pCtx                      /* First argument passed to xFilter */
 10152  10248   );
 10153  10249   
 10154  10250   /*
 10155  10251   ** CAPI3REF: Generate A Changeset From A Session Object
        10252  +** METHOD: sqlite3_session
 10156  10253   **
 10157  10254   ** Obtain a changeset containing changes to the tables attached to the 
 10158  10255   ** session object passed as the first argument. If successful, 
 10159  10256   ** set *ppChangeset to point to a buffer containing the changeset 
 10160  10257   ** and *pnChangeset to the size of the changeset in bytes before returning
 10161  10258   ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
 10162  10259   ** zero and return an SQLite error code.
................................................................................
 10258  10355   SQLITE_API int sqlite3session_changeset(
 10259  10356     sqlite3_session *pSession,      /* Session object */
 10260  10357     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
 10261  10358     void **ppChangeset              /* OUT: Buffer containing changeset */
 10262  10359   );
 10263  10360   
 10264  10361   /*
 10265         -** CAPI3REF: Load The Difference Between Tables Into A Session 
        10362  +** CAPI3REF: Load The Difference Between Tables Into A Session
        10363  +** METHOD: sqlite3_session
 10266  10364   **
 10267  10365   ** If it is not already attached to the session object passed as the first
 10268  10366   ** argument, this function attaches table zTbl in the same manner as the
 10269  10367   ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
 10270  10368   ** does not have a primary key, this function is a no-op (but does not return
 10271  10369   ** an error).
 10272  10370   **
................................................................................
 10323  10421     const char *zTbl,
 10324  10422     char **pzErrMsg
 10325  10423   );
 10326  10424   
 10327  10425   
 10328  10426   /*
 10329  10427   ** CAPI3REF: Generate A Patchset From A Session Object
        10428  +** METHOD: sqlite3_session
 10330  10429   **
 10331  10430   ** The differences between a patchset and a changeset are that:
 10332  10431   **
 10333  10432   ** <ul>
 10334  10433   **   <li> DELETE records consist of the primary key fields only. The 
 10335  10434   **        original values of other fields are omitted.
 10336  10435   **   <li> The original values of any modified fields are omitted from 
................................................................................
 10374  10473   ** guaranteed that a call to sqlite3session_changeset() will return a 
 10375  10474   ** changeset containing zero changes.
 10376  10475   */
 10377  10476   SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
 10378  10477   
 10379  10478   /*
 10380  10479   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
        10480  +** CONSTRUCTOR: sqlite3_changeset_iter
 10381  10481   **
 10382  10482   ** Create an iterator used to iterate through the contents of a changeset.
 10383  10483   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
 10384  10484   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
 10385  10485   ** SQLite error code is returned.
 10386  10486   **
 10387  10487   ** The following functions can be used to advance and query a changeset 
................................................................................
 10414  10514     int nChangeset,                 /* Size of changeset blob in bytes */
 10415  10515     void *pChangeset                /* Pointer to blob containing changeset */
 10416  10516   );
 10417  10517   
 10418  10518   
 10419  10519   /*
 10420  10520   ** CAPI3REF: Advance A Changeset Iterator
        10521  +** METHOD: sqlite3_changeset_iter
 10421  10522   **
 10422  10523   ** This function may only be used with iterators created by function
 10423  10524   ** [sqlite3changeset_start()]. If it is called on an iterator passed to
 10424  10525   ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
 10425  10526   ** is returned and the call has no effect.
 10426  10527   **
 10427  10528   ** Immediately after an iterator is created by sqlite3changeset_start(), it
................................................................................
 10438  10539   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
 10439  10540   ** SQLITE_NOMEM.
 10440  10541   */
 10441  10542   SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
 10442  10543   
 10443  10544   /*
 10444  10545   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
        10546  +** METHOD: sqlite3_changeset_iter
 10445  10547   **
 10446  10548   ** The pIter argument passed to this function may either be an iterator
 10447  10549   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10448  10550   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10449  10551   ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
 10450  10552   ** is not the case, this function returns [SQLITE_MISUSE].
 10451  10553   **
................................................................................
 10472  10574     int *pnCol,                     /* OUT: Number of columns in table */
 10473  10575     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
 10474  10576     int *pbIndirect                 /* OUT: True for an 'indirect' change */
 10475  10577   );
 10476  10578   
 10477  10579   /*
 10478  10580   ** CAPI3REF: Obtain The Primary Key Definition Of A Table
        10581  +** METHOD: sqlite3_changeset_iter
 10479  10582   **
 10480  10583   ** For each modified table, a changeset includes the following:
 10481  10584   **
 10482  10585   ** <ul>
 10483  10586   **   <li> The number of columns in the table, and
 10484  10587   **   <li> Which of those columns make up the tables PRIMARY KEY.
 10485  10588   ** </ul>
................................................................................
 10503  10606     sqlite3_changeset_iter *pIter,  /* Iterator object */
 10504  10607     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
 10505  10608     int *pnCol                      /* OUT: Number of entries in output array */
 10506  10609   );
 10507  10610   
 10508  10611   /*
 10509  10612   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
        10613  +** METHOD: sqlite3_changeset_iter
 10510  10614   **
 10511  10615   ** The pIter argument passed to this function may either be an iterator
 10512  10616   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10513  10617   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10514  10618   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
 10515  10619   ** Furthermore, it may only be called if the type of change that the iterator
 10516  10620   ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
................................................................................
 10533  10637     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10534  10638     int iVal,                       /* Column number */
 10535  10639     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
 10536  10640   );
 10537  10641   
 10538  10642   /*
 10539  10643   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
        10644  +** METHOD: sqlite3_changeset_iter
 10540  10645   **
 10541  10646   ** The pIter argument passed to this function may either be an iterator
 10542  10647   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10543  10648   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10544  10649   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
 10545  10650   ** Furthermore, it may only be called if the type of change that the iterator
 10546  10651   ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
................................................................................
 10566  10671     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10567  10672     int iVal,                       /* Column number */
 10568  10673     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
 10569  10674   );
 10570  10675   
 10571  10676   /*
 10572  10677   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
        10678  +** METHOD: sqlite3_changeset_iter
 10573  10679   **
 10574  10680   ** This function should only be used with iterator objects passed to a
 10575  10681   ** conflict-handler callback by [sqlite3changeset_apply()] with either
 10576  10682   ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
 10577  10683   ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
 10578  10684   ** is set to NULL.
 10579  10685   **
................................................................................
 10593  10699     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10594  10700     int iVal,                       /* Column number */
 10595  10701     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
 10596  10702   );
 10597  10703   
 10598  10704   /*
 10599  10705   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
        10706  +** METHOD: sqlite3_changeset_iter
 10600  10707   **
 10601  10708   ** This function may only be called with an iterator passed to an
 10602  10709   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
 10603  10710   ** it sets the output variable to the total number of known foreign key
 10604  10711   ** violations in the destination database and returns SQLITE_OK.
 10605  10712   **
 10606  10713   ** In all other cases this function returns SQLITE_MISUSE.
................................................................................
 10609  10716     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10610  10717     int *pnOut                      /* OUT: Number of FK violations */
 10611  10718   );
 10612  10719   
 10613  10720   
 10614  10721   /*
 10615  10722   ** CAPI3REF: Finalize A Changeset Iterator
        10723  +** METHOD: sqlite3_changeset_iter
 10616  10724   **
 10617  10725   ** This function is used to finalize an iterator allocated with
 10618  10726   ** [sqlite3changeset_start()].
 10619  10727   **
 10620  10728   ** This function should only be called on iterators created using the
 10621  10729   ** [sqlite3changeset_start()] function. If an application calls this
 10622  10730   ** function with an iterator passed to a conflict-handler by
................................................................................
 10625  10733   **
 10626  10734   ** If an error was encountered within a call to an sqlite3changeset_xxx()
 10627  10735   ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
 10628  10736   ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
 10629  10737   ** to that error is returned by this function. Otherwise, SQLITE_OK is
 10630  10738   ** returned. This is to allow the following pattern (pseudo-code):
 10631  10739   **
        10740  +** <pre>
 10632  10741   **   sqlite3changeset_start();
 10633  10742   **   while( SQLITE_ROW==sqlite3changeset_next() ){
 10634  10743   **     // Do something with change.
 10635  10744   **   }
 10636  10745   **   rc = sqlite3changeset_finalize();
 10637  10746   **   if( rc!=SQLITE_OK ){
 10638  10747   **     // An error has occurred 
 10639  10748   **   }
        10749  +** </pre>
 10640  10750   */
 10641  10751   SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
 10642  10752   
 10643  10753   /*
 10644  10754   ** CAPI3REF: Invert A Changeset
 10645  10755   **
 10646  10756   ** This function is used to "invert" a changeset object. Applying an inverted
................................................................................
 10680  10790   ** single changeset. The result is a changeset equivalent to applying
 10681  10791   ** changeset A followed by changeset B. 
 10682  10792   **
 10683  10793   ** This function combines the two input changesets using an 
 10684  10794   ** sqlite3_changegroup object. Calling it produces similar results as the
 10685  10795   ** following code fragment:
 10686  10796   **
        10797  +** <pre>
 10687  10798   **   sqlite3_changegroup *pGrp;
 10688  10799   **   rc = sqlite3_changegroup_new(&pGrp);
 10689  10800   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
 10690  10801   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
 10691  10802   **   if( rc==SQLITE_OK ){
 10692  10803   **     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
 10693  10804   **   }else{
 10694  10805   **     *ppOut = 0;
 10695  10806   **     *pnOut = 0;
 10696  10807   **   }
        10808  +** </pre>
 10697  10809   **
 10698  10810   ** Refer to the sqlite3_changegroup documentation below for details.
 10699  10811   */
 10700  10812   SQLITE_API int sqlite3changeset_concat(
 10701  10813     int nA,                         /* Number of bytes in buffer pA */
 10702  10814     void *pA,                       /* Pointer to buffer containing changeset A */
 10703  10815     int nB,                         /* Number of bytes in buffer pB */
................................................................................
 10705  10817     int *pnOut,                     /* OUT: Number of bytes in output changeset */
 10706  10818     void **ppOut                    /* OUT: Buffer containing output changeset */
 10707  10819   );
 10708  10820   
 10709  10821   
 10710  10822   /*
 10711  10823   ** CAPI3REF: Changegroup Handle
        10824  +**
        10825  +** A changegroup is an object used to combine two or more 
        10826  +** [changesets] or [patchsets]
 10712  10827   */
 10713  10828   typedef struct sqlite3_changegroup sqlite3_changegroup;
 10714  10829   
 10715  10830   /*
 10716  10831   ** CAPI3REF: Create A New Changegroup Object
        10832  +** CONSTRUCTOR: sqlite3_changegroup
 10717  10833   **
 10718  10834   ** An sqlite3_changegroup object is used to combine two or more changesets
 10719  10835   ** (or patchsets) into a single changeset (or patchset). A single changegroup
 10720  10836   ** object may combine changesets or patchsets, but not both. The output is
 10721  10837   ** always in the same format as the input.
 10722  10838   **
 10723  10839   ** If successful, this function returns SQLITE_OK and populates (*pp) with
................................................................................
 10747  10863   ** sqlite3changegroup_output() functions, also available are the streaming
 10748  10864   ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
 10749  10865   */
 10750  10866   SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
 10751  10867   
 10752  10868   /*
 10753  10869   ** CAPI3REF: Add A Changeset To A Changegroup
        10870  +** METHOD: sqlite3_changegroup
 10754  10871   **
 10755  10872   ** Add all changes within the changeset (or patchset) in buffer pData (size
 10756  10873   ** nData bytes) to the changegroup. 
 10757  10874   **
 10758  10875   ** If the buffer contains a patchset, then all prior calls to this function
 10759  10876   ** on the same changegroup object must also have specified patchsets. Or, if
 10760  10877   ** the buffer contains a changeset, so must have the earlier calls to this
................................................................................
 10824  10941   **
 10825  10942   ** If no error occurs, SQLITE_OK is returned.
 10826  10943   */
 10827  10944   SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
 10828  10945   
 10829  10946   /*
 10830  10947   ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
        10948  +** METHOD: sqlite3_changegroup
 10831  10949   **
 10832  10950   ** Obtain a buffer containing a changeset (or patchset) representing the
 10833  10951   ** current contents of the changegroup. If the inputs to the changegroup
 10834  10952   ** were themselves changesets, the output is a changeset. Or, if the
 10835  10953   ** inputs were patchsets, the output is also a patchset.
 10836  10954   **
 10837  10955   ** As with the output of the sqlite3session_changeset() and
................................................................................
 10854  10972     sqlite3_changegroup*,
 10855  10973     int *pnData,                    /* OUT: Size of output buffer in bytes */
 10856  10974     void **ppData                   /* OUT: Pointer to output buffer */
 10857  10975   );
 10858  10976   
 10859  10977   /*
 10860  10978   ** CAPI3REF: Delete A Changegroup Object
        10979  +** DESTRUCTOR: sqlite3_changegroup
 10861  10980   */
 10862  10981   SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
 10863  10982   
 10864  10983   /*
 10865  10984   ** CAPI3REF: Apply A Changeset To A Database
 10866  10985   **
 10867         -** Apply a changeset to a database. This function attempts to update the
 10868         -** "main" database attached to handle db with the changes found in the
 10869         -** changeset passed via the second and third arguments.
        10986  +** Apply a changeset or patchset to a database. These functions attempt to
        10987  +** update the "main" database attached to handle db with the changes found in
        10988  +** the changeset passed via the second and third arguments. 
 10870  10989   **
 10871         -** The fourth argument (xFilter) passed to this function is the "filter
        10990  +** The fourth argument (xFilter) passed to these functions is the "filter
 10872  10991   ** callback". If it is not NULL, then for each table affected by at least one
 10873  10992   ** change in the changeset, the filter callback is invoked with
 10874  10993   ** the table name as the second argument, and a copy of the context pointer
 10875         -** passed as the sixth argument to this function as the first. If the "filter
 10876         -** callback" returns zero, then no attempt is made to apply any changes to 
 10877         -** the table. Otherwise, if the return value is non-zero or the xFilter
 10878         -** argument to this function is NULL, all changes related to the table are
 10879         -** attempted.
        10994  +** passed as the sixth argument as the first. If the "filter callback"
        10995  +** returns zero, then no attempt is made to apply any changes to the table.
        10996  +** Otherwise, if the return value is non-zero or the xFilter argument to
        10997  +** is NULL, all changes related to the table are attempted.
 10880  10998   **
 10881  10999   ** For each table that is not excluded by the filter callback, this function 
 10882  11000   ** tests that the target database contains a compatible table. A table is 
 10883  11001   ** considered compatible if all of the following are true:
 10884  11002   **
 10885  11003   ** <ul>
 10886  11004   **   <li> The table has the same name as the name recorded in the 
................................................................................
 10917  11035   ** actions are taken by sqlite3changeset_apply() depending on the value
 10918  11036   ** returned by each invocation of the conflict-handler function. Refer to
 10919  11037   ** the documentation for the three 
 10920  11038   ** [SQLITE_CHANGESET_OMIT|available return values] for details.
 10921  11039   **
 10922  11040   ** <dl>
 10923  11041   ** <dt>DELETE Changes<dd>
 10924         -**   For each DELETE change, this function checks if the target database 
        11042  +**   For each DELETE change, the function checks if the target database 
 10925  11043   **   contains a row with the same primary key value (or values) as the 
 10926  11044   **   original row values stored in the changeset. If it does, and the values 
 10927  11045   **   stored in all non-primary key columns also match the values stored in 
 10928  11046   **   the changeset the row is deleted from the target database.
 10929  11047   **
 10930  11048   **   If a row with matching primary key values is found, but one or more of
 10931  11049   **   the non-primary key fields contains a value different from the original
................................................................................
 10962  11080   **   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 
 10963  11081   **   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
 10964  11082   **   This includes the case where the INSERT operation is re-attempted because 
 10965  11083   **   an earlier call to the conflict handler function returned 
 10966  11084   **   [SQLITE_CHANGESET_REPLACE].
 10967  11085   **
 10968  11086   ** <dt>UPDATE Changes<dd>
 10969         -**   For each UPDATE change, this function checks if the target database 
        11087  +**   For each UPDATE change, the function checks if the target database 
 10970  11088   **   contains a row with the same primary key value (or values) as the 
 10971  11089   **   original row values stored in the changeset. If it does, and the values 
 10972  11090   **   stored in all modified non-primary key columns also match the values
 10973  11091   **   stored in the changeset the row is updated within the target database.
 10974  11092   **
 10975  11093   **   If a row with matching primary key values is found, but one or more of
 10976  11094   **   the modified non-primary key fields contains a value different from an
................................................................................
 10993  11111   ** </dl>
 10994  11112   **
 10995  11113   ** It is safe to execute SQL statements, including those that write to the
 10996  11114   ** table that the callback related to, from within the xConflict callback.
 10997  11115   ** This can be used to further customize the applications conflict
 10998  11116   ** resolution strategy.
 10999  11117   **
 11000         -** All changes made by this function are enclosed in a savepoint transaction.
        11118  +** All changes made by these functions are enclosed in a savepoint transaction.
 11001  11119   ** If any other error (aside from a constraint failure when attempting to
 11002  11120   ** write to the target database) occurs, then the savepoint transaction is
 11003  11121   ** rolled back, restoring the target database to its original state, and an 
 11004  11122   ** SQLite error code returned.
        11123  +**
        11124  +** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
        11125  +** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
        11126  +** may set (*ppRebase) to point to a "rebase" that may be used with the 
        11127  +** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
        11128  +** is set to the size of the buffer in bytes. It is the responsibility of the
        11129  +** caller to eventually free any such buffer using sqlite3_free(). The buffer
        11130  +** is only allocated and populated if one or more conflicts were encountered
        11131  +** while applying the patchset. See comments surrounding the sqlite3_rebaser
        11132  +** APIs for further details.
 11005  11133   */
 11006  11134   SQLITE_API int sqlite3changeset_apply(
 11007  11135     sqlite3 *db,                    /* Apply change to "main" db of this handle */
 11008  11136     int nChangeset,                 /* Size of changeset in bytes */
 11009  11137     void *pChangeset,               /* Changeset blob */
 11010  11138     int(*xFilter)(
 11011  11139       void *pCtx,                   /* Copy of sixth arg to _apply() */
................................................................................
 11013  11141     ),
 11014  11142     int(*xConflict)(
 11015  11143       void *pCtx,                   /* Copy of sixth arg to _apply() */
 11016  11144       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11017  11145       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11018  11146     ),
 11019  11147     void *pCtx                      /* First argument passed to xConflict */
        11148  +);
        11149  +SQLITE_API int sqlite3changeset_apply_v2(
        11150  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
        11151  +  int nChangeset,                 /* Size of changeset in bytes */
        11152  +  void *pChangeset,               /* Changeset blob */
        11153  +  int(*xFilter)(
        11154  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        11155  +    const char *zTab              /* Table name */
        11156  +  ),
        11157  +  int(*xConflict)(
        11158  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        11159  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
        11160  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
        11161  +  ),
        11162  +  void *pCtx,                     /* First argument passed to xConflict */
        11163  +  void **ppRebase, int *pnRebase
 11020  11164   );
 11021  11165   
 11022  11166   /* 
 11023  11167   ** CAPI3REF: Constants Passed To The Conflict Handler
 11024  11168   **
 11025  11169   ** Values that may be passed as the second argument to a conflict-handler.
 11026  11170   **
................................................................................
 11111  11255   **   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
 11112  11256   ** </dl>
 11113  11257   */
 11114  11258   #define SQLITE_CHANGESET_OMIT       0
 11115  11259   #define SQLITE_CHANGESET_REPLACE    1
 11116  11260   #define SQLITE_CHANGESET_ABORT      2
 11117  11261   
        11262  +/* 
        11263  +** CAPI3REF: Rebasing changesets
        11264  +** EXPERIMENTAL
        11265  +**
        11266  +** Suppose there is a site hosting a database in state S0. And that
        11267  +** modifications are made that move that database to state S1 and a
        11268  +** changeset recorded (the "local" changeset). Then, a changeset based
        11269  +** on S0 is received from another site (the "remote" changeset) and 
        11270  +** applied to the database. The database is then in state 
        11271  +** (S1+"remote"), where the exact state depends on any conflict
        11272  +** resolution decisions (OMIT or REPLACE) made while applying "remote".
        11273  +** Rebasing a changeset is to update it to take those conflict 
        11274  +** resolution decisions into account, so that the same conflicts
        11275  +** do not have to be resolved elsewhere in the network. 
        11276  +**
        11277  +** For example, if both the local and remote changesets contain an
        11278  +** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
        11279  +**
        11280  +**   local:  INSERT INTO t1 VALUES(1, 'v1');
        11281  +**   remote: INSERT INTO t1 VALUES(1, 'v2');
        11282  +**
        11283  +** and the conflict resolution is REPLACE, then the INSERT change is
        11284  +** removed from the local changeset (it was overridden). Or, if the
        11285  +** conflict resolution was "OMIT", then the local changeset is modified
        11286  +** to instead contain:
        11287  +**
        11288  +**           UPDATE t1 SET b = 'v2' WHERE a=1;
        11289  +**
        11290  +** Changes within the local changeset are rebased as follows:
        11291  +**
        11292  +** <dl>
        11293  +** <dt>Local INSERT<dd>
        11294  +**   This may only conflict with a remote INSERT. If the conflict 
        11295  +**   resolution was OMIT, then add an UPDATE change to the rebased
        11296  +**   changeset. Or, if the conflict resolution was REPLACE, add
        11297  +**   nothing to the rebased changeset.
        11298  +**
        11299  +** <dt>Local DELETE<dd>
        11300  +**   This may conflict with a remote UPDATE or DELETE. In both cases the
        11301  +**   only possible resolution is OMIT. If the remote operation was a
        11302  +**   DELETE, then add no change to the rebased changeset. If the remote
        11303  +**   operation was an UPDATE, then the old.* fields of change are updated
        11304  +**   to reflect the new.* values in the UPDATE.
        11305  +**
        11306  +** <dt>Local UPDATE<dd>
        11307  +**   This may conflict with a remote UPDATE or DELETE. If it conflicts
        11308  +**   with a DELETE, and the conflict resolution was OMIT, then the update
        11309  +**   is changed into an INSERT. Any undefined values in the new.* record
        11310  +**   from the update change are filled in using the old.* values from
        11311  +**   the conflicting DELETE. Or, if the conflict resolution was REPLACE,
        11312  +**   the UPDATE change is simply omitted from the rebased changeset.
        11313  +**
        11314  +**   If conflict is with a remote UPDATE and the resolution is OMIT, then
        11315  +**   the old.* values are rebased using the new.* values in the remote
        11316  +**   change. Or, if the resolution is REPLACE, then the change is copied
        11317  +**   into the rebased changeset with updates to columns also updated by
        11318  +**   the conflicting remote UPDATE removed. If this means no columns would 
        11319  +**   be updated, the change is omitted.
        11320  +** </dl>
        11321  +**
        11322  +** A local change may be rebased against multiple remote changes 
        11323  +** simultaneously. If a single key is modified by multiple remote 
        11324  +** changesets, they are combined as follows before the local changeset
        11325  +** is rebased:
        11326  +**
        11327  +** <ul>
        11328  +**    <li> If there has been one or more REPLACE resolutions on a
        11329  +**         key, it is rebased according to a REPLACE.
        11330  +**
        11331  +**    <li> If there have been no REPLACE resolutions on a key, then
        11332  +**         the local changeset is rebased according to the most recent
        11333  +**         of the OMIT resolutions.
        11334  +** </ul>
        11335  +**
        11336  +** Note that conflict resolutions from multiple remote changesets are 
        11337  +** combined on a per-field basis, not per-row. This means that in the 
        11338  +** case of multiple remote UPDATE operations, some fields of a single 
        11339  +** local change may be rebased for REPLACE while others are rebased for 
        11340  +** OMIT.
        11341  +**
        11342  +** In order to rebase a local changeset, the remote changeset must first
        11343  +** be applied to the local database using sqlite3changeset_apply_v2() and
        11344  +** the buffer of rebase information captured. Then:
        11345  +**
        11346  +** <ol>
        11347  +**   <li> An sqlite3_rebaser object is created by calling 
        11348  +**        sqlite3rebaser_create().
        11349  +**   <li> The new object is configured with the rebase buffer obtained from
        11350  +**        sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
        11351  +**        If the local changeset is to be rebased against multiple remote
        11352  +**        changesets, then sqlite3rebaser_configure() should be called
        11353  +**        multiple times, in the same order that the multiple
        11354  +**        sqlite3changeset_apply_v2() calls were made.
        11355  +**   <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
        11356  +**   <li> The sqlite3_rebaser object is deleted by calling
        11357  +**        sqlite3rebaser_delete().
        11358  +** </ol>
        11359  +*/
        11360  +typedef struct sqlite3_rebaser sqlite3_rebaser;
        11361  +
        11362  +/*
        11363  +** CAPI3REF: Create a changeset rebaser object.
        11364  +** EXPERIMENTAL
        11365  +**
        11366  +** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
        11367  +** point to the new object and return SQLITE_OK. Otherwise, if an error
        11368  +** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew) 
        11369  +** to NULL. 
        11370  +*/
        11371  +SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
        11372  +
        11373  +/*
        11374  +** CAPI3REF: Configure a changeset rebaser object.
        11375  +** EXPERIMENTAL
        11376  +**
        11377  +** Configure the changeset rebaser object to rebase changesets according
        11378  +** to the conflict resolutions described by buffer pRebase (size nRebase
        11379  +** bytes), which must have been obtained from a previous call to
        11380  +** sqlite3changeset_apply_v2().
        11381  +*/
        11382  +SQLITE_API int sqlite3rebaser_configure(
        11383  +  sqlite3_rebaser*, 
        11384  +  int nRebase, const void *pRebase
        11385  +); 
        11386  +
        11387  +/*
        11388  +** CAPI3REF: Rebase a changeset
        11389  +** EXPERIMENTAL
        11390  +**
        11391  +** Argument pIn must point to a buffer containing a changeset nIn bytes
        11392  +** in size. This function allocates and populates a buffer with a copy
        11393  +** of the changeset rebased rebased according to the configuration of the
        11394  +** rebaser object passed as the first argument. If successful, (*ppOut)
        11395  +** is set to point to the new buffer containing the rebased changset and 
        11396  +** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
        11397  +** responsibility of the caller to eventually free the new buffer using
        11398  +** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
        11399  +** are set to zero and an SQLite error code returned.
        11400  +*/
        11401  +SQLITE_API int sqlite3rebaser_rebase(
        11402  +  sqlite3_rebaser*,
        11403  +  int nIn, const void *pIn, 
        11404  +  int *pnOut, void **ppOut 
        11405  +);
        11406  +
        11407  +/*
        11408  +** CAPI3REF: Delete a changeset rebaser object.
        11409  +** EXPERIMENTAL
        11410  +**
        11411  +** Delete the changeset rebaser object and all associated resources. There
        11412  +** should be one call to this function for each successful invocation
        11413  +** of sqlite3rebaser_create().
        11414  +*/
        11415  +SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p); 
        11416  +
 11118  11417   /*
 11119  11418   ** CAPI3REF: Streaming Versions of API functions.
 11120  11419   **
 11121  11420   ** The six streaming API xxx_strm() functions serve similar purposes to the 
 11122  11421   ** corresponding non-streaming API functions:
 11123  11422   **
 11124  11423   ** <table border=1 style="margin-left:8ex;margin-right:8ex">
................................................................................
 11214  11513     ),
 11215  11514     int(*xConflict)(
 11216  11515       void *pCtx,                   /* Copy of sixth arg to _apply() */
 11217  11516       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11218  11517       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11219  11518     ),
 11220  11519     void *pCtx                      /* First argument passed to xConflict */
        11520  +);
        11521  +SQLITE_API int sqlite3changeset_apply_v2_strm(
        11522  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
        11523  +  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
        11524  +  void *pIn,                                          /* First arg for xInput */
        11525  +  int(*xFilter)(
        11526  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        11527  +    const char *zTab              /* Table name */
        11528  +  ),
        11529  +  int(*xConflict)(
        11530  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        11531  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
        11532  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
        11533  +  ),
        11534  +  void *pCtx,                     /* First argument passed to xConflict */
        11535  +  void **ppRebase, int *pnRebase
 11221  11536   );
 11222  11537   SQLITE_API int sqlite3changeset_concat_strm(
 11223  11538     int (*xInputA)(void *pIn, void *pData, int *pnData),
 11224  11539     void *pInA,
 11225  11540     int (*xInputB)(void *pIn, void *pData, int *pnData),
 11226  11541     void *pInB,
 11227  11542     int (*xOutput)(void *pOut, const void *pData, int nData),
................................................................................
 11251  11566   SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
 11252  11567       int (*xInput)(void *pIn, void *pData, int *pnData),
 11253  11568       void *pIn
 11254  11569   );
 11255  11570   SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
 11256  11571       int (*xOutput)(void *pOut, const void *pData, int nData), 
 11257  11572       void *pOut
        11573  +);
        11574  +SQLITE_API int sqlite3rebaser_rebase_strm(
        11575  +  sqlite3_rebaser *pRebaser,
        11576  +  int (*xInput)(void *pIn, void *pData, int *pnData),
        11577  +  void *pIn,
        11578  +  int (*xOutput)(void *pOut, const void *pData, int nData),
        11579  +  void *pOut
 11258  11580   );
 11259  11581   
 11260  11582   
 11261  11583   /*
 11262  11584   ** Make sure we can call this stuff from C++.
 11263  11585   */
 11264  11586   #if 0
................................................................................
 12669  12991   #define TK_CASE                           136
 12670  12992   #define TK_WHEN                           137
 12671  12993   #define TK_THEN                           138
 12672  12994   #define TK_ELSE                           139
 12673  12995   #define TK_INDEX                          140
 12674  12996   #define TK_ALTER                          141
 12675  12997   #define TK_ADD                            142
 12676         -#define TK_ISNOT                          143
 12677         -#define TK_FUNCTION                       144
 12678         -#define TK_COLUMN                         145
 12679         -#define TK_AGG_FUNCTION                   146
 12680         -#define TK_AGG_COLUMN                     147
 12681         -#define TK_UMINUS                         148
 12682         -#define TK_UPLUS                          149
 12683         -#define TK_REGISTER                       150
 12684         -#define TK_VECTOR                         151
 12685         -#define TK_SELECT_COLUMN                  152
 12686         -#define TK_IF_NULL_ROW                    153
 12687         -#define TK_ASTERISK                       154
 12688         -#define TK_SPAN                           155
 12689         -#define TK_END_OF_FILE                    156
 12690         -#define TK_UNCLOSED_STRING                157
 12691         -#define TK_SPACE                          158
 12692         -#define TK_ILLEGAL                        159
        12998  +#define TK_TRUEFALSE                      143
        12999  +#define TK_ISNOT                          144
        13000  +#define TK_FUNCTION                       145
        13001  +#define TK_COLUMN                         146
        13002  +#define TK_AGG_FUNCTION                   147
        13003  +#define TK_AGG_COLUMN                     148
        13004  +#define TK_UMINUS                         149
        13005  +#define TK_UPLUS                          150
        13006  +#define TK_TRUTH                          151
        13007  +#define TK_REGISTER                       152
        13008  +#define TK_VECTOR                         153
        13009  +#define TK_SELECT_COLUMN                  154
        13010  +#define TK_IF_NULL_ROW                    155
        13011  +#define TK_ASTERISK                       156
        13012  +#define TK_SPAN                           157
        13013  +#define TK_END_OF_FILE                    158
        13014  +#define TK_UNCLOSED_STRING                159
        13015  +#define TK_SPACE                          160
        13016  +#define TK_ILLEGAL                        161
 12693  13017   
 12694  13018   /* The token codes above must all fit in 8 bits */
 12695  13019   #define TKFLG_MASK           0xff  
 12696  13020   
 12697  13021   /* Flags that can be added to a token code when it is not
 12698  13022   ** being stored in a u8: */
 12699  13023   #define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
................................................................................
 13129  13453   ** The sqlite.busyHandler member of the sqlite struct contains the busy
 13130  13454   ** callback for the database handle. Each pager opened via the sqlite
 13131  13455   ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
 13132  13456   ** callback is currently invoked only from within pager.c.
 13133  13457   */
 13134  13458   typedef struct BusyHandler BusyHandler;
 13135  13459   struct BusyHandler {
 13136         -  int (*xFunc)(void *,int);  /* The busy callback */
 13137         -  void *pArg;                /* First arg to busy callback */
 13138         -  int nBusy;                 /* Incremented with each busy call */
        13460  +  int (*xBusyHandler)(void *,int);  /* The busy callback */
        13461  +  void *pBusyArg;                   /* First arg to busy callback */
        13462  +  int nBusy;                        /* Incremented with each busy call */
        13463  +  u8 bExtraFileArg;                 /* Include sqlite3_file as callback arg */
 13139  13464   };
 13140  13465   
 13141  13466   /*
 13142  13467   ** Name of the master database table.  The master database table
 13143  13468   ** is a special table that holds the names and attributes of all
 13144  13469   ** user tables and indices.
 13145  13470   */
................................................................................
 13931  14256   #define OP_Subtract       89 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 13932  14257   #define OP_Multiply       90 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 13933  14258   #define OP_Divide         91 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 13934  14259   #define OP_Remainder      92 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 13935  14260   #define OP_Concat         93 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 13936  14261   #define OP_Compare        94 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 13937  14262   #define OP_BitNot         95 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
 13938         -#define OP_Offset         96 /* synopsis: r[P3] = sqlite_offset(P1)        */
        14263  +#define OP_IsTrue         96 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
 13939  14264   #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 13940         -#define OP_Column         98 /* synopsis: r[P3]=PX                         */
 13941         -#define OP_Affinity       99 /* synopsis: affinity(r[P1@P2])               */
 13942         -#define OP_MakeRecord    100 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 13943         -#define OP_Count         101 /* synopsis: r[P2]=count()                    */
 13944         -#define OP_ReadCookie    102
 13945         -#define OP_SetCookie     103
 13946         -#define OP_ReopenIdx     104 /* synopsis: root=P2 iDb=P3                   */
 13947         -#define OP_OpenRead      105 /* synopsis: root=P2 iDb=P3                   */
 13948         -#define OP_OpenWrite     106 /* synopsis: root=P2 iDb=P3                   */
 13949         -#define OP_OpenDup       107
 13950         -#define OP_OpenAutoindex 108 /* synopsis: nColumn=P2                       */
 13951         -#define OP_OpenEphemeral 109 /* synopsis: nColumn=P2                       */
 13952         -#define OP_SorterOpen    110
 13953         -#define OP_SequenceTest  111 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 13954         -#define OP_OpenPseudo    112 /* synopsis: P3 columns in r[P2]              */
 13955         -#define OP_Close         113
 13956         -#define OP_ColumnsUsed   114
 13957         -#define OP_Sequence      115 /* synopsis: r[P2]=cursor[P1].ctr++           */
 13958         -#define OP_NewRowid      116 /* synopsis: r[P2]=rowid                      */
 13959         -#define OP_Insert        117 /* synopsis: intkey=r[P3] data=r[P2]          */
 13960         -#define OP_InsertInt     118 /* synopsis: intkey=P3 data=r[P2]             */
 13961         -#define OP_Delete        119
 13962         -#define OP_ResetCount    120
 13963         -#define OP_SorterCompare 121 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 13964         -#define OP_SorterData    122 /* synopsis: r[P2]=data                       */
 13965         -#define OP_RowData       123 /* synopsis: r[P2]=data                       */
 13966         -#define OP_Rowid         124 /* synopsis: r[P2]=rowid                      */
 13967         -#define OP_NullRow       125
 13968         -#define OP_SeekEnd       126
 13969         -#define OP_SorterInsert  127 /* synopsis: key=r[P2]                        */
 13970         -#define OP_IdxInsert     128 /* synopsis: key=r[P2]                        */
 13971         -#define OP_IdxDelete     129 /* synopsis: key=r[P2@P3]                     */
 13972         -#define OP_DeferredSeek  130 /* synopsis: Move P3 to P1.rowid if needed    */
 13973         -#define OP_IdxRowid      131 /* synopsis: r[P2]=rowid                      */
        14265  +#define OP_Offset         98 /* synopsis: r[P3] = sqlite_offset(P1)        */
        14266  +#define OP_Column         99 /* synopsis: r[P3]=PX                         */
        14267  +#define OP_Affinity      100 /* synopsis: affinity(r[P1@P2])               */
        14268  +#define OP_MakeRecord    101 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
        14269  +#define OP_Count         102 /* synopsis: r[P2]=count()                    */
        14270  +#define OP_ReadCookie    103
        14271  +#define OP_SetCookie     104
        14272  +#define OP_ReopenIdx     105 /* synopsis: root=P2 iDb=P3                   */
        14273  +#define OP_OpenRead      106 /* synopsis: root=P2 iDb=P3                   */
        14274  +#define OP_OpenWrite     107 /* synopsis: root=P2 iDb=P3                   */
        14275  +#define OP_OpenDup       108
        14276  +#define OP_OpenAutoindex 109 /* synopsis: nColumn=P2                       */
        14277  +#define OP_OpenEphemeral 110 /* synopsis: nColumn=P2                       */
        14278  +#define OP_SorterOpen    111
        14279  +#define OP_SequenceTest  112 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
        14280  +#define OP_OpenPseudo    113 /* synopsis: P3 columns in r[P2]              */
        14281  +#define OP_Close         114
        14282  +#define OP_ColumnsUsed   115
        14283  +#define OP_Sequence      116 /* synopsis: r[P2]=cursor[P1].ctr++           */
        14284  +#define OP_NewRowid      117 /* synopsis: r[P2]=rowid                      */
        14285  +#define OP_Insert        118 /* synopsis: intkey=r[P3] data=r[P2]          */
        14286  +#define OP_InsertInt     119 /* synopsis: intkey=P3 data=r[P2]             */
        14287  +#define OP_Delete        120
        14288  +#define OP_ResetCount    121
        14289  +#define OP_SorterCompare 122 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
        14290  +#define OP_SorterData    123 /* synopsis: r[P2]=data                       */
        14291  +#define OP_RowData       124 /* synopsis: r[P2]=data                       */
        14292  +#define OP_Rowid         125 /* synopsis: r[P2]=rowid                      */
        14293  +#define OP_NullRow       126
        14294  +#define OP_SeekEnd       127
        14295  +#define OP_SorterInsert  128 /* synopsis: key=r[P2]                        */
        14296  +#define OP_IdxInsert     129 /* synopsis: key=r[P2]                        */
        14297  +#define OP_IdxDelete     130 /* synopsis: key=r[P2@P3]                     */
        14298  +#define OP_DeferredSeek  131 /* synopsis: Move P3 to P1.rowid if needed    */
 13974  14299   #define OP_Real          132 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 13975         -#define OP_Destroy       133
 13976         -#define OP_Clear         134
 13977         -#define OP_ResetSorter   135
 13978         -#define OP_CreateBtree   136 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 13979         -#define OP_SqlExec       137
 13980         -#define OP_ParseSchema   138
 13981         -#define OP_LoadAnalysis  139
 13982         -#define OP_DropTable     140
 13983         -#define OP_DropIndex     141
 13984         -#define OP_DropTrigger   142
 13985         -#define OP_IntegrityCk   143
 13986         -#define OP_RowSetAdd     144 /* synopsis: rowset(P1)=r[P2]                 */
 13987         -#define OP_Param         145
 13988         -#define OP_FkCounter     146 /* synopsis: fkctr[P1]+=P2                    */
 13989         -#define OP_MemMax        147 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 13990         -#define OP_OffsetLimit   148 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 13991         -#define OP_AggStep0      149 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 13992         -#define OP_AggStep       150 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 13993         -#define OP_AggFinal      151 /* synopsis: accum=r[P1] N=P2                 */
 13994         -#define OP_Expire        152
 13995         -#define OP_TableLock     153 /* synopsis: iDb=P1 root=P2 write=P3          */
 13996         -#define OP_VBegin        154
 13997         -#define OP_VCreate       155
 13998         -#define OP_VDestroy      156
 13999         -#define OP_VOpen         157
 14000         -#define OP_VColumn       158 /* synopsis: r[P3]=vcolumn(P2)                */
 14001         -#define OP_VRename       159
 14002         -#define OP_Pagecount     160
 14003         -#define OP_MaxPgcnt      161
 14004         -#define OP_PureFunc0     162
 14005         -#define OP_Function0     163 /* synopsis: r[P3]=func(r[P2@P5])             */
 14006         -#define OP_PureFunc      164
 14007         -#define OP_Function      165 /* synopsis: r[P3]=func(r[P2@P5])             */
 14008         -#define OP_Trace         166
 14009         -#define OP_CursorHint    167
 14010         -#define OP_Noop          168
 14011         -#define OP_Explain       169
        14300  +#define OP_IdxRowid      133 /* synopsis: r[P2]=rowid                      */
        14301  +#define OP_Destroy       134
        14302  +#define OP_Clear         135
        14303  +#define OP_ResetSorter   136
        14304  +#define OP_CreateBtree   137 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
        14305  +#define OP_SqlExec       138
        14306  +#define OP_ParseSchema   139
        14307  +#define OP_LoadAnalysis  140
        14308  +#define OP_DropTable     141
        14309  +#define OP_DropIndex     142
        14310  +#define OP_DropTrigger   143
        14311  +#define OP_IntegrityCk   144
        14312  +#define OP_RowSetAdd     145 /* synopsis: rowset(P1)=r[P2]                 */
        14313  +#define OP_Param         146
        14314  +#define OP_FkCounter     147 /* synopsis: fkctr[P1]+=P2                    */
        14315  +#define OP_MemMax        148 /* synopsis: r[P1]=max(r[P1],r[P2])           */
        14316  +#define OP_OffsetLimit   149 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
        14317  +#define OP_AggStep0      150 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        14318  +#define OP_AggStep       151 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        14319  +#define OP_AggFinal      152 /* synopsis: accum=r[P1] N=P2                 */
        14320  +#define OP_Expire        153
        14321  +#define OP_TableLock     154 /* synopsis: iDb=P1 root=P2 write=P3          */
        14322  +#define OP_VBegin        155
        14323  +#define OP_VCreate       156
        14324  +#define OP_VDestroy      157
        14325  +#define OP_VOpen         158
        14326  +#define OP_VColumn       159 /* synopsis: r[P3]=vcolumn(P2)                */
        14327  +#define OP_VRename       160
        14328  +#define OP_Pagecount     161
        14329  +#define OP_MaxPgcnt      162
        14330  +#define OP_PureFunc0     163
        14331  +#define OP_Function0     164 /* synopsis: r[P3]=func(r[P2@P5])             */
        14332  +#define OP_PureFunc      165
        14333  +#define OP_Function      166 /* synopsis: r[P3]=func(r[P2@P5])             */
        14334  +#define OP_Trace         167
        14335  +#define OP_CursorHint    168
        14336  +#define OP_Noop          169
        14337  +#define OP_Explain       170
 14012  14338   
 14013  14339   /* Properties such as "out2" or "jump" that are specified in
 14014  14340   ** comments following the "case" for each opcode in the vdbe.c
 14015  14341   ** are encoded into bitvectors as follows:
 14016  14342   */
 14017  14343   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 14018  14344   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 14029  14355   /*  40 */ 0x01, 0x01, 0x23, 0x26, 0x26, 0x0b, 0x01, 0x01,\
 14030  14356   /*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 14031  14357   /*  56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x02,\
 14032  14358   /*  64 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
 14033  14359   /*  72 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 14034  14360   /*  80 */ 0x02, 0x02, 0x02, 0x00, 0x26, 0x26, 0x26, 0x26,\
 14035  14361   /*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 14036         -/*  96 */ 0x20, 0x10, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
        14362  +/*  96 */ 0x12, 0x10, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10,\
 14037  14363   /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14038         -/* 112 */ 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\
 14039         -/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x04,\
 14040         -/* 128 */ 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\
 14041         -/* 136 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14042         -/* 144 */ 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00,\
        14364  +/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
        14365  +/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,\
        14366  +/* 128 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
        14367  +/* 136 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
        14368  +/* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\
 14043  14369   /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14044         -/* 160 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14045         -/* 168 */ 0x00, 0x00,}
        14370  +/* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
        14371  +/* 168 */ 0x00, 0x00, 0x00,}
 14046  14372   
 14047  14373   /* The sqlite3P2Values() routine is able to run faster if it knows
 14048  14374   ** the value of the largest JUMP opcode.  The smaller the maximum
 14049  14375   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 14050  14376   ** generated this include file strives to group all JUMP opcodes
 14051  14377   ** together near the beginning of the list.
 14052  14378   */
................................................................................
 14339  14665     int,
 14340  14666     void(*)(DbPage*)
 14341  14667   );
 14342  14668   SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
 14343  14669   SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
 14344  14670   
 14345  14671   /* Functions used to configure a Pager object. */
 14346         -SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
        14672  +SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
 14347  14673   SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
 14348  14674   #ifdef SQLITE_HAS_CODEC
 14349  14675   SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*);
 14350  14676   #endif
 14351  14677   SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
 14352  14678   SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
 14353  14679   SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
................................................................................
 14425  14751   SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
 14426  14752   SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
 14427  14753   SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
 14428  14754   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
 14429  14755   SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
 14430  14756   SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
 14431  14757   SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
        14758  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        14759  +SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager);
        14760  +#else
        14761  +# define sqlite3PagerResetLockTimeout(X)
        14762  +#endif
 14432  14763   
 14433  14764   /* Functions used to truncate the database file. */
 14434  14765   SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
 14435  14766   
 14436  14767   SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
 14437  14768   
 14438  14769   #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
................................................................................
 15244  15575     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
 15245  15576     int aLimit[SQLITE_N_LIMIT];   /* Limits */
 15246  15577     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
 15247  15578     struct sqlite3InitInfo {      /* Information used during initialization */
 15248  15579       int newTnum;                /* Rootpage of table being initialized */
 15249  15580       u8 iDb;                     /* Which db file is being initialized */
 15250  15581       u8 busy;                    /* TRUE if currently initializing */
 15251         -    u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
 15252         -    u8 imposterTable;           /* Building an imposter table */
        15582  +    unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
        15583  +    unsigned imposterTable : 1; /* Building an imposter table */
        15584  +    unsigned reopenMemdb : 1;   /* ATTACH is really a reopen using MemDB */
 15253  15585     } init;
 15254  15586     int nVdbeActive;              /* Number of VDBEs currently running */
 15255  15587     int nVdbeRead;                /* Number of active VDBEs that read or write */
 15256  15588     int nVdbeWrite;               /* Number of active VDBEs that read and write */
 15257  15589     int nVdbeExec;                /* Number of nested calls to VdbeExec() */
 15258  15590     int nVDestroy;                /* Number of active OP_VDestroy operations */
 15259  15591     int nExtension;               /* Number of loaded extensions */
................................................................................
 15410  15742   #define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
 15411  15743   #define SQLITE_Transitive     0x0080   /* Transitive constraints */
 15412  15744   #define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
 15413  15745   #define SQLITE_CountOfView    0x0200   /* The count-of-view optimization */
 15414  15746   #define SQLITE_CursorHints    0x0400   /* Add OP_CursorHint opcodes */
 15415  15747   #define SQLITE_Stat34         0x0800   /* Use STAT3 or STAT4 data */
 15416  15748      /* TH3 expects the Stat34  ^^^^^^ value to be 0x0800.  Don't change it */
        15749  +#define SQLITE_PushDown       0x1000   /* The push-down optimization */
        15750  +#define SQLITE_SimplifyJoin   0x2000   /* Convert LEFT JOIN to JOIN */
 15417  15751   #define SQLITE_AllOpts        0xffff   /* All optimizations */
 15418  15752   
 15419  15753   /*
 15420  15754   ** Macros for testing whether or not optimizations are enabled or disabled.
 15421  15755   */
 15422  15756   #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
 15423  15757   #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
................................................................................
 15633  15967   };
 15634  15968   
 15635  15969   /* Allowed values for Column.colFlags:
 15636  15970   */
 15637  15971   #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
 15638  15972   #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
 15639  15973   #define COLFLAG_HASTYPE  0x0004    /* Type name follows column name */
        15974  +#define COLFLAG_UNIQUE   0x0008    /* Column def contains "UNIQUE" or "PK" */
 15640  15975   
 15641  15976   /*
 15642  15977   ** A "Collating Sequence" is defined by an instance of the following
 15643  15978   ** structure. Conceptually, a collating sequence consists of a name and
 15644  15979   ** a comparison routine that defines the order of that sequence.
 15645  15980   **
 15646  15981   ** If CollSeq.xCmp is NULL, it means that the
................................................................................
 16870  17205     yDbMask writeMask;   /* Start a write transaction on these databases */
 16871  17206     yDbMask cookieMask;  /* Bitmask of schema verified databases */
 16872  17207     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
 16873  17208     int regRoot;         /* Register holding root page number for new objects */
 16874  17209     int nMaxArg;         /* Max args passed to user function by sub-program */
 16875  17210   #if SELECTTRACE_ENABLED
 16876  17211     int nSelect;         /* Number of SELECT statements seen */
 16877         -  int nSelectIndent;   /* How far to indent SELECTTRACE() output */
 16878  17212   #endif
 16879  17213   #ifndef SQLITE_OMIT_SHARED_CACHE
 16880  17214     int nTableLock;        /* Number of locks in aTableLock */
 16881  17215     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 16882  17216   #endif
 16883  17217     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 16884  17218     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
................................................................................
 17234  17568       int n;                                    /* A counter */
 17235  17569       int iCur;                                 /* A cursor number */
 17236  17570       SrcList *pSrcList;                        /* FROM clause */
 17237  17571       struct SrcCount *pSrcCount;               /* Counting column references */
 17238  17572       struct CCurHint *pCCurHint;               /* Used by codeCursorHint() */
 17239  17573       int *aiCol;                               /* array of column indexes */
 17240  17574       struct IdxCover *pIdxCover;               /* Check for index coverage */
 17241         -    struct IdxExprTrans *pIdxTrans;           /* Convert indexed expr to column */
        17575  +    struct IdxExprTrans *pIdxTrans;           /* Convert idxed expr to column */
 17242  17576       ExprList *pGroupBy;                       /* GROUP BY clause */
 17243         -    struct HavingToWhereCtx *pHavingCtx;      /* HAVING to WHERE clause ctx */
        17577  +    Select *pSelect;                          /* HAVING to WHERE clause ctx */
 17244  17578     } u;
 17245  17579   };
 17246  17580   
 17247  17581   /* Forward declarations */
 17248  17582   SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
 17249  17583   SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
 17250  17584   SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
................................................................................
 17700  18034   SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*);
 17701  18035   SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int);
 17702  18036   SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
 17703  18037   SQLITE_PRIVATE int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
 17704  18038   SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*, Expr*, int);
 17705  18039   SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
 17706  18040   SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
        18041  +SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int);
 17707  18042   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 17708  18043   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 17709  18044   SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
 17710  18045   SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
 17711  18046   SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
 17712  18047   #ifndef SQLITE_UNTESTABLE
 17713  18048   SQLITE_PRIVATE void sqlite3PrngSaveState(void);
................................................................................
 17717  18052   SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
 17718  18053   SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
 17719  18054   SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
 17720  18055   SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
 17721  18056   SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
 17722  18057   SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
 17723  18058   SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
        18059  +SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
        18060  +SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
 17724  18061   SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
 17725  18062   SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
 17726  18063   SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
 17727  18064   SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
 17728  18065   SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
 17729  18066   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 17730  18067   SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
................................................................................
 17898  18235   SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
 17899  18236   SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
 17900  18237   SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
 17901  18238   
 17902  18239   #if defined(SQLITE_NEED_ERR_NAME)
 17903  18240   SQLITE_PRIVATE const char *sqlite3ErrName(int);
 17904  18241   #endif
        18242  +
        18243  +#ifdef SQLITE_ENABLE_DESERIALIZE
        18244  +SQLITE_PRIVATE int sqlite3MemdbInit(void);
        18245  +#endif
 17905  18246   
 17906  18247   SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 17907  18248   SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 17908  18249   SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
 17909  18250   SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
 17910  18251   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 17911  18252   SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
................................................................................
 17947  18288   SQLITE_PRIVATE const Token sqlite3IntTokens[];
 17948  18289   SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
 17949  18290   SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
 17950  18291   #ifndef SQLITE_OMIT_WSD
 17951  18292   SQLITE_PRIVATE int sqlite3PendingByte;
 17952  18293   #endif
 17953  18294   #endif
        18295  +#ifdef VDBE_PROFILE
        18296  +SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
        18297  +#endif
 17954  18298   SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 17955  18299   SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 17956  18300   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 17957  18301   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 17958  18302   SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 17959  18303   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 17960  18304   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
................................................................................
 17969  18313   SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
 17970  18314   SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
 17971  18315   SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
 17972  18316   SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
 17973  18317   SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
 17974  18318   SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
 17975  18319   SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
 17976         -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
        18320  +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
 17977  18321   SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
 17978  18322   SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
 17979  18323   SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
 17980  18324   SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
 17981  18325   SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
 17982  18326   SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
 17983  18327   SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
................................................................................
 18570  18914   ** Constant tokens for values 0 and 1.
 18571  18915   */
 18572  18916   SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
 18573  18917      { "0", 1 },
 18574  18918      { "1", 1 }
 18575  18919   };
 18576  18920   
        18921  +#ifdef VDBE_PROFILE
        18922  +/*
        18923  +** The following performance counter can be used in place of
        18924  +** sqlite3Hwtime() for profiling.  This is a no-op on standard builds.
        18925  +*/
        18926  +SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
        18927  +#endif
 18577  18928   
 18578  18929   /*
 18579  18930   ** The value of the "pending" byte must be 0x40000000 (1 byte past the
 18580  18931   ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
 18581  18932   ** the database page that contains the pending byte.  It never attempts
 18582  18933   ** to read or write that page.  The pending byte page is set aside
 18583  18934   ** for use by the VFS layers as space for managing file locks.
................................................................................
 18853  19204   
 18854  19205   /* One or more of the following flags are set to indicate the validOK
 18855  19206   ** representations of the value stored in the Mem struct.
 18856  19207   **
 18857  19208   ** If the MEM_Null flag is set, then the value is an SQL NULL value.
 18858  19209   ** For a pointer type created using sqlite3_bind_pointer() or
 18859  19210   ** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
 18860         -** If both MEM_Null and MEM_Zero are set, that means that the value is
 18861         -** an unchanging column value from VColumn.
 18862  19211   **
 18863  19212   ** If the MEM_Str flag is set then Mem.z points at a string representation.
 18864  19213   ** Usually this is encoded in the same unicode encoding as the main
 18865  19214   ** database (see below for exceptions). If the MEM_Term flag is also
 18866  19215   ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
 18867  19216   ** flags may coexist with the MEM_Str flag.
 18868  19217   */
................................................................................
 18948  19297     Mem *pOut;              /* The return value is stored here */
 18949  19298     FuncDef *pFunc;         /* Pointer to function information */
 18950  19299     Mem *pMem;              /* Memory cell used to store aggregate context */
 18951  19300     Vdbe *pVdbe;            /* The VM that owns this context */
 18952  19301     int iOp;                /* Instruction number of OP_Function */
 18953  19302     int isError;            /* Error code returned by the function. */
 18954  19303     u8 skipFlag;            /* Skip accumulator loading if true */
 18955         -  u8 fErrorOrAux;         /* isError!=0 or pVdbe->pAuxData modified */
 18956  19304     u8 argc;                /* Number of arguments */
 18957  19305     sqlite3_value *argv[1]; /* Argument set */
 18958  19306   };
 18959  19307   
 18960  19308   /* A bitfield type for use inside of structures.  Always follow with :N where
 18961  19309   ** N is the number of bits.
 18962  19310   */
................................................................................
 19118  19466   SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
 19119  19467   SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
 19120  19468   SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
 19121  19469   SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
 19122  19470   SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
 19123  19471   SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
 19124  19472   SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
        19473  +SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
 19125  19474   SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
 19126  19475   SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
 19127  19476   SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
 19128  19477   SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
 19129  19478   SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
 19130  19479   SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 19131  19480   SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
................................................................................
 19511  19860       }
 19512  19861   
 19513  19862       /*
 19514  19863       ** Set *pCurrent to the total cache hits or misses encountered by all
 19515  19864       ** pagers the database handle is connected to. *pHighwater is always set 
 19516  19865       ** to zero.
 19517  19866       */
        19867  +    case SQLITE_DBSTATUS_CACHE_SPILL:
        19868  +      op = SQLITE_DBSTATUS_CACHE_WRITE+1;
        19869  +      /* Fall through into the next case */
 19518  19870       case SQLITE_DBSTATUS_CACHE_HIT:
 19519  19871       case SQLITE_DBSTATUS_CACHE_MISS:
 19520  19872       case SQLITE_DBSTATUS_CACHE_WRITE:{
 19521  19873         int i;
 19522  19874         int nRet = 0;
 19523  19875         assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
 19524  19876         assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
................................................................................
 20932  21284   ** when simply tossing information over the wall to the VFS and we do not
 20933  21285   ** really care if the VFS receives and understands the information since it
 20934  21286   ** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
 20935  21287   ** routine has no return value since the return value would be meaningless.
 20936  21288   */
 20937  21289   SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
 20938  21290   #ifdef SQLITE_TEST
 20939         -  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
        21291  +  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
        21292  +   && op!=SQLITE_FCNTL_LOCK_TIMEOUT
        21293  +  ){
 20940  21294       /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
 20941  21295       ** is using a regular VFS, it is called after the corresponding
 20942  21296       ** transaction has been committed. Injecting a fault at this point
 20943  21297       ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
 20944  21298       ** but the transaction is committed anyway.
 20945  21299       **
 20946  21300       ** The core must call OsFileControl() though, not OsFileControlHint(),
 20947  21301       ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
 20948  21302       ** means the commit really has failed and an error should be returned
 20949  21303       ** to the user.  */
 20950  21304       DO_OS_MALLOC_TEST(id);
 20951  21305     }
 20952  21306   #endif
        21307  +  if( id->pMethods==0 ) return SQLITE_NOTFOUND;
 20953  21308     return id->pMethods->xFileControl(id, op, pArg);
 20954  21309   }
 20955  21310   SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
 20956         -  (void)id->pMethods->xFileControl(id, op, pArg);
        21311  +  if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
 20957  21312   }
 20958  21313   
 20959  21314   SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
 20960  21315     int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
 20961  21316     return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
 20962  21317   }
 20963  21318   SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
................................................................................
 24104  24459   #if SQLITE_MUTEX_NREF
 24105  24460     volatile int nRef;         /* Number of entrances */
 24106  24461     volatile pthread_t owner;  /* Thread that is within this mutex */
 24107  24462     int trace;                 /* True to trace changes */
 24108  24463   #endif
 24109  24464   };
 24110  24465   #if SQLITE_MUTEX_NREF
 24111         -#define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0}
        24466  +# define SQLITE3_MUTEX_INITIALIZER(id) \
        24467  +     {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
 24112  24468   #elif defined(SQLITE_ENABLE_API_ARMOR)
 24113         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 }
        24469  +# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
 24114  24470   #else
 24115         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
        24471  +#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
 24116  24472   #endif
 24117  24473   
 24118  24474   /*
 24119  24475   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 24120  24476   ** intended for use only inside assert() statements.  On some platforms,
 24121  24477   ** there might be race conditions that can cause these routines to
 24122  24478   ** deliver incorrect results.  In particular, if pthread_equal() is
................................................................................
 24205  24561   ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
 24206  24562   ** returns a different mutex on every call.  But for the static 
 24207  24563   ** mutex types, the same mutex is returned on every call that has
 24208  24564   ** the same type number.
 24209  24565   */
 24210  24566   static sqlite3_mutex *pthreadMutexAlloc(int iType){
 24211  24567     static sqlite3_mutex staticMutexes[] = {
 24212         -    SQLITE3_MUTEX_INITIALIZER,
 24213         -    SQLITE3_MUTEX_INITIALIZER,
 24214         -    SQLITE3_MUTEX_INITIALIZER,
 24215         -    SQLITE3_MUTEX_INITIALIZER,
 24216         -    SQLITE3_MUTEX_INITIALIZER,
 24217         -    SQLITE3_MUTEX_INITIALIZER,
 24218         -    SQLITE3_MUTEX_INITIALIZER,
 24219         -    SQLITE3_MUTEX_INITIALIZER,
 24220         -    SQLITE3_MUTEX_INITIALIZER,
 24221         -    SQLITE3_MUTEX_INITIALIZER,
 24222         -    SQLITE3_MUTEX_INITIALIZER,
 24223         -    SQLITE3_MUTEX_INITIALIZER
        24568  +    SQLITE3_MUTEX_INITIALIZER(2),
        24569  +    SQLITE3_MUTEX_INITIALIZER(3),
        24570  +    SQLITE3_MUTEX_INITIALIZER(4),
        24571  +    SQLITE3_MUTEX_INITIALIZER(5),
        24572  +    SQLITE3_MUTEX_INITIALIZER(6),
        24573  +    SQLITE3_MUTEX_INITIALIZER(7),
        24574  +    SQLITE3_MUTEX_INITIALIZER(8),
        24575  +    SQLITE3_MUTEX_INITIALIZER(9),
        24576  +    SQLITE3_MUTEX_INITIALIZER(10),
        24577  +    SQLITE3_MUTEX_INITIALIZER(11),
        24578  +    SQLITE3_MUTEX_INITIALIZER(12),
        24579  +    SQLITE3_MUTEX_INITIALIZER(13)
 24224  24580     };
 24225  24581     sqlite3_mutex *p;
 24226  24582     switch( iType ){
 24227  24583       case SQLITE_MUTEX_RECURSIVE: {
 24228  24584         p = sqlite3MallocZero( sizeof(*p) );
 24229  24585         if( p ){
 24230  24586   #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
................................................................................
 24235  24591           /* Use a recursive mutex if it is available */
 24236  24592           pthread_mutexattr_t recursiveAttr;
 24237  24593           pthread_mutexattr_init(&recursiveAttr);
 24238  24594           pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
 24239  24595           pthread_mutex_init(&p->mutex, &recursiveAttr);
 24240  24596           pthread_mutexattr_destroy(&recursiveAttr);
 24241  24597   #endif
        24598  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
        24599  +        p->id = SQLITE_MUTEX_RECURSIVE;
        24600  +#endif
 24242  24601         }
 24243  24602         break;
 24244  24603       }
 24245  24604       case SQLITE_MUTEX_FAST: {
 24246  24605         p = sqlite3MallocZero( sizeof(*p) );
 24247  24606         if( p ){
 24248  24607           pthread_mutex_init(&p->mutex, 0);
        24608  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
        24609  +        p->id = SQLITE_MUTEX_FAST;
        24610  +#endif
 24249  24611         }
 24250  24612         break;
 24251  24613       }
 24252  24614       default: {
 24253  24615   #ifdef SQLITE_ENABLE_API_ARMOR
 24254  24616         if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
 24255  24617           (void)SQLITE_MISUSE_BKPT;
................................................................................
 24257  24619         }
 24258  24620   #endif
 24259  24621         p = &staticMutexes[iType-2];
 24260  24622         break;
 24261  24623       }
 24262  24624     }
 24263  24625   #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
 24264         -  if( p ) p->id = iType;
        24626  +  assert( p==0 || p->id==iType );
 24265  24627   #endif
 24266  24628     return p;
 24267  24629   }
 24268  24630   
 24269  24631   
 24270  24632   /*
 24271  24633   ** This routine deallocates a previously
................................................................................
 24774  25136   */
 24775  25137   struct sqlite3_mutex {
 24776  25138     CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
 24777  25139     int id;                    /* Mutex type */
 24778  25140   #ifdef SQLITE_DEBUG
 24779  25141     volatile int nRef;         /* Number of enterances */
 24780  25142     volatile DWORD owner;      /* Thread holding this mutex */
 24781         -  volatile int trace;        /* True to trace changes */
        25143  +  volatile LONG trace;       /* True to trace changes */
 24782  25144   #endif
 24783  25145   };
 24784  25146   
 24785  25147   /*
 24786  25148   ** These are the initializer values used when declaring a "static" mutex
 24787  25149   ** on Win32.  It should be noted that all mutexes require initialization
 24788  25150   ** on the Win32 platform.
 24789  25151   */
 24790  25152   #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
 24791  25153   
 24792  25154   #ifdef SQLITE_DEBUG
 24793         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
        25155  +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
 24794  25156                                       0L, (DWORD)0, 0 }
 24795  25157   #else
 24796         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
        25158  +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
 24797  25159   #endif
 24798  25160   
 24799  25161   #ifdef SQLITE_DEBUG
 24800  25162   /*
 24801  25163   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 24802  25164   ** intended for use only inside assert() statements.
 24803  25165   */
................................................................................
 24832  25194   #endif
 24833  25195   }
 24834  25196   
 24835  25197   /*
 24836  25198   ** Initialize and deinitialize the mutex subsystem.
 24837  25199   */
 24838  25200   static sqlite3_mutex winMutex_staticMutexes[] = {
 24839         -  SQLITE3_MUTEX_INITIALIZER,
 24840         -  SQLITE3_MUTEX_INITIALIZER,
 24841         -  SQLITE3_MUTEX_INITIALIZER,
 24842         -  SQLITE3_MUTEX_INITIALIZER,
 24843         -  SQLITE3_MUTEX_INITIALIZER,
 24844         -  SQLITE3_MUTEX_INITIALIZER,
 24845         -  SQLITE3_MUTEX_INITIALIZER,
 24846         -  SQLITE3_MUTEX_INITIALIZER,
 24847         -  SQLITE3_MUTEX_INITIALIZER,
 24848         -  SQLITE3_MUTEX_INITIALIZER,
 24849         -  SQLITE3_MUTEX_INITIALIZER,
 24850         -  SQLITE3_MUTEX_INITIALIZER
        25201  +  SQLITE3_MUTEX_INITIALIZER(2),
        25202  +  SQLITE3_MUTEX_INITIALIZER(3),
        25203  +  SQLITE3_MUTEX_INITIALIZER(4),
        25204  +  SQLITE3_MUTEX_INITIALIZER(5),
        25205  +  SQLITE3_MUTEX_INITIALIZER(6),
        25206  +  SQLITE3_MUTEX_INITIALIZER(7),
        25207  +  SQLITE3_MUTEX_INITIALIZER(8),
        25208  +  SQLITE3_MUTEX_INITIALIZER(9),
        25209  +  SQLITE3_MUTEX_INITIALIZER(10),
        25210  +  SQLITE3_MUTEX_INITIALIZER(11),
        25211  +  SQLITE3_MUTEX_INITIALIZER(12),
        25212  +  SQLITE3_MUTEX_INITIALIZER(13)
 24851  25213   };
 24852  25214   
 24853  25215   static int winMutex_isInit = 0;
 24854  25216   static int winMutex_isNt = -1; /* <0 means "need to query" */
 24855  25217   
 24856  25218   /* As the winMutexInit() and winMutexEnd() functions are called as part
 24857  25219   ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
................................................................................
 24973  25335   #ifdef SQLITE_ENABLE_API_ARMOR
 24974  25336         if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
 24975  25337           (void)SQLITE_MISUSE_BKPT;
 24976  25338           return 0;
 24977  25339         }
 24978  25340   #endif
 24979  25341         p = &winMutex_staticMutexes[iType-2];
 24980         -      p->id = iType;
 24981  25342   #ifdef SQLITE_DEBUG
 24982  25343   #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
 24983         -      p->trace = 1;
        25344  +      InterlockedCompareExchange(&p->trace, 1, 0);
 24984  25345   #endif
 24985  25346   #endif
 24986  25347         break;
 24987  25348       }
 24988  25349     }
        25350  +  assert( p==0 || p->id==iType );
 24989  25351     return p;
 24990  25352   }
 24991  25353   
 24992  25354   
 24993  25355   /*
 24994  25356   ** This routine deallocates a previously
 24995  25357   ** allocated mutex.  SQLite is careful to deallocate every
................................................................................
 26060  26422     double rounder;            /* Used for rounding floating point values */
 26061  26423     etByte flag_dp;            /* True if decimal point should be shown */
 26062  26424     etByte flag_rtz;           /* True if trailing zeros should be removed */
 26063  26425   #endif
 26064  26426     PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
 26065  26427     char buf[etBUFSIZE];       /* Conversion buffer */
 26066  26428   
        26429  +  /* pAccum never starts out with an empty buffer that was obtained from 
        26430  +  ** malloc().  This precondition is required by the mprintf("%z...")
        26431  +  ** optimization. */
        26432  +  assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
        26433  +
 26067  26434     bufpt = 0;
 26068  26435     if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
 26069  26436       pArgList = va_arg(ap, PrintfArguments*);
 26070  26437       bArgList = 1;
 26071  26438     }else{
 26072  26439       bArgList = 0;
 26073  26440     }
................................................................................
 26478  26845           buf[0] = '%';
 26479  26846           bufpt = buf;
 26480  26847           length = 1;
 26481  26848           break;
 26482  26849         case etCHARX:
 26483  26850           if( bArgList ){
 26484  26851             bufpt = getTextArg(pArgList);
 26485         -          c = bufpt ? bufpt[0] : 0;
        26852  +          length = 1;
        26853  +          if( bufpt ){
        26854  +            buf[0] = c = *(bufpt++);
        26855  +            if( (c&0xc0)==0xc0 ){
        26856  +              while( length<4 && (bufpt[0]&0xc0)==0x80 ){
        26857  +                buf[length++] = *(bufpt++);
        26858  +              }
        26859  +            }
        26860  +          }else{
        26861  +            buf[0] = 0;
        26862  +          }
 26486  26863           }else{
 26487         -          c = va_arg(ap,int);
        26864  +          unsigned int ch = va_arg(ap,unsigned int);
        26865  +          if( ch<0x00080 ){
        26866  +            buf[0] = ch & 0xff;
        26867  +            length = 1;
        26868  +          }else if( ch<0x00800 ){
        26869  +            buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
        26870  +            buf[1] = 0x80 + (u8)(ch & 0x3f);
        26871  +            length = 2;
        26872  +          }else if( ch<0x10000 ){
        26873  +            buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
        26874  +            buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
        26875  +            buf[2] = 0x80 + (u8)(ch & 0x3f);
        26876  +            length = 3;
        26877  +          }else{
        26878  +            buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
        26879  +            buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
        26880  +            buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
        26881  +            buf[3] = 0x80 + (u8)(ch & 0x3f);
        26882  +            length = 4;
        26883  +          }
 26488  26884           }
 26489  26885           if( precision>1 ){
 26490  26886             width -= precision-1;
 26491  26887             if( width>1 && !flag_leftjustify ){
 26492  26888               sqlite3AppendChar(pAccum, width-1, ' ');
 26493  26889               width = 0;
 26494  26890             }
 26495         -          sqlite3AppendChar(pAccum, precision-1, c);
        26891  +          while( precision-- > 1 ){
        26892  +            sqlite3StrAccumAppend(pAccum, buf, length);
        26893  +          }
 26496  26894           }
 26497         -        length = 1;
 26498         -        buf[0] = c;
 26499  26895           bufpt = buf;
 26500         -        break;
        26896  +        flag_altform2 = 1;
        26897  +        goto adjust_width_for_utf8;
 26501  26898         case etSTRING:
 26502  26899         case etDYNSTRING:
 26503  26900           if( bArgList ){
 26504  26901             bufpt = getTextArg(pArgList);
 26505  26902             xtype = etSTRING;
 26506  26903           }else{
 26507  26904             bufpt = va_arg(ap,char*);
 26508  26905           }
 26509  26906           if( bufpt==0 ){
 26510  26907             bufpt = "";
 26511  26908           }else if( xtype==etDYNSTRING ){
        26909  +          if( pAccum->nChar==0 && pAccum->mxAlloc && width==0 && precision<0 ){
        26910  +            /* Special optimization for sqlite3_mprintf("%z..."):
        26911  +            ** Extend an existing memory allocation rather than creating
        26912  +            ** a new one. */
        26913  +            assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
        26914  +            pAccum->zText = bufpt;
        26915  +            pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
        26916  +            pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
        26917  +            pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
        26918  +            length = 0;
        26919  +            break;
        26920  +          }
 26512  26921             zExtra = bufpt;
 26513  26922           }
 26514  26923           if( precision>=0 ){
 26515         -          for(length=0; length<precision && bufpt[length]; length++){}
        26924  +          if( flag_altform2 ){
        26925  +            /* Set length to the number of bytes needed in order to display
        26926  +            ** precision characters */
        26927  +            unsigned char *z = (unsigned char*)bufpt;
        26928  +            while( precision-- > 0 && z[0] ){
        26929  +              SQLITE_SKIP_UTF8(z);
        26930  +            }
        26931  +            length = (int)(z - (unsigned char*)bufpt);
        26932  +          }else{
        26933  +            for(length=0; length<precision && bufpt[length]; length++){}
        26934  +          }
 26516  26935           }else{
 26517  26936             length = 0x7fffffff & (int)strlen(bufpt);
 26518  26937           }
        26938  +      adjust_width_for_utf8:
        26939  +        if( flag_altform2 && width>0 ){
        26940  +          /* Adjust width to account for extra bytes in UTF-8 characters */
        26941  +          int ii = length - 1;
        26942  +          while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
        26943  +        }
 26519  26944           break;
 26520         -      case etSQLESCAPE:           /* Escape ' characters */
 26521         -      case etSQLESCAPE2:          /* Escape ' and enclose in '...' */
 26522         -      case etSQLESCAPE3: {        /* Escape " characters */
        26945  +      case etSQLESCAPE:           /* %q: Escape ' characters */
        26946  +      case etSQLESCAPE2:          /* %Q: Escape ' and enclose in '...' */
        26947  +      case etSQLESCAPE3: {        /* %w: Escape " characters */
 26523  26948           int i, j, k, n, isnull;
 26524  26949           int needQuote;
 26525  26950           char ch;
 26526  26951           char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
 26527  26952           char *escarg;
 26528  26953   
 26529  26954           if( bArgList ){
 26530  26955             escarg = getTextArg(pArgList);
 26531  26956           }else{
 26532  26957             escarg = va_arg(ap,char*);
 26533  26958           }
 26534  26959           isnull = escarg==0;
 26535  26960           if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
        26961  +        /* For %q, %Q, and %w, the precision is the number of byte (or
        26962  +        ** characters if the ! flags is present) to use from the input.
        26963  +        ** Because of the extra quoting characters inserted, the number
        26964  +        ** of output characters may be larger than the precision.
        26965  +        */
 26536  26966           k = precision;
 26537  26967           for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
 26538  26968             if( ch==q )  n++;
        26969  +          if( flag_altform2 && (ch&0xc0)==0xc0 ){
        26970  +            while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
        26971  +          }
 26539  26972           }
 26540  26973           needQuote = !isnull && xtype==etSQLESCAPE2;
 26541  26974           n += i + 3;
 26542  26975           if( n>etBUFSIZE ){
 26543  26976             bufpt = zExtra = sqlite3Malloc( n );
 26544  26977             if( bufpt==0 ){
 26545  26978               setStrAccumError(pAccum, STRACCUM_NOMEM);
................................................................................
 26554  26987           for(i=0; i<k; i++){
 26555  26988             bufpt[j++] = ch = escarg[i];
 26556  26989             if( ch==q ) bufpt[j++] = ch;
 26557  26990           }
 26558  26991           if( needQuote ) bufpt[j++] = q;
 26559  26992           bufpt[j] = 0;
 26560  26993           length = j;
 26561         -        /* The precision in %q and %Q means how many input characters to
 26562         -        ** consume, not the length of the output...
 26563         -        ** if( precision>=0 && precision<length ) length = precision; */
 26564         -        break;
        26994  +        goto adjust_width_for_utf8;
 26565  26995         }
 26566  26996         case etTOKEN: {
 26567  26997           Token *pToken;
 26568  26998           if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
 26569  26999           pToken = va_arg(ap, Token*);
 26570  27000           assert( bArgList==0 );
 26571  27001           if( pToken && pToken->n ){
................................................................................
 26596  27026           assert( xtype==etINVALID );
 26597  27027           return;
 26598  27028         }
 26599  27029       }/* End switch over the format type */
 26600  27030       /*
 26601  27031       ** The text of the conversion is pointed to by "bufpt" and is
 26602  27032       ** "length" characters long.  The field width is "width".  Do
 26603         -    ** the output.
        27033  +    ** the output.  Both length and width are in bytes, not characters,
        27034  +    ** at this point.  If the "!" flag was present on string conversions
        27035  +    ** indicating that width and precision should be expressed in characters,
        27036  +    ** then the values have been translated prior to reaching this point.
 26604  27037       */
 26605  27038       width -= length;
 26606  27039       if( width>0 ){
 26607  27040         if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 26608  27041         sqlite3StrAccumAppend(pAccum, bufpt, length);
 26609  27042         if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 26610  27043       }else{
................................................................................
 27111  27544     pView = sqlite3TreeViewPush(pView, moreToFollow);
 27112  27545     if( p->pWith ){
 27113  27546       sqlite3TreeViewWith(pView, p->pWith, 1);
 27114  27547       cnt = 1;
 27115  27548       sqlite3TreeViewPush(pView, 1);
 27116  27549     }
 27117  27550     do{
        27551  +#if SELECTTRACE_ENABLED
        27552  +    sqlite3TreeViewLine(pView,
        27553  +      "SELECT%s%s (%s/%p) selFlags=0x%x nSelectRow=%d",
        27554  +      ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
        27555  +      ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
        27556  +      p->zSelName, p, p->selFlags,
        27557  +      (int)p->nSelectRow
        27558  +    );
        27559  +#else
 27118  27560       sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
 27119  27561         ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
 27120  27562         ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags,
 27121  27563         (int)p->nSelectRow
 27122  27564       );
        27565  +#endif
 27123  27566       if( cnt++ ) sqlite3TreeViewPop(pView);
 27124  27567       if( p->pPrior ){
 27125  27568         n = 1000;
 27126  27569       }else{
 27127  27570         n = 0;
 27128  27571         if( p->pSrc && p->pSrc->nSrc ) n++;
 27129  27572         if( p->pWhere ) n++;
................................................................................
 27265  27708       case TK_STRING: {
 27266  27709         sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
 27267  27710         break;
 27268  27711       }
 27269  27712       case TK_NULL: {
 27270  27713         sqlite3TreeViewLine(pView,"NULL");
 27271  27714         break;
        27715  +    }
        27716  +    case TK_TRUEFALSE: {
        27717  +      sqlite3TreeViewLine(pView,
        27718  +         sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
        27719  +      break;
 27272  27720       }
 27273  27721   #ifndef SQLITE_OMIT_BLOB_LITERAL
 27274  27722       case TK_BLOB: {
 27275  27723         sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
 27276  27724         break;
 27277  27725       }
 27278  27726   #endif
................................................................................
 27321  27769   
 27322  27770       case TK_UMINUS:  zUniOp = "UMINUS"; break;
 27323  27771       case TK_UPLUS:   zUniOp = "UPLUS";  break;
 27324  27772       case TK_BITNOT:  zUniOp = "BITNOT"; break;
 27325  27773       case TK_NOT:     zUniOp = "NOT";    break;
 27326  27774       case TK_ISNULL:  zUniOp = "ISNULL"; break;
 27327  27775       case TK_NOTNULL: zUniOp = "NOTNULL"; break;
        27776  +
        27777  +    case TK_TRUTH: {
        27778  +      int x;
        27779  +      const char *azOp[] = {
        27780  +         "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
        27781  +      };
        27782  +      assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
        27783  +      assert( pExpr->pRight );
        27784  +      assert( pExpr->pRight->op==TK_TRUEFALSE );
        27785  +      x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
        27786  +      zUniOp = azOp[x];
        27787  +      break;
        27788  +    }
 27328  27789   
 27329  27790       case TK_SPAN: {
 27330  27791         sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
 27331  27792         sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 27332  27793         break;
 27333  27794       }
 27334  27795   
................................................................................
 29059  29520   /*
 29060  29521   ** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
 29061  29522   ** routine does *not* accept hexadecimal notation.
 29062  29523   **
 29063  29524   ** Returns:
 29064  29525   **
 29065  29526   **     0    Successful transformation.  Fits in a 64-bit signed integer.
 29066         -**     1    Excess text after the integer value
        29527  +**     1    Excess non-space text after the integer value
 29067  29528   **     2    Integer too large for a 64-bit signed integer or is malformed
 29068  29529   **     3    Special case of 9223372036854775808
 29069  29530   **
 29070  29531   ** length is the number of bytes in the string (bytes, not characters).
 29071  29532   ** The string is not necessarily zero-terminated.  The encoding is
 29072  29533   ** given by enc.
 29073  29534   */
................................................................................
 29102  29563       }
 29103  29564     }
 29104  29565     zStart = zNum;
 29105  29566     while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
 29106  29567     for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
 29107  29568       u = u*10 + c - '0';
 29108  29569     }
        29570  +  testcase( i==18*incr );
        29571  +  testcase( i==19*incr );
        29572  +  testcase( i==20*incr );
 29109  29573     if( u>LARGEST_INT64 ){
        29574  +    /* This test and assignment is needed only to suppress UB warnings
        29575  +    ** from clang and -fsanitize=undefined.  This test and assignment make
        29576  +    ** the code a little larger and slower, and no harm comes from omitting
        29577  +    ** them, but we must appaise the undefined-behavior pharisees. */
 29110  29578       *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
 29111  29579     }else if( neg ){
 29112  29580       *pNum = -(i64)u;
 29113  29581     }else{
 29114  29582       *pNum = (i64)u;
 29115  29583     }
 29116         -  testcase( i==18 );
 29117         -  testcase( i==19 );
 29118         -  testcase( i==20 );
 29119         -  if( &zNum[i]<zEnd              /* Extra bytes at the end */
 29120         -   || (i==0 && zStart==zNum)     /* No digits */
        29584  +  rc = 0;
        29585  +  if( (i==0 && zStart==zNum)     /* No digits */
 29121  29586      || nonNum                     /* UTF16 with high-order bytes non-zero */
 29122  29587     ){
 29123  29588       rc = 1;
 29124         -  }else{
 29125         -    rc = 0;
        29589  +  }else if( &zNum[i]<zEnd ){     /* Extra bytes at the end */
        29590  +    int jj = i;
        29591  +    do{
        29592  +      if( !sqlite3Isspace(zNum[jj]) ){
        29593  +        rc = 1;          /* Extra non-space text after the integer */
        29594  +        break;
        29595  +      }
        29596  +      jj += incr;
        29597  +    }while( &zNum[jj]<zEnd );
 29126  29598     }
 29127         -  if( i>19*incr ){                /* Too many digits */
 29128         -    /* zNum is empty or contains non-numeric text or is longer
 29129         -    ** than 19 digits (thus guaranteeing that it is too large) */
 29130         -    return 2;
 29131         -  }else if( i<19*incr ){
        29599  +  if( i<19*incr ){
 29132  29600       /* Less than 19 digits, so we know that it fits in 64 bits */
 29133  29601       assert( u<=LARGEST_INT64 );
 29134  29602       return rc;
 29135  29603     }else{
 29136  29604       /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
 29137         -    c = compare2pow63(zNum, incr);
        29605  +    c = i>19*incr ? 1 : compare2pow63(zNum, incr);
 29138  29606       if( c<0 ){
 29139  29607         /* zNum is less than 9223372036854775808 so it fits */
 29140  29608         assert( u<=LARGEST_INT64 );
 29141  29609         return rc;
 29142         -    }else if( c>0 ){
 29143         -      /* zNum is greater than 9223372036854775808 so it overflows */
 29144         -      return 2;
 29145  29610       }else{
 29146         -      /* zNum is exactly 9223372036854775808.  Fits if negative.  The
 29147         -      ** special case 2 overflow if positive */
 29148         -      assert( u-1==LARGEST_INT64 );
 29149         -      return neg ? rc : 3;
        29611  +      *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
        29612  +      if( c>0 ){
        29613  +        /* zNum is greater than 9223372036854775808 so it overflows */
        29614  +        return 2;
        29615  +      }else{
        29616  +        /* zNum is exactly 9223372036854775808.  Fits if negative.  The
        29617  +        ** special case 2 overflow if positive */
        29618  +        assert( u-1==LARGEST_INT64 );
        29619  +        return neg ? rc : 3;
        29620  +      }
 29150  29621       }
 29151  29622     }
 29152  29623   }
 29153  29624   
 29154  29625   /*
 29155  29626   ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
 29156  29627   ** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
................................................................................
 30459  30930       /*  89 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 30460  30931       /*  90 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 30461  30932       /*  91 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 30462  30933       /*  92 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 30463  30934       /*  93 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 30464  30935       /*  94 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 30465  30936       /*  95 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 30466         -    /*  96 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
        30937  +    /*  96 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
 30467  30938       /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 30468         -    /*  98 */ "Column"           OpHelp("r[P3]=PX"),
 30469         -    /*  99 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 30470         -    /* 100 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 30471         -    /* 101 */ "Count"            OpHelp("r[P2]=count()"),
 30472         -    /* 102 */ "ReadCookie"       OpHelp(""),
 30473         -    /* 103 */ "SetCookie"        OpHelp(""),
 30474         -    /* 104 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 30475         -    /* 105 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 30476         -    /* 106 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
 30477         -    /* 107 */ "OpenDup"          OpHelp(""),
 30478         -    /* 108 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 30479         -    /* 109 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 30480         -    /* 110 */ "SorterOpen"       OpHelp(""),
 30481         -    /* 111 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 30482         -    /* 112 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 30483         -    /* 113 */ "Close"            OpHelp(""),
 30484         -    /* 114 */ "ColumnsUsed"      OpHelp(""),
 30485         -    /* 115 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 30486         -    /* 116 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 30487         -    /* 117 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 30488         -    /* 118 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 30489         -    /* 119 */ "Delete"           OpHelp(""),
 30490         -    /* 120 */ "ResetCount"       OpHelp(""),
 30491         -    /* 121 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 30492         -    /* 122 */ "SorterData"       OpHelp("r[P2]=data"),
 30493         -    /* 123 */ "RowData"          OpHelp("r[P2]=data"),
 30494         -    /* 124 */ "Rowid"            OpHelp("r[P2]=rowid"),
 30495         -    /* 125 */ "NullRow"          OpHelp(""),
 30496         -    /* 126 */ "SeekEnd"          OpHelp(""),
 30497         -    /* 127 */ "SorterInsert"     OpHelp("key=r[P2]"),
 30498         -    /* 128 */ "IdxInsert"        OpHelp("key=r[P2]"),
 30499         -    /* 129 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 30500         -    /* 130 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 30501         -    /* 131 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        30939  +    /*  98 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
        30940  +    /*  99 */ "Column"           OpHelp("r[P3]=PX"),
        30941  +    /* 100 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
        30942  +    /* 101 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        30943  +    /* 102 */ "Count"            OpHelp("r[P2]=count()"),
        30944  +    /* 103 */ "ReadCookie"       OpHelp(""),
        30945  +    /* 104 */ "SetCookie"        OpHelp(""),
        30946  +    /* 105 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
        30947  +    /* 106 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        30948  +    /* 107 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        30949  +    /* 108 */ "OpenDup"          OpHelp(""),
        30950  +    /* 109 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
        30951  +    /* 110 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
        30952  +    /* 111 */ "SorterOpen"       OpHelp(""),
        30953  +    /* 112 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
        30954  +    /* 113 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
        30955  +    /* 114 */ "Close"            OpHelp(""),
        30956  +    /* 115 */ "ColumnsUsed"      OpHelp(""),
        30957  +    /* 116 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
        30958  +    /* 117 */ "NewRowid"         OpHelp("r[P2]=rowid"),
        30959  +    /* 118 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
        30960  +    /* 119 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
        30961  +    /* 120 */ "Delete"           OpHelp(""),
        30962  +    /* 121 */ "ResetCount"       OpHelp(""),
        30963  +    /* 122 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
        30964  +    /* 123 */ "SorterData"       OpHelp("r[P2]=data"),
        30965  +    /* 124 */ "RowData"          OpHelp("r[P2]=data"),
        30966  +    /* 125 */ "Rowid"            OpHelp("r[P2]=rowid"),
        30967  +    /* 126 */ "NullRow"          OpHelp(""),
        30968  +    /* 127 */ "SeekEnd"          OpHelp(""),
        30969  +    /* 128 */ "SorterInsert"     OpHelp("key=r[P2]"),
        30970  +    /* 129 */ "IdxInsert"        OpHelp("key=r[P2]"),
        30971  +    /* 130 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        30972  +    /* 131 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 30502  30973       /* 132 */ "Real"             OpHelp("r[P2]=P4"),
 30503         -    /* 133 */ "Destroy"          OpHelp(""),
 30504         -    /* 134 */ "Clear"            OpHelp(""),
 30505         -    /* 135 */ "ResetSorter"      OpHelp(""),
 30506         -    /* 136 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
 30507         -    /* 137 */ "SqlExec"          OpHelp(""),
 30508         -    /* 138 */ "ParseSchema"      OpHelp(""),
 30509         -    /* 139 */ "LoadAnalysis"     OpHelp(""),
 30510         -    /* 140 */ "DropTable"        OpHelp(""),
 30511         -    /* 141 */ "DropIndex"        OpHelp(""),
 30512         -    /* 142 */ "DropTrigger"      OpHelp(""),
 30513         -    /* 143 */ "IntegrityCk"      OpHelp(""),
 30514         -    /* 144 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 30515         -    /* 145 */ "Param"            OpHelp(""),
 30516         -    /* 146 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 30517         -    /* 147 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 30518         -    /* 148 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
 30519         -    /* 149 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
 30520         -    /* 150 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 30521         -    /* 151 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 30522         -    /* 152 */ "Expire"           OpHelp(""),
 30523         -    /* 153 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 30524         -    /* 154 */ "VBegin"           OpHelp(""),
 30525         -    /* 155 */ "VCreate"          OpHelp(""),
 30526         -    /* 156 */ "VDestroy"         OpHelp(""),
 30527         -    /* 157 */ "VOpen"            OpHelp(""),
 30528         -    /* 158 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 30529         -    /* 159 */ "VRename"          OpHelp(""),
 30530         -    /* 160 */ "Pagecount"        OpHelp(""),
 30531         -    /* 161 */ "MaxPgcnt"         OpHelp(""),
 30532         -    /* 162 */ "PureFunc0"        OpHelp(""),
 30533         -    /* 163 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
 30534         -    /* 164 */ "PureFunc"         OpHelp(""),
 30535         -    /* 165 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
 30536         -    /* 166 */ "Trace"            OpHelp(""),
 30537         -    /* 167 */ "CursorHint"       OpHelp(""),
 30538         -    /* 168 */ "Noop"             OpHelp(""),
 30539         -    /* 169 */ "Explain"          OpHelp(""),
        30974  +    /* 133 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        30975  +    /* 134 */ "Destroy"          OpHelp(""),
        30976  +    /* 135 */ "Clear"            OpHelp(""),
        30977  +    /* 136 */ "ResetSorter"      OpHelp(""),
        30978  +    /* 137 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
        30979  +    /* 138 */ "SqlExec"          OpHelp(""),
        30980  +    /* 139 */ "ParseSchema"      OpHelp(""),
        30981  +    /* 140 */ "LoadAnalysis"     OpHelp(""),
        30982  +    /* 141 */ "DropTable"        OpHelp(""),
        30983  +    /* 142 */ "DropIndex"        OpHelp(""),
        30984  +    /* 143 */ "DropTrigger"      OpHelp(""),
        30985  +    /* 144 */ "IntegrityCk"      OpHelp(""),
        30986  +    /* 145 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        30987  +    /* 146 */ "Param"            OpHelp(""),
        30988  +    /* 147 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        30989  +    /* 148 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        30990  +    /* 149 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
        30991  +    /* 150 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
        30992  +    /* 151 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
        30993  +    /* 152 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        30994  +    /* 153 */ "Expire"           OpHelp(""),
        30995  +    /* 154 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        30996  +    /* 155 */ "VBegin"           OpHelp(""),
        30997  +    /* 156 */ "VCreate"          OpHelp(""),
        30998  +    /* 157 */ "VDestroy"         OpHelp(""),
        30999  +    /* 158 */ "VOpen"            OpHelp(""),
        31000  +    /* 159 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        31001  +    /* 160 */ "VRename"          OpHelp(""),
        31002  +    /* 161 */ "Pagecount"        OpHelp(""),
        31003  +    /* 162 */ "MaxPgcnt"         OpHelp(""),
        31004  +    /* 163 */ "PureFunc0"        OpHelp(""),
        31005  +    /* 164 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
        31006  +    /* 165 */ "PureFunc"         OpHelp(""),
        31007  +    /* 166 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
        31008  +    /* 167 */ "Trace"            OpHelp(""),
        31009  +    /* 168 */ "CursorHint"       OpHelp(""),
        31010  +    /* 169 */ "Noop"             OpHelp(""),
        31011  +    /* 170 */ "Explain"          OpHelp(""),
 30540  31012     };
 30541  31013     return azName[i];
 30542  31014   }
 30543  31015   #endif
 30544  31016   
 30545  31017   /************** End of opcodes.c *********************************************/
 30546  31018   /************** Begin file os_unix.c *****************************************/
................................................................................
 30771  31243     int deviceCharacteristics;          /* Precomputed device characteristics */
 30772  31244   #if SQLITE_ENABLE_LOCKING_STYLE
 30773  31245     int openFlags;                      /* The flags specified at open() */
 30774  31246   #endif
 30775  31247   #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
 30776  31248     unsigned fsFlags;                   /* cached details from statfs() */
 30777  31249   #endif
        31250  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        31251  +  unsigned iBusyTimeout;              /* Wait this many millisec on locks */
        31252  +#endif
 30778  31253   #if OS_VXWORKS
 30779  31254     struct vxworksFileId *pId;          /* Unique file ID */
 30780  31255   #endif
 30781  31256   #ifdef SQLITE_DEBUG
 30782  31257     /* The next group of variables are used to track whether or not the
 30783  31258     ** transaction counter in bytes 24-27 of database files are updated
 30784  31259     ** whenever any part of the database changes.  An assertion fault will
................................................................................
 31208  31683   #if defined(HAVE_FCHOWN)
 31209  31684     { "fchown",       (sqlite3_syscall_ptr)fchown,          0 },
 31210  31685   #else
 31211  31686     { "fchown",       (sqlite3_syscall_ptr)0,               0 },
 31212  31687   #endif
 31213  31688   #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
 31214  31689   
        31690  +#if defined(HAVE_FCHOWN)
 31215  31691     { "geteuid",      (sqlite3_syscall_ptr)geteuid,         0 },
        31692  +#else
        31693  +  { "geteuid",      (sqlite3_syscall_ptr)0,               0 },
        31694  +#endif
 31216  31695   #define osGeteuid   ((uid_t(*)(void))aSyscall[21].pCurrent)
 31217  31696   
 31218  31697   #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
 31219  31698     { "mmap",         (sqlite3_syscall_ptr)mmap,            0 },
 31220  31699   #else
 31221  31700     { "mmap",         (sqlite3_syscall_ptr)0,               0 },
 31222  31701   #endif
................................................................................
 31436  31915   ** is held when required. This function is only used as part of assert() 
 31437  31916   ** statements. e.g.
 31438  31917   **
 31439  31918   **   unixEnterMutex()
 31440  31919   **     assert( unixMutexHeld() );
 31441  31920   **   unixEnterLeave()
 31442  31921   */
        31922  +static sqlite3_mutex *unixBigLock = 0;
 31443  31923   static void unixEnterMutex(void){
 31444         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31924  +  sqlite3_mutex_enter(unixBigLock);
 31445  31925   }
 31446  31926   static void unixLeaveMutex(void){
 31447         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31927  +  sqlite3_mutex_leave(unixBigLock);
 31448  31928   }
 31449  31929   #ifdef SQLITE_DEBUG
 31450  31930   static int unixMutexHeld(void) {
 31451         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31931  +  return sqlite3_mutex_held(unixBigLock);
 31452  31932   }
 31453  31933   #endif
 31454  31934   
 31455  31935   
 31456  31936   #ifdef SQLITE_HAVE_OS_TRACE
 31457  31937   /*
 31458  31938   ** Helper function for printing out trace information from debugging
................................................................................
 32199  32679     
 32200  32680     unixLeaveMutex();
 32201  32681     OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
 32202  32682   
 32203  32683     *pResOut = reserved;
 32204  32684     return rc;
 32205  32685   }
        32686  +
        32687  +/*
        32688  +** Set a posix-advisory-lock.
        32689  +**
        32690  +** There are two versions of this routine.  If compiled with
        32691  +** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
        32692  +** which is a pointer to a unixFile.  If the unixFile->iBusyTimeout
        32693  +** value is set, then it is the number of milliseconds to wait before
        32694  +** failing the lock.  The iBusyTimeout value is always reset back to
        32695  +** zero on each call.
        32696  +**
        32697  +** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
        32698  +** attempt to set the lock.
        32699  +*/
        32700  +#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
        32701  +# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
        32702  +#else
        32703  +static int osSetPosixAdvisoryLock(
        32704  +  int h,                /* The file descriptor on which to take the lock */
        32705  +  struct flock *pLock,  /* The description of the lock */
        32706  +  unixFile *pFile       /* Structure holding timeout value */
        32707  +){
        32708  +  int rc = osFcntl(h,F_SETLK,pLock);
        32709  +  while( rc<0 && pFile->iBusyTimeout>0 ){
        32710  +    /* On systems that support some kind of blocking file lock with a timeout,
        32711  +    ** make appropriate changes here to invoke that blocking file lock.  On
        32712  +    ** generic posix, however, there is no such API.  So we simply try the
        32713  +    ** lock once every millisecond until either the timeout expires, or until
        32714  +    ** the lock is obtained. */
        32715  +    usleep(1000);
        32716  +    rc = osFcntl(h,F_SETLK,pLock);
        32717  +    pFile->iBusyTimeout--;
        32718  +  }
        32719  +  return rc;
        32720  +}
        32721  +#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
        32722  +
 32206  32723   
 32207  32724   /*
 32208  32725   ** Attempt to set a system-lock on the file pFile.  The lock is 
 32209  32726   ** described by pLock.
 32210  32727   **
 32211  32728   ** If the pFile was opened read/write from unix-excl, then the only lock
 32212  32729   ** ever obtained is an exclusive lock, and it is obtained exactly once
................................................................................
 32232  32749       if( pInode->bProcessLock==0 ){
 32233  32750         struct flock lock;
 32234  32751         assert( pInode->nLock==0 );
 32235  32752         lock.l_whence = SEEK_SET;
 32236  32753         lock.l_start = SHARED_FIRST;
 32237  32754         lock.l_len = SHARED_SIZE;
 32238  32755         lock.l_type = F_WRLCK;
 32239         -      rc = osFcntl(pFile->h, F_SETLK, &lock);
        32756  +      rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
 32240  32757         if( rc<0 ) return rc;
 32241  32758         pInode->bProcessLock = 1;
 32242  32759         pInode->nLock++;
 32243  32760       }else{
 32244  32761         rc = 0;
 32245  32762       }
 32246  32763     }else{
 32247         -    rc = osFcntl(pFile->h, F_SETLK, pLock);
        32764  +    rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
 32248  32765     }
 32249  32766     return rc;
 32250  32767   }
 32251  32768   
 32252  32769   /*
 32253  32770   ** Lock the file with the lock specified by parameter eFileLock - one
 32254  32771   ** of the following:
................................................................................
 34600  35117         }
 34601  35118         return SQLITE_OK;
 34602  35119       }
 34603  35120       case SQLITE_FCNTL_HAS_MOVED: {
 34604  35121         *(int*)pArg = fileHasMoved(pFile);
 34605  35122         return SQLITE_OK;
 34606  35123       }
        35124  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        35125  +    case SQLITE_FCNTL_LOCK_TIMEOUT: {
        35126  +      pFile->iBusyTimeout = *(int*)pArg;
        35127  +      return SQLITE_OK;
        35128  +    }
        35129  +#endif
 34607  35130   #if SQLITE_MAX_MMAP_SIZE>0
 34608  35131       case SQLITE_FCNTL_MMAP_SIZE: {
 34609  35132         i64 newLimit = *(i64*)pArg;
 34610  35133         int rc = SQLITE_OK;
 34611  35134         if( newLimit>sqlite3GlobalConfig.mxMmap ){
 34612  35135           newLimit = sqlite3GlobalConfig.mxMmap;
 34613  35136         }
................................................................................
 34915  35438     assert( n==1 || lockType!=F_RDLCK );
 34916  35439   
 34917  35440     /* Locks are within range */
 34918  35441     assert( n>=1 && n<=SQLITE_SHM_NLOCK );
 34919  35442   
 34920  35443     if( pShmNode->h>=0 ){
 34921  35444       /* Initialize the locking parameters */
 34922         -    memset(&f, 0, sizeof(f));
 34923  35445       f.l_type = lockType;
 34924  35446       f.l_whence = SEEK_SET;
 34925  35447       f.l_start = ofst;
 34926  35448       f.l_len = n;
 34927         -
 34928         -    rc = osFcntl(pShmNode->h, F_SETLK, &f);
        35449  +    rc = osSetPosixAdvisoryLock(pShmNode->h, &f, pFile);
 34929  35450       rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 34930  35451     }
 34931  35452   
 34932  35453     /* Update the global lock state and do debug tracing */
 34933  35454   #ifdef SQLITE_DEBUG
 34934  35455     { u16 mask;
 34935  35456     OSTRACE(("SHM-LOCK "));
................................................................................
 36586  37107     ** the same instant might all reset the PRNG.  But multiple resets
 36587  37108     ** are harmless.
 36588  37109     */
 36589  37110     if( randomnessPid!=osGetpid(0) ){
 36590  37111       randomnessPid = osGetpid(0);
 36591  37112       sqlite3_randomness(0,0);
 36592  37113     }
 36593         -
 36594  37114     memset(p, 0, sizeof(unixFile));
 36595  37115   
 36596  37116     if( eType==SQLITE_OPEN_MAIN_DB ){
 36597  37117       UnixUnusedFd *pUnused;
 36598  37118       pUnused = findReusableFd(zName, flags);
 36599  37119       if( pUnused ){
 36600  37120         fd = pUnused->fd;
................................................................................
 38461  38981     ** correctly.  See ticket [bb3a86e890c8e96ab] */
 38462  38982     assert( ArraySize(aSyscall)==29 );
 38463  38983   
 38464  38984     /* Register all VFSes defined in the aVfs[] array */
 38465  38985     for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
 38466  38986       sqlite3_vfs_register(&aVfs[i], i==0);
 38467  38987     }
        38988  +  unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
 38468  38989     return SQLITE_OK; 
 38469  38990   }
 38470  38991   
 38471  38992   /*
 38472  38993   ** Shutdown the operating system interface.
 38473  38994   **
 38474  38995   ** Some operating systems might need to do some cleanup in this routine,
 38475  38996   ** to release dynamically allocated objects.  But not on unix.
 38476  38997   ** This routine is a no-op for unix.
 38477  38998   */
 38478  38999   SQLITE_API int sqlite3_os_end(void){ 
        39000  +  unixBigLock = 0;
 38479  39001     return SQLITE_OK; 
 38480  39002   }
 38481  39003    
 38482  39004   #endif /* SQLITE_OS_UNIX */
 38483  39005   
 38484  39006   /************** End of os_unix.c *********************************************/
 38485  39007   /************** Begin file os_win.c ******************************************/
................................................................................
 42310  42832   ** is held when required. This function is only used as part of assert()
 42311  42833   ** statements. e.g.
 42312  42834   **
 42313  42835   **   winShmEnterMutex()
 42314  42836   **     assert( winShmMutexHeld() );
 42315  42837   **   winShmLeaveMutex()
 42316  42838   */
        42839  +static sqlite3_mutex *winBigLock = 0;
 42317  42840   static void winShmEnterMutex(void){
 42318         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42841  +  sqlite3_mutex_enter(winBigLock);
 42319  42842   }
 42320  42843   static void winShmLeaveMutex(void){
 42321         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42844  +  sqlite3_mutex_leave(winBigLock);
 42322  42845   }
 42323  42846   #ifndef NDEBUG
 42324  42847   static int winShmMutexHeld(void) {
 42325         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42848  +  return sqlite3_mutex_held(winBigLock);
 42326  42849   }
 42327  42850   #endif
 42328  42851   
 42329  42852   /*
 42330  42853   ** Object used to represent a single file opened and mmapped to provide
 42331  42854   ** shared memory.  When multiple threads all reference the same
 42332  42855   ** log-summary, each thread has its own winFile object, but they all
................................................................................
 44740  45263   #endif
 44741  45264   
 44742  45265     sqlite3_vfs_register(&winNolockVfs, 0);
 44743  45266   
 44744  45267   #if defined(SQLITE_WIN32_HAS_WIDE)
 44745  45268     sqlite3_vfs_register(&winLongPathNolockVfs, 0);
 44746  45269   #endif
        45270  +
        45271  +#ifndef SQLITE_OMIT_WAL
        45272  +  winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
        45273  +#endif
 44747  45274   
 44748  45275     return SQLITE_OK;
 44749  45276   }
 44750  45277   
 44751  45278   SQLITE_API int sqlite3_os_end(void){
 44752  45279   #if SQLITE_OS_WINRT
 44753  45280     if( sleepObj!=NULL ){
 44754  45281       osCloseHandle(sleepObj);
 44755  45282       sleepObj = NULL;
 44756  45283     }
 44757  45284   #endif
        45285  +
        45286  +#ifndef SQLITE_OMIT_WAL
        45287  +  winBigLock = 0;
        45288  +#endif
        45289  +
 44758  45290     return SQLITE_OK;
 44759  45291   }
 44760  45292   
 44761  45293   #endif /* SQLITE_OS_WIN */
 44762  45294   
 44763  45295   /************** End of os_win.c **********************************************/
        45296  +/************** Begin file memdb.c *******************************************/
        45297  +/*
        45298  +** 2016-09-07
        45299  +**
        45300  +** The author disclaims copyright to this source code.  In place of
        45301  +** a legal notice, here is a blessing:
        45302  +**
        45303  +**    May you do good and not evil.
        45304  +**    May you find forgiveness for yourself and forgive others.
        45305  +**    May you share freely, never taking more than you give.
        45306  +**
        45307  +******************************************************************************
        45308  +**
        45309  +** This file implements an in-memory VFS. A database is held as a contiguous
        45310  +** block of memory.
        45311  +**
        45312  +** This file also implements interface sqlite3_serialize() and
        45313  +** sqlite3_deserialize().
        45314  +*/
        45315  +#ifdef SQLITE_ENABLE_DESERIALIZE
        45316  +/* #include "sqliteInt.h" */
        45317  +
        45318  +/*
        45319  +** Forward declaration of objects used by this utility
        45320  +*/
        45321  +typedef struct sqlite3_vfs MemVfs;
        45322  +typedef struct MemFile MemFile;
        45323  +
        45324  +/* Access to a lower-level VFS that (might) implement dynamic loading,
        45325  +** access to randomness, etc.
        45326  +*/
        45327  +#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
        45328  +
        45329  +/* An open file */
        45330  +struct MemFile {
        45331  +  sqlite3_file base;              /* IO methods */
        45332  +  sqlite3_int64 sz;               /* Size of the file */
        45333  +  sqlite3_int64 szMax;            /* Space allocated to aData */
        45334  +  unsigned char *aData;           /* content of the file */
        45335  +  int nMmap;                      /* Number of memory mapped pages */
        45336  +  unsigned mFlags;                /* Flags */
        45337  +  int eLock;                      /* Most recent lock against this file */
        45338  +};
        45339  +
        45340  +/*
        45341  +** Methods for MemFile
        45342  +*/
        45343  +static int memdbClose(sqlite3_file*);
        45344  +static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
        45345  +static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
        45346  +static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
        45347  +static int memdbSync(sqlite3_file*, int flags);
        45348  +static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
        45349  +static int memdbLock(sqlite3_file*, int);
        45350  +/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
        45351  +static int memdbFileControl(sqlite3_file*, int op, void *pArg);
        45352  +/* static int memdbSectorSize(sqlite3_file*); // not used */
        45353  +static int memdbDeviceCharacteristics(sqlite3_file*);
        45354  +static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
        45355  +static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
        45356  +
        45357  +/*
        45358  +** Methods for MemVfs
        45359  +*/
        45360  +static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
        45361  +/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
        45362  +static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
        45363  +static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
        45364  +static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
        45365  +static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
        45366  +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
        45367  +static void memdbDlClose(sqlite3_vfs*, void*);
        45368  +static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
        45369  +static int memdbSleep(sqlite3_vfs*, int microseconds);
        45370  +/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
        45371  +static int memdbGetLastError(sqlite3_vfs*, int, char *);
        45372  +static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
        45373  +
        45374  +static sqlite3_vfs memdb_vfs = {
        45375  +  2,                           /* iVersion */
        45376  +  0,                           /* szOsFile (set when registered) */
        45377  +  1024,                        /* mxPathname */
        45378  +  0,                           /* pNext */
        45379  +  "memdb",                     /* zName */
        45380  +  0,                           /* pAppData (set when registered) */ 
        45381  +  memdbOpen,                   /* xOpen */
        45382  +  0, /* memdbDelete, */        /* xDelete */
        45383  +  memdbAccess,                 /* xAccess */
        45384  +  memdbFullPathname,           /* xFullPathname */
        45385  +  memdbDlOpen,                 /* xDlOpen */
        45386  +  memdbDlError,                /* xDlError */
        45387  +  memdbDlSym,                  /* xDlSym */
        45388  +  memdbDlClose,                /* xDlClose */
        45389  +  memdbRandomness,             /* xRandomness */
        45390  +  memdbSleep,                  /* xSleep */
        45391  +  0, /* memdbCurrentTime, */   /* xCurrentTime */
        45392  +  memdbGetLastError,           /* xGetLastError */
        45393  +  memdbCurrentTimeInt64        /* xCurrentTimeInt64 */
        45394  +};
        45395  +
        45396  +static const sqlite3_io_methods memdb_io_methods = {
        45397  +  3,                              /* iVersion */
        45398  +  memdbClose,                      /* xClose */
        45399  +  memdbRead,                       /* xRead */
        45400  +  memdbWrite,                      /* xWrite */
        45401  +  memdbTruncate,                   /* xTruncate */
        45402  +  memdbSync,                       /* xSync */
        45403  +  memdbFileSize,                   /* xFileSize */
        45404  +  memdbLock,                       /* xLock */
        45405  +  memdbLock,                       /* xUnlock - same as xLock in this case */ 
        45406  +  0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
        45407  +  memdbFileControl,                /* xFileControl */
        45408  +  0, /* memdbSectorSize,*/         /* xSectorSize */
        45409  +  memdbDeviceCharacteristics,      /* xDeviceCharacteristics */
        45410  +  0,                               /* xShmMap */
        45411  +  0,                               /* xShmLock */
        45412  +  0,                               /* xShmBarrier */
        45413  +  0,                               /* xShmUnmap */
        45414  +  memdbFetch,                      /* xFetch */
        45415  +  memdbUnfetch                     /* xUnfetch */
        45416  +};
        45417  +
        45418  +
        45419  +
        45420  +/*
        45421  +** Close an memdb-file.
        45422  +**
        45423  +** The pData pointer is owned by the application, so there is nothing
        45424  +** to free.
        45425  +*/
        45426  +static int memdbClose(sqlite3_file *pFile){
        45427  +  MemFile *p = (MemFile *)pFile;
        45428  +  if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
        45429  +  return SQLITE_OK;
        45430  +}
        45431  +
        45432  +/*
        45433  +** Read data from an memdb-file.
        45434  +*/
        45435  +static int memdbRead(
        45436  +  sqlite3_file *pFile, 
        45437  +  void *zBuf, 
        45438  +  int iAmt, 
        45439  +  sqlite_int64 iOfst
        45440  +){
        45441  +  MemFile *p = (MemFile *)pFile;
        45442  +  if( iOfst+iAmt>p->sz ){
        45443  +    memset(zBuf, 0, iAmt);
        45444  +    if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
        45445  +    return SQLITE_IOERR_SHORT_READ;
        45446  +  }
        45447  +  memcpy(zBuf, p->aData+iOfst, iAmt);
        45448  +  return SQLITE_OK;
        45449  +}
        45450  +
        45451  +/*
        45452  +** Try to enlarge the memory allocation to hold at least sz bytes
        45453  +*/
        45454  +static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
        45455  +  unsigned char *pNew;
        45456  +  if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
        45457  +    return SQLITE_FULL;
        45458  +  }
        45459  +  pNew = sqlite3_realloc64(p->aData, newSz);
        45460  +  if( pNew==0 ) return SQLITE_NOMEM;
        45461  +  p->aData = pNew;
        45462  +  p->szMax = newSz;
        45463  +  return SQLITE_OK;
        45464  +}
        45465  +
        45466  +/*
        45467  +** Write data to an memdb-file.
        45468  +*/
        45469  +static int memdbWrite(
        45470  +  sqlite3_file *pFile,
        45471  +  const void *z,
        45472  +  int iAmt,
        45473  +  sqlite_int64 iOfst
        45474  +){
        45475  +  MemFile *p = (MemFile *)pFile;
        45476  +  if( iOfst+iAmt>p->sz ){
        45477  +    int rc;
        45478  +    if( iOfst+iAmt>p->szMax
        45479  +     && (rc = memdbEnlarge(p, (iOfst+iAmt)*2))!=SQLITE_OK
        45480  +    ){
        45481  +      return rc;
        45482  +    }
        45483  +    if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
        45484  +    p->sz = iOfst+iAmt;
        45485  +  }
        45486  +  memcpy(p->aData+iOfst, z, iAmt);
        45487  +  return SQLITE_OK;
        45488  +}
        45489  +
        45490  +/*
        45491  +** Truncate an memdb-file.
        45492  +**
        45493  +** In rollback mode (which is always the case for memdb, as it does not
        45494  +** support WAL mode) the truncate() method is only used to reduce
        45495  +** the size of a file, never to increase the size.
        45496  +*/
        45497  +static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
        45498  +  MemFile *p = (MemFile *)pFile;
        45499  +  if( NEVER(size>p->sz) ) return SQLITE_FULL;
        45500  +  p->sz = size; 
        45501  +  return SQLITE_OK;
        45502  +}
        45503  +
        45504  +/*
        45505  +** Sync an memdb-file.
        45506  +*/
        45507  +static int memdbSync(sqlite3_file *pFile, int flags){
        45508  +  return SQLITE_OK;
        45509  +}
        45510  +
        45511  +/*
        45512  +** Return the current file-size of an memdb-file.
        45513  +*/
        45514  +static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
        45515  +  MemFile *p = (MemFile *)pFile;
        45516  +  *pSize = p->sz;
        45517  +  return SQLITE_OK;
        45518  +}
        45519  +
        45520  +/*
        45521  +** Lock an memdb-file.
        45522  +*/
        45523  +static int memdbLock(sqlite3_file *pFile, int eLock){
        45524  +  MemFile *p = (MemFile *)pFile;
        45525  +  p->eLock = eLock;
        45526  +  return SQLITE_OK;
        45527  +}
        45528  +
        45529  +#if 0 /* Never used because memdbAccess() always returns false */
        45530  +/*
        45531  +** Check if another file-handle holds a RESERVED lock on an memdb-file.
        45532  +*/
        45533  +static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
        45534  +  *pResOut = 0;
        45535  +  return SQLITE_OK;
        45536  +}
        45537  +#endif
        45538  +
        45539  +/*
        45540  +** File control method. For custom operations on an memdb-file.
        45541  +*/
        45542  +static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
        45543  +  MemFile *p = (MemFile *)pFile;
        45544  +  int rc = SQLITE_NOTFOUND;
        45545  +  if( op==SQLITE_FCNTL_VFSNAME ){
        45546  +    *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
        45547  +    rc = SQLITE_OK;
        45548  +  }
        45549  +  return rc;
        45550  +}
        45551  +
        45552  +#if 0  /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
        45553  +/*
        45554  +** Return the sector-size in bytes for an memdb-file.
        45555  +*/
        45556  +static int memdbSectorSize(sqlite3_file *pFile){
        45557  +  return 1024;
        45558  +}
        45559  +#endif
        45560  +
        45561  +/*
        45562  +** Return the device characteristic flags supported by an memdb-file.
        45563  +*/
        45564  +static int memdbDeviceCharacteristics(sqlite3_file *pFile){
        45565  +  return SQLITE_IOCAP_ATOMIC | 
        45566  +         SQLITE_IOCAP_POWERSAFE_OVERWRITE |
        45567  +         SQLITE_IOCAP_SAFE_APPEND |
        45568  +         SQLITE_IOCAP_SEQUENTIAL;
        45569  +}
        45570  +
        45571  +/* Fetch a page of a memory-mapped file */
        45572  +static int memdbFetch(
        45573  +  sqlite3_file *pFile,
        45574  +  sqlite3_int64 iOfst,
        45575  +  int iAmt,
        45576  +  void **pp
        45577  +){
        45578  +  MemFile *p = (MemFile *)pFile;
        45579  +  p->nMmap++;
        45580  +  *pp = (void*)(p->aData + iOfst);
        45581  +  return SQLITE_OK;
        45582  +}
        45583  +
        45584  +/* Release a memory-mapped page */
        45585  +static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
        45586  +  MemFile *p = (MemFile *)pFile;
        45587  +  p->nMmap--;
        45588  +  return SQLITE_OK;
        45589  +}
        45590  +
        45591  +/*
        45592  +** Open an mem file handle.
        45593  +*/
        45594  +static int memdbOpen(
        45595  +  sqlite3_vfs *pVfs,
        45596  +  const char *zName,
        45597  +  sqlite3_file *pFile,
        45598  +  int flags,
        45599  +  int *pOutFlags
        45600  +){
        45601  +  MemFile *p = (MemFile*)pFile;
        45602  +  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
        45603  +    return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
        45604  +  }
        45605  +  memset(p, 0, sizeof(*p));
        45606  +  p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
        45607  +  assert( pOutFlags!=0 );  /* True because flags==SQLITE_OPEN_MAIN_DB */
        45608  +  *pOutFlags = flags | SQLITE_OPEN_MEMORY;
        45609  +  p->base.pMethods = &memdb_io_methods;
        45610  +  return SQLITE_OK;
        45611  +}
        45612  +
        45613  +#if 0 /* Only used to delete rollback journals, master journals, and WAL
        45614  +      ** files, none of which exist in memdb.  So this routine is never used */
        45615  +/*
        45616  +** Delete the file located at zPath. If the dirSync argument is true,
        45617  +** ensure the file-system modifications are synced to disk before
        45618  +** returning.
        45619  +*/
        45620  +static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
        45621  +  return SQLITE_IOERR_DELETE;
        45622  +}
        45623  +#endif
        45624  +
        45625  +/*
        45626  +** Test for access permissions. Return true if the requested permission
        45627  +** is available, or false otherwise.
        45628  +**
        45629  +** With memdb, no files ever exist on disk.  So always return false.
        45630  +*/
        45631  +static int memdbAccess(
        45632  +  sqlite3_vfs *pVfs, 
        45633  +  const char *zPath, 
        45634  +  int flags, 
        45635  +  int *pResOut
        45636  +){
        45637  +  *pResOut = 0;
        45638  +  return SQLITE_OK;
        45639  +}
        45640  +
        45641  +/*
        45642  +** Populate buffer zOut with the full canonical pathname corresponding
        45643  +** to the pathname in zPath. zOut is guaranteed to point to a buffer
        45644  +** of at least (INST_MAX_PATHNAME+1) bytes.
        45645  +*/
        45646  +static int memdbFullPathname(
        45647  +  sqlite3_vfs *pVfs, 
        45648  +  const char *zPath, 
        45649  +  int nOut, 
        45650  +  char *zOut
        45651  +){
        45652  +  sqlite3_snprintf(nOut, zOut, "%s", zPath);
        45653  +  return SQLITE_OK;
        45654  +}
        45655  +
        45656  +/*
        45657  +** Open the dynamic library located at zPath and return a handle.
        45658  +*/
        45659  +static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
        45660  +  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
        45661  +}
        45662  +
        45663  +/*
        45664  +** Populate the buffer zErrMsg (size nByte bytes) with a human readable
        45665  +** utf-8 string describing the most recent error encountered associated 
        45666  +** with dynamic libraries.
        45667  +*/
        45668  +static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
        45669  +  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
        45670  +}
        45671  +
        45672  +/*
        45673  +** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
        45674  +*/
        45675  +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
        45676  +  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
        45677  +}
        45678  +
        45679  +/*
        45680  +** Close the dynamic library handle pHandle.
        45681  +*/
        45682  +static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
        45683  +  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
        45684  +}
        45685  +
        45686  +/*
        45687  +** Populate the buffer pointed to by zBufOut with nByte bytes of 
        45688  +** random data.
        45689  +*/
        45690  +static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
        45691  +  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
        45692  +}
        45693  +
        45694  +/*
        45695  +** Sleep for nMicro microseconds. Return the number of microseconds 
        45696  +** actually slept.
        45697  +*/
        45698  +static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
        45699  +  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
        45700  +}
        45701  +
        45702  +#if 0  /* Never used.  Modern cores only call xCurrentTimeInt64() */
        45703  +/*
        45704  +** Return the current time as a Julian Day number in *pTimeOut.
        45705  +*/
        45706  +static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
        45707  +  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
        45708  +}
        45709  +#endif
        45710  +
        45711  +static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
        45712  +  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
        45713  +}
        45714  +static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
        45715  +  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
        45716  +}
        45717  +
        45718  +/*
        45719  +** Translate a database connection pointer and schema name into a
        45720  +** MemFile pointer.
        45721  +*/
        45722  +static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
        45723  +  MemFile *p = 0;
        45724  +  int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
        45725  +  if( rc ) return 0;
        45726  +  if( p->base.pMethods!=&memdb_io_methods ) return 0;
        45727  +  return p;
        45728  +}
        45729  +
        45730  +/*
        45731  +** Return the serialization of a database
        45732  +*/
        45733  +SQLITE_API unsigned char *sqlite3_serialize(
        45734  +  sqlite3 *db,              /* The database connection */
        45735  +  const char *zSchema,      /* Which database within the connection */
        45736  +  sqlite3_int64 *piSize,    /* Write size here, if not NULL */
        45737  +  unsigned int mFlags       /* Maybe SQLITE_SERIALIZE_NOCOPY */
        45738  +){
        45739  +  MemFile *p;
        45740  +  int iDb;
        45741  +  Btree *pBt;
        45742  +  sqlite3_int64 sz;
        45743  +  int szPage = 0;
        45744  +  sqlite3_stmt *pStmt = 0;
        45745  +  unsigned char *pOut;
        45746  +  char *zSql;
        45747  +  int rc;
        45748  +
        45749  +#ifdef SQLITE_ENABLE_API_ARMOR
        45750  +  if( !sqlite3SafetyCheckOk(db) ){
        45751  +    (void)SQLITE_MISUSE_BKPT;
        45752  +    return 0;
        45753  +  }
        45754  +#endif
        45755  +
        45756  +  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
        45757  +  p = memdbFromDbSchema(db, zSchema);
        45758  +  iDb = sqlite3FindDbName(db, zSchema);
        45759  +  if( piSize ) *piSize = -1;
        45760  +  if( iDb<0 ) return 0;
        45761  +  if( p ){
        45762  +    if( piSize ) *piSize = p->sz;
        45763  +    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
        45764  +      pOut = p->aData;
        45765  +    }else{
        45766  +      pOut = sqlite3_malloc64( p->sz );
        45767  +      if( pOut ) memcpy(pOut, p->aData, p->sz);
        45768  +    }
        45769  +    return pOut;
        45770  +  }
        45771  +  pBt = db->aDb[iDb].pBt;
        45772  +  if( pBt==0 ) return 0;
        45773  +  szPage = sqlite3BtreeGetPageSize(pBt);
        45774  +  zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
        45775  +  rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
        45776  +  sqlite3_free(zSql);
        45777  +  if( rc ) return 0;
        45778  +  rc = sqlite3_step(pStmt);
        45779  +  if( rc!=SQLITE_ROW ){
        45780  +    pOut = 0;
        45781  +  }else{
        45782  +    sz = sqlite3_column_int64(pStmt, 0)*szPage;
        45783  +    if( piSize ) *piSize = sz;
        45784  +    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
        45785  +      pOut = 0;
        45786  +    }else{
        45787  +      pOut = sqlite3_malloc64( sz );
        45788  +      if( pOut ){
        45789  +        int nPage = sqlite3_column_int(pStmt, 0);
        45790  +        Pager *pPager = sqlite3BtreePager(pBt);
        45791  +        int pgno;
        45792  +        for(pgno=1; pgno<=nPage; pgno++){
        45793  +          DbPage *pPage = 0;
        45794  +          unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
        45795  +          rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
        45796  +          if( rc==SQLITE_OK ){
        45797  +            memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
        45798  +          }else{
        45799  +            memset(pTo, 0, szPage);
        45800  +          }
        45801  +          sqlite3PagerUnref(pPage);       
        45802  +        }
        45803  +      }
        45804  +    }
        45805  +  }
        45806  +  sqlite3_finalize(pStmt);
        45807  +  return pOut;
        45808  +}
        45809  +
        45810  +/* Convert zSchema to a MemDB and initialize its content.
        45811  +*/
        45812  +SQLITE_API int sqlite3_deserialize(
        45813  +  sqlite3 *db,            /* The database connection */
        45814  +  const char *zSchema,    /* Which DB to reopen with the deserialization */
        45815  +  unsigned char *pData,   /* The serialized database content */
        45816  +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
        45817  +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
        45818  +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
        45819  +){
        45820  +  MemFile *p;
        45821  +  char *zSql;
        45822  +  sqlite3_stmt *pStmt = 0;
        45823  +  int rc;
        45824  +  int iDb;
        45825  +
        45826  +#ifdef SQLITE_ENABLE_API_ARMOR
        45827  +  if( !sqlite3SafetyCheckOk(db) ){
        45828  +    return SQLITE_MISUSE_BKPT;
        45829  +  }
        45830  +  if( szDb<0 ) return SQLITE_MISUSE_BKPT;
        45831  +  if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
        45832  +#endif
        45833  +
        45834  +  sqlite3_mutex_enter(db->mutex);
        45835  +  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
        45836  +  iDb = sqlite3FindDbName(db, zSchema);
        45837  +  if( iDb<0 ){
        45838  +    rc = SQLITE_ERROR;
        45839  +    goto end_deserialize;
        45840  +  }    
        45841  +  zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
        45842  +  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
        45843  +  sqlite3_free(zSql);
        45844  +  if( rc ) goto end_deserialize;
        45845  +  db->init.iDb = (u8)iDb;
        45846  +  db->init.reopenMemdb = 1;
        45847  +  rc = sqlite3_step(pStmt);
        45848  +  db->init.reopenMemdb = 0;
        45849  +  if( rc!=SQLITE_DONE ){
        45850  +    rc = SQLITE_ERROR;
        45851  +    goto end_deserialize;
        45852  +  }
        45853  +  p = memdbFromDbSchema(db, zSchema);
        45854  +  if( p==0 ){
        45855  +    rc = SQLITE_ERROR;
        45856  +  }else{
        45857  +    p->aData = pData;
        45858  +    p->sz = szDb;
        45859  +    p->szMax = szBuf;
        45860  +    p->mFlags = mFlags;
        45861  +    rc = SQLITE_OK;
        45862  +  }
        45863  +
        45864  +end_deserialize:
        45865  +  sqlite3_finalize(pStmt);
        45866  +  sqlite3_mutex_leave(db->mutex);
        45867  +  return rc;
        45868  +}
        45869  +
        45870  +/* 
        45871  +** This routine is called when the extension is loaded.
        45872  +** Register the new VFS.
        45873  +*/
        45874  +SQLITE_PRIVATE int sqlite3MemdbInit(void){
        45875  +  sqlite3_vfs *pLower = sqlite3_vfs_find(0);
        45876  +  int sz = pLower->szOsFile;
        45877  +  memdb_vfs.pAppData = pLower;
        45878  +  /* In all known configurations of SQLite, the size of a default
        45879  +  ** sqlite3_file is greater than the size of a memdb sqlite3_file.
        45880  +  ** Should that ever change, remove the following NEVER() */
        45881  +  if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
        45882  +  memdb_vfs.szOsFile = sz;
        45883  +  return sqlite3_vfs_register(&memdb_vfs, 0);
        45884  +}
        45885  +#endif /* SQLITE_ENABLE_DESERIALIZE */
        45886  +
        45887  +/************** End of memdb.c ***********************************************/
 44764  45888   /************** Begin file bitvec.c ******************************************/
 44765  45889   /*
 44766  45890   ** 2008 February 16
 44767  45891   **
 44768  45892   ** The author disclaims copyright to this source code.  In place of
 44769  45893   ** a legal notice, here is a blessing:
 44770  45894   **
................................................................................
 45605  46729       }
 45606  46730       if( pPg ){
 45607  46731         int rc;
 45608  46732   #ifdef SQLITE_LOG_CACHE_SPILL
 45609  46733         sqlite3_log(SQLITE_FULL, 
 45610  46734                     "spill page %d making room for %d - cache used: %d/%d",
 45611  46735                     pPg->pgno, pgno,
 45612         -                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
        46736  +                  sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
 45613  46737                   numberOfCachePages(pCache));
 45614  46738   #endif
 45615  46739         pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
 45616  46740         rc = pCache->xStress(pCache->pStress, pPg);
 45617  46741         pcacheDump(pCache);
 45618  46742         if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
 45619  46743           return rc;
................................................................................
 48661  49785     int pageSize;               /* Number of bytes in a page */
 48662  49786     Pgno mxPgno;                /* Maximum allowed size of the database */
 48663  49787     i64 journalSizeLimit;       /* Size limit for persistent journal files */
 48664  49788     char *zFilename;            /* Name of the database file */
 48665  49789     char *zJournal;             /* Name of the journal file */
 48666  49790     int (*xBusyHandler)(void*); /* Function to call when busy */
 48667  49791     void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
 48668         -  int aStat[3];               /* Total cache hits, misses and writes */
        49792  +  int aStat[4];               /* Total cache hits, misses, writes, spills */
 48669  49793   #ifdef SQLITE_TEST
 48670  49794     int nRead;                  /* Database pages read */
 48671  49795   #endif
 48672  49796     void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
 48673  49797     int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
 48674  49798   #ifdef SQLITE_HAS_CODEC
 48675  49799     void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
................................................................................
 48689  49813   ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
 48690  49814   ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS 
 48691  49815   ** or CACHE_WRITE to sqlite3_db_status().
 48692  49816   */
 48693  49817   #define PAGER_STAT_HIT   0
 48694  49818   #define PAGER_STAT_MISS  1
 48695  49819   #define PAGER_STAT_WRITE 2
        49820  +#define PAGER_STAT_SPILL 3
 48696  49821   
 48697  49822   /*
 48698  49823   ** The following global variables hold counters used for
 48699  49824   ** testing purposes only.  These variables do not exist in
 48700  49825   ** a non-testing build.  These variables are not thread-safe.
 48701  49826   */
 48702  49827   #ifdef SQLITE_TEST
................................................................................
 49175  50300     assert( isOpen(pPager->fd) );
 49176  50301     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
 49177  50302   #else
 49178  50303     UNUSED_PARAMETER(pPager);
 49179  50304   #endif
 49180  50305   
 49181  50306   #ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
 49182         -  if( dc&SQLITE_IOCAP_BATCH_ATOMIC ){
        50307  +  if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
 49183  50308       return -1;
 49184  50309     }
 49185  50310   #endif
 49186  50311   
 49187  50312   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 49188  50313     {
 49189  50314       int nSector = pPager->sectorSize;
................................................................................
 50091  51216       ** successfully committed, but the EXCLUSIVE lock is still held on the
 50092  51217       ** file. So it is safe to truncate the database file to its minimum
 50093  51218       ** required size.  */
 50094  51219       assert( pPager->eLock==EXCLUSIVE_LOCK );
 50095  51220       rc = pager_truncate(pPager, pPager->dbSize);
 50096  51221     }
 50097  51222   
 50098         -  if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
        51223  +  if( rc==SQLITE_OK && bCommit ){
 50099  51224       rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
 50100  51225       if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 50101  51226     }
 50102  51227   
 50103  51228     if( !pPager->exclusiveMode 
 50104  51229      && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
 50105  51230     ){
................................................................................
 50910  52035     }
 50911  52036     /* Following a rollback, the database file should be back in its original
 50912  52037     ** state prior to the start of the transaction, so invoke the
 50913  52038     ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
 50914  52039     ** assertion that the transaction counter was modified.
 50915  52040     */
 50916  52041   #ifdef SQLITE_DEBUG
 50917         -  if( pPager->fd->pMethods ){
 50918         -    sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
 50919         -  }
        52042  +  sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
 50920  52043   #endif
 50921  52044   
 50922  52045     /* If this playback is happening automatically as a result of an IO or 
 50923  52046     ** malloc error that occurred after the change-counter was updated but 
 50924  52047     ** before the transaction was committed, then the change-counter 
 50925  52048     ** modification may just have been reverted. If this happens in exclusive 
 50926  52049     ** mode, then subsequent transactions performed by the connection will not
................................................................................
 51665  52788   **   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
 51666  52789   **   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
 51667  52790   **
 51668  52791   ** If the busy-handler callback returns non-zero, the lock is 
 51669  52792   ** retried. If it returns zero, then the SQLITE_BUSY error is
 51670  52793   ** returned to the caller of the pager API function.
 51671  52794   */
 51672         -SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
        52795  +SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
 51673  52796     Pager *pPager,                       /* Pager object */
 51674  52797     int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
 51675  52798     void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
 51676  52799   ){
        52800  +  void **ap;
 51677  52801     pPager->xBusyHandler = xBusyHandler;
 51678  52802     pPager->pBusyHandlerArg = pBusyHandlerArg;
 51679         -
 51680         -  if( isOpen(pPager->fd) ){
 51681         -    void **ap = (void **)&pPager->xBusyHandler;
 51682         -    assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
 51683         -    assert( ap[1]==pBusyHandlerArg );
 51684         -    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
 51685         -  }
        52803  +  ap = (void **)&pPager->xBusyHandler;
        52804  +  assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
        52805  +  assert( ap[1]==pBusyHandlerArg );
        52806  +  sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
 51686  52807   }
 51687  52808   
 51688  52809   /*
 51689  52810   ** Change the page size used by the Pager object. The new page size 
 51690  52811   ** is passed in *pPageSize.
 51691  52812   **
 51692  52813   ** If the pager is in the error state when this function is called, it
................................................................................
 52064  53185     PgHdr *pNext;
 52065  53186     for(p=pPager->pMmapFreelist; p; p=pNext){
 52066  53187       pNext = p->pDirty;
 52067  53188       sqlite3_free(p);
 52068  53189     }
 52069  53190   }
 52070  53191   
        53192  +/* Verify that the database file has not be deleted or renamed out from
        53193  +** under the pager.  Return SQLITE_OK if the database is still where it ought
        53194  +** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
        53195  +** code from sqlite3OsAccess()) if the database has gone missing.
        53196  +*/
        53197  +static int databaseIsUnmoved(Pager *pPager){
        53198  +  int bHasMoved = 0;
        53199  +  int rc;
        53200  +
        53201  +  if( pPager->tempFile ) return SQLITE_OK;
        53202  +  if( pPager->dbSize==0 ) return SQLITE_OK;
        53203  +  assert( pPager->zFilename && pPager->zFilename[0] );
        53204  +  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
        53205  +  if( rc==SQLITE_NOTFOUND ){
        53206  +    /* If the HAS_MOVED file-control is unimplemented, assume that the file
        53207  +    ** has not been moved.  That is the historical behavior of SQLite: prior to
        53208  +    ** version 3.8.3, it never checked */
        53209  +    rc = SQLITE_OK;
        53210  +  }else if( rc==SQLITE_OK && bHasMoved ){
        53211  +    rc = SQLITE_READONLY_DBMOVED;
        53212  +  }
        53213  +  return rc;
        53214  +}
        53215  +
 52071  53216   
 52072  53217   /*
 52073  53218   ** Shutdown the page cache.  Free all memory and close all files.
 52074  53219   **
 52075  53220   ** If a transaction was in progress when this routine is called, that
 52076  53221   ** transaction is rolled back.  All outstanding pages are invalidated
 52077  53222   ** and their memory is freed.  Any attempt to use a page associated
................................................................................
 52080  53225   **
 52081  53226   ** This function always succeeds. If a transaction is active an attempt
 52082  53227   ** is made to roll it back. If an error occurs during the rollback 
 52083  53228   ** a hot journal may be left in the filesystem but no error is returned
 52084  53229   ** to the caller.
 52085  53230   */
 52086  53231   SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
 52087         -  u8 *pTmp = (u8 *)pPager->pTmpSpace;
 52088         -
        53232  +  u8 *pTmp = (u8*)pPager->pTmpSpace;
 52089  53233     assert( db || pagerUseWal(pPager)==0 );
 52090  53234     assert( assert_pager_state(pPager) );
 52091  53235     disable_simulated_io_errors();
 52092  53236     sqlite3BeginBenignMalloc();
 52093  53237     pagerFreeMapHdrs(pPager);
 52094  53238     /* pPager->errCode = 0; */
 52095  53239     pPager->exclusiveMode = 0;
 52096  53240   #ifndef SQLITE_OMIT_WAL
 52097         -  assert( db || pPager->pWal==0 );
 52098         -  sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,
 52099         -      (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp)
 52100         -  );
 52101         -  pPager->pWal = 0;
        53241  +  {
        53242  +    u8 *a = 0;
        53243  +    assert( db || pPager->pWal==0 );
        53244  +    if( db && 0==(db->flags & SQLITE_NoCkptOnClose) 
        53245  +     && SQLITE_OK==databaseIsUnmoved(pPager)
        53246  +    ){
        53247  +      a = pTmp;
        53248  +    }
        53249  +    sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
        53250  +    pPager->pWal = 0;
        53251  +  }
 52102  53252   #endif
 52103  53253     pager_reset(pPager);
 52104  53254     if( MEMDB ){
 52105  53255       pager_unlock(pPager);
 52106  53256     }else{
 52107  53257       /* If it is open, sync the journal file before calling UnlockAndRollback.
 52108  53258       ** If this is not done, then an unsynced portion of the open journal 
................................................................................
 52551  53701     if( pPager->doNotSpill
 52552  53702      && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
 52553  53703         || (pPg->flags & PGHDR_NEED_SYNC)!=0)
 52554  53704     ){
 52555  53705       return SQLITE_OK;
 52556  53706     }
 52557  53707   
        53708  +  pPager->aStat[PAGER_STAT_SPILL]++;
 52558  53709     pPg->pDirty = 0;
 52559  53710     if( pagerUseWal(pPager) ){
 52560  53711       /* Write a single frame for this page to the log. */
 52561  53712       rc = subjournalPageIfRequired(pPg); 
 52562  53713       if( rc==SQLITE_OK ){
 52563  53714         rc = pagerWalFrames(pPager, pPg, 0, 0);
 52564  53715       }
................................................................................
 52656  53807     void (*xReinit)(DbPage*) /* Function to reinitialize pages */
 52657  53808   ){
 52658  53809     u8 *pPtr;
 52659  53810     Pager *pPager = 0;       /* Pager object to allocate and return */
 52660  53811     int rc = SQLITE_OK;      /* Return code */
 52661  53812     int tempFile = 0;        /* True for temp files (incl. in-memory files) */
 52662  53813     int memDb = 0;           /* True if this is an in-memory file */
        53814  +#ifdef SQLITE_ENABLE_DESERIALIZE
        53815  +  int memJM = 0;           /* Memory journal mode */
        53816  +#else
        53817  +# define memJM 0
        53818  +#endif
 52663  53819     int readOnly = 0;        /* True if this is a read-only file */
 52664  53820     int journalFileSize;     /* Bytes to allocate for each journal fd */
 52665  53821     char *zPathname = 0;     /* Full path to database file */
 52666  53822     int nPathname = 0;       /* Number of bytes in zPathname */
 52667  53823     int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
 52668  53824     int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
 52669  53825     u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
................................................................................
 52783  53939   
 52784  53940     /* Open the pager file.
 52785  53941     */
 52786  53942     if( zFilename && zFilename[0] ){
 52787  53943       int fout = 0;                    /* VFS flags returned by xOpen() */
 52788  53944       rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
 52789  53945       assert( !memDb );
 52790         -    readOnly = (fout&SQLITE_OPEN_READONLY);
        53946  +#ifdef SQLITE_ENABLE_DESERIALIZE
        53947  +    memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
        53948  +#endif
        53949  +    readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
 52791  53950   
 52792  53951       /* If the file was successfully opened for read/write access,
 52793  53952       ** choose a default page size in case we have to create the
 52794  53953       ** database file. The default page size is the maximum of:
 52795  53954       **
 52796  53955       **    + SQLITE_DEFAULT_PAGE_SIZE,
 52797  53956       **    + The value returned by sqlite3OsSectorSize()
................................................................................
 52914  54073     /* pPager->pLast = 0; */
 52915  54074     pPager->nExtra = (u16)nExtra;
 52916  54075     pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
 52917  54076     assert( isOpen(pPager->fd) || tempFile );
 52918  54077     setSectorSize(pPager);
 52919  54078     if( !useJournal ){
 52920  54079       pPager->journalMode = PAGER_JOURNALMODE_OFF;
 52921         -  }else if( memDb ){
        54080  +  }else if( memDb || memJM ){
 52922  54081       pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
 52923  54082     }
 52924  54083     /* pPager->xBusyHandler = 0; */
 52925  54084     /* pPager->pBusyHandlerArg = 0; */
 52926  54085     pPager->xReiniter = xReinit;
 52927  54086     setGetterMethod(pPager);
 52928  54087     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
................................................................................
 52929  54088     /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
 52930  54089   
 52931  54090     *ppPager = pPager;
 52932  54091     return SQLITE_OK;
 52933  54092   }
 52934  54093   
 52935  54094   
 52936         -/* Verify that the database file has not be deleted or renamed out from
 52937         -** under the pager.  Return SQLITE_OK if the database is still were it ought
 52938         -** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
 52939         -** code from sqlite3OsAccess()) if the database has gone missing.
 52940         -*/
 52941         -static int databaseIsUnmoved(Pager *pPager){
 52942         -  int bHasMoved = 0;
 52943         -  int rc;
 52944         -
 52945         -  if( pPager->tempFile ) return SQLITE_OK;
 52946         -  if( pPager->dbSize==0 ) return SQLITE_OK;
 52947         -  assert( pPager->zFilename && pPager->zFilename[0] );
 52948         -  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
 52949         -  if( rc==SQLITE_NOTFOUND ){
 52950         -    /* If the HAS_MOVED file-control is unimplemented, assume that the file
 52951         -    ** has not been moved.  That is the historical behavior of SQLite: prior to
 52952         -    ** version 3.8.3, it never checked */
 52953         -    rc = SQLITE_OK;
 52954         -  }else if( rc==SQLITE_OK && bHasMoved ){
 52955         -    rc = SQLITE_READONLY_DBMOVED;
 52956         -  }
 52957         -  return rc;
 52958         -}
 52959         -
 52960  54095   
 52961  54096   /*
 52962  54097   ** This function is called after transitioning from PAGER_UNLOCK to
 52963  54098   ** PAGER_SHARED state. It tests if there is a hot journal present in
 52964  54099   ** the file-system for the given pager. A hot journal is one that 
 52965  54100   ** needs to be played back. According to this function, a hot-journal
 52966  54101   ** file exists if the following criteria are met:
................................................................................
 53640  54775   }
 53641  54776   SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
 53642  54777     Pager *pPager;
 53643  54778     assert( pPg!=0 );
 53644  54779     assert( pPg->pgno==1 );
 53645  54780     assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
 53646  54781     pPager = pPg->pPager;
        54782  +  sqlite3PagerResetLockTimeout(pPager);
 53647  54783     sqlite3PcacheRelease(pPg);
 53648  54784     pagerUnlockIfUnused(pPager);
 53649  54785   }
 53650  54786   
 53651  54787   /*
 53652  54788   ** This function is called at the start of every write transaction.
 53653  54789   ** There must already be a RESERVED or EXCLUSIVE lock on the database 
................................................................................
 54235  55371   ** or pages with the Pager.noSync flag set.
 54236  55372   **
 54237  55373   ** If successful, or if called on a pager for which it is a no-op, this
 54238  55374   ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
 54239  55375   */
 54240  55376   SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
 54241  55377     int rc = SQLITE_OK;
 54242         -
 54243         -  if( isOpen(pPager->fd) ){
 54244         -    void *pArg = (void*)zMaster;
 54245         -    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
 54246         -    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 54247         -  }
        55378  +  void *pArg = (void*)zMaster;
        55379  +  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
        55380  +  if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 54248  55381     if( rc==SQLITE_OK && !pPager->noSync ){
 54249  55382       assert( !MEMDB );
 54250  55383       rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
 54251  55384     }
 54252  55385     return rc;
 54253  55386   }
 54254  55387   
................................................................................
 54461  55594           rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
 54462  55595           if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 54463  55596         }
 54464  55597         rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
 54465  55598         if( bBatch ){
 54466  55599           if( rc==SQLITE_OK ){
 54467  55600             rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
 54468         -        }else{
 54469         -          sqlite3OsFileControl(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
        55601  +        }
        55602  +        if( rc!=SQLITE_OK ){
        55603  +          sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
 54470  55604           }
 54471  55605         }
 54472  55606   
 54473  55607         if( rc!=SQLITE_OK ){
 54474  55608           assert( rc!=SQLITE_IOERR_BLOCKED );
 54475  55609           goto commit_phase_one_exit;
 54476  55610         }
................................................................................
 54686  55820     a[9] = pPager->nRead;
 54687  55821     a[10] = pPager->aStat[PAGER_STAT_WRITE];
 54688  55822     return a;
 54689  55823   }
 54690  55824   #endif
 54691  55825   
 54692  55826   /*
 54693         -** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
 54694         -** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
        55827  +** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
        55828  +** or _WRITE+1.  The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
        55829  +** of SQLITE_DBSTATUS_CACHE_SPILL.  The _SPILL case is not contiguous because
        55830  +** it was added later.
        55831  +**
        55832  +** Before returning, *pnVal is incremented by the
 54695  55833   ** current cache hit or miss count, according to the value of eStat. If the 
 54696  55834   ** reset parameter is non-zero, the cache hit or miss count is zeroed before 
 54697  55835   ** returning.
 54698  55836   */
 54699  55837   SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
 54700  55838   
 54701  55839     assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
 54702  55840          || eStat==SQLITE_DBSTATUS_CACHE_MISS
 54703  55841          || eStat==SQLITE_DBSTATUS_CACHE_WRITE
        55842  +       || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
 54704  55843     );
 54705  55844   
 54706  55845     assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
 54707  55846     assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
 54708         -  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
        55847  +  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
        55848  +           && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
 54709  55849   
 54710         -  *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
        55850  +  eStat -= SQLITE_DBSTATUS_CACHE_HIT;
        55851  +  *pnVal += pPager->aStat[eStat];
 54711  55852     if( reset ){
 54712         -    pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
        55853  +    pPager->aStat[eStat] = 0;
 54713  55854     }
 54714  55855   }
 54715  55856   
 54716  55857   /*
 54717  55858   ** Return true if this is an in-memory or temp-file backed pager.
 54718  55859   */
 54719  55860   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
................................................................................
 54908  56049   ** Return the file handle for the database file associated
 54909  56050   ** with the pager.  This might return NULL if the file has
 54910  56051   ** not yet been opened.
 54911  56052   */
 54912  56053   SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
 54913  56054     return pPager->fd;
 54914  56055   }
        56056  +
        56057  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        56058  +/*
        56059  +** Reset the lock timeout for pager.
        56060  +*/
        56061  +SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){
        56062  +  int x = 0;
        56063  +  sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x);
        56064  +}
        56065  +#endif
 54915  56066   
 54916  56067   /*
 54917  56068   ** Return the file handle for the journal file (if it exists).
 54918  56069   ** This will be either the rollback journal or the WAL file.
 54919  56070   */
 54920  56071   SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
 54921  56072   #if SQLITE_OMIT_WAL
................................................................................
 55369  56520     if( pPager->pWal ){
 55370  56521       rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
 55371  56522           (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
 55372  56523           pPager->pBusyHandlerArg,
 55373  56524           pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
 55374  56525           pnLog, pnCkpt
 55375  56526       );
        56527  +    sqlite3PagerResetLockTimeout(pPager);
 55376  56528     }
 55377  56529     return rc;
 55378  56530   }
 55379  56531   
 55380  56532   SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
 55381  56533     return sqlite3WalCallback(pPager->pWal);
 55382  56534   }
................................................................................
 56151  57303   ** so.  It is safe to enlarge the wal-index if pWal->writeLock is true
 56152  57304   ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
 56153  57305   **
 56154  57306   ** If this call is successful, *ppPage is set to point to the wal-index
 56155  57307   ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
 56156  57308   ** then an SQLite error code is returned and *ppPage is set to 0.
 56157  57309   */
 56158         -static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
        57310  +static SQLITE_NOINLINE int walIndexPageRealloc(
        57311  +  Wal *pWal,               /* The WAL context */
        57312  +  int iPage,               /* The page we seek */
        57313  +  volatile u32 **ppPage    /* Write the page pointer here */
        57314  +){
 56159  57315     int rc = SQLITE_OK;
 56160  57316   
 56161  57317     /* Enlarge the pWal->apWiData[] array if required */
 56162  57318     if( pWal->nWiData<=iPage ){
 56163  57319       int nByte = sizeof(u32*)*(iPage+1);
 56164  57320       volatile u32 **apNew;
 56165  57321       apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
................................................................................
 56170  57326       memset((void*)&apNew[pWal->nWiData], 0,
 56171  57327              sizeof(u32*)*(iPage+1-pWal->nWiData));
 56172  57328       pWal->apWiData = apNew;
 56173  57329       pWal->nWiData = iPage+1;
 56174  57330     }
 56175  57331   
 56176  57332     /* Request a pointer to the required page from the VFS */
 56177         -  if( pWal->apWiData[iPage]==0 ){
 56178         -    if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
 56179         -      pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
 56180         -      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
 56181         -    }else{
 56182         -      rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
 56183         -          pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
 56184         -      );
 56185         -      assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
 56186         -      testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
 56187         -      if( (rc&0xff)==SQLITE_READONLY ){
 56188         -        pWal->readOnly |= WAL_SHM_RDONLY;
 56189         -        if( rc==SQLITE_READONLY ){
 56190         -          rc = SQLITE_OK;
 56191         -        }
        57333  +  assert( pWal->apWiData[iPage]==0 );
        57334  +  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
        57335  +    pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
        57336  +    if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
        57337  +  }else{
        57338  +    rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
        57339  +        pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
        57340  +    );
        57341  +    assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
        57342  +    testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
        57343  +    if( (rc&0xff)==SQLITE_READONLY ){
        57344  +      pWal->readOnly |= WAL_SHM_RDONLY;
        57345  +      if( rc==SQLITE_READONLY ){
        57346  +        rc = SQLITE_OK;
 56192  57347         }
 56193  57348       }
 56194  57349     }
 56195  57350   
 56196  57351     *ppPage = pWal->apWiData[iPage];
 56197  57352     assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
 56198  57353     return rc;
        57354  +}
        57355  +static int walIndexPage(
        57356  +  Wal *pWal,               /* The WAL context */
        57357  +  int iPage,               /* The page we seek */
        57358  +  volatile u32 **ppPage    /* Write the page pointer here */
        57359  +){
        57360  +  if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
        57361  +    return walIndexPageRealloc(pWal, iPage, ppPage);
        57362  +  }
        57363  +  return SQLITE_OK;
 56199  57364   }
 56200  57365   
 56201  57366   /*
 56202  57367   ** Return a pointer to the WalCkptInfo structure in the wal-index.
 56203  57368   */
 56204  57369   static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
 56205  57370     assert( pWal->nWiData>0 && pWal->apWiData[0] );
................................................................................
 57169  58334   */
 57170  58335   static void walIteratorFree(WalIterator *p){
 57171  58336     sqlite3_free(p);
 57172  58337   }
 57173  58338   
 57174  58339   /*
 57175  58340   ** Construct a WalInterator object that can be used to loop over all 
 57176         -** pages in the WAL in ascending order. The caller must hold the checkpoint
 57177         -** lock.
        58341  +** pages in the WAL following frame nBackfill in ascending order. Frames
        58342  +** nBackfill or earlier may be included - excluding them is an optimization
        58343  +** only. The caller must hold the checkpoint lock.
 57178  58344   **
 57179  58345   ** On success, make *pp point to the newly allocated WalInterator object
 57180  58346   ** return SQLITE_OK. Otherwise, return an error code. If this routine
 57181  58347   ** returns an error, the value of *pp is undefined.
 57182  58348   **
 57183  58349   ** The calling routine should invoke walIteratorFree() to destroy the
 57184  58350   ** WalIterator object when it has finished with it.
 57185  58351   */
 57186         -static int walIteratorInit(Wal *pWal, WalIterator **pp){
        58352  +static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
 57187  58353     WalIterator *p;                 /* Return value */
 57188  58354     int nSegment;                   /* Number of segments to merge */
 57189  58355     u32 iLast;                      /* Last frame in log */
 57190  58356     int nByte;                      /* Number of bytes to allocate */
 57191  58357     int i;                          /* Iterator variable */
 57192  58358     ht_slot *aTmp;                  /* Temp space used by merge-sort */
 57193  58359     int rc = SQLITE_OK;             /* Return Code */
................................................................................
 57216  58382     aTmp = (ht_slot *)sqlite3_malloc64(
 57217  58383         sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
 57218  58384     );
 57219  58385     if( !aTmp ){
 57220  58386       rc = SQLITE_NOMEM_BKPT;
 57221  58387     }
 57222  58388   
 57223         -  for(i=0; rc==SQLITE_OK && i<nSegment; i++){
        58389  +  for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
 57224  58390       volatile ht_slot *aHash;
 57225  58391       u32 iZero;
 57226  58392       volatile u32 *aPgno;
 57227  58393   
 57228  58394       rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
 57229  58395       if( rc==SQLITE_OK ){
 57230  58396         int j;                      /* Counter variable */
................................................................................
 57250  58416         p->aSegment[i].aPgno = (u32 *)aPgno;
 57251  58417       }
 57252  58418     }
 57253  58419     sqlite3_free(aTmp);
 57254  58420   
 57255  58421     if( rc!=SQLITE_OK ){
 57256  58422       walIteratorFree(p);
        58423  +    p = 0;
 57257  58424     }
 57258  58425     *pp = p;
 57259  58426     return rc;
 57260  58427   }
 57261  58428   
 57262  58429   /*
 57263  58430   ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
................................................................................
 57372  58539   
 57373  58540     szPage = walPagesize(pWal);
 57374  58541     testcase( szPage<=32768 );
 57375  58542     testcase( szPage>=65536 );
 57376  58543     pInfo = walCkptInfo(pWal);
 57377  58544     if( pInfo->nBackfill<pWal->hdr.mxFrame ){
 57378  58545   
 57379         -    /* Allocate the iterator */
 57380         -    rc = walIteratorInit(pWal, &pIter);
 57381         -    if( rc!=SQLITE_OK ){
 57382         -      return rc;
 57383         -    }
 57384         -    assert( pIter );
 57385         -
 57386  58546       /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
 57387  58547       ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
 57388  58548       assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
 57389  58549   
 57390  58550       /* Compute in mxSafeFrame the index of the last frame of the WAL that is
 57391  58551       ** safe to write into the database.  Frames beyond mxSafeFrame might
 57392  58552       ** overwrite database pages that are in use by active readers and thus
................................................................................
 57415  58575             xBusy = 0;
 57416  58576           }else{
 57417  58577             goto walcheckpoint_out;
 57418  58578           }
 57419  58579         }
 57420  58580       }
 57421  58581   
 57422         -    if( pInfo->nBackfill<mxSafeFrame
        58582  +    /* Allocate the iterator */
        58583  +    if( pInfo->nBackfill<mxSafeFrame ){
        58584  +      rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
        58585  +      assert( rc==SQLITE_OK || pIter==0 );
        58586  +    }
        58587  +
        58588  +    if( pIter
 57423  58589        && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
 57424  58590       ){
 57425  58591         i64 nSize;                    /* Current size of database file */
 57426  58592         u32 nBackfill = pInfo->nBackfill;
 57427  58593   
 57428  58594         pInfo->nBackfillAttempted = mxSafeFrame;
 57429  58595   
................................................................................
 58465  59631     **     This condition filters out normal hash-table collisions.
 58466  59632     **
 58467  59633     **   (iFrame<=iLast): 
 58468  59634     **     This condition filters out entries that were added to the hash
 58469  59635     **     table after the current read-transaction had started.
 58470  59636     */
 58471  59637     iMinHash = walFramePage(pWal->minFrame);
 58472         -  for(iHash=walFramePage(iLast); iHash>=iMinHash && iRead==0; iHash--){
        59638  +  for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
 58473  59639       volatile ht_slot *aHash;      /* Pointer to hash table */
 58474  59640       volatile u32 *aPgno;          /* Pointer to array of page numbers */
 58475  59641       u32 iZero;                    /* Frame number corresponding to aPgno[0] */
 58476  59642       int iKey;                     /* Hash slot index */
 58477  59643       int nCollide;                 /* Number of hash collisions remaining */
 58478  59644       int rc;                       /* Error code */
 58479  59645   
................................................................................
 58488  59654           assert( iFrame>iRead || CORRUPT_DB );
 58489  59655           iRead = iFrame;
 58490  59656         }
 58491  59657         if( (nCollide--)==0 ){
 58492  59658           return SQLITE_CORRUPT_BKPT;
 58493  59659         }
 58494  59660       }
        59661  +    if( iRead ) break;
 58495  59662     }
 58496  59663   
 58497  59664   #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 58498  59665     /* If expensive assert() statements are available, do a linear search
 58499  59666     ** of the wal-index file content. Make sure the results agree with the
 58500  59667     ** result obtained using the hash indexes above.  */
 58501  59668     {
................................................................................
 59902  61069   **    eState==FAULT:                   Cursor fault with skipNext as error code.
 59903  61070   */
 59904  61071   struct BtCursor {
 59905  61072     u8 eState;                /* One of the CURSOR_XXX constants (see below) */
 59906  61073     u8 curFlags;              /* zero or more BTCF_* flags defined below */
 59907  61074     u8 curPagerFlags;         /* Flags to send to sqlite3PagerGet() */
 59908  61075     u8 hints;                 /* As configured by CursorSetHints() */
 59909         -  int nOvflAlloc;           /* Allocated size of aOverflow[] array */
 59910         -  Btree *pBtree;            /* The Btree to which this cursor belongs */
 59911         -  BtShared *pBt;            /* The BtShared this cursor points to */
 59912         -  BtCursor *pNext;          /* Forms a linked list of all cursors */
 59913         -  Pgno *aOverflow;          /* Cache of overflow page locations */
 59914         -  CellInfo info;            /* A parse of the cell we are pointing at */
 59915         -  i64 nKey;                 /* Size of pKey, or last integer key */
 59916         -  void *pKey;               /* Saved key that was cursor last known position */
 59917         -  Pgno pgnoRoot;            /* The root page of this tree */
 59918  61076     int skipNext;    /* Prev() is noop if negative. Next() is noop if positive.
 59919  61077                      ** Error code if eState==CURSOR_FAULT */
        61078  +  Btree *pBtree;            /* The Btree to which this cursor belongs */
        61079  +  Pgno *aOverflow;          /* Cache of overflow page locations */
        61080  +  void *pKey;               /* Saved key that was cursor last known position */
 59920  61081     /* All fields above are zeroed when the cursor is allocated.  See
 59921  61082     ** sqlite3BtreeCursorZero().  Fields that follow must be manually
 59922  61083     ** initialized. */
        61084  +#define BTCURSOR_FIRST_UNINIT pBt   /* Name of first uninitialized field */
        61085  +  BtShared *pBt;            /* The BtShared this cursor points to */
        61086  +  BtCursor *pNext;          /* Forms a linked list of all cursors */
        61087  +  CellInfo info;            /* A parse of the cell we are pointing at */
        61088  +  i64 nKey;                 /* Size of pKey, or last integer key */
        61089  +  Pgno pgnoRoot;            /* The root page of this tree */
 59923  61090     i8 iPage;                 /* Index of current page in apPage */
 59924  61091     u8 curIntKey;             /* Value of apPage[0]->intKey */
 59925  61092     u16 ix;                   /* Current index for apPage[iPage] */
 59926  61093     u16 aiIdx[BTCURSOR_MAX_DEPTH-1];     /* Current index in apPage[i] */
 59927  61094     struct KeyInfo *pKeyInfo;            /* Arg passed to comparison function */
 59928  61095     MemPage *pPage;                        /* Current page */
 59929  61096     MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
................................................................................
 59965  61132   ** CURSOR_FAULT:
 59966  61133   **   An unrecoverable error (an I/O error or a malloc failure) has occurred
 59967  61134   **   on a different connection that shares the BtShared cache with this
 59968  61135   **   cursor.  The error has left the cache in an inconsistent state.
 59969  61136   **   Do nothing else with this cursor.  Any attempt to use the cursor
 59970  61137   **   should return the error code stored in BtCursor.skipNext
 59971  61138   */
 59972         -#define CURSOR_INVALID           0
 59973         -#define CURSOR_VALID             1
        61139  +#define CURSOR_VALID             0
        61140  +#define CURSOR_INVALID           1
 59974  61141   #define CURSOR_SKIPNEXT          2
 59975  61142   #define CURSOR_REQUIRESEEK       3
 59976  61143   #define CURSOR_FAULT             4
 59977  61144   
 59978  61145   /* 
 59979  61146   ** The database page the PENDING_BYTE occupies. This page is never used.
 59980  61147   */
................................................................................
 62630  63797   /*
 62631  63798   ** Invoke the busy handler for a btree.
 62632  63799   */
 62633  63800   static int btreeInvokeBusyHandler(void *pArg){
 62634  63801     BtShared *pBt = (BtShared*)pArg;
 62635  63802     assert( pBt->db );
 62636  63803     assert( sqlite3_mutex_held(pBt->db->mutex) );
 62637         -  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
        63804  +  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
        63805  +                                  sqlite3PagerFile(pBt->pPager));
 62638  63806   }
 62639  63807   
 62640  63808   /*
 62641  63809   ** Open a database file.
 62642  63810   ** 
 62643  63811   ** zFilename is the name of the database file.  If zFilename is NULL
 62644  63812   ** then an ephemeral database is created.  The ephemeral database might
................................................................................
 62808  63976         rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
 62809  63977       }
 62810  63978       if( rc!=SQLITE_OK ){
 62811  63979         goto btree_open_out;
 62812  63980       }
 62813  63981       pBt->openFlags = (u8)flags;
 62814  63982       pBt->db = db;
 62815         -    sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
        63983  +    sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
 62816  63984       p->pBt = pBt;
 62817  63985     
 62818  63986       pBt->pCursor = 0;
 62819  63987       pBt->pPage1 = 0;
 62820  63988       if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
 62821  63989   #if defined(SQLITE_SECURE_DELETE)
 62822  63990       pBt->btsFlags |= BTS_SECURE_DELETE;
................................................................................
 63771  64939       }
 63772  64940     
 63773  64941       if( rc!=SQLITE_OK ){
 63774  64942         unlockBtreeIfUnused(pBt);
 63775  64943       }
 63776  64944     }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
 63777  64945             btreeInvokeBusyHandler(pBt) );
        64946  +  sqlite3PagerResetLockTimeout(pBt->pPager);
 63778  64947   
 63779  64948     if( rc==SQLITE_OK ){
 63780  64949       if( p->inTrans==TRANS_NONE ){
 63781  64950         pBt->nTransaction++;
 63782  64951   #ifndef SQLITE_OMIT_SHARED_CACHE
 63783  64952         if( p->sharable ){
 63784  64953           assert( p->lock.pBtree==p && p->lock.iTable==1 );
................................................................................
 64744  65913   **
 64745  65914   ** The simple approach here would be to memset() the entire object
 64746  65915   ** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
 64747  65916   ** do not need to be zeroed and they are large, so we can save a lot
 64748  65917   ** of run-time by skipping the initialization of those elements.
 64749  65918   */
 64750  65919   SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
 64751         -  memset(p, 0, offsetof(BtCursor, iPage));
        65920  +  memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
 64752  65921   }
 64753  65922   
 64754  65923   /*
 64755  65924   ** Close a cursor.  The read lock on the database file is released
 64756  65925   ** when the last cursor is closed.
 64757  65926   */
 64758  65927   SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
................................................................................
 64787  65956   ** BtCursor.info structure.  If it is not already valid, call
 64788  65957   ** btreeParseCell() to fill it in.
 64789  65958   **
 64790  65959   ** BtCursor.info is a cache of the information in the current cell.
 64791  65960   ** Using this cache reduces the number of calls to btreeParseCell().
 64792  65961   */
 64793  65962   #ifndef NDEBUG
        65963  +  static int cellInfoEqual(CellInfo *a, CellInfo *b){
        65964  +    if( a->nKey!=b->nKey ) return 0;
        65965  +    if( a->pPayload!=b->pPayload ) return 0;
        65966  +    if( a->nPayload!=b->nPayload ) return 0;
        65967  +    if( a->nLocal!=b->nLocal ) return 0;
        65968  +    if( a->nSize!=b->nSize ) return 0;
        65969  +    return 1;
        65970  +  }
 64794  65971     static void assertCellInfo(BtCursor *pCur){
 64795  65972       CellInfo info;
 64796  65973       memset(&info, 0, sizeof(info));
 64797  65974       btreeParseCell(pCur->pPage, pCur->ix, &info);
 64798         -    assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
        65975  +    assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
 64799  65976     }
 64800  65977   #else
 64801  65978     #define assertCellInfo(x)
 64802  65979   #endif
 64803  65980   static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
 64804  65981     if( pCur->info.nSize==0 ){
 64805  65982       pCur->curFlags |= BTCF_ValidNKey;
................................................................................
 65067  66244       ** The aOverflow[] array is sized at one entry for each overflow page
 65068  66245       ** in the overflow chain. The page number of the first overflow page is
 65069  66246       ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
 65070  66247       ** means "not yet known" (the cache is lazily populated).
 65071  66248       */
 65072  66249       if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
 65073  66250         int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
 65074         -      if( nOvfl>pCur->nOvflAlloc ){
        66251  +      if( pCur->aOverflow==0
        66252  +       || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
        66253  +      ){
 65075  66254           Pgno *aNew = (Pgno*)sqlite3Realloc(
 65076  66255               pCur->aOverflow, nOvfl*2*sizeof(Pgno)
 65077  66256           );
 65078  66257           if( aNew==0 ){
 65079  66258             return SQLITE_NOMEM_BKPT;
 65080  66259           }else{
 65081         -          pCur->nOvflAlloc = nOvfl*2;
 65082  66260             pCur->aOverflow = aNew;
 65083  66261           }
 65084  66262         }
 65085  66263         memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
 65086  66264         pCur->curFlags |= BTCF_ValidOvfl;
 65087  66265       }else{
 65088  66266         /* If the overflow page-list cache has been allocated and the
................................................................................
 66588  67766   static void freePage(MemPage *pPage, int *pRC){
 66589  67767     if( (*pRC)==SQLITE_OK ){
 66590  67768       *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
 66591  67769     }
 66592  67770   }
 66593  67771   
 66594  67772   /*
 66595         -** Free any overflow pages associated with the given Cell.  Write the
 66596         -** local Cell size (the number of bytes on the original page, omitting
 66597         -** overflow) into *pnSize.
        67773  +** Free any overflow pages associated with the given Cell.  Store
        67774  +** size information about the cell in pInfo.
 66598  67775   */
 66599  67776   static int clearCell(
 66600  67777     MemPage *pPage,          /* The page that contains the Cell */
 66601  67778     unsigned char *pCell,    /* First byte of the Cell */
 66602  67779     CellInfo *pInfo          /* Size information about the cell */
 66603  67780   ){
 66604  67781     BtShared *pBt;
................................................................................
 67794  68971       */
 67795  68972       if( pOld->aData[0]!=apOld[0]->aData[0] ){
 67796  68973         rc = SQLITE_CORRUPT_BKPT;
 67797  68974         goto balance_cleanup;
 67798  68975       }
 67799  68976   
 67800  68977       /* Load b.apCell[] with pointers to all cells in pOld.  If pOld
 67801         -    ** constains overflow cells, include them in the b.apCell[] array
        68978  +    ** contains overflow cells, include them in the b.apCell[] array
 67802  68979       ** in the correct spot.
 67803  68980       **
 67804  68981       ** Note that when there are multiple overflow cells, it is always the
 67805  68982       ** case that they are sequential and adjacent.  This invariant arises
 67806  68983       ** because multiple overflows can only occurs when inserting divider
 67807  68984       ** cells into a parent on a prior balance, and divider cells are always
 67808  68985       ** adjacent and are inserted in order.  There is an assert() tagged
................................................................................
 71279  72456         ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
 71280  72457       );
 71281  72458     }
 71282  72459     return 1;
 71283  72460   }
 71284  72461   #endif
 71285  72462   
        72463  +#ifdef SQLITE_DEBUG
        72464  +/*
        72465  +** Check that string value of pMem agrees with its integer or real value.
        72466  +**
        72467  +** A single int or real value always converts to the same strings.  But
        72468  +** many different strings can be converted into the same int or real.
        72469  +** If a table contains a numeric value and an index is based on the
        72470  +** corresponding string value, then it is important that the string be
        72471  +** derived from the numeric value, not the other way around, to ensure
        72472  +** that the index and table are consistent.  See ticket
        72473  +** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
        72474  +** an example.
        72475  +**
        72476  +** This routine looks at pMem to verify that if it has both a numeric
        72477  +** representation and a string representation then the string rep has
        72478  +** been derived from the numeric and not the other way around.  It returns
        72479  +** true if everything is ok and false if there is a problem.
        72480  +**
        72481  +** This routine is for use inside of assert() statements only.
        72482  +*/
        72483  +SQLITE_PRIVATE int sqlite3VdbeMemConsistentDualRep(Mem *p){
        72484  +  char zBuf[100];
        72485  +  char *z;
        72486  +  int i, j, incr;
        72487  +  if( (p->flags & MEM_Str)==0 ) return 1;
        72488  +  if( (p->flags & (MEM_Int|MEM_Real))==0 ) return 1;
        72489  +  if( p->flags & MEM_Int ){
        72490  +    sqlite3_snprintf(sizeof(zBuf),zBuf,"%lld",p->u.i);
        72491  +  }else{
        72492  +    sqlite3_snprintf(sizeof(zBuf),zBuf,"%!.15g",p->u.r);
        72493  +  }
        72494  +  z = p->z;
        72495  +  i = j = 0;
        72496  +  incr = 1;
        72497  +  if( p->enc!=SQLITE_UTF8 ){
        72498  +    incr = 2;
        72499  +    if( p->enc==SQLITE_UTF16BE ) z++;
        72500  +  }
        72501  +  while( zBuf[j] ){
        72502  +    if( zBuf[j++]!=z[i] ) return 0;
        72503  +    i += incr;
        72504  +  }
        72505  +  return 1;
        72506  +}
        72507  +#endif /* SQLITE_DEBUG */
 71286  72508   
 71287  72509   /*
 71288  72510   ** If pMem is an object with a valid string representation, this routine
 71289  72511   ** ensures the internal encoding for the string representation is
 71290  72512   ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
 71291  72513   **
 71292  72514   ** If pMem is not a string object, or the encoding of the string
................................................................................
 71712  72934     }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
 71713  72935       return memRealValue(pMem);
 71714  72936     }else{
 71715  72937       /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 71716  72938       return (double)0;
 71717  72939     }
 71718  72940   }
        72941  +
        72942  +/*
        72943  +** Return 1 if pMem represents true, and return 0 if pMem represents false.
        72944  +** Return the value ifNull if pMem is NULL.  
        72945  +*/
        72946  +SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
        72947  +  if( pMem->flags & MEM_Int ) return pMem->u.i!=0;
        72948  +  if( pMem->flags & MEM_Null ) return ifNull;
        72949  +  return sqlite3VdbeRealValue(pMem)!=0.0;
        72950  +}
 71719  72951   
 71720  72952   /*
 71721  72953   ** The MEM structure is already a MEM_Real.  Try to also make it a
 71722  72954   ** MEM_Int if we can.
 71723  72955   */
 71724  72956   SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
 71725  72957     i64 ix;
................................................................................
 71767  72999     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 71768  73000     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 71769  73001   
 71770  73002     pMem->u.r = sqlite3VdbeRealValue(pMem);
 71771  73003     MemSetTypeFlag(pMem, MEM_Real);
 71772  73004     return SQLITE_OK;
 71773  73005   }
        73006  +
        73007  +/* Compare a floating point value to an integer.  Return true if the two
        73008  +** values are the same within the precision of the floating point value.
        73009  +**
        73010  +** For some versions of GCC on 32-bit machines, if you do the more obvious
        73011  +** comparison of "r1==(double)i" you sometimes get an answer of false even
        73012  +** though the r1 and (double)i values are bit-for-bit the same.
        73013  +*/
        73014  +static int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
        73015  +  double r2 = (double)i;
        73016  +  return memcmp(&r1, &r2, sizeof(r1))==0;
        73017  +}
 71774  73018   
 71775  73019   /*
 71776  73020   ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
 71777  73021   ** Invalidate any prior representations.
 71778  73022   **
 71779  73023   ** Every effort is made to force the conversion, even if the input
 71780  73024   ** is a string that does not look completely like a number.  Convert
................................................................................
 71787  73031       assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 71788  73032       rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc);
 71789  73033       if( rc==0 ){
 71790  73034         MemSetTypeFlag(pMem, MEM_Int);
 71791  73035       }else{
 71792  73036         i64 i = pMem->u.i;
 71793  73037         sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
 71794         -      if( rc==1 && pMem->u.r==(double)i ){
        73038  +      if( rc==1 && sqlite3RealSameAsInt(pMem->u.r, i) ){
 71795  73039           pMem->u.i = i;
 71796  73040           MemSetTypeFlag(pMem, MEM_Int);
 71797  73041         }else{
 71798  73042           MemSetTypeFlag(pMem, MEM_Real);
 71799  73043         }
 71800  73044       }
 71801  73045     }
................................................................................
 72270  73514     }else{
 72271  73515       sqlite3VdbeMemStringify(pVal, enc, 0);
 72272  73516       assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
 72273  73517     }
 72274  73518     assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
 72275  73519                 || pVal->db->mallocFailed );
 72276  73520     if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
        73521  +    assert( sqlite3VdbeMemConsistentDualRep(pVal) );
 72277  73522       return pVal->z;
 72278  73523     }else{
 72279  73524       return 0;
 72280  73525     }
 72281  73526   }
 72282  73527   
 72283  73528   /* This function is only available internally, it is not part of the
................................................................................
 72292  73537   */
 72293  73538   SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
 72294  73539     if( !pVal ) return 0;
 72295  73540     assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
 72296  73541     assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
 72297  73542     assert( (pVal->flags & MEM_RowSet)==0 );
 72298  73543     if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
        73544  +    assert( sqlite3VdbeMemConsistentDualRep(pVal) );
 72299  73545       return pVal->z;
 72300  73546     }
 72301  73547     if( pVal->flags&MEM_Null ){
 72302  73548       return 0;
 72303  73549     }
 72304  73550     return valueToText(pVal, enc);
 72305  73551   }
................................................................................
 78073  79319   SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
 78074  79320     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78075  79321     sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
 78076  79322   }
 78077  79323   SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
 78078  79324     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78079  79325     pCtx->isError = SQLITE_ERROR;
 78080         -  pCtx->fErrorOrAux = 1;
 78081  79326     sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
 78082  79327   }
 78083  79328   #ifndef SQLITE_OMIT_UTF16
 78084  79329   SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
 78085  79330     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78086  79331     pCtx->isError = SQLITE_ERROR;
 78087         -  pCtx->fErrorOrAux = 1;
 78088  79332     sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
 78089  79333   }
 78090  79334   #endif
 78091  79335   SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
 78092  79336     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78093  79337     sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
 78094  79338   }
................................................................................
 78186  79430     if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
 78187  79431       return SQLITE_TOOBIG;
 78188  79432     }
 78189  79433     sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
 78190  79434     return SQLITE_OK;
 78191  79435   }
 78192  79436   SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
 78193         -  pCtx->isError = errCode;
 78194         -  pCtx->fErrorOrAux = 1;
        79437  +  pCtx->isError = errCode ? errCode : -1;
 78195  79438   #ifdef SQLITE_DEBUG
 78196  79439     if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
 78197  79440   #endif
 78198  79441     if( pCtx->pOut->flags & MEM_Null ){
 78199  79442       sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, 
 78200  79443                            SQLITE_UTF8, SQLITE_STATIC);
 78201  79444     }
 78202  79445   }
 78203  79446   
 78204  79447   /* Force an SQLITE_TOOBIG error. */
 78205  79448   SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
 78206  79449     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78207  79450     pCtx->isError = SQLITE_TOOBIG;
 78208         -  pCtx->fErrorOrAux = 1;
 78209  79451     sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 
 78210  79452                          SQLITE_UTF8, SQLITE_STATIC);
 78211  79453   }
 78212  79454   
 78213  79455   /* An SQLITE_NOMEM error. */
 78214  79456   SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
 78215  79457     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78216  79458     sqlite3VdbeMemSetNull(pCtx->pOut);
 78217  79459     pCtx->isError = SQLITE_NOMEM_BKPT;
 78218         -  pCtx->fErrorOrAux = 1;
 78219  79460     sqlite3OomFault(pCtx->pOut->db);
 78220  79461   }
 78221  79462   
 78222  79463   /*
 78223  79464   ** This function is called after a transaction has been committed. It 
 78224  79465   ** invokes callbacks registered with sqlite3_wal_hook() as required.
 78225  79466   */
................................................................................
 78618  79859     if( pAuxData==0 ){
 78619  79860       pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
 78620  79861       if( !pAuxData ) goto failed;
 78621  79862       pAuxData->iAuxOp = pCtx->iOp;
 78622  79863       pAuxData->iAuxArg = iArg;
 78623  79864       pAuxData->pNextAux = pVdbe->pAuxData;
 78624  79865       pVdbe->pAuxData = pAuxData;
 78625         -    if( pCtx->fErrorOrAux==0 ){
 78626         -      pCtx->isError = 0;
 78627         -      pCtx->fErrorOrAux = 1;
 78628         -    }
        79866  +    if( pCtx->isError==0 ) pCtx->isError = -1;
 78629  79867     }else if( pAuxData->xDeleteAux ){
 78630  79868       pAuxData->xDeleteAux(pAuxData->pAux);
 78631  79869     }
 78632  79870   
 78633  79871     pAuxData->pAux = pAux;
 78634  79872     pAuxData->xDeleteAux = xDelete;
 78635  79873     return;
................................................................................
 79377  80615   /*
 79378  80616   ** Return the value of a status counter for a prepared statement
 79379  80617   */
 79380  80618   SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
 79381  80619     Vdbe *pVdbe = (Vdbe*)pStmt;
 79382  80620     u32 v;
 79383  80621   #ifdef SQLITE_ENABLE_API_ARMOR
 79384         -  if( !pStmt ){
        80622  +  if( !pStmt 
        80623  +   || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
        80624  +  ){
 79385  80625       (void)SQLITE_MISUSE_BKPT;
 79386  80626       return 0;
 79387  80627     }
 79388  80628   #endif
 79389  80629     if( op==SQLITE_STMTSTATUS_MEMUSED ){
 79390  80630       sqlite3 *db = pVdbe->db;
 79391  80631       sqlite3_mutex_enter(db->mutex);
................................................................................
 80151  81391       pRec->u.i = iValue;
 80152  81392       pRec->flags |= MEM_Int;
 80153  81393     }else{
 80154  81394       pRec->u.r = rValue;
 80155  81395       pRec->flags |= MEM_Real;
 80156  81396       if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
 80157  81397     }
        81398  +  /* TEXT->NUMERIC is many->one.  Hence, it is important to invalidate the
        81399  +  ** string representation after computing a numeric equivalent, because the
        81400  +  ** string representation might not be the canonical representation for the
        81401  +  ** numeric value.  Ticket [343634942dd54ab57b7024] 2018-01-31. */
        81402  +  pRec->flags &= ~MEM_Str;
 80158  81403   }
 80159  81404   
 80160  81405   /*
 80161  81406   ** Processing is determine by the affinity parameter:
 80162  81407   **
 80163  81408   ** SQLITE_AFF_INTEGER:
 80164  81409   ** SQLITE_AFF_REAL:
................................................................................
 80619  81864     for(pOp=&aOp[p->pc]; 1; pOp++){
 80620  81865       /* Errors are detected by individual opcodes, with an immediate
 80621  81866       ** jumps to abort_due_to_error. */
 80622  81867       assert( rc==SQLITE_OK );
 80623  81868   
 80624  81869       assert( pOp>=aOp && pOp<&aOp[p->nOp]);
 80625  81870   #ifdef VDBE_PROFILE
 80626         -    start = sqlite3Hwtime();
        81871  +    start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
 80627  81872   #endif
 80628  81873       nVmStep++;
 80629  81874   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 80630  81875       if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
 80631  81876   #endif
 80632  81877   
 80633  81878       /* Only allow tracing if SQLITE_DEBUG is defined.
................................................................................
 82143  83388   ** give a NULL output.
 82144  83389   */
 82145  83390   case OP_And:              /* same as TK_AND, in1, in2, out3 */
 82146  83391   case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
 82147  83392     int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 82148  83393     int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 82149  83394   
 82150         -  pIn1 = &aMem[pOp->p1];
 82151         -  if( pIn1->flags & MEM_Null ){
 82152         -    v1 = 2;
 82153         -  }else{
 82154         -    v1 = sqlite3VdbeIntValue(pIn1)!=0;
 82155         -  }
 82156         -  pIn2 = &aMem[pOp->p2];
 82157         -  if( pIn2->flags & MEM_Null ){
 82158         -    v2 = 2;
 82159         -  }else{
 82160         -    v2 = sqlite3VdbeIntValue(pIn2)!=0;
 82161         -  }
        83395  +  v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
        83396  +  v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
 82162  83397     if( pOp->opcode==OP_And ){
 82163  83398       static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
 82164  83399       v1 = and_logic[v1*3+v2];
 82165  83400     }else{
 82166  83401       static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
 82167  83402       v1 = or_logic[v1*3+v2];
 82168  83403     }
................................................................................
 82171  83406       MemSetTypeFlag(pOut, MEM_Null);
 82172  83407     }else{
 82173  83408       pOut->u.i = v1;
 82174  83409       MemSetTypeFlag(pOut, MEM_Int);
 82175  83410     }
 82176  83411     break;
 82177  83412   }
        83413  +
        83414  +/* Opcode: IsTrue P1 P2 P3 P4 *
        83415  +** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
        83416  +**
        83417  +** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
        83418  +** IS NOT FALSE operators.
        83419  +**
        83420  +** Interpret the value in register P1 as a boolean value.  Store that
        83421  +** boolean (a 0 or 1) in register P2.  Or if the value in register P1 is 
        83422  +** NULL, then the P3 is stored in register P2.  Invert the answer if P4
        83423  +** is 1.
        83424  +**
        83425  +** The logic is summarized like this:
        83426  +**
        83427  +** <ul> 
        83428  +** <li> If P3==0 and P4==0  then  r[P2] := r[P1] IS TRUE
        83429  +** <li> If P3==1 and P4==1  then  r[P2] := r[P1] IS FALSE
        83430  +** <li> If P3==0 and P4==1  then  r[P2] := r[P1] IS NOT TRUE
        83431  +** <li> If P3==1 and P4==0  then  r[P2] := r[P1] IS NOT FALSE
        83432  +** </ul>
        83433  +*/
        83434  +case OP_IsTrue: {               /* in1, out2 */
        83435  +  assert( pOp->p4type==P4_INT32 );
        83436  +  assert( pOp->p4.i==0 || pOp->p4.i==1 );
        83437  +  assert( pOp->p3==0 || pOp->p3==1 );
        83438  +  sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
        83439  +      sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
        83440  +  break;
        83441  +}
 82178  83442   
 82179  83443   /* Opcode: Not P1 P2 * * *
 82180  83444   ** Synopsis: r[P2]= !r[P1]
 82181  83445   **
 82182  83446   ** Interpret the value in register P1 as a boolean value.  Store the
 82183  83447   ** boolean complement in register P2.  If the value in register P1 is 
 82184  83448   ** NULL, then a NULL is stored in P2.
 82185  83449   */
 82186  83450   case OP_Not: {                /* same as TK_NOT, in1, out2 */
 82187  83451     pIn1 = &aMem[pOp->p1];
 82188  83452     pOut = &aMem[pOp->p2];
 82189         -  sqlite3VdbeMemSetNull(pOut);
 82190  83453     if( (pIn1->flags & MEM_Null)==0 ){
 82191         -    pOut->flags = MEM_Int;
 82192         -    pOut->u.i = !sqlite3VdbeIntValue(pIn1);
        83454  +    sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
        83455  +  }else{
        83456  +    sqlite3VdbeMemSetNull(pOut);
 82193  83457     }
 82194  83458     break;
 82195  83459   }
 82196  83460   
 82197  83461   /* Opcode: BitNot P1 P2 * * *
 82198  83462   ** Synopsis: r[P1]= ~r[P1]
 82199  83463   **
................................................................................
 82252  83516   
 82253  83517   /* Opcode: If P1 P2 P3 * *
 82254  83518   **
 82255  83519   ** Jump to P2 if the value in register P1 is true.  The value
 82256  83520   ** is considered true if it is numeric and non-zero.  If the value
 82257  83521   ** in P1 is NULL then take the jump if and only if P3 is non-zero.
 82258  83522   */
        83523  +case OP_If:  {               /* jump, in1 */
        83524  +  int c;
        83525  +  c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
        83526  +  VdbeBranchTaken(c!=0, 2);
        83527  +  if( c ) goto jump_to_p2;
        83528  +  break;
        83529  +}
        83530  +
 82259  83531   /* Opcode: IfNot P1 P2 P3 * *
 82260  83532   **
 82261  83533   ** Jump to P2 if the value in register P1 is False.  The value
 82262  83534   ** is considered false if it has a numeric value of zero.  If the value
 82263  83535   ** in P1 is NULL then take the jump if and only if P3 is non-zero.
 82264  83536   */
 82265         -case OP_If:                 /* jump, in1 */
 82266  83537   case OP_IfNot: {            /* jump, in1 */
 82267  83538     int c;
 82268         -  pIn1 = &aMem[pOp->p1];
 82269         -  if( pIn1->flags & MEM_Null ){
 82270         -    c = pOp->p3;
 82271         -  }else{
 82272         -#ifdef SQLITE_OMIT_FLOATING_POINT
 82273         -    c = sqlite3VdbeIntValue(pIn1)!=0;
 82274         -#else
 82275         -    c = sqlite3VdbeRealValue(pIn1)!=0.0;
 82276         -#endif
 82277         -    if( pOp->opcode==OP_IfNot ) c = !c;
 82278         -  }
        83539  +  c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
 82279  83540     VdbeBranchTaken(c!=0, 2);
 82280         -  if( c ){
 82281         -    goto jump_to_p2;
 82282         -  }
        83541  +  if( c ) goto jump_to_p2;
 82283  83542     break;
 82284  83543   }
 82285  83544   
 82286  83545   /* Opcode: IsNull P1 P2 * * *
 82287  83546   ** Synopsis: if r[P1]==NULL goto P2
 82288  83547   **
 82289  83548   ** Jump to P2 if the value in register P1 is NULL.
................................................................................
 82336  83595   ** Store in register r[P3] the byte offset into the database file that is the
 82337  83596   ** start of the payload for the record at which that cursor P1 is currently
 82338  83597   ** pointing.
 82339  83598   **
 82340  83599   ** P2 is the column number for the argument to the sqlite_offset() function.
 82341  83600   ** This opcode does not use P2 itself, but the P2 value is used by the
 82342  83601   ** code generator.  The P1, P2, and P3 operands to this opcode are the
 82343         -** as as for OP_Column.
        83602  +** same as for OP_Column.
 82344  83603   **
 82345  83604   ** This opcode is only available if SQLite is compiled with the
 82346  83605   ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
 82347  83606   */
 82348  83607   case OP_Offset: {          /* out3 */
 82349  83608     VdbeCursor *pC;    /* The VDBE cursor */
 82350  83609     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
................................................................................
 84244  85503     VdbeFrame *pFrame;     /* Root frame of VDBE */
 84245  85504   
 84246  85505     v = 0;
 84247  85506     res = 0;
 84248  85507     pOut = out2Prerelease(p, pOp);
 84249  85508     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 84250  85509     pC = p->apCsr[pOp->p1];
        85510  +  if( !pC->isTable ){
        85511  +    rc = SQLITE_CORRUPT_BKPT;
        85512  +    goto abort_due_to_error;
        85513  +  }
 84251  85514     assert( pC!=0 );
 84252  85515     assert( pC->eCurType==CURTYPE_BTREE );
 84253  85516     assert( pC->uc.pCursor!=0 );
 84254  85517     {
 84255  85518       /* The next rowid or record number (different terms for the same
 84256  85519       ** thing) is obtained in a two-step algorithm.
 84257  85520       **
................................................................................
 86180  87443     sqlite3_context *pCtx;
 86181  87444   
 86182  87445     assert( pOp->p4type==P4_FUNCDEF );
 86183  87446     n = pOp->p5;
 86184  87447     assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
 86185  87448     assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
 86186  87449     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
 86187         -  pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
        87450  +  pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
        87451  +               (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
 86188  87452     if( pCtx==0 ) goto no_mem;
 86189  87453     pCtx->pMem = 0;
        87454  +  pCtx->pOut = (Mem*)&(pCtx->argv[n]);
        87455  +  sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
 86190  87456     pCtx->pFunc = pOp->p4.pFunc;
 86191  87457     pCtx->iOp = (int)(pOp - aOp);
 86192  87458     pCtx->pVdbe = p;
        87459  +  pCtx->skipFlag = 0;
        87460  +  pCtx->isError = 0;
 86193  87461     pCtx->argc = n;
 86194  87462     pOp->p4type = P4_FUNCCTX;
 86195  87463     pOp->p4.pCtx = pCtx;
 86196  87464     pOp->opcode = OP_AggStep;
 86197  87465     /* Fall through into OP_AggStep */
 86198  87466   }
 86199  87467   case OP_AggStep: {
 86200  87468     int i;
 86201  87469     sqlite3_context *pCtx;
 86202  87470     Mem *pMem;
 86203         -  Mem t;
 86204  87471   
 86205  87472     assert( pOp->p4type==P4_FUNCCTX );
 86206  87473     pCtx = pOp->p4.pCtx;
 86207  87474     pMem = &aMem[pOp->p3];
 86208  87475   
 86209  87476     /* If this function is inside of a trigger, the register array in aMem[]
 86210  87477     ** might change from one evaluation to the next.  The next block of code
................................................................................
 86219  87486     for(i=0; i<pCtx->argc; i++){
 86220  87487       assert( memIsValid(pCtx->argv[i]) );
 86221  87488       REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
 86222  87489     }
 86223  87490   #endif
 86224  87491   
 86225  87492     pMem->n++;
 86226         -  sqlite3VdbeMemInit(&t, db, MEM_Null);
 86227         -  pCtx->pOut = &t;
 86228         -  pCtx->fErrorOrAux = 0;
 86229         -  pCtx->skipFlag = 0;
        87493  +  assert( pCtx->pOut->flags==MEM_Null );
        87494  +  assert( pCtx->isError==0 );
        87495  +  assert( pCtx->skipFlag==0 );
 86230  87496     (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
 86231         -  if( pCtx->fErrorOrAux ){
 86232         -    if( pCtx->isError ){
 86233         -      sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
        87497  +  if( pCtx->isError ){
        87498  +    if( pCtx->isError>0 ){
        87499  +      sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
 86234  87500         rc = pCtx->isError;
 86235  87501       }
 86236         -    sqlite3VdbeMemRelease(&t);
        87502  +    if( pCtx->skipFlag ){
        87503  +      assert( pOp[-1].opcode==OP_CollSeq );
        87504  +      i = pOp[-1].p1;
        87505  +      if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
        87506  +      pCtx->skipFlag = 0;
        87507  +    }
        87508  +    sqlite3VdbeMemRelease(pCtx->pOut);
        87509  +    pCtx->pOut->flags = MEM_Null;
        87510  +    pCtx->isError = 0;
 86237  87511       if( rc ) goto abort_due_to_error;
 86238         -  }else{
 86239         -    assert( t.flags==MEM_Null );
 86240  87512     }
 86241         -  if( pCtx->skipFlag ){
 86242         -    assert( pOp[-1].opcode==OP_CollSeq );
 86243         -    i = pOp[-1].p1;
 86244         -    if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
 86245         -  }
        87513  +  assert( pCtx->pOut->flags==MEM_Null );
        87514  +  assert( pCtx->skipFlag==0 );
 86246  87515     break;
 86247  87516   }
 86248  87517   
 86249  87518   /* Opcode: AggFinal P1 P2 * P4 *
 86250  87519   ** Synopsis: accum=r[P1] N=P2
 86251  87520   **
 86252  87521   ** Execute the finalizer function for an aggregate.  P1 is
................................................................................
 86725  87994       pDest->flags = MEM_Null|MEM_Zero;
 86726  87995       pDest->u.nZero = 0;
 86727  87996     }else{
 86728  87997       MemSetTypeFlag(pDest, MEM_Null);
 86729  87998     }
 86730  87999     rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
 86731  88000     sqlite3VtabImportErrmsg(p, pVtab);
 86732         -  if( sContext.isError ){
        88001  +  if( sContext.isError>0 ){
        88002  +    sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
 86733  88003       rc = sContext.isError;
 86734  88004     }
 86735  88005     sqlite3VdbeChangeEncoding(pDest, encoding);
 86736  88006     REGISTER_TRACE(pOp->p3, pDest);
 86737  88007     UPDATE_MAX_BLOBSIZE(pDest);
 86738  88008   
 86739  88009     if( sqlite3VdbeMemTooBig(pDest) ){
................................................................................
 86990  88260     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
 86991  88261     pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
 86992  88262     if( pCtx==0 ) goto no_mem;
 86993  88263     pCtx->pOut = 0;
 86994  88264     pCtx->pFunc = pOp->p4.pFunc;
 86995  88265     pCtx->iOp = (int)(pOp - aOp);
 86996  88266     pCtx->pVdbe = p;
        88267  +  pCtx->isError = 0;
 86997  88268     pCtx->argc = n;
 86998  88269     pOp->p4type = P4_FUNCCTX;
 86999  88270     pOp->p4.pCtx = pCtx;
 87000  88271     assert( OP_PureFunc == OP_PureFunc0+2 );
 87001  88272     assert( OP_Function == OP_Function0+2 );
 87002  88273     pOp->opcode += 2;
 87003  88274     /* Fall through into OP_Function */
................................................................................
 87024  88295   #ifdef SQLITE_DEBUG
 87025  88296     for(i=0; i<pCtx->argc; i++){
 87026  88297       assert( memIsValid(pCtx->argv[i]) );
 87027  88298       REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
 87028  88299     }
 87029  88300   #endif
 87030  88301     MemSetTypeFlag(pOut, MEM_Null);
 87031         -  pCtx->fErrorOrAux = 0;
        88302  +  assert( pCtx->isError==0 );
 87032  88303     (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
 87033  88304   
 87034  88305     /* If the function returned an error, throw an exception */
 87035         -  if( pCtx->fErrorOrAux ){
 87036         -    if( pCtx->isError ){
        88306  +  if( pCtx->isError ){
        88307  +    if( pCtx->isError>0 ){
 87037  88308         sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
 87038  88309         rc = pCtx->isError;
 87039  88310       }
 87040  88311       sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
        88312  +    pCtx->isError = 0;
 87041  88313       if( rc ) goto abort_due_to_error;
 87042  88314     }
 87043  88315   
 87044  88316     /* Copy the result of the function into register P3 */
 87045  88317     if( pOut->flags & (MEM_Str|MEM_Blob) ){
 87046  88318       sqlite3VdbeChangeEncoding(pOut, encoding);
 87047  88319       if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
................................................................................
 87075  88347   ** first time they are evaluated for this run.
 87076  88348   **
 87077  88349   ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
 87078  88350   ** error is encountered.
 87079  88351   */
 87080  88352   case OP_Trace:
 87081  88353   case OP_Init: {          /* jump */
        88354  +  int i;
        88355  +#ifndef SQLITE_OMIT_TRACE
 87082  88356     char *zTrace;
 87083         -  int i;
        88357  +#endif
 87084  88358   
 87085  88359     /* If the P4 argument is not NULL, then it must be an SQL comment string.
 87086  88360     ** The "--" string is broken up to prevent false-positives with srcck1.c.
 87087  88361     **
 87088  88362     ** This assert() provides evidence for:
 87089  88363     ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
 87090  88364     ** would have been returned by the legacy sqlite3_trace() interface by
................................................................................
 87193  88467   ** readability.  From this point on down, the normal indentation rules are
 87194  88468   ** restored.
 87195  88469   *****************************************************************************/
 87196  88470       }
 87197  88471   
 87198  88472   #ifdef VDBE_PROFILE
 87199  88473       {
 87200         -      u64 endTime = sqlite3Hwtime();
        88474  +      u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
 87201  88475         if( endTime>start ) pOrigOp->cycles += endTime - start;
 87202  88476         pOrigOp->cnt++;
 87203  88477       }
 87204  88478   #endif
 87205  88479   
 87206  88480       /* The following code adds nothing to the actual functionality
 87207  88481       ** of the program.  It is only here for testing and debugging.
................................................................................
 91580  92854     ** Z is a string literal if it doesn't match any column names.  In that
 91581  92855     ** case, we need to return right away and not make any changes to
 91582  92856     ** pExpr.
 91583  92857     **
 91584  92858     ** Because no reference was made to outer contexts, the pNC->nRef
 91585  92859     ** fields are not changed in any context.
 91586  92860     */
 91587         -  if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
 91588         -    pExpr->op = TK_STRING;
 91589         -    pExpr->pTab = 0;
 91590         -    return WRC_Prune;
        92861  +  if( cnt==0 && zTab==0 ){
        92862  +    assert( pExpr->op==TK_ID );
        92863  +    if( ExprHasProperty(pExpr,EP_DblQuoted) ){
        92864  +      pExpr->op = TK_STRING;
        92865  +      pExpr->pTab = 0;
        92866  +      return WRC_Prune;
        92867  +    }
        92868  +    if( sqlite3ExprIdToTrueFalse(pExpr) ){
        92869  +      return WRC_Prune;
        92870  +    }
 91591  92871     }
 91592  92872   
 91593  92873     /*
 91594  92874     ** cnt==0 means there was not match.  cnt>1 means there were two or
 91595  92875     ** more matches.  Either way, we have an error.
 91596  92876     */
 91597  92877     if( cnt!=1 ){
................................................................................
 91932  93212         }
 91933  93213         break;
 91934  93214       }
 91935  93215       case TK_VARIABLE: {
 91936  93216         notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
 91937  93217         break;
 91938  93218       }
        93219  +    case TK_IS:
        93220  +    case TK_ISNOT: {
        93221  +      Expr *pRight;
        93222  +      assert( !ExprHasProperty(pExpr, EP_Reduced) );
        93223  +      /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
        93224  +      ** and "x IS NOT FALSE". */
        93225  +      if( (pRight = pExpr->pRight)->op==TK_ID ){
        93226  +        int rc = resolveExprStep(pWalker, pRight);
        93227  +        if( rc==WRC_Abort ) return WRC_Abort;
        93228  +        if( pRight->op==TK_TRUEFALSE ){
        93229  +          pExpr->op2 = pExpr->op;
        93230  +          pExpr->op = TK_TRUTH;
        93231  +          return WRC_Continue;
        93232  +        }
        93233  +      }
        93234  +      /* Fall thru */
        93235  +    }
 91939  93236       case TK_BETWEEN:
 91940  93237       case TK_EQ:
 91941  93238       case TK_NE:
 91942  93239       case TK_LT:
 91943  93240       case TK_LE:
 91944  93241       case TK_GT:
 91945         -    case TK_GE:
 91946         -    case TK_IS:
 91947         -    case TK_ISNOT: {
        93242  +    case TK_GE: {
 91948  93243         int nLeft, nRight;
 91949  93244         if( pParse->db->mallocFailed ) break;
 91950  93245         assert( pExpr->pLeft!=0 );
 91951  93246         nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
 91952  93247         if( pExpr->op==TK_BETWEEN ){
 91953  93248           nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
 91954  93249           if( nRight==nLeft ){
................................................................................
 94414  95709   ** This callback is used by multiple expression walkers.
 94415  95710   */
 94416  95711   SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
 94417  95712     UNUSED_PARAMETER(NotUsed);
 94418  95713     pWalker->eCode = 0;
 94419  95714     return WRC_Abort;
 94420  95715   }
        95716  +
        95717  +/*
        95718  +** If the input expression is an ID with the name "true" or "false"
        95719  +** then convert it into an TK_TRUEFALSE term.  Return non-zero if
        95720  +** the conversion happened, and zero if the expression is unaltered.
        95721  +*/
        95722  +SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
        95723  +  assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
        95724  +  if( sqlite3StrICmp(pExpr->u.zToken, "true")==0
        95725  +   || sqlite3StrICmp(pExpr->u.zToken, "false")==0
        95726  +  ){
        95727  +    pExpr->op = TK_TRUEFALSE;
        95728  +    return 1;
        95729  +  }
        95730  +  return 0;
        95731  +}
        95732  +
        95733  +/*
        95734  +** The argument must be a TK_TRUEFALSE Expr node.  Return 1 if it is TRUE
        95735  +** and 0 if it is FALSE.
        95736  +*/
        95737  +SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
        95738  +  assert( pExpr->op==TK_TRUEFALSE );
        95739  +  assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
        95740  +       || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
        95741  +  return pExpr->u.zToken[4]==0;
        95742  +}
        95743  +
 94421  95744   
 94422  95745   /*
 94423  95746   ** These routines are Walker callbacks used to check expressions to
 94424  95747   ** see if they are "constant" for some definition of constant.  The
 94425  95748   ** Walker.eCode value determines the type of "constant" we are looking
 94426  95749   ** for.
 94427  95750   **
................................................................................
 94462  95785         if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
 94463  95786           return WRC_Continue;
 94464  95787         }else{
 94465  95788           pWalker->eCode = 0;
 94466  95789           return WRC_Abort;
 94467  95790         }
 94468  95791       case TK_ID:
        95792  +      /* Convert "true" or "false" in a DEFAULT clause into the
        95793  +      ** appropriate TK_TRUEFALSE operator */
        95794  +      if( sqlite3ExprIdToTrueFalse(pExpr) ){
        95795  +        return WRC_Prune;
        95796  +      }
        95797  +      /* Fall thru */
 94469  95798       case TK_COLUMN:
 94470  95799       case TK_AGG_FUNCTION:
 94471  95800       case TK_AGG_COLUMN:
 94472  95801         testcase( pExpr->op==TK_ID );
 94473  95802         testcase( pExpr->op==TK_COLUMN );
 94474  95803         testcase( pExpr->op==TK_AGG_FUNCTION );
 94475  95804         testcase( pExpr->op==TK_AGG_COLUMN );
................................................................................
 96225  97554         return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
 96226  97555                                  pExpr->iColumn, iTab, target,
 96227  97556                                  pExpr->op2);
 96228  97557       }
 96229  97558       case TK_INTEGER: {
 96230  97559         codeInteger(pParse, pExpr, 0, target);
 96231  97560         return target;
        97561  +    }
        97562  +    case TK_TRUEFALSE: {
        97563  +      sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
        97564  +      return target;
 96232  97565       }
 96233  97566   #ifndef SQLITE_OMIT_FLOATING_POINT
 96234  97567       case TK_FLOAT: {
 96235  97568         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 96236  97569         codeReal(v, pExpr->u.zToken, 0, target);
 96237  97570         return target;
 96238  97571       }
................................................................................
 96380  97713       case TK_NOT: {
 96381  97714         assert( TK_BITNOT==OP_BitNot );   testcase( op==TK_BITNOT );
 96382  97715         assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
 96383  97716         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 96384  97717         testcase( regFree1==0 );
 96385  97718         sqlite3VdbeAddOp2(v, op, r1, inReg);
 96386  97719         break;
        97720  +    }
        97721  +    case TK_TRUTH: {
        97722  +      int isTrue;    /* IS TRUE or IS NOT TRUE */
        97723  +      int bNormal;   /* IS TRUE or IS FALSE */
        97724  +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
        97725  +      testcase( regFree1==0 );
        97726  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
        97727  +      bNormal = pExpr->op2==TK_IS;
        97728  +      testcase( isTrue && bNormal);
        97729  +      testcase( !isTrue && bNormal);
        97730  +      sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
        97731  +      break;
 96387  97732       }
 96388  97733       case TK_ISNULL:
 96389  97734       case TK_NOTNULL: {
 96390  97735         int addr;
 96391  97736         assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
 96392  97737         assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
 96393  97738         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
................................................................................
 97155  98500         sqlite3ExprCachePop(pParse);
 97156  98501         break;
 97157  98502       }
 97158  98503       case TK_NOT: {
 97159  98504         testcase( jumpIfNull==0 );
 97160  98505         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
 97161  98506         break;
        98507  +    }
        98508  +    case TK_TRUTH: {
        98509  +      int isNot;      /* IS NOT TRUE or IS NOT FALSE */
        98510  +      int isTrue;     /* IS TRUE or IS NOT TRUE */
        98511  +      testcase( jumpIfNull==0 );
        98512  +      isNot = pExpr->op2==TK_ISNOT;
        98513  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
        98514  +      testcase( isTrue && isNot );
        98515  +      testcase( !isTrue && isNot );
        98516  +      if( isTrue ^ isNot ){
        98517  +        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
        98518  +                          isNot ? SQLITE_JUMPIFNULL : 0);
        98519  +      }else{
        98520  +        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
        98521  +                           isNot ? SQLITE_JUMPIFNULL : 0);
        98522  +      }
        98523  +      break;
 97162  98524       }
 97163  98525       case TK_IS:
 97164  98526       case TK_ISNOT:
 97165  98527         testcase( op==TK_IS );
 97166  98528         testcase( op==TK_ISNOT );
 97167  98529         op = (op==TK_IS) ? TK_EQ : TK_NE;
 97168  98530         jumpIfNull = SQLITE_NULLEQ;
................................................................................
 97309  98671         sqlite3ExprCachePop(pParse);
 97310  98672         break;
 97311  98673       }
 97312  98674       case TK_NOT: {
 97313  98675         testcase( jumpIfNull==0 );
 97314  98676         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
 97315  98677         break;
        98678  +    }
        98679  +    case TK_TRUTH: {
        98680  +      int isNot;   /* IS NOT TRUE or IS NOT FALSE */
        98681  +      int isTrue;  /* IS TRUE or IS NOT TRUE */
        98682  +      testcase( jumpIfNull==0 );
        98683  +      isNot = pExpr->op2==TK_ISNOT;
        98684  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
        98685  +      testcase( isTrue && isNot );
        98686  +      testcase( !isTrue && isNot );
        98687  +      if( isTrue ^ isNot ){
        98688  +        /* IS TRUE and IS NOT FALSE */
        98689  +        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
        98690  +                           isNot ? 0 : SQLITE_JUMPIFNULL);
        98691  +
        98692  +      }else{
        98693  +        /* IS FALSE and IS NOT TRUE */
        98694  +        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
        98695  +                          isNot ? 0 : SQLITE_JUMPIFNULL);
        98696  +      }
        98697  +      break;
 97316  98698       }
 97317  98699       case TK_IS:
 97318  98700       case TK_ISNOT:
 97319  98701         testcase( pExpr->op==TK_IS );
 97320  98702         testcase( pExpr->op==TK_ISNOT );
 97321  98703         op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
 97322  98704         jumpIfNull = SQLITE_NULLEQ;
................................................................................
 97596  98978     if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){
 97597  98979       Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft);
 97598  98980       testcase( pX!=pE1->pLeft );
 97599  98981       if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1;
 97600  98982     }
 97601  98983     return 0;
 97602  98984   }
        98985  +
        98986  +/*
        98987  +** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
        98988  +** If the expression node requires that the table at pWalker->iCur
        98989  +** have a non-NULL column, then set pWalker->eCode to 1 and abort.
        98990  +*/
        98991  +static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
        98992  +  /* This routine is only called for WHERE clause expressions and so it
        98993  +  ** cannot have any TK_AGG_COLUMN entries because those are only found
        98994  +  ** in HAVING clauses.  We can get a TK_AGG_FUNCTION in a WHERE clause,
        98995  +  ** but that is an illegal construct and the query will be rejected at
        98996  +  ** a later stage of processing, so the TK_AGG_FUNCTION case does not
        98997  +  ** need to be considered here. */
        98998  +  assert( pExpr->op!=TK_AGG_COLUMN );
        98999  +  testcase( pExpr->op==TK_AGG_FUNCTION );
        99000  +
        99001  +  if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
        99002  +  switch( pExpr->op ){
        99003  +    case TK_ISNULL:
        99004  +    case TK_IS:
        99005  +    case TK_OR:
        99006  +    case TK_CASE:
        99007  +    case TK_IN:
        99008  +    case TK_FUNCTION:
        99009  +      testcase( pExpr->op==TK_ISNULL );
        99010  +      testcase( pExpr->op==TK_IS );
        99011  +      testcase( pExpr->op==TK_OR );
        99012  +      testcase( pExpr->op==TK_CASE );
        99013  +      testcase( pExpr->op==TK_IN );
        99014  +      testcase( pExpr->op==TK_FUNCTION );
        99015  +      return WRC_Prune;
        99016  +    case TK_COLUMN:
        99017  +      if( pWalker->u.iCur==pExpr->iTable ){
        99018  +        pWalker->eCode = 1;
        99019  +        return WRC_Abort;
        99020  +      }
        99021  +      return WRC_Prune;
        99022  +    default:
        99023  +      return WRC_Continue;
        99024  +  }
        99025  +}
        99026  +
        99027  +/*
        99028  +** Return true (non-zero) if expression p can only be true if at least
        99029  +** one column of table iTab is non-null.  In other words, return true
        99030  +** if expression p will always be NULL or false if every column of iTab
        99031  +** is NULL.
        99032  +**
        99033  +** False negatives are acceptable.  In other words, it is ok to return
        99034  +** zero even if expression p will never be true of every column of iTab
        99035  +** is NULL.  A false negative is merely a missed optimization opportunity.
        99036  +**
        99037  +** False positives are not allowed, however.  A false positive may result
        99038  +** in an incorrect answer.
        99039  +**
        99040  +** Terms of p that are marked with EP_FromJoin (and hence that come from
        99041  +** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
        99042  +**
        99043  +** This routine is used to check if a LEFT JOIN can be converted into
        99044  +** an ordinary JOIN.  The p argument is the WHERE clause.  If the WHERE
        99045  +** clause requires that some column of the right table of the LEFT JOIN
        99046  +** be non-NULL, then the LEFT JOIN can be safely converted into an
        99047  +** ordinary join.
        99048  +*/
        99049  +SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
        99050  +  Walker w;
        99051  +  w.xExprCallback = impliesNotNullRow;
        99052  +  w.xSelectCallback = 0;
        99053  +  w.xSelectCallback2 = 0;
        99054  +  w.eCode = 0;
        99055  +  w.u.iCur = iTab;
        99056  +  sqlite3WalkExpr(&w, p);
        99057  +  return w.eCode;
        99058  +}
 97603  99059   
 97604  99060   /*
 97605  99061   ** An instance of the following structure is used by the tree walker
 97606  99062   ** to determine if an expression can be evaluated by reference to the
 97607  99063   ** index only, without having to do a search for the corresponding
 97608  99064   ** table entry.  The IdxCover.pIdx field is the index.  IdxCover.iCur
 97609  99065   ** is the cursor for the table.
................................................................................
 99841 101297     if( v==0 || NEVER(pTab==0) ){
 99842 101298       return;
 99843 101299     }
 99844 101300     if( pTab->tnum==0 ){
 99845 101301       /* Do not gather statistics on views or virtual tables */
 99846 101302       return;
 99847 101303     }
 99848         -  if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){
       101304  +  if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
 99849 101305       /* Do not gather statistics on system tables */
 99850 101306       return;
 99851 101307     }
 99852 101308     assert( sqlite3BtreeHoldsAllMutexes(db) );
 99853 101309     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 99854 101310     assert( iDb>=0 );
 99855 101311     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
................................................................................
100820 102276   **
100821 102277   **     ATTACH DATABASE x AS y KEY z
100822 102278   **
100823 102279   **     SELECT sqlite_attach(x, y, z)
100824 102280   **
100825 102281   ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
100826 102282   ** third argument.
       102283  +**
       102284  +** If the db->init.reopenMemdb flags is set, then instead of attaching a
       102285  +** new database, close the database on db->init.iDb and reopen it as an
       102286  +** empty MemDB.
100827 102287   */
100828 102288   static void attachFunc(
100829 102289     sqlite3_context *context,
100830 102290     int NotUsed,
100831 102291     sqlite3_value **argv
100832 102292   ){
100833 102293     int i;
................................................................................
100840 102300     unsigned int flags;
100841 102301     Db *aNew;                 /* New array of Db pointers */
100842 102302     Db *pNew;                 /* Db object for the newly attached database */
100843 102303     char *zErrDyn = 0;
100844 102304     sqlite3_vfs *pVfs;
100845 102305   
100846 102306     UNUSED_PARAMETER(NotUsed);
100847         -
100848 102307     zFile = (const char *)sqlite3_value_text(argv[0]);
100849 102308     zName = (const char *)sqlite3_value_text(argv[1]);
100850 102309     if( zFile==0 ) zFile = "";
100851 102310     if( zName==0 ) zName = "";
100852 102311   
100853         -  /* Check for the following errors:
100854         -  **
100855         -  **     * Too many attached databases,
100856         -  **     * Transaction currently open
100857         -  **     * Specified database name already being used.
100858         -  */
100859         -  if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
100860         -    zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
100861         -      db->aLimit[SQLITE_LIMIT_ATTACHED]
100862         -    );
100863         -    goto attach_error;
100864         -  }
100865         -  for(i=0; i<db->nDb; i++){
100866         -    char *z = db->aDb[i].zDbSName;
100867         -    assert( z && zName );
100868         -    if( sqlite3StrICmp(z, zName)==0 ){
100869         -      zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
       102312  +#ifdef SQLITE_ENABLE_DESERIALIZE
       102313  +# define REOPEN_AS_MEMDB(db)  (db->init.reopenMemdb)
       102314  +#else
       102315  +# define REOPEN_AS_MEMDB(db)  (0)
       102316  +#endif
       102317  +
       102318  +  if( REOPEN_AS_MEMDB(db) ){
       102319  +    /* This is not a real ATTACH.  Instead, this routine is being called
       102320  +    ** from sqlite3_deserialize() to close database db->init.iDb and
       102321  +    ** reopen it as a MemDB */
       102322  +    pVfs = sqlite3_vfs_find("memdb");
       102323  +    if( pVfs==0 ) return;
       102324  +    pNew = &db->aDb[db->init.iDb];
       102325  +    if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
       102326  +    pNew->pBt = 0;
       102327  +    pNew->pSchema = 0;
       102328  +    rc = sqlite3BtreeOpen(pVfs, "x", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
       102329  +  }else{
       102330  +    /* This is a real ATTACH
       102331  +    **
       102332  +    ** Check for the following errors:
       102333  +    **
       102334  +    **     * Too many attached databases,
       102335  +    **     * Transaction currently open
       102336  +    **     * Specified database name already being used.
       102337  +    */
       102338  +    if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
       102339  +      zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
       102340  +        db->aLimit[SQLITE_LIMIT_ATTACHED]
       102341  +      );
100870 102342         goto attach_error;
100871 102343       }
100872         -  }
100873         -
100874         -  /* Allocate the new entry in the db->aDb[] array and initialize the schema
100875         -  ** hash tables.
100876         -  */
100877         -  if( db->aDb==db->aDbStatic ){
100878         -    aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
100879         -    if( aNew==0 ) return;
100880         -    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
100881         -  }else{
100882         -    aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
100883         -    if( aNew==0 ) return;
100884         -  }
100885         -  db->aDb = aNew;
100886         -  pNew = &db->aDb[db->nDb];
100887         -  memset(pNew, 0, sizeof(*pNew));
100888         -
100889         -  /* Open the database file. If the btree is successfully opened, use
100890         -  ** it to obtain the database schema. At this point the schema may
100891         -  ** or may not be initialized.
100892         -  */
100893         -  flags = db->openFlags;
100894         -  rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
100895         -  if( rc!=SQLITE_OK ){
100896         -    if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
100897         -    sqlite3_result_error(context, zErr, -1);
100898         -    sqlite3_free(zErr);
100899         -    return;
100900         -  }
100901         -  assert( pVfs );
100902         -  flags |= SQLITE_OPEN_MAIN_DB;
100903         -  rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
100904         -  sqlite3_free( zPath );
100905         -  db->nDb++;
       102344  +    for(i=0; i<db->nDb; i++){
       102345  +      char *z = db->aDb[i].zDbSName;
       102346  +      assert( z && zName );
       102347  +      if( sqlite3StrICmp(z, zName)==0 ){
       102348  +        zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
       102349  +        goto attach_error;
       102350  +      }
       102351  +    }
       102352  +  
       102353  +    /* Allocate the new entry in the db->aDb[] array and initialize the schema
       102354  +    ** hash tables.
       102355  +    */
       102356  +    if( db->aDb==db->aDbStatic ){
       102357  +      aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
       102358  +      if( aNew==0 ) return;
       102359  +      memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
       102360  +    }else{
       102361  +      aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
       102362  +      if( aNew==0 ) return;
       102363  +    }
       102364  +    db->aDb = aNew;
       102365  +    pNew = &db->aDb[db->nDb];
       102366  +    memset(pNew, 0, sizeof(*pNew));
       102367  +  
       102368  +    /* Open the database file. If the btree is successfully opened, use
       102369  +    ** it to obtain the database schema. At this point the schema may
       102370  +    ** or may not be initialized.
       102371  +    */
       102372  +    flags = db->openFlags;
       102373  +    rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
       102374  +    if( rc!=SQLITE_OK ){
       102375  +      if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
       102376  +      sqlite3_result_error(context, zErr, -1);
       102377  +      sqlite3_free(zErr);
       102378  +      return;
       102379  +    }
       102380  +    assert( pVfs );
       102381  +    flags |= SQLITE_OPEN_MAIN_DB;
       102382  +    rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
       102383  +    sqlite3_free( zPath );
       102384  +    db->nDb++;
       102385  +  }
100906 102386     db->skipBtreeMutex = 0;
100907 102387     if( rc==SQLITE_CONSTRAINT ){
100908 102388       rc = SQLITE_ERROR;
100909 102389       zErrDyn = sqlite3MPrintf(db, "database is already attached");
100910 102390     }else if( rc==SQLITE_OK ){
100911 102391       Pager *pPager;
100912 102392       pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
................................................................................
100925 102405   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
100926 102406       sqlite3BtreeSetPagerFlags(pNew->pBt,
100927 102407                         PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
100928 102408   #endif
100929 102409       sqlite3BtreeLeave(pNew->pBt);
100930 102410     }
100931 102411     pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
100932         -  pNew->zDbSName = sqlite3DbStrDup(db, zName);
       102412  +  if( !REOPEN_AS_MEMDB(db) ) pNew->zDbSName = sqlite3DbStrDup(db, zName);
100933 102413     if( rc==SQLITE_OK && pNew->zDbSName==0 ){
100934 102414       rc = SQLITE_NOMEM_BKPT;
100935 102415     }
100936 102416   
100937 102417   
100938 102418   #ifdef SQLITE_HAS_CODEC
100939 102419     if( rc==SQLITE_OK ){
................................................................................
100965 102445           break;
100966 102446       }
100967 102447     }
100968 102448   #endif
100969 102449   
100970 102450     /* If the file was opened successfully, read the schema for the new database.
100971 102451     ** If this fails, or if opening the file failed, then close the file and 
100972         -  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
100973         -  ** we found it.
       102452  +  ** remove the entry from the db->aDb[] array. i.e. put everything back the
       102453  +  ** way we found it.
100974 102454     */
100975 102455     if( rc==SQLITE_OK ){
100976 102456       sqlite3BtreeEnterAll(db);
       102457  +    db->init.iDb = 0;
100977 102458       rc = sqlite3Init(db, &zErrDyn);
100978 102459       sqlite3BtreeLeaveAll(db);
       102460  +    assert( zErrDyn==0 || rc!=SQLITE_OK );
100979 102461     }
100980 102462   #ifdef SQLITE_USER_AUTHENTICATION
100981 102463     if( rc==SQLITE_OK ){
100982 102464       u8 newAuth = 0;
100983 102465       rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
100984 102466       if( newAuth<db->auth.authLevel ){
100985 102467         rc = SQLITE_AUTH_USER;
100986 102468       }
100987 102469     }
100988 102470   #endif
100989 102471     if( rc ){
100990         -    int iDb = db->nDb - 1;
100991         -    assert( iDb>=2 );
100992         -    if( db->aDb[iDb].pBt ){
100993         -      sqlite3BtreeClose(db->aDb[iDb].pBt);
100994         -      db->aDb[iDb].pBt = 0;
100995         -      db->aDb[iDb].pSchema = 0;
100996         -    }
100997         -    sqlite3ResetAllSchemasOfConnection(db);
100998         -    db->nDb = iDb;
100999         -    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
101000         -      sqlite3OomFault(db);
101001         -      sqlite3DbFree(db, zErrDyn);
101002         -      zErrDyn = sqlite3MPrintf(db, "out of memory");
101003         -    }else if( zErrDyn==0 ){
101004         -      zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
       102472  +    if( !REOPEN_AS_MEMDB(db) ){
       102473  +      int iDb = db->nDb - 1;
       102474  +      assert( iDb>=2 );
       102475  +      if( db->aDb[iDb].pBt ){
       102476  +        sqlite3BtreeClose(db->aDb[iDb].pBt);
       102477  +        db->aDb[iDb].pBt = 0;
       102478  +        db->aDb[iDb].pSchema = 0;
       102479  +      }
       102480  +      sqlite3ResetAllSchemasOfConnection(db);
       102481  +      db->nDb = iDb;
       102482  +      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
       102483  +        sqlite3OomFault(db);
       102484  +        sqlite3DbFree(db, zErrDyn);
       102485  +        zErrDyn = sqlite3MPrintf(db, "out of memory");
       102486  +      }else if( zErrDyn==0 ){
       102487  +        zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
       102488  +      }
101005 102489       }
101006 102490       goto attach_error;
101007 102491     }
101008 102492     
101009 102493     return;
101010 102494   
101011 102495   attach_error:
................................................................................
101268 102752         return 1;
101269 102753       }
101270 102754       if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
101271 102755         return 1;
101272 102756       }
101273 102757       if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
101274 102758         return 1;
       102759  +    }
       102760  +    if( pSelect->pWith ){
       102761  +      int i;
       102762  +      for(i=0; i<pSelect->pWith->nCte; i++){
       102763  +        if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
       102764  +          return 1;
       102765  +        }
       102766  +      }
101275 102767       }
101276 102768       pSelect = pSelect->pPrior;
101277 102769     }
101278 102770     return 0;
101279 102771   }
101280 102772   SQLITE_PRIVATE int sqlite3FixExpr(
101281 102773     DbFixer *pFix,     /* Context of the fixation */
................................................................................
102732 104224   ** This routine is called by the parser while in the middle of
102733 104225   ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
102734 104226   ** been seen on a column.  This routine sets the notNull flag on
102735 104227   ** the column currently under construction.
102736 104228   */
102737 104229   SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
102738 104230     Table *p;
       104231  +  Column *pCol;
102739 104232     p = pParse->pNewTable;
102740 104233     if( p==0 || NEVER(p->nCol<1) ) return;
102741         -  p->aCol[p->nCol-1].notNull = (u8)onError;
       104234  +  pCol = &p->aCol[p->nCol-1];
       104235  +  pCol->notNull = (u8)onError;
102742 104236     p->tabFlags |= TF_HasNotNull;
       104237  +
       104238  +  /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
       104239  +  ** on this column.  */
       104240  +  if( pCol->colFlags & COLFLAG_UNIQUE ){
       104241  +    Index *pIdx;
       104242  +    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
       104243  +      assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
       104244  +      if( pIdx->aiColumn[0]==p->nCol-1 ){
       104245  +        pIdx->uniqNotNull = 1;
       104246  +      }
       104247  +    }
       104248  +  }
102743 104249   }
102744 104250   
102745 104251   /*
102746 104252   ** Scan the column type name zType (length nType) and return the
102747 104253   ** associated affinity type.
102748 104254   **
102749 104255   ** This routine does a case-independent search of zType for the 
................................................................................
102852 104358     if( p!=0 ){
102853 104359       pCol = &(p->aCol[p->nCol-1]);
102854 104360       if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
102855 104361         sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
102856 104362             pCol->zName);
102857 104363       }else{
102858 104364         /* A copy of pExpr is used instead of the original, as pExpr contains
102859         -      ** tokens that point to volatile memory.	
       104365  +      ** tokens that point to volatile memory.
102860 104366         */
102861 104367         Expr x;
102862 104368         sqlite3ExprDelete(db, pCol->pDflt);
102863 104369         memset(&x, 0, sizeof(x));
102864 104370         x.op = TK_SPAN;
102865 104371         x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
102866 104372         x.pLeft = pExpr;
................................................................................
103470 104976     if( pEnd==0 && pSelect==0 ){
103471 104977       return;
103472 104978     }
103473 104979     assert( !db->mallocFailed );
103474 104980     p = pParse->pNewTable;
103475 104981     if( p==0 ) return;
103476 104982   
103477         -  assert( !db->init.busy || !pSelect );
103478         -
103479 104983     /* If the db->init.busy is 1 it means we are reading the SQL off the
103480 104984     ** "sqlite_master" or "sqlite_temp_master" table on the disk.
103481 104985     ** So do not write to the disk again.  Extract the root page number
103482 104986     ** for the table from the db->init.newTnum field.  (The page number
103483 104987     ** should have been put there by the sqliteOpenCb routine.)
103484 104988     **
103485 104989     ** If the root page number is 1, that means this is the sqlite_master
103486 104990     ** table itself.  So mark it read-only.
103487 104991     */
103488 104992     if( db->init.busy ){
       104993  +    if( pSelect ){
       104994  +      sqlite3ErrorMsg(pParse, "");
       104995  +      return;
       104996  +    }
103489 104997       p->tnum = db->init.newTnum;
103490 104998       if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
103491 104999     }
103492 105000   
103493 105001     /* Special processing for WITHOUT ROWID Tables */
103494 105002     if( tabOpts & TF_WithoutRowid ){
103495 105003       if( (p->tabFlags & TF_Autoincrement) ){
................................................................................
103772 105280   */
103773 105281   SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
103774 105282     Table *pSelTab;   /* A fake table from which we get the result set */
103775 105283     Select *pSel;     /* Copy of the SELECT that implements the view */
103776 105284     int nErr = 0;     /* Number of errors encountered */
103777 105285     int n;            /* Temporarily holds the number of cursors assigned */
103778 105286     sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
103779         -#ifndef SQLITE_OMIT_VIRTUALTABLE	
       105287  +#ifndef SQLITE_OMIT_VIRTUALTABLE
103780 105288     int rc;
103781 105289   #endif
103782 105290   #ifndef SQLITE_OMIT_AUTHORIZATION
103783 105291     sqlite3_xauth xAuth;       /* Saved xAuth pointer */
103784 105292   #endif
103785 105293   
103786 105294     assert( pTable );
................................................................................
104699 106207   
104700 106208     /* If pList==0, it means this routine was called to make a primary
104701 106209     ** key out of the last column added to the table under construction.
104702 106210     ** So create a fake list to simulate this.
104703 106211     */
104704 106212     if( pList==0 ){
104705 106213       Token prevCol;
104706         -    sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName);
       106214  +    Column *pCol = &pTab->aCol[pTab->nCol-1];
       106215  +    pCol->colFlags |= COLFLAG_UNIQUE;
       106216  +    sqlite3TokenInit(&prevCol, pCol->zName);
104707 106217       pList = sqlite3ExprListAppend(pParse, 0,
104708 106218                 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
104709 106219       if( pList==0 ) goto exit_create_index;
104710 106220       assert( pList->nExpr==1 );
104711 106221       sqlite3ExprListSetSortOrder(pList, sortOrder);
104712 106222     }else{
104713 106223       sqlite3ExprListCheckLength(pParse, pList, "index");
................................................................................
107544 109054   }
107545 109055   
107546 109056   /*
107547 109057   ** Indicate that the accumulator load should be skipped on this
107548 109058   ** iteration of the aggregate loop.
107549 109059   */
107550 109060   static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
       109061  +  assert( context->isError<=0 );
       109062  +  context->isError = -1;
107551 109063     context->skipFlag = 1;
107552 109064   }
107553 109065   
107554 109066   /*
107555 109067   ** Implementation of the non-aggregate min() and max() functions
107556 109068   */
107557 109069   static void minmaxFunc(
................................................................................
107610 109122   ** Implementation of the length() function
107611 109123   */
107612 109124   static void lengthFunc(
107613 109125     sqlite3_context *context,
107614 109126     int argc,
107615 109127     sqlite3_value **argv
107616 109128   ){
107617         -  int len;
107618         -
107619 109129     assert( argc==1 );
107620 109130     UNUSED_PARAMETER(argc);
107621 109131     switch( sqlite3_value_type(argv[0]) ){
107622 109132       case SQLITE_BLOB:
107623 109133       case SQLITE_INTEGER:
107624 109134       case SQLITE_FLOAT: {
107625 109135         sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
107626 109136         break;
107627 109137       }
107628 109138       case SQLITE_TEXT: {
107629 109139         const unsigned char *z = sqlite3_value_text(argv[0]);
       109140  +      const unsigned char *z0;
       109141  +      unsigned char c;
107630 109142         if( z==0 ) return;
107631         -      len = 0;
107632         -      while( *z ){
107633         -        len++;
107634         -        SQLITE_SKIP_UTF8(z);
       109143  +      z0 = z;
       109144  +      while( (c = *z)!=0 ){
       109145  +        z++;
       109146  +        if( c>=0xc0 ){
       109147  +          while( (*z & 0xc0)==0x80 ){ z++; z0++; }
       109148  +        }
107635 109149         }
107636         -      sqlite3_result_int(context, len);
       109150  +      sqlite3_result_int(context, (int)(z-z0));
107637 109151         break;
107638 109152       }
107639 109153       default: {
107640 109154         sqlite3_result_null(context);
107641 109155         break;
107642 109156       }
107643 109157     }
................................................................................
108704 110218     unsigned char *zOut;              /* The output */
108705 110219     int nStr;                /* Size of zStr */
108706 110220     int nPattern;            /* Size of zPattern */
108707 110221     int nRep;                /* Size of zRep */
108708 110222     i64 nOut;                /* Maximum size of zOut */
108709 110223     int loopLimit;           /* Last zStr[] that might match zPattern[] */
108710 110224     int i, j;                /* Loop counters */
       110225  +  unsigned cntExpand;      /* Number zOut expansions */
       110226  +  sqlite3 *db = sqlite3_context_db_handle(context);
108711 110227   
108712 110228     assert( argc==3 );
108713 110229     UNUSED_PARAMETER(argc);
108714 110230     zStr = sqlite3_value_text(argv[0]);
108715 110231     if( zStr==0 ) return;
108716 110232     nStr = sqlite3_value_bytes(argv[0]);
108717 110233     assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
................................................................................
108735 110251     nOut = nStr + 1;
108736 110252     assert( nOut<SQLITE_MAX_LENGTH );
108737 110253     zOut = contextMalloc(context, (i64)nOut);
108738 110254     if( zOut==0 ){
108739 110255       return;
108740 110256     }
108741 110257     loopLimit = nStr - nPattern;  
       110258  +  cntExpand = 0;
108742 110259     for(i=j=0; i<=loopLimit; i++){
108743 110260       if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
108744 110261         zOut[j++] = zStr[i];
108745 110262       }else{
108746         -      u8 *zOld;
108747         -      sqlite3 *db = sqlite3_context_db_handle(context);
108748         -      nOut += nRep - nPattern;
108749         -      testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
108750         -      testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
108751         -      if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108752         -        sqlite3_result_error_toobig(context);
108753         -        sqlite3_free(zOut);
108754         -        return;
108755         -      }
108756         -      zOld = zOut;
108757         -      zOut = sqlite3_realloc64(zOut, (int)nOut);
108758         -      if( zOut==0 ){
108759         -        sqlite3_result_error_nomem(context);
108760         -        sqlite3_free(zOld);
108761         -        return;
       110263  +      if( nRep>nPattern ){
       110264  +        nOut += nRep - nPattern;
       110265  +        testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
       110266  +        testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
       110267  +        if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
       110268  +          sqlite3_result_error_toobig(context);
       110269  +          sqlite3_free(zOut);
       110270  +          return;
       110271  +        }
       110272  +        cntExpand++;
       110273  +        if( (cntExpand&(cntExpand-1))==0 ){
       110274  +          /* Grow the size of the output buffer only on substitutions
       110275  +          ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
       110276  +          u8 *zOld;
       110277  +          zOld = zOut;
       110278  +          zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1));
       110279  +          if( zOut==0 ){
       110280  +            sqlite3_result_error_nomem(context);
       110281  +            sqlite3_free(zOld);
       110282  +            return;
       110283  +          }
       110284  +        }
108762 110285         }
108763 110286         memcpy(&zOut[j], zRep, nRep);
108764 110287         j += nRep;
108765 110288         i += nPattern-1;
108766 110289       }
108767 110290     }
108768         -  assert( j+nStr-i+1==nOut );
       110291  +  assert( j+nStr-i+1<=nOut );
108769 110292     memcpy(&zOut[j], &zStr[i], nStr-i);
108770 110293     j += nStr - i;
108771 110294     assert( j<=nOut );
108772 110295     zOut[j] = 0;
108773 110296     sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
108774 110297   }
108775 110298   
................................................................................
111043 112566   **
111044 112567   ** There is at most one AutoincInfo structure per table even if the
111045 112568   ** same table is autoincremented multiple times due to inserts within
111046 112569   ** triggers.  A new AutoincInfo structure is created if this is the
111047 112570   ** first use of table pTab.  On 2nd and subsequent uses, the original
111048 112571   ** AutoincInfo structure is used.
111049 112572   **
111050         -** Three memory locations are allocated:
       112573  +** Four consecutive registers are allocated:
111051 112574   **
111052         -**   (1)  Register to hold the name of the pTab table.
111053         -**   (2)  Register to hold the maximum ROWID of pTab.
111054         -**   (3)  Register to hold the rowid in sqlite_sequence of pTab
       112575  +**   (1)  The name of the pTab table.
       112576  +**   (2)  The maximum ROWID of pTab.
       112577  +**   (3)  The rowid in sqlite_sequence of pTab
       112578  +**   (4)  The original value of the max ROWID in pTab, or NULL if none
111055 112579   **
111056 112580   ** The 2nd register is the one that is returned.  That is all the
111057 112581   ** insert routine needs to know about.
111058 112582   */
111059 112583   static int autoIncBegin(
111060 112584     Parse *pParse,      /* Parsing context */
111061 112585     int iDb,            /* Index of the database holding pTab */
................................................................................
111075 112599         if( pInfo==0 ) return 0;
111076 112600         pInfo->pNext = pToplevel->pAinc;
111077 112601         pToplevel->pAinc = pInfo;
111078 112602         pInfo->pTab = pTab;
111079 112603         pInfo->iDb = iDb;
111080 112604         pToplevel->nMem++;                  /* Register to hold name of table */
111081 112605         pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
111082         -      pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
       112606  +      pToplevel->nMem +=2;       /* Rowid in sqlite_sequence + orig max val */
111083 112607       }
111084 112608       memId = pInfo->regCtr;
111085 112609     }
111086 112610     return memId;
111087 112611   }
111088 112612   
111089 112613   /*
................................................................................
111103 112627     assert( sqlite3IsToplevel(pParse) );
111104 112628   
111105 112629     assert( v );   /* We failed long ago if this is not so */
111106 112630     for(p = pParse->pAinc; p; p = p->pNext){
111107 112631       static const int iLn = VDBE_OFFSET_LINENO(2);
111108 112632       static const VdbeOpList autoInc[] = {
111109 112633         /* 0  */ {OP_Null,    0,  0, 0},
111110         -      /* 1  */ {OP_Rewind,  0,  9, 0},
       112634  +      /* 1  */ {OP_Rewind,  0, 10, 0},
111111 112635         /* 2  */ {OP_Column,  0,  0, 0},
111112         -      /* 3  */ {OP_Ne,      0,  7, 0},
       112636  +      /* 3  */ {OP_Ne,      0,  9, 0},
111113 112637         /* 4  */ {OP_Rowid,   0,  0, 0},
111114 112638         /* 5  */ {OP_Column,  0,  1, 0},
111115         -      /* 6  */ {OP_Goto,    0,  9, 0},
111116         -      /* 7  */ {OP_Next,    0,  2, 0},
111117         -      /* 8  */ {OP_Integer, 0,  0, 0},
111118         -      /* 9  */ {OP_Close,   0,  0, 0} 
       112639  +      /* 6  */ {OP_AddImm,  0,  0, 0},
       112640  +      /* 7  */ {OP_Copy,    0,  0, 0},
       112641  +      /* 8  */ {OP_Goto,    0, 11, 0},
       112642  +      /* 9  */ {OP_Next,    0,  2, 0},
       112643  +      /* 10 */ {OP_Integer, 0,  0, 0},
       112644  +      /* 11 */ {OP_Close,   0,  0, 0} 
111119 112645       };
111120 112646       VdbeOp *aOp;
111121 112647       pDb = &db->aDb[p->iDb];
111122 112648       memId = p->regCtr;
111123 112649       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
111124 112650       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
111125 112651       sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
111126 112652       aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
111127 112653       if( aOp==0 ) break;
111128 112654       aOp[0].p2 = memId;
111129         -    aOp[0].p3 = memId+1;
       112655  +    aOp[0].p3 = memId+2;
111130 112656       aOp[2].p3 = memId;
111131 112657       aOp[3].p1 = memId-1;
111132 112658       aOp[3].p3 = memId;
111133 112659       aOp[3].p5 = SQLITE_JUMPIFNULL;
111134 112660       aOp[4].p2 = memId+1;
111135 112661       aOp[5].p3 = memId;
111136         -    aOp[8].p2 = memId;
       112662  +    aOp[6].p1 = memId;
       112663  +    aOp[7].p2 = memId+2;
       112664  +    aOp[7].p1 = memId;
       112665  +    aOp[10].p2 = memId;
111137 112666     }
111138 112667   }
111139 112668   
111140 112669   /*
111141 112670   ** Update the maximum rowid for an autoincrement calculation.
111142 112671   **
111143 112672   ** This routine should be called when the regRowid register holds a
................................................................................
111176 112705       VdbeOp *aOp;
111177 112706       Db *pDb = &db->aDb[p->iDb];
111178 112707       int iRec;
111179 112708       int memId = p->regCtr;
111180 112709   
111181 112710       iRec = sqlite3GetTempReg(pParse);
111182 112711       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
       112712  +    sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
       112713  +    VdbeCoverage(v);
111183 112714       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
111184 112715       aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
111185 112716       if( aOp==0 ) break;
111186 112717       aOp[0].p1 = memId+1;
111187 112718       aOp[1].p2 = memId+1;
111188 112719       aOp[2].p1 = memId-1;
111189 112720       aOp[2].p3 = iRec;
................................................................................
113824 115355   #define sqlite3_prepare_v3             sqlite3_api->prepare_v3
113825 115356   #define sqlite3_prepare16_v3           sqlite3_api->prepare16_v3
113826 115357   #define sqlite3_bind_pointer           sqlite3_api->bind_pointer
113827 115358   #define sqlite3_result_pointer         sqlite3_api->result_pointer
113828 115359   #define sqlite3_value_pointer          sqlite3_api->value_pointer
113829 115360   /* Version 3.22.0 and later */
113830 115361   #define sqlite3_vtab_nochange          sqlite3_api->vtab_nochange
113831         -#define sqlite3_value_nochange         sqltie3_api->value_nochange
113832         -#define sqlite3_vtab_collation         sqltie3_api->vtab_collation
       115362  +#define sqlite3_value_nochange         sqlite3_api->value_nochange
       115363  +#define sqlite3_vtab_collation         sqlite3_api->vtab_collation
113833 115364   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
113834 115365   
113835 115366   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
113836 115367     /* This case when the file really is being compiled as a loadable 
113837 115368     ** extension */
113838 115369   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
113839 115370   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
117809 119340     const char *zExtra   /* Error information */
117810 119341   ){
117811 119342     sqlite3 *db = pData->db;
117812 119343     if( !db->mallocFailed && (db->flags & SQLITE_WriteSchema)==0 ){
117813 119344       char *z;
117814 119345       if( zObj==0 ) zObj = "?";
117815 119346       z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
117816         -    if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
       119347  +    if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
117817 119348       sqlite3DbFree(db, *pData->pzErrMsg);
117818 119349       *pData->pzErrMsg = z;
117819 119350     }
117820 119351     pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT;
117821 119352   }
117822 119353   
117823 119354   /*
................................................................................
118703 120234   /*
118704 120235   ** Trace output macros
118705 120236   */
118706 120237   #if SELECTTRACE_ENABLED
118707 120238   /***/ int sqlite3SelectTrace = 0;
118708 120239   # define SELECTTRACE(K,P,S,X)  \
118709 120240     if(sqlite3SelectTrace&(K))   \
118710         -    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
118711         -        (S)->zSelName,(S)),\
       120241  +    sqlite3DebugPrintf("%s/%p: ",(S)->zSelName,(S)),\
118712 120242       sqlite3DebugPrintf X
118713 120243   #else
118714 120244   # define SELECTTRACE(K,P,S,X)
118715 120245   #endif
118716 120246   
118717 120247   
118718 120248   /*
................................................................................
119064 120594           setJoinExpr(p->x.pList->a[i].pExpr, iTable);
119065 120595         }
119066 120596       }
119067 120597       setJoinExpr(p->pLeft, iTable);
119068 120598       p = p->pRight;
119069 120599     } 
119070 120600   }
       120601  +
       120602  +/* Undo the work of setJoinExpr().  In the expression tree p, convert every
       120603  +** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
       120604  +** an ordinary term that omits the EP_FromJoin mark.
       120605  +**
       120606  +** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
       120607  +*/
       120608  +static void unsetJoinExpr(Expr *p, int iTable){
       120609  +  while( p ){
       120610  +    if( ExprHasProperty(p, EP_FromJoin)
       120611  +     && (iTable<0 || p->iRightJoinTable==iTable) ){
       120612  +      ExprClearProperty(p, EP_FromJoin);
       120613  +    }
       120614  +    if( p->op==TK_FUNCTION && p->x.pList ){
       120615  +      int i;
       120616  +      for(i=0; i<p->x.pList->nExpr; i++){
       120617  +        unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
       120618  +      }
       120619  +    }
       120620  +    unsetJoinExpr(p->pLeft, iTable);
       120621  +    p = p->pRight;
       120622  +  } 
       120623  +}
119071 120624   
119072 120625   /*
119073 120626   ** This routine processes the join information for a SELECT statement.
119074 120627   ** ON and USING clauses are converted into extra terms of the WHERE clause.
119075 120628   ** NATURAL joins also create extra WHERE clause terms.
119076 120629   **
119077 120630   ** The terms of a FROM clause are contained in the Select.pSrc structure.
................................................................................
119948 121501       bSeq = 0;
119949 121502     }else{
119950 121503       addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
119951 121504       codeOffset(v, p->iOffset, addrContinue);
119952 121505       iSortTab = iTab;
119953 121506       bSeq = 1;
119954 121507     }
119955         -  for(i=0, iCol=nKey+bSeq; i<nSortData; i++){
       121508  +  for(i=0, iCol=nKey+bSeq-1; i<nSortData; i++){
       121509  +    if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
       121510  +  }
       121511  +  for(i=nSortData-1; i>=0; i--){
119956 121512       int iRead;
119957 121513       if( aOutEx[i].u.x.iOrderByCol ){
119958 121514         iRead = aOutEx[i].u.x.iOrderByCol-1;
119959 121515       }else{
119960         -      iRead = iCol++;
       121516  +      iRead = iCol--;
119961 121517       }
119962 121518       sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
119963 121519       VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
119964 121520     }
119965 121521     switch( eDest ){
119966 121522       case SRT_Table:
119967 121523       case SRT_EphemTab: {
................................................................................
122430 123986         ** (the only way this can happen is if the compound sub-query is
122431 123987         ** currently part of pSub->pSrc). See ticket [d11a6e908f].  */
122432 123988         ExprList *pOrderBy = pSub->pOrderBy;
122433 123989         for(i=0; i<pOrderBy->nExpr; i++){
122434 123990           pOrderBy->a[i].u.x.iOrderByCol = 0;
122435 123991         }
122436 123992         assert( pParent->pOrderBy==0 );
122437         -      assert( pSub->pPrior==0 );
122438 123993         pParent->pOrderBy = pOrderBy;
122439 123994         pSub->pOrderBy = 0;
122440 123995       }
122441 123996       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
122442 123997       if( isLeftJoin>0 ){
122443 123998         setJoinExpr(pWhere, iNewParent);
122444 123999       }
................................................................................
122514 124069   **           to suppress it. **)
122515 124070   **
122516 124071   **   (2) The inner query is the recursive part of a common table expression.
122517 124072   **
122518 124073   **   (3) The inner query has a LIMIT clause (since the changes to the WHERE
122519 124074   **       close would change the meaning of the LIMIT).
122520 124075   **
122521         -**   (4) The inner query is the right operand of a LEFT JOIN.  (The caller
122522         -**       enforces this restriction since this routine does not have enough
122523         -**       information to know.)
       124076  +**   (4) The inner query is the right operand of a LEFT JOIN and the
       124077  +**       expression to be pushed down does not come from the ON clause
       124078  +**       on that LEFT JOIN.
122524 124079   **
122525 124080   **   (5) The WHERE clause expression originates in the ON or USING clause
122526         -**       of a LEFT JOIN.
       124081  +**       of a LEFT JOIN where iCursor is not the right-hand table of that
       124082  +**       left join.  An example:
       124083  +**
       124084  +**           SELECT *
       124085  +**           FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
       124086  +**           JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
       124087  +**           LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
       124088  +**
       124089  +**       The correct answer is three rows:  (1,1,NULL),(2,2,8),(2,2,9).
       124090  +**       But if the (b2=2) term were to be pushed down into the bb subquery,
       124091  +**       then the (1,1,NULL) row would be suppressed.
122527 124092   **
122528 124093   ** Return 0 if no changes are made and non-zero if one or more WHERE clause
122529 124094   ** terms are duplicated into the subquery.
122530 124095   */
122531 124096   static int pushDownWhereTerms(
122532 124097     Parse *pParse,        /* Parse context (for malloc() and error reporting) */
122533 124098     Select *pSubq,        /* The subquery whose WHERE clause is to be augmented */
122534 124099     Expr *pWhere,         /* The WHERE clause of the outer query */
122535         -  int iCursor           /* Cursor number of the subquery */
       124100  +  int iCursor,          /* Cursor number of the subquery */
       124101  +  int isLeftJoin        /* True if pSubq is the right term of a LEFT JOIN */
122536 124102   ){
122537 124103     Expr *pNew;
122538 124104     int nChng = 0;
122539 124105     if( pWhere==0 ) return 0;
122540 124106     if( pSubq->selFlags & SF_Recursive ) return 0;  /* restriction (2) */
122541 124107   
122542 124108   #ifdef SQLITE_DEBUG
................................................................................
122552 124118     }
122553 124119   #endif
122554 124120   
122555 124121     if( pSubq->pLimit!=0 ){
122556 124122       return 0; /* restriction (3) */
122557 124123     }
122558 124124     while( pWhere->op==TK_AND ){
122559         -    nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, iCursor);
       124125  +    nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
       124126  +                                iCursor, isLeftJoin);
122560 124127       pWhere = pWhere->pLeft;
122561 124128     }
122562         -  if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction (5) */
       124129  +  if( isLeftJoin
       124130  +   && (ExprHasProperty(pWhere,EP_FromJoin)==0
       124131  +         || pWhere->iRightJoinTable!=iCursor)
       124132  +  ){
       124133  +    return 0; /* restriction (4) */
       124134  +  }
       124135  +  if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
       124136  +    return 0; /* restriction (5) */
       124137  +  }
122563 124138     if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
122564 124139       nChng++;
122565 124140       while( pSubq ){
122566 124141         SubstContext x;
122567 124142         pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
       124143  +      unsetJoinExpr(pNew, -1);
122568 124144         x.pParse = pParse;
122569 124145         x.iTable = iCursor;
122570 124146         x.iNewTable = iCursor;
122571 124147         x.isLeftJoin = 0;
122572 124148         x.pEList = pSubq->pEList;
122573 124149         pNew = substExpr(&x, pNew);
122574 124150         if( pSubq->selFlags & SF_Aggregate ){
................................................................................
123592 125168       );
123593 125169     }
123594 125170   }
123595 125171   #else
123596 125172   # define explainSimpleCount(a,b,c)
123597 125173   #endif
123598 125174   
123599         -/*
123600         -** Context object for havingToWhereExprCb().
123601         -*/
123602         -struct HavingToWhereCtx {
123603         -  Expr **ppWhere;
123604         -  ExprList *pGroupBy;
123605         -};
123606         -
123607 125175   /*
123608 125176   ** sqlite3WalkExpr() callback used by havingToWhere().
123609 125177   **
123610 125178   ** If the node passed to the callback is a TK_AND node, return 
123611 125179   ** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
123612 125180   **
123613 125181   ** Otherwise, return WRC_Prune. In this case, also check if the 
123614 125182   ** sub-expression matches the criteria for being moved to the WHERE
123615 125183   ** clause. If so, add it to the WHERE clause and replace the sub-expression
123616 125184   ** within the HAVING expression with a constant "1".
123617 125185   */
123618 125186   static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
123619 125187     if( pExpr->op!=TK_AND ){
123620         -    struct HavingToWhereCtx *p = pWalker->u.pHavingCtx;
123621         -    if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, p->pGroupBy) ){
       125188  +    Select *pS = pWalker->u.pSelect;
       125189  +    if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
123622 125190         sqlite3 *db = pWalker->pParse->db;
123623 125191         Expr *pNew = sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[1], 0);
123624 125192         if( pNew ){
123625         -        Expr *pWhere = *(p->ppWhere);
       125193  +        Expr *pWhere = pS->pWhere;
123626 125194           SWAP(Expr, *pNew, *pExpr);
123627 125195           pNew = sqlite3ExprAnd(db, pWhere, pNew);
123628         -        *(p->ppWhere) = pNew;
       125196  +        pS->pWhere = pNew;
       125197  +        pWalker->eCode = 1;
123629 125198         }
123630 125199       }
123631 125200       return WRC_Prune;
123632 125201     }
123633 125202     return WRC_Continue;
123634 125203   }
123635 125204   
................................................................................
123644 125213   **
123645 125214   **   SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
123646 125215   **
123647 125216   ** A term of the HAVING expression is eligible for transfer if it consists
123648 125217   ** entirely of constants and expressions that are also GROUP BY terms that
123649 125218   ** use the "BINARY" collation sequence.
123650 125219   */
123651         -static void havingToWhere(
123652         -  Parse *pParse,
123653         -  ExprList *pGroupBy,
123654         -  Expr *pHaving, 
123655         -  Expr **ppWhere
123656         -){
123657         -  struct HavingToWhereCtx sCtx;
       125220  +static void havingToWhere(Parse *pParse, Select *p){
123658 125221     Walker sWalker;
123659         -
123660         -  sCtx.ppWhere = ppWhere;
123661         -  sCtx.pGroupBy = pGroupBy;
123662         -
123663 125222     memset(&sWalker, 0, sizeof(sWalker));
123664 125223     sWalker.pParse = pParse;
123665 125224     sWalker.xExprCallback = havingToWhereExprCb;
123666         -  sWalker.u.pHavingCtx = &sCtx;
123667         -  sqlite3WalkExpr(&sWalker, pHaving);
       125225  +  sWalker.u.pSelect = p;
       125226  +  sqlite3WalkExpr(&sWalker, p->pHaving);
       125227  +#if SELECTTRACE_ENABLED
       125228  +  if( sWalker.eCode && (sqlite3SelectTrace & 0x100)!=0 ){
       125229  +    SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
       125230  +    sqlite3TreeViewSelect(0, p, 0);
       125231  +  }
       125232  +#endif
123668 125233   }
123669 125234   
123670 125235   /*
123671 125236   ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
123672 125237   ** If it is, then return the SrcList_item for the prior view.  If it is not,
123673 125238   ** then return 0.
123674 125239   */
................................................................................
123821 125386     db = pParse->db;
123822 125387     if( p==0 || db->mallocFailed || pParse->nErr ){
123823 125388       return 1;
123824 125389     }
123825 125390     if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
123826 125391     memset(&sAggInfo, 0, sizeof(sAggInfo));
123827 125392   #if SELECTTRACE_ENABLED
123828         -  pParse->nSelectIndent++;
123829 125393     SELECTTRACE(1,pParse,p, ("begin processing:\n"));
123830 125394     if( sqlite3SelectTrace & 0x100 ){
123831 125395       sqlite3TreeViewSelect(0, p, 0);
123832 125396     }
123833 125397   #endif
123834 125398   
123835 125399     assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
................................................................................
123867 125431     ** does not already exist */
123868 125432     v = sqlite3GetVdbe(pParse);
123869 125433     if( v==0 ) goto select_end;
123870 125434     if( pDest->eDest==SRT_Output ){
123871 125435       generateColumnNames(pParse, p);
123872 125436     }
123873 125437   
123874         -  /* Try to flatten subqueries in the FROM clause up into the main query
       125438  +  /* Try to various optimizations (flattening subqueries, and strength
       125439  +  ** reduction of join operators) in the FROM clause up into the main query
123875 125440     */
123876 125441   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
123877 125442     for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
123878 125443       struct SrcList_item *pItem = &pTabList->a[i];
123879 125444       Select *pSub = pItem->pSelect;
123880 125445       Table *pTab = pItem->pTab;
       125446  +
       125447  +    /* Convert LEFT JOIN into JOIN if there are terms of the right table
       125448  +    ** of the LEFT JOIN used in the WHERE clause.
       125449  +    */
       125450  +    if( (pItem->fg.jointype & JT_LEFT)!=0
       125451  +     && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
       125452  +     && OptimizationEnabled(db, SQLITE_SimplifyJoin)
       125453  +    ){
       125454  +      SELECTTRACE(0x100,pParse,p,
       125455  +                ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
       125456  +      pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
       125457  +      unsetJoinExpr(p->pWhere, pItem->iCursor);
       125458  +    }
       125459  +
       125460  +    /* No futher action if this term of the FROM clause is no a subquery */
123881 125461       if( pSub==0 ) continue;
123882 125462   
123883 125463       /* Catch mismatch in the declared columns of a view and the number of
123884 125464       ** columns in the SELECT on the RHS */
123885 125465       if( pTab->nCol!=pSub->pEList->nExpr ){
123886 125466         sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
123887 125467                         pTab->nCol, pTab->zName, pSub->pEList->nExpr);
................................................................................
123942 125522     ** procedure.
123943 125523     */
123944 125524     if( p->pPrior ){
123945 125525       rc = multiSelect(pParse, p, pDest);
123946 125526       explainSetInteger(pParse->iSelectId, iRestoreSelectId);
123947 125527   #if SELECTTRACE_ENABLED
123948 125528       SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
123949         -    pParse->nSelectIndent--;
123950 125529   #endif
123951 125530       return rc;
123952 125531     }
123953 125532   #endif
123954 125533   
123955 125534     /* For each term in the FROM clause, do two things:
123956 125535     ** (1) Authorized unreferenced tables
................................................................................
124015 125594       ** an exact limit.
124016 125595       */
124017 125596       pParse->nHeight += sqlite3SelectExprHeight(p);
124018 125597   
124019 125598       /* Make copies of constant WHERE-clause terms in the outer query down
124020 125599       ** inside the subquery.  This can help the subquery to run more efficiently.
124021 125600       */
124022         -    if( (pItem->fg.jointype & JT_OUTER)==0
124023         -     && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor)
       125601  +    if( OptimizationEnabled(db, SQLITE_PushDown)
       125602  +     && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
       125603  +                           (pItem->fg.jointype & JT_OUTER)!=0)
124024 125604       ){
124025 125605   #if SELECTTRACE_ENABLED
124026 125606         if( sqlite3SelectTrace & 0x100 ){
124027 125607           SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
124028 125608           sqlite3TreeViewSelect(0, p, 0);
124029 125609         }
124030 125610   #endif
       125611  +    }else{
       125612  +      SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
124031 125613       }
124032 125614   
124033 125615       zSavedAuthContext = pParse->zAuthContext;
124034 125616       pParse->zAuthContext = pItem->zName;
124035 125617   
124036 125618       /* Generate code to implement the subquery
124037 125619       **
................................................................................
124226 125808     if( !isAgg && pGroupBy==0 ){
124227 125809       /* No aggregate functions and no GROUP BY clause */
124228 125810       u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
124229 125811       assert( WHERE_USE_LIMIT==SF_FixedLimit );
124230 125812       wctrlFlags |= p->selFlags & SF_FixedLimit;
124231 125813   
124232 125814       /* Begin the database scan. */
       125815  +    SELECTTRACE(1,pParse,p,("WhereBegin\n"));
124233 125816       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
124234 125817                                  p->pEList, wctrlFlags, p->nSelectRow);
124235 125818       if( pWInfo==0 ) goto select_end;
124236 125819       if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
124237 125820         p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
124238 125821       }
124239 125822       if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
................................................................................
124327 125910       sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
124328 125911       sAggInfo.pGroupBy = pGroupBy;
124329 125912       sqlite3ExprAnalyzeAggList(&sNC, pEList);
124330 125913       sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
124331 125914       if( pHaving ){
124332 125915         if( pGroupBy ){
124333 125916           assert( pWhere==p->pWhere );
124334         -        havingToWhere(pParse, pGroupBy, pHaving, &p->pWhere);
       125917  +        assert( pHaving==p->pHaving );
       125918  +        assert( pGroupBy==p->pGroupBy );
       125919  +        havingToWhere(pParse, p);
124335 125920           pWhere = p->pWhere;
124336 125921         }
124337 125922         sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
124338 125923       }
124339 125924       sAggInfo.nAccumulator = sAggInfo.nColumn;
124340 125925       if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
124341 125926         minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
................................................................................
124414 125999   
124415 126000         /* Begin a loop that will extract all source rows in GROUP BY order.
124416 126001         ** This might involve two separate loops with an OP_Sort in between, or
124417 126002         ** it might be a single loop that uses an index to extract information
124418 126003         ** in the right order to begin with.
124419 126004         */
124420 126005         sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
       126006  +      SELECTTRACE(1,pParse,p,("WhereBegin\n"));
124421 126007         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
124422 126008             WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
124423 126009         );
124424 126010         if( pWInfo==0 ) goto select_end;
124425 126011         if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
124426 126012           /* The optimizer is able to deliver rows in group by order so
124427 126013           ** we do not have to sort.  The OP_OpenEphemeral table will be
................................................................................
124669 126255           ** minMaxFlag will have been previously set to either
124670 126256           ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
124671 126257           ** be an appropriate ORDER BY expression for the optimization.
124672 126258           */
124673 126259           assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
124674 126260           assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
124675 126261   
       126262  +        SELECTTRACE(1,pParse,p,("WhereBegin\n"));
124676 126263           pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
124677 126264                                      0, minMaxFlag, 0);
124678 126265           if( pWInfo==0 ){
124679 126266             goto select_end;
124680 126267           }
124681 126268           updateAccumulator(pParse, &sAggInfo);
124682 126269           if( sqlite3WhereIsOrdered(pWInfo)>0 ){
................................................................................
124724 126311   select_end:
124725 126312     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
124726 126313     sqlite3ExprListDelete(db, pMinMaxOrderBy);
124727 126314     sqlite3DbFree(db, sAggInfo.aCol);
124728 126315     sqlite3DbFree(db, sAggInfo.aFunc);
124729 126316   #if SELECTTRACE_ENABLED
124730 126317     SELECTTRACE(1,pParse,p,("end processing\n"));
124731         -  pParse->nSelectIndent--;
124732 126318   #endif
124733 126319     return rc;
124734 126320   }
124735 126321   
124736 126322   /************** End of select.c **********************************************/
124737 126323   /************** Begin file table.c *******************************************/
124738 126324   /*
................................................................................
127020 128606   
127021 128607     /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
127022 128608     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
127023 128609     if( rc!=SQLITE_OK ) return rc;
127024 128610     while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
127025 128611       const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
127026 128612       assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
127027         -    if( zSubSql ){
127028         -      assert( zSubSql[0]!='S' );
       128613  +    assert( sqlite3_strnicmp(zSubSql,"SELECT",6)!=0 || CORRUPT_DB );
       128614  +    if( zSubSql && zSubSql[0]!='S' ){
127029 128615         rc = execSql(db, pzErrMsg, zSubSql);
127030 128616         if( rc!=SQLITE_OK ) break;
127031 128617       }
127032 128618     }
127033 128619     assert( rc!=SQLITE_ROW );
127034 128620     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
127035 128621     if( rc ){
................................................................................
130559 132145         testcase( pStart->wtFlags & TERM_VIRTUAL );
130560 132146         pX = pStart->pExpr;
130561 132147         assert( pX!=0 );
130562 132148         testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
130563 132149         if( sqlite3ExprIsVector(pX->pRight) ){
130564 132150           r1 = rTemp = sqlite3GetTempReg(pParse);
130565 132151           codeExprOrVector(pParse, pX->pRight, r1, 1);
130566         -        op = aMoveOp[(pX->op - TK_GT) | 0x0001];
       132152  +        testcase( pX->op==TK_GT );
       132153  +        testcase( pX->op==TK_GE );
       132154  +        testcase( pX->op==TK_LT );
       132155  +        testcase( pX->op==TK_LE );
       132156  +        op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
       132157  +        assert( pX->op!=TK_GT || op==OP_SeekGE );
       132158  +        assert( pX->op!=TK_GE || op==OP_SeekGE );
       132159  +        assert( pX->op!=TK_LT || op==OP_SeekLE );
       132160  +        assert( pX->op!=TK_LE || op==OP_SeekLE );
130567 132161         }else{
130568 132162           r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
130569 132163           disableTerm(pLevel, pStart);
130570 132164           op = aMoveOp[(pX->op - TK_GT)];
130571 132165         }
130572 132166         sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
130573 132167         VdbeComment((v, "pk"));
................................................................................
131283 132877           continue;
131284 132878         }
131285 132879         if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
131286 132880           if( iNext==0 ) iNext = 3;
131287 132881           continue;
131288 132882         }
131289 132883   
131290         -      if( pTerm->wtFlags & TERM_LIKECOND ){
       132884  +      if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
131291 132885           /* If the TERM_LIKECOND flag is set, that means that the range search
131292 132886           ** is sufficient to guarantee that the LIKE operator is true, so we
131293 132887           ** can skip the call to the like(A,B) function.  But this only works
131294 132888           ** for strings.  So do not skip the call to the function on the pass
131295 132889           ** that compares BLOBs. */
131296 132890   #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
131297 132891           continue;
131298 132892   #else
131299 132893           u32 x = pLevel->iLikeRepCntr;
131300         -        assert( x>0 );
131301         -        skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If, (int)(x>>1));
       132894  +        if( x>0 ){
       132895  +          skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
       132896  +        }
131302 132897           VdbeCoverage(v);
131303 132898   #endif
131304 132899         }
131305 132900   #ifdef WHERETRACE_ENABLED /* 0xffff */
131306 132901         if( sqlite3WhereTrace ){
131307 132902           VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
131308 132903                            pWC->nTerm-j, pTerm, iLoop));
................................................................................
131334 132929       pE = pTerm->pExpr;
131335 132930       assert( !ExprHasProperty(pE, EP_FromJoin) );
131336 132931       assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
131337 132932       pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
131338 132933                       WO_EQ|WO_IN|WO_IS, 0);
131339 132934       if( pAlt==0 ) continue;
131340 132935       if( pAlt->wtFlags & (TERM_CODED) ) continue;
       132936  +    if( (pAlt->eOperator & WO_IN) 
       132937  +     && (pAlt->pExpr->flags & EP_xIsSelect)
       132938  +     && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
       132939  +    ){
       132940  +      continue;
       132941  +    }
131341 132942       testcase( pAlt->eOperator & WO_EQ );
131342 132943       testcase( pAlt->eOperator & WO_IS );
131343 132944       testcase( pAlt->eOperator & WO_IN );
131344 132945       VdbeModuleComment((v, "begin transitive constraint"));
131345 132946       sEAlt = *pAlt->pExpr;
131346 132947       sEAlt.pLeft = pE->pLeft;
131347 132948       sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL);
................................................................................
132248 133849       mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
132249 133850       mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
132250 133851       if( ALWAYS(pSrc!=0) ){
132251 133852         int i;
132252 133853         for(i=0; i<pSrc->nSrc; i++){
132253 133854           mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
132254 133855           mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
       133856  +        if( pSrc->a[i].fg.isTabFunc ){
       133857  +          mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
       133858  +        }
132255 133859         }
132256 133860       }
132257 133861       pS = pS->pPrior;
132258 133862     }
132259 133863     return mask;
132260 133864   }
132261 133865   
................................................................................
132660 134264   
132661 134265         pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
132662 134266         transferJoinMarkings(pNew, pExpr);
132663 134267         idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
132664 134268         exprAnalyze(pSrc, pWC, idxNew);
132665 134269       }
132666 134270       pTerm = &pWC->a[idxTerm];
132667         -    pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL;  /* Disable the original */
       134271  +    pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL;  /* Disable the original */
132668 134272       pTerm->eOperator = 0;
132669 134273     }
132670 134274   
132671 134275     /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
132672 134276     ** a virtual term for each vector component. The expression object
132673 134277     ** used by each such virtual term is pExpr (the full vector IN(...) 
132674 134278     ** expression). The WhereTerm.iField variable identifies the index within
................................................................................
135385 136989       }else if( eOp & (WO_EQ|WO_IS) ){
135386 136990         int iCol = pProbe->aiColumn[saved_nEq];
135387 136991         pNew->wsFlags |= WHERE_COLUMN_EQ;
135388 136992         assert( saved_nEq==pNew->u.btree.nEq );
135389 136993         if( iCol==XN_ROWID 
135390 136994          || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
135391 136995         ){
135392         -        if( iCol>=0 && pProbe->uniqNotNull==0 ){
135393         -          pNew->wsFlags |= WHERE_UNQ_WANTED;
135394         -        }else{
       136996  +        if( iCol==XN_ROWID || pProbe->uniqNotNull 
       136997  +         || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ) 
       136998  +        ){
135395 136999             pNew->wsFlags |= WHERE_ONEROW;
       137000  +        }else{
       137001  +          pNew->wsFlags |= WHERE_UNQ_WANTED;
135396 137002           }
135397 137003         }
135398 137004       }else if( eOp & WO_ISNULL ){
135399 137005         pNew->wsFlags |= WHERE_COLUMN_NULL;
135400 137006       }else if( eOp & (WO_GT|WO_GE) ){
135401 137007         testcase( eOp & WO_GT );
135402 137008         testcase( eOp & WO_GE );
................................................................................
137535 139141     ** preserves SQLite's legacy behaviour in the following two cases:
137536 139142     **
137537 139143     **   FROM ... WHERE random()>0;           -- eval random() once per row
137538 139144     **   FROM ... WHERE (SELECT random())>0;  -- eval random() once overall
137539 139145     */
137540 139146     for(ii=0; ii<sWLB.pWC->nTerm; ii++){
137541 139147       WhereTerm *pT = &sWLB.pWC->a[ii];
       139148  +    if( pT->wtFlags & TERM_VIRTUAL ) continue;
137542 139149       if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
137543 139150         sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
137544 139151         pT->wtFlags |= TERM_CODED;
137545 139152       }
137546 139153     }
137547 139154   
137548 139155     if( wctrlFlags & WHERE_WANT_DISTINCT ){
................................................................................
139993 141600     if( stateno>YY_MAX_SHIFT ) return stateno;
139994 141601     assert( stateno <= YY_SHIFT_COUNT );
139995 141602   #if defined(YYCOVERAGE)
139996 141603     yycoverage[stateno][iLookAhead] = 1;
139997 141604   #endif
139998 141605     do{
139999 141606       i = yy_shift_ofst[stateno];
140000         -    assert( i>=0 && i+YYNTOKEN<=sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
       141607  +    assert( i>=0 );
       141608  +    assert( i+YYNTOKEN<=(int)sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
140001 141609       assert( iLookAhead!=YYNOCODE );
140002 141610       assert( iLookAhead < YYNTOKEN );
140003 141611       i += iLookAhead;
140004 141612       if( yy_lookahead[i]!=iLookAhead ){
140005 141613   #ifdef YYFALLBACK
140006 141614         YYCODETYPE iFallback;            /* Fallback token */
140007 141615         if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
................................................................................
140707 142315     Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[-1].minor.yy314, 0);
140708 142316     sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy336);
140709 142317   }
140710 142318           break;
140711 142319         case 34: /* ccons ::= DEFAULT scanpt ID|INDEXED */
140712 142320   {
140713 142321     Expr *p = tokenExpr(pParse, TK_STRING, yymsp[0].minor.yy0);
       142322  +  if( p ){
       142323  +    sqlite3ExprIdToTrueFalse(p);
       142324  +    testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
       142325  +  }
140714 142326     sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
140715 142327   }
140716 142328           break;
140717 142329         case 35: /* ccons ::= NOT NULL onconf */
140718 142330   {sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
140719 142331           break;
140720 142332         case 36: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
................................................................................
140900 142512     ** then extract the first few alphanumeric characters from within that
140901 142513     ** comment to be the zSelName value.  Otherwise, the label is #N where
140902 142514     ** is an integer that is incremented with each SELECT statement seen.
140903 142515     */
140904 142516     if( yymsp[-8].minor.yy387!=0 ){
140905 142517       const char *z = s.z+6;
140906 142518       int i;
140907         -    sqlite3_snprintf(sizeof(yymsp[-8].minor.yy387->zSelName), yymsp[-8].minor.yy387->zSelName, "#%d",
140908         -                     ++pParse->nSelect);
       142519  +    sqlite3_snprintf(sizeof(yymsp[-8].minor.yy387->zSelName), yymsp[-8].minor.yy387->zSelName,"#%d",++pParse->nSelect);
140909 142520       while( z[0]==' ' ) z++;
140910 142521       if( z[0]=='/' && z[1]=='*' ){
140911 142522         z += 2;
140912 142523         while( z[0]==' ' ) z++;
140913 142524         for(i=0; sqlite3Isalnum(z[i]); i++){}
140914 142525         sqlite3_snprintf(sizeof(yymsp[-8].minor.yy387->zSelName), yymsp[-8].minor.yy387->zSelName, "%.*s", i, z);
140915 142526       }
................................................................................
143600 145211       if( sqlite3GlobalConfig.isPCacheInit==0 ){
143601 145212         rc = sqlite3PcacheInitialize();
143602 145213       }
143603 145214       if( rc==SQLITE_OK ){
143604 145215         sqlite3GlobalConfig.isPCacheInit = 1;
143605 145216         rc = sqlite3OsInit();
143606 145217       }
       145218  +#ifdef SQLITE_ENABLE_DESERIALIZE
       145219  +    if( rc==SQLITE_OK ){
       145220  +      rc = sqlite3MemdbInit();
       145221  +    }
       145222  +#endif
143607 145223       if( rc==SQLITE_OK ){
143608 145224         sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
143609 145225             sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
143610 145226         sqlite3GlobalConfig.isInit = 1;
143611 145227   #ifdef SQLITE_EXTRA_INIT
143612 145228         bRunExtraInit = 1;
143613 145229   #endif
................................................................................
143632 145248     ** been compiled correctly.  It is important to run this code, but
143633 145249     ** we don't want to run it too often and soak up CPU cycles for no
143634 145250     ** reason.  So we run it once during initialization.
143635 145251     */
143636 145252   #ifndef NDEBUG
143637 145253   #ifndef SQLITE_OMIT_FLOATING_POINT
143638 145254     /* This section of code's only "output" is via assert() statements. */
143639         -  if ( rc==SQLITE_OK ){
       145255  +  if( rc==SQLITE_OK ){
143640 145256       u64 x = (((u64)1)<<63)-1;
143641 145257       double y;
143642 145258       assert(sizeof(x)==8);
143643 145259       assert(sizeof(x)==sizeof(y));
143644 145260       memcpy(&y, &x, 8);
143645 145261       assert( sqlite3IsNaN(y) );
143646 145262     }
................................................................................
144799 146415   #else
144800 146416       /* SQLITE_NOLFS       */ 0,
144801 146417   #endif
144802 146418       /* SQLITE_AUTH        */ "authorization denied",
144803 146419       /* SQLITE_FORMAT      */ 0,
144804 146420       /* SQLITE_RANGE       */ "column index out of range",
144805 146421       /* SQLITE_NOTADB      */ "file is not a database",
       146422  +    /* SQLITE_NOTICE      */ "notification message",
       146423  +    /* SQLITE_WARNING     */ "warning message",
144806 146424     };
144807 146425     const char *zErr = "unknown error";
144808 146426     switch( rc ){
144809 146427       case SQLITE_ABORT_ROLLBACK: {
144810 146428         zErr = "abort due to ROLLBACK";
144811 146429         break;
       146430  +    }
       146431  +    case SQLITE_ROW: {
       146432  +      zErr = "another row available";
       146433  +      break;
       146434  +    }
       146435  +    case SQLITE_DONE: {
       146436  +      zErr = "no more rows available";
       146437  +      break;
144812 146438       }
144813 146439       default: {
144814 146440         rc &= 0xff;
144815 146441         if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
144816 146442           zErr = aMsg[rc];
144817 146443         }
144818 146444         break;
................................................................................
144822 146448   }
144823 146449   
144824 146450   /*
144825 146451   ** This routine implements a busy callback that sleeps and tries
144826 146452   ** again until a timeout value is reached.  The timeout value is
144827 146453   ** an integer number of milliseconds passed in as the first
144828 146454   ** argument.
       146455  +**
       146456  +** Return non-zero to retry the lock.  Return zero to stop trying
       146457  +** and cause SQLite to return SQLITE_BUSY.
144829 146458   */
144830 146459   static int sqliteDefaultBusyCallback(
144831         - void *ptr,               /* Database connection */
144832         - int count                /* Number of times table has been busy */
       146460  +  void *ptr,               /* Database connection */
       146461  +  int count,               /* Number of times table has been busy */
       146462  +  sqlite3_file *pFile      /* The file on which the lock occurred */
144833 146463   ){
144834 146464   #if SQLITE_OS_WIN || HAVE_USLEEP
       146465  +  /* This case is for systems that have support for sleeping for fractions of
       146466  +  ** a second.  Examples:  All windows systems, unix systems with usleep() */
144835 146467     static const u8 delays[] =
144836 146468        { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
144837 146469     static const u8 totals[] =
144838 146470        { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
144839 146471   # define NDELAY ArraySize(delays)
144840 146472     sqlite3 *db = (sqlite3 *)ptr;
144841         -  int timeout = db->busyTimeout;
       146473  +  int tmout = db->busyTimeout;
144842 146474     int delay, prior;
144843 146475   
       146476  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
       146477  +  if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT,&tmout)==SQLITE_OK ){
       146478  +    if( count ){
       146479  +      tmout = 0;
       146480  +      sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT, &tmout);
       146481  +      return 0;
       146482  +    }else{
       146483  +      return 1;
       146484  +    }
       146485  +  }
       146486  +#else
       146487  +  UNUSED_PARAMETER(pFile);
       146488  +#endif
144844 146489     assert( count>=0 );
144845 146490     if( count < NDELAY ){
144846 146491       delay = delays[count];
144847 146492       prior = totals[count];
144848 146493     }else{
144849 146494       delay = delays[NDELAY-1];
144850 146495       prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
144851 146496     }
144852         -  if( prior + delay > timeout ){
144853         -    delay = timeout - prior;
       146497  +  if( prior + delay > tmout ){
       146498  +    delay = tmout - prior;
144854 146499       if( delay<=0 ) return 0;
144855 146500     }
144856 146501     sqlite3OsSleep(db->pVfs, delay*1000);
144857 146502     return 1;
144858 146503   #else
       146504  +  /* This case for unix systems that lack usleep() support.  Sleeping
       146505  +  ** must be done in increments of whole seconds */
144859 146506     sqlite3 *db = (sqlite3 *)ptr;
144860         -  int timeout = ((sqlite3 *)ptr)->busyTimeout;
144861         -  if( (count+1)*1000 > timeout ){
       146507  +  int tmout = ((sqlite3 *)ptr)->busyTimeout;
       146508  +  UNUSED_PARAMETER(pFile);
       146509  +  if( (count+1)*1000 > tmout ){
144862 146510       return 0;
144863 146511     }
144864 146512     sqlite3OsSleep(db->pVfs, 1000000);
144865 146513     return 1;
144866 146514   #endif
144867 146515   }
144868 146516   
144869 146517   /*
144870 146518   ** Invoke the given busy handler.
144871 146519   **
144872         -** This routine is called when an operation failed with a lock.
       146520  +** This routine is called when an operation failed to acquire a
       146521  +** lock on VFS file pFile.
       146522  +**
144873 146523   ** If this routine returns non-zero, the lock is retried.  If it
144874 146524   ** returns 0, the operation aborts with an SQLITE_BUSY error.
144875 146525   */
144876         -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
       146526  +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){
144877 146527     int rc;
144878         -  if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
144879         -  rc = p->xFunc(p->pArg, p->nBusy);
       146528  +  if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
       146529  +  if( p->bExtraFileArg ){
       146530  +    /* Add an extra parameter with the pFile pointer to the end of the
       146531  +    ** callback argument list */
       146532  +    int (*xTra)(void*,int,sqlite3_file*);
       146533  +    xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler;
       146534  +    rc = xTra(p->pBusyArg, p->nBusy, pFile);
       146535  +  }else{
       146536  +    /* Legacy style busy handler callback */
       146537  +    rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
       146538  +  }
144880 146539     if( rc==0 ){
144881 146540       p->nBusy = -1;
144882 146541     }else{
144883 146542       p->nBusy++;
144884 146543     }
144885 146544     return rc; 
144886 146545   }
................................................................................
144894 146553     int (*xBusy)(void*,int),
144895 146554     void *pArg
144896 146555   ){
144897 146556   #ifdef SQLITE_ENABLE_API_ARMOR
144898 146557     if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
144899 146558   #endif
144900 146559     sqlite3_mutex_enter(db->mutex);
144901         -  db->busyHandler.xFunc = xBusy;
144902         -  db->busyHandler.pArg = pArg;
       146560  +  db->busyHandler.xBusyHandler = xBusy;
       146561  +  db->busyHandler.pBusyArg = pArg;
144903 146562     db->busyHandler.nBusy = 0;
       146563  +  db->busyHandler.bExtraFileArg = 0;
144904 146564     db->busyTimeout = 0;
144905 146565     sqlite3_mutex_leave(db->mutex);
144906 146566     return SQLITE_OK;
144907 146567   }
144908 146568   
144909 146569   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
144910 146570   /*
................................................................................
144944 146604   ** specified number of milliseconds before returning 0.
144945 146605   */
144946 146606   SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
144947 146607   #ifdef SQLITE_ENABLE_API_ARMOR
144948 146608     if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
144949 146609   #endif
144950 146610     if( ms>0 ){
144951         -    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
       146611  +    sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
       146612  +                             (void*)db);
144952 146613       db->busyTimeout = ms;
       146614  +    db->busyHandler.bExtraFileArg = 1;
144953 146615     }else{
144954 146616       sqlite3_busy_handler(db, 0, 0);
144955 146617     }
144956 146618     return SQLITE_OK;
144957 146619   }
144958 146620   
144959 146621   /*
................................................................................
146938 148600         rc = SQLITE_OK;
146939 148601       }else if( op==SQLITE_FCNTL_VFS_POINTER ){
146940 148602         *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
146941 148603         rc = SQLITE_OK;
146942 148604       }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
146943 148605         *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
146944 148606         rc = SQLITE_OK;
146945         -    }else if( fd->pMethods ){
       148607  +    }else{
146946 148608         rc = sqlite3OsFileControl(fd, op, pArg);
146947         -    }else{
146948         -      rc = SQLITE_NOTFOUND;
146949 148609       }
146950 148610       sqlite3BtreeLeave(pBtree);
146951 148611     }
146952 148612     sqlite3_mutex_leave(db->mutex);
146953 148613     return rc;
146954 148614   }
146955 148615   
................................................................................
160792 162452     sqlite3_stmt *pStmt;
160793 162453     int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
160794 162454     if( rc==SQLITE_OK ){
160795 162455       sqlite3_bind_int64(pStmt, 1, iBlock);
160796 162456       sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
160797 162457       sqlite3_step(pStmt);
160798 162458       rc = sqlite3_reset(pStmt);
       162459  +    sqlite3_bind_null(pStmt, 2);
160799 162460     }
160800 162461     return rc;
160801 162462   }
160802 162463   
160803 162464   /*
160804 162465   ** Find the largest relative level number in the table. If successful, set
160805 162466   ** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
................................................................................
160848 162509         char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
160849 162510         if( !zEnd ) return SQLITE_NOMEM;
160850 162511         sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
160851 162512       }
160852 162513       sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
160853 162514       sqlite3_step(pStmt);
160854 162515       rc = sqlite3_reset(pStmt);
       162516  +    sqlite3_bind_null(pStmt, 6);
160855 162517     }
160856 162518     return rc;
160857 162519   }
160858 162520   
160859 162521   /*
160860 162522   ** Return the size of the common prefix (if any) shared by zPrev and
160861 162523   ** zNext, in bytes. For example, 
................................................................................
162327 163989       *pRC = rc;
162328 163990       return;
162329 163991     }
162330 163992     sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
162331 163993     sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
162332 163994     sqlite3_step(pStmt);
162333 163995     *pRC = sqlite3_reset(pStmt);
       163996  +  sqlite3_bind_null(pStmt, 2);
162334 163997     sqlite3_free(a);
162335 163998   }
162336 163999   
162337 164000   /*
162338 164001   ** Merge the entire database so that there is one segment for each 
162339 164002   ** iIndex/iLangid combination.
162340 164003   */
................................................................................
163515 165178       if( rc==SQLITE_OK ){
163516 165179         sqlite3_bind_int64(pChomp, 1, iNewStart);
163517 165180         sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
163518 165181         sqlite3_bind_int64(pChomp, 3, iAbsLevel);
163519 165182         sqlite3_bind_int(pChomp, 4, iIdx);
163520 165183         sqlite3_step(pChomp);
163521 165184         rc = sqlite3_reset(pChomp);
       165185  +      sqlite3_bind_null(pChomp, 2);
163522 165186       }
163523 165187     }
163524 165188   
163525 165189     sqlite3_free(root.a);
163526 165190     sqlite3_free(block.a);
163527 165191     return rc;
163528 165192   }
................................................................................
163594 165258   
163595 165259     rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
163596 165260     if( rc==SQLITE_OK ){
163597 165261       sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
163598 165262       sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
163599 165263       sqlite3_step(pReplace);
163600 165264       rc = sqlite3_reset(pReplace);
       165265  +    sqlite3_bind_null(pReplace, 2);
163601 165266     }
163602 165267   
163603 165268     return rc;
163604 165269   }
163605 165270   
163606 165271   /*
163607 165272   ** Load an incr-merge hint from the database. The incr-merge hint, if one 
................................................................................
164408 166073     sqlite3_vtab *pVtab,            /* FTS3 vtab object */
164409 166074     int nArg,                       /* Size of argument array */
164410 166075     sqlite3_value **apVal,          /* Array of arguments */
164411 166076     sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
164412 166077   ){
164413 166078     Fts3Table *p = (Fts3Table *)pVtab;
164414 166079     int rc = SQLITE_OK;             /* Return Code */
164415         -  int isRemove = 0;               /* True for an UPDATE or DELETE */
164416 166080     u32 *aSzIns = 0;                /* Sizes of inserted documents */
164417 166081     u32 *aSzDel = 0;                /* Sizes of deleted documents */
164418 166082     int nChng = 0;                  /* Net change in number of documents */
164419 166083     int bInsertDone = 0;
164420 166084   
164421 166085     /* At this point it must be known if the %_stat table exists or not.
164422 166086     ** So bHasStat may not be 2.  */
................................................................................
164506 166170       goto update_out;
164507 166171     }
164508 166172   
164509 166173     /* If this is a DELETE or UPDATE operation, remove the old record. */
164510 166174     if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
164511 166175       assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
164512 166176       rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
164513         -    isRemove = 1;
164514 166177     }
164515 166178     
164516 166179     /* If this is an INSERT or UPDATE operation, insert the new record. */
164517 166180     if( nArg>1 && rc==SQLITE_OK ){
164518 166181       int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
164519 166182       if( bInsertDone==0 ){
164520 166183         rc = fts3InsertData(p, apVal, pRowid);
164521 166184         if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
164522 166185           rc = FTS_CORRUPT_VTAB;
164523 166186         }
164524 166187       }
164525         -    if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
       166188  +    if( rc==SQLITE_OK ){
164526 166189         rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
164527 166190       }
164528 166191       if( rc==SQLITE_OK ){
164529 166192         assert( p->iPrevDocid==*pRowid );
164530 166193         rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
164531 166194       }
164532 166195       if( p->bHasDocsize ){
................................................................................
167826 169489       }else{
167827 169490         sqlite3_bind_null(p, 1);
167828 169491       }
167829 169492       sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
167830 169493       sqlite3_step(p);
167831 169494       pNode->isDirty = 0;
167832 169495       rc = sqlite3_reset(p);
       169496  +    sqlite3_bind_null(p, 2);
167833 169497       if( pNode->iNode==0 && rc==SQLITE_OK ){
167834 169498         pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
167835 169499         nodeHashInsert(pRtree, pNode);
167836 169500       }
167837 169501     }
167838 169502     return rc;
167839 169503   }
................................................................................
174513 176177   
174514 176178       while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
174515 176179         int bKey = sqlite3_column_int(pXInfo, 5);
174516 176180         if( bKey ){
174517 176181           int iCid = sqlite3_column_int(pXInfo, 1);
174518 176182           int bDesc = sqlite3_column_int(pXInfo, 3);
174519 176183           const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
174520         -        zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma, 
       176184  +        zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma, 
174521 176185               iCid, pIter->azTblType[iCid], zCollate
174522 176186           );
174523 176187           zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
174524 176188           zComma = ", ";
174525 176189         }
174526 176190       }
174527 176191       zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
................................................................................
174574 176238         );
174575 176239   
174576 176240         if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
174577 176241           /* If the target table column is an "INTEGER PRIMARY KEY", add
174578 176242           ** "PRIMARY KEY" to the imposter table column declaration. */
174579 176243           zPk = "PRIMARY KEY ";
174580 176244         }
174581         -      zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s", 
       176245  +      zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s", 
174582 176246             zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
174583 176247             (pIter->abNotNull[iCol] ? " NOT NULL" : "")
174584 176248         );
174585 176249         zComma = ", ";
174586 176250       }
174587 176251   
174588 176252       if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
................................................................................
178008 179672     pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
178009 179673   
178010 179674     /* If connected to a ZIPVFS backend, override the page size and
178011 179675     ** offset with actual values obtained from ZIPVFS.
178012 179676     */
178013 179677     fd = sqlite3PagerFile(pPager);
178014 179678     x[0] = pCsr->iPageno;
178015         -  if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
       179679  +  if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
178016 179680       pCsr->iOffset = x[0];
178017 179681       pCsr->szPage = (int)x[1];
178018 179682     }
178019 179683   }
178020 179684   
178021 179685   /*
178022 179686   ** Move a statvfs cursor to the next entry in the file.
................................................................................
178938 180602   **
178939 180603   ** As in the changeset format, each field of the single record that is part
178940 180604   ** of a patchset change is associated with the correspondingly positioned
178941 180605   ** table column, counting from left to right within the CREATE TABLE 
178942 180606   ** statement.
178943 180607   **
178944 180608   ** For a DELETE change, all fields within the record except those associated
178945         -** with PRIMARY KEY columns are set to "undefined". The PRIMARY KEY fields
178946         -** contain the values identifying the row to delete.
       180609  +** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
       180610  +** values identifying the row to delete.
178947 180611   **
178948 180612   ** For an UPDATE change, all fields except those associated with PRIMARY KEY
178949 180613   ** columns and columns that are modified by the UPDATE are set to "undefined".
178950 180614   ** PRIMARY KEY fields contain the values identifying the table row to update,
178951 180615   ** and fields associated with modified columns contain the new column values.
178952 180616   **
178953 180617   ** The records associated with INSERT changes are in the same format as for
................................................................................
179222 180886   ** The buffer that the argument points to contains a serialized SQL value.
179223 180887   ** Return the number of bytes of space occupied by the value (including
179224 180888   ** the type byte).
179225 180889   */
179226 180890   static int sessionSerialLen(u8 *a){
179227 180891     int e = *a;
179228 180892     int n;
179229         -  if( e==0 ) return 1;
       180893  +  if( e==0 || e==0xFF ) return 1;
179230 180894     if( e==SQLITE_NULL ) return 1;
179231 180895     if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
179232 180896     return sessionVarintGet(&a[1], &n) + 1 + n;
179233 180897   }
179234 180898   
179235 180899   /*
179236 180900   ** Based on the primary key values stored in change aRecord, calculate a
................................................................................
179302 180966     int iCol;                       /* Used to iterate through table columns */
179303 180967   
179304 180968     for(iCol=0; iCol<pTab->nCol; iCol++){
179305 180969       if( pTab->abPK[iCol] ){
179306 180970         int n1 = sessionSerialLen(a1);
179307 180971         int n2 = sessionSerialLen(a2);
179308 180972   
179309         -      if( pTab->abPK[iCol] && (n1!=n2 || memcmp(a1, a2, n1)) ){
       180973  +      if( n1!=n2 || memcmp(a1, a2, n1) ){
179310 180974           return 0;
179311 180975         }
179312 180976         a1 += n1;
179313 180977         a2 += n2;
179314 180978       }else{
179315 180979         if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
179316 180980         if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
................................................................................
179545 181209           a += sessionVarintGet(a, &n);
179546 181210           if( sqlite3_value_bytes(pVal)!=n ) return 0;
179547 181211           if( eType==SQLITE_TEXT ){
179548 181212             z = sqlite3_value_text(pVal);
179549 181213           }else{
179550 181214             z = sqlite3_value_blob(pVal);
179551 181215           }
179552         -        if( memcmp(a, z, n) ) return 0;
       181216  +        if( n>0 && memcmp(a, z, n) ) return 0;
179553 181217           a += n;
179554 181218         }
179555 181219       }
179556 181220     }
179557 181221   
179558 181222     return 1;
179559 181223   }
................................................................................
179821 181485     int op,                         /* One of SQLITE_UPDATE, INSERT, DELETE */
179822 181486     sqlite3_session *pSession,      /* Session object pTab is attached to */
179823 181487     SessionTable *pTab              /* Table that change applies to */
179824 181488   ){
179825 181489     int iHash; 
179826 181490     int bNull = 0; 
179827 181491     int rc = SQLITE_OK;
179828         -  SessionStat1Ctx stat1;
       181492  +  SessionStat1Ctx stat1 = {0};
179829 181493   
179830 181494     if( pSession->rc ) return;
179831 181495   
179832 181496     /* Load table details if required */
179833 181497     if( sessionInitTable(pSession, pTab) ) return;
179834 181498   
179835 181499     /* Check the number of columns in this xPreUpdate call matches the 
................................................................................
180889 182553     int nSql = -1;
180890 182554   
180891 182555     if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
180892 182556       zSql = sqlite3_mprintf(
180893 182557           "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
180894 182558           "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
180895 182559       );
       182560  +    if( zSql==0 ) rc = SQLITE_NOMEM;
180896 182561     }else{
180897 182562       int i;
180898 182563       const char *zSep = "";
180899 182564       SessionBuffer buf = {0, 0, 0};
180900 182565   
180901 182566       sessionAppendStr(&buf, "SELECT * FROM ", &rc);
180902 182567       sessionAppendIdent(&buf, zDb, &rc);
................................................................................
181426 183091     int rc = SQLITE_OK;
181427 183092   
181428 183093     for(i=0; i<nCol && rc==SQLITE_OK; i++){
181429 183094       int eType = 0;                /* Type of value (SQLITE_NULL, TEXT etc.) */
181430 183095       if( abPK && abPK[i]==0 ) continue;
181431 183096       rc = sessionInputBuffer(pIn, 9);
181432 183097       if( rc==SQLITE_OK ){
181433         -      eType = pIn->aData[pIn->iNext++];
181434         -    }
181435         -
181436         -    assert( apOut[i]==0 );
181437         -    if( eType ){
181438         -      apOut[i] = sqlite3ValueNew(0);
181439         -      if( !apOut[i] ) rc = SQLITE_NOMEM;
       183098  +      if( pIn->iNext>=pIn->nData ){
       183099  +        rc = SQLITE_CORRUPT_BKPT;
       183100  +      }else{
       183101  +        eType = pIn->aData[pIn->iNext++];
       183102  +        assert( apOut[i]==0 );
       183103  +        if( eType ){
       183104  +          apOut[i] = sqlite3ValueNew(0);
       183105  +          if( !apOut[i] ) rc = SQLITE_NOMEM;
       183106  +        }
       183107  +      }
181440 183108       }
181441 183109   
181442 183110       if( rc==SQLITE_OK ){
181443 183111         u8 *aVal = &pIn->aData[pIn->iNext];
181444 183112         if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
181445 183113           int nByte;
181446 183114           pIn->iNext += sessionVarintGet(aVal, &nByte);
181447 183115           rc = sessionInputBuffer(pIn, nByte);
181448 183116           if( rc==SQLITE_OK ){
181449         -          u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
181450         -          rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
       183117  +          if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
       183118  +            rc = SQLITE_CORRUPT_BKPT;
       183119  +          }else{
       183120  +            u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
       183121  +            rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
       183122  +            pIn->iNext += nByte;
       183123  +          }
181451 183124           }
181452         -        pIn->iNext += nByte;
181453 183125         }
181454 183126         if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
181455 183127           sqlite3_int64 v = sessionGetI64(aVal);
181456 183128           if( eType==SQLITE_INTEGER ){
181457 183129             sqlite3VdbeMemSetInt64(apOut[i], v);
181458 183130           }else{
181459 183131             double d;
................................................................................
181485 183157     int rc = SQLITE_OK;
181486 183158     int nCol = 0;
181487 183159     int nRead = 0;
181488 183160   
181489 183161     rc = sessionInputBuffer(pIn, 9);
181490 183162     if( rc==SQLITE_OK ){
181491 183163       nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
181492         -    rc = sessionInputBuffer(pIn, nRead+nCol+100);
181493         -    nRead += nCol;
       183164  +    /* The hard upper limit for the number of columns in an SQLite
       183165  +    ** database table is, according to sqliteLimit.h, 32676. So 
       183166  +    ** consider any table-header that purports to have more than 65536 
       183167  +    ** columns to be corrupt. This is convenient because otherwise, 
       183168  +    ** if the (nCol>65536) condition below were omitted, a sufficiently 
       183169  +    ** large value for nCol may cause nRead to wrap around and become 
       183170  +    ** negative. Leading to a crash. */
       183171  +    if( nCol<0 || nCol>65536 ){
       183172  +      rc = SQLITE_CORRUPT_BKPT;
       183173  +    }else{
       183174  +      rc = sessionInputBuffer(pIn, nRead+nCol+100);
       183175  +      nRead += nCol;
       183176  +    }
181494 183177     }
181495 183178   
181496 183179     while( rc==SQLITE_OK ){
181497 183180       while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
181498 183181         nRead++;
181499 183182       }
181500 183183       if( (pIn->iNext + nRead)<pIn->nData ) break;
................................................................................
181563 183246     assert( p->rc==SQLITE_OK );
181564 183247   
181565 183248     rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
181566 183249     if( rc==SQLITE_OK ){
181567 183250       int nByte;
181568 183251       int nVarint;
181569 183252       nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
181570         -    nCopy -= nVarint;
181571         -    p->in.iNext += nVarint;
181572         -    nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
181573         -    p->tblhdr.nBuf = 0;
181574         -    sessionBufferGrow(&p->tblhdr, nByte, &rc);
       183253  +    if( p->nCol>0 ){
       183254  +      nCopy -= nVarint;
       183255  +      p->in.iNext += nVarint;
       183256  +      nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
       183257  +      p->tblhdr.nBuf = 0;
       183258  +      sessionBufferGrow(&p->tblhdr, nByte, &rc);
       183259  +    }else{
       183260  +      rc = SQLITE_CORRUPT_BKPT;
       183261  +    }
181575 183262     }
181576 183263   
181577 183264     if( rc==SQLITE_OK ){
181578 183265       int iPK = sizeof(sqlite3_value*)*p->nCol*2;
181579 183266       memset(p->tblhdr.aBuf, 0, iPK);
181580 183267       memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
181581 183268       p->in.iNext += nCopy;
................................................................................
181602 183289   ** successfully advanced to the next change in the changeset, an SQLite 
181603 183290   ** error code if an error occurs, or SQLITE_DONE if there are no further 
181604 183291   ** changes in the changeset.
181605 183292   */
181606 183293   static int sessionChangesetNext(
181607 183294     sqlite3_changeset_iter *p,      /* Changeset iterator */
181608 183295     u8 **paRec,                     /* If non-NULL, store record pointer here */
181609         -  int *pnRec                      /* If non-NULL, store size of record here */
       183296  +  int *pnRec,                     /* If non-NULL, store size of record here */
       183297  +  int *pbNew                      /* If non-NULL, true if new table */
181610 183298   ){
181611 183299     int i;
181612 183300     u8 op;
181613 183301   
181614 183302     assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
181615 183303   
181616 183304     /* If the iterator is in the error-state, return immediately. */
................................................................................
181637 183325     }
181638 183326   
181639 183327     sessionDiscardData(&p->in);
181640 183328     p->in.iCurrent = p->in.iNext;
181641 183329   
181642 183330     op = p->in.aData[p->in.iNext++];
181643 183331     while( op=='T' || op=='P' ){
       183332  +    if( pbNew ) *pbNew = 1;
181644 183333       p->bPatchset = (op=='P');
181645 183334       if( sessionChangesetReadTblhdr(p) ) return p->rc;
181646 183335       if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
181647 183336       p->in.iCurrent = p->in.iNext;
181648 183337       if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
181649 183338       op = p->in.aData[p->in.iNext++];
181650 183339     }
       183340  +
       183341  +  if( p->zTab==0 ){
       183342  +    /* The first record in the changeset is not a table header. Must be a
       183343  +    ** corrupt changeset. */
       183344  +    assert( p->in.iNext==1 );
       183345  +    return (p->rc = SQLITE_CORRUPT_BKPT);
       183346  +  }
181651 183347   
181652 183348     p->op = op;
181653 183349     p->bIndirect = p->in.aData[p->in.iNext++];
181654 183350     if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
181655 183351       return (p->rc = SQLITE_CORRUPT_BKPT);
181656 183352     }
181657 183353   
................................................................................
181687 183383       if( p->bPatchset && p->op==SQLITE_UPDATE ){
181688 183384         /* If this is an UPDATE that is part of a patchset, then all PK and
181689 183385         ** modified fields are present in the new.* record. The old.* record
181690 183386         ** is currently completely empty. This block shifts the PK fields from
181691 183387         ** new.* to old.*, to accommodate the code that reads these arrays.  */
181692 183388         for(i=0; i<p->nCol; i++){
181693 183389           assert( p->apValue[i]==0 );
181694         -        assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
181695 183390           if( p->abPK[i] ){
181696 183391             p->apValue[i] = p->apValue[i+p->nCol];
       183392  +          if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
181697 183393             p->apValue[i+p->nCol] = 0;
181698 183394           }
181699 183395         }
181700 183396       }
181701 183397     }
181702 183398   
181703 183399     return SQLITE_ROW;
................................................................................
181708 183404   ** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
181709 183405   ** or SQLITE_CORRUPT.
181710 183406   **
181711 183407   ** This function may not be called on iterators passed to a conflict handler
181712 183408   ** callback by changeset_apply().
181713 183409   */
181714 183410   SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
181715         -  return sessionChangesetNext(p, 0, 0);
       183411  +  return sessionChangesetNext(p, 0, 0, 0);
181716 183412   }
181717 183413   
181718 183414   /*
181719 183415   ** The following function extracts information on the current change
181720 183416   ** from a changeset iterator. It may only be called after changeset_next()
181721 183417   ** has returned SQLITE_ROW.
181722 183418   */
................................................................................
182087 183783     sqlite3_stmt *pSelect;          /* SELECT statement */
182088 183784     int nCol;                       /* Size of azCol[] and abPK[] arrays */
182089 183785     const char **azCol;             /* Array of column names */
182090 183786     u8 *abPK;                       /* Boolean array - true if column is in PK */
182091 183787     int bStat1;                     /* True if table is sqlite_stat1 */
182092 183788     int bDeferConstraints;          /* True to defer constraints */
182093 183789     SessionBuffer constraints;      /* Deferred constraints are stored here */
       183790  +  SessionBuffer rebase;           /* Rebase information (if any) here */
       183791  +  int bRebaseStarted;             /* If table header is already in rebase */
182094 183792   };
182095 183793   
182096 183794   /*
182097 183795   ** Formulate a statement to DELETE a row from database db. Assuming a table
182098 183796   ** structure like this:
182099 183797   **
182100 183798   **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
................................................................................
182353 184051     if( rc==SQLITE_OK ){
182354 184052       rc = sessionPrepare(db, &p->pDelete,
182355 184053           "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
182356 184054           "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
182357 184055           "AND (?4 OR stat IS ?3)"
182358 184056       );
182359 184057     }
182360         -  assert( rc==SQLITE_OK );
182361 184058     return rc;
182362 184059   }
182363 184060   
182364 184061   /*
182365 184062   ** A wrapper around sqlite3_bind_value() that detects an extra problem. 
182366 184063   ** See comments in the body of this function for details.
182367 184064   */
................................................................................
182414 184111     ** in the code below. */
182415 184112     assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
182416 184113   
182417 184114     for(i=0; rc==SQLITE_OK && i<nCol; i++){
182418 184115       if( !abPK || abPK[i] ){
182419 184116         sqlite3_value *pVal;
182420 184117         (void)xValue(pIter, i, &pVal);
182421         -      rc = sessionBindValue(pStmt, i+1, pVal);
       184118  +      if( pVal==0 ){
       184119  +        /* The value in the changeset was "undefined". This indicates a
       184120  +        ** corrupt changeset blob.  */
       184121  +        rc = SQLITE_CORRUPT_BKPT;
       184122  +      }else{
       184123  +        rc = sessionBindValue(pStmt, i+1, pVal);
       184124  +      }
182422 184125       }
182423 184126     }
182424 184127     return rc;
182425 184128   }
182426 184129   
182427 184130   /*
182428 184131   ** SQL statement pSelect is as generated by the sessionSelectRow() function.
................................................................................
182458 184161         nCol, abPK, pSelect
182459 184162     );
182460 184163   
182461 184164     if( rc==SQLITE_OK ){
182462 184165       rc = sqlite3_step(pSelect);
182463 184166       if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
182464 184167     }
       184168  +
       184169  +  return rc;
       184170  +}
       184171  +
       184172  +/*
       184173  +** This function is called from within sqlite3changset_apply_v2() when
       184174  +** a conflict is encountered and resolved using conflict resolution
       184175  +** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
       184176  +** It adds a conflict resolution record to the buffer in 
       184177  +** SessionApplyCtx.rebase, which will eventually be returned to the caller
       184178  +** of apply_v2() as the "rebase" buffer.
       184179  +**
       184180  +** Return SQLITE_OK if successful, or an SQLite error code otherwise.
       184181  +*/
       184182  +static int sessionRebaseAdd(
       184183  +  SessionApplyCtx *p,             /* Apply context */
       184184  +  int eType,                      /* Conflict resolution (OMIT or REPLACE) */
       184185  +  sqlite3_changeset_iter *pIter   /* Iterator pointing at current change */
       184186  +){
       184187  +  int rc = SQLITE_OK;
       184188  +  int i;
       184189  +  int eOp = pIter->op;
       184190  +  if( p->bRebaseStarted==0 ){
       184191  +    /* Append a table-header to the rebase buffer */
       184192  +    const char *zTab = pIter->zTab;
       184193  +    sessionAppendByte(&p->rebase, 'T', &rc);
       184194  +    sessionAppendVarint(&p->rebase, p->nCol, &rc);
       184195  +    sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
       184196  +    sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
       184197  +    p->bRebaseStarted = 1;
       184198  +  }
       184199  +
       184200  +  assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
       184201  +  assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
       184202  +
       184203  +  sessionAppendByte(&p->rebase, 
       184204  +      (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
       184205  +  );
       184206  +  sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
       184207  +  for(i=0; i<p->nCol; i++){
       184208  +    sqlite3_value *pVal = 0;
       184209  +    if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
       184210  +      sqlite3changeset_old(pIter, i, &pVal);
       184211  +    }else{
       184212  +      sqlite3changeset_new(pIter, i, &pVal);
       184213  +    }
       184214  +    sessionAppendValue(&p->rebase, pVal, &rc);
       184215  +  }
182465 184216   
182466 184217     return rc;
182467 184218   }
182468 184219   
182469 184220   /*
182470 184221   ** Invoke the conflict handler for the change that the changeset iterator
182471 184222   ** currently points to.
................................................................................
182537 184288     }else if( rc==SQLITE_OK ){
182538 184289       if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
182539 184290         /* Instead of invoking the conflict handler, append the change blob
182540 184291         ** to the SessionApplyCtx.constraints buffer. */
182541 184292         u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
182542 184293         int nBlob = pIter->in.iNext - pIter->in.iCurrent;
182543 184294         sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
182544         -      res = SQLITE_CHANGESET_OMIT;
       184295  +      return SQLITE_OK;
182545 184296       }else{
182546 184297         /* No other row with the new.* primary key. */
182547 184298         res = xConflict(pCtx, eType+1, pIter);
182548 184299         if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
182549 184300       }
182550 184301     }
182551 184302   
................................................................................
182563 184314           rc = SQLITE_ABORT;
182564 184315           break;
182565 184316   
182566 184317         default:
182567 184318           rc = SQLITE_MISUSE;
182568 184319           break;
182569 184320       }
       184321  +    if( rc==SQLITE_OK ){
       184322  +      rc = sessionRebaseAdd(p, res, pIter);
       184323  +    }
182570 184324     }
182571 184325   
182572 184326     return rc;
182573 184327   }
182574 184328   
182575 184329   /*
182576 184330   ** Attempt to apply the change that the iterator passed as the first argument
................................................................................
182738 184492     void *pCtx                      /* First argument passed to xConflict */
182739 184493   ){
182740 184494     int bReplace = 0;
182741 184495     int bRetry = 0;
182742 184496     int rc;
182743 184497   
182744 184498     rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
182745         -  assert( rc==SQLITE_OK || (bRetry==0 && bReplace==0) );
182746         -
182747         -  /* If the bRetry flag is set, the change has not been applied due to an
182748         -  ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
182749         -  ** a row with the correct PK is present in the db, but one or more other
182750         -  ** fields do not contain the expected values) and the conflict handler 
182751         -  ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
182752         -  ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
182753         -  ** the SQLITE_CHANGESET_DATA problem.  */
182754         -  if( bRetry ){
182755         -    assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
182756         -    rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
182757         -  }
182758         -
182759         -  /* If the bReplace flag is set, the change is an INSERT that has not
182760         -  ** been performed because the database already contains a row with the
182761         -  ** specified primary key and the conflict handler returned
182762         -  ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
182763         -  ** before reattempting the INSERT.  */
182764         -  else if( bReplace ){
182765         -    assert( pIter->op==SQLITE_INSERT );
182766         -    rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
182767         -    if( rc==SQLITE_OK ){
182768         -      rc = sessionBindRow(pIter, 
182769         -          sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
182770         -      sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
182771         -    }
182772         -    if( rc==SQLITE_OK ){
182773         -      sqlite3_step(pApply->pDelete);
182774         -      rc = sqlite3_reset(pApply->pDelete);
182775         -    }
182776         -    if( rc==SQLITE_OK ){
       184499  +  if( rc==SQLITE_OK ){
       184500  +    /* If the bRetry flag is set, the change has not been applied due to an
       184501  +    ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
       184502  +    ** a row with the correct PK is present in the db, but one or more other
       184503  +    ** fields do not contain the expected values) and the conflict handler 
       184504  +    ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
       184505  +    ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
       184506  +    ** the SQLITE_CHANGESET_DATA problem.  */
       184507  +    if( bRetry ){
       184508  +      assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
182777 184509         rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
182778 184510       }
182779         -    if( rc==SQLITE_OK ){
182780         -      rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
       184511  +
       184512  +    /* If the bReplace flag is set, the change is an INSERT that has not
       184513  +    ** been performed because the database already contains a row with the
       184514  +    ** specified primary key and the conflict handler returned
       184515  +    ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
       184516  +    ** before reattempting the INSERT.  */
       184517  +    else if( bReplace ){
       184518  +      assert( pIter->op==SQLITE_INSERT );
       184519  +      rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
       184520  +      if( rc==SQLITE_OK ){
       184521  +        rc = sessionBindRow(pIter, 
       184522  +            sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
       184523  +        sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
       184524  +      }
       184525  +      if( rc==SQLITE_OK ){
       184526  +        sqlite3_step(pApply->pDelete);
       184527  +        rc = sqlite3_reset(pApply->pDelete);
       184528  +      }
       184529  +      if( rc==SQLITE_OK ){
       184530  +        rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
       184531  +      }
       184532  +      if( rc==SQLITE_OK ){
       184533  +        rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
       184534  +      }
182781 184535       }
182782 184536     }
182783 184537   
182784 184538     return rc;
182785 184539   }
182786 184540   
182787 184541   /*
................................................................................
182849 184603       const char *zTab              /* Table name */
182850 184604     ),
182851 184605     int(*xConflict)(
182852 184606       void *pCtx,                   /* Copy of fifth arg to _apply() */
182853 184607       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
182854 184608       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
182855 184609     ),
182856         -  void *pCtx                      /* First argument passed to xConflict */
       184610  +  void *pCtx,                     /* First argument passed to xConflict */
       184611  +  void **ppRebase, int *pnRebase  /* OUT: Rebase information */
182857 184612   ){
182858 184613     int schemaMismatch = 0;
182859 184614     int rc;                         /* Return code */
182860 184615     const char *zTab = 0;           /* Name of current table */
182861 184616     int nTab = 0;                   /* Result of sqlite3Strlen30(zTab) */
182862 184617     SessionApplyCtx sApply;         /* changeset_apply() context object */
182863 184618     int bPatchset;
................................................................................
182887 184642         if( rc!=SQLITE_OK ) break;
182888 184643   
182889 184644         sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
182890 184645         sqlite3_finalize(sApply.pDelete);
182891 184646         sqlite3_finalize(sApply.pUpdate); 
182892 184647         sqlite3_finalize(sApply.pInsert);
182893 184648         sqlite3_finalize(sApply.pSelect);
182894         -      memset(&sApply, 0, sizeof(sApply));
182895 184649         sApply.db = db;
       184650  +      sApply.pDelete = 0;
       184651  +      sApply.pUpdate = 0;
       184652  +      sApply.pInsert = 0;
       184653  +      sApply.pSelect = 0;
       184654  +      sApply.nCol = 0;
       184655  +      sApply.azCol = 0;
       184656  +      sApply.abPK = 0;
       184657  +      sApply.bStat1 = 0;
182896 184658         sApply.bDeferConstraints = 1;
       184659  +      sApply.bRebaseStarted = 0;
       184660  +      memset(&sApply.constraints, 0, sizeof(SessionBuffer));
182897 184661   
182898 184662         /* If an xFilter() callback was specified, invoke it now. If the 
182899 184663         ** xFilter callback returns zero, skip this table. If it returns
182900 184664         ** non-zero, proceed. */
182901 184665         schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
182902 184666         if( schemaMismatch ){
182903 184667           zTab = sqlite3_mprintf("%s", zNew);
................................................................................
182999 184763     if( rc==SQLITE_OK ){
183000 184764       rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
183001 184765     }else{
183002 184766       sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
183003 184767       sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
183004 184768     }
183005 184769   
       184770  +  if( rc==SQLITE_OK && bPatchset==0 && ppRebase && pnRebase ){
       184771  +    *ppRebase = (void*)sApply.rebase.aBuf;
       184772  +    *pnRebase = sApply.rebase.nBuf;
       184773  +    sApply.rebase.aBuf = 0;
       184774  +  }
183006 184775     sqlite3_finalize(sApply.pInsert);
183007 184776     sqlite3_finalize(sApply.pDelete);
183008 184777     sqlite3_finalize(sApply.pUpdate);
183009 184778     sqlite3_finalize(sApply.pSelect);
183010 184779     sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
183011 184780     sqlite3_free((char*)sApply.constraints.aBuf);
       184781  +  sqlite3_free((char*)sApply.rebase.aBuf);
183012 184782     sqlite3_mutex_leave(sqlite3_db_mutex(db));
183013 184783     return rc;
183014 184784   }
       184785  +
       184786  +/*
       184787  +** Apply the changeset passed via pChangeset/nChangeset to the main 
       184788  +** database attached to handle "db".
       184789  +*/
       184790  +SQLITE_API int sqlite3changeset_apply_v2(
       184791  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
       184792  +  int nChangeset,                 /* Size of changeset in bytes */
       184793  +  void *pChangeset,               /* Changeset blob */
       184794  +  int(*xFilter)(
       184795  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184796  +    const char *zTab              /* Table name */
       184797  +  ),
       184798  +  int(*xConflict)(
       184799  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184800  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
       184801  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
       184802  +  ),
       184803  +  void *pCtx,                     /* First argument passed to xConflict */
       184804  +  void **ppRebase, int *pnRebase
       184805  +){
       184806  +  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
       184807  +  int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
       184808  +  if( rc==SQLITE_OK ){
       184809  +    rc = sessionChangesetApply(
       184810  +        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
       184811  +    );
       184812  +  }
       184813  +  return rc;
       184814  +}
183015 184815   
183016 184816   /*
183017 184817   ** Apply the changeset passed via pChangeset/nChangeset to the main database
183018 184818   ** attached to handle "db". Invoke the supplied conflict handler callback
183019 184819   ** to resolve any conflicts encountered while applying the change.
183020 184820   */
183021 184821   SQLITE_API int sqlite3changeset_apply(
................................................................................
183029 184829     int(*xConflict)(
183030 184830       void *pCtx,                   /* Copy of fifth arg to _apply() */
183031 184831       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
183032 184832       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
183033 184833     ),
183034 184834     void *pCtx                      /* First argument passed to xConflict */
183035 184835   ){
183036         -  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
183037         -  int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
183038         -  if( rc==SQLITE_OK ){
183039         -    rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
183040         -  }
183041         -  return rc;
       184836  +  return sqlite3changeset_apply_v2(
       184837  +      db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0
       184838  +  );
183042 184839   }
183043 184840   
183044 184841   /*
183045 184842   ** Apply the changeset passed via xInput/pIn to the main database
183046 184843   ** attached to handle "db". Invoke the supplied conflict handler callback
183047 184844   ** to resolve any conflicts encountered while applying the change.
183048 184845   */
       184846  +SQLITE_API int sqlite3changeset_apply_v2_strm(
       184847  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
       184848  +  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
       184849  +  void *pIn,                                          /* First arg for xInput */
       184850  +  int(*xFilter)(
       184851  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184852  +    const char *zTab              /* Table name */
       184853  +  ),
       184854  +  int(*xConflict)(
       184855  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184856  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
       184857  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
       184858  +  ),
       184859  +  void *pCtx,                     /* First argument passed to xConflict */
       184860  +  void **ppRebase, int *pnRebase
       184861  +){
       184862  +  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
       184863  +  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
       184864  +  if( rc==SQLITE_OK ){
       184865  +    rc = sessionChangesetApply(
       184866  +        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
       184867  +    );
       184868  +  }
       184869  +  return rc;
       184870  +}
183049 184871   SQLITE_API int sqlite3changeset_apply_strm(
183050 184872     sqlite3 *db,                    /* Apply change to "main" db of this handle */
183051 184873     int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
183052 184874     void *pIn,                                          /* First arg for xInput */
183053 184875     int(*xFilter)(
183054 184876       void *pCtx,                   /* Copy of sixth arg to _apply() */
183055 184877       const char *zTab              /* Table name */
................................................................................
183057 184879     int(*xConflict)(
183058 184880       void *pCtx,                   /* Copy of sixth arg to _apply() */
183059 184881       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
183060 184882       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
183061 184883     ),
183062 184884     void *pCtx                      /* First argument passed to xConflict */
183063 184885   ){
183064         -  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
183065         -  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
183066         -  if( rc==SQLITE_OK ){
183067         -    rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
183068         -  }
183069         -  return rc;
       184886  +  return sqlite3changeset_apply_v2_strm(
       184887  +      db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0
       184888  +  );
183070 184889   }
183071 184890   
183072 184891   /*
183073 184892   ** sqlite3_changegroup handle.
183074 184893   */
183075 184894   struct sqlite3_changegroup {
183076 184895     int rc;                         /* Error code */
................................................................................
183081 184900   /*
183082 184901   ** This function is called to merge two changes to the same row together as
183083 184902   ** part of an sqlite3changeset_concat() operation. A new change object is
183084 184903   ** allocated and a pointer to it stored in *ppNew.
183085 184904   */
183086 184905   static int sessionChangeMerge(
183087 184906     SessionTable *pTab,             /* Table structure */
       184907  +  int bRebase,                    /* True for a rebase hash-table */
183088 184908     int bPatchset,                  /* True for patchsets */
183089 184909     SessionChange *pExist,          /* Existing change */
183090 184910     int op2,                        /* Second change operation */
183091 184911     int bIndirect,                  /* True if second change is indirect */
183092 184912     u8 *aRec,                       /* Second change record */
183093 184913     int nRec,                       /* Number of bytes in aRec */
183094 184914     SessionChange **ppNew           /* OUT: Merged change */
183095 184915   ){
183096 184916     SessionChange *pNew = 0;
       184917  +  int rc = SQLITE_OK;
183097 184918   
183098 184919     if( !pExist ){
183099 184920       pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
183100 184921       if( !pNew ){
183101 184922         return SQLITE_NOMEM;
183102 184923       }
183103 184924       memset(pNew, 0, sizeof(SessionChange));
183104 184925       pNew->op = op2;
183105 184926       pNew->bIndirect = bIndirect;
183106         -    pNew->nRecord = nRec;
183107 184927       pNew->aRecord = (u8*)&pNew[1];
183108         -    memcpy(pNew->aRecord, aRec, nRec);
       184928  +    if( bIndirect==0 || bRebase==0 ){
       184929  +      pNew->nRecord = nRec;
       184930  +      memcpy(pNew->aRecord, aRec, nRec);
       184931  +    }else{
       184932  +      int i;
       184933  +      u8 *pIn = aRec;
       184934  +      u8 *pOut = pNew->aRecord;
       184935  +      for(i=0; i<pTab->nCol; i++){
       184936  +        int nIn = sessionSerialLen(pIn);
       184937  +        if( *pIn==0 ){
       184938  +          *pOut++ = 0;
       184939  +        }else if( pTab->abPK[i]==0 ){
       184940  +          *pOut++ = 0xFF;
       184941  +        }else{
       184942  +          memcpy(pOut, pIn, nIn);
       184943  +          pOut += nIn;
       184944  +        }
       184945  +        pIn += nIn;
       184946  +      }
       184947  +      pNew->nRecord = pOut - pNew->aRecord;
       184948  +    }
       184949  +  }else if( bRebase ){
       184950  +    if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
       184951  +      *ppNew = pExist;
       184952  +    }else{
       184953  +      int nByte = nRec + pExist->nRecord + sizeof(SessionChange);
       184954  +      pNew = (SessionChange*)sqlite3_malloc(nByte);
       184955  +      if( pNew==0 ){
       184956  +        rc = SQLITE_NOMEM;
       184957  +      }else{
       184958  +        int i;
       184959  +        u8 *a1 = pExist->aRecord;
       184960  +        u8 *a2 = aRec;
       184961  +        u8 *pOut;
       184962  +
       184963  +        memset(pNew, 0, nByte);
       184964  +        pNew->bIndirect = bIndirect || pExist->bIndirect;
       184965  +        pNew->op = op2;
       184966  +        pOut = pNew->aRecord = (u8*)&pNew[1];
       184967  +
       184968  +        for(i=0; i<pTab->nCol; i++){
       184969  +          int n1 = sessionSerialLen(a1);
       184970  +          int n2 = sessionSerialLen(a2);
       184971  +          if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
       184972  +            *pOut++ = 0xFF;
       184973  +          }else if( *a2==0 ){
       184974  +            memcpy(pOut, a1, n1);
       184975  +            pOut += n1;
       184976  +          }else{
       184977  +            memcpy(pOut, a2, n2);
       184978  +            pOut += n2;
       184979  +          }
       184980  +          a1 += n1;
       184981  +          a2 += n2;
       184982  +        }
       184983  +        pNew->nRecord = pOut - pNew->aRecord;
       184984  +      }
       184985  +      sqlite3_free(pExist);
       184986  +    }
183109 184987     }else{
183110 184988       int op1 = pExist->op;
183111 184989   
183112 184990       /* 
183113 184991       **   op1=INSERT, op2=INSERT      ->      Unsupported. Discard op2.
183114 184992       **   op1=INSERT, op2=UPDATE      ->      INSERT.
183115 184993       **   op1=INSERT, op2=DELETE      ->      (none)
................................................................................
183195 185073           pNew->nRecord = (int)(aCsr - pNew->aRecord);
183196 185074         }
183197 185075         sqlite3_free(pExist);
183198 185076       }
183199 185077     }
183200 185078   
183201 185079     *ppNew = pNew;
183202         -  return SQLITE_OK;
       185080  +  return rc;
183203 185081   }
183204 185082   
183205 185083   /*
183206 185084   ** Add all changes in the changeset traversed by the iterator passed as
183207 185085   ** the first argument to the changegroup hash tables.
183208 185086   */
183209 185087   static int sessionChangesetToHash(
183210 185088     sqlite3_changeset_iter *pIter,   /* Iterator to read from */
183211         -  sqlite3_changegroup *pGrp        /* Changegroup object to add changeset to */
       185089  +  sqlite3_changegroup *pGrp,       /* Changegroup object to add changeset to */
       185090  +  int bRebase                      /* True if hash table is for rebasing */
183212 185091   ){
183213 185092     u8 *aRec;
183214 185093     int nRec;
183215 185094     int rc = SQLITE_OK;
183216 185095     SessionTable *pTab = 0;
183217 185096   
183218         -
183219         -  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec) ){
       185097  +  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
183220 185098       const char *zNew;
183221 185099       int nCol;
183222 185100       int op;
183223 185101       int iHash;
183224 185102       int bIndirect;
183225 185103       SessionChange *pChange;
183226 185104       SessionChange *pExist = 0;
................................................................................
183292 185170           pExist = *pp;
183293 185171           *pp = (*pp)->pNext;
183294 185172           pTab->nEntry--;
183295 185173           break;
183296 185174         }
183297 185175       }
183298 185176   
183299         -    rc = sessionChangeMerge(pTab, 
       185177  +    rc = sessionChangeMerge(pTab, bRebase, 
183300 185178           pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
183301 185179       );
183302 185180       if( rc ) break;
183303 185181       if( pChange ){
183304 185182         pChange->pNext = pTab->apChange[iHash];
183305 185183         pTab->apChange[iHash] = pChange;
183306 185184         pTab->nEntry++;
................................................................................
183400 185278   */
183401 185279   SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
183402 185280     sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
183403 185281     int rc;                         /* Return code */
183404 185282   
183405 185283     rc = sqlite3changeset_start(&pIter, nData, pData);
183406 185284     if( rc==SQLITE_OK ){
183407         -    rc = sessionChangesetToHash(pIter, pGrp);
       185285  +    rc = sessionChangesetToHash(pIter, pGrp, 0);
183408 185286     }
183409 185287     sqlite3changeset_finalize(pIter);
183410 185288     return rc;
183411 185289   }
183412 185290   
183413 185291   /*
183414 185292   ** Obtain a buffer containing a changeset representing the concatenation
................................................................................
183431 185309     void *pIn
183432 185310   ){
183433 185311     sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
183434 185312     int rc;                         /* Return code */
183435 185313   
183436 185314     rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
183437 185315     if( rc==SQLITE_OK ){
183438         -    rc = sessionChangesetToHash(pIter, pGrp);
       185316  +    rc = sessionChangesetToHash(pIter, pGrp, 0);
183439 185317     }
183440 185318     sqlite3changeset_finalize(pIter);
183441 185319     return rc;
183442 185320   }
183443 185321   
183444 185322   /*
183445 185323   ** Streaming versions of changegroup_output().
................................................................................
183515 185393     if( rc==SQLITE_OK ){
183516 185394       rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
183517 185395     }
183518 185396     sqlite3changegroup_delete(pGrp);
183519 185397   
183520 185398     return rc;
183521 185399   }
       185400  +
       185401  +/*
       185402  +** Changeset rebaser handle.
       185403  +*/
       185404  +struct sqlite3_rebaser {
       185405  +  sqlite3_changegroup grp;        /* Hash table */
       185406  +};
       185407  +
       185408  +/*
       185409  +** Buffers a1 and a2 must both contain a sessions module record nCol
       185410  +** fields in size. This function appends an nCol sessions module 
       185411  +** record to buffer pBuf that is a copy of a1, except that for
       185412  +** each field that is undefined in a1[], swap in the field from a2[].
       185413  +*/
       185414  +static void sessionAppendRecordMerge(
       185415  +  SessionBuffer *pBuf,            /* Buffer to append to */
       185416  +  int nCol,                       /* Number of columns in each record */
       185417  +  u8 *a1, int n1,                 /* Record 1 */
       185418  +  u8 *a2, int n2,                 /* Record 2 */
       185419  +  int *pRc                        /* IN/OUT: error code */
       185420  +){
       185421  +  sessionBufferGrow(pBuf, n1+n2, pRc);
       185422  +  if( *pRc==SQLITE_OK ){
       185423  +    int i;
       185424  +    u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
       185425  +    for(i=0; i<nCol; i++){
       185426  +      int nn1 = sessionSerialLen(a1);
       185427  +      int nn2 = sessionSerialLen(a2);
       185428  +      if( *a1==0 || *a1==0xFF ){
       185429  +        memcpy(pOut, a2, nn2);
       185430  +        pOut += nn2;
       185431  +      }else{
       185432  +        memcpy(pOut, a1, nn1);
       185433  +        pOut += nn1;
       185434  +      }
       185435  +      a1 += nn1;
       185436  +      a2 += nn2;
       185437  +    }
       185438  +
       185439  +    pBuf->nBuf = pOut-pBuf->aBuf;
       185440  +    assert( pBuf->nBuf<=pBuf->nAlloc );
       185441  +  }
       185442  +}
       185443  +
       185444  +/*
       185445  +** This function is called when rebasing a local UPDATE change against one 
       185446  +** or more remote UPDATE changes. The aRec/nRec buffer contains the current
       185447  +** old.* and new.* records for the change. The rebase buffer (a single
       185448  +** record) is in aChange/nChange. The rebased change is appended to buffer
       185449  +** pBuf.
       185450  +**
       185451  +** Rebasing the UPDATE involves: 
       185452  +**
       185453  +**   * Removing any changes to fields for which the corresponding field
       185454  +**     in the rebase buffer is set to "replaced" (type 0xFF). If this
       185455  +**     means the UPDATE change updates no fields, nothing is appended
       185456  +**     to the output buffer.
       185457  +**
       185458  +**   * For each field modified by the local change for which the 
       185459  +**     corresponding field in the rebase buffer is not "undefined" (0x00)
       185460  +**     or "replaced" (0xFF), the old.* value is replaced by the value
       185461  +**     in the rebase buffer.
       185462  +*/
       185463  +static void sessionAppendPartialUpdate(
       185464  +  SessionBuffer *pBuf,            /* Append record here */
       185465  +  sqlite3_changeset_iter *pIter,  /* Iterator pointed at local change */
       185466  +  u8 *aRec, int nRec,             /* Local change */
       185467  +  u8 *aChange, int nChange,       /* Record to rebase against */
       185468  +  int *pRc                        /* IN/OUT: Return Code */
       185469  +){
       185470  +  sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
       185471  +  if( *pRc==SQLITE_OK ){
       185472  +    int bData = 0;
       185473  +    u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
       185474  +    int i;
       185475  +    u8 *a1 = aRec;
       185476  +    u8 *a2 = aChange;
       185477  +
       185478  +    *pOut++ = SQLITE_UPDATE;
       185479  +    *pOut++ = pIter->bIndirect;
       185480  +    for(i=0; i<pIter->nCol; i++){
       185481  +      int n1 = sessionSerialLen(a1);
       185482  +      int n2 = sessionSerialLen(a2);
       185483  +      if( pIter->abPK[i] || a2[0]==0 ){
       185484  +        if( !pIter->abPK[i] ) bData = 1;
       185485  +        memcpy(pOut, a1, n1);
       185486  +        pOut += n1;
       185487  +      }else if( a2[0]!=0xFF ){
       185488  +        bData = 1;
       185489  +        memcpy(pOut, a2, n2);
       185490  +        pOut += n2;
       185491  +      }else{
       185492  +        *pOut++ = '\0';
       185493  +      }
       185494  +      a1 += n1;
       185495  +      a2 += n2;
       185496  +    }
       185497  +    if( bData ){
       185498  +      a2 = aChange;
       185499  +      for(i=0; i<pIter->nCol; i++){
       185500  +        int n1 = sessionSerialLen(a1);
       185501  +        int n2 = sessionSerialLen(a2);
       185502  +        if( pIter->abPK[i] || a2[0]!=0xFF ){
       185503  +          memcpy(pOut, a1, n1);
       185504  +          pOut += n1;
       185505  +        }else{
       185506  +          *pOut++ = '\0';
       185507  +        }
       185508  +        a1 += n1;
       185509  +        a2 += n2;
       185510  +      }
       185511  +      pBuf->nBuf = (pOut - pBuf->aBuf);
       185512  +    }
       185513  +  }
       185514  +}
       185515  +
       185516  +/*
       185517  +** pIter is configured to iterate through a changeset. This function rebases 
       185518  +** that changeset according to the current configuration of the rebaser 
       185519  +** object passed as the first argument. If no error occurs and argument xOutput
       185520  +** is not NULL, then the changeset is returned to the caller by invoking
       185521  +** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
       185522  +** then (*ppOut) is set to point to a buffer containing the rebased changeset
       185523  +** before this function returns. In this case (*pnOut) is set to the size of
       185524  +** the buffer in bytes.  It is the responsibility of the caller to eventually
       185525  +** free the (*ppOut) buffer using sqlite3_free(). 
       185526  +**
       185527  +** If an error occurs, an SQLite error code is returned. If ppOut and
       185528  +** pnOut are not NULL, then the two output parameters are set to 0 before
       185529  +** returning.
       185530  +*/
       185531  +static int sessionRebase(
       185532  +  sqlite3_rebaser *p,             /* Rebaser hash table */
       185533  +  sqlite3_changeset_iter *pIter,  /* Input data */
       185534  +  int (*xOutput)(void *pOut, const void *pData, int nData),
       185535  +  void *pOut,                     /* Context for xOutput callback */
       185536  +  int *pnOut,                     /* OUT: Number of bytes in output changeset */
       185537  +  void **ppOut                    /* OUT: Inverse of pChangeset */
       185538  +){
       185539  +  int rc = SQLITE_OK;
       185540  +  u8 *aRec = 0;
       185541  +  int nRec = 0;
       185542  +  int bNew = 0;
       185543  +  SessionTable *pTab = 0;
       185544  +  SessionBuffer sOut = {0,0,0};
       185545  +
       185546  +  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
       185547  +    SessionChange *pChange = 0;
       185548  +    int bDone = 0;
       185549  +
       185550  +    if( bNew ){
       185551  +      const char *zTab = pIter->zTab;
       185552  +      for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
       185553  +        if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
       185554  +      }
       185555  +      bNew = 0;
       185556  +
       185557  +      /* A patchset may not be rebased */
       185558  +      if( pIter->bPatchset ){
       185559  +        rc = SQLITE_ERROR;
       185560  +      }
       185561  +
       185562  +      /* Append a table header to the output for this new table */
       185563  +      sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
       185564  +      sessionAppendVarint(&sOut, pIter->nCol, &rc);
       185565  +      sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
       185566  +      sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
       185567  +    }
       185568  +
       185569  +    if( pTab && rc==SQLITE_OK ){
       185570  +      int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
       185571  +
       185572  +      for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
       185573  +        if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
       185574  +          break;
       185575  +        }
       185576  +      }
       185577  +    }
       185578  +
       185579  +    if( pChange ){
       185580  +      assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
       185581  +      switch( pIter->op ){
       185582  +        case SQLITE_INSERT:
       185583  +          if( pChange->op==SQLITE_INSERT ){
       185584  +            bDone = 1;
       185585  +            if( pChange->bIndirect==0 ){
       185586  +              sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
       185587  +              sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185588  +              sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
       185589  +              sessionAppendBlob(&sOut, aRec, nRec, &rc);
       185590  +            }
       185591  +          }
       185592  +          break;
       185593  +
       185594  +        case SQLITE_UPDATE:
       185595  +          bDone = 1;
       185596  +          if( pChange->op==SQLITE_DELETE ){
       185597  +            if( pChange->bIndirect==0 ){
       185598  +              u8 *pCsr = aRec;
       185599  +              sessionSkipRecord(&pCsr, pIter->nCol);
       185600  +              sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
       185601  +              sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185602  +              sessionAppendRecordMerge(&sOut, pIter->nCol,
       185603  +                  pCsr, nRec-(pCsr-aRec), 
       185604  +                  pChange->aRecord, pChange->nRecord, &rc
       185605  +              );
       185606  +            }
       185607  +          }else{
       185608  +            sessionAppendPartialUpdate(&sOut, pIter,
       185609  +                aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
       185610  +            );
       185611  +          }
       185612  +          break;
       185613  +
       185614  +        default:
       185615  +          assert( pIter->op==SQLITE_DELETE );
       185616  +          bDone = 1;
       185617  +          if( pChange->op==SQLITE_INSERT ){
       185618  +            sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
       185619  +            sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185620  +            sessionAppendRecordMerge(&sOut, pIter->nCol,
       185621  +                pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
       185622  +            );
       185623  +          }
       185624  +          break;
       185625  +      }
       185626  +    }
       185627  +
       185628  +    if( bDone==0 ){
       185629  +      sessionAppendByte(&sOut, pIter->op, &rc);
       185630  +      sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185631  +      sessionAppendBlob(&sOut, aRec, nRec, &rc);
       185632  +    }
       185633  +    if( rc==SQLITE_OK && xOutput && sOut.nBuf>SESSIONS_STRM_CHUNK_SIZE ){
       185634  +      rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
       185635  +      sOut.nBuf = 0;
       185636  +    }
       185637  +    if( rc ) break;
       185638  +  }
       185639  +
       185640  +  if( rc!=SQLITE_OK ){
       185641  +    sqlite3_free(sOut.aBuf);
       185642  +    memset(&sOut, 0, sizeof(sOut));
       185643  +  }
       185644  +
       185645  +  if( rc==SQLITE_OK ){
       185646  +    if( xOutput ){
       185647  +      if( sOut.nBuf>0 ){
       185648  +        rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
       185649  +      }
       185650  +    }else{
       185651  +      *ppOut = (void*)sOut.aBuf;
       185652  +      *pnOut = sOut.nBuf;
       185653  +      sOut.aBuf = 0;
       185654  +    }
       185655  +  }
       185656  +  sqlite3_free(sOut.aBuf);
       185657  +  return rc;
       185658  +}
       185659  +
       185660  +/* 
       185661  +** Create a new rebaser object.
       185662  +*/
       185663  +SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
       185664  +  int rc = SQLITE_OK;
       185665  +  sqlite3_rebaser *pNew;
       185666  +
       185667  +  pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
       185668  +  if( pNew==0 ){
       185669  +    rc = SQLITE_NOMEM;
       185670  +  }else{
       185671  +    memset(pNew, 0, sizeof(sqlite3_rebaser));
       185672  +  }
       185673  +  *ppNew = pNew;
       185674  +  return rc;
       185675  +}
       185676  +
       185677  +/* 
       185678  +** Call this one or more times to configure a rebaser.
       185679  +*/
       185680  +SQLITE_API int sqlite3rebaser_configure(
       185681  +  sqlite3_rebaser *p, 
       185682  +  int nRebase, const void *pRebase
       185683  +){
       185684  +  sqlite3_changeset_iter *pIter = 0;   /* Iterator opened on pData/nData */
       185685  +  int rc;                              /* Return code */
       185686  +  rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
       185687  +  if( rc==SQLITE_OK ){
       185688  +    rc = sessionChangesetToHash(pIter, &p->grp, 1);
       185689  +  }
       185690  +  sqlite3changeset_finalize(pIter);
       185691  +  return rc;
       185692  +}
       185693  +
       185694  +/* 
       185695  +** Rebase a changeset according to current rebaser configuration 
       185696  +*/
       185697  +SQLITE_API int sqlite3rebaser_rebase(
       185698  +  sqlite3_rebaser *p,
       185699  +  int nIn, const void *pIn, 
       185700  +  int *pnOut, void **ppOut 
       185701  +){
       185702  +  sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */  
       185703  +  int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
       185704  +
       185705  +  if( rc==SQLITE_OK ){
       185706  +    rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
       185707  +    sqlite3changeset_finalize(pIter);
       185708  +  }
       185709  +
       185710  +  return rc;
       185711  +}
       185712  +
       185713  +/* 
       185714  +** Rebase a changeset according to current rebaser configuration 
       185715  +*/
       185716  +SQLITE_API int sqlite3rebaser_rebase_strm(
       185717  +  sqlite3_rebaser *p,
       185718  +  int (*xInput)(void *pIn, void *pData, int *pnData),
       185719  +  void *pIn,
       185720  +  int (*xOutput)(void *pOut, const void *pData, int nData),
       185721  +  void *pOut
       185722  +){
       185723  +  sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */  
       185724  +  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
       185725  +
       185726  +  if( rc==SQLITE_OK ){
       185727  +    rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
       185728  +    sqlite3changeset_finalize(pIter);
       185729  +  }
       185730  +
       185731  +  return rc;
       185732  +}
       185733  +
       185734  +/* 
       185735  +** Destroy a rebaser object 
       185736  +*/
       185737  +SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
       185738  +  if( p ){
       185739  +    sessionDeleteTable(p->grp.pList);
       185740  +    sqlite3_free(p);
       185741  +  }
       185742  +}
183522 185743   
183523 185744   #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
183524 185745   
183525 185746   /************** End of sqlite3session.c **************************************/
183526 185747   /************** Begin file json1.c *******************************************/
183527 185748   /*
183528 185749   ** 2015-08-12
................................................................................
188051 190272     if( stateno>fts5YY_MAX_SHIFT ) return stateno;
188052 190273     assert( stateno <= fts5YY_SHIFT_COUNT );
188053 190274   #if defined(fts5YYCOVERAGE)
188054 190275     fts5yycoverage[stateno][iLookAhead] = 1;
188055 190276   #endif
188056 190277     do{
188057 190278       i = fts5yy_shift_ofst[stateno];
188058         -    assert( i>=0 && i+fts5YYNFTS5TOKEN<=sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]) );
       190279  +    assert( i>=0 );
       190280  +    assert( i+fts5YYNFTS5TOKEN<=(int)sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]) );
188059 190281       assert( iLookAhead!=fts5YYNOCODE );
188060 190282       assert( iLookAhead < fts5YYNFTS5TOKEN );
188061 190283       i += iLookAhead;
188062 190284       if( fts5yy_lookahead[i]!=iLookAhead ){
188063 190285   #ifdef fts5YYFALLBACK
188064 190286         fts5YYCODETYPE iFallback;            /* Fallback token */
188065 190287         if( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])
................................................................................
192493 194715       }
192494 194716   
192495 194717       if( sCtx.pPhrase==0 ){
192496 194718         /* This happens when parsing a token or quoted phrase that contains
192497 194719         ** no token characters at all. (e.g ... MATCH '""'). */
192498 194720         sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
192499 194721       }else if( sCtx.pPhrase->nTerm ){
192500         -      sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = bPrefix;
       194722  +      sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
192501 194723       }
192502 194724       pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
192503 194725     }
192504 194726   
192505 194727     return sCtx.pPhrase;
192506 194728   }
192507 194729   
................................................................................
194956 197178       if( p->rc ) return;
194957 197179     }
194958 197180   
194959 197181     sqlite3_bind_int64(p->pWriter, 1, iRowid);
194960 197182     sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
194961 197183     sqlite3_step(p->pWriter);
194962 197184     p->rc = sqlite3_reset(p->pWriter);
       197185  +  sqlite3_bind_null(p->pWriter, 2);
194963 197186   }
194964 197187   
194965 197188   /*
194966 197189   ** Execute the following SQL:
194967 197190   **
194968 197191   **     DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
194969 197192   */
................................................................................
196584 198807     sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC);
196585 198808     if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){
196586 198809       i64 val = sqlite3_column_int(pIdxSelect, 0);
196587 198810       iPg = (int)(val>>1);
196588 198811       bDlidx = (val & 0x0001);
196589 198812     }
196590 198813     p->rc = sqlite3_reset(pIdxSelect);
       198814  +  sqlite3_bind_null(pIdxSelect, 2);
196591 198815   
196592 198816     if( iPg<pSeg->pgnoFirst ){
196593 198817       iPg = pSeg->pgnoFirst;
196594 198818       bDlidx = 0;
196595 198819     }
196596 198820   
196597 198821     pIter->iLeafPgno = iPg - 1;
................................................................................
197796 200020           sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
197797 200021           if( p->rc==SQLITE_OK ){
197798 200022             u8 aBlob[2] = {0xff, 0xff};
197799 200023             sqlite3_bind_int(pIdxSelect, 1, iSegid);
197800 200024             sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
197801 200025             assert( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
197802 200026             p->rc = sqlite3_reset(pIdxSelect);
       200027  +          sqlite3_bind_null(pIdxSelect, 2);
197803 200028           }
197804 200029         }
197805 200030   #endif
197806 200031       }
197807 200032     }
197808 200033   
197809 200034     return iSegid;
................................................................................
197922 200147       const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
197923 200148       /* The following was already done in fts5WriteInit(): */
197924 200149       /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
197925 200150       sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
197926 200151       sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
197927 200152       sqlite3_step(p->pIdxWriter);
197928 200153       p->rc = sqlite3_reset(p->pIdxWriter);
       200154  +    sqlite3_bind_null(p->pIdxWriter, 2);
197929 200155     }
197930 200156     pWriter->iBtPage = 0;
197931 200157   }
197932 200158   
197933 200159   /*
197934 200160   ** This is called once for each leaf page except the first that contains
197935 200161   ** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
................................................................................
203329 205555   static void fts5SourceIdFunc(
203330 205556     sqlite3_context *pCtx,          /* Function call context */
203331 205557     int nArg,                       /* Number of args */
203332 205558     sqlite3_value **apUnused        /* Function arguments */
203333 205559   ){
203334 205560     assert( nArg==0 );
203335 205561     UNUSED_PARAM2(nArg, apUnused);
203336         -  sqlite3_result_text(pCtx, "fts5: 2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d", -1, SQLITE_TRANSIENT);
       205562  +  sqlite3_result_text(pCtx, "fts5: 2018-04-02 11:04:16 736b53f57f70b23172c30880186dce7ad9baa3b74e3838cae5847cffb98f5cd2", -1, SQLITE_TRANSIENT);
203337 205563   }
203338 205564   
203339 205565   static int fts5Init(sqlite3 *db){
203340 205566     static const sqlite3_module fts5Mod = {
203341 205567       /* iVersion      */ 2,
203342 205568       /* xCreate       */ fts5CreateMethod,
203343 205569       /* xConnect      */ fts5ConnectMethod,
................................................................................
203905 206131       sqlite3_stmt *pReplace = 0;
203906 206132       rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
203907 206133       if( rc==SQLITE_OK ){
203908 206134         sqlite3_bind_int64(pReplace, 1, iRowid);
203909 206135         sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
203910 206136         sqlite3_step(pReplace);
203911 206137         rc = sqlite3_reset(pReplace);
       206138  +      sqlite3_bind_null(pReplace, 2);
203912 206139       }
203913 206140     }
203914 206141     return rc;
203915 206142   }
203916 206143   
203917 206144   /*
203918 206145   ** Load the contents of the "averages" record from disk into the 
................................................................................
204565 206792       if( pVal ){
204566 206793         sqlite3_bind_value(pReplace, 2, pVal);
204567 206794       }else{
204568 206795         sqlite3_bind_int(pReplace, 2, iVal);
204569 206796       }
204570 206797       sqlite3_step(pReplace);
204571 206798       rc = sqlite3_reset(pReplace);
       206799  +    sqlite3_bind_null(pReplace, 1);
204572 206800     }
204573 206801     if( rc==SQLITE_OK && pVal ){
204574 206802       int iNew = p->pConfig->iCookie + 1;
204575 206803       rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
204576 206804       if( rc==SQLITE_OK ){
204577 206805         p->pConfig->iCookie = iNew;
204578 206806       }
................................................................................
207597 209825   #endif
207598 209826     return rc;
207599 209827   }
207600 209828   #endif /* SQLITE_CORE */
207601 209829   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
207602 209830   
207603 209831   /************** End of stmt.c ************************************************/
207604         -#if __LINE__!=207604
       209832  +#if __LINE__!=209832
207605 209833   #undef SQLITE_SOURCE_ID
207606         -#define SQLITE_SOURCE_ID      "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2alt2"
       209834  +#define SQLITE_SOURCE_ID      "2018-04-02 11:04:16 736b53f57f70b23172c30880186dce7ad9baa3b74e3838cae5847cffb98falt2"
207607 209835   #endif
207608 209836   /* Return the source-id for this library */
207609 209837   SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
207610 209838   /************************** End of sqlite3.c ******************************/

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

   119    119   ** been edited in any way since it was last checked in, then the last
   120    120   ** four hexadecimal digits of the hash may be modified.
   121    121   **
   122    122   ** See also: [sqlite3_libversion()],
   123    123   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   124    124   ** [sqlite_version()] and [sqlite_source_id()].
   125    125   */
   126         -#define SQLITE_VERSION        "3.22.0"
   127         -#define SQLITE_VERSION_NUMBER 3022000
   128         -#define SQLITE_SOURCE_ID      "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
          126  +#define SQLITE_VERSION        "3.23.0"
          127  +#define SQLITE_VERSION_NUMBER 3023000
          128  +#define SQLITE_SOURCE_ID      "2018-04-02 11:04:16 736b53f57f70b23172c30880186dce7ad9baa3b74e3838cae5847cffb98f5cd2"
   129    129   
   130    130   /*
   131    131   ** CAPI3REF: Run-Time Library Version Numbers
   132    132   ** KEYWORDS: sqlite3_version sqlite3_sourceid
   133    133   **
   134    134   ** These interfaces provide the same information as the [SQLITE_VERSION],
   135    135   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1060   1060   ** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
  1061   1061   ** operations since the previous successful call to 
  1062   1062   ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
  1063   1063   ** ^This file control takes the file descriptor out of batch write mode
  1064   1064   ** so that all subsequent write operations are independent.
  1065   1065   ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
  1066   1066   ** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
         1067  +**
         1068  +** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
         1069  +** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
         1070  +** a file lock using the xLock or xShmLock methods of the VFS to wait
         1071  +** for up to M milliseconds before failing, where M is the single 
         1072  +** unsigned integer parameter.
  1067   1073   ** </ul>
  1068   1074   */
  1069   1075   #define SQLITE_FCNTL_LOCKSTATE               1
  1070   1076   #define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
  1071   1077   #define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
  1072   1078   #define SQLITE_FCNTL_LAST_ERRNO              4
  1073   1079   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
  1094   1100   #define SQLITE_FCNTL_VFS_POINTER            27
  1095   1101   #define SQLITE_FCNTL_JOURNAL_POINTER        28
  1096   1102   #define SQLITE_FCNTL_WIN32_GET_HANDLE       29
  1097   1103   #define SQLITE_FCNTL_PDB                    30
  1098   1104   #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
  1099   1105   #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
  1100   1106   #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
         1107  +#define SQLITE_FCNTL_LOCK_TIMEOUT           34
  1101   1108   
  1102   1109   /* deprecated names */
  1103   1110   #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
  1104   1111   #define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
  1105   1112   #define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
  1106   1113   
  1107   1114   
................................................................................
  2050   2057   **
  2051   2058   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  2052   2059   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  2053   2060   ** database handle, SQLite checks if this will mean that there are now no 
  2054   2061   ** connections at all to the database. If so, it performs a checkpoint 
  2055   2062   ** operation before closing the connection. This option may be used to
  2056   2063   ** override this behaviour. The first parameter passed to this operation
  2057         -** is an integer - non-zero to disable checkpoints-on-close, or zero (the
  2058         -** default) to enable them. The second parameter is a pointer to an integer
         2064  +** is an integer - positive to disable checkpoints-on-close, or zero (the
         2065  +** default) to enable them, and negative to leave the setting unchanged.
         2066  +** The second parameter is a pointer to an integer
  2059   2067   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  2060   2068   ** have been disabled - 0 if they are not disabled, 1 if they are.
  2061   2069   ** </dd>
         2070  +**
  2062   2071   ** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  2063   2072   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  2064   2073   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  2065   2074   ** a single SQL query statement will always use the same algorithm regardless
  2066   2075   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  2067   2076   ** that look at the values of bound parameters, which can make some queries
  2068   2077   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  2069   2078   ** the QPSG active, SQLite will always use the same query plan in the field as
  2070   2079   ** was used during testing in the lab.
         2080  +** The first argument to this setting is an integer which is 0 to disable 
         2081  +** the QPSG, positive to enable QPSG, or negative to leave the setting
         2082  +** unchanged. The second parameter is a pointer to an integer into which
         2083  +** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
         2084  +** following this call.
  2071   2085   ** </dd>
         2086  +**
  2072   2087   ** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  2073   2088   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  2074   2089   ** include output for any operations performed by trigger programs. This
  2075   2090   ** option is used to set or clear (the default) a flag that governs this
  2076   2091   ** behavior. The first parameter passed to this operation is an integer -
  2077         -** non-zero to enable output for trigger programs, or zero to disable it.
         2092  +** positive to enable output for trigger programs, or zero to disable it,
         2093  +** or negative to leave the setting unchanged.
  2078   2094   ** The second parameter is a pointer to an integer into which is written 
  2079   2095   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  2080   2096   ** it is not disabled, 1 if it is.  
  2081   2097   ** </dd>
  2082   2098   ** </dl>
  2083   2099   */
  2084   2100   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
................................................................................
  2492   2508   SQLITE_API void sqlite3_free_table(char **result);
  2493   2509   
  2494   2510   /*
  2495   2511   ** CAPI3REF: Formatted String Printing Functions
  2496   2512   **
  2497   2513   ** These routines are work-alikes of the "printf()" family of functions
  2498   2514   ** from the standard C library.
  2499         -** These routines understand most of the common K&R formatting options,
  2500         -** plus some additional non-standard formats, detailed below.
  2501         -** Note that some of the more obscure formatting options from recent
  2502         -** C-library standards are omitted from this implementation.
         2515  +** These routines understand most of the common formatting options from
         2516  +** the standard library printf() 
         2517  +** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
         2518  +** See the [built-in printf()] documentation for details.
  2503   2519   **
  2504   2520   ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  2505         -** results into memory obtained from [sqlite3_malloc()].
         2521  +** results into memory obtained from [sqlite3_malloc64()].
  2506   2522   ** The strings returned by these two routines should be
  2507   2523   ** released by [sqlite3_free()].  ^Both routines return a
  2508         -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
         2524  +** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
  2509   2525   ** memory to hold the resulting string.
  2510   2526   **
  2511   2527   ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  2512   2528   ** the standard C library.  The result is written into the
  2513   2529   ** buffer supplied as the second parameter whose size is given by
  2514   2530   ** the first parameter. Note that the order of the
  2515   2531   ** first two parameters is reversed from snprintf().)^  This is an
................................................................................
  2525   2541   ** guarantees that the buffer is always zero-terminated.  ^The first
  2526   2542   ** parameter "n" is the total size of the buffer, including space for
  2527   2543   ** the zero terminator.  So the longest string that can be completely
  2528   2544   ** written will be n-1 characters.
  2529   2545   **
  2530   2546   ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  2531   2547   **
  2532         -** These routines all implement some additional formatting
  2533         -** options that are useful for constructing SQL statements.
  2534         -** All of the usual printf() formatting options apply.  In addition, there
  2535         -** is are "%q", "%Q", "%w" and "%z" options.
  2536         -**
  2537         -** ^(The %q option works like %s in that it substitutes a nul-terminated
  2538         -** string from the argument list.  But %q also doubles every '\'' character.
  2539         -** %q is designed for use inside a string literal.)^  By doubling each '\''
  2540         -** character it escapes that character and allows it to be inserted into
  2541         -** the string.
  2542         -**
  2543         -** For example, assume the string variable zText contains text as follows:
  2544         -**
  2545         -** <blockquote><pre>
  2546         -**  char *zText = "It's a happy day!";
  2547         -** </pre></blockquote>
  2548         -**
  2549         -** One can use this text in an SQL statement as follows:
  2550         -**
  2551         -** <blockquote><pre>
  2552         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  2553         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  2554         -**  sqlite3_free(zSQL);
  2555         -** </pre></blockquote>
  2556         -**
  2557         -** Because the %q format string is used, the '\'' character in zText
  2558         -** is escaped and the SQL generated is as follows:
  2559         -**
  2560         -** <blockquote><pre>
  2561         -**  INSERT INTO table1 VALUES('It''s a happy day!')
  2562         -** </pre></blockquote>
  2563         -**
  2564         -** This is correct.  Had we used %s instead of %q, the generated SQL
  2565         -** would have looked like this:
  2566         -**
  2567         -** <blockquote><pre>
  2568         -**  INSERT INTO table1 VALUES('It's a happy day!');
  2569         -** </pre></blockquote>
  2570         -**
  2571         -** This second example is an SQL syntax error.  As a general rule you should
  2572         -** always use %q instead of %s when inserting text into a string literal.
  2573         -**
  2574         -** ^(The %Q option works like %q except it also adds single quotes around
  2575         -** the outside of the total string.  Additionally, if the parameter in the
  2576         -** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  2577         -** single quotes).)^  So, for example, one could say:
  2578         -**
  2579         -** <blockquote><pre>
  2580         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  2581         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  2582         -**  sqlite3_free(zSQL);
  2583         -** </pre></blockquote>
  2584         -**
  2585         -** The code above will render a correct SQL statement in the zSQL
  2586         -** variable even if the zText variable is a NULL pointer.
  2587         -**
  2588         -** ^(The "%w" formatting option is like "%q" except that it expects to
  2589         -** be contained within double-quotes instead of single quotes, and it
  2590         -** escapes the double-quote character instead of the single-quote
  2591         -** character.)^  The "%w" formatting option is intended for safely inserting
  2592         -** table and column names into a constructed SQL statement.
  2593         -**
  2594         -** ^(The "%z" formatting option works like "%s" but with the
  2595         -** addition that after the string has been read and copied into
  2596         -** the result, [sqlite3_free()] is called on the input string.)^
         2548  +** See also:  [built-in printf()], [printf() SQL function]
  2597   2549   */
  2598   2550   SQLITE_API char *sqlite3_mprintf(const char*,...);
  2599   2551   SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  2600   2552   SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  2601   2553   SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  2602   2554   
  2603   2555   /*
................................................................................
  3655   3607   ** a schema change, on the first  [sqlite3_step()] call following any change
  3656   3608   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  3657   3609   ** ^The specific value of WHERE-clause [parameter] might influence the 
  3658   3610   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  3659   3611   ** or [GLOB] operator or if the parameter is compared to an indexed column
  3660   3612   ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  3661   3613   ** </li>
         3614  +** </ol>
  3662   3615   **
  3663   3616   ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
  3664   3617   ** the extra prepFlags parameter, which is a bit array consisting of zero or
  3665   3618   ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
  3666   3619   ** sqlite3_prepare_v2() interface works exactly the same as
  3667   3620   ** sqlite3_prepare_v3() with a zero prepFlags parameter.
  3668         -** </ol>
  3669   3621   */
  3670   3622   SQLITE_API int sqlite3_prepare(
  3671   3623     sqlite3 *db,            /* Database handle */
  3672   3624     const char *zSql,       /* SQL statement, UTF-8 encoded */
  3673   3625     int nByte,              /* Maximum length of zSql in bytes. */
  3674   3626     sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3675   3627     const char **pzTail     /* OUT: Pointer to unused portion of zSql */
................................................................................
  7289   7241   ** wal file in wal mode databases, or the number of pages written to the
  7290   7242   ** database file in rollback mode databases. Any pages written as part of
  7291   7243   ** transaction rollback or database recovery operations are not included.
  7292   7244   ** If an IO or other error occurs while writing a page to disk, the effect
  7293   7245   ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  7294   7246   ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  7295   7247   ** </dd>
         7248  +**
         7249  +** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
         7250  +** <dd>This parameter returns the number of dirty cache entries that have
         7251  +** been written to disk in the middle of a transaction due to the page
         7252  +** cache overflowing. Transactions are more efficient if they are written
         7253  +** to disk all at once. When pages spill mid-transaction, that introduces
         7254  +** additional overhead. This parameter can be used help identify
         7255  +** inefficiencies that can be resolve by increasing the cache size.
         7256  +** </dd>
  7296   7257   **
  7297   7258   ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  7298   7259   ** <dd>This parameter returns zero for the current value if and only if
  7299   7260   ** all foreign key constraints (deferred or immediate) have been
  7300   7261   ** resolved.)^  ^The highwater mark is always 0.
  7301   7262   ** </dd>
  7302   7263   ** </dl>
................................................................................
  7309   7270   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  7310   7271   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  7311   7272   #define SQLITE_DBSTATUS_CACHE_HIT            7
  7312   7273   #define SQLITE_DBSTATUS_CACHE_MISS           8
  7313   7274   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  7314   7275   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  7315   7276   #define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
  7316         -#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
         7277  +#define SQLITE_DBSTATUS_CACHE_SPILL         12
         7278  +#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
  7317   7279   
  7318   7280   
  7319   7281   /*
  7320   7282   ** CAPI3REF: Prepared Statement Status
  7321   7283   ** METHOD: sqlite3_stmt
  7322   7284   **
  7323   7285   ** ^(Each prepared statement maintains various
................................................................................
  8789   8751   ** transaction open on the database, or if the database is not a wal mode
  8790   8752   ** database.
  8791   8753   **
  8792   8754   ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  8793   8755   */
  8794   8756   SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
  8795   8757   
         8758  +/*
         8759  +** CAPI3REF: Serialize a database
         8760  +**
         8761  +** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
         8762  +** that is a serialization of the S database on [database connection] D.
         8763  +** If P is not a NULL pointer, then the size of the database in bytes
         8764  +** is written into *P.
         8765  +**
         8766  +** For an ordinary on-disk database file, the serialization is just a
         8767  +** copy of the disk file.  For an in-memory database or a "TEMP" database,
         8768  +** the serialization is the same sequence of bytes which would be written
         8769  +** to disk if that database where backed up to disk.
         8770  +**
         8771  +** The usual case is that sqlite3_serialize() copies the serialization of
         8772  +** the database into memory obtained from [sqlite3_malloc64()] and returns
         8773  +** a pointer to that memory.  The caller is responsible for freeing the
         8774  +** returned value to avoid a memory leak.  However, if the F argument
         8775  +** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
         8776  +** are made, and the sqlite3_serialize() function will return a pointer
         8777  +** to the contiguous memory representation of the database that SQLite
         8778  +** is currently using for that database, or NULL if the no such contiguous
         8779  +** memory representation of the database exists.  A contiguous memory
         8780  +** representation of the database will usually only exist if there has
         8781  +** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
         8782  +** values of D and S.
         8783  +** The size of the database is written into *P even if the 
         8784  +** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
         8785  +** of the database exists.
         8786  +**
         8787  +** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
         8788  +** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
         8789  +** allocation error occurs.
         8790  +**
         8791  +** This interface is only available if SQLite is compiled with the
         8792  +** [SQLITE_ENABLE_DESERIALIZE] option.
         8793  +*/
         8794  +SQLITE_API unsigned char *sqlite3_serialize(
         8795  +  sqlite3 *db,           /* The database connection */
         8796  +  const char *zSchema,   /* Which DB to serialize. ex: "main", "temp", ... */
         8797  +  sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
         8798  +  unsigned int mFlags    /* Zero or more SQLITE_SERIALIZE_* flags */
         8799  +);
         8800  +
         8801  +/*
         8802  +** CAPI3REF: Flags for sqlite3_serialize
         8803  +**
         8804  +** Zero or more of the following constants can be OR-ed together for
         8805  +** the F argument to [sqlite3_serialize(D,S,P,F)].
         8806  +**
         8807  +** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
         8808  +** a pointer to contiguous in-memory database that it is currently using,
         8809  +** without making a copy of the database.  If SQLite is not currently using
         8810  +** a contiguous in-memory database, then this option causes
         8811  +** [sqlite3_serialize()] to return a NULL pointer.  SQLite will only be
         8812  +** using a contiguous in-memory database if it has been initialized by a
         8813  +** prior call to [sqlite3_deserialize()].
         8814  +*/
         8815  +#define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
         8816  +
         8817  +/*
         8818  +** CAPI3REF: Deserialize a database
         8819  +**
         8820  +** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the 
         8821  +** [database connection] D to disconnect from database S and then
         8822  +** reopen S as an in-memory database based on the serialization contained
         8823  +** in P.  The serialized database P is N bytes in size.  M is the size of
         8824  +** the buffer P, which might be larger than N.  If M is larger than N, and
         8825  +** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
         8826  +** permitted to add content to the in-memory database as long as the total
         8827  +** size does not exceed M bytes.
         8828  +**
         8829  +** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
         8830  +** invoke sqlite3_free() on the serialization buffer when the database
         8831  +** connection closes.  If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
         8832  +** SQLite will try to increase the buffer size using sqlite3_realloc64()
         8833  +** if writes on the database cause it to grow larger than M bytes.
         8834  +**
         8835  +** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
         8836  +** database is currently in a read transaction or is involved in a backup
         8837  +** operation.
         8838  +**
         8839  +** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the 
         8840  +** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
         8841  +** [sqlite3_free()] is invoked on argument P prior to returning.
         8842  +**
         8843  +** This interface is only available if SQLite is compiled with the
         8844  +** [SQLITE_ENABLE_DESERIALIZE] option.
         8845  +*/
         8846  +SQLITE_API int sqlite3_deserialize(
         8847  +  sqlite3 *db,            /* The database connection */
         8848  +  const char *zSchema,    /* Which DB to reopen with the deserialization */
         8849  +  unsigned char *pData,   /* The serialized database content */
         8850  +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
         8851  +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
         8852  +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
         8853  +);
         8854  +
         8855  +/*
         8856  +** CAPI3REF: Flags for sqlite3_deserialize()
         8857  +**
         8858  +** The following are allowed values for 6th argument (the F argument) to
         8859  +** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
         8860  +**
         8861  +** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
         8862  +** in the P argument is held in memory obtained from [sqlite3_malloc64()]
         8863  +** and that SQLite should take ownership of this memory and automatically
         8864  +** free it when it has finished using it.  Without this flag, the caller
         8865  +** is resposible for freeing any dynamically allocated memory.
         8866  +**
         8867  +** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
         8868  +** grow the size of the database using calls to [sqlite3_realloc64()].  This
         8869  +** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
         8870  +** Without this flag, the deserialized database cannot increase in size beyond
         8871  +** the number of bytes specified by the M parameter.
         8872  +**
         8873  +** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
         8874  +** should be treated as read-only.
         8875  +*/
         8876  +#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
         8877  +#define SQLITE_DESERIALIZE_RESIZEABLE  2 /* Resize using sqlite3_realloc64() */
         8878  +#define SQLITE_DESERIALIZE_READONLY    4 /* Database is read-only */
         8879  +
  8796   8880   /*
  8797   8881   ** Undo the hack that converts floating point types to integer for
  8798   8882   ** builds on processors without floating point support.
  8799   8883   */
  8800   8884   #ifdef SQLITE_OMIT_FLOATING_POINT
  8801   8885   # undef double
  8802   8886   #endif
................................................................................
  8936   9020   #ifdef __cplusplus
  8937   9021   extern "C" {
  8938   9022   #endif
  8939   9023   
  8940   9024   
  8941   9025   /*
  8942   9026   ** CAPI3REF: Session Object Handle
         9027  +**
         9028  +** An instance of this object is a [session] that can be used to
         9029  +** record changes to a database.
  8943   9030   */
  8944   9031   typedef struct sqlite3_session sqlite3_session;
  8945   9032   
  8946   9033   /*
  8947   9034   ** CAPI3REF: Changeset Iterator Handle
         9035  +**
         9036  +** An instance of this object acts as a cursor for iterating
         9037  +** over the elements of a [changeset] or [patchset].
  8948   9038   */
  8949   9039   typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
  8950   9040   
  8951   9041   /*
  8952   9042   ** CAPI3REF: Create A New Session Object
         9043  +** CONSTRUCTOR: sqlite3_session
  8953   9044   **
  8954   9045   ** Create a new session object attached to database handle db. If successful,
  8955   9046   ** a pointer to the new object is written to *ppSession and SQLITE_OK is
  8956   9047   ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
  8957   9048   ** error code (e.g. SQLITE_NOMEM) is returned.
  8958   9049   **
  8959   9050   ** It is possible to create multiple session objects attached to a single
................................................................................
  8982   9073     sqlite3 *db,                    /* Database handle */
  8983   9074     const char *zDb,                /* Name of db (e.g. "main") */
  8984   9075     sqlite3_session **ppSession     /* OUT: New session object */
  8985   9076   );
  8986   9077   
  8987   9078   /*
  8988   9079   ** CAPI3REF: Delete A Session Object
         9080  +** DESTRUCTOR: sqlite3_session
  8989   9081   **
  8990   9082   ** Delete a session object previously allocated using 
  8991   9083   ** [sqlite3session_create()]. Once a session object has been deleted, the
  8992   9084   ** results of attempting to use pSession with any other session module
  8993   9085   ** function are undefined.
  8994   9086   **
  8995   9087   ** Session objects must be deleted before the database handle to which they
................................................................................
  8997   9089   ** [sqlite3session_create()] for details.
  8998   9090   */
  8999   9091   SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
  9000   9092   
  9001   9093   
  9002   9094   /*
  9003   9095   ** CAPI3REF: Enable Or Disable A Session Object
         9096  +** METHOD: sqlite3_session
  9004   9097   **
  9005   9098   ** Enable or disable the recording of changes by a session object. When
  9006   9099   ** enabled, a session object records changes made to the database. When
  9007   9100   ** disabled - it does not. A newly created session object is enabled.
  9008   9101   ** Refer to the documentation for [sqlite3session_changeset()] for further
  9009   9102   ** details regarding how enabling and disabling a session object affects
  9010   9103   ** the eventual changesets.
................................................................................
  9016   9109   ** The return value indicates the final state of the session object: 0 if 
  9017   9110   ** the session is disabled, or 1 if it is enabled.
  9018   9111   */
  9019   9112   SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
  9020   9113   
  9021   9114   /*
  9022   9115   ** CAPI3REF: Set Or Clear the Indirect Change Flag
         9116  +** METHOD: sqlite3_session
  9023   9117   **
  9024   9118   ** Each change recorded by a session object is marked as either direct or
  9025   9119   ** indirect. A change is marked as indirect if either:
  9026   9120   **
  9027   9121   ** <ul>
  9028   9122   **   <li> The session object "indirect" flag is set when the change is
  9029   9123   **        made, or
................................................................................
  9045   9139   ** The return value indicates the final state of the indirect flag: 0 if 
  9046   9140   ** it is clear, or 1 if it is set.
  9047   9141   */
  9048   9142   SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
  9049   9143   
  9050   9144   /*
  9051   9145   ** CAPI3REF: Attach A Table To A Session Object
         9146  +** METHOD: sqlite3_session
  9052   9147   **
  9053   9148   ** If argument zTab is not NULL, then it is the name of a table to attach
  9054   9149   ** to the session object passed as the first argument. All subsequent changes 
  9055   9150   ** made to the table while the session object is enabled will be recorded. See 
  9056   9151   ** documentation for [sqlite3session_changeset()] for further details.
  9057   9152   **
  9058   9153   ** Or, if argument zTab is NULL, then changes are recorded for all tables
................................................................................
  9107   9202   SQLITE_API int sqlite3session_attach(
  9108   9203     sqlite3_session *pSession,      /* Session object */
  9109   9204     const char *zTab                /* Table name */
  9110   9205   );
  9111   9206   
  9112   9207   /*
  9113   9208   ** CAPI3REF: Set a table filter on a Session Object.
         9209  +** METHOD: sqlite3_session
  9114   9210   **
  9115   9211   ** The second argument (xFilter) is the "filter callback". For changes to rows 
  9116   9212   ** in tables that are not attached to the Session object, the filter is called
  9117   9213   ** to determine whether changes to the table's rows should be tracked or not. 
  9118   9214   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
  9119   9215   ** attached, xFilter will not be called again.
  9120   9216   */
................................................................................
  9125   9221       const char *zTab              /* Table name */
  9126   9222     ),
  9127   9223     void *pCtx                      /* First argument passed to xFilter */
  9128   9224   );
  9129   9225   
  9130   9226   /*
  9131   9227   ** CAPI3REF: Generate A Changeset From A Session Object
         9228  +** METHOD: sqlite3_session
  9132   9229   **
  9133   9230   ** Obtain a changeset containing changes to the tables attached to the 
  9134   9231   ** session object passed as the first argument. If successful, 
  9135   9232   ** set *ppChangeset to point to a buffer containing the changeset 
  9136   9233   ** and *pnChangeset to the size of the changeset in bytes before returning
  9137   9234   ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
  9138   9235   ** zero and return an SQLite error code.
................................................................................
  9234   9331   SQLITE_API int sqlite3session_changeset(
  9235   9332     sqlite3_session *pSession,      /* Session object */
  9236   9333     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
  9237   9334     void **ppChangeset              /* OUT: Buffer containing changeset */
  9238   9335   );
  9239   9336   
  9240   9337   /*
  9241         -** CAPI3REF: Load The Difference Between Tables Into A Session 
         9338  +** CAPI3REF: Load The Difference Between Tables Into A Session
         9339  +** METHOD: sqlite3_session
  9242   9340   **
  9243   9341   ** If it is not already attached to the session object passed as the first
  9244   9342   ** argument, this function attaches table zTbl in the same manner as the
  9245   9343   ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
  9246   9344   ** does not have a primary key, this function is a no-op (but does not return
  9247   9345   ** an error).
  9248   9346   **
................................................................................
  9299   9397     const char *zTbl,
  9300   9398     char **pzErrMsg
  9301   9399   );
  9302   9400   
  9303   9401   
  9304   9402   /*
  9305   9403   ** CAPI3REF: Generate A Patchset From A Session Object
         9404  +** METHOD: sqlite3_session
  9306   9405   **
  9307   9406   ** The differences between a patchset and a changeset are that:
  9308   9407   **
  9309   9408   ** <ul>
  9310   9409   **   <li> DELETE records consist of the primary key fields only. The 
  9311   9410   **        original values of other fields are omitted.
  9312   9411   **   <li> The original values of any modified fields are omitted from 
................................................................................
  9350   9449   ** guaranteed that a call to sqlite3session_changeset() will return a 
  9351   9450   ** changeset containing zero changes.
  9352   9451   */
  9353   9452   SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
  9354   9453   
  9355   9454   /*
  9356   9455   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
         9456  +** CONSTRUCTOR: sqlite3_changeset_iter
  9357   9457   **
  9358   9458   ** Create an iterator used to iterate through the contents of a changeset.
  9359   9459   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
  9360   9460   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
  9361   9461   ** SQLite error code is returned.
  9362   9462   **
  9363   9463   ** The following functions can be used to advance and query a changeset 
................................................................................
  9390   9490     int nChangeset,                 /* Size of changeset blob in bytes */
  9391   9491     void *pChangeset                /* Pointer to blob containing changeset */
  9392   9492   );
  9393   9493   
  9394   9494   
  9395   9495   /*
  9396   9496   ** CAPI3REF: Advance A Changeset Iterator
         9497  +** METHOD: sqlite3_changeset_iter
  9397   9498   **
  9398   9499   ** This function may only be used with iterators created by function
  9399   9500   ** [sqlite3changeset_start()]. If it is called on an iterator passed to
  9400   9501   ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
  9401   9502   ** is returned and the call has no effect.
  9402   9503   **
  9403   9504   ** Immediately after an iterator is created by sqlite3changeset_start(), it
................................................................................
  9414   9515   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
  9415   9516   ** SQLITE_NOMEM.
  9416   9517   */
  9417   9518   SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
  9418   9519   
  9419   9520   /*
  9420   9521   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
         9522  +** METHOD: sqlite3_changeset_iter
  9421   9523   **
  9422   9524   ** The pIter argument passed to this function may either be an iterator
  9423   9525   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9424   9526   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  9425   9527   ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
  9426   9528   ** is not the case, this function returns [SQLITE_MISUSE].
  9427   9529   **
................................................................................
  9448   9550     int *pnCol,                     /* OUT: Number of columns in table */
  9449   9551     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
  9450   9552     int *pbIndirect                 /* OUT: True for an 'indirect' change */
  9451   9553   );
  9452   9554   
  9453   9555   /*
  9454   9556   ** CAPI3REF: Obtain The Primary Key Definition Of A Table
         9557  +** METHOD: sqlite3_changeset_iter
  9455   9558   **
  9456   9559   ** For each modified table, a changeset includes the following:
  9457   9560   **
  9458   9561   ** <ul>
  9459   9562   **   <li> The number of columns in the table, and
  9460   9563   **   <li> Which of those columns make up the tables PRIMARY KEY.
  9461   9564   ** </ul>
................................................................................
  9479   9582     sqlite3_changeset_iter *pIter,  /* Iterator object */
  9480   9583     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
  9481   9584     int *pnCol                      /* OUT: Number of entries in output array */
  9482   9585   );
  9483   9586   
  9484   9587   /*
  9485   9588   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
         9589  +** METHOD: sqlite3_changeset_iter
  9486   9590   **
  9487   9591   ** The pIter argument passed to this function may either be an iterator
  9488   9592   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9489   9593   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  9490   9594   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
  9491   9595   ** Furthermore, it may only be called if the type of change that the iterator
  9492   9596   ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
................................................................................
  9509   9613     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9510   9614     int iVal,                       /* Column number */
  9511   9615     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
  9512   9616   );
  9513   9617   
  9514   9618   /*
  9515   9619   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
         9620  +** METHOD: sqlite3_changeset_iter
  9516   9621   **
  9517   9622   ** The pIter argument passed to this function may either be an iterator
  9518   9623   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9519   9624   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  9520   9625   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
  9521   9626   ** Furthermore, it may only be called if the type of change that the iterator
  9522   9627   ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
................................................................................
  9542   9647     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9543   9648     int iVal,                       /* Column number */
  9544   9649     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
  9545   9650   );
  9546   9651   
  9547   9652   /*
  9548   9653   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
         9654  +** METHOD: sqlite3_changeset_iter
  9549   9655   **
  9550   9656   ** This function should only be used with iterator objects passed to a
  9551   9657   ** conflict-handler callback by [sqlite3changeset_apply()] with either
  9552   9658   ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
  9553   9659   ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
  9554   9660   ** is set to NULL.
  9555   9661   **
................................................................................
  9569   9675     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9570   9676     int iVal,                       /* Column number */
  9571   9677     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
  9572   9678   );
  9573   9679   
  9574   9680   /*
  9575   9681   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
         9682  +** METHOD: sqlite3_changeset_iter
  9576   9683   **
  9577   9684   ** This function may only be called with an iterator passed to an
  9578   9685   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
  9579   9686   ** it sets the output variable to the total number of known foreign key
  9580   9687   ** violations in the destination database and returns SQLITE_OK.
  9581   9688   **
  9582   9689   ** In all other cases this function returns SQLITE_MISUSE.
................................................................................
  9585   9692     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9586   9693     int *pnOut                      /* OUT: Number of FK violations */
  9587   9694   );
  9588   9695   
  9589   9696   
  9590   9697   /*
  9591   9698   ** CAPI3REF: Finalize A Changeset Iterator
         9699  +** METHOD: sqlite3_changeset_iter
  9592   9700   **
  9593   9701   ** This function is used to finalize an iterator allocated with
  9594   9702   ** [sqlite3changeset_start()].
  9595   9703   **
  9596   9704   ** This function should only be called on iterators created using the
  9597   9705   ** [sqlite3changeset_start()] function. If an application calls this
  9598   9706   ** function with an iterator passed to a conflict-handler by
................................................................................
  9601   9709   **
  9602   9710   ** If an error was encountered within a call to an sqlite3changeset_xxx()
  9603   9711   ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
  9604   9712   ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
  9605   9713   ** to that error is returned by this function. Otherwise, SQLITE_OK is
  9606   9714   ** returned. This is to allow the following pattern (pseudo-code):
  9607   9715   **
         9716  +** <pre>
  9608   9717   **   sqlite3changeset_start();
  9609   9718   **   while( SQLITE_ROW==sqlite3changeset_next() ){
  9610   9719   **     // Do something with change.
  9611   9720   **   }
  9612   9721   **   rc = sqlite3changeset_finalize();
  9613   9722   **   if( rc!=SQLITE_OK ){
  9614   9723   **     // An error has occurred 
  9615   9724   **   }
         9725  +** </pre>
  9616   9726   */
  9617   9727   SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
  9618   9728   
  9619   9729   /*
  9620   9730   ** CAPI3REF: Invert A Changeset
  9621   9731   **
  9622   9732   ** This function is used to "invert" a changeset object. Applying an inverted
................................................................................
  9656   9766   ** single changeset. The result is a changeset equivalent to applying
  9657   9767   ** changeset A followed by changeset B. 
  9658   9768   **
  9659   9769   ** This function combines the two input changesets using an 
  9660   9770   ** sqlite3_changegroup object. Calling it produces similar results as the
  9661   9771   ** following code fragment:
  9662   9772   **
         9773  +** <pre>
  9663   9774   **   sqlite3_changegroup *pGrp;
  9664   9775   **   rc = sqlite3_changegroup_new(&pGrp);
  9665   9776   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
  9666   9777   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
  9667   9778   **   if( rc==SQLITE_OK ){
  9668   9779   **     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
  9669   9780   **   }else{
  9670   9781   **     *ppOut = 0;
  9671   9782   **     *pnOut = 0;
  9672   9783   **   }
         9784  +** </pre>
  9673   9785   **
  9674   9786   ** Refer to the sqlite3_changegroup documentation below for details.
  9675   9787   */
  9676   9788   SQLITE_API int sqlite3changeset_concat(
  9677   9789     int nA,                         /* Number of bytes in buffer pA */
  9678   9790     void *pA,                       /* Pointer to buffer containing changeset A */
  9679   9791     int nB,                         /* Number of bytes in buffer pB */
................................................................................
  9681   9793     int *pnOut,                     /* OUT: Number of bytes in output changeset */
  9682   9794     void **ppOut                    /* OUT: Buffer containing output changeset */
  9683   9795   );
  9684   9796   
  9685   9797   
  9686   9798   /*
  9687   9799   ** CAPI3REF: Changegroup Handle
         9800  +**
         9801  +** A changegroup is an object used to combine two or more 
         9802  +** [changesets] or [patchsets]
  9688   9803   */
  9689   9804   typedef struct sqlite3_changegroup sqlite3_changegroup;
  9690   9805   
  9691   9806   /*
  9692   9807   ** CAPI3REF: Create A New Changegroup Object
         9808  +** CONSTRUCTOR: sqlite3_changegroup
  9693   9809   **
  9694   9810   ** An sqlite3_changegroup object is used to combine two or more changesets
  9695   9811   ** (or patchsets) into a single changeset (or patchset). A single changegroup
  9696   9812   ** object may combine changesets or patchsets, but not both. The output is
  9697   9813   ** always in the same format as the input.
  9698   9814   **
  9699   9815   ** If successful, this function returns SQLITE_OK and populates (*pp) with
................................................................................
  9723   9839   ** sqlite3changegroup_output() functions, also available are the streaming
  9724   9840   ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
  9725   9841   */
  9726   9842   SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
  9727   9843   
  9728   9844   /*
  9729   9845   ** CAPI3REF: Add A Changeset To A Changegroup
         9846  +** METHOD: sqlite3_changegroup
  9730   9847   **
  9731   9848   ** Add all changes within the changeset (or patchset) in buffer pData (size
  9732   9849   ** nData bytes) to the changegroup. 
  9733   9850   **
  9734   9851   ** If the buffer contains a patchset, then all prior calls to this function
  9735   9852   ** on the same changegroup object must also have specified patchsets. Or, if
  9736   9853   ** the buffer contains a changeset, so must have the earlier calls to this
................................................................................
  9800   9917   **
  9801   9918   ** If no error occurs, SQLITE_OK is returned.
  9802   9919   */
  9803   9920   SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
  9804   9921   
  9805   9922   /*
  9806   9923   ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
         9924  +** METHOD: sqlite3_changegroup
  9807   9925   **
  9808   9926   ** Obtain a buffer containing a changeset (or patchset) representing the
  9809   9927   ** current contents of the changegroup. If the inputs to the changegroup
  9810   9928   ** were themselves changesets, the output is a changeset. Or, if the
  9811   9929   ** inputs were patchsets, the output is also a patchset.
  9812   9930   **
  9813   9931   ** As with the output of the sqlite3session_changeset() and
................................................................................
  9830   9948     sqlite3_changegroup*,
  9831   9949     int *pnData,                    /* OUT: Size of output buffer in bytes */
  9832   9950     void **ppData                   /* OUT: Pointer to output buffer */
  9833   9951   );
  9834   9952   
  9835   9953   /*
  9836   9954   ** CAPI3REF: Delete A Changegroup Object
         9955  +** DESTRUCTOR: sqlite3_changegroup
  9837   9956   */
  9838   9957   SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
  9839   9958   
  9840   9959   /*
  9841   9960   ** CAPI3REF: Apply A Changeset To A Database
  9842   9961   **
  9843         -** Apply a changeset to a database. This function attempts to update the
  9844         -** "main" database attached to handle db with the changes found in the
  9845         -** changeset passed via the second and third arguments.
         9962  +** Apply a changeset or patchset to a database. These functions attempt to
         9963  +** update the "main" database attached to handle db with the changes found in
         9964  +** the changeset passed via the second and third arguments. 
  9846   9965   **
  9847         -** The fourth argument (xFilter) passed to this function is the "filter
         9966  +** The fourth argument (xFilter) passed to these functions is the "filter
  9848   9967   ** callback". If it is not NULL, then for each table affected by at least one
  9849   9968   ** change in the changeset, the filter callback is invoked with
  9850   9969   ** the table name as the second argument, and a copy of the context pointer
  9851         -** passed as the sixth argument to this function as the first. If the "filter
  9852         -** callback" returns zero, then no attempt is made to apply any changes to 
  9853         -** the table. Otherwise, if the return value is non-zero or the xFilter
  9854         -** argument to this function is NULL, all changes related to the table are
  9855         -** attempted.
         9970  +** passed as the sixth argument as the first. If the "filter callback"
         9971  +** returns zero, then no attempt is made to apply any changes to the table.
         9972  +** Otherwise, if the return value is non-zero or the xFilter argument to
         9973  +** is NULL, all changes related to the table are attempted.
  9856   9974   **
  9857   9975   ** For each table that is not excluded by the filter callback, this function 
  9858   9976   ** tests that the target database contains a compatible table. A table is 
  9859   9977   ** considered compatible if all of the following are true:
  9860   9978   **
  9861   9979   ** <ul>
  9862   9980   **   <li> The table has the same name as the name recorded in the 
................................................................................
  9893  10011   ** actions are taken by sqlite3changeset_apply() depending on the value
  9894  10012   ** returned by each invocation of the conflict-handler function. Refer to
  9895  10013   ** the documentation for the three 
  9896  10014   ** [SQLITE_CHANGESET_OMIT|available return values] for details.
  9897  10015   **
  9898  10016   ** <dl>
  9899  10017   ** <dt>DELETE Changes<dd>
  9900         -**   For each DELETE change, this function checks if the target database 
        10018  +**   For each DELETE change, the function checks if the target database 
  9901  10019   **   contains a row with the same primary key value (or values) as the 
  9902  10020   **   original row values stored in the changeset. If it does, and the values 
  9903  10021   **   stored in all non-primary key columns also match the values stored in 
  9904  10022   **   the changeset the row is deleted from the target database.
  9905  10023   **
  9906  10024   **   If a row with matching primary key values is found, but one or more of
  9907  10025   **   the non-primary key fields contains a value different from the original
................................................................................
  9938  10056   **   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 
  9939  10057   **   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
  9940  10058   **   This includes the case where the INSERT operation is re-attempted because 
  9941  10059   **   an earlier call to the conflict handler function returned 
  9942  10060   **   [SQLITE_CHANGESET_REPLACE].
  9943  10061   **
  9944  10062   ** <dt>UPDATE Changes<dd>
  9945         -**   For each UPDATE change, this function checks if the target database 
        10063  +**   For each UPDATE change, the function checks if the target database 
  9946  10064   **   contains a row with the same primary key value (or values) as the 
  9947  10065   **   original row values stored in the changeset. If it does, and the values 
  9948  10066   **   stored in all modified non-primary key columns also match the values
  9949  10067   **   stored in the changeset the row is updated within the target database.
  9950  10068   **
  9951  10069   **   If a row with matching primary key values is found, but one or more of
  9952  10070   **   the modified non-primary key fields contains a value different from an
................................................................................
  9969  10087   ** </dl>
  9970  10088   **
  9971  10089   ** It is safe to execute SQL statements, including those that write to the
  9972  10090   ** table that the callback related to, from within the xConflict callback.
  9973  10091   ** This can be used to further customize the applications conflict
  9974  10092   ** resolution strategy.
  9975  10093   **
  9976         -** All changes made by this function are enclosed in a savepoint transaction.
        10094  +** All changes made by these functions are enclosed in a savepoint transaction.
  9977  10095   ** If any other error (aside from a constraint failure when attempting to
  9978  10096   ** write to the target database) occurs, then the savepoint transaction is
  9979  10097   ** rolled back, restoring the target database to its original state, and an 
  9980  10098   ** SQLite error code returned.
        10099  +**
        10100  +** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
        10101  +** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
        10102  +** may set (*ppRebase) to point to a "rebase" that may be used with the 
        10103  +** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
        10104  +** is set to the size of the buffer in bytes. It is the responsibility of the
        10105  +** caller to eventually free any such buffer using sqlite3_free(). The buffer
        10106  +** is only allocated and populated if one or more conflicts were encountered
        10107  +** while applying the patchset. See comments surrounding the sqlite3_rebaser
        10108  +** APIs for further details.
  9981  10109   */
  9982  10110   SQLITE_API int sqlite3changeset_apply(
  9983  10111     sqlite3 *db,                    /* Apply change to "main" db of this handle */
  9984  10112     int nChangeset,                 /* Size of changeset in bytes */
  9985  10113     void *pChangeset,               /* Changeset blob */
  9986  10114     int(*xFilter)(
  9987  10115       void *pCtx,                   /* Copy of sixth arg to _apply() */
................................................................................
  9989  10117     ),
  9990  10118     int(*xConflict)(
  9991  10119       void *pCtx,                   /* Copy of sixth arg to _apply() */
  9992  10120       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
  9993  10121       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
  9994  10122     ),
  9995  10123     void *pCtx                      /* First argument passed to xConflict */
        10124  +);
        10125  +SQLITE_API int sqlite3changeset_apply_v2(
        10126  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
        10127  +  int nChangeset,                 /* Size of changeset in bytes */
        10128  +  void *pChangeset,               /* Changeset blob */
        10129  +  int(*xFilter)(
        10130  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        10131  +    const char *zTab              /* Table name */
        10132  +  ),
        10133  +  int(*xConflict)(
        10134  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        10135  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
        10136  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
        10137  +  ),
        10138  +  void *pCtx,                     /* First argument passed to xConflict */
        10139  +  void **ppRebase, int *pnRebase
  9996  10140   );
  9997  10141   
  9998  10142   /* 
  9999  10143   ** CAPI3REF: Constants Passed To The Conflict Handler
 10000  10144   **
 10001  10145   ** Values that may be passed as the second argument to a conflict-handler.
 10002  10146   **
................................................................................
 10087  10231   **   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
 10088  10232   ** </dl>
 10089  10233   */
 10090  10234   #define SQLITE_CHANGESET_OMIT       0
 10091  10235   #define SQLITE_CHANGESET_REPLACE    1
 10092  10236   #define SQLITE_CHANGESET_ABORT      2
 10093  10237   
        10238  +/* 
        10239  +** CAPI3REF: Rebasing changesets
        10240  +** EXPERIMENTAL
        10241  +**
        10242  +** Suppose there is a site hosting a database in state S0. And that
        10243  +** modifications are made that move that database to state S1 and a
        10244  +** changeset recorded (the "local" changeset). Then, a changeset based
        10245  +** on S0 is received from another site (the "remote" changeset) and 
        10246  +** applied to the database. The database is then in state 
        10247  +** (S1+"remote"), where the exact state depends on any conflict
        10248  +** resolution decisions (OMIT or REPLACE) made while applying "remote".
        10249  +** Rebasing a changeset is to update it to take those conflict 
        10250  +** resolution decisions into account, so that the same conflicts
        10251  +** do not have to be resolved elsewhere in the network. 
        10252  +**
        10253  +** For example, if both the local and remote changesets contain an
        10254  +** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
        10255  +**
        10256  +**   local:  INSERT INTO t1 VALUES(1, 'v1');
        10257  +**   remote: INSERT INTO t1 VALUES(1, 'v2');
        10258  +**
        10259  +** and the conflict resolution is REPLACE, then the INSERT change is
        10260  +** removed from the local changeset (it was overridden). Or, if the
        10261  +** conflict resolution was "OMIT", then the local changeset is modified
        10262  +** to instead contain:
        10263  +**
        10264  +**           UPDATE t1 SET b = 'v2' WHERE a=1;
        10265  +**
        10266  +** Changes within the local changeset are rebased as follows:
        10267  +**
        10268  +** <dl>
        10269  +** <dt>Local INSERT<dd>
        10270  +**   This may only conflict with a remote INSERT. If the conflict 
        10271  +**   resolution was OMIT, then add an UPDATE change to the rebased
        10272  +**   changeset. Or, if the conflict resolution was REPLACE, add
        10273  +**   nothing to the rebased changeset.
        10274  +**
        10275  +** <dt>Local DELETE<dd>
        10276  +**   This may conflict with a remote UPDATE or DELETE. In both cases the
        10277  +**   only possible resolution is OMIT. If the remote operation was a
        10278  +**   DELETE, then add no change to the rebased changeset. If the remote
        10279  +**   operation was an UPDATE, then the old.* fields of change are updated
        10280  +**   to reflect the new.* values in the UPDATE.
        10281  +**
        10282  +** <dt>Local UPDATE<dd>
        10283  +**   This may conflict with a remote UPDATE or DELETE. If it conflicts
        10284  +**   with a DELETE, and the conflict resolution was OMIT, then the update
        10285  +**   is changed into an INSERT. Any undefined values in the new.* record
        10286  +**   from the update change are filled in using the old.* values from
        10287  +**   the conflicting DELETE. Or, if the conflict resolution was REPLACE,
        10288  +**   the UPDATE change is simply omitted from the rebased changeset.
        10289  +**
        10290  +**   If conflict is with a remote UPDATE and the resolution is OMIT, then
        10291  +**   the old.* values are rebased using the new.* values in the remote
        10292  +**   change. Or, if the resolution is REPLACE, then the change is copied
        10293  +**   into the rebased changeset with updates to columns also updated by
        10294  +**   the conflicting remote UPDATE removed. If this means no columns would 
        10295  +**   be updated, the change is omitted.
        10296  +** </dl>
        10297  +**
        10298  +** A local change may be rebased against multiple remote changes 
        10299  +** simultaneously. If a single key is modified by multiple remote 
        10300  +** changesets, they are combined as follows before the local changeset
        10301  +** is rebased:
        10302  +**
        10303  +** <ul>
        10304  +**    <li> If there has been one or more REPLACE resolutions on a
        10305  +**         key, it is rebased according to a REPLACE.
        10306  +**
        10307  +**    <li> If there have been no REPLACE resolutions on a key, then
        10308  +**         the local changeset is rebased according to the most recent
        10309  +**         of the OMIT resolutions.
        10310  +** </ul>
        10311  +**
        10312  +** Note that conflict resolutions from multiple remote changesets are 
        10313  +** combined on a per-field basis, not per-row. This means that in the 
        10314  +** case of multiple remote UPDATE operations, some fields of a single 
        10315  +** local change may be rebased for REPLACE while others are rebased for 
        10316  +** OMIT.
        10317  +**
        10318  +** In order to rebase a local changeset, the remote changeset must first
        10319  +** be applied to the local database using sqlite3changeset_apply_v2() and
        10320  +** the buffer of rebase information captured. Then:
        10321  +**
        10322  +** <ol>
        10323  +**   <li> An sqlite3_rebaser object is created by calling 
        10324  +**        sqlite3rebaser_create().
        10325  +**   <li> The new object is configured with the rebase buffer obtained from
        10326  +**        sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
        10327  +**        If the local changeset is to be rebased against multiple remote
        10328  +**        changesets, then sqlite3rebaser_configure() should be called
        10329  +**        multiple times, in the same order that the multiple
        10330  +**        sqlite3changeset_apply_v2() calls were made.
        10331  +**   <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
        10332  +**   <li> The sqlite3_rebaser object is deleted by calling
        10333  +**        sqlite3rebaser_delete().
        10334  +** </ol>
        10335  +*/
        10336  +typedef struct sqlite3_rebaser sqlite3_rebaser;
        10337  +
        10338  +/*
        10339  +** CAPI3REF: Create a changeset rebaser object.
        10340  +** EXPERIMENTAL
        10341  +**
        10342  +** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
        10343  +** point to the new object and return SQLITE_OK. Otherwise, if an error
        10344  +** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew) 
        10345  +** to NULL. 
        10346  +*/
        10347  +SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
        10348  +
        10349  +/*
        10350  +** CAPI3REF: Configure a changeset rebaser object.
        10351  +** EXPERIMENTAL
        10352  +**
        10353  +** Configure the changeset rebaser object to rebase changesets according
        10354  +** to the conflict resolutions described by buffer pRebase (size nRebase
        10355  +** bytes), which must have been obtained from a previous call to
        10356  +** sqlite3changeset_apply_v2().
        10357  +*/
        10358  +SQLITE_API int sqlite3rebaser_configure(
        10359  +  sqlite3_rebaser*, 
        10360  +  int nRebase, const void *pRebase
        10361  +); 
        10362  +
        10363  +/*
        10364  +** CAPI3REF: Rebase a changeset
        10365  +** EXPERIMENTAL
        10366  +**
        10367  +** Argument pIn must point to a buffer containing a changeset nIn bytes
        10368  +** in size. This function allocates and populates a buffer with a copy
        10369  +** of the changeset rebased rebased according to the configuration of the
        10370  +** rebaser object passed as the first argument. If successful, (*ppOut)
        10371  +** is set to point to the new buffer containing the rebased changset and 
        10372  +** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
        10373  +** responsibility of the caller to eventually free the new buffer using
        10374  +** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
        10375  +** are set to zero and an SQLite error code returned.
        10376  +*/
        10377  +SQLITE_API int sqlite3rebaser_rebase(
        10378  +  sqlite3_rebaser*,
        10379  +  int nIn, const void *pIn, 
        10380  +  int *pnOut, void **ppOut 
        10381  +);
        10382  +
        10383  +/*
        10384  +** CAPI3REF: Delete a changeset rebaser object.
        10385  +** EXPERIMENTAL
        10386  +**
        10387  +** Delete the changeset rebaser object and all associated resources. There
        10388  +** should be one call to this function for each successful invocation
        10389  +** of sqlite3rebaser_create().
        10390  +*/
        10391  +SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p); 
        10392  +
 10094  10393   /*
 10095  10394   ** CAPI3REF: Streaming Versions of API functions.
 10096  10395   **
 10097  10396   ** The six streaming API xxx_strm() functions serve similar purposes to the 
 10098  10397   ** corresponding non-streaming API functions:
 10099  10398   **
 10100  10399   ** <table border=1 style="margin-left:8ex;margin-right:8ex">
................................................................................
 10190  10489     ),
 10191  10490     int(*xConflict)(
 10192  10491       void *pCtx,                   /* Copy of sixth arg to _apply() */
 10193  10492       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 10194  10493       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 10195  10494     ),
 10196  10495     void *pCtx                      /* First argument passed to xConflict */
        10496  +);
        10497  +SQLITE_API int sqlite3changeset_apply_v2_strm(
        10498  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
        10499  +  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
        10500  +  void *pIn,                                          /* First arg for xInput */
        10501  +  int(*xFilter)(
        10502  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        10503  +    const char *zTab              /* Table name */
        10504  +  ),
        10505  +  int(*xConflict)(
        10506  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        10507  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
        10508  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
        10509  +  ),
        10510  +  void *pCtx,                     /* First argument passed to xConflict */
        10511  +  void **ppRebase, int *pnRebase
 10197  10512   );
 10198  10513   SQLITE_API int sqlite3changeset_concat_strm(
 10199  10514     int (*xInputA)(void *pIn, void *pData, int *pnData),
 10200  10515     void *pInA,
 10201  10516     int (*xInputB)(void *pIn, void *pData, int *pnData),
 10202  10517     void *pInB,
 10203  10518     int (*xOutput)(void *pOut, const void *pData, int nData),
................................................................................
 10227  10542   SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
 10228  10543       int (*xInput)(void *pIn, void *pData, int *pnData),
 10229  10544       void *pIn
 10230  10545   );
 10231  10546   SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
 10232  10547       int (*xOutput)(void *pOut, const void *pData, int nData), 
 10233  10548       void *pOut
        10549  +);
        10550  +SQLITE_API int sqlite3rebaser_rebase_strm(
        10551  +  sqlite3_rebaser *pRebaser,
        10552  +  int (*xInput)(void *pIn, void *pData, int *pnData),
        10553  +  void *pIn,
        10554  +  int (*xOutput)(void *pOut, const void *pData, int nData),
        10555  +  void *pOut
 10234  10556   );
 10235  10557   
 10236  10558   
 10237  10559   /*
 10238  10560   ** Make sure we can call this stuff from C++.
 10239  10561   */
 10240  10562   #ifdef __cplusplus