System.Data.SQLite
Check-in [86b43d35ac]
Not logged in

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

Overview
Comment:Update SQLite core library to the latest trunk code.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 86b43d35acadf79ebe26fd18cc901f9ac65129d0
User & Date: mistachkin 2017-02-09 17:43:57
Original Comment: Update SQLite core librar to the latest trunk code.
Context
2017-02-10
21:34
Add 'Community Support Policies' information to the news page. check-in: f371f13c73 user: mistachkin tags: trunk
2017-02-09
17:43
Update SQLite core library to the latest trunk code. check-in: 86b43d35ac user: mistachkin tags: trunk
2017-02-08
01:08
Improve consistency of wording on the landing page regarding legacy versions. check-in: f2c11b3b87 user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

   200    200   # define _LARGE_FILE       1
   201    201   # ifndef _FILE_OFFSET_BITS
   202    202   #   define _FILE_OFFSET_BITS 64
   203    203   # endif
   204    204   # define _LARGEFILE_SOURCE 1
   205    205   #endif
   206    206   
   207         -/* What version of GCC is being used.  0 means GCC is not being used */
   208         -#ifdef __GNUC__
          207  +/* The GCC_VERSION, CLANG_VERSION, and MSVC_VERSION macros are used to
          208  +** conditionally include optimizations for each of these compilers.  A
          209  +** value of 0 means that compiler is not being used.  The
          210  +** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
          211  +** optimizations, and hence set all compiler macros to 0
          212  +*/
          213  +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
   209    214   # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
   210    215   #else
   211    216   # define GCC_VERSION 0
   212    217   #endif
   213         -
   214         -/* What version of CLANG is being used.  0 means CLANG is not being used */
   215         -#if defined(__clang__) && !defined(_WIN32)
          218  +#if defined(__clang__) && !defined(_WIN32) && !defined(SQLITE_DISABLE_INTRINSIC)
   216    219   # define CLANG_VERSION \
   217    220               (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
   218    221   #else
   219    222   # define CLANG_VERSION 0
          223  +#endif
          224  +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
          225  +# define MSVC_VERSION _MSC_VER
          226  +#else
          227  +# define MSVC_VERSION 0
   220    228   #endif
   221    229   
   222    230   /* Needed for various definitions... */
   223    231   #if defined(__GNUC__) && !defined(_GNU_SOURCE)
   224    232   # define _GNU_SOURCE
   225    233   #endif
   226    234   
................................................................................
   387    395   **
   388    396   ** See also: [sqlite3_libversion()],
   389    397   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   390    398   ** [sqlite_version()] and [sqlite_source_id()].
   391    399   */
   392    400   #define SQLITE_VERSION        "3.17.0"
   393    401   #define SQLITE_VERSION_NUMBER 3017000
   394         -#define SQLITE_SOURCE_ID      "2017-01-19 18:20:36 ffd559afd32dcdce9c733ebccdee88fda9b689cf"
          402  +#define SQLITE_SOURCE_ID      "2017-02-09 17:12:22 798fb9d70d2e5f95e64237b04d6692360133381a"
   395    403   
   396    404   /*
   397    405   ** CAPI3REF: Run-Time Library Version Numbers
   398    406   ** KEYWORDS: sqlite3_version sqlite3_sourceid
   399    407   **
   400    408   ** These interfaces provide the same information as the [SQLITE_VERSION],
   401    409   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
   840    848   ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
   841    849   ** information is written to disk in the same order as calls
   842    850   ** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
   843    851   ** after reboot following a crash or power loss, the only bytes in a
   844    852   ** file that were written at the application level might have changed
   845    853   ** and that adjacent bytes, even bytes within the same sector are
   846    854   ** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
   847         -** flag indicate that a file cannot be deleted when open.  The
          855  +** flag indicates that a file cannot be deleted when open.  The
   848    856   ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
   849    857   ** read-only media and cannot be changed even by processes with
   850    858   ** elevated privileges.
   851    859   */
   852    860   #define SQLITE_IOCAP_ATOMIC                 0x00000001
   853    861   #define SQLITE_IOCAP_ATOMIC512              0x00000002
   854    862   #define SQLITE_IOCAP_ATOMIC1K               0x00000004
................................................................................
   990    998   ** <li> [SQLITE_IOCAP_ATOMIC4K]
   991    999   ** <li> [SQLITE_IOCAP_ATOMIC8K]
   992   1000   ** <li> [SQLITE_IOCAP_ATOMIC16K]
   993   1001   ** <li> [SQLITE_IOCAP_ATOMIC32K]
   994   1002   ** <li> [SQLITE_IOCAP_ATOMIC64K]
   995   1003   ** <li> [SQLITE_IOCAP_SAFE_APPEND]
   996   1004   ** <li> [SQLITE_IOCAP_SEQUENTIAL]
         1005  +** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
         1006  +** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
         1007  +** <li> [SQLITE_IOCAP_IMMUTABLE]
   997   1008   ** </ul>
   998   1009   **
   999   1010   ** The SQLITE_IOCAP_ATOMIC property means that all writes of
  1000   1011   ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
  1001   1012   ** mean that writes of blocks that are nnn bytes in size and
  1002   1013   ** are aligned to an address which is an integer multiple of
  1003   1014   ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
................................................................................
  5678   5689   ** ^In the case of an update, this is the [rowid] after the update takes place.
  5679   5690   **
  5680   5691   ** ^(The update hook is not invoked when internal system tables are
  5681   5692   ** modified (i.e. sqlite_master and sqlite_sequence).)^
  5682   5693   ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
  5683   5694   **
  5684   5695   ** ^In the current implementation, the update hook
  5685         -** is not invoked when duplication rows are deleted because of an
         5696  +** is not invoked when conflicting rows are deleted because of an
  5686   5697   ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
  5687   5698   ** invoked when rows are deleted using the [truncate optimization].
  5688   5699   ** The exceptions defined in this paragraph might change in a future
  5689   5700   ** release of SQLite.
  5690   5701   **
  5691   5702   ** The update hook implementation must not do anything that will modify
  5692   5703   ** the database connection that invoked the update hook.  Any actions
................................................................................
  6460   6471   **         being opened for read/write access)^.
  6461   6472   ** </ul>
  6462   6473   **
  6463   6474   ** ^Unless it returns SQLITE_MISUSE, this function sets the 
  6464   6475   ** [database connection] error code and message accessible via 
  6465   6476   ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. 
  6466   6477   **
         6478  +** A BLOB referenced by sqlite3_blob_open() may be read using the
         6479  +** [sqlite3_blob_read()] interface and modified by using
         6480  +** [sqlite3_blob_write()].  The [BLOB handle] can be moved to a
         6481  +** different row of the same table using the [sqlite3_blob_reopen()]
         6482  +** interface.  However, the column, table, or database of a [BLOB handle]
         6483  +** cannot be changed after the [BLOB handle] is opened.
  6467   6484   **
  6468   6485   ** ^(If the row that a BLOB handle points to is modified by an
  6469   6486   ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
  6470   6487   ** then the BLOB handle is marked as "expired".
  6471   6488   ** This is true if any column of the row is changed, even a column
  6472   6489   ** other than the one the BLOB handle is open on.)^
  6473   6490   ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
