System.Data.SQLite
Check-in [5540efea0c]
Not logged in

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

Overview
Comment:Update SQLite core library to the 3.23.0 release.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5540efea0c93d46e7c650b12799117d246768626
User & Date: mistachkin 2018-04-05 03:42:54
Context
2018-04-05
03:45
Pickup the SQLite core library 3.23.0 docs from upstream. check-in: 6bb5d25e27 user: mistachkin tags: trunk
03:42
Update SQLite core library to the 3.23.0 release. check-in: 5540efea0c user: mistachkin tags: trunk
03:39
Add some comments to a test file. check-in: 72990f698b user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Doc/Extra/Provider/version.html.

    41     41         </table>
    42     42       </div>
    43     43       <div id="mainSection">
    44     44       <div id="mainBody">
    45     45       <h1 class="heading">Version History</h1>
    46     46       <p><b>1.0.109.0 - May XX, 2018 <font color="red">(release scheduled)</font></b></p>
    47     47       <ul>
    48         -      <li>Updated to <a href="https://www.sqlite.org/draft/releaselog/3_23_0.html">SQLite 3.23.0</a>.</li>
           48  +      <li>Updated to <a href="https://www.sqlite.org/releaselog/3_23_0.html">SQLite 3.23.0</a>.</li>
    49     49         <li>Prevent GetSchemaTable from throwing InvalidCastException. Fix for <a href="https://system.data.sqlite.org/index.html/info/baf42ee135">[baf42ee135]</a>.</li>
    50     50       </ul>
    51     51       <p><b>1.0.108.0 - March 2, 2018</b></p>
    52     52       <ul>
    53     53         <li>Support extended result codes when messages are looked up without the SQLite core library.</li>
    54     54         <li>Override System.Object members for the SQLiteException class to improve its ToString return value. Pursuant to <a href="https://system.data.sqlite.org/index.html/info/53962f9eff">[53962f9eff]</a>.</li>
    55     55         <li>More database connection configuration options for the <a href="https://www.sqlite.org/c3ref/db_config.html">sqlite3_db_config()</a> interface.&nbsp;<b>** Potentially Incompatible Change **</b></li>

Changes to SQLite.Interop/src/core/sqlite3.c.

   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
................................................................................
  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   1150   #define SQLITE_VERSION        "3.23.0"
  1151   1151   #define SQLITE_VERSION_NUMBER 3023000
  1152         -#define SQLITE_SOURCE_ID      "2018-03-08 18:09:22 0f5a8666b8b479bb6e470590659c1775fb9b0d6a9eee931cb48f98651cd7ffcb"
         1152  +#define SQLITE_SOURCE_ID      "2018-04-02 11:04:16 736b53f57f70b23172c30880186dce7ad9baa3b74e3838cae5847cffb98f5cd2"
  1153   1153   
  1154   1154   /*
  1155   1155   ** CAPI3REF: Run-Time Library Version Numbers
  1156   1156   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1157   1157   **
  1158   1158   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1159   1159   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  2084   2084   ** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
  2085   2085   ** operations since the previous successful call to 
  2086   2086   ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
  2087   2087   ** ^This file control takes the file descriptor out of batch write mode
  2088   2088   ** so that all subsequent write operations are independent.
  2089   2089   ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
  2090   2090   ** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
         2091  +**
         2092  +** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
         2093  +** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
         2094  +** a file lock using the xLock or xShmLock methods of the VFS to wait
         2095  +** for up to M milliseconds before failing, where M is the single 
         2096  +** unsigned integer parameter.
  2091   2097   ** </ul>
  2092   2098   */
  2093   2099   #define SQLITE_FCNTL_LOCKSTATE               1
  2094   2100   #define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
  2095   2101   #define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
  2096   2102   #define SQLITE_FCNTL_LAST_ERRNO              4
  2097   2103   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
  2118   2124   #define SQLITE_FCNTL_VFS_POINTER            27
  2119   2125   #define SQLITE_FCNTL_JOURNAL_POINTER        28
  2120   2126   #define SQLITE_FCNTL_WIN32_GET_HANDLE       29
  2121   2127   #define SQLITE_FCNTL_PDB                    30
  2122   2128   #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
  2123   2129   #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
  2124   2130   #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
         2131  +#define SQLITE_FCNTL_LOCK_TIMEOUT           34
  2125   2132   
  2126   2133   /* deprecated names */
  2127   2134   #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
  2128   2135   #define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
  2129   2136   #define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
  2130   2137   
  2131   2138   
................................................................................
  3074   3081   **
  3075   3082   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  3076   3083   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  3077   3084   ** database handle, SQLite checks if this will mean that there are now no 
  3078   3085   ** connections at all to the database. If so, it performs a checkpoint 
  3079   3086   ** operation before closing the connection. This option may be used to
  3080   3087   ** override this behaviour. The first parameter passed to this operation
  3081         -** is an integer - non-zero to disable checkpoints-on-close, or zero (the
  3082         -** default) to enable them. The second parameter is a pointer to an integer
         3088  +** is an integer - positive to disable checkpoints-on-close, or zero (the
         3089  +** default) to enable them, and negative to leave the setting unchanged.
         3090  +** The second parameter is a pointer to an integer
  3083   3091   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  3084   3092   ** have been disabled - 0 if they are not disabled, 1 if they are.
  3085   3093   ** </dd>
         3094  +**
  3086   3095   ** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  3087   3096   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  3088   3097   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  3089   3098   ** a single SQL query statement will always use the same algorithm regardless
  3090   3099   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  3091   3100   ** that look at the values of bound parameters, which can make some queries
  3092   3101   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  3093   3102   ** the QPSG active, SQLite will always use the same query plan in the field as
  3094   3103   ** was used during testing in the lab.
         3104  +** The first argument to this setting is an integer which is 0 to disable 
         3105  +** the QPSG, positive to enable QPSG, or negative to leave the setting
         3106  +** unchanged. The second parameter is a pointer to an integer into which
         3107  +** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
         3108  +** following this call.
  3095   3109   ** </dd>
         3110  +**
  3096   3111   ** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  3097   3112   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  3098   3113   ** include output for any operations performed by trigger programs. This
  3099   3114   ** option is used to set or clear (the default) a flag that governs this
  3100   3115   ** behavior. The first parameter passed to this operation is an integer -
  3101         -** non-zero to enable output for trigger programs, or zero to disable it.
         3116  +** positive to enable output for trigger programs, or zero to disable it,
         3117  +** or negative to leave the setting unchanged.
  3102   3118   ** The second parameter is a pointer to an integer into which is written 
  3103   3119   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  3104   3120   ** it is not disabled, 1 if it is.  
  3105   3121   ** </dd>
  3106   3122   ** </dl>
  3107   3123   */
  3108   3124   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
................................................................................
  8249   8265   ** wal file in wal mode databases, or the number of pages written to the
  8250   8266   ** database file in rollback mode databases. Any pages written as part of
  8251   8267   ** transaction rollback or database recovery operations are not included.
  8252   8268   ** If an IO or other error occurs while writing a page to disk, the effect
  8253   8269   ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  8254   8270   ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  8255   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>
  8256   8281   **
  8257   8282   ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  8258   8283   ** <dd>This parameter returns zero for the current value if and only if
  8259   8284   ** all foreign key constraints (deferred or immediate) have been
  8260   8285   ** resolved.)^  ^The highwater mark is always 0.
  8261   8286   ** </dd>
  8262   8287   ** </dl>
................................................................................
  8269   8294   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  8270   8295   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  8271   8296   #define SQLITE_DBSTATUS_CACHE_HIT            7
  8272   8297   #define SQLITE_DBSTATUS_CACHE_MISS           8
  8273   8298   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  8274   8299   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  8275   8300   #define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
  8276         -#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
         8301  +#define SQLITE_DBSTATUS_CACHE_SPILL         12
         8302  +#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
  8277   8303   
  8278   8304   
  8279   8305   /*
  8280   8306   ** CAPI3REF: Prepared Statement Status
  8281   8307   ** METHOD: sqlite3_stmt
  8282   8308   **
  8283   8309   ** ^(Each prepared statement maintains various
................................................................................
  9812   9838   */
  9813   9839   #define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
  9814   9840   
  9815   9841   /*
  9816   9842   ** CAPI3REF: Deserialize a database
  9817   9843   **
  9818   9844   ** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the 
  9819         -** [database connection] D to disconnection from database S and then
         9845  +** [database connection] D to disconnect from database S and then
  9820   9846   ** reopen S as an in-memory database based on the serialization contained
  9821   9847   ** in P.  The serialized database P is N bytes in size.  M is the size of
  9822   9848   ** the buffer P, which might be larger than N.  If M is larger than N, and
  9823   9849   ** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
  9824   9850   ** permitted to add content to the in-memory database as long as the total
  9825   9851   ** size does not exceed M bytes.
  9826   9852   **
................................................................................
  9859   9885   ** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
  9860   9886   ** in the P argument is held in memory obtained from [sqlite3_malloc64()]
  9861   9887   ** and that SQLite should take ownership of this memory and automatically
  9862   9888   ** free it when it has finished using it.  Without this flag, the caller
  9863   9889   ** is resposible for freeing any dynamically allocated memory.
  9864   9890   **
  9865   9891   ** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
  9866         -** grow the size of the database usign calls to [sqlite3_realloc64()].  This
         9892  +** grow the size of the database using calls to [sqlite3_realloc64()].  This
  9867   9893   ** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
  9868   9894   ** Without this flag, the deserialized database cannot increase in size beyond
  9869   9895   ** the number of bytes specified by the M parameter.
  9870   9896   **
  9871   9897   ** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
  9872   9898   ** should be treated as read-only.
  9873   9899   */
................................................................................
 10953  10979   ** DESTRUCTOR: sqlite3_changegroup
 10954  10980   */
 10955  10981   SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
 10956  10982   
 10957  10983   /*
 10958  10984   ** CAPI3REF: Apply A Changeset To A Database
 10959  10985   **
 10960         -** Apply a changeset to a database. This function attempts to update the
 10961         -** "main" database attached to handle db with the changes found in the
 10962         -** 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. 
 10963  10989   **
 10964         -** The fourth argument (xFilter) passed to this function is the "filter
        10990  +** The fourth argument (xFilter) passed to these functions is the "filter
 10965  10991   ** callback". If it is not NULL, then for each table affected by at least one
 10966  10992   ** change in the changeset, the filter callback is invoked with
 10967  10993   ** the table name as the second argument, and a copy of the context pointer
 10968         -** passed as the sixth argument to this function as the first. If the "filter
 10969         -** callback" returns zero, then no attempt is made to apply any changes to 
 10970         -** the table. Otherwise, if the return value is non-zero or the xFilter
 10971         -** argument to this function is NULL, all changes related to the table are
 10972         -** 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.
 10973  10998   **
 10974  10999   ** For each table that is not excluded by the filter callback, this function 
 10975  11000   ** tests that the target database contains a compatible table. A table is 
 10976  11001   ** considered compatible if all of the following are true:
 10977  11002   **
 10978  11003   ** <ul>
 10979  11004   **   <li> The table has the same name as the name recorded in the 
................................................................................
 11010  11035   ** actions are taken by sqlite3changeset_apply() depending on the value
 11011  11036   ** returned by each invocation of the conflict-handler function. Refer to
 11012  11037   ** the documentation for the three 
 11013  11038   ** [SQLITE_CHANGESET_OMIT|available return values] for details.
 11014  11039   **
 11015  11040   ** <dl>
 11016  11041   ** <dt>DELETE Changes<dd>
 11017         -**   For each DELETE change, this function checks if the target database 
        11042  +**   For each DELETE change, the function checks if the target database 
 11018  11043   **   contains a row with the same primary key value (or values) as the 
 11019  11044   **   original row values stored in the changeset. If it does, and the values 
 11020  11045   **   stored in all non-primary key columns also match the values stored in 
 11021  11046   **   the changeset the row is deleted from the target database.
 11022  11047   **
 11023  11048   **   If a row with matching primary key values is found, but one or more of
 11024  11049   **   the non-primary key fields contains a value different from the original
................................................................................
 11055  11080   **   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 
 11056  11081   **   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
 11057  11082   **   This includes the case where the INSERT operation is re-attempted because 
 11058  11083   **   an earlier call to the conflict handler function returned 
 11059  11084   **   [SQLITE_CHANGESET_REPLACE].
 11060  11085   **
 11061  11086   ** <dt>UPDATE Changes<dd>
 11062         -**   For each UPDATE change, this function checks if the target database 
        11087  +**   For each UPDATE change, the function checks if the target database 
 11063  11088   **   contains a row with the same primary key value (or values) as the 
 11064  11089   **   original row values stored in the changeset. If it does, and the values 
 11065  11090   **   stored in all modified non-primary key columns also match the values
 11066  11091   **   stored in the changeset the row is updated within the target database.
 11067  11092   **
 11068  11093   **   If a row with matching primary key values is found, but one or more of
 11069  11094   **   the modified non-primary key fields contains a value different from an
................................................................................
 11086  11111   ** </dl>
 11087  11112   **
 11088  11113   ** It is safe to execute SQL statements, including those that write to the
 11089  11114   ** table that the callback related to, from within the xConflict callback.
 11090  11115   ** This can be used to further customize the applications conflict
 11091  11116   ** resolution strategy.
 11092  11117   **
 11093         -** 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.
 11094  11119   ** If any other error (aside from a constraint failure when attempting to
 11095  11120   ** write to the target database) occurs, then the savepoint transaction is
 11096  11121   ** rolled back, restoring the target database to its original state, and an 
 11097  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.
 11098  11133   */
 11099  11134   SQLITE_API int sqlite3changeset_apply(
 11100  11135     sqlite3 *db,                    /* Apply change to "main" db of this handle */
 11101  11136     int nChangeset,                 /* Size of changeset in bytes */
 11102  11137     void *pChangeset,               /* Changeset blob */
 11103  11138     int(*xFilter)(
 11104  11139       void *pCtx,                   /* Copy of sixth arg to _apply() */
................................................................................
 11106  11141     ),
 11107  11142     int(*xConflict)(
 11108  11143       void *pCtx,                   /* Copy of sixth arg to _apply() */
 11109  11144       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11110  11145       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11111  11146     ),
 11112  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
 11113  11164   );
 11114  11165   
 11115  11166   /* 
 11116  11167   ** CAPI3REF: Constants Passed To The Conflict Handler
 11117  11168   **
 11118  11169   ** Values that may be passed as the second argument to a conflict-handler.
 11119  11170   **
................................................................................
 11204  11255   **   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
 11205  11256   ** </dl>
 11206  11257   */
 11207  11258   #define SQLITE_CHANGESET_OMIT       0
 11208  11259   #define SQLITE_CHANGESET_REPLACE    1
 11209  11260   #define SQLITE_CHANGESET_ABORT      2
 11210  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  +
 11211  11417   /*
 11212  11418   ** CAPI3REF: Streaming Versions of API functions.
 11213  11419   **
 11214  11420   ** The six streaming API xxx_strm() functions serve similar purposes to the 
 11215  11421   ** corresponding non-streaming API functions:
 11216  11422   **
 11217  11423   ** <table border=1 style="margin-left:8ex;margin-right:8ex">
................................................................................
 11307  11513     ),
 11308  11514     int(*xConflict)(
 11309  11515       void *pCtx,                   /* Copy of sixth arg to _apply() */
 11310  11516       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11311  11517       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11312  11518     ),
 11313  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
 11314  11536   );
 11315  11537   SQLITE_API int sqlite3changeset_concat_strm(
 11316  11538     int (*xInputA)(void *pIn, void *pData, int *pnData),
 11317  11539     void *pInA,
 11318  11540     int (*xInputB)(void *pIn, void *pData, int *pnData),
 11319  11541     void *pInB,
 11320  11542     int (*xOutput)(void *pOut, const void *pData, int nData),
................................................................................
 11344  11566   SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
 11345  11567       int (*xInput)(void *pIn, void *pData, int *pnData),
 11346  11568       void *pIn
 11347  11569   );
 11348  11570   SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
 11349  11571       int (*xOutput)(void *pOut, const void *pData, int nData), 
 11350  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
 11351  11580   );
 11352  11581   
 11353  11582   
 11354  11583   /*
 11355  11584   ** Make sure we can call this stuff from C++.
 11356  11585   */
 11357  11586   #if 0
................................................................................
 13224  13453   ** The sqlite.busyHandler member of the sqlite struct contains the busy
 13225  13454   ** callback for the database handle. Each pager opened via the sqlite
 13226  13455   ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
 13227  13456   ** callback is currently invoked only from within pager.c.
 13228  13457   */
 13229  13458   typedef struct BusyHandler BusyHandler;
 13230  13459   struct BusyHandler {
 13231         -  int (*xFunc)(void *,int);  /* The busy callback */
 13232         -  void *pArg;                /* First arg to busy callback */
 13233         -  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 */
 13234  13464   };
 13235  13465   
 13236  13466   /*
 13237  13467   ** Name of the master database table.  The master database table
 13238  13468   ** is a special table that holds the names and attributes of all
 13239  13469   ** user tables and indices.
 13240  13470   */
................................................................................
 14435  14665     int,
 14436  14666     void(*)(DbPage*)
 14437  14667   );
 14438  14668   SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
 14439  14669   SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
 14440  14670   
 14441  14671   /* Functions used to configure a Pager object. */
 14442         -SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
        14672  +SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
 14443  14673   SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
 14444  14674   #ifdef SQLITE_HAS_CODEC
 14445  14675   SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*);
 14446  14676   #endif
 14447  14677   SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
 14448  14678   SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
 14449  14679   SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
................................................................................
 14521  14751   SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
 14522  14752   SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
 14523  14753   SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
 14524  14754   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
 14525  14755   SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
 14526  14756   SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
 14527  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
 14528  14763   
 14529  14764   /* Functions used to truncate the database file. */
 14530  14765   SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
 14531  14766   
 14532  14767   SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
 14533  14768   
 14534  14769   #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
................................................................................
 15507  15742   #define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
 15508  15743   #define SQLITE_Transitive     0x0080   /* Transitive constraints */
 15509  15744   #define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
 15510  15745   #define SQLITE_CountOfView    0x0200   /* The count-of-view optimization */
 15511  15746   #define SQLITE_CursorHints    0x0400   /* Add OP_CursorHint opcodes */
 15512  15747   #define SQLITE_Stat34         0x0800   /* Use STAT3 or STAT4 data */
 15513  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 */
 15514  15751   #define SQLITE_AllOpts        0xffff   /* All optimizations */
 15515  15752   
 15516  15753   /*
 15517  15754   ** Macros for testing whether or not optimizations are enabled or disabled.
 15518  15755   */
 15519  15756   #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
 15520  15757   #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
................................................................................
 16968  17205     yDbMask writeMask;   /* Start a write transaction on these databases */
 16969  17206     yDbMask cookieMask;  /* Bitmask of schema verified databases */
 16970  17207     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
 16971  17208     int regRoot;         /* Register holding root page number for new objects */
 16972  17209     int nMaxArg;         /* Max args passed to user function by sub-program */
 16973  17210   #if SELECTTRACE_ENABLED
 16974  17211     int nSelect;         /* Number of SELECT statements seen */
 16975         -  int nSelectIndent;   /* How far to indent SELECTTRACE() output */
 16976  17212   #endif
 16977  17213   #ifndef SQLITE_OMIT_SHARED_CACHE
 16978  17214     int nTableLock;        /* Number of locks in aTableLock */
 16979  17215     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 16980  17216   #endif
 16981  17217     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 16982  17218     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
