sqllogictest
Check-in [573f24b86a]
Not logged in

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

Overview
Comment:Update the built-in SQLite to the first 3.23.0 beta.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 573f24b86a7421177f031079ec6993901c792e4c
User & Date: drh 2018-03-28 20:02:10
Context
2018-05-30
01:40
Update the built-in SQLite to the first 3.24.0 beta. check-in: f50f35585d user: drh tags: trunk
2018-03-28
20:02
Update the built-in SQLite to the first 3.23.0 beta. check-in: 573f24b86a user: drh tags: trunk
2018-01-15
22:41
Update the built-in SQLite to the first 3.22.0 beta check-in: 6996cda303 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/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-15 19:00:35 b0b7d0363acf38c2178e2d3041d8ce2a0de061a51caa64670dbf539ee6d4356b"
         1150  +#define SQLITE_VERSION        "3.23.0"
         1151  +#define SQLITE_VERSION_NUMBER 3023000
         1152  +#define SQLITE_SOURCE_ID      "2018-03-28 15:56:55 eb29b3369e76ec1df25a5484d8ec5fb924e23d5c70aaa4d794b2b17ee187alt1"
  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
................................................................................
  1530   1530   #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
  1531   1531   #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
  1532   1532   #define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
  1533   1533   #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
  1534   1534   #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
  1535   1535   #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
  1536   1536   #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
  1537         -#define SQLITE_CANTOPEN_DIRTYWAL       (SQLITE_CANTOPEN | (5<<8))
  1538   1537   #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
  1539   1538   #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
  1540   1539   #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
  1541   1540   #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
  1542   1541   #define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
  1543   1542   #define SQLITE_READONLY_CANTINIT       (SQLITE_READONLY | (5<<8))
  1544   1543   #define SQLITE_READONLY_DIRECTORY      (SQLITE_READONLY | (6<<8))
................................................................................
  2085   2084   ** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
  2086   2085   ** operations since the previous successful call to 
  2087   2086   ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
  2088   2087   ** ^This file control takes the file descriptor out of batch write mode
  2089   2088   ** so that all subsequent write operations are independent.
  2090   2089   ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
  2091   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.
  2092   2097   ** </ul>
  2093   2098   */
  2094   2099   #define SQLITE_FCNTL_LOCKSTATE               1
  2095   2100   #define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
  2096   2101   #define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
  2097   2102   #define SQLITE_FCNTL_LAST_ERRNO              4
  2098   2103   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
  2119   2124   #define SQLITE_FCNTL_VFS_POINTER            27
  2120   2125   #define SQLITE_FCNTL_JOURNAL_POINTER        28
  2121   2126   #define SQLITE_FCNTL_WIN32_GET_HANDLE       29
  2122   2127   #define SQLITE_FCNTL_PDB                    30
  2123   2128   #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
  2124   2129   #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
  2125   2130   #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
         2131  +#define SQLITE_FCNTL_LOCK_TIMEOUT           34
  2126   2132   
  2127   2133   /* deprecated names */
  2128   2134   #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
  2129   2135   #define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
  2130   2136   #define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
  2131   2137   
  2132   2138   
................................................................................
  3075   3081   **
  3076   3082   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  3077   3083   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  3078   3084   ** database handle, SQLite checks if this will mean that there are now no 
  3079   3085   ** connections at all to the database. If so, it performs a checkpoint 
  3080   3086   ** operation before closing the connection. This option may be used to
  3081   3087   ** override this behaviour. The first parameter passed to this operation
  3082         -** is an integer - non-zero to disable checkpoints-on-close, or zero (the
  3083         -** 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
  3084   3091   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  3085   3092   ** have been disabled - 0 if they are not disabled, 1 if they are.
  3086   3093   ** </dd>
         3094  +**
  3087   3095   ** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  3088   3096   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  3089   3097   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  3090   3098   ** a single SQL query statement will always use the same algorithm regardless
  3091   3099   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  3092   3100   ** that look at the values of bound parameters, which can make some queries
  3093   3101   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  3094   3102   ** the QPSG active, SQLite will always use the same query plan in the field as
  3095   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.
  3096   3109   ** </dd>
         3110  +**
  3097   3111   ** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  3098   3112   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  3099   3113   ** include output for any operations performed by trigger programs. This
  3100   3114   ** option is used to set or clear (the default) a flag that governs this
  3101   3115   ** behavior. The first parameter passed to this operation is an integer -
  3102         -** 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.
  3103   3118   ** The second parameter is a pointer to an integer into which is written 
  3104   3119   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  3105   3120   ** it is not disabled, 1 if it is.  
  3106   3121   ** </dd>
  3107   3122   ** </dl>
  3108   3123   */
  3109   3124   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
................................................................................
  3517   3532   SQLITE_API void sqlite3_free_table(char **result);
  3518   3533   
  3519   3534   /*
  3520   3535   ** CAPI3REF: Formatted String Printing Functions
  3521   3536   **
  3522   3537   ** These routines are work-alikes of the "printf()" family of functions
  3523   3538   ** from the standard C library.
  3524         -** These routines understand most of the common K&R formatting options,
  3525         -** plus some additional non-standard formats, detailed below.
  3526         -** Note that some of the more obscure formatting options from recent
  3527         -** 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.
  3528   3543   **
  3529   3544   ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  3530         -** results into memory obtained from [sqlite3_malloc()].
         3545  +** results into memory obtained from [sqlite3_malloc64()].
  3531   3546   ** The strings returned by these two routines should be
  3532   3547   ** released by [sqlite3_free()].  ^Both routines return a
  3533         -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
         3548  +** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
  3534   3549   ** memory to hold the resulting string.
  3535   3550   **
  3536   3551   ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  3537   3552   ** the standard C library.  The result is written into the
  3538   3553   ** buffer supplied as the second parameter whose size is given by
  3539   3554   ** the first parameter. Note that the order of the
  3540   3555   ** first two parameters is reversed from snprintf().)^  This is an
................................................................................
  3550   3565   ** guarantees that the buffer is always zero-terminated.  ^The first
  3551   3566   ** parameter "n" is the total size of the buffer, including space for
  3552   3567   ** the zero terminator.  So the longest string that can be completely
  3553   3568   ** written will be n-1 characters.
  3554   3569   **
  3555   3570   ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  3556   3571   **
  3557         -** These routines all implement some additional formatting
  3558         -** options that are useful for constructing SQL statements.
  3559         -** All of the usual printf() formatting options apply.  In addition, there
  3560         -** is are "%q", "%Q", "%w" and "%z" options.
  3561         -**
  3562         -** ^(The %q option works like %s in that it substitutes a nul-terminated
  3563         -** string from the argument list.  But %q also doubles every '\'' character.
  3564         -** %q is designed for use inside a string literal.)^  By doubling each '\''
  3565         -** character it escapes that character and allows it to be inserted into
  3566         -** the string.
  3567         -**
  3568         -** For example, assume the string variable zText contains text as follows:
  3569         -**
  3570         -** <blockquote><pre>
  3571         -**  char *zText = "It's a happy day!";
  3572         -** </pre></blockquote>
  3573         -**
  3574         -** One can use this text in an SQL statement as follows:
  3575         -**
  3576         -** <blockquote><pre>
  3577         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  3578         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  3579         -**  sqlite3_free(zSQL);
  3580         -** </pre></blockquote>
  3581         -**
  3582         -** Because the %q format string is used, the '\'' character in zText
  3583         -** is escaped and the SQL generated is as follows:
  3584         -**
  3585         -** <blockquote><pre>
  3586         -**  INSERT INTO table1 VALUES('It''s a happy day!')
  3587         -** </pre></blockquote>
  3588         -**
  3589         -** This is correct.  Had we used %s instead of %q, the generated SQL
  3590         -** would have looked like this:
  3591         -**
  3592         -** <blockquote><pre>
  3593         -**  INSERT INTO table1 VALUES('It's a happy day!');
  3594         -** </pre></blockquote>
  3595         -**
  3596         -** This second example is an SQL syntax error.  As a general rule you should
  3597         -** always use %q instead of %s when inserting text into a string literal.
  3598         -**
  3599         -** ^(The %Q option works like %q except it also adds single quotes around
  3600         -** the outside of the total string.  Additionally, if the parameter in the
  3601         -** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  3602         -** single quotes).)^  So, for example, one could say:
  3603         -**
  3604         -** <blockquote><pre>
  3605         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  3606         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  3607         -**  sqlite3_free(zSQL);
  3608         -** </pre></blockquote>
  3609         -**
  3610         -** The code above will render a correct SQL statement in the zSQL
  3611         -** variable even if the zText variable is a NULL pointer.
  3612         -**
  3613         -** ^(The "%w" formatting option is like "%q" except that it expects to
  3614         -** be contained within double-quotes instead of single quotes, and it
  3615         -** escapes the double-quote character instead of the single-quote
  3616         -** character.)^  The "%w" formatting option is intended for safely inserting
  3617         -** table and column names into a constructed SQL statement.
  3618         -**
  3619         -** ^(The "%z" formatting option works like "%s" but with the
  3620         -** addition that after the string has been read and copied into
  3621         -** the result, [sqlite3_free()] is called on the input string.)^
         3572  +** See also:  [built-in printf()], [printf() SQL function]
  3622   3573   */
  3623   3574   SQLITE_API char *sqlite3_mprintf(const char*,...);
  3624   3575   SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  3625   3576   SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  3626   3577   SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  3627   3578   
  3628   3579   /*
................................................................................
  3972   3923      void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
  3973   3924   
  3974   3925   /*
  3975   3926   ** CAPI3REF: SQL Trace Event Codes
  3976   3927   ** KEYWORDS: SQLITE_TRACE
  3977   3928   **
  3978   3929   ** These constants identify classes of events that can be monitored
  3979         -** using the [sqlite3_trace_v2()] tracing logic.  The third argument
  3980         -** to [sqlite3_trace_v2()] is an OR-ed combination of one or more of
         3930  +** using the [sqlite3_trace_v2()] tracing logic.  The M argument
         3931  +** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
  3981   3932   ** the following constants.  ^The first argument to the trace callback
  3982   3933   ** is one of the following constants.
  3983   3934   **
  3984   3935   ** New tracing constants may be added in future releases.
  3985   3936   **
  3986   3937   ** ^A trace callback has four arguments: xCallback(T,C,P,X).
  3987   3938   ** ^The T argument is one of the integer type codes above.
................................................................................
  4680   4631   ** a schema change, on the first  [sqlite3_step()] call following any change
  4681   4632   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  4682   4633   ** ^The specific value of WHERE-clause [parameter] might influence the 
  4683   4634   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  4684   4635   ** or [GLOB] operator or if the parameter is compared to an indexed column
  4685   4636   ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  4686   4637   ** </li>
         4638  +** </ol>
  4687   4639   **
  4688   4640   ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
  4689   4641   ** the extra prepFlags parameter, which is a bit array consisting of zero or
  4690   4642   ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
  4691   4643   ** sqlite3_prepare_v2() interface works exactly the same as
  4692   4644   ** sqlite3_prepare_v3() with a zero prepFlags parameter.
  4693         -** </ol>
  4694   4645   */
  4695   4646   SQLITE_API int sqlite3_prepare(
  4696   4647     sqlite3 *db,            /* Database handle */
  4697   4648     const char *zSql,       /* SQL statement, UTF-8 encoded */
  4698   4649     int nByte,              /* Maximum length of zSql in bytes. */
  4699   4650     sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  4700   4651     const char **pzTail     /* OUT: Pointer to unused portion of zSql */
................................................................................
  8314   8265   ** wal file in wal mode databases, or the number of pages written to the
  8315   8266   ** database file in rollback mode databases. Any pages written as part of
  8316   8267   ** transaction rollback or database recovery operations are not included.
  8317   8268   ** If an IO or other error occurs while writing a page to disk, the effect
  8318   8269   ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  8319   8270   ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  8320   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>
  8321   8281   **
  8322   8282   ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  8323   8283   ** <dd>This parameter returns zero for the current value if and only if
  8324   8284   ** all foreign key constraints (deferred or immediate) have been
  8325   8285   ** resolved.)^  ^The highwater mark is always 0.
  8326   8286   ** </dd>
  8327   8287   ** </dl>
................................................................................
  8334   8294   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  8335   8295   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  8336   8296   #define SQLITE_DBSTATUS_CACHE_HIT            7
  8337   8297   #define SQLITE_DBSTATUS_CACHE_MISS           8
  8338   8298   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  8339   8299   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  8340   8300   #define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
  8341         -#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
         8301  +#define SQLITE_DBSTATUS_CACHE_SPILL         12
         8302  +#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
  8342   8303   
  8343   8304   
  8344   8305   /*
  8345   8306   ** CAPI3REF: Prepared Statement Status
  8346   8307   ** METHOD: sqlite3_stmt
  8347   8308   **
  8348   8309   ** ^(Each prepared statement maintains various
................................................................................
  9814   9775   ** transaction open on the database, or if the database is not a wal mode
  9815   9776   ** database.
  9816   9777   **
  9817   9778   ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  9818   9779   */
  9819   9780   SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
  9820   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  +
  9821   9904   /*
  9822   9905   ** Undo the hack that converts floating point types to integer for
  9823   9906   ** builds on processors without floating point support.
  9824   9907   */
  9825   9908   #ifdef SQLITE_OMIT_FLOATING_POINT
  9826   9909   # undef double
  9827   9910   #endif
................................................................................
  9961  10044   #if 0
  9962  10045   extern "C" {
  9963  10046   #endif
  9964  10047   
  9965  10048   
  9966  10049   /*
  9967  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.
  9968  10054   */
  9969  10055   typedef struct sqlite3_session sqlite3_session;
  9970  10056   
  9971  10057   /*
  9972  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].
  9973  10062   */
  9974  10063   typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
  9975  10064   
  9976  10065   /*
  9977  10066   ** CAPI3REF: Create A New Session Object
        10067  +** CONSTRUCTOR: sqlite3_session
  9978  10068   **
  9979  10069   ** Create a new session object attached to database handle db. If successful,
  9980  10070   ** a pointer to the new object is written to *ppSession and SQLITE_OK is
  9981  10071   ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
  9982  10072   ** error code (e.g. SQLITE_NOMEM) is returned.
  9983  10073   **
  9984  10074   ** It is possible to create multiple session objects attached to a single
................................................................................
 10007  10097     sqlite3 *db,                    /* Database handle */
 10008  10098     const char *zDb,                /* Name of db (e.g. "main") */
 10009  10099     sqlite3_session **ppSession     /* OUT: New session object */
 10010  10100   );
 10011  10101   
 10012  10102   /*
 10013  10103   ** CAPI3REF: Delete A Session Object
        10104  +** DESTRUCTOR: sqlite3_session
 10014  10105   **
 10015  10106   ** Delete a session object previously allocated using 
 10016  10107   ** [sqlite3session_create()]. Once a session object has been deleted, the
 10017  10108   ** results of attempting to use pSession with any other session module
 10018  10109   ** function are undefined.
 10019  10110   **
 10020  10111   ** Session objects must be deleted before the database handle to which they
................................................................................
 10022  10113   ** [sqlite3session_create()] for details.
 10023  10114   */
 10024  10115   SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
 10025  10116   
 10026  10117   
 10027  10118   /*
 10028  10119   ** CAPI3REF: Enable Or Disable A Session Object
        10120  +** METHOD: sqlite3_session
 10029  10121   **
 10030  10122   ** Enable or disable the recording of changes by a session object. When
 10031  10123   ** enabled, a session object records changes made to the database. When
 10032  10124   ** disabled - it does not. A newly created session object is enabled.
 10033  10125   ** Refer to the documentation for [sqlite3session_changeset()] for further
 10034  10126   ** details regarding how enabling and disabling a session object affects
 10035  10127   ** the eventual changesets.
................................................................................
 10041  10133   ** The return value indicates the final state of the session object: 0 if 
 10042  10134   ** the session is disabled, or 1 if it is enabled.
 10043  10135   */
 10044  10136   SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
 10045  10137   
 10046  10138   /*
 10047  10139   ** CAPI3REF: Set Or Clear the Indirect Change Flag
        10140  +** METHOD: sqlite3_session
 10048  10141   **
 10049  10142   ** Each change recorded by a session object is marked as either direct or
 10050  10143   ** indirect. A change is marked as indirect if either:
 10051  10144   **
 10052  10145   ** <ul>
 10053  10146   **   <li> The session object "indirect" flag is set when the change is
 10054  10147   **        made, or
................................................................................
 10070  10163   ** The return value indicates the final state of the indirect flag: 0 if 
 10071  10164   ** it is clear, or 1 if it is set.
 10072  10165   */
 10073  10166   SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
 10074  10167   
 10075  10168   /*
 10076  10169   ** CAPI3REF: Attach A Table To A Session Object
        10170  +** METHOD: sqlite3_session
 10077  10171   **
 10078  10172   ** If argument zTab is not NULL, then it is the name of a table to attach
 10079  10173   ** to the session object passed as the first argument. All subsequent changes 
 10080  10174   ** made to the table while the session object is enabled will be recorded. See 
 10081  10175   ** documentation for [sqlite3session_changeset()] for further details.
 10082  10176   **
 10083  10177   ** Or, if argument zTab is NULL, then changes are recorded for all tables
................................................................................
 10095  10189   ** no changes will be recorded in either of these scenarios.
 10096  10190   **
 10097  10191   ** Changes are not recorded for individual rows that have NULL values stored
 10098  10192   ** in one or more of their PRIMARY KEY columns.
 10099  10193   **
 10100  10194   ** SQLITE_OK is returned if the call completes without error. Or, if an error 
 10101  10195   ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
        10196  +**
        10197  +** <h3>Special sqlite_stat1 Handling</h3>
        10198  +**
        10199  +** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to 
        10200  +** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
        10201  +**  <pre>
        10202  +**  &nbsp;     CREATE TABLE sqlite_stat1(tbl,idx,stat)  
        10203  +**  </pre>
        10204  +**
        10205  +** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are 
        10206  +** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes 
        10207  +** are recorded for rows for which (idx IS NULL) is true. However, for such
        10208  +** rows a zero-length blob (SQL value X'') is stored in the changeset or
        10209  +** patchset instead of a NULL value. This allows such changesets to be
        10210  +** manipulated by legacy implementations of sqlite3changeset_invert(),
        10211  +** concat() and similar.
        10212  +**
        10213  +** The sqlite3changeset_apply() function automatically converts the 
        10214  +** zero-length blob back to a NULL value when updating the sqlite_stat1
        10215  +** table. However, if the application calls sqlite3changeset_new(),
        10216  +** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset 
        10217  +** iterator directly (including on a changeset iterator passed to a
        10218  +** conflict-handler callback) then the X'' value is returned. The application
        10219  +** must translate X'' to NULL itself if required.
        10220  +**
        10221  +** Legacy (older than 3.22.0) versions of the sessions module cannot capture
        10222  +** changes made to the sqlite_stat1 table. Legacy versions of the
        10223  +** sqlite3changeset_apply() function silently ignore any modifications to the
        10224  +** sqlite_stat1 table that are part of a changeset or patchset.
 10102  10225   */
 10103  10226   SQLITE_API int sqlite3session_attach(
 10104  10227     sqlite3_session *pSession,      /* Session object */
 10105  10228     const char *zTab                /* Table name */
 10106  10229   );
 10107  10230   
 10108  10231   /*
 10109  10232   ** CAPI3REF: Set a table filter on a Session Object.
        10233  +** METHOD: sqlite3_session
 10110  10234   **
 10111  10235   ** The second argument (xFilter) is the "filter callback". For changes to rows 
 10112  10236   ** in tables that are not attached to the Session object, the filter is called
 10113  10237   ** to determine whether changes to the table's rows should be tracked or not. 
 10114  10238   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
 10115  10239   ** attached, xFilter will not be called again.
 10116  10240   */
................................................................................
 10121  10245       const char *zTab              /* Table name */
 10122  10246     ),
 10123  10247     void *pCtx                      /* First argument passed to xFilter */
 10124  10248   );
 10125  10249   
 10126  10250   /*
 10127  10251   ** CAPI3REF: Generate A Changeset From A Session Object
        10252  +** METHOD: sqlite3_session
 10128  10253   **
 10129  10254   ** Obtain a changeset containing changes to the tables attached to the 
 10130  10255   ** session object passed as the first argument. If successful, 
 10131  10256   ** set *ppChangeset to point to a buffer containing the changeset 
 10132  10257   ** and *pnChangeset to the size of the changeset in bytes before returning
 10133  10258   ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
 10134  10259   ** zero and return an SQLite error code.
................................................................................
 10230  10355   SQLITE_API int sqlite3session_changeset(
 10231  10356     sqlite3_session *pSession,      /* Session object */
 10232  10357     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
 10233  10358     void **ppChangeset              /* OUT: Buffer containing changeset */
 10234  10359   );
 10235  10360   
 10236  10361   /*
 10237         -** CAPI3REF: Load The Difference Between Tables Into A Session 
        10362  +** CAPI3REF: Load The Difference Between Tables Into A Session
        10363  +** METHOD: sqlite3_session
 10238  10364   **
 10239  10365   ** If it is not already attached to the session object passed as the first
 10240  10366   ** argument, this function attaches table zTbl in the same manner as the
 10241  10367   ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
 10242  10368   ** does not have a primary key, this function is a no-op (but does not return
 10243  10369   ** an error).
 10244  10370   **
................................................................................
 10295  10421     const char *zTbl,
 10296  10422     char **pzErrMsg
 10297  10423   );
 10298  10424   
 10299  10425   
 10300  10426   /*
 10301  10427   ** CAPI3REF: Generate A Patchset From A Session Object
        10428  +** METHOD: sqlite3_session
 10302  10429   **
 10303  10430   ** The differences between a patchset and a changeset are that:
 10304  10431   **
 10305  10432   ** <ul>
 10306  10433   **   <li> DELETE records consist of the primary key fields only. The 
 10307  10434   **        original values of other fields are omitted.
 10308  10435   **   <li> The original values of any modified fields are omitted from 
................................................................................
 10346  10473   ** guaranteed that a call to sqlite3session_changeset() will return a 
 10347  10474   ** changeset containing zero changes.
 10348  10475   */
 10349  10476   SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
 10350  10477   
 10351  10478   /*
 10352  10479   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
        10480  +** CONSTRUCTOR: sqlite3_changeset_iter
 10353  10481   **
 10354  10482   ** Create an iterator used to iterate through the contents of a changeset.
 10355  10483   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
 10356  10484   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
 10357  10485   ** SQLite error code is returned.
 10358  10486   **
 10359  10487   ** The following functions can be used to advance and query a changeset 
................................................................................
 10386  10514     int nChangeset,                 /* Size of changeset blob in bytes */
 10387  10515     void *pChangeset                /* Pointer to blob containing changeset */
 10388  10516   );
 10389  10517   
 10390  10518   
 10391  10519   /*
 10392  10520   ** CAPI3REF: Advance A Changeset Iterator
        10521  +** METHOD: sqlite3_changeset_iter
 10393  10522   **
 10394  10523   ** This function may only be used with iterators created by function
 10395  10524   ** [sqlite3changeset_start()]. If it is called on an iterator passed to
 10396  10525   ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
 10397  10526   ** is returned and the call has no effect.
 10398  10527   **
 10399  10528   ** Immediately after an iterator is created by sqlite3changeset_start(), it
................................................................................
 10410  10539   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
 10411  10540   ** SQLITE_NOMEM.
 10412  10541   */
 10413  10542   SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
 10414  10543   
 10415  10544   /*
 10416  10545   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
        10546  +** METHOD: sqlite3_changeset_iter
 10417  10547   **
 10418  10548   ** The pIter argument passed to this function may either be an iterator
 10419  10549   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10420  10550   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10421  10551   ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
 10422  10552   ** is not the case, this function returns [SQLITE_MISUSE].
 10423  10553   **
................................................................................
 10444  10574     int *pnCol,                     /* OUT: Number of columns in table */
 10445  10575     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
 10446  10576     int *pbIndirect                 /* OUT: True for an 'indirect' change */
 10447  10577   );
 10448  10578   
 10449  10579   /*
 10450  10580   ** CAPI3REF: Obtain The Primary Key Definition Of A Table
        10581  +** METHOD: sqlite3_changeset_iter
 10451  10582   **
 10452  10583   ** For each modified table, a changeset includes the following:
 10453  10584   **
 10454  10585   ** <ul>
 10455  10586   **   <li> The number of columns in the table, and
 10456  10587   **   <li> Which of those columns make up the tables PRIMARY KEY.
 10457  10588   ** </ul>
................................................................................
 10475  10606     sqlite3_changeset_iter *pIter,  /* Iterator object */
 10476  10607     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
 10477  10608     int *pnCol                      /* OUT: Number of entries in output array */
 10478  10609   );
 10479  10610   
 10480  10611   /*
 10481  10612   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
        10613  +** METHOD: sqlite3_changeset_iter
 10482  10614   **
 10483  10615   ** The pIter argument passed to this function may either be an iterator
 10484  10616   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10485  10617   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10486  10618   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
 10487  10619   ** Furthermore, it may only be called if the type of change that the iterator
 10488  10620   ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
................................................................................
 10505  10637     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10506  10638     int iVal,                       /* Column number */
 10507  10639     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
 10508  10640   );
 10509  10641   
 10510  10642   /*
 10511  10643   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
        10644  +** METHOD: sqlite3_changeset_iter
 10512  10645   **
 10513  10646   ** The pIter argument passed to this function may either be an iterator
 10514  10647   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10515  10648   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10516  10649   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
 10517  10650   ** Furthermore, it may only be called if the type of change that the iterator
 10518  10651   ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
................................................................................
 10538  10671     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10539  10672     int iVal,                       /* Column number */
 10540  10673     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
 10541  10674   );
 10542  10675   
 10543  10676   /*
 10544  10677   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
        10678  +** METHOD: sqlite3_changeset_iter
 10545  10679   **
 10546  10680   ** This function should only be used with iterator objects passed to a
 10547  10681   ** conflict-handler callback by [sqlite3changeset_apply()] with either
 10548  10682   ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
 10549  10683   ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
 10550  10684   ** is set to NULL.
 10551  10685   **
................................................................................
 10565  10699     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10566  10700     int iVal,                       /* Column number */
 10567  10701     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
 10568  10702   );
 10569  10703   
 10570  10704   /*
 10571  10705   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
        10706  +** METHOD: sqlite3_changeset_iter
 10572  10707   **
 10573  10708   ** This function may only be called with an iterator passed to an
 10574  10709   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
 10575  10710   ** it sets the output variable to the total number of known foreign key
 10576  10711   ** violations in the destination database and returns SQLITE_OK.
 10577  10712   **
 10578  10713   ** In all other cases this function returns SQLITE_MISUSE.
................................................................................
 10581  10716     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10582  10717     int *pnOut                      /* OUT: Number of FK violations */
 10583  10718   );
 10584  10719   
 10585  10720   
 10586  10721   /*
 10587  10722   ** CAPI3REF: Finalize A Changeset Iterator
        10723  +** METHOD: sqlite3_changeset_iter
 10588  10724   **
 10589  10725   ** This function is used to finalize an iterator allocated with
 10590  10726   ** [sqlite3changeset_start()].
 10591  10727   **
 10592  10728   ** This function should only be called on iterators created using the
 10593  10729   ** [sqlite3changeset_start()] function. If an application calls this
 10594  10730   ** function with an iterator passed to a conflict-handler by
................................................................................
 10597  10733   **
 10598  10734   ** If an error was encountered within a call to an sqlite3changeset_xxx()
 10599  10735   ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
 10600  10736   ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
 10601  10737   ** to that error is returned by this function. Otherwise, SQLITE_OK is
 10602  10738   ** returned. This is to allow the following pattern (pseudo-code):
 10603  10739   **
        10740  +** <pre>
 10604  10741   **   sqlite3changeset_start();
 10605  10742   **   while( SQLITE_ROW==sqlite3changeset_next() ){
 10606  10743   **     // Do something with change.
 10607  10744   **   }
 10608  10745   **   rc = sqlite3changeset_finalize();
 10609  10746   **   if( rc!=SQLITE_OK ){
 10610  10747   **     // An error has occurred 
 10611  10748   **   }
        10749  +** </pre>
 10612  10750   */
 10613  10751   SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
 10614  10752   
 10615  10753   /*
 10616  10754   ** CAPI3REF: Invert A Changeset
 10617  10755   **
 10618  10756   ** This function is used to "invert" a changeset object. Applying an inverted
................................................................................
 10652  10790   ** single changeset. The result is a changeset equivalent to applying
 10653  10791   ** changeset A followed by changeset B. 
 10654  10792   **
 10655  10793   ** This function combines the two input changesets using an 
 10656  10794   ** sqlite3_changegroup object. Calling it produces similar results as the
 10657  10795   ** following code fragment:
 10658  10796   **
        10797  +** <pre>
 10659  10798   **   sqlite3_changegroup *pGrp;
 10660  10799   **   rc = sqlite3_changegroup_new(&pGrp);
 10661  10800   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
 10662  10801   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
 10663  10802   **   if( rc==SQLITE_OK ){
 10664  10803   **     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
 10665  10804   **   }else{
 10666  10805   **     *ppOut = 0;
 10667  10806   **     *pnOut = 0;
 10668  10807   **   }
        10808  +** </pre>
 10669  10809   **
 10670  10810   ** Refer to the sqlite3_changegroup documentation below for details.
 10671  10811   */
 10672  10812   SQLITE_API int sqlite3changeset_concat(
 10673  10813     int nA,                         /* Number of bytes in buffer pA */
 10674  10814     void *pA,                       /* Pointer to buffer containing changeset A */
 10675  10815     int nB,                         /* Number of bytes in buffer pB */
................................................................................
 10677  10817     int *pnOut,                     /* OUT: Number of bytes in output changeset */
 10678  10818     void **ppOut                    /* OUT: Buffer containing output changeset */
 10679  10819   );
 10680  10820   
 10681  10821   
 10682  10822   /*
 10683  10823   ** CAPI3REF: Changegroup Handle
        10824  +**
        10825  +** A changegroup is an object used to combine two or more 
        10826  +** [changesets] or [patchsets]
 10684  10827   */
 10685  10828   typedef struct sqlite3_changegroup sqlite3_changegroup;
 10686  10829   
 10687  10830   /*
 10688  10831   ** CAPI3REF: Create A New Changegroup Object
        10832  +** CONSTRUCTOR: sqlite3_changegroup
 10689  10833   **
 10690  10834   ** An sqlite3_changegroup object is used to combine two or more changesets
 10691  10835   ** (or patchsets) into a single changeset (or patchset). A single changegroup
 10692  10836   ** object may combine changesets or patchsets, but not both. The output is
 10693  10837   ** always in the same format as the input.
 10694  10838   **
 10695  10839   ** If successful, this function returns SQLITE_OK and populates (*pp) with
................................................................................
 10719  10863   ** sqlite3changegroup_output() functions, also available are the streaming
 10720  10864   ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
 10721  10865   */
 10722  10866   SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
 10723  10867   
 10724  10868   /*
 10725  10869   ** CAPI3REF: Add A Changeset To A Changegroup
        10870  +** METHOD: sqlite3_changegroup
 10726  10871   **
 10727  10872   ** Add all changes within the changeset (or patchset) in buffer pData (size
 10728  10873   ** nData bytes) to the changegroup. 
 10729  10874   **
 10730  10875   ** If the buffer contains a patchset, then all prior calls to this function
 10731  10876   ** on the same changegroup object must also have specified patchsets. Or, if
 10732  10877   ** the buffer contains a changeset, so must have the earlier calls to this
................................................................................
 10796  10941   **
 10797  10942   ** If no error occurs, SQLITE_OK is returned.
 10798  10943   */
 10799  10944   SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
 10800  10945   
 10801  10946   /*
 10802  10947   ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
        10948  +** METHOD: sqlite3_changegroup
 10803  10949   **
 10804  10950   ** Obtain a buffer containing a changeset (or patchset) representing the
 10805  10951   ** current contents of the changegroup. If the inputs to the changegroup
 10806  10952   ** were themselves changesets, the output is a changeset. Or, if the
 10807  10953   ** inputs were patchsets, the output is also a patchset.
 10808  10954   **
 10809  10955   ** As with the output of the sqlite3session_changeset() and
................................................................................
 10826  10972     sqlite3_changegroup*,
 10827  10973     int *pnData,                    /* OUT: Size of output buffer in bytes */
 10828  10974     void **ppData                   /* OUT: Pointer to output buffer */
 10829  10975   );
 10830  10976   
 10831  10977   /*
 10832  10978   ** CAPI3REF: Delete A Changegroup Object
        10979  +** DESTRUCTOR: sqlite3_changegroup
 10833  10980   */
 10834  10981   SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
 10835  10982   
 10836  10983   /*
 10837  10984   ** CAPI3REF: Apply A Changeset To A Database
 10838  10985   **
 10839         -** Apply a changeset to a database. This function attempts to update the
 10840         -** "main" database attached to handle db with the changes found in the
 10841         -** 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. 
 10842  10989   **
 10843         -** The fourth argument (xFilter) passed to this function is the "filter
        10990  +** The fourth argument (xFilter) passed to these functions is the "filter
 10844  10991   ** callback". If it is not NULL, then for each table affected by at least one
 10845  10992   ** change in the changeset, the filter callback is invoked with
 10846  10993   ** the table name as the second argument, and a copy of the context pointer
 10847         -** passed as the sixth argument to this function as the first. If the "filter
 10848         -** callback" returns zero, then no attempt is made to apply any changes to 
 10849         -** the table. Otherwise, if the return value is non-zero or the xFilter
 10850         -** argument to this function is NULL, all changes related to the table are
 10851         -** 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.
 10852  10998   **
 10853  10999   ** For each table that is not excluded by the filter callback, this function 
 10854  11000   ** tests that the target database contains a compatible table. A table is 
 10855  11001   ** considered compatible if all of the following are true:
 10856  11002   **
 10857  11003   ** <ul>
 10858  11004   **   <li> The table has the same name as the name recorded in the 
................................................................................
 10889  11035   ** actions are taken by sqlite3changeset_apply() depending on the value
 10890  11036   ** returned by each invocation of the conflict-handler function. Refer to
 10891  11037   ** the documentation for the three 
 10892  11038   ** [SQLITE_CHANGESET_OMIT|available return values] for details.
 10893  11039   **
 10894  11040   ** <dl>
 10895  11041   ** <dt>DELETE Changes<dd>
 10896         -**   For each DELETE change, this function checks if the target database 
        11042  +**   For each DELETE change, the function checks if the target database 
 10897  11043   **   contains a row with the same primary key value (or values) as the 
 10898  11044   **   original row values stored in the changeset. If it does, and the values 
 10899  11045   **   stored in all non-primary key columns also match the values stored in 
 10900  11046   **   the changeset the row is deleted from the target database.
 10901  11047   **
 10902  11048   **   If a row with matching primary key values is found, but one or more of
 10903  11049   **   the non-primary key fields contains a value different from the original
................................................................................
 10934  11080   **   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 
 10935  11081   **   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
 10936  11082   **   This includes the case where the INSERT operation is re-attempted because 
 10937  11083   **   an earlier call to the conflict handler function returned 
 10938  11084   **   [SQLITE_CHANGESET_REPLACE].
 10939  11085   **
 10940  11086   ** <dt>UPDATE Changes<dd>
 10941         -**   For each UPDATE change, this function checks if the target database 
        11087  +**   For each UPDATE change, the function checks if the target database 
 10942  11088   **   contains a row with the same primary key value (or values) as the 
 10943  11089   **   original row values stored in the changeset. If it does, and the values 
 10944  11090   **   stored in all modified non-primary key columns also match the values
 10945  11091   **   stored in the changeset the row is updated within the target database.
 10946  11092   **
 10947  11093   **   If a row with matching primary key values is found, but one or more of
 10948  11094   **   the modified non-primary key fields contains a value different from an
................................................................................
 10965  11111   ** </dl>
 10966  11112   **
 10967  11113   ** It is safe to execute SQL statements, including those that write to the
 10968  11114   ** table that the callback related to, from within the xConflict callback.
 10969  11115   ** This can be used to further customize the applications conflict
 10970  11116   ** resolution strategy.
 10971  11117   **
 10972         -** 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.
 10973  11119   ** If any other error (aside from a constraint failure when attempting to
 10974  11120   ** write to the target database) occurs, then the savepoint transaction is
 10975  11121   ** rolled back, restoring the target database to its original state, and an 
 10976  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.
 10977  11133   */
 10978  11134   SQLITE_API int sqlite3changeset_apply(
 10979  11135     sqlite3 *db,                    /* Apply change to "main" db of this handle */
 10980  11136     int nChangeset,                 /* Size of changeset in bytes */
 10981  11137     void *pChangeset,               /* Changeset blob */
 10982  11138     int(*xFilter)(
 10983  11139       void *pCtx,                   /* Copy of sixth arg to _apply() */
................................................................................
 10985  11141     ),
 10986  11142     int(*xConflict)(
 10987  11143       void *pCtx,                   /* Copy of sixth arg to _apply() */
 10988  11144       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 10989  11145       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 10990  11146     ),
 10991  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
 10992  11164   );
 10993  11165   
 10994  11166   /* 
 10995  11167   ** CAPI3REF: Constants Passed To The Conflict Handler
 10996  11168   **
 10997  11169   ** Values that may be passed as the second argument to a conflict-handler.
 10998  11170   **
................................................................................
 11083  11255   **   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
 11084  11256   ** </dl>
 11085  11257   */
 11086  11258   #define SQLITE_CHANGESET_OMIT       0
 11087  11259   #define SQLITE_CHANGESET_REPLACE    1
 11088  11260   #define SQLITE_CHANGESET_ABORT      2
 11089  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  +
 11090  11417   /*
 11091  11418   ** CAPI3REF: Streaming Versions of API functions.
 11092  11419   **
 11093  11420   ** The six streaming API xxx_strm() functions serve similar purposes to the 
 11094  11421   ** corresponding non-streaming API functions:
 11095  11422   **
 11096  11423   ** <table border=1 style="margin-left:8ex;margin-right:8ex">
................................................................................
 11186  11513     ),
 11187  11514     int(*xConflict)(
 11188  11515       void *pCtx,                   /* Copy of sixth arg to _apply() */
 11189  11516       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11190  11517       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11191  11518     ),
 11192  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
 11193  11536   );
 11194  11537   SQLITE_API int sqlite3changeset_concat_strm(
 11195  11538     int (*xInputA)(void *pIn, void *pData, int *pnData),
 11196  11539     void *pInA,
 11197  11540     int (*xInputB)(void *pIn, void *pData, int *pnData),
 11198  11541     void *pInB,
 11199  11542     int (*xOutput)(void *pOut, const void *pData, int nData),
................................................................................
 11223  11566   SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
 11224  11567       int (*xInput)(void *pIn, void *pData, int *pnData),
 11225  11568       void *pIn
 11226  11569   );
 11227  11570   SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
 11228  11571       int (*xOutput)(void *pOut, const void *pData, int nData), 
 11229  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
 11230  11580   );
 11231  11581   
 11232  11582   
 11233  11583   /*
 11234  11584   ** Make sure we can call this stuff from C++.
 11235  11585   */
 11236  11586   #if 0
................................................................................
 12641  12991   #define TK_CASE                           136
 12642  12992   #define TK_WHEN                           137
 12643  12993   #define TK_THEN                           138
 12644  12994   #define TK_ELSE                           139
 12645  12995   #define TK_INDEX                          140
 12646  12996   #define TK_ALTER                          141
 12647  12997   #define TK_ADD                            142
 12648         -#define TK_ISNOT                          143
 12649         -#define TK_FUNCTION                       144
 12650         -#define TK_COLUMN                         145
 12651         -#define TK_AGG_FUNCTION                   146
 12652         -#define TK_AGG_COLUMN                     147
 12653         -#define TK_UMINUS                         148
 12654         -#define TK_UPLUS                          149
 12655         -#define TK_REGISTER                       150
 12656         -#define TK_VECTOR                         151
 12657         -#define TK_SELECT_COLUMN                  152
 12658         -#define TK_IF_NULL_ROW                    153
 12659         -#define TK_ASTERISK                       154
 12660         -#define TK_SPAN                           155
 12661         -#define TK_END_OF_FILE                    156
 12662         -#define TK_UNCLOSED_STRING                157
 12663         -#define TK_SPACE                          158
 12664         -#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
 12665  13017   
 12666  13018   /* The token codes above must all fit in 8 bits */
 12667  13019   #define TKFLG_MASK           0xff  
 12668  13020   
 12669  13021   /* Flags that can be added to a token code when it is not
 12670  13022   ** being stored in a u8: */
 12671  13023   #define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
................................................................................
 13101  13453   ** The sqlite.busyHandler member of the sqlite struct contains the busy
 13102  13454   ** callback for the database handle. Each pager opened via the sqlite
 13103  13455   ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
 13104  13456   ** callback is currently invoked only from within pager.c.
 13105  13457   */
 13106  13458   typedef struct BusyHandler BusyHandler;
 13107  13459   struct BusyHandler {
 13108         -  int (*xFunc)(void *,int);  /* The busy callback */
 13109         -  void *pArg;                /* First arg to busy callback */
 13110         -  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 */
 13111  13464   };
 13112  13465   
 13113  13466   /*
 13114  13467   ** Name of the master database table.  The master database table
 13115  13468   ** is a special table that holds the names and attributes of all
 13116  13469   ** user tables and indices.
 13117  13470   */
................................................................................
 13903  14256   #define OP_Subtract       89 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 13904  14257   #define OP_Multiply       90 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 13905  14258   #define OP_Divide         91 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 13906  14259   #define OP_Remainder      92 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 13907  14260   #define OP_Concat         93 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 13908  14261   #define OP_Compare        94 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 13909  14262   #define OP_BitNot         95 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
 13910         -#define OP_Offset         96 /* synopsis: r[P3] = sqlite_offset(P1)        */
        14263  +#define OP_IsTrue         96 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
 13911  14264   #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 13912         -#define OP_Column         98 /* synopsis: r[P3]=PX                         */
 13913         -#define OP_Affinity       99 /* synopsis: affinity(r[P1@P2])               */
 13914         -#define OP_MakeRecord    100 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 13915         -#define OP_Count         101 /* synopsis: r[P2]=count()                    */
 13916         -#define OP_ReadCookie    102
 13917         -#define OP_SetCookie     103
 13918         -#define OP_ReopenIdx     104 /* synopsis: root=P2 iDb=P3                   */
 13919         -#define OP_OpenRead      105 /* synopsis: root=P2 iDb=P3                   */
 13920         -#define OP_OpenWrite     106 /* synopsis: root=P2 iDb=P3                   */
 13921         -#define OP_OpenDup       107
 13922         -#define OP_OpenAutoindex 108 /* synopsis: nColumn=P2                       */
 13923         -#define OP_OpenEphemeral 109 /* synopsis: nColumn=P2                       */
 13924         -#define OP_SorterOpen    110
 13925         -#define OP_SequenceTest  111 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 13926         -#define OP_OpenPseudo    112 /* synopsis: P3 columns in r[P2]              */
 13927         -#define OP_Close         113
 13928         -#define OP_ColumnsUsed   114
 13929         -#define OP_Sequence      115 /* synopsis: r[P2]=cursor[P1].ctr++           */
 13930         -#define OP_NewRowid      116 /* synopsis: r[P2]=rowid                      */
 13931         -#define OP_Insert        117 /* synopsis: intkey=r[P3] data=r[P2]          */
 13932         -#define OP_InsertInt     118 /* synopsis: intkey=P3 data=r[P2]             */
 13933         -#define OP_Delete        119
 13934         -#define OP_ResetCount    120
 13935         -#define OP_SorterCompare 121 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 13936         -#define OP_SorterData    122 /* synopsis: r[P2]=data                       */
 13937         -#define OP_RowData       123 /* synopsis: r[P2]=data                       */
 13938         -#define OP_Rowid         124 /* synopsis: r[P2]=rowid                      */
 13939         -#define OP_NullRow       125
 13940         -#define OP_SeekEnd       126
 13941         -#define OP_SorterInsert  127 /* synopsis: key=r[P2]                        */
 13942         -#define OP_IdxInsert     128 /* synopsis: key=r[P2]                        */
 13943         -#define OP_IdxDelete     129 /* synopsis: key=r[P2@P3]                     */
 13944         -#define OP_DeferredSeek  130 /* synopsis: Move P3 to P1.rowid if needed    */
 13945         -#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    */
 13946  14299   #define OP_Real          132 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 13947         -#define OP_Destroy       133
 13948         -#define OP_Clear         134
 13949         -#define OP_ResetSorter   135
 13950         -#define OP_CreateBtree   136 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 13951         -#define OP_SqlExec       137
 13952         -#define OP_ParseSchema   138
 13953         -#define OP_LoadAnalysis  139
 13954         -#define OP_DropTable     140
 13955         -#define OP_DropIndex     141
 13956         -#define OP_DropTrigger   142
 13957         -#define OP_IntegrityCk   143
 13958         -#define OP_RowSetAdd     144 /* synopsis: rowset(P1)=r[P2]                 */
 13959         -#define OP_Param         145
 13960         -#define OP_FkCounter     146 /* synopsis: fkctr[P1]+=P2                    */
 13961         -#define OP_MemMax        147 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 13962         -#define OP_OffsetLimit   148 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 13963         -#define OP_AggStep0      149 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 13964         -#define OP_AggStep       150 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 13965         -#define OP_AggFinal      151 /* synopsis: accum=r[P1] N=P2                 */
 13966         -#define OP_Expire        152
 13967         -#define OP_TableLock     153 /* synopsis: iDb=P1 root=P2 write=P3          */
 13968         -#define OP_VBegin        154
 13969         -#define OP_VCreate       155
 13970         -#define OP_VDestroy      156
 13971         -#define OP_VOpen         157
 13972         -#define OP_VColumn       158 /* synopsis: r[P3]=vcolumn(P2)                */
 13973         -#define OP_VRename       159
 13974         -#define OP_Pagecount     160
 13975         -#define OP_MaxPgcnt      161
 13976         -#define OP_PureFunc0     162
 13977         -#define OP_Function0     163 /* synopsis: r[P3]=func(r[P2@P5])             */
 13978         -#define OP_PureFunc      164
 13979         -#define OP_Function      165 /* synopsis: r[P3]=func(r[P2@P5])             */
 13980         -#define OP_Trace         166
 13981         -#define OP_CursorHint    167
 13982         -#define OP_Noop          168
 13983         -#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
 13984  14338   
 13985  14339   /* Properties such as "out2" or "jump" that are specified in
 13986  14340   ** comments following the "case" for each opcode in the vdbe.c
 13987  14341   ** are encoded into bitvectors as follows:
 13988  14342   */
 13989  14343   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 13990  14344   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 14001  14355   /*  40 */ 0x01, 0x01, 0x23, 0x26, 0x26, 0x0b, 0x01, 0x01,\
 14002  14356   /*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 14003  14357   /*  56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x02,\
 14004  14358   /*  64 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
 14005  14359   /*  72 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 14006  14360   /*  80 */ 0x02, 0x02, 0x02, 0x00, 0x26, 0x26, 0x26, 0x26,\
 14007  14361   /*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 14008         -/*  96 */ 0x20, 0x10, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
        14362  +/*  96 */ 0x12, 0x10, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10,\
 14009  14363   /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14010         -/* 112 */ 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\
 14011         -/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x04,\
 14012         -/* 128 */ 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\
 14013         -/* 136 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14014         -/* 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,\
 14015  14369   /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14016         -/* 160 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14017         -/* 168 */ 0x00, 0x00,}
        14370  +/* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
        14371  +/* 168 */ 0x00, 0x00, 0x00,}
 14018  14372   
 14019  14373   /* The sqlite3P2Values() routine is able to run faster if it knows
 14020  14374   ** the value of the largest JUMP opcode.  The smaller the maximum
 14021  14375   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 14022  14376   ** generated this include file strives to group all JUMP opcodes
 14023  14377   ** together near the beginning of the list.
 14024  14378   */
................................................................................
 14311  14665     int,
 14312  14666     void(*)(DbPage*)
 14313  14667   );
 14314  14668   SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
 14315  14669   SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
 14316  14670   
 14317  14671   /* Functions used to configure a Pager object. */
 14318         -SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
        14672  +SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
 14319  14673   SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
 14320  14674   #ifdef SQLITE_HAS_CODEC
 14321  14675   SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*);
 14322  14676   #endif
 14323  14677   SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
 14324  14678   SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
 14325  14679   SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
................................................................................
 14397  14751   SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
 14398  14752   SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
 14399  14753   SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
 14400  14754   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
 14401  14755   SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
 14402  14756   SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
 14403  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
 14404  14763   
 14405  14764   /* Functions used to truncate the database file. */
 14406  14765   SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
 14407  14766   
 14408  14767   SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
 14409  14768   
 14410  14769   #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
................................................................................
 15216  15575     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
 15217  15576     int aLimit[SQLITE_N_LIMIT];   /* Limits */
 15218  15577     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
 15219  15578     struct sqlite3InitInfo {      /* Information used during initialization */
 15220  15579       int newTnum;                /* Rootpage of table being initialized */
 15221  15580       u8 iDb;                     /* Which db file is being initialized */
 15222  15581       u8 busy;                    /* TRUE if currently initializing */
 15223         -    u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
 15224         -    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 */
 15225  15585     } init;
 15226  15586     int nVdbeActive;              /* Number of VDBEs currently running */
 15227  15587     int nVdbeRead;                /* Number of active VDBEs that read or write */
 15228  15588     int nVdbeWrite;               /* Number of active VDBEs that read and write */
 15229  15589     int nVdbeExec;                /* Number of nested calls to VdbeExec() */
 15230  15590     int nVDestroy;                /* Number of active OP_VDestroy operations */
 15231  15591     int nExtension;               /* Number of loaded extensions */
................................................................................
 15382  15742   #define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
 15383  15743   #define SQLITE_Transitive     0x0080   /* Transitive constraints */
 15384  15744   #define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
 15385  15745   #define SQLITE_CountOfView    0x0200   /* The count-of-view optimization */
 15386  15746   #define SQLITE_CursorHints    0x0400   /* Add OP_CursorHint opcodes */
 15387  15747   #define SQLITE_Stat34         0x0800   /* Use STAT3 or STAT4 data */
 15388  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 */
 15389  15751   #define SQLITE_AllOpts        0xffff   /* All optimizations */
 15390  15752   
 15391  15753   /*
 15392  15754   ** Macros for testing whether or not optimizations are enabled or disabled.
 15393  15755   */
 15394  15756   #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
 15395  15757   #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
................................................................................
 15605  15967   };
 15606  15968   
 15607  15969   /* Allowed values for Column.colFlags:
 15608  15970   */
 15609  15971   #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
 15610  15972   #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
 15611  15973   #define COLFLAG_HASTYPE  0x0004    /* Type name follows column name */
        15974  +#define COLFLAG_UNIQUE   0x0008    /* Column def contains "UNIQUE" or "PK" */
 15612  15975   
 15613  15976   /*
 15614  15977   ** A "Collating Sequence" is defined by an instance of the following
 15615  15978   ** structure. Conceptually, a collating sequence consists of a name and
 15616  15979   ** a comparison routine that defines the order of that sequence.
 15617  15980   **
 15618  15981   ** If CollSeq.xCmp is NULL, it means that the
................................................................................
 16842  17205     yDbMask writeMask;   /* Start a write transaction on these databases */
 16843  17206     yDbMask cookieMask;  /* Bitmask of schema verified databases */
 16844  17207     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
 16845  17208     int regRoot;         /* Register holding root page number for new objects */
 16846  17209     int nMaxArg;         /* Max args passed to user function by sub-program */
 16847  17210   #if SELECTTRACE_ENABLED
 16848  17211     int nSelect;         /* Number of SELECT statements seen */
 16849         -  int nSelectIndent;   /* How far to indent SELECTTRACE() output */
 16850  17212   #endif
 16851  17213   #ifndef SQLITE_OMIT_SHARED_CACHE
 16852  17214     int nTableLock;        /* Number of locks in aTableLock */
 16853  17215     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 16854  17216   #endif
 16855  17217     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 16856  17218     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
................................................................................
 17206  17568       int n;                                    /* A counter */
 17207  17569       int iCur;                                 /* A cursor number */
 17208  17570       SrcList *pSrcList;                        /* FROM clause */
 17209  17571       struct SrcCount *pSrcCount;               /* Counting column references */
 17210  17572       struct CCurHint *pCCurHint;               /* Used by codeCursorHint() */
 17211  17573       int *aiCol;                               /* array of column indexes */
 17212  17574       struct IdxCover *pIdxCover;               /* Check for index coverage */
 17213         -    struct IdxExprTrans *pIdxTrans;           /* Convert indexed expr to column */
        17575  +    struct IdxExprTrans *pIdxTrans;           /* Convert idxed expr to column */
 17214  17576       ExprList *pGroupBy;                       /* GROUP BY clause */
 17215         -    struct HavingToWhereCtx *pHavingCtx;      /* HAVING to WHERE clause ctx */
        17577  +    Select *pSelect;                          /* HAVING to WHERE clause ctx */
 17216  17578     } u;
 17217  17579   };
 17218  17580   
 17219  17581   /* Forward declarations */
 17220  17582   SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
 17221  17583   SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
 17222  17584   SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
................................................................................
 17672  18034   SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*);
 17673  18035   SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int);
 17674  18036   SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
 17675  18037   SQLITE_PRIVATE int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
 17676  18038   SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*, Expr*, int);
 17677  18039   SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
 17678  18040   SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
        18041  +SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int);
 17679  18042   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 17680  18043   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 17681  18044   SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
 17682  18045   SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
 17683  18046   SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
 17684  18047   #ifndef SQLITE_UNTESTABLE
 17685  18048   SQLITE_PRIVATE void sqlite3PrngSaveState(void);
................................................................................
 17689  18052   SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
 17690  18053   SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
 17691  18054   SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
 17692  18055   SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
 17693  18056   SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
 17694  18057   SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
 17695  18058   SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
        18059  +SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
        18060  +SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
 17696  18061   SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
 17697  18062   SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
 17698  18063   SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
 17699  18064   SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
 17700  18065   SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
 17701  18066   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 17702  18067   SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
................................................................................
 17870  18235   SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
 17871  18236   SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
 17872  18237   SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
 17873  18238   
 17874  18239   #if defined(SQLITE_NEED_ERR_NAME)
 17875  18240   SQLITE_PRIVATE const char *sqlite3ErrName(int);
 17876  18241   #endif
        18242  +
        18243  +#ifdef SQLITE_ENABLE_DESERIALIZE
        18244  +SQLITE_PRIVATE int sqlite3MemdbInit(void);
        18245  +#endif
 17877  18246   
 17878  18247   SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 17879  18248   SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 17880  18249   SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
 17881  18250   SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
 17882  18251   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 17883  18252   SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
................................................................................
 17919  18288   SQLITE_PRIVATE const Token sqlite3IntTokens[];
 17920  18289   SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
 17921  18290   SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
 17922  18291   #ifndef SQLITE_OMIT_WSD
 17923  18292   SQLITE_PRIVATE int sqlite3PendingByte;
 17924  18293   #endif
 17925  18294   #endif
        18295  +#ifdef VDBE_PROFILE
        18296  +SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
        18297  +#endif
 17926  18298   SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 17927  18299   SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 17928  18300   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 17929  18301   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 17930  18302   SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 17931  18303   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 17932  18304   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
................................................................................
 17941  18313   SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
 17942  18314   SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
 17943  18315   SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
 17944  18316   SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
 17945  18317   SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
 17946  18318   SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
 17947  18319   SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
 17948         -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
        18320  +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
 17949  18321   SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
 17950  18322   SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
 17951  18323   SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
 17952  18324   SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
 17953  18325   SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
 17954  18326   SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
 17955  18327   SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
................................................................................
 18542  18914   ** Constant tokens for values 0 and 1.
 18543  18915   */
 18544  18916   SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
 18545  18917      { "0", 1 },
 18546  18918      { "1", 1 }
 18547  18919   };
 18548  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
 18549  18928   
 18550  18929   /*
 18551  18930   ** The value of the "pending" byte must be 0x40000000 (1 byte past the
 18552  18931   ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
 18553  18932   ** the database page that contains the pending byte.  It never attempts
 18554  18933   ** to read or write that page.  The pending byte page is set aside
 18555  18934   ** for use by the VFS layers as space for managing file locks.
................................................................................
 18825  19204   
 18826  19205   /* One or more of the following flags are set to indicate the validOK
 18827  19206   ** representations of the value stored in the Mem struct.
 18828  19207   **
 18829  19208   ** If the MEM_Null flag is set, then the value is an SQL NULL value.
 18830  19209   ** For a pointer type created using sqlite3_bind_pointer() or
 18831  19210   ** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
 18832         -** If both MEM_Null and MEM_Zero are set, that means that the value is
 18833         -** an unchanging column value from VColumn.
 18834  19211   **
 18835  19212   ** If the MEM_Str flag is set then Mem.z points at a string representation.
 18836  19213   ** Usually this is encoded in the same unicode encoding as the main
 18837  19214   ** database (see below for exceptions). If the MEM_Term flag is also
 18838  19215   ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
 18839  19216   ** flags may coexist with the MEM_Str flag.
 18840  19217   */
................................................................................
 18920  19297     Mem *pOut;              /* The return value is stored here */
 18921  19298     FuncDef *pFunc;         /* Pointer to function information */
 18922  19299     Mem *pMem;              /* Memory cell used to store aggregate context */
 18923  19300     Vdbe *pVdbe;            /* The VM that owns this context */
 18924  19301     int iOp;                /* Instruction number of OP_Function */
 18925  19302     int isError;            /* Error code returned by the function. */
 18926  19303     u8 skipFlag;            /* Skip accumulator loading if true */
 18927         -  u8 fErrorOrAux;         /* isError!=0 or pVdbe->pAuxData modified */
 18928         -  u8 bVtabNoChng;         /* Fetching an unchanging column in a vtab UPDATE */
 18929  19304     u8 argc;                /* Number of arguments */
 18930  19305     sqlite3_value *argv[1]; /* Argument set */
 18931  19306   };
 18932  19307   
 18933  19308   /* A bitfield type for use inside of structures.  Always follow with :N where
 18934  19309   ** N is the number of bits.
 18935  19310   */
................................................................................
 19091  19466   SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
 19092  19467   SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
 19093  19468   SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
 19094  19469   SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
 19095  19470   SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
 19096  19471   SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
 19097  19472   SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
        19473  +SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
 19098  19474   SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
 19099  19475   SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
 19100  19476   SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
 19101  19477   SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
 19102  19478   SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
 19103  19479   SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 19104  19480   SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
................................................................................
 19484  19860       }
 19485  19861   
 19486  19862       /*
 19487  19863       ** Set *pCurrent to the total cache hits or misses encountered by all
 19488  19864       ** pagers the database handle is connected to. *pHighwater is always set 
 19489  19865       ** to zero.
 19490  19866       */
        19867  +    case SQLITE_DBSTATUS_CACHE_SPILL:
        19868  +      op = SQLITE_DBSTATUS_CACHE_WRITE+1;
        19869  +      /* Fall through into the next case */
 19491  19870       case SQLITE_DBSTATUS_CACHE_HIT:
 19492  19871       case SQLITE_DBSTATUS_CACHE_MISS:
 19493  19872       case SQLITE_DBSTATUS_CACHE_WRITE:{
 19494  19873         int i;
 19495  19874         int nRet = 0;
 19496  19875         assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
 19497  19876         assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
................................................................................
 20905  21284   ** when simply tossing information over the wall to the VFS and we do not
 20906  21285   ** really care if the VFS receives and understands the information since it
 20907  21286   ** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
 20908  21287   ** routine has no return value since the return value would be meaningless.
 20909  21288   */
 20910  21289   SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
 20911  21290   #ifdef SQLITE_TEST
 20912         -  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
        21291  +  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
        21292  +   && op!=SQLITE_FCNTL_LOCK_TIMEOUT
        21293  +  ){
 20913  21294       /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
 20914  21295       ** is using a regular VFS, it is called after the corresponding
 20915  21296       ** transaction has been committed. Injecting a fault at this point
 20916  21297       ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
 20917  21298       ** but the transaction is committed anyway.
 20918  21299       **
 20919  21300       ** The core must call OsFileControl() though, not OsFileControlHint(),
................................................................................
 24077  24458   #if SQLITE_MUTEX_NREF
 24078  24459     volatile int nRef;         /* Number of entrances */
 24079  24460     volatile pthread_t owner;  /* Thread that is within this mutex */
 24080  24461     int trace;                 /* True to trace changes */
 24081  24462   #endif
 24082  24463   };
 24083  24464   #if SQLITE_MUTEX_NREF
 24084         -#define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0}
        24465  +# define SQLITE3_MUTEX_INITIALIZER(id) \
        24466  +     {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
 24085  24467   #elif defined(SQLITE_ENABLE_API_ARMOR)
 24086         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 }
        24468  +# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
 24087  24469   #else
 24088         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
        24470  +#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
 24089  24471   #endif
 24090  24472   
 24091  24473   /*
 24092  24474   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 24093  24475   ** intended for use only inside assert() statements.  On some platforms,
 24094  24476   ** there might be race conditions that can cause these routines to
 24095  24477   ** deliver incorrect results.  In particular, if pthread_equal() is
................................................................................
 24178  24560   ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
 24179  24561   ** returns a different mutex on every call.  But for the static 
 24180  24562   ** mutex types, the same mutex is returned on every call that has
 24181  24563   ** the same type number.
 24182  24564   */
 24183  24565   static sqlite3_mutex *pthreadMutexAlloc(int iType){
 24184  24566     static sqlite3_mutex staticMutexes[] = {
 24185         -    SQLITE3_MUTEX_INITIALIZER,
 24186         -    SQLITE3_MUTEX_INITIALIZER,
 24187         -    SQLITE3_MUTEX_INITIALIZER,
 24188         -    SQLITE3_MUTEX_INITIALIZER,
 24189         -    SQLITE3_MUTEX_INITIALIZER,
 24190         -    SQLITE3_MUTEX_INITIALIZER,
 24191         -    SQLITE3_MUTEX_INITIALIZER,
 24192         -    SQLITE3_MUTEX_INITIALIZER,
 24193         -    SQLITE3_MUTEX_INITIALIZER,
 24194         -    SQLITE3_MUTEX_INITIALIZER,
 24195         -    SQLITE3_MUTEX_INITIALIZER,
 24196         -    SQLITE3_MUTEX_INITIALIZER
        24567  +    SQLITE3_MUTEX_INITIALIZER(2),
        24568  +    SQLITE3_MUTEX_INITIALIZER(3),
        24569  +    SQLITE3_MUTEX_INITIALIZER(4),
        24570  +    SQLITE3_MUTEX_INITIALIZER(5),
        24571  +    SQLITE3_MUTEX_INITIALIZER(6),
        24572  +    SQLITE3_MUTEX_INITIALIZER(7),
        24573  +    SQLITE3_MUTEX_INITIALIZER(8),
        24574  +    SQLITE3_MUTEX_INITIALIZER(9),
        24575  +    SQLITE3_MUTEX_INITIALIZER(10),
        24576  +    SQLITE3_MUTEX_INITIALIZER(11),
        24577  +    SQLITE3_MUTEX_INITIALIZER(12),
        24578  +    SQLITE3_MUTEX_INITIALIZER(13)
 24197  24579     };
 24198  24580     sqlite3_mutex *p;
 24199  24581     switch( iType ){
 24200  24582       case SQLITE_MUTEX_RECURSIVE: {
 24201  24583         p = sqlite3MallocZero( sizeof(*p) );
 24202  24584         if( p ){
 24203  24585   #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
................................................................................
 24208  24590           /* Use a recursive mutex if it is available */
 24209  24591           pthread_mutexattr_t recursiveAttr;
 24210  24592           pthread_mutexattr_init(&recursiveAttr);
 24211  24593           pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
 24212  24594           pthread_mutex_init(&p->mutex, &recursiveAttr);
 24213  24595           pthread_mutexattr_destroy(&recursiveAttr);
 24214  24596   #endif
        24597  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
        24598  +        p->id = SQLITE_MUTEX_RECURSIVE;
        24599  +#endif
 24215  24600         }
 24216  24601         break;
 24217  24602       }
 24218  24603       case SQLITE_MUTEX_FAST: {
 24219  24604         p = sqlite3MallocZero( sizeof(*p) );
 24220  24605         if( p ){
 24221  24606           pthread_mutex_init(&p->mutex, 0);
        24607  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
        24608  +        p->id = SQLITE_MUTEX_FAST;
        24609  +#endif
 24222  24610         }
 24223  24611         break;
 24224  24612       }
 24225  24613       default: {
 24226  24614   #ifdef SQLITE_ENABLE_API_ARMOR
 24227  24615         if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
 24228  24616           (void)SQLITE_MISUSE_BKPT;
................................................................................
 24230  24618         }
 24231  24619   #endif
 24232  24620         p = &staticMutexes[iType-2];
 24233  24621         break;
 24234  24622       }
 24235  24623     }
 24236  24624   #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
 24237         -  if( p ) p->id = iType;
        24625  +  assert( p==0 || p->id==iType );
 24238  24626   #endif
 24239  24627     return p;
 24240  24628   }
 24241  24629   
 24242  24630   
 24243  24631   /*
 24244  24632   ** This routine deallocates a previously
................................................................................
 24747  25135   */
 24748  25136   struct sqlite3_mutex {
 24749  25137     CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
 24750  25138     int id;                    /* Mutex type */
 24751  25139   #ifdef SQLITE_DEBUG
 24752  25140     volatile int nRef;         /* Number of enterances */
 24753  25141     volatile DWORD owner;      /* Thread holding this mutex */
 24754         -  volatile int trace;        /* True to trace changes */
        25142  +  volatile LONG trace;       /* True to trace changes */
 24755  25143   #endif
 24756  25144   };
 24757  25145   
 24758  25146   /*
 24759  25147   ** These are the initializer values used when declaring a "static" mutex
 24760  25148   ** on Win32.  It should be noted that all mutexes require initialization
 24761  25149   ** on the Win32 platform.
 24762  25150   */
 24763  25151   #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
 24764  25152   
 24765  25153   #ifdef SQLITE_DEBUG
 24766         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
        25154  +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
 24767  25155                                       0L, (DWORD)0, 0 }
 24768  25156   #else
 24769         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
        25157  +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
 24770  25158   #endif
 24771  25159   
 24772  25160   #ifdef SQLITE_DEBUG
 24773  25161   /*
 24774  25162   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 24775  25163   ** intended for use only inside assert() statements.
 24776  25164   */
................................................................................
 24805  25193   #endif
 24806  25194   }
 24807  25195   
 24808  25196   /*
 24809  25197   ** Initialize and deinitialize the mutex subsystem.
 24810  25198   */
 24811  25199   static sqlite3_mutex winMutex_staticMutexes[] = {
 24812         -  SQLITE3_MUTEX_INITIALIZER,
 24813         -  SQLITE3_MUTEX_INITIALIZER,
 24814         -  SQLITE3_MUTEX_INITIALIZER,
 24815         -  SQLITE3_MUTEX_INITIALIZER,
 24816         -  SQLITE3_MUTEX_INITIALIZER,
 24817         -  SQLITE3_MUTEX_INITIALIZER,
 24818         -  SQLITE3_MUTEX_INITIALIZER,
 24819         -  SQLITE3_MUTEX_INITIALIZER,
 24820         -  SQLITE3_MUTEX_INITIALIZER,
 24821         -  SQLITE3_MUTEX_INITIALIZER,
 24822         -  SQLITE3_MUTEX_INITIALIZER,
 24823         -  SQLITE3_MUTEX_INITIALIZER
        25200  +  SQLITE3_MUTEX_INITIALIZER(2),
        25201  +  SQLITE3_MUTEX_INITIALIZER(3),
        25202  +  SQLITE3_MUTEX_INITIALIZER(4),
        25203  +  SQLITE3_MUTEX_INITIALIZER(5),
        25204  +  SQLITE3_MUTEX_INITIALIZER(6),
        25205  +  SQLITE3_MUTEX_INITIALIZER(7),
        25206  +  SQLITE3_MUTEX_INITIALIZER(8),
        25207  +  SQLITE3_MUTEX_INITIALIZER(9),
        25208  +  SQLITE3_MUTEX_INITIALIZER(10),
        25209  +  SQLITE3_MUTEX_INITIALIZER(11),
        25210  +  SQLITE3_MUTEX_INITIALIZER(12),
        25211  +  SQLITE3_MUTEX_INITIALIZER(13)
 24824  25212   };
 24825  25213   
 24826  25214   static int winMutex_isInit = 0;
 24827  25215   static int winMutex_isNt = -1; /* <0 means "need to query" */
 24828  25216   
 24829  25217   /* As the winMutexInit() and winMutexEnd() functions are called as part
 24830  25218   ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
................................................................................
 24946  25334   #ifdef SQLITE_ENABLE_API_ARMOR
 24947  25335         if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
 24948  25336           (void)SQLITE_MISUSE_BKPT;
 24949  25337           return 0;
 24950  25338         }
 24951  25339   #endif
 24952  25340         p = &winMutex_staticMutexes[iType-2];
 24953         -      p->id = iType;
 24954  25341   #ifdef SQLITE_DEBUG
 24955  25342   #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
 24956         -      p->trace = 1;
        25343  +      InterlockedCompareExchange(&p->trace, 1, 0);
 24957  25344   #endif
 24958  25345   #endif
 24959  25346         break;
 24960  25347       }
 24961  25348     }
        25349  +  assert( p==0 || p->id==iType );
 24962  25350     return p;
 24963  25351   }
 24964  25352   
 24965  25353   
 24966  25354   /*
 24967  25355   ** This routine deallocates a previously
 24968  25356   ** allocated mutex.  SQLite is careful to deallocate every
................................................................................
 26033  26421     double rounder;            /* Used for rounding floating point values */
 26034  26422     etByte flag_dp;            /* True if decimal point should be shown */
 26035  26423     etByte flag_rtz;           /* True if trailing zeros should be removed */
 26036  26424   #endif
 26037  26425     PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
 26038  26426     char buf[etBUFSIZE];       /* Conversion buffer */
 26039  26427   
        26428  +  /* pAccum never starts out with an empty buffer that was obtained from 
        26429  +  ** malloc().  This precondition is required by the mprintf("%z...")
        26430  +  ** optimization. */
        26431  +  assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
        26432  +
 26040  26433     bufpt = 0;
 26041  26434     if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
 26042  26435       pArgList = va_arg(ap, PrintfArguments*);
 26043  26436       bArgList = 1;
 26044  26437     }else{
 26045  26438       bArgList = 0;
 26046  26439     }
................................................................................
 26451  26844           buf[0] = '%';
 26452  26845           bufpt = buf;
 26453  26846           length = 1;
 26454  26847           break;
 26455  26848         case etCHARX:
 26456  26849           if( bArgList ){
 26457  26850             bufpt = getTextArg(pArgList);
 26458         -          c = bufpt ? bufpt[0] : 0;
        26851  +          length = 1;
        26852  +          if( bufpt ){
        26853  +            buf[0] = c = *(bufpt++);
        26854  +            if( (c&0xc0)==0xc0 ){
        26855  +              while( length<4 && (bufpt[0]&0xc0)==0x80 ){
        26856  +                buf[length++] = *(bufpt++);
        26857  +              }
        26858  +            }
        26859  +          }else{
        26860  +            buf[0] = 0;
        26861  +          }
 26459  26862           }else{
 26460         -          c = va_arg(ap,int);
        26863  +          unsigned int ch = va_arg(ap,unsigned int);
        26864  +          if( ch<0x00080 ){
        26865  +            buf[0] = ch & 0xff;
        26866  +            length = 1;
        26867  +          }else if( ch<0x00800 ){
        26868  +            buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
        26869  +            buf[1] = 0x80 + (u8)(ch & 0x3f);
        26870  +            length = 2;
        26871  +          }else if( ch<0x10000 ){
        26872  +            buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
        26873  +            buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
        26874  +            buf[2] = 0x80 + (u8)(ch & 0x3f);
        26875  +            length = 3;
        26876  +          }else{
        26877  +            buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
        26878  +            buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
        26879  +            buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
        26880  +            buf[3] = 0x80 + (u8)(ch & 0x3f);
        26881  +            length = 4;
        26882  +          }
 26461  26883           }
 26462  26884           if( precision>1 ){
 26463  26885             width -= precision-1;
 26464  26886             if( width>1 && !flag_leftjustify ){
 26465  26887               sqlite3AppendChar(pAccum, width-1, ' ');
 26466  26888               width = 0;
 26467  26889             }
 26468         -          sqlite3AppendChar(pAccum, precision-1, c);
        26890  +          while( precision-- > 1 ){
        26891  +            sqlite3StrAccumAppend(pAccum, buf, length);
        26892  +          }
 26469  26893           }
 26470         -        length = 1;
 26471         -        buf[0] = c;
 26472  26894           bufpt = buf;
 26473         -        break;
        26895  +        flag_altform2 = 1;
        26896  +        goto adjust_width_for_utf8;
 26474  26897         case etSTRING:
 26475  26898         case etDYNSTRING:
 26476  26899           if( bArgList ){
 26477  26900             bufpt = getTextArg(pArgList);
 26478  26901             xtype = etSTRING;
 26479  26902           }else{
 26480  26903             bufpt = va_arg(ap,char*);
 26481  26904           }
 26482  26905           if( bufpt==0 ){
 26483  26906             bufpt = "";
 26484  26907           }else if( xtype==etDYNSTRING ){
        26908  +          if( pAccum->nChar==0 && pAccum->mxAlloc && width==0 && precision<0 ){
        26909  +            /* Special optimization for sqlite3_mprintf("%z..."):
        26910  +            ** Extend an existing memory allocation rather than creating
        26911  +            ** a new one. */
        26912  +            assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
        26913  +            pAccum->zText = bufpt;
        26914  +            pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
        26915  +            pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
        26916  +            pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
        26917  +            length = 0;
        26918  +            break;
        26919  +          }
 26485  26920             zExtra = bufpt;
 26486  26921           }
 26487  26922           if( precision>=0 ){
 26488         -          for(length=0; length<precision && bufpt[length]; length++){}
        26923  +          if( flag_altform2 ){
        26924  +            /* Set length to the number of bytes needed in order to display
        26925  +            ** precision characters */
        26926  +            unsigned char *z = (unsigned char*)bufpt;
        26927  +            while( precision-- > 0 && z[0] ){
        26928  +              SQLITE_SKIP_UTF8(z);
        26929  +            }
        26930  +            length = (int)(z - (unsigned char*)bufpt);
        26931  +          }else{
        26932  +            for(length=0; length<precision && bufpt[length]; length++){}
        26933  +          }
 26489  26934           }else{
 26490  26935             length = 0x7fffffff & (int)strlen(bufpt);
 26491  26936           }
        26937  +      adjust_width_for_utf8:
        26938  +        if( flag_altform2 && width>0 ){
        26939  +          /* Adjust width to account for extra bytes in UTF-8 characters */
        26940  +          int ii = length - 1;
        26941  +          while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
        26942  +        }
 26492  26943           break;
 26493         -      case etSQLESCAPE:           /* Escape ' characters */
 26494         -      case etSQLESCAPE2:          /* Escape ' and enclose in '...' */
 26495         -      case etSQLESCAPE3: {        /* Escape " characters */
        26944  +      case etSQLESCAPE:           /* %q: Escape ' characters */
        26945  +      case etSQLESCAPE2:          /* %Q: Escape ' and enclose in '...' */
        26946  +      case etSQLESCAPE3: {        /* %w: Escape " characters */
 26496  26947           int i, j, k, n, isnull;
 26497  26948           int needQuote;
 26498  26949           char ch;
 26499  26950           char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
 26500  26951           char *escarg;
 26501  26952   
 26502  26953           if( bArgList ){
 26503  26954             escarg = getTextArg(pArgList);
 26504  26955           }else{
 26505  26956             escarg = va_arg(ap,char*);
 26506  26957           }
 26507  26958           isnull = escarg==0;
 26508  26959           if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
        26960  +        /* For %q, %Q, and %w, the precision is the number of byte (or
        26961  +        ** characters if the ! flags is present) to use from the input.
        26962  +        ** Because of the extra quoting characters inserted, the number
        26963  +        ** of output characters may be larger than the precision.
        26964  +        */
 26509  26965           k = precision;
 26510  26966           for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
 26511  26967             if( ch==q )  n++;
        26968  +          if( flag_altform2 && (ch&0xc0)==0xc0 ){
        26969  +            while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
        26970  +          }
 26512  26971           }
 26513  26972           needQuote = !isnull && xtype==etSQLESCAPE2;
 26514  26973           n += i + 3;
 26515  26974           if( n>etBUFSIZE ){
 26516  26975             bufpt = zExtra = sqlite3Malloc( n );
 26517  26976             if( bufpt==0 ){
 26518  26977               setStrAccumError(pAccum, STRACCUM_NOMEM);
................................................................................
 26527  26986           for(i=0; i<k; i++){
 26528  26987             bufpt[j++] = ch = escarg[i];
 26529  26988             if( ch==q ) bufpt[j++] = ch;
 26530  26989           }
 26531  26990           if( needQuote ) bufpt[j++] = q;
 26532  26991           bufpt[j] = 0;
 26533  26992           length = j;
 26534         -        /* The precision in %q and %Q means how many input characters to
 26535         -        ** consume, not the length of the output...
 26536         -        ** if( precision>=0 && precision<length ) length = precision; */
 26537         -        break;
        26993  +        goto adjust_width_for_utf8;
 26538  26994         }
 26539  26995         case etTOKEN: {
 26540  26996           Token *pToken;
 26541  26997           if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
 26542  26998           pToken = va_arg(ap, Token*);
 26543  26999           assert( bArgList==0 );
 26544  27000           if( pToken && pToken->n ){
................................................................................
 26569  27025           assert( xtype==etINVALID );
 26570  27026           return;
 26571  27027         }
 26572  27028       }/* End switch over the format type */
 26573  27029       /*
 26574  27030       ** The text of the conversion is pointed to by "bufpt" and is
 26575  27031       ** "length" characters long.  The field width is "width".  Do
 26576         -    ** the output.
        27032  +    ** the output.  Both length and width are in bytes, not characters,
        27033  +    ** at this point.  If the "!" flag was present on string conversions
        27034  +    ** indicating that width and precision should be expressed in characters,
        27035  +    ** then the values have been translated prior to reaching this point.
 26577  27036       */
 26578  27037       width -= length;
 26579  27038       if( width>0 ){
 26580  27039         if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 26581  27040         sqlite3StrAccumAppend(pAccum, bufpt, length);
 26582  27041         if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 26583  27042       }else{
................................................................................
 27084  27543     pView = sqlite3TreeViewPush(pView, moreToFollow);
 27085  27544     if( p->pWith ){
 27086  27545       sqlite3TreeViewWith(pView, p->pWith, 1);
 27087  27546       cnt = 1;
 27088  27547       sqlite3TreeViewPush(pView, 1);
 27089  27548     }
 27090  27549     do{
        27550  +#if SELECTTRACE_ENABLED
        27551  +    sqlite3TreeViewLine(pView,
        27552  +      "SELECT%s%s (%s/%p) selFlags=0x%x nSelectRow=%d",
        27553  +      ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
        27554  +      ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
        27555  +      p->zSelName, p, p->selFlags,
        27556  +      (int)p->nSelectRow
        27557  +    );
        27558  +#else
 27091  27559       sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
 27092  27560         ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
 27093  27561         ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags,
 27094  27562         (int)p->nSelectRow
 27095  27563       );
        27564  +#endif
 27096  27565       if( cnt++ ) sqlite3TreeViewPop(pView);
 27097  27566       if( p->pPrior ){
 27098  27567         n = 1000;
 27099  27568       }else{
 27100  27569         n = 0;
 27101  27570         if( p->pSrc && p->pSrc->nSrc ) n++;
 27102  27571         if( p->pWhere ) n++;
................................................................................
 27238  27707       case TK_STRING: {
 27239  27708         sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
 27240  27709         break;
 27241  27710       }
 27242  27711       case TK_NULL: {
 27243  27712         sqlite3TreeViewLine(pView,"NULL");
 27244  27713         break;
        27714  +    }
        27715  +    case TK_TRUEFALSE: {
        27716  +      sqlite3TreeViewLine(pView,
        27717  +         sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
        27718  +      break;
 27245  27719       }
 27246  27720   #ifndef SQLITE_OMIT_BLOB_LITERAL
 27247  27721       case TK_BLOB: {
 27248  27722         sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
 27249  27723         break;
 27250  27724       }
 27251  27725   #endif
................................................................................
 27294  27768   
 27295  27769       case TK_UMINUS:  zUniOp = "UMINUS"; break;
 27296  27770       case TK_UPLUS:   zUniOp = "UPLUS";  break;
 27297  27771       case TK_BITNOT:  zUniOp = "BITNOT"; break;
 27298  27772       case TK_NOT:     zUniOp = "NOT";    break;
 27299  27773       case TK_ISNULL:  zUniOp = "ISNULL"; break;
 27300  27774       case TK_NOTNULL: zUniOp = "NOTNULL"; break;
        27775  +
        27776  +    case TK_TRUTH: {
        27777  +      int x;
        27778  +      const char *azOp[] = {
        27779  +         "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
        27780  +      };
        27781  +      assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
        27782  +      assert( pExpr->pRight );
        27783  +      assert( pExpr->pRight->op==TK_TRUEFALSE );
        27784  +      x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
        27785  +      zUniOp = azOp[x];
        27786  +      break;
        27787  +    }
 27301  27788   
 27302  27789       case TK_SPAN: {
 27303  27790         sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
 27304  27791         sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 27305  27792         break;
 27306  27793       }
 27307  27794   
................................................................................
 28764  29251   /*
 28765  29252   ** Compute 10 to the E-th power.  Examples:  E==1 results in 10.
 28766  29253   ** E==2 results in 100.  E==50 results in 1.0e50.
 28767  29254   **
 28768  29255   ** This routine only works for values of E between 1 and 341.
 28769  29256   */
 28770  29257   static LONGDOUBLE_TYPE sqlite3Pow10(int E){
        29258  +#if defined(_MSC_VER)
        29259  +  static const LONGDOUBLE_TYPE x[] = {
        29260  +    1.0e+001,
        29261  +    1.0e+002,
        29262  +    1.0e+004,
        29263  +    1.0e+008,
        29264  +    1.0e+016,
        29265  +    1.0e+032,
        29266  +    1.0e+064,
        29267  +    1.0e+128,
        29268  +    1.0e+256
        29269  +  };
        29270  +  LONGDOUBLE_TYPE r = 1.0;
        29271  +  int i;
        29272  +  assert( E>=0 && E<=307 );
        29273  +  for(i=0; E!=0; i++, E >>=1){
        29274  +    if( E & 1 ) r *= x[i];
        29275  +  }
        29276  +  return r;
        29277  +#else
 28771  29278     LONGDOUBLE_TYPE x = 10.0;
 28772  29279     LONGDOUBLE_TYPE r = 1.0;
 28773  29280     while(1){
 28774  29281       if( E & 1 ) r *= x;
 28775  29282       E >>= 1;
 28776  29283       if( E==0 ) break;
 28777  29284       x *= x;
 28778  29285     }
 28779  29286     return r; 
        29287  +#endif
 28780  29288   }
 28781  29289   
 28782  29290   /*
 28783  29291   ** The string z[] is an text representation of a real number.
 28784  29292   ** Convert this string to a double and write it into *pResult.
 28785  29293   **
 28786  29294   ** The string z[] is length bytes in length (bytes, not characters) and
................................................................................
 29011  29519   /*
 29012  29520   ** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
 29013  29521   ** routine does *not* accept hexadecimal notation.
 29014  29522   **
 29015  29523   ** Returns:
 29016  29524   **
 29017  29525   **     0    Successful transformation.  Fits in a 64-bit signed integer.
 29018         -**     1    Excess text after the integer value
        29526  +**     1    Excess non-space text after the integer value
 29019  29527   **     2    Integer too large for a 64-bit signed integer or is malformed
 29020  29528   **     3    Special case of 9223372036854775808
 29021  29529   **
 29022  29530   ** length is the number of bytes in the string (bytes, not characters).
 29023  29531   ** The string is not necessarily zero-terminated.  The encoding is
 29024  29532   ** given by enc.
 29025  29533   */
................................................................................
 29054  29562       }
 29055  29563     }
 29056  29564     zStart = zNum;
 29057  29565     while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
 29058  29566     for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
 29059  29567       u = u*10 + c - '0';
 29060  29568     }
        29569  +  testcase( i==18*incr );
        29570  +  testcase( i==19*incr );
        29571  +  testcase( i==20*incr );
 29061  29572     if( u>LARGEST_INT64 ){
        29573  +    /* This test and assignment is needed only to suppress UB warnings
        29574  +    ** from clang and -fsanitize=undefined.  This test and assignment make
        29575  +    ** the code a little larger and slower, and no harm comes from omitting
        29576  +    ** them, but we must appaise the undefined-behavior pharisees. */
 29062  29577       *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
 29063  29578     }else if( neg ){
 29064  29579       *pNum = -(i64)u;
 29065  29580     }else{
 29066  29581       *pNum = (i64)u;
 29067  29582     }
 29068         -  testcase( i==18 );
 29069         -  testcase( i==19 );
 29070         -  testcase( i==20 );
 29071         -  if( &zNum[i]<zEnd              /* Extra bytes at the end */
 29072         -   || (i==0 && zStart==zNum)     /* No digits */
        29583  +  rc = 0;
        29584  +  if( (i==0 && zStart==zNum)     /* No digits */
 29073  29585      || nonNum                     /* UTF16 with high-order bytes non-zero */
 29074  29586     ){
 29075  29587       rc = 1;
 29076         -  }else{
 29077         -    rc = 0;
        29588  +  }else if( &zNum[i]<zEnd ){     /* Extra bytes at the end */
        29589  +    int jj = i;
        29590  +    do{
        29591  +      if( !sqlite3Isspace(zNum[jj]) ){
        29592  +        rc = 1;          /* Extra non-space text after the integer */
        29593  +        break;
        29594  +      }
        29595  +      jj += incr;
        29596  +    }while( &zNum[jj]<zEnd );
 29078  29597     }
 29079         -  if( i>19*incr ){                /* Too many digits */
 29080         -    /* zNum is empty or contains non-numeric text or is longer
 29081         -    ** than 19 digits (thus guaranteeing that it is too large) */
 29082         -    return 2;
 29083         -  }else if( i<19*incr ){
        29598  +  if( i<19*incr ){
 29084  29599       /* Less than 19 digits, so we know that it fits in 64 bits */
 29085  29600       assert( u<=LARGEST_INT64 );
 29086  29601       return rc;
 29087  29602     }else{
 29088  29603       /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
 29089         -    c = compare2pow63(zNum, incr);
        29604  +    c = i>19*incr ? 1 : compare2pow63(zNum, incr);
 29090  29605       if( c<0 ){
 29091  29606         /* zNum is less than 9223372036854775808 so it fits */
 29092  29607         assert( u<=LARGEST_INT64 );
 29093  29608         return rc;
 29094         -    }else if( c>0 ){
 29095         -      /* zNum is greater than 9223372036854775808 so it overflows */
 29096         -      return 2;
 29097  29609       }else{
 29098         -      /* zNum is exactly 9223372036854775808.  Fits if negative.  The
 29099         -      ** special case 2 overflow if positive */
 29100         -      assert( u-1==LARGEST_INT64 );
 29101         -      return neg ? rc : 3;
        29610  +      *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
        29611  +      if( c>0 ){
        29612  +        /* zNum is greater than 9223372036854775808 so it overflows */
        29613  +        return 2;
        29614  +      }else{
        29615  +        /* zNum is exactly 9223372036854775808.  Fits if negative.  The
        29616  +        ** special case 2 overflow if positive */
        29617  +        assert( u-1==LARGEST_INT64 );
        29618  +        return neg ? rc : 3;
        29619  +      }
 29102  29620       }
 29103  29621     }
 29104  29622   }
 29105  29623   
 29106  29624   /*
 29107  29625   ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
 29108  29626   ** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
................................................................................
 30411  30929       /*  89 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 30412  30930       /*  90 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 30413  30931       /*  91 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 30414  30932       /*  92 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 30415  30933       /*  93 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 30416  30934       /*  94 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 30417  30935       /*  95 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 30418         -    /*  96 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
        30936  +    /*  96 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
 30419  30937       /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 30420         -    /*  98 */ "Column"           OpHelp("r[P3]=PX"),
 30421         -    /*  99 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 30422         -    /* 100 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 30423         -    /* 101 */ "Count"            OpHelp("r[P2]=count()"),
 30424         -    /* 102 */ "ReadCookie"       OpHelp(""),
 30425         -    /* 103 */ "SetCookie"        OpHelp(""),
 30426         -    /* 104 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 30427         -    /* 105 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 30428         -    /* 106 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
 30429         -    /* 107 */ "OpenDup"          OpHelp(""),
 30430         -    /* 108 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 30431         -    /* 109 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 30432         -    /* 110 */ "SorterOpen"       OpHelp(""),
 30433         -    /* 111 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 30434         -    /* 112 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 30435         -    /* 113 */ "Close"            OpHelp(""),
 30436         -    /* 114 */ "ColumnsUsed"      OpHelp(""),
 30437         -    /* 115 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 30438         -    /* 116 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 30439         -    /* 117 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 30440         -    /* 118 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 30441         -    /* 119 */ "Delete"           OpHelp(""),
 30442         -    /* 120 */ "ResetCount"       OpHelp(""),
 30443         -    /* 121 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 30444         -    /* 122 */ "SorterData"       OpHelp("r[P2]=data"),
 30445         -    /* 123 */ "RowData"          OpHelp("r[P2]=data"),
 30446         -    /* 124 */ "Rowid"            OpHelp("r[P2]=rowid"),
 30447         -    /* 125 */ "NullRow"          OpHelp(""),
 30448         -    /* 126 */ "SeekEnd"          OpHelp(""),
 30449         -    /* 127 */ "SorterInsert"     OpHelp("key=r[P2]"),
 30450         -    /* 128 */ "IdxInsert"        OpHelp("key=r[P2]"),
 30451         -    /* 129 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 30452         -    /* 130 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 30453         -    /* 131 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        30938  +    /*  98 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
        30939  +    /*  99 */ "Column"           OpHelp("r[P3]=PX"),
        30940  +    /* 100 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
        30941  +    /* 101 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        30942  +    /* 102 */ "Count"            OpHelp("r[P2]=count()"),
        30943  +    /* 103 */ "ReadCookie"       OpHelp(""),
        30944  +    /* 104 */ "SetCookie"        OpHelp(""),
        30945  +    /* 105 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
        30946  +    /* 106 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        30947  +    /* 107 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        30948  +    /* 108 */ "OpenDup"          OpHelp(""),
        30949  +    /* 109 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
        30950  +    /* 110 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
        30951  +    /* 111 */ "SorterOpen"       OpHelp(""),
        30952  +    /* 112 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
        30953  +    /* 113 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
        30954  +    /* 114 */ "Close"            OpHelp(""),
        30955  +    /* 115 */ "ColumnsUsed"      OpHelp(""),
        30956  +    /* 116 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
        30957  +    /* 117 */ "NewRowid"         OpHelp("r[P2]=rowid"),
        30958  +    /* 118 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
        30959  +    /* 119 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
        30960  +    /* 120 */ "Delete"           OpHelp(""),
        30961  +    /* 121 */ "ResetCount"       OpHelp(""),
        30962  +    /* 122 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
        30963  +    /* 123 */ "SorterData"       OpHelp("r[P2]=data"),
        30964  +    /* 124 */ "RowData"          OpHelp("r[P2]=data"),
        30965  +    /* 125 */ "Rowid"            OpHelp("r[P2]=rowid"),
        30966  +    /* 126 */ "NullRow"          OpHelp(""),
        30967  +    /* 127 */ "SeekEnd"          OpHelp(""),
        30968  +    /* 128 */ "SorterInsert"     OpHelp("key=r[P2]"),
        30969  +    /* 129 */ "IdxInsert"        OpHelp("key=r[P2]"),
        30970  +    /* 130 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        30971  +    /* 131 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 30454  30972       /* 132 */ "Real"             OpHelp("r[P2]=P4"),
 30455         -    /* 133 */ "Destroy"          OpHelp(""),
 30456         -    /* 134 */ "Clear"            OpHelp(""),
 30457         -    /* 135 */ "ResetSorter"      OpHelp(""),
 30458         -    /* 136 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
 30459         -    /* 137 */ "SqlExec"          OpHelp(""),
 30460         -    /* 138 */ "ParseSchema"      OpHelp(""),
 30461         -    /* 139 */ "LoadAnalysis"     OpHelp(""),
 30462         -    /* 140 */ "DropTable"        OpHelp(""),
 30463         -    /* 141 */ "DropIndex"        OpHelp(""),
 30464         -    /* 142 */ "DropTrigger"      OpHelp(""),
 30465         -    /* 143 */ "IntegrityCk"      OpHelp(""),
 30466         -    /* 144 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 30467         -    /* 145 */ "Param"            OpHelp(""),
 30468         -    /* 146 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 30469         -    /* 147 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 30470         -    /* 148 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
 30471         -    /* 149 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
 30472         -    /* 150 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 30473         -    /* 151 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 30474         -    /* 152 */ "Expire"           OpHelp(""),
 30475         -    /* 153 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 30476         -    /* 154 */ "VBegin"           OpHelp(""),
 30477         -    /* 155 */ "VCreate"          OpHelp(""),
 30478         -    /* 156 */ "VDestroy"         OpHelp(""),
 30479         -    /* 157 */ "VOpen"            OpHelp(""),
 30480         -    /* 158 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 30481         -    /* 159 */ "VRename"          OpHelp(""),
 30482         -    /* 160 */ "Pagecount"        OpHelp(""),
 30483         -    /* 161 */ "MaxPgcnt"         OpHelp(""),
 30484         -    /* 162 */ "PureFunc0"        OpHelp(""),
 30485         -    /* 163 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
 30486         -    /* 164 */ "PureFunc"         OpHelp(""),
 30487         -    /* 165 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
 30488         -    /* 166 */ "Trace"            OpHelp(""),
 30489         -    /* 167 */ "CursorHint"       OpHelp(""),
 30490         -    /* 168 */ "Noop"             OpHelp(""),
 30491         -    /* 169 */ "Explain"          OpHelp(""),
        30973  +    /* 133 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        30974  +    /* 134 */ "Destroy"          OpHelp(""),
        30975  +    /* 135 */ "Clear"            OpHelp(""),
        30976  +    /* 136 */ "ResetSorter"      OpHelp(""),
        30977  +    /* 137 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
        30978  +    /* 138 */ "SqlExec"          OpHelp(""),
        30979  +    /* 139 */ "ParseSchema"      OpHelp(""),
        30980  +    /* 140 */ "LoadAnalysis"     OpHelp(""),
        30981  +    /* 141 */ "DropTable"        OpHelp(""),
        30982  +    /* 142 */ "DropIndex"        OpHelp(""),
        30983  +    /* 143 */ "DropTrigger"      OpHelp(""),
        30984  +    /* 144 */ "IntegrityCk"      OpHelp(""),
        30985  +    /* 145 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        30986  +    /* 146 */ "Param"            OpHelp(""),
        30987  +    /* 147 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        30988  +    /* 148 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        30989  +    /* 149 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
        30990  +    /* 150 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
        30991  +    /* 151 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
        30992  +    /* 152 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        30993  +    /* 153 */ "Expire"           OpHelp(""),
        30994  +    /* 154 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        30995  +    /* 155 */ "VBegin"           OpHelp(""),
        30996  +    /* 156 */ "VCreate"          OpHelp(""),
        30997  +    /* 157 */ "VDestroy"         OpHelp(""),
        30998  +    /* 158 */ "VOpen"            OpHelp(""),
        30999  +    /* 159 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        31000  +    /* 160 */ "VRename"          OpHelp(""),
        31001  +    /* 161 */ "Pagecount"        OpHelp(""),
        31002  +    /* 162 */ "MaxPgcnt"         OpHelp(""),
        31003  +    /* 163 */ "PureFunc0"        OpHelp(""),
        31004  +    /* 164 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
        31005  +    /* 165 */ "PureFunc"         OpHelp(""),
        31006  +    /* 166 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
        31007  +    /* 167 */ "Trace"            OpHelp(""),
        31008  +    /* 168 */ "CursorHint"       OpHelp(""),
        31009  +    /* 169 */ "Noop"             OpHelp(""),
        31010  +    /* 170 */ "Explain"          OpHelp(""),
 30492  31011     };
 30493  31012     return azName[i];
 30494  31013   }
 30495  31014   #endif
 30496  31015   
 30497  31016   /************** End of opcodes.c *********************************************/
 30498  31017   /************** Begin file os_unix.c *****************************************/
................................................................................
 30723  31242     int deviceCharacteristics;          /* Precomputed device characteristics */
 30724  31243   #if SQLITE_ENABLE_LOCKING_STYLE
 30725  31244     int openFlags;                      /* The flags specified at open() */
 30726  31245   #endif
 30727  31246   #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
 30728  31247     unsigned fsFlags;                   /* cached details from statfs() */
 30729  31248   #endif
        31249  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        31250  +  unsigned iBusyTimeout;              /* Wait this many millisec on locks */
        31251  +#endif
 30730  31252   #if OS_VXWORKS
 30731  31253     struct vxworksFileId *pId;          /* Unique file ID */
 30732  31254   #endif
 30733  31255   #ifdef SQLITE_DEBUG
 30734  31256     /* The next group of variables are used to track whether or not the
 30735  31257     ** transaction counter in bytes 24-27 of database files are updated
 30736  31258     ** whenever any part of the database changes.  An assertion fault will
................................................................................
 31160  31682   #if defined(HAVE_FCHOWN)
 31161  31683     { "fchown",       (sqlite3_syscall_ptr)fchown,          0 },
 31162  31684   #else
 31163  31685     { "fchown",       (sqlite3_syscall_ptr)0,               0 },
 31164  31686   #endif
 31165  31687   #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
 31166  31688   
        31689  +#if defined(HAVE_FCHOWN)
 31167  31690     { "geteuid",      (sqlite3_syscall_ptr)geteuid,         0 },
        31691  +#else
        31692  +  { "geteuid",      (sqlite3_syscall_ptr)0,               0 },
        31693  +#endif
 31168  31694   #define osGeteuid   ((uid_t(*)(void))aSyscall[21].pCurrent)
 31169  31695   
 31170  31696   #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
 31171  31697     { "mmap",         (sqlite3_syscall_ptr)mmap,            0 },
 31172  31698   #else
 31173  31699     { "mmap",         (sqlite3_syscall_ptr)0,               0 },
 31174  31700   #endif
................................................................................
 31388  31914   ** is held when required. This function is only used as part of assert() 
 31389  31915   ** statements. e.g.
 31390  31916   **
 31391  31917   **   unixEnterMutex()
 31392  31918   **     assert( unixMutexHeld() );
 31393  31919   **   unixEnterLeave()
 31394  31920   */
        31921  +static sqlite3_mutex *unixBigLock = 0;
 31395  31922   static void unixEnterMutex(void){
 31396         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31923  +  sqlite3_mutex_enter(unixBigLock);
 31397  31924   }
 31398  31925   static void unixLeaveMutex(void){
 31399         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31926  +  sqlite3_mutex_leave(unixBigLock);
 31400  31927   }
 31401  31928   #ifdef SQLITE_DEBUG
 31402  31929   static int unixMutexHeld(void) {
 31403         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31930  +  return sqlite3_mutex_held(unixBigLock);
 31404  31931   }
 31405  31932   #endif
 31406  31933   
 31407  31934   
 31408  31935   #ifdef SQLITE_HAVE_OS_TRACE
 31409  31936   /*
 31410  31937   ** Helper function for printing out trace information from debugging
................................................................................
 32151  32678     
 32152  32679     unixLeaveMutex();
 32153  32680     OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
 32154  32681   
 32155  32682     *pResOut = reserved;
 32156  32683     return rc;
 32157  32684   }
        32685  +
        32686  +/*
        32687  +** Set a posix-advisory-lock.
        32688  +**
        32689  +** There are two versions of this routine.  If compiled with
        32690  +** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
        32691  +** which is a pointer to a unixFile.  If the unixFile->iBusyTimeout
        32692  +** value is set, then it is the number of milliseconds to wait before
        32693  +** failing the lock.  The iBusyTimeout value is always reset back to
        32694  +** zero on each call.
        32695  +**
        32696  +** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
        32697  +** attempt to set the lock.
        32698  +*/
        32699  +#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
        32700  +# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
        32701  +#else
        32702  +static int osSetPosixAdvisoryLock(
        32703  +  int h,                /* The file descriptor on which to take the lock */
        32704  +  struct flock *pLock,  /* The description of the lock */
        32705  +  unixFile *pFile       /* Structure holding timeout value */
        32706  +){
        32707  +  int rc = osFcntl(h,F_SETLK,pLock);
        32708  +  while( rc<0 && pFile->iBusyTimeout>0 ){
        32709  +    /* On systems that support some kind of blocking file lock with a timeout,
        32710  +    ** make appropriate changes here to invoke that blocking file lock.  On
        32711  +    ** generic posix, however, there is no such API.  So we simply try the
        32712  +    ** lock once every millisecond until either the timeout expires, or until
        32713  +    ** the lock is obtained. */
        32714  +    usleep(1000);
        32715  +    rc = osFcntl(h,F_SETLK,pLock);
        32716  +    pFile->iBusyTimeout--;
        32717  +  }
        32718  +  return rc;
        32719  +}
        32720  +#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
        32721  +
 32158  32722   
 32159  32723   /*
 32160  32724   ** Attempt to set a system-lock on the file pFile.  The lock is 
 32161  32725   ** described by pLock.
 32162  32726   **
 32163  32727   ** If the pFile was opened read/write from unix-excl, then the only lock
 32164  32728   ** ever obtained is an exclusive lock, and it is obtained exactly once
................................................................................
 32184  32748       if( pInode->bProcessLock==0 ){
 32185  32749         struct flock lock;
 32186  32750         assert( pInode->nLock==0 );
 32187  32751         lock.l_whence = SEEK_SET;
 32188  32752         lock.l_start = SHARED_FIRST;
 32189  32753         lock.l_len = SHARED_SIZE;
 32190  32754         lock.l_type = F_WRLCK;
 32191         -      rc = osFcntl(pFile->h, F_SETLK, &lock);
        32755  +      rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
 32192  32756         if( rc<0 ) return rc;
 32193  32757         pInode->bProcessLock = 1;
 32194  32758         pInode->nLock++;
 32195  32759       }else{
 32196  32760         rc = 0;
 32197  32761       }
 32198  32762     }else{
 32199         -    rc = osFcntl(pFile->h, F_SETLK, pLock);
        32763  +    rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
 32200  32764     }
 32201  32765     return rc;
 32202  32766   }
 32203  32767   
 32204  32768   /*
 32205  32769   ** Lock the file with the lock specified by parameter eFileLock - one
 32206  32770   ** of the following:
................................................................................
 34552  35116         }
 34553  35117         return SQLITE_OK;
 34554  35118       }
 34555  35119       case SQLITE_FCNTL_HAS_MOVED: {
 34556  35120         *(int*)pArg = fileHasMoved(pFile);
 34557  35121         return SQLITE_OK;
 34558  35122       }
        35123  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        35124  +    case SQLITE_FCNTL_LOCK_TIMEOUT: {
        35125  +      pFile->iBusyTimeout = *(int*)pArg;
        35126  +      return SQLITE_OK;
        35127  +    }
        35128  +#endif
 34559  35129   #if SQLITE_MAX_MMAP_SIZE>0
 34560  35130       case SQLITE_FCNTL_MMAP_SIZE: {
 34561  35131         i64 newLimit = *(i64*)pArg;
 34562  35132         int rc = SQLITE_OK;
 34563  35133         if( newLimit>sqlite3GlobalConfig.mxMmap ){
 34564  35134           newLimit = sqlite3GlobalConfig.mxMmap;
 34565  35135         }
................................................................................
 34867  35437     assert( n==1 || lockType!=F_RDLCK );
 34868  35438   
 34869  35439     /* Locks are within range */
 34870  35440     assert( n>=1 && n<=SQLITE_SHM_NLOCK );
 34871  35441   
 34872  35442     if( pShmNode->h>=0 ){
 34873  35443       /* Initialize the locking parameters */
 34874         -    memset(&f, 0, sizeof(f));
 34875  35444       f.l_type = lockType;
 34876  35445       f.l_whence = SEEK_SET;
 34877  35446       f.l_start = ofst;
 34878  35447       f.l_len = n;
 34879         -
 34880         -    rc = osFcntl(pShmNode->h, F_SETLK, &f);
        35448  +    rc = osSetPosixAdvisoryLock(pShmNode->h, &f, pFile);
 34881  35449       rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 34882  35450     }
 34883  35451   
 34884  35452     /* Update the global lock state and do debug tracing */
 34885  35453   #ifdef SQLITE_DEBUG
 34886  35454     { u16 mask;
 34887  35455     OSTRACE(("SHM-LOCK "));
................................................................................
 36538  37106     ** the same instant might all reset the PRNG.  But multiple resets
 36539  37107     ** are harmless.
 36540  37108     */
 36541  37109     if( randomnessPid!=osGetpid(0) ){
 36542  37110       randomnessPid = osGetpid(0);
 36543  37111       sqlite3_randomness(0,0);
 36544  37112     }
 36545         -
 36546  37113     memset(p, 0, sizeof(unixFile));
 36547  37114   
 36548  37115     if( eType==SQLITE_OPEN_MAIN_DB ){
 36549  37116       UnixUnusedFd *pUnused;
 36550  37117       pUnused = findReusableFd(zName, flags);
 36551  37118       if( pUnused ){
 36552  37119         fd = pUnused->fd;
................................................................................
 38413  38980     ** correctly.  See ticket [bb3a86e890c8e96ab] */
 38414  38981     assert( ArraySize(aSyscall)==29 );
 38415  38982   
 38416  38983     /* Register all VFSes defined in the aVfs[] array */
 38417  38984     for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
 38418  38985       sqlite3_vfs_register(&aVfs[i], i==0);
 38419  38986     }
        38987  +  unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
 38420  38988     return SQLITE_OK; 
 38421  38989   }
 38422  38990   
 38423  38991   /*
 38424  38992   ** Shutdown the operating system interface.
 38425  38993   **
 38426  38994   ** Some operating systems might need to do some cleanup in this routine,
 38427  38995   ** to release dynamically allocated objects.  But not on unix.
 38428  38996   ** This routine is a no-op for unix.
 38429  38997   */
 38430  38998   SQLITE_API int sqlite3_os_end(void){ 
        38999  +  unixBigLock = 0;
 38431  39000     return SQLITE_OK; 
 38432  39001   }
 38433  39002    
 38434  39003   #endif /* SQLITE_OS_UNIX */
 38435  39004   
 38436  39005   /************** End of os_unix.c *********************************************/
 38437  39006   /************** Begin file os_win.c ******************************************/
................................................................................
 42262  42831   ** is held when required. This function is only used as part of assert()
 42263  42832   ** statements. e.g.
 42264  42833   **
 42265  42834   **   winShmEnterMutex()
 42266  42835   **     assert( winShmMutexHeld() );
 42267  42836   **   winShmLeaveMutex()
 42268  42837   */
        42838  +static sqlite3_mutex *winBigLock = 0;
 42269  42839   static void winShmEnterMutex(void){
 42270         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42840  +  sqlite3_mutex_enter(winBigLock);
 42271  42841   }
 42272  42842   static void winShmLeaveMutex(void){
 42273         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42843  +  sqlite3_mutex_leave(winBigLock);
 42274  42844   }
 42275  42845   #ifndef NDEBUG
 42276  42846   static int winShmMutexHeld(void) {
 42277         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42847  +  return sqlite3_mutex_held(winBigLock);
 42278  42848   }
 42279  42849   #endif
 42280  42850   
 42281  42851   /*
 42282  42852   ** Object used to represent a single file opened and mmapped to provide
 42283  42853   ** shared memory.  When multiple threads all reference the same
 42284  42854   ** log-summary, each thread has its own winFile object, but they all
................................................................................
 42496  43066   ** file are currently open, in this process or in other processes, then
 42497  43067   ** the file must be truncated to zero length or have its header cleared.
 42498  43068   */
 42499  43069   static int winOpenSharedMemory(winFile *pDbFd){
 42500  43070     struct winShm *p;                  /* The connection to be opened */
 42501  43071     winShmNode *pShmNode = 0;          /* The underlying mmapped file */
 42502  43072     int rc = SQLITE_OK;                /* Result code */
 42503         -  int rc2 = SQLITE_ERROR;            /* winOpen result code */
 42504  43073     winShmNode *pNew;                  /* Newly allocated winShmNode */
 42505  43074     int nName;                         /* Size of zName in bytes */
 42506  43075   
 42507  43076     assert( pDbFd->pShm==0 );    /* Not previously opened */
 42508  43077   
 42509  43078     /* Allocate space for the new sqlite3_shm object.  Also speculatively
 42510  43079     ** allocate space for a new winShmNode and filename.
................................................................................
 42530  43099       ** use FILE_ID_BOTH_DIR_INFO Structure.
 42531  43100       */
 42532  43101       if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
 42533  43102     }
 42534  43103     if( pShmNode ){
 42535  43104       sqlite3_free(pNew);
 42536  43105     }else{
        43106  +    int inFlags = SQLITE_OPEN_WAL;
        43107  +    int outFlags = 0;
        43108  +
 42537  43109       pShmNode = pNew;
 42538  43110       pNew = 0;
 42539  43111       ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
 42540  43112       pShmNode->pNext = winShmNodeList;
 42541  43113       winShmNodeList = pShmNode;
 42542  43114   
 42543  43115       if( sqlite3GlobalConfig.bCoreMutex ){
................................................................................
 42545  43117         if( pShmNode->mutex==0 ){
 42546  43118           rc = SQLITE_IOERR_NOMEM_BKPT;
 42547  43119           goto shm_open_err;
 42548  43120         }
 42549  43121       }
 42550  43122   
 42551  43123       if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
 42552         -      rc2 = winOpen(pDbFd->pVfs,
 42553         -                    pShmNode->zFilename,
 42554         -                    (sqlite3_file*)&pShmNode->hFile,
 42555         -                    SQLITE_OPEN_WAL|SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE,
 42556         -                    0);
        43124  +      inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
        43125  +    }else{
        43126  +      inFlags |= SQLITE_OPEN_READONLY;
 42557  43127       }
 42558         -    if( rc2!=SQLITE_OK ){
 42559         -      rc2 = winOpen(pDbFd->pVfs,
 42560         -                    pShmNode->zFilename,
 42561         -                    (sqlite3_file*)&pShmNode->hFile,
 42562         -                    SQLITE_OPEN_WAL|SQLITE_OPEN_READONLY,
 42563         -                    0);
 42564         -      if( rc2!=SQLITE_OK ){
 42565         -        rc = winLogError(rc2, osGetLastError(), "winOpenShm",
 42566         -                         pShmNode->zFilename);
 42567         -        goto shm_open_err;
 42568         -      }
 42569         -      pShmNode->isReadonly = 1;
        43128  +    rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
        43129  +                 (sqlite3_file*)&pShmNode->hFile,
        43130  +                 inFlags, &outFlags);
        43131  +    if( rc!=SQLITE_OK ){
        43132  +      rc = winLogError(rc, osGetLastError(), "winOpenShm",
        43133  +                       pShmNode->zFilename);
        43134  +      goto shm_open_err;
 42570  43135       }
        43136  +    if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
 42571  43137   
 42572  43138       rc = winLockSharedMemory(pShmNode);
 42573  43139       if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
 42574  43140     }
 42575  43141   
 42576  43142     /* Make the new connection a child of the winShmNode */
 42577  43143     p->pShmNode = pShmNode;
................................................................................
 43749  44315         h = osCreateFile2((LPCWSTR)zConverted,
 43750  44316                           dwDesiredAccess,
 43751  44317                           dwShareMode,
 43752  44318                           dwCreationDisposition,
 43753  44319                           &extendedParameters);
 43754  44320         if( h!=INVALID_HANDLE_VALUE ) break;
 43755  44321         if( isReadWrite ){
 43756         -        int isRO = 0;
 43757         -        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        44322  +        int rc2, isRO = 0;
        44323  +        sqlite3BeginBenignMalloc();
        44324  +        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        44325  +        sqlite3EndBenignMalloc();
 43758  44326           if( rc2==SQLITE_OK && isRO ) break;
 43759  44327         }
 43760  44328       }while( winRetryIoerr(&cnt, &lastErrno) );
 43761  44329   #else
 43762  44330       do{
 43763  44331         h = osCreateFileW((LPCWSTR)zConverted,
 43764  44332                           dwDesiredAccess,
 43765  44333                           dwShareMode, NULL,
 43766  44334                           dwCreationDisposition,
 43767  44335                           dwFlagsAndAttributes,
 43768  44336                           NULL);
 43769  44337         if( h!=INVALID_HANDLE_VALUE ) break;
 43770  44338         if( isReadWrite ){
 43771         -        int isRO = 0;
 43772         -        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        44339  +        int rc2, isRO = 0;
        44340  +        sqlite3BeginBenignMalloc();
        44341  +        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        44342  +        sqlite3EndBenignMalloc();
 43773  44343           if( rc2==SQLITE_OK && isRO ) break;
 43774  44344         }
 43775  44345       }while( winRetryIoerr(&cnt, &lastErrno) );
 43776  44346   #endif
 43777  44347     }
 43778  44348   #ifdef SQLITE_WIN32_HAS_ANSI
 43779  44349     else{
................................................................................
 43782  44352                           dwDesiredAccess,
 43783  44353                           dwShareMode, NULL,
 43784  44354                           dwCreationDisposition,
 43785  44355                           dwFlagsAndAttributes,
 43786  44356                           NULL);
 43787  44357         if( h!=INVALID_HANDLE_VALUE ) break;
 43788  44358         if( isReadWrite ){
 43789         -        int isRO = 0;
 43790         -        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        44359  +        int rc2, isRO = 0;
        44360  +        sqlite3BeginBenignMalloc();
        44361  +        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        44362  +        sqlite3EndBenignMalloc();
 43791  44363           if( rc2==SQLITE_OK && isRO ) break;
 43792  44364         }
 43793  44365       }while( winRetryIoerr(&cnt, &lastErrno) );
 43794  44366     }
 43795  44367   #endif
 43796  44368     winLogIoerr(cnt, __LINE__);
 43797  44369   
................................................................................
 44690  45262   #endif
 44691  45263   
 44692  45264     sqlite3_vfs_register(&winNolockVfs, 0);
 44693  45265   
 44694  45266   #if defined(SQLITE_WIN32_HAS_WIDE)
 44695  45267     sqlite3_vfs_register(&winLongPathNolockVfs, 0);
 44696  45268   #endif
        45269  +
        45270  +#ifndef SQLITE_OMIT_WAL
        45271  +  winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
        45272  +#endif
 44697  45273   
 44698  45274     return SQLITE_OK;
 44699  45275   }
 44700  45276   
 44701  45277   SQLITE_API int sqlite3_os_end(void){
 44702  45278   #if SQLITE_OS_WINRT
 44703  45279     if( sleepObj!=NULL ){
 44704  45280       osCloseHandle(sleepObj);
 44705  45281       sleepObj = NULL;
 44706  45282     }
 44707  45283   #endif
        45284  +
        45285  +#ifndef SQLITE_OMIT_WAL
        45286  +  winBigLock = 0;
        45287  +#endif
        45288  +
 44708  45289     return SQLITE_OK;
 44709  45290   }
 44710  45291   
 44711  45292   #endif /* SQLITE_OS_WIN */
 44712  45293   
 44713  45294   /************** End of os_win.c **********************************************/
        45295  +/************** Begin file memdb.c *******************************************/
        45296  +/*
        45297  +** 2016-09-07
        45298  +**
        45299  +** The author disclaims copyright to this source code.  In place of
        45300  +** a legal notice, here is a blessing:
        45301  +**
        45302  +**    May you do good and not evil.
        45303  +**    May you find forgiveness for yourself and forgive others.
        45304  +**    May you share freely, never taking more than you give.
        45305  +**
        45306  +******************************************************************************
        45307  +**
        45308  +** This file implements an in-memory VFS. A database is held as a contiguous
        45309  +** block of memory.
        45310  +**
        45311  +** This file also implements interface sqlite3_serialize() and
        45312  +** sqlite3_deserialize().
        45313  +*/
        45314  +#ifdef SQLITE_ENABLE_DESERIALIZE
        45315  +/* #include "sqliteInt.h" */
        45316  +
        45317  +/*
        45318  +** Forward declaration of objects used by this utility
        45319  +*/
        45320  +typedef struct sqlite3_vfs MemVfs;
        45321  +typedef struct MemFile MemFile;
        45322  +
        45323  +/* Access to a lower-level VFS that (might) implement dynamic loading,
        45324  +** access to randomness, etc.
        45325  +*/
        45326  +#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
        45327  +
        45328  +/* An open file */
        45329  +struct MemFile {
        45330  +  sqlite3_file base;              /* IO methods */
        45331  +  sqlite3_int64 sz;               /* Size of the file */
        45332  +  sqlite3_int64 szMax;            /* Space allocated to aData */
        45333  +  unsigned char *aData;           /* content of the file */
        45334  +  int nMmap;                      /* Number of memory mapped pages */
        45335  +  unsigned mFlags;                /* Flags */
        45336  +  int eLock;                      /* Most recent lock against this file */
        45337  +};
        45338  +
        45339  +/*
        45340  +** Methods for MemFile
        45341  +*/
        45342  +static int memdbClose(sqlite3_file*);
        45343  +static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
        45344  +static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
        45345  +static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
        45346  +static int memdbSync(sqlite3_file*, int flags);
        45347  +static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
        45348  +static int memdbLock(sqlite3_file*, int);
        45349  +/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
        45350  +static int memdbFileControl(sqlite3_file*, int op, void *pArg);
        45351  +/* static int memdbSectorSize(sqlite3_file*); // not used */
        45352  +static int memdbDeviceCharacteristics(sqlite3_file*);
        45353  +static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
        45354  +static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
        45355  +
        45356  +/*
        45357  +** Methods for MemVfs
        45358  +*/
        45359  +static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
        45360  +/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
        45361  +static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
        45362  +static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
        45363  +static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
        45364  +static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
        45365  +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
        45366  +static void memdbDlClose(sqlite3_vfs*, void*);
        45367  +static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
        45368  +static int memdbSleep(sqlite3_vfs*, int microseconds);
        45369  +/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
        45370  +static int memdbGetLastError(sqlite3_vfs*, int, char *);
        45371  +static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
        45372  +
        45373  +static sqlite3_vfs memdb_vfs = {
        45374  +  2,                           /* iVersion */
        45375  +  0,                           /* szOsFile (set when registered) */
        45376  +  1024,                        /* mxPathname */
        45377  +  0,                           /* pNext */
        45378  +  "memdb",                     /* zName */
        45379  +  0,                           /* pAppData (set when registered) */ 
        45380  +  memdbOpen,                   /* xOpen */
        45381  +  0, /* memdbDelete, */        /* xDelete */
        45382  +  memdbAccess,                 /* xAccess */
        45383  +  memdbFullPathname,           /* xFullPathname */
        45384  +  memdbDlOpen,                 /* xDlOpen */
        45385  +  memdbDlError,                /* xDlError */
        45386  +  memdbDlSym,                  /* xDlSym */
        45387  +  memdbDlClose,                /* xDlClose */
        45388  +  memdbRandomness,             /* xRandomness */
        45389  +  memdbSleep,                  /* xSleep */
        45390  +  0, /* memdbCurrentTime, */   /* xCurrentTime */
        45391  +  memdbGetLastError,           /* xGetLastError */
        45392  +  memdbCurrentTimeInt64        /* xCurrentTimeInt64 */
        45393  +};
        45394  +
        45395  +static const sqlite3_io_methods memdb_io_methods = {
        45396  +  3,                              /* iVersion */
        45397  +  memdbClose,                      /* xClose */
        45398  +  memdbRead,                       /* xRead */
        45399  +  memdbWrite,                      /* xWrite */
        45400  +  memdbTruncate,                   /* xTruncate */
        45401  +  memdbSync,                       /* xSync */
        45402  +  memdbFileSize,                   /* xFileSize */
        45403  +  memdbLock,                       /* xLock */
        45404  +  memdbLock,                       /* xUnlock - same as xLock in this case */ 
        45405  +  0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
        45406  +  memdbFileControl,                /* xFileControl */
        45407  +  0, /* memdbSectorSize,*/         /* xSectorSize */
        45408  +  memdbDeviceCharacteristics,      /* xDeviceCharacteristics */
        45409  +  0,                               /* xShmMap */
        45410  +  0,                               /* xShmLock */
        45411  +  0,                               /* xShmBarrier */
        45412  +  0,                               /* xShmUnmap */
        45413  +  memdbFetch,                      /* xFetch */
        45414  +  memdbUnfetch                     /* xUnfetch */
        45415  +};
        45416  +
        45417  +
        45418  +
        45419  +/*
        45420  +** Close an memdb-file.
        45421  +**
        45422  +** The pData pointer is owned by the application, so there is nothing
        45423  +** to free.
        45424  +*/
        45425  +static int memdbClose(sqlite3_file *pFile){
        45426  +  MemFile *p = (MemFile *)pFile;
        45427  +  if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
        45428  +  return SQLITE_OK;
        45429  +}
        45430  +
        45431  +/*
        45432  +** Read data from an memdb-file.
        45433  +*/
        45434  +static int memdbRead(
        45435  +  sqlite3_file *pFile, 
        45436  +  void *zBuf, 
        45437  +  int iAmt, 
        45438  +  sqlite_int64 iOfst
        45439  +){
        45440  +  MemFile *p = (MemFile *)pFile;
        45441  +  if( iOfst+iAmt>p->sz ){
        45442  +    memset(zBuf, 0, iAmt);
        45443  +    if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
        45444  +    return SQLITE_IOERR_SHORT_READ;
        45445  +  }
        45446  +  memcpy(zBuf, p->aData+iOfst, iAmt);
        45447  +  return SQLITE_OK;
        45448  +}
        45449  +
        45450  +/*
        45451  +** Try to enlarge the memory allocation to hold at least sz bytes
        45452  +*/
        45453  +static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
        45454  +  unsigned char *pNew;
        45455  +  if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
        45456  +    return SQLITE_FULL;
        45457  +  }
        45458  +  pNew = sqlite3_realloc64(p->aData, newSz);
        45459  +  if( pNew==0 ) return SQLITE_NOMEM;
        45460  +  p->aData = pNew;
        45461  +  p->szMax = newSz;
        45462  +  return SQLITE_OK;
        45463  +}
        45464  +
        45465  +/*
        45466  +** Write data to an memdb-file.
        45467  +*/
        45468  +static int memdbWrite(
        45469  +  sqlite3_file *pFile,
        45470  +  const void *z,
        45471  +  int iAmt,
        45472  +  sqlite_int64 iOfst
        45473  +){
        45474  +  MemFile *p = (MemFile *)pFile;
        45475  +  if( iOfst+iAmt>p->sz ){
        45476  +    int rc;
        45477  +    if( iOfst+iAmt>p->szMax
        45478  +     && (rc = memdbEnlarge(p, (iOfst+iAmt)*2))!=SQLITE_OK
        45479  +    ){
        45480  +      return rc;
        45481  +    }
        45482  +    if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
        45483  +    p->sz = iOfst+iAmt;
        45484  +  }
        45485  +  memcpy(p->aData+iOfst, z, iAmt);
        45486  +  return SQLITE_OK;
        45487  +}
        45488  +
        45489  +/*
        45490  +** Truncate an memdb-file.
        45491  +**
        45492  +** In rollback mode (which is always the case for memdb, as it does not
        45493  +** support WAL mode) the truncate() method is only used to reduce
        45494  +** the size of a file, never to increase the size.
        45495  +*/
        45496  +static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
        45497  +  MemFile *p = (MemFile *)pFile;
        45498  +  if( NEVER(size>p->sz) ) return SQLITE_FULL;
        45499  +  p->sz = size; 
        45500  +  return SQLITE_OK;
        45501  +}
        45502  +
        45503  +/*
        45504  +** Sync an memdb-file.
        45505  +*/
        45506  +static int memdbSync(sqlite3_file *pFile, int flags){
        45507  +  return SQLITE_OK;
        45508  +}
        45509  +
        45510  +/*
        45511  +** Return the current file-size of an memdb-file.
        45512  +*/
        45513  +static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
        45514  +  MemFile *p = (MemFile *)pFile;
        45515  +  *pSize = p->sz;
        45516  +  return SQLITE_OK;
        45517  +}
        45518  +
        45519  +/*
        45520  +** Lock an memdb-file.
        45521  +*/
        45522  +static int memdbLock(sqlite3_file *pFile, int eLock){
        45523  +  MemFile *p = (MemFile *)pFile;
        45524  +  p->eLock = eLock;
        45525  +  return SQLITE_OK;
        45526  +}
        45527  +
        45528  +#if 0 /* Never used because memdbAccess() always returns false */
        45529  +/*
        45530  +** Check if another file-handle holds a RESERVED lock on an memdb-file.
        45531  +*/
        45532  +static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
        45533  +  *pResOut = 0;
        45534  +  return SQLITE_OK;
        45535  +}
        45536  +#endif
        45537  +
        45538  +/*
        45539  +** File control method. For custom operations on an memdb-file.
        45540  +*/
        45541  +static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
        45542  +  MemFile *p = (MemFile *)pFile;
        45543  +  int rc = SQLITE_NOTFOUND;
        45544  +  if( op==SQLITE_FCNTL_VFSNAME ){
        45545  +    *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
        45546  +    rc = SQLITE_OK;
        45547  +  }
        45548  +  return rc;
        45549  +}
        45550  +
        45551  +#if 0  /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
        45552  +/*
        45553  +** Return the sector-size in bytes for an memdb-file.
        45554  +*/
        45555  +static int memdbSectorSize(sqlite3_file *pFile){
        45556  +  return 1024;
        45557  +}
        45558  +#endif
        45559  +
        45560  +/*
        45561  +** Return the device characteristic flags supported by an memdb-file.
        45562  +*/
        45563  +static int memdbDeviceCharacteristics(sqlite3_file *pFile){
        45564  +  return SQLITE_IOCAP_ATOMIC | 
        45565  +         SQLITE_IOCAP_POWERSAFE_OVERWRITE |
        45566  +         SQLITE_IOCAP_SAFE_APPEND |
        45567  +         SQLITE_IOCAP_SEQUENTIAL;
        45568  +}
        45569  +
        45570  +/* Fetch a page of a memory-mapped file */
        45571  +static int memdbFetch(
        45572  +  sqlite3_file *pFile,
        45573  +  sqlite3_int64 iOfst,
        45574  +  int iAmt,
        45575  +  void **pp
        45576  +){
        45577  +  MemFile *p = (MemFile *)pFile;
        45578  +  p->nMmap++;
        45579  +  *pp = (void*)(p->aData + iOfst);
        45580  +  return SQLITE_OK;
        45581  +}
        45582  +
        45583  +/* Release a memory-mapped page */
        45584  +static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
        45585  +  MemFile *p = (MemFile *)pFile;
        45586  +  p->nMmap--;
        45587  +  return SQLITE_OK;
        45588  +}
        45589  +
        45590  +/*
        45591  +** Open an mem file handle.
        45592  +*/
        45593  +static int memdbOpen(
        45594  +  sqlite3_vfs *pVfs,
        45595  +  const char *zName,
        45596  +  sqlite3_file *pFile,
        45597  +  int flags,
        45598  +  int *pOutFlags
        45599  +){
        45600  +  MemFile *p = (MemFile*)pFile;
        45601  +  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
        45602  +    return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
        45603  +  }
        45604  +  memset(p, 0, sizeof(*p));
        45605  +  p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
        45606  +  assert( pOutFlags!=0 );  /* True because flags==SQLITE_OPEN_MAIN_DB */
        45607  +  *pOutFlags = flags | SQLITE_OPEN_MEMORY;
        45608  +  p->base.pMethods = &memdb_io_methods;
        45609  +  return SQLITE_OK;
        45610  +}
        45611  +
        45612  +#if 0 /* Only used to delete rollback journals, master journals, and WAL
        45613  +      ** files, none of which exist in memdb.  So this routine is never used */
        45614  +/*
        45615  +** Delete the file located at zPath. If the dirSync argument is true,
        45616  +** ensure the file-system modifications are synced to disk before
        45617  +** returning.
        45618  +*/
        45619  +static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
        45620  +  return SQLITE_IOERR_DELETE;
        45621  +}
        45622  +#endif
        45623  +
        45624  +/*
        45625  +** Test for access permissions. Return true if the requested permission
        45626  +** is available, or false otherwise.
        45627  +**
        45628  +** With memdb, no files ever exist on disk.  So always return false.
        45629  +*/
        45630  +static int memdbAccess(
        45631  +  sqlite3_vfs *pVfs, 
        45632  +  const char *zPath, 
        45633  +  int flags, 
        45634  +  int *pResOut
        45635  +){
        45636  +  *pResOut = 0;
        45637  +  return SQLITE_OK;
        45638  +}
        45639  +
        45640  +/*
        45641  +** Populate buffer zOut with the full canonical pathname corresponding
        45642  +** to the pathname in zPath. zOut is guaranteed to point to a buffer
        45643  +** of at least (INST_MAX_PATHNAME+1) bytes.
        45644  +*/
        45645  +static int memdbFullPathname(
        45646  +  sqlite3_vfs *pVfs, 
        45647  +  const char *zPath, 
        45648  +  int nOut, 
        45649  +  char *zOut
        45650  +){
        45651  +  sqlite3_snprintf(nOut, zOut, "%s", zPath);
        45652  +  return SQLITE_OK;
        45653  +}
        45654  +
        45655  +/*
        45656  +** Open the dynamic library located at zPath and return a handle.
        45657  +*/
        45658  +static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
        45659  +  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
        45660  +}
        45661  +
        45662  +/*
        45663  +** Populate the buffer zErrMsg (size nByte bytes) with a human readable
        45664  +** utf-8 string describing the most recent error encountered associated 
        45665  +** with dynamic libraries.
        45666  +*/
        45667  +static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
        45668  +  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
        45669  +}
        45670  +
        45671  +/*
        45672  +** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
        45673  +*/
        45674  +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
        45675  +  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
        45676  +}
        45677  +
        45678  +/*
        45679  +** Close the dynamic library handle pHandle.
        45680  +*/
        45681  +static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
        45682  +  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
        45683  +}
        45684  +
        45685  +/*
        45686  +** Populate the buffer pointed to by zBufOut with nByte bytes of 
        45687  +** random data.
        45688  +*/
        45689  +static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
        45690  +  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
        45691  +}
        45692  +
        45693  +/*
        45694  +** Sleep for nMicro microseconds. Return the number of microseconds 
        45695  +** actually slept.
        45696  +*/
        45697  +static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
        45698  +  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
        45699  +}
        45700  +
        45701  +#if 0  /* Never used.  Modern cores only call xCurrentTimeInt64() */
        45702  +/*
        45703  +** Return the current time as a Julian Day number in *pTimeOut.
        45704  +*/
        45705  +static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
        45706  +  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
        45707  +}
        45708  +#endif
        45709  +
        45710  +static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
        45711  +  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
        45712  +}
        45713  +static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
        45714  +  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
        45715  +}
        45716  +
        45717  +/*
        45718  +** Translate a database connection pointer and schema name into a
        45719  +** MemFile pointer.
        45720  +*/
        45721  +static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
        45722  +  MemFile *p = 0;
        45723  +  int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
        45724  +  if( rc ) return 0;
        45725  +  if( p->base.pMethods!=&memdb_io_methods ) return 0;
        45726  +  return p;
        45727  +}
        45728  +
        45729  +/*
        45730  +** Return the serialization of a database
        45731  +*/
        45732  +SQLITE_API unsigned char *sqlite3_serialize(
        45733  +  sqlite3 *db,              /* The database connection */
        45734  +  const char *zSchema,      /* Which database within the connection */
        45735  +  sqlite3_int64 *piSize,    /* Write size here, if not NULL */
        45736  +  unsigned int mFlags       /* Maybe SQLITE_SERIALIZE_NOCOPY */
        45737  +){
        45738  +  MemFile *p;
        45739  +  int iDb;
        45740  +  Btree *pBt;
        45741  +  sqlite3_int64 sz;
        45742  +  int szPage = 0;
        45743  +  sqlite3_stmt *pStmt = 0;
        45744  +  unsigned char *pOut;
        45745  +  char *zSql;
        45746  +  int rc;
        45747  +
        45748  +#ifdef SQLITE_ENABLE_API_ARMOR
        45749  +  if( !sqlite3SafetyCheckOk(db) ){
        45750  +    (void)SQLITE_MISUSE_BKPT;
        45751  +    return 0;
        45752  +  }
        45753  +#endif
        45754  +
        45755  +  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
        45756  +  p = memdbFromDbSchema(db, zSchema);
        45757  +  iDb = sqlite3FindDbName(db, zSchema);
        45758  +  if( piSize ) *piSize = -1;
        45759  +  if( iDb<0 ) return 0;
        45760  +  if( p ){
        45761  +    if( piSize ) *piSize = p->sz;
        45762  +    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
        45763  +      pOut = p->aData;
        45764  +    }else{
        45765  +      pOut = sqlite3_malloc64( p->sz );
        45766  +      if( pOut ) memcpy(pOut, p->aData, p->sz);
        45767  +    }
        45768  +    return pOut;
        45769  +  }
        45770  +  pBt = db->aDb[iDb].pBt;
        45771  +  if( pBt==0 ) return 0;
        45772  +  szPage = sqlite3BtreeGetPageSize(pBt);
        45773  +  zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
        45774  +  rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
        45775  +  sqlite3_free(zSql);
        45776  +  if( rc ) return 0;
        45777  +  rc = sqlite3_step(pStmt);
        45778  +  if( rc!=SQLITE_ROW ){
        45779  +    pOut = 0;
        45780  +  }else{
        45781  +    sz = sqlite3_column_int64(pStmt, 0)*szPage;
        45782  +    if( piSize ) *piSize = sz;
        45783  +    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
        45784  +      pOut = 0;
        45785  +    }else{
        45786  +      pOut = sqlite3_malloc64( sz );
        45787  +      if( pOut ){
        45788  +        int nPage = sqlite3_column_int(pStmt, 0);
        45789  +        Pager *pPager = sqlite3BtreePager(pBt);
        45790  +        int pgno;
        45791  +        for(pgno=1; pgno<=nPage; pgno++){
        45792  +          DbPage *pPage = 0;
        45793  +          unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
        45794  +          rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
        45795  +          if( rc==SQLITE_OK ){
        45796  +            memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
        45797  +          }else{
        45798  +            memset(pTo, 0, szPage);
        45799  +          }
        45800  +          sqlite3PagerUnref(pPage);       
        45801  +        }
        45802  +      }
        45803  +    }
        45804  +  }
        45805  +  sqlite3_finalize(pStmt);
        45806  +  return pOut;
        45807  +}
        45808  +
        45809  +/* Convert zSchema to a MemDB and initialize its content.
        45810  +*/
        45811  +SQLITE_API int sqlite3_deserialize(
        45812  +  sqlite3 *db,            /* The database connection */
        45813  +  const char *zSchema,    /* Which DB to reopen with the deserialization */
        45814  +  unsigned char *pData,   /* The serialized database content */
        45815  +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
        45816  +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
        45817  +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
        45818  +){
        45819  +  MemFile *p;
        45820  +  char *zSql;
        45821  +  sqlite3_stmt *pStmt = 0;
        45822  +  int rc;
        45823  +  int iDb;
        45824  +
        45825  +#ifdef SQLITE_ENABLE_API_ARMOR
        45826  +  if( !sqlite3SafetyCheckOk(db) ){
        45827  +    return SQLITE_MISUSE_BKPT;
        45828  +  }
        45829  +  if( szDb<0 ) return SQLITE_MISUSE_BKPT;
        45830  +  if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
        45831  +#endif
        45832  +
        45833  +  sqlite3_mutex_enter(db->mutex);
        45834  +  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
        45835  +  iDb = sqlite3FindDbName(db, zSchema);
        45836  +  if( iDb<0 ){
        45837  +    rc = SQLITE_ERROR;
        45838  +    goto end_deserialize;
        45839  +  }    
        45840  +  zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
        45841  +  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
        45842  +  sqlite3_free(zSql);
        45843  +  if( rc ) goto end_deserialize;
        45844  +  db->init.iDb = (u8)iDb;
        45845  +  db->init.reopenMemdb = 1;
        45846  +  rc = sqlite3_step(pStmt);
        45847  +  db->init.reopenMemdb = 0;
        45848  +  if( rc!=SQLITE_DONE ){
        45849  +    rc = SQLITE_ERROR;
        45850  +    goto end_deserialize;
        45851  +  }
        45852  +  p = memdbFromDbSchema(db, zSchema);
        45853  +  if( p==0 ){
        45854  +    rc = SQLITE_ERROR;
        45855  +  }else{
        45856  +    p->aData = pData;
        45857  +    p->sz = szDb;
        45858  +    p->szMax = szBuf;
        45859  +    p->mFlags = mFlags;
        45860  +    rc = SQLITE_OK;
        45861  +  }
        45862  +
        45863  +end_deserialize:
        45864  +  sqlite3_finalize(pStmt);
        45865  +  sqlite3_mutex_leave(db->mutex);
        45866  +  return rc;
        45867  +}
        45868  +
        45869  +/* 
        45870  +** This routine is called when the extension is loaded.
        45871  +** Register the new VFS.
        45872  +*/
        45873  +SQLITE_PRIVATE int sqlite3MemdbInit(void){
        45874  +  sqlite3_vfs *pLower = sqlite3_vfs_find(0);
        45875  +  int sz = pLower->szOsFile;
        45876  +  memdb_vfs.pAppData = pLower;
        45877  +  /* In all known configurations of SQLite, the size of a default
        45878  +  ** sqlite3_file is greater than the size of a memdb sqlite3_file.
        45879  +  ** Should that ever change, remove the following NEVER() */
        45880  +  if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
        45881  +  memdb_vfs.szOsFile = sz;
        45882  +  return sqlite3_vfs_register(&memdb_vfs, 0);
        45883  +}
        45884  +#endif /* SQLITE_ENABLE_DESERIALIZE */
        45885  +
        45886  +/************** End of memdb.c ***********************************************/
 44714  45887   /************** Begin file bitvec.c ******************************************/
 44715  45888   /*
 44716  45889   ** 2008 February 16
 44717  45890   **
 44718  45891   ** The author disclaims copyright to this source code.  In place of
 44719  45892   ** a legal notice, here is a blessing:
 44720  45893   **
................................................................................
 45555  46728       }
 45556  46729       if( pPg ){
 45557  46730         int rc;
 45558  46731   #ifdef SQLITE_LOG_CACHE_SPILL
 45559  46732         sqlite3_log(SQLITE_FULL, 
 45560  46733                     "spill page %d making room for %d - cache used: %d/%d",
 45561  46734                     pPg->pgno, pgno,
 45562         -                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
        46735  +                  sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
 45563  46736                   numberOfCachePages(pCache));
 45564  46737   #endif
 45565  46738         pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
 45566  46739         rc = pCache->xStress(pCache->pStress, pPg);
 45567  46740         pcacheDump(pCache);
 45568  46741         if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
 45569  46742           return rc;
................................................................................
 48611  49784     int pageSize;               /* Number of bytes in a page */
 48612  49785     Pgno mxPgno;                /* Maximum allowed size of the database */
 48613  49786     i64 journalSizeLimit;       /* Size limit for persistent journal files */
 48614  49787     char *zFilename;            /* Name of the database file */
 48615  49788     char *zJournal;             /* Name of the journal file */
 48616  49789     int (*xBusyHandler)(void*); /* Function to call when busy */
 48617  49790     void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
 48618         -  int aStat[3];               /* Total cache hits, misses and writes */
        49791  +  int aStat[4];               /* Total cache hits, misses, writes, spills */
 48619  49792   #ifdef SQLITE_TEST
 48620  49793     int nRead;                  /* Database pages read */
 48621  49794   #endif
 48622  49795     void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
 48623  49796     int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
 48624  49797   #ifdef SQLITE_HAS_CODEC
 48625  49798     void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
................................................................................
 48639  49812   ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
 48640  49813   ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS 
 48641  49814   ** or CACHE_WRITE to sqlite3_db_status().
 48642  49815   */
 48643  49816   #define PAGER_STAT_HIT   0
 48644  49817   #define PAGER_STAT_MISS  1
 48645  49818   #define PAGER_STAT_WRITE 2
        49819  +#define PAGER_STAT_SPILL 3
 48646  49820   
 48647  49821   /*
 48648  49822   ** The following global variables hold counters used for
 48649  49823   ** testing purposes only.  These variables do not exist in
 48650  49824   ** a non-testing build.  These variables are not thread-safe.
 48651  49825   */
 48652  49826   #ifdef SQLITE_TEST
................................................................................
 49125  50299     assert( isOpen(pPager->fd) );
 49126  50300     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
 49127  50301   #else
 49128  50302     UNUSED_PARAMETER(pPager);
 49129  50303   #endif
 49130  50304   
 49131  50305   #ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
 49132         -  if( dc&SQLITE_IOCAP_BATCH_ATOMIC ){
        50306  +  if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
 49133  50307       return -1;
 49134  50308     }
 49135  50309   #endif
 49136  50310   
 49137  50311   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 49138  50312     {
 49139  50313       int nSector = pPager->sectorSize;
................................................................................
 51615  52789   **   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
 51616  52790   **   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
 51617  52791   **
 51618  52792   ** If the busy-handler callback returns non-zero, the lock is 
 51619  52793   ** retried. If it returns zero, then the SQLITE_BUSY error is
 51620  52794   ** returned to the caller of the pager API function.
 51621  52795   */
 51622         -SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
        52796  +SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
 51623  52797     Pager *pPager,                       /* Pager object */
 51624  52798     int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
 51625  52799     void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
 51626  52800   ){
 51627  52801     pPager->xBusyHandler = xBusyHandler;
 51628  52802     pPager->pBusyHandlerArg = pBusyHandlerArg;
 51629  52803   
................................................................................
 52014  53188     PgHdr *pNext;
 52015  53189     for(p=pPager->pMmapFreelist; p; p=pNext){
 52016  53190       pNext = p->pDirty;
 52017  53191       sqlite3_free(p);
 52018  53192     }
 52019  53193   }
 52020  53194   
        53195  +/* Verify that the database file has not be deleted or renamed out from
        53196  +** under the pager.  Return SQLITE_OK if the database is still where it ought
        53197  +** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
        53198  +** code from sqlite3OsAccess()) if the database has gone missing.
        53199  +*/
        53200  +static int databaseIsUnmoved(Pager *pPager){
        53201  +  int bHasMoved = 0;
        53202  +  int rc;
        53203  +
        53204  +  if( pPager->tempFile ) return SQLITE_OK;
        53205  +  if( pPager->dbSize==0 ) return SQLITE_OK;
        53206  +  assert( pPager->zFilename && pPager->zFilename[0] );
        53207  +  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
        53208  +  if( rc==SQLITE_NOTFOUND ){
        53209  +    /* If the HAS_MOVED file-control is unimplemented, assume that the file
        53210  +    ** has not been moved.  That is the historical behavior of SQLite: prior to
        53211  +    ** version 3.8.3, it never checked */
        53212  +    rc = SQLITE_OK;
        53213  +  }else if( rc==SQLITE_OK && bHasMoved ){
        53214  +    rc = SQLITE_READONLY_DBMOVED;
        53215  +  }
        53216  +  return rc;
        53217  +}
        53218  +
 52021  53219   
 52022  53220   /*
 52023  53221   ** Shutdown the page cache.  Free all memory and close all files.
 52024  53222   **
 52025  53223   ** If a transaction was in progress when this routine is called, that
 52026  53224   ** transaction is rolled back.  All outstanding pages are invalidated
 52027  53225   ** and their memory is freed.  Any attempt to use a page associated
................................................................................
 52030  53228   **
 52031  53229   ** This function always succeeds. If a transaction is active an attempt
 52032  53230   ** is made to roll it back. If an error occurs during the rollback 
 52033  53231   ** a hot journal may be left in the filesystem but no error is returned
 52034  53232   ** to the caller.
 52035  53233   */
 52036  53234   SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
 52037         -  u8 *pTmp = (u8 *)pPager->pTmpSpace;
 52038         -
        53235  +  u8 *pTmp = (u8*)pPager->pTmpSpace;
 52039  53236     assert( db || pagerUseWal(pPager)==0 );
 52040  53237     assert( assert_pager_state(pPager) );
 52041  53238     disable_simulated_io_errors();
 52042  53239     sqlite3BeginBenignMalloc();
 52043  53240     pagerFreeMapHdrs(pPager);
 52044  53241     /* pPager->errCode = 0; */
 52045  53242     pPager->exclusiveMode = 0;
 52046  53243   #ifndef SQLITE_OMIT_WAL
 52047         -  assert( db || pPager->pWal==0 );
 52048         -  sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,
 52049         -      (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp)
 52050         -  );
 52051         -  pPager->pWal = 0;
        53244  +  {
        53245  +    u8 *a = 0;
        53246  +    assert( db || pPager->pWal==0 );
        53247  +    if( db && 0==(db->flags & SQLITE_NoCkptOnClose) 
        53248  +     && SQLITE_OK==databaseIsUnmoved(pPager)
        53249  +    ){
        53250  +      a = pTmp;
        53251  +    }
        53252  +    sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
        53253  +    pPager->pWal = 0;
        53254  +  }
 52052  53255   #endif
 52053  53256     pager_reset(pPager);
 52054  53257     if( MEMDB ){
 52055  53258       pager_unlock(pPager);
 52056  53259     }else{
 52057  53260       /* If it is open, sync the journal file before calling UnlockAndRollback.
 52058  53261       ** If this is not done, then an unsynced portion of the open journal 
................................................................................
 52501  53704     if( pPager->doNotSpill
 52502  53705      && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
 52503  53706         || (pPg->flags & PGHDR_NEED_SYNC)!=0)
 52504  53707     ){
 52505  53708       return SQLITE_OK;
 52506  53709     }
 52507  53710   
        53711  +  pPager->aStat[PAGER_STAT_SPILL]++;
 52508  53712     pPg->pDirty = 0;
 52509  53713     if( pagerUseWal(pPager) ){
 52510  53714       /* Write a single frame for this page to the log. */
 52511  53715       rc = subjournalPageIfRequired(pPg); 
 52512  53716       if( rc==SQLITE_OK ){
 52513  53717         rc = pagerWalFrames(pPager, pPg, 0, 0);
 52514  53718       }
................................................................................
 52606  53810     void (*xReinit)(DbPage*) /* Function to reinitialize pages */
 52607  53811   ){
 52608  53812     u8 *pPtr;
 52609  53813     Pager *pPager = 0;       /* Pager object to allocate and return */
 52610  53814     int rc = SQLITE_OK;      /* Return code */
 52611  53815     int tempFile = 0;        /* True for temp files (incl. in-memory files) */
 52612  53816     int memDb = 0;           /* True if this is an in-memory file */
        53817  +#ifdef SQLITE_ENABLE_DESERIALIZE
        53818  +  int memJM = 0;           /* Memory journal mode */
        53819  +#else
        53820  +# define memJM 0
        53821  +#endif
 52613  53822     int readOnly = 0;        /* True if this is a read-only file */
 52614  53823     int journalFileSize;     /* Bytes to allocate for each journal fd */
 52615  53824     char *zPathname = 0;     /* Full path to database file */
 52616  53825     int nPathname = 0;       /* Number of bytes in zPathname */
 52617  53826     int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
 52618  53827     int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
 52619  53828     u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
................................................................................
 52733  53942   
 52734  53943     /* Open the pager file.
 52735  53944     */
 52736  53945     if( zFilename && zFilename[0] ){
 52737  53946       int fout = 0;                    /* VFS flags returned by xOpen() */
 52738  53947       rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
 52739  53948       assert( !memDb );
 52740         -    readOnly = (fout&SQLITE_OPEN_READONLY);
        53949  +#ifdef SQLITE_ENABLE_DESERIALIZE
        53950  +    memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
        53951  +#endif
        53952  +    readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
 52741  53953   
 52742  53954       /* If the file was successfully opened for read/write access,
 52743  53955       ** choose a default page size in case we have to create the
 52744  53956       ** database file. The default page size is the maximum of:
 52745  53957       **
 52746  53958       **    + SQLITE_DEFAULT_PAGE_SIZE,
 52747  53959       **    + The value returned by sqlite3OsSectorSize()
................................................................................
 52864  54076     /* pPager->pLast = 0; */
 52865  54077     pPager->nExtra = (u16)nExtra;
 52866  54078     pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
 52867  54079     assert( isOpen(pPager->fd) || tempFile );
 52868  54080     setSectorSize(pPager);
 52869  54081     if( !useJournal ){
 52870  54082       pPager->journalMode = PAGER_JOURNALMODE_OFF;
 52871         -  }else if( memDb ){
        54083  +  }else if( memDb || memJM ){
 52872  54084       pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
 52873  54085     }
 52874  54086     /* pPager->xBusyHandler = 0; */
 52875  54087     /* pPager->pBusyHandlerArg = 0; */
 52876  54088     pPager->xReiniter = xReinit;
 52877  54089     setGetterMethod(pPager);
 52878  54090     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
................................................................................
 52879  54091     /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
 52880  54092   
 52881  54093     *ppPager = pPager;
 52882  54094     return SQLITE_OK;
 52883  54095   }
 52884  54096   
 52885  54097   
 52886         -/* Verify that the database file has not be deleted or renamed out from
 52887         -** under the pager.  Return SQLITE_OK if the database is still were it ought
 52888         -** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
 52889         -** code from sqlite3OsAccess()) if the database has gone missing.
 52890         -*/
 52891         -static int databaseIsUnmoved(Pager *pPager){
 52892         -  int bHasMoved = 0;
 52893         -  int rc;
 52894         -
 52895         -  if( pPager->tempFile ) return SQLITE_OK;
 52896         -  if( pPager->dbSize==0 ) return SQLITE_OK;
 52897         -  assert( pPager->zFilename && pPager->zFilename[0] );
 52898         -  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
 52899         -  if( rc==SQLITE_NOTFOUND ){
 52900         -    /* If the HAS_MOVED file-control is unimplemented, assume that the file
 52901         -    ** has not been moved.  That is the historical behavior of SQLite: prior to
 52902         -    ** version 3.8.3, it never checked */
 52903         -    rc = SQLITE_OK;
 52904         -  }else if( rc==SQLITE_OK && bHasMoved ){
 52905         -    rc = SQLITE_READONLY_DBMOVED;
 52906         -  }
 52907         -  return rc;
 52908         -}
 52909         -
 52910  54098   
 52911  54099   /*
 52912  54100   ** This function is called after transitioning from PAGER_UNLOCK to
 52913  54101   ** PAGER_SHARED state. It tests if there is a hot journal present in
 52914  54102   ** the file-system for the given pager. A hot journal is one that 
 52915  54103   ** needs to be played back. According to this function, a hot-journal
 52916  54104   ** file exists if the following criteria are met:
................................................................................
 53590  54778   }
 53591  54779   SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
 53592  54780     Pager *pPager;
 53593  54781     assert( pPg!=0 );
 53594  54782     assert( pPg->pgno==1 );
 53595  54783     assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
 53596  54784     pPager = pPg->pPager;
        54785  +  sqlite3PagerResetLockTimeout(pPager);
 53597  54786     sqlite3PcacheRelease(pPg);
 53598  54787     pagerUnlockIfUnused(pPager);
 53599  54788   }
 53600  54789   
 53601  54790   /*
 53602  54791   ** This function is called at the start of every write transaction.
 53603  54792   ** There must already be a RESERVED or EXCLUSIVE lock on the database 
................................................................................
 54411  55600           rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
 54412  55601           if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 54413  55602         }
 54414  55603         rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
 54415  55604         if( bBatch ){
 54416  55605           if( rc==SQLITE_OK ){
 54417  55606             rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
 54418         -        }else{
 54419         -          sqlite3OsFileControl(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
        55607  +        }
        55608  +        if( rc!=SQLITE_OK ){
        55609  +          sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
 54420  55610           }
 54421  55611         }
 54422  55612   
 54423  55613         if( rc!=SQLITE_OK ){
 54424  55614           assert( rc!=SQLITE_IOERR_BLOCKED );
 54425  55615           goto commit_phase_one_exit;
 54426  55616         }
................................................................................
 54636  55826     a[9] = pPager->nRead;
 54637  55827     a[10] = pPager->aStat[PAGER_STAT_WRITE];
 54638  55828     return a;
 54639  55829   }
 54640  55830   #endif
 54641  55831   
 54642  55832   /*
 54643         -** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
 54644         -** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
        55833  +** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
        55834  +** or _WRITE+1.  The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
        55835  +** of SQLITE_DBSTATUS_CACHE_SPILL.  The _SPILL case is not contiguous because
        55836  +** it was added later.
        55837  +**
        55838  +** Before returning, *pnVal is incremented by the
 54645  55839   ** current cache hit or miss count, according to the value of eStat. If the 
 54646  55840   ** reset parameter is non-zero, the cache hit or miss count is zeroed before 
 54647  55841   ** returning.
 54648  55842   */
 54649  55843   SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
 54650  55844   
 54651  55845     assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
 54652  55846          || eStat==SQLITE_DBSTATUS_CACHE_MISS
 54653  55847          || eStat==SQLITE_DBSTATUS_CACHE_WRITE
        55848  +       || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
 54654  55849     );
 54655  55850   
 54656  55851     assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
 54657  55852     assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
 54658         -  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
        55853  +  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
        55854  +           && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
 54659  55855   
 54660         -  *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
        55856  +  eStat -= SQLITE_DBSTATUS_CACHE_HIT;
        55857  +  *pnVal += pPager->aStat[eStat];
 54661  55858     if( reset ){
 54662         -    pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
        55859  +    pPager->aStat[eStat] = 0;
 54663  55860     }
 54664  55861   }
 54665  55862   
 54666  55863   /*
 54667  55864   ** Return true if this is an in-memory or temp-file backed pager.
 54668  55865   */
 54669  55866   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
................................................................................
 54858  56055   ** Return the file handle for the database file associated
 54859  56056   ** with the pager.  This might return NULL if the file has
 54860  56057   ** not yet been opened.
 54861  56058   */
 54862  56059   SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
 54863  56060     return pPager->fd;
 54864  56061   }
        56062  +
        56063  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        56064  +/*
        56065  +** Reset the lock timeout for pager.
        56066  +*/
        56067  +SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){
        56068  +  if( isOpen(pPager->fd) ){
        56069  +    int x = 0;
        56070  +    sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x);
        56071  +  }
        56072  +}
        56073  +#endif
 54865  56074   
 54866  56075   /*
 54867  56076   ** Return the file handle for the journal file (if it exists).
 54868  56077   ** This will be either the rollback journal or the WAL file.
 54869  56078   */
 54870  56079   SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
 54871  56080   #if SQLITE_OMIT_WAL
................................................................................
 55319  56528     if( pPager->pWal ){
 55320  56529       rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
 55321  56530           (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
 55322  56531           pPager->pBusyHandlerArg,
 55323  56532           pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
 55324  56533           pnLog, pnCkpt
 55325  56534       );
        56535  +    sqlite3PagerResetLockTimeout(pPager);
 55326  56536     }
 55327  56537     return rc;
 55328  56538   }
 55329  56539   
 55330  56540   SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
 55331  56541     return sqlite3WalCallback(pPager->pWal);
 55332  56542   }
................................................................................
 56101  57311   ** so.  It is safe to enlarge the wal-index if pWal->writeLock is true
 56102  57312   ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
 56103  57313   **
 56104  57314   ** If this call is successful, *ppPage is set to point to the wal-index
 56105  57315   ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
 56106  57316   ** then an SQLite error code is returned and *ppPage is set to 0.
 56107  57317   */
 56108         -static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
        57318  +static SQLITE_NOINLINE int walIndexPageRealloc(
        57319  +  Wal *pWal,               /* The WAL context */
        57320  +  int iPage,               /* The page we seek */
        57321  +  volatile u32 **ppPage    /* Write the page pointer here */
        57322  +){
 56109  57323     int rc = SQLITE_OK;
 56110  57324   
 56111  57325     /* Enlarge the pWal->apWiData[] array if required */
 56112  57326     if( pWal->nWiData<=iPage ){
 56113  57327       int nByte = sizeof(u32*)*(iPage+1);
 56114  57328       volatile u32 **apNew;
 56115  57329       apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
................................................................................
 56120  57334       memset((void*)&apNew[pWal->nWiData], 0,
 56121  57335              sizeof(u32*)*(iPage+1-pWal->nWiData));
 56122  57336       pWal->apWiData = apNew;
 56123  57337       pWal->nWiData = iPage+1;
 56124  57338     }
 56125  57339   
 56126  57340     /* Request a pointer to the required page from the VFS */
 56127         -  if( pWal->apWiData[iPage]==0 ){
 56128         -    if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
 56129         -      pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
 56130         -      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
 56131         -    }else{
 56132         -      rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
 56133         -          pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
 56134         -      );
 56135         -      assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
 56136         -      testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
 56137         -      if( (rc&0xff)==SQLITE_READONLY ){
 56138         -        pWal->readOnly |= WAL_SHM_RDONLY;
 56139         -        if( rc==SQLITE_READONLY ){
 56140         -          rc = SQLITE_OK;
 56141         -        }
        57341  +  assert( pWal->apWiData[iPage]==0 );
        57342  +  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
        57343  +    pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
        57344  +    if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
        57345  +  }else{
        57346  +    rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
        57347  +        pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
        57348  +    );
        57349  +    assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
        57350  +    testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
        57351  +    if( (rc&0xff)==SQLITE_READONLY ){
        57352  +      pWal->readOnly |= WAL_SHM_RDONLY;
        57353  +      if( rc==SQLITE_READONLY ){
        57354  +        rc = SQLITE_OK;
 56142  57355         }
 56143  57356       }
 56144  57357     }
 56145  57358   
 56146  57359     *ppPage = pWal->apWiData[iPage];
 56147  57360     assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
 56148  57361     return rc;
        57362  +}
        57363  +static int walIndexPage(
        57364  +  Wal *pWal,               /* The WAL context */
        57365  +  int iPage,               /* The page we seek */
        57366  +  volatile u32 **ppPage    /* Write the page pointer here */
        57367  +){
        57368  +  if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
        57369  +    return walIndexPageRealloc(pWal, iPage, ppPage);
        57370  +  }
        57371  +  return SQLITE_OK;
 56149  57372   }
 56150  57373   
 56151  57374   /*
 56152  57375   ** Return a pointer to the WalCkptInfo structure in the wal-index.
 56153  57376   */
 56154  57377   static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
 56155  57378     assert( pWal->nWiData>0 && pWal->apWiData[0] );
................................................................................
 57119  58342   */
 57120  58343   static void walIteratorFree(WalIterator *p){
 57121  58344     sqlite3_free(p);
 57122  58345   }
 57123  58346   
 57124  58347   /*
 57125  58348   ** Construct a WalInterator object that can be used to loop over all 
 57126         -** pages in the WAL in ascending order. The caller must hold the checkpoint
 57127         -** lock.
        58349  +** pages in the WAL following frame nBackfill in ascending order. Frames
        58350  +** nBackfill or earlier may be included - excluding them is an optimization
        58351  +** only. The caller must hold the checkpoint lock.
 57128  58352   **
 57129  58353   ** On success, make *pp point to the newly allocated WalInterator object
 57130  58354   ** return SQLITE_OK. Otherwise, return an error code. If this routine
 57131  58355   ** returns an error, the value of *pp is undefined.
 57132  58356   **
 57133  58357   ** The calling routine should invoke walIteratorFree() to destroy the
 57134  58358   ** WalIterator object when it has finished with it.
 57135  58359   */
 57136         -static int walIteratorInit(Wal *pWal, WalIterator **pp){
        58360  +static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
 57137  58361     WalIterator *p;                 /* Return value */
 57138  58362     int nSegment;                   /* Number of segments to merge */
 57139  58363     u32 iLast;                      /* Last frame in log */
 57140  58364     int nByte;                      /* Number of bytes to allocate */
 57141  58365     int i;                          /* Iterator variable */
 57142  58366     ht_slot *aTmp;                  /* Temp space used by merge-sort */
 57143  58367     int rc = SQLITE_OK;             /* Return Code */
................................................................................
 57166  58390     aTmp = (ht_slot *)sqlite3_malloc64(
 57167  58391         sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
 57168  58392     );
 57169  58393     if( !aTmp ){
 57170  58394       rc = SQLITE_NOMEM_BKPT;
 57171  58395     }
 57172  58396   
 57173         -  for(i=0; rc==SQLITE_OK && i<nSegment; i++){
        58397  +  for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
 57174  58398       volatile ht_slot *aHash;
 57175  58399       u32 iZero;
 57176  58400       volatile u32 *aPgno;
 57177  58401   
 57178  58402       rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
 57179  58403       if( rc==SQLITE_OK ){
 57180  58404         int j;                      /* Counter variable */
................................................................................
 57200  58424         p->aSegment[i].aPgno = (u32 *)aPgno;
 57201  58425       }
 57202  58426     }
 57203  58427     sqlite3_free(aTmp);
 57204  58428   
 57205  58429     if( rc!=SQLITE_OK ){
 57206  58430       walIteratorFree(p);
        58431  +    p = 0;
 57207  58432     }
 57208  58433     *pp = p;
 57209  58434     return rc;
 57210  58435   }
 57211  58436   
 57212  58437   /*
 57213  58438   ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
................................................................................
 57322  58547   
 57323  58548     szPage = walPagesize(pWal);
 57324  58549     testcase( szPage<=32768 );
 57325  58550     testcase( szPage>=65536 );
 57326  58551     pInfo = walCkptInfo(pWal);
 57327  58552     if( pInfo->nBackfill<pWal->hdr.mxFrame ){
 57328  58553   
 57329         -    /* Allocate the iterator */
 57330         -    rc = walIteratorInit(pWal, &pIter);
 57331         -    if( rc!=SQLITE_OK ){
 57332         -      return rc;
 57333         -    }
 57334         -    assert( pIter );
 57335         -
 57336  58554       /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
 57337  58555       ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
 57338  58556       assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
 57339  58557   
 57340  58558       /* Compute in mxSafeFrame the index of the last frame of the WAL that is
 57341  58559       ** safe to write into the database.  Frames beyond mxSafeFrame might
 57342  58560       ** overwrite database pages that are in use by active readers and thus
................................................................................
 57365  58583             xBusy = 0;
 57366  58584           }else{
 57367  58585             goto walcheckpoint_out;
 57368  58586           }
 57369  58587         }
 57370  58588       }
 57371  58589   
 57372         -    if( pInfo->nBackfill<mxSafeFrame
        58590  +    /* Allocate the iterator */
        58591  +    if( pInfo->nBackfill<mxSafeFrame ){
        58592  +      rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
        58593  +      assert( rc==SQLITE_OK || pIter==0 );
        58594  +    }
        58595  +
        58596  +    if( pIter
 57373  58597        && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
 57374  58598       ){
 57375  58599         i64 nSize;                    /* Current size of database file */
 57376  58600         u32 nBackfill = pInfo->nBackfill;
 57377  58601   
 57378  58602         pInfo->nBackfillAttempted = mxSafeFrame;
 57379  58603   
................................................................................
 58415  59639     **     This condition filters out normal hash-table collisions.
 58416  59640     **
 58417  59641     **   (iFrame<=iLast): 
 58418  59642     **     This condition filters out entries that were added to the hash
 58419  59643     **     table after the current read-transaction had started.
 58420  59644     */
 58421  59645     iMinHash = walFramePage(pWal->minFrame);
 58422         -  for(iHash=walFramePage(iLast); iHash>=iMinHash && iRead==0; iHash--){
        59646  +  for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
 58423  59647       volatile ht_slot *aHash;      /* Pointer to hash table */
 58424  59648       volatile u32 *aPgno;          /* Pointer to array of page numbers */
 58425  59649       u32 iZero;                    /* Frame number corresponding to aPgno[0] */
 58426  59650       int iKey;                     /* Hash slot index */
 58427  59651       int nCollide;                 /* Number of hash collisions remaining */
 58428  59652       int rc;                       /* Error code */
 58429  59653   
................................................................................
 58438  59662           assert( iFrame>iRead || CORRUPT_DB );
 58439  59663           iRead = iFrame;
 58440  59664         }
 58441  59665         if( (nCollide--)==0 ){
 58442  59666           return SQLITE_CORRUPT_BKPT;
 58443  59667         }
 58444  59668       }
        59669  +    if( iRead ) break;
 58445  59670     }
 58446  59671   
 58447  59672   #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 58448  59673     /* If expensive assert() statements are available, do a linear search
 58449  59674     ** of the wal-index file content. Make sure the results agree with the
 58450  59675     ** result obtained using the hash indexes above.  */
 58451  59676     {
................................................................................
 59852  61077   **    eState==FAULT:                   Cursor fault with skipNext as error code.
 59853  61078   */
 59854  61079   struct BtCursor {
 59855  61080     u8 eState;                /* One of the CURSOR_XXX constants (see below) */
 59856  61081     u8 curFlags;              /* zero or more BTCF_* flags defined below */
 59857  61082     u8 curPagerFlags;         /* Flags to send to sqlite3PagerGet() */
 59858  61083     u8 hints;                 /* As configured by CursorSetHints() */
 59859         -  int nOvflAlloc;           /* Allocated size of aOverflow[] array */
 59860         -  Btree *pBtree;            /* The Btree to which this cursor belongs */
 59861         -  BtShared *pBt;            /* The BtShared this cursor points to */
 59862         -  BtCursor *pNext;          /* Forms a linked list of all cursors */
 59863         -  Pgno *aOverflow;          /* Cache of overflow page locations */
 59864         -  CellInfo info;            /* A parse of the cell we are pointing at */
 59865         -  i64 nKey;                 /* Size of pKey, or last integer key */
 59866         -  void *pKey;               /* Saved key that was cursor last known position */
 59867         -  Pgno pgnoRoot;            /* The root page of this tree */
 59868  61084     int skipNext;    /* Prev() is noop if negative. Next() is noop if positive.
 59869  61085                      ** Error code if eState==CURSOR_FAULT */
        61086  +  Btree *pBtree;            /* The Btree to which this cursor belongs */
        61087  +  Pgno *aOverflow;          /* Cache of overflow page locations */
        61088  +  void *pKey;               /* Saved key that was cursor last known position */
 59870  61089     /* All fields above are zeroed when the cursor is allocated.  See
 59871  61090     ** sqlite3BtreeCursorZero().  Fields that follow must be manually
 59872  61091     ** initialized. */
        61092  +#define BTCURSOR_FIRST_UNINIT pBt   /* Name of first uninitialized field */
        61093  +  BtShared *pBt;            /* The BtShared this cursor points to */
        61094  +  BtCursor *pNext;          /* Forms a linked list of all cursors */
        61095  +  CellInfo info;            /* A parse of the cell we are pointing at */
        61096  +  i64 nKey;                 /* Size of pKey, or last integer key */
        61097  +  Pgno pgnoRoot;            /* The root page of this tree */
 59873  61098     i8 iPage;                 /* Index of current page in apPage */
 59874  61099     u8 curIntKey;             /* Value of apPage[0]->intKey */
 59875  61100     u16 ix;                   /* Current index for apPage[iPage] */
 59876  61101     u16 aiIdx[BTCURSOR_MAX_DEPTH-1];     /* Current index in apPage[i] */
 59877  61102     struct KeyInfo *pKeyInfo;            /* Arg passed to comparison function */
 59878  61103     MemPage *pPage;                        /* Current page */
 59879  61104     MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
................................................................................
 59915  61140   ** CURSOR_FAULT:
 59916  61141   **   An unrecoverable error (an I/O error or a malloc failure) has occurred
 59917  61142   **   on a different connection that shares the BtShared cache with this
 59918  61143   **   cursor.  The error has left the cache in an inconsistent state.
 59919  61144   **   Do nothing else with this cursor.  Any attempt to use the cursor
 59920  61145   **   should return the error code stored in BtCursor.skipNext
 59921  61146   */
 59922         -#define CURSOR_INVALID           0
 59923         -#define CURSOR_VALID             1
        61147  +#define CURSOR_VALID             0
        61148  +#define CURSOR_INVALID           1
 59924  61149   #define CURSOR_SKIPNEXT          2
 59925  61150   #define CURSOR_REQUIRESEEK       3
 59926  61151   #define CURSOR_FAULT             4
 59927  61152   
 59928  61153   /* 
 59929  61154   ** The database page the PENDING_BYTE occupies. This page is never used.
 59930  61155   */
................................................................................
 60472  61697   ** If SQLITE_DEBUG is not defined, then this macro is equivalent to
 60473  61698   ** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
 60474  61699   ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
 60475  61700   ** with the page number and filename associated with the (MemPage*).
 60476  61701   */
 60477  61702   #ifdef SQLITE_DEBUG
 60478  61703   int corruptPageError(int lineno, MemPage *p){
 60479         -  char *zMsg = sqlite3_mprintf("database corruption page %d of %s",
        61704  +  char *zMsg;
        61705  +  sqlite3BeginBenignMalloc();
        61706  +  zMsg = sqlite3_mprintf("database corruption page %d of %s",
 60480  61707         (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
 60481  61708     );
        61709  +  sqlite3EndBenignMalloc();
 60482  61710     if( zMsg ){
 60483  61711       sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
 60484  61712     }
 60485  61713     sqlite3_free(zMsg);
 60486  61714     return SQLITE_CORRUPT_BKPT;
 60487  61715   }
 60488  61716   # define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
................................................................................
 62577  63805   /*
 62578  63806   ** Invoke the busy handler for a btree.
 62579  63807   */
 62580  63808   static int btreeInvokeBusyHandler(void *pArg){
 62581  63809     BtShared *pBt = (BtShared*)pArg;
 62582  63810     assert( pBt->db );
 62583  63811     assert( sqlite3_mutex_held(pBt->db->mutex) );
 62584         -  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
        63812  +  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
        63813  +                                  sqlite3PagerFile(pBt->pPager));
 62585  63814   }
 62586  63815   
 62587  63816   /*
 62588  63817   ** Open a database file.
 62589  63818   ** 
 62590  63819   ** zFilename is the name of the database file.  If zFilename is NULL
 62591  63820   ** then an ephemeral database is created.  The ephemeral database might
................................................................................
 62755  63984         rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
 62756  63985       }
 62757  63986       if( rc!=SQLITE_OK ){
 62758  63987         goto btree_open_out;
 62759  63988       }
 62760  63989       pBt->openFlags = (u8)flags;
 62761  63990       pBt->db = db;
 62762         -    sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
        63991  +    sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
 62763  63992       p->pBt = pBt;
 62764  63993     
 62765  63994       pBt->pCursor = 0;
 62766  63995       pBt->pPage1 = 0;
 62767  63996       if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
 62768  63997   #if defined(SQLITE_SECURE_DELETE)
 62769  63998       pBt->btsFlags |= BTS_SECURE_DELETE;
................................................................................
 63718  64947       }
 63719  64948     
 63720  64949       if( rc!=SQLITE_OK ){
 63721  64950         unlockBtreeIfUnused(pBt);
 63722  64951       }
 63723  64952     }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
 63724  64953             btreeInvokeBusyHandler(pBt) );
        64954  +  sqlite3PagerResetLockTimeout(pBt->pPager);
 63725  64955   
 63726  64956     if( rc==SQLITE_OK ){
 63727  64957       if( p->inTrans==TRANS_NONE ){
 63728  64958         pBt->nTransaction++;
 63729  64959   #ifndef SQLITE_OMIT_SHARED_CACHE
 63730  64960         if( p->sharable ){
 63731  64961           assert( p->lock.pBtree==p && p->lock.iTable==1 );
................................................................................
 64691  65921   **
 64692  65922   ** The simple approach here would be to memset() the entire object
 64693  65923   ** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
 64694  65924   ** do not need to be zeroed and they are large, so we can save a lot
 64695  65925   ** of run-time by skipping the initialization of those elements.
 64696  65926   */
 64697  65927   SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
 64698         -  memset(p, 0, offsetof(BtCursor, iPage));
        65928  +  memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
 64699  65929   }
 64700  65930   
 64701  65931   /*
 64702  65932   ** Close a cursor.  The read lock on the database file is released
 64703  65933   ** when the last cursor is closed.
 64704  65934   */
 64705  65935   SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
................................................................................
 64734  65964   ** BtCursor.info structure.  If it is not already valid, call
 64735  65965   ** btreeParseCell() to fill it in.
 64736  65966   **
 64737  65967   ** BtCursor.info is a cache of the information in the current cell.
 64738  65968   ** Using this cache reduces the number of calls to btreeParseCell().
 64739  65969   */
 64740  65970   #ifndef NDEBUG
        65971  +  static int cellInfoEqual(CellInfo *a, CellInfo *b){
        65972  +    if( a->nKey!=b->nKey ) return 0;
        65973  +    if( a->pPayload!=b->pPayload ) return 0;
        65974  +    if( a->nPayload!=b->nPayload ) return 0;
        65975  +    if( a->nLocal!=b->nLocal ) return 0;
        65976  +    if( a->nSize!=b->nSize ) return 0;
        65977  +    return 1;
        65978  +  }
 64741  65979     static void assertCellInfo(BtCursor *pCur){
 64742  65980       CellInfo info;
 64743  65981       memset(&info, 0, sizeof(info));
 64744  65982       btreeParseCell(pCur->pPage, pCur->ix, &info);
 64745         -    assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
        65983  +    assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
 64746  65984     }
 64747  65985   #else
 64748  65986     #define assertCellInfo(x)
 64749  65987   #endif
 64750  65988   static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
 64751  65989     if( pCur->info.nSize==0 ){
 64752  65990       pCur->curFlags |= BTCF_ValidNKey;
................................................................................
 65014  66252       ** The aOverflow[] array is sized at one entry for each overflow page
 65015  66253       ** in the overflow chain. The page number of the first overflow page is
 65016  66254       ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
 65017  66255       ** means "not yet known" (the cache is lazily populated).
 65018  66256       */
 65019  66257       if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
 65020  66258         int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
 65021         -      if( nOvfl>pCur->nOvflAlloc ){
        66259  +      if( pCur->aOverflow==0
        66260  +       || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
        66261  +      ){
 65022  66262           Pgno *aNew = (Pgno*)sqlite3Realloc(
 65023  66263               pCur->aOverflow, nOvfl*2*sizeof(Pgno)
 65024  66264           );
 65025  66265           if( aNew==0 ){
 65026  66266             return SQLITE_NOMEM_BKPT;
 65027  66267           }else{
 65028         -          pCur->nOvflAlloc = nOvfl*2;
 65029  66268             pCur->aOverflow = aNew;
 65030  66269           }
 65031  66270         }
 65032  66271         memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
 65033  66272         pCur->curFlags |= BTCF_ValidOvfl;
 65034  66273       }else{
 65035  66274         /* If the overflow page-list cache has been allocated and the
................................................................................
 66535  67774   static void freePage(MemPage *pPage, int *pRC){
 66536  67775     if( (*pRC)==SQLITE_OK ){
 66537  67776       *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
 66538  67777     }
 66539  67778   }
 66540  67779   
 66541  67780   /*
 66542         -** Free any overflow pages associated with the given Cell.  Write the
 66543         -** local Cell size (the number of bytes on the original page, omitting
 66544         -** overflow) into *pnSize.
        67781  +** Free any overflow pages associated with the given Cell.  Store
        67782  +** size information about the cell in pInfo.
 66545  67783   */
 66546  67784   static int clearCell(
 66547  67785     MemPage *pPage,          /* The page that contains the Cell */
 66548  67786     unsigned char *pCell,    /* First byte of the Cell */
 66549  67787     CellInfo *pInfo          /* Size information about the cell */
 66550  67788   ){
 66551  67789     BtShared *pBt;
................................................................................
 67741  68979       */
 67742  68980       if( pOld->aData[0]!=apOld[0]->aData[0] ){
 67743  68981         rc = SQLITE_CORRUPT_BKPT;
 67744  68982         goto balance_cleanup;
 67745  68983       }
 67746  68984   
 67747  68985       /* Load b.apCell[] with pointers to all cells in pOld.  If pOld
 67748         -    ** constains overflow cells, include them in the b.apCell[] array
        68986  +    ** contains overflow cells, include them in the b.apCell[] array
 67749  68987       ** in the correct spot.
 67750  68988       **
 67751  68989       ** Note that when there are multiple overflow cells, it is always the
 67752  68990       ** case that they are sequential and adjacent.  This invariant arises
 67753  68991       ** because multiple overflows can only occurs when inserting divider
 67754  68992       ** cells into a parent on a prior balance, and divider cells are always
 67755  68993       ** adjacent and are inserted in order.  There is an assert() tagged
................................................................................
 71226  72464         ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
 71227  72465       );
 71228  72466     }
 71229  72467     return 1;
 71230  72468   }
 71231  72469   #endif
 71232  72470   
        72471  +#ifdef SQLITE_DEBUG
        72472  +/*
        72473  +** Check that string value of pMem agrees with its integer or real value.
        72474  +**
        72475  +** A single int or real value always converts to the same strings.  But
        72476  +** many different strings can be converted into the same int or real.
        72477  +** If a table contains a numeric value and an index is based on the
        72478  +** corresponding string value, then it is important that the string be
        72479  +** derived from the numeric value, not the other way around, to ensure
        72480  +** that the index and table are consistent.  See ticket
        72481  +** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
        72482  +** an example.
        72483  +**
        72484  +** This routine looks at pMem to verify that if it has both a numeric
        72485  +** representation and a string representation then the string rep has
        72486  +** been derived from the numeric and not the other way around.  It returns
        72487  +** true if everything is ok and false if there is a problem.
        72488  +**
        72489  +** This routine is for use inside of assert() statements only.
        72490  +*/
        72491  +SQLITE_PRIVATE int sqlite3VdbeMemConsistentDualRep(Mem *p){
        72492  +  char zBuf[100];
        72493  +  char *z;
        72494  +  int i, j, incr;
        72495  +  if( (p->flags & MEM_Str)==0 ) return 1;
        72496  +  if( (p->flags & (MEM_Int|MEM_Real))==0 ) return 1;
        72497  +  if( p->flags & MEM_Int ){
        72498  +    sqlite3_snprintf(sizeof(zBuf),zBuf,"%lld",p->u.i);
        72499  +  }else{
        72500  +    sqlite3_snprintf(sizeof(zBuf),zBuf,"%!.15g",p->u.r);
        72501  +  }
        72502  +  z = p->z;
        72503  +  i = j = 0;
        72504  +  incr = 1;
        72505  +  if( p->enc!=SQLITE_UTF8 ){
        72506  +    incr = 2;
        72507  +    if( p->enc==SQLITE_UTF16BE ) z++;
        72508  +  }
        72509  +  while( zBuf[j] ){
        72510  +    if( zBuf[j++]!=z[i] ) return 0;
        72511  +    i += incr;
        72512  +  }
        72513  +  return 1;
        72514  +}
        72515  +#endif /* SQLITE_DEBUG */
 71233  72516   
 71234  72517   /*
 71235  72518   ** If pMem is an object with a valid string representation, this routine
 71236  72519   ** ensures the internal encoding for the string representation is
 71237  72520   ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
 71238  72521   **
 71239  72522   ** If pMem is not a string object, or the encoding of the string
................................................................................
 71659  72942     }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
 71660  72943       return memRealValue(pMem);
 71661  72944     }else{
 71662  72945       /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 71663  72946       return (double)0;
 71664  72947     }
 71665  72948   }
        72949  +
        72950  +/*
        72951  +** Return 1 if pMem represents true, and return 0 if pMem represents false.
        72952  +** Return the value ifNull if pMem is NULL.  
        72953  +*/
        72954  +SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
        72955  +  if( pMem->flags & MEM_Int ) return pMem->u.i!=0;
        72956  +  if( pMem->flags & MEM_Null ) return ifNull;
        72957  +  return sqlite3VdbeRealValue(pMem)!=0.0;
        72958  +}
 71666  72959   
 71667  72960   /*
 71668  72961   ** The MEM structure is already a MEM_Real.  Try to also make it a
 71669  72962   ** MEM_Int if we can.
 71670  72963   */
 71671  72964   SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
 71672  72965     i64 ix;
................................................................................
 71714  73007     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 71715  73008     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 71716  73009   
 71717  73010     pMem->u.r = sqlite3VdbeRealValue(pMem);
 71718  73011     MemSetTypeFlag(pMem, MEM_Real);
 71719  73012     return SQLITE_OK;
 71720  73013   }
        73014  +
        73015  +/* Compare a floating point value to an integer.  Return true if the two
        73016  +** values are the same within the precision of the floating point value.
        73017  +**
        73018  +** For some versions of GCC on 32-bit machines, if you do the more obvious
        73019  +** comparison of "r1==(double)i" you sometimes get an answer of false even
        73020  +** though the r1 and (double)i values are bit-for-bit the same.
        73021  +*/
        73022  +static int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
        73023  +  double r2 = (double)i;
        73024  +  return memcmp(&r1, &r2, sizeof(r1))==0;
        73025  +}
 71721  73026   
 71722  73027   /*
 71723  73028   ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
 71724  73029   ** Invalidate any prior representations.
 71725  73030   **
 71726  73031   ** Every effort is made to force the conversion, even if the input
 71727  73032   ** is a string that does not look completely like a number.  Convert
................................................................................
 71734  73039       assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 71735  73040       rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc);
 71736  73041       if( rc==0 ){
 71737  73042         MemSetTypeFlag(pMem, MEM_Int);
 71738  73043       }else{
 71739  73044         i64 i = pMem->u.i;
 71740  73045         sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
 71741         -      if( rc==1 && pMem->u.r==(double)i ){
        73046  +      if( rc==1 && sqlite3RealSameAsInt(pMem->u.r, i) ){
 71742  73047           pMem->u.i = i;
 71743  73048           MemSetTypeFlag(pMem, MEM_Int);
 71744  73049         }else{
 71745  73050           MemSetTypeFlag(pMem, MEM_Real);
 71746  73051         }
 71747  73052       }
 71748  73053     }
................................................................................
 72217  73522     }else{
 72218  73523       sqlite3VdbeMemStringify(pVal, enc, 0);
 72219  73524       assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
 72220  73525     }
 72221  73526     assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
 72222  73527                 || pVal->db->mallocFailed );
 72223  73528     if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
        73529  +    assert( sqlite3VdbeMemConsistentDualRep(pVal) );
 72224  73530       return pVal->z;
 72225  73531     }else{
 72226  73532       return 0;
 72227  73533     }
 72228  73534   }
 72229  73535   
 72230  73536   /* This function is only available internally, it is not part of the
................................................................................
 72239  73545   */
 72240  73546   SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
 72241  73547     if( !pVal ) return 0;
 72242  73548     assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
 72243  73549     assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
 72244  73550     assert( (pVal->flags & MEM_RowSet)==0 );
 72245  73551     if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
        73552  +    assert( sqlite3VdbeMemConsistentDualRep(pVal) );
 72246  73553       return pVal->z;
 72247  73554     }
 72248  73555     if( pVal->flags&MEM_Null ){
 72249  73556       return 0;
 72250  73557     }
 72251  73558     return valueToText(pVal, enc);
 72252  73559   }
................................................................................
 78020  79327   SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
 78021  79328     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78022  79329     sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
 78023  79330   }
 78024  79331   SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
 78025  79332     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78026  79333     pCtx->isError = SQLITE_ERROR;
 78027         -  pCtx->fErrorOrAux = 1;
 78028  79334     sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
 78029  79335   }
 78030  79336   #ifndef SQLITE_OMIT_UTF16
 78031  79337   SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
 78032  79338     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78033  79339     pCtx->isError = SQLITE_ERROR;
 78034         -  pCtx->fErrorOrAux = 1;
 78035  79340     sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
 78036  79341   }
 78037  79342   #endif
 78038  79343   SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
 78039  79344     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78040  79345     sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
 78041  79346   }
................................................................................
 78133  79438     if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
 78134  79439       return SQLITE_TOOBIG;
 78135  79440     }
 78136  79441     sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
 78137  79442     return SQLITE_OK;
 78138  79443   }
 78139  79444   SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
 78140         -  pCtx->isError = errCode;
 78141         -  pCtx->fErrorOrAux = 1;
        79445  +  pCtx->isError = errCode ? errCode : -1;
 78142  79446   #ifdef SQLITE_DEBUG
 78143  79447     if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
 78144  79448   #endif
 78145  79449     if( pCtx->pOut->flags & MEM_Null ){
 78146  79450       sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, 
 78147  79451                            SQLITE_UTF8, SQLITE_STATIC);
 78148  79452     }
 78149  79453   }
 78150  79454   
 78151  79455   /* Force an SQLITE_TOOBIG error. */
 78152  79456   SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
 78153  79457     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78154  79458     pCtx->isError = SQLITE_TOOBIG;
 78155         -  pCtx->fErrorOrAux = 1;
 78156  79459     sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 
 78157  79460                          SQLITE_UTF8, SQLITE_STATIC);
 78158  79461   }
 78159  79462   
 78160  79463   /* An SQLITE_NOMEM error. */
 78161  79464   SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
 78162  79465     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 78163  79466     sqlite3VdbeMemSetNull(pCtx->pOut);
 78164  79467     pCtx->isError = SQLITE_NOMEM_BKPT;
 78165         -  pCtx->fErrorOrAux = 1;
 78166  79468     sqlite3OomFault(pCtx->pOut->db);
 78167  79469   }
 78168  79470   
 78169  79471   /*
 78170  79472   ** This function is called after a transaction has been committed. It 
 78171  79473   ** invokes callbacks registered with sqlite3_wal_hook() as required.
 78172  79474   */
................................................................................
 78565  79867     if( pAuxData==0 ){
 78566  79868       pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
 78567  79869       if( !pAuxData ) goto failed;
 78568  79870       pAuxData->iAuxOp = pCtx->iOp;
 78569  79871       pAuxData->iAuxArg = iArg;
 78570  79872       pAuxData->pNextAux = pVdbe->pAuxData;
 78571  79873       pVdbe->pAuxData = pAuxData;
 78572         -    if( pCtx->fErrorOrAux==0 ){
 78573         -      pCtx->isError = 0;
 78574         -      pCtx->fErrorOrAux = 1;
 78575         -    }
        79874  +    if( pCtx->isError==0 ) pCtx->isError = -1;
 78576  79875     }else if( pAuxData->xDeleteAux ){
 78577  79876       pAuxData->xDeleteAux(pAuxData->pAux);
 78578  79877     }
 78579  79878   
 78580  79879     pAuxData->pAux = pAux;
 78581  79880     pAuxData->xDeleteAux = xDelete;
 78582  79881     return;
................................................................................
 79324  80623   /*
 79325  80624   ** Return the value of a status counter for a prepared statement
 79326  80625   */
 79327  80626   SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
 79328  80627     Vdbe *pVdbe = (Vdbe*)pStmt;
 79329  80628     u32 v;
 79330  80629   #ifdef SQLITE_ENABLE_API_ARMOR
 79331         -  if( !pStmt ){
        80630  +  if( !pStmt 
        80631  +   || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
        80632  +  ){
 79332  80633       (void)SQLITE_MISUSE_BKPT;
 79333  80634       return 0;
 79334  80635     }
 79335  80636   #endif
 79336  80637     if( op==SQLITE_STMTSTATUS_MEMUSED ){
 79337  80638       sqlite3 *db = pVdbe->db;
 79338  80639       sqlite3_mutex_enter(db->mutex);
................................................................................
 80098  81399       pRec->u.i = iValue;
 80099  81400       pRec->flags |= MEM_Int;
 80100  81401     }else{
 80101  81402       pRec->u.r = rValue;
 80102  81403       pRec->flags |= MEM_Real;
 80103  81404       if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
 80104  81405     }
        81406  +  /* TEXT->NUMERIC is many->one.  Hence, it is important to invalidate the
        81407  +  ** string representation after computing a numeric equivalent, because the
        81408  +  ** string representation might not be the canonical representation for the
        81409  +  ** numeric value.  Ticket [343634942dd54ab57b7024] 2018-01-31. */
        81410  +  pRec->flags &= ~MEM_Str;
 80105  81411   }
 80106  81412   
 80107  81413   /*
 80108  81414   ** Processing is determine by the affinity parameter:
 80109  81415   **
 80110  81416   ** SQLITE_AFF_INTEGER:
 80111  81417   ** SQLITE_AFF_REAL:
................................................................................
 80566  81872     for(pOp=&aOp[p->pc]; 1; pOp++){
 80567  81873       /* Errors are detected by individual opcodes, with an immediate
 80568  81874       ** jumps to abort_due_to_error. */
 80569  81875       assert( rc==SQLITE_OK );
 80570  81876   
 80571  81877       assert( pOp>=aOp && pOp<&aOp[p->nOp]);
 80572  81878   #ifdef VDBE_PROFILE
 80573         -    start = sqlite3Hwtime();
        81879  +    start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
 80574  81880   #endif
 80575  81881       nVmStep++;
 80576  81882   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 80577  81883       if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
 80578  81884   #endif
 80579  81885   
 80580  81886       /* Only allow tracing if SQLITE_DEBUG is defined.
................................................................................
 82090  83396   ** give a NULL output.
 82091  83397   */
 82092  83398   case OP_And:              /* same as TK_AND, in1, in2, out3 */
 82093  83399   case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
 82094  83400     int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 82095  83401     int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 82096  83402   
 82097         -  pIn1 = &aMem[pOp->p1];
 82098         -  if( pIn1->flags & MEM_Null ){
 82099         -    v1 = 2;
 82100         -  }else{
 82101         -    v1 = sqlite3VdbeIntValue(pIn1)!=0;
 82102         -  }
 82103         -  pIn2 = &aMem[pOp->p2];
 82104         -  if( pIn2->flags & MEM_Null ){
 82105         -    v2 = 2;
 82106         -  }else{
 82107         -    v2 = sqlite3VdbeIntValue(pIn2)!=0;
 82108         -  }
        83403  +  v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
        83404  +  v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
 82109  83405     if( pOp->opcode==OP_And ){
 82110  83406       static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
 82111  83407       v1 = and_logic[v1*3+v2];
 82112  83408     }else{
 82113  83409       static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
 82114  83410       v1 = or_logic[v1*3+v2];
 82115  83411     }
................................................................................
 82118  83414       MemSetTypeFlag(pOut, MEM_Null);
 82119  83415     }else{
 82120  83416       pOut->u.i = v1;
 82121  83417       MemSetTypeFlag(pOut, MEM_Int);
 82122  83418     }
 82123  83419     break;
 82124  83420   }
        83421  +
        83422  +/* Opcode: IsTrue P1 P2 P3 P4 *
        83423  +** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
        83424  +**
        83425  +** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
        83426  +** IS NOT FALSE operators.
        83427  +**
        83428  +** Interpret the value in register P1 as a boolean value.  Store that
        83429  +** boolean (a 0 or 1) in register P2.  Or if the value in register P1 is 
        83430  +** NULL, then the P3 is stored in register P2.  Invert the answer if P4
        83431  +** is 1.
        83432  +**
        83433  +** The logic is summarized like this:
        83434  +**
        83435  +** <ul> 
        83436  +** <li> If P3==0 and P4==0  then  r[P2] := r[P1] IS TRUE
        83437  +** <li> If P3==1 and P4==1  then  r[P2] := r[P1] IS FALSE
        83438  +** <li> If P3==0 and P4==1  then  r[P2] := r[P1] IS NOT TRUE
        83439  +** <li> If P3==1 and P4==0  then  r[P2] := r[P1] IS NOT FALSE
        83440  +** </ul>
        83441  +*/
        83442  +case OP_IsTrue: {               /* in1, out2 */
        83443  +  assert( pOp->p4type==P4_INT32 );
        83444  +  assert( pOp->p4.i==0 || pOp->p4.i==1 );
        83445  +  assert( pOp->p3==0 || pOp->p3==1 );
        83446  +  sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
        83447  +      sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
        83448  +  break;
        83449  +}
 82125  83450   
 82126  83451   /* Opcode: Not P1 P2 * * *
 82127  83452   ** Synopsis: r[P2]= !r[P1]
 82128  83453   **
 82129  83454   ** Interpret the value in register P1 as a boolean value.  Store the
 82130  83455   ** boolean complement in register P2.  If the value in register P1 is 
 82131  83456   ** NULL, then a NULL is stored in P2.
 82132  83457   */
 82133  83458   case OP_Not: {                /* same as TK_NOT, in1, out2 */
 82134  83459     pIn1 = &aMem[pOp->p1];
 82135  83460     pOut = &aMem[pOp->p2];
 82136         -  sqlite3VdbeMemSetNull(pOut);
 82137  83461     if( (pIn1->flags & MEM_Null)==0 ){
 82138         -    pOut->flags = MEM_Int;
 82139         -    pOut->u.i = !sqlite3VdbeIntValue(pIn1);
        83462  +    sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
        83463  +  }else{
        83464  +    sqlite3VdbeMemSetNull(pOut);
 82140  83465     }
 82141  83466     break;
 82142  83467   }
 82143  83468   
 82144  83469   /* Opcode: BitNot P1 P2 * * *
 82145  83470   ** Synopsis: r[P1]= ~r[P1]
 82146  83471   **
................................................................................
 82199  83524   
 82200  83525   /* Opcode: If P1 P2 P3 * *
 82201  83526   **
 82202  83527   ** Jump to P2 if the value in register P1 is true.  The value
 82203  83528   ** is considered true if it is numeric and non-zero.  If the value
 82204  83529   ** in P1 is NULL then take the jump if and only if P3 is non-zero.
 82205  83530   */
        83531  +case OP_If:  {               /* jump, in1 */
        83532  +  int c;
        83533  +  c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
        83534  +  VdbeBranchTaken(c!=0, 2);
        83535  +  if( c ) goto jump_to_p2;
        83536  +  break;
        83537  +}
        83538  +
 82206  83539   /* Opcode: IfNot P1 P2 P3 * *
 82207  83540   **
 82208  83541   ** Jump to P2 if the value in register P1 is False.  The value
 82209  83542   ** is considered false if it has a numeric value of zero.  If the value
 82210  83543   ** in P1 is NULL then take the jump if and only if P3 is non-zero.
 82211  83544   */
 82212         -case OP_If:                 /* jump, in1 */
 82213  83545   case OP_IfNot: {            /* jump, in1 */
 82214  83546     int c;
 82215         -  pIn1 = &aMem[pOp->p1];
 82216         -  if( pIn1->flags & MEM_Null ){
 82217         -    c = pOp->p3;
 82218         -  }else{
 82219         -#ifdef SQLITE_OMIT_FLOATING_POINT
 82220         -    c = sqlite3VdbeIntValue(pIn1)!=0;
 82221         -#else
 82222         -    c = sqlite3VdbeRealValue(pIn1)!=0.0;
 82223         -#endif
 82224         -    if( pOp->opcode==OP_IfNot ) c = !c;
 82225         -  }
        83547  +  c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
 82226  83548     VdbeBranchTaken(c!=0, 2);
 82227         -  if( c ){
 82228         -    goto jump_to_p2;
 82229         -  }
        83549  +  if( c ) goto jump_to_p2;
 82230  83550     break;
 82231  83551   }
 82232  83552   
 82233  83553   /* Opcode: IsNull P1 P2 * * *
 82234  83554   ** Synopsis: if r[P1]==NULL goto P2
 82235  83555   **
 82236  83556   ** Jump to P2 if the value in register P1 is NULL.
................................................................................
 82283  83603   ** Store in register r[P3] the byte offset into the database file that is the
 82284  83604   ** start of the payload for the record at which that cursor P1 is currently
 82285  83605   ** pointing.
 82286  83606   **
 82287  83607   ** P2 is the column number for the argument to the sqlite_offset() function.
 82288  83608   ** This opcode does not use P2 itself, but the P2 value is used by the
 82289  83609   ** code generator.  The P1, P2, and P3 operands to this opcode are the
 82290         -** as as for OP_Column.
        83610  +** same as for OP_Column.
 82291  83611   **
 82292  83612   ** This opcode is only available if SQLite is compiled with the
 82293  83613   ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
 82294  83614   */
 82295  83615   case OP_Offset: {          /* out3 */
 82296  83616     VdbeCursor *pC;    /* The VDBE cursor */
 82297  83617     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
................................................................................
 84191  85511     VdbeFrame *pFrame;     /* Root frame of VDBE */
 84192  85512   
 84193  85513     v = 0;
 84194  85514     res = 0;
 84195  85515     pOut = out2Prerelease(p, pOp);
 84196  85516     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 84197  85517     pC = p->apCsr[pOp->p1];
        85518  +  if( !pC->isTable ){
        85519  +    rc = SQLITE_CORRUPT_BKPT;
        85520  +    goto abort_due_to_error;
        85521  +  }
 84198  85522     assert( pC!=0 );
 84199  85523     assert( pC->eCurType==CURTYPE_BTREE );
 84200  85524     assert( pC->uc.pCursor!=0 );
 84201  85525     {
 84202  85526       /* The next rowid or record number (different terms for the same
 84203  85527       ** thing) is obtained in a two-step algorithm.
 84204  85528       **
................................................................................
 86127  87451     sqlite3_context *pCtx;
 86128  87452   
 86129  87453     assert( pOp->p4type==P4_FUNCDEF );
 86130  87454     n = pOp->p5;
 86131  87455     assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
 86132  87456     assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
 86133  87457     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
 86134         -  pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
        87458  +  pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
        87459  +               (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
 86135  87460     if( pCtx==0 ) goto no_mem;
 86136  87461     pCtx->pMem = 0;
        87462  +  pCtx->pOut = (Mem*)&(pCtx->argv[n]);
        87463  +  sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
 86137  87464     pCtx->pFunc = pOp->p4.pFunc;
 86138  87465     pCtx->iOp = (int)(pOp - aOp);
 86139  87466     pCtx->pVdbe = p;
        87467  +  pCtx->skipFlag = 0;
        87468  +  pCtx->isError = 0;
 86140  87469     pCtx->argc = n;
 86141  87470     pOp->p4type = P4_FUNCCTX;
 86142  87471     pOp->p4.pCtx = pCtx;
 86143  87472     pOp->opcode = OP_AggStep;
 86144  87473     /* Fall through into OP_AggStep */
 86145  87474   }
 86146  87475   case OP_AggStep: {
 86147  87476     int i;
 86148  87477     sqlite3_context *pCtx;
 86149  87478     Mem *pMem;
 86150         -  Mem t;
 86151  87479   
 86152  87480     assert( pOp->p4type==P4_FUNCCTX );
 86153  87481     pCtx = pOp->p4.pCtx;
 86154  87482     pMem = &aMem[pOp->p3];
 86155  87483   
 86156  87484     /* If this function is inside of a trigger, the register array in aMem[]
 86157  87485     ** might change from one evaluation to the next.  The next block of code
................................................................................
 86166  87494     for(i=0; i<pCtx->argc; i++){
 86167  87495       assert( memIsValid(pCtx->argv[i]) );
 86168  87496       REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
 86169  87497     }
 86170  87498   #endif
 86171  87499   
 86172  87500     pMem->n++;
 86173         -  sqlite3VdbeMemInit(&t, db, MEM_Null);
 86174         -  pCtx->pOut = &t;
 86175         -  pCtx->fErrorOrAux = 0;
 86176         -  pCtx->skipFlag = 0;
        87501  +  assert( pCtx->pOut->flags==MEM_Null );
        87502  +  assert( pCtx->isError==0 );
        87503  +  assert( pCtx->skipFlag==0 );
 86177  87504     (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
 86178         -  if( pCtx->fErrorOrAux ){
 86179         -    if( pCtx->isError ){
 86180         -      sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
        87505  +  if( pCtx->isError ){
        87506  +    if( pCtx->isError>0 ){
        87507  +      sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
 86181  87508         rc = pCtx->isError;
 86182  87509       }
 86183         -    sqlite3VdbeMemRelease(&t);
        87510  +    if( pCtx->skipFlag ){
        87511  +      assert( pOp[-1].opcode==OP_CollSeq );
        87512  +      i = pOp[-1].p1;
        87513  +      if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
        87514  +      pCtx->skipFlag = 0;
        87515  +    }
        87516  +    sqlite3VdbeMemRelease(pCtx->pOut);
        87517  +    pCtx->pOut->flags = MEM_Null;
        87518  +    pCtx->isError = 0;
 86184  87519       if( rc ) goto abort_due_to_error;
 86185         -  }else{
 86186         -    assert( t.flags==MEM_Null );
 86187  87520     }
 86188         -  if( pCtx->skipFlag ){
 86189         -    assert( pOp[-1].opcode==OP_CollSeq );
 86190         -    i = pOp[-1].p1;
 86191         -    if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
 86192         -  }
        87521  +  assert( pCtx->pOut->flags==MEM_Null );
        87522  +  assert( pCtx->skipFlag==0 );
 86193  87523     break;
 86194  87524   }
 86195  87525   
 86196  87526   /* Opcode: AggFinal P1 P2 * P4 *
 86197  87527   ** Synopsis: accum=r[P1] N=P2
 86198  87528   **
 86199  87529   ** Execute the finalizer function for an aggregate.  P1 is
................................................................................
 86672  88002       pDest->flags = MEM_Null|MEM_Zero;
 86673  88003       pDest->u.nZero = 0;
 86674  88004     }else{
 86675  88005       MemSetTypeFlag(pDest, MEM_Null);
 86676  88006     }
 86677  88007     rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
 86678  88008     sqlite3VtabImportErrmsg(p, pVtab);
 86679         -  if( sContext.isError ){
        88009  +  if( sContext.isError>0 ){
        88010  +    sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
 86680  88011       rc = sContext.isError;
 86681  88012     }
 86682  88013     sqlite3VdbeChangeEncoding(pDest, encoding);
 86683  88014     REGISTER_TRACE(pOp->p3, pDest);
 86684  88015     UPDATE_MAX_BLOBSIZE(pDest);
 86685  88016   
 86686  88017     if( sqlite3VdbeMemTooBig(pDest) ){
................................................................................
 86937  88268     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
 86938  88269     pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
 86939  88270     if( pCtx==0 ) goto no_mem;
 86940  88271     pCtx->pOut = 0;
 86941  88272     pCtx->pFunc = pOp->p4.pFunc;
 86942  88273     pCtx->iOp = (int)(pOp - aOp);
 86943  88274     pCtx->pVdbe = p;
        88275  +  pCtx->isError = 0;
 86944  88276     pCtx->argc = n;
 86945  88277     pOp->p4type = P4_FUNCCTX;
 86946  88278     pOp->p4.pCtx = pCtx;
 86947  88279     assert( OP_PureFunc == OP_PureFunc0+2 );
 86948  88280     assert( OP_Function == OP_Function0+2 );
 86949  88281     pOp->opcode += 2;
 86950  88282     /* Fall through into OP_Function */
................................................................................
 86971  88303   #ifdef SQLITE_DEBUG
 86972  88304     for(i=0; i<pCtx->argc; i++){
 86973  88305       assert( memIsValid(pCtx->argv[i]) );
 86974  88306       REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
 86975  88307     }
 86976  88308   #endif
 86977  88309     MemSetTypeFlag(pOut, MEM_Null);
 86978         -  pCtx->fErrorOrAux = 0;
        88310  +  assert( pCtx->isError==0 );
 86979  88311     (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
 86980  88312   
 86981  88313     /* If the function returned an error, throw an exception */
 86982         -  if( pCtx->fErrorOrAux ){
 86983         -    if( pCtx->isError ){
        88314  +  if( pCtx->isError ){
        88315  +    if( pCtx->isError>0 ){
 86984  88316         sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
 86985  88317         rc = pCtx->isError;
 86986  88318       }
 86987  88319       sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
        88320  +    pCtx->isError = 0;
 86988  88321       if( rc ) goto abort_due_to_error;
 86989  88322     }
 86990  88323   
 86991  88324     /* Copy the result of the function into register P3 */
 86992  88325     if( pOut->flags & (MEM_Str|MEM_Blob) ){
 86993  88326       sqlite3VdbeChangeEncoding(pOut, encoding);
 86994  88327       if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
................................................................................
 87022  88355   ** first time they are evaluated for this run.
 87023  88356   **
 87024  88357   ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
 87025  88358   ** error is encountered.
 87026  88359   */
 87027  88360   case OP_Trace:
 87028  88361   case OP_Init: {          /* jump */
        88362  +  int i;
        88363  +#ifndef SQLITE_OMIT_TRACE
 87029  88364     char *zTrace;
 87030         -  int i;
        88365  +#endif
 87031  88366   
 87032  88367     /* If the P4 argument is not NULL, then it must be an SQL comment string.
 87033  88368     ** The "--" string is broken up to prevent false-positives with srcck1.c.
 87034  88369     **
 87035  88370     ** This assert() provides evidence for:
 87036  88371     ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
 87037  88372     ** would have been returned by the legacy sqlite3_trace() interface by
................................................................................
 87140  88475   ** readability.  From this point on down, the normal indentation rules are
 87141  88476   ** restored.
 87142  88477   *****************************************************************************/
 87143  88478       }
 87144  88479   
 87145  88480   #ifdef VDBE_PROFILE
 87146  88481       {
 87147         -      u64 endTime = sqlite3Hwtime();
        88482  +      u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
 87148  88483         if( endTime>start ) pOrigOp->cycles += endTime - start;
 87149  88484         pOrigOp->cnt++;
 87150  88485       }
 87151  88486   #endif
 87152  88487   
 87153  88488       /* The following code adds nothing to the actual functionality
 87154  88489       ** of the program.  It is only here for testing and debugging.
................................................................................
 91527  92862     ** Z is a string literal if it doesn't match any column names.  In that
 91528  92863     ** case, we need to return right away and not make any changes to
 91529  92864     ** pExpr.
 91530  92865     **
 91531  92866     ** Because no reference was made to outer contexts, the pNC->nRef
 91532  92867     ** fields are not changed in any context.
 91533  92868     */
 91534         -  if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
 91535         -    pExpr->op = TK_STRING;
 91536         -    pExpr->pTab = 0;
 91537         -    return WRC_Prune;
        92869  +  if( cnt==0 && zTab==0 ){
        92870  +    assert( pExpr->op==TK_ID );
        92871  +    if( ExprHasProperty(pExpr,EP_DblQuoted) ){
        92872  +      pExpr->op = TK_STRING;
        92873  +      pExpr->pTab = 0;
        92874  +      return WRC_Prune;
        92875  +    }
        92876  +    if( sqlite3ExprIdToTrueFalse(pExpr) ){
        92877  +      return WRC_Prune;
        92878  +    }
 91538  92879     }
 91539  92880   
 91540  92881     /*
 91541  92882     ** cnt==0 means there was not match.  cnt>1 means there were two or
 91542  92883     ** more matches.  Either way, we have an error.
 91543  92884     */
 91544  92885     if( cnt!=1 ){
................................................................................
 91879  93220         }
 91880  93221         break;
 91881  93222       }
 91882  93223       case TK_VARIABLE: {
 91883  93224         notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
 91884  93225         break;
 91885  93226       }
        93227  +    case TK_IS:
        93228  +    case TK_ISNOT: {
        93229  +      Expr *pRight;
        93230  +      assert( !ExprHasProperty(pExpr, EP_Reduced) );
        93231  +      /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
        93232  +      ** and "x IS NOT FALSE". */
        93233  +      if( (pRight = pExpr->pRight)->op==TK_ID ){
        93234  +        int rc = resolveExprStep(pWalker, pRight);
        93235  +        if( rc==WRC_Abort ) return WRC_Abort;
        93236  +        if( pRight->op==TK_TRUEFALSE ){
        93237  +          pExpr->op2 = pExpr->op;
        93238  +          pExpr->op = TK_TRUTH;
        93239  +          return WRC_Continue;
        93240  +        }
        93241  +      }
        93242  +      /* Fall thru */
        93243  +    }
 91886  93244       case TK_BETWEEN:
 91887  93245       case TK_EQ:
 91888  93246       case TK_NE:
 91889  93247       case TK_LT:
 91890  93248       case TK_LE:
 91891  93249       case TK_GT:
 91892         -    case TK_GE:
 91893         -    case TK_IS:
 91894         -    case TK_ISNOT: {
        93250  +    case TK_GE: {
 91895  93251         int nLeft, nRight;
 91896  93252         if( pParse->db->mallocFailed ) break;
 91897  93253         assert( pExpr->pLeft!=0 );
 91898  93254         nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
 91899  93255         if( pExpr->op==TK_BETWEEN ){
 91900  93256           nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
 91901  93257           if( nRight==nLeft ){
................................................................................
 93288  94644     if( p ){
 93289  94645       int i;
 93290  94646       for(i=0; i<p->nExpr; i++){
 93291  94647         heightOfExpr(p->a[i].pExpr, pnHeight);
 93292  94648       }
 93293  94649     }
 93294  94650   }
 93295         -static void heightOfSelect(Select *p, int *pnHeight){
 93296         -  if( p ){
        94651  +static void heightOfSelect(Select *pSelect, int *pnHeight){
        94652  +  Select *p;
        94653  +  for(p=pSelect; p; p=p->pPrior){
 93297  94654       heightOfExpr(p->pWhere, pnHeight);
 93298  94655       heightOfExpr(p->pHaving, pnHeight);
 93299  94656       heightOfExpr(p->pLimit, pnHeight);
 93300  94657       heightOfExprList(p->pEList, pnHeight);
 93301  94658       heightOfExprList(p->pGroupBy, pnHeight);
 93302  94659       heightOfExprList(p->pOrderBy, pnHeight);
 93303         -    heightOfSelect(p->pPrior, pnHeight);
 93304  94660     }
 93305  94661   }
 93306  94662   
 93307  94663   /*
 93308  94664   ** Set the Expr.nHeight variable in the structure passed as an 
 93309  94665   ** argument. An expression with no children, Expr.pList or 
 93310  94666   ** Expr.pSelect member has a height of 1. Any other expression
................................................................................
 94361  95717   ** This callback is used by multiple expression walkers.
 94362  95718   */
 94363  95719   SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
 94364  95720     UNUSED_PARAMETER(NotUsed);
 94365  95721     pWalker->eCode = 0;
 94366  95722     return WRC_Abort;
 94367  95723   }
        95724  +
        95725  +/*
        95726  +** If the input expression is an ID with the name "true" or "false"
        95727  +** then convert it into an TK_TRUEFALSE term.  Return non-zero if
        95728  +** the conversion happened, and zero if the expression is unaltered.
        95729  +*/
        95730  +SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
        95731  +  assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
        95732  +  if( sqlite3StrICmp(pExpr->u.zToken, "true")==0
        95733  +   || sqlite3StrICmp(pExpr->u.zToken, "false")==0
        95734  +  ){
        95735  +    pExpr->op = TK_TRUEFALSE;
        95736  +    return 1;
        95737  +  }
        95738  +  return 0;
        95739  +}
        95740  +
        95741  +/*
        95742  +** The argument must be a TK_TRUEFALSE Expr node.  Return 1 if it is TRUE
        95743  +** and 0 if it is FALSE.
        95744  +*/
        95745  +SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
        95746  +  assert( pExpr->op==TK_TRUEFALSE );
        95747  +  assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
        95748  +       || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
        95749  +  return pExpr->u.zToken[4]==0;
        95750  +}
        95751  +
 94368  95752   
 94369  95753   /*
 94370  95754   ** These routines are Walker callbacks used to check expressions to
 94371  95755   ** see if they are "constant" for some definition of constant.  The
 94372  95756   ** Walker.eCode value determines the type of "constant" we are looking
 94373  95757   ** for.
 94374  95758   **
................................................................................
 94409  95793         if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
 94410  95794           return WRC_Continue;
 94411  95795         }else{
 94412  95796           pWalker->eCode = 0;
 94413  95797           return WRC_Abort;
 94414  95798         }
 94415  95799       case TK_ID:
        95800  +      /* Convert "true" or "false" in a DEFAULT clause into the
        95801  +      ** appropriate TK_TRUEFALSE operator */
        95802  +      if( sqlite3ExprIdToTrueFalse(pExpr) ){
        95803  +        return WRC_Prune;
        95804  +      }
        95805  +      /* Fall thru */
 94416  95806       case TK_COLUMN:
 94417  95807       case TK_AGG_FUNCTION:
 94418  95808       case TK_AGG_COLUMN:
 94419  95809         testcase( pExpr->op==TK_ID );
 94420  95810         testcase( pExpr->op==TK_COLUMN );
 94421  95811         testcase( pExpr->op==TK_AGG_FUNCTION );
 94422  95812         testcase( pExpr->op==TK_AGG_COLUMN );
................................................................................
 96172  97562         return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
 96173  97563                                  pExpr->iColumn, iTab, target,
 96174  97564                                  pExpr->op2);
 96175  97565       }
 96176  97566       case TK_INTEGER: {
 96177  97567         codeInteger(pParse, pExpr, 0, target);
 96178  97568         return target;
        97569  +    }
        97570  +    case TK_TRUEFALSE: {
        97571  +      sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
        97572  +      return target;
 96179  97573       }
 96180  97574   #ifndef SQLITE_OMIT_FLOATING_POINT
 96181  97575       case TK_FLOAT: {
 96182  97576         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 96183  97577         codeReal(v, pExpr->u.zToken, 0, target);
 96184  97578         return target;
 96185  97579       }
................................................................................
 96327  97721       case TK_NOT: {
 96328  97722         assert( TK_BITNOT==OP_BitNot );   testcase( op==TK_BITNOT );
 96329  97723         assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
 96330  97724         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 96331  97725         testcase( regFree1==0 );
 96332  97726         sqlite3VdbeAddOp2(v, op, r1, inReg);
 96333  97727         break;
        97728  +    }
        97729  +    case TK_TRUTH: {
        97730  +      int isTrue;    /* IS TRUE or IS NOT TRUE */
        97731  +      int bNormal;   /* IS TRUE or IS FALSE */
        97732  +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
        97733  +      testcase( regFree1==0 );
        97734  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
        97735  +      bNormal = pExpr->op2==TK_IS;
        97736  +      testcase( isTrue && bNormal);
        97737  +      testcase( !isTrue && bNormal);
        97738  +      sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
        97739  +      break;
 96334  97740       }
 96335  97741       case TK_ISNULL:
 96336  97742       case TK_NOTNULL: {
 96337  97743         int addr;
 96338  97744         assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
 96339  97745         assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
 96340  97746         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
................................................................................
 97102  98508         sqlite3ExprCachePop(pParse);
 97103  98509         break;
 97104  98510       }
 97105  98511       case TK_NOT: {
 97106  98512         testcase( jumpIfNull==0 );
 97107  98513         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
 97108  98514         break;
        98515  +    }
        98516  +    case TK_TRUTH: {
        98517  +      int isNot;      /* IS NOT TRUE or IS NOT FALSE */
        98518  +      int isTrue;     /* IS TRUE or IS NOT TRUE */
        98519  +      testcase( jumpIfNull==0 );
        98520  +      isNot = pExpr->op2==TK_ISNOT;
        98521  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
        98522  +      testcase( isTrue && isNot );
        98523  +      testcase( !isTrue && isNot );
        98524  +      if( isTrue ^ isNot ){
        98525  +        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
        98526  +                          isNot ? SQLITE_JUMPIFNULL : 0);
        98527  +      }else{
        98528  +        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
        98529  +                           isNot ? SQLITE_JUMPIFNULL : 0);
        98530  +      }
        98531  +      break;
 97109  98532       }
 97110  98533       case TK_IS:
 97111  98534       case TK_ISNOT:
 97112  98535         testcase( op==TK_IS );
 97113  98536         testcase( op==TK_ISNOT );
 97114  98537         op = (op==TK_IS) ? TK_EQ : TK_NE;
 97115  98538         jumpIfNull = SQLITE_NULLEQ;
................................................................................
 97256  98679         sqlite3ExprCachePop(pParse);
 97257  98680         break;
 97258  98681       }
 97259  98682       case TK_NOT: {
 97260  98683         testcase( jumpIfNull==0 );
 97261  98684         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
 97262  98685         break;
        98686  +    }
        98687  +    case TK_TRUTH: {
        98688  +      int isNot;   /* IS NOT TRUE or IS NOT FALSE */
        98689  +      int isTrue;  /* IS TRUE or IS NOT TRUE */
        98690  +      testcase( jumpIfNull==0 );
        98691  +      isNot = pExpr->op2==TK_ISNOT;
        98692  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
        98693  +      testcase( isTrue && isNot );
        98694  +      testcase( !isTrue && isNot );
        98695  +      if( isTrue ^ isNot ){
        98696  +        /* IS TRUE and IS NOT FALSE */
        98697  +        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
        98698  +                           isNot ? 0 : SQLITE_JUMPIFNULL);
        98699  +
        98700  +      }else{
        98701  +        /* IS FALSE and IS NOT TRUE */
        98702  +        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
        98703  +                          isNot ? 0 : SQLITE_JUMPIFNULL);
        98704  +      }
        98705  +      break;
 97263  98706       }
 97264  98707       case TK_IS:
 97265  98708       case TK_ISNOT:
 97266  98709         testcase( pExpr->op==TK_IS );
 97267  98710         testcase( pExpr->op==TK_ISNOT );
 97268  98711         op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
 97269  98712         jumpIfNull = SQLITE_NULLEQ;
................................................................................
 97543  98986     if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){
 97544  98987       Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft);
 97545  98988       testcase( pX!=pE1->pLeft );
 97546  98989       if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1;
 97547  98990     }
 97548  98991     return 0;
 97549  98992   }
        98993  +
        98994  +/*
        98995  +** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
        98996  +** If the expression node requires that the table at pWalker->iCur
        98997  +** have a non-NULL column, then set pWalker->eCode to 1 and abort.
        98998  +*/
        98999  +static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
        99000  +  /* This routine is only called for WHERE clause expressions and so it
        99001  +  ** cannot have any TK_AGG_COLUMN entries because those are only found
        99002  +  ** in HAVING clauses.  We can get a TK_AGG_FUNCTION in a WHERE clause,
        99003  +  ** but that is an illegal construct and the query will be rejected at
        99004  +  ** a later stage of processing, so the TK_AGG_FUNCTION case does not
        99005  +  ** need to be considered here. */
        99006  +  assert( pExpr->op!=TK_AGG_COLUMN );
        99007  +  testcase( pExpr->op==TK_AGG_FUNCTION );
        99008  +
        99009  +  if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
        99010  +  switch( pExpr->op ){
        99011  +    case TK_ISNULL:
        99012  +    case TK_IS:
        99013  +    case TK_OR:
        99014  +    case TK_CASE:
        99015  +    case TK_IN:
        99016  +    case TK_FUNCTION:
        99017  +      testcase( pExpr->op==TK_ISNULL );
        99018  +      testcase( pExpr->op==TK_IS );
        99019  +      testcase( pExpr->op==TK_OR );
        99020  +      testcase( pExpr->op==TK_CASE );
        99021  +      testcase( pExpr->op==TK_IN );
        99022  +      testcase( pExpr->op==TK_FUNCTION );
        99023  +      return WRC_Prune;
        99024  +    case TK_COLUMN:
        99025  +      if( pWalker->u.iCur==pExpr->iTable ){
        99026  +        pWalker->eCode = 1;
        99027  +        return WRC_Abort;
        99028  +      }
        99029  +      return WRC_Prune;
        99030  +    default:
        99031  +      return WRC_Continue;
        99032  +  }
        99033  +}
        99034  +
        99035  +/*
        99036  +** Return true (non-zero) if expression p can only be true if at least
        99037  +** one column of table iTab is non-null.  In other words, return true
        99038  +** if expression p will always be NULL or false if every column of iTab
        99039  +** is NULL.
        99040  +**
        99041  +** False negatives are acceptable.  In other words, it is ok to return
        99042  +** zero even if expression p will never be true of every column of iTab
        99043  +** is NULL.  A false negative is merely a missed optimization opportunity.
        99044  +**
        99045  +** False positives are not allowed, however.  A false positive may result
        99046  +** in an incorrect answer.
        99047  +**
        99048  +** Terms of p that are marked with EP_FromJoin (and hence that come from
        99049  +** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
        99050  +**
        99051  +** This routine is used to check if a LEFT JOIN can be converted into
        99052  +** an ordinary JOIN.  The p argument is the WHERE clause.  If the WHERE
        99053  +** clause requires that some column of the right table of the LEFT JOIN
        99054  +** be non-NULL, then the LEFT JOIN can be safely converted into an
        99055  +** ordinary join.
        99056  +*/
        99057  +SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
        99058  +  Walker w;
        99059  +  w.xExprCallback = impliesNotNullRow;
        99060  +  w.xSelectCallback = 0;
        99061  +  w.xSelectCallback2 = 0;
        99062  +  w.eCode = 0;
        99063  +  w.u.iCur = iTab;
        99064  +  sqlite3WalkExpr(&w, p);
        99065  +  return w.eCode;
        99066  +}
 97550  99067   
 97551  99068   /*
 97552  99069   ** An instance of the following structure is used by the tree walker
 97553  99070   ** to determine if an expression can be evaluated by reference to the
 97554  99071   ** index only, without having to do a search for the corresponding
 97555  99072   ** table entry.  The IdxCover.pIdx field is the index.  IdxCover.iCur
 97556  99073   ** is the cursor for the table.
................................................................................
 99788 101305     if( v==0 || NEVER(pTab==0) ){
 99789 101306       return;
 99790 101307     }
 99791 101308     if( pTab->tnum==0 ){
 99792 101309       /* Do not gather statistics on views or virtual tables */
 99793 101310       return;
 99794 101311     }
 99795         -  if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){
       101312  +  if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
 99796 101313       /* Do not gather statistics on system tables */
 99797 101314       return;
 99798 101315     }
 99799 101316     assert( sqlite3BtreeHoldsAllMutexes(db) );
 99800 101317     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 99801 101318     assert( iDb>=0 );
 99802 101319     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
................................................................................
100082 101599       jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
100083 101600       sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
100084 101601       assert( "BBB"[0]==SQLITE_AFF_TEXT );
100085 101602       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
100086 101603       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
100087 101604       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
100088 101605       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
       101606  +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
       101607  +    sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
       101608  +#endif
100089 101609       sqlite3VdbeJumpHere(v, jZeroRows);
100090 101610     }
100091 101611   }
100092 101612   
100093 101613   
100094 101614   /*
100095 101615   ** Generate code that will cause the most recent index analysis to
................................................................................
100764 102284   **
100765 102285   **     ATTACH DATABASE x AS y KEY z
100766 102286   **
100767 102287   **     SELECT sqlite_attach(x, y, z)
100768 102288   **
100769 102289   ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
100770 102290   ** third argument.
       102291  +**
       102292  +** If the db->init.reopenMemdb flags is set, then instead of attaching a
       102293  +** new database, close the database on db->init.iDb and reopen it as an
       102294  +** empty MemDB.
100771 102295   */
100772 102296   static void attachFunc(
100773 102297     sqlite3_context *context,
100774 102298     int NotUsed,
100775 102299     sqlite3_value **argv
100776 102300   ){
100777 102301     int i;
................................................................................
100784 102308     unsigned int flags;
100785 102309     Db *aNew;                 /* New array of Db pointers */
100786 102310     Db *pNew;                 /* Db object for the newly attached database */
100787 102311     char *zErrDyn = 0;
100788 102312     sqlite3_vfs *pVfs;
100789 102313   
100790 102314     UNUSED_PARAMETER(NotUsed);
100791         -
100792 102315     zFile = (const char *)sqlite3_value_text(argv[0]);
100793 102316     zName = (const char *)sqlite3_value_text(argv[1]);
100794 102317     if( zFile==0 ) zFile = "";
100795 102318     if( zName==0 ) zName = "";
100796 102319   
100797         -  /* Check for the following errors:
100798         -  **
100799         -  **     * Too many attached databases,
100800         -  **     * Transaction currently open
100801         -  **     * Specified database name already being used.
100802         -  */
100803         -  if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
100804         -    zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
100805         -      db->aLimit[SQLITE_LIMIT_ATTACHED]
100806         -    );
100807         -    goto attach_error;
100808         -  }
100809         -  for(i=0; i<db->nDb; i++){
100810         -    char *z = db->aDb[i].zDbSName;
100811         -    assert( z && zName );
100812         -    if( sqlite3StrICmp(z, zName)==0 ){
100813         -      zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
       102320  +#ifdef SQLITE_ENABLE_DESERIALIZE
       102321  +# define REOPEN_AS_MEMDB(db)  (db->init.reopenMemdb)
       102322  +#else
       102323  +# define REOPEN_AS_MEMDB(db)  (0)
       102324  +#endif
       102325  +
       102326  +  if( REOPEN_AS_MEMDB(db) ){
       102327  +    /* This is not a real ATTACH.  Instead, this routine is being called
       102328  +    ** from sqlite3_deserialize() to close database db->init.iDb and
       102329  +    ** reopen it as a MemDB */
       102330  +    pVfs = sqlite3_vfs_find("memdb");
       102331  +    if( pVfs==0 ) return;
       102332  +    pNew = &db->aDb[db->init.iDb];
       102333  +    if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
       102334  +    pNew->pBt = 0;
       102335  +    pNew->pSchema = 0;
       102336  +    rc = sqlite3BtreeOpen(pVfs, "x", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
       102337  +  }else{
       102338  +    /* This is a real ATTACH
       102339  +    **
       102340  +    ** Check for the following errors:
       102341  +    **
       102342  +    **     * Too many attached databases,
       102343  +    **     * Transaction currently open
       102344  +    **     * Specified database name already being used.
       102345  +    */
       102346  +    if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
       102347  +      zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
       102348  +        db->aLimit[SQLITE_LIMIT_ATTACHED]
       102349  +      );
100814 102350         goto attach_error;
100815 102351       }
100816         -  }
100817         -
100818         -  /* Allocate the new entry in the db->aDb[] array and initialize the schema
100819         -  ** hash tables.
100820         -  */
100821         -  if( db->aDb==db->aDbStatic ){
100822         -    aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
100823         -    if( aNew==0 ) return;
100824         -    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
100825         -  }else{
100826         -    aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
100827         -    if( aNew==0 ) return;
100828         -  }
100829         -  db->aDb = aNew;
100830         -  pNew = &db->aDb[db->nDb];
100831         -  memset(pNew, 0, sizeof(*pNew));
100832         -
100833         -  /* Open the database file. If the btree is successfully opened, use
100834         -  ** it to obtain the database schema. At this point the schema may
100835         -  ** or may not be initialized.
100836         -  */
100837         -  flags = db->openFlags;
100838         -  rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
100839         -  if( rc!=SQLITE_OK ){
100840         -    if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
100841         -    sqlite3_result_error(context, zErr, -1);
100842         -    sqlite3_free(zErr);
100843         -    return;
100844         -  }
100845         -  assert( pVfs );
100846         -  flags |= SQLITE_OPEN_MAIN_DB;
100847         -  rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
100848         -  sqlite3_free( zPath );
100849         -  db->nDb++;
       102352  +    for(i=0; i<db->nDb; i++){
       102353  +      char *z = db->aDb[i].zDbSName;
       102354  +      assert( z && zName );
       102355  +      if( sqlite3StrICmp(z, zName)==0 ){
       102356  +        zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
       102357  +        goto attach_error;
       102358  +      }
       102359  +    }
       102360  +  
       102361  +    /* Allocate the new entry in the db->aDb[] array and initialize the schema
       102362  +    ** hash tables.
       102363  +    */
       102364  +    if( db->aDb==db->aDbStatic ){
       102365  +      aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
       102366  +      if( aNew==0 ) return;
       102367  +      memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
       102368  +    }else{
       102369  +      aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
       102370  +      if( aNew==0 ) return;
       102371  +    }
       102372  +    db->aDb = aNew;
       102373  +    pNew = &db->aDb[db->nDb];
       102374  +    memset(pNew, 0, sizeof(*pNew));
       102375  +  
       102376  +    /* Open the database file. If the btree is successfully opened, use
       102377  +    ** it to obtain the database schema. At this point the schema may
       102378  +    ** or may not be initialized.
       102379  +    */
       102380  +    flags = db->openFlags;
       102381  +    rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
       102382  +    if( rc!=SQLITE_OK ){
       102383  +      if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
       102384  +      sqlite3_result_error(context, zErr, -1);
       102385  +      sqlite3_free(zErr);
       102386  +      return;
       102387  +    }
       102388  +    assert( pVfs );
       102389  +    flags |= SQLITE_OPEN_MAIN_DB;
       102390  +    rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
       102391  +    sqlite3_free( zPath );
       102392  +    db->nDb++;
       102393  +  }
100850 102394     db->skipBtreeMutex = 0;
100851 102395     if( rc==SQLITE_CONSTRAINT ){
100852 102396       rc = SQLITE_ERROR;
100853 102397       zErrDyn = sqlite3MPrintf(db, "database is already attached");
100854 102398     }else if( rc==SQLITE_OK ){
100855 102399       Pager *pPager;
100856 102400       pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
................................................................................
100869 102413   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
100870 102414       sqlite3BtreeSetPagerFlags(pNew->pBt,
100871 102415                         PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
100872 102416   #endif
100873 102417       sqlite3BtreeLeave(pNew->pBt);
100874 102418     }
100875 102419     pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
100876         -  pNew->zDbSName = sqlite3DbStrDup(db, zName);
       102420  +  if( !REOPEN_AS_MEMDB(db) ) pNew->zDbSName = sqlite3DbStrDup(db, zName);
100877 102421     if( rc==SQLITE_OK && pNew->zDbSName==0 ){
100878 102422       rc = SQLITE_NOMEM_BKPT;
100879 102423     }
100880 102424   
100881 102425   
100882 102426   #ifdef SQLITE_HAS_CODEC
100883 102427     if( rc==SQLITE_OK ){
................................................................................
100909 102453           break;
100910 102454       }
100911 102455     }
100912 102456   #endif
100913 102457   
100914 102458     /* If the file was opened successfully, read the schema for the new database.
100915 102459     ** If this fails, or if opening the file failed, then close the file and 
100916         -  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
100917         -  ** we found it.
       102460  +  ** remove the entry from the db->aDb[] array. i.e. put everything back the
       102461  +  ** way we found it.
100918 102462     */
100919 102463     if( rc==SQLITE_OK ){
100920 102464       sqlite3BtreeEnterAll(db);
       102465  +    db->init.iDb = 0;
100921 102466       rc = sqlite3Init(db, &zErrDyn);
100922 102467       sqlite3BtreeLeaveAll(db);
       102468  +    assert( zErrDyn==0 || rc!=SQLITE_OK );
100923 102469     }
100924 102470   #ifdef SQLITE_USER_AUTHENTICATION
100925 102471     if( rc==SQLITE_OK ){
100926 102472       u8 newAuth = 0;
100927 102473       rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
100928 102474       if( newAuth<db->auth.authLevel ){
100929 102475         rc = SQLITE_AUTH_USER;
100930 102476       }
100931 102477     }
100932 102478   #endif
100933 102479     if( rc ){
100934         -    int iDb = db->nDb - 1;
100935         -    assert( iDb>=2 );
100936         -    if( db->aDb[iDb].pBt ){
100937         -      sqlite3BtreeClose(db->aDb[iDb].pBt);
100938         -      db->aDb[iDb].pBt = 0;
100939         -      db->aDb[iDb].pSchema = 0;
100940         -    }
100941         -    sqlite3ResetAllSchemasOfConnection(db);
100942         -    db->nDb = iDb;
100943         -    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
100944         -      sqlite3OomFault(db);
100945         -      sqlite3DbFree(db, zErrDyn);
100946         -      zErrDyn = sqlite3MPrintf(db, "out of memory");
100947         -    }else if( zErrDyn==0 ){
100948         -      zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
       102480  +    if( !REOPEN_AS_MEMDB(db) ){
       102481  +      int iDb = db->nDb - 1;
       102482  +      assert( iDb>=2 );
       102483  +      if( db->aDb[iDb].pBt ){
       102484  +        sqlite3BtreeClose(db->aDb[iDb].pBt);
       102485  +        db->aDb[iDb].pBt = 0;
       102486  +        db->aDb[iDb].pSchema = 0;
       102487  +      }
       102488  +      sqlite3ResetAllSchemasOfConnection(db);
       102489  +      db->nDb = iDb;
       102490  +      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
       102491  +        sqlite3OomFault(db);
       102492  +        sqlite3DbFree(db, zErrDyn);
       102493  +        zErrDyn = sqlite3MPrintf(db, "out of memory");
       102494  +      }else if( zErrDyn==0 ){
       102495  +        zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
       102496  +      }
100949 102497       }
100950 102498       goto attach_error;
100951 102499     }
100952 102500     
100953 102501     return;
100954 102502   
100955 102503   attach_error:
................................................................................
101212 102760         return 1;
101213 102761       }
101214 102762       if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
101215 102763         return 1;
101216 102764       }
101217 102765       if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
101218 102766         return 1;
       102767  +    }
       102768  +    if( pSelect->pWith ){
       102769  +      int i;
       102770  +      for(i=0; i<pSelect->pWith->nCte; i++){
       102771  +        if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
       102772  +          return 1;
       102773  +        }
       102774  +      }
101219 102775       }
101220 102776       pSelect = pSelect->pPrior;
101221 102777     }
101222 102778     return 0;
101223 102779   }
101224 102780   SQLITE_PRIVATE int sqlite3FixExpr(
101225 102781     DbFixer *pFix,     /* Context of the fixation */
................................................................................
102676 104232   ** This routine is called by the parser while in the middle of
102677 104233   ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
102678 104234   ** been seen on a column.  This routine sets the notNull flag on
102679 104235   ** the column currently under construction.
102680 104236   */
102681 104237   SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
102682 104238     Table *p;
       104239  +  Column *pCol;
102683 104240     p = pParse->pNewTable;
102684 104241     if( p==0 || NEVER(p->nCol<1) ) return;
102685         -  p->aCol[p->nCol-1].notNull = (u8)onError;
       104242  +  pCol = &p->aCol[p->nCol-1];
       104243  +  pCol->notNull = (u8)onError;
102686 104244     p->tabFlags |= TF_HasNotNull;
       104245  +
       104246  +  /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
       104247  +  ** on this column.  */
       104248  +  if( pCol->colFlags & COLFLAG_UNIQUE ){
       104249  +    Index *pIdx;
       104250  +    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
       104251  +      assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
       104252  +      if( pIdx->aiColumn[0]==p->nCol-1 ){
       104253  +        pIdx->uniqNotNull = 1;
       104254  +      }
       104255  +    }
       104256  +  }
102687 104257   }
102688 104258   
102689 104259   /*
102690 104260   ** Scan the column type name zType (length nType) and return the
102691 104261   ** associated affinity type.
102692 104262   **
102693 104263   ** This routine does a case-independent search of zType for the 
................................................................................
102796 104366     if( p!=0 ){
102797 104367       pCol = &(p->aCol[p->nCol-1]);
102798 104368       if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
102799 104369         sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
102800 104370             pCol->zName);
102801 104371       }else{
102802 104372         /* A copy of pExpr is used instead of the original, as pExpr contains
102803         -      ** tokens that point to volatile memory.	
       104373  +      ** tokens that point to volatile memory.
102804 104374         */
102805 104375         Expr x;
102806 104376         sqlite3ExprDelete(db, pCol->pDflt);
102807 104377         memset(&x, 0, sizeof(x));
102808 104378         x.op = TK_SPAN;
102809 104379         x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
102810 104380         x.pLeft = pExpr;
................................................................................
103414 104984     if( pEnd==0 && pSelect==0 ){
103415 104985       return;
103416 104986     }
103417 104987     assert( !db->mallocFailed );
103418 104988     p = pParse->pNewTable;
103419 104989     if( p==0 ) return;
103420 104990   
103421         -  assert( !db->init.busy || !pSelect );
103422         -
103423 104991     /* If the db->init.busy is 1 it means we are reading the SQL off the
103424 104992     ** "sqlite_master" or "sqlite_temp_master" table on the disk.
103425 104993     ** So do not write to the disk again.  Extract the root page number
103426 104994     ** for the table from the db->init.newTnum field.  (The page number
103427 104995     ** should have been put there by the sqliteOpenCb routine.)
103428 104996     **
103429 104997     ** If the root page number is 1, that means this is the sqlite_master
103430 104998     ** table itself.  So mark it read-only.
103431 104999     */
103432 105000     if( db->init.busy ){
       105001  +    if( pSelect ){
       105002  +      sqlite3ErrorMsg(pParse, "");
       105003  +      return;
       105004  +    }
103433 105005       p->tnum = db->init.newTnum;
103434 105006       if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
103435 105007     }
103436 105008   
103437 105009     /* Special processing for WITHOUT ROWID Tables */
103438 105010     if( tabOpts & TF_WithoutRowid ){
103439 105011       if( (p->tabFlags & TF_Autoincrement) ){
................................................................................
103716 105288   */
103717 105289   SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
103718 105290     Table *pSelTab;   /* A fake table from which we get the result set */
103719 105291     Select *pSel;     /* Copy of the SELECT that implements the view */
103720 105292     int nErr = 0;     /* Number of errors encountered */
103721 105293     int n;            /* Temporarily holds the number of cursors assigned */
103722 105294     sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
103723         -#ifndef SQLITE_OMIT_VIRTUALTABLE	
       105295  +#ifndef SQLITE_OMIT_VIRTUALTABLE
103724 105296     int rc;
103725 105297   #endif
103726 105298   #ifndef SQLITE_OMIT_AUTHORIZATION
103727 105299     sqlite3_xauth xAuth;       /* Saved xAuth pointer */
103728 105300   #endif
103729 105301   
103730 105302     assert( pTable );
................................................................................
104643 106215   
104644 106216     /* If pList==0, it means this routine was called to make a primary
104645 106217     ** key out of the last column added to the table under construction.
104646 106218     ** So create a fake list to simulate this.
104647 106219     */
104648 106220     if( pList==0 ){
104649 106221       Token prevCol;
104650         -    sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName);
       106222  +    Column *pCol = &pTab->aCol[pTab->nCol-1];
       106223  +    pCol->colFlags |= COLFLAG_UNIQUE;
       106224  +    sqlite3TokenInit(&prevCol, pCol->zName);
104651 106225       pList = sqlite3ExprListAppend(pParse, 0,
104652 106226                 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
104653 106227       if( pList==0 ) goto exit_create_index;
104654 106228       assert( pList->nExpr==1 );
104655 106229       sqlite3ExprListSetSortOrder(pList, sortOrder);
104656 106230     }else{
104657 106231       sqlite3ExprListCheckLength(pParse, pList, "index");
................................................................................
107488 109062   }
107489 109063   
107490 109064   /*
107491 109065   ** Indicate that the accumulator load should be skipped on this
107492 109066   ** iteration of the aggregate loop.
107493 109067   */
107494 109068   static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
       109069  +  assert( context->isError<=0 );
       109070  +  context->isError = -1;
107495 109071     context->skipFlag = 1;
107496 109072   }
107497 109073   
107498 109074   /*
107499 109075   ** Implementation of the non-aggregate min() and max() functions
107500 109076   */
107501 109077   static void minmaxFunc(
................................................................................
107554 109130   ** Implementation of the length() function
107555 109131   */
107556 109132   static void lengthFunc(
107557 109133     sqlite3_context *context,
107558 109134     int argc,
107559 109135     sqlite3_value **argv
107560 109136   ){
107561         -  int len;
107562         -
107563 109137     assert( argc==1 );
107564 109138     UNUSED_PARAMETER(argc);
107565 109139     switch( sqlite3_value_type(argv[0]) ){
107566 109140       case SQLITE_BLOB:
107567 109141       case SQLITE_INTEGER:
107568 109142       case SQLITE_FLOAT: {
107569 109143         sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
107570 109144         break;
107571 109145       }
107572 109146       case SQLITE_TEXT: {
107573 109147         const unsigned char *z = sqlite3_value_text(argv[0]);
       109148  +      const unsigned char *z0;
       109149  +      unsigned char c;
107574 109150         if( z==0 ) return;
107575         -      len = 0;
107576         -      while( *z ){
107577         -        len++;
107578         -        SQLITE_SKIP_UTF8(z);
       109151  +      z0 = z;
       109152  +      while( (c = *z)!=0 ){
       109153  +        z++;
       109154  +        if( c>=0xc0 ){
       109155  +          while( (*z & 0xc0)==0x80 ){ z++; z0++; }
       109156  +        }
107579 109157         }
107580         -      sqlite3_result_int(context, len);
       109158  +      sqlite3_result_int(context, (int)(z-z0));
107581 109159         break;
107582 109160       }
107583 109161       default: {
107584 109162         sqlite3_result_null(context);
107585 109163         break;
107586 109164       }
107587 109165     }
................................................................................
108648 110226     unsigned char *zOut;              /* The output */
108649 110227     int nStr;                /* Size of zStr */
108650 110228     int nPattern;            /* Size of zPattern */
108651 110229     int nRep;                /* Size of zRep */
108652 110230     i64 nOut;                /* Maximum size of zOut */
108653 110231     int loopLimit;           /* Last zStr[] that might match zPattern[] */
108654 110232     int i, j;                /* Loop counters */
       110233  +  unsigned cntExpand;      /* Number zOut expansions */
       110234  +  sqlite3 *db = sqlite3_context_db_handle(context);
108655 110235   
108656 110236     assert( argc==3 );
108657 110237     UNUSED_PARAMETER(argc);
108658 110238     zStr = sqlite3_value_text(argv[0]);
108659 110239     if( zStr==0 ) return;
108660 110240     nStr = sqlite3_value_bytes(argv[0]);
108661 110241     assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
................................................................................
108679 110259     nOut = nStr + 1;
108680 110260     assert( nOut<SQLITE_MAX_LENGTH );
108681 110261     zOut = contextMalloc(context, (i64)nOut);
108682 110262     if( zOut==0 ){
108683 110263       return;
108684 110264     }
108685 110265     loopLimit = nStr - nPattern;  
       110266  +  cntExpand = 0;
108686 110267     for(i=j=0; i<=loopLimit; i++){
108687 110268       if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
108688 110269         zOut[j++] = zStr[i];
108689 110270       }else{
108690         -      u8 *zOld;
108691         -      sqlite3 *db = sqlite3_context_db_handle(context);
108692         -      nOut += nRep - nPattern;
108693         -      testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
108694         -      testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
108695         -      if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108696         -        sqlite3_result_error_toobig(context);
108697         -        sqlite3_free(zOut);
108698         -        return;
108699         -      }
108700         -      zOld = zOut;
108701         -      zOut = sqlite3_realloc64(zOut, (int)nOut);
108702         -      if( zOut==0 ){
108703         -        sqlite3_result_error_nomem(context);
108704         -        sqlite3_free(zOld);
108705         -        return;
       110271  +      if( nRep>nPattern ){
       110272  +        nOut += nRep - nPattern;
       110273  +        testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
       110274  +        testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
       110275  +        if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
       110276  +          sqlite3_result_error_toobig(context);
       110277  +          sqlite3_free(zOut);
       110278  +          return;
       110279  +        }
       110280  +        cntExpand++;
       110281  +        if( (cntExpand&(cntExpand-1))==0 ){
       110282  +          /* Grow the size of the output buffer only on substitutions
       110283  +          ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
       110284  +          u8 *zOld;
       110285  +          zOld = zOut;
       110286  +          zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1));
       110287  +          if( zOut==0 ){
       110288  +            sqlite3_result_error_nomem(context);
       110289  +            sqlite3_free(zOld);
       110290  +            return;
       110291  +          }
       110292  +        }
108706 110293         }
108707 110294         memcpy(&zOut[j], zRep, nRep);
108708 110295         j += nRep;
108709 110296         i += nPattern-1;
108710 110297       }
108711 110298     }
108712         -  assert( j+nStr-i+1==nOut );
       110299  +  assert( j+nStr-i+1<=nOut );
108713 110300     memcpy(&zOut[j], &zStr[i], nStr-i);
108714 110301     j += nStr - i;
108715 110302     assert( j<=nOut );
108716 110303     zOut[j] = 0;
108717 110304     sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
108718 110305   }
108719 110306   
................................................................................
110987 112574   **
110988 112575   ** There is at most one AutoincInfo structure per table even if the
110989 112576   ** same table is autoincremented multiple times due to inserts within
110990 112577   ** triggers.  A new AutoincInfo structure is created if this is the
110991 112578   ** first use of table pTab.  On 2nd and subsequent uses, the original
110992 112579   ** AutoincInfo structure is used.
110993 112580   **
110994         -** Three memory locations are allocated:
       112581  +** Four consecutive registers are allocated:
110995 112582   **
110996         -**   (1)  Register to hold the name of the pTab table.
110997         -**   (2)  Register to hold the maximum ROWID of pTab.
110998         -**   (3)  Register to hold the rowid in sqlite_sequence of pTab
       112583  +**   (1)  The name of the pTab table.
       112584  +**   (2)  The maximum ROWID of pTab.
       112585  +**   (3)  The rowid in sqlite_sequence of pTab
       112586  +**   (4)  The original value of the max ROWID in pTab, or NULL if none
110999 112587   **
111000 112588   ** The 2nd register is the one that is returned.  That is all the
111001 112589   ** insert routine needs to know about.
111002 112590   */
111003 112591   static int autoIncBegin(
111004 112592     Parse *pParse,      /* Parsing context */
111005 112593     int iDb,            /* Index of the database holding pTab */
................................................................................
111019 112607         if( pInfo==0 ) return 0;
111020 112608         pInfo->pNext = pToplevel->pAinc;
111021 112609         pToplevel->pAinc = pInfo;
111022 112610         pInfo->pTab = pTab;
111023 112611         pInfo->iDb = iDb;
111024 112612         pToplevel->nMem++;                  /* Register to hold name of table */
111025 112613         pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
111026         -      pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
       112614  +      pToplevel->nMem +=2;       /* Rowid in sqlite_sequence + orig max val */
111027 112615       }
111028 112616       memId = pInfo->regCtr;
111029 112617     }
111030 112618     return memId;
111031 112619   }
111032 112620   
111033 112621   /*
................................................................................
111047 112635     assert( sqlite3IsToplevel(pParse) );
111048 112636   
111049 112637     assert( v );   /* We failed long ago if this is not so */
111050 112638     for(p = pParse->pAinc; p; p = p->pNext){
111051 112639       static const int iLn = VDBE_OFFSET_LINENO(2);
111052 112640       static const VdbeOpList autoInc[] = {
111053 112641         /* 0  */ {OP_Null,    0,  0, 0},
111054         -      /* 1  */ {OP_Rewind,  0,  9, 0},
       112642  +      /* 1  */ {OP_Rewind,  0, 10, 0},
111055 112643         /* 2  */ {OP_Column,  0,  0, 0},
111056         -      /* 3  */ {OP_Ne,      0,  7, 0},
       112644  +      /* 3  */ {OP_Ne,      0,  9, 0},
111057 112645         /* 4  */ {OP_Rowid,   0,  0, 0},
111058 112646         /* 5  */ {OP_Column,  0,  1, 0},
111059         -      /* 6  */ {OP_Goto,    0,  9, 0},
111060         -      /* 7  */ {OP_Next,    0,  2, 0},
111061         -      /* 8  */ {OP_Integer, 0,  0, 0},
111062         -      /* 9  */ {OP_Close,   0,  0, 0} 
       112647  +      /* 6  */ {OP_AddImm,  0,  0, 0},
       112648  +      /* 7  */ {OP_Copy,    0,  0, 0},
       112649  +      /* 8  */ {OP_Goto,    0, 11, 0},
       112650  +      /* 9  */ {OP_Next,    0,  2, 0},
       112651  +      /* 10 */ {OP_Integer, 0,  0, 0},
       112652  +      /* 11 */ {OP_Close,   0,  0, 0} 
111063 112653       };
111064 112654       VdbeOp *aOp;
111065 112655       pDb = &db->aDb[p->iDb];
111066 112656       memId = p->regCtr;
111067 112657       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
111068 112658       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
111069 112659       sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
111070 112660       aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
111071 112661       if( aOp==0 ) break;
111072 112662       aOp[0].p2 = memId;
111073         -    aOp[0].p3 = memId+1;
       112663  +    aOp[0].p3 = memId+2;
111074 112664       aOp[2].p3 = memId;
111075 112665       aOp[3].p1 = memId-1;
111076 112666       aOp[3].p3 = memId;
111077 112667       aOp[3].p5 = SQLITE_JUMPIFNULL;
111078 112668       aOp[4].p2 = memId+1;
111079 112669       aOp[5].p3 = memId;
111080         -    aOp[8].p2 = memId;
       112670  +    aOp[6].p1 = memId;
       112671  +    aOp[7].p2 = memId+2;
       112672  +    aOp[7].p1 = memId;
       112673  +    aOp[10].p2 = memId;
111081 112674     }
111082 112675   }
111083 112676   
111084 112677   /*
111085 112678   ** Update the maximum rowid for an autoincrement calculation.
111086 112679   **
111087 112680   ** This routine should be called when the regRowid register holds a
................................................................................
111120 112713       VdbeOp *aOp;
111121 112714       Db *pDb = &db->aDb[p->iDb];
111122 112715       int iRec;
111123 112716       int memId = p->regCtr;
111124 112717   
111125 112718       iRec = sqlite3GetTempReg(pParse);
111126 112719       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
       112720  +    sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
       112721  +    VdbeCoverage(v);
111127 112722       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
111128 112723       aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
111129 112724       if( aOp==0 ) break;
111130 112725       aOp[0].p1 = memId+1;
111131 112726       aOp[1].p2 = memId+1;
111132 112727       aOp[2].p1 = memId-1;
111133 112728       aOp[2].p3 = iRec;
................................................................................
113499 115094     int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
113500 115095                         sqlite3_stmt**,const void**);
113501 115096     int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
113502 115097     void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
113503 115098     void *(*value_pointer)(sqlite3_value*,const char*);
113504 115099     int (*vtab_nochange)(sqlite3_context*);
113505 115100     int (*value_nochange)(sqlite3_value*);
       115101  +  const char *(*vtab_collation)(sqlite3_index_info*,int);
113506 115102   };
113507 115103   
113508 115104   /*
113509 115105   ** This is the function signature used for all extension entry points.  It
113510 115106   ** is also defined in the file "loadext.c".
113511 115107   */
113512 115108   typedef int (*sqlite3_loadext_entry)(
................................................................................
113767 115363   #define sqlite3_prepare_v3             sqlite3_api->prepare_v3
113768 115364   #define sqlite3_prepare16_v3           sqlite3_api->prepare16_v3
113769 115365   #define sqlite3_bind_pointer           sqlite3_api->bind_pointer
113770 115366   #define sqlite3_result_pointer         sqlite3_api->result_pointer
113771 115367   #define sqlite3_value_pointer          sqlite3_api->value_pointer
113772 115368   /* Version 3.22.0 and later */
113773 115369   #define sqlite3_vtab_nochange          sqlite3_api->vtab_nochange
113774         -#define sqlite3_value_nochange         sqltie3_api->value_nochange
       115370  +#define sqlite3_value_nochange         sqlite3_api->value_nochange
       115371  +#define sqlite3_vtab_collation         sqlite3_api->vtab_collation
113775 115372   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
113776 115373   
113777 115374   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
113778 115375     /* This case when the file really is being compiled as a loadable 
113779 115376     ** extension */
113780 115377   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
113781 115378   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
114205 115802     sqlite3_prepare_v3,
114206 115803     sqlite3_prepare16_v3,
114207 115804     sqlite3_bind_pointer,
114208 115805     sqlite3_result_pointer,
114209 115806     sqlite3_value_pointer,
114210 115807     /* Version 3.22.0 and later */
114211 115808     sqlite3_vtab_nochange,
114212         -  sqlite3_value_nochange
       115809  +  sqlite3_value_nochange,
       115810  +  sqlite3_vtab_collation
114213 115811   };
114214 115812   
114215 115813   /*
114216 115814   ** Attempt to load an SQLite extension library contained in the file
114217 115815   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
114218 115816   ** default entry point name (sqlite3_extension_init) is used.  Use
114219 115817   ** of the default name is recommended.
................................................................................
117750 119348     const char *zExtra   /* Error information */
117751 119349   ){
117752 119350     sqlite3 *db = pData->db;
117753 119351     if( !db->mallocFailed && (db->flags & SQLITE_WriteSchema)==0 ){
117754 119352       char *z;
117755 119353       if( zObj==0 ) zObj = "?";
117756 119354       z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
117757         -    if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
       119355  +    if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
117758 119356       sqlite3DbFree(db, *pData->pzErrMsg);
117759 119357       *pData->pzErrMsg = z;
117760 119358     }
117761 119359     pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT;
117762 119360   }
117763 119361   
117764 119362   /*
................................................................................
118644 120242   /*
118645 120243   ** Trace output macros
118646 120244   */
118647 120245   #if SELECTTRACE_ENABLED
118648 120246   /***/ int sqlite3SelectTrace = 0;
118649 120247   # define SELECTTRACE(K,P,S,X)  \
118650 120248     if(sqlite3SelectTrace&(K))   \
118651         -    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
118652         -        (S)->zSelName,(S)),\
       120249  +    sqlite3DebugPrintf("%s/%p: ",(S)->zSelName,(S)),\
118653 120250       sqlite3DebugPrintf X
118654 120251   #else
118655 120252   # define SELECTTRACE(K,P,S,X)
118656 120253   #endif
118657 120254   
118658 120255   
118659 120256   /*
................................................................................
119005 120602           setJoinExpr(p->x.pList->a[i].pExpr, iTable);
119006 120603         }
119007 120604       }
119008 120605       setJoinExpr(p->pLeft, iTable);
119009 120606       p = p->pRight;
119010 120607     } 
119011 120608   }
       120609  +
       120610  +/* Undo the work of setJoinExpr().  In the expression tree p, convert every
       120611  +** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
       120612  +** an ordinary term that omits the EP_FromJoin mark.
       120613  +**
       120614  +** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
       120615  +*/
       120616  +static void unsetJoinExpr(Expr *p, int iTable){
       120617  +  while( p ){
       120618  +    if( ExprHasProperty(p, EP_FromJoin)
       120619  +     && (iTable<0 || p->iRightJoinTable==iTable) ){
       120620  +      ExprClearProperty(p, EP_FromJoin);
       120621  +    }
       120622  +    if( p->op==TK_FUNCTION && p->x.pList ){
       120623  +      int i;
       120624  +      for(i=0; i<p->x.pList->nExpr; i++){
       120625  +        unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
       120626  +      }
       120627  +    }
       120628  +    unsetJoinExpr(p->pLeft, iTable);
       120629  +    p = p->pRight;
       120630  +  } 
       120631  +}
119012 120632   
119013 120633   /*
119014 120634   ** This routine processes the join information for a SELECT statement.
119015 120635   ** ON and USING clauses are converted into extra terms of the WHERE clause.
119016 120636   ** NATURAL joins also create extra WHERE clause terms.
119017 120637   **
119018 120638   ** The terms of a FROM clause are contained in the Select.pSrc structure.
................................................................................
119889 121509       bSeq = 0;
119890 121510     }else{
119891 121511       addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
119892 121512       codeOffset(v, p->iOffset, addrContinue);
119893 121513       iSortTab = iTab;
119894 121514       bSeq = 1;
119895 121515     }
119896         -  for(i=0, iCol=nKey+bSeq; i<nSortData; i++){
       121516  +  for(i=0, iCol=nKey+bSeq-1; i<nSortData; i++){
       121517  +    if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
       121518  +  }
       121519  +  for(i=nSortData-1; i>=0; i--){
119897 121520       int iRead;
119898 121521       if( aOutEx[i].u.x.iOrderByCol ){
119899 121522         iRead = aOutEx[i].u.x.iOrderByCol-1;
119900 121523       }else{
119901         -      iRead = iCol++;
       121524  +      iRead = iCol--;
119902 121525       }
119903 121526       sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
119904 121527       VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
119905 121528     }
119906 121529     switch( eDest ){
119907 121530       case SRT_Table:
119908 121531       case SRT_EphemTab: {
................................................................................
122455 124078   **           to suppress it. **)
122456 124079   **
122457 124080   **   (2) The inner query is the recursive part of a common table expression.
122458 124081   **
122459 124082   **   (3) The inner query has a LIMIT clause (since the changes to the WHERE
122460 124083   **       close would change the meaning of the LIMIT).
122461 124084   **
122462         -**   (4) The inner query is the right operand of a LEFT JOIN.  (The caller
122463         -**       enforces this restriction since this routine does not have enough
122464         -**       information to know.)
       124085  +**   (4) The inner query is the right operand of a LEFT JOIN and the
       124086  +**       expression to be pushed down does not come from the ON clause
       124087  +**       on that LEFT JOIN.
122465 124088   **
122466 124089   **   (5) The WHERE clause expression originates in the ON or USING clause
122467         -**       of a LEFT JOIN.
       124090  +**       of a LEFT JOIN where iCursor is not the right-hand table of that
       124091  +**       left join.  An example:
       124092  +**
       124093  +**           SELECT *
       124094  +**           FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
       124095  +**           JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
       124096  +**           LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
       124097  +**
       124098  +**       The correct answer is three rows:  (1,1,NULL),(2,2,8),(2,2,9).
       124099  +**       But if the (b2=2) term were to be pushed down into the bb subquery,
       124100  +**       then the (1,1,NULL) row would be suppressed.
122468 124101   **
122469 124102   ** Return 0 if no changes are made and non-zero if one or more WHERE clause
122470 124103   ** terms are duplicated into the subquery.
122471 124104   */
122472 124105   static int pushDownWhereTerms(
122473 124106     Parse *pParse,        /* Parse context (for malloc() and error reporting) */
122474 124107     Select *pSubq,        /* The subquery whose WHERE clause is to be augmented */
122475 124108     Expr *pWhere,         /* The WHERE clause of the outer query */
122476         -  int iCursor           /* Cursor number of the subquery */
       124109  +  int iCursor,          /* Cursor number of the subquery */
       124110  +  int isLeftJoin        /* True if pSubq is the right term of a LEFT JOIN */
122477 124111   ){
122478 124112     Expr *pNew;
122479 124113     int nChng = 0;
122480 124114     if( pWhere==0 ) return 0;
122481 124115     if( pSubq->selFlags & SF_Recursive ) return 0;  /* restriction (2) */
122482 124116   
122483 124117   #ifdef SQLITE_DEBUG
................................................................................
122493 124127     }
122494 124128   #endif
122495 124129   
122496 124130     if( pSubq->pLimit!=0 ){
122497 124131       return 0; /* restriction (3) */
122498 124132     }
122499 124133     while( pWhere->op==TK_AND ){
122500         -    nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, iCursor);
       124134  +    nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
       124135  +                                iCursor, isLeftJoin);
122501 124136       pWhere = pWhere->pLeft;
122502 124137     }
122503         -  if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction (5) */
       124138  +  if( isLeftJoin
       124139  +   && (ExprHasProperty(pWhere,EP_FromJoin)==0
       124140  +         || pWhere->iRightJoinTable!=iCursor)
       124141  +  ){
       124142  +    return 0; /* restriction (4) */
       124143  +  }
       124144  +  if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
       124145  +    return 0; /* restriction (5) */
       124146  +  }
122504 124147     if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
122505 124148       nChng++;
122506 124149       while( pSubq ){
122507 124150         SubstContext x;
122508 124151         pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
       124152  +      unsetJoinExpr(pNew, -1);
122509 124153         x.pParse = pParse;
122510 124154         x.iTable = iCursor;
122511 124155         x.iNewTable = iCursor;
122512 124156         x.isLeftJoin = 0;
122513 124157         x.pEList = pSubq->pEList;
122514 124158         pNew = substExpr(&x, pNew);
122515 124159         if( pSubq->selFlags & SF_Aggregate ){
................................................................................
123533 125177       );
123534 125178     }
123535 125179   }
123536 125180   #else
123537 125181   # define explainSimpleCount(a,b,c)
123538 125182   #endif
123539 125183   
123540         -/*
123541         -** Context object for havingToWhereExprCb().
123542         -*/
123543         -struct HavingToWhereCtx {
123544         -  Expr **ppWhere;
123545         -  ExprList *pGroupBy;
123546         -};
123547         -
123548 125184   /*
123549 125185   ** sqlite3WalkExpr() callback used by havingToWhere().
123550 125186   **
123551 125187   ** If the node passed to the callback is a TK_AND node, return 
123552 125188   ** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
123553 125189   **
123554 125190   ** Otherwise, return WRC_Prune. In this case, also check if the 
123555 125191   ** sub-expression matches the criteria for being moved to the WHERE
123556 125192   ** clause. If so, add it to the WHERE clause and replace the sub-expression
123557 125193   ** within the HAVING expression with a constant "1".
123558 125194   */
123559 125195   static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
123560 125196     if( pExpr->op!=TK_AND ){
123561         -    struct HavingToWhereCtx *p = pWalker->u.pHavingCtx;
123562         -    if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, p->pGroupBy) ){
       125197  +    Select *pS = pWalker->u.pSelect;
       125198  +    if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
123563 125199         sqlite3 *db = pWalker->pParse->db;
123564 125200         Expr *pNew = sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[1], 0);
123565 125201         if( pNew ){
123566         -        Expr *pWhere = *(p->ppWhere);
       125202  +        Expr *pWhere = pS->pWhere;
123567 125203           SWAP(Expr, *pNew, *pExpr);
123568 125204           pNew = sqlite3ExprAnd(db, pWhere, pNew);
123569         -        *(p->ppWhere) = pNew;
       125205  +        pS->pWhere = pNew;
       125206  +        pWalker->eCode = 1;
123570 125207         }
123571 125208       }
123572 125209       return WRC_Prune;
123573 125210     }
123574 125211     return WRC_Continue;
123575 125212   }
123576 125213   
................................................................................
123585 125222   **
123586 125223   **   SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
123587 125224   **
123588 125225   ** A term of the HAVING expression is eligible for transfer if it consists
123589 125226   ** entirely of constants and expressions that are also GROUP BY terms that
123590 125227   ** use the "BINARY" collation sequence.
123591 125228   */
123592         -static void havingToWhere(
123593         -  Parse *pParse,
123594         -  ExprList *pGroupBy,
123595         -  Expr *pHaving, 
123596         -  Expr **ppWhere
123597         -){
123598         -  struct HavingToWhereCtx sCtx;
       125229  +static void havingToWhere(Parse *pParse, Select *p){
123599 125230     Walker sWalker;
123600         -
123601         -  sCtx.ppWhere = ppWhere;
123602         -  sCtx.pGroupBy = pGroupBy;
123603         -
123604 125231     memset(&sWalker, 0, sizeof(sWalker));
123605 125232     sWalker.pParse = pParse;
123606 125233     sWalker.xExprCallback = havingToWhereExprCb;
123607         -  sWalker.u.pHavingCtx = &sCtx;
123608         -  sqlite3WalkExpr(&sWalker, pHaving);
       125234  +  sWalker.u.pSelect = p;
       125235  +  sqlite3WalkExpr(&sWalker, p->pHaving);
       125236  +#if SELECTTRACE_ENABLED
       125237  +  if( sWalker.eCode && (sqlite3SelectTrace & 0x100)!=0 ){
       125238  +    SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
       125239  +    sqlite3TreeViewSelect(0, p, 0);
       125240  +  }
       125241  +#endif
123609 125242   }
123610 125243   
123611 125244   /*
123612 125245   ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
123613 125246   ** If it is, then return the SrcList_item for the prior view.  If it is not,
123614 125247   ** then return 0.
123615 125248   */
................................................................................
123762 125395     db = pParse->db;
123763 125396     if( p==0 || db->mallocFailed || pParse->nErr ){
123764 125397       return 1;
123765 125398     }
123766 125399     if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
123767 125400     memset(&sAggInfo, 0, sizeof(sAggInfo));
123768 125401   #if SELECTTRACE_ENABLED
123769         -  pParse->nSelectIndent++;
123770 125402     SELECTTRACE(1,pParse,p, ("begin processing:\n"));
123771 125403     if( sqlite3SelectTrace & 0x100 ){
123772 125404       sqlite3TreeViewSelect(0, p, 0);
123773 125405     }
123774 125406   #endif
123775 125407   
123776 125408     assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
................................................................................
123808 125440     ** does not already exist */
123809 125441     v = sqlite3GetVdbe(pParse);
123810 125442     if( v==0 ) goto select_end;
123811 125443     if( pDest->eDest==SRT_Output ){
123812 125444       generateColumnNames(pParse, p);
123813 125445     }
123814 125446   
123815         -  /* Try to flatten subqueries in the FROM clause up into the main query
       125447  +  /* Try to various optimizations (flattening subqueries, and strength
       125448  +  ** reduction of join operators) in the FROM clause up into the main query
123816 125449     */
123817 125450   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
123818 125451     for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
123819 125452       struct SrcList_item *pItem = &pTabList->a[i];
123820 125453       Select *pSub = pItem->pSelect;
123821 125454       Table *pTab = pItem->pTab;
       125455  +
       125456  +    /* Convert LEFT JOIN into JOIN if there are terms of the right table
       125457  +    ** of the LEFT JOIN used in the WHERE clause.
       125458  +    */
       125459  +    if( (pItem->fg.jointype & JT_LEFT)!=0
       125460  +     && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
       125461  +     && OptimizationEnabled(db, SQLITE_SimplifyJoin)
       125462  +    ){
       125463  +      SELECTTRACE(0x100,pParse,p,
       125464  +                ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
       125465  +      pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
       125466  +      unsetJoinExpr(p->pWhere, pItem->iCursor);
       125467  +    }
       125468  +
       125469  +    /* No futher action if this term of the FROM clause is no a subquery */
123822 125470       if( pSub==0 ) continue;
123823 125471   
123824 125472       /* Catch mismatch in the declared columns of a view and the number of
123825 125473       ** columns in the SELECT on the RHS */
123826 125474       if( pTab->nCol!=pSub->pEList->nExpr ){
123827 125475         sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
123828 125476                         pTab->nCol, pTab->zName, pSub->pEList->nExpr);
................................................................................
123883 125531     ** procedure.
123884 125532     */
123885 125533     if( p->pPrior ){
123886 125534       rc = multiSelect(pParse, p, pDest);
123887 125535       explainSetInteger(pParse->iSelectId, iRestoreSelectId);
123888 125536   #if SELECTTRACE_ENABLED
123889 125537       SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
123890         -    pParse->nSelectIndent--;
123891 125538   #endif
123892 125539       return rc;
123893 125540     }
123894 125541   #endif
123895 125542   
123896 125543     /* For each term in the FROM clause, do two things:
123897 125544     ** (1) Authorized unreferenced tables
................................................................................
123956 125603       ** an exact limit.
123957 125604       */
123958 125605       pParse->nHeight += sqlite3SelectExprHeight(p);
123959 125606   
123960 125607       /* Make copies of constant WHERE-clause terms in the outer query down
123961 125608       ** inside the subquery.  This can help the subquery to run more efficiently.
123962 125609       */
123963         -    if( (pItem->fg.jointype & JT_OUTER)==0
123964         -     && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor)
       125610  +    if( OptimizationEnabled(db, SQLITE_PushDown)
       125611  +     && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
       125612  +                           (pItem->fg.jointype & JT_OUTER)!=0)
123965 125613       ){
123966 125614   #if SELECTTRACE_ENABLED
123967 125615         if( sqlite3SelectTrace & 0x100 ){
123968 125616           SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
123969 125617           sqlite3TreeViewSelect(0, p, 0);
123970 125618         }
123971 125619   #endif
       125620  +    }else{
       125621  +      SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
123972 125622       }
123973 125623   
123974 125624       zSavedAuthContext = pParse->zAuthContext;
123975 125625       pParse->zAuthContext = pItem->zName;
123976 125626   
123977 125627       /* Generate code to implement the subquery
123978 125628       **
................................................................................
124167 125817     if( !isAgg && pGroupBy==0 ){
124168 125818       /* No aggregate functions and no GROUP BY clause */
124169 125819       u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
124170 125820       assert( WHERE_USE_LIMIT==SF_FixedLimit );
124171 125821       wctrlFlags |= p->selFlags & SF_FixedLimit;
124172 125822   
124173 125823       /* Begin the database scan. */
       125824  +    SELECTTRACE(1,pParse,p,("WhereBegin\n"));
124174 125825       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
124175 125826                                  p->pEList, wctrlFlags, p->nSelectRow);
124176 125827       if( pWInfo==0 ) goto select_end;
124177 125828       if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
124178 125829         p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
124179 125830       }
124180 125831       if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
................................................................................
124268 125919       sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
124269 125920       sAggInfo.pGroupBy = pGroupBy;
124270 125921       sqlite3ExprAnalyzeAggList(&sNC, pEList);
124271 125922       sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
124272 125923       if( pHaving ){
124273 125924         if( pGroupBy ){
124274 125925           assert( pWhere==p->pWhere );
124275         -        havingToWhere(pParse, pGroupBy, pHaving, &p->pWhere);
       125926  +        assert( pHaving==p->pHaving );
       125927  +        assert( pGroupBy==p->pGroupBy );
       125928  +        havingToWhere(pParse, p);
124276 125929           pWhere = p->pWhere;
124277 125930         }
124278 125931         sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
124279 125932       }
124280 125933       sAggInfo.nAccumulator = sAggInfo.nColumn;
124281 125934       if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
124282 125935         minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
................................................................................
124355 126008   
124356 126009         /* Begin a loop that will extract all source rows in GROUP BY order.
124357 126010         ** This might involve two separate loops with an OP_Sort in between, or
124358 126011         ** it might be a single loop that uses an index to extract information
124359 126012         ** in the right order to begin with.
124360 126013         */
124361 126014         sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
       126015  +      SELECTTRACE(1,pParse,p,("WhereBegin\n"));
124362 126016         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
124363 126017             WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
124364 126018         );
124365 126019         if( pWInfo==0 ) goto select_end;
124366 126020         if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
124367 126021           /* The optimizer is able to deliver rows in group by order so
124368 126022           ** we do not have to sort.  The OP_OpenEphemeral table will be
................................................................................
124610 126264           ** minMaxFlag will have been previously set to either
124611 126265           ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
124612 126266           ** be an appropriate ORDER BY expression for the optimization.
124613 126267           */
124614 126268           assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
124615 126269           assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
124616 126270   
       126271  +        SELECTTRACE(1,pParse,p,("WhereBegin\n"));
124617 126272           pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
124618 126273                                      0, minMaxFlag, 0);
124619 126274           if( pWInfo==0 ){
124620 126275             goto select_end;
124621 126276           }
124622 126277           updateAccumulator(pParse, &sAggInfo);
124623 126278           if( sqlite3WhereIsOrdered(pWInfo)>0 ){
................................................................................
124665 126320   select_end:
124666 126321     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
124667 126322     sqlite3ExprListDelete(db, pMinMaxOrderBy);
124668 126323     sqlite3DbFree(db, sAggInfo.aCol);
124669 126324     sqlite3DbFree(db, sAggInfo.aFunc);
124670 126325   #if SELECTTRACE_ENABLED
124671 126326     SELECTTRACE(1,pParse,p,("end processing\n"));
124672         -  pParse->nSelectIndent--;
124673 126327   #endif
124674 126328     return rc;
124675 126329   }
124676 126330   
124677 126331   /************** End of select.c **********************************************/
124678 126332   /************** Begin file table.c *******************************************/
124679 126333   /*
................................................................................
126961 128615   
126962 128616     /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
126963 128617     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
126964 128618     if( rc!=SQLITE_OK ) return rc;
126965 128619     while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
126966 128620       const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
126967 128621       assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
126968         -    if( zSubSql ){
126969         -      assert( zSubSql[0]!='S' );
       128622  +    assert( sqlite3_strnicmp(zSubSql,"SELECT",6)!=0 || CORRUPT_DB );
       128623  +    if( zSubSql && zSubSql[0]!='S' ){
126970 128624         rc = execSql(db, pzErrMsg, zSubSql);
126971 128625         if( rc!=SQLITE_OK ) break;
126972 128626       }
126973 128627     }
126974 128628     assert( rc!=SQLITE_ROW );
126975 128629     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
126976 128630     if( rc ){
................................................................................
130500 132154         testcase( pStart->wtFlags & TERM_VIRTUAL );
130501 132155         pX = pStart->pExpr;
130502 132156         assert( pX!=0 );
130503 132157         testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
130504 132158         if( sqlite3ExprIsVector(pX->pRight) ){
130505 132159           r1 = rTemp = sqlite3GetTempReg(pParse);
130506 132160           codeExprOrVector(pParse, pX->pRight, r1, 1);
130507         -        op = aMoveOp[(pX->op - TK_GT) | 0x0001];
       132161  +        testcase( pX->op==TK_GT );
       132162  +        testcase( pX->op==TK_GE );
       132163  +        testcase( pX->op==TK_LT );
       132164  +        testcase( pX->op==TK_LE );
       132165  +        op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
       132166  +        assert( pX->op!=TK_GT || op==OP_SeekGE );
       132167  +        assert( pX->op!=TK_GE || op==OP_SeekGE );
       132168  +        assert( pX->op!=TK_LT || op==OP_SeekLE );
       132169  +        assert( pX->op!=TK_LE || op==OP_SeekLE );
130508 132170         }else{
130509 132171           r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
130510 132172           disableTerm(pLevel, pStart);
130511 132173           op = aMoveOp[(pX->op - TK_GT)];
130512 132174         }
130513 132175         sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
130514 132176         VdbeComment((v, "pk"));
................................................................................
131275 132937       pE = pTerm->pExpr;
131276 132938       assert( !ExprHasProperty(pE, EP_FromJoin) );
131277 132939       assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
131278 132940       pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
131279 132941                       WO_EQ|WO_IN|WO_IS, 0);
131280 132942       if( pAlt==0 ) continue;
131281 132943       if( pAlt->wtFlags & (TERM_CODED) ) continue;
       132944  +    if( (pAlt->eOperator & WO_IN) 
       132945  +     && (pAlt->pExpr->flags & EP_xIsSelect)
       132946  +     && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
       132947  +    ){
       132948  +      continue;
       132949  +    }
131282 132950       testcase( pAlt->eOperator & WO_EQ );
131283 132951       testcase( pAlt->eOperator & WO_IS );
131284 132952       testcase( pAlt->eOperator & WO_IN );
131285 132953       VdbeModuleComment((v, "begin transitive constraint"));
131286 132954       sEAlt = *pAlt->pExpr;
131287 132955       sEAlt.pLeft = pE->pLeft;
131288 132956       sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL);
................................................................................
132189 133857       mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
132190 133858       mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
132191 133859       if( ALWAYS(pSrc!=0) ){
132192 133860         int i;
132193 133861         for(i=0; i<pSrc->nSrc; i++){
132194 133862           mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
132195 133863           mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
       133864  +        if( pSrc->a[i].fg.isTabFunc ){
       133865  +          mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
       133866  +        }
132196 133867         }
132197 133868       }
132198 133869       pS = pS->pPrior;
132199 133870     }
132200 133871     return mask;
132201 133872   }
132202 133873   
................................................................................
132601 134272   
132602 134273         pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
132603 134274         transferJoinMarkings(pNew, pExpr);
132604 134275         idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
132605 134276         exprAnalyze(pSrc, pWC, idxNew);
132606 134277       }
132607 134278       pTerm = &pWC->a[idxTerm];
132608         -    pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL;  /* Disable the original */
       134279  +    pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL;  /* Disable the original */
132609 134280       pTerm->eOperator = 0;
132610 134281     }
132611 134282   
132612 134283     /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
132613 134284     ** a virtual term for each vector component. The expression object
132614 134285     ** used by each such virtual term is pExpr (the full vector IN(...) 
132615 134286     ** expression). The WhereTerm.iField variable identifies the index within
................................................................................
135326 136997       }else if( eOp & (WO_EQ|WO_IS) ){
135327 136998         int iCol = pProbe->aiColumn[saved_nEq];
135328 136999         pNew->wsFlags |= WHERE_COLUMN_EQ;
135329 137000         assert( saved_nEq==pNew->u.btree.nEq );
135330 137001         if( iCol==XN_ROWID 
135331 137002          || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
135332 137003         ){
135333         -        if( iCol>=0 && pProbe->uniqNotNull==0 ){
135334         -          pNew->wsFlags |= WHERE_UNQ_WANTED;
135335         -        }else{
       137004  +        if( iCol==XN_ROWID || pProbe->uniqNotNull 
       137005  +         || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ) 
       137006  +        ){
135336 137007             pNew->wsFlags |= WHERE_ONEROW;
       137008  +        }else{
       137009  +          pNew->wsFlags |= WHERE_UNQ_WANTED;
135337 137010           }
135338 137011         }
135339 137012       }else if( eOp & WO_ISNULL ){
135340 137013         pNew->wsFlags |= WHERE_COLUMN_NULL;
135341 137014       }else if( eOp & (WO_GT|WO_GE) ){
135342 137015         testcase( eOp & WO_GT );
135343 137016         testcase( eOp & WO_GE );
................................................................................
137476 139149     ** preserves SQLite's legacy behaviour in the following two cases:
137477 139150     **
137478 139151     **   FROM ... WHERE random()>0;           -- eval random() once per row
137479 139152     **   FROM ... WHERE (SELECT random())>0;  -- eval random() once overall
137480 139153     */
137481 139154     for(ii=0; ii<sWLB.pWC->nTerm; ii++){
137482 139155       WhereTerm *pT = &sWLB.pWC->a[ii];
       139156  +    if( pT->wtFlags & TERM_VIRTUAL ) continue;
137483 139157       if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
137484 139158         sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
137485 139159         pT->wtFlags |= TERM_CODED;
137486 139160       }
137487 139161     }
137488 139162   
137489 139163     if( wctrlFlags & WHERE_WANT_DISTINCT ){
................................................................................
137646 139320       }
137647 139321     }
137648 139322     WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
137649 139323     pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
137650 139324   
137651 139325     /* If the caller is an UPDATE or DELETE statement that is requesting
137652 139326     ** to use a one-pass algorithm, determine if this is appropriate.
       139327  +  **
       139328  +  ** A one-pass approach can be used if the caller has requested one
       139329  +  ** and either (a) the scan visits at most one row or (b) each
       139330  +  ** of the following are true:
       139331  +  **
       139332  +  **   * the caller has indicated that a one-pass approach can be used
       139333  +  **     with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
       139334  +  **   * the table is not a virtual table, and
       139335  +  **   * either the scan does not use the OR optimization or the caller
       139336  +  **     is a DELETE operation (WHERE_DUPLICATES_OK is only specified
       139337  +  **     for DELETE).
       139338  +  **
       139339  +  ** The last qualification is because an UPDATE statement uses
       139340  +  ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
       139341  +  ** use a one-pass approach, and this is not set accurately for scans
       139342  +  ** that use the OR optimization.
137653 139343     */
137654 139344     assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
137655 139345     if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
137656 139346       int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
137657 139347       int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
137658         -    if( bOnerow
137659         -     || ((wctrlFlags & WHERE_ONEPASS_MULTIROW)!=0
137660         -           && 0==(wsFlags & WHERE_VIRTUALTABLE))
137661         -    ){
       139348  +    if( bOnerow || (
       139349  +        0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW)
       139350  +     && 0==(wsFlags & WHERE_VIRTUALTABLE)
       139351  +     && (0==(wsFlags & WHERE_MULTI_OR) || (wctrlFlags & WHERE_DUPLICATES_OK))
       139352  +    )){
137662 139353         pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
137663 139354         if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
137664 139355           if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
137665 139356             bFordelete = OPFLAG_FORDELETE;
137666 139357           }
137667 139358           pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
137668 139359         }
................................................................................
139917 141608     if( stateno>YY_MAX_SHIFT ) return stateno;
139918 141609     assert( stateno <= YY_SHIFT_COUNT );
139919 141610   #if defined(YYCOVERAGE)
139920 141611     yycoverage[stateno][iLookAhead] = 1;
139921 141612   #endif
139922 141613     do{
139923 141614       i = yy_shift_ofst[stateno];
139924         -    assert( i>=0 && i+YYNTOKEN<=sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
       141615  +    assert( i>=0 );
       141616  +    assert( i+YYNTOKEN<=(int)sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
139925 141617       assert( iLookAhead!=YYNOCODE );
139926 141618       assert( iLookAhead < YYNTOKEN );
139927 141619       i += iLookAhead;
139928 141620       if( yy_lookahead[i]!=iLookAhead ){
139929 141621   #ifdef YYFALLBACK
139930 141622         YYCODETYPE iFallback;            /* Fallback token */
139931 141623         if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
................................................................................
140442 142134     sqlite3ParserTOKENTYPE yyLookaheadToken  /* Value of the lookahead token */
140443 142135   ){
140444 142136     int yygoto;                     /* The next state */
140445 142137     int yyact;                      /* The next action */
140446 142138     yyStackEntry *yymsp;            /* The top of the parser's stack */
140447 142139     int yysize;                     /* Amount to pop the stack */
140448 142140     sqlite3ParserARG_FETCH;
       142141  +  (void)yyLookahead;
       142142  +  (void)yyLookaheadToken;
140449 142143     yymsp = yypParser->yytos;
140450 142144   #ifndef NDEBUG
140451 142145     if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
140452 142146       yysize = yyRuleInfo[yyruleno].nrhs;
140453 142147       if( yysize ){
140454 142148         fprintf(yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
140455 142149           yyTracePrompt,
................................................................................
140629 142323     Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[-1].minor.yy314, 0);
140630 142324     sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy336);
140631 142325   }
140632 142326           break;
140633 142327         case 34: /* ccons ::= DEFAULT scanpt ID|INDEXED */
140634 142328   {
140635 142329     Expr *p = tokenExpr(pParse, TK_STRING, yymsp[0].minor.yy0);
       142330  +  if( p ){
       142331  +    sqlite3ExprIdToTrueFalse(p);
       142332  +    testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
       142333  +  }
140636 142334     sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
140637 142335   }
140638 142336           break;
140639 142337         case 35: /* ccons ::= NOT NULL onconf */
140640 142338   {sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
140641 142339           break;
140642 142340         case 36: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
................................................................................
140822 142520     ** then extract the first few alphanumeric characters from within that
140823 142521     ** comment to be the zSelName value.  Otherwise, the label is #N where
140824 142522     ** is an integer that is incremented with each SELECT statement seen.
140825 142523     */
140826 142524     if( yymsp[-8].minor.yy387!=0 ){
140827 142525       const char *z = s.z+6;
140828 142526       int i;
140829         -    sqlite3_snprintf(sizeof(yymsp[-8].minor.yy387->zSelName), yymsp[-8].minor.yy387->zSelName, "#%d",
140830         -                     ++pParse->nSelect);
       142527  +    sqlite3_snprintf(sizeof(yymsp[-8].minor.yy387->zSelName), yymsp[-8].minor.yy387->zSelName,"#%d",++pParse->nSelect);
140831 142528       while( z[0]==' ' ) z++;
140832 142529       if( z[0]=='/' && z[1]=='*' ){
140833 142530         z += 2;
140834 142531         while( z[0]==' ' ) z++;
140835 142532         for(i=0; sqlite3Isalnum(z[i]); i++){}
140836 142533         sqlite3_snprintf(sizeof(yymsp[-8].minor.yy387->zSelName), yymsp[-8].minor.yy387->zSelName, "%.*s", i, z);
140837 142534       }
................................................................................
143257 144954   ******************************************************************************
143258 144955   **
143259 144956   ** This header file is used by programs that want to link against the
143260 144957   ** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
143261 144958   */
143262 144959   /* #include "sqlite3.h" */
143263 144960   
       144961  +#ifdef SQLITE_OMIT_VIRTUALTABLE
       144962  +# undef SQLITE_ENABLE_RTREE
       144963  +#endif
       144964  +
143264 144965   #if 0
143265 144966   extern "C" {
143266 144967   #endif  /* __cplusplus */
143267 144968   
143268 144969   SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
143269 144970   
143270 144971   #if 0
................................................................................
143518 145219       if( sqlite3GlobalConfig.isPCacheInit==0 ){
143519 145220         rc = sqlite3PcacheInitialize();
143520 145221       }
143521 145222       if( rc==SQLITE_OK ){
143522 145223         sqlite3GlobalConfig.isPCacheInit = 1;
143523 145224         rc = sqlite3OsInit();
143524 145225       }
       145226  +#ifdef SQLITE_ENABLE_DESERIALIZE
       145227  +    if( rc==SQLITE_OK ){
       145228  +      rc = sqlite3MemdbInit();
       145229  +    }
       145230  +#endif
143525 145231       if( rc==SQLITE_OK ){
143526 145232         sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
143527 145233             sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
143528 145234         sqlite3GlobalConfig.isInit = 1;
143529 145235   #ifdef SQLITE_EXTRA_INIT
143530 145236         bRunExtraInit = 1;
143531 145237   #endif
................................................................................
143550 145256     ** been compiled correctly.  It is important to run this code, but
143551 145257     ** we don't want to run it too often and soak up CPU cycles for no
143552 145258     ** reason.  So we run it once during initialization.
143553 145259     */
143554 145260   #ifndef NDEBUG
143555 145261   #ifndef SQLITE_OMIT_FLOATING_POINT
143556 145262     /* This section of code's only "output" is via assert() statements. */
143557         -  if ( rc==SQLITE_OK ){
       145263  +  if( rc==SQLITE_OK ){
143558 145264       u64 x = (((u64)1)<<63)-1;
143559 145265       double y;
143560 145266       assert(sizeof(x)==8);
143561 145267       assert(sizeof(x)==sizeof(y));
143562 145268       memcpy(&y, &x, 8);
143563 145269       assert( sqlite3IsNaN(y) );
143564 145270     }
................................................................................
144717 146423   #else
144718 146424       /* SQLITE_NOLFS       */ 0,
144719 146425   #endif
144720 146426       /* SQLITE_AUTH        */ "authorization denied",
144721 146427       /* SQLITE_FORMAT      */ 0,
144722 146428       /* SQLITE_RANGE       */ "column index out of range",
144723 146429       /* SQLITE_NOTADB      */ "file is not a database",
       146430  +    /* SQLITE_NOTICE      */ "notification message",
       146431  +    /* SQLITE_WARNING     */ "warning message",
144724 146432     };
144725 146433     const char *zErr = "unknown error";
144726 146434     switch( rc ){
144727 146435       case SQLITE_ABORT_ROLLBACK: {
144728 146436         zErr = "abort due to ROLLBACK";
144729 146437         break;
       146438  +    }
       146439  +    case SQLITE_ROW: {
       146440  +      zErr = "another row available";
       146441  +      break;
       146442  +    }
       146443  +    case SQLITE_DONE: {
       146444  +      zErr = "no more rows available";
       146445  +      break;
144730 146446       }
144731 146447       default: {
144732 146448         rc &= 0xff;
144733 146449         if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
144734 146450           zErr = aMsg[rc];
144735 146451         }
144736 146452         break;
................................................................................
144740 146456   }
144741 146457   
144742 146458   /*
144743 146459   ** This routine implements a busy callback that sleeps and tries
144744 146460   ** again until a timeout value is reached.  The timeout value is
144745 146461   ** an integer number of milliseconds passed in as the first
144746 146462   ** argument.
       146463  +**
       146464  +** Return non-zero to retry the lock.  Return zero to stop trying
       146465  +** and cause SQLite to return SQLITE_BUSY.
144747 146466   */
144748 146467   static int sqliteDefaultBusyCallback(
144749         - void *ptr,               /* Database connection */
144750         - int count                /* Number of times table has been busy */
       146468  +  void *ptr,               /* Database connection */
       146469  +  int count,               /* Number of times table has been busy */
       146470  +  sqlite3_file *pFile      /* The file on which the lock occurred */
144751 146471   ){
144752 146472   #if SQLITE_OS_WIN || HAVE_USLEEP
       146473  +  /* This case is for systems that have support for sleeping for fractions of
       146474  +  ** a second.  Examples:  All windows systems, unix systems with usleep() */
144753 146475     static const u8 delays[] =
144754 146476        { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
144755 146477     static const u8 totals[] =
144756 146478        { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
144757 146479   # define NDELAY ArraySize(delays)
144758 146480     sqlite3 *db = (sqlite3 *)ptr;
144759         -  int timeout = db->busyTimeout;
       146481  +  int tmout = db->busyTimeout;
144760 146482     int delay, prior;
144761 146483   
       146484  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
       146485  +  if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT,&tmout)==SQLITE_OK ){
       146486  +    if( count ){
       146487  +      tmout = 0;
       146488  +      sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT, &tmout);
       146489  +      return 0;
       146490  +    }else{
       146491  +      return 1;
       146492  +    }
       146493  +  }
       146494  +#endif
144762 146495     assert( count>=0 );
144763 146496     if( count < NDELAY ){
144764 146497       delay = delays[count];
144765 146498       prior = totals[count];
144766 146499     }else{
144767 146500       delay = delays[NDELAY-1];
144768 146501       prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
144769 146502     }
144770         -  if( prior + delay > timeout ){
144771         -    delay = timeout - prior;
       146503  +  if( prior + delay > tmout ){
       146504  +    delay = tmout - prior;
144772 146505       if( delay<=0 ) return 0;
144773 146506     }
144774 146507     sqlite3OsSleep(db->pVfs, delay*1000);
144775 146508     return 1;
144776 146509   #else
       146510  +  /* This case for unix systems that lack usleep() support.  Sleeping
       146511  +  ** must be done in increments of whole seconds */
144777 146512     sqlite3 *db = (sqlite3 *)ptr;
144778         -  int timeout = ((sqlite3 *)ptr)->busyTimeout;
144779         -  if( (count+1)*1000 > timeout ){
       146513  +  int tmout = ((sqlite3 *)ptr)->busyTimeout;
       146514  +  if( (count+1)*1000 > tmout ){
144780 146515       return 0;
144781 146516     }
144782 146517     sqlite3OsSleep(db->pVfs, 1000000);
144783 146518     return 1;
144784 146519   #endif
144785 146520   }
144786 146521   
144787 146522   /*
144788 146523   ** Invoke the given busy handler.
144789 146524   **
144790         -** This routine is called when an operation failed with a lock.
       146525  +** This routine is called when an operation failed to acquire a
       146526  +** lock on VFS file pFile.
       146527  +**
144791 146528   ** If this routine returns non-zero, the lock is retried.  If it
144792 146529   ** returns 0, the operation aborts with an SQLITE_BUSY error.
144793 146530   */
144794         -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
       146531  +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){
144795 146532     int rc;
144796         -  if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
144797         -  rc = p->xFunc(p->pArg, p->nBusy);
       146533  +  if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
       146534  +  if( p->bExtraFileArg ){
       146535  +    /* Add an extra parameter with the pFile pointer to the end of the
       146536  +    ** callback argument list */
       146537  +    int (*xTra)(void*,int,sqlite3_file*);
       146538  +    xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler;
       146539  +    rc = xTra(p->pBusyArg, p->nBusy, pFile);
       146540  +  }else{
       146541  +    /* Legacy style busy handler callback */
       146542  +    rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
       146543  +  }
144798 146544     if( rc==0 ){
144799 146545       p->nBusy = -1;
144800 146546     }else{
144801 146547       p->nBusy++;
144802 146548     }
144803 146549     return rc; 
144804 146550   }
................................................................................
144812 146558     int (*xBusy)(void*,int),
144813 146559     void *pArg
144814 146560   ){
144815 146561   #ifdef SQLITE_ENABLE_API_ARMOR
144816 146562     if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
144817 146563   #endif
144818 146564     sqlite3_mutex_enter(db->mutex);
144819         -  db->busyHandler.xFunc = xBusy;
144820         -  db->busyHandler.pArg = pArg;
       146565  +  db->busyHandler.xBusyHandler = xBusy;
       146566  +  db->busyHandler.pBusyArg = pArg;
144821 146567     db->busyHandler.nBusy = 0;
       146568  +  db->busyHandler.bExtraFileArg = 0;
144822 146569     db->busyTimeout = 0;
144823 146570     sqlite3_mutex_leave(db->mutex);
144824 146571     return SQLITE_OK;
144825 146572   }
144826 146573   
144827 146574   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
144828 146575   /*
................................................................................
144862 146609   ** specified number of milliseconds before returning 0.
144863 146610   */
144864 146611   SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
144865 146612   #ifdef SQLITE_ENABLE_API_ARMOR
144866 146613     if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
144867 146614   #endif
144868 146615     if( ms>0 ){
144869         -    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
       146616  +    sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
       146617  +                             (void*)db);
144870 146618       db->busyTimeout = ms;
       146619  +    db->busyHandler.bExtraFileArg = 1;
144871 146620     }else{
144872 146621       sqlite3_busy_handler(db, 0, 0);
144873 146622     }
144874 146623     return SQLITE_OK;
144875 146624   }
144876 146625   
144877 146626   /*
................................................................................
160710 162459     sqlite3_stmt *pStmt;
160711 162460     int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
160712 162461     if( rc==SQLITE_OK ){
160713 162462       sqlite3_bind_int64(pStmt, 1, iBlock);
160714 162463       sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
160715 162464       sqlite3_step(pStmt);
160716 162465       rc = sqlite3_reset(pStmt);
       162466  +    sqlite3_bind_null(pStmt, 2);
160717 162467     }
160718 162468     return rc;
160719 162469   }
160720 162470   
160721 162471   /*
160722 162472   ** Find the largest relative level number in the table. If successful, set
160723 162473   ** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
................................................................................
160766 162516         char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
160767 162517         if( !zEnd ) return SQLITE_NOMEM;
160768 162518         sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
160769 162519       }
160770 162520       sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
160771 162521       sqlite3_step(pStmt);
160772 162522       rc = sqlite3_reset(pStmt);
       162523  +    sqlite3_bind_null(pStmt, 6);
160773 162524     }
160774 162525     return rc;
160775 162526   }
160776 162527   
160777 162528   /*
160778 162529   ** Return the size of the common prefix (if any) shared by zPrev and
160779 162530   ** zNext, in bytes. For example, 
................................................................................
162245 163996       *pRC = rc;
162246 163997       return;
162247 163998     }
162248 163999     sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
162249 164000     sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
162250 164001     sqlite3_step(pStmt);
162251 164002     *pRC = sqlite3_reset(pStmt);
       164003  +  sqlite3_bind_null(pStmt, 2);
162252 164004     sqlite3_free(a);
162253 164005   }
162254 164006   
162255 164007   /*
162256 164008   ** Merge the entire database so that there is one segment for each 
162257 164009   ** iIndex/iLangid combination.
162258 164010   */
................................................................................
163433 165185       if( rc==SQLITE_OK ){
163434 165186         sqlite3_bind_int64(pChomp, 1, iNewStart);
163435 165187         sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
163436 165188         sqlite3_bind_int64(pChomp, 3, iAbsLevel);
163437 165189         sqlite3_bind_int(pChomp, 4, iIdx);
163438 165190         sqlite3_step(pChomp);
163439 165191         rc = sqlite3_reset(pChomp);
       165192  +      sqlite3_bind_null(pChomp, 2);
163440 165193       }
163441 165194     }
163442 165195   
163443 165196     sqlite3_free(root.a);
163444 165197     sqlite3_free(block.a);
163445 165198     return rc;
163446 165199   }
................................................................................
163512 165265   
163513 165266     rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
163514 165267     if( rc==SQLITE_OK ){
163515 165268       sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
163516 165269       sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
163517 165270       sqlite3_step(pReplace);
163518 165271       rc = sqlite3_reset(pReplace);
       165272  +    sqlite3_bind_null(pReplace, 2);
163519 165273     }
163520 165274   
163521 165275     return rc;
163522 165276   }
163523 165277   
163524 165278   /*
163525 165279   ** Load an incr-merge hint from the database. The incr-merge hint, if one 
................................................................................
164326 166080     sqlite3_vtab *pVtab,            /* FTS3 vtab object */
164327 166081     int nArg,                       /* Size of argument array */
164328 166082     sqlite3_value **apVal,          /* Array of arguments */
164329 166083     sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
164330 166084   ){
164331 166085     Fts3Table *p = (Fts3Table *)pVtab;
164332 166086     int rc = SQLITE_OK;             /* Return Code */
164333         -  int isRemove = 0;               /* True for an UPDATE or DELETE */
164334 166087     u32 *aSzIns = 0;                /* Sizes of inserted documents */
164335 166088     u32 *aSzDel = 0;                /* Sizes of deleted documents */
164336 166089     int nChng = 0;                  /* Net change in number of documents */
164337 166090     int bInsertDone = 0;
164338 166091   
164339 166092     /* At this point it must be known if the %_stat table exists or not.
164340 166093     ** So bHasStat may not be 2.  */
................................................................................
164424 166177       goto update_out;
164425 166178     }
164426 166179   
164427 166180     /* If this is a DELETE or UPDATE operation, remove the old record. */
164428 166181     if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
164429 166182       assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
164430 166183       rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
164431         -    isRemove = 1;
164432 166184     }
164433 166185     
164434 166186     /* If this is an INSERT or UPDATE operation, insert the new record. */
164435 166187     if( nArg>1 && rc==SQLITE_OK ){
164436 166188       int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
164437 166189       if( bInsertDone==0 ){
164438 166190         rc = fts3InsertData(p, apVal, pRowid);
164439 166191         if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
164440 166192           rc = FTS_CORRUPT_VTAB;
164441 166193         }
164442 166194       }
164443         -    if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
       166195  +    if( rc==SQLITE_OK ){
164444 166196         rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
164445 166197       }
164446 166198       if( rc==SQLITE_OK ){
164447 166199         assert( p->iPrevDocid==*pRowid );
164448 166200         rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
164449 166201       }
164450 166202       if( p->bHasDocsize ){
................................................................................
167011 168763   **   3. The remainder of the node contains the node entries. Each entry
167012 168764   **      consists of a single 8-byte integer followed by an even number
167013 168765   **      of 4-byte coordinates. For leaf nodes the integer is the rowid
167014 168766   **      of a record. For internal nodes it is the node number of a
167015 168767   **      child page.
167016 168768   */
167017 168769   
167018         -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
       168770  +#if !defined(SQLITE_CORE) \
       168771  +  || (defined(SQLITE_ENABLE_RTREE) && !defined(SQLITE_OMIT_VIRTUALTABLE))
167019 168772   
167020 168773   #ifndef SQLITE_CORE
167021 168774   /*   #include "sqlite3ext.h" */
167022 168775     SQLITE_EXTENSION_INIT1
167023 168776   #else
167024 168777   /*   #include "sqlite3.h" */
167025 168778   #endif
................................................................................
167743 169496       }else{
167744 169497         sqlite3_bind_null(p, 1);
167745 169498       }
167746 169499       sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
167747 169500       sqlite3_step(p);
167748 169501       pNode->isDirty = 0;
167749 169502       rc = sqlite3_reset(p);
       169503  +    sqlite3_bind_null(p, 2);
167750 169504       if( pNode->iNode==0 && rc==SQLITE_OK ){
167751 169505         pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
167752 169506         nodeHashInsert(pRtree, pNode);
167753 169507       }
167754 169508     }
167755 169509     return rc;
167756 169510   }
................................................................................
174430 176184   
174431 176185       while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
174432 176186         int bKey = sqlite3_column_int(pXInfo, 5);
174433 176187         if( bKey ){
174434 176188           int iCid = sqlite3_column_int(pXInfo, 1);
174435 176189           int bDesc = sqlite3_column_int(pXInfo, 3);
174436 176190           const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
174437         -        zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma, 
       176191  +        zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma, 
174438 176192               iCid, pIter->azTblType[iCid], zCollate
174439 176193           );
174440 176194           zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
174441 176195           zComma = ", ";
174442 176196         }
174443 176197       }
174444 176198       zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
................................................................................
174491 176245         );
174492 176246   
174493 176247         if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
174494 176248           /* If the target table column is an "INTEGER PRIMARY KEY", add
174495 176249           ** "PRIMARY KEY" to the imposter table column declaration. */
174496 176250           zPk = "PRIMARY KEY ";
174497 176251         }
174498         -      zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s", 
       176252  +      zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s", 
174499 176253             zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
174500 176254             (pIter->abNotNull[iCol] ? " NOT NULL" : "")
174501 176255         );
174502 176256         zComma = ", ";
174503 176257       }
174504 176258   
174505 176259       if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
................................................................................
178669 180423     char *zDb;                      /* Name of database session is attached to */
178670 180424     int bEnable;                    /* True if currently recording */
178671 180425     int bIndirect;                  /* True if all changes are indirect */
178672 180426     int bAutoAttach;                /* True to auto-attach tables */
178673 180427     int rc;                         /* Non-zero if an error has occurred */
178674 180428     void *pFilterCtx;               /* First argument to pass to xTableFilter */
178675 180429     int (*xTableFilter)(void *pCtx, const char *zTab);
       180430  +  sqlite3_value *pZeroBlob;       /* Value containing X'' */
178676 180431     sqlite3_session *pNext;         /* Next session object on same db. */
178677 180432     SessionTable *pTable;           /* List of attached tables */
178678 180433     SessionHook hook;               /* APIs to grab new and old data with */
178679 180434   };
178680 180435   
178681 180436   /*
178682 180437   ** Instances of this structure are used to build strings or binary records.
................................................................................
178736 180491   ** a subset of the initial values that the modified row contained at the
178737 180492   ** start of the session. Or no initial values if the row was inserted.
178738 180493   */
178739 180494   struct SessionTable {
178740 180495     SessionTable *pNext;
178741 180496     char *zName;                    /* Local name of table */
178742 180497     int nCol;                       /* Number of columns in table zName */
       180498  +  int bStat1;                     /* True if this is sqlite_stat1 */
178743 180499     const char **azCol;             /* Column names */
178744 180500     u8 *abPK;                       /* Array of primary key flags */
178745 180501     int nEntry;                     /* Total number of entries in hash table */
178746 180502     int nChange;                    /* Size of apChange[] array */
178747 180503     SessionChange **apChange;       /* Hash table buckets */
178748 180504   };
178749 180505   
................................................................................
178853 180609   **
178854 180610   ** As in the changeset format, each field of the single record that is part
178855 180611   ** of a patchset change is associated with the correspondingly positioned
178856 180612   ** table column, counting from left to right within the CREATE TABLE 
178857 180613   ** statement.
178858 180614   **
178859 180615   ** For a DELETE change, all fields within the record except those associated
178860         -** with PRIMARY KEY columns are set to "undefined". The PRIMARY KEY fields
178861         -** contain the values identifying the row to delete.
       180616  +** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
       180617  +** values identifying the row to delete.
178862 180618   **
178863 180619   ** For an UPDATE change, all fields except those associated with PRIMARY KEY
178864 180620   ** columns and columns that are modified by the UPDATE are set to "undefined".
178865 180621   ** PRIMARY KEY fields contain the values identifying the table row to update,
178866 180622   ** and fields associated with modified columns contain the new column values.
178867 180623   **
178868 180624   ** The records associated with INSERT changes are in the same format as for
................................................................................
179119 180875             z = (const u8 *)sqlite3_value_blob(pVal);
179120 180876           }
179121 180877           n = sqlite3_value_bytes(pVal);
179122 180878           if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
179123 180879           h = sessionHashAppendBlob(h, n, z);
179124 180880         }else{
179125 180881           assert( eType==SQLITE_NULL );
       180882  +        assert( pTab->bStat1==0 || i!=1 );
179126 180883           *pbNullPK = 1;
179127 180884         }
179128 180885       }
179129 180886     }
179130 180887   
179131 180888     *piHash = (h % pTab->nChange);
179132 180889     return SQLITE_OK;
................................................................................
179136 180893   ** The buffer that the argument points to contains a serialized SQL value.
179137 180894   ** Return the number of bytes of space occupied by the value (including
179138 180895   ** the type byte).
179139 180896   */
179140 180897   static int sessionSerialLen(u8 *a){
179141 180898     int e = *a;
179142 180899     int n;
179143         -  if( e==0 ) return 1;
       180900  +  if( e==0 || e==0xFF ) return 1;
179144 180901     if( e==SQLITE_NULL ) return 1;
179145 180902     if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
179146 180903     return sessionVarintGet(&a[1], &n) + 1 + n;
179147 180904   }
179148 180905   
179149 180906   /*
179150 180907   ** Based on the primary key values stored in change aRecord, calculate a
................................................................................
179216 180973     int iCol;                       /* Used to iterate through table columns */
179217 180974   
179218 180975     for(iCol=0; iCol<pTab->nCol; iCol++){
179219 180976       if( pTab->abPK[iCol] ){
179220 180977         int n1 = sessionSerialLen(a1);
179221 180978         int n2 = sessionSerialLen(a2);
179222 180979   
179223         -      if( pTab->abPK[iCol] && (n1!=n2 || memcmp(a1, a2, n1)) ){
       180980  +      if( n1!=n2 || memcmp(a1, a2, n1) ){
179224 180981           return 0;
179225 180982         }
179226 180983         a1 += n1;
179227 180984         a2 += n2;
179228 180985       }else{
179229 180986         if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
179230 180987         if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
................................................................................
179459 181216           a += sessionVarintGet(a, &n);
179460 181217           if( sqlite3_value_bytes(pVal)!=n ) return 0;
179461 181218           if( eType==SQLITE_TEXT ){
179462 181219             z = sqlite3_value_text(pVal);
179463 181220           }else{
179464 181221             z = sqlite3_value_blob(pVal);
179465 181222           }
179466         -        if( memcmp(a, z, n) ) return 0;
       181223  +        if( n>0 && memcmp(a, z, n) ) return 0;
179467 181224           a += n;
179468         -        break;
179469 181225         }
179470 181226       }
179471 181227     }
179472 181228   
179473 181229     return 1;
179474 181230   }
179475 181231   
................................................................................
179671 181427         int i;
179672 181428         for(i=0; i<pTab->nCol; i++){
179673 181429           if( abPK[i] ){
179674 181430             pTab->abPK = abPK;
179675 181431             break;
179676 181432           }
179677 181433         }
       181434  +      if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
       181435  +        pTab->bStat1 = 1;
       181436  +      }
179678 181437       }
179679 181438     }
179680 181439     return (pSession->rc || pTab->abPK==0);
179681 181440   }
       181441  +
       181442  +/*
       181443  +** Versions of the four methods in object SessionHook for use with the
       181444  +** sqlite_stat1 table. The purpose of this is to substitute a zero-length
       181445  +** blob each time a NULL value is read from the "idx" column of the
       181446  +** sqlite_stat1 table.
       181447  +*/
       181448  +typedef struct SessionStat1Ctx SessionStat1Ctx;
       181449  +struct SessionStat1Ctx {
       181450  +  SessionHook hook;
       181451  +  sqlite3_session *pSession;
       181452  +};
       181453  +static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
       181454  +  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
       181455  +  sqlite3_value *pVal = 0;
       181456  +  int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
       181457  +  if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
       181458  +    pVal = p->pSession->pZeroBlob;
       181459  +  }
       181460  +  *ppVal = pVal;
       181461  +  return rc;
       181462  +}
       181463  +static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
       181464  +  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
       181465  +  sqlite3_value *pVal = 0;
       181466  +  int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
       181467  +  if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
       181468  +    pVal = p->pSession->pZeroBlob;
       181469  +  }
       181470  +  *ppVal = pVal;
       181471  +  return rc;
       181472  +}
       181473  +static int sessionStat1Count(void *pCtx){
       181474  +  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
       181475  +  return p->hook.xCount(p->hook.pCtx);
       181476  +}
       181477  +static int sessionStat1Depth(void *pCtx){
       181478  +  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
       181479  +  return p->hook.xDepth(p->hook.pCtx);
       181480  +}
       181481  +
179682 181482   
179683 181483   /*
179684 181484   ** This function is only called from with a pre-update-hook reporting a 
179685 181485   ** change on table pTab (attached to session pSession). The type of change
179686 181486   ** (UPDATE, INSERT, DELETE) is specified by the first argument.
179687 181487   **
179688 181488   ** Unless one is already present or an error occurs, an entry is added
................................................................................
179692 181492     int op,                         /* One of SQLITE_UPDATE, INSERT, DELETE */
179693 181493     sqlite3_session *pSession,      /* Session object pTab is attached to */
179694 181494     SessionTable *pTab              /* Table that change applies to */
179695 181495   ){
179696 181496     int iHash; 
179697 181497     int bNull = 0; 
179698 181498     int rc = SQLITE_OK;
       181499  +  SessionStat1Ctx stat1 = {0};
179699 181500   
179700 181501     if( pSession->rc ) return;
179701 181502   
179702 181503     /* Load table details if required */
179703 181504     if( sessionInitTable(pSession, pTab) ) return;
179704 181505   
179705 181506     /* Check the number of columns in this xPreUpdate call matches the 
................................................................................
179710 181511     }
179711 181512   
179712 181513     /* Grow the hash table if required */
179713 181514     if( sessionGrowHash(0, pTab) ){
179714 181515       pSession->rc = SQLITE_NOMEM;
179715 181516       return;
179716 181517     }
       181518  +
       181519  +  if( pTab->bStat1 ){
       181520  +    stat1.hook = pSession->hook;
       181521  +    stat1.pSession = pSession;
       181522  +    pSession->hook.pCtx = (void*)&stat1;
       181523  +    pSession->hook.xNew = sessionStat1New;
       181524  +    pSession->hook.xOld = sessionStat1Old;
       181525  +    pSession->hook.xCount = sessionStat1Count;
       181526  +    pSession->hook.xDepth = sessionStat1Depth;
       181527  +    if( pSession->pZeroBlob==0 ){
       181528  +      sqlite3_value *p = sqlite3ValueNew(0);
       181529  +      if( p==0 ){
       181530  +        rc = SQLITE_NOMEM;
       181531  +        goto error_out;
       181532  +      }
       181533  +      sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC);
       181534  +      pSession->pZeroBlob = p;
       181535  +    }
       181536  +  }
179717 181537   
179718 181538     /* Calculate the hash-key for this change. If the primary key of the row
179719 181539     ** includes a NULL value, exit early. Such changes are ignored by the
179720 181540     ** session module. */
179721 181541     rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT, &iHash, &bNull);
179722 181542     if( rc!=SQLITE_OK ) goto error_out;
179723 181543   
................................................................................
179800 181620           pC->bIndirect = 0;
179801 181621         }
179802 181622       }
179803 181623     }
179804 181624   
179805 181625     /* If an error has occurred, mark the session object as failed. */
179806 181626    error_out:
       181627  +  if( pTab->bStat1 ){
       181628  +    pSession->hook = stat1.hook;
       181629  +  }
179807 181630     if( rc!=SQLITE_OK ){
179808 181631       pSession->rc = rc;
179809 181632     }
179810 181633   }
179811 181634   
179812 181635   static int sessionFindTable(
179813 181636     sqlite3_session *pSession, 
................................................................................
180261 182084       if( (*pp)==pSession ){
180262 182085         *pp = (*pp)->pNext;
180263 182086         if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
180264 182087         break;
180265 182088       }
180266 182089     }
180267 182090     sqlite3_mutex_leave(sqlite3_db_mutex(db));
       182091  +  sqlite3ValueFree(pSession->pZeroBlob);
180268 182092   
180269 182093     /* Delete all attached table objects. And the contents of their 
180270 182094     ** associated hash-tables. */
180271 182095     sessionDeleteTable(pSession->pTable);
180272 182096   
180273 182097     /* Free the session object itself. */
180274 182098     sqlite3_free(pSession);
................................................................................
180728 182552     const char *zTab,               /* Table name */
180729 182553     int nCol,                       /* Number of columns in table */
180730 182554     const char **azCol,             /* Names of table columns */
180731 182555     u8 *abPK,                       /* PRIMARY KEY  array */
180732 182556     sqlite3_stmt **ppStmt           /* OUT: Prepared SELECT statement */
180733 182557   ){
180734 182558     int rc = SQLITE_OK;
180735         -  int i;
180736         -  const char *zSep = "";
180737         -  SessionBuffer buf = {0, 0, 0};
180738         -
180739         -  sessionAppendStr(&buf, "SELECT * FROM ", &rc);
180740         -  sessionAppendIdent(&buf, zDb, &rc);
180741         -  sessionAppendStr(&buf, ".", &rc);
180742         -  sessionAppendIdent(&buf, zTab, &rc);
180743         -  sessionAppendStr(&buf, " WHERE ", &rc);
180744         -  for(i=0; i<nCol; i++){
180745         -    if( abPK[i] ){
180746         -      sessionAppendStr(&buf, zSep, &rc);
180747         -      sessionAppendIdent(&buf, azCol[i], &rc);
180748         -      sessionAppendStr(&buf, " = ?", &rc);
180749         -      sessionAppendInteger(&buf, i+1, &rc);
180750         -      zSep = " AND ";
180751         -    }
180752         -  }
180753         -  if( rc==SQLITE_OK ){
180754         -    rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, ppStmt, 0);
180755         -  }
180756         -  sqlite3_free(buf.aBuf);
       182559  +  char *zSql = 0;
       182560  +  int nSql = -1;
       182561  +
       182562  +  if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
       182563  +    zSql = sqlite3_mprintf(
       182564  +        "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
       182565  +        "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
       182566  +    );
       182567  +    if( zSql==0 ) rc = SQLITE_NOMEM;
       182568  +  }else{
       182569  +    int i;
       182570  +    const char *zSep = "";
       182571  +    SessionBuffer buf = {0, 0, 0};
       182572  +
       182573  +    sessionAppendStr(&buf, "SELECT * FROM ", &rc);
       182574  +    sessionAppendIdent(&buf, zDb, &rc);
       182575  +    sessionAppendStr(&buf, ".", &rc);
       182576  +    sessionAppendIdent(&buf, zTab, &rc);
       182577  +    sessionAppendStr(&buf, " WHERE ", &rc);
       182578  +    for(i=0; i<nCol; i++){
       182579  +      if( abPK[i] ){
       182580  +        sessionAppendStr(&buf, zSep, &rc);
       182581  +        sessionAppendIdent(&buf, azCol[i], &rc);
       182582  +        sessionAppendStr(&buf, " IS ?", &rc);
       182583  +        sessionAppendInteger(&buf, i+1, &rc);
       182584  +        zSep = " AND ";
       182585  +      }
       182586  +    }
       182587  +    zSql = (char*)buf.aBuf;
       182588  +    nSql = buf.nBuf;
       182589  +  }
       182590  +
       182591  +  if( rc==SQLITE_OK ){
       182592  +    rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
       182593  +  }
       182594  +  sqlite3_free(zSql);
180757 182595     return rc;
180758 182596   }
180759 182597   
180760 182598   /*
180761 182599   ** Bind the PRIMARY KEY values from the change passed in argument pChange
180762 182600   ** to the SELECT statement passed as the first argument. The SELECT statement
180763 182601   ** is as prepared by function sessionSelectStmt().
................................................................................
181260 183098     int rc = SQLITE_OK;
181261 183099   
181262 183100     for(i=0; i<nCol && rc==SQLITE_OK; i++){
181263 183101       int eType = 0;                /* Type of value (SQLITE_NULL, TEXT etc.) */
181264 183102       if( abPK && abPK[i]==0 ) continue;
181265 183103       rc = sessionInputBuffer(pIn, 9);
181266 183104       if( rc==SQLITE_OK ){
181267         -      eType = pIn->aData[pIn->iNext++];
181268         -    }
181269         -
181270         -    assert( apOut[i]==0 );
181271         -    if( eType ){
181272         -      apOut[i] = sqlite3ValueNew(0);
181273         -      if( !apOut[i] ) rc = SQLITE_NOMEM;
       183105  +      if( pIn->iNext>=pIn->nData ){
       183106  +        rc = SQLITE_CORRUPT_BKPT;
       183107  +      }else{
       183108  +        eType = pIn->aData[pIn->iNext++];
       183109  +        assert( apOut[i]==0 );
       183110  +        if( eType ){
       183111  +          apOut[i] = sqlite3ValueNew(0);
       183112  +          if( !apOut[i] ) rc = SQLITE_NOMEM;
       183113  +        }
       183114  +      }
181274 183115       }
181275 183116   
181276 183117       if( rc==SQLITE_OK ){
181277 183118         u8 *aVal = &pIn->aData[pIn->iNext];
181278 183119         if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
181279 183120           int nByte;
181280 183121           pIn->iNext += sessionVarintGet(aVal, &nByte);
181281 183122           rc = sessionInputBuffer(pIn, nByte);
181282 183123           if( rc==SQLITE_OK ){
181283         -          u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
181284         -          rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
       183124  +          if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
       183125  +            rc = SQLITE_CORRUPT_BKPT;
       183126  +          }else{
       183127  +            u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
       183128  +            rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
       183129  +            pIn->iNext += nByte;
       183130  +          }
181285 183131           }
181286         -        pIn->iNext += nByte;
181287 183132         }
181288 183133         if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
181289 183134           sqlite3_int64 v = sessionGetI64(aVal);
181290 183135           if( eType==SQLITE_INTEGER ){
181291 183136             sqlite3VdbeMemSetInt64(apOut[i], v);
181292 183137           }else{
181293 183138             double d;
................................................................................
181319 183164     int rc = SQLITE_OK;
181320 183165     int nCol = 0;
181321 183166     int nRead = 0;
181322 183167   
181323 183168     rc = sessionInputBuffer(pIn, 9);
181324 183169     if( rc==SQLITE_OK ){
181325 183170       nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
181326         -    rc = sessionInputBuffer(pIn, nRead+nCol+100);
181327         -    nRead += nCol;
       183171  +    /* The hard upper limit for the number of columns in an SQLite
       183172  +    ** database table is, according to sqliteLimit.h, 32676. So 
       183173  +    ** consider any table-header that purports to have more than 65536 
       183174  +    ** columns to be corrupt. This is convenient because otherwise, 
       183175  +    ** if the (nCol>65536) condition below were omitted, a sufficiently 
       183176  +    ** large value for nCol may cause nRead to wrap around and become 
       183177  +    ** negative. Leading to a crash. */
       183178  +    if( nCol<0 || nCol>65536 ){
       183179  +      rc = SQLITE_CORRUPT_BKPT;
       183180  +    }else{
       183181  +      rc = sessionInputBuffer(pIn, nRead+nCol+100);
       183182  +      nRead += nCol;
       183183  +    }
181328 183184     }
181329 183185   
181330 183186     while( rc==SQLITE_OK ){
181331 183187       while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
181332 183188         nRead++;
181333 183189       }
181334 183190       if( (pIn->iNext + nRead)<pIn->nData ) break;
................................................................................
181397 183253     assert( p->rc==SQLITE_OK );
181398 183254   
181399 183255     rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
181400 183256     if( rc==SQLITE_OK ){
181401 183257       int nByte;
181402 183258       int nVarint;
181403 183259       nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
181404         -    nCopy -= nVarint;
181405         -    p->in.iNext += nVarint;
181406         -    nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
181407         -    p->tblhdr.nBuf = 0;
181408         -    sessionBufferGrow(&p->tblhdr, nByte, &rc);
       183260  +    if( p->nCol>0 ){
       183261  +      nCopy -= nVarint;
       183262  +      p->in.iNext += nVarint;
       183263  +      nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
       183264  +      p->tblhdr.nBuf = 0;
       183265  +      sessionBufferGrow(&p->tblhdr, nByte, &rc);
       183266  +    }else{
       183267  +      rc = SQLITE_CORRUPT_BKPT;
       183268  +    }
181409 183269     }
181410 183270   
181411 183271     if( rc==SQLITE_OK ){
181412 183272       int iPK = sizeof(sqlite3_value*)*p->nCol*2;
181413 183273       memset(p->tblhdr.aBuf, 0, iPK);
181414 183274       memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
181415 183275       p->in.iNext += nCopy;
................................................................................
181436 183296   ** successfully advanced to the next change in the changeset, an SQLite 
181437 183297   ** error code if an error occurs, or SQLITE_DONE if there are no further 
181438 183298   ** changes in the changeset.
181439 183299   */
181440 183300   static int sessionChangesetNext(
181441 183301     sqlite3_changeset_iter *p,      /* Changeset iterator */
181442 183302     u8 **paRec,                     /* If non-NULL, store record pointer here */
181443         -  int *pnRec                      /* If non-NULL, store size of record here */
       183303  +  int *pnRec,                     /* If non-NULL, store size of record here */
       183304  +  int *pbNew                      /* If non-NULL, true if new table */
181444 183305   ){
181445 183306     int i;
181446 183307     u8 op;
181447 183308   
181448 183309     assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
181449 183310   
181450 183311     /* If the iterator is in the error-state, return immediately. */
................................................................................
181471 183332     }
181472 183333   
181473 183334     sessionDiscardData(&p->in);
181474 183335     p->in.iCurrent = p->in.iNext;
181475 183336   
181476 183337     op = p->in.aData[p->in.iNext++];
181477 183338     while( op=='T' || op=='P' ){
       183339  +    if( pbNew ) *pbNew = 1;
181478 183340       p->bPatchset = (op=='P');
181479 183341       if( sessionChangesetReadTblhdr(p) ) return p->rc;
181480 183342       if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
181481 183343       p->in.iCurrent = p->in.iNext;
181482 183344       if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
181483 183345       op = p->in.aData[p->in.iNext++];
181484 183346     }
       183347  +
       183348  +  if( p->zTab==0 ){
       183349  +    /* The first record in the changeset is not a table header. Must be a
       183350  +    ** corrupt changeset. */
       183351  +    assert( p->in.iNext==1 );
       183352  +    return (p->rc = SQLITE_CORRUPT_BKPT);
       183353  +  }
181485 183354   
181486 183355     p->op = op;
181487 183356     p->bIndirect = p->in.aData[p->in.iNext++];
181488 183357     if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
181489 183358       return (p->rc = SQLITE_CORRUPT_BKPT);
181490 183359     }
181491 183360   
................................................................................
181521 183390       if( p->bPatchset && p->op==SQLITE_UPDATE ){
181522 183391         /* If this is an UPDATE that is part of a patchset, then all PK and
181523 183392         ** modified fields are present in the new.* record. The old.* record
181524 183393         ** is currently completely empty. This block shifts the PK fields from
181525 183394         ** new.* to old.*, to accommodate the code that reads these arrays.  */
181526 183395         for(i=0; i<p->nCol; i++){
181527 183396           assert( p->apValue[i]==0 );
181528         -        assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
181529 183397           if( p->abPK[i] ){
181530 183398             p->apValue[i] = p->apValue[i+p->nCol];
       183399  +          if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
181531 183400             p->apValue[i+p->nCol] = 0;
181532 183401           }
181533 183402         }
181534 183403       }
181535 183404     }
181536 183405   
181537 183406     return SQLITE_ROW;
................................................................................
181542 183411   ** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
181543 183412   ** or SQLITE_CORRUPT.
181544 183413   **
181545 183414   ** This function may not be called on iterators passed to a conflict handler
181546 183415   ** callback by changeset_apply().
181547 183416   */
181548 183417   SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
181549         -  return sessionChangesetNext(p, 0, 0);
       183418  +  return sessionChangesetNext(p, 0, 0, 0);
181550 183419   }
181551 183420   
181552 183421   /*
181553 183422   ** The following function extracts information on the current change
181554 183423   ** from a changeset iterator. It may only be called after changeset_next()
181555 183424   ** has returned SQLITE_ROW.
181556 183425   */
................................................................................
181918 183787     sqlite3_stmt *pDelete;          /* DELETE statement */
181919 183788     sqlite3_stmt *pUpdate;          /* UPDATE statement */
181920 183789     sqlite3_stmt *pInsert;          /* INSERT statement */
181921 183790     sqlite3_stmt *pSelect;          /* SELECT statement */
181922 183791     int nCol;                       /* Size of azCol[] and abPK[] arrays */
181923 183792     const char **azCol;             /* Array of column names */
181924 183793     u8 *abPK;                       /* Boolean array - true if column is in PK */
181925         -
       183794  +  int bStat1;                     /* True if table is sqlite_stat1 */
181926 183795     int bDeferConstraints;          /* True to defer constraints */
181927 183796     SessionBuffer constraints;      /* Deferred constraints are stored here */
       183797  +  SessionBuffer rebase;           /* Rebase information (if any) here */
       183798  +  int bRebaseStarted;             /* If table header is already in rebase */
181928 183799   };
181929 183800   
181930 183801   /*
181931 183802   ** Formulate a statement to DELETE a row from database db. Assuming a table
181932 183803   ** structure like this:
181933 183804   **
181934 183805   **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
................................................................................
182087 183958     if( rc==SQLITE_OK ){
182088 183959       rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
182089 183960     }
182090 183961     sqlite3_free(buf.aBuf);
182091 183962   
182092 183963     return rc;
182093 183964   }
       183965  +
182094 183966   
182095 183967   /*
182096 183968   ** Formulate and prepare an SQL statement to query table zTab by primary
182097 183969   ** key. Assuming the following table structure:
182098 183970   **
182099 183971   **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
182100 183972   **
................................................................................
182148 184020   
182149 184021     if( rc==SQLITE_OK ){
182150 184022       rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
182151 184023     }
182152 184024     sqlite3_free(buf.aBuf);
182153 184025     return rc;
182154 184026   }
       184027  +
       184028  +static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
       184029  +  return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
       184030  +}
       184031  +
       184032  +/*
       184033  +** Prepare statements for applying changes to the sqlite_stat1 table.
       184034  +** These are similar to those created by sessionSelectRow(),
       184035  +** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for 
       184036  +** other tables.
       184037  +*/
       184038  +static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
       184039  +  int rc = sessionSelectRow(db, "sqlite_stat1", p);
       184040  +  if( rc==SQLITE_OK ){
       184041  +    rc = sessionPrepare(db, &p->pInsert,
       184042  +        "INSERT INTO main.sqlite_stat1 VALUES(?1, "
       184043  +        "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
       184044  +        "?3)"
       184045  +    );
       184046  +  }
       184047  +  if( rc==SQLITE_OK ){
       184048  +    rc = sessionPrepare(db, &p->pUpdate,
       184049  +        "UPDATE main.sqlite_stat1 SET "
       184050  +        "tbl = CASE WHEN ?2 THEN ?3 ELSE tbl END, "
       184051  +        "idx = CASE WHEN ?5 THEN ?6 ELSE idx END, "
       184052  +        "stat = CASE WHEN ?8 THEN ?9 ELSE stat END  "
       184053  +        "WHERE tbl=?1 AND idx IS "
       184054  +        "CASE WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL ELSE ?4 END "
       184055  +        "AND (?10 OR ?8=0 OR stat IS ?7)"
       184056  +    );
       184057  +  }
       184058  +  if( rc==SQLITE_OK ){
       184059  +    rc = sessionPrepare(db, &p->pDelete,
       184060  +        "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
       184061  +        "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
       184062  +        "AND (?4 OR stat IS ?3)"
       184063  +    );
       184064  +  }
       184065  +  return rc;
       184066  +}
182155 184067   
182156 184068   /*
182157 184069   ** A wrapper around sqlite3_bind_value() that detects an extra problem. 
182158 184070   ** See comments in the body of this function for details.
182159 184071   */
182160 184072   static int sessionBindValue(
182161 184073     sqlite3_stmt *pStmt,            /* Statement to bind value to */
................................................................................
182206 184118     ** in the code below. */
182207 184119     assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
182208 184120   
182209 184121     for(i=0; rc==SQLITE_OK && i<nCol; i++){
182210 184122       if( !abPK || abPK[i] ){
182211 184123         sqlite3_value *pVal;
182212 184124         (void)xValue(pIter, i, &pVal);
182213         -      rc = sessionBindValue(pStmt, i+1, pVal);
       184125  +      if( pVal==0 ){
       184126  +        /* The value in the changeset was "undefined". This indicates a
       184127  +        ** corrupt changeset blob.  */
       184128  +        rc = SQLITE_CORRUPT_BKPT;
       184129  +      }else{
       184130  +        rc = sessionBindValue(pStmt, i+1, pVal);
       184131  +      }
182214 184132       }
182215 184133     }
182216 184134     return rc;
182217 184135   }
182218 184136   
182219 184137   /*
182220 184138   ** SQL statement pSelect is as generated by the sessionSelectRow() function.
................................................................................
182250 184168         nCol, abPK, pSelect
182251 184169     );
182252 184170   
182253 184171     if( rc==SQLITE_OK ){
182254 184172       rc = sqlite3_step(pSelect);
182255 184173       if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
182256 184174     }
       184175  +
       184176  +  return rc;
       184177  +}
       184178  +
       184179  +/*
       184180  +** This function is called from within sqlite3changset_apply_v2() when
       184181  +** a conflict is encountered and resolved using conflict resolution
       184182  +** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
       184183  +** It adds a conflict resolution record to the buffer in 
       184184  +** SessionApplyCtx.rebase, which will eventually be returned to the caller
       184185  +** of apply_v2() as the "rebase" buffer.
       184186  +**
       184187  +** Return SQLITE_OK if successful, or an SQLite error code otherwise.
       184188  +*/
       184189  +static int sessionRebaseAdd(
       184190  +  SessionApplyCtx *p,             /* Apply context */
       184191  +  int eType,                      /* Conflict resolution (OMIT or REPLACE) */
       184192  +  sqlite3_changeset_iter *pIter   /* Iterator pointing at current change */
       184193  +){
       184194  +  int rc = SQLITE_OK;
       184195  +  int i;
       184196  +  int eOp = pIter->op;
       184197  +  if( p->bRebaseStarted==0 ){
       184198  +    /* Append a table-header to the rebase buffer */
       184199  +    const char *zTab = pIter->zTab;
       184200  +    sessionAppendByte(&p->rebase, 'T', &rc);
       184201  +    sessionAppendVarint(&p->rebase, p->nCol, &rc);
       184202  +    sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
       184203  +    sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
       184204  +    p->bRebaseStarted = 1;
       184205  +  }
       184206  +
       184207  +  assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
       184208  +  assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
       184209  +
       184210  +  sessionAppendByte(&p->rebase, 
       184211  +      (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
       184212  +  );
       184213  +  sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
       184214  +  for(i=0; i<p->nCol; i++){
       184215  +    sqlite3_value *pVal = 0;
       184216  +    if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
       184217  +      sqlite3changeset_old(pIter, i, &pVal);
       184218  +    }else{
       184219  +      sqlite3changeset_new(pIter, i, &pVal);
       184220  +    }
       184221  +    sessionAppendValue(&p->rebase, pVal, &rc);
       184222  +  }
182257 184223   
182258 184224     return rc;
182259 184225   }
182260 184226   
182261 184227   /*
182262 184228   ** Invoke the conflict handler for the change that the changeset iterator
182263 184229   ** currently points to.
................................................................................
182329 184295     }else if( rc==SQLITE_OK ){
182330 184296       if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
182331 184297         /* Instead of invoking the conflict handler, append the change blob
182332 184298         ** to the SessionApplyCtx.constraints buffer. */
182333 184299         u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
182334 184300         int nBlob = pIter->in.iNext - pIter->in.iCurrent;
182335 184301         sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
182336         -      res = SQLITE_CHANGESET_OMIT;
       184302  +      return SQLITE_OK;
182337 184303       }else{
182338 184304         /* No other row with the new.* primary key. */
182339 184305         res = xConflict(pCtx, eType+1, pIter);
182340 184306         if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
182341 184307       }
182342 184308     }
182343 184309   
................................................................................
182355 184321           rc = SQLITE_ABORT;
182356 184322           break;
182357 184323   
182358 184324         default:
182359 184325           rc = SQLITE_MISUSE;
182360 184326           break;
182361 184327       }
       184328  +    if( rc==SQLITE_OK ){
       184329  +      rc = sessionRebaseAdd(p, res, pIter);
       184330  +    }
182362 184331     }
182363 184332   
182364 184333     return rc;
182365 184334   }
182366 184335   
182367 184336   /*
182368 184337   ** Attempt to apply the change that the iterator passed as the first argument
................................................................................
182479 184448         rc = sessionConflictHandler(
182480 184449             SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
182481 184450         );
182482 184451       }
182483 184452   
182484 184453     }else{
182485 184454       assert( op==SQLITE_INSERT );
182486         -    rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
182487         -    if( rc!=SQLITE_OK ) return rc;
       184455  +    if( p->bStat1 ){
       184456  +      /* Check if there is a conflicting row. For sqlite_stat1, this needs
       184457  +      ** to be done using a SELECT, as there is no PRIMARY KEY in the 
       184458  +      ** database schema to throw an exception if a duplicate is inserted.  */
       184459  +      rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
       184460  +      if( rc==SQLITE_ROW ){
       184461  +        rc = SQLITE_CONSTRAINT;
       184462  +        sqlite3_reset(p->pSelect);
       184463  +      }
       184464  +    }
182488 184465   
182489         -    sqlite3_step(p->pInsert);
182490         -    rc = sqlite3_reset(p->pInsert);
       184466  +    if( rc==SQLITE_OK ){
       184467  +      rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
       184468  +      if( rc!=SQLITE_OK ) return rc;
       184469  +
       184470  +      sqlite3_step(p->pInsert);
       184471  +      rc = sqlite3_reset(p->pInsert);
       184472  +    }
       184473  +
182491 184474       if( (rc&0xff)==SQLITE_CONSTRAINT ){
182492 184475         rc = sessionConflictHandler(
182493 184476             SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
182494 184477         );
182495 184478       }
182496 184479     }
182497 184480   
................................................................................
182516 184499     void *pCtx                      /* First argument passed to xConflict */
182517 184500   ){
182518 184501     int bReplace = 0;
182519 184502     int bRetry = 0;
182520 184503     int rc;
182521 184504   
182522 184505     rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
182523         -  assert( rc==SQLITE_OK || (bRetry==0 && bReplace==0) );
182524         -
182525         -  /* If the bRetry flag is set, the change has not been applied due to an
182526         -  ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
182527         -  ** a row with the correct PK is present in the db, but one or more other
182528         -  ** fields do not contain the expected values) and the conflict handler 
182529         -  ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
182530         -  ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
182531         -  ** the SQLITE_CHANGESET_DATA problem.  */
182532         -  if( bRetry ){
182533         -    assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
182534         -    rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
182535         -  }
182536         -
182537         -  /* If the bReplace flag is set, the change is an INSERT that has not
182538         -  ** been performed because the database already contains a row with the
182539         -  ** specified primary key and the conflict handler returned
182540         -  ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
182541         -  ** before reattempting the INSERT.  */
182542         -  else if( bReplace ){
182543         -    assert( pIter->op==SQLITE_INSERT );
182544         -    rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
182545         -    if( rc==SQLITE_OK ){
182546         -      rc = sessionBindRow(pIter, 
182547         -          sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
182548         -      sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
182549         -    }
182550         -    if( rc==SQLITE_OK ){
182551         -      sqlite3_step(pApply->pDelete);
182552         -      rc = sqlite3_reset(pApply->pDelete);
182553         -    }
182554         -    if( rc==SQLITE_OK ){
       184506  +  if( rc==SQLITE_OK ){
       184507  +    /* If the bRetry flag is set, the change has not been applied due to an
       184508  +    ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
       184509  +    ** a row with the correct PK is present in the db, but one or more other
       184510  +    ** fields do not contain the expected values) and the conflict handler 
       184511  +    ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
       184512  +    ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
       184513  +    ** the SQLITE_CHANGESET_DATA problem.  */
       184514  +    if( bRetry ){
       184515  +      assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
182555 184516         rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
182556 184517       }
182557         -    if( rc==SQLITE_OK ){
182558         -      rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
       184518  +
       184519  +    /* If the bReplace flag is set, the change is an INSERT that has not
       184520  +    ** been performed because the database already contains a row with the
       184521  +    ** specified primary key and the conflict handler returned
       184522  +    ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
       184523  +    ** before reattempting the INSERT.  */
       184524  +    else if( bReplace ){
       184525  +      assert( pIter->op==SQLITE_INSERT );
       184526  +      rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
       184527  +      if( rc==SQLITE_OK ){
       184528  +        rc = sessionBindRow(pIter, 
       184529  +            sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
       184530  +        sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
       184531  +      }
       184532  +      if( rc==SQLITE_OK ){
       184533  +        sqlite3_step(pApply->pDelete);
       184534  +        rc = sqlite3_reset(pApply->pDelete);
       184535  +      }
       184536  +      if( rc==SQLITE_OK ){
       184537  +        rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
       184538  +      }
       184539  +      if( rc==SQLITE_OK ){
       184540  +        rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
       184541  +      }
182559 184542       }
182560 184543     }
182561 184544   
182562 184545     return rc;
182563 184546   }
182564 184547   
182565 184548   /*
................................................................................
182627 184610       const char *zTab              /* Table name */
182628 184611     ),
182629 184612     int(*xConflict)(
182630 184613       void *pCtx,                   /* Copy of fifth arg to _apply() */
182631 184614       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
182632 184615       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
182633 184616     ),
182634         -  void *pCtx                      /* First argument passed to xConflict */
       184617  +  void *pCtx,                     /* First argument passed to xConflict */
       184618  +  void **ppRebase, int *pnRebase  /* OUT: Rebase information */
182635 184619   ){
182636 184620     int schemaMismatch = 0;
182637 184621     int rc;                         /* Return code */
182638 184622     const char *zTab = 0;           /* Name of current table */
182639 184623     int nTab = 0;                   /* Result of sqlite3Strlen30(zTab) */
182640 184624     SessionApplyCtx sApply;         /* changeset_apply() context object */
182641 184625     int bPatchset;
................................................................................
182665 184649         if( rc!=SQLITE_OK ) break;
182666 184650   
182667 184651         sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
182668 184652         sqlite3_finalize(sApply.pDelete);
182669 184653         sqlite3_finalize(sApply.pUpdate); 
182670 184654         sqlite3_finalize(sApply.pInsert);
182671 184655         sqlite3_finalize(sApply.pSelect);
182672         -      memset(&sApply, 0, sizeof(sApply));
182673 184656         sApply.db = db;
       184657  +      sApply.pDelete = 0;
       184658  +      sApply.pUpdate = 0;
       184659  +      sApply.pInsert = 0;
       184660  +      sApply.pSelect = 0;
       184661  +      sApply.nCol = 0;
       184662  +      sApply.azCol = 0;
       184663  +      sApply.abPK = 0;
       184664  +      sApply.bStat1 = 0;
182674 184665         sApply.bDeferConstraints = 1;
       184666  +      sApply.bRebaseStarted = 0;
       184667  +      memset(&sApply.constraints, 0, sizeof(SessionBuffer));
182675 184668   
182676 184669         /* If an xFilter() callback was specified, invoke it now. If the 
182677 184670         ** xFilter callback returns zero, skip this table. If it returns
182678 184671         ** non-zero, proceed. */
182679 184672         schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
182680 184673         if( schemaMismatch ){
182681 184674           zTab = sqlite3_mprintf("%s", zNew);
................................................................................
182716 184709             schemaMismatch = 1;
182717 184710             sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
182718 184711                 "primary key mismatch for table %s", zTab
182719 184712             );
182720 184713           }
182721 184714           else{
182722 184715             sApply.nCol = nCol;
182723         -          if((rc = sessionSelectRow(db, zTab, &sApply))
182724         -          || (rc = sessionUpdateRow(db, zTab, &sApply))
182725         -          || (rc = sessionDeleteRow(db, zTab, &sApply))
182726         -          || (rc = sessionInsertRow(db, zTab, &sApply))
182727         -          ){
182728         -            break;
       184716  +          if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
       184717  +            if( (rc = sessionStat1Sql(db, &sApply) ) ){
       184718  +              break;
       184719  +            }
       184720  +            sApply.bStat1 = 1;
       184721  +          }else{
       184722  +            if((rc = sessionSelectRow(db, zTab, &sApply))
       184723  +                || (rc = sessionUpdateRow(db, zTab, &sApply))
       184724  +                || (rc = sessionDeleteRow(db, zTab, &sApply))
       184725  +                || (rc = sessionInsertRow(db, zTab, &sApply))
       184726  +              ){
       184727  +              break;
       184728  +            }
       184729  +            sApply.bStat1 = 0;
182729 184730             }
182730 184731           }
182731 184732           nTab = sqlite3Strlen30(zTab);
182732 184733         }
182733 184734       }
182734 184735   
182735 184736       /* If there is a schema mismatch on the current table, proceed to the
................................................................................
182769 184770     if( rc==SQLITE_OK ){
182770 184771       rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
182771 184772     }else{
182772 184773       sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
182773 184774       sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
182774 184775     }
182775 184776   
       184777  +  if( rc==SQLITE_OK && bPatchset==0 && ppRebase && pnRebase ){
       184778  +    *ppRebase = (void*)sApply.rebase.aBuf;
       184779  +    *pnRebase = sApply.rebase.nBuf;
       184780  +    sApply.rebase.aBuf = 0;
       184781  +  }
182776 184782     sqlite3_finalize(sApply.pInsert);
182777 184783     sqlite3_finalize(sApply.pDelete);
182778 184784     sqlite3_finalize(sApply.pUpdate);
182779 184785     sqlite3_finalize(sApply.pSelect);
182780 184786     sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
182781 184787     sqlite3_free((char*)sApply.constraints.aBuf);
       184788  +  sqlite3_free((char*)sApply.rebase.aBuf);
182782 184789     sqlite3_mutex_leave(sqlite3_db_mutex(db));
182783 184790     return rc;
182784 184791   }
       184792  +
       184793  +/*
       184794  +** Apply the changeset passed via pChangeset/nChangeset to the main 
       184795  +** database attached to handle "db".
       184796  +*/
       184797  +SQLITE_API int sqlite3changeset_apply_v2(
       184798  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
       184799  +  int nChangeset,                 /* Size of changeset in bytes */
       184800  +  void *pChangeset,               /* Changeset blob */
       184801  +  int(*xFilter)(
       184802  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184803  +    const char *zTab              /* Table name */
       184804  +  ),
       184805  +  int(*xConflict)(
       184806  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184807  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
       184808  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
       184809  +  ),
       184810  +  void *pCtx,                     /* First argument passed to xConflict */
       184811  +  void **ppRebase, int *pnRebase
       184812  +){
       184813  +  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
       184814  +  int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
       184815  +  if( rc==SQLITE_OK ){
       184816  +    rc = sessionChangesetApply(
       184817  +        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
       184818  +    );
       184819  +  }
       184820  +  return rc;
       184821  +}
182785 184822   
182786 184823   /*
182787 184824   ** Apply the changeset passed via pChangeset/nChangeset to the main database
182788 184825   ** attached to handle "db". Invoke the supplied conflict handler callback
182789 184826   ** to resolve any conflicts encountered while applying the change.
182790 184827   */
182791 184828   SQLITE_API int sqlite3changeset_apply(
................................................................................
182799 184836     int(*xConflict)(
182800 184837       void *pCtx,                   /* Copy of fifth arg to _apply() */
182801 184838       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
182802 184839       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
182803 184840     ),
182804 184841     void *pCtx                      /* First argument passed to xConflict */
182805 184842   ){
182806         -  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
182807         -  int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
182808         -  if( rc==SQLITE_OK ){
182809         -    rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
182810         -  }
182811         -  return rc;
       184843  +  return sqlite3changeset_apply_v2(
       184844  +      db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0
       184845  +  );
182812 184846   }
182813 184847   
182814 184848   /*
182815 184849   ** Apply the changeset passed via xInput/pIn to the main database
182816 184850   ** attached to handle "db". Invoke the supplied conflict handler callback
182817 184851   ** to resolve any conflicts encountered while applying the change.
182818 184852   */
       184853  +SQLITE_API int sqlite3changeset_apply_v2_strm(
       184854  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
       184855  +  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
       184856  +  void *pIn,                                          /* First arg for xInput */
       184857  +  int(*xFilter)(
       184858  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184859  +    const char *zTab              /* Table name */
       184860  +  ),
       184861  +  int(*xConflict)(
       184862  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184863  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
       184864  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
       184865  +  ),
       184866  +  void *pCtx,                     /* First argument passed to xConflict */
       184867  +  void **ppRebase, int *pnRebase
       184868  +){
       184869  +  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
       184870  +  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
       184871  +  if( rc==SQLITE_OK ){
       184872  +    rc = sessionChangesetApply(
       184873  +        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
       184874  +    );
       184875  +  }
       184876  +  return rc;
       184877  +}
182819 184878   SQLITE_API int sqlite3changeset_apply_strm(
182820 184879     sqlite3 *db,                    /* Apply change to "main" db of this handle */
182821 184880     int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
182822 184881     void *pIn,                                          /* First arg for xInput */
182823 184882     int(*xFilter)(
182824 184883       void *pCtx,                   /* Copy of sixth arg to _apply() */
182825 184884       const char *zTab              /* Table name */
................................................................................
182827 184886     int(*xConflict)(
182828 184887       void *pCtx,                   /* Copy of sixth arg to _apply() */
182829 184888       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
182830 184889       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
182831 184890     ),
182832 184891     void *pCtx                      /* First argument passed to xConflict */
182833 184892   ){
182834         -  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
182835         -  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
182836         -  if( rc==SQLITE_OK ){
182837         -    rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
182838         -  }
182839         -  return rc;
       184893  +  return sqlite3changeset_apply_v2_strm(
       184894  +      db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0
       184895  +  );
182840 184896   }
182841 184897   
182842 184898   /*
182843 184899   ** sqlite3_changegroup handle.
182844 184900   */
182845 184901   struct sqlite3_changegroup {
182846 184902     int rc;                         /* Error code */
................................................................................
182851 184907   /*
182852 184908   ** This function is called to merge two changes to the same row together as
182853 184909   ** part of an sqlite3changeset_concat() operation. A new change object is
182854 184910   ** allocated and a pointer to it stored in *ppNew.
182855 184911   */
182856 184912   static int sessionChangeMerge(
182857 184913     SessionTable *pTab,             /* Table structure */
       184914  +  int bRebase,                    /* True for a rebase hash-table */
182858 184915     int bPatchset,                  /* True for patchsets */
182859 184916     SessionChange *pExist,          /* Existing change */
182860 184917     int op2,                        /* Second change operation */
182861 184918     int bIndirect,                  /* True if second change is indirect */
182862 184919     u8 *aRec,                       /* Second change record */
182863 184920     int nRec,                       /* Number of bytes in aRec */
182864 184921     SessionChange **ppNew           /* OUT: Merged change */
182865 184922   ){
182866 184923     SessionChange *pNew = 0;
       184924  +  int rc = SQLITE_OK;
182867 184925   
182868 184926     if( !pExist ){
182869 184927       pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
182870 184928       if( !pNew ){
182871 184929         return SQLITE_NOMEM;
182872 184930       }
182873 184931       memset(pNew, 0, sizeof(SessionChange));
182874 184932       pNew->op = op2;
182875 184933       pNew->bIndirect = bIndirect;
182876         -    pNew->nRecord = nRec;
182877 184934       pNew->aRecord = (u8*)&pNew[1];
182878         -    memcpy(pNew->aRecord, aRec, nRec);
       184935  +    if( bIndirect==0 || bRebase==0 ){
       184936  +      pNew->nRecord = nRec;
       184937  +      memcpy(pNew->aRecord, aRec, nRec);
       184938  +    }else{
       184939  +      int i;
       184940  +      u8 *pIn = aRec;
       184941  +      u8 *pOut = pNew->aRecord;
       184942  +      for(i=0; i<pTab->nCol; i++){
       184943  +        int nIn = sessionSerialLen(pIn);
       184944  +        if( *pIn==0 ){
       184945  +          *pOut++ = 0;
       184946  +        }else if( pTab->abPK[i]==0 ){
       184947  +          *pOut++ = 0xFF;
       184948  +        }else{
       184949  +          memcpy(pOut, pIn, nIn);
       184950  +          pOut += nIn;
       184951  +        }
       184952  +        pIn += nIn;
       184953  +      }
       184954  +      pNew->nRecord = pOut - pNew->aRecord;
       184955  +    }
       184956  +  }else if( bRebase ){
       184957  +    if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
       184958  +      *ppNew = pExist;
       184959  +    }else{
       184960  +      int nByte = nRec + pExist->nRecord + sizeof(SessionChange);
       184961  +      pNew = (SessionChange*)sqlite3_malloc(nByte);
       184962  +      if( pNew==0 ){
       184963  +        rc = SQLITE_NOMEM;
       184964  +      }else{
       184965  +        int i;
       184966  +        u8 *a1 = pExist->aRecord;
       184967  +        u8 *a2 = aRec;
       184968  +        u8 *pOut;
       184969  +
       184970  +        memset(pNew, 0, nByte);
       184971  +        pNew->bIndirect = bIndirect || pExist->bIndirect;
       184972  +        pNew->op = op2;
       184973  +        pOut = pNew->aRecord = (u8*)&pNew[1];
       184974  +
       184975  +        for(i=0; i<pTab->nCol; i++){
       184976  +          int n1 = sessionSerialLen(a1);
       184977  +          int n2 = sessionSerialLen(a2);
       184978  +          if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
       184979  +            *pOut++ = 0xFF;
       184980  +          }else if( *a2==0 ){
       184981  +            memcpy(pOut, a1, n1);
       184982  +            pOut += n1;
       184983  +          }else{
       184984  +            memcpy(pOut, a2, n2);
       184985  +            pOut += n2;
       184986  +          }
       184987  +          a1 += n1;
       184988  +          a2 += n2;
       184989  +        }
       184990  +        pNew->nRecord = pOut - pNew->aRecord;
       184991  +      }
       184992  +      sqlite3_free(pExist);
       184993  +    }
182879 184994     }else{
182880 184995       int op1 = pExist->op;
182881 184996   
182882 184997       /* 
182883 184998       **   op1=INSERT, op2=INSERT      ->      Unsupported. Discard op2.
182884 184999       **   op1=INSERT, op2=UPDATE      ->      INSERT.
182885 185000       **   op1=INSERT, op2=DELETE      ->      (none)
................................................................................
182965 185080           pNew->nRecord = (int)(aCsr - pNew->aRecord);
182966 185081         }
182967 185082         sqlite3_free(pExist);
182968 185083       }
182969 185084     }
182970 185085   
182971 185086     *ppNew = pNew;
182972         -  return SQLITE_OK;
       185087  +  return rc;
182973 185088   }
182974 185089   
182975 185090   /*
182976 185091   ** Add all changes in the changeset traversed by the iterator passed as
182977 185092   ** the first argument to the changegroup hash tables.
182978 185093   */
182979 185094   static int sessionChangesetToHash(
182980 185095     sqlite3_changeset_iter *pIter,   /* Iterator to read from */
182981         -  sqlite3_changegroup *pGrp        /* Changegroup object to add changeset to */
       185096  +  sqlite3_changegroup *pGrp,       /* Changegroup object to add changeset to */
       185097  +  int bRebase                      /* True if hash table is for rebasing */
182982 185098   ){
182983 185099     u8 *aRec;
182984 185100     int nRec;
182985 185101     int rc = SQLITE_OK;
182986 185102     SessionTable *pTab = 0;
182987 185103   
182988         -
182989         -  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec) ){
       185104  +  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
182990 185105       const char *zNew;
182991 185106       int nCol;
182992 185107       int op;
182993 185108       int iHash;
182994 185109       int bIndirect;
182995 185110       SessionChange *pChange;
182996 185111       SessionChange *pExist = 0;
................................................................................
183062 185177           pExist = *pp;
183063 185178           *pp = (*pp)->pNext;
183064 185179           pTab->nEntry--;
183065 185180           break;
183066 185181         }
183067 185182       }
183068 185183   
183069         -    rc = sessionChangeMerge(pTab, 
       185184  +    rc = sessionChangeMerge(pTab, bRebase, 
183070 185185           pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
183071 185186       );
183072 185187       if( rc ) break;
183073 185188       if( pChange ){
183074 185189         pChange->pNext = pTab->apChange[iHash];
183075 185190         pTab->apChange[iHash] = pChange;
183076 185191         pTab->nEntry++;
................................................................................
183170 185285   */
183171 185286   SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
183172 185287     sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
183173 185288     int rc;                         /* Return code */
183174 185289   
183175 185290     rc = sqlite3changeset_start(&pIter, nData, pData);
183176 185291     if( rc==SQLITE_OK ){
183177         -    rc = sessionChangesetToHash(pIter, pGrp);
       185292  +    rc = sessionChangesetToHash(pIter, pGrp, 0);
183178 185293     }
183179 185294     sqlite3changeset_finalize(pIter);
183180 185295     return rc;
183181 185296   }
183182 185297   
183183 185298   /*
183184 185299   ** Obtain a buffer containing a changeset representing the concatenation
................................................................................
183201 185316     void *pIn
183202 185317   ){
183203 185318     sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
183204 185319     int rc;                         /* Return code */
183205 185320   
183206 185321     rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
183207 185322     if( rc==SQLITE_OK ){
183208         -    rc = sessionChangesetToHash(pIter, pGrp);
       185323  +    rc = sessionChangesetToHash(pIter, pGrp, 0);
183209 185324     }
183210 185325     sqlite3changeset_finalize(pIter);
183211 185326     return rc;
183212 185327   }
183213 185328   
183214 185329   /*
183215 185330   ** Streaming versions of changegroup_output().
................................................................................
183285 185400     if( rc==SQLITE_OK ){
183286 185401       rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
183287 185402     }
183288 185403     sqlite3changegroup_delete(pGrp);
183289 185404   
183290 185405     return rc;
183291 185406   }
       185407  +
       185408  +/*
       185409  +** Changeset rebaser handle.
       185410  +*/
       185411  +struct sqlite3_rebaser {
       185412  +  sqlite3_changegroup grp;        /* Hash table */
       185413  +};
       185414  +
       185415  +/*
       185416  +** Buffers a1 and a2 must both contain a sessions module record nCol
       185417  +** fields in size. This function appends an nCol sessions module 
       185418  +** record to buffer pBuf that is a copy of a1, except that for
       185419  +** each field that is undefined in a1[], swap in the field from a2[].
       185420  +*/
       185421  +static void sessionAppendRecordMerge(
       185422  +  SessionBuffer *pBuf,            /* Buffer to append to */
       185423  +  int nCol,                       /* Number of columns in each record */
       185424  +  u8 *a1, int n1,                 /* Record 1 */
       185425  +  u8 *a2, int n2,                 /* Record 2 */
       185426  +  int *pRc                        /* IN/OUT: error code */
       185427  +){
       185428  +  sessionBufferGrow(pBuf, n1+n2, pRc);
       185429  +  if( *pRc==SQLITE_OK ){
       185430  +    int i;
       185431  +    u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
       185432  +    for(i=0; i<nCol; i++){
       185433  +      int nn1 = sessionSerialLen(a1);
       185434  +      int nn2 = sessionSerialLen(a2);
       185435  +      if( *a1==0 || *a1==0xFF ){
       185436  +        memcpy(pOut, a2, nn2);
       185437  +        pOut += nn2;
       185438  +      }else{
       185439  +        memcpy(pOut, a1, nn1);
       185440  +        pOut += nn1;
       185441  +      }
       185442  +      a1 += nn1;
       185443  +      a2 += nn2;
       185444  +    }
       185445  +
       185446  +    pBuf->nBuf = pOut-pBuf->aBuf;
       185447  +    assert( pBuf->nBuf<=pBuf->nAlloc );
       185448  +  }
       185449  +}
       185450  +
       185451  +/*
       185452  +** This function is called when rebasing a local UPDATE change against one 
       185453  +** or more remote UPDATE changes. The aRec/nRec buffer contains the current
       185454  +** old.* and new.* records for the change. The rebase buffer (a single
       185455  +** record) is in aChange/nChange. The rebased change is appended to buffer
       185456  +** pBuf.
       185457  +**
       185458  +** Rebasing the UPDATE involves: 
       185459  +**
       185460  +**   * Removing any changes to fields for which the corresponding field
       185461  +**     in the rebase buffer is set to "replaced" (type 0xFF). If this
       185462  +**     means the UPDATE change updates no fields, nothing is appended
       185463  +**     to the output buffer.
       185464  +**
       185465  +**   * For each field modified by the local change for which the 
       185466  +**     corresponding field in the rebase buffer is not "undefined" (0x00)
       185467  +**     or "replaced" (0xFF), the old.* value is replaced by the value
       185468  +**     in the rebase buffer.
       185469  +*/
       185470  +static void sessionAppendPartialUpdate(
       185471  +  SessionBuffer *pBuf,            /* Append record here */
       185472  +  sqlite3_changeset_iter *pIter,  /* Iterator pointed at local change */
       185473  +  u8 *aRec, int nRec,             /* Local change */
       185474  +  u8 *aChange, int nChange,       /* Record to rebase against */
       185475  +  int *pRc                        /* IN/OUT: Return Code */
       185476  +){
       185477  +  sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
       185478  +  if( *pRc==SQLITE_OK ){
       185479  +    int bData = 0;
       185480  +    u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
       185481  +    int i;
       185482  +    u8 *a1 = aRec;
       185483  +    u8 *a2 = aChange;
       185484  +
       185485  +    *pOut++ = SQLITE_UPDATE;
       185486  +    *pOut++ = pIter->bIndirect;
       185487  +    for(i=0; i<pIter->nCol; i++){
       185488  +      int n1 = sessionSerialLen(a1);
       185489  +      int n2 = sessionSerialLen(a2);
       185490  +      if( pIter->abPK[i] || a2[0]==0 ){
       185491  +        if( !pIter->abPK[i] ) bData = 1;
       185492  +        memcpy(pOut, a1, n1);
       185493  +        pOut += n1;
       185494  +      }else if( a2[0]!=0xFF ){
       185495  +        bData = 1;
       185496  +        memcpy(pOut, a2, n2);
       185497  +        pOut += n2;
       185498  +      }else{
       185499  +        *pOut++ = '\0';
       185500  +      }
       185501  +      a1 += n1;
       185502  +      a2 += n2;
       185503  +    }
       185504  +    if( bData ){
       185505  +      a2 = aChange;
       185506  +      for(i=0; i<pIter->nCol; i++){
       185507  +        int n1 = sessionSerialLen(a1);
       185508  +        int n2 = sessionSerialLen(a2);
       185509  +        if( pIter->abPK[i] || a2[0]!=0xFF ){
       185510  +          memcpy(pOut, a1, n1);
       185511  +          pOut += n1;
       185512  +        }else{
       185513  +          *pOut++ = '\0';
       185514  +        }
       185515  +        a1 += n1;
       185516  +        a2 += n2;
       185517  +      }
       185518  +      pBuf->nBuf = (pOut - pBuf->aBuf);
       185519  +    }
       185520  +  }
       185521  +}
       185522  +
       185523  +/*
       185524  +** pIter is configured to iterate through a changeset. This function rebases 
       185525  +** that changeset according to the current configuration of the rebaser 
       185526  +** object passed as the first argument. If no error occurs and argument xOutput
       185527  +** is not NULL, then the changeset is returned to the caller by invoking
       185528  +** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
       185529  +** then (*ppOut) is set to point to a buffer containing the rebased changeset
       185530  +** before this function returns. In this case (*pnOut) is set to the size of
       185531  +** the buffer in bytes.  It is the responsibility of the caller to eventually
       185532  +** free the (*ppOut) buffer using sqlite3_free(). 
       185533  +**
       185534  +** If an error occurs, an SQLite error code is returned. If ppOut and
       185535  +** pnOut are not NULL, then the two output parameters are set to 0 before
       185536  +** returning.
       185537  +*/
       185538  +static int sessionRebase(
       185539  +  sqlite3_rebaser *p,             /* Rebaser hash table */
       185540  +  sqlite3_changeset_iter *pIter,  /* Input data */
       185541  +  int (*xOutput)(void *pOut, const void *pData, int nData),
       185542  +  void *pOut,                     /* Context for xOutput callback */
       185543  +  int *pnOut,                     /* OUT: Number of bytes in output changeset */
       185544  +  void **ppOut                    /* OUT: Inverse of pChangeset */
       185545  +){
       185546  +  int rc = SQLITE_OK;
       185547  +  u8 *aRec = 0;
       185548  +  int nRec = 0;
       185549  +  int bNew = 0;
       185550  +  SessionTable *pTab = 0;
       185551  +  SessionBuffer sOut = {0,0,0};
       185552  +
       185553  +  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
       185554  +    SessionChange *pChange = 0;
       185555  +    int bDone = 0;
       185556  +
       185557  +    if( bNew ){
       185558  +      const char *zTab = pIter->zTab;
       185559  +      for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
       185560  +        if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
       185561  +      }
       185562  +      bNew = 0;
       185563  +
       185564  +      /* A patchset may not be rebased */
       185565  +      if( pIter->bPatchset ){
       185566  +        rc = SQLITE_ERROR;
       185567  +      }
       185568  +
       185569  +      /* Append a table header to the output for this new table */
       185570  +      sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
       185571  +      sessionAppendVarint(&sOut, pIter->nCol, &rc);
       185572  +      sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
       185573  +      sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
       185574  +    }
       185575  +
       185576  +    if( pTab && rc==SQLITE_OK ){
       185577  +      int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
       185578  +
       185579  +      for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
       185580  +        if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
       185581  +          break;
       185582  +        }
       185583  +      }
       185584  +    }
       185585  +
       185586  +    if( pChange ){
       185587  +      assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
       185588  +      switch( pIter->op ){
       185589  +        case SQLITE_INSERT:
       185590  +          if( pChange->op==SQLITE_INSERT ){
       185591  +            bDone = 1;
       185592  +            if( pChange->bIndirect==0 ){
       185593  +              sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
       185594  +              sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185595  +              sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
       185596  +              sessionAppendBlob(&sOut, aRec, nRec, &rc);
       185597  +            }
       185598  +          }
       185599  +          break;
       185600  +
       185601  +        case SQLITE_UPDATE:
       185602  +          bDone = 1;
       185603  +          if( pChange->op==SQLITE_DELETE ){
       185604  +            if( pChange->bIndirect==0 ){
       185605  +              u8 *pCsr = aRec;
       185606  +              sessionSkipRecord(&pCsr, pIter->nCol);
       185607  +              sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
       185608  +              sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185609  +              sessionAppendRecordMerge(&sOut, pIter->nCol,
       185610  +                  pCsr, nRec-(pCsr-aRec), 
       185611  +                  pChange->aRecord, pChange->nRecord, &rc
       185612  +              );
       185613  +            }
       185614  +          }else{
       185615  +            sessionAppendPartialUpdate(&sOut, pIter,
       185616  +                aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
       185617  +            );
       185618  +          }
       185619  +          break;
       185620  +
       185621  +        default:
       185622  +          assert( pIter->op==SQLITE_DELETE );
       185623  +          bDone = 1;
       185624  +          if( pChange->op==SQLITE_INSERT ){
       185625  +            sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
       185626  +            sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185627  +            sessionAppendRecordMerge(&sOut, pIter->nCol,
       185628  +                pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
       185629  +            );
       185630  +          }
       185631  +          break;
       185632  +      }
       185633  +    }
       185634  +
       185635  +    if( bDone==0 ){
       185636  +      sessionAppendByte(&sOut, pIter->op, &rc);
       185637  +      sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185638  +      sessionAppendBlob(&sOut, aRec, nRec, &rc);
       185639  +    }
       185640  +    if( rc==SQLITE_OK && xOutput && sOut.nBuf>SESSIONS_STRM_CHUNK_SIZE ){
       185641  +      rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
       185642  +      sOut.nBuf = 0;
       185643  +    }
       185644  +    if( rc ) break;
       185645  +  }
       185646  +
       185647  +  if( rc!=SQLITE_OK ){
       185648  +    sqlite3_free(sOut.aBuf);
       185649  +    memset(&sOut, 0, sizeof(sOut));
       185650  +  }
       185651  +
       185652  +  if( rc==SQLITE_OK ){
       185653  +    if( xOutput ){
       185654  +      if( sOut.nBuf>0 ){
       185655  +        rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
       185656  +      }
       185657  +    }else{
       185658  +      *ppOut = (void*)sOut.aBuf;
       185659  +      *pnOut = sOut.nBuf;
       185660  +      sOut.aBuf = 0;
       185661  +    }
       185662  +  }
       185663  +  sqlite3_free(sOut.aBuf);
       185664  +  return rc;
       185665  +}
       185666  +
       185667  +/* 
       185668  +** Create a new rebaser object.
       185669  +*/
       185670  +SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
       185671  +  int rc = SQLITE_OK;
       185672  +  sqlite3_rebaser *pNew;
       185673  +
       185674  +  pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
       185675  +  if( pNew==0 ){
       185676  +    rc = SQLITE_NOMEM;
       185677  +  }else{
       185678  +    memset(pNew, 0, sizeof(sqlite3_rebaser));
       185679  +  }
       185680  +  *ppNew = pNew;
       185681  +  return rc;
       185682  +}
       185683  +
       185684  +/* 
       185685  +** Call this one or more times to configure a rebaser.
       185686  +*/
       185687  +SQLITE_API int sqlite3rebaser_configure(
       185688  +  sqlite3_rebaser *p, 
       185689  +  int nRebase, const void *pRebase
       185690  +){
       185691  +  sqlite3_changeset_iter *pIter = 0;   /* Iterator opened on pData/nData */
       185692  +  int rc;                              /* Return code */
       185693  +  rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
       185694  +  if( rc==SQLITE_OK ){
       185695  +    rc = sessionChangesetToHash(pIter, &p->grp, 1);
       185696  +  }
       185697  +  sqlite3changeset_finalize(pIter);
       185698  +  return rc;
       185699  +}
       185700  +
       185701  +/* 
       185702  +** Rebase a changeset according to current rebaser configuration 
       185703  +*/
       185704  +SQLITE_API int sqlite3rebaser_rebase(
       185705  +  sqlite3_rebaser *p,
       185706  +  int nIn, const void *pIn, 
       185707  +  int *pnOut, void **ppOut 
       185708  +){
       185709  +  sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */  
       185710  +  int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
       185711  +
       185712  +  if( rc==SQLITE_OK ){
       185713  +    rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
       185714  +    sqlite3changeset_finalize(pIter);
       185715  +  }
       185716  +
       185717  +  return rc;
       185718  +}
       185719  +
       185720  +/* 
       185721  +** Rebase a changeset according to current rebaser configuration 
       185722  +*/
       185723  +SQLITE_API int sqlite3rebaser_rebase_strm(
       185724  +  sqlite3_rebaser *p,
       185725  +  int (*xInput)(void *pIn, void *pData, int *pnData),
       185726  +  void *pIn,
       185727  +  int (*xOutput)(void *pOut, const void *pData, int nData),
       185728  +  void *pOut
       185729  +){
       185730  +  sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */  
       185731  +  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
       185732  +
       185733  +  if( rc==SQLITE_OK ){
       185734  +    rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
       185735  +    sqlite3changeset_finalize(pIter);
       185736  +  }
       185737  +
       185738  +  return rc;
       185739  +}
       185740  +
       185741  +/* 
       185742  +** Destroy a rebaser object 
       185743  +*/
       185744  +SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
       185745  +  if( p ){
       185746  +    sessionDeleteTable(p->grp.pList);
       185747  +    sqlite3_free(p);
       185748  +  }
       185749  +}
183292 185750   
183293 185751   #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
183294 185752   
183295 185753   /************** End of sqlite3session.c **************************************/
183296 185754   /************** Begin file json1.c *******************************************/
183297 185755   /*
183298 185756   ** 2015-08-12
................................................................................
187821 190279     if( stateno>fts5YY_MAX_SHIFT ) return stateno;
187822 190280     assert( stateno <= fts5YY_SHIFT_COUNT );
187823 190281   #if defined(fts5YYCOVERAGE)
187824 190282     fts5yycoverage[stateno][iLookAhead] = 1;
187825 190283   #endif
187826 190284     do{
187827 190285       i = fts5yy_shift_ofst[stateno];
187828         -    assert( i>=0 && i+fts5YYNFTS5TOKEN<=sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]) );
       190286  +    assert( i>=0 );
       190287  +    assert( i+fts5YYNFTS5TOKEN<=(int)sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]) );
187829 190288       assert( iLookAhead!=fts5YYNOCODE );
187830 190289       assert( iLookAhead < fts5YYNFTS5TOKEN );
187831 190290       i += iLookAhead;
187832 190291       if( fts5yy_lookahead[i]!=iLookAhead ){
187833 190292   #ifdef fts5YYFALLBACK
187834 190293         fts5YYCODETYPE iFallback;            /* Fallback token */
187835 190294         if( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])
................................................................................
188044 190503     sqlite3Fts5ParserFTS5TOKENTYPE fts5yyLookaheadToken  /* Value of the lookahead token */
188045 190504   ){
188046 190505     int fts5yygoto;                     /* The next state */
188047 190506     int fts5yyact;                      /* The next action */
188048 190507     fts5yyStackEntry *fts5yymsp;            /* The top of the parser's stack */
188049 190508     int fts5yysize;                     /* Amount to pop the stack */
188050 190509     sqlite3Fts5ParserARG_FETCH;
       190510  +  (void)fts5yyLookahead;
       190511  +  (void)fts5yyLookaheadToken;
188051 190512     fts5yymsp = fts5yypParser->fts5yytos;
188052 190513   #ifndef NDEBUG
188053 190514     if( fts5yyTraceFILE && fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) ){
188054 190515       fts5yysize = fts5yyRuleInfo[fts5yyruleno].nrhs;
188055 190516       if( fts5yysize ){
188056 190517         fprintf(fts5yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
188057 190518           fts5yyTracePrompt,
................................................................................
192261 194722       }
192262 194723   
192263 194724       if( sCtx.pPhrase==0 ){
192264 194725         /* This happens when parsing a token or quoted phrase that contains
192265 194726         ** no token characters at all. (e.g ... MATCH '""'). */
192266 194727         sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
192267 194728       }else if( sCtx.pPhrase->nTerm ){
192268         -      sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = bPrefix;
       194729  +      sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
192269 194730       }
192270 194731       pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
192271 194732     }
192272 194733   
192273 194734     return sCtx.pPhrase;
192274 194735   }
192275 194736   
................................................................................
194724 197185       if( p->rc ) return;
194725 197186     }
194726 197187   
194727 197188     sqlite3_bind_int64(p->pWriter, 1, iRowid);
194728 197189     sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
194729 197190     sqlite3_step(p->pWriter);
194730 197191     p->rc = sqlite3_reset(p->pWriter);
       197192  +  sqlite3_bind_null(p->pWriter, 2);
194731 197193   }
194732 197194   
194733 197195   /*
194734 197196   ** Execute the following SQL:
194735 197197   **
194736 197198   **     DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
194737 197199   */
................................................................................
196352 198814     sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC);
196353 198815     if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){
196354 198816       i64 val = sqlite3_column_int(pIdxSelect, 0);
196355 198817       iPg = (int)(val>>1);
196356 198818       bDlidx = (val & 0x0001);
196357 198819     }
196358 198820     p->rc = sqlite3_reset(pIdxSelect);
       198821  +  sqlite3_bind_null(pIdxSelect, 2);
196359 198822   
196360 198823     if( iPg<pSeg->pgnoFirst ){
196361 198824       iPg = pSeg->pgnoFirst;
196362 198825       bDlidx = 0;
196363 198826     }
196364 198827   
196365 198828     pIter->iLeafPgno = iPg - 1;
................................................................................
197564 200027           sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
197565 200028           if( p->rc==SQLITE_OK ){
197566 200029             u8 aBlob[2] = {0xff, 0xff};
197567 200030             sqlite3_bind_int(pIdxSelect, 1, iSegid);
197568 200031             sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
197569 200032             assert( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
197570 200033             p->rc = sqlite3_reset(pIdxSelect);
       200034  +          sqlite3_bind_null(pIdxSelect, 2);
197571 200035           }
197572 200036         }
197573 200037   #endif
197574 200038       }
197575 200039     }
197576 200040   
197577 200041     return iSegid;
................................................................................
197690 200154       const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
197691 200155       /* The following was already done in fts5WriteInit(): */
197692 200156       /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
197693 200157       sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
197694 200158       sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
197695 200159       sqlite3_step(p->pIdxWriter);
197696 200160       p->rc = sqlite3_reset(p->pIdxWriter);
       200161  +    sqlite3_bind_null(p->pIdxWriter, 2);
197697 200162     }
197698 200163     pWriter->iBtPage = 0;
197699 200164   }
197700 200165   
197701 200166   /*
197702 200167   ** This is called once for each leaf page except the first that contains
197703 200168   ** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
................................................................................
203097 205562   static void fts5SourceIdFunc(
203098 205563     sqlite3_context *pCtx,          /* Function call context */
203099 205564     int nArg,                       /* Number of args */
203100 205565     sqlite3_value **apUnused        /* Function arguments */
203101 205566   ){
203102 205567     assert( nArg==0 );
203103 205568     UNUSED_PARAM2(nArg, apUnused);
203104         -  sqlite3_result_text(pCtx, "fts5: 2018-01-15 19:00:35 b0b7d0363acf38c2178e2d3041d8ce2a0de061a51caa64670dbf539ee6d4356b", -1, SQLITE_TRANSIENT);
       205569  +  sqlite3_result_text(pCtx, "fts5: 2018-03-26 16:37:53 6c40c5574f4ae9795a142d01a8f84afd1b72678ea5f6bfca14a8646c4e862605", -1, SQLITE_TRANSIENT);
203105 205570   }
203106 205571   
203107 205572   static int fts5Init(sqlite3 *db){
203108 205573     static const sqlite3_module fts5Mod = {
203109 205574       /* iVersion      */ 2,
203110 205575       /* xCreate       */ fts5CreateMethod,
203111 205576       /* xConnect      */ fts5ConnectMethod,
................................................................................
203673 206138       sqlite3_stmt *pReplace = 0;
203674 206139       rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
203675 206140       if( rc==SQLITE_OK ){
203676 206141         sqlite3_bind_int64(pReplace, 1, iRowid);
203677 206142         sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
203678 206143         sqlite3_step(pReplace);
203679 206144         rc = sqlite3_reset(pReplace);
       206145  +      sqlite3_bind_null(pReplace, 2);
203680 206146       }
203681 206147     }
203682 206148     return rc;
203683 206149   }
203684 206150   
203685 206151   /*
203686 206152   ** Load the contents of the "averages" record from disk into the 
................................................................................
204333 206799       if( pVal ){
204334 206800         sqlite3_bind_value(pReplace, 2, pVal);
204335 206801       }else{
204336 206802         sqlite3_bind_int(pReplace, 2, iVal);
204337 206803       }
204338 206804       sqlite3_step(pReplace);
204339 206805       rc = sqlite3_reset(pReplace);
       206806  +    sqlite3_bind_null(pReplace, 1);
204340 206807     }
204341 206808     if( rc==SQLITE_OK && pVal ){
204342 206809       int iNew = p->pConfig->iCookie + 1;
204343 206810       rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
204344 206811       if( rc==SQLITE_OK ){
204345 206812         p->pConfig->iCookie = iNew;
204346 206813       }
................................................................................
207365 209832   #endif
207366 209833     return rc;
207367 209834   }
207368 209835   #endif /* SQLITE_CORE */
207369 209836   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
207370 209837   
207371 209838   /************** End of stmt.c ************************************************/
207372         -#if __LINE__!=207372
       209839  +#if __LINE__!=209839
207373 209840   #undef SQLITE_SOURCE_ID
207374         -#define SQLITE_SOURCE_ID      "2018-01-15 19:00:35 b0b7d0363acf38c2178e2d3041d8ce2a0de061a51caa64670dbf539ee6d4alt2"
       209841  +#define SQLITE_SOURCE_ID      "2018-03-28 15:56:55 eb29b3369e76ec1df25a5484d8ec5fb924e23d5c70aaa4d794b2b17ee187alt2"
207375 209842   #endif
207376 209843   /* Return the source-id for this library */
207377 209844   SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
207378 209845   /************************** End of sqlite3.c ******************************/

Changes to src/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-15 19:00:35 b0b7d0363acf38c2178e2d3041d8ce2a0de061a51caa64670dbf539ee6d4356b"
          126  +#define SQLITE_VERSION        "3.23.0"
          127  +#define SQLITE_VERSION_NUMBER 3023000
          128  +#define SQLITE_SOURCE_ID      "2018-03-28 15:56:55 eb29b3369e76ec1df25a5484d8ec5fb924e23d5c70aaa4d794b2b17ee187alt1"
   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
................................................................................
   506    506   #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
   507    507   #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
   508    508   #define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
   509    509   #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
   510    510   #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
   511    511   #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
   512    512   #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
   513         -#define SQLITE_CANTOPEN_DIRTYWAL       (SQLITE_CANTOPEN | (5<<8))
   514    513   #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
   515    514   #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
   516    515   #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
   517    516   #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
   518    517   #define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
   519    518   #define SQLITE_READONLY_CANTINIT       (SQLITE_READONLY | (5<<8))
   520    519   #define SQLITE_READONLY_DIRECTORY      (SQLITE_READONLY | (6<<8))
................................................................................
  1061   1060   ** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
  1062   1061   ** operations since the previous successful call to 
  1063   1062   ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
  1064   1063   ** ^This file control takes the file descriptor out of batch write mode
  1065   1064   ** so that all subsequent write operations are independent.
  1066   1065   ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
  1067   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.
  1068   1073   ** </ul>
  1069   1074   */
  1070   1075   #define SQLITE_FCNTL_LOCKSTATE               1
  1071   1076   #define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
  1072   1077   #define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
  1073   1078   #define SQLITE_FCNTL_LAST_ERRNO              4
  1074   1079   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
  1095   1100   #define SQLITE_FCNTL_VFS_POINTER            27
  1096   1101   #define SQLITE_FCNTL_JOURNAL_POINTER        28
  1097   1102   #define SQLITE_FCNTL_WIN32_GET_HANDLE       29
  1098   1103   #define SQLITE_FCNTL_PDB                    30
  1099   1104   #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
  1100   1105   #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
  1101   1106   #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
         1107  +#define SQLITE_FCNTL_LOCK_TIMEOUT           34
  1102   1108   
  1103   1109   /* deprecated names */
  1104   1110   #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
  1105   1111   #define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
  1106   1112   #define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
  1107   1113   
  1108   1114   
................................................................................
  2051   2057   **
  2052   2058   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  2053   2059   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  2054   2060   ** database handle, SQLite checks if this will mean that there are now no 
  2055   2061   ** connections at all to the database. If so, it performs a checkpoint 
  2056   2062   ** operation before closing the connection. This option may be used to
  2057   2063   ** override this behaviour. The first parameter passed to this operation
  2058         -** is an integer - non-zero to disable checkpoints-on-close, or zero (the
  2059         -** 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
  2060   2067   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  2061   2068   ** have been disabled - 0 if they are not disabled, 1 if they are.
  2062   2069   ** </dd>
         2070  +**
  2063   2071   ** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  2064   2072   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  2065   2073   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  2066   2074   ** a single SQL query statement will always use the same algorithm regardless
  2067   2075   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  2068   2076   ** that look at the values of bound parameters, which can make some queries
  2069   2077   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  2070   2078   ** the QPSG active, SQLite will always use the same query plan in the field as
  2071   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.
  2072   2085   ** </dd>
         2086  +**
  2073   2087   ** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  2074   2088   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  2075   2089   ** include output for any operations performed by trigger programs. This
  2076   2090   ** option is used to set or clear (the default) a flag that governs this
  2077   2091   ** behavior. The first parameter passed to this operation is an integer -
  2078         -** 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.
  2079   2094   ** The second parameter is a pointer to an integer into which is written 
  2080   2095   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  2081   2096   ** it is not disabled, 1 if it is.  
  2082   2097   ** </dd>
  2083   2098   ** </dl>
  2084   2099   */
  2085   2100   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
................................................................................
  2493   2508   SQLITE_API void sqlite3_free_table(char **result);
  2494   2509   
  2495   2510   /*
  2496   2511   ** CAPI3REF: Formatted String Printing Functions
  2497   2512   **
  2498   2513   ** These routines are work-alikes of the "printf()" family of functions
  2499   2514   ** from the standard C library.
  2500         -** These routines understand most of the common K&R formatting options,
  2501         -** plus some additional non-standard formats, detailed below.
  2502         -** Note that some of the more obscure formatting options from recent
  2503         -** 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.
  2504   2519   **
  2505   2520   ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  2506         -** results into memory obtained from [sqlite3_malloc()].
         2521  +** results into memory obtained from [sqlite3_malloc64()].
  2507   2522   ** The strings returned by these two routines should be
  2508   2523   ** released by [sqlite3_free()].  ^Both routines return a
  2509         -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
         2524  +** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
  2510   2525   ** memory to hold the resulting string.
  2511   2526   **
  2512   2527   ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  2513   2528   ** the standard C library.  The result is written into the
  2514   2529   ** buffer supplied as the second parameter whose size is given by
  2515   2530   ** the first parameter. Note that the order of the
  2516   2531   ** first two parameters is reversed from snprintf().)^  This is an
................................................................................
  2526   2541   ** guarantees that the buffer is always zero-terminated.  ^The first
  2527   2542   ** parameter "n" is the total size of the buffer, including space for
  2528   2543   ** the zero terminator.  So the longest string that can be completely
  2529   2544   ** written will be n-1 characters.
  2530   2545   **
  2531   2546   ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  2532   2547   **
  2533         -** These routines all implement some additional formatting
  2534         -** options that are useful for constructing SQL statements.
  2535         -** All of the usual printf() formatting options apply.  In addition, there
  2536         -** is are "%q", "%Q", "%w" and "%z" options.
  2537         -**
  2538         -** ^(The %q option works like %s in that it substitutes a nul-terminated
  2539         -** string from the argument list.  But %q also doubles every '\'' character.
  2540         -** %q is designed for use inside a string literal.)^  By doubling each '\''
  2541         -** character it escapes that character and allows it to be inserted into
  2542         -** the string.
  2543         -**
  2544         -** For example, assume the string variable zText contains text as follows:
  2545         -**
  2546         -** <blockquote><pre>
  2547         -**  char *zText = "It's a happy day!";
  2548         -** </pre></blockquote>
  2549         -**
  2550         -** One can use this text in an SQL statement as follows:
  2551         -**
  2552         -** <blockquote><pre>
  2553         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  2554         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  2555         -**  sqlite3_free(zSQL);
  2556         -** </pre></blockquote>
  2557         -**
  2558         -** Because the %q format string is used, the '\'' character in zText
  2559         -** is escaped and the SQL generated is as follows:
  2560         -**
  2561         -** <blockquote><pre>
  2562         -**  INSERT INTO table1 VALUES('It''s a happy day!')
  2563         -** </pre></blockquote>
  2564         -**
  2565         -** This is correct.  Had we used %s instead of %q, the generated SQL
  2566         -** would have looked like this:
  2567         -**
  2568         -** <blockquote><pre>
  2569         -**  INSERT INTO table1 VALUES('It's a happy day!');
  2570         -** </pre></blockquote>
  2571         -**
  2572         -** This second example is an SQL syntax error.  As a general rule you should
  2573         -** always use %q instead of %s when inserting text into a string literal.
  2574         -**
  2575         -** ^(The %Q option works like %q except it also adds single quotes around
  2576         -** the outside of the total string.  Additionally, if the parameter in the
  2577         -** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  2578         -** single quotes).)^  So, for example, one could say:
  2579         -**
  2580         -** <blockquote><pre>
  2581         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  2582         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  2583         -**  sqlite3_free(zSQL);
  2584         -** </pre></blockquote>
  2585         -**
  2586         -** The code above will render a correct SQL statement in the zSQL
  2587         -** variable even if the zText variable is a NULL pointer.
  2588         -**
  2589         -** ^(The "%w" formatting option is like "%q" except that it expects to
  2590         -** be contained within double-quotes instead of single quotes, and it
  2591         -** escapes the double-quote character instead of the single-quote
  2592         -** character.)^  The "%w" formatting option is intended for safely inserting
  2593         -** table and column names into a constructed SQL statement.
  2594         -**
  2595         -** ^(The "%z" formatting option works like "%s" but with the
  2596         -** addition that after the string has been read and copied into
  2597         -** the result, [sqlite3_free()] is called on the input string.)^
         2548  +** See also:  [built-in printf()], [printf() SQL function]
  2598   2549   */
  2599   2550   SQLITE_API char *sqlite3_mprintf(const char*,...);
  2600   2551   SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  2601   2552   SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  2602   2553   SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  2603   2554   
  2604   2555   /*
................................................................................
  2948   2899      void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
  2949   2900   
  2950   2901   /*
  2951   2902   ** CAPI3REF: SQL Trace Event Codes
  2952   2903   ** KEYWORDS: SQLITE_TRACE
  2953   2904   **
  2954   2905   ** These constants identify classes of events that can be monitored
  2955         -** using the [sqlite3_trace_v2()] tracing logic.  The third argument
  2956         -** to [sqlite3_trace_v2()] is an OR-ed combination of one or more of
         2906  +** using the [sqlite3_trace_v2()] tracing logic.  The M argument
         2907  +** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
  2957   2908   ** the following constants.  ^The first argument to the trace callback
  2958   2909   ** is one of the following constants.
  2959   2910   **
  2960   2911   ** New tracing constants may be added in future releases.
  2961   2912   **
  2962   2913   ** ^A trace callback has four arguments: xCallback(T,C,P,X).
  2963   2914   ** ^The T argument is one of the integer type codes above.
................................................................................
  3656   3607   ** a schema change, on the first  [sqlite3_step()] call following any change
  3657   3608   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  3658   3609   ** ^The specific value of WHERE-clause [parameter] might influence the 
  3659   3610   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  3660   3611   ** or [GLOB] operator or if the parameter is compared to an indexed column
  3661   3612   ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  3662   3613   ** </li>
         3614  +** </ol>
  3663   3615   **
  3664   3616   ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
  3665   3617   ** the extra prepFlags parameter, which is a bit array consisting of zero or
  3666   3618   ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
  3667   3619   ** sqlite3_prepare_v2() interface works exactly the same as
  3668   3620   ** sqlite3_prepare_v3() with a zero prepFlags parameter.
  3669         -** </ol>
  3670   3621   */
  3671   3622   SQLITE_API int sqlite3_prepare(
  3672   3623     sqlite3 *db,            /* Database handle */
  3673   3624     const char *zSql,       /* SQL statement, UTF-8 encoded */
  3674   3625     int nByte,              /* Maximum length of zSql in bytes. */
  3675   3626     sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3676   3627     const char **pzTail     /* OUT: Pointer to unused portion of zSql */
................................................................................
  7290   7241   ** wal file in wal mode databases, or the number of pages written to the
  7291   7242   ** database file in rollback mode databases. Any pages written as part of
  7292   7243   ** transaction rollback or database recovery operations are not included.
  7293   7244   ** If an IO or other error occurs while writing a page to disk, the effect
  7294   7245   ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  7295   7246   ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  7296   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>
  7297   7257   **
  7298   7258   ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  7299   7259   ** <dd>This parameter returns zero for the current value if and only if
  7300   7260   ** all foreign key constraints (deferred or immediate) have been
  7301   7261   ** resolved.)^  ^The highwater mark is always 0.
  7302   7262   ** </dd>
  7303   7263   ** </dl>
................................................................................
  7310   7270   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  7311   7271   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  7312   7272   #define SQLITE_DBSTATUS_CACHE_HIT            7
  7313   7273   #define SQLITE_DBSTATUS_CACHE_MISS           8
  7314   7274   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  7315   7275   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  7316   7276   #define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
  7317         -#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
         7277  +#define SQLITE_DBSTATUS_CACHE_SPILL         12
         7278  +#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
  7318   7279   
  7319   7280   
  7320   7281   /*
  7321   7282   ** CAPI3REF: Prepared Statement Status
  7322   7283   ** METHOD: sqlite3_stmt
  7323   7284   **
  7324   7285   ** ^(Each prepared statement maintains various
................................................................................
  8790   8751   ** transaction open on the database, or if the database is not a wal mode
  8791   8752   ** database.
  8792   8753   **
  8793   8754   ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  8794   8755   */
  8795   8756   SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
  8796   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  +
  8797   8880   /*
  8798   8881   ** Undo the hack that converts floating point types to integer for
  8799   8882   ** builds on processors without floating point support.
  8800   8883   */
  8801   8884   #ifdef SQLITE_OMIT_FLOATING_POINT
  8802   8885   # undef double
  8803   8886   #endif
................................................................................
  8937   9020   #ifdef __cplusplus
  8938   9021   extern "C" {
  8939   9022   #endif
  8940   9023   
  8941   9024   
  8942   9025   /*
  8943   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.
  8944   9030   */
  8945   9031   typedef struct sqlite3_session sqlite3_session;
  8946   9032   
  8947   9033   /*
  8948   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].
  8949   9038   */
  8950   9039   typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
  8951   9040   
  8952   9041   /*
  8953   9042   ** CAPI3REF: Create A New Session Object
         9043  +** CONSTRUCTOR: sqlite3_session
  8954   9044   **
  8955   9045   ** Create a new session object attached to database handle db. If successful,
  8956   9046   ** a pointer to the new object is written to *ppSession and SQLITE_OK is
  8957   9047   ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
  8958   9048   ** error code (e.g. SQLITE_NOMEM) is returned.
  8959   9049   **
  8960   9050   ** It is possible to create multiple session objects attached to a single
................................................................................
  8983   9073     sqlite3 *db,                    /* Database handle */
  8984   9074     const char *zDb,                /* Name of db (e.g. "main") */
  8985   9075     sqlite3_session **ppSession     /* OUT: New session object */
  8986   9076   );
  8987   9077   
  8988   9078   /*
  8989   9079   ** CAPI3REF: Delete A Session Object
         9080  +** DESTRUCTOR: sqlite3_session
  8990   9081   **
  8991   9082   ** Delete a session object previously allocated using 
  8992   9083   ** [sqlite3session_create()]. Once a session object has been deleted, the
  8993   9084   ** results of attempting to use pSession with any other session module
  8994   9085   ** function are undefined.
  8995   9086   **
  8996   9087   ** Session objects must be deleted before the database handle to which they
................................................................................
  8998   9089   ** [sqlite3session_create()] for details.
  8999   9090   */
  9000   9091   SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
  9001   9092   
  9002   9093   
  9003   9094   /*
  9004   9095   ** CAPI3REF: Enable Or Disable A Session Object
         9096  +** METHOD: sqlite3_session
  9005   9097   **
  9006   9098   ** Enable or disable the recording of changes by a session object. When
  9007   9099   ** enabled, a session object records changes made to the database. When
  9008   9100   ** disabled - it does not. A newly created session object is enabled.
  9009   9101   ** Refer to the documentation for [sqlite3session_changeset()] for further
  9010   9102   ** details regarding how enabling and disabling a session object affects
  9011   9103   ** the eventual changesets.
................................................................................
  9017   9109   ** The return value indicates the final state of the session object: 0 if 
  9018   9110   ** the session is disabled, or 1 if it is enabled.
  9019   9111   */
  9020   9112   SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
  9021   9113   
  9022   9114   /*
  9023   9115   ** CAPI3REF: Set Or Clear the Indirect Change Flag
         9116  +** METHOD: sqlite3_session
  9024   9117   **
  9025   9118   ** Each change recorded by a session object is marked as either direct or
  9026   9119   ** indirect. A change is marked as indirect if either:
  9027   9120   **
  9028   9121   ** <ul>
  9029   9122   **   <li> The session object "indirect" flag is set when the change is
  9030   9123   **        made, or
................................................................................
  9046   9139   ** The return value indicates the final state of the indirect flag: 0 if 
  9047   9140   ** it is clear, or 1 if it is set.
  9048   9141   */
  9049   9142   SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
  9050   9143   
  9051   9144   /*
  9052   9145   ** CAPI3REF: Attach A Table To A Session Object
         9146  +** METHOD: sqlite3_session
  9053   9147   **
  9054   9148   ** If argument zTab is not NULL, then it is the name of a table to attach
  9055   9149   ** to the session object passed as the first argument. All subsequent changes 
  9056   9150   ** made to the table while the session object is enabled will be recorded. See 
  9057   9151   ** documentation for [sqlite3session_changeset()] for further details.
  9058   9152   **
  9059   9153   ** Or, if argument zTab is NULL, then changes are recorded for all tables
................................................................................
  9071   9165   ** no changes will be recorded in either of these scenarios.
  9072   9166   **
  9073   9167   ** Changes are not recorded for individual rows that have NULL values stored
  9074   9168   ** in one or more of their PRIMARY KEY columns.
  9075   9169   **
  9076   9170   ** SQLITE_OK is returned if the call completes without error. Or, if an error 
  9077   9171   ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
         9172  +**
         9173  +** <h3>Special sqlite_stat1 Handling</h3>
         9174  +**
         9175  +** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to 
         9176  +** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
         9177  +**  <pre>
         9178  +**  &nbsp;     CREATE TABLE sqlite_stat1(tbl,idx,stat)  
         9179  +**  </pre>
         9180  +**
         9181  +** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are 
         9182  +** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes 
         9183  +** are recorded for rows for which (idx IS NULL) is true. However, for such
         9184  +** rows a zero-length blob (SQL value X'') is stored in the changeset or
         9185  +** patchset instead of a NULL value. This allows such changesets to be
         9186  +** manipulated by legacy implementations of sqlite3changeset_invert(),
         9187  +** concat() and similar.
         9188  +**
         9189  +** The sqlite3changeset_apply() function automatically converts the 
         9190  +** zero-length blob back to a NULL value when updating the sqlite_stat1
         9191  +** table. However, if the application calls sqlite3changeset_new(),
         9192  +** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset 
         9193  +** iterator directly (including on a changeset iterator passed to a
         9194  +** conflict-handler callback) then the X'' value is returned. The application
         9195  +** must translate X'' to NULL itself if required.
         9196  +**
         9197  +** Legacy (older than 3.22.0) versions of the sessions module cannot capture
         9198  +** changes made to the sqlite_stat1 table. Legacy versions of the
         9199  +** sqlite3changeset_apply() function silently ignore any modifications to the
         9200  +** sqlite_stat1 table that are part of a changeset or patchset.
  9078   9201   */
  9079   9202   SQLITE_API int sqlite3session_attach(
  9080   9203     sqlite3_session *pSession,      /* Session object */
  9081   9204     const char *zTab                /* Table name */
  9082   9205   );
  9083   9206   
  9084   9207   /*
  9085   9208   ** CAPI3REF: Set a table filter on a Session Object.
         9209  +** METHOD: sqlite3_session
  9086   9210   **
  9087   9211   ** The second argument (xFilter) is the "filter callback". For changes to rows 
  9088   9212   ** in tables that are not attached to the Session object, the filter is called
  9089   9213   ** to determine whether changes to the table's rows should be tracked or not. 
  9090   9214   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
  9091   9215   ** attached, xFilter will not be called again.
  9092   9216   */
................................................................................
  9097   9221       const char *zTab              /* Table name */
  9098   9222     ),
  9099   9223     void *pCtx                      /* First argument passed to xFilter */
  9100   9224   );
  9101   9225   
  9102   9226   /*
  9103   9227   ** CAPI3REF: Generate A Changeset From A Session Object
         9228  +** METHOD: sqlite3_session
  9104   9229   **
  9105   9230   ** Obtain a changeset containing changes to the tables attached to the 
  9106   9231   ** session object passed as the first argument. If successful, 
  9107   9232   ** set *ppChangeset to point to a buffer containing the changeset 
  9108   9233   ** and *pnChangeset to the size of the changeset in bytes before returning
  9109   9234   ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
  9110   9235   ** zero and return an SQLite error code.
................................................................................
  9206   9331   SQLITE_API int sqlite3session_changeset(
  9207   9332     sqlite3_session *pSession,      /* Session object */
  9208   9333     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
  9209   9334     void **ppChangeset              /* OUT: Buffer containing changeset */
  9210   9335   );
  9211   9336   
  9212   9337   /*
  9213         -** CAPI3REF: Load The Difference Between Tables Into A Session 
         9338  +** CAPI3REF: Load The Difference Between Tables Into A Session
         9339  +** METHOD: sqlite3_session
  9214   9340   **
  9215   9341   ** If it is not already attached to the session object passed as the first
  9216   9342   ** argument, this function attaches table zTbl in the same manner as the
  9217   9343   ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
  9218   9344   ** does not have a primary key, this function is a no-op (but does not return
  9219   9345   ** an error).
  9220   9346   **
................................................................................
  9271   9397     const char *zTbl,
  9272   9398     char **pzErrMsg
  9273   9399   );
  9274   9400   
  9275   9401   
  9276   9402   /*
  9277   9403   ** CAPI3REF: Generate A Patchset From A Session Object
         9404  +** METHOD: sqlite3_session
  9278   9405   **
  9279   9406   ** The differences between a patchset and a changeset are that:
  9280   9407   **
  9281   9408   ** <ul>
  9282   9409   **   <li> DELETE records consist of the primary key fields only. The 
  9283   9410   **        original values of other fields are omitted.
  9284   9411   **   <li> The original values of any modified fields are omitted from 
................................................................................
  9322   9449   ** guaranteed that a call to sqlite3session_changeset() will return a 
  9323   9450   ** changeset containing zero changes.
  9324   9451   */
  9325   9452   SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
  9326   9453   
  9327   9454   /*
  9328   9455   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
         9456  +** CONSTRUCTOR: sqlite3_changeset_iter
  9329   9457   **
  9330   9458   ** Create an iterator used to iterate through the contents of a changeset.
  9331   9459   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
  9332   9460   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
  9333   9461   ** SQLite error code is returned.
  9334   9462   **
  9335   9463   ** The following functions can be used to advance and query a changeset 
................................................................................
  9362   9490     int nChangeset,                 /* Size of changeset blob in bytes */
  9363   9491     void *pChangeset                /* Pointer to blob containing changeset */
  9364   9492   );
  9365   9493   
  9366   9494   
  9367   9495   /*
  9368   9496   ** CAPI3REF: Advance A Changeset Iterator
         9497  +** METHOD: sqlite3_changeset_iter
  9369   9498   **
  9370   9499   ** This function may only be used with iterators created by function
  9371   9500   ** [sqlite3changeset_start()]. If it is called on an iterator passed to
  9372   9501   ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
  9373   9502   ** is returned and the call has no effect.
  9374   9503   **
  9375   9504   ** Immediately after an iterator is created by sqlite3changeset_start(), it
................................................................................
  9386   9515   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
  9387   9516   ** SQLITE_NOMEM.
  9388   9517   */
  9389   9518   SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
  9390   9519   
  9391   9520   /*
  9392   9521   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
         9522  +** METHOD: sqlite3_changeset_iter
  9393   9523   **
  9394   9524   ** The pIter argument passed to this function may either be an iterator
  9395   9525   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9396   9526   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  9397   9527   ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
  9398   9528   ** is not the case, this function returns [SQLITE_MISUSE].
  9399   9529   **
................................................................................
  9420   9550     int *pnCol,                     /* OUT: Number of columns in table */
  9421   9551     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
  9422   9552     int *pbIndirect                 /* OUT: True for an 'indirect' change */
  9423   9553   );
  9424   9554   
  9425   9555   /*
  9426   9556   ** CAPI3REF: Obtain The Primary Key Definition Of A Table
         9557  +** METHOD: sqlite3_changeset_iter
  9427   9558   **
  9428   9559   ** For each modified table, a changeset includes the following:
  9429   9560   **
  9430   9561   ** <ul>
  9431   9562   **   <li> The number of columns in the table, and
  9432   9563   **   <li> Which of those columns make up the tables PRIMARY KEY.
  9433   9564   ** </ul>
................................................................................
  9451   9582     sqlite3_changeset_iter *pIter,  /* Iterator object */
  9452   9583     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
  9453   9584     int *pnCol                      /* OUT: Number of entries in output array */
  9454   9585   );
  9455   9586   
  9456   9587   /*
  9457   9588   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
         9589  +** METHOD: sqlite3_changeset_iter
  9458   9590   **
  9459   9591   ** The pIter argument passed to this function may either be an iterator
  9460   9592   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9461   9593   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  9462   9594   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
  9463   9595   ** Furthermore, it may only be called if the type of change that the iterator
  9464   9596   ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
................................................................................
  9481   9613     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9482   9614     int iVal,                       /* Column number */
  9483   9615     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
  9484   9616   );
  9485   9617   
  9486   9618   /*
  9487   9619   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
         9620  +** METHOD: sqlite3_changeset_iter
  9488   9621   **
  9489   9622   ** The pIter argument passed to this function may either be an iterator
  9490   9623   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9491   9624   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
  9492   9625   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
  9493   9626   ** Furthermore, it may only be called if the type of change that the iterator
  9494   9627   ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
................................................................................
  9514   9647     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9515   9648     int iVal,                       /* Column number */
  9516   9649     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
  9517   9650   );
  9518   9651   
  9519   9652   /*
  9520   9653   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
         9654  +** METHOD: sqlite3_changeset_iter
  9521   9655   **
  9522   9656   ** This function should only be used with iterator objects passed to a
  9523   9657   ** conflict-handler callback by [sqlite3changeset_apply()] with either
  9524   9658   ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
  9525   9659   ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
  9526   9660   ** is set to NULL.
  9527   9661   **
................................................................................
  9541   9675     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9542   9676     int iVal,                       /* Column number */
  9543   9677     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
  9544   9678   );
  9545   9679   
  9546   9680   /*
  9547   9681   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
         9682  +** METHOD: sqlite3_changeset_iter
  9548   9683   **
  9549   9684   ** This function may only be called with an iterator passed to an
  9550   9685   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
  9551   9686   ** it sets the output variable to the total number of known foreign key
  9552   9687   ** violations in the destination database and returns SQLITE_OK.
  9553   9688   **
  9554   9689   ** In all other cases this function returns SQLITE_MISUSE.
................................................................................
  9557   9692     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9558   9693     int *pnOut                      /* OUT: Number of FK violations */
  9559   9694   );
  9560   9695   
  9561   9696   
  9562   9697   /*
  9563   9698   ** CAPI3REF: Finalize A Changeset Iterator
         9699  +** METHOD: sqlite3_changeset_iter
  9564   9700   **
  9565   9701   ** This function is used to finalize an iterator allocated with
  9566   9702   ** [sqlite3changeset_start()].
  9567   9703   **
  9568   9704   ** This function should only be called on iterators created using the
  9569   9705   ** [sqlite3changeset_start()] function. If an application calls this
  9570   9706   ** function with an iterator passed to a conflict-handler by
................................................................................
  9573   9709   **
  9574   9710   ** If an error was encountered within a call to an sqlite3changeset_xxx()
  9575   9711   ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
  9576   9712   ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
  9577   9713   ** to that error is returned by this function. Otherwise, SQLITE_OK is
  9578   9714   ** returned. This is to allow the following pattern (pseudo-code):
  9579   9715   **
         9716  +** <pre>
  9580   9717   **   sqlite3changeset_start();
  9581   9718   **   while( SQLITE_ROW==sqlite3changeset_next() ){
  9582   9719   **     // Do something with change.
  9583   9720   **   }
  9584   9721   **   rc = sqlite3changeset_finalize();
  9585   9722   **   if( rc!=SQLITE_OK ){
  9586   9723   **     // An error has occurred 
  9587   9724   **   }
         9725  +** </pre>
  9588   9726   */
  9589   9727   SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
  9590   9728   
  9591   9729   /*
  9592   9730   ** CAPI3REF: Invert A Changeset
  9593   9731   **
  9594   9732   ** This function is used to "invert" a changeset object. Applying an inverted
................................................................................
  9628   9766   ** single changeset. The result is a changeset equivalent to applying
  9629   9767   ** changeset A followed by changeset B. 
  9630   9768   **
  9631   9769   ** This function combines the two input changesets using an 
  9632   9770   ** sqlite3_changegroup object. Calling it produces similar results as the
  9633   9771   ** following code fragment:
  9634   9772   **
         9773  +** <pre>
  9635   9774   **   sqlite3_changegroup *pGrp;
  9636   9775   **   rc = sqlite3_changegroup_new(&pGrp);
  9637   9776   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
  9638   9777   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
  9639   9778   **   if( rc==SQLITE_OK ){
  9640   9779   **     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
  9641   9780   **   }else{
  9642   9781   **     *ppOut = 0;
  9643   9782   **     *pnOut = 0;
  9644   9783   **   }
         9784  +** </pre>
  9645   9785   **
  9646   9786   ** Refer to the sqlite3_changegroup documentation below for details.
  9647   9787   */
  9648   9788   SQLITE_API int sqlite3changeset_concat(
  9649   9789     int nA,                         /* Number of bytes in buffer pA */
  9650   9790     void *pA,                       /* Pointer to buffer containing changeset A */
  9651   9791     int nB,                         /* Number of bytes in buffer pB */
................................................................................
  9653   9793     int *pnOut,                     /* OUT: Number of bytes in output changeset */
  9654   9794     void **ppOut                    /* OUT: Buffer containing output changeset */
  9655   9795   );
  9656   9796   
  9657   9797   
  9658   9798   /*
  9659   9799   ** CAPI3REF: Changegroup Handle
         9800  +**
         9801  +** A changegroup is an object used to combine two or more 
         9802  +** [changesets] or [patchsets]
  9660   9803   */
  9661   9804   typedef struct sqlite3_changegroup sqlite3_changegroup;
  9662   9805   
  9663   9806   /*
  9664   9807   ** CAPI3REF: Create A New Changegroup Object
         9808  +** CONSTRUCTOR: sqlite3_changegroup
  9665   9809   **
  9666   9810   ** An sqlite3_changegroup object is used to combine two or more changesets
  9667   9811   ** (or patchsets) into a single changeset (or patchset). A single changegroup
  9668   9812   ** object may combine changesets or patchsets, but not both. The output is
  9669   9813   ** always in the same format as the input.
  9670   9814   **
  9671   9815   ** If successful, this function returns SQLITE_OK and populates (*pp) with
................................................................................
  9695   9839   ** sqlite3changegroup_output() functions, also available are the streaming
  9696   9840   ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
  9697   9841   */
  9698   9842   SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
  9699   9843   
  9700   9844   /*
  9701   9845   ** CAPI3REF: Add A Changeset To A Changegroup
         9846  +** METHOD: sqlite3_changegroup
  9702   9847   **
  9703   9848   ** Add all changes within the changeset (or patchset) in buffer pData (size
  9704   9849   ** nData bytes) to the changegroup. 
  9705   9850   **
  9706   9851   ** If the buffer contains a patchset, then all prior calls to this function
  9707   9852   ** on the same changegroup object must also have specified patchsets. Or, if
  9708   9853   ** the buffer contains a changeset, so must have the earlier calls to this
................................................................................
  9772   9917   **
  9773   9918   ** If no error occurs, SQLITE_OK is returned.
  9774   9919   */
  9775   9920   SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
  9776   9921   
  9777   9922   /*
  9778   9923   ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
         9924  +** METHOD: sqlite3_changegroup
  9779   9925   **
  9780   9926   ** Obtain a buffer containing a changeset (or patchset) representing the
  9781   9927   ** current contents of the changegroup. If the inputs to the changegroup
  9782   9928   ** were themselves changesets, the output is a changeset. Or, if the
  9783   9929   ** inputs were patchsets, the output is also a patchset.
  9784   9930   **
  9785   9931   ** As with the output of the sqlite3session_changeset() and
................................................................................
  9802   9948     sqlite3_changegroup*,
  9803   9949     int *pnData,                    /* OUT: Size of output buffer in bytes */
  9804   9950     void **ppData                   /* OUT: Pointer to output buffer */
  9805   9951   );
  9806   9952   
  9807   9953   /*
  9808   9954   ** CAPI3REF: Delete A Changegroup Object
         9955  +** DESTRUCTOR: sqlite3_changegroup
  9809   9956   */
  9810   9957   SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
  9811   9958   
  9812   9959   /*
  9813   9960   ** CAPI3REF: Apply A Changeset To A Database
  9814   9961   **
  9815         -** Apply a changeset to a database. This function attempts to update the
  9816         -** "main" database attached to handle db with the changes found in the
  9817         -** 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. 
  9818   9965   **
  9819         -** The fourth argument (xFilter) passed to this function is the "filter
         9966  +** The fourth argument (xFilter) passed to these functions is the "filter
  9820   9967   ** callback". If it is not NULL, then for each table affected by at least one
  9821   9968   ** change in the changeset, the filter callback is invoked with
  9822   9969   ** the table name as the second argument, and a copy of the context pointer
  9823         -** passed as the sixth argument to this function as the first. If the "filter
  9824         -** callback" returns zero, then no attempt is made to apply any changes to 
  9825         -** the table. Otherwise, if the return value is non-zero or the xFilter
  9826         -** argument to this function is NULL, all changes related to the table are
  9827         -** 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.
  9828   9974   **
  9829   9975   ** For each table that is not excluded by the filter callback, this function 
  9830   9976   ** tests that the target database contains a compatible table. A table is 
  9831   9977   ** considered compatible if all of the following are true:
  9832   9978   **
  9833   9979   ** <ul>
  9834   9980   **   <li> The table has the same name as the name recorded in the 
................................................................................
  9865  10011   ** actions are taken by sqlite3changeset_apply() depending on the value
  9866  10012   ** returned by each invocation of the conflict-handler function. Refer to
  9867  10013   ** the documentation for the three 
  9868  10014   ** [SQLITE_CHANGESET_OMIT|available return values] for details.
  9869  10015   **
  9870  10016   ** <dl>
  9871  10017   ** <dt>DELETE Changes<dd>
  9872         -**   For each DELETE change, this function checks if the target database 
        10018  +**   For each DELETE change, the function checks if the target database 
  9873  10019   **   contains a row with the same primary key value (or values) as the 
  9874  10020   **   original row values stored in the changeset. If it does, and the values 
  9875  10021   **   stored in all non-primary key columns also match the values stored in 
  9876  10022   **   the changeset the row is deleted from the target database.
  9877  10023   **
  9878  10024   **   If a row with matching primary key values is found, but one or more of
  9879  10025   **   the non-primary key fields contains a value different from the original
................................................................................
  9910  10056   **   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 
  9911  10057   **   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
  9912  10058   **   This includes the case where the INSERT operation is re-attempted because 
  9913  10059   **   an earlier call to the conflict handler function returned 
  9914  10060   **   [SQLITE_CHANGESET_REPLACE].
  9915  10061   **
  9916  10062   ** <dt>UPDATE Changes<dd>
  9917         -**   For each UPDATE change, this function checks if the target database 
        10063  +**   For each UPDATE change, the function checks if the target database 
  9918  10064   **   contains a row with the same primary key value (or values) as the 
  9919  10065   **   original row values stored in the changeset. If it does, and the values 
  9920  10066   **   stored in all modified non-primary key columns also match the values
  9921  10067   **   stored in the changeset the row is updated within the target database.
  9922  10068   **
  9923  10069   **   If a row with matching primary key values is found, but one or more of
  9924  10070   **   the modified non-primary key fields contains a value different from an
................................................................................
  9941  10087   ** </dl>
  9942  10088   **
  9943  10089   ** It is safe to execute SQL statements, including those that write to the
  9944  10090   ** table that the callback related to, from within the xConflict callback.
  9945  10091   ** This can be used to further customize the applications conflict
  9946  10092   ** resolution strategy.
  9947  10093   **
  9948         -** 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.
  9949  10095   ** If any other error (aside from a constraint failure when attempting to
  9950  10096   ** write to the target database) occurs, then the savepoint transaction is
  9951  10097   ** rolled back, restoring the target database to its original state, and an 
  9952  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.
  9953  10109   */
  9954  10110   SQLITE_API int sqlite3changeset_apply(
  9955  10111     sqlite3 *db,                    /* Apply change to "main" db of this handle */
  9956  10112     int nChangeset,                 /* Size of changeset in bytes */
  9957  10113     void *pChangeset,               /* Changeset blob */
  9958  10114     int(*xFilter)(
  9959  10115       void *pCtx,                   /* Copy of sixth arg to _apply() */
................................................................................
  9961  10117     ),
  9962  10118     int(*xConflict)(
  9963  10119       void *pCtx,                   /* Copy of sixth arg to _apply() */
  9964  10120       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
  9965  10121       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
  9966  10122     ),
  9967  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
  9968  10140   );
  9969  10141   
  9970  10142   /* 
  9971  10143   ** CAPI3REF: Constants Passed To The Conflict Handler
  9972  10144   **
  9973  10145   ** Values that may be passed as the second argument to a conflict-handler.
  9974  10146   **
................................................................................
 10059  10231   **   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
 10060  10232   ** </dl>
 10061  10233   */
 10062  10234   #define SQLITE_CHANGESET_OMIT       0
 10063  10235   #define SQLITE_CHANGESET_REPLACE    1
 10064  10236   #define SQLITE_CHANGESET_ABORT      2
 10065  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  +
 10066  10393   /*
 10067  10394   ** CAPI3REF: Streaming Versions of API functions.
 10068  10395   **
 10069  10396   ** The six streaming API xxx_strm() functions serve similar purposes to the 
 10070  10397   ** corresponding non-streaming API functions:
 10071  10398   **
 10072  10399   ** <table border=1 style="margin-left:8ex;margin-right:8ex">
................................................................................
 10162  10489     ),
 10163  10490     int(*xConflict)(
 10164  10491       void *pCtx,                   /* Copy of sixth arg to _apply() */
 10165  10492       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 10166  10493       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 10167  10494     ),
 10168  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
 10169  10512   );
 10170  10513   SQLITE_API int sqlite3changeset_concat_strm(
 10171  10514     int (*xInputA)(void *pIn, void *pData, int *pnData),
 10172  10515     void *pInA,
 10173  10516     int (*xInputB)(void *pIn, void *pData, int *pnData),
 10174  10517     void *pInB,
 10175  10518     int (*xOutput)(void *pOut, const void *pData, int nData),
................................................................................
 10199  10542   SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
 10200  10543       int (*xInput)(void *pIn, void *pData, int *pnData),
 10201  10544       void *pIn
 10202  10545   );
 10203  10546   SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
 10204  10547       int (*xOutput)(void *pOut, const void *pData, int nData), 
 10205  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
 10206  10556   );
 10207  10557   
 10208  10558   
 10209  10559   /*
 10210  10560   ** Make sure we can call this stuff from C++.
 10211  10561   */
 10212  10562   #ifdef __cplusplus