................................................................................
  6483   6500   **
  6484   6501   ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
  6485   6502   ** and the built-in [zeroblob] SQL function may be used to create a 
  6486   6503   ** zero-filled blob to read or write using the incremental-blob interface.
  6487   6504   **
  6488   6505   ** To avoid a resource leak, every open [BLOB handle] should eventually
  6489   6506   ** be released by a call to [sqlite3_blob_close()].
         6507  +**
         6508  +** See also: [sqlite3_blob_close()],
         6509  +** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
         6510  +** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
  6490   6511   */
  6491   6512   SQLITE_API int sqlite3_blob_open(
  6492   6513     sqlite3*,
  6493   6514     const char *zDb,
  6494   6515     const char *zTable,
  6495   6516     const char *zColumn,
  6496   6517     sqlite3_int64 iRow,
................................................................................
  6498   6519     sqlite3_blob **ppBlob
  6499   6520   );
  6500   6521   
  6501   6522   /*
  6502   6523   ** CAPI3REF: Move a BLOB Handle to a New Row
  6503   6524   ** METHOD: sqlite3_blob
  6504   6525   **
  6505         -** ^This function is used to move an existing blob handle so that it points
         6526  +** ^This function is used to move an existing [BLOB handle] so that it points
  6506   6527   ** to a different row of the same database table. ^The new row is identified
  6507   6528   ** by the rowid value passed as the second argument. Only the row can be
  6508   6529   ** changed. ^The database, table and column on which the blob handle is open
  6509         -** remain the same. Moving an existing blob handle to a new row can be
         6530  +** remain the same. Moving an existing [BLOB handle] to a new row is
  6510   6531   ** faster than closing the existing handle and opening a new one.
  6511   6532   **
  6512   6533   ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
  6513   6534   ** it must exist and there must be either a blob or text value stored in
  6514   6535   ** the nominated column.)^ ^If the new row is not present in the table, or if
  6515   6536   ** it does not contain a blob or text value, or if another error occurs, an
  6516   6537   ** SQLite error code is returned and the blob handle is considered aborted.
................................................................................
  8431   8452   ** CAPI3REF: The pre-update hook.
  8432   8453   **
  8433   8454   ** ^These interfaces are only available if SQLite is compiled using the
  8434   8455   ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
  8435   8456   **
  8436   8457   ** ^The [sqlite3_preupdate_hook()] interface registers a callback function
  8437   8458   ** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
  8438         -** on a [rowid table].
         8459  +** on a database table.
  8439   8460   ** ^At most one preupdate hook may be registered at a time on a single
  8440   8461   ** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
  8441   8462   ** the previous setting.
  8442   8463   ** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
  8443   8464   ** with a NULL pointer as the second parameter.
  8444   8465   ** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
  8445   8466   ** the first parameter to callbacks.
  8446   8467   **
  8447         -** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate
  8448         -** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID]
  8449         -** tables.
         8468  +** ^The preupdate hook only fires for changes to real database tables; the
         8469  +** preupdate hook is not invoked for changes to [virtual tables] or to
         8470  +** system tables like sqlite_master or sqlite_stat1.
  8450   8471   **
  8451   8472   ** ^The second parameter to the preupdate callback is a pointer to
  8452   8473   ** the [database connection] that registered the preupdate hook.
  8453   8474   ** ^The third parameter to the preupdate callback is one of the constants
  8454   8475   ** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
  8455   8476   ** kind of update operation that is about to occur.
  8456   8477   ** ^(The fourth parameter to the preupdate callback is the name of the
  8457   8478   ** database within the database connection that is being modified.  This
  8458   8479   ** will be "main" for the main database or "temp" for TEMP tables or 
  8459   8480   ** the name given after the AS keyword in the [ATTACH] statement for attached
  8460   8481   ** databases.)^
  8461   8482   ** ^The fifth parameter to the preupdate callback is the name of the
  8462   8483   ** table that is being modified.
  8463         -** ^The sixth parameter to the preupdate callback is the initial [rowid] of the
  8464         -** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is
  8465         -** undefined for SQLITE_INSERT changes.
  8466         -** ^The seventh parameter to the preupdate callback is the final [rowid] of
  8467         -** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is
  8468         -** undefined for SQLITE_DELETE changes.
         8484  +**
         8485  +** For an UPDATE or DELETE operation on a [rowid table], the sixth
         8486  +** parameter passed to the preupdate callback is the initial [rowid] of the 
         8487  +** row being modified or deleted. For an INSERT operation on a rowid table,
         8488  +** or any operation on a WITHOUT ROWID table, the value of the sixth 
         8489  +** parameter is undefined. For an INSERT or UPDATE on a rowid table the
         8490  +** seventh parameter is the final rowid value of the row being inserted
         8491  +** or updated. The value of the seventh parameter passed to the callback
         8492  +** function is not defined for operations on WITHOUT ROWID tables, or for
         8493  +** INSERT operations on rowid tables.
  8469   8494   **
  8470   8495   ** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
  8471   8496   ** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
  8472   8497   ** provide additional information about a preupdate event. These routines
  8473   8498   ** may only be called from within a preupdate callback.  Invoking any of
  8474   8499   ** these routines from outside of a preupdate callback or with a
  8475   8500   ** [database connection] pointer that is different from the one supplied
................................................................................
  8897   8922   ** either of these things are undefined.
  8898   8923   **
  8899   8924   ** The session object will be used to create changesets for tables in
  8900   8925   ** database zDb, where zDb is either "main", or "temp", or the name of an
  8901   8926   ** attached database. It is not an error if database zDb is not attached
  8902   8927   ** to the database when the session object is created.
  8903   8928   */
  8904         -int sqlite3session_create(
         8929  +SQLITE_API int sqlite3session_create(
  8905   8930     sqlite3 *db,                    /* Database handle */
  8906   8931     const char *zDb,                /* Name of db (e.g. "main") */
  8907   8932     sqlite3_session **ppSession     /* OUT: New session object */
  8908   8933   );
  8909   8934   
  8910   8935   /*
  8911   8936   ** CAPI3REF: Delete A Session Object
................................................................................
  8915   8940   ** results of attempting to use pSession with any other session module
  8916   8941   ** function are undefined.
  8917   8942   **
  8918   8943   ** Session objects must be deleted before the database handle to which they
  8919   8944   ** are attached is closed. Refer to the documentation for 
  8920   8945   ** [sqlite3session_create()] for details.
  8921   8946   */
  8922         -void sqlite3session_delete(sqlite3_session *pSession);
         8947  +SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
  8923   8948   
  8924   8949   
  8925   8950   /*
  8926   8951   ** CAPI3REF: Enable Or Disable A Session Object
  8927   8952   **
  8928   8953   ** Enable or disable the recording of changes by a session object. When
  8929   8954   ** enabled, a session object records changes made to the database. When
................................................................................
  8935   8960   ** Passing zero to this function disables the session. Passing a value
  8936   8961   ** greater than zero enables it. Passing a value less than zero is a 
  8937   8962   ** no-op, and may be used to query the current state of the session.
  8938   8963   **
  8939   8964   ** The return value indicates the final state of the session object: 0 if 
  8940   8965   ** the session is disabled, or 1 if it is enabled.
  8941   8966   */
  8942         -int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
         8967  +SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
  8943   8968   
  8944   8969   /*
  8945   8970   ** CAPI3REF: Set Or Clear the Indirect Change Flag
  8946   8971   **
  8947   8972   ** Each change recorded by a session object is marked as either direct or
  8948   8973   ** indirect. A change is marked as indirect if either:
  8949   8974   **
................................................................................
  8964   8989   ** is set. Passing a value less than zero does not modify the current value
  8965   8990   ** of the indirect flag, and may be used to query the current state of the 
  8966   8991   ** indirect flag for the specified session object.
  8967   8992   **
  8968   8993   ** The return value indicates the final state of the indirect flag: 0 if 
  8969   8994   ** it is clear, or 1 if it is set.
  8970   8995   */
  8971         -int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
         8996  +SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
  8972   8997   
  8973   8998   /*
  8974   8999   ** CAPI3REF: Attach A Table To A Session Object
  8975   9000   **
  8976   9001   ** If argument zTab is not NULL, then it is the name of a table to attach
  8977   9002   ** to the session object passed as the first argument. All subsequent changes 
  8978   9003   ** made to the table while the session object is enabled will be recorded. See 
................................................................................
  8994   9019   **
  8995   9020   ** Changes are not recorded for individual rows that have NULL values stored
  8996   9021   ** in one or more of their PRIMARY KEY columns.
  8997   9022   **
  8998   9023   ** SQLITE_OK is returned if the call completes without error. Or, if an error 
  8999   9024   ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
  9000   9025   */
  9001         -int sqlite3session_attach(
         9026  +SQLITE_API int sqlite3session_attach(
  9002   9027     sqlite3_session *pSession,      /* Session object */
  9003   9028     const char *zTab                /* Table name */
  9004   9029   );
  9005   9030   
  9006   9031   /*
  9007   9032   ** CAPI3REF: Set a table filter on a Session Object.
  9008   9033   **
  9009   9034   ** The second argument (xFilter) is the "filter callback". For changes to rows 
  9010   9035   ** in tables that are not attached to the Session object, the filter is called
  9011   9036   ** to determine whether changes to the table's rows should be tracked or not. 
  9012   9037   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
  9013   9038   ** attached, xFilter will not be called again.
  9014   9039   */
  9015         -void sqlite3session_table_filter(
         9040  +SQLITE_API void sqlite3session_table_filter(
  9016   9041     sqlite3_session *pSession,      /* Session object */
  9017   9042     int(*xFilter)(
  9018   9043       void *pCtx,                   /* Copy of third arg to _filter_table() */
  9019   9044       const char *zTab              /* Table name */
  9020   9045     ),
  9021   9046     void *pCtx                      /* First argument passed to xFilter */
  9022   9047   );
................................................................................
  9121   9146   ** is inserted while a session object is enabled, then later deleted while 
  9122   9147   ** the same session object is disabled, no INSERT record will appear in the
  9123   9148   ** changeset, even though the delete took place while the session was disabled.
  9124   9149   ** Or, if one field of a row is updated while a session is disabled, and 
  9125   9150   ** another field of the same row is updated while the session is enabled, the
  9126   9151   ** resulting changeset will contain an UPDATE change that updates both fields.
  9127   9152   */
  9128         -int sqlite3session_changeset(
         9153  +SQLITE_API int sqlite3session_changeset(
  9129   9154     sqlite3_session *pSession,      /* Session object */
  9130   9155     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
  9131   9156     void **ppChangeset              /* OUT: Buffer containing changeset */
  9132   9157   );
  9133   9158   
  9134   9159   /*
  9135   9160   ** CAPI3REF: Load The Difference Between Tables Into A Session 
................................................................................
  9165   9190   **   <li> For each row (primary key) that exists in the to-table but not in 
  9166   9191   **     the from-table, an INSERT record is added to the session object.
  9167   9192   **
  9168   9193   **   <li> For each row (primary key) that exists in the to-table but not in 
  9169   9194   **     the from-table, a DELETE record is added to the session object.
  9170   9195   **
  9171   9196   **   <li> For each row (primary key) that exists in both tables, but features 
  9172         -**     different in each, an UPDATE record is added to the session.
         9197  +**     different non-PK values in each, an UPDATE record is added to the
         9198  +**     session.  
  9173   9199   ** </ul>
  9174   9200   **
  9175   9201   ** To clarify, if this function is called and then a changeset constructed
  9176   9202   ** using [sqlite3session_changeset()], then after applying that changeset to 
  9177   9203   ** database zFrom the contents of the two compatible tables would be 
  9178   9204   ** identical.
  9179   9205   **
................................................................................
  9182   9208   **
  9183   9209   ** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
  9184   9210   ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
  9185   9211   ** may be set to point to a buffer containing an English language error 
  9186   9212   ** message. It is the responsibility of the caller to free this buffer using
  9187   9213   ** sqlite3_free().
  9188   9214   */
  9189         -int sqlite3session_diff(
         9215  +SQLITE_API int sqlite3session_diff(
  9190   9216     sqlite3_session *pSession,
  9191   9217     const char *zFromDb,
  9192   9218     const char *zTbl,
  9193   9219     char **pzErrMsg
  9194   9220   );
  9195   9221   
  9196   9222   
................................................................................
  9218   9244   ** in the same way as for changesets.
  9219   9245   **
  9220   9246   ** Changes within a patchset are ordered in the same way as for changesets
  9221   9247   ** generated by the sqlite3session_changeset() function (i.e. all changes for
  9222   9248   ** a single table are grouped together, tables appear in the order in which
  9223   9249   ** they were attached to the session object).
  9224   9250   */
  9225         -int sqlite3session_patchset(
         9251  +SQLITE_API int sqlite3session_patchset(
  9226   9252     sqlite3_session *pSession,      /* Session object */
  9227   9253     int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
  9228   9254     void **ppPatchset               /* OUT: Buffer containing changeset */
  9229   9255   );
  9230   9256   
  9231   9257   /*
  9232   9258   ** CAPI3REF: Test if a changeset has recorded any changes.
................................................................................
  9239   9265   ** [sqlite3session_changeset()] on the session handle may still return a
  9240   9266   ** changeset that contains no changes. This can happen when a row in 
  9241   9267   ** an attached table is modified and then later on the original values 
  9242   9268   ** are restored. However, if this function returns non-zero, then it is
  9243   9269   ** guaranteed that a call to sqlite3session_changeset() will return a 
  9244   9270   ** changeset containing zero changes.
  9245   9271   */
  9246         -int sqlite3session_isempty(sqlite3_session *pSession);
         9272  +SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
  9247   9273   
  9248   9274   /*
  9249   9275   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
  9250   9276   **
  9251   9277   ** Create an iterator used to iterate through the contents of a changeset.
  9252   9278   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
  9253   9279   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
................................................................................
  9274   9300   ** that apply to a single table are grouped together. This means that when 
  9275   9301   ** an application iterates through a changeset using an iterator created by 
  9276   9302   ** this function, all changes that relate to a single table are visited 
  9277   9303   ** consecutively. There is no chance that the iterator will visit a change 
  9278   9304   ** the applies to table X, then one for table Y, and then later on visit 
  9279   9305   ** another change for table X.
  9280   9306   */
  9281         -int sqlite3changeset_start(
         9307  +SQLITE_API int sqlite3changeset_start(
  9282   9308     sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
  9283   9309     int nChangeset,                 /* Size of changeset blob in bytes */
  9284   9310     void *pChangeset                /* Pointer to blob containing changeset */
  9285   9311   );
  9286   9312   
  9287   9313   
  9288   9314   /*
................................................................................
  9303   9329   ** Otherwise, if all changes in the changeset have already been visited,
  9304   9330   ** SQLITE_DONE is returned.
  9305   9331   **
  9306   9332   ** If an error occurs, an SQLite error code is returned. Possible error 
  9307   9333   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
  9308   9334   ** SQLITE_NOMEM.
  9309   9335   */
  9310         -int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
         9336  +SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
  9311   9337   
  9312   9338   /*
  9313   9339   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
  9314   9340   **
  9315   9341   ** The pIter argument passed to this function may either be an iterator
  9316   9342   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9317   9343   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
................................................................................
  9331   9357   ** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the 
  9332   9358   ** type of change that the iterator currently points to.
  9333   9359   **
  9334   9360   ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
  9335   9361   ** SQLite error code is returned. The values of the output variables may not
  9336   9362   ** be trusted in this case.
  9337   9363   */
  9338         -int sqlite3changeset_op(
         9364  +SQLITE_API int sqlite3changeset_op(
  9339   9365     sqlite3_changeset_iter *pIter,  /* Iterator object */
  9340   9366     const char **pzTab,             /* OUT: Pointer to table name */
  9341   9367     int *pnCol,                     /* OUT: Number of columns in table */
  9342   9368     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
  9343   9369     int *pbIndirect                 /* OUT: True for an 'indirect' change */
  9344   9370   );
  9345   9371   
................................................................................
  9364   9390   ** in the table.
  9365   9391   **
  9366   9392   ** If this function is called when the iterator does not point to a valid
  9367   9393   ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
  9368   9394   ** SQLITE_OK is returned and the output variables populated as described
  9369   9395   ** above.
  9370   9396   */
  9371         -int sqlite3changeset_pk(
         9397  +SQLITE_API int sqlite3changeset_pk(
  9372   9398     sqlite3_changeset_iter *pIter,  /* Iterator object */
  9373   9399     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
  9374   9400     int *pnCol                      /* OUT: Number of entries in output array */
  9375   9401   );
  9376   9402   
  9377   9403   /*
  9378   9404   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
................................................................................
  9394   9420   ** original row values stored as part of the UPDATE or DELETE change and
  9395   9421   ** returns SQLITE_OK. The name of the function comes from the fact that this 
  9396   9422   ** is similar to the "old.*" columns available to update or delete triggers.
  9397   9423   **
  9398   9424   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9399   9425   ** is returned and *ppValue is set to NULL.
  9400   9426   */
  9401         -int sqlite3changeset_old(
         9427  +SQLITE_API int sqlite3changeset_old(
  9402   9428     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9403   9429     int iVal,                       /* Column number */
  9404   9430     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
  9405   9431   );
  9406   9432   
  9407   9433   /*
  9408   9434   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
................................................................................
  9427   9453   ** SQLITE_OK returned. The name of the function comes from the fact that 
  9428   9454   ** this is similar to the "new.*" columns available to update or delete 
  9429   9455   ** triggers.
  9430   9456   **
  9431   9457   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9432   9458   ** is returned and *ppValue is set to NULL.
  9433   9459   */
  9434         -int sqlite3changeset_new(
         9460  +SQLITE_API int sqlite3changeset_new(
  9435   9461     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9436   9462     int iVal,                       /* Column number */
  9437   9463     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
  9438   9464   );
  9439   9465   
  9440   9466   /*
  9441   9467   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
................................................................................
  9454   9480   ** sqlite3_value object containing the iVal'th value from the 
  9455   9481   ** "conflicting row" associated with the current conflict-handler callback
  9456   9482   ** and returns SQLITE_OK.
  9457   9483   **
  9458   9484   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9459   9485   ** is returned and *ppValue is set to NULL.
  9460   9486   */
  9461         -int sqlite3changeset_conflict(
         9487  +SQLITE_API int sqlite3changeset_conflict(
  9462   9488     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9463   9489     int iVal,                       /* Column number */
  9464   9490     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
  9465   9491   );
  9466   9492   
  9467   9493   /*
  9468   9494   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
................................................................................
  9470   9496   ** This function may only be called with an iterator passed to an
  9471   9497   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
  9472   9498   ** it sets the output variable to the total number of known foreign key
  9473   9499   ** violations in the destination database and returns SQLITE_OK.
  9474   9500   **
  9475   9501   ** In all other cases this function returns SQLITE_MISUSE.
  9476   9502   */
  9477         -int sqlite3changeset_fk_conflicts(
         9503  +SQLITE_API int sqlite3changeset_fk_conflicts(
  9478   9504     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9479   9505     int *pnOut                      /* OUT: Number of FK violations */
  9480   9506   );
  9481   9507   
  9482   9508   
  9483   9509   /*
  9484   9510   ** CAPI3REF: Finalize A Changeset Iterator
................................................................................
  9503   9529   **     // Do something with change.
  9504   9530   **   }
  9505   9531   **   rc = sqlite3changeset_finalize();
  9506   9532   **   if( rc!=SQLITE_OK ){
  9507   9533   **     // An error has occurred 
  9508   9534   **   }
  9509   9535   */
  9510         -int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
         9536  +SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
  9511   9537   
  9512   9538   /*
  9513   9539   ** CAPI3REF: Invert A Changeset
  9514   9540   **
  9515   9541   ** This function is used to "invert" a changeset object. Applying an inverted
  9516   9542   ** changeset to a database reverses the effects of applying the uninverted
  9517   9543   ** changeset. Specifically:
................................................................................
  9533   9559   ** It is the responsibility of the caller to eventually call sqlite3_free()
  9534   9560   ** on the *ppOut pointer to free the buffer allocation following a successful 
  9535   9561   ** call to this function.
  9536   9562   **
  9537   9563   ** WARNING/TODO: This function currently assumes that the input is a valid
  9538   9564   ** changeset. If it is not, the results are undefined.
  9539   9565   */
  9540         -int sqlite3changeset_invert(
         9566  +SQLITE_API int sqlite3changeset_invert(
  9541   9567     int nIn, const void *pIn,       /* Input changeset */
  9542   9568     int *pnOut, void **ppOut        /* OUT: Inverse of input */
  9543   9569   );
  9544   9570   
  9545   9571   /*
  9546   9572   ** CAPI3REF: Concatenate Two Changeset Objects
  9547   9573   **
................................................................................
  9562   9588   **   }else{
  9563   9589   **     *ppOut = 0;
  9564   9590   **     *pnOut = 0;
  9565   9591   **   }
  9566   9592   **
  9567   9593   ** Refer to the sqlite3_changegroup documentation below for details.
  9568   9594   */
  9569         -int sqlite3changeset_concat(
         9595  +SQLITE_API int sqlite3changeset_concat(
  9570   9596     int nA,                         /* Number of bytes in buffer pA */
  9571   9597     void *pA,                       /* Pointer to buffer containing changeset A */
  9572   9598     int nB,                         /* Number of bytes in buffer pB */
  9573   9599     void *pB,                       /* Pointer to buffer containing changeset B */
  9574   9600     int *pnOut,                     /* OUT: Number of bytes in output changeset */
  9575   9601     void **ppOut                    /* OUT: Buffer containing output changeset */
  9576   9602   );
................................................................................
  9750   9776   ** For each table that is not excluded by the filter callback, this function 
  9751   9777   ** tests that the target database contains a compatible table. A table is 
  9752   9778   ** considered compatible if all of the following are true:
  9753   9779   **
  9754   9780   ** <ul>
  9755   9781   **   <li> The table has the same name as the name recorded in the 
  9756   9782   **        changeset, and
  9757         -**   <li> The table has the same number of columns as recorded in the 
         9783  +**   <li> The table has at least as many columns as recorded in the 
  9758   9784   **        changeset, and
  9759   9785   **   <li> The table has primary key columns in the same position as 
  9760   9786   **        recorded in the changeset.
  9761   9787   ** </ul>
  9762   9788   **
  9763   9789   ** If there is no compatible table, it is not an error, but none of the
  9764   9790   ** changes associated with the table are applied. A warning message is issued
................................................................................
  9795   9821   **   original row values stored in the changeset. If it does, and the values 
  9796   9822   **   stored in all non-primary key columns also match the values stored in 
  9797   9823   **   the changeset the row is deleted from the target database.
  9798   9824   **
  9799   9825   **   If a row with matching primary key values is found, but one or more of
  9800   9826   **   the non-primary key fields contains a value different from the original
  9801   9827   **   row value stored in the changeset, the conflict-handler function is
  9802         -**   invoked with [SQLITE_CHANGESET_DATA] as the second argument.
         9828  +**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
         9829  +**   database table has more columns than are recorded in the changeset,
         9830  +**   only the values of those non-primary key fields are compared against
         9831  +**   the current database contents - any trailing database table columns
         9832  +**   are ignored.
  9803   9833   **
  9804   9834   **   If no row with matching primary key values is found in the database,
  9805   9835   **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  9806   9836   **   passed as the second argument.
  9807   9837   **
  9808   9838   **   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
  9809   9839   **   (which can only happen if a foreign key constraint is violated), the
................................................................................
  9810   9840   **   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
  9811   9841   **   passed as the second argument. This includes the case where the DELETE
  9812   9842   **   operation is attempted because an earlier call to the conflict handler
  9813   9843   **   function returned [SQLITE_CHANGESET_REPLACE].
  9814   9844   **
  9815   9845   ** <dt>INSERT Changes<dd>
  9816   9846   **   For each INSERT change, an attempt is made to insert the new row into
  9817         -**   the database.
         9847  +**   the database. If the changeset row contains fewer fields than the
         9848  +**   database table, the trailing fields are populated with their default
         9849  +**   values.
  9818   9850   **
  9819   9851   **   If the attempt to insert the row fails because the database already 
  9820   9852   **   contains a row with the same primary key values, the conflict handler
  9821   9853   **   function is invoked with the second argument set to 
  9822   9854   **   [SQLITE_CHANGESET_CONFLICT].
  9823   9855   **
  9824   9856   **   If the attempt to insert the row fails because of some other constraint
................................................................................
  9828   9860   **   an earlier call to the conflict handler function returned 
  9829   9861   **   [SQLITE_CHANGESET_REPLACE].
  9830   9862   **
  9831   9863   ** <dt>UPDATE Changes<dd>
  9832   9864   **   For each UPDATE change, this function checks if the target database 
  9833   9865   **   contains a row with the same primary key value (or values) as the 
  9834   9866   **   original row values stored in the changeset. If it does, and the values 
  9835         -**   stored in all non-primary key columns also match the values stored in 
  9836         -**   the changeset the row is updated within the target database.
         9867  +**   stored in all modified non-primary key columns also match the values
         9868  +**   stored in the changeset the row is updated within the target database.
  9837   9869   **
  9838   9870   **   If a row with matching primary key values is found, but one or more of
  9839         -**   the non-primary key fields contains a value different from an original
  9840         -**   row value stored in the changeset, the conflict-handler function is
  9841         -**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
         9871  +**   the modified non-primary key fields contains a value different from an
         9872  +**   original row value stored in the changeset, the conflict-handler function
         9873  +**   is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
  9842   9874   **   UPDATE changes only contain values for non-primary key fields that are
  9843   9875   **   to be modified, only those fields need to match the original values to
  9844   9876   **   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
  9845   9877   **
  9846   9878   **   If no row with matching primary key values is found in the database,
  9847   9879   **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  9848   9880   **   passed as the second argument.
................................................................................
  9862   9894   **
  9863   9895   ** All changes made by this function are enclosed in a savepoint transaction.
  9864   9896   ** If any other error (aside from a constraint failure when attempting to
  9865   9897   ** write to the target database) occurs, then the savepoint transaction is
  9866   9898   ** rolled back, restoring the target database to its original state, and an 
  9867   9899   ** SQLite error code returned.
  9868   9900   */
  9869         -int sqlite3changeset_apply(
         9901  +SQLITE_API int sqlite3changeset_apply(
  9870   9902     sqlite3 *db,                    /* Apply change to "main" db of this handle */
  9871   9903     int nChangeset,                 /* Size of changeset in bytes */
  9872   9904     void *pChangeset,               /* Changeset blob */
  9873   9905     int(*xFilter)(
  9874   9906       void *pCtx,                   /* Copy of sixth arg to _apply() */
  9875   9907       const char *zTab              /* Table name */
  9876   9908     ),
................................................................................
 10063  10095   ** is immediately abandoned and the streaming API function returns a copy
 10064  10096   ** of the xOutput error code to the application.
 10065  10097   **
 10066  10098   ** The sessions module never invokes an xOutput callback with the third 
 10067  10099   ** parameter set to a value less than or equal to zero. Other than this,
 10068  10100   ** no guarantees are made as to the size of the chunks of data returned.
 10069  10101   */
 10070         -int sqlite3changeset_apply_strm(
        10102  +SQLITE_API int sqlite3changeset_apply_strm(
 10071  10103     sqlite3 *db,                    /* Apply change to "main" db of this handle */
 10072  10104     int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
 10073  10105     void *pIn,                                          /* First arg for xInput */
 10074  10106     int(*xFilter)(
 10075  10107       void *pCtx,                   /* Copy of sixth arg to _apply() */
 10076  10108       const char *zTab              /* Table name */
 10077  10109     ),
................................................................................
 10078  10110     int(*xConflict)(
 10079  10111       void *pCtx,                   /* Copy of sixth arg to _apply() */
 10080  10112       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 10081  10113       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 10082  10114     ),
 10083  10115     void *pCtx                      /* First argument passed to xConflict */
 10084  10116   );
 10085         -int sqlite3changeset_concat_strm(
        10117  +SQLITE_API int sqlite3changeset_concat_strm(
 10086  10118     int (*xInputA)(void *pIn, void *pData, int *pnData),
 10087  10119     void *pInA,
 10088  10120     int (*xInputB)(void *pIn, void *pData, int *pnData),
 10089  10121     void *pInB,
 10090  10122     int (*xOutput)(void *pOut, const void *pData, int nData),
 10091  10123     void *pOut
 10092  10124   );
 10093         -int sqlite3changeset_invert_strm(
        10125  +SQLITE_API int sqlite3changeset_invert_strm(
 10094  10126     int (*xInput)(void *pIn, void *pData, int *pnData),
 10095  10127     void *pIn,
 10096  10128     int (*xOutput)(void *pOut, const void *pData, int nData),
 10097  10129     void *pOut
 10098  10130   );
 10099         -int sqlite3changeset_start_strm(
        10131  +SQLITE_API int sqlite3changeset_start_strm(
 10100  10132     sqlite3_changeset_iter **pp,
 10101  10133     int (*xInput)(void *pIn, void *pData, int *pnData),
 10102  10134     void *pIn
 10103  10135   );
 10104         -int sqlite3session_changeset_strm(
        10136  +SQLITE_API int sqlite3session_changeset_strm(
 10105  10137     sqlite3_session *pSession,
 10106  10138     int (*xOutput)(void *pOut, const void *pData, int nData),
 10107  10139     void *pOut
 10108  10140   );
 10109         -int sqlite3session_patchset_strm(
        10141  +SQLITE_API int sqlite3session_patchset_strm(
 10110  10142     sqlite3_session *pSession,
 10111  10143     int (*xOutput)(void *pOut, const void *pData, int nData),
 10112  10144     void *pOut
 10113  10145   );
 10114  10146   int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
 10115  10147       int (*xInput)(void *pIn, void *pData, int *pnData),
 10116  10148       void *pIn
................................................................................
 11009  11041   */
 11010  11042   #if !defined(SQLITE_DISABLE_INTRINSIC)
 11011  11043   #  if defined(_MSC_VER) && _MSC_VER>=1400
 11012  11044   #    if !defined(_WIN32_WCE)
 11013  11045   #      include <intrin.h>
 11014  11046   #      pragma intrinsic(_byteswap_ushort)
 11015  11047   #      pragma intrinsic(_byteswap_ulong)
        11048  +#      pragma intrinsic(_byteswap_uint64)
 11016  11049   #      pragma intrinsic(_ReadWriteBarrier)
 11017  11050   #    else
 11018  11051   #      include <cmnintrin.h>
 11019  11052   #    endif
 11020  11053   #  endif
 11021  11054   #endif
 11022  11055   
................................................................................
 11547  11580   /************** Continuing where we left off in sqliteInt.h ******************/
 11548  11581   #include <stdio.h>
 11549  11582   #include <stdlib.h>
 11550  11583   #include <string.h>
 11551  11584   #include <assert.h>
 11552  11585   #include <stddef.h>
 11553  11586   
        11587  +/*
        11588  +** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
        11589  +** This allows better measurements of where memcpy() is used when running
        11590  +** cachegrind.  But this macro version of memcpy() is very slow so it
        11591  +** should not be used in production.  This is a performance measurement
        11592  +** hack only.
        11593  +*/
        11594  +#ifdef SQLITE_INLINE_MEMCPY
        11595  +# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
        11596  +                        int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
        11597  +#endif
        11598  +
 11554  11599   /*
 11555  11600   ** If compiling for a processor that lacks floating point support,
 11556  11601   ** substitute integer for floating-point
 11557  11602   */
 11558  11603   #ifdef SQLITE_OMIT_FLOATING_POINT
 11559  11604   # define double sqlite_int64
 11560  11605   # define float sqlite_int64
................................................................................
 11811  11856   
 11812  11857   /*
 11813  11858   ** Macros to determine whether the machine is big or little endian,
 11814  11859   ** and whether or not that determination is run-time or compile-time.
 11815  11860   **
 11816  11861   ** For best performance, an attempt is made to guess at the byte-order
 11817  11862   ** using C-preprocessor macros.  If that is unsuccessful, or if
 11818         -** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
        11863  +** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
 11819  11864   ** at run-time.
 11820  11865   */
 11821         -#if (defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
        11866  +#ifndef SQLITE_BYTEORDER
        11867  +# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
 11822  11868        defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
 11823  11869        defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
 11824         -     defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
 11825         -# define SQLITE_BYTEORDER    1234
 11826         -# define SQLITE_BIGENDIAN    0
 11827         -# define SQLITE_LITTLEENDIAN 1
 11828         -# define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
        11870  +     defined(__arm__)
        11871  +#   define SQLITE_BYTEORDER    1234
        11872  +# elif defined(sparc)    || defined(__ppc__)
        11873  +#   define SQLITE_BYTEORDER    4321
        11874  +# else
        11875  +#   define SQLITE_BYTEORDER 0
        11876  +# endif
 11829  11877   #endif
 11830         -#if (defined(sparc)    || defined(__ppc__))  \
 11831         -    && !defined(SQLITE_RUNTIME_BYTEORDER)
 11832         -# define SQLITE_BYTEORDER    4321
        11878  +#if SQLITE_BYTEORDER==4321
 11833  11879   # define SQLITE_BIGENDIAN    1
 11834  11880   # define SQLITE_LITTLEENDIAN 0
 11835  11881   # define SQLITE_UTF16NATIVE  SQLITE_UTF16BE
 11836         -#endif
 11837         -#if !defined(SQLITE_BYTEORDER)
        11882  +#elif SQLITE_BYTEORDER==1234
        11883  +# define SQLITE_BIGENDIAN    0
        11884  +# define SQLITE_LITTLEENDIAN 1
        11885  +# define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
        11886  +#else
 11838  11887   # ifdef SQLITE_AMALGAMATION
 11839  11888     const int sqlite3one = 1;
 11840  11889   # else
 11841  11890     extern const int sqlite3one;
 11842  11891   # endif
 11843         -# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
 11844  11892   # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
 11845  11893   # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
 11846  11894   # define SQLITE_UTF16NATIVE  (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
 11847  11895   #endif
 11848  11896   
 11849  11897   /*
 11850  11898   ** Constants for the largest and smallest possible 64-bit signed integers.
................................................................................
 12526  12574   ** A single instruction of the virtual machine has an opcode
 12527  12575   ** and as many as three operands.  The instruction is recorded
 12528  12576   ** as an instance of the following structure:
 12529  12577   */
 12530  12578   struct VdbeOp {
 12531  12579     u8 opcode;          /* What operation to perform */
 12532  12580     signed char p4type; /* One of the P4_xxx constants for p4 */
 12533         -  u8 notUsed1;
 12534         -  u8 p5;              /* Fifth parameter is an unsigned character */
        12581  +  u16 p5;             /* Fifth parameter is an unsigned 16-bit integer */
 12535  12582     int p1;             /* First operand */
 12536  12583     int p2;             /* Second parameter (often the jump destination) */
 12537  12584     int p3;             /* The third parameter */
 12538  12585     union p4union {     /* fourth parameter */
 12539  12586       int i;                 /* Integer value if p4type==P4_INT32 */
 12540  12587       void *p;               /* Generic pointer */
 12541  12588       char *z;               /* Pointer to data for string (char array) types */
................................................................................
 12888  12935   #endif
 12889  12936   SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
 12890  12937   SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
 12891  12938   SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
 12892  12939   SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
 12893  12940   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
 12894  12941   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
 12895         -SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
        12942  +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
 12896  12943   SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
 12897  12944   SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
 12898  12945   SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
 12899  12946   SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
 12900  12947   SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
 12901  12948   SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
 12902  12949   SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
................................................................................
 13190  13237   
 13191  13238   #ifndef SQLITE_OMIT_WAL
 13192  13239   SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
 13193  13240   SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
 13194  13241   SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
 13195  13242   SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
 13196  13243   SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
 13197         -SQLITE_PRIVATE   int sqlite3PagerUseWal(Pager *pPager);
        13244  +# ifdef SQLITE_DIRECT_OVERFLOW_READ
        13245  +SQLITE_PRIVATE   int sqlite3PagerUseWal(Pager *pPager, Pgno);
        13246  +# endif
 13198  13247   # ifdef SQLITE_ENABLE_SNAPSHOT
 13199  13248   SQLITE_PRIVATE   int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
 13200  13249   SQLITE_PRIVATE   int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
 13201  13250   SQLITE_PRIVATE   int sqlite3PagerSnapshotRecover(Pager *pPager);
 13202  13251   # endif
 13203  13252   #else
 13204         -# define sqlite3PagerUseWal(x) 0
        13253  +# define sqlite3PagerUseWal(x,y) 0
 13205  13254   #endif
 13206  13255   
 13207  13256   #ifdef SQLITE_ENABLE_ZIPVFS
 13208  13257   SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
 13209  13258   #endif
 13210  13259   
 13211  13260   /* Functions used to query pager state and configuration. */
................................................................................
 15294  15343                                         ** the OR optimization  */
 15295  15344   #define WHERE_GROUPBY          0x0040 /* pOrderBy is really a GROUP BY */
 15296  15345   #define WHERE_DISTINCTBY       0x0080 /* pOrderby is really a DISTINCT clause */
 15297  15346   #define WHERE_WANT_DISTINCT    0x0100 /* All output needs to be distinct */
 15298  15347   #define WHERE_SORTBYGROUP      0x0200 /* Support sqlite3WhereIsSorted() */
 15299  15348   #define WHERE_SEEK_TABLE       0x0400 /* Do not defer seeks on main table */
 15300  15349   #define WHERE_ORDERBY_LIMIT    0x0800 /* ORDERBY+LIMIT on the inner loop */
 15301         -                        /*     0x1000    not currently used */
        15350  +#define WHERE_SEEK_UNIQ_TABLE  0x1000 /* Do not defer seeks if unique */
 15302  15351                           /*     0x2000    not currently used */
 15303  15352   #define WHERE_USE_LIMIT        0x4000 /* Use the LIMIT in cost estimates */
 15304  15353                           /*     0x8000    not currently used */
 15305  15354   
 15306  15355   /* Allowed return values from sqlite3WhereIsDistinct()
 15307  15356   */
 15308  15357   #define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
................................................................................
 15759  15808   #define OPFLAG_NCHANGE       0x01    /* OP_Insert: Set to update db->nChange */
 15760  15809                                        /* Also used in P2 (not P5) of OP_Delete */
 15761  15810   #define OPFLAG_EPHEM         0x01    /* OP_Column: Ephemeral output is ok */
 15762  15811   #define OPFLAG_LASTROWID     0x20    /* Set to update db->lastRowid */
 15763  15812   #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
 15764  15813   #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
 15765  15814   #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
 15766         -#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 15767  15815   #define OPFLAG_ISNOOP        0x40    /* OP_Delete does pre-update-hook only */
 15768         -#endif
 15769  15816   #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
 15770  15817   #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
 15771  15818   #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
 15772  15819   #define OPFLAG_SEEKEQ        0x02    /* OP_Open** cursor uses EQ seek only */
 15773  15820   #define OPFLAG_FORDELETE     0x08    /* OP_Open should use BTREE_FORDELETE */
 15774  15821   #define OPFLAG_P2ISREG       0x10    /* P2 to OP_Open** is a register number */
 15775  15822   #define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
................................................................................
 16492  16539   SQLITE_PRIVATE void sqlite3GenerateRowDelete(
 16493  16540       Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
 16494  16541   SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
 16495  16542   SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
 16496  16543   SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
 16497  16544   SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
 16498  16545                                        u8,u8,int,int*,int*);
        16546  +#ifdef SQLITE_ENABLE_NULL_TRIM
        16547  +SQLITE_PRIVATE   void sqlite3SetMakeRecordP5(Vdbe*,Table*);
        16548  +#else
        16549  +# define sqlite3SetMakeRecordP5(A,B)
        16550  +#endif
 16499  16551   SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
 16500  16552   SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
 16501  16553   SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
 16502  16554   SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
 16503  16555   SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
 16504  16556   SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
 16505  16557   SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
................................................................................
 16770  16822   SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
 16771  16823   SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
 16772  16824   #endif
 16773  16825   
 16774  16826   /*
 16775  16827   ** The interface to the LEMON-generated parser
 16776  16828   */
 16777         -SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
 16778         -SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
        16829  +#ifndef SQLITE_AMALGAMATION
        16830  +SQLITE_PRIVATE   void *sqlite3ParserAlloc(void*(*)(u64));
        16831  +SQLITE_PRIVATE   void sqlite3ParserFree(void*, void(*)(void*));
        16832  +#endif
 16779  16833   SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
 16780  16834   #ifdef YYTRACKMAXSTACKDEPTH
 16781  16835   SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
 16782  16836   #endif
 16783  16837   
 16784  16838   SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
 16785  16839   #ifndef SQLITE_OMIT_LOAD_EXTENSION
................................................................................
 16881  16935   SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
 16882  16936   #else
 16883  16937     #define sqlite3FkActions(a,b,c,d,e,f)
 16884  16938     #define sqlite3FkCheck(a,b,c,d,e,f)
 16885  16939     #define sqlite3FkDropTable(a,b,c)
 16886  16940     #define sqlite3FkOldmask(a,b)         0
 16887  16941     #define sqlite3FkRequired(a,b,c,d)    0
        16942  +  #define sqlite3FkReferences(a)        0
 16888  16943   #endif
 16889  16944   #ifndef SQLITE_OMIT_FOREIGN_KEY
 16890  16945   SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
 16891  16946   SQLITE_PRIVATE   int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
 16892  16947   #else
 16893  16948     #define sqlite3FkDelete(a,b)
 16894  16949     #define sqlite3FkLocateIndex(a,b,c,d,e)
................................................................................
 18247  18302     UnpackedRecord *pUnpacked;      /* Unpacked version of aRecord[] */
 18248  18303     UnpackedRecord *pNewUnpacked;   /* Unpacked version of new.* record */
 18249  18304     int iNewReg;                    /* Register for new.* values */
 18250  18305     i64 iKey1;                      /* First key value passed to hook */
 18251  18306     i64 iKey2;                      /* Second key value passed to hook */
 18252  18307     Mem *aNew;                      /* Array of new.* values */
 18253  18308     Table *pTab;                    /* Schema object being upated */          
        18309  +  Index *pPk;                     /* PK index if pTab is WITHOUT ROWID */
 18254  18310   };
 18255  18311   
 18256  18312   /*
 18257  18313   ** Function prototypes
 18258  18314   */
 18259  18315   SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
 18260  18316   SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
................................................................................
 20647  20703   **
 20648  20704   ** For this low-level routine, we are guaranteed that nByte>0 because
 20649  20705   ** cases of nByte<=0 will be intercepted and dealt with by higher level
 20650  20706   ** routines.
 20651  20707   */
 20652  20708   static void *sqlite3MemMalloc(int nByte){
 20653  20709   #ifdef SQLITE_MALLOCSIZE
 20654         -  void *p = SQLITE_MALLOC( nByte );
        20710  +  void *p;
        20711  +  testcase( ROUND8(nByte)==nByte );
        20712  +  p = SQLITE_MALLOC( nByte );
 20655  20713     if( p==0 ){
 20656  20714       testcase( sqlite3GlobalConfig.xLog!=0 );
 20657  20715       sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
 20658  20716     }
 20659  20717     return p;
 20660  20718   #else
 20661  20719     sqlite3_int64 *p;
 20662  20720     assert( nByte>0 );
 20663         -  nByte = ROUND8(nByte);
        20721  +  testcase( ROUND8(nByte)!=nByte );
 20664  20722     p = SQLITE_MALLOC( nByte+8 );
 20665  20723     if( p ){
 20666  20724       p[0] = nByte;
 20667  20725       p++;
 20668  20726     }else{
 20669  20727       testcase( sqlite3GlobalConfig.xLog!=0 );
 20670  20728       sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
................................................................................
 23778  23836   ** compiled without mutexes (SQLITE_THREADSAFE=0).
 23779  23837   */
 23780  23838   SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
 23781  23839   #if defined(SQLITE_MEMORY_BARRIER)
 23782  23840     SQLITE_MEMORY_BARRIER;
 23783  23841   #elif defined(__GNUC__)
 23784  23842     __sync_synchronize();
 23785         -#elif !defined(SQLITE_DISABLE_INTRINSIC) && \
 23786         -      defined(_MSC_VER) && _MSC_VER>=1300
        23843  +#elif MSCV_VERSION>=1300
 23787  23844     _ReadWriteBarrier();
 23788  23845   #elif defined(MemoryBarrier)
 23789  23846     MemoryBarrier();
 23790  23847   #endif
 23791  23848   }
 23792  23849   
 23793  23850   /*
................................................................................
 24313  24370   
 24314  24371   /*
 24315  24372   ** Do a memory allocation with statistics and alarms.  Assume the
 24316  24373   ** lock is already held.
 24317  24374   */
 24318  24375   static void mallocWithAlarm(int n, void **pp){
 24319  24376     void *p;
 24320         -  int nFull = 0;
        24377  +  int nFull;
 24321  24378     assert( sqlite3_mutex_held(mem0.mutex) );
        24379  +  assert( n>0 );
        24380  +
        24381  +  /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
        24382  +  ** implementation of malloc_good_size(), which must be called in debug
        24383  +  ** mode and specifically when the DMD "Dark Matter Detector" is enabled
        24384  +  ** or else a crash results.  Hence, do not attempt to optimize out the
        24385  +  ** following xRoundup() call. */
        24386  +  nFull = sqlite3GlobalConfig.m.xRoundup(n);
        24387  +
 24322  24388     sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
 24323  24389     if( mem0.alarmThreshold>0 ){
 24324  24390       sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 24325         -    nFull = sqlite3GlobalConfig.m.xRoundup(n);
 24326  24391       if( nUsed >= mem0.alarmThreshold - nFull ){
 24327  24392         mem0.nearlyFull = 1;
 24328  24393         sqlite3MallocAlarm(nFull);
 24329  24394       }else{
 24330  24395         mem0.nearlyFull = 0;
 24331  24396       }
 24332  24397     }
 24333         -  p = sqlite3GlobalConfig.m.xMalloc(n);
        24398  +  p = sqlite3GlobalConfig.m.xMalloc(nFull);
 24334  24399   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 24335  24400     if( p==0 && mem0.alarmThreshold>0 ){
 24336  24401       sqlite3MallocAlarm(nFull);
 24337         -    p = sqlite3GlobalConfig.m.xMalloc(n);
        24402  +    p = sqlite3GlobalConfig.m.xMalloc(nFull);
 24338  24403     }
 24339  24404   #endif
 24340  24405     if( p ){
 24341  24406       nFull = sqlite3MallocSize(p);
 24342  24407       sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
 24343  24408       sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
 24344  24409     }
................................................................................
 28628  28693   ** Read or write a four-byte big-endian integer value.
 28629  28694   */
 28630  28695   SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
 28631  28696   #if SQLITE_BYTEORDER==4321
 28632  28697     u32 x;
 28633  28698     memcpy(&x,p,4);
 28634  28699     return x;
 28635         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28636         -    && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
        28700  +#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
 28637  28701     u32 x;
 28638  28702     memcpy(&x,p,4);
 28639  28703     return __builtin_bswap32(x);
 28640         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28641         -    && defined(_MSC_VER) && _MSC_VER>=1300
        28704  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
 28642  28705     u32 x;
 28643  28706     memcpy(&x,p,4);
 28644  28707     return _byteswap_ulong(x);
 28645  28708   #else
 28646  28709     testcase( p[0]&0x80 );
 28647  28710     return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
 28648  28711   #endif
 28649  28712   }
 28650  28713   SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
 28651  28714   #if SQLITE_BYTEORDER==4321
 28652  28715     memcpy(p,&v,4);
 28653         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28654         -    && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
        28716  +#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
 28655  28717     u32 x = __builtin_bswap32(v);
 28656  28718     memcpy(p,&x,4);
 28657         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28658         -    && defined(_MSC_VER) && _MSC_VER>=1300
        28719  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
 28659  28720     u32 x = _byteswap_ulong(v);
 28660  28721     memcpy(p,&x,4);
 28661  28722   #else
 28662  28723     p[0] = (u8)(v>>24);
 28663  28724     p[1] = (u8)(v>>16);
 28664  28725     p[2] = (u8)(v>>8);
 28665  28726     p[3] = (u8)v;
................................................................................
 28767  28828   /*
 28768  28829   ** Attempt to add, substract, or multiply the 64-bit signed value iB against
 28769  28830   ** the other 64-bit signed integer at *pA and store the result in *pA.
 28770  28831   ** Return 0 on success.  Or if the operation would have resulted in an
 28771  28832   ** overflow, leave *pA unchanged and return 1.
 28772  28833   */
 28773  28834   SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
 28774         -#if !defined(SQLITE_DISABLE_INTRINSIC) \
 28775         -    && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
        28835  +#if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
 28776  28836     return __builtin_add_overflow(*pA, iB, pA);
 28777  28837   #else
 28778  28838     i64 iA = *pA;
 28779  28839     testcase( iA==0 ); testcase( iA==1 );
 28780  28840     testcase( iB==-1 ); testcase( iB==0 );
 28781  28841     if( iB>=0 ){
 28782  28842       testcase( iA>0 && LARGEST_INT64 - iA == iB );
................................................................................
 28788  28848       if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
 28789  28849     }
 28790  28850     *pA += iB;
 28791  28851     return 0; 
 28792  28852   #endif
 28793  28853   }
 28794  28854   SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
 28795         -#if !defined(SQLITE_DISABLE_INTRINSIC) \
 28796         -    && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
        28855  +#if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
 28797  28856     return __builtin_sub_overflow(*pA, iB, pA);
 28798  28857   #else
 28799  28858     testcase( iB==SMALLEST_INT64+1 );
 28800  28859     if( iB==SMALLEST_INT64 ){
 28801  28860       testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
 28802  28861       if( (*pA)>=0 ) return 1;
 28803  28862       *pA -= iB;
................................................................................
 28804  28863       return 0;
 28805  28864     }else{
 28806  28865       return sqlite3AddInt64(pA, -iB);
 28807  28866     }
 28808  28867   #endif
 28809  28868   }
 28810  28869   SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
 28811         -#if !defined(SQLITE_DISABLE_INTRINSIC) \
 28812         -    && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
        28870  +#if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
 28813  28871     return __builtin_mul_overflow(*pA, iB, pA);
 28814  28872   #else
 28815  28873     i64 iA = *pA;
 28816  28874     if( iB>0 ){
 28817  28875       if( iA>LARGEST_INT64/iB ) return 1;
 28818  28876       if( iA<SMALLEST_INT64/iB ) return 1;
 28819  28877     }else if( iB<0 ){
................................................................................
 47528  47586   ** instead of
 47529  47587   **
 47530  47588   **   if( pPager->jfd->pMethods ){ ...
 47531  47589   */
 47532  47590   #define isOpen(pFd) ((pFd)->pMethods!=0)
 47533  47591   
 47534  47592   /*
 47535         -** Return true if this pager uses a write-ahead log instead of the usual
 47536         -** rollback journal. Otherwise false.
        47593  +** Return true if this pager uses a write-ahead log to read page pgno.
        47594  +** Return false if the pager reads pgno directly from the database.
 47537  47595   */
        47596  +#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ)
        47597  +SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){
        47598  +  u32 iRead = 0;
        47599  +  int rc;
        47600  +  if( pPager->pWal==0 ) return 0;
        47601  +  rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
        47602  +  return rc || iRead;
        47603  +}
        47604  +#endif
 47538  47605   #ifndef SQLITE_OMIT_WAL
 47539         -SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager){
 47540         -  return (pPager->pWal!=0);
 47541         -}
 47542         -# define pagerUseWal(x) sqlite3PagerUseWal(x)
        47606  +# define pagerUseWal(x) ((x)->pWal!=0)
 47543  47607   #else
 47544  47608   # define pagerUseWal(x) 0
 47545  47609   # define pagerRollbackWal(x) 0
 47546  47610   # define pagerWalFrames(v,w,x,y) 0
 47547  47611   # define pagerOpenWalIfPresent(z) SQLITE_OK
 47548  47612   # define pagerBeginReadTransaction(z) SQLITE_OK
 47549  47613   #endif
................................................................................
 58488  58552   /*
 58489  58553   ** get2byteAligned(), unlike get2byte(), requires that its argument point to a
 58490  58554   ** two-byte aligned address.  get2bytea() is only used for accessing the
 58491  58555   ** cell addresses in a btree header.
 58492  58556   */
 58493  58557   #if SQLITE_BYTEORDER==4321
 58494  58558   # define get2byteAligned(x)  (*(u16*)(x))
 58495         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 58496         -    && GCC_VERSION>=4008000
        58559  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
 58497  58560   # define get2byteAligned(x)  __builtin_bswap16(*(u16*)(x))
 58498         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 58499         -    && defined(_MSC_VER) && _MSC_VER>=1300
        58561  +#elif SQLITE_BYTEORDER==1234 && MSCV_VERSION>=1300
 58500  58562   # define get2byteAligned(x)  _byteswap_ushort(*(u16*)(x))
 58501  58563   #else
 58502  58564   # define get2byteAligned(x)  ((x)[0]<<8 | (x)[1])
 58503  58565   #endif
 58504  58566   
 58505  58567   /************** End of btreeInt.h ********************************************/
 58506  58568   /************** Continuing where we left off in btmutex.c ********************/
................................................................................
 62833  62895   SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
 62834  62896     int rc = SQLITE_OK;
 62835  62897     if( p && p->inTrans==TRANS_WRITE ){
 62836  62898       BtShared *pBt = p->pBt;
 62837  62899       assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
 62838  62900       assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
 62839  62901       sqlite3BtreeEnter(p);
 62840         -    rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
        62902  +    if( op==SAVEPOINT_ROLLBACK ){
        62903  +      rc = saveAllCursors(pBt, 0, 0);
        62904  +    }
        62905  +    if( rc==SQLITE_OK ){
        62906  +      rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
        62907  +    }
 62841  62908       if( rc==SQLITE_OK ){
 62842  62909         if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
 62843  62910           pBt->nPage = 0;
 62844  62911         }
 62845  62912         rc = newDatabase(pBt);
 62846  62913         pBt->nPage = get4byte(28 + pBt->pPage1->aData);
 62847  62914   
................................................................................
 63219  63286   /*
 63220  63287   ** This function is used to read or overwrite payload information
 63221  63288   ** for the entry that the pCur cursor is pointing to. The eOp
 63222  63289   ** argument is interpreted as follows:
 63223  63290   **
 63224  63291   **   0: The operation is a read. Populate the overflow cache.
 63225  63292   **   1: The operation is a write. Populate the overflow cache.
 63226         -**   2: The operation is a read. Do not populate the overflow cache.
 63227  63293   **
 63228  63294   ** A total of "amt" bytes are read or written beginning at "offset".
 63229  63295   ** Data is read to or from the buffer pBuf.
 63230  63296   **
 63231  63297   ** The content being read or written might appear on the main page
 63232  63298   ** or be scattered out on multiple overflow pages.
 63233  63299   **
 63234         -** If the current cursor entry uses one or more overflow pages and the
 63235         -** eOp argument is not 2, this function may allocate space for and lazily 
 63236         -** populates the overflow page-list cache array (BtCursor.aOverflow). 
        63300  +** If the current cursor entry uses one or more overflow pages
        63301  +** this function may allocate space for and lazily populate
        63302  +** the overflow page-list cache array (BtCursor.aOverflow). 
 63237  63303   ** Subsequent calls use this cache to make seeking to the supplied offset 
 63238  63304   ** more efficient.
 63239  63305   **
 63240         -** Once an overflow page-list cache has been allocated, it may be
        63306  +** Once an overflow page-list cache has been allocated, it must be
 63241  63307   ** invalidated if some other cursor writes to the same table, or if
 63242  63308   ** the cursor is moved to a different row. Additionally, in auto-vacuum
 63243  63309   ** mode, the following events may invalidate an overflow page-list cache.
 63244  63310   **
 63245  63311   **   * An incremental vacuum,
 63246  63312   **   * A commit in auto_vacuum="full" mode,
 63247  63313   **   * Creating a table (may require moving an overflow page).
................................................................................
 63255  63321   ){
 63256  63322     unsigned char *aPayload;
 63257  63323     int rc = SQLITE_OK;
 63258  63324     int iIdx = 0;
 63259  63325     MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
 63260  63326     BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
 63261  63327   #ifdef SQLITE_DIRECT_OVERFLOW_READ
 63262         -  unsigned char * const pBufStart = pBuf;
 63263         -  int bEnd;                                 /* True if reading to end of data */
        63328  +  unsigned char * const pBufStart = pBuf;     /* Start of original out buffer */
 63264  63329   #endif
 63265  63330   
 63266  63331     assert( pPage );
        63332  +  assert( eOp==0 || eOp==1 );
 63267  63333     assert( pCur->eState==CURSOR_VALID );
 63268  63334     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 63269  63335     assert( cursorHoldsMutex(pCur) );
 63270         -  assert( eOp!=2 || offset==0 );    /* Always start from beginning for eOp==2 */
 63271  63336   
 63272  63337     getCellInfo(pCur);
 63273  63338     aPayload = pCur->info.pPayload;
 63274         -#ifdef SQLITE_DIRECT_OVERFLOW_READ
 63275         -  bEnd = offset+amt==pCur->info.nPayload;
 63276         -#endif
 63277  63339     assert( offset+amt <= pCur->info.nPayload );
 63278  63340   
 63279  63341     assert( aPayload > pPage->aData );
 63280  63342     if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
 63281  63343       /* Trying to read or write past the end of the data is an error.  The
 63282  63344       ** conditional above is really:
 63283  63345       **    &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
................................................................................
 63288  63350   
 63289  63351     /* Check if data must be read/written to/from the btree page itself. */
 63290  63352     if( offset<pCur->info.nLocal ){
 63291  63353       int a = amt;
 63292  63354       if( a+offset>pCur->info.nLocal ){
 63293  63355         a = pCur->info.nLocal - offset;
 63294  63356       }
 63295         -    rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage);
        63357  +    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
 63296  63358       offset = 0;
 63297  63359       pBuf += a;
 63298  63360       amt -= a;
 63299  63361     }else{
 63300  63362       offset -= pCur->info.nLocal;
 63301  63363     }
 63302  63364   
................................................................................
 63304  63366     if( rc==SQLITE_OK && amt>0 ){
 63305  63367       const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
 63306  63368       Pgno nextPage;
 63307  63369   
 63308  63370       nextPage = get4byte(&aPayload[pCur->info.nLocal]);
 63309  63371   
 63310  63372       /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
 63311         -    ** Except, do not allocate aOverflow[] for eOp==2.
 63312  63373       **
 63313  63374       ** The aOverflow[] array is sized at one entry for each overflow page
 63314  63375       ** in the overflow chain. The page number of the first overflow page is
 63315  63376       ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
 63316  63377       ** means "not yet known" (the cache is lazily populated).
 63317  63378       */
 63318         -    if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
        63379  +    if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
 63319  63380         int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
 63320  63381         if( nOvfl>pCur->nOvflAlloc ){
 63321  63382           Pgno *aNew = (Pgno*)sqlite3Realloc(
 63322  63383               pCur->aOverflow, nOvfl*2*sizeof(Pgno)
 63323  63384           );
 63324  63385           if( aNew==0 ){
 63325         -          rc = SQLITE_NOMEM_BKPT;
        63386  +          return SQLITE_NOMEM_BKPT;
 63326  63387           }else{
 63327  63388             pCur->nOvflAlloc = nOvfl*2;
 63328  63389             pCur->aOverflow = aNew;
 63329  63390           }
 63330  63391         }
 63331         -      if( rc==SQLITE_OK ){
 63332         -        memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
 63333         -        pCur->curFlags |= BTCF_ValidOvfl;
        63392  +      memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
        63393  +      pCur->curFlags |= BTCF_ValidOvfl;
        63394  +    }else{
        63395  +      /* If the overflow page-list cache has been allocated and the
        63396  +      ** entry for the first required overflow page is valid, skip
        63397  +      ** directly to it.
        63398  +      */
        63399  +      if( pCur->aOverflow[offset/ovflSize] ){
        63400  +        iIdx = (offset/ovflSize);
        63401  +        nextPage = pCur->aOverflow[iIdx];
        63402  +        offset = (offset%ovflSize);
 63334  63403         }
 63335  63404       }
 63336  63405   
 63337         -    /* If the overflow page-list cache has been allocated and the
 63338         -    ** entry for the first required overflow page is valid, skip
 63339         -    ** directly to it.
 63340         -    */
 63341         -    if( (pCur->curFlags & BTCF_ValidOvfl)!=0
 63342         -     && pCur->aOverflow[offset/ovflSize]
 63343         -    ){
 63344         -      iIdx = (offset/ovflSize);
 63345         -      nextPage = pCur->aOverflow[iIdx];
 63346         -      offset = (offset%ovflSize);
 63347         -    }
 63348         -
 63349         -    for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
 63350         -
        63406  +    assert( rc==SQLITE_OK && amt>0 );
        63407  +    while( nextPage ){
 63351  63408         /* If required, populate the overflow page-list cache. */
 63352         -      if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
 63353         -        assert( pCur->aOverflow[iIdx]==0
 63354         -                || pCur->aOverflow[iIdx]==nextPage
 63355         -                || CORRUPT_DB );
 63356         -        pCur->aOverflow[iIdx] = nextPage;
 63357         -      }
        63409  +      assert( pCur->aOverflow[iIdx]==0
        63410  +              || pCur->aOverflow[iIdx]==nextPage
        63411  +              || CORRUPT_DB );
        63412  +      pCur->aOverflow[iIdx] = nextPage;
 63358  63413   
 63359  63414         if( offset>=ovflSize ){
 63360  63415           /* The only reason to read this page is to obtain the page
 63361  63416           ** number for the next page in the overflow chain. The page
 63362  63417           ** data is not required. So first try to lookup the overflow
 63363  63418           ** page-list cache, if any, then fall back to the getOverflowPage()
 63364  63419           ** function.
 63365         -        **
 63366         -        ** Note that the aOverflow[] array must be allocated because eOp!=2
 63367         -        ** here.  If eOp==2, then offset==0 and this branch is never taken.
 63368  63420           */
 63369         -        assert( eOp!=2 );
 63370  63421           assert( pCur->curFlags & BTCF_ValidOvfl );
 63371  63422           assert( pCur->pBtree->db==pBt->db );
 63372  63423           if( pCur->aOverflow[iIdx+1] ){
 63373  63424             nextPage = pCur->aOverflow[iIdx+1];
 63374  63425           }else{
 63375  63426             rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
 63376  63427           }
 63377  63428           offset -= ovflSize;
 63378  63429         }else{
 63379  63430           /* Need to read this page properly. It contains some of the
 63380  63431           ** range of data that is being read (eOp==0) or written (eOp!=0).
 63381  63432           */
 63382  63433   #ifdef SQLITE_DIRECT_OVERFLOW_READ
 63383         -        sqlite3_file *fd;
        63434  +        sqlite3_file *fd;      /* File from which to do direct overflow read */
 63384  63435   #endif
 63385  63436           int a = amt;
 63386  63437           if( a + offset > ovflSize ){
 63387  63438             a = ovflSize - offset;
 63388  63439           }
 63389  63440   
 63390  63441   #ifdef SQLITE_DIRECT_OVERFLOW_READ
 63391  63442           /* If all the following are true:
 63392  63443           **
 63393  63444           **   1) this is a read operation, and 
 63394  63445           **   2) data is required from the start of this overflow page, and
 63395         -        **   3) the database is file-backed, and
 63396         -        **   4) there is no open write-transaction, and
 63397         -        **   5) the database is not a WAL database,
 63398         -        **   6) all data from the page is being read.
 63399         -        **   7) at least 4 bytes have already been read into the output buffer 
        63446  +        **   3) there is no open write-transaction, and
        63447  +        **   4) the database is file-backed, and
        63448  +        **   5) the page is not in the WAL file
        63449  +        **   6) at least 4 bytes have already been read into the output buffer 
 63400  63450           **
 63401  63451           ** then data can be read directly from the database file into the
 63402  63452           ** output buffer, bypassing the page-cache altogether. This speeds
 63403  63453           ** up loading large records that span many overflow pages.
 63404  63454           */
 63405         -        if( (eOp&0x01)==0                                      /* (1) */
        63455  +        if( eOp==0                                             /* (1) */
 63406  63456            && offset==0                                          /* (2) */
 63407         -         && (bEnd || a==ovflSize)                              /* (6) */
 63408         -         && pBt->inTransaction==TRANS_READ                     /* (4) */
 63409         -         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
 63410         -         && 0==sqlite3PagerUseWal(pBt->pPager)                 /* (5) */
 63411         -         && &pBuf[-4]>=pBufStart                               /* (7) */
        63457  +         && pBt->inTransaction==TRANS_READ                     /* (3) */
        63458  +         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (4) */
        63459  +         && 0==sqlite3PagerUseWal(pBt->pPager, nextPage)       /* (5) */
        63460  +         && &pBuf[-4]>=pBufStart                               /* (6) */
 63412  63461           ){
 63413  63462             u8 aSave[4];
 63414  63463             u8 *aWrite = &pBuf[-4];
 63415         -          assert( aWrite>=pBufStart );                         /* hence (7) */
        63464  +          assert( aWrite>=pBufStart );                         /* due to (6) */
 63416  63465             memcpy(aSave, aWrite, 4);
 63417  63466             rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
 63418  63467             nextPage = get4byte(aWrite);
 63419  63468             memcpy(aWrite, aSave, 4);
 63420  63469           }else
 63421  63470   #endif
 63422  63471   
 63423  63472           {
 63424  63473             DbPage *pDbPage;
 63425  63474             rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
 63426         -              ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0)
        63475  +              (eOp==0 ? PAGER_GET_READONLY : 0)
 63427  63476             );
 63428  63477             if( rc==SQLITE_OK ){
 63429  63478               aPayload = sqlite3PagerGetData(pDbPage);
 63430  63479               nextPage = get4byte(aPayload);
 63431         -            rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage);
        63480  +            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
 63432  63481               sqlite3PagerUnref(pDbPage);
 63433  63482               offset = 0;
 63434  63483             }
 63435  63484           }
 63436  63485           amt -= a;
        63486  +        if( amt==0 ) return rc;
 63437  63487           pBuf += a;
 63438  63488         }
        63489  +      if( rc ) break;
        63490  +      iIdx++;
 63439  63491       }
 63440  63492     }
 63441  63493   
 63442  63494     if( rc==SQLITE_OK && amt>0 ){
 63443         -    return SQLITE_CORRUPT_BKPT;
        63495  +    return SQLITE_CORRUPT_BKPT; /* Overflow chain ends prematurely */
 63444  63496     }
 63445  63497     return rc;
 63446  63498   }
 63447  63499   
 63448  63500   /*
 63449  63501   ** Read part of the payload for the row at which that cursor pCur is currently
 63450  63502   ** pointing.  "amt" bytes will be transferred into pBuf[].  The transfer
................................................................................
 63465  63517   SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
 63466  63518     assert( cursorHoldsMutex(pCur) );
 63467  63519     assert( pCur->eState==CURSOR_VALID );
 63468  63520     assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
 63469  63521     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 63470  63522     return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
 63471  63523   }
        63524  +
        63525  +/*
        63526  +** This variant of sqlite3BtreePayload() works even if the cursor has not
        63527  +** in the CURSOR_VALID state.  It is only used by the sqlite3_blob_read()
        63528  +** interface.
        63529  +*/
 63472  63530   #ifndef SQLITE_OMIT_INCRBLOB
 63473         -SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
        63531  +static SQLITE_NOINLINE int accessPayloadChecked(
        63532  +  BtCursor *pCur,
        63533  +  u32 offset,
        63534  +  u32 amt,
        63535  +  void *pBuf
        63536  +){
 63474  63537     int rc;
 63475  63538     if ( pCur->eState==CURSOR_INVALID ){
 63476  63539       return SQLITE_ABORT;
 63477  63540     }
 63478  63541     assert( cursorOwnsBtShared(pCur) );
 63479         -  rc = restoreCursorPosition(pCur);
 63480         -  if( rc==SQLITE_OK ){
 63481         -    assert( pCur->eState==CURSOR_VALID );
 63482         -    assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
 63483         -    assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 63484         -    rc = accessPayload(pCur, offset, amt, pBuf, 0);
        63542  +  rc = btreeRestoreCursorPosition(pCur);
        63543  +  return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
        63544  +}
        63545  +SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
        63546  +  if( pCur->eState==CURSOR_VALID ){
        63547  +    assert( cursorOwnsBtShared(pCur) );
        63548  +    return accessPayload(pCur, offset, amt, pBuf, 0);
        63549  +  }else{
        63550  +    return accessPayloadChecked(pCur, offset, amt, pBuf);
 63485  63551     }
 63486         -  return rc;
 63487  63552   }
 63488  63553   #endif /* SQLITE_OMIT_INCRBLOB */
 63489  63554   
 63490  63555   /*
 63491  63556   ** Return a pointer to payload information from the entry that the 
 63492  63557   ** pCur cursor is pointing to.  The pointer is to the beginning of
 63493  63558   ** the key if index btrees (pPage->intKey==0) and is the data for
................................................................................
 63885  63950     if( pIdxKey==0
 63886  63951      && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
 63887  63952     ){
 63888  63953       if( pCur->info.nKey==intKey ){
 63889  63954         *pRes = 0;
 63890  63955         return SQLITE_OK;
 63891  63956       }
 63892         -    if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
 63893         -      *pRes = -1;
 63894         -      return SQLITE_OK;
        63957  +    if( pCur->info.nKey<intKey ){
        63958  +      if( (pCur->curFlags & BTCF_AtLast)!=0 ){
        63959  +        *pRes = -1;
        63960  +        return SQLITE_OK;
        63961  +      }
        63962  +      /* If the requested key is one more than the previous key, then
        63963  +      ** try to get there using sqlite3BtreeNext() rather than a full
        63964  +      ** binary search.  This is an optimization only.  The correct answer
        63965  +      ** is still obtained without this ase, only a little more slowely */
        63966  +      if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
        63967  +        *pRes = 0;
        63968  +        rc = sqlite3BtreeNext(pCur, pRes);
        63969  +        if( rc ) return rc;
        63970  +        if( *pRes==0 ){
        63971  +          getCellInfo(pCur);
        63972  +          if( pCur->info.nKey==intKey ){
        63973  +            return SQLITE_OK;
        63974  +          }
        63975  +        }
        63976  +      }
 63895  63977       }
 63896  63978     }
 63897  63979   
 63898  63980     if( pIdxKey ){
 63899  63981       xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
 63900  63982       pIdxKey->errCode = 0;
 63901  63983       assert( pIdxKey->default_rc==1 
................................................................................
 64023  64105             }
 64024  64106             pCellKey = sqlite3Malloc( nCell+18 );
 64025  64107             if( pCellKey==0 ){
 64026  64108               rc = SQLITE_NOMEM_BKPT;
 64027  64109               goto moveto_finish;
 64028  64110             }
 64029  64111             pCur->aiIdx[pCur->iPage] = (u16)idx;
 64030         -          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
        64112  +          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
        64113  +          pCur->curFlags &= ~BTCF_ValidOvfl;
 64031  64114             if( rc ){
 64032  64115               sqlite3_free(pCellKey);
 64033  64116               goto moveto_finish;
 64034  64117             }
 64035  64118             c = xRecordCompare(nCell, pCellKey, pIdxKey);
 64036  64119             sqlite3_free(pCellKey);
 64037  64120           }
................................................................................
 71841  71924   }
 71842  71925   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
 71843  71926     sqlite3VdbeGetOp(p,addr)->p2 = val;
 71844  71927   }
 71845  71928   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
 71846  71929     sqlite3VdbeGetOp(p,addr)->p3 = val;
 71847  71930   }
 71848         -SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){
        71931  +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
 71849  71932     assert( p->nOp>0 || p->db->mallocFailed );
 71850  71933     if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
 71851  71934   }
 71852  71935   
 71853  71936   /*
 71854  71937   ** Change the P2 operand of instruction addr so that it points to
 71855  71938   ** the address of the next instruction to be coded.
................................................................................
 73555  73638   ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
 73556  73639   ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the 
 73557  73640   ** statement transaction is committed.
 73558  73641   **
 73559  73642   ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 
 73560  73643   ** Otherwise SQLITE_OK.
 73561  73644   */
 73562         -SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
        73645  +static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
 73563  73646     sqlite3 *const db = p->db;
 73564  73647     int rc = SQLITE_OK;
        73648  +  int i;
        73649  +  const int iSavepoint = p->iStatement-1;
 73565  73650   
 73566         -  /* If p->iStatement is greater than zero, then this Vdbe opened a 
 73567         -  ** statement transaction that should be closed here. The only exception
 73568         -  ** is that an IO error may have occurred, causing an emergency rollback.
 73569         -  ** In this case (db->nStatement==0), and there is nothing to do.
 73570         -  */
 73571         -  if( db->nStatement && p->iStatement ){
 73572         -    int i;
 73573         -    const int iSavepoint = p->iStatement-1;
        73651  +  assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
        73652  +  assert( db->nStatement>0 );
        73653  +  assert( p->iStatement==(db->nStatement+db->nSavepoint) );
 73574  73654   
 73575         -    assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
 73576         -    assert( db->nStatement>0 );
 73577         -    assert( p->iStatement==(db->nStatement+db->nSavepoint) );
 73578         -
 73579         -    for(i=0; i<db->nDb; i++){ 
 73580         -      int rc2 = SQLITE_OK;
 73581         -      Btree *pBt = db->aDb[i].pBt;
 73582         -      if( pBt ){
 73583         -        if( eOp==SAVEPOINT_ROLLBACK ){
 73584         -          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
 73585         -        }
 73586         -        if( rc2==SQLITE_OK ){
 73587         -          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
 73588         -        }
 73589         -        if( rc==SQLITE_OK ){
 73590         -          rc = rc2;
 73591         -        }
 73592         -      }
 73593         -    }
 73594         -    db->nStatement--;
 73595         -    p->iStatement = 0;
 73596         -
 73597         -    if( rc==SQLITE_OK ){
        73655  +  for(i=0; i<db->nDb; i++){ 
        73656  +    int rc2 = SQLITE_OK;
        73657  +    Btree *pBt = db->aDb[i].pBt;
        73658  +    if( pBt ){
 73598  73659         if( eOp==SAVEPOINT_ROLLBACK ){
 73599         -        rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
        73660  +        rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
        73661  +      }
        73662  +      if( rc2==SQLITE_OK ){
        73663  +        rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
 73600  73664         }
 73601  73665         if( rc==SQLITE_OK ){
 73602         -        rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
        73666  +        rc = rc2;
 73603  73667         }
 73604  73668       }
        73669  +  }
        73670  +  db->nStatement--;
        73671  +  p->iStatement = 0;
 73605  73672   
 73606         -    /* If the statement transaction is being rolled back, also restore the 
 73607         -    ** database handles deferred constraint counter to the value it had when 
 73608         -    ** the statement transaction was opened.  */
        73673  +  if( rc==SQLITE_OK ){
 73609  73674       if( eOp==SAVEPOINT_ROLLBACK ){
 73610         -      db->nDeferredCons = p->nStmtDefCons;
 73611         -      db->nDeferredImmCons = p->nStmtDefImmCons;
        73675  +      rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
 73612  73676       }
        73677  +    if( rc==SQLITE_OK ){
        73678  +      rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
        73679  +    }
        73680  +  }
        73681  +
        73682  +  /* If the statement transaction is being rolled back, also restore the 
        73683  +  ** database handles deferred constraint counter to the value it had when 
        73684  +  ** the statement transaction was opened.  */
        73685  +  if( eOp==SAVEPOINT_ROLLBACK ){
        73686  +    db->nDeferredCons = p->nStmtDefCons;
        73687  +    db->nDeferredImmCons = p->nStmtDefImmCons;
 73613  73688     }
 73614  73689     return rc;
 73615  73690   }
        73691  +SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
        73692  +  if( p->db->nStatement && p->iStatement ){
        73693  +    return vdbeCloseStatement(p, eOp);
        73694  +  }
        73695  +  return SQLITE_OK;
        73696  +}
        73697  +
 73616  73698   
 73617  73699   /*
 73618  73700   ** This function is called when a transaction opened by the database 
 73619  73701   ** handle associated with the VM passed as an argument is about to be 
 73620  73702   ** committed. If there are outstanding deferred foreign key constraint
 73621  73703   ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
 73622  73704   **
................................................................................
 75643  75725   ** If the second argument is not NULL, release any allocations associated 
 75644  75726   ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
 75645  75727   ** structure itself, using sqlite3DbFree().
 75646  75728   **
 75647  75729   ** This function is used to free UnpackedRecord structures allocated by
 75648  75730   ** the vdbeUnpackRecord() function found in vdbeapi.c.
 75649  75731   */
 75650         -static void vdbeFreeUnpacked(sqlite3 *db, UnpackedRecord *p){
        75732  +static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
 75651  75733     if( p ){
 75652  75734       int i;
 75653         -    for(i=0; i<p->nField; i++){
        75735  +    for(i=0; i<nField; i++){
 75654  75736         Mem *pMem = &p->aMem[i];
 75655  75737         if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
 75656  75738       }
 75657  75739       sqlite3DbFree(db, p);
 75658  75740     }
 75659  75741   }
 75660  75742   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
................................................................................
 75679  75761     i64 iKey2;
 75680  75762     PreUpdate preupdate;
 75681  75763     const char *zTbl = pTab->zName;
 75682  75764     static const u8 fakeSortOrder = 0;
 75683  75765   
 75684  75766     assert( db->pPreUpdate==0 );
 75685  75767     memset(&preupdate, 0, sizeof(PreUpdate));
 75686         -  if( op==SQLITE_UPDATE ){
 75687         -    iKey2 = v->aMem[iReg].u.i;
        75768  +  if( HasRowid(pTab)==0 ){
        75769  +    iKey1 = iKey2 = 0;
        75770  +    preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
 75688  75771     }else{
 75689         -    iKey2 = iKey1;
        75772  +    if( op==SQLITE_UPDATE ){
        75773  +      iKey2 = v->aMem[iReg].u.i;
        75774  +    }else{
        75775  +      iKey2 = iKey1;
        75776  +    }
 75690  75777     }
 75691  75778   
 75692  75779     assert( pCsr->nField==pTab->nCol 
 75693  75780          || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
 75694  75781     );
 75695  75782   
 75696  75783     preupdate.v = v;
................................................................................
 75705  75792     preupdate.iKey2 = iKey2;
 75706  75793     preupdate.pTab = pTab;
 75707  75794   
 75708  75795     db->pPreUpdate = &preupdate;
 75709  75796     db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
 75710  75797     db->pPreUpdate = 0;
 75711  75798     sqlite3DbFree(db, preupdate.aRecord);
 75712         -  vdbeFreeUnpacked(db, preupdate.pUnpacked);
 75713         -  vdbeFreeUnpacked(db, preupdate.pNewUnpacked);
        75799  +  vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pUnpacked);
        75800  +  vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pNewUnpacked);
 75714  75801     if( preupdate.aNew ){
 75715  75802       int i;
 75716  75803       for(i=0; i<pCsr->nField; i++){
 75717  75804         sqlite3VdbeMemRelease(&preupdate.aNew[i]);
 75718  75805       }
 75719  75806       sqlite3DbFree(db, preupdate.aNew);
 75720  75807     }
................................................................................
 77381  77468   
 77382  77469   /*
 77383  77470   ** This function is called from within a pre-update callback to retrieve
 77384  77471   ** a field of the row currently being updated or deleted.
 77385  77472   */
 77386  77473   SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
 77387  77474     PreUpdate *p = db->pPreUpdate;
        77475  +  Mem *pMem;
 77388  77476     int rc = SQLITE_OK;
 77389  77477   
 77390  77478     /* Test that this call is being made from within an SQLITE_DELETE or
 77391  77479     ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
 77392  77480     if( !p || p->op==SQLITE_INSERT ){
 77393  77481       rc = SQLITE_MISUSE_BKPT;
 77394  77482       goto preupdate_old_out;
 77395  77483     }
        77484  +  if( p->pPk ){
        77485  +    iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
        77486  +  }
 77396  77487     if( iIdx>=p->pCsr->nField || iIdx<0 ){
 77397  77488       rc = SQLITE_RANGE;
 77398  77489       goto preupdate_old_out;
 77399  77490     }
 77400  77491   
 77401  77492     /* If the old.* record has not yet been loaded into memory, do so now. */
 77402  77493     if( p->pUnpacked==0 ){
................................................................................
 77414  77505       if( rc!=SQLITE_OK ){
 77415  77506         sqlite3DbFree(db, aRec);
 77416  77507         goto preupdate_old_out;
 77417  77508       }
 77418  77509       p->aRecord = aRec;
 77419  77510     }
 77420  77511   
 77421         -  if( iIdx>=p->pUnpacked->nField ){
        77512  +  pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
        77513  +  if( iIdx==p->pTab->iPKey ){
        77514  +    sqlite3VdbeMemSetInt64(pMem, p->iKey1);
        77515  +  }else if( iIdx>=p->pUnpacked->nField ){
 77422  77516       *ppValue = (sqlite3_value *)columnNullValue();
 77423         -  }else{
 77424         -    Mem *pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
 77425         -    *ppValue = &p->pUnpacked->aMem[iIdx];
 77426         -    if( iIdx==p->pTab->iPKey ){
 77427         -      sqlite3VdbeMemSetInt64(pMem, p->iKey1);
 77428         -    }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
 77429         -      if( pMem->flags & MEM_Int ){
 77430         -        sqlite3VdbeMemRealify(pMem);
 77431         -      }
        77517  +  }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
        77518  +    if( pMem->flags & MEM_Int ){
        77519  +      sqlite3VdbeMemRealify(pMem);
 77432  77520       }
 77433  77521     }
 77434  77522   
 77435  77523    preupdate_old_out:
 77436  77524     sqlite3Error(db, rc);
 77437  77525     return sqlite3ApiExit(db, rc);
 77438  77526   }
................................................................................
 77477  77565     int rc = SQLITE_OK;
 77478  77566     Mem *pMem;
 77479  77567   
 77480  77568     if( !p || p->op==SQLITE_DELETE ){
 77481  77569       rc = SQLITE_MISUSE_BKPT;
 77482  77570       goto preupdate_new_out;
 77483  77571     }
        77572  +  if( p->pPk && p->op!=SQLITE_UPDATE ){
        77573  +    iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
        77574  +  }
 77484  77575     if( iIdx>=p->pCsr->nField || iIdx<0 ){
 77485  77576       rc = SQLITE_RANGE;
 77486  77577       goto preupdate_new_out;
 77487  77578     }
 77488  77579   
 77489  77580     if( p->op==SQLITE_INSERT ){
 77490  77581       /* For an INSERT, memory cell p->iNewReg contains the serialized record
................................................................................
 77497  77588         pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
 77498  77589         if( !pUnpack ){
 77499  77590           rc = SQLITE_NOMEM;
 77500  77591           goto preupdate_new_out;
 77501  77592         }
 77502  77593         p->pNewUnpacked = pUnpack;
 77503  77594       }
 77504         -    if( iIdx>=pUnpack->nField ){
        77595  +    pMem = &pUnpack->aMem[iIdx];
        77596  +    if( iIdx==p->pTab->iPKey ){
        77597  +      sqlite3VdbeMemSetInt64(pMem, p->iKey2);
        77598  +    }else if( iIdx>=pUnpack->nField ){
 77505  77599         pMem = (sqlite3_value *)columnNullValue();
 77506         -    }else{
 77507         -      pMem = &pUnpack->aMem[iIdx];
 77508         -      if( iIdx==p->pTab->iPKey ){
 77509         -        sqlite3VdbeMemSetInt64(pMem, p->iKey2);
 77510         -      }
 77511  77600       }
 77512  77601     }else{
 77513  77602       /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
 77514  77603       ** value. Make a copy of the cell contents and return a pointer to it.
 77515  77604       ** It is not safe to return a pointer to the memory cell itself as the
 77516  77605       ** caller may modify the value text encoding.
 77517  77606       */
................................................................................
 78480  78569     unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
 78481  78570   #endif
 78482  78571     Mem *aMem = p->aMem;       /* Copy of p->aMem */
 78483  78572     Mem *pIn1 = 0;             /* 1st input operand */
 78484  78573     Mem *pIn2 = 0;             /* 2nd input operand */
 78485  78574     Mem *pIn3 = 0;             /* 3rd input operand */
 78486  78575     Mem *pOut = 0;             /* Output operand */
 78487         -  int *aPermute = 0;         /* Permutation of columns for OP_Compare */
 78488         -  i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
 78489  78576   #ifdef VDBE_PROFILE
 78490  78577     u64 start;                 /* CPU clock count at start of opcode */
 78491  78578   #endif
 78492  78579     /*** INSERT STACK UNION HERE ***/
 78493  78580   
 78494  78581     assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
 78495  78582     sqlite3VdbeEnter(p);
................................................................................
 78496  78583     if( p->rc==SQLITE_NOMEM ){
 78497  78584       /* This happens if a malloc() inside a call to sqlite3_column_text() or
 78498  78585       ** sqlite3_column_text16() failed.  */
 78499  78586       goto no_mem;
 78500  78587     }
 78501  78588     assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
 78502  78589     assert( p->bIsReader || p->readOnly!=0 );
 78503         -  p->rc = SQLITE_OK;
 78504  78590     p->iCurrentTime = 0;
 78505  78591     assert( p->explain==0 );
 78506  78592     p->pResultSet = 0;
 78507  78593     db->busyHandler.nBusy = 0;
 78508  78594     if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 78509  78595     sqlite3VdbeIOTraceSql(p);
 78510  78596   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
................................................................................
 78857  78943     if( pOp->p1==SQLITE_OK && p->pFrame ){
 78858  78944       /* Halt the sub-program. Return control to the parent frame. */
 78859  78945       pFrame = p->pFrame;
 78860  78946       p->pFrame = pFrame->pParent;
 78861  78947       p->nFrame--;
 78862  78948       sqlite3VdbeSetChanges(db, p->nChange);
 78863  78949       pcx = sqlite3VdbeFrameRestore(pFrame);
 78864         -    lastRowid = db->lastRowid;
 78865  78950       if( pOp->p2==OE_Ignore ){
 78866  78951         /* Instruction pcx is the OP_Program that invoked the sub-program 
 78867  78952         ** currently being halted. If the p2 instruction of this OP_Halt
 78868  78953         ** instruction is set to OE_Ignore, then the sub-program is throwing
 78869  78954         ** an IGNORE exception. In this case jump to the address specified
 78870  78955         ** as the p2 of the calling OP_Program.  */
 78871  78956         pcx = p->aOp[pcx].p2-1;
................................................................................
 79579  79664     for(i=0; i<pCtx->argc; i++){
 79580  79665       assert( memIsValid(pCtx->argv[i]) );
 79581  79666       REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
 79582  79667     }
 79583  79668   #endif
 79584  79669     MemSetTypeFlag(pCtx->pOut, MEM_Null);
 79585  79670     pCtx->fErrorOrAux = 0;
 79586         -  db->lastRowid = lastRowid;
 79587  79671     (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
 79588         -  lastRowid = db->lastRowid;  /* Remember rowid changes made by xSFunc */
 79589  79672   
 79590  79673     /* If the function returned an error, throw an exception */
 79591  79674     if( pCtx->fErrorOrAux ){
 79592  79675       if( pCtx->isError ){
 79593  79676         sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
 79594  79677         rc = pCtx->isError;
 79595  79678       }
................................................................................
 80037  80120     if( iCompare!=0 ) goto jump_to_p2;
 80038  80121     break;
 80039  80122   }
 80040  80123   
 80041  80124   
 80042  80125   /* Opcode: Permutation * * * P4 *
 80043  80126   **
 80044         -** Set the permutation used by the OP_Compare operator to be the array
 80045         -** of integers in P4.
        80127  +** Set the permutation used by the OP_Compare operator in the next
        80128  +** instruction.  The permutation is stored in the P4 operand.
 80046  80129   **
 80047  80130   ** The permutation is only valid until the next OP_Compare that has
 80048  80131   ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 
 80049  80132   ** occur immediately prior to the OP_Compare.
 80050  80133   **
 80051  80134   ** The first integer in the P4 integer array is the length of the array
 80052  80135   ** and does not become part of the permutation.
 80053  80136   */
 80054  80137   case OP_Permutation: {
 80055  80138     assert( pOp->p4type==P4_INTARRAY );
 80056  80139     assert( pOp->p4.ai );
 80057         -  aPermute = pOp->p4.ai + 1;
        80140  +  assert( pOp[1].opcode==OP_Compare );
        80141  +  assert( pOp[1].p5 & OPFLAG_PERMUTE );
 80058  80142     break;
 80059  80143   }
 80060  80144   
 80061  80145   /* Opcode: Compare P1 P2 P3 P4 P5
 80062  80146   ** Synopsis: r[P1@P3] <-> r[P2@P3]
 80063  80147   **
 80064  80148   ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
................................................................................
 80083  80167     int i;
 80084  80168     int p1;
 80085  80169     int p2;
 80086  80170     const KeyInfo *pKeyInfo;
 80087  80171     int idx;
 80088  80172     CollSeq *pColl;    /* Collating sequence to use on this term */
 80089  80173     int bRev;          /* True for DESCENDING sort order */
        80174  +  int *aPermute;     /* The permutation */
 80090  80175   
 80091         -  if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
        80176  +  if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
        80177  +    aPermute = 0;
        80178  +  }else{
        80179  +    assert( pOp>aOp );
        80180  +    assert( pOp[-1].opcode==OP_Permutation );
        80181  +    assert( pOp[-1].p4type==P4_INTARRAY );
        80182  +    aPermute = pOp[-1].p4.ai + 1;
        80183  +    assert( aPermute!=0 );
        80184  +  }
 80092  80185     n = pOp->p3;
 80093  80186     pKeyInfo = pOp->p4.pKeyInfo;
 80094  80187     assert( n>0 );
 80095  80188     assert( pKeyInfo!=0 );
 80096  80189     p1 = pOp->p1;
 80097  80190     p2 = pOp->p2;
 80098  80191   #if SQLITE_DEBUG
................................................................................
 80117  80210       bRev = pKeyInfo->aSortOrder[i];
 80118  80211       iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
 80119  80212       if( iCompare ){
 80120  80213         if( bRev ) iCompare = -iCompare;
 80121  80214         break;
 80122  80215       }
 80123  80216     }
 80124         -  aPermute = 0;
 80125  80217     break;
 80126  80218   }
 80127  80219   
 80128  80220   /* Opcode: Jump P1 P2 P3 * *
 80129  80221   **
 80130  80222   ** Jump to the instruction at address P1, P2, or P3 depending on whether
 80131  80223   ** in the most recent OP_Compare instruction the P1 vector was less than
................................................................................
 80673  80765     if( zAffinity ){
 80674  80766       pRec = pData0;
 80675  80767       do{
 80676  80768         applyAffinity(pRec++, *(zAffinity++), encoding);
 80677  80769         assert( zAffinity[0]==0 || pRec<=pLast );
 80678  80770       }while( zAffinity[0] );
 80679  80771     }
        80772  +
        80773  +#ifdef SQLITE_ENABLE_NULL_TRIM
        80774  +  /* NULLs can be safely trimmed from the end of the record, as long as
        80775  +  ** as the schema format is 2 or more and none of the omitted columns
        80776  +  ** have a non-NULL default value.  Also, the record must be left with
        80777  +  ** at least one field.  If P5>0 then it will be one more than the
        80778  +  ** index of the right-most column with a non-NULL default value */
        80779  +  if( pOp->p5 ){
        80780  +    while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
        80781  +      pLast--;
        80782  +      nField--;
        80783  +    }
        80784  +  }
        80785  +#endif
 80680  80786   
 80681  80787     /* Loop through the elements that will make up the record to figure
 80682  80788     ** out how much space is required for the new record.
 80683  80789     */
 80684  80790     pRec = pLast;
 80685  80791     do{
 80686  80792       assert( memIsValid(pRec) );
................................................................................
 82263  82369     pData = &aMem[pOp->p2];
 82264  82370     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 82265  82371     assert( memIsValid(pData) );
 82266  82372     pC = p->apCsr[pOp->p1];
 82267  82373     assert( pC!=0 );
 82268  82374     assert( pC->eCurType==CURTYPE_BTREE );
 82269  82375     assert( pC->uc.pCursor!=0 );
 82270         -  assert( pC->isTable );
        82376  +  assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
 82271  82377     assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
 82272  82378     REGISTER_TRACE(pOp->p2, pData);
 82273  82379   
 82274  82380     if( pOp->opcode==OP_Insert ){
 82275  82381       pKey = &aMem[pOp->p3];
 82276  82382       assert( pKey->flags & MEM_Int );
 82277  82383       assert( memIsValid(pKey) );
................................................................................
 82279  82385       x.nKey = pKey->u.i;
 82280  82386     }else{
 82281  82387       assert( pOp->opcode==OP_InsertInt );
 82282  82388       x.nKey = pOp->p3;
 82283  82389     }
 82284  82390   
 82285  82391     if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
 82286         -    assert( pC->isTable );
 82287  82392       assert( pC->iDb>=0 );
 82288  82393       zDb = db->aDb[pC->iDb].zDbSName;
 82289  82394       pTab = pOp->p4.pTab;
 82290         -    assert( HasRowid(pTab) );
        82395  +    assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
 82291  82396       op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
 82292  82397     }else{
 82293         -    pTab = 0; /* Not needed.  Silence a comiler warning. */
        82398  +    pTab = 0; /* Not needed.  Silence a compiler warning. */
 82294  82399       zDb = 0;  /* Not needed.  Silence a compiler warning. */
 82295  82400     }
 82296  82401   
 82297  82402   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 82298  82403     /* Invoke the pre-update hook, if any */
 82299  82404     if( db->xPreUpdateCallback 
 82300  82405      && pOp->p4type==P4_TABLE
 82301  82406      && !(pOp->p5 & OPFLAG_ISUPDATE)
 82302  82407     ){
 82303  82408       sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
 82304  82409     }
        82410  +  if( pOp->p5 & OPFLAG_ISNOOP ) break;
 82305  82411   #endif
 82306  82412   
 82307  82413     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 82308         -  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = x.nKey;
        82414  +  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
 82309  82415     if( pData->flags & MEM_Null ){
 82310  82416       x.pData = 0;
 82311  82417       x.nData = 0;
 82312  82418     }else{
 82313  82419       assert( pData->flags & (MEM_Blob|MEM_Str) );
 82314  82420       x.pData = pData->z;
 82315  82421       x.nData = pData->n;
................................................................................
 82410  82516     }else{
 82411  82517       zDb = 0;   /* Not needed.  Silence a compiler warning. */
 82412  82518       pTab = 0;  /* Not needed.  Silence a compiler warning. */
 82413  82519     }
 82414  82520   
 82415  82521   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 82416  82522     /* Invoke the pre-update-hook if required. */
 82417         -  if( db->xPreUpdateCallback && pOp->p4.pTab && HasRowid(pTab) ){
 82418         -    assert( !(opflags & OPFLAG_ISUPDATE) || (aMem[pOp->p3].flags & MEM_Int) );
        82523  +  if( db->xPreUpdateCallback && pOp->p4.pTab ){
        82524  +    assert( !(opflags & OPFLAG_ISUPDATE) 
        82525  +         || HasRowid(pTab)==0 
        82526  +         || (aMem[pOp->p3].flags & MEM_Int) 
        82527  +    );
 82419  82528       sqlite3VdbePreUpdateHook(p, pC,
 82420  82529           (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, 
 82421  82530           zDb, pTab, pC->movetoTarget,
 82422  82531           pOp->p3
 82423  82532       );
 82424  82533     }
 82425  82534     if( opflags & OPFLAG_ISNOOP ) break;
................................................................................
 83746  83855           || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
 83747  83856       assert( pProgram->nCsr==pFrame->nChildCsr );
 83748  83857       assert( (int)(pOp - aOp)==pFrame->pc );
 83749  83858     }
 83750  83859   
 83751  83860     p->nFrame++;
 83752  83861     pFrame->pParent = p->pFrame;
 83753         -  pFrame->lastRowid = lastRowid;
        83862  +  pFrame->lastRowid = db->lastRowid;
 83754  83863     pFrame->nChange = p->nChange;
 83755  83864     pFrame->nDbChange = p->db->nChange;
 83756  83865     assert( pFrame->pAuxData==0 );
 83757  83866     pFrame->pAuxData = p->pAuxData;
 83758  83867     p->pAuxData = 0;
 83759  83868     p->nChange = 0;
 83760  83869     p->pFrame = pFrame;
................................................................................
 84687  84796       }
 84688  84797       db->vtabOnConflict = pOp->p5;
 84689  84798       rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
 84690  84799       db->vtabOnConflict = vtabOnConflict;
 84691  84800       sqlite3VtabImportErrmsg(p, pVtab);
 84692  84801       if( rc==SQLITE_OK && pOp->p1 ){
 84693  84802         assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
 84694         -      db->lastRowid = lastRowid = rowid;
        84803  +      db->lastRowid = rowid;
 84695  84804       }
 84696  84805       if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
 84697  84806         if( pOp->p5==OE_Ignore ){
 84698  84807           rc = SQLITE_OK;
 84699  84808         }else{
 84700  84809           p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
 84701  84810         }
................................................................................
 84923  85032       sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
 84924  85033     }
 84925  85034   
 84926  85035     /* This is the only way out of this procedure.  We have to
 84927  85036     ** release the mutexes on btrees that were acquired at the
 84928  85037     ** top. */
 84929  85038   vdbe_return:
 84930         -  db->lastRowid = lastRowid;
 84931  85039     testcase( nVmStep>0 );
 84932  85040     p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
 84933  85041     sqlite3VdbeLeave(p);
 84934  85042     assert( rc!=SQLITE_OK || nExtraDelete==0 
 84935  85043          || sqlite3_strlike("DELETE%",p->zSql,0)!=0 
 84936  85044     );
 84937  85045     return rc;
................................................................................
 84987  85095   #ifndef SQLITE_OMIT_INCRBLOB
 84988  85096   
 84989  85097   /*
 84990  85098   ** Valid sqlite3_blob* handles point to Incrblob structures.
 84991  85099   */
 84992  85100   typedef struct Incrblob Incrblob;
 84993  85101   struct Incrblob {
 84994         -  int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
 84995  85102     int nByte;              /* Size of open blob, in bytes */
 84996  85103     int iOffset;            /* Byte offset of blob in cursor data */
 84997         -  int iCol;               /* Table column this handle is open on */
        85104  +  u16 iCol;               /* Table column this handle is open on */
 84998  85105     BtCursor *pCsr;         /* Cursor pointing at blob row */
 84999  85106     sqlite3_stmt *pStmt;    /* Statement holding cursor open */
 85000  85107     sqlite3 *db;            /* The associated database */
 85001  85108     char *zDb;              /* Database name */
 85002  85109     Table *pTab;            /* Table object */
 85003  85110   };
 85004  85111   
................................................................................
 85021  85128   ** immediately return SQLITE_ABORT.
 85022  85129   */
 85023  85130   static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
 85024  85131     int rc;                         /* Error code */
 85025  85132     char *zErr = 0;                 /* Error message */
 85026  85133     Vdbe *v = (Vdbe *)p->pStmt;
 85027  85134   
 85028         -  /* Set the value of the SQL statements only variable to integer iRow. 
 85029         -  ** This is done directly instead of using sqlite3_bind_int64() to avoid 
 85030         -  ** triggering asserts related to mutexes.
        85135  +  /* Set the value of register r[1] in the SQL statement to integer iRow. 
        85136  +  ** This is done directly as a performance optimization
 85031  85137     */
 85032         -  assert( v->aVar[0].flags&MEM_Int );
 85033         -  v->aVar[0].u.i = iRow;
        85138  +  v->aMem[1].flags = MEM_Int;
        85139  +  v->aMem[1].u.i = iRow;
 85034  85140   
 85035         -  rc = sqlite3_step(p->pStmt);
        85141  +  /* If the statement has been run before (and is paused at the OP_ResultRow)
        85142  +  ** then back it up to the point where it does the OP_SeekRowid.  This could
        85143  +  ** have been down with an extra OP_Goto, but simply setting the program
        85144  +  ** counter is faster. */
        85145  +  if( v->pc>3 ){
        85146  +    v->pc = 3;
        85147  +    rc = sqlite3VdbeExec(v);
        85148  +  }else{
        85149  +    rc = sqlite3_step(p->pStmt);
        85150  +  }
 85036  85151     if( rc==SQLITE_ROW ){
 85037  85152       VdbeCursor *pC = v->apCsr[0];
 85038         -    u32 type = pC->aType[p->iCol];
        85153  +    u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
        85154  +    testcase( pC->nHdrParsed==p->iCol );
        85155  +    testcase( pC->nHdrParsed==p->iCol+1 );
 85039  85156       if( type<12 ){
 85040  85157         zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
 85041  85158             type==0?"null": type==7?"real": "integer"
 85042  85159         );
 85043  85160         rc = SQLITE_ERROR;
 85044  85161         sqlite3_finalize(p->pStmt);
 85045  85162         p->pStmt = 0;
................................................................................
 85076  85193   */
 85077  85194   SQLITE_API int sqlite3_blob_open(
 85078  85195     sqlite3* db,            /* The database connection */
 85079  85196     const char *zDb,        /* The attached database containing the blob */
 85080  85197     const char *zTable,     /* The table containing the blob */
 85081  85198     const char *zColumn,    /* The column containing the blob */
 85082  85199     sqlite_int64 iRow,      /* The row containing the glob */
 85083         -  int flags,              /* True -> read/write access, false -> read-only */
        85200  +  int wrFlag,             /* True -> read/write access, false -> read-only */
 85084  85201     sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
 85085  85202   ){
 85086  85203     int nAttempt = 0;
 85087  85204     int iCol;               /* Index of zColumn in row-record */
 85088  85205     int rc = SQLITE_OK;
 85089  85206     char *zErr = 0;
 85090  85207     Table *pTab;
................................................................................
 85098  85215   #endif
 85099  85216     *ppBlob = 0;
 85100  85217   #ifdef SQLITE_ENABLE_API_ARMOR
 85101  85218     if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
 85102  85219       return SQLITE_MISUSE_BKPT;
 85103  85220     }
 85104  85221   #endif
 85105         -  flags = !!flags;                /* flags = (flags ? 1 : 0); */
        85222  +  wrFlag = !!wrFlag;                /* wrFlag = (wrFlag ? 1 : 0); */
 85106  85223   
 85107  85224     sqlite3_mutex_enter(db->mutex);
 85108  85225   
 85109  85226     pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
 85110  85227     if( !pBlob ) goto blob_open_out;
 85111  85228     pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
 85112  85229     if( !pParse ) goto blob_open_out;
................................................................................
 85158  85275         rc = SQLITE_ERROR;
 85159  85276         sqlite3BtreeLeaveAll(db);
 85160  85277         goto blob_open_out;
 85161  85278       }
 85162  85279   
 85163  85280       /* If the value is being opened for writing, check that the
 85164  85281       ** column is not indexed, and that it is not part of a foreign key. 
 85165         -    ** It is against the rules to open a column to which either of these
 85166         -    ** descriptions applies for writing.  */
 85167         -    if( flags ){
        85282  +    */
        85283  +    if( wrFlag ){
 85168  85284         const char *zFault = 0;
 85169  85285         Index *pIdx;
 85170  85286   #ifndef SQLITE_OMIT_FOREIGN_KEY
 85171  85287         if( db->flags&SQLITE_ForeignKeys ){
 85172  85288           /* Check that the column is not part of an FK child key definition. It
 85173  85289           ** is not necessary to check if it is part of a parent key, as parent
 85174  85290           ** key columns must be indexed. The check below will pick up this 
................................................................................
 85221  85337         ** which closes the b-tree cursor and (possibly) commits the 
 85222  85338         ** transaction.
 85223  85339         */
 85224  85340         static const int iLn = VDBE_OFFSET_LINENO(2);
 85225  85341         static const VdbeOpList openBlob[] = {
 85226  85342           {OP_TableLock,      0, 0, 0},  /* 0: Acquire a read or write lock */
 85227  85343           {OP_OpenRead,       0, 0, 0},  /* 1: Open a cursor */
 85228         -        {OP_Variable,       1, 1, 0},  /* 2: Move ?1 into reg[1] */
 85229         -        {OP_NotExists,      0, 7, 1},  /* 3: Seek the cursor */
 85230         -        {OP_Column,         0, 0, 1},  /* 4  */
 85231         -        {OP_ResultRow,      1, 0, 0},  /* 5  */
 85232         -        {OP_Goto,           0, 2, 0},  /* 6  */
 85233         -        {OP_Halt,           0, 0, 0},  /* 7  */
        85344  +        /* blobSeekToRow() will initialize r[1] to the desired rowid */
        85345  +        {OP_NotExists,      0, 5, 1},  /* 2: Seek the cursor to rowid=r[1] */
        85346  +        {OP_Column,         0, 0, 1},  /* 3  */
        85347  +        {OP_ResultRow,      1, 0, 0},  /* 4  */
        85348  +        {OP_Halt,           0, 0, 0},  /* 5  */
 85234  85349         };
 85235  85350         Vdbe *v = (Vdbe *)pBlob->pStmt;
 85236  85351         int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 85237  85352         VdbeOp *aOp;
 85238  85353   
 85239         -      sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, 
        85354  +      sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag, 
 85240  85355                              pTab->pSchema->schema_cookie,
 85241  85356                              pTab->pSchema->iGeneration);
 85242  85357         sqlite3VdbeChangeP5(v, 1);     
 85243  85358         aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
 85244  85359   
 85245  85360         /* Make sure a mutex is held on the table to be accessed */
 85246  85361         sqlite3VdbeUsesBtree(v, iDb); 
................................................................................
 85249  85364           assert( aOp!=0 );
 85250  85365           /* Configure the OP_TableLock instruction */
 85251  85366   #ifdef SQLITE_OMIT_SHARED_CACHE
 85252  85367           aOp[0].opcode = OP_Noop;
 85253  85368   #else
 85254  85369           aOp[0].p1 = iDb;
 85255  85370           aOp[0].p2 = pTab->tnum;
 85256         -        aOp[0].p3 = flags;
        85371  +        aOp[0].p3 = wrFlag;
 85257  85372           sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
 85258  85373         }
 85259  85374         if( db->mallocFailed==0 ){
 85260  85375   #endif
 85261  85376   
 85262  85377           /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
 85263  85378           ** parameter of the other to pTab->tnum.  */
 85264         -        if( flags ) aOp[1].opcode = OP_OpenWrite;
        85379  +        if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
 85265  85380           aOp[1].p2 = pTab->tnum;
 85266  85381           aOp[1].p3 = iDb;   
 85267  85382   
 85268  85383           /* Configure the number of columns. Configure the cursor to
 85269  85384           ** think that the table has one more column than it really
 85270  85385           ** does. An OP_Column to retrieve this imaginary column will
 85271  85386           ** always return an SQL NULL. This is useful because it means
 85272  85387           ** we can invoke OP_Column to fill in the vdbe cursors type 
 85273  85388           ** and offset cache without causing any IO.
 85274  85389           */
 85275  85390           aOp[1].p4type = P4_INT32;
 85276  85391           aOp[1].p4.i = pTab->nCol+1;
 85277         -        aOp[4].p2 = pTab->nCol;
        85392  +        aOp[3].p2 = pTab->nCol;
 85278  85393   
 85279         -        pParse->nVar = 1;
        85394  +        pParse->nVar = 0;
 85280  85395           pParse->nMem = 1;
 85281  85396           pParse->nTab = 1;
 85282  85397           sqlite3VdbeMakeReady(v, pParse);
 85283  85398         }
 85284  85399       }
 85285  85400      
 85286         -    pBlob->flags = flags;
 85287  85401       pBlob->iCol = iCol;
 85288  85402       pBlob->db = db;
 85289  85403       sqlite3BtreeLeaveAll(db);
 85290  85404       if( db->mallocFailed ){
 85291  85405         goto blob_open_out;
 85292  85406       }
 85293         -    sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
 85294  85407       rc = blobSeekToRow(pBlob, iRow, &zErr);
 85295  85408     } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
 85296  85409   
 85297  85410   blob_open_out:
 85298  85411     if( rc==SQLITE_OK && db->mallocFailed==0 ){
 85299  85412       *ppBlob = (sqlite3_blob *)pBlob;
 85300  85413     }else{
................................................................................
 88823  88936   *************************************************************************
 88824  88937   **
 88825  88938   ** This file contains routines used for walking the parser tree and
 88826  88939   ** resolve all identifiers by associating them with a particular
 88827  88940   ** table and column.
 88828  88941   */
 88829  88942   /* #include "sqliteInt.h" */
 88830         -/* #include <stdlib.h> */
 88831         -/* #include <string.h> */
 88832  88943   
 88833  88944   /*
 88834  88945   ** Walk the expression tree pExpr and increase the aggregate function
 88835  88946   ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
 88836  88947   ** This needs to occur when copying a TK_AGG_FUNCTION node from an
 88837  88948   ** outer query into an inner subquery.
 88838  88949   **
................................................................................
 91319  91430       x = (ynVar)(++pParse->nVar);
 91320  91431     }else{
 91321  91432       int doAdd = 0;
 91322  91433       if( z[0]=='?' ){
 91323  91434         /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
 91324  91435         ** use it as the variable number */
 91325  91436         i64 i;
 91326         -      int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
 91327         -      x = (ynVar)i;
        91437  +      int bOk;
        91438  +      if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
        91439  +        i = z[1]-'0';  /* The common case of ?N for a single digit N */
        91440  +        bOk = 1;
        91441  +      }else{
        91442  +        bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
        91443  +      }
 91328  91444         testcase( i==0 );
 91329  91445         testcase( i==1 );
 91330  91446         testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
 91331  91447         testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
 91332  91448         if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
 91333  91449           sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
 91334  91450               db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
 91335  91451           return;
 91336  91452         }
        91453  +      x = (ynVar)i;
 91337  91454         if( x>pParse->nVar ){
 91338  91455           pParse->nVar = (int)x;
 91339  91456           doAdd = 1;
 91340  91457         }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
 91341  91458           doAdd = 1;
 91342  91459         }
 91343  91460       }else{
................................................................................
 91764  91881       struct IdList_item *pNewItem = &pNew->a[i];
 91765  91882       struct IdList_item *pOldItem = &p->a[i];
 91766  91883       pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 91767  91884       pNewItem->idx = pOldItem->idx;
 91768  91885     }
 91769  91886     return pNew;
 91770  91887   }
 91771         -SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
 91772         -  Select *pNew, *pPrior;
        91888  +SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
        91889  +  Select *pRet = 0;
        91890  +  Select *pNext = 0;
        91891  +  Select **pp = &pRet;
        91892  +  Select *p;
        91893  +
 91773  91894     assert( db!=0 );
 91774         -  if( p==0 ) return 0;
 91775         -  pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
 91776         -  if( pNew==0 ) return 0;
 91777         -  pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
 91778         -  pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
 91779         -  pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
 91780         -  pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
 91781         -  pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
 91782         -  pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
 91783         -  pNew->op = p->op;
 91784         -  pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
 91785         -  if( pPrior ) pPrior->pNext = pNew;
 91786         -  pNew->pNext = 0;
 91787         -  pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
 91788         -  pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
 91789         -  pNew->iLimit = 0;
 91790         -  pNew->iOffset = 0;
 91791         -  pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
 91792         -  pNew->addrOpenEphm[0] = -1;
 91793         -  pNew->addrOpenEphm[1] = -1;
 91794         -  pNew->nSelectRow = p->nSelectRow;
 91795         -  pNew->pWith = withDup(db, p->pWith);
 91796         -  sqlite3SelectSetName(pNew, p->zSelName);
 91797         -  return pNew;
        91895  +  for(p=pDup; p; p=p->pPrior){
        91896  +    Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
        91897  +    if( pNew==0 ) break;
        91898  +    pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
        91899  +    pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
        91900  +    pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
        91901  +    pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
        91902  +    pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
        91903  +    pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
        91904  +    pNew->op = p->op;
        91905  +    pNew->pNext = pNext;
        91906  +    pNew->pPrior = 0;
        91907  +    pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
        91908  +    pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
        91909  +    pNew->iLimit = 0;
        91910  +    pNew->iOffset = 0;
        91911  +    pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
        91912  +    pNew->addrOpenEphm[0] = -1;
        91913  +    pNew->addrOpenEphm[1] = -1;
        91914  +    pNew->nSelectRow = p->nSelectRow;
        91915  +    pNew->pWith = withDup(db, p->pWith);
        91916  +    sqlite3SelectSetName(pNew, p->zSelName);
        91917  +    *pp = pNew;
        91918  +    pp = &pNew->pPrior;
        91919  +    pNext = pNew;
        91920  +  }
        91921  +
        91922  +  return pRet;
 91798  91923   }
 91799  91924   #else
 91800  91925   SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
 91801  91926     assert( p==0 );
 91802  91927     return 0;
 91803  91928   }
 91804  91929   #endif
................................................................................
 97076  97201   /*
 97077  97202   ** Implementation of the stat_get(P,J) SQL function.  This routine is
 97078  97203   ** used to query statistical information that has been gathered into
 97079  97204   ** the Stat4Accum object by prior calls to stat_push().  The P parameter
 97080  97205   ** has type BLOB but it is really just a pointer to the Stat4Accum object.
 97081  97206   ** The content to returned is determined by the parameter J
 97082  97207   ** which is one of the STAT_GET_xxxx values defined above.
        97208  +**
        97209  +** The stat_get(P,J) function is not available to generic SQL.  It is
        97210  +** inserted as part of a manually constructed bytecode program.  (See
        97211  +** the callStatGet() routine below.)  It is guaranteed that the P
        97212  +** parameter will always be a poiner to a Stat4Accum object, never a
        97213  +** NULL.
 97083  97214   **
 97084  97215   ** If neither STAT3 nor STAT4 are enabled, then J is always
 97085  97216   ** STAT_GET_STAT1 and is hence omitted and this routine becomes
 97086  97217   ** a one-parameter function, stat_get(P), that always returns the
 97087  97218   ** stat1 table entry information.
 97088  97219   */
 97089  97220   static void statGet(
................................................................................
104670 104801     ** the update-hook is not invoked for rows removed by REPLACE, but the 
104671 104802     ** pre-update-hook is.
104672 104803     */ 
104673 104804     if( pTab->pSelect==0 ){
104674 104805       u8 p5 = 0;
104675 104806       sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
104676 104807       sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
104677         -    sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
       104808  +    if( pParse->nested==0 ){
       104809  +      sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
       104810  +    }
104678 104811       if( eMode!=ONEPASS_OFF ){
104679 104812         sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
104680 104813       }
104681 104814       if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
104682 104815         sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
104683 104816       }
104684 104817       if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
................................................................................
109803 109936       sqlite3VdbeJumpHere(v, ipkBottom);
109804 109937     }
109805 109938     
109806 109939     *pbMayReplace = seenReplace;
109807 109940     VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
109808 109941   }
109809 109942   
       109943  +#ifdef SQLITE_ENABLE_NULL_TRIM
       109944  +/*
       109945  +** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
       109946  +** to be the number of columns in table pTab that must not be NULL-trimmed.
       109947  +**
       109948  +** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
       109949  +*/
       109950  +SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
       109951  +  u16 i;
       109952  +
       109953  +  /* Records with omitted columns are only allowed for schema format
       109954  +  ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
       109955  +  if( pTab->pSchema->file_format<2 ) return;
       109956  +
       109957  +  for(i=pTab->nCol; i>1 && pTab->aCol[i-1].pDflt==0; i--){}
       109958  +  sqlite3VdbeChangeP5(v, i);
       109959  +}
       109960  +#endif
       109961  +
109810 109962   /*
109811 109963   ** This routine generates code to finish the INSERT or UPDATE operation
109812 109964   ** that was started by a prior call to sqlite3GenerateConstraintChecks.
109813 109965   ** A consecutive range of registers starting at regNewData contains the
109814 109966   ** rowid and the content to be inserted.
109815 109967   **
109816 109968   ** The arguments to this routine should be the same as the first six
................................................................................
109846 109998     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
109847 109999       if( aRegIdx[i]==0 ) continue;
109848 110000       bAffinityDone = 1;
109849 110001       if( pIdx->pPartIdxWhere ){
109850 110002         sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
109851 110003         VdbeCoverage(v);
109852 110004       }
109853         -    sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
109854         -                         aRegIdx[i]+1,
109855         -                         pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
109856         -    pik_flags = 0;
109857         -    if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
       110005  +    pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
109858 110006       if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
109859 110007         assert( pParse->nested==0 );
109860 110008         pik_flags |= OPFLAG_NCHANGE;
109861 110009         pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
       110010  +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
       110011  +      if( update_flags==0 ){
       110012  +        sqlite3VdbeAddOp4(v, OP_InsertInt, 
       110013  +            iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
       110014  +        );
       110015  +        sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
       110016  +      }
       110017  +#endif
109862 110018       }
       110019  +    sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
       110020  +                         aRegIdx[i]+1,
       110021  +                         pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
109863 110022       sqlite3VdbeChangeP5(v, pik_flags);
109864 110023     }
109865 110024     if( !HasRowid(pTab) ) return;
109866 110025     regData = regNewData + 1;
109867 110026     regRec = sqlite3GetTempReg(pParse);
109868 110027     sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
       110028  +  sqlite3SetMakeRecordP5(v, pTab);
109869 110029     if( !bAffinityDone ){
109870 110030       sqlite3TableAffinity(v, pTab, 0);
109871 110031       sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
109872 110032     }
109873 110033     if( pParse->nested ){
109874 110034       pik_flags = 0;
109875 110035     }else{
................................................................................
111096 111256   #endif
111097 111257   
111098 111258   #endif /* SQLITE3EXT_H */
111099 111259   
111100 111260   /************** End of sqlite3ext.h ******************************************/
111101 111261   /************** Continuing where we left off in loadext.c ********************/
111102 111262   /* #include "sqliteInt.h" */
111103         -/* #include <string.h> */
111104 111263   
111105 111264   #ifndef SQLITE_OMIT_LOAD_EXTENSION
111106 111265   /*
111107 111266   ** Some API routines are omitted when various features are
111108 111267   ** excluded from a build of SQLite.  Substitute a NULL pointer
111109 111268   ** for any missing APIs.
111110 111269   */
................................................................................
112760 112919     return azModeName[eMode];
112761 112920   }
112762 112921   
112763 112922   /*
112764 112923   ** Locate a pragma in the aPragmaName[] array.
112765 112924   */
112766 112925   static const PragmaName *pragmaLocate(const char *zName){
112767         -  int upr, lwr, mid, rc;
       112926  +  int upr, lwr, mid = 0, rc;
112768 112927     lwr = 0;
112769 112928     upr = ArraySize(aPragmaName)-1;
112770 112929     while( lwr<=upr ){
112771 112930       mid = (lwr+upr)/2;
112772 112931       rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
112773 112932       if( rc==0 ) break;
112774 112933       if( rc<0 ){
................................................................................
119803 119962         return SQLITE_ERROR;
119804 119963       }
119805 119964       assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
119806 119965   
119807 119966       pCte->zCteErr = "circular reference: %s";
119808 119967       pSavedWith = pParse->pWith;
119809 119968       pParse->pWith = pWith;
119810         -    sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
       119969  +    if( bMayRecursive ){
       119970  +      Select *pPrior = pSel->pPrior;
       119971  +      assert( pPrior->pWith==0 );
       119972  +      pPrior->pWith = pSel->pWith;
       119973  +      sqlite3WalkSelect(pWalker, pPrior);
       119974  +      pPrior->pWith = 0;
       119975  +    }else{
       119976  +      sqlite3WalkSelect(pWalker, pSel);
       119977  +    }
119811 119978       pParse->pWith = pWith;
119812 119979   
119813 119980       for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
119814 119981       pEList = pLeft->pEList;
119815 119982       if( pCte->pCols ){
119816 119983         if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
119817 119984           sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
................................................................................
119847 120014   **
119848 120015   ** This function is used as the xSelectCallback2() callback by
119849 120016   ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
119850 120017   ** names and other FROM clause elements. 
119851 120018   */
119852 120019   static void selectPopWith(Walker *pWalker, Select *p){
119853 120020     Parse *pParse = pWalker->pParse;
119854         -  With *pWith = findRightmost(p)->pWith;
119855         -  if( pWith!=0 ){
119856         -    assert( pParse->pWith==pWith );
119857         -    pParse->pWith = pWith->pOuter;
       120021  +  if( pParse->pWith && p->pPrior==0 ){
       120022  +    With *pWith = findRightmost(p)->pWith;
       120023  +    if( pWith!=0 ){
       120024  +      assert( pParse->pWith==pWith );
       120025  +      pParse->pWith = pWith->pOuter;
       120026  +    }
119858 120027     }
119859 120028   }
119860 120029   #else
119861 120030   #define selectPopWith 0
119862 120031   #endif
119863 120032   
119864 120033   /*
................................................................................
119900 120069       return WRC_Abort;
119901 120070     }
119902 120071     if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
119903 120072       return WRC_Prune;
119904 120073     }
119905 120074     pTabList = p->pSrc;
119906 120075     pEList = p->pEList;
119907         -  if( pWalker->xSelectCallback2==selectPopWith ){
119908         -    sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
       120076  +  if( p->pWith ){
       120077  +    sqlite3WithPush(pParse, p->pWith, 0);
119909 120078     }
119910 120079   
119911 120080     /* Make sure cursor numbers have been assigned to all entries in
119912 120081     ** the FROM clause of the SELECT statement.
119913 120082     */
119914 120083     sqlite3SrcListAssignCursors(pParse, pTabList);
119915 120084   
................................................................................
120188 120357     w.xExprCallback = sqlite3ExprWalkNoop;
120189 120358     w.pParse = pParse;
120190 120359     if( pParse->hasCompound ){
120191 120360       w.xSelectCallback = convertCompoundSelectToSubquery;
120192 120361       sqlite3WalkSelect(&w, pSelect);
120193 120362     }
120194 120363     w.xSelectCallback = selectExpander;
120195         -  if( (pSelect->selFlags & SF_MultiValue)==0 ){
120196         -    w.xSelectCallback2 = selectPopWith;
120197         -  }
       120364  +  w.xSelectCallback2 = selectPopWith;
120198 120365     sqlite3WalkSelect(&w, pSelect);
120199 120366   }
120200 120367   
120201 120368   
120202 120369   #ifndef SQLITE_OMIT_SUBQUERY
120203 120370   /*
120204 120371   ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
................................................................................
121358 121525   ** interface routines.  These are just wrappers around the main
121359 121526   ** interface routine of sqlite3_exec().
121360 121527   **
121361 121528   ** These routines are in a separate files so that they will not be linked
121362 121529   ** if they are not used.
121363 121530   */
121364 121531   /* #include "sqliteInt.h" */
121365         -/* #include <stdlib.h> */
121366         -/* #include <string.h> */
121367 121532   
121368 121533   #ifndef SQLITE_OMIT_GET_TABLE
121369 121534   
121370 121535   /*
121371 121536   ** This structure is used to pass data from sqlite3_get_table() through
121372 121537   ** to the callback function is uses to build the result.
121373 121538   */
................................................................................
123038 123203     /* Begin the database scan. 
123039 123204     **
123040 123205     ** Do not consider a single-pass strategy for a multi-row update if
123041 123206     ** there are any triggers or foreign keys to process, or rows may
123042 123207     ** be deleted as a result of REPLACE conflict handling. Any of these
123043 123208     ** things might disturb a cursor being used to scan through the table
123044 123209     ** or index, causing a single-pass approach to malfunction.  */
123045         -  flags = WHERE_ONEPASS_DESIRED | WHERE_SEEK_TABLE;
       123210  +  flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
123046 123211     if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
123047 123212       flags |= WHERE_ONEPASS_MULTIROW;
123048 123213     }
123049 123214     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
123050 123215     if( pWInfo==0 ) goto update_cleanup;
123051 123216   
123052 123217     /* A one-pass strategy that might update more than one row may not
................................................................................
127260 127425         testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
127261 127426       }
127262 127427   
127263 127428       /* Seek the table cursor, if required */
127264 127429       if( omitTable ){
127265 127430         /* pIdx is a covering index.  No need to access the main table. */
127266 127431       }else if( HasRowid(pIdx->pTable) ){
127267         -      if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE)!=0 ){
       127432  +      if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || (
       127433  +          (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE) 
       127434  +       && (pWInfo->eOnePass==ONEPASS_SINGLE)
       127435  +      )){
127268 127436           iRowidReg = ++pParse->nMem;
127269 127437           sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
127270 127438           sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
127271 127439           sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
127272 127440           VdbeCoverage(v);
127273 127441         }else{
127274 127442           codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
................................................................................
134173 134341   
134174 134342   /*
134175 134343   ** Indicate that sqlite3ParserFree() will never be called with a null
134176 134344   ** pointer.
134177 134345   */
134178 134346   #define YYPARSEFREENEVERNULL 1
134179 134347   
       134348  +/*
       134349  +** In the amalgamation, the parse.c file generated by lemon and the
       134350  +** tokenize.c file are concatenated.  In that case, sqlite3RunParser()
       134351  +** has access to the the size of the yyParser object and so the parser
       134352  +** engine can be allocated from stack.  In that case, only the
       134353  +** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
       134354  +** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
       134355  +** omitted.
       134356  +*/
       134357  +#ifdef SQLITE_AMALGAMATION
       134358  +# define sqlite3Parser_ENGINEALWAYSONSTACK 1
       134359  +#endif
       134360  +
134180 134361   /*
134181 134362   ** Alternative datatype for the argument to the malloc() routine passed
134182 134363   ** into sqlite3ParserAlloc().  The default is size_t.
134183 134364   */
134184 134365   #define YYMALLOCARGTYPE  u64
134185 134366   
134186 134367   /*
................................................................................
135621 135802   ** putting an appropriate #define in the %include section of the input
135622 135803   ** grammar.
135623 135804   */
135624 135805   #ifndef YYMALLOCARGTYPE
135625 135806   # define YYMALLOCARGTYPE size_t
135626 135807   #endif
135627 135808   
       135809  +/* Initialize a new parser that has already been allocated.
       135810  +*/
       135811  +SQLITE_PRIVATE void sqlite3ParserInit(void *yypParser){
       135812  +  yyParser *pParser = (yyParser*)yypParser;
       135813  +#ifdef YYTRACKMAXSTACKDEPTH
       135814  +  pParser->yyhwm = 0;
       135815  +#endif
       135816  +#if YYSTACKDEPTH<=0
       135817  +  pParser->yytos = NULL;
       135818  +  pParser->yystack = NULL;
       135819  +  pParser->yystksz = 0;
       135820  +  if( yyGrowStack(pParser) ){
       135821  +    pParser->yystack = &pParser->yystk0;
       135822  +    pParser->yystksz = 1;
       135823  +  }
       135824  +#endif
       135825  +#ifndef YYNOERRORRECOVERY
       135826  +  pParser->yyerrcnt = -1;
       135827  +#endif
       135828  +  pParser->yytos = pParser->yystack;
       135829  +  pParser->yystack[0].stateno = 0;
       135830  +  pParser->yystack[0].major = 0;
       135831  +}
       135832  +
       135833  +#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135628 135834   /* 
135629 135835   ** This function allocates a new parser.
135630 135836   ** The only argument is a pointer to a function which works like
135631 135837   ** malloc.
135632 135838   **
135633 135839   ** Inputs:
135634 135840   ** A pointer to the function used to allocate memory.
................................................................................
135636 135842   ** Outputs:
135637 135843   ** A pointer to a parser.  This pointer is used in subsequent calls
135638 135844   ** to sqlite3Parser and sqlite3ParserFree.
135639 135845   */
135640 135846   SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){
135641 135847     yyParser *pParser;
135642 135848     pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
135643         -  if( pParser ){
135644         -#ifdef YYTRACKMAXSTACKDEPTH
135645         -    pParser->yyhwm = 0;
135646         -#endif
135647         -#if YYSTACKDEPTH<=0
135648         -    pParser->yytos = NULL;
135649         -    pParser->yystack = NULL;
135650         -    pParser->yystksz = 0;
135651         -    if( yyGrowStack(pParser) ){
135652         -      pParser->yystack = &pParser->yystk0;
135653         -      pParser->yystksz = 1;
135654         -    }
135655         -#endif
135656         -#ifndef YYNOERRORRECOVERY
135657         -    pParser->yyerrcnt = -1;
135658         -#endif
135659         -    pParser->yytos = pParser->yystack;
135660         -    pParser->yystack[0].stateno = 0;
135661         -    pParser->yystack[0].major = 0;
135662         -  }
       135849  +  if( pParser ) sqlite3ParserInit(pParser);
135663 135850     return pParser;
135664 135851   }
       135852  +#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
       135853  +
135665 135854   
135666 135855   /* The following function deletes the "minor type" or semantic value
135667 135856   ** associated with a symbol.  The symbol can be either a terminal
135668 135857   ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
135669 135858   ** a pointer to the value to be deleted.  The code used to do the 
135670 135859   ** deletions is derived from the %destructor and/or %token_destructor
135671 135860   ** directives of the input grammar.
................................................................................
135783 135972         yyTracePrompt,
135784 135973         yyTokenName[yytos->major]);
135785 135974     }
135786 135975   #endif
135787 135976     yy_destructor(pParser, yytos->major, &yytos->minor);
135788 135977   }
135789 135978   
       135979  +/*
       135980  +** Clear all secondary memory allocations from the parser
       135981  +*/
       135982  +SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
       135983  +  yyParser *pParser = (yyParser*)p;
       135984  +  while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
       135985  +#if YYSTACKDEPTH<=0
       135986  +  if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
       135987  +#endif
       135988  +}
       135989  +
       135990  +#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135790 135991   /* 
135791 135992   ** Deallocate and destroy a parser.  Destructors are called for
135792 135993   ** all stack elements before shutting the parser down.
135793 135994   **
135794 135995   ** If the YYPARSEFREENEVERNULL macro exists (for example because it
135795 135996   ** is defined in a %include section of the input grammar) then it is
135796 135997   ** assumed that the input pointer is never NULL.
135797 135998   */
135798 135999   SQLITE_PRIVATE void sqlite3ParserFree(
135799 136000     void *p,                    /* The parser to be deleted */
135800 136001     void (*freeProc)(void*)     /* Function used to reclaim memory */
135801 136002   ){
135802         -  yyParser *pParser = (yyParser*)p;
135803 136003   #ifndef YYPARSEFREENEVERNULL
135804         -  if( pParser==0 ) return;
       136004  +  if( p==0 ) return;
135805 136005   #endif
135806         -  while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
135807         -#if YYSTACKDEPTH<=0
135808         -  if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
135809         -#endif
135810         -  (*freeProc)((void*)pParser);
       136006  +  sqlite3ParserFinalize(p);
       136007  +  (*freeProc)(p);
135811 136008   }
       136009  +#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
135812 136010   
135813 136011   /*
135814 136012   ** Return the peak depth of the stack for a parser.
135815 136013   */
135816 136014   #ifdef YYTRACKMAXSTACKDEPTH
135817 136015   SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
135818 136016     yyParser *pParser = (yyParser*)p;
................................................................................
138658 138856     int nErr = 0;                   /* Number of errors encountered */
138659 138857     int i;                          /* Loop counter */
138660 138858     void *pEngine;                  /* The LEMON-generated LALR(1) parser */
138661 138859     int tokenType;                  /* type of the next token */
138662 138860     int lastTokenParsed = -1;       /* type of the previous token */
138663 138861     sqlite3 *db = pParse->db;       /* The database connection */
138664 138862     int mxSqlLen;                   /* Max length of an SQL string */
       138863  +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
       138864  +  unsigned char zSpace[sizeof(yyParser)];  /* Space for parser engine object */
       138865  +#endif
138665 138866   
138666 138867     assert( zSql!=0 );
138667 138868     mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
138668 138869     if( db->nVdbeActive==0 ){
138669 138870       db->u1.isInterrupted = 0;
138670 138871     }
138671 138872     pParse->rc = SQLITE_OK;
138672 138873     pParse->zTail = zSql;
138673 138874     i = 0;
138674 138875     assert( pzErrMsg!=0 );
138675 138876     /* sqlite3ParserTrace(stdout, "parser: "); */
       138877  +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
       138878  +  pEngine = zSpace;
       138879  +  sqlite3ParserInit(pEngine);
       138880  +#else
138676 138881     pEngine = sqlite3ParserAlloc(sqlite3Malloc);
138677 138882     if( pEngine==0 ){
138678 138883       sqlite3OomFault(db);
138679 138884       return SQLITE_NOMEM_BKPT;
138680 138885     }
       138886  +#endif
138681 138887     assert( pParse->pNewTable==0 );
138682 138888     assert( pParse->pNewTrigger==0 );
138683 138889     assert( pParse->nVar==0 );
138684 138890     assert( pParse->pVList==0 );
138685 138891     while( 1 ){
138686 138892       assert( i>=0 );
138687 138893       if( zSql[i]!=0 ){
................................................................................
138725 138931   #ifdef YYTRACKMAXSTACKDEPTH
138726 138932     sqlite3_mutex_enter(sqlite3MallocMutex());
138727 138933     sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
138728 138934         sqlite3ParserStackPeak(pEngine)
138729 138935     );
138730 138936     sqlite3_mutex_leave(sqlite3MallocMutex());
138731 138937   #endif /* YYDEBUG */
       138938  +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
       138939  +  sqlite3ParserFinalize(pEngine);
       138940  +#else
138732 138941     sqlite3ParserFree(pEngine, sqlite3_free);
       138942  +#endif
138733 138943     if( db->mallocFailed ){
138734 138944       pParse->rc = SQLITE_NOMEM_BKPT;
138735 138945     }
138736 138946     if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
138737 138947       pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
138738 138948     }
138739 138949     assert( pzErrMsg!=0 );
................................................................................
144388 144598     int nAutoincrmerge;             /* Value configured by 'automerge' */
144389 144599     u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
144390 144600   
144391 144601     /* Precompiled statements used by the implementation. Each of these 
144392 144602     ** statements is run and reset within a single virtual table API call. 
144393 144603     */
144394 144604     sqlite3_stmt *aStmt[40];
       144605  +  sqlite3_stmt *pSeekStmt;        /* Cache for fts3CursorSeekStmt() */
144395 144606   
144396 144607     char *zReadExprlist;
144397 144608     char *zWriteExprlist;
144398 144609   
144399 144610     int nNodeSize;                  /* Soft limit for node size */
144400 144611     u8 bFts4;                       /* True for FTS4, false for FTS3 */
144401 144612     u8 bHasStat;                    /* True if %_stat table exists (2==unknown) */
................................................................................
144457 144668   ** the xOpen method. Cursors are destroyed using the xClose method.
144458 144669   */
144459 144670   struct Fts3Cursor {
144460 144671     sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
144461 144672     i16 eSearch;                    /* Search strategy (see below) */
144462 144673     u8 isEof;                       /* True if at End Of Results */
144463 144674     u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
       144675  +  u8 bSeekStmt;                   /* True if pStmt is a seek */
144464 144676     sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
144465 144677     Fts3Expr *pExpr;                /* Parsed MATCH query string */
144466 144678     int iLangid;                    /* Language being queried for */
144467 144679     int nPhrase;                    /* Number of matchable phrases in query */
144468 144680     Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
144469 144681     sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
144470 144682     char *pNextId;                  /* Pointer into the body of aDoclist */
................................................................................
144979 145191     Fts3Table *p = (Fts3Table *)pVtab;
144980 145192     int i;
144981 145193   
144982 145194     assert( p->nPendingData==0 );
144983 145195     assert( p->pSegments==0 );
144984 145196   
144985 145197     /* Free any prepared statements held */
       145198  +  sqlite3_finalize(p->pSeekStmt);
144986 145199     for(i=0; i<SizeofArray(p->aStmt); i++){
144987 145200       sqlite3_finalize(p->aStmt[i]);
144988 145201     }
144989 145202     sqlite3_free(p->zSegmentsTbl);
144990 145203     sqlite3_free(p->zReadExprlist);
144991 145204     sqlite3_free(p->zWriteExprlist);
144992 145205     sqlite3_free(p->zContentTbl);
................................................................................
145850 146063     p->db = db;
145851 146064     p->nColumn = nCol;
145852 146065     p->nPendingData = 0;
145853 146066     p->azColumn = (char **)&p[1];
145854 146067     p->pTokenizer = pTokenizer;
145855 146068     p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
145856 146069     p->bHasDocsize = (isFts4 && bNoDocsize==0);
145857         -  p->bHasStat = isFts4;
145858         -  p->bFts4 = isFts4;
145859         -  p->bDescIdx = bDescIdx;
       146070  +  p->bHasStat = (u8)isFts4;
       146071  +  p->bFts4 = (u8)isFts4;
       146072  +  p->bDescIdx = (u8)bDescIdx;
145860 146073     p->nAutoincrmerge = 0xff;   /* 0xff means setting unknown */
145861 146074     p->zContentTbl = zContent;
145862 146075     p->zLanguageid = zLanguageid;
145863 146076     zContent = 0;
145864 146077     zLanguageid = 0;
145865 146078     TESTONLY( p->inTransaction = -1 );
145866 146079     TESTONLY( p->mxSavepoint = -1 );
................................................................................
146166 146379     *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
146167 146380     if( !pCsr ){
146168 146381       return SQLITE_NOMEM;
146169 146382     }
146170 146383     memset(pCsr, 0, sizeof(Fts3Cursor));
146171 146384     return SQLITE_OK;
146172 146385   }
       146386  +
       146387  +/*
       146388  +** Finalize the statement handle at pCsr->pStmt.
       146389  +**
       146390  +** Or, if that statement handle is one created by fts3CursorSeekStmt(),
       146391  +** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
       146392  +** pointer there instead of finalizing it.
       146393  +*/
       146394  +static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
       146395  +  if( pCsr->bSeekStmt ){
       146396  +    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
       146397  +    if( p->pSeekStmt==0 ){
       146398  +      p->pSeekStmt = pCsr->pStmt;
       146399  +      sqlite3_reset(pCsr->pStmt);
       146400  +      pCsr->pStmt = 0;
       146401  +    }
       146402  +    pCsr->bSeekStmt = 0;
       146403  +  }
       146404  +  sqlite3_finalize(pCsr->pStmt);
       146405  +}