................................................................................
 17332  17568       int n;                                    /* A counter */
 17333  17569       int iCur;                                 /* A cursor number */
 17334  17570       SrcList *pSrcList;                        /* FROM clause */
 17335  17571       struct SrcCount *pSrcCount;               /* Counting column references */
 17336  17572       struct CCurHint *pCCurHint;               /* Used by codeCursorHint() */
 17337  17573       int *aiCol;                               /* array of column indexes */
 17338  17574       struct IdxCover *pIdxCover;               /* Check for index coverage */
 17339         -    struct IdxExprTrans *pIdxTrans;           /* Convert indexed expr to column */
        17575  +    struct IdxExprTrans *pIdxTrans;           /* Convert idxed expr to column */
 17340  17576       ExprList *pGroupBy;                       /* GROUP BY clause */
 17341         -    struct HavingToWhereCtx *pHavingCtx;      /* HAVING to WHERE clause ctx */
        17577  +    Select *pSelect;                          /* HAVING to WHERE clause ctx */
 17342  17578     } u;
 17343  17579   };
 17344  17580   
 17345  17581   /* Forward declarations */
 17346  17582   SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
 17347  17583   SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
 17348  17584   SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
................................................................................
 17798  18034   SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*);
 17799  18035   SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int);
 17800  18036   SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
 17801  18037   SQLITE_PRIVATE int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
 17802  18038   SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*, Expr*, int);
 17803  18039   SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
 17804  18040   SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
        18041  +SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int);
 17805  18042   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 17806  18043   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 17807  18044   SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
 17808  18045   SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
 17809  18046   SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
 17810  18047   #ifndef SQLITE_UNTESTABLE
 17811  18048   SQLITE_PRIVATE void sqlite3PrngSaveState(void);
................................................................................
 18076  18313   SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
 18077  18314   SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
 18078  18315   SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
 18079  18316   SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
 18080  18317   SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
 18081  18318   SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
 18082  18319   SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
 18083         -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
        18320  +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
 18084  18321   SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
 18085  18322   SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
 18086  18323   SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
 18087  18324   SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
 18088  18325   SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
 18089  18326   SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
 18090  18327   SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
................................................................................
 19623  19860       }
 19624  19861   
 19625  19862       /*
 19626  19863       ** Set *pCurrent to the total cache hits or misses encountered by all
 19627  19864       ** pagers the database handle is connected to. *pHighwater is always set 
 19628  19865       ** to zero.
 19629  19866       */
        19867  +    case SQLITE_DBSTATUS_CACHE_SPILL:
        19868  +      op = SQLITE_DBSTATUS_CACHE_WRITE+1;
        19869  +      /* Fall through into the next case */
 19630  19870       case SQLITE_DBSTATUS_CACHE_HIT:
 19631  19871       case SQLITE_DBSTATUS_CACHE_MISS:
 19632  19872       case SQLITE_DBSTATUS_CACHE_WRITE:{
 19633  19873         int i;
 19634  19874         int nRet = 0;
 19635  19875         assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
 19636  19876         assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
................................................................................
 21044  21284   ** when simply tossing information over the wall to the VFS and we do not
 21045  21285   ** really care if the VFS receives and understands the information since it
 21046  21286   ** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
 21047  21287   ** routine has no return value since the return value would be meaningless.
 21048  21288   */
 21049  21289   SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
 21050  21290   #ifdef SQLITE_TEST
 21051         -  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
        21291  +  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
        21292  +   && op!=SQLITE_FCNTL_LOCK_TIMEOUT
        21293  +  ){
 21052  21294       /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
 21053  21295       ** is using a regular VFS, it is called after the corresponding
 21054  21296       ** transaction has been committed. Injecting a fault at this point
 21055  21297       ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
 21056  21298       ** but the transaction is committed anyway.
 21057  21299       **
 21058  21300       ** The core must call OsFileControl() though, not OsFileControlHint(),
 21059  21301       ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
 21060  21302       ** means the commit really has failed and an error should be returned
 21061  21303       ** to the user.  */
 21062  21304       DO_OS_MALLOC_TEST(id);
 21063  21305     }
 21064  21306   #endif
        21307  +  if( id->pMethods==0 ) return SQLITE_NOTFOUND;
 21065  21308     return id->pMethods->xFileControl(id, op, pArg);
 21066  21309   }
 21067  21310   SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
 21068         -  (void)id->pMethods->xFileControl(id, op, pArg);
        21311  +  if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
 21069  21312   }
 21070  21313   
 21071  21314   SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
 21072  21315     int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
 21073  21316     return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
 21074  21317   }
 21075  21318   SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
................................................................................
 27301  27544     pView = sqlite3TreeViewPush(pView, moreToFollow);
 27302  27545     if( p->pWith ){
 27303  27546       sqlite3TreeViewWith(pView, p->pWith, 1);
 27304  27547       cnt = 1;
 27305  27548       sqlite3TreeViewPush(pView, 1);
 27306  27549     }
 27307  27550     do{
        27551  +#if SELECTTRACE_ENABLED
        27552  +    sqlite3TreeViewLine(pView,
        27553  +      "SELECT%s%s (%s/%p) selFlags=0x%x nSelectRow=%d",
        27554  +      ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
        27555  +      ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
        27556  +      p->zSelName, p, p->selFlags,
        27557  +      (int)p->nSelectRow
        27558  +    );
        27559  +#else
 27308  27560       sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
 27309  27561         ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
 27310  27562         ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags,
 27311  27563         (int)p->nSelectRow
 27312  27564       );
        27565  +#endif
 27313  27566       if( cnt++ ) sqlite3TreeViewPop(pView);
 27314  27567       if( p->pPrior ){
 27315  27568         n = 1000;
 27316  27569       }else{
 27317  27570         n = 0;
 27318  27571         if( p->pSrc && p->pSrc->nSrc ) n++;
 27319  27572         if( p->pWhere ) n++;
................................................................................
 30990  31243     int deviceCharacteristics;          /* Precomputed device characteristics */
 30991  31244   #if SQLITE_ENABLE_LOCKING_STYLE
 30992  31245     int openFlags;                      /* The flags specified at open() */
 30993  31246   #endif
 30994  31247   #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
 30995  31248     unsigned fsFlags;                   /* cached details from statfs() */
 30996  31249   #endif
        31250  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        31251  +  unsigned iBusyTimeout;              /* Wait this many millisec on locks */
        31252  +#endif
 30997  31253   #if OS_VXWORKS
 30998  31254     struct vxworksFileId *pId;          /* Unique file ID */
 30999  31255   #endif
 31000  31256   #ifdef SQLITE_DEBUG
 31001  31257     /* The next group of variables are used to track whether or not the
 31002  31258     ** transaction counter in bytes 24-27 of database files are updated
 31003  31259     ** whenever any part of the database changes.  An assertion fault will
................................................................................
 32423  32679     
 32424  32680     unixLeaveMutex();
 32425  32681     OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
 32426  32682   
 32427  32683     *pResOut = reserved;
 32428  32684     return rc;
 32429  32685   }
        32686  +
        32687  +/*
        32688  +** Set a posix-advisory-lock.
        32689  +**
        32690  +** There are two versions of this routine.  If compiled with
        32691  +** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
        32692  +** which is a pointer to a unixFile.  If the unixFile->iBusyTimeout
        32693  +** value is set, then it is the number of milliseconds to wait before
        32694  +** failing the lock.  The iBusyTimeout value is always reset back to
        32695  +** zero on each call.
        32696  +**
        32697  +** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
        32698  +** attempt to set the lock.
        32699  +*/
        32700  +#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
        32701  +# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
        32702  +#else
        32703  +static int osSetPosixAdvisoryLock(
        32704  +  int h,                /* The file descriptor on which to take the lock */
        32705  +  struct flock *pLock,  /* The description of the lock */
        32706  +  unixFile *pFile       /* Structure holding timeout value */
        32707  +){
        32708  +  int rc = osFcntl(h,F_SETLK,pLock);
        32709  +  while( rc<0 && pFile->iBusyTimeout>0 ){
        32710  +    /* On systems that support some kind of blocking file lock with a timeout,
        32711  +    ** make appropriate changes here to invoke that blocking file lock.  On
        32712  +    ** generic posix, however, there is no such API.  So we simply try the
        32713  +    ** lock once every millisecond until either the timeout expires, or until
        32714  +    ** the lock is obtained. */
        32715  +    usleep(1000);
        32716  +    rc = osFcntl(h,F_SETLK,pLock);
        32717  +    pFile->iBusyTimeout--;
        32718  +  }
        32719  +  return rc;
        32720  +}
        32721  +#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
        32722  +
 32430  32723   
 32431  32724   /*
 32432  32725   ** Attempt to set a system-lock on the file pFile.  The lock is 
 32433  32726   ** described by pLock.
 32434  32727   **
 32435  32728   ** If the pFile was opened read/write from unix-excl, then the only lock
 32436  32729   ** ever obtained is an exclusive lock, and it is obtained exactly once
................................................................................
 32456  32749       if( pInode->bProcessLock==0 ){
 32457  32750         struct flock lock;
 32458  32751         assert( pInode->nLock==0 );
 32459  32752         lock.l_whence = SEEK_SET;
 32460  32753         lock.l_start = SHARED_FIRST;
 32461  32754         lock.l_len = SHARED_SIZE;
 32462  32755         lock.l_type = F_WRLCK;
 32463         -      rc = osFcntl(pFile->h, F_SETLK, &lock);
        32756  +      rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
 32464  32757         if( rc<0 ) return rc;
 32465  32758         pInode->bProcessLock = 1;
 32466  32759         pInode->nLock++;
 32467  32760       }else{
 32468  32761         rc = 0;
 32469  32762       }
 32470  32763     }else{
 32471         -    rc = osFcntl(pFile->h, F_SETLK, pLock);
        32764  +    rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
 32472  32765     }
 32473  32766     return rc;
 32474  32767   }
 32475  32768   
 32476  32769   /*
 32477  32770   ** Lock the file with the lock specified by parameter eFileLock - one
 32478  32771   ** of the following:
................................................................................
 34824  35117         }
 34825  35118         return SQLITE_OK;
 34826  35119       }
 34827  35120       case SQLITE_FCNTL_HAS_MOVED: {
 34828  35121         *(int*)pArg = fileHasMoved(pFile);
 34829  35122         return SQLITE_OK;
 34830  35123       }
        35124  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        35125  +    case SQLITE_FCNTL_LOCK_TIMEOUT: {
        35126  +      pFile->iBusyTimeout = *(int*)pArg;
        35127  +      return SQLITE_OK;
        35128  +    }
        35129  +#endif
 34831  35130   #if SQLITE_MAX_MMAP_SIZE>0
 34832  35131       case SQLITE_FCNTL_MMAP_SIZE: {
 34833  35132         i64 newLimit = *(i64*)pArg;
 34834  35133         int rc = SQLITE_OK;
 34835  35134         if( newLimit>sqlite3GlobalConfig.mxMmap ){
 34836  35135           newLimit = sqlite3GlobalConfig.mxMmap;
 34837  35136         }
................................................................................
 35143  35442   
 35144  35443     if( pShmNode->h>=0 ){
 35145  35444       /* Initialize the locking parameters */
 35146  35445       f.l_type = lockType;
 35147  35446       f.l_whence = SEEK_SET;
 35148  35447       f.l_start = ofst;
 35149  35448       f.l_len = n;
 35150         -    rc = osFcntl(pShmNode->h, F_SETLK, &f);
        35449  +    rc = osSetPosixAdvisoryLock(pShmNode->h, &f, pFile);
 35151  35450       rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 35152  35451     }
 35153  35452   
 35154  35453     /* Update the global lock state and do debug tracing */
 35155  35454   #ifdef SQLITE_DEBUG
 35156  35455     { u16 mask;
 35157  35456     OSTRACE(("SHM-LOCK "));
................................................................................
 45003  45302   **
 45004  45303   **    May you do good and not evil.
 45005  45304   **    May you find forgiveness for yourself and forgive others.
 45006  45305   **    May you share freely, never taking more than you give.
 45007  45306   **
 45008  45307   ******************************************************************************
 45009  45308   **
 45010         -** This file implements in-memory VFS.  A database is held as a contiguous
        45309  +** This file implements an in-memory VFS. A database is held as a contiguous
 45011  45310   ** block of memory.
 45012  45311   **
 45013  45312   ** This file also implements interface sqlite3_serialize() and
 45014  45313   ** sqlite3_deserialize().
 45015  45314   */
 45016  45315   #ifdef SQLITE_ENABLE_DESERIALIZE
 45017  45316   /* #include "sqliteInt.h" */
................................................................................
 45442  45741     Btree *pBt;
 45443  45742     sqlite3_int64 sz;
 45444  45743     int szPage = 0;
 45445  45744     sqlite3_stmt *pStmt = 0;
 45446  45745     unsigned char *pOut;
 45447  45746     char *zSql;
 45448  45747     int rc;
        45748  +
        45749  +#ifdef SQLITE_ENABLE_API_ARMOR
        45750  +  if( !sqlite3SafetyCheckOk(db) ){
        45751  +    (void)SQLITE_MISUSE_BKPT;
        45752  +    return 0;
        45753  +  }
        45754  +#endif
 45449  45755   
 45450  45756     if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
 45451  45757     p = memdbFromDbSchema(db, zSchema);
 45452  45758     iDb = sqlite3FindDbName(db, zSchema);
 45453  45759     if( piSize ) *piSize = -1;
 45454  45760     if( iDb<0 ) return 0;
 45455  45761     if( p ){
................................................................................
 45512  45818     unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
 45513  45819   ){
 45514  45820     MemFile *p;
 45515  45821     char *zSql;
 45516  45822     sqlite3_stmt *pStmt = 0;
 45517  45823     int rc;
 45518  45824     int iDb;
        45825  +
        45826  +#ifdef SQLITE_ENABLE_API_ARMOR
        45827  +  if( !sqlite3SafetyCheckOk(db) ){
        45828  +    return SQLITE_MISUSE_BKPT;
        45829  +  }
        45830  +  if( szDb<0 ) return SQLITE_MISUSE_BKPT;
        45831  +  if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
        45832  +#endif
 45519  45833   
 45520  45834     sqlite3_mutex_enter(db->mutex);
 45521  45835     if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
 45522  45836     iDb = sqlite3FindDbName(db, zSchema);
 45523  45837     if( iDb<0 ){
 45524  45838       rc = SQLITE_ERROR;
 45525  45839       goto end_deserialize;
................................................................................
 46415  46729       }
 46416  46730       if( pPg ){
 46417  46731         int rc;
 46418  46732   #ifdef SQLITE_LOG_CACHE_SPILL
 46419  46733         sqlite3_log(SQLITE_FULL, 
 46420  46734                     "spill page %d making room for %d - cache used: %d/%d",
 46421  46735                     pPg->pgno, pgno,
 46422         -                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
        46736  +                  sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
 46423  46737                   numberOfCachePages(pCache));
 46424  46738   #endif
 46425  46739         pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
 46426  46740         rc = pCache->xStress(pCache->pStress, pPg);
 46427  46741         pcacheDump(pCache);
 46428  46742         if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
 46429  46743           return rc;
................................................................................
 49471  49785     int pageSize;               /* Number of bytes in a page */
 49472  49786     Pgno mxPgno;                /* Maximum allowed size of the database */
 49473  49787     i64 journalSizeLimit;       /* Size limit for persistent journal files */
 49474  49788     char *zFilename;            /* Name of the database file */
 49475  49789     char *zJournal;             /* Name of the journal file */
 49476  49790     int (*xBusyHandler)(void*); /* Function to call when busy */
 49477  49791     void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
 49478         -  int aStat[3];               /* Total cache hits, misses and writes */
        49792  +  int aStat[4];               /* Total cache hits, misses, writes, spills */
 49479  49793   #ifdef SQLITE_TEST
 49480  49794     int nRead;                  /* Database pages read */
 49481  49795   #endif
 49482  49796     void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
 49483  49797     int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
 49484  49798   #ifdef SQLITE_HAS_CODEC
 49485  49799     void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
................................................................................
 49499  49813   ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
 49500  49814   ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS 
 49501  49815   ** or CACHE_WRITE to sqlite3_db_status().
 49502  49816   */
 49503  49817   #define PAGER_STAT_HIT   0
 49504  49818   #define PAGER_STAT_MISS  1
 49505  49819   #define PAGER_STAT_WRITE 2
        49820  +#define PAGER_STAT_SPILL 3
 49506  49821   
 49507  49822   /*
 49508  49823   ** The following global variables hold counters used for
 49509  49824   ** testing purposes only.  These variables do not exist in
 49510  49825   ** a non-testing build.  These variables are not thread-safe.
 49511  49826   */
 49512  49827   #ifdef SQLITE_TEST
................................................................................
 50901  51216       ** successfully committed, but the EXCLUSIVE lock is still held on the
 50902  51217       ** file. So it is safe to truncate the database file to its minimum
 50903  51218       ** required size.  */
 50904  51219       assert( pPager->eLock==EXCLUSIVE_LOCK );
 50905  51220       rc = pager_truncate(pPager, pPager->dbSize);
 50906  51221     }
 50907  51222   
 50908         -  if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
        51223  +  if( rc==SQLITE_OK && bCommit ){
 50909  51224       rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
 50910  51225       if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 50911  51226     }
 50912  51227   
 50913  51228     if( !pPager->exclusiveMode 
 50914  51229      && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
 50915  51230     ){
................................................................................
 51720  52035     }
 51721  52036     /* Following a rollback, the database file should be back in its original
 51722  52037     ** state prior to the start of the transaction, so invoke the
 51723  52038     ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
 51724  52039     ** assertion that the transaction counter was modified.
 51725  52040     */
 51726  52041   #ifdef SQLITE_DEBUG
 51727         -  if( pPager->fd->pMethods ){
 51728         -    sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
 51729         -  }
        52042  +  sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
 51730  52043   #endif
 51731  52044   
 51732  52045     /* If this playback is happening automatically as a result of an IO or 
 51733  52046     ** malloc error that occurred after the change-counter was updated but 
 51734  52047     ** before the transaction was committed, then the change-counter 
 51735  52048     ** modification may just have been reverted. If this happens in exclusive 
 51736  52049     ** mode, then subsequent transactions performed by the connection will not
................................................................................
 52475  52788   **   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
 52476  52789   **   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
 52477  52790   **
 52478  52791   ** If the busy-handler callback returns non-zero, the lock is 
 52479  52792   ** retried. If it returns zero, then the SQLITE_BUSY error is
 52480  52793   ** returned to the caller of the pager API function.
 52481  52794   */
 52482         -SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
        52795  +SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
 52483  52796     Pager *pPager,                       /* Pager object */
 52484  52797     int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
 52485  52798     void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
 52486  52799   ){
        52800  +  void **ap;
 52487  52801     pPager->xBusyHandler = xBusyHandler;
 52488  52802     pPager->pBusyHandlerArg = pBusyHandlerArg;
 52489         -
 52490         -  if( isOpen(pPager->fd) ){
 52491         -    void **ap = (void **)&pPager->xBusyHandler;
 52492         -    assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
 52493         -    assert( ap[1]==pBusyHandlerArg );
 52494         -    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
 52495         -  }
        52803  +  ap = (void **)&pPager->xBusyHandler;
        52804  +  assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
        52805  +  assert( ap[1]==pBusyHandlerArg );
        52806  +  sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
 52496  52807   }
 52497  52808   
 52498  52809   /*
 52499  52810   ** Change the page size used by the Pager object. The new page size 
 52500  52811   ** is passed in *pPageSize.
 52501  52812   **
 52502  52813   ** If the pager is in the error state when this function is called, it
................................................................................
 53390  53701     if( pPager->doNotSpill
 53391  53702      && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
 53392  53703         || (pPg->flags & PGHDR_NEED_SYNC)!=0)
 53393  53704     ){
 53394  53705       return SQLITE_OK;
 53395  53706     }
 53396  53707   
        53708  +  pPager->aStat[PAGER_STAT_SPILL]++;
 53397  53709     pPg->pDirty = 0;
 53398  53710     if( pagerUseWal(pPager) ){
 53399  53711       /* Write a single frame for this page to the log. */
 53400  53712       rc = subjournalPageIfRequired(pPg); 
 53401  53713       if( rc==SQLITE_OK ){
 53402  53714         rc = pagerWalFrames(pPager, pPg, 0, 0);
 53403  53715       }
................................................................................
 54463  54775   }
 54464  54776   SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
 54465  54777     Pager *pPager;
 54466  54778     assert( pPg!=0 );
 54467  54779     assert( pPg->pgno==1 );
 54468  54780     assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
 54469  54781     pPager = pPg->pPager;
        54782  +  sqlite3PagerResetLockTimeout(pPager);
 54470  54783     sqlite3PcacheRelease(pPg);
 54471  54784     pagerUnlockIfUnused(pPager);
 54472  54785   }
 54473  54786   
 54474  54787   /*
 54475  54788   ** This function is called at the start of every write transaction.
 54476  54789   ** There must already be a RESERVED or EXCLUSIVE lock on the database 
................................................................................
 55058  55371   ** or pages with the Pager.noSync flag set.
 55059  55372   **
 55060  55373   ** If successful, or if called on a pager for which it is a no-op, this
 55061  55374   ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
 55062  55375   */
 55063  55376   SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
 55064  55377     int rc = SQLITE_OK;
 55065         -
 55066         -  if( isOpen(pPager->fd) ){
 55067         -    void *pArg = (void*)zMaster;
 55068         -    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
 55069         -    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 55070         -  }
        55378  +  void *pArg = (void*)zMaster;
        55379  +  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
        55380  +  if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 55071  55381     if( rc==SQLITE_OK && !pPager->noSync ){
 55072  55382       assert( !MEMDB );
 55073  55383       rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
 55074  55384     }
 55075  55385     return rc;
 55076  55386   }
 55077  55387   