146173 146406   
146174 146407   /*
146175 146408   ** Close the cursor.  For additional information see the documentation
146176 146409   ** on the xClose method of the virtual table interface.
146177 146410   */
146178 146411   static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
146179 146412     Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
146180 146413     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
146181         -  sqlite3_finalize(pCsr->pStmt);
       146414  +  fts3CursorFinalizeStmt(pCsr);
146182 146415     sqlite3Fts3ExprFree(pCsr->pExpr);
146183 146416     sqlite3Fts3FreeDeferredTokens(pCsr);
146184 146417     sqlite3_free(pCsr->aDoclist);
146185 146418     sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
146186 146419     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
146187 146420     sqlite3_free(pCsr);
146188 146421     return SQLITE_OK;
................................................................................
146192 146425   ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
146193 146426   ** compose and prepare an SQL statement of the form:
146194 146427   **
146195 146428   **    "SELECT <columns> FROM %_content WHERE rowid = ?"
146196 146429   **
146197 146430   ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
146198 146431   ** it. If an error occurs, return an SQLite error code.
146199         -**
146200         -** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
146201 146432   */
146202         -static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
       146433  +static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
146203 146434     int rc = SQLITE_OK;
146204 146435     if( pCsr->pStmt==0 ){
146205 146436       Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
146206 146437       char *zSql;
146207         -    zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
146208         -    if( !zSql ) return SQLITE_NOMEM;
146209         -    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
146210         -    sqlite3_free(zSql);
       146438  +    if( p->pSeekStmt ){
       146439  +      pCsr->pStmt = p->pSeekStmt;
       146440  +      p->pSeekStmt = 0;
       146441  +    }else{
       146442  +      zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
       146443  +      if( !zSql ) return SQLITE_NOMEM;
       146444  +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
       146445  +      sqlite3_free(zSql);
       146446  +    }
       146447  +    if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
146211 146448     }
146212         -  *ppStmt = pCsr->pStmt;
146213 146449     return rc;
146214 146450   }
146215 146451   
146216 146452   /*
146217 146453   ** Position the pCsr->pStmt statement so that it is on the row
146218 146454   ** of the %_content table that contains the last match.  Return
146219 146455   ** SQLITE_OK on success.  
146220 146456   */
146221 146457   static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
146222 146458     int rc = SQLITE_OK;
146223 146459     if( pCsr->isRequireSeek ){
146224         -    sqlite3_stmt *pStmt = 0;
146225         -
146226         -    rc = fts3CursorSeekStmt(pCsr, &pStmt);
       146460  +    rc = fts3CursorSeekStmt(pCsr);
146227 146461       if( rc==SQLITE_OK ){
146228 146462         sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
146229 146463         pCsr->isRequireSeek = 0;
146230 146464         if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
146231 146465           return SQLITE_OK;
146232 146466         }else{
146233 146467           rc = sqlite3_reset(pCsr->pStmt);
................................................................................
147677 147911     if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
147678 147912     if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
147679 147913     if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
147680 147914     if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
147681 147915     assert( iIdx==nVal );
147682 147916   
147683 147917     /* In case the cursor has been used before, clear it now. */
147684         -  sqlite3_finalize(pCsr->pStmt);
       147918  +  fts3CursorFinalizeStmt(pCsr);
147685 147919     sqlite3_free(pCsr->aDoclist);
147686 147920     sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
147687 147921     sqlite3Fts3ExprFree(pCsr->pExpr);
147688 147922     memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
147689 147923   
147690 147924     /* Set the lower and upper bounds on docids to return */
147691 147925     pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
................................................................................
147745 147979       if( zSql ){
147746 147980         rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
147747 147981         sqlite3_free(zSql);
147748 147982       }else{
147749 147983         rc = SQLITE_NOMEM;
147750 147984       }
147751 147985     }else if( eSearch==FTS3_DOCID_SEARCH ){
147752         -    rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
       147986  +    rc = fts3CursorSeekStmt(pCsr);
147753 147987       if( rc==SQLITE_OK ){
147754 147988         rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
147755 147989       }
147756 147990     }
147757 147991     if( rc!=SQLITE_OK ) return rc;
147758 147992   
147759 147993     return fts3NextMethod(pCursor);
................................................................................
147909 148143       char *zSql = sqlite3_mprintf(zFmt, p->zDb, p->zName);
147910 148144       if( zSql ){
147911 148145         sqlite3_stmt *pStmt = 0;
147912 148146         rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
147913 148147         if( rc==SQLITE_OK ){
147914 148148           int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW);
147915 148149           rc = sqlite3_finalize(pStmt);
147916         -        if( rc==SQLITE_OK ) p->bHasStat = bHasStat;
       148150  +        if( rc==SQLITE_OK ) p->bHasStat = (u8)bHasStat;
147917 148151         }
147918 148152         sqlite3_free(zSql);
147919 148153       }else{
147920 148154         rc = SQLITE_NOMEM;
147921 148155       }
147922 148156     }
147923 148157     return rc;
................................................................................
162765 162999   /* #include <string.h> */
162766 163000   /* #include <assert.h> */
162767 163001   /* #include <stdio.h> */
162768 163002   
162769 163003   #ifndef SQLITE_AMALGAMATION
162770 163004   #include "sqlite3rtree.h"
162771 163005   typedef sqlite3_int64 i64;
       163006  +typedef sqlite3_uint64 u64;