................................................................................
 55510  55820     a[9] = pPager->nRead;
 55511  55821     a[10] = pPager->aStat[PAGER_STAT_WRITE];
 55512  55822     return a;
 55513  55823   }
 55514  55824   #endif
 55515  55825   
 55516  55826   /*
 55517         -** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
 55518         -** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
        55827  +** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
        55828  +** or _WRITE+1.  The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
        55829  +** of SQLITE_DBSTATUS_CACHE_SPILL.  The _SPILL case is not contiguous because
        55830  +** it was added later.
        55831  +**
        55832  +** Before returning, *pnVal is incremented by the
 55519  55833   ** current cache hit or miss count, according to the value of eStat. If the 
 55520  55834   ** reset parameter is non-zero, the cache hit or miss count is zeroed before 
 55521  55835   ** returning.
 55522  55836   */
 55523  55837   SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
 55524  55838   
 55525  55839     assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
 55526  55840          || eStat==SQLITE_DBSTATUS_CACHE_MISS
 55527  55841          || eStat==SQLITE_DBSTATUS_CACHE_WRITE
        55842  +       || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
 55528  55843     );
 55529  55844   
 55530  55845     assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
 55531  55846     assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
 55532         -  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
        55847  +  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
        55848  +           && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
 55533  55849   
 55534         -  *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
        55850  +  eStat -= SQLITE_DBSTATUS_CACHE_HIT;
        55851  +  *pnVal += pPager->aStat[eStat];
 55535  55852     if( reset ){
 55536         -    pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
        55853  +    pPager->aStat[eStat] = 0;
 55537  55854     }
 55538  55855   }
 55539  55856   
 55540  55857   /*
 55541  55858   ** Return true if this is an in-memory or temp-file backed pager.
 55542  55859   */
 55543  55860   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
................................................................................
 55732  56049   ** Return the file handle for the database file associated
 55733  56050   ** with the pager.  This might return NULL if the file has
 55734  56051   ** not yet been opened.
 55735  56052   */
 55736  56053   SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
 55737  56054     return pPager->fd;
 55738  56055   }
        56056  +
        56057  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        56058  +/*
        56059  +** Reset the lock timeout for pager.
        56060  +*/
        56061  +SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){
        56062  +  int x = 0;
        56063  +  sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x);
        56064  +}
        56065  +#endif
 55739  56066   
 55740  56067   /*
 55741  56068   ** Return the file handle for the journal file (if it exists).
 55742  56069   ** This will be either the rollback journal or the WAL file.
 55743  56070   */
 55744  56071   SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
 55745  56072   #if SQLITE_OMIT_WAL
................................................................................
 56193  56520     if( pPager->pWal ){
 56194  56521       rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
 56195  56522           (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
 56196  56523           pPager->pBusyHandlerArg,
 56197  56524           pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
 56198  56525           pnLog, pnCkpt
 56199  56526       );
        56527  +    sqlite3PagerResetLockTimeout(pPager);
 56200  56528     }
 56201  56529     return rc;
 56202  56530   }
 56203  56531   
 56204  56532   SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
 56205  56533     return sqlite3WalCallback(pPager->pWal);
 56206  56534   }
................................................................................
 63469  63797   /*
 63470  63798   ** Invoke the busy handler for a btree.
 63471  63799   */
 63472  63800   static int btreeInvokeBusyHandler(void *pArg){
 63473  63801     BtShared *pBt = (BtShared*)pArg;
 63474  63802     assert( pBt->db );
 63475  63803     assert( sqlite3_mutex_held(pBt->db->mutex) );
 63476         -  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
        63804  +  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
        63805  +                                  sqlite3PagerFile(pBt->pPager));
 63477  63806   }
 63478  63807   
 63479  63808   /*
 63480  63809   ** Open a database file.
 63481  63810   ** 
 63482  63811   ** zFilename is the name of the database file.  If zFilename is NULL
 63483  63812   ** then an ephemeral database is created.  The ephemeral database might
................................................................................
 63647  63976         rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
 63648  63977       }
 63649  63978       if( rc!=SQLITE_OK ){
 63650  63979         goto btree_open_out;
 63651  63980       }
 63652  63981       pBt->openFlags = (u8)flags;
 63653  63982       pBt->db = db;
 63654         -    sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
        63983  +    sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
 63655  63984       p->pBt = pBt;
 63656  63985     
 63657  63986       pBt->pCursor = 0;
 63658  63987       pBt->pPage1 = 0;
 63659  63988       if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
 63660  63989   #if defined(SQLITE_SECURE_DELETE)
 63661  63990       pBt->btsFlags |= BTS_SECURE_DELETE;
................................................................................
 64610  64939       }
 64611  64940     
 64612  64941       if( rc!=SQLITE_OK ){
 64613  64942         unlockBtreeIfUnused(pBt);
 64614  64943       }
 64615  64944     }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
 64616  64945             btreeInvokeBusyHandler(pBt) );
        64946  +  sqlite3PagerResetLockTimeout(pBt->pPager);
 64617  64947   
 64618  64948     if( rc==SQLITE_OK ){
 64619  64949       if( p->inTrans==TRANS_NONE ){
 64620  64950         pBt->nTransaction++;
 64621  64951   #ifndef SQLITE_OMIT_SHARED_CACHE
 64622  64952         if( p->sharable ){
 64623  64953           assert( p->lock.pBtree==p && p->lock.iTable==1 );
................................................................................
 80285  80615   /*
 80286  80616   ** Return the value of a status counter for a prepared statement
 80287  80617   */
 80288  80618   SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
 80289  80619     Vdbe *pVdbe = (Vdbe*)pStmt;
 80290  80620     u32 v;
 80291  80621   #ifdef SQLITE_ENABLE_API_ARMOR
 80292         -  if( !pStmt ){
        80622  +  if( !pStmt 
        80623  +   || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
        80624  +  ){
 80293  80625       (void)SQLITE_MISUSE_BKPT;
 80294  80626       return 0;
 80295  80627     }
 80296  80628   #endif
 80297  80629     if( op==SQLITE_STMTSTATUS_MEMUSED ){
 80298  80630       sqlite3 *db = pVdbe->db;
 80299  80631       sqlite3_mutex_enter(db->mutex);
................................................................................
 85171  85503     VdbeFrame *pFrame;     /* Root frame of VDBE */
 85172  85504   
 85173  85505     v = 0;
 85174  85506     res = 0;
 85175  85507     pOut = out2Prerelease(p, pOp);
 85176  85508     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 85177  85509     pC = p->apCsr[pOp->p1];
        85510  +  if( !pC->isTable ){
        85511  +    rc = SQLITE_CORRUPT_BKPT;
        85512  +    goto abort_due_to_error;
        85513  +  }
 85178  85514     assert( pC!=0 );
 85179  85515     assert( pC->eCurType==CURTYPE_BTREE );
 85180  85516     assert( pC->uc.pCursor!=0 );
 85181  85517     {
 85182  85518       /* The next rowid or record number (different terms for the same
 85183  85519       ** thing) is obtained in a two-step algorithm.
 85184  85520       **
................................................................................
 98642  98978     if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){
 98643  98979       Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft);
 98644  98980       testcase( pX!=pE1->pLeft );
 98645  98981       if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1;
 98646  98982     }
 98647  98983     return 0;
 98648  98984   }
        98985  +
        98986  +/*
        98987  +** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
        98988  +** If the expression node requires that the table at pWalker->iCur
        98989  +** have a non-NULL column, then set pWalker->eCode to 1 and abort.
        98990  +*/
        98991  +static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
        98992  +  /* This routine is only called for WHERE clause expressions and so it
        98993  +  ** cannot have any TK_AGG_COLUMN entries because those are only found
        98994  +  ** in HAVING clauses.  We can get a TK_AGG_FUNCTION in a WHERE clause,
        98995  +  ** but that is an illegal construct and the query will be rejected at
        98996  +  ** a later stage of processing, so the TK_AGG_FUNCTION case does not
        98997  +  ** need to be considered here. */
        98998  +  assert( pExpr->op!=TK_AGG_COLUMN );
        98999  +  testcase( pExpr->op==TK_AGG_FUNCTION );
        99000  +
        99001  +  if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
        99002  +  switch( pExpr->op ){
        99003  +    case TK_ISNULL:
        99004  +    case TK_IS:
        99005  +    case TK_OR:
        99006  +    case TK_CASE:
        99007  +    case TK_IN:
        99008  +    case TK_FUNCTION:
        99009  +      testcase( pExpr->op==TK_ISNULL );
        99010  +      testcase( pExpr->op==TK_IS );
        99011  +      testcase( pExpr->op==TK_OR );
        99012  +      testcase( pExpr->op==TK_CASE );
        99013  +      testcase( pExpr->op==TK_IN );
        99014  +      testcase( pExpr->op==TK_FUNCTION );
        99015  +      return WRC_Prune;
        99016  +    case TK_COLUMN:
        99017  +      if( pWalker->u.iCur==pExpr->iTable ){
        99018  +        pWalker->eCode = 1;
        99019  +        return WRC_Abort;
        99020  +      }
        99021  +      return WRC_Prune;
        99022  +    default:
        99023  +      return WRC_Continue;
        99024  +  }
        99025  +}
        99026  +
        99027  +/*
        99028  +** Return true (non-zero) if expression p can only be true if at least
        99029  +** one column of table iTab is non-null.  In other words, return true
        99030  +** if expression p will always be NULL or false if every column of iTab
        99031  +** is NULL.
        99032  +**
        99033  +** False negatives are acceptable.  In other words, it is ok to return
        99034  +** zero even if expression p will never be true of every column of iTab
        99035  +** is NULL.  A false negative is merely a missed optimization opportunity.
        99036  +**
        99037  +** False positives are not allowed, however.  A false positive may result
        99038  +** in an incorrect answer.
        99039  +**
        99040  +** Terms of p that are marked with EP_FromJoin (and hence that come from
        99041  +** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
        99042  +**
        99043  +** This routine is used to check if a LEFT JOIN can be converted into
        99044  +** an ordinary JOIN.  The p argument is the WHERE clause.  If the WHERE
        99045  +** clause requires that some column of the right table of the LEFT JOIN
        99046  +** be non-NULL, then the LEFT JOIN can be safely converted into an
        99047  +** ordinary join.
        99048  +*/
        99049  +SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
        99050  +  Walker w;
        99051  +  w.xExprCallback = impliesNotNullRow;
        99052  +  w.xSelectCallback = 0;
        99053  +  w.xSelectCallback2 = 0;
        99054  +  w.eCode = 0;
        99055  +  w.u.iCur = iTab;
        99056  +  sqlite3WalkExpr(&w, p);
        99057  +  return w.eCode;
        99058  +}
 98649  99059   
 98650  99060   /*
 98651  99061   ** An instance of the following structure is used by the tree walker
 98652  99062   ** to determine if an expression can be evaluated by reference to the
 98653  99063   ** index only, without having to do a search for the corresponding
 98654  99064   ** table entry.  The IdxCover.pIdx field is the index.  IdxCover.iCur
 98655  99065   ** is the cursor for the table.
................................................................................
100887 101297     if( v==0 || NEVER(pTab==0) ){
100888 101298       return;
100889 101299     }
100890 101300     if( pTab->tnum==0 ){
100891 101301       /* Do not gather statistics on views or virtual tables */
100892 101302       return;
100893 101303     }
100894         -  if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){
       101304  +  if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
100895 101305       /* Do not gather statistics on system tables */
100896 101306       return;
100897 101307     }
100898 101308     assert( sqlite3BtreeHoldsAllMutexes(db) );
100899 101309     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
100900 101310     assert( iDb>=0 );
100901 101311     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
................................................................................
103948 104358     if( p!=0 ){
103949 104359       pCol = &(p->aCol[p->nCol-1]);
103950 104360       if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
103951 104361         sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
103952 104362             pCol->zName);
103953 104363       }else{
103954 104364         /* A copy of pExpr is used instead of the original, as pExpr contains
103955         -      ** tokens that point to volatile memory.	
       104365  +      ** tokens that point to volatile memory.
103956 104366         */
103957 104367         Expr x;
103958 104368         sqlite3ExprDelete(db, pCol->pDflt);
103959 104369         memset(&x, 0, sizeof(x));
103960 104370         x.op = TK_SPAN;
103961 104371         x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
103962 104372         x.pLeft = pExpr;
................................................................................
104566 104976     if( pEnd==0 && pSelect==0 ){
104567 104977       return;
104568 104978     }
104569 104979     assert( !db->mallocFailed );
104570 104980     p = pParse->pNewTable;
104571 104981     if( p==0 ) return;
104572 104982   
104573         -  assert( !db->init.busy || !pSelect );
104574         -
104575 104983     /* If the db->init.busy is 1 it means we are reading the SQL off the
104576 104984     ** "sqlite_master" or "sqlite_temp_master" table on the disk.
104577 104985     ** So do not write to the disk again.  Extract the root page number
104578 104986     ** for the table from the db->init.newTnum field.  (The page number
104579 104987     ** should have been put there by the sqliteOpenCb routine.)
104580 104988     **
104581 104989     ** If the root page number is 1, that means this is the sqlite_master
104582 104990     ** table itself.  So mark it read-only.
104583 104991     */
104584 104992     if( db->init.busy ){
       104993  +    if( pSelect ){
       104994  +      sqlite3ErrorMsg(pParse, "");
       104995  +      return;
       104996  +    }
104585 104997       p->tnum = db->init.newTnum;
104586 104998       if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
104587 104999     }
104588 105000   
104589 105001     /* Special processing for WITHOUT ROWID Tables */
104590 105002     if( tabOpts & TF_WithoutRowid ){
104591 105003       if( (p->tabFlags & TF_Autoincrement) ){
................................................................................
104868 105280   */
104869 105281   SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
104870 105282     Table *pSelTab;   /* A fake table from which we get the result set */
104871 105283     Select *pSel;     /* Copy of the SELECT that implements the view */
104872 105284     int nErr = 0;     /* Number of errors encountered */
104873 105285     int n;            /* Temporarily holds the number of cursors assigned */
104874 105286     sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
104875         -#ifndef SQLITE_OMIT_VIRTUALTABLE	
       105287  +#ifndef SQLITE_OMIT_VIRTUALTABLE
104876 105288     int rc;
104877 105289   #endif
104878 105290   #ifndef SQLITE_OMIT_AUTHORIZATION
104879 105291     sqlite3_xauth xAuth;       /* Saved xAuth pointer */
104880 105292   #endif
104881 105293   
104882 105294     assert( pTable );
................................................................................
112154 112566   **
112155 112567   ** There is at most one AutoincInfo structure per table even if the
112156 112568   ** same table is autoincremented multiple times due to inserts within
112157 112569   ** triggers.  A new AutoincInfo structure is created if this is the
112158 112570   ** first use of table pTab.  On 2nd and subsequent uses, the original
112159 112571   ** AutoincInfo structure is used.
112160 112572   **
112161         -** Three memory locations are allocated:
       112573  +** Four consecutive registers are allocated:
112162 112574   **
112163         -**   (1)  Register to hold the name of the pTab table.
112164         -**   (2)  Register to hold the maximum ROWID of pTab.
112165         -**   (3)  Register to hold the rowid in sqlite_sequence of pTab
       112575  +**   (1)  The name of the pTab table.
       112576  +**   (2)  The maximum ROWID of pTab.
       112577  +**   (3)  The rowid in sqlite_sequence of pTab
       112578  +**   (4)  The original value of the max ROWID in pTab, or NULL if none
112166 112579   **
112167 112580   ** The 2nd register is the one that is returned.  That is all the
112168 112581   ** insert routine needs to know about.
112169 112582   */
112170 112583   static int autoIncBegin(
112171 112584     Parse *pParse,      /* Parsing context */
112172 112585     int iDb,            /* Index of the database holding pTab */
................................................................................
112186 112599         if( pInfo==0 ) return 0;
112187 112600         pInfo->pNext = pToplevel->pAinc;
112188 112601         pToplevel->pAinc = pInfo;
112189 112602         pInfo->pTab = pTab;
112190 112603         pInfo->iDb = iDb;
112191 112604         pToplevel->nMem++;                  /* Register to hold name of table */
112192 112605         pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
112193         -      pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
       112606  +      pToplevel->nMem +=2;       /* Rowid in sqlite_sequence + orig max val */
112194 112607       }
112195 112608       memId = pInfo->regCtr;
112196 112609     }
112197 112610     return memId;
112198 112611   }
112199 112612   
112200 112613   /*
................................................................................
112214 112627     assert( sqlite3IsToplevel(pParse) );
112215 112628   
112216 112629     assert( v );   /* We failed long ago if this is not so */
112217 112630     for(p = pParse->pAinc; p; p = p->pNext){
112218 112631       static const int iLn = VDBE_OFFSET_LINENO(2);
112219 112632       static const VdbeOpList autoInc[] = {
112220 112633         /* 0  */ {OP_Null,    0,  0, 0},
112221         -      /* 1  */ {OP_Rewind,  0,  9, 0},
       112634  +      /* 1  */ {OP_Rewind,  0, 10, 0},
112222 112635         /* 2  */ {OP_Column,  0,  0, 0},
112223         -      /* 3  */ {OP_Ne,      0,  7, 0},
       112636  +      /* 3  */ {OP_Ne,      0,  9, 0},
112224 112637         /* 4  */ {OP_Rowid,   0,  0, 0},
112225 112638         /* 5  */ {OP_Column,  0,  1, 0},
112226         -      /* 6  */ {OP_Goto,    0,  9, 0},
112227         -      /* 7  */ {OP_Next,    0,  2, 0},
112228         -      /* 8  */ {OP_Integer, 0,  0, 0},
112229         -      /* 9  */ {OP_Close,   0,  0, 0} 
       112639  +      /* 6  */ {OP_AddImm,  0,  0, 0},
       112640  +      /* 7  */ {OP_Copy,    0,  0, 0},
       112641  +      /* 8  */ {OP_Goto,    0, 11, 0},
       112642  +      /* 9  */ {OP_Next,    0,  2, 0},
       112643  +      /* 10 */ {OP_Integer, 0,  0, 0},
       112644  +      /* 11 */ {OP_Close,   0,  0, 0} 
112230 112645       };
112231 112646       VdbeOp *aOp;
112232 112647       pDb = &db->aDb[p->iDb];
112233 112648       memId = p->regCtr;
112234 112649       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
112235 112650       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
112236 112651       sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
112237 112652       aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
112238 112653       if( aOp==0 ) break;
112239 112654       aOp[0].p2 = memId;
112240         -    aOp[0].p3 = memId+1;
       112655  +    aOp[0].p3 = memId+2;
112241 112656       aOp[2].p3 = memId;
112242 112657       aOp[3].p1 = memId-1;
112243 112658       aOp[3].p3 = memId;
112244 112659       aOp[3].p5 = SQLITE_JUMPIFNULL;
112245 112660       aOp[4].p2 = memId+1;
112246 112661       aOp[5].p3 = memId;
112247         -    aOp[8].p2 = memId;
       112662  +    aOp[6].p1 = memId;
       112663  +    aOp[7].p2 = memId+2;
       112664  +    aOp[7].p1 = memId;
       112665  +    aOp[10].p2 = memId;
112248 112666     }
112249 112667   }
112250 112668   
112251 112669   /*
112252 112670   ** Update the maximum rowid for an autoincrement calculation.
112253 112671   **
112254 112672   ** This routine should be called when the regRowid register holds a
................................................................................
112287 112705       VdbeOp *aOp;
112288 112706       Db *pDb = &db->aDb[p->iDb];
112289 112707       int iRec;
112290 112708       int memId = p->regCtr;
112291 112709   
112292 112710       iRec = sqlite3GetTempReg(pParse);
112293 112711       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
       112712  +    sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
       112713  +    VdbeCoverage(v);
112294 112714       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
112295 112715       aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
112296 112716       if( aOp==0 ) break;
112297 112717       aOp[0].p1 = memId+1;
112298 112718       aOp[1].p2 = memId+1;
112299 112719       aOp[2].p1 = memId-1;
112300 112720       aOp[2].p3 = iRec;
................................................................................
118920 119340     const char *zExtra   /* Error information */
118921 119341   ){
118922 119342     sqlite3 *db = pData->db;
118923 119343     if( !db->mallocFailed && (db->flags & SQLITE_WriteSchema)==0 ){
118924 119344       char *z;
118925 119345       if( zObj==0 ) zObj = "?";
118926 119346       z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
118927         -    if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
       119347  +    if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
118928 119348       sqlite3DbFree(db, *pData->pzErrMsg);
118929 119349       *pData->pzErrMsg = z;
118930 119350     }
118931 119351     pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT;
118932 119352   }
118933 119353   
118934 119354   /*
................................................................................
119814 120234   /*
119815 120235   ** Trace output macros
119816 120236   */
119817 120237   #if SELECTTRACE_ENABLED
119818 120238   /***/ int sqlite3SelectTrace = 0;
119819 120239   # define SELECTTRACE(K,P,S,X)  \
119820 120240     if(sqlite3SelectTrace&(K))   \
119821         -    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
119822         -        (S)->zSelName,(S)),\
       120241  +    sqlite3DebugPrintf("%s/%p: ",(S)->zSelName,(S)),\
119823 120242       sqlite3DebugPrintf X
119824 120243   #else
119825 120244   # define SELECTTRACE(K,P,S,X)
119826 120245   #endif
119827 120246   
119828 120247   
119829 120248   /*
................................................................................
120175 120594           setJoinExpr(p->x.pList->a[i].pExpr, iTable);
120176 120595         }
120177 120596       }
120178 120597       setJoinExpr(p->pLeft, iTable);
120179 120598       p = p->pRight;
120180 120599     } 
120181 120600   }
       120601  +
       120602  +/* Undo the work of setJoinExpr().  In the expression tree p, convert every
       120603  +** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
       120604  +** an ordinary term that omits the EP_FromJoin mark.
       120605  +**
       120606  +** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
       120607  +*/
       120608  +static void unsetJoinExpr(Expr *p, int iTable){
       120609  +  while( p ){
       120610  +    if( ExprHasProperty(p, EP_FromJoin)
       120611  +     && (iTable<0 || p->iRightJoinTable==iTable) ){
       120612  +      ExprClearProperty(p, EP_FromJoin);
       120613  +    }
       120614  +    if( p->op==TK_FUNCTION && p->x.pList ){
       120615  +      int i;
       120616  +      for(i=0; i<p->x.pList->nExpr; i++){
       120617  +        unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
       120618  +      }
       120619  +    }
       120620  +    unsetJoinExpr(p->pLeft, iTable);
       120621  +    p = p->pRight;
       120622  +  } 
       120623  +}
120182 120624   
120183 120625   /*
120184 120626   ** This routine processes the join information for a SELECT statement.
120185 120627   ** ON and USING clauses are converted into extra terms of the WHERE clause.
120186 120628   ** NATURAL joins also create extra WHERE clause terms.
120187 120629   **
120188 120630   ** The terms of a FROM clause are contained in the Select.pSrc structure.
................................................................................
123544 123986         ** (the only way this can happen is if the compound sub-query is
123545 123987         ** currently part of pSub->pSrc). See ticket [d11a6e908f].  */
123546 123988         ExprList *pOrderBy = pSub->pOrderBy;
123547 123989         for(i=0; i<pOrderBy->nExpr; i++){
123548 123990           pOrderBy->a[i].u.x.iOrderByCol = 0;
123549 123991         }
123550 123992         assert( pParent->pOrderBy==0 );
123551         -      assert( pSub->pPrior==0 );
123552 123993         pParent->pOrderBy = pOrderBy;
123553 123994         pSub->pOrderBy = 0;
123554 123995       }
123555 123996       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
123556 123997       if( isLeftJoin>0 ){
123557 123998         setJoinExpr(pWhere, iNewParent);
123558 123999       }
................................................................................
123628 124069   **           to suppress it. **)
123629 124070   **
123630 124071   **   (2) The inner query is the recursive part of a common table expression.
123631 124072   **
123632 124073   **   (3) The inner query has a LIMIT clause (since the changes to the WHERE
123633 124074   **       close would change the meaning of the LIMIT).
123634 124075   **
123635         -**   (4) The inner query is the right operand of a LEFT JOIN.  (The caller
123636         -**       enforces this restriction since this routine does not have enough
123637         -**       information to know.)
       124076  +**   (4) The inner query is the right operand of a LEFT JOIN and the
       124077  +**       expression to be pushed down does not come from the ON clause
       124078  +**       on that LEFT JOIN.
123638 124079   **
123639 124080   **   (5) The WHERE clause expression originates in the ON or USING clause
123640         -**       of a LEFT JOIN.
       124081  +**       of a LEFT JOIN where iCursor is not the right-hand table of that
       124082  +**       left join.  An example:
       124083  +**
       124084  +**           SELECT *
       124085  +**           FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
       124086  +**           JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
       124087  +**           LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
       124088  +**
       124089  +**       The correct answer is three rows:  (1,1,NULL),(2,2,8),(2,2,9).
       124090  +**       But if the (b2=2) term were to be pushed down into the bb subquery,
       124091  +**       then the (1,1,NULL) row would be suppressed.
123641 124092   **
123642 124093   ** Return 0 if no changes are made and non-zero if one or more WHERE clause
123643 124094   ** terms are duplicated into the subquery.
123644 124095   */
123645 124096   static int pushDownWhereTerms(
123646 124097     Parse *pParse,        /* Parse context (for malloc() and error reporting) */
123647 124098     Select *pSubq,        /* The subquery whose WHERE clause is to be augmented */
123648 124099     Expr *pWhere,         /* The WHERE clause of the outer query */
123649         -  int iCursor           /* Cursor number of the subquery */
       124100  +  int iCursor,          /* Cursor number of the subquery */
       124101  +  int isLeftJoin        /* True if pSubq is the right term of a LEFT JOIN */
123650 124102   ){
123651 124103     Expr *pNew;
123652 124104     int nChng = 0;
123653 124105     if( pWhere==0 ) return 0;
123654 124106     if( pSubq->selFlags & SF_Recursive ) return 0;  /* restriction (2) */
123655 124107   
123656 124108   #ifdef SQLITE_DEBUG
................................................................................
123666 124118     }
123667 124119   #endif
123668 124120   
123669 124121     if( pSubq->pLimit!=0 ){
123670 124122       return 0; /* restriction (3) */
123671 124123     }
123672 124124     while( pWhere->op==TK_AND ){
123673         -    nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, iCursor);
       124125  +    nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
       124126  +                                iCursor, isLeftJoin);
123674 124127       pWhere = pWhere->pLeft;
123675 124128     }
123676         -  if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction (5) */
       124129  +  if( isLeftJoin
       124130  +   && (ExprHasProperty(pWhere,EP_FromJoin)==0
       124131  +         || pWhere->iRightJoinTable!=iCursor)
       124132  +  ){
       124133  +    return 0; /* restriction (4) */
       124134  +  }
       124135  +  if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
       124136  +    return 0; /* restriction (5) */
       124137  +  }
123677 124138     if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
123678 124139       nChng++;
123679 124140       while( pSubq ){
123680 124141         SubstContext x;
123681 124142         pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
       124143  +      unsetJoinExpr(pNew, -1);
123682 124144         x.pParse = pParse;
123683 124145         x.iTable = iCursor;
123684 124146         x.iNewTable = iCursor;
123685 124147         x.isLeftJoin = 0;
123686 124148         x.pEList = pSubq->pEList;
123687 124149         pNew = substExpr(&x, pNew);
123688 124150         if( pSubq->selFlags & SF_Aggregate ){
................................................................................
124706 125168       );
124707 125169     }
124708 125170   }
124709 125171   #else
124710 125172   # define explainSimpleCount(a,b,c)
124711 125173   #endif
124712 125174   
124713         -/*
124714         -** Context object for havingToWhereExprCb().
124715         -*/
124716         -struct HavingToWhereCtx {
124717         -  Expr **ppWhere;
124718         -  ExprList *pGroupBy;
124719         -};
124720         -
124721 125175   /*
124722 125176   ** sqlite3WalkExpr() callback used by havingToWhere().
124723 125177   **
124724 125178   ** If the node passed to the callback is a TK_AND node, return 
124725 125179   ** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
124726 125180   **
124727 125181   ** Otherwise, return WRC_Prune. In this case, also check if the 
124728 125182   ** sub-expression matches the criteria for being moved to the WHERE
124729 125183   ** clause. If so, add it to the WHERE clause and replace the sub-expression
124730 125184   ** within the HAVING expression with a constant "1".
124731 125185   */
124732 125186   static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
124733 125187     if( pExpr->op!=TK_AND ){
124734         -    struct HavingToWhereCtx *p = pWalker->u.pHavingCtx;
124735         -    if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, p->pGroupBy) ){
       125188  +    Select *pS = pWalker->u.pSelect;
       125189  +    if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
124736 125190         sqlite3 *db = pWalker->pParse->db;
124737 125191         Expr *pNew = sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[1], 0);
124738 125192         if( pNew ){
124739         -        Expr *pWhere = *(p->ppWhere);
       125193  +        Expr *pWhere = pS->pWhere;
124740 125194           SWAP(Expr, *pNew, *pExpr);
124741 125195           pNew = sqlite3ExprAnd(db, pWhere, pNew);
124742         -        *(p->ppWhere) = pNew;
       125196  +        pS->pWhere = pNew;
       125197  +        pWalker->eCode = 1;
124743 125198         }
124744 125199       }
124745 125200       return WRC_Prune;
124746 125201     }
124747 125202     return WRC_Continue;
124748 125203   }
124749 125204   
................................................................................
124758 125213   **
124759 125214   **   SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
124760 125215   **
124761 125216   ** A term of the HAVING expression is eligible for transfer if it consists
124762 125217   ** entirely of constants and expressions that are also GROUP BY terms that
124763 125218   ** use the "BINARY" collation sequence.
124764 125219   */
124765         -static void havingToWhere(
124766         -  Parse *pParse,
124767         -  ExprList *pGroupBy,
124768         -  Expr *pHaving, 
124769         -  Expr **ppWhere
124770         -){
124771         -  struct HavingToWhereCtx sCtx;
       125220  +static void havingToWhere(Parse *pParse, Select *p){
124772 125221     Walker sWalker;
124773         -
124774         -  sCtx.ppWhere = ppWhere;
124775         -  sCtx.pGroupBy = pGroupBy;
124776         -
124777 125222     memset(&sWalker, 0, sizeof(sWalker));
124778 125223     sWalker.pParse = pParse;
124779 125224     sWalker.xExprCallback = havingToWhereExprCb;
124780         -  sWalker.u.pHavingCtx = &sCtx;
124781         -  sqlite3WalkExpr(&sWalker, pHaving);
       125225  +  sWalker.u.pSelect = p;
       125226  +  sqlite3WalkExpr(&sWalker, p->pHaving);
       125227  +#if SELECTTRACE_ENABLED
       125228  +  if( sWalker.eCode && (sqlite3SelectTrace & 0x100)!=0 ){
       125229  +    SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
       125230  +    sqlite3TreeViewSelect(0, p, 0);
       125231  +  }
       125232  +#endif
124782 125233   }
124783 125234   
124784 125235   /*
124785 125236   ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
124786 125237   ** If it is, then return the SrcList_item for the prior view.  If it is not,
124787 125238   ** then return 0.
124788 125239   */
................................................................................
124935 125386     db = pParse->db;
124936 125387     if( p==0 || db->mallocFailed || pParse->nErr ){
124937 125388       return 1;
124938 125389     }
124939 125390     if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
124940 125391     memset(&sAggInfo, 0, sizeof(sAggInfo));
124941 125392   #if SELECTTRACE_ENABLED
124942         -  pParse->nSelectIndent++;
124943 125393     SELECTTRACE(1,pParse,p, ("begin processing:\n"));
124944 125394     if( sqlite3SelectTrace & 0x100 ){
124945 125395       sqlite3TreeViewSelect(0, p, 0);
124946 125396     }
124947 125397   #endif
124948 125398   
124949 125399     assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
................................................................................
124981 125431     ** does not already exist */
124982 125432     v = sqlite3GetVdbe(pParse);
124983 125433     if( v==0 ) goto select_end;
124984 125434     if( pDest->eDest==SRT_Output ){
124985 125435       generateColumnNames(pParse, p);
124986 125436     }
124987 125437   
124988         -  /* Try to flatten subqueries in the FROM clause up into the main query
       125438  +  /* Try to various optimizations (flattening subqueries, and strength
       125439  +  ** reduction of join operators) in the FROM clause up into the main query
124989 125440     */
124990 125441   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
124991 125442     for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
124992 125443       struct SrcList_item *pItem = &pTabList->a[i];
124993 125444       Select *pSub = pItem->pSelect;
124994 125445       Table *pTab = pItem->pTab;
       125446  +
       125447  +    /* Convert LEFT JOIN into JOIN if there are terms of the right table
       125448  +    ** of the LEFT JOIN used in the WHERE clause.
       125449  +    */
       125450  +    if( (pItem->fg.jointype & JT_LEFT)!=0
       125451  +     && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
       125452  +     && OptimizationEnabled(db, SQLITE_SimplifyJoin)
       125453  +    ){
       125454  +      SELECTTRACE(0x100,pParse,p,
       125455  +                ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
       125456  +      pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
       125457  +      unsetJoinExpr(p->pWhere, pItem->iCursor);
       125458  +    }
       125459  +
       125460  +    /* No futher action if this term of the FROM clause is no a subquery */
124995 125461       if( pSub==0 ) continue;
124996 125462   
124997 125463       /* Catch mismatch in the declared columns of a view and the number of
124998 125464       ** columns in the SELECT on the RHS */
124999 125465       if( pTab->nCol!=pSub->pEList->nExpr ){
125000 125466         sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
125001 125467                         pTab->nCol, pTab->zName, pSub->pEList->nExpr);
................................................................................
125056 125522     ** procedure.
125057 125523     */
125058 125524     if( p->pPrior ){
125059 125525       rc = multiSelect(pParse, p, pDest);
125060 125526       explainSetInteger(pParse->iSelectId, iRestoreSelectId);
125061 125527   #if SELECTTRACE_ENABLED
125062 125528       SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
125063         -    pParse->nSelectIndent--;
125064 125529   #endif
125065 125530       return rc;
125066 125531     }
125067 125532   #endif
125068 125533   
125069 125534     /* For each term in the FROM clause, do two things:
125070 125535     ** (1) Authorized unreferenced tables
................................................................................
125129 125594       ** an exact limit.
125130 125595       */
125131 125596       pParse->nHeight += sqlite3SelectExprHeight(p);
125132 125597   
125133 125598       /* Make copies of constant WHERE-clause terms in the outer query down
125134 125599       ** inside the subquery.  This can help the subquery to run more efficiently.
125135 125600       */
125136         -    if( (pItem->fg.jointype & JT_OUTER)==0
125137         -     && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor)
       125601  +    if( OptimizationEnabled(db, SQLITE_PushDown)
       125602  +     && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
       125603  +                           (pItem->fg.jointype & JT_OUTER)!=0)
125138 125604       ){
125139 125605   #if SELECTTRACE_ENABLED
125140 125606         if( sqlite3SelectTrace & 0x100 ){
125141 125607           SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
125142 125608           sqlite3TreeViewSelect(0, p, 0);
125143 125609         }
125144 125610   #endif
       125611  +    }else{
       125612  +      SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
125145 125613       }
125146 125614   
125147 125615       zSavedAuthContext = pParse->zAuthContext;
125148 125616       pParse->zAuthContext = pItem->zName;
125149 125617   
125150 125618       /* Generate code to implement the subquery
125151 125619       **
................................................................................
125340 125808     if( !isAgg && pGroupBy==0 ){
125341 125809       /* No aggregate functions and no GROUP BY clause */
125342 125810       u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
125343 125811       assert( WHERE_USE_LIMIT==SF_FixedLimit );
125344 125812       wctrlFlags |= p->selFlags & SF_FixedLimit;
125345 125813   
125346 125814       /* Begin the database scan. */
       125815  +    SELECTTRACE(1,pParse,p,("WhereBegin\n"));
125347 125816       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
125348 125817                                  p->pEList, wctrlFlags, p->nSelectRow);
125349 125818       if( pWInfo==0 ) goto select_end;
125350 125819       if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
125351 125820         p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
125352 125821       }
125353 125822       if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
................................................................................
125441 125910       sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
125442 125911       sAggInfo.pGroupBy = pGroupBy;
125443 125912       sqlite3ExprAnalyzeAggList(&sNC, pEList);
125444 125913       sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
125445 125914       if( pHaving ){
125446 125915         if( pGroupBy ){
125447 125916           assert( pWhere==p->pWhere );
125448         -        havingToWhere(pParse, pGroupBy, pHaving, &p->pWhere);
       125917  +        assert( pHaving==p->pHaving );
       125918  +        assert( pGroupBy==p->pGroupBy );
       125919  +        havingToWhere(pParse, p);
125449 125920           pWhere = p->pWhere;
125450 125921         }
125451 125922         sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
125452 125923       }
125453 125924       sAggInfo.nAccumulator = sAggInfo.nColumn;
125454 125925       if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
125455 125926         minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
................................................................................
125528 125999   
125529 126000         /* Begin a loop that will extract all source rows in GROUP BY order.
125530 126001         ** This might involve two separate loops with an OP_Sort in between, or
125531 126002         ** it might be a single loop that uses an index to extract information
125532 126003         ** in the right order to begin with.
125533 126004         */
125534 126005         sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
       126006  +      SELECTTRACE(1,pParse,p,("WhereBegin\n"));