162772 163007   typedef unsigned char u8;
162773 163008   typedef unsigned short u16;
162774 163009   typedef unsigned int u32;
162775 163010   #endif
162776 163011   
162777 163012   /*  The following macro is used to suppress compiler warnings.
162778 163013   */
................................................................................
162813 163048   ** An rtree virtual-table object.
162814 163049   */
162815 163050   struct Rtree {
162816 163051     sqlite3_vtab base;          /* Base class.  Must be first */
162817 163052     sqlite3 *db;                /* Host database connection */
162818 163053     int iNodeSize;              /* Size in bytes of each node in the node table */
162819 163054     u8 nDim;                    /* Number of dimensions */
       163055  +  u8 nDim2;                   /* Twice the number of dimensions */
162820 163056     u8 eCoordType;              /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
162821 163057     u8 nBytesPerCell;           /* Bytes consumed per cell */
       163058  +  u8 inWrTrans;               /* True if inside write transaction */
162822 163059     int iDepth;                 /* Current depth of the r-tree structure */
162823 163060     char *zDb;                  /* Name of database containing r-tree table */
162824 163061     char *zName;                /* Name of r-tree table */ 
162825         -  int nBusy;                  /* Current number of users of this structure */
       163062  +  u32 nBusy;                  /* Current number of users of this structure */
162826 163063     i64 nRowEst;                /* Estimated number of rows in this table */
       163064  +  u32 nCursor;                /* Number of open cursors */
162827 163065   
162828 163066     /* List of nodes removed during a CondenseTree operation. List is
162829 163067     ** linked together via the pointer normally used for hash chains -
162830 163068     ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 
162831 163069     ** headed by the node (leaf nodes have RtreeNode.iNode==0).
162832 163070     */
162833 163071     RtreeNode *pDeleted;
162834 163072     int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
       163073  +
       163074  +  /* Blob I/O on xxx_node */
       163075  +  sqlite3_blob *pNodeBlob;
162835 163076   
162836 163077     /* Statements to read/write/delete a record from xxx_node */
162837         -  sqlite3_stmt *pReadNode;
162838 163078     sqlite3_stmt *pWriteNode;
162839 163079     sqlite3_stmt *pDeleteNode;
162840 163080   
162841 163081     /* Statements to read/write/delete a record from xxx_rowid */
162842 163082     sqlite3_stmt *pReadRowid;
162843 163083     sqlite3_stmt *pWriteRowid;
162844 163084     sqlite3_stmt *pDeleteRowid;
................................................................................
163059 163299   #ifndef MAX
163060 163300   # define MAX(x,y) ((x) < (y) ? (y) : (x))
163061 163301   #endif
163062 163302   #ifndef MIN
163063 163303   # define MIN(x,y) ((x) > (y) ? (y) : (x))
163064 163304   #endif
163065 163305   
       163306  +/* What version of GCC is being used.  0 means GCC is not being used */
       163307  +#ifndef GCC_VERSION
       163308  +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
       163309  +# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
       163310  +#else
       163311  +# define GCC_VERSION 0
       163312  +#endif
       163313  +#endif
       163314  +
       163315  +/* What version of CLANG is being used.  0 means CLANG is not being used */
       163316  +#ifndef CLANG_VERSION
       163317  +#if defined(__clang__) && !defined(_WIN32) && !defined(SQLITE_DISABLE_INTRINSIC)
       163318  +# define CLANG_VERSION \
       163319  +            (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
       163320  +#else
       163321  +# define CLANG_VERSION 0
       163322  +#endif
       163323  +#endif
       163324  +
       163325  +/* The testcase() macro should already be defined in the amalgamation.  If
       163326  +** it is not, make it a no-op.
       163327  +*/
       163328  +#ifndef SQLITE_AMALGAMATION
       163329  +# define testcase(X)
       163330  +#endif
       163331  +
       163332  +/*
       163333  +** Macros to determine whether the machine is big or little endian,
       163334  +** and whether or not that determination is run-time or compile-time.
       163335  +**
       163336  +** For best performance, an attempt is made to guess at the byte-order
       163337  +** using C-preprocessor macros.  If that is unsuccessful, or if
       163338  +** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
       163339  +** at run-time.
       163340  +*/
       163341  +#ifndef SQLITE_BYTEORDER
       163342  +#if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
       163343  +    defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
       163344  +    defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
       163345  +    defined(__arm__)
       163346  +# define SQLITE_BYTEORDER    1234
       163347  +#elif defined(sparc)    || defined(__ppc__)
       163348  +# define SQLITE_BYTEORDER    4321
       163349  +#else
       163350  +# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
       163351  +#endif
       163352  +#endif
       163353  +
       163354  +
       163355  +/* What version of MSVC is being used.  0 means MSVC is not being used */
       163356  +#ifndef MSVC_VERSION
       163357  +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
       163358  +# define MSVC_VERSION _MSC_VER
       163359  +#else
       163360  +# define MSVC_VERSION 0
       163361  +#endif
       163362  +#endif
       163363  +
163066 163364   /*
163067 163365   ** Functions to deserialize a 16 bit integer, 32 bit real number and
163068 163366   ** 64 bit integer. The deserialized value is returned.
163069 163367   */
163070 163368   static int readInt16(u8 *p){
163071 163369     return (p[0]<<8) + p[1];
163072 163370   }
163073 163371   static void readCoord(u8 *p, RtreeCoord *pCoord){
       163372  +  assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
       163373  +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
       163374  +  pCoord->u = _byteswap_ulong(*(u32*)p);
       163375  +#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163376  +  pCoord->u = __builtin_bswap32(*(u32*)p);
       163377  +#elif SQLITE_BYTEORDER==4321
       163378  +  pCoord->u = *(u32*)p;
       163379  +#else
163074 163380     pCoord->u = (
163075 163381       (((u32)p[0]) << 24) + 
163076 163382       (((u32)p[1]) << 16) + 
163077 163383       (((u32)p[2]) <<  8) + 
163078 163384       (((u32)p[3]) <<  0)
163079 163385     );
       163386  +#endif
163080 163387   }
163081 163388   static i64 readInt64(u8 *p){
       163389  +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
       163390  +  u64 x;
       163391  +  memcpy(&x, p, 8);
       163392  +  return (i64)_byteswap_uint64(x);
       163393  +#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163394  +  u64 x;
       163395  +  memcpy(&x, p, 8);
       163396  +  return (i64)__builtin_bswap64(x);
       163397  +#elif SQLITE_BYTEORDER==4321
       163398  +  i64 x;
       163399  +  memcpy(&x, p, 8);
       163400  +  return x;
       163401  +#else
163082 163402     return (
163083 163403       (((i64)p[0]) << 56) + 
163084 163404       (((i64)p[1]) << 48) + 
163085 163405       (((i64)p[2]) << 40) + 
163086 163406       (((i64)p[3]) << 32) + 
163087 163407       (((i64)p[4]) << 24) + 
163088 163408       (((i64)p[5]) << 16) + 
163089 163409       (((i64)p[6]) <<  8) + 
163090 163410       (((i64)p[7]) <<  0)
163091 163411     );
       163412  +#endif
163092 163413   }
163093 163414   
163094 163415   /*
163095 163416   ** Functions to serialize a 16 bit integer, 32 bit real number and
163096 163417   ** 64 bit integer. The value returned is the number of bytes written
163097 163418   ** to the argument buffer (always 2, 4 and 8 respectively).
163098 163419   */
163099         -static int writeInt16(u8 *p, int i){
       163420  +static void writeInt16(u8 *p, int i){
163100 163421     p[0] = (i>> 8)&0xFF;
163101 163422     p[1] = (i>> 0)&0xFF;
163102         -  return 2;
163103 163423   }
163104 163424   static int writeCoord(u8 *p, RtreeCoord *pCoord){
163105 163425     u32 i;
       163426  +  assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
163106 163427     assert( sizeof(RtreeCoord)==4 );
163107 163428     assert( sizeof(u32)==4 );
       163429  +#if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163430  +  i = __builtin_bswap32(pCoord->u);
       163431  +  memcpy(p, &i, 4);
       163432  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
       163433  +  i = _byteswap_ulong(pCoord->u);
       163434  +  memcpy(p, &i, 4);
       163435  +#elif SQLITE_BYTEORDER==4321
       163436  +  i = pCoord->u;
       163437  +  memcpy(p, &i, 4);
       163438  +#else
163108 163439     i = pCoord->u;
163109 163440     p[0] = (i>>24)&0xFF;
163110 163441     p[1] = (i>>16)&0xFF;
163111 163442     p[2] = (i>> 8)&0xFF;
163112 163443     p[3] = (i>> 0)&0xFF;
       163444  +#endif
163113 163445     return 4;
163114 163446   }
163115 163447   static int writeInt64(u8 *p, i64 i){
       163448  +#if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163449  +  i = (i64)__builtin_bswap64((u64)i);
       163450  +  memcpy(p, &i, 8);
       163451  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
       163452  +  i = (i64)_byteswap_uint64((u64)i);
       163453  +  memcpy(p, &i, 8);
       163454  +#elif SQLITE_BYTEORDER==4321
       163455  +  memcpy(p, &i, 8);
       163456  +#else
163116 163457     p[0] = (i>>56)&0xFF;
163117 163458     p[1] = (i>>48)&0xFF;
163118 163459     p[2] = (i>>40)&0xFF;
163119 163460     p[3] = (i>>32)&0xFF;
163120 163461     p[4] = (i>>24)&0xFF;
163121 163462     p[5] = (i>>16)&0xFF;
163122 163463     p[6] = (i>> 8)&0xFF;
163123 163464     p[7] = (i>> 0)&0xFF;
       163465  +#endif
163124 163466     return 8;
163125 163467   }
163126 163468   
163127 163469   /*
163128 163470   ** Increment the reference count of node p.
163129 163471   */
163130 163472   static void nodeReference(RtreeNode *p){
................................................................................
163198 163540       pNode->nRef = 1;
163199 163541       pNode->pParent = pParent;
163200 163542       pNode->isDirty = 1;
163201 163543       nodeReference(pParent);
163202 163544     }
163203 163545     return pNode;
163204 163546   }
       163547  +
       163548  +/*
       163549  +** Clear the Rtree.pNodeBlob object
       163550  +*/
       163551  +static void nodeBlobReset(Rtree *pRtree){
       163552  +  if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
       163553  +    sqlite3_blob *pBlob = pRtree->pNodeBlob;
       163554  +    pRtree->pNodeBlob = 0;
       163555  +    sqlite3_blob_close(pBlob);
       163556  +  }
       163557  +}
163205 163558   
163206 163559   /*
163207 163560   ** Obtain a reference to an r-tree node.
163208 163561   */
163209 163562   static int nodeAcquire(
163210 163563     Rtree *pRtree,             /* R-tree structure */
163211 163564     i64 iNode,                 /* Node number to load */
163212 163565     RtreeNode *pParent,        /* Either the parent node or NULL */
163213 163566     RtreeNode **ppNode         /* OUT: Acquired node */
163214 163567   ){
163215         -  int rc;
163216         -  int rc2 = SQLITE_OK;
163217         -  RtreeNode *pNode;
       163568  +  int rc = SQLITE_OK;
       163569  +  RtreeNode *pNode = 0;
163218 163570   
163219 163571     /* Check if the requested node is already in the hash table. If so,
163220 163572     ** increase its reference count and return it.
163221 163573     */
163222 163574     if( (pNode = nodeHashLookup(pRtree, iNode)) ){
163223 163575       assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
163224 163576       if( pParent && !pNode->pParent ){
................................................................................
163226 163578         pNode->pParent = pParent;
163227 163579       }
163228 163580       pNode->nRef++;
163229 163581       *ppNode = pNode;
163230 163582       return SQLITE_OK;
163231 163583     }
163232 163584   
163233         -  sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
163234         -  rc = sqlite3_step(pRtree->pReadNode);
163235         -  if( rc==SQLITE_ROW ){
163236         -    const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
163237         -    if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
163238         -      pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
163239         -      if( !pNode ){
163240         -        rc2 = SQLITE_NOMEM;
163241         -      }else{
163242         -        pNode->pParent = pParent;
163243         -        pNode->zData = (u8 *)&pNode[1];
163244         -        pNode->nRef = 1;
163245         -        pNode->iNode = iNode;
163246         -        pNode->isDirty = 0;
163247         -        pNode->pNext = 0;
163248         -        memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
163249         -        nodeReference(pParent);
163250         -      }
163251         -    }
163252         -  }
163253         -  rc = sqlite3_reset(pRtree->pReadNode);
163254         -  if( rc==SQLITE_OK ) rc = rc2;
       163585  +  if( pRtree->pNodeBlob ){
       163586  +    sqlite3_blob *pBlob = pRtree->pNodeBlob;
       163587  +    pRtree->pNodeBlob = 0;
       163588  +    rc = sqlite3_blob_reopen(pBlob, iNode);
       163589  +    pRtree->pNodeBlob = pBlob;
       163590  +    if( rc ){
       163591  +      nodeBlobReset(pRtree);
       163592  +      if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
       163593  +    }
       163594  +  }
       163595  +  if( pRtree->pNodeBlob==0 ){
       163596  +    char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
       163597  +    if( zTab==0 ) return SQLITE_NOMEM;
       163598  +    rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
       163599  +                           &pRtree->pNodeBlob);
       163600  +    sqlite3_free(zTab);
       163601  +  }
       163602  +  if( rc ){
       163603  +    nodeBlobReset(pRtree);
       163604  +    *ppNode = 0;
       163605  +    /* If unable to open an sqlite3_blob on the desired row, that can only
       163606  +    ** be because the shadow tables hold erroneous data. */
       163607  +    if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB;
       163608  +  }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
       163609  +    pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
       163610  +    if( !pNode ){
       163611  +      rc = SQLITE_NOMEM;
       163612  +    }else{
       163613  +      pNode->pParent = pParent;
       163614  +      pNode->zData = (u8 *)&pNode[1];
       163615  +      pNode->nRef = 1;
       163616  +      pNode->iNode = iNode;
       163617  +      pNode->isDirty = 0;
       163618  +      pNode->pNext = 0;
       163619  +      rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
       163620  +                             pRtree->iNodeSize, 0);
       163621  +      nodeReference(pParent);
       163622  +    }
       163623  +  }