125535 126007         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
125536 126008             WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
125537 126009         );
125538 126010         if( pWInfo==0 ) goto select_end;
125539 126011         if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
125540 126012           /* The optimizer is able to deliver rows in group by order so
125541 126013           ** we do not have to sort.  The OP_OpenEphemeral table will be
................................................................................
125783 126255           ** minMaxFlag will have been previously set to either
125784 126256           ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
125785 126257           ** be an appropriate ORDER BY expression for the optimization.
125786 126258           */
125787 126259           assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
125788 126260           assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
125789 126261   
       126262  +        SELECTTRACE(1,pParse,p,("WhereBegin\n"));
125790 126263           pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
125791 126264                                      0, minMaxFlag, 0);
125792 126265           if( pWInfo==0 ){
125793 126266             goto select_end;
125794 126267           }
125795 126268           updateAccumulator(pParse, &sAggInfo);
125796 126269           if( sqlite3WhereIsOrdered(pWInfo)>0 ){
................................................................................
125838 126311   select_end:
125839 126312     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
125840 126313     sqlite3ExprListDelete(db, pMinMaxOrderBy);
125841 126314     sqlite3DbFree(db, sAggInfo.aCol);
125842 126315     sqlite3DbFree(db, sAggInfo.aFunc);
125843 126316   #if SELECTTRACE_ENABLED
125844 126317     SELECTTRACE(1,pParse,p,("end processing\n"));
125845         -  pParse->nSelectIndent--;
125846 126318   #endif
125847 126319     return rc;
125848 126320   }
125849 126321   
125850 126322   /************** End of select.c **********************************************/
125851 126323   /************** Begin file table.c *******************************************/
125852 126324   /*
................................................................................
132405 132877           continue;
132406 132878         }
132407 132879         if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
132408 132880           if( iNext==0 ) iNext = 3;
132409 132881           continue;
132410 132882         }
132411 132883   
132412         -      if( pTerm->wtFlags & TERM_LIKECOND ){
       132884  +      if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
132413 132885           /* If the TERM_LIKECOND flag is set, that means that the range search
132414 132886           ** is sufficient to guarantee that the LIKE operator is true, so we
132415 132887           ** can skip the call to the like(A,B) function.  But this only works
132416 132888           ** for strings.  So do not skip the call to the function on the pass
132417 132889           ** that compares BLOBs. */
132418 132890   #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
132419 132891           continue;
132420 132892   #else
132421 132893           u32 x = pLevel->iLikeRepCntr;
132422         -        assert( x>0 );
132423         -        skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If, (int)(x>>1));
       132894  +        if( x>0 ){
       132895  +          skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
       132896  +        }
132424 132897           VdbeCoverage(v);
132425 132898   #endif
132426 132899         }
132427 132900   #ifdef WHERETRACE_ENABLED /* 0xffff */
132428 132901         if( sqlite3WhereTrace ){
132429 132902           VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
132430 132903                            pWC->nTerm-j, pTerm, iLoop));
................................................................................
142039 142512     ** then extract the first few alphanumeric characters from within that
142040 142513     ** comment to be the zSelName value.  Otherwise, the label is #N where
142041 142514     ** is an integer that is incremented with each SELECT statement seen.
142042 142515     */
142043 142516     if( yymsp[-8].minor.yy387!=0 ){
142044 142517       const char *z = s.z+6;
142045 142518       int i;
142046         -    sqlite3_snprintf(sizeof(yymsp[-8].minor.yy387->zSelName), yymsp[-8].minor.yy387->zSelName, "#%d",
142047         -                     ++pParse->nSelect);
       142519  +    sqlite3_snprintf(sizeof(yymsp[-8].minor.yy387->zSelName), yymsp[-8].minor.yy387->zSelName,"#%d",++pParse->nSelect);
142048 142520       while( z[0]==' ' ) z++;
142049 142521       if( z[0]=='/' && z[1]=='*' ){
142050 142522         z += 2;
142051 142523         while( z[0]==' ' ) z++;
142052 142524         for(i=0; sqlite3Isalnum(z[i]); i++){}
142053 142525         sqlite3_snprintf(sizeof(yymsp[-8].minor.yy387->zSelName), yymsp[-8].minor.yy387->zSelName, "%.*s", i, z);
142054 142526       }
................................................................................
145976 146448   }
145977 146449   
145978 146450   /*
145979 146451   ** This routine implements a busy callback that sleeps and tries
145980 146452   ** again until a timeout value is reached.  The timeout value is
145981 146453   ** an integer number of milliseconds passed in as the first
145982 146454   ** argument.
       146455  +**
       146456  +** Return non-zero to retry the lock.  Return zero to stop trying
       146457  +** and cause SQLite to return SQLITE_BUSY.
145983 146458   */
145984 146459   static int sqliteDefaultBusyCallback(
145985         - void *ptr,               /* Database connection */
145986         - int count                /* Number of times table has been busy */
       146460  +  void *ptr,               /* Database connection */
       146461  +  int count,               /* Number of times table has been busy */
       146462  +  sqlite3_file *pFile      /* The file on which the lock occurred */
145987 146463   ){
145988 146464   #if SQLITE_OS_WIN || HAVE_USLEEP
       146465  +  /* This case is for systems that have support for sleeping for fractions of
       146466  +  ** a second.  Examples:  All windows systems, unix systems with usleep() */
145989 146467     static const u8 delays[] =
145990 146468        { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
145991 146469     static const u8 totals[] =
145992 146470        { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
145993 146471   # define NDELAY ArraySize(delays)
145994 146472     sqlite3 *db = (sqlite3 *)ptr;
145995         -  int timeout = db->busyTimeout;
       146473  +  int tmout = db->busyTimeout;
145996 146474     int delay, prior;
145997 146475   
       146476  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
       146477  +  if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT,&tmout)==SQLITE_OK ){
       146478  +    if( count ){
       146479  +      tmout = 0;
       146480  +      sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT, &tmout);
       146481  +      return 0;
       146482  +    }else{
       146483  +      return 1;
       146484  +    }
       146485  +  }
       146486  +#else
       146487  +  UNUSED_PARAMETER(pFile);
       146488  +#endif
145998 146489     assert( count>=0 );
145999 146490     if( count < NDELAY ){
146000 146491       delay = delays[count];
146001 146492       prior = totals[count];
146002 146493     }else{
146003 146494       delay = delays[NDELAY-1];
146004 146495       prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
146005 146496     }
146006         -  if( prior + delay > timeout ){
146007         -    delay = timeout - prior;
       146497  +  if( prior + delay > tmout ){
       146498  +    delay = tmout - prior;
146008 146499       if( delay<=0 ) return 0;
146009 146500     }
146010 146501     sqlite3OsSleep(db->pVfs, delay*1000);
146011 146502     return 1;
146012 146503   #else
       146504  +  /* This case for unix systems that lack usleep() support.  Sleeping
       146505  +  ** must be done in increments of whole seconds */
146013 146506     sqlite3 *db = (sqlite3 *)ptr;
146014         -  int timeout = ((sqlite3 *)ptr)->busyTimeout;
146015         -  if( (count+1)*1000 > timeout ){
       146507  +  int tmout = ((sqlite3 *)ptr)->busyTimeout;
       146508  +  UNUSED_PARAMETER(pFile);
       146509  +  if( (count+1)*1000 > tmout ){
146016 146510       return 0;
146017 146511     }
146018 146512     sqlite3OsSleep(db->pVfs, 1000000);
146019 146513     return 1;
146020 146514   #endif
146021 146515   }
146022 146516   
146023 146517   /*
146024 146518   ** Invoke the given busy handler.
146025 146519   **
146026         -** This routine is called when an operation failed with a lock.
       146520  +** This routine is called when an operation failed to acquire a
       146521  +** lock on VFS file pFile.
       146522  +**
146027 146523   ** If this routine returns non-zero, the lock is retried.  If it
146028 146524   ** returns 0, the operation aborts with an SQLITE_BUSY error.
146029 146525   */
146030         -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
       146526  +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){
146031 146527     int rc;
146032         -  if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
146033         -  rc = p->xFunc(p->pArg, p->nBusy);
       146528  +  if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
       146529  +  if( p->bExtraFileArg ){
       146530  +    /* Add an extra parameter with the pFile pointer to the end of the
       146531  +    ** callback argument list */
       146532  +    int (*xTra)(void*,int,sqlite3_file*);
       146533  +    xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler;
       146534  +    rc = xTra(p->pBusyArg, p->nBusy, pFile);
       146535  +  }else{
       146536  +    /* Legacy style busy handler callback */
       146537  +    rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
       146538  +  }
146034 146539     if( rc==0 ){
146035 146540       p->nBusy = -1;
146036 146541     }else{
146037 146542       p->nBusy++;
146038 146543     }
146039 146544     return rc; 
146040 146545   }
................................................................................
146048 146553     int (*xBusy)(void*,int),
146049 146554     void *pArg
146050 146555   ){
146051 146556   #ifdef SQLITE_ENABLE_API_ARMOR
146052 146557     if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
146053 146558   #endif
146054 146559     sqlite3_mutex_enter(db->mutex);
146055         -  db->busyHandler.xFunc = xBusy;
146056         -  db->busyHandler.pArg = pArg;
       146560  +  db->busyHandler.xBusyHandler = xBusy;
       146561  +  db->busyHandler.pBusyArg = pArg;
146057 146562     db->busyHandler.nBusy = 0;
       146563  +  db->busyHandler.bExtraFileArg = 0;
146058 146564     db->busyTimeout = 0;
146059 146565     sqlite3_mutex_leave(db->mutex);
146060 146566     return SQLITE_OK;
146061 146567   }
146062 146568   
146063 146569   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
146064 146570   /*
................................................................................
146098 146604   ** specified number of milliseconds before returning 0.
146099 146605   */
146100 146606   SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
146101 146607   #ifdef SQLITE_ENABLE_API_ARMOR
146102 146608     if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
146103 146609   #endif
146104 146610     if( ms>0 ){
146105         -    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
       146611  +    sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
       146612  +                             (void*)db);
146106 146613       db->busyTimeout = ms;
       146614  +    db->busyHandler.bExtraFileArg = 1;
146107 146615     }else{
146108 146616       sqlite3_busy_handler(db, 0, 0);
146109 146617     }
146110 146618     return SQLITE_OK;
146111 146619   }
146112 146620   
146113 146621   /*
................................................................................
148092 148600         rc = SQLITE_OK;
148093 148601       }else if( op==SQLITE_FCNTL_VFS_POINTER ){
148094 148602         *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
148095 148603         rc = SQLITE_OK;
148096 148604       }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
148097 148605         *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
148098 148606         rc = SQLITE_OK;
148099         -    }else if( fd->pMethods ){
       148607  +    }else{
148100 148608         rc = sqlite3OsFileControl(fd, op, pArg);
148101         -    }else{
148102         -      rc = SQLITE_NOTFOUND;
148103 148609       }
148104 148610       sqlite3BtreeLeave(pBtree);
148105 148611     }
148106 148612     sqlite3_mutex_leave(db->mutex);
148107 148613     return rc;
148108 148614   }
148109 148615   
................................................................................
175671 176177   
175672 176178       while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
175673 176179         int bKey = sqlite3_column_int(pXInfo, 5);
175674 176180         if( bKey ){
175675 176181           int iCid = sqlite3_column_int(pXInfo, 1);
175676 176182           int bDesc = sqlite3_column_int(pXInfo, 3);
175677 176183           const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
175678         -        zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma, 
       176184  +        zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma, 
175679 176185               iCid, pIter->azTblType[iCid], zCollate
175680 176186           );
175681 176187           zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
175682 176188           zComma = ", ";
175683 176189         }
175684 176190       }
175685 176191       zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
................................................................................
175732 176238         );
175733 176239   
175734 176240         if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
175735 176241           /* If the target table column is an "INTEGER PRIMARY KEY", add
175736 176242           ** "PRIMARY KEY" to the imposter table column declaration. */
175737 176243           zPk = "PRIMARY KEY ";
175738 176244         }
175739         -      zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s", 
       176245  +      zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s", 
175740 176246             zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
175741 176247             (pIter->abNotNull[iCol] ? " NOT NULL" : "")
175742 176248         );
175743 176249         zComma = ", ";
175744 176250       }
175745 176251   
175746 176252       if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
................................................................................
179166 179672     pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
179167 179673   
179168 179674     /* If connected to a ZIPVFS backend, override the page size and
179169 179675     ** offset with actual values obtained from ZIPVFS.
179170 179676     */
179171 179677     fd = sqlite3PagerFile(pPager);
179172 179678     x[0] = pCsr->iPageno;
179173         -  if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
       179679  +  if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
179174 179680       pCsr->iOffset = x[0];
179175 179681       pCsr->szPage = (int)x[1];
179176 179682     }
179177 179683   }
179178 179684   
179179 179685   /*
179180 179686   ** Move a statvfs cursor to the next entry in the file.
................................................................................
180096 180602   **
180097 180603   ** As in the changeset format, each field of the single record that is part
180098 180604   ** of a patchset change is associated with the correspondingly positioned
180099 180605   ** table column, counting from left to right within the CREATE TABLE 
180100 180606   ** statement.
180101 180607   **
180102 180608   ** For a DELETE change, all fields within the record except those associated
180103         -** with PRIMARY KEY columns are set to "undefined". The PRIMARY KEY fields
180104         -** contain the values identifying the row to delete.
       180609  +** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
       180610  +** values identifying the row to delete.
180105 180611   **
180106 180612   ** For an UPDATE change, all fields except those associated with PRIMARY KEY
180107 180613   ** columns and columns that are modified by the UPDATE are set to "undefined".
180108 180614   ** PRIMARY KEY fields contain the values identifying the table row to update,
180109 180615   ** and fields associated with modified columns contain the new column values.
180110 180616   **
180111 180617   ** The records associated with INSERT changes are in the same format as for
................................................................................
180380 180886   ** The buffer that the argument points to contains a serialized SQL value.
180381 180887   ** Return the number of bytes of space occupied by the value (including
180382 180888   ** the type byte).
180383 180889   */
180384 180890   static int sessionSerialLen(u8 *a){
180385 180891     int e = *a;
180386 180892     int n;
180387         -  if( e==0 ) return 1;
       180893  +  if( e==0 || e==0xFF ) return 1;
180388 180894     if( e==SQLITE_NULL ) return 1;
180389 180895     if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
180390 180896     return sessionVarintGet(&a[1], &n) + 1 + n;
180391 180897   }
180392 180898   
180393 180899   /*
180394 180900   ** Based on the primary key values stored in change aRecord, calculate a
................................................................................
180460 180966     int iCol;                       /* Used to iterate through table columns */
180461 180967   
180462 180968     for(iCol=0; iCol<pTab->nCol; iCol++){
180463 180969       if( pTab->abPK[iCol] ){
180464 180970         int n1 = sessionSerialLen(a1);
180465 180971         int n2 = sessionSerialLen(a2);
180466 180972   
180467         -      if( pTab->abPK[iCol] && (n1!=n2 || memcmp(a1, a2, n1)) ){
       180973  +      if( n1!=n2 || memcmp(a1, a2, n1) ){
180468 180974           return 0;
180469 180975         }
180470 180976         a1 += n1;
180471 180977         a2 += n2;
180472 180978       }else{
180473 180979         if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
180474 180980         if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
................................................................................
180703 181209           a += sessionVarintGet(a, &n);
180704 181210           if( sqlite3_value_bytes(pVal)!=n ) return 0;
180705 181211           if( eType==SQLITE_TEXT ){
180706 181212             z = sqlite3_value_text(pVal);
180707 181213           }else{
180708 181214             z = sqlite3_value_blob(pVal);
180709 181215           }
180710         -        if( memcmp(a, z, n) ) return 0;
       181216  +        if( n>0 && memcmp(a, z, n) ) return 0;
180711 181217           a += n;
180712 181218         }
180713 181219       }
180714 181220     }
180715 181221   
180716 181222     return 1;
180717 181223   }
................................................................................
182047 182553     int nSql = -1;
182048 182554   
182049 182555     if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
182050 182556       zSql = sqlite3_mprintf(
182051 182557           "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
182052 182558           "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
182053 182559       );
       182560  +    if( zSql==0 ) rc = SQLITE_NOMEM;
182054 182561     }else{
182055 182562       int i;
182056 182563       const char *zSep = "";
182057 182564       SessionBuffer buf = {0, 0, 0};
182058 182565   
182059 182566       sessionAppendStr(&buf, "SELECT * FROM ", &rc);
182060 182567       sessionAppendIdent(&buf, zDb, &rc);
................................................................................
182782 183289   ** successfully advanced to the next change in the changeset, an SQLite 
182783 183290   ** error code if an error occurs, or SQLITE_DONE if there are no further 
182784 183291   ** changes in the changeset.
182785 183292   */
182786 183293   static int sessionChangesetNext(
182787 183294     sqlite3_changeset_iter *p,      /* Changeset iterator */
182788 183295     u8 **paRec,                     /* If non-NULL, store record pointer here */
182789         -  int *pnRec                      /* If non-NULL, store size of record here */
       183296  +  int *pnRec,                     /* If non-NULL, store size of record here */
       183297  +  int *pbNew                      /* If non-NULL, true if new table */
182790 183298   ){
182791 183299     int i;
182792 183300     u8 op;
182793 183301   
182794 183302     assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
182795 183303   
182796 183304     /* If the iterator is in the error-state, return immediately. */
................................................................................
182817 183325     }
182818 183326   
182819 183327     sessionDiscardData(&p->in);
182820 183328     p->in.iCurrent = p->in.iNext;
182821 183329   
182822 183330     op = p->in.aData[p->in.iNext++];
182823 183331     while( op=='T' || op=='P' ){
       183332  +    if( pbNew ) *pbNew = 1;
182824 183333       p->bPatchset = (op=='P');
182825 183334       if( sessionChangesetReadTblhdr(p) ) return p->rc;
182826 183335       if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
182827 183336       p->in.iCurrent = p->in.iNext;
182828 183337       if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
182829 183338       op = p->in.aData[p->in.iNext++];
182830 183339     }
................................................................................
182895 183404   ** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
182896 183405   ** or SQLITE_CORRUPT.
182897 183406   **
182898 183407   ** This function may not be called on iterators passed to a conflict handler
182899 183408   ** callback by changeset_apply().
182900 183409   */
182901 183410   SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
182902         -  return sessionChangesetNext(p, 0, 0);
       183411  +  return sessionChangesetNext(p, 0, 0, 0);
182903 183412   }
182904 183413   
182905 183414   /*
182906 183415   ** The following function extracts information on the current change
182907 183416   ** from a changeset iterator. It may only be called after changeset_next()
182908 183417   ** has returned SQLITE_ROW.
182909 183418   */
................................................................................
183274 183783     sqlite3_stmt *pSelect;          /* SELECT statement */
183275 183784     int nCol;                       /* Size of azCol[] and abPK[] arrays */
183276 183785     const char **azCol;             /* Array of column names */
183277 183786     u8 *abPK;                       /* Boolean array - true if column is in PK */
183278 183787     int bStat1;                     /* True if table is sqlite_stat1 */
183279 183788     int bDeferConstraints;          /* True to defer constraints */
183280 183789     SessionBuffer constraints;      /* Deferred constraints are stored here */
       183790  +  SessionBuffer rebase;           /* Rebase information (if any) here */
       183791  +  int bRebaseStarted;             /* If table header is already in rebase */
183281 183792   };
183282 183793   
183283 183794   /*
183284 183795   ** Formulate a statement to DELETE a row from database db. Assuming a table
183285 183796   ** structure like this:
183286 183797   **
183287 183798   **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
................................................................................
183540 184051     if( rc==SQLITE_OK ){
183541 184052       rc = sessionPrepare(db, &p->pDelete,
183542 184053           "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
183543 184054           "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
183544 184055           "AND (?4 OR stat IS ?3)"
183545 184056       );
183546 184057     }
183547         -  assert( rc==SQLITE_OK );
183548 184058     return rc;
183549 184059   }
183550 184060   
183551 184061   /*
183552 184062   ** A wrapper around sqlite3_bind_value() that detects an extra problem. 
183553 184063   ** See comments in the body of this function for details.
183554 184064   */
................................................................................
183654 184164     if( rc==SQLITE_OK ){
183655 184165       rc = sqlite3_step(pSelect);
183656 184166       if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
183657 184167     }
183658 184168   
183659 184169     return rc;
183660 184170   }
       184171  +
       184172  +/*
       184173  +** This function is called from within sqlite3changset_apply_v2() when
       184174  +** a conflict is encountered and resolved using conflict resolution
       184175  +** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
       184176  +** It adds a conflict resolution record to the buffer in 
       184177  +** SessionApplyCtx.rebase, which will eventually be returned to the caller
       184178  +** of apply_v2() as the "rebase" buffer.
       184179  +**
       184180  +** Return SQLITE_OK if successful, or an SQLite error code otherwise.
       184181  +*/
       184182  +static int sessionRebaseAdd(
       184183  +  SessionApplyCtx *p,             /* Apply context */
       184184  +  int eType,                      /* Conflict resolution (OMIT or REPLACE) */
       184185  +  sqlite3_changeset_iter *pIter   /* Iterator pointing at current change */
       184186  +){
       184187  +  int rc = SQLITE_OK;
       184188  +  int i;
       184189  +  int eOp = pIter->op;
       184190  +  if( p->bRebaseStarted==0 ){
       184191  +    /* Append a table-header to the rebase buffer */
       184192  +    const char *zTab = pIter->zTab;
       184193  +    sessionAppendByte(&p->rebase, 'T', &rc);
       184194  +    sessionAppendVarint(&p->rebase, p->nCol, &rc);
       184195  +    sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
       184196  +    sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
       184197  +    p->bRebaseStarted = 1;
       184198  +  }
       184199  +
       184200  +  assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
       184201  +  assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
       184202  +
       184203  +  sessionAppendByte(&p->rebase, 
       184204  +      (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
       184205  +  );
       184206  +  sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
       184207  +  for(i=0; i<p->nCol; i++){
       184208  +    sqlite3_value *pVal = 0;
       184209  +    if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
       184210  +      sqlite3changeset_old(pIter, i, &pVal);
       184211  +    }else{
       184212  +      sqlite3changeset_new(pIter, i, &pVal);
       184213  +    }
       184214  +    sessionAppendValue(&p->rebase, pVal, &rc);
       184215  +  }
       184216  +
       184217  +  return rc;
       184218  +}
183661 184219   
183662 184220   /*
183663 184221   ** Invoke the conflict handler for the change that the changeset iterator
183664 184222   ** currently points to.
183665 184223   **
183666 184224   ** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
183667 184225   ** If argument pbReplace is NULL, then the type of conflict handler invoked
................................................................................
183730 184288     }else if( rc==SQLITE_OK ){
183731 184289       if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
183732 184290         /* Instead of invoking the conflict handler, append the change blob
183733 184291         ** to the SessionApplyCtx.constraints buffer. */
183734 184292         u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
183735 184293         int nBlob = pIter->in.iNext - pIter->in.iCurrent;
183736 184294         sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
183737         -      res = SQLITE_CHANGESET_OMIT;
       184295  +      return SQLITE_OK;
183738 184296       }else{
183739 184297         /* No other row with the new.* primary key. */
183740 184298         res = xConflict(pCtx, eType+1, pIter);
183741 184299         if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
183742 184300       }
183743 184301     }
183744 184302   
................................................................................
183756 184314           rc = SQLITE_ABORT;
183757 184315           break;
183758 184316   
183759 184317         default:
183760 184318           rc = SQLITE_MISUSE;
183761 184319           break;
183762 184320       }
       184321  +    if( rc==SQLITE_OK ){
       184322  +      rc = sessionRebaseAdd(p, res, pIter);
       184323  +    }