163255 163624   
163256 163625     /* If the root node was just loaded, set pRtree->iDepth to the height
163257 163626     ** of the r-tree structure. A height of zero means all data is stored on
163258 163627     ** the root node. A height of one means the children of the root node
163259 163628     ** are the leaves, and so on. If the depth as specified on the root node
163260 163629     ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
163261 163630     */
................................................................................
163299 163668     RtreeNode *pNode,          /* The node into which the cell is to be written */
163300 163669     RtreeCell *pCell,          /* The cell to write */
163301 163670     int iCell                  /* Index into pNode into which pCell is written */
163302 163671   ){
163303 163672     int ii;
163304 163673     u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
163305 163674     p += writeInt64(p, pCell->iRowid);
163306         -  for(ii=0; ii<(pRtree->nDim*2); ii++){
       163675  +  for(ii=0; ii<pRtree->nDim2; ii++){
163307 163676       p += writeCoord(p, &pCell->aCoord[ii]);
163308 163677     }
163309 163678     pNode->isDirty = 1;
163310 163679   }
163311 163680   
163312 163681   /*
163313 163682   ** Remove the cell with index iCell from node pNode.
................................................................................
163433 163802     Rtree *pRtree,               /* The overall R-Tree */
163434 163803     RtreeNode *pNode,            /* The node containing the cell to be read */
163435 163804     int iCell,                   /* Index of the cell within the node */
163436 163805     RtreeCell *pCell             /* OUT: Write the cell contents here */
163437 163806   ){
163438 163807     u8 *pData;
163439 163808     RtreeCoord *pCoord;
163440         -  int ii;
       163809  +  int ii = 0;
163441 163810     pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
163442 163811     pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
163443 163812     pCoord = pCell->aCoord;
163444         -  for(ii=0; ii<pRtree->nDim*2; ii++){
163445         -    readCoord(&pData[ii*4], &pCoord[ii]);
163446         -  }
       163813  +  do{
       163814  +    readCoord(pData, &pCoord[ii]);
       163815  +    readCoord(pData+4, &pCoord[ii+1]);
       163816  +    pData += 8;
       163817  +    ii += 2;
       163818  +  }while( ii<pRtree->nDim2 );
163447 163819   }
163448 163820   
163449 163821   
163450 163822   /* Forward declaration for the function that does the work of
163451 163823   ** the virtual table module xCreate() and xConnect() methods.
163452 163824   */
163453 163825   static int rtreeInit(
................................................................................
163490 163862   /*
163491 163863   ** Decrement the r-tree reference count. When the reference count reaches
163492 163864   ** zero the structure is deleted.
163493 163865   */
163494 163866   static void rtreeRelease(Rtree *pRtree){
163495 163867     pRtree->nBusy--;
163496 163868     if( pRtree->nBusy==0 ){
163497         -    sqlite3_finalize(pRtree->pReadNode);
       163869  +    pRtree->inWrTrans = 0;
       163870  +    pRtree->nCursor = 0;
       163871  +    nodeBlobReset(pRtree);
163498 163872       sqlite3_finalize(pRtree->pWriteNode);
163499 163873       sqlite3_finalize(pRtree->pDeleteNode);
163500 163874       sqlite3_finalize(pRtree->pReadRowid);
163501 163875       sqlite3_finalize(pRtree->pWriteRowid);
163502 163876       sqlite3_finalize(pRtree->pDeleteRowid);
163503 163877       sqlite3_finalize(pRtree->pReadParent);
163504 163878       sqlite3_finalize(pRtree->pWriteParent);
................................................................................
163528 163902       pRtree->zDb, pRtree->zName, 
163529 163903       pRtree->zDb, pRtree->zName,
163530 163904       pRtree->zDb, pRtree->zName
163531 163905     );
163532 163906     if( !zCreate ){
163533 163907       rc = SQLITE_NOMEM;
163534 163908     }else{
       163909  +    nodeBlobReset(pRtree);
163535 163910       rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
163536 163911       sqlite3_free(zCreate);
163537 163912     }
163538 163913     if( rc==SQLITE_OK ){
163539 163914       rtreeRelease(pRtree);
163540 163915     }
163541 163916   
................................................................................
163543 163918   }
163544 163919   
163545 163920   /* 
163546 163921   ** Rtree virtual table module xOpen method.
163547 163922   */
163548 163923   static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
163549 163924     int rc = SQLITE_NOMEM;
       163925  +  Rtree *pRtree = (Rtree *)pVTab;
163550 163926     RtreeCursor *pCsr;
163551 163927   
163552 163928     pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
163553 163929     if( pCsr ){
163554 163930       memset(pCsr, 0, sizeof(RtreeCursor));
163555 163931       pCsr->base.pVtab = pVTab;
163556 163932       rc = SQLITE_OK;
       163933  +    pRtree->nCursor++;
163557 163934     }
163558 163935     *ppCursor = (sqlite3_vtab_cursor *)pCsr;
163559 163936   
163560 163937     return rc;
163561 163938   }
163562 163939   
163563 163940   
................................................................................
163582 163959   /* 
163583 163960   ** Rtree virtual table module xClose method.
163584 163961   */
163585 163962   static int rtreeClose(sqlite3_vtab_cursor *cur){
163586 163963     Rtree *pRtree = (Rtree *)(cur->pVtab);
163587 163964     int ii;
163588 163965     RtreeCursor *pCsr = (RtreeCursor *)cur;
       163966  +  assert( pRtree->nCursor>0 );
163589 163967     freeCursorConstraints(pCsr);
163590 163968     sqlite3_free(pCsr->aPoint);
163591 163969     for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
163592 163970     sqlite3_free(pCsr);
       163971  +  pRtree->nCursor--;
       163972  +  nodeBlobReset(pRtree);
163593 163973     return SQLITE_OK;
163594 163974   }
163595 163975   
163596 163976   /*
163597 163977   ** Rtree virtual table module xEof method.
163598 163978   **
163599 163979   ** Return non-zero if the cursor does not currently point to a valid 
................................................................................
163608 163988   ** Convert raw bits from the on-disk RTree record into a coordinate value.
163609 163989   ** The on-disk format is big-endian and needs to be converted for little-
163610 163990   ** endian platforms.  The on-disk record stores integer coordinates if
163611 163991   ** eInt is true and it stores 32-bit floating point records if eInt is
163612 163992   ** false.  a[] is the four bytes of the on-disk record to be decoded.
163613 163993   ** Store the results in "r".
163614 163994   **
163615         -** There are three versions of this macro, one each for little-endian and
163616         -** big-endian processors and a third generic implementation.  The endian-
163617         -** specific implementations are much faster and are preferred if the
163618         -** processor endianness is known at compile-time.  The SQLITE_BYTEORDER
163619         -** macro is part of sqliteInt.h and hence the endian-specific
163620         -** implementation will only be used if this module is compiled as part
163621         -** of the amalgamation.
       163995  +** There are five versions of this macro.  The last one is generic.  The
       163996  +** other four are various architectures-specific optimizations.
163622 163997   */
163623         -#if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234
       163998  +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
       163999  +#define RTREE_DECODE_COORD(eInt, a, r) {                        \
       164000  +    RtreeCoord c;    /* Coordinate decoded */                   \
       164001  +    c.u = _byteswap_ulong(*(u32*)a);                            \
       164002  +    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
       164003  +}
       164004  +#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       164005  +#define RTREE_DECODE_COORD(eInt, a, r) {                        \
       164006  +    RtreeCoord c;    /* Coordinate decoded */                   \
       164007  +    c.u = __builtin_bswap32(*(u32*)a);                          \
       164008  +    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
       164009  +}
       164010  +#elif SQLITE_BYTEORDER==1234