183763 184324     }
183764 184325   
183765 184326     return rc;
183766 184327   }
183767 184328   
183768 184329   /*
183769 184330   ** Attempt to apply the change that the iterator passed as the first argument
................................................................................
183931 184492     void *pCtx                      /* First argument passed to xConflict */
183932 184493   ){
183933 184494     int bReplace = 0;
183934 184495     int bRetry = 0;
183935 184496     int rc;
183936 184497   
183937 184498     rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
183938         -  assert( rc==SQLITE_OK || (bRetry==0 && bReplace==0) );
183939         -
183940         -  /* If the bRetry flag is set, the change has not been applied due to an
183941         -  ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
183942         -  ** a row with the correct PK is present in the db, but one or more other
183943         -  ** fields do not contain the expected values) and the conflict handler 
183944         -  ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
183945         -  ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
183946         -  ** the SQLITE_CHANGESET_DATA problem.  */
183947         -  if( bRetry ){
183948         -    assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
183949         -    rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
183950         -  }
183951         -
183952         -  /* If the bReplace flag is set, the change is an INSERT that has not
183953         -  ** been performed because the database already contains a row with the
183954         -  ** specified primary key and the conflict handler returned
183955         -  ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
183956         -  ** before reattempting the INSERT.  */
183957         -  else if( bReplace ){
183958         -    assert( pIter->op==SQLITE_INSERT );
183959         -    rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
183960         -    if( rc==SQLITE_OK ){
183961         -      rc = sessionBindRow(pIter, 
183962         -          sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
183963         -      sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
183964         -    }
183965         -    if( rc==SQLITE_OK ){
183966         -      sqlite3_step(pApply->pDelete);
183967         -      rc = sqlite3_reset(pApply->pDelete);
183968         -    }
183969         -    if( rc==SQLITE_OK ){
       184499  +  if( rc==SQLITE_OK ){
       184500  +    /* If the bRetry flag is set, the change has not been applied due to an
       184501  +    ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
       184502  +    ** a row with the correct PK is present in the db, but one or more other
       184503  +    ** fields do not contain the expected values) and the conflict handler 
       184504  +    ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
       184505  +    ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
       184506  +    ** the SQLITE_CHANGESET_DATA problem.  */
       184507  +    if( bRetry ){
       184508  +      assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
183970 184509         rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
183971 184510       }
183972         -    if( rc==SQLITE_OK ){
183973         -      rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
       184511  +
       184512  +    /* If the bReplace flag is set, the change is an INSERT that has not
       184513  +    ** been performed because the database already contains a row with the
       184514  +    ** specified primary key and the conflict handler returned
       184515  +    ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
       184516  +    ** before reattempting the INSERT.  */
       184517  +    else if( bReplace ){
       184518  +      assert( pIter->op==SQLITE_INSERT );
       184519  +      rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
       184520  +      if( rc==SQLITE_OK ){
       184521  +        rc = sessionBindRow(pIter, 
       184522  +            sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
       184523  +        sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
       184524  +      }
       184525  +      if( rc==SQLITE_OK ){
       184526  +        sqlite3_step(pApply->pDelete);
       184527  +        rc = sqlite3_reset(pApply->pDelete);
       184528  +      }
       184529  +      if( rc==SQLITE_OK ){
       184530  +        rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
       184531  +      }
       184532  +      if( rc==SQLITE_OK ){
       184533  +        rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
       184534  +      }
183974 184535       }
183975 184536     }
183976 184537   
183977 184538     return rc;
183978 184539   }
183979 184540   
183980 184541   /*
................................................................................
184042 184603       const char *zTab              /* Table name */
184043 184604     ),
184044 184605     int(*xConflict)(
184045 184606       void *pCtx,                   /* Copy of fifth arg to _apply() */
184046 184607       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
184047 184608       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
184048 184609     ),
184049         -  void *pCtx                      /* First argument passed to xConflict */
       184610  +  void *pCtx,                     /* First argument passed to xConflict */
       184611  +  void **ppRebase, int *pnRebase  /* OUT: Rebase information */
184050 184612   ){
184051 184613     int schemaMismatch = 0;
184052 184614     int rc;                         /* Return code */
184053 184615     const char *zTab = 0;           /* Name of current table */
184054 184616     int nTab = 0;                   /* Result of sqlite3Strlen30(zTab) */
184055 184617     SessionApplyCtx sApply;         /* changeset_apply() context object */
184056 184618     int bPatchset;
................................................................................
184080 184642         if( rc!=SQLITE_OK ) break;
184081 184643   
184082 184644         sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
184083 184645         sqlite3_finalize(sApply.pDelete);
184084 184646         sqlite3_finalize(sApply.pUpdate); 
184085 184647         sqlite3_finalize(sApply.pInsert);
184086 184648         sqlite3_finalize(sApply.pSelect);
184087         -      memset(&sApply, 0, sizeof(sApply));
184088 184649         sApply.db = db;
       184650  +      sApply.pDelete = 0;
       184651  +      sApply.pUpdate = 0;
       184652  +      sApply.pInsert = 0;
       184653  +      sApply.pSelect = 0;
       184654  +      sApply.nCol = 0;
       184655  +      sApply.azCol = 0;
       184656  +      sApply.abPK = 0;
       184657  +      sApply.bStat1 = 0;
184089 184658         sApply.bDeferConstraints = 1;
       184659  +      sApply.bRebaseStarted = 0;
       184660  +      memset(&sApply.constraints, 0, sizeof(SessionBuffer));
184090 184661   
184091 184662         /* If an xFilter() callback was specified, invoke it now. If the 
184092 184663         ** xFilter callback returns zero, skip this table. If it returns
184093 184664         ** non-zero, proceed. */
184094 184665         schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
184095 184666         if( schemaMismatch ){
184096 184667           zTab = sqlite3_mprintf("%s", zNew);
................................................................................
184192 184763     if( rc==SQLITE_OK ){
184193 184764       rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
184194 184765     }else{
184195 184766       sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
184196 184767       sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
184197 184768     }
184198 184769   
       184770  +  if( rc==SQLITE_OK && bPatchset==0 && ppRebase && pnRebase ){
       184771  +    *ppRebase = (void*)sApply.rebase.aBuf;
       184772  +    *pnRebase = sApply.rebase.nBuf;
       184773  +    sApply.rebase.aBuf = 0;
       184774  +  }
184199 184775     sqlite3_finalize(sApply.pInsert);
184200 184776     sqlite3_finalize(sApply.pDelete);
184201 184777     sqlite3_finalize(sApply.pUpdate);
184202 184778     sqlite3_finalize(sApply.pSelect);
184203 184779     sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
184204 184780     sqlite3_free((char*)sApply.constraints.aBuf);
       184781  +  sqlite3_free((char*)sApply.rebase.aBuf);
184205 184782     sqlite3_mutex_leave(sqlite3_db_mutex(db));
184206 184783     return rc;
184207 184784   }
       184785  +
       184786  +/*
       184787  +** Apply the changeset passed via pChangeset/nChangeset to the main 
       184788  +** database attached to handle "db".
       184789  +*/
       184790  +SQLITE_API int sqlite3changeset_apply_v2(
       184791  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
       184792  +  int nChangeset,                 /* Size of changeset in bytes */
       184793  +  void *pChangeset,               /* Changeset blob */
       184794  +  int(*xFilter)(
       184795  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184796  +    const char *zTab              /* Table name */
       184797  +  ),
       184798  +  int(*xConflict)(
       184799  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184800  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
       184801  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
       184802  +  ),
       184803  +  void *pCtx,                     /* First argument passed to xConflict */
       184804  +  void **ppRebase, int *pnRebase
       184805  +){
       184806  +  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
       184807  +  int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
       184808  +  if( rc==SQLITE_OK ){
       184809  +    rc = sessionChangesetApply(
       184810  +        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
       184811  +    );
       184812  +  }
       184813  +  return rc;
       184814  +}
184208 184815   
184209 184816   /*
184210 184817   ** Apply the changeset passed via pChangeset/nChangeset to the main database
184211 184818   ** attached to handle "db". Invoke the supplied conflict handler callback
184212 184819   ** to resolve any conflicts encountered while applying the change.
184213 184820   */
184214 184821   SQLITE_API int sqlite3changeset_apply(
................................................................................
184222 184829     int(*xConflict)(
184223 184830       void *pCtx,                   /* Copy of fifth arg to _apply() */
184224 184831       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
184225 184832       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
184226 184833     ),
184227 184834     void *pCtx                      /* First argument passed to xConflict */
184228 184835   ){
184229         -  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
184230         -  int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
184231         -  if( rc==SQLITE_OK ){
184232         -    rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
184233         -  }
184234         -  return rc;
       184836  +  return sqlite3changeset_apply_v2(
       184837  +      db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0
       184838  +  );
184235 184839   }
184236 184840   
184237 184841   /*
184238 184842   ** Apply the changeset passed via xInput/pIn to the main database
184239 184843   ** attached to handle "db". Invoke the supplied conflict handler callback
184240 184844   ** to resolve any conflicts encountered while applying the change.
184241 184845   */
       184846  +SQLITE_API int sqlite3changeset_apply_v2_strm(
       184847  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
       184848  +  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
       184849  +  void *pIn,                                          /* First arg for xInput */
       184850  +  int(*xFilter)(
       184851  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184852  +    const char *zTab              /* Table name */
       184853  +  ),
       184854  +  int(*xConflict)(
       184855  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
       184856  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
       184857  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
       184858  +  ),
       184859  +  void *pCtx,                     /* First argument passed to xConflict */
       184860  +  void **ppRebase, int *pnRebase
       184861  +){
       184862  +  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
       184863  +  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
       184864  +  if( rc==SQLITE_OK ){
       184865  +    rc = sessionChangesetApply(
       184866  +        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
       184867  +    );
       184868  +  }
       184869  +  return rc;
       184870  +}
184242 184871   SQLITE_API int sqlite3changeset_apply_strm(
184243 184872     sqlite3 *db,                    /* Apply change to "main" db of this handle */
184244 184873     int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
184245 184874     void *pIn,                                          /* First arg for xInput */
184246 184875     int(*xFilter)(
184247 184876       void *pCtx,                   /* Copy of sixth arg to _apply() */
184248 184877       const char *zTab              /* Table name */
................................................................................
184250 184879     int(*xConflict)(
184251 184880       void *pCtx,                   /* Copy of sixth arg to _apply() */
184252 184881       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
184253 184882       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
184254 184883     ),
184255 184884     void *pCtx                      /* First argument passed to xConflict */
184256 184885   ){
184257         -  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
184258         -  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
184259         -  if( rc==SQLITE_OK ){
184260         -    rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
184261         -  }
184262         -  return rc;
       184886  +  return sqlite3changeset_apply_v2_strm(
       184887  +      db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0
       184888  +  );
184263 184889   }
184264 184890   
184265 184891   /*
184266 184892   ** sqlite3_changegroup handle.
184267 184893   */
184268 184894   struct sqlite3_changegroup {
184269 184895     int rc;                         /* Error code */
................................................................................
184274 184900   /*
184275 184901   ** This function is called to merge two changes to the same row together as
184276 184902   ** part of an sqlite3changeset_concat() operation. A new change object is
184277 184903   ** allocated and a pointer to it stored in *ppNew.
184278 184904   */
184279 184905   static int sessionChangeMerge(
184280 184906     SessionTable *pTab,             /* Table structure */
       184907  +  int bRebase,                    /* True for a rebase hash-table */
184281 184908     int bPatchset,                  /* True for patchsets */
184282 184909     SessionChange *pExist,          /* Existing change */
184283 184910     int op2,                        /* Second change operation */
184284 184911     int bIndirect,                  /* True if second change is indirect */
184285 184912     u8 *aRec,                       /* Second change record */
184286 184913     int nRec,                       /* Number of bytes in aRec */
184287 184914     SessionChange **ppNew           /* OUT: Merged change */
184288 184915   ){
184289 184916     SessionChange *pNew = 0;
       184917  +  int rc = SQLITE_OK;
184290 184918   
184291 184919     if( !pExist ){
184292 184920       pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
184293 184921       if( !pNew ){
184294 184922         return SQLITE_NOMEM;
184295 184923       }
184296 184924       memset(pNew, 0, sizeof(SessionChange));
184297 184925       pNew->op = op2;
184298 184926       pNew->bIndirect = bIndirect;
184299         -    pNew->nRecord = nRec;
184300 184927       pNew->aRecord = (u8*)&pNew[1];
184301         -    memcpy(pNew->aRecord, aRec, nRec);
       184928  +    if( bIndirect==0 || bRebase==0 ){
       184929  +      pNew->nRecord = nRec;
       184930  +      memcpy(pNew->aRecord, aRec, nRec);
       184931  +    }else{
       184932  +      int i;
       184933  +      u8 *pIn = aRec;
       184934  +      u8 *pOut = pNew->aRecord;
       184935  +      for(i=0; i<pTab->nCol; i++){
       184936  +        int nIn = sessionSerialLen(pIn);
       184937  +        if( *pIn==0 ){
       184938  +          *pOut++ = 0;
       184939  +        }else if( pTab->abPK[i]==0 ){
       184940  +          *pOut++ = 0xFF;
       184941  +        }else{
       184942  +          memcpy(pOut, pIn, nIn);
       184943  +          pOut += nIn;
       184944  +        }
       184945  +        pIn += nIn;
       184946  +      }
       184947  +      pNew->nRecord = pOut - pNew->aRecord;
       184948  +    }
       184949  +  }else if( bRebase ){
       184950  +    if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
       184951  +      *ppNew = pExist;
       184952  +    }else{
       184953  +      int nByte = nRec + pExist->nRecord + sizeof(SessionChange);
       184954  +      pNew = (SessionChange*)sqlite3_malloc(nByte);
       184955  +      if( pNew==0 ){
       184956  +        rc = SQLITE_NOMEM;
       184957  +      }else{
       184958  +        int i;
       184959  +        u8 *a1 = pExist->aRecord;
       184960  +        u8 *a2 = aRec;
       184961  +        u8 *pOut;
       184962  +
       184963  +        memset(pNew, 0, nByte);
       184964  +        pNew->bIndirect = bIndirect || pExist->bIndirect;
       184965  +        pNew->op = op2;
       184966  +        pOut = pNew->aRecord = (u8*)&pNew[1];
       184967  +
       184968  +        for(i=0; i<pTab->nCol; i++){
       184969  +          int n1 = sessionSerialLen(a1);
       184970  +          int n2 = sessionSerialLen(a2);
       184971  +          if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
       184972  +            *pOut++ = 0xFF;
       184973  +          }else if( *a2==0 ){
       184974  +            memcpy(pOut, a1, n1);
       184975  +            pOut += n1;
       184976  +          }else{
       184977  +            memcpy(pOut, a2, n2);
       184978  +            pOut += n2;
       184979  +          }
       184980  +          a1 += n1;
       184981  +          a2 += n2;
       184982  +        }
       184983  +        pNew->nRecord = pOut - pNew->aRecord;
       184984  +      }
       184985  +      sqlite3_free(pExist);
       184986  +    }
184302 184987     }else{
184303 184988       int op1 = pExist->op;
184304 184989   
184305 184990       /* 
184306 184991       **   op1=INSERT, op2=INSERT      ->      Unsupported. Discard op2.
184307 184992       **   op1=INSERT, op2=UPDATE      ->      INSERT.
184308 184993       **   op1=INSERT, op2=DELETE      ->      (none)
................................................................................
184388 185073           pNew->nRecord = (int)(aCsr - pNew->aRecord);
184389 185074         }
184390 185075         sqlite3_free(pExist);
184391 185076       }
184392 185077     }
184393 185078   
184394 185079     *ppNew = pNew;
184395         -  return SQLITE_OK;
       185080  +  return rc;
184396 185081   }
184397 185082   
184398 185083   /*
184399 185084   ** Add all changes in the changeset traversed by the iterator passed as
184400 185085   ** the first argument to the changegroup hash tables.
184401 185086   */
184402 185087   static int sessionChangesetToHash(
184403 185088     sqlite3_changeset_iter *pIter,   /* Iterator to read from */
184404         -  sqlite3_changegroup *pGrp        /* Changegroup object to add changeset to */
       185089  +  sqlite3_changegroup *pGrp,       /* Changegroup object to add changeset to */
       185090  +  int bRebase                      /* True if hash table is for rebasing */
184405 185091   ){
184406 185092     u8 *aRec;
184407 185093     int nRec;
184408 185094     int rc = SQLITE_OK;
184409 185095     SessionTable *pTab = 0;
184410 185096   
184411         -
184412         -  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec) ){
       185097  +  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
184413 185098       const char *zNew;
184414 185099       int nCol;
184415 185100       int op;
184416 185101       int iHash;
184417 185102       int bIndirect;
184418 185103       SessionChange *pChange;
184419 185104       SessionChange *pExist = 0;
................................................................................
184485 185170           pExist = *pp;
184486 185171           *pp = (*pp)->pNext;
184487 185172           pTab->nEntry--;
184488 185173           break;
184489 185174         }
184490 185175       }
184491 185176   
184492         -    rc = sessionChangeMerge(pTab, 
       185177  +    rc = sessionChangeMerge(pTab, bRebase, 
184493 185178           pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
184494 185179       );
184495 185180       if( rc ) break;
184496 185181       if( pChange ){
184497 185182         pChange->pNext = pTab->apChange[iHash];
184498 185183         pTab->apChange[iHash] = pChange;
184499 185184         pTab->nEntry++;
................................................................................
184593 185278   */
184594 185279   SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
184595 185280     sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
184596 185281     int rc;                         /* Return code */
184597 185282   
184598 185283     rc = sqlite3changeset_start(&pIter, nData, pData);
184599 185284     if( rc==SQLITE_OK ){
184600         -    rc = sessionChangesetToHash(pIter, pGrp);
       185285  +    rc = sessionChangesetToHash(pIter, pGrp, 0);
184601 185286     }
184602 185287     sqlite3changeset_finalize(pIter);
184603 185288     return rc;
184604 185289   }
184605 185290   
184606 185291   /*
184607 185292   ** Obtain a buffer containing a changeset representing the concatenation
................................................................................
184624 185309     void *pIn
184625 185310   ){
184626 185311     sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
184627 185312     int rc;                         /* Return code */
184628 185313   
184629 185314     rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
184630 185315     if( rc==SQLITE_OK ){
184631         -    rc = sessionChangesetToHash(pIter, pGrp);
       185316  +    rc = sessionChangesetToHash(pIter, pGrp, 0);
184632 185317     }
184633 185318     sqlite3changeset_finalize(pIter);
184634 185319     return rc;
184635 185320   }
184636 185321   
184637 185322   /*
184638 185323   ** Streaming versions of changegroup_output().
................................................................................
184708 185393     if( rc==SQLITE_OK ){
184709 185394       rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
184710 185395     }
184711 185396     sqlite3changegroup_delete(pGrp);
184712 185397   
184713 185398     return rc;
184714 185399   }
       185400  +
       185401  +/*
       185402  +** Changeset rebaser handle.
       185403  +*/
       185404  +struct sqlite3_rebaser {
       185405  +  sqlite3_changegroup grp;        /* Hash table */
       185406  +};
       185407  +
       185408  +/*
       185409  +** Buffers a1 and a2 must both contain a sessions module record nCol
       185410  +** fields in size. This function appends an nCol sessions module 
       185411  +** record to buffer pBuf that is a copy of a1, except that for
       185412  +** each field that is undefined in a1[], swap in the field from a2[].
       185413  +*/
       185414  +static void sessionAppendRecordMerge(
       185415  +  SessionBuffer *pBuf,            /* Buffer to append to */
       185416  +  int nCol,                       /* Number of columns in each record */
       185417  +  u8 *a1, int n1,                 /* Record 1 */
       185418  +  u8 *a2, int n2,                 /* Record 2 */
       185419  +  int *pRc                        /* IN/OUT: error code */
       185420  +){
       185421  +  sessionBufferGrow(pBuf, n1+n2, pRc);
       185422  +  if( *pRc==SQLITE_OK ){
       185423  +    int i;
       185424  +    u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
       185425  +    for(i=0; i<nCol; i++){
       185426  +      int nn1 = sessionSerialLen(a1);
       185427  +      int nn2 = sessionSerialLen(a2);
       185428  +      if( *a1==0 || *a1==0xFF ){
       185429  +        memcpy(pOut, a2, nn2);
       185430  +        pOut += nn2;
       185431  +      }else{
       185432  +        memcpy(pOut, a1, nn1);
       185433  +        pOut += nn1;
       185434  +      }
       185435  +      a1 += nn1;
       185436  +      a2 += nn2;
       185437  +    }
       185438  +
       185439  +    pBuf->nBuf = pOut-pBuf->aBuf;
       185440  +    assert( pBuf->nBuf<=pBuf->nAlloc );
       185441  +  }
       185442  +}
       185443  +
       185444  +/*
       185445  +** This function is called when rebasing a local UPDATE change against one 
       185446  +** or more remote UPDATE changes. The aRec/nRec buffer contains the current
       185447  +** old.* and new.* records for the change. The rebase buffer (a single
       185448  +** record) is in aChange/nChange. The rebased change is appended to buffer
       185449  +** pBuf.
       185450  +**
       185451  +** Rebasing the UPDATE involves: 
       185452  +**
       185453  +**   * Removing any changes to fields for which the corresponding field
       185454  +**     in the rebase buffer is set to "replaced" (type 0xFF). If this
       185455  +**     means the UPDATE change updates no fields, nothing is appended
       185456  +**     to the output buffer.
       185457  +**
       185458  +**   * For each field modified by the local change for which the 
       185459  +**     corresponding field in the rebase buffer is not "undefined" (0x00)
       185460  +**     or "replaced" (0xFF), the old.* value is replaced by the value
       185461  +**     in the rebase buffer.
       185462  +*/
       185463  +static void sessionAppendPartialUpdate(
       185464  +  SessionBuffer *pBuf,            /* Append record here */
       185465  +  sqlite3_changeset_iter *pIter,  /* Iterator pointed at local change */
       185466  +  u8 *aRec, int nRec,             /* Local change */
       185467  +  u8 *aChange, int nChange,       /* Record to rebase against */
       185468  +  int *pRc                        /* IN/OUT: Return Code */
       185469  +){
       185470  +  sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
       185471  +  if( *pRc==SQLITE_OK ){
       185472  +    int bData = 0;
       185473  +    u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
       185474  +    int i;
       185475  +    u8 *a1 = aRec;
       185476  +    u8 *a2 = aChange;
       185477  +
       185478  +    *pOut++ = SQLITE_UPDATE;
       185479  +    *pOut++ = pIter->bIndirect;
       185480  +    for(i=0; i<pIter->nCol; i++){
       185481  +      int n1 = sessionSerialLen(a1);
       185482  +      int n2 = sessionSerialLen(a2);
       185483  +      if( pIter->abPK[i] || a2[0]==0 ){
       185484  +        if( !pIter->abPK[i] ) bData = 1;
       185485  +        memcpy(pOut, a1, n1);
       185486  +        pOut += n1;
       185487  +      }else if( a2[0]!=0xFF ){
       185488  +        bData = 1;
       185489  +        memcpy(pOut, a2, n2);
       185490  +        pOut += n2;
       185491  +      }else{
       185492  +        *pOut++ = '\0';
       185493  +      }
       185494  +      a1 += n1;
       185495  +      a2 += n2;
       185496  +    }
       185497  +    if( bData ){
       185498  +      a2 = aChange;
       185499  +      for(i=0; i<pIter->nCol; i++){
       185500  +        int n1 = sessionSerialLen(a1);
       185501  +        int n2 = sessionSerialLen(a2);
       185502  +        if( pIter->abPK[i] || a2[0]!=0xFF ){
       185503  +          memcpy(pOut, a1, n1);
       185504  +          pOut += n1;
       185505  +        }else{
       185506  +          *pOut++ = '\0';
       185507  +        }
       185508  +        a1 += n1;
       185509  +        a2 += n2;
       185510  +      }
       185511  +      pBuf->nBuf = (pOut - pBuf->aBuf);
       185512  +    }
       185513  +  }
       185514  +}
       185515  +
       185516  +/*
       185517  +** pIter is configured to iterate through a changeset. This function rebases 
       185518  +** that changeset according to the current configuration of the rebaser 
       185519  +** object passed as the first argument. If no error occurs and argument xOutput
       185520  +** is not NULL, then the changeset is returned to the caller by invoking
       185521  +** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
       185522  +** then (*ppOut) is set to point to a buffer containing the rebased changeset
       185523  +** before this function returns. In this case (*pnOut) is set to the size of
       185524  +** the buffer in bytes.  It is the responsibility of the caller to eventually
       185525  +** free the (*ppOut) buffer using sqlite3_free(). 
       185526  +**
       185527  +** If an error occurs, an SQLite error code is returned. If ppOut and
       185528  +** pnOut are not NULL, then the two output parameters are set to 0 before
       185529  +** returning.
       185530  +*/
       185531  +static int sessionRebase(
       185532  +  sqlite3_rebaser *p,             /* Rebaser hash table */
       185533  +  sqlite3_changeset_iter *pIter,  /* Input data */
       185534  +  int (*xOutput)(void *pOut, const void *pData, int nData),
       185535  +  void *pOut,                     /* Context for xOutput callback */
       185536  +  int *pnOut,                     /* OUT: Number of bytes in output changeset */
       185537  +  void **ppOut                    /* OUT: Inverse of pChangeset */
       185538  +){
       185539  +  int rc = SQLITE_OK;
       185540  +  u8 *aRec = 0;
       185541  +  int nRec = 0;
       185542  +  int bNew = 0;
       185543  +  SessionTable *pTab = 0;
       185544  +  SessionBuffer sOut = {0,0,0};
       185545  +
       185546  +  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
       185547  +    SessionChange *pChange = 0;
       185548  +    int bDone = 0;
       185549  +
       185550  +    if( bNew ){
       185551  +      const char *zTab = pIter->zTab;
       185552  +      for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
       185553  +        if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
       185554  +      }
       185555  +      bNew = 0;
       185556  +
       185557  +      /* A patchset may not be rebased */
       185558  +      if( pIter->bPatchset ){
       185559  +        rc = SQLITE_ERROR;
       185560  +      }
       185561  +
       185562  +      /* Append a table header to the output for this new table */
       185563  +      sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
       185564  +      sessionAppendVarint(&sOut, pIter->nCol, &rc);
       185565  +      sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
       185566  +      sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
       185567  +    }
       185568  +
       185569  +    if( pTab && rc==SQLITE_OK ){
       185570  +      int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
       185571  +
       185572  +      for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
       185573  +        if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
       185574  +          break;
       185575  +        }
       185576  +      }
       185577  +    }
       185578  +
       185579  +    if( pChange ){
       185580  +      assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
       185581  +      switch( pIter->op ){
       185582  +        case SQLITE_INSERT:
       185583  +          if( pChange->op==SQLITE_INSERT ){
       185584  +            bDone = 1;
       185585  +            if( pChange->bIndirect==0 ){
       185586  +              sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
       185587  +              sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185588  +              sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
       185589  +              sessionAppendBlob(&sOut, aRec, nRec, &rc);
       185590  +            }
       185591  +          }
       185592  +          break;
       185593  +
       185594  +        case SQLITE_UPDATE:
       185595  +          bDone = 1;
       185596  +          if( pChange->op==SQLITE_DELETE ){
       185597  +            if( pChange->bIndirect==0 ){
       185598  +              u8 *pCsr = aRec;
       185599  +              sessionSkipRecord(&pCsr, pIter->nCol);
       185600  +              sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
       185601  +              sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185602  +              sessionAppendRecordMerge(&sOut, pIter->nCol,
       185603  +                  pCsr, nRec-(pCsr-aRec), 
       185604  +                  pChange->aRecord, pChange->nRecord, &rc
       185605  +              );
       185606  +            }
       185607  +          }else{
       185608  +            sessionAppendPartialUpdate(&sOut, pIter,
       185609  +                aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
       185610  +            );
       185611  +          }
       185612  +          break;
       185613  +
       185614  +        default:
       185615  +          assert( pIter->op==SQLITE_DELETE );
       185616  +          bDone = 1;
       185617  +          if( pChange->op==SQLITE_INSERT ){
       185618  +            sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
       185619  +            sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185620  +            sessionAppendRecordMerge(&sOut, pIter->nCol,
       185621  +                pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
       185622  +            );
       185623  +          }
       185624  +          break;
       185625  +      }
       185626  +    }
       185627  +
       185628  +    if( bDone==0 ){
       185629  +      sessionAppendByte(&sOut, pIter->op, &rc);
       185630  +      sessionAppendByte(&sOut, pIter->bIndirect, &rc);
       185631  +      sessionAppendBlob(&sOut, aRec, nRec, &rc);
       185632  +    }
       185633  +    if( rc==SQLITE_OK && xOutput && sOut.nBuf>SESSIONS_STRM_CHUNK_SIZE ){
       185634  +      rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
       185635  +      sOut.nBuf = 0;
       185636  +    }
       185637  +    if( rc ) break;
       185638  +  }
       185639  +
       185640  +  if( rc!=SQLITE_OK ){
       185641  +    sqlite3_free(sOut.aBuf);
       185642  +    memset(&sOut, 0, sizeof(sOut));
       185643  +  }
       185644  +
       185645  +  if( rc==SQLITE_OK ){
       185646  +    if( xOutput ){
       185647  +      if( sOut.nBuf>0 ){
       185648  +        rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
       185649  +      }
       185650  +    }else{
       185651  +      *ppOut = (void*)sOut.aBuf;
       185652  +      *pnOut = sOut.nBuf;
       185653  +      sOut.aBuf = 0;
       185654  +    }
       185655  +  }
       185656  +  sqlite3_free(sOut.aBuf);
       185657  +  return rc;
       185658  +}
       185659  +
       185660  +/* 
       185661  +** Create a new rebaser object.
       185662  +*/
       185663  +SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
       185664  +  int rc = SQLITE_OK;
       185665  +  sqlite3_rebaser *pNew;
       185666  +
       185667  +  pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
       185668  +  if( pNew==0 ){
       185669  +    rc = SQLITE_NOMEM;
       185670  +  }else{
       185671  +    memset(pNew, 0, sizeof(sqlite3_rebaser));
       185672  +  }
       185673  +  *ppNew = pNew;
       185674  +  return rc;
       185675  +}
       185676  +
       185677  +/* 
       185678  +** Call this one or more times to configure a rebaser.
       185679  +*/
       185680  +SQLITE_API int sqlite3rebaser_configure(
       185681  +  sqlite3_rebaser *p, 
       185682  +  int nRebase, const void *pRebase
       185683  +){
       185684  +  sqlite3_changeset_iter *pIter = 0;   /* Iterator opened on pData/nData */
       185685  +  int rc;                              /* Return code */
       185686  +  rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
       185687  +  if( rc==SQLITE_OK ){
       185688  +    rc = sessionChangesetToHash(pIter, &p->grp, 1);
       185689  +  }
       185690  +  sqlite3changeset_finalize(pIter);
       185691  +  return rc;
       185692  +}
       185693  +
       185694  +/* 
       185695  +** Rebase a changeset according to current rebaser configuration 
       185696  +*/
       185697  +SQLITE_API int sqlite3rebaser_rebase(
       185698  +  sqlite3_rebaser *p,
       185699  +  int nIn, const void *pIn, 
       185700  +  int *pnOut, void **ppOut 
       185701  +){
       185702  +  sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */  
       185703  +  int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
       185704  +
       185705  +  if( rc==SQLITE_OK ){
       185706  +    rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
       185707  +    sqlite3changeset_finalize(pIter);
       185708  +  }
       185709  +
       185710  +  return rc;
       185711  +}
       185712  +
       185713  +/* 
       185714  +** Rebase a changeset according to current rebaser configuration 
       185715  +*/
       185716  +SQLITE_API int sqlite3rebaser_rebase_strm(
       185717  +  sqlite3_rebaser *p,
       185718  +  int (*xInput)(void *pIn, void *pData, int *pnData),
       185719  +  void *pIn,
       185720  +  int (*xOutput)(void *pOut, const void *pData, int nData),
       185721  +  void *pOut
       185722  +){
       185723  +  sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */  
       185724  +  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
       185725  +
       185726  +  if( rc==SQLITE_OK ){
       185727  +    rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
       185728  +    sqlite3changeset_finalize(pIter);
       185729  +  }
       185730  +
       185731  +  return rc;
       185732  +}
       185733  +
       185734  +/* 
       185735  +** Destroy a rebaser object 
       185736  +*/
       185737  +SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
       185738  +  if( p ){
       185739  +    sessionDeleteTable(p->grp.pList);
       185740  +    sqlite3_free(p);
       185741  +  }
       185742  +}
184715 185743   
184716 185744   #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
184717 185745   
184718 185746   /************** End of sqlite3session.c **************************************/
184719 185747   /************** Begin file json1.c *******************************************/
184720 185748   /*
184721 185749   ** 2015-08-12
................................................................................
204527 205555   static void fts5SourceIdFunc(
204528 205556     sqlite3_context *pCtx,          /* Function call context */
204529 205557     int nArg,                       /* Number of args */
204530 205558     sqlite3_value **apUnused        /* Function arguments */
204531 205559   ){
204532 205560     assert( nArg==0 );
204533 205561     UNUSED_PARAM2(nArg, apUnused);
204534         -  sqlite3_result_text(pCtx, "fts5: 2018-03-08 18:09:22 0f5a8666b8b479bb6e470590659c1775fb9b0d6a9eee931cb48f98651cd7ffcb", -1, SQLITE_TRANSIENT);
       205562  +  sqlite3_result_text(pCtx, "fts5: 2018-04-02 11:04:16 736b53f57f70b23172c30880186dce7ad9baa3b74e3838cae5847cffb98f5cd2", -1, SQLITE_TRANSIENT);
204535 205563   }
204536 205564   
204537 205565   static int fts5Init(sqlite3 *db){
204538 205566     static const sqlite3_module fts5Mod = {
204539 205567       /* iVersion      */ 2,
204540 205568       /* xCreate       */ fts5CreateMethod,
204541 205569       /* xConnect      */ fts5ConnectMethod,
................................................................................
208797 209825   #endif
208798 209826     return rc;
208799 209827   }
208800 209828   #endif /* SQLITE_CORE */
208801 209829   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
208802 209830   
208803 209831   /************** End of stmt.c ************************************************/
208804         -#if __LINE__!=208804
       209832  +#if __LINE__!=209832
208805 209833   #undef SQLITE_SOURCE_ID
208806         -#define SQLITE_SOURCE_ID      "2018-03-08 18:09:22 0f5a8666b8b479bb6e470590659c1775fb9b0d6a9eee931cb48f98651cd7alt2"
       209834  +#define SQLITE_SOURCE_ID      "2018-04-02 11:04:16 736b53f57f70b23172c30880186dce7ad9baa3b74e3838cae5847cffb98falt2"
208807 209835   #endif
208808 209836   /* Return the source-id for this library */
208809 209837   SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
208810 209838   /************************** End of sqlite3.c ******************************/

Changes to SQLite.Interop/src/core/sqlite3.h.

   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    126   #define SQLITE_VERSION        "3.23.0"
   127    127   #define SQLITE_VERSION_NUMBER 3023000
   128         -#define SQLITE_SOURCE_ID      "2018-03-08 18:09:22 0f5a8666b8b479bb6e470590659c1775fb9b0d6a9eee931cb48f98651cd7ffcb"
          128  +#define SQLITE_SOURCE_ID      "2018-04-02 11:04:16 736b53f57f70b23172c30880186dce7ad9baa3b74e3838cae5847cffb98f5cd2"
   129    129   
   130    130   /*
   131    131   ** CAPI3REF: Run-Time Library Version Numbers
   132    132   ** KEYWORDS: sqlite3_version sqlite3_sourceid
   133    133   **
   134    134   ** These interfaces provide the same information as the [SQLITE_VERSION],
   135    135   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1060   1060   ** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
  1061   1061   ** operations since the previous successful call to 
  1062   1062   ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
  1063   1063   ** ^This file control takes the file descriptor out of batch write mode
  1064   1064   ** so that all subsequent write operations are independent.
  1065   1065   ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
  1066   1066   ** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
         1067  +**
         1068  +** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
         1069  +** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
         1070  +** a file lock using the xLock or xShmLock methods of the VFS to wait
         1071  +** for up to M milliseconds before failing, where M is the single 
         1072  +** unsigned integer parameter.
  1067   1073   ** </ul>
  1068   1074   */
  1069   1075   #define SQLITE_FCNTL_LOCKSTATE               1
  1070   1076   #define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
  1071   1077   #define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
  1072   1078   #define SQLITE_FCNTL_LAST_ERRNO              4
  1073   1079   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
  1094   1100   #define SQLITE_FCNTL_VFS_POINTER            27
  1095   1101   #define SQLITE_FCNTL_JOURNAL_POINTER        28
  1096   1102   #define SQLITE_FCNTL_WIN32_GET_HANDLE       29
  1097   1103   #define SQLITE_FCNTL_PDB                    30
  1098   1104   #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
  1099   1105   #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
  1100   1106   #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
         1107  +#define SQLITE_FCNTL_LOCK_TIMEOUT           34
  1101   1108   
  1102   1109   /* deprecated names */
  1103   1110   #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
  1104   1111   #define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
  1105   1112   #define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
  1106   1113   
  1107   1114   
................................................................................
  2050   2057   **
  2051   2058   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  2052   2059   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  2053   2060   ** database handle, SQLite checks if this will mean that there are now no 
  2054   2061   ** connections at all to the database. If so, it performs a checkpoint 
  2055   2062   ** operation before closing the connection. This option may be used to
  2056   2063   ** override this behaviour. The first parameter passed to this operation
  2057         -** is an integer - non-zero to disable checkpoints-on-close, or zero (the
  2058         -** default) to enable them. The second parameter is a pointer to an integer
         2064  +** is an integer - positive to disable checkpoints-on-close, or zero (the
         2065  +** default) to enable them, and negative to leave the setting unchanged.
         2066  +** The second parameter is a pointer to an integer
  2059   2067   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  2060   2068   ** have been disabled - 0 if they are not disabled, 1 if they are.
  2061   2069   ** </dd>
         2070  +**
  2062   2071   ** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  2063   2072   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  2064   2073   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  2065   2074   ** a single SQL query statement will always use the same algorithm regardless
  2066   2075   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  2067   2076   ** that look at the values of bound parameters, which can make some queries
  2068   2077   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  2069   2078   ** the QPSG active, SQLite will always use the same query plan in the field as
  2070   2079   ** was used during testing in the lab.
         2080  +** The first argument to this setting is an integer which is 0 to disable 
         2081  +** the QPSG, positive to enable QPSG, or negative to leave the setting
         2082  +** unchanged. The second parameter is a pointer to an integer into which
         2083  +** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
         2084  +** following this call.
  2071   2085   ** </dd>
         2086  +**
  2072   2087   ** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  2073   2088   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  2074   2089   ** include output for any operations performed by trigger programs. This
  2075   2090   ** option is used to set or clear (the default) a flag that governs this
  2076   2091   ** behavior. The first parameter passed to this operation is an integer -
  2077         -** non-zero to enable output for trigger programs, or zero to disable it.
         2092  +** positive to enable output for trigger programs, or zero to disable it,
         2093  +** or negative to leave the setting unchanged.
  2078   2094   ** The second parameter is a pointer to an integer into which is written 
  2079   2095   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  2080   2096   ** it is not disabled, 1 if it is.  
  2081   2097   ** </dd>
  2082   2098   ** </dl>
  2083   2099   */
  2084   2100   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
................................................................................
  7225   7241   ** wal file in wal mode databases, or the number of pages written to the
  7226   7242   ** database file in rollback mode databases. Any pages written as part of
  7227   7243   ** transaction rollback or database recovery operations are not included.
  7228   7244   ** If an IO or other error occurs while writing a page to disk, the effect
  7229   7245   ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  7230   7246   ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  7231   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>
  7232   7257   **
  7233   7258   ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  7234   7259   ** <dd>This parameter returns zero for the current value if and only if
  7235   7260   ** all foreign key constraints (deferred or immediate) have been
  7236   7261   ** resolved.)^  ^The highwater mark is always 0.
  7237   7262   ** </dd>
  7238   7263   ** </dl>
................................................................................
  7245   7270   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  7246   7271   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  7247   7272   #define SQLITE_DBSTATUS_CACHE_HIT            7
  7248   7273   #define SQLITE_DBSTATUS_CACHE_MISS           8
  7249   7274   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  7250   7275   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  7251   7276   #define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
  7252         -#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
         7277  +#define SQLITE_DBSTATUS_CACHE_SPILL         12
         7278  +#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
  7253   7279   
  7254   7280   
  7255   7281   /*
  7256   7282   ** CAPI3REF: Prepared Statement Status
  7257   7283   ** METHOD: sqlite3_stmt
  7258   7284   **
  7259   7285   ** ^(Each prepared statement maintains various
................................................................................
  8788   8814   */
  8789   8815   #define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
  8790   8816   
  8791   8817   /*
  8792   8818   ** CAPI3REF: Deserialize a database
  8793   8819   **
  8794   8820   ** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the 
  8795         -** [database connection] D to disconnection from database S and then
         8821  +** [database connection] D to disconnect from database S and then
  8796   8822   ** reopen S as an in-memory database based on the serialization contained
  8797   8823   ** in P.  The serialized database P is N bytes in size.  M is the size of
  8798   8824   ** the buffer P, which might be larger than N.  If M is larger than N, and
  8799   8825   ** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
  8800   8826   ** permitted to add content to the in-memory database as long as the total
  8801   8827   ** size does not exceed M bytes.
  8802   8828   **
................................................................................
  8835   8861   ** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
  8836   8862   ** in the P argument is held in memory obtained from [sqlite3_malloc64()]
  8837   8863   ** and that SQLite should take ownership of this memory and automatically
  8838   8864   ** free it when it has finished using it.  Without this flag, the caller
  8839   8865   ** is resposible for freeing any dynamically allocated memory.
  8840   8866   **
  8841   8867   ** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
  8842         -** grow the size of the database usign calls to [sqlite3_realloc64()].  This
         8868  +** grow the size of the database using calls to [sqlite3_realloc64()].  This
  8843   8869   ** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
  8844   8870   ** Without this flag, the deserialized database cannot increase in size beyond
  8845   8871   ** the number of bytes specified by the M parameter.
  8846   8872   **
  8847   8873   ** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
  8848   8874   ** should be treated as read-only.
  8849   8875   */
................................................................................
  9929   9955   ** DESTRUCTOR: sqlite3_changegroup
  9930   9956   */
  9931   9957   SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
  9932   9958   
  9933   9959   /*
  9934   9960   ** CAPI3REF: Apply A Changeset To A Database
  9935   9961   **
  9936         -** Apply a changeset to a database. This function attempts to update the
  9937         -** "main" database attached to handle db with the changes found in the
  9938         -** 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. 
  9939   9965   **
  9940         -** The fourth argument (xFilter) passed to this function is the "filter
         9966  +** The fourth argument (xFilter) passed to these functions is the "filter
  9941   9967   ** callback". If it is not NULL, then for each table affected by at least one
  9942   9968   ** change in the changeset, the filter callback is invoked with
  9943   9969   ** the table name as the second argument, and a copy of the context pointer
  9944         -** passed as the sixth argument to this function as the first. If the "filter
  9945         -** callback" returns zero, then no attempt is made to apply any changes to 
  9946         -** the table. Otherwise, if the return value is non-zero or the xFilter
  9947         -** argument to this function is NULL, all changes related to the table are
  9948         -** 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.
  9949   9974   **
  9950   9975   ** For each table that is not excluded by the filter callback, this function 
  9951   9976   ** tests that the target database contains a compatible table. A table is 
  9952   9977   ** considered compatible if all of the following are true:
  9953   9978   **
  9954   9979   ** <ul>
  9955   9980   **   <li> The table has the same name as the name recorded in the 
................................................................................
  9986  10011   ** actions are taken by sqlite3changeset_apply() depending on the value
  9987  10012   ** returned by each invocation of the conflict-handler function. Refer to
  9988  10013   ** the documentation for the three 
  9989  10014   ** [SQLITE_CHANGESET_OMIT|available return values] for details.
  9990  10015   **
  9991  10016   ** <dl>
  9992  10017   ** <dt>DELETE Changes<dd>
  9993         -**   For each DELETE change, this function checks if the target database 
        10018  +**   For each DELETE change, the function checks if the target database 
  9994  10019   **   contains a row with the same primary key value (or values) as the 
  9995  10020   **   original row values stored in the changeset. If it does, and the values 
  9996  10021   **   stored in all non-primary key columns also match the values stored in 
  9997  10022   **   the changeset the row is deleted from the target database.
  9998  10023   **
  9999  10024   **   If a row with matching primary key values is found, but one or more of
 10000  10025   **   the non-primary key fields contains a value different from the original
................................................................................
 10031  10056   **   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 
 10032  10057   **   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
 10033  10058   **   This includes the case where the INSERT operation is re-attempted because 
 10034  10059   **   an earlier call to the conflict handler function returned 
 10035  10060   **   [SQLITE_CHANGESET_REPLACE].
 10036  10061   **
 10037  10062   ** <dt>UPDATE Changes<dd>
 10038         -**   For each UPDATE change, this function checks if the target database 
        10063  +**   For each UPDATE change, the function checks if the target database 
 10039  10064   **   contains a row with the same primary key value (or values) as the 
 10040  10065   **   original row values stored in the changeset. If it does, and the values 
 10041  10066   **   stored in all modified non-primary key columns also match the values
 10042  10067   **   stored in the changeset the row is updated within the target database.
 10043  10068   **
 10044  10069   **   If a row with matching primary key values is found, but one or more of
 10045  10070   **   the modified non-primary key fields contains a value different from an
................................................................................
 10062  10087   ** </dl>
 10063  10088   **
 10064  10089   ** It is safe to execute SQL statements, including those that write to the
 10065  10090   ** table that the callback related to, from within the xConflict callback.
 10066  10091   ** This can be used to further customize the applications conflict
 10067  10092   ** resolution strategy.
 10068  10093   **
 10069         -** 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.
 10070  10095   ** If any other error (aside from a constraint failure when attempting to
 10071  10096   ** write to the target database) occurs, then the savepoint transaction is
 10072  10097   ** rolled back, restoring the target database to its original state, and an 
 10073  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.
 10074  10109   */
 10075  10110   SQLITE_API int sqlite3changeset_apply(
 10076  10111     sqlite3 *db,                    /* Apply change to "main" db of this handle */
 10077  10112     int nChangeset,                 /* Size of changeset in bytes */
 10078  10113     void *pChangeset,               /* Changeset blob */
 10079  10114     int(*xFilter)(
 10080  10115       void *pCtx,                   /* Copy of sixth arg to _apply() */
................................................................................
 10082  10117     ),
 10083  10118     int(*xConflict)(
 10084  10119       void *pCtx,                   /* Copy of sixth arg to _apply() */
 10085  10120       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 10086  10121       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 10087  10122     ),
 10088  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
 10089  10140   );
 10090  10141   
 10091  10142   /* 
 10092  10143   ** CAPI3REF: Constants Passed To The Conflict Handler
 10093  10144   **
 10094  10145   ** Values that may be passed as the second argument to a conflict-handler.
 10095  10146   **
................................................................................
 10180  10231   **   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
 10181  10232   ** </dl>
 10182  10233   */
 10183  10234   #define SQLITE_CHANGESET_OMIT       0
 10184  10235   #define SQLITE_CHANGESET_REPLACE    1
 10185  10236   #define SQLITE_CHANGESET_ABORT      2
 10186  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  +
 10187  10393   /*
 10188  10394   ** CAPI3REF: Streaming Versions of API functions.
 10189  10395   **
 10190  10396   ** The six streaming API xxx_strm() functions serve similar purposes to the 
 10191  10397   ** corresponding non-streaming API functions:
 10192  10398   **
 10193  10399   ** <table border=1 style="margin-left:8ex;margin-right:8ex">
................................................................................
 10283  10489     ),
 10284  10490     int(*xConflict)(
 10285  10491       void *pCtx,                   /* Copy of sixth arg to _apply() */
 10286  10492       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 10287  10493       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 10288  10494     ),
 10289  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
 10290  10512   );
 10291  10513   SQLITE_API int sqlite3changeset_concat_strm(
 10292  10514     int (*xInputA)(void *pIn, void *pData, int *pnData),
 10293  10515     void *pInA,
 10294  10516     int (*xInputB)(void *pIn, void *pData, int *pnData),
 10295  10517     void *pInB,
 10296  10518     int (*xOutput)(void *pOut, const void *pData, int nData),
................................................................................
 10320  10542   SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
 10321  10543       int (*xInput)(void *pIn, void *pData, int *pnData),
 10322  10544       void *pIn
 10323  10545   );
 10324  10546   SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
 10325  10547       int (*xOutput)(void *pOut, const void *pData, int nData), 
 10326  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
 10327  10556   );
 10328  10557   
 10329  10558   
 10330  10559   /*
 10331  10560   ** Make sure we can call this stuff from C++.
 10332  10561   */
 10333  10562   #ifdef __cplusplus

Changes to SQLite.Interop/src/ext/fts5.c.

 17459  17459   static void fts5SourceIdFunc(
 17460  17460     sqlite3_context *pCtx,          /* Function call context */
 17461  17461     int nArg,                       /* Number of args */
 17462  17462     sqlite3_value **apUnused        /* Function arguments */
 17463  17463   ){
 17464  17464     assert( nArg==0 );
 17465  17465     UNUSED_PARAM2(nArg, apUnused);
 17466         -  sqlite3_result_text(pCtx, "fts5: 2018-03-08 18:09:22 0f5a8666b8b479bb6e470590659c1775fb9b0d6a9eee931cb48f98651cd7ffcb", -1, SQLITE_TRANSIENT);
        17466  +  sqlite3_result_text(pCtx, "fts5: 2018-04-02 11:04:16 736b53f57f70b23172c30880186dce7ad9baa3b74e3838cae5847cffb98f5cd2", -1, SQLITE_TRANSIENT);
 17467  17467   }
 17468  17468   
 17469  17469   static int fts5Init(sqlite3 *db){
 17470  17470     static const sqlite3_module fts5Mod = {
 17471  17471       /* iVersion      */ 2,
 17472  17472       /* xCreate       */ fts5CreateMethod,
 17473  17473       /* xConnect      */ fts5ConnectMethod,

Changes to readme.htm.

     2      2   <html>
     3      3   <head>
     4      4   <title></title>
     5      5   </head>
     6      6   <body>
     7      7   ADO.NET SQLite Data Provider<br />
     8      8   Version 1.0.109.0 - May XX, 2018 <font color="red">(release scheduled)</font><br />
     9         -Using <a href="https://www.sqlite.org/draft/releaselog/3_23_0.html">SQLite 3.23.0</a><br />Originally written by Robert Simpson<br />
            9  +Using <a href="https://www.sqlite.org/releaselog/3_23_0.html">SQLite 3.23.0</a><br />Originally written by Robert Simpson<br />
    10     10   Released to the public domain, use at your own risk!<br />
    11     11   Official provider website:&nbsp;<a href="https://system.data.sqlite.org/">https://system.data.sqlite.org/</a><br />
    12     12   Legacy versions:&nbsp;<a href="https://sourceforge.net/projects/sqlite-dotnet2/">https://sourceforge.net/projects/sqlite-dotnet2/</a><br />
    13     13   <br />
    14     14   The current development version can be downloaded from <a href="https://system.data.sqlite.org/index.html/timeline?y=ci">
    15     15   https://system.data.sqlite.org/index.html/timeline?y=ci</a>
    16     16   <br />
................................................................................
   207    207   
   208    208   <h2><b>Version History</b></h2>
   209    209   
   210    210   <p>
   211    211       <b>1.0.109.0 - May XX, 2018 <font color="red">(release scheduled)</font></b>
   212    212   </p>
   213    213   <ul>
   214         -    <li>Updated to <a href="https://www.sqlite.org/draft/releaselog/3_23_0.html">SQLite 3.23.0</a>.</li>
          214  +    <li>Updated to <a href="https://www.sqlite.org/releaselog/3_23_0.html">SQLite 3.23.0</a>.</li>
   215    215       <li>Prevent GetSchemaTable from throwing InvalidCastException. Fix for [baf42ee135].</li>
   216    216   </ul>
   217    217   <p>
   218    218       <b>1.0.XXX.0 - March 2, 2018</b>
   219    219   </p>
   220    220   <ul>
   221    221       <li>Support extended result codes when messages are looked up without the SQLite core library.</li>

Changes to www/news.wiki.

    44     44   
    45     45   <div align="center"><h2><b>Version History</b></h2></div>
    46     46   
    47     47   <p>
    48     48       <b>1.0.109.0 - May XX, 2018 <font color="red">(release scheduled)</font></b>
    49     49   </p>
    50     50   <ul>
    51         -    <li>Updated to [https://www.sqlite.org/draft/releaselog/3_23_0.html|SQLite 3.23.0].</li>
           51  +    <li>Updated to [https://www.sqlite.org/releaselog/3_23_0.html|SQLite 3.23.0].</li>
    52     52       <li>Prevent GetSchemaTable from throwing InvalidCastException. Fix for [baf42ee135].</li>
    53     53   </ul>
    54     54   <p>
    55     55       <b>1.0.108.0 - March 2, 2018</b>
    56     56   </p>
    57     57   <ul>
    58     58       <li>Support extended result codes when messages are looked up without the SQLite core library.</li>