163624 164011   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
163625 164012       RtreeCoord c;    /* Coordinate decoded */                   \
163626 164013       memcpy(&c.u,a,4);                                           \
163627 164014       c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)|                   \
163628 164015             ((c.u&0xff)<<24)|((c.u&0xff00)<<8);                   \
163629 164016       r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163630 164017   }
163631         -#elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321
       164018  +#elif SQLITE_BYTEORDER==4321
163632 164019   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
163633 164020       RtreeCoord c;    /* Coordinate decoded */                   \
163634 164021       memcpy(&c.u,a,4);                                           \
163635 164022       r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163636 164023   }
163637 164024   #else
163638 164025   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
................................................................................
163651 164038     RtreeConstraint *pConstraint,  /* The constraint to test */
163652 164039     int eInt,                      /* True if RTree holding integer coordinates */
163653 164040     u8 *pCellData,                 /* Raw cell content */
163654 164041     RtreeSearchPoint *pSearch,     /* Container of this cell */
163655 164042     sqlite3_rtree_dbl *prScore,    /* OUT: score for the cell */
163656 164043     int *peWithin                  /* OUT: visibility of the cell */
163657 164044   ){
163658         -  int i;                                                /* Loop counter */
163659 164045     sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
163660 164046     int nCoord = pInfo->nCoord;                           /* No. of coordinates */
163661 164047     int rc;                                             /* Callback return code */
       164048  +  RtreeCoord c;                                       /* Translator union */
163662 164049     sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2];   /* Decoded coordinates */
163663 164050   
163664 164051     assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
163665 164052     assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
163666 164053   
163667 164054     if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
163668 164055       pInfo->iRowid = readInt64(pCellData);
163669 164056     }
163670 164057     pCellData += 8;
163671         -  for(i=0; i<nCoord; i++, pCellData += 4){
163672         -    RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]);
       164058  +#ifndef SQLITE_RTREE_INT_ONLY
       164059  +  if( eInt==0 ){
       164060  +    switch( nCoord ){
       164061  +      case 10:  readCoord(pCellData+36, &c); aCoord[9] = c.f;
       164062  +                readCoord(pCellData+32, &c); aCoord[8] = c.f;
       164063  +      case 8:   readCoord(pCellData+28, &c); aCoord[7] = c.f;
       164064  +                readCoord(pCellData+24, &c); aCoord[6] = c.f;
       164065  +      case 6:   readCoord(pCellData+20, &c); aCoord[5] = c.f;
       164066  +                readCoord(pCellData+16, &c); aCoord[4] = c.f;
       164067  +      case 4:   readCoord(pCellData+12, &c); aCoord[3] = c.f;
       164068  +                readCoord(pCellData+8,  &c); aCoord[2] = c.f;
       164069  +      default:  readCoord(pCellData+4,  &c); aCoord[1] = c.f;
       164070  +                readCoord(pCellData,    &c); aCoord[0] = c.f;
       164071  +    }
       164072  +  }else
       164073  +#endif
       164074  +  {
       164075  +    switch( nCoord ){
       164076  +      case 10:  readCoord(pCellData+36, &c); aCoord[9] = c.i;
       164077  +                readCoord(pCellData+32, &c); aCoord[8] = c.i;
       164078  +      case 8:   readCoord(pCellData+28, &c); aCoord[7] = c.i;
       164079  +                readCoord(pCellData+24, &c); aCoord[6] = c.i;
       164080  +      case 6:   readCoord(pCellData+20, &c); aCoord[5] = c.i;
       164081  +                readCoord(pCellData+16, &c); aCoord[4] = c.i;
       164082  +      case 4:   readCoord(pCellData+12, &c); aCoord[3] = c.i;
       164083  +                readCoord(pCellData+8,  &c); aCoord[2] = c.i;
       164084  +      default:  readCoord(pCellData+4,  &c); aCoord[1] = c.i;
       164085  +                readCoord(pCellData,    &c); aCoord[0] = c.i;
       164086  +    }
163673 164087     }
163674 164088     if( pConstraint->op==RTREE_MATCH ){
       164089  +    int eWithin = 0;
163675 164090       rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
163676         -                              nCoord, aCoord, &i);
163677         -    if( i==0 ) *peWithin = NOT_WITHIN;
       164091  +                              nCoord, aCoord, &eWithin);
       164092  +    if( eWithin==0 ) *peWithin = NOT_WITHIN;
163678 164093       *prScore = RTREE_ZERO;
163679 164094     }else{
163680 164095       pInfo->aCoord = aCoord;
163681 164096       pInfo->iLevel = pSearch->iLevel - 1;
163682 164097       pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
163683 164098       pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
163684 164099       rc = pConstraint->u.xQueryFunc(pInfo);
................................................................................
163706 164121     /* p->iCoord might point to either a lower or upper bound coordinate
163707 164122     ** in a coordinate pair.  But make pCellData point to the lower bound.
163708 164123     */
163709 164124     pCellData += 8 + 4*(p->iCoord&0xfe);
163710 164125   
163711 164126     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
163712 164127         || p->op==RTREE_GT || p->op==RTREE_EQ );
       164128  +  assert( ((((char*)pCellData) - (char*)0)&3)==0 );  /* 4-byte aligned */
163713 164129     switch( p->op ){
163714 164130       case RTREE_LE:
163715 164131       case RTREE_LT:
163716 164132       case RTREE_EQ:
163717 164133         RTREE_DECODE_COORD(eInt, pCellData, val);
163718 164134         /* val now holds the lower bound of the coordinate pair */
163719 164135         if( p->u.rValue>=val ) return;
................................................................................
163746 164162     int *peWithin              /* Adjust downward, as appropriate */
163747 164163   ){
163748 164164     RtreeDValue xN;      /* Coordinate value converted to a double */
163749 164165   
163750 164166     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
163751 164167         || p->op==RTREE_GT || p->op==RTREE_EQ );
163752 164168     pCellData += 8 + p->iCoord*4;
       164169  +  assert( ((((char*)pCellData) - (char*)0)&3)==0 );  /* 4-byte aligned */
163753 164170     RTREE_DECODE_COORD(eInt, pCellData, xN);
163754 164171     switch( p->op ){
163755 164172       case RTREE_LE: if( xN <= p->u.rValue ) return;  break;
163756 164173       case RTREE_LT: if( xN <  p->u.rValue ) return;  break;
163757 164174       case RTREE_GE: if( xN >= p->u.rValue ) return;  break;
163758 164175       case RTREE_GT: if( xN >  p->u.rValue ) return;  break;
163759 164176       default:       if( xN == p->u.rValue ) return;  break;
................................................................................
163814 164231     if( pA->rScore>pB->rScore ) return +1;
163815 164232     if( pA->iLevel<pB->iLevel ) return -1;
163816 164233     if( pA->iLevel>pB->iLevel ) return +1;
163817 164234     return 0;
163818 164235   }
163819 164236   
163820 164237   /*
163821         -** Interchange to search points in a cursor.
       164238  +** Interchange two search points in a cursor.
163822 164239   */
163823 164240   static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
163824 164241     RtreeSearchPoint t = p->aPoint[i];
163825 164242     assert( i<j );
163826 164243     p->aPoint[i] = p->aPoint[j];
163827 164244     p->aPoint[j] = t;
163828 164245     i++; j++;
................................................................................
164062 164479         if( p->iCell>=nCell ){
164063 164480           RTREE_QUEUE_TRACE(pCur, "POP-S:");
164064 164481           rtreeSearchPointPop(pCur);
164065 164482         }
164066 164483         if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
164067 164484         p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
164068 164485         if( p==0 ) return SQLITE_NOMEM;
164069         -      p->eWithin = eWithin;
       164486  +      p->eWithin = (u8)eWithin;
164070 164487         p->id = x.id;
164071 164488         p->iCell = x.iCell;
164072 164489         RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
164073 164490         break;
164074 164491       }
164075 164492       if( p->iCell>=nCell ){
164076 164493         RTREE_QUEUE_TRACE(pCur, "POP-Se:");
................................................................................
164121 164538     RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
164122 164539   
164123 164540     if( rc ) return rc;
164124 164541     if( p==0 ) return SQLITE_OK;
164125 164542     if( i==0 ){
164126 164543       sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
164127 164544     }else{
164128         -    if( rc ) return rc;
164129 164545       nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
164130 164546   #ifndef SQLITE_RTREE_INT_ONLY
164131 164547       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164132 164548         sqlite3_result_double(ctx, c.f);
164133 164549       }else
164134 164550   #endif
164135 164551       {
................................................................................
164250 164666       if( rc==SQLITE_OK && pLeaf!=0 ){
164251 164667         p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
164252 164668         assert( p!=0 );  /* Always returns pCsr->sPoint */
164253 164669         pCsr->aNode[0] = pLeaf;
164254 164670         p->id = iNode;
164255 164671         p->eWithin = PARTLY_WITHIN;
164256 164672         rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
164257         -      p->iCell = iCell;
       164673  +      p->iCell = (u8)iCell;
164258 164674         RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
164259 164675       }else{
164260 164676         pCsr->atEOF = 1;
164261 164677       }
164262 164678     }else{
164263 164679       /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
164264 164680       ** with the configured constraints. 
................................................................................
164283 164699               ** can be cast into an RtreeMatchArg object. One created using
164284 164700               ** an sqlite3_rtree_geometry_callback() SQL user function.
164285 164701               */
164286 164702               rc = deserializeGeometry(argv[ii], p);
164287 164703               if( rc!=SQLITE_OK ){
164288 164704                 break;
164289 164705               }
164290         -            p->pInfo->nCoord = pRtree->nDim*2;
       164706  +            p->pInfo->nCoord = pRtree->nDim2;
164291 164707               p->pInfo->anQueue = pCsr->anQueue;
164292 164708               p->pInfo->mxLevel = pRtree->iDepth + 1;
164293 164709             }else{
164294 164710   #ifdef SQLITE_RTREE_INT_ONLY
164295 164711               p->u.rValue = sqlite3_value_int64(argv[ii]);
164296 164712   #else
164297 164713               p->u.rValue = sqlite3_value_double(argv[ii]);
................................................................................
164298 164714   #endif
164299 164715             }
164300 164716           }
164301 164717         }
164302 164718       }
164303 164719       if( rc==SQLITE_OK ){
164304 164720         RtreeSearchPoint *pNew;
164305         -      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1);
       164721  +      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
164306 164722         if( pNew==0 ) return SQLITE_NOMEM;
164307 164723         pNew->id = 1;
164308 164724         pNew->iCell = 0;
164309 164725         pNew->eWithin = PARTLY_WITHIN;
164310 164726         assert( pCsr->bPoint==1 );
164311 164727         pCsr->aNode[0] = pRoot;
164312 164728         pRoot = 0;
................................................................................
164316 164732     }
164317 164733   
164318 164734     nodeRelease(pRtree, pRoot);
164319 164735     rtreeRelease(pRtree);
164320 164736     return rc;
164321 164737   }
164322 164738   
164323         -/*
164324         -** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
164325         -** extension is currently being used by a version of SQLite too old to
164326         -** support estimatedRows. In that case this function is a no-op.
164327         -*/
164328         -static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
164329         -#if SQLITE_VERSION_NUMBER>=3008002
164330         -  if( sqlite3_libversion_number()>=3008002 ){
164331         -    pIdxInfo->estimatedRows = nRow;
164332         -  }
164333         -#endif
164334         -}
164335         -
164336 164739   /*
164337 164740   ** Rtree virtual table module xBestIndex method. There are three
164338 164741   ** table scan strategies to choose from (in order from most to 
164339 164742   ** least desirable):
164340 164743   **
164341 164744   **   idxNum     idxStr        Strategy
164342 164745   **   ------------------------------------------------
................................................................................
164408 164811         /* This strategy involves a two rowid lookups on an B-Tree structures
164409 164812         ** and then a linear search of an R-Tree node. This should be 
164410 164813         ** considered almost as quick as a direct rowid lookup (for which 
164411 164814         ** sqlite uses an internal cost of 0.0). It is expected to return
164412 164815         ** a single row.
164413 164816         */ 
164414 164817         pIdxInfo->estimatedCost = 30.0;
164415         -      setEstimatedRows(pIdxInfo, 1);
       164818  +      pIdxInfo->estimatedRows = 1;
164416 164819         return SQLITE_OK;
164417 164820       }
164418 164821   
164419 164822       if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
164420 164823         u8 op;
164421 164824         switch( p->op ){
164422 164825           case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
................................................................................
164426 164829           case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
164427 164830           default:
164428 164831             assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
164429 164832             op = RTREE_MATCH; 
164430 164833             break;
164431 164834         }
164432 164835         zIdxStr[iIdx++] = op;
164433         -      zIdxStr[iIdx++] = p->iColumn - 1 + '0';
       164836  +      zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
164434 164837         pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
164435 164838         pIdxInfo->aConstraintUsage[ii].omit = 1;
164436 164839       }
164437 164840     }
164438 164841   
164439 164842     pIdxInfo->idxNum = 2;
164440 164843     pIdxInfo->needToFreeIdxStr = 1;
164441 164844     if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
164442 164845       return SQLITE_NOMEM;
164443 164846     }
164444 164847   
164445 164848     nRow = pRtree->nRowEst >> (iIdx/2);
164446 164849     pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
164447         -  setEstimatedRows(pIdxInfo, nRow);
       164850  +  pIdxInfo->estimatedRows = nRow;
164448 164851   
164449 164852     return rc;
164450 164853   }
164451 164854   
164452 164855   /*
164453 164856   ** Return the N-dimensional volumn of the cell stored in *p.
164454 164857   */
164455 164858   static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
164456 164859     RtreeDValue area = (RtreeDValue)1;
164457         -  int ii;
164458         -  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164459         -    area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
       164860  +  assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
       164861  +#ifndef SQLITE_RTREE_INT_ONLY
       164862  +  if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
       164863  +    switch( pRtree->nDim ){
       164864  +      case 5:  area  = p->aCoord[9].f - p->aCoord[8].f;
       164865  +      case 4:  area *= p->aCoord[7].f - p->aCoord[6].f;
       164866  +      case 3:  area *= p->aCoord[5].f - p->aCoord[4].f;
       164867  +      case 2:  area *= p->aCoord[3].f - p->aCoord[2].f;
       164868  +      default: area *= p->aCoord[1].f - p->aCoord[0].f;
       164869  +    }
       164870  +  }else
       164871  +#endif
       164872  +  {
       164873  +    switch( pRtree->nDim ){
       164874  +      case 5:  area  = p->aCoord[9].i - p->aCoord[8].i;
       164875  +      case 4:  area *= p->aCoord[7].i - p->aCoord[6].i;
       164876  +      case 3:  area *= p->aCoord[5].i - p->aCoord[4].i;
       164877  +      case 2:  area *= p->aCoord[3].i - p->aCoord[2].i;
       164878  +      default: area *= p->aCoord[1].i - p->aCoord[0].i;
       164879  +    }
164460 164880     }
164461 164881     return area;
164462 164882   }
164463 164883   
164464 164884   /*
164465 164885   ** Return the margin length of cell p. The margin length is the sum
164466 164886   ** of the objects size in each dimension.
164467 164887   */
164468 164888   static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
164469         -  RtreeDValue margin = (RtreeDValue)0;
164470         -  int ii;
164471         -  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       164889  +  RtreeDValue margin = 0;
       164890  +  int ii = pRtree->nDim2 - 2;
       164891  +  do{
164472 164892       margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
164473         -  }
       164893  +    ii -= 2;
       164894  +  }while( ii>=0 );
164474 164895     return margin;
164475 164896   }
164476 164897   
164477 164898   /*
164478 164899   ** Store the union of cells p1 and p2 in p1.
164479 164900   */
164480 164901   static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164481         -  int ii;
       164902  +  int ii = 0;
164482 164903     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164483         -    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       164904  +    do{
164484 164905         p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
164485 164906         p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
164486         -    }
       164907  +      ii += 2;
       164908  +    }while( ii<pRtree->nDim2 );
164487 164909     }else{
164488         -    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       164910  +    do{
164489 164911         p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
164490 164912         p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
164491         -    }
       164913  +      ii += 2;
       164914  +    }while( ii<pRtree->nDim2 );
164492 164915     }
164493 164916   }
164494 164917   
164495 164918   /*
164496 164919   ** Return true if the area covered by p2 is a subset of the area covered
164497 164920   ** by p1. False otherwise.
164498 164921   */
164499 164922   static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164500 164923     int ii;
164501 164924     int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
164502         -  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       164925  +  for(ii=0; ii<pRtree->nDim2; ii+=2){
164503 164926       RtreeCoord *a1 = &p1->aCoord[ii];
164504 164927       RtreeCoord *a2 = &p2->aCoord[ii];
164505 164928       if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) 
164506 164929        || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i)) 
164507 164930       ){
164508 164931         return 0;
164509 164932       }
................................................................................
164530 164953     int nCell
164531 164954   ){
164532 164955     int ii;
164533 164956     RtreeDValue overlap = RTREE_ZERO;
164534 164957     for(ii=0; ii<nCell; ii++){
164535 164958       int jj;
164536 164959       RtreeDValue o = (RtreeDValue)1;
164537         -    for(jj=0; jj<(pRtree->nDim*2); jj+=2){
       164960  +    for(jj=0; jj<pRtree->nDim2; jj+=2){
164538 164961         RtreeDValue x1, x2;
164539 164962         x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
164540 164963         x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
164541 164964         if( x2<x1 ){
164542 164965           o = (RtreeDValue)0;
164543 164966           break;
164544 164967         }else{
................................................................................
165586 166009       **
165587 166010       ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
165588 166011       ** with "column" that are interpreted as table constraints.
165589 166012       ** Example:  CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
165590 166013       ** This problem was discovered after years of use, so we silently ignore
165591 166014       ** these kinds of misdeclared tables to avoid breaking any legacy.
165592 166015       */
165593         -    assert( nData<=(pRtree->nDim*2 + 3) );
       166016  +    assert( nData<=(pRtree->nDim2 + 3) );
165594 166017   
165595 166018   #ifndef SQLITE_RTREE_INT_ONLY
165596 166019       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
165597 166020         for(ii=0; ii<nData-4; ii+=2){
165598 166021           cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
165599 166022           cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
165600 166023           if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
................................................................................
165675 166098       }
165676 166099     }
165677 166100   
165678 166101   constraint:
165679 166102     rtreeRelease(pRtree);
165680 166103     return rc;
165681 166104   }
       166105  +
       166106  +/*
       166107  +** Called when a transaction starts.
       166108  +*/
       166109  +static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
       166110  +  Rtree *pRtree = (Rtree *)pVtab;
       166111  +  assert( pRtree->inWrTrans==0 );
       166112  +  pRtree->inWrTrans++;
       166113  +  return SQLITE_OK;
       166114  +}
       166115  +
       166116  +/*
       166117  +** Called when a transaction completes (either by COMMIT or ROLLBACK).
       166118  +** The sqlite3_blob object should be released at this point.
       166119  +*/
       166120  +static int rtreeEndTransaction(sqlite3_vtab *pVtab){
       166121  +  Rtree *pRtree = (Rtree *)pVtab;
       166122  +  pRtree->inWrTrans = 0;
       166123  +  nodeBlobReset(pRtree);
       166124  +  return SQLITE_OK;
       166125  +}
165682 166126   
165683 166127   /*
165684 166128   ** The xRename method for rtree module virtual tables.
165685 166129   */
165686 166130   static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
165687 166131     Rtree *pRtree = (Rtree *)pVtab;
165688 166132     int rc = SQLITE_NOMEM;
................................................................................
165696 166140     );
165697 166141     if( zSql ){
165698 166142       rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
165699 166143       sqlite3_free(zSql);
165700 166144     }
165701 166145     return rc;
165702 166146   }
       166147  +
165703 166148   
165704 166149   /*
165705 166150   ** This function populates the pRtree->nRowEst variable with an estimate
165706 166151   ** of the number of rows in the virtual table. If possible, this is based
165707 166152   ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
165708 166153   */
165709 166154   static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
................................................................................
165756 166201     rtreeClose,                 /* xClose - close a cursor */
165757 166202     rtreeFilter,                /* xFilter - configure scan constraints */
165758 166203     rtreeNext,                  /* xNext - advance a cursor */
165759 166204     rtreeEof,                   /* xEof */
165760 166205     rtreeColumn,                /* xColumn - read data */
165761 166206     rtreeRowid,                 /* xRowid - read data */
165762 166207     rtreeUpdate,                /* xUpdate - write data */
165763         -  0,                          /* xBegin - begin transaction */
165764         -  0,                          /* xSync - sync transaction */
165765         -  0,                          /* xCommit - commit transaction */
165766         -  0,                          /* xRollback - rollback transaction */
       166208  +  rtreeBeginTransaction,      /* xBegin - begin transaction */
       166209  +  rtreeEndTransaction,        /* xSync - sync transaction */
       166210  +  rtreeEndTransaction,        /* xCommit - commit transaction */
       166211  +  rtreeEndTransaction,        /* xRollback - rollback transaction */
165767 166212     0,                          /* xFindFunction - function overloading */
165768 166213     rtreeRename,                /* xRename - rename the table */
165769 166214     0,                          /* xSavepoint */
165770 166215     0,                          /* xRelease */
165771         -  0                           /* xRollbackTo */
       166216  +  0,                          /* xRollbackTo */
165772 166217   };
165773 166218   
165774 166219   static int rtreeSqlInit(
165775 166220     Rtree *pRtree, 
165776 166221     sqlite3 *db, 
165777 166222     const char *zDb, 
165778 166223     const char *zPrefix, 
165779 166224     int isCreate
165780 166225   ){
165781 166226     int rc = SQLITE_OK;
165782 166227   
165783         -  #define N_STATEMENT 9
       166228  +  #define N_STATEMENT 8
165784 166229     static const char *azSql[N_STATEMENT] = {
165785         -    /* Read and write the xxx_node table */
165786         -    "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
       166230  +    /* Write the xxx_node table */
165787 166231       "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
165788 166232       "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
165789 166233   
165790 166234       /* Read and write the xxx_rowid table */
165791 166235       "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
165792 166236       "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
165793 166237       "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
................................................................................
165817 166261       rc = sqlite3_exec(db, zCreate, 0, 0, 0);
165818 166262       sqlite3_free(zCreate);
165819 166263       if( rc!=SQLITE_OK ){
165820 166264         return rc;
165821 166265       }
165822 166266     }
165823 166267   
165824         -  appStmt[0] = &pRtree->pReadNode;
165825         -  appStmt[1] = &pRtree->pWriteNode;
165826         -  appStmt[2] = &pRtree->pDeleteNode;
165827         -  appStmt[3] = &pRtree->pReadRowid;
165828         -  appStmt[4] = &pRtree->pWriteRowid;
165829         -  appStmt[5] = &pRtree->pDeleteRowid;
165830         -  appStmt[6] = &pRtree->pReadParent;
165831         -  appStmt[7] = &pRtree->pWriteParent;
165832         -  appStmt[8] = &pRtree->pDeleteParent;
       166268  +  appStmt[0] = &pRtree->pWriteNode;
       166269  +  appStmt[1] = &pRtree->pDeleteNode;
       166270  +  appStmt[2] = &pRtree->pReadRowid;
       166271  +  appStmt[3] = &pRtree->pWriteRowid;
       166272  +  appStmt[4] = &pRtree->pDeleteRowid;
       166273  +  appStmt[5] = &pRtree->pReadParent;
       166274  +  appStmt[6] = &pRtree->pWriteParent;
       166275  +  appStmt[7] = &pRtree->pDeleteParent;
165833 166276   
165834 166277     rc = rtreeQueryStat1(db, pRtree);
165835 166278     for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
165836 166279       char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
165837 166280       if( zSql ){
165838 166281         rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0); 
165839 166282       }else{
................................................................................
165963 166406       return SQLITE_NOMEM;
165964 166407     }
165965 166408     memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
165966 166409     pRtree->nBusy = 1;
165967 166410     pRtree->base.pModule = &rtreeModule;
165968 166411     pRtree->zDb = (char *)&pRtree[1];
165969 166412     pRtree->zName = &pRtree->zDb[nDb+1];
165970         -  pRtree->nDim = (argc-4)/2;
165971         -  pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
165972         -  pRtree->eCoordType = eCoordType;
       166413  +  pRtree->nDim = (u8)((argc-4)/2);
       166414  +  pRtree->nDim2 = pRtree->nDim*2;
       166415  +  pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
       166416  +  pRtree->eCoordType = (u8)eCoordType;
165973 166417     memcpy(pRtree->zDb, argv[1], nDb);
165974 166418     memcpy(pRtree->zName, argv[2], nName);
165975 166419   
165976 166420     /* Figure out the node size to use. */
165977 166421     rc = getNodeSize(db, pRtree, isCreate, pzErr);
165978 166422   
165979 166423     /* Create/Connect to the underlying relational database schema. If
................................................................................
166038 166482     RtreeNode node;
166039 166483     Rtree tree;
166040 166484     int ii;
166041 166485   
166042 166486     UNUSED_PARAMETER(nArg);
166043 166487     memset(&node, 0, sizeof(RtreeNode));
166044 166488     memset(&tree, 0, sizeof(Rtree));
166045         -  tree.nDim = sqlite3_value_int(apArg[0]);
       166489  +  tree.nDim = (u8)sqlite3_value_int(apArg[0]);
       166490  +  tree.nDim2 = tree.nDim*2;
166046 166491     tree.nBytesPerCell = 8 + 8 * tree.nDim;
166047 166492     node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
166048 166493   
166049 166494     for(ii=0; ii<NCELL(&node); ii++){
166050 166495       char zCell[512];
166051 166496       int nCell = 0;
166052 166497       RtreeCell cell;
166053 166498       int jj;
166054 166499   
166055 166500       nodeGetCell(&tree, &node, ii, &cell);
166056 166501       sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
166057 166502       nCell = (int)strlen(zCell);
166058         -    for(jj=0; jj<tree.nDim*2; jj++){
       166503  +    for(jj=0; jj<tree.nDim2; jj++){
166059 166504   #ifndef SQLITE_RTREE_INT_ONLY
166060 166505         sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
166061 166506                          (double)cell.aCoord[jj].f);
166062 166507   #else
166063 166508         sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
166064 166509                          cell.aCoord[jj].i);
166065 166510   #endif
................................................................................
166759 167204     }
166760 167205   }
166761 167206   
166762 167207   /*
166763 167208   ** Register the ICU extension functions with database db.
166764 167209   */
166765 167210   SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
166766         -  struct IcuScalar {
       167211  +  static const struct IcuScalar {
166767 167212       const char *zName;                        /* Function name */
166768         -    int nArg;                                 /* Number of arguments */
166769         -    int enc;                                  /* Optimal text encoding */
166770         -    void *pContext;                           /* sqlite3_user_data() context */
       167213  +    unsigned char nArg;                       /* Number of arguments */
       167214  +    unsigned short enc;                       /* Optimal text encoding */
       167215  +    unsigned char iContext;                   /* sqlite3_user_data() context */
166771 167216       void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
166772 167217     } scalars[] = {
166773         -    {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC,          0, icuRegexpFunc},
166774         -
166775         -    {"lower",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
166776         -    {"lower",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
166777         -    {"upper",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166778         -    {"upper",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166779         -
166780         -    {"lower",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuCaseFunc16},
166781         -    {"lower",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuCaseFunc16},
166782         -    {"upper",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,  (void*)1, icuCaseFunc16},
166783         -    {"upper",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,  (void*)1, icuCaseFunc16},
166784         -
166785         -    {"like",   2, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuLikeFunc},
166786         -    {"like",   3, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuLikeFunc},
166787         -
166788         -    {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
       167218  +    {"icu_load_collation",  2, SQLITE_UTF8,                1, icuLoadCollation},
       167219  +    {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC,         0, icuRegexpFunc},
       167220  +    {"lower",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC,       0, icuCaseFunc16},
       167221  +    {"lower",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC,       0, icuCaseFunc16},
       167222  +    {"upper",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC,       1, icuCaseFunc16},
       167223  +    {"upper",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC,       1, icuCaseFunc16},
       167224  +    {"lower",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
       167225  +    {"lower",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
       167226  +    {"upper",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,        1, icuCaseFunc16},
       167227  +    {"upper",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        1, icuCaseFunc16},
       167228  +    {"like",   2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuLikeFunc},
       167229  +    {"like",   3, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuLikeFunc},
166789 167230     };
166790         -
166791 167231     int rc = SQLITE_OK;
166792 167232     int i;
166793 167233   
       167234  +  
166794 167235     for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
166795         -    struct IcuScalar *p = &scalars[i];
       167236  +    const struct IcuScalar *p = &scalars[i];
166796 167237       rc = sqlite3_create_function(
166797         -        db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
       167238  +        db, p->zName, p->nArg, p->enc, 
       167239  +        p->iContext ? (void*)db : (void*)0,
       167240  +        p->xFunc, 0, 0
166798 167241       );
166799 167242     }
166800 167243   
166801 167244     return rc;
166802 167245   }
166803 167246   
166804 167247   #if !SQLITE_CORE
................................................................................
176155 176598     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
176156 176599     int iVal,                       /* Index of conflict record value to fetch */
176157 176600     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
176158 176601   ){
176159 176602     if( !pIter->pConflict ){
176160 176603       return SQLITE_MISUSE;
176161 176604     }
176162         -  if( iVal<0 || iVal>=sqlite3_column_count(pIter->pConflict) ){
       176605  +  if( iVal<0 || iVal>=pIter->nCol ){
176163 176606       return SQLITE_RANGE;
176164 176607     }
176165 176608     *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
176166 176609     return SQLITE_OK;
176167 176610   }
176168 176611   
176169 176612   /*
................................................................................
176622 177065   ){
176623 177066     int rc = SQLITE_OK;
176624 177067     int i;
176625 177068     SessionBuffer buf = {0, 0, 0};
176626 177069   
176627 177070     sessionAppendStr(&buf, "INSERT INTO main.", &rc);
176628 177071     sessionAppendIdent(&buf, zTab, &rc);
176629         -  sessionAppendStr(&buf, " VALUES(?", &rc);
       177072  +  sessionAppendStr(&buf, "(", &rc);
       177073  +  for(i=0; i<p->nCol; i++){
       177074  +    if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
       177075  +    sessionAppendIdent(&buf, p->azCol[i], &rc);
       177076  +  }
       177077  +
       177078  +  sessionAppendStr(&buf, ") VALUES(?", &rc);
176630 177079     for(i=1; i<p->nCol; i++){
176631 177080       sessionAppendStr(&buf, ", ?", &rc);
176632 177081     }
176633 177082     sessionAppendStr(&buf, ")", &rc);
176634 177083   
176635 177084     if( rc==SQLITE_OK ){
176636 177085       rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
................................................................................
177168 177617           if( zTab==0 ){
177169 177618             rc = SQLITE_NOMEM;
177170 177619             break;
177171 177620           }
177172 177621           nTab = (int)strlen(zTab);
177173 177622           sApply.azCol = (const char **)zTab;
177174 177623         }else{
       177624  +        int nMinCol = 0;
       177625  +        int i;
       177626  +
177175 177627           sqlite3changeset_pk(pIter, &abPK, 0);
177176 177628           rc = sessionTableInfo(
177177 177629               db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
177178 177630           );
177179 177631           if( rc!=SQLITE_OK ) break;
       177632  +        for(i=0; i<sApply.nCol; i++){
       177633  +          if( sApply.abPK[i] ) nMinCol = i+1;
       177634  +        }
177180 177635     
177181 177636           if( sApply.nCol==0 ){
177182 177637             schemaMismatch = 1;
177183 177638             sqlite3_log(SQLITE_SCHEMA, 
177184 177639                 "sqlite3changeset_apply(): no such table: %s", zTab
177185 177640             );
177186 177641           }
177187         -        else if( sApply.nCol!=nCol ){
       177642  +        else if( sApply.nCol<nCol ){
177188 177643             schemaMismatch = 1;
177189 177644             sqlite3_log(SQLITE_SCHEMA, 
177190         -              "sqlite3changeset_apply(): table %s has %d columns, expected %d", 
       177645  +              "sqlite3changeset_apply(): table %s has %d columns, "
       177646  +              "expected %d or more", 
177191 177647                 zTab, sApply.nCol, nCol
177192 177648             );
177193 177649           }
177194         -        else if( memcmp(sApply.abPK, abPK, nCol)!=0 ){
       177650  +        else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
177195 177651             schemaMismatch = 1;
177196 177652             sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
177197 177653                 "primary key mismatch for table %s", zTab
177198 177654             );
177199 177655           }
177200         -        else if( 
177201         -            (rc = sessionSelectRow(db, zTab, &sApply))
177202         -         || (rc = sessionUpdateRow(db, zTab, &sApply))
177203         -         || (rc = sessionDeleteRow(db, zTab, &sApply))
177204         -         || (rc = sessionInsertRow(db, zTab, &sApply))
177205         -        ){
177206         -          break;
       177656  +        else{
       177657  +          sApply.nCol = nCol;
       177658  +          if((rc = sessionSelectRow(db, zTab, &sApply))
       177659  +          || (rc = sessionUpdateRow(db, zTab, &sApply))
       177660  +          || (rc = sessionDeleteRow(db, zTab, &sApply))
       177661  +          || (rc = sessionInsertRow(db, zTab, &sApply))
       177662  +          ){
       177663  +            break;
       177664  +          }
177207 177665           }
177208 177666           nTab = sqlite3Strlen30(zTab);
177209 177667         }
177210 177668       }
177211 177669   
177212 177670       /* If there is a schema mismatch on the current table, proceed to the
177213 177671       ** next change. A log message has already been issued. */
................................................................................
177791 178249   ** For the time being, all JSON is stored as pure text.  (We might add
177792 178250   ** a JSONB type in the future which stores a binary encoding of JSON in
177793 178251   ** a BLOB, but there is no support for JSONB in the current implementation.
177794 178252   ** This implementation parses JSON text at 250 MB/s, so it is hard to see
177795 178253   ** how JSONB might improve on that.)
177796 178254   */
177797 178255   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
177798         -#if !defined(_SQLITEINT_H_)
       178256  +#if !defined(SQLITEINT_H)
177799 178257   /* #include "sqlite3ext.h" */
177800 178258   #endif
177801 178259   SQLITE_EXTENSION_INIT1
177802 178260   /* #include <assert.h> */
177803 178261   /* #include <string.h> */
177804 178262   /* #include <stdlib.h> */
177805 178263   /* #include <stdarg.h> */
................................................................................
181842 182300   ** putting an appropriate #define in the %include section of the input
181843 182301   ** grammar.
181844 182302   */
181845 182303   #ifndef fts5YYMALLOCARGTYPE
181846 182304   # define fts5YYMALLOCARGTYPE size_t
181847 182305   #endif
181848 182306   
       182307  +/* Initialize a new parser that has already been allocated.
       182308  +*/
       182309  +static void sqlite3Fts5ParserInit(void *fts5yypParser){
       182310  +  fts5yyParser *pParser = (fts5yyParser*)fts5yypParser;
       182311  +#ifdef fts5YYTRACKMAXSTACKDEPTH
       182312  +  pParser->fts5yyhwm = 0;
       182313  +#endif
       182314  +#if fts5YYSTACKDEPTH<=0
       182315  +  pParser->fts5yytos = NULL;
       182316  +  pParser->fts5yystack = NULL;
       182317  +  pParser->fts5yystksz = 0;
       182318  +  if( fts5yyGrowStack(pParser) ){
       182319  +    pParser->fts5yystack = &pParser->fts5yystk0;
       182320  +    pParser->fts5yystksz = 1;
       182321  +  }
       182322  +#endif
       182323  +#ifndef fts5YYNOERRORRECOVERY
       182324  +  pParser->fts5yyerrcnt = -1;
       182325  +#endif
       182326  +  pParser->fts5yytos = pParser->fts5yystack;
       182327  +  pParser->fts5yystack[0].stateno = 0;
       182328  +  pParser->fts5yystack[0].major = 0;
       182329  +}
       182330  +
       182331  +#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
181849 182332   /* 
181850 182333   ** This function allocates a new parser.
181851 182334   ** The only argument is a pointer to a function which works like
181852 182335   ** malloc.
181853 182336   **
181854 182337   ** Inputs:
181855 182338   ** A pointer to the function used to allocate memory.
................................................................................
181857 182340   ** Outputs:
181858 182341   ** A pointer to a parser.  This pointer is used in subsequent calls
181859 182342   ** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
181860 182343   */
181861 182344   static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE)){
181862 182345     fts5yyParser *pParser;
181863 182346     pParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
181864         -  if( pParser ){
181865         -#ifdef fts5YYTRACKMAXSTACKDEPTH
181866         -    pParser->fts5yyhwm = 0;
181867         -#endif
181868         -#if fts5YYSTACKDEPTH<=0
181869         -    pParser->fts5yytos = NULL;
181870         -    pParser->fts5yystack = NULL;
181871         -    pParser->fts5yystksz = 0;
181872         -    if( fts5yyGrowStack(pParser) ){
181873         -      pParser->fts5yystack = &pParser->fts5yystk0;
181874         -      pParser->fts5yystksz = 1;
181875         -    }
181876         -#endif
181877         -#ifndef fts5YYNOERRORRECOVERY
181878         -    pParser->fts5yyerrcnt = -1;
181879         -#endif
181880         -    pParser->fts5yytos = pParser->fts5yystack;
181881         -    pParser->fts5yystack[0].stateno = 0;
181882         -    pParser->fts5yystack[0].major = 0;
181883         -  }
       182347  +  if( pParser ) sqlite3Fts5ParserInit(pParser);
181884 182348     return pParser;
181885 182349   }
       182350  +#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
       182351  +
181886 182352   
181887 182353   /* The following function deletes the "minor type" or semantic value
181888 182354   ** associated with a symbol.  The symbol can be either a terminal
181889 182355   ** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
181890 182356   ** a pointer to the value to be deleted.  The code used to do the 
181891 182357   ** deletions is derived from the %destructor and/or %token_destructor
181892 182358   ** directives of the input grammar.
................................................................................
181960 182426         fts5yyTracePrompt,
181961 182427         fts5yyTokenName[fts5yytos->major]);
181962 182428     }
181963 182429   #endif
181964 182430     fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
181965 182431   }
181966 182432   
       182433  +/*
       182434  +** Clear all secondary memory allocations from the parser
       182435  +*/
       182436  +static void sqlite3Fts5ParserFinalize(void *p){
       182437  +  fts5yyParser *pParser = (fts5yyParser*)p;
       182438  +  while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
       182439  +#if fts5YYSTACKDEPTH<=0
       182440  +  if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
       182441  +#endif
       182442  +}
       182443  +
       182444  +#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
181967 182445   /* 
181968 182446   ** Deallocate and destroy a parser.  Destructors are called for
181969 182447   ** all stack elements before shutting the parser down.
181970 182448   **
181971 182449   ** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
181972 182450   ** is defined in a %include section of the input grammar) then it is
181973 182451   ** assumed that the input pointer is never NULL.
181974 182452   */
181975 182453   static void sqlite3Fts5ParserFree(
181976 182454     void *p,                    /* The parser to be deleted */
181977 182455     void (*freeProc)(void*)     /* Function used to reclaim memory */
181978 182456   ){
181979         -  fts5yyParser *pParser = (fts5yyParser*)p;
181980 182457   #ifndef fts5YYPARSEFREENEVERNULL
181981         -  if( pParser==0 ) return;
       182458  +  if( p==0 ) return;
181982 182459   #endif
181983         -  while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
181984         -#if fts5YYSTACKDEPTH<=0
181985         -  if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
181986         -#endif
181987         -  (*freeProc)((void*)pParser);
       182460  +  sqlite3Fts5ParserFinalize(p);
       182461  +  (*freeProc)(p);
181988 182462   }
       182463  +#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
181989 182464   
181990 182465   /*
181991 182466   ** Return the peak depth of the stack for a parser.
181992 182467   */
181993 182468   #ifdef fts5YYTRACKMAXSTACKDEPTH
181994 182469   static int sqlite3Fts5ParserStackPeak(void *p){
181995 182470     fts5yyParser *pParser = (fts5yyParser*)p;
................................................................................
186320 186795     char *z = 0;
186321 186796   
186322 186797     memset(&sCtx, 0, sizeof(TokenCtx));
186323 186798     sCtx.pPhrase = pAppend;
186324 186799   
186325 186800     rc = fts5ParseStringFromToken(pToken, &z);
186326 186801     if( rc==SQLITE_OK ){
186327         -    int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_QUERY : 0);
       186802  +    int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
186328 186803       int n;
186329 186804       sqlite3Fts5Dequote(z);
186330 186805       n = (int)strlen(z);
186331 186806       rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
186332 186807     }
186333 186808     sqlite3_free(z);
186334 186809     if( rc || (rc = sCtx.rc) ){
................................................................................
197061 197536   static void fts5SourceIdFunc(
197062 197537     sqlite3_context *pCtx,          /* Function call context */
197063 197538     int nArg,                       /* Number of args */
197064 197539     sqlite3_value **apUnused        /* Function arguments */
197065 197540   ){
197066 197541     assert( nArg==0 );
197067 197542     UNUSED_PARAM2(nArg, apUnused);
197068         -  sqlite3_result_text(pCtx, "fts5: 2017-01-19 18:20:36 ffd559afd32dcdce9c733ebccdee88fda9b689cf", -1, SQLITE_TRANSIENT);
       197543  +  sqlite3_result_text(pCtx, "fts5: 2017-02-09 17:12:22 798fb9d70d2e5f95e64237b04d6692360133381a", -1, SQLITE_TRANSIENT);
197069 197544   }
197070 197545   
197071 197546   static int fts5Init(sqlite3 *db){
197072 197547     static const sqlite3_module fts5Mod = {
197073 197548       /* iVersion      */ 2,
197074 197549       /* xCreate       */ fts5CreateMethod,
197075 197550       /* xConnect      */ fts5ConnectMethod,

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

   119    119   **
   120    120   ** See also: [sqlite3_libversion()],
   121    121   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   122    122   ** [sqlite_version()] and [sqlite_source_id()].
   123    123   */
   124    124   #define SQLITE_VERSION        "3.17.0"
   125    125   #define SQLITE_VERSION_NUMBER 3017000
   126         -#define SQLITE_SOURCE_ID      "2017-01-19 18:20:36 ffd559afd32dcdce9c733ebccdee88fda9b689cf"
          126  +#define SQLITE_SOURCE_ID      "2017-02-09 17:12:22 798fb9d70d2e5f95e64237b04d6692360133381a"
   127    127   
   128    128   /*
   129    129   ** CAPI3REF: Run-Time Library Version Numbers
   130    130   ** KEYWORDS: sqlite3_version sqlite3_sourceid
   131    131   **
   132    132   ** These interfaces provide the same information as the [SQLITE_VERSION],
   133    133   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
   572    572   ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
   573    573   ** information is written to disk in the same order as calls
   574    574   ** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
   575    575   ** after reboot following a crash or power loss, the only bytes in a
   576    576   ** file that were written at the application level might have changed
   577    577   ** and that adjacent bytes, even bytes within the same sector are
   578    578   ** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
   579         -** flag indicate that a file cannot be deleted when open.  The
          579  +** flag indicates that a file cannot be deleted when open.  The
   580    580   ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
   581    581   ** read-only media and cannot be changed even by processes with
   582    582   ** elevated privileges.
   583    583   */
   584    584   #define SQLITE_IOCAP_ATOMIC                 0x00000001
   585    585   #define SQLITE_IOCAP_ATOMIC512              0x00000002
   586    586   #define SQLITE_IOCAP_ATOMIC1K               0x00000004
................................................................................
   722    722   ** <li> [SQLITE_IOCAP_ATOMIC4K]
   723    723   ** <li> [SQLITE_IOCAP_ATOMIC8K]
   724    724   ** <li> [SQLITE_IOCAP_ATOMIC16K]
   725    725   ** <li> [SQLITE_IOCAP_ATOMIC32K]
   726    726   ** <li> [SQLITE_IOCAP_ATOMIC64K]
   727    727   ** <li> [SQLITE_IOCAP_SAFE_APPEND]
   728    728   ** <li> [SQLITE_IOCAP_SEQUENTIAL]
          729  +** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
          730  +** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
          731  +** <li> [SQLITE_IOCAP_IMMUTABLE]
   729    732   ** </ul>
   730    733   **
   731    734   ** The SQLITE_IOCAP_ATOMIC property means that all writes of
   732    735   ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
   733    736   ** mean that writes of blocks that are nnn bytes in size and
   734    737   ** are aligned to an address which is an integer multiple of
   735    738   ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
................................................................................
  5410   5413   ** ^In the case of an update, this is the [rowid] after the update takes place.
  5411   5414   **
  5412   5415   ** ^(The update hook is not invoked when internal system tables are
  5413   5416   ** modified (i.e. sqlite_master and sqlite_sequence).)^
  5414   5417   ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
  5415   5418   **
  5416   5419   ** ^In the current implementation, the update hook
  5417         -** is not invoked when duplication rows are deleted because of an
         5420  +** is not invoked when conflicting rows are deleted because of an
  5418   5421   ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
  5419   5422   ** invoked when rows are deleted using the [truncate optimization].
  5420   5423   ** The exceptions defined in this paragraph might change in a future
  5421   5424   ** release of SQLite.
  5422   5425   **
  5423   5426   ** The update hook implementation must not do anything that will modify
  5424   5427   ** the database connection that invoked the update hook.  Any actions
................................................................................
  6192   6195   **         being opened for read/write access)^.
  6193   6196   ** </ul>
  6194   6197   **
  6195   6198   ** ^Unless it returns SQLITE_MISUSE, this function sets the 
  6196   6199   ** [database connection] error code and message accessible via 
  6197   6200   ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. 
  6198   6201   **
         6202  +** A BLOB referenced by sqlite3_blob_open() may be read using the
         6203  +** [sqlite3_blob_read()] interface and modified by using
         6204  +** [sqlite3_blob_write()].  The [BLOB handle] can be moved to a
         6205  +** different row of the same table using the [sqlite3_blob_reopen()]
         6206  +** interface.  However, the column, table, or database of a [BLOB handle]
         6207  +** cannot be changed after the [BLOB handle] is opened.
  6199   6208   **
  6200   6209   ** ^(If the row that a BLOB handle points to is modified by an
  6201   6210   ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
  6202   6211   ** then the BLOB handle is marked as "expired".
  6203   6212   ** This is true if any column of the row is changed, even a column
  6204   6213   ** other than the one the BLOB handle is open on.)^
  6205   6214   ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
................................................................................
  6215   6224   **
  6216   6225   ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
  6217   6226   ** and the built-in [zeroblob] SQL function may be used to create a 
  6218   6227   ** zero-filled blob to read or write using the incremental-blob interface.
  6219   6228   **
  6220   6229   ** To avoid a resource leak, every open [BLOB handle] should eventually
  6221   6230   ** be released by a call to [sqlite3_blob_close()].
         6231  +**
         6232  +** See also: [sqlite3_blob_close()],
         6233  +** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
         6234  +** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
  6222   6235   */
  6223   6236   SQLITE_API int sqlite3_blob_open(
  6224   6237     sqlite3*,
  6225   6238     const char *zDb,
  6226   6239     const char *zTable,
  6227   6240     const char *zColumn,
  6228   6241     sqlite3_int64 iRow,
................................................................................
  6230   6243     sqlite3_blob **ppBlob
  6231   6244   );
  6232   6245   
  6233   6246   /*
  6234   6247   ** CAPI3REF: Move a BLOB Handle to a New Row
  6235   6248   ** METHOD: sqlite3_blob
  6236   6249   **
  6237         -** ^This function is used to move an existing blob handle so that it points
         6250  +** ^This function is used to move an existing [BLOB handle] so that it points
  6238   6251   ** to a different row of the same database table. ^The new row is identified
  6239   6252   ** by the rowid value passed as the second argument. Only the row can be
  6240   6253   ** changed. ^The database, table and column on which the blob handle is open
  6241         -** remain the same. Moving an existing blob handle to a new row can be
         6254  +** remain the same. Moving an existing [BLOB handle] to a new row is
  6242   6255   ** faster than closing the existing handle and opening a new one.
  6243   6256   **
  6244   6257   ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
  6245   6258   ** it must exist and there must be either a blob or text value stored in
  6246   6259   ** the nominated column.)^ ^If the new row is not present in the table, or if
  6247   6260   ** it does not contain a blob or text value, or if another error occurs, an
  6248   6261   ** SQLite error code is returned and the blob handle is considered aborted.
................................................................................
  8163   8176   ** CAPI3REF: The pre-update hook.
  8164   8177   **
  8165   8178   ** ^These interfaces are only available if SQLite is compiled using the
  8166   8179   ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
  8167   8180   **
  8168   8181   ** ^The [sqlite3_preupdate_hook()] interface registers a callback function
  8169   8182   ** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
  8170         -** on a [rowid table].
         8183  +** on a database table.
  8171   8184   ** ^At most one preupdate hook may be registered at a time on a single
  8172   8185   ** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
  8173   8186   ** the previous setting.
  8174   8187   ** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
  8175   8188   ** with a NULL pointer as the second parameter.
  8176   8189   ** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
  8177   8190   ** the first parameter to callbacks.
  8178   8191   **
  8179         -** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate
  8180         -** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID]
  8181         -** tables.
         8192  +** ^The preupdate hook only fires for changes to real database tables; the
         8193  +** preupdate hook is not invoked for changes to [virtual tables] or to
         8194  +** system tables like sqlite_master or sqlite_stat1.
  8182   8195   **
  8183   8196   ** ^The second parameter to the preupdate callback is a pointer to
  8184   8197   ** the [database connection] that registered the preupdate hook.
  8185   8198   ** ^The third parameter to the preupdate callback is one of the constants
  8186   8199   ** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
  8187   8200   ** kind of update operation that is about to occur.
  8188   8201   ** ^(The fourth parameter to the preupdate callback is the name of the
  8189   8202   ** database within the database connection that is being modified.  This
  8190   8203   ** will be "main" for the main database or "temp" for TEMP tables or 
  8191   8204   ** the name given after the AS keyword in the [ATTACH] statement for attached
  8192   8205   ** databases.)^
  8193   8206   ** ^The fifth parameter to the preupdate callback is the name of the
  8194   8207   ** table that is being modified.
  8195         -** ^The sixth parameter to the preupdate callback is the initial [rowid] of the
  8196         -** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is
  8197         -** undefined for SQLITE_INSERT changes.
  8198         -** ^The seventh parameter to the preupdate callback is the final [rowid] of
  8199         -** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is
  8200         -** undefined for SQLITE_DELETE changes.
         8208  +**
         8209  +** For an UPDATE or DELETE operation on a [rowid table], the sixth
         8210  +** parameter passed to the preupdate callback is the initial [rowid] of the 
         8211  +** row being modified or deleted. For an INSERT operation on a rowid table,
         8212  +** or any operation on a WITHOUT ROWID table, the value of the sixth 
         8213  +** parameter is undefined. For an INSERT or UPDATE on a rowid table the
         8214  +** seventh parameter is the final rowid value of the row being inserted
         8215  +** or updated. The value of the seventh parameter passed to the callback
         8216  +** function is not defined for operations on WITHOUT ROWID tables, or for
         8217  +** INSERT operations on rowid tables.
  8201   8218   **
  8202   8219   ** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
  8203   8220   ** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
  8204   8221   ** provide additional information about a preupdate event. These routines
  8205   8222   ** may only be called from within a preupdate callback.  Invoking any of
  8206   8223   ** these routines from outside of a preupdate callback or with a
  8207   8224   ** [database connection] pointer that is different from the one supplied
................................................................................
  8629   8646   ** either of these things are undefined.
  8630   8647   **
  8631   8648   ** The session object will be used to create changesets for tables in
  8632   8649   ** database zDb, where zDb is either "main", or "temp", or the name of an
  8633   8650   ** attached database. It is not an error if database zDb is not attached
  8634   8651   ** to the database when the session object is created.
  8635   8652   */
  8636         -int sqlite3session_create(
         8653  +SQLITE_API int sqlite3session_create(
  8637   8654     sqlite3 *db,                    /* Database handle */
  8638   8655     const char *zDb,                /* Name of db (e.g. "main") */
  8639   8656     sqlite3_session **ppSession     /* OUT: New session object */
  8640   8657   );
  8641   8658   
  8642   8659   /*
  8643   8660   ** CAPI3REF: Delete A Session Object
................................................................................
  8647   8664   ** results of attempting to use pSession with any other session module
  8648   8665   ** function are undefined.
  8649   8666   **
  8650   8667   ** Session objects must be deleted before the database handle to which they
  8651   8668   ** are attached is closed. Refer to the documentation for 
  8652   8669   ** [sqlite3session_create()] for details.
  8653   8670   */
  8654         -void sqlite3session_delete(sqlite3_session *pSession);
         8671  +SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
  8655   8672   
  8656   8673   
  8657   8674   /*
  8658   8675   ** CAPI3REF: Enable Or Disable A Session Object
  8659   8676   **
  8660   8677   ** Enable or disable the recording of changes by a session object. When
  8661   8678   ** enabled, a session object records changes made to the database. When
................................................................................
  8667   8684   ** Passing zero to this function disables the session. Passing a value
  8668   8685   ** greater than zero enables it. Passing a value less than zero is a 
  8669   8686   ** no-op, and may be used to query the current state of the session.
  8670   8687   **
  8671   8688   ** The return value indicates the final state of the session object: 0 if 
  8672   8689   ** the session is disabled, or 1 if it is enabled.
  8673   8690   */
  8674         -int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
         8691  +SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
  8675   8692   
  8676   8693   /*
  8677   8694   ** CAPI3REF: Set Or Clear the Indirect Change Flag
  8678   8695   **
  8679   8696   ** Each change recorded by a session object is marked as either direct or
  8680   8697   ** indirect. A change is marked as indirect if either:
  8681   8698   **
................................................................................
  8696   8713   ** is set. Passing a value less than zero does not modify the current value
  8697   8714   ** of the indirect flag, and may be used to query the current state of the 
  8698   8715   ** indirect flag for the specified session object.
  8699   8716   **
  8700   8717   ** The return value indicates the final state of the indirect flag: 0 if 
  8701   8718   ** it is clear, or 1 if it is set.
  8702   8719   */
  8703         -int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
         8720  +SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
  8704   8721   
  8705   8722   /*
  8706   8723   ** CAPI3REF: Attach A Table To A Session Object
  8707   8724   **
  8708   8725   ** If argument zTab is not NULL, then it is the name of a table to attach
  8709   8726   ** to the session object passed as the first argument. All subsequent changes 
  8710   8727   ** made to the table while the session object is enabled will be recorded. See 
................................................................................
  8726   8743   **
  8727   8744   ** Changes are not recorded for individual rows that have NULL values stored
  8728   8745   ** in one or more of their PRIMARY KEY columns.
  8729   8746   **
  8730   8747   ** SQLITE_OK is returned if the call completes without error. Or, if an error 
  8731   8748   ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
  8732   8749   */
  8733         -int sqlite3session_attach(
         8750  +SQLITE_API int sqlite3session_attach(
  8734   8751     sqlite3_session *pSession,      /* Session object */
  8735   8752     const char *zTab                /* Table name */
  8736   8753   );
  8737   8754   
  8738   8755   /*
  8739   8756   ** CAPI3REF: Set a table filter on a Session Object.
  8740   8757   **
  8741   8758   ** The second argument (xFilter) is the "filter callback". For changes to rows 
  8742   8759   ** in tables that are not attached to the Session object, the filter is called
  8743   8760   ** to determine whether changes to the table's rows should be tracked or not. 
  8744   8761   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
  8745   8762   ** attached, xFilter will not be called again.
  8746   8763   */
  8747         -void sqlite3session_table_filter(
         8764  +SQLITE_API void sqlite3session_table_filter(
  8748   8765     sqlite3_session *pSession,      /* Session object */
  8749   8766     int(*xFilter)(
  8750   8767       void *pCtx,                   /* Copy of third arg to _filter_table() */
  8751   8768       const char *zTab              /* Table name */
  8752   8769     ),
  8753   8770     void *pCtx                      /* First argument passed to xFilter */
  8754   8771   );
................................................................................
  8853   8870   ** is inserted while a session object is enabled, then later deleted while 
  8854   8871   ** the same session object is disabled, no INSERT record will appear in the
  8855   8872   ** changeset, even though the delete took place while the session was disabled.
  8856   8873   ** Or, if one field of a row is updated while a session is disabled, and 
  8857   8874   ** another field of the same row is updated while the session is enabled, the
  8858   8875   ** resulting changeset will contain an UPDATE change that updates both fields.
  8859   8876   */
  8860         -int sqlite3session_changeset(
         8877  +SQLITE_API int sqlite3session_changeset(
  8861   8878     sqlite3_session *pSession,      /* Session object */
  8862   8879     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
  8863   8880     void **ppChangeset              /* OUT: Buffer containing changeset */
  8864   8881   );
  8865   8882   
  8866   8883   /*
  8867   8884   ** CAPI3REF: Load The Difference Between Tables Into A Session 
................................................................................
  8897   8914   **   <li> For each row (primary key) that exists in the to-table but not in 
  8898   8915   **     the from-table, an INSERT record is added to the session object.
  8899   8916   **
  8900   8917   **   <li> For each row (primary key) that exists in the to-table but not in 
  8901   8918   **     the from-table, a DELETE record is added to the session object.
  8902   8919   **
  8903   8920   **   <li> For each row (primary key) that exists in both tables, but features 
  8904         -**     different in each, an UPDATE record is added to the session.
         8921  +**     different non-PK values in each, an UPDATE record is added to the
         8922  +**     session.  
  8905   8923   ** </ul>
  8906   8924   **
  8907   8925   ** To clarify, if this function is called and then a changeset constructed
  8908   8926   ** using [sqlite3session_changeset()], then after applying that changeset to 
  8909   8927   ** database zFrom the contents of the two compatible tables would be 
  8910   8928   ** identical.
  8911   8929   **
................................................................................
  8914   8932   **
  8915   8933   ** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
  8916   8934   ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
  8917   8935   ** may be set to point to a buffer containing an English language error 
  8918   8936   ** message. It is the responsibility of the caller to free this buffer using
  8919   8937   ** sqlite3_free().
  8920   8938   */
  8921         -int sqlite3session_diff(
         8939  +SQLITE_API int sqlite3session_diff(
  8922   8940     sqlite3_session *pSession,
  8923   8941     const char *zFromDb,
  8924   8942     const char *zTbl,
  8925   8943     char **pzErrMsg
  8926   8944   );
  8927   8945   
  8928   8946   
................................................................................
  8950   8968   ** in the same way as for changesets.
  8951   8969   **
  8952   8970   ** Changes within a patchset are ordered in the same way as for changesets
  8953   8971   ** generated by the sqlite3session_changeset() function (i.e. all changes for
  8954   8972   ** a single table are grouped together, tables appear in the order in which
  8955   8973   ** they were attached to the session object).
  8956   8974   */
  8957         -int sqlite3session_patchset(
         8975  +SQLITE_API int sqlite3session_patchset(
  8958   8976     sqlite3_session *pSession,      /* Session object */
  8959   8977     int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
  8960   8978     void **ppPatchset               /* OUT: Buffer containing changeset */
  8961   8979   );
  8962   8980   
  8963   8981   /*
  8964   8982   ** CAPI3REF: Test if a changeset has recorded any changes.
................................................................................
  8971   8989   ** [sqlite3session_changeset()] on the session handle may still return a
  8972   8990   ** changeset that contains no changes. This can happen when a row in 
  8973   8991   ** an attached table is modified and then later on the original values 
  8974   8992   ** are restored. However, if this function returns non-zero, then it is
  8975   8993   ** guaranteed that a call to sqlite3session_changeset() will return a 
  8976   8994   ** changeset containing zero changes.
  8977   8995   */
  8978         -int sqlite3session_isempty(sqlite3_session *pSession);
         8996  +SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
  8979   8997   
  8980   8998   /*
  8981   8999   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
  8982   9000   **
  8983   9001   ** Create an iterator used to iterate through the contents of a changeset.
  8984   9002   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
  8985   9003   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
................................................................................
  9006   9024   ** that apply to a single table are grouped together. This means that when 
  9007   9025   ** an application iterates through a changeset using an iterator created by 
  9008   9026   ** this function, all changes that relate to a single table are visited 
  9009   9027   ** consecutively. There is no chance that the iterator will visit a change 
  9010   9028   ** the applies to table X, then one for table Y, and then later on visit 
  9011   9029   ** another change for table X.
  9012   9030   */
  9013         -int sqlite3changeset_start(
         9031  +SQLITE_API int sqlite3changeset_start(
  9014   9032     sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
  9015   9033     int nChangeset,                 /* Size of changeset blob in bytes */
  9016   9034     void *pChangeset                /* Pointer to blob containing changeset */
  9017   9035   );
  9018   9036   
  9019   9037   
  9020   9038   /*
................................................................................
  9035   9053   ** Otherwise, if all changes in the changeset have already been visited,
  9036   9054   ** SQLITE_DONE is returned.
  9037   9055   **
  9038   9056   ** If an error occurs, an SQLite error code is returned. Possible error 
  9039   9057   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
  9040   9058   ** SQLITE_NOMEM.
  9041   9059   */
  9042         -int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
         9060  +SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
  9043   9061   
  9044   9062   /*
  9045   9063   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
  9046   9064   **
  9047   9065   ** The pIter argument passed to this function may either be an iterator
  9048   9066   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9049   9067   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
................................................................................
  9063   9081   ** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the 
  9064   9082   ** type of change that the iterator currently points to.
  9065   9083   **
  9066   9084   ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
  9067   9085   ** SQLite error code is returned. The values of the output variables may not
  9068   9086   ** be trusted in this case.
  9069   9087   */
  9070         -int sqlite3changeset_op(
         9088  +SQLITE_API int sqlite3changeset_op(
  9071   9089     sqlite3_changeset_iter *pIter,  /* Iterator object */
  9072   9090     const char **pzTab,             /* OUT: Pointer to table name */
  9073   9091     int *pnCol,                     /* OUT: Number of columns in table */
  9074   9092     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
  9075   9093     int *pbIndirect                 /* OUT: True for an 'indirect' change */
  9076   9094   );
  9077   9095   
................................................................................
  9096   9114   ** in the table.
  9097   9115   **
  9098   9116   ** If this function is called when the iterator does not point to a valid
  9099   9117   ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
  9100   9118   ** SQLITE_OK is returned and the output variables populated as described
  9101   9119   ** above.
  9102   9120   */
  9103         -int sqlite3changeset_pk(
         9121  +SQLITE_API int sqlite3changeset_pk(
  9104   9122     sqlite3_changeset_iter *pIter,  /* Iterator object */
  9105   9123     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
  9106   9124     int *pnCol                      /* OUT: Number of entries in output array */
  9107   9125   );
  9108   9126   
  9109   9127   /*
  9110   9128   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
................................................................................
  9126   9144   ** original row values stored as part of the UPDATE or DELETE change and
  9127   9145   ** returns SQLITE_OK. The name of the function comes from the fact that this 
  9128   9146   ** is similar to the "old.*" columns available to update or delete triggers.
  9129   9147   **
  9130   9148   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9131   9149   ** is returned and *ppValue is set to NULL.
  9132   9150   */
  9133         -int sqlite3changeset_old(
         9151  +SQLITE_API int sqlite3changeset_old(
  9134   9152     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9135   9153     int iVal,                       /* Column number */
  9136   9154     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
  9137   9155   );
  9138   9156   
  9139   9157   /*
  9140   9158   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
................................................................................
  9159   9177   ** SQLITE_OK returned. The name of the function comes from the fact that 
  9160   9178   ** this is similar to the "new.*" columns available to update or delete 
  9161   9179   ** triggers.
  9162   9180   **
  9163   9181   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9164   9182   ** is returned and *ppValue is set to NULL.
  9165   9183   */
  9166         -int sqlite3changeset_new(
         9184  +SQLITE_API int sqlite3changeset_new(
  9167   9185     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9168   9186     int iVal,                       /* Column number */
  9169   9187     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
  9170   9188   );
  9171   9189   
  9172   9190   /*
  9173   9191   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
................................................................................
  9186   9204   ** sqlite3_value object containing the iVal'th value from the 
  9187   9205   ** "conflicting row" associated with the current conflict-handler callback
  9188   9206   ** and returns SQLITE_OK.
  9189   9207   **
  9190   9208   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9191   9209   ** is returned and *ppValue is set to NULL.
  9192   9210   */
  9193         -int sqlite3changeset_conflict(
         9211  +SQLITE_API int sqlite3changeset_conflict(
  9194   9212     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9195   9213     int iVal,                       /* Column number */
  9196   9214     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
  9197   9215   );
  9198   9216   
  9199   9217   /*
  9200   9218   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
................................................................................
  9202   9220   ** This function may only be called with an iterator passed to an
  9203   9221   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
  9204   9222   ** it sets the output variable to the total number of known foreign key
  9205   9223   ** violations in the destination database and returns SQLITE_OK.
  9206   9224   **
  9207   9225   ** In all other cases this function returns SQLITE_MISUSE.
  9208   9226   */
  9209         -int sqlite3changeset_fk_conflicts(
         9227  +SQLITE_API int sqlite3changeset_fk_conflicts(
  9210   9228     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9211   9229     int *pnOut                      /* OUT: Number of FK violations */
  9212   9230   );
  9213   9231   
  9214   9232   
  9215   9233   /*
  9216   9234   ** CAPI3REF: Finalize A Changeset Iterator
................................................................................
  9235   9253   **     // Do something with change.
  9236   9254   **   }
  9237   9255   **   rc = sqlite3changeset_finalize();
  9238   9256   **   if( rc!=SQLITE_OK ){
  9239   9257   **     // An error has occurred 
  9240   9258   **   }
  9241   9259   */
  9242         -int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
         9260  +SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
  9243   9261   
  9244   9262   /*
  9245   9263   ** CAPI3REF: Invert A Changeset
  9246   9264   **
  9247   9265   ** This function is used to "invert" a changeset object. Applying an inverted
  9248   9266   ** changeset to a database reverses the effects of applying the uninverted
  9249   9267   ** changeset. Specifically:
................................................................................
  9265   9283   ** It is the responsibility of the caller to eventually call sqlite3_free()
  9266   9284   ** on the *ppOut pointer to free the buffer allocation following a successful 
  9267   9285   ** call to this function.
  9268   9286   **
  9269   9287   ** WARNING/TODO: This function currently assumes that the input is a valid
  9270   9288   ** changeset. If it is not, the results are undefined.
  9271   9289   */
  9272         -int sqlite3changeset_invert(
         9290  +SQLITE_API int sqlite3changeset_invert(
  9273   9291     int nIn, const void *pIn,       /* Input changeset */
  9274   9292     int *pnOut, void **ppOut        /* OUT: Inverse of input */
  9275   9293   );
  9276   9294   
  9277   9295   /*
  9278   9296   ** CAPI3REF: Concatenate Two Changeset Objects
  9279   9297   **
................................................................................
  9294   9312   **   }else{
  9295   9313   **     *ppOut = 0;
  9296   9314   **     *pnOut = 0;
  9297   9315   **   }
  9298   9316   **
  9299   9317   ** Refer to the sqlite3_changegroup documentation below for details.
  9300   9318   */
  9301         -int sqlite3changeset_concat(
         9319  +SQLITE_API int sqlite3changeset_concat(
  9302   9320     int nA,                         /* Number of bytes in buffer pA */
  9303   9321     void *pA,                       /* Pointer to buffer containing changeset A */
  9304   9322     int nB,                         /* Number of bytes in buffer pB */
  9305   9323     void *pB,                       /* Pointer to buffer containing changeset B */
  9306   9324     int *pnOut,                     /* OUT: Number of bytes in output changeset */
  9307   9325     void **ppOut                    /* OUT: Buffer containing output changeset */
  9308   9326   );
................................................................................
  9482   9500   ** For each table that is not excluded by the filter callback, this function 
  9483   9501   ** tests that the target database contains a compatible table. A table is 
  9484   9502   ** considered compatible if all of the following are true:
  9485   9503   **
  9486   9504   ** <ul>
  9487   9505   **   <li> The table has the same name as the name recorded in the 
  9488   9506   **        changeset, and
  9489         -**   <li> The table has the same number of columns as recorded in the 
         9507  +**   <li> The table has at least as many columns as recorded in the 
  9490   9508   **        changeset, and
  9491   9509   **   <li> The table has primary key columns in the same position as 
  9492   9510   **        recorded in the changeset.
  9493   9511   ** </ul>
  9494   9512   **
  9495   9513   ** If there is no compatible table, it is not an error, but none of the
  9496   9514   ** changes associated with the table are applied. A warning message is issued
................................................................................
  9527   9545   **   original row values stored in the changeset. If it does, and the values 
  9528   9546   **   stored in all non-primary key columns also match the values stored in 
  9529   9547   **   the changeset the row is deleted from the target database.
  9530   9548   **
  9531   9549   **   If a row with matching primary key values is found, but one or more of
  9532   9550   **   the non-primary key fields contains a value different from the original
  9533   9551   **   row value stored in the changeset, the conflict-handler function is
  9534         -**   invoked with [SQLITE_CHANGESET_DATA] as the second argument.
         9552  +**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
         9553  +**   database table has more columns than are recorded in the changeset,
         9554  +**   only the values of those non-primary key fields are compared against
         9555  +**   the current database contents - any trailing database table columns
         9556  +**   are ignored.
  9535   9557   **
  9536   9558   **   If no row with matching primary key values is found in the database,
  9537   9559   **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  9538   9560   **   passed as the second argument.
  9539   9561   **
  9540   9562   **   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
  9541   9563   **   (which can only happen if a foreign key constraint is violated), the
................................................................................
  9542   9564   **   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
  9543   9565   **   passed as the second argument. This includes the case where the DELETE
  9544   9566   **   operation is attempted because an earlier call to the conflict handler
  9545   9567   **   function returned [SQLITE_CHANGESET_REPLACE].
  9546   9568   **
  9547   9569   ** <dt>INSERT Changes<dd>
  9548   9570   **   For each INSERT change, an attempt is made to insert the new row into
  9549         -**   the database.
         9571  +**   the database. If the changeset row contains fewer fields than the
         9572  +**   database table, the trailing fields are populated with their default
         9573  +**   values.
  9550   9574   **
  9551   9575   **   If the attempt to insert the row fails because the database already 
  9552   9576   **   contains a row with the same primary key values, the conflict handler
  9553   9577   **   function is invoked with the second argument set to 
  9554   9578   **   [SQLITE_CHANGESET_CONFLICT].
  9555   9579   **
  9556   9580   **   If the attempt to insert the row fails because of some other constraint
................................................................................
  9560   9584   **   an earlier call to the conflict handler function returned 
  9561   9585   **   [SQLITE_CHANGESET_REPLACE].
  9562   9586   **
  9563   9587   ** <dt>UPDATE Changes<dd>
  9564   9588   **   For each UPDATE change, this function checks if the target database 
  9565   9589   **   contains a row with the same primary key value (or values) as the 
  9566   9590   **   original row values stored in the changeset. If it does, and the values 
  9567         -**   stored in all non-primary key columns also match the values stored in 
  9568         -**   the changeset the row is updated within the target database.
         9591  +**   stored in all modified non-primary key columns also match the values
         9592  +**   stored in the changeset the row is updated within the target database.
  9569   9593   **
  9570   9594   **   If a row with matching primary key values is found, but one or more of
  9571         -**   the non-primary key fields contains a value different from an original
  9572         -**   row value stored in the changeset, the conflict-handler function is
  9573         -**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
         9595  +**   the modified non-primary key fields contains a value different from an
         9596  +**   original row value stored in the changeset, the conflict-handler function
         9597  +**   is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
  9574   9598   **   UPDATE changes only contain values for non-primary key fields that are
  9575   9599   **   to be modified, only those fields need to match the original values to
  9576   9600   **   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
  9577   9601   **
  9578   9602   **   If no row with matching primary key values is found in the database,
  9579   9603   **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  9580   9604   **   passed as the second argument.
................................................................................
  9594   9618   **
  9595   9619   ** All changes made by this function are enclosed in a savepoint transaction.
  9596   9620   ** If any other error (aside from a constraint failure when attempting to
  9597   9621   ** write to the target database) occurs, then the savepoint transaction is
  9598   9622   ** rolled back, restoring the target database to its original state, and an 
  9599   9623   ** SQLite error code returned.
  9600   9624   */
  9601         -int sqlite3changeset_apply(
         9625  +SQLITE_API int sqlite3changeset_apply(
  9602   9626     sqlite3 *db,                    /* Apply change to "main" db of this handle */
  9603   9627     int nChangeset,                 /* Size of changeset in bytes */
  9604   9628     void *pChangeset,               /* Changeset blob */
  9605   9629     int(*xFilter)(
  9606   9630       void *pCtx,                   /* Copy of sixth arg to _apply() */
  9607   9631       const char *zTab              /* Table name */
  9608   9632     ),
................................................................................
  9795   9819   ** is immediately abandoned and the streaming API function returns a copy
  9796   9820   ** of the xOutput error code to the application.
  9797   9821   **
  9798   9822   ** The sessions module never invokes an xOutput callback with the third 
  9799   9823   ** parameter set to a value less than or equal to zero. Other than this,
  9800   9824   ** no guarantees are made as to the size of the chunks of data returned.
  9801   9825   */
  9802         -int sqlite3changeset_apply_strm(
         9826  +SQLITE_API int sqlite3changeset_apply_strm(
  9803   9827     sqlite3 *db,                    /* Apply change to "main" db of this handle */
  9804   9828     int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
  9805   9829     void *pIn,                                          /* First arg for xInput */
  9806   9830     int(*xFilter)(
  9807   9831       void *pCtx,                   /* Copy of sixth arg to _apply() */
  9808   9832       const char *zTab              /* Table name */
  9809   9833     ),
................................................................................
  9810   9834     int(*xConflict)(
  9811   9835       void *pCtx,                   /* Copy of sixth arg to _apply() */
  9812   9836       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
  9813   9837       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
  9814   9838     ),
  9815   9839     void *pCtx                      /* First argument passed to xConflict */
  9816   9840   );
  9817         -int sqlite3changeset_concat_strm(
         9841  +SQLITE_API int sqlite3changeset_concat_strm(
  9818   9842     int (*xInputA)(void *pIn, void *pData, int *pnData),
  9819   9843     void *pInA,
  9820   9844     int (*xInputB)(void *pIn, void *pData, int *pnData),
  9821   9845     void *pInB,
  9822   9846     int (*xOutput)(void *pOut, const void *pData, int nData),
  9823   9847     void *pOut
  9824   9848   );
  9825         -int sqlite3changeset_invert_strm(
         9849  +SQLITE_API int sqlite3changeset_invert_strm(
  9826   9850     int (*xInput)(void *pIn, void *pData, int *pnData),
  9827   9851     void *pIn,
  9828   9852     int (*xOutput)(void *pOut, const void *pData, int nData),
  9829   9853     void *pOut
  9830   9854   );
  9831         -int sqlite3changeset_start_strm(
         9855  +SQLITE_API int sqlite3changeset_start_strm(
  9832   9856     sqlite3_changeset_iter **pp,
  9833   9857     int (*xInput)(void *pIn, void *pData, int *pnData),
  9834   9858     void *pIn
  9835   9859   );
  9836         -int sqlite3session_changeset_strm(
         9860  +SQLITE_API int sqlite3session_changeset_strm(
  9837   9861     sqlite3_session *pSession,
  9838   9862     int (*xOutput)(void *pOut, const void *pData, int nData),
  9839   9863     void *pOut
  9840   9864   );
  9841         -int sqlite3session_patchset_strm(
         9865  +SQLITE_API int sqlite3session_patchset_strm(
  9842   9866     sqlite3_session *pSession,
  9843   9867     int (*xOutput)(void *pOut, const void *pData, int nData),
  9844   9868     void *pOut
  9845   9869   );
  9846   9870   int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
  9847   9871       int (*xInput)(void *pIn, void *pData, int *pnData),
  9848   9872       void *pIn

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

  1842   1842   ** putting an appropriate #define in the %include section of the input
  1843   1843   ** grammar.
  1844   1844   */
  1845   1845   #ifndef fts5YYMALLOCARGTYPE
  1846   1846   # define fts5YYMALLOCARGTYPE size_t
  1847   1847   #endif
  1848   1848   
         1849  +/* Initialize a new parser that has already been allocated.
         1850  +*/
         1851  +static void sqlite3Fts5ParserInit(void *fts5yypParser){
         1852  +  fts5yyParser *pParser = (fts5yyParser*)fts5yypParser;
         1853  +#ifdef fts5YYTRACKMAXSTACKDEPTH
         1854  +  pParser->fts5yyhwm = 0;
         1855  +#endif
         1856  +#if fts5YYSTACKDEPTH<=0
         1857  +  pParser->fts5yytos = NULL;
         1858  +  pParser->fts5yystack = NULL;
         1859  +  pParser->fts5yystksz = 0;
         1860  +  if( fts5yyGrowStack(pParser) ){
         1861  +    pParser->fts5yystack = &pParser->fts5yystk0;
         1862  +    pParser->fts5yystksz = 1;
         1863  +  }
         1864  +#endif
         1865  +#ifndef fts5YYNOERRORRECOVERY
         1866  +  pParser->fts5yyerrcnt = -1;
         1867  +#endif
         1868  +  pParser->fts5yytos = pParser->fts5yystack;
         1869  +  pParser->fts5yystack[0].stateno = 0;
         1870  +  pParser->fts5yystack[0].major = 0;
         1871  +}
         1872  +
         1873  +#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
  1849   1874   /* 
  1850   1875   ** This function allocates a new parser.
  1851   1876   ** The only argument is a pointer to a function which works like
  1852   1877   ** malloc.
  1853   1878   **
  1854   1879   ** Inputs:
  1855   1880   ** A pointer to the function used to allocate memory.
................................................................................
  1857   1882   ** Outputs:
  1858   1883   ** A pointer to a parser.  This pointer is used in subsequent calls
  1859   1884   ** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
  1860   1885   */
  1861   1886   static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE)){
  1862   1887     fts5yyParser *pParser;
  1863   1888     pParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
  1864         -  if( pParser ){
  1865         -#ifdef fts5YYTRACKMAXSTACKDEPTH
  1866         -    pParser->fts5yyhwm = 0;
  1867         -#endif
  1868         -#if fts5YYSTACKDEPTH<=0
  1869         -    pParser->fts5yytos = NULL;
  1870         -    pParser->fts5yystack = NULL;
  1871         -    pParser->fts5yystksz = 0;
  1872         -    if( fts5yyGrowStack(pParser) ){
  1873         -      pParser->fts5yystack = &pParser->fts5yystk0;
  1874         -      pParser->fts5yystksz = 1;
  1875         -    }
  1876         -#endif
  1877         -#ifndef fts5YYNOERRORRECOVERY
  1878         -    pParser->fts5yyerrcnt = -1;
  1879         -#endif
  1880         -    pParser->fts5yytos = pParser->fts5yystack;
  1881         -    pParser->fts5yystack[0].stateno = 0;
  1882         -    pParser->fts5yystack[0].major = 0;
  1883         -  }
         1889  +  if( pParser ) sqlite3Fts5ParserInit(pParser);
  1884   1890     return pParser;
  1885   1891   }
         1892  +#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
         1893  +
  1886   1894   
  1887   1895   /* The following function deletes the "minor type" or semantic value
  1888   1896   ** associated with a symbol.  The symbol can be either a terminal
  1889   1897   ** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
  1890   1898   ** a pointer to the value to be deleted.  The code used to do the 
  1891   1899   ** deletions is derived from the %destructor and/or %token_destructor
  1892   1900   ** directives of the input grammar.
................................................................................
  1909   1917       ** inside the C code.
  1910   1918       */
  1911   1919   /********* Begin destructor definitions ***************************************/
  1912   1920       case 16: /* input */
  1913   1921   {
  1914   1922   #line 83 "fts5parse.y"
  1915   1923    (void)pParse; 
  1916         -#line 517 "fts5parse.c"
         1924  +#line 525 "fts5parse.c"
  1917   1925   }
  1918   1926         break;
  1919   1927       case 17: /* expr */
  1920   1928       case 18: /* cnearset */
  1921   1929       case 19: /* exprlist */
  1922   1930   {
  1923   1931   #line 89 "fts5parse.y"
  1924   1932    sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24)); 
  1925         -#line 526 "fts5parse.c"
         1933  +#line 534 "fts5parse.c"
  1926   1934   }
  1927   1935         break;
  1928   1936       case 20: /* nearset */
  1929   1937       case 23: /* nearphrases */
  1930   1938   {
  1931   1939   #line 143 "fts5parse.y"
  1932   1940    sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46)); 
  1933         -#line 534 "fts5parse.c"
         1941  +#line 542 "fts5parse.c"
  1934   1942   }
  1935   1943         break;
  1936   1944       case 21: /* colset */
  1937   1945       case 22: /* colsetlist */
  1938   1946   {
  1939   1947   #line 119 "fts5parse.y"
  1940   1948    sqlite3_free((fts5yypminor->fts5yy11)); 
  1941         -#line 542 "fts5parse.c"
         1949  +#line 550 "fts5parse.c"
  1942   1950   }
  1943   1951         break;
  1944   1952       case 24: /* phrase */
  1945   1953   {
  1946   1954   #line 174 "fts5parse.y"
  1947   1955    sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53)); 
  1948         -#line 549 "fts5parse.c"
         1956  +#line 557 "fts5parse.c"
  1949   1957   }
  1950   1958         break;
  1951   1959   /********* End destructor definitions *****************************************/
  1952   1960       default:  break;   /* If no destructor action specified: do nothing */
  1953   1961     }
  1954   1962   }
  1955   1963   
................................................................................
  1970   1978         fts5yyTracePrompt,
  1971   1979         fts5yyTokenName[fts5yytos->major]);
  1972   1980     }
  1973   1981   #endif
  1974   1982     fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
  1975   1983   }
  1976   1984   
         1985  +/*
         1986  +** Clear all secondary memory allocations from the parser
         1987  +*/
         1988  +static void sqlite3Fts5ParserFinalize(void *p){
         1989  +  fts5yyParser *pParser = (fts5yyParser*)p;
         1990  +  while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
         1991  +#if fts5YYSTACKDEPTH<=0
         1992  +  if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
         1993  +#endif
         1994  +}
         1995  +
         1996  +#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
  1977   1997   /* 
  1978   1998   ** Deallocate and destroy a parser.  Destructors are called for
  1979   1999   ** all stack elements before shutting the parser down.
  1980   2000   **
  1981   2001   ** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
  1982   2002   ** is defined in a %include section of the input grammar) then it is
  1983   2003   ** assumed that the input pointer is never NULL.
  1984   2004   */
  1985   2005   static void sqlite3Fts5ParserFree(
  1986   2006     void *p,                    /* The parser to be deleted */
  1987   2007     void (*freeProc)(void*)     /* Function used to reclaim memory */
  1988   2008   ){
  1989         -  fts5yyParser *pParser = (fts5yyParser*)p;
  1990   2009   #ifndef fts5YYPARSEFREENEVERNULL
  1991         -  if( pParser==0 ) return;
         2010  +  if( p==0 ) return;
  1992   2011   #endif
  1993         -  while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
  1994         -#if fts5YYSTACKDEPTH<=0
  1995         -  if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
  1996         -#endif
  1997         -  (*freeProc)((void*)pParser);
         2012  +  sqlite3Fts5ParserFinalize(p);
         2013  +  (*freeProc)(p);
  1998   2014   }
         2015  +#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
  1999   2016   
  2000   2017   /*
  2001   2018   ** Return the peak depth of the stack for a parser.
  2002   2019   */
  2003   2020   #ifdef fts5YYTRACKMAXSTACKDEPTH
  2004   2021   static int sqlite3Fts5ParserStackPeak(void *p){
  2005   2022     fts5yyParser *pParser = (fts5yyParser*)p;
................................................................................
  2114   2131      while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
  2115   2132      /* Here code is inserted which will execute if the parser
  2116   2133      ** stack every overflows */
  2117   2134   /******** Begin %stack_overflow code ******************************************/
  2118   2135   #line 36 "fts5parse.y"
  2119   2136   
  2120   2137     sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
  2121         -#line 722 "fts5parse.c"
         2138  +#line 739 "fts5parse.c"
  2122   2139   /******** End %stack_overflow code ********************************************/
  2123   2140      sqlite3Fts5ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
  2124   2141   }
  2125   2142   
  2126   2143   /*
  2127   2144   ** Print tracing information for a SHIFT action
  2128   2145   */
................................................................................
  2280   2297     **     break;
  2281   2298     */
  2282   2299   /********** Begin reduce actions **********************************************/
  2283   2300           fts5YYMINORTYPE fts5yylhsminor;
  2284   2301         case 0: /* input ::= expr */
  2285   2302   #line 82 "fts5parse.y"
  2286   2303   { sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy24); }
  2287         -#line 888 "fts5parse.c"
         2304  +#line 905 "fts5parse.c"
  2288   2305           break;
  2289   2306         case 1: /* expr ::= expr AND expr */
  2290   2307   #line 92 "fts5parse.y"
  2291   2308   {
  2292   2309     fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
  2293   2310   }
  2294         -#line 895 "fts5parse.c"
         2311  +#line 912 "fts5parse.c"
  2295   2312     fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
  2296   2313           break;
  2297   2314         case 2: /* expr ::= expr OR expr */
  2298   2315   #line 95 "fts5parse.y"
  2299   2316   {
  2300   2317     fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
  2301   2318   }
  2302         -#line 903 "fts5parse.c"
         2319  +#line 920 "fts5parse.c"
  2303   2320     fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
  2304   2321           break;
  2305   2322         case 3: /* expr ::= expr NOT expr */
  2306   2323   #line 98 "fts5parse.y"
  2307   2324   {
  2308   2325     fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
  2309   2326   }
  2310         -#line 911 "fts5parse.c"
         2327  +#line 928 "fts5parse.c"
  2311   2328     fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
  2312   2329           break;
  2313   2330         case 4: /* expr ::= LP expr RP */
  2314   2331   #line 102 "fts5parse.y"
  2315   2332   {fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
  2316         -#line 917 "fts5parse.c"
         2333  +#line 934 "fts5parse.c"
  2317   2334           break;
  2318   2335         case 5: /* expr ::= exprlist */
  2319   2336         case 6: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==6);
  2320   2337   #line 103 "fts5parse.y"
  2321   2338   {fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
  2322         -#line 923 "fts5parse.c"
         2339  +#line 940 "fts5parse.c"
  2323   2340     fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
  2324   2341           break;
  2325   2342         case 7: /* exprlist ::= exprlist cnearset */
  2326   2343   #line 106 "fts5parse.y"
  2327   2344   {
  2328   2345     fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24);
  2329   2346   }
  2330         -#line 931 "fts5parse.c"
         2347  +#line 948 "fts5parse.c"
  2331   2348     fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
  2332   2349           break;
  2333   2350         case 8: /* cnearset ::= nearset */
  2334   2351   #line 110 "fts5parse.y"
  2335   2352   { 
  2336   2353     fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46); 
  2337   2354   }
  2338         -#line 939 "fts5parse.c"
         2355  +#line 956 "fts5parse.c"
  2339   2356     fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
  2340   2357           break;
  2341   2358         case 9: /* cnearset ::= colset COLON nearset */
  2342   2359   #line 113 "fts5parse.y"
  2343   2360   { 
  2344   2361     sqlite3Fts5ParseSetColset(pParse, fts5yymsp[0].minor.fts5yy46, fts5yymsp[-2].minor.fts5yy11);
  2345   2362     fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46); 
  2346   2363   }
  2347         -#line 948 "fts5parse.c"
         2364  +#line 965 "fts5parse.c"
  2348   2365     fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
  2349   2366           break;
  2350   2367         case 10: /* colset ::= MINUS LCP colsetlist RCP */
  2351   2368   #line 123 "fts5parse.y"
  2352   2369   { 
  2353   2370       fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
  2354   2371   }
  2355         -#line 956 "fts5parse.c"
         2372  +#line 973 "fts5parse.c"
  2356   2373           break;
  2357   2374         case 11: /* colset ::= LCP colsetlist RCP */
  2358   2375   #line 126 "fts5parse.y"
  2359   2376   { fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
  2360         -#line 961 "fts5parse.c"
         2377  +#line 978 "fts5parse.c"
  2361   2378           break;
  2362   2379         case 12: /* colset ::= STRING */
  2363   2380   #line 127 "fts5parse.y"
  2364   2381   {
  2365   2382     fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
  2366   2383   }
  2367         -#line 968 "fts5parse.c"
         2384  +#line 985 "fts5parse.c"
  2368   2385     fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
  2369   2386           break;
  2370   2387         case 13: /* colset ::= MINUS STRING */
  2371   2388   #line 130 "fts5parse.y"
  2372   2389   {
  2373   2390     fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
  2374   2391     fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
  2375   2392   }
  2376         -#line 977 "fts5parse.c"
         2393  +#line 994 "fts5parse.c"
  2377   2394           break;
  2378   2395         case 14: /* colsetlist ::= colsetlist STRING */
  2379   2396   #line 135 "fts5parse.y"
  2380   2397   { 
  2381   2398     fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
  2382         -#line 983 "fts5parse.c"
         2399  +#line 1000 "fts5parse.c"
  2383   2400     fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
  2384   2401           break;
  2385   2402         case 15: /* colsetlist ::= STRING */
  2386   2403   #line 137 "fts5parse.y"
  2387   2404   { 
  2388   2405     fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0); 
  2389   2406   }
  2390         -#line 991 "fts5parse.c"
         2407  +#line 1008 "fts5parse.c"
  2391   2408     fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
  2392   2409           break;
  2393   2410         case 16: /* nearset ::= phrase */
  2394   2411   #line 146 "fts5parse.y"
  2395   2412   { fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
  2396         -#line 997 "fts5parse.c"
         2413  +#line 1014 "fts5parse.c"
  2397   2414     fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
  2398   2415           break;
  2399   2416         case 17: /* nearset ::= STRING LP nearphrases neardist_opt RP */
  2400   2417   #line 147 "fts5parse.y"
  2401   2418   {
  2402   2419     sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
  2403   2420     sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
  2404   2421     fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
  2405   2422   }
  2406         -#line 1007 "fts5parse.c"
         2423  +#line 1024 "fts5parse.c"
  2407   2424     fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
  2408   2425           break;
  2409   2426         case 18: /* nearphrases ::= phrase */
  2410   2427   #line 153 "fts5parse.y"
  2411   2428   { 
  2412   2429     fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); 
  2413   2430   }
  2414         -#line 1015 "fts5parse.c"
         2431  +#line 1032 "fts5parse.c"
  2415   2432     fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
  2416   2433           break;
  2417   2434         case 19: /* nearphrases ::= nearphrases phrase */
  2418   2435   #line 156 "fts5parse.y"
  2419   2436   {
  2420   2437     fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
  2421   2438   }
  2422         -#line 1023 "fts5parse.c"
         2439  +#line 1040 "fts5parse.c"
  2423   2440     fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
  2424   2441           break;
  2425   2442         case 20: /* neardist_opt ::= */
  2426   2443   #line 163 "fts5parse.y"
  2427   2444   { fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
  2428         -#line 1029 "fts5parse.c"
         2445  +#line 1046 "fts5parse.c"
  2429   2446           break;
  2430   2447         case 21: /* neardist_opt ::= COMMA STRING */
  2431   2448   #line 164 "fts5parse.y"
  2432   2449   { fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
  2433         -#line 1034 "fts5parse.c"
         2450  +#line 1051 "fts5parse.c"
  2434   2451           break;
  2435   2452         case 22: /* phrase ::= phrase PLUS STRING star_opt */
  2436   2453   #line 176 "fts5parse.y"
  2437   2454   { 
  2438   2455     fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
  2439   2456   }
  2440         -#line 1041 "fts5parse.c"
         2457  +#line 1058 "fts5parse.c"
  2441   2458     fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
  2442   2459           break;
  2443   2460         case 23: /* phrase ::= STRING star_opt */
  2444   2461   #line 179 "fts5parse.y"
  2445   2462   { 
  2446   2463     fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
  2447   2464   }
  2448         -#line 1049 "fts5parse.c"
         2465  +#line 1066 "fts5parse.c"
  2449   2466     fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
  2450   2467           break;
  2451   2468         case 24: /* star_opt ::= STAR */
  2452   2469   #line 188 "fts5parse.y"
  2453   2470   { fts5yymsp[0].minor.fts5yy4 = 1; }
  2454         -#line 1055 "fts5parse.c"
         2471  +#line 1072 "fts5parse.c"
  2455   2472           break;
  2456   2473         case 25: /* star_opt ::= */
  2457   2474   #line 189 "fts5parse.y"
  2458   2475   { fts5yymsp[1].minor.fts5yy4 = 0; }
  2459         -#line 1060 "fts5parse.c"
         2476  +#line 1077 "fts5parse.c"
  2460   2477           break;
  2461   2478         default:
  2462   2479           break;
  2463   2480   /********** End reduce actions ************************************************/
  2464   2481     };
  2465   2482     assert( fts5yyruleno<sizeof(fts5yyRuleInfo)/sizeof(fts5yyRuleInfo[0]) );
  2466   2483     fts5yygoto = fts5yyRuleInfo[fts5yyruleno].lhs;
................................................................................
  2517   2534   /************ Begin %syntax_error code ****************************************/
  2518   2535   #line 30 "fts5parse.y"
  2519   2536   
  2520   2537     UNUSED_PARAM(fts5yymajor); /* Silence a compiler warning */
  2521   2538     sqlite3Fts5ParseError(
  2522   2539       pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
  2523   2540     );
  2524         -#line 1125 "fts5parse.c"
         2541  +#line 1142 "fts5parse.c"
  2525   2542   /************ End %syntax_error code ******************************************/
  2526   2543     sqlite3Fts5ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
  2527   2544   }
  2528   2545   
  2529   2546   /*
  2530   2547   ** The following is executed when the parser accepts
  2531   2548   */
................................................................................
  6388   6405     char *z = 0;
  6389   6406   
  6390   6407     memset(&sCtx, 0, sizeof(TokenCtx));
  6391   6408     sCtx.pPhrase = pAppend;
  6392   6409   
  6393   6410     rc = fts5ParseStringFromToken(pToken, &z);
  6394   6411     if( rc==SQLITE_OK ){
  6395         -    int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_QUERY : 0);
         6412  +    int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
  6396   6413       int n;
  6397   6414       sqlite3Fts5Dequote(z);
  6398   6415       n = (int)strlen(z);
  6399   6416       rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
  6400   6417     }
  6401   6418     sqlite3_free(z);
  6402   6419     if( rc || (rc = sCtx.rc) ){
................................................................................
 17132  17149   static void fts5SourceIdFunc(
 17133  17150     sqlite3_context *pCtx,          /* Function call context */
 17134  17151     int nArg,                       /* Number of args */
 17135  17152     sqlite3_value **apUnused        /* Function arguments */
 17136  17153   ){
 17137  17154     assert( nArg==0 );
 17138  17155     UNUSED_PARAM2(nArg, apUnused);
 17139         -  sqlite3_result_text(pCtx, "fts5: 2017-01-19 18:20:36 ffd559afd32dcdce9c733ebccdee88fda9b689cf", -1, SQLITE_TRANSIENT);
        17156  +  sqlite3_result_text(pCtx, "fts5: 2017-02-09 17:12:22 798fb9d70d2e5f95e64237b04d6692360133381a", -1, SQLITE_TRANSIENT);
 17140  17157   }
 17141  17158   
 17142  17159   static int fts5Init(sqlite3 *db){
 17143  17160     static const sqlite3_module fts5Mod = {
 17144  17161       /* iVersion      */ 2,
 17145  17162       /* xCreate       */ fts5CreateMethod,
 17146  17163       /* xConnect      */ fts5ConnectMethod,

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

    18     18   ** For the time being, all JSON is stored as pure text.  (We might add
    19     19   ** a JSONB type in the future which stores a binary encoding of JSON in
    20     20   ** a BLOB, but there is no support for JSONB in the current implementation.
    21     21   ** This implementation parses JSON text at 250 MB/s, so it is hard to see
    22     22   ** how JSONB might improve on that.)
    23     23   */
    24     24   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
    25         -#if !defined(_SQLITEINT_H_)
           25  +#if !defined(SQLITEINT_H)
    26     26   #include "sqlite3ext.h"
    27     27   #endif
    28     28   SQLITE_EXTENSION_INIT1
    29     29   #include <assert.h>
    30     30   #include <string.h>
    31     31   #include <stdlib.h>
    32     32   #include <stdarg.h>