SQLite Android Bindings
Check-in [46756eb243]
Not logged in

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

Overview
Comment:Upgrade this project to version 3.17.0.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:46756eb2432b96f9ef22ecf60be9cd3999b16eed
User & Date: dan 2017-02-13 16:27:26
Context
2017-03-30
14:34
Update this project to SQLite version 3.18.0. check-in: dc68ae8e82 user: dan tags: trunk
2017-02-13
16:27
Upgrade this project to version 3.17.0. check-in: 46756eb243 user: dan tags: trunk
2017-01-03
18:38
Update this project to sqlite 3.16.1. check-in: b3ab16ec53 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.16.1.  By combining all the individual C code files into this
            3  +** version 3.17.0.  By combining all the individual C code files into this
     4      4   ** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
   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
          217  +#endif
          218  +#if defined(__clang__) && !defined(_WIN32) && !defined(SQLITE_DISABLE_INTRINSIC)
          219  +# define CLANG_VERSION \
          220  +            (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
          221  +#else
          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
   212    228   #endif
   213    229   
   214    230   /* Needed for various definitions... */
   215    231   #if defined(__GNUC__) && !defined(_GNU_SOURCE)
   216    232   # define _GNU_SOURCE
   217    233   #endif
   218    234   
................................................................................
   377    393   ** string contains the date and time of the check-in (UTC) and an SHA1
   378    394   ** hash of the entire source tree.
   379    395   **
   380    396   ** See also: [sqlite3_libversion()],
   381    397   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   382    398   ** [sqlite_version()] and [sqlite_source_id()].
   383    399   */
   384         -#define SQLITE_VERSION        "3.16.1"
   385         -#define SQLITE_VERSION_NUMBER 3016001
   386         -#define SQLITE_SOURCE_ID      "2017-01-03 18:27:03 979f04392853b8053817a3eea2fc679947b437fd"
          400  +#define SQLITE_VERSION        "3.17.0"
          401  +#define SQLITE_VERSION_NUMBER 3017000
          402  +#define SQLITE_SOURCE_ID      "2017-02-13 16:02:40 ada05cfa86ad7f5645450ac7a2a21c9aa6e57d2c"
   387    403   
   388    404   /*
   389    405   ** CAPI3REF: Run-Time Library Version Numbers
   390    406   ** KEYWORDS: sqlite3_version sqlite3_sourceid
   391    407   **
   392    408   ** These interfaces provide the same information as the [SQLITE_VERSION],
   393    409   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
   515    531   ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
   516    532   ** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
   517    533   ** sqlite3_uint64 and sqlite_uint64 types can store integer values 
   518    534   ** between 0 and +18446744073709551615 inclusive.
   519    535   */
   520    536   #ifdef SQLITE_INT64_TYPE
   521    537     typedef SQLITE_INT64_TYPE sqlite_int64;
   522         -  typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
          538  +# ifdef SQLITE_UINT64_TYPE
          539  +    typedef SQLITE_UINT64_TYPE sqlite_uint64;
          540  +# else  
          541  +    typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
          542  +# endif
   523    543   #elif defined(_MSC_VER) || defined(__BORLANDC__)
   524    544     typedef __int64 sqlite_int64;
   525    545     typedef unsigned __int64 sqlite_uint64;
   526    546   #else
   527    547     typedef long long int sqlite_int64;
   528    548     typedef unsigned long long int sqlite_uint64;
   529    549   #endif
................................................................................
   828    848   ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
   829    849   ** information is written to disk in the same order as calls
   830    850   ** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
   831    851   ** after reboot following a crash or power loss, the only bytes in a
   832    852   ** file that were written at the application level might have changed
   833    853   ** and that adjacent bytes, even bytes within the same sector are
   834    854   ** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
   835         -** flag indicate that a file cannot be deleted when open.  The
          855  +** flag indicates that a file cannot be deleted when open.  The
   836    856   ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
   837    857   ** read-only media and cannot be changed even by processes with
   838    858   ** elevated privileges.
   839    859   */
   840    860   #define SQLITE_IOCAP_ATOMIC                 0x00000001
   841    861   #define SQLITE_IOCAP_ATOMIC512              0x00000002
   842    862   #define SQLITE_IOCAP_ATOMIC1K               0x00000004
................................................................................
   978    998   ** <li> [SQLITE_IOCAP_ATOMIC4K]
   979    999   ** <li> [SQLITE_IOCAP_ATOMIC8K]
   980   1000   ** <li> [SQLITE_IOCAP_ATOMIC16K]
   981   1001   ** <li> [SQLITE_IOCAP_ATOMIC32K]
   982   1002   ** <li> [SQLITE_IOCAP_ATOMIC64K]
   983   1003   ** <li> [SQLITE_IOCAP_SAFE_APPEND]
   984   1004   ** <li> [SQLITE_IOCAP_SEQUENTIAL]
         1005  +** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
         1006  +** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
         1007  +** <li> [SQLITE_IOCAP_IMMUTABLE]
   985   1008   ** </ul>
   986   1009   **
   987   1010   ** The SQLITE_IOCAP_ATOMIC property means that all writes of
   988   1011   ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
   989   1012   ** mean that writes of blocks that are nnn bytes in size and
   990   1013   ** are aligned to an address which is an integer multiple of
   991   1014   ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
................................................................................
  4152   4175   SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
  4153   4176   
  4154   4177   /*
  4155   4178   ** CAPI3REF: Number Of Columns In A Result Set
  4156   4179   ** METHOD: sqlite3_stmt
  4157   4180   **
  4158   4181   ** ^Return the number of columns in the result set returned by the
  4159         -** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
  4160         -** statement that does not return data (for example an [UPDATE]).
         4182  +** [prepared statement]. ^If this routine returns 0, that means the 
         4183  +** [prepared statement] returns no data (for example an [UPDATE]).
         4184  +** ^However, just because this routine returns a positive number does not
         4185  +** mean that one or more rows of data will be returned.  ^A SELECT statement
         4186  +** will always have a positive sqlite3_column_count() but depending on the
         4187  +** WHERE clause constraints and the table content, it might return no rows.
  4161   4188   **
  4162   4189   ** See also: [sqlite3_data_count()]
  4163   4190   */
  4164   4191   SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
  4165   4192   
  4166   4193   /*
  4167   4194   ** CAPI3REF: Column Names In A Result Set
................................................................................
  5662   5689   ** ^In the case of an update, this is the [rowid] after the update takes place.
  5663   5690   **
  5664   5691   ** ^(The update hook is not invoked when internal system tables are
  5665   5692   ** modified (i.e. sqlite_master and sqlite_sequence).)^
  5666   5693   ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
  5667   5694   **
  5668   5695   ** ^In the current implementation, the update hook
  5669         -** is not invoked when duplication rows are deleted because of an
         5696  +** is not invoked when conflicting rows are deleted because of an
  5670   5697   ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
  5671   5698   ** invoked when rows are deleted using the [truncate optimization].
  5672   5699   ** The exceptions defined in this paragraph might change in a future
  5673   5700   ** release of SQLite.
  5674   5701   **
  5675   5702   ** The update hook implementation must not do anything that will modify
  5676   5703   ** the database connection that invoked the update hook.  Any actions
................................................................................
  6444   6471   **         being opened for read/write access)^.
  6445   6472   ** </ul>
  6446   6473   **
  6447   6474   ** ^Unless it returns SQLITE_MISUSE, this function sets the 
  6448   6475   ** [database connection] error code and message accessible via 
  6449   6476   ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. 
  6450   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.
  6451   6484   **
  6452   6485   ** ^(If the row that a BLOB handle points to is modified by an
  6453   6486   ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
  6454   6487   ** then the BLOB handle is marked as "expired".
  6455   6488   ** This is true if any column of the row is changed, even a column
  6456   6489   ** other than the one the BLOB handle is open on.)^
  6457   6490   ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
................................................................................
  6467   6500   **
  6468   6501   ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
  6469   6502   ** and the built-in [zeroblob] SQL function may be used to create a 
  6470   6503   ** zero-filled blob to read or write using the incremental-blob interface.
  6471   6504   **
  6472   6505   ** To avoid a resource leak, every open [BLOB handle] should eventually
  6473   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()].
  6474   6511   */
  6475   6512   SQLITE_API int sqlite3_blob_open(
  6476   6513     sqlite3*,
  6477   6514     const char *zDb,
  6478   6515     const char *zTable,
  6479   6516     const char *zColumn,
  6480   6517     sqlite3_int64 iRow,
................................................................................
  6482   6519     sqlite3_blob **ppBlob
  6483   6520   );
  6484   6521   
  6485   6522   /*
  6486   6523   ** CAPI3REF: Move a BLOB Handle to a New Row
  6487   6524   ** METHOD: sqlite3_blob
  6488   6525   **
  6489         -** ^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
  6490   6527   ** to a different row of the same database table. ^The new row is identified
  6491   6528   ** by the rowid value passed as the second argument. Only the row can be
  6492   6529   ** changed. ^The database, table and column on which the blob handle is open
  6493         -** 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
  6494   6531   ** faster than closing the existing handle and opening a new one.
  6495   6532   **
  6496   6533   ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
  6497   6534   ** it must exist and there must be either a blob or text value stored in
  6498   6535   ** the nominated column.)^ ^If the new row is not present in the table, or if
  6499   6536   ** it does not contain a blob or text value, or if another error occurs, an
  6500   6537   ** SQLite error code is returned and the blob handle is considered aborted.
................................................................................
  8415   8452   ** CAPI3REF: The pre-update hook.
  8416   8453   **
  8417   8454   ** ^These interfaces are only available if SQLite is compiled using the
  8418   8455   ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
  8419   8456   **
  8420   8457   ** ^The [sqlite3_preupdate_hook()] interface registers a callback function
  8421   8458   ** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
  8422         -** on a [rowid table].
         8459  +** on a database table.
  8423   8460   ** ^At most one preupdate hook may be registered at a time on a single
  8424   8461   ** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
  8425   8462   ** the previous setting.
  8426   8463   ** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
  8427   8464   ** with a NULL pointer as the second parameter.
  8428   8465   ** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
  8429   8466   ** the first parameter to callbacks.
  8430   8467   **
  8431         -** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate
  8432         -** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID]
  8433         -** 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.
  8434   8471   **
  8435   8472   ** ^The second parameter to the preupdate callback is a pointer to
  8436   8473   ** the [database connection] that registered the preupdate hook.
  8437   8474   ** ^The third parameter to the preupdate callback is one of the constants
  8438   8475   ** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
  8439   8476   ** kind of update operation that is about to occur.
  8440   8477   ** ^(The fourth parameter to the preupdate callback is the name of the
  8441   8478   ** database within the database connection that is being modified.  This
  8442   8479   ** will be "main" for the main database or "temp" for TEMP tables or 
  8443   8480   ** the name given after the AS keyword in the [ATTACH] statement for attached
  8444   8481   ** databases.)^
  8445   8482   ** ^The fifth parameter to the preupdate callback is the name of the
  8446   8483   ** table that is being modified.
  8447         -** ^The sixth parameter to the preupdate callback is the initial [rowid] of the
  8448         -** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is
  8449         -** undefined for SQLITE_INSERT changes.
  8450         -** ^The seventh parameter to the preupdate callback is the final [rowid] of
  8451         -** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is
  8452         -** 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.
  8453   8494   **
  8454   8495   ** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
  8455   8496   ** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
  8456   8497   ** provide additional information about a preupdate event. These routines
  8457   8498   ** may only be called from within a preupdate callback.  Invoking any of
  8458   8499   ** these routines from outside of a preupdate callback or with a
  8459   8500   ** [database connection] pointer that is different from the one supplied
................................................................................
  8881   8922   ** either of these things are undefined.
  8882   8923   **
  8883   8924   ** The session object will be used to create changesets for tables in
  8884   8925   ** database zDb, where zDb is either "main", or "temp", or the name of an
  8885   8926   ** attached database. It is not an error if database zDb is not attached
  8886   8927   ** to the database when the session object is created.
  8887   8928   */
  8888         -int sqlite3session_create(
         8929  +SQLITE_API int sqlite3session_create(
  8889   8930     sqlite3 *db,                    /* Database handle */
  8890   8931     const char *zDb,                /* Name of db (e.g. "main") */
  8891   8932     sqlite3_session **ppSession     /* OUT: New session object */
  8892   8933   );
  8893   8934   
  8894   8935   /*
  8895   8936   ** CAPI3REF: Delete A Session Object
................................................................................
  8899   8940   ** results of attempting to use pSession with any other session module
  8900   8941   ** function are undefined.
  8901   8942   **
  8902   8943   ** Session objects must be deleted before the database handle to which they
  8903   8944   ** are attached is closed. Refer to the documentation for 
  8904   8945   ** [sqlite3session_create()] for details.
  8905   8946   */
  8906         -void sqlite3session_delete(sqlite3_session *pSession);
         8947  +SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
  8907   8948   
  8908   8949   
  8909   8950   /*
  8910   8951   ** CAPI3REF: Enable Or Disable A Session Object
  8911   8952   **
  8912   8953   ** Enable or disable the recording of changes by a session object. When
  8913   8954   ** enabled, a session object records changes made to the database. When
................................................................................
  8919   8960   ** Passing zero to this function disables the session. Passing a value
  8920   8961   ** greater than zero enables it. Passing a value less than zero is a 
  8921   8962   ** no-op, and may be used to query the current state of the session.
  8922   8963   **
  8923   8964   ** The return value indicates the final state of the session object: 0 if 
  8924   8965   ** the session is disabled, or 1 if it is enabled.
  8925   8966   */
  8926         -int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
         8967  +SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
  8927   8968   
  8928   8969   /*
  8929   8970   ** CAPI3REF: Set Or Clear the Indirect Change Flag
  8930   8971   **
  8931   8972   ** Each change recorded by a session object is marked as either direct or
  8932   8973   ** indirect. A change is marked as indirect if either:
  8933   8974   **
................................................................................
  8948   8989   ** is set. Passing a value less than zero does not modify the current value
  8949   8990   ** of the indirect flag, and may be used to query the current state of the 
  8950   8991   ** indirect flag for the specified session object.
  8951   8992   **
  8952   8993   ** The return value indicates the final state of the indirect flag: 0 if 
  8953   8994   ** it is clear, or 1 if it is set.
  8954   8995   */
  8955         -int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
         8996  +SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
  8956   8997   
  8957   8998   /*
  8958   8999   ** CAPI3REF: Attach A Table To A Session Object
  8959   9000   **
  8960   9001   ** If argument zTab is not NULL, then it is the name of a table to attach
  8961   9002   ** to the session object passed as the first argument. All subsequent changes 
  8962   9003   ** made to the table while the session object is enabled will be recorded. See 
................................................................................
  8978   9019   **
  8979   9020   ** Changes are not recorded for individual rows that have NULL values stored
  8980   9021   ** in one or more of their PRIMARY KEY columns.
  8981   9022   **
  8982   9023   ** SQLITE_OK is returned if the call completes without error. Or, if an error 
  8983   9024   ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
  8984   9025   */
  8985         -int sqlite3session_attach(
         9026  +SQLITE_API int sqlite3session_attach(
  8986   9027     sqlite3_session *pSession,      /* Session object */
  8987   9028     const char *zTab                /* Table name */
  8988   9029   );
  8989   9030   
  8990   9031   /*
  8991   9032   ** CAPI3REF: Set a table filter on a Session Object.
  8992   9033   **
  8993   9034   ** The second argument (xFilter) is the "filter callback". For changes to rows 
  8994   9035   ** in tables that are not attached to the Session object, the filter is called
  8995   9036   ** to determine whether changes to the table's rows should be tracked or not. 
  8996   9037   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
  8997   9038   ** attached, xFilter will not be called again.
  8998   9039   */
  8999         -void sqlite3session_table_filter(
         9040  +SQLITE_API void sqlite3session_table_filter(
  9000   9041     sqlite3_session *pSession,      /* Session object */
  9001   9042     int(*xFilter)(
  9002   9043       void *pCtx,                   /* Copy of third arg to _filter_table() */
  9003   9044       const char *zTab              /* Table name */
  9004   9045     ),
  9005   9046     void *pCtx                      /* First argument passed to xFilter */
  9006   9047   );
................................................................................
  9105   9146   ** is inserted while a session object is enabled, then later deleted while 
  9106   9147   ** the same session object is disabled, no INSERT record will appear in the
  9107   9148   ** changeset, even though the delete took place while the session was disabled.
  9108   9149   ** Or, if one field of a row is updated while a session is disabled, and 
  9109   9150   ** another field of the same row is updated while the session is enabled, the
  9110   9151   ** resulting changeset will contain an UPDATE change that updates both fields.
  9111   9152   */
  9112         -int sqlite3session_changeset(
         9153  +SQLITE_API int sqlite3session_changeset(
  9113   9154     sqlite3_session *pSession,      /* Session object */
  9114   9155     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
  9115   9156     void **ppChangeset              /* OUT: Buffer containing changeset */
  9116   9157   );
  9117   9158   
  9118   9159   /*
  9119   9160   ** CAPI3REF: Load The Difference Between Tables Into A Session 
................................................................................
  9149   9190   **   <li> For each row (primary key) that exists in the to-table but not in 
  9150   9191   **     the from-table, an INSERT record is added to the session object.
  9151   9192   **
  9152   9193   **   <li> For each row (primary key) that exists in the to-table but not in 
  9153   9194   **     the from-table, a DELETE record is added to the session object.
  9154   9195   **
  9155   9196   **   <li> For each row (primary key) that exists in both tables, but features 
  9156         -**     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.  
  9157   9199   ** </ul>
  9158   9200   **
  9159   9201   ** To clarify, if this function is called and then a changeset constructed
  9160   9202   ** using [sqlite3session_changeset()], then after applying that changeset to 
  9161   9203   ** database zFrom the contents of the two compatible tables would be 
  9162   9204   ** identical.
  9163   9205   **
................................................................................
  9166   9208   **
  9167   9209   ** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
  9168   9210   ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
  9169   9211   ** may be set to point to a buffer containing an English language error 
  9170   9212   ** message. It is the responsibility of the caller to free this buffer using
  9171   9213   ** sqlite3_free().
  9172   9214   */
  9173         -int sqlite3session_diff(
         9215  +SQLITE_API int sqlite3session_diff(
  9174   9216     sqlite3_session *pSession,
  9175   9217     const char *zFromDb,
  9176   9218     const char *zTbl,
  9177   9219     char **pzErrMsg
  9178   9220   );
  9179   9221   
  9180   9222   
................................................................................
  9202   9244   ** in the same way as for changesets.
  9203   9245   **
  9204   9246   ** Changes within a patchset are ordered in the same way as for changesets
  9205   9247   ** generated by the sqlite3session_changeset() function (i.e. all changes for
  9206   9248   ** a single table are grouped together, tables appear in the order in which
  9207   9249   ** they were attached to the session object).
  9208   9250   */
  9209         -int sqlite3session_patchset(
         9251  +SQLITE_API int sqlite3session_patchset(
  9210   9252     sqlite3_session *pSession,      /* Session object */
  9211   9253     int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
  9212   9254     void **ppPatchset               /* OUT: Buffer containing changeset */
  9213   9255   );
  9214   9256   
  9215   9257   /*
  9216   9258   ** CAPI3REF: Test if a changeset has recorded any changes.
................................................................................
  9223   9265   ** [sqlite3session_changeset()] on the session handle may still return a
  9224   9266   ** changeset that contains no changes. This can happen when a row in 
  9225   9267   ** an attached table is modified and then later on the original values 
  9226   9268   ** are restored. However, if this function returns non-zero, then it is
  9227   9269   ** guaranteed that a call to sqlite3session_changeset() will return a 
  9228   9270   ** changeset containing zero changes.
  9229   9271   */
  9230         -int sqlite3session_isempty(sqlite3_session *pSession);
         9272  +SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
  9231   9273   
  9232   9274   /*
  9233   9275   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
  9234   9276   **
  9235   9277   ** Create an iterator used to iterate through the contents of a changeset.
  9236   9278   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
  9237   9279   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
................................................................................
  9258   9300   ** that apply to a single table are grouped together. This means that when 
  9259   9301   ** an application iterates through a changeset using an iterator created by 
  9260   9302   ** this function, all changes that relate to a single table are visited 
  9261   9303   ** consecutively. There is no chance that the iterator will visit a change 
  9262   9304   ** the applies to table X, then one for table Y, and then later on visit 
  9263   9305   ** another change for table X.
  9264   9306   */
  9265         -int sqlite3changeset_start(
         9307  +SQLITE_API int sqlite3changeset_start(
  9266   9308     sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
  9267   9309     int nChangeset,                 /* Size of changeset blob in bytes */
  9268   9310     void *pChangeset                /* Pointer to blob containing changeset */
  9269   9311   );
  9270   9312   
  9271   9313   
  9272   9314   /*
................................................................................
  9287   9329   ** Otherwise, if all changes in the changeset have already been visited,
  9288   9330   ** SQLITE_DONE is returned.
  9289   9331   **
  9290   9332   ** If an error occurs, an SQLite error code is returned. Possible error 
  9291   9333   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
  9292   9334   ** SQLITE_NOMEM.
  9293   9335   */
  9294         -int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
         9336  +SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
  9295   9337   
  9296   9338   /*
  9297   9339   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
  9298   9340   **
  9299   9341   ** The pIter argument passed to this function may either be an iterator
  9300   9342   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9301   9343   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
................................................................................
  9315   9357   ** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the 
  9316   9358   ** type of change that the iterator currently points to.
  9317   9359   **
  9318   9360   ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
  9319   9361   ** SQLite error code is returned. The values of the output variables may not
  9320   9362   ** be trusted in this case.
  9321   9363   */
  9322         -int sqlite3changeset_op(
         9364  +SQLITE_API int sqlite3changeset_op(
  9323   9365     sqlite3_changeset_iter *pIter,  /* Iterator object */
  9324   9366     const char **pzTab,             /* OUT: Pointer to table name */
  9325   9367     int *pnCol,                     /* OUT: Number of columns in table */
  9326   9368     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
  9327   9369     int *pbIndirect                 /* OUT: True for an 'indirect' change */
  9328   9370   );
  9329   9371   
................................................................................
  9348   9390   ** in the table.
  9349   9391   **
  9350   9392   ** If this function is called when the iterator does not point to a valid
  9351   9393   ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
  9352   9394   ** SQLITE_OK is returned and the output variables populated as described
  9353   9395   ** above.
  9354   9396   */
  9355         -int sqlite3changeset_pk(
         9397  +SQLITE_API int sqlite3changeset_pk(
  9356   9398     sqlite3_changeset_iter *pIter,  /* Iterator object */
  9357   9399     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
  9358   9400     int *pnCol                      /* OUT: Number of entries in output array */
  9359   9401   );
  9360   9402   
  9361   9403   /*
  9362   9404   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
................................................................................
  9378   9420   ** original row values stored as part of the UPDATE or DELETE change and
  9379   9421   ** returns SQLITE_OK. The name of the function comes from the fact that this 
  9380   9422   ** is similar to the "old.*" columns available to update or delete triggers.
  9381   9423   **
  9382   9424   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9383   9425   ** is returned and *ppValue is set to NULL.
  9384   9426   */
  9385         -int sqlite3changeset_old(
         9427  +SQLITE_API int sqlite3changeset_old(
  9386   9428     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9387   9429     int iVal,                       /* Column number */
  9388   9430     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
  9389   9431   );
  9390   9432   
  9391   9433   /*
  9392   9434   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
................................................................................
  9411   9453   ** SQLITE_OK returned. The name of the function comes from the fact that 
  9412   9454   ** this is similar to the "new.*" columns available to update or delete 
  9413   9455   ** triggers.
  9414   9456   **
  9415   9457   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9416   9458   ** is returned and *ppValue is set to NULL.
  9417   9459   */
  9418         -int sqlite3changeset_new(
         9460  +SQLITE_API int sqlite3changeset_new(
  9419   9461     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9420   9462     int iVal,                       /* Column number */
  9421   9463     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
  9422   9464   );
  9423   9465   
  9424   9466   /*
  9425   9467   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
................................................................................
  9438   9480   ** sqlite3_value object containing the iVal'th value from the 
  9439   9481   ** "conflicting row" associated with the current conflict-handler callback
  9440   9482   ** and returns SQLITE_OK.
  9441   9483   **
  9442   9484   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9443   9485   ** is returned and *ppValue is set to NULL.
  9444   9486   */
  9445         -int sqlite3changeset_conflict(
         9487  +SQLITE_API int sqlite3changeset_conflict(
  9446   9488     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9447   9489     int iVal,                       /* Column number */
  9448   9490     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
  9449   9491   );
  9450   9492   
  9451   9493   /*
  9452   9494   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
................................................................................
  9454   9496   ** This function may only be called with an iterator passed to an
  9455   9497   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
  9456   9498   ** it sets the output variable to the total number of known foreign key
  9457   9499   ** violations in the destination database and returns SQLITE_OK.
  9458   9500   **
  9459   9501   ** In all other cases this function returns SQLITE_MISUSE.
  9460   9502   */
  9461         -int sqlite3changeset_fk_conflicts(
         9503  +SQLITE_API int sqlite3changeset_fk_conflicts(
  9462   9504     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9463   9505     int *pnOut                      /* OUT: Number of FK violations */
  9464   9506   );
  9465   9507   
  9466   9508   
  9467   9509   /*
  9468   9510   ** CAPI3REF: Finalize A Changeset Iterator
................................................................................
  9487   9529   **     // Do something with change.
  9488   9530   **   }
  9489   9531   **   rc = sqlite3changeset_finalize();
  9490   9532   **   if( rc!=SQLITE_OK ){
  9491   9533   **     // An error has occurred 
  9492   9534   **   }
  9493   9535   */
  9494         -int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
         9536  +SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
  9495   9537   
  9496   9538   /*
  9497   9539   ** CAPI3REF: Invert A Changeset
  9498   9540   **
  9499   9541   ** This function is used to "invert" a changeset object. Applying an inverted
  9500   9542   ** changeset to a database reverses the effects of applying the uninverted
  9501   9543   ** changeset. Specifically:
................................................................................
  9517   9559   ** It is the responsibility of the caller to eventually call sqlite3_free()
  9518   9560   ** on the *ppOut pointer to free the buffer allocation following a successful 
  9519   9561   ** call to this function.
  9520   9562   **
  9521   9563   ** WARNING/TODO: This function currently assumes that the input is a valid
  9522   9564   ** changeset. If it is not, the results are undefined.
  9523   9565   */
  9524         -int sqlite3changeset_invert(
         9566  +SQLITE_API int sqlite3changeset_invert(
  9525   9567     int nIn, const void *pIn,       /* Input changeset */
  9526   9568     int *pnOut, void **ppOut        /* OUT: Inverse of input */
  9527   9569   );
  9528   9570   
  9529   9571   /*
  9530   9572   ** CAPI3REF: Concatenate Two Changeset Objects
  9531   9573   **
................................................................................
  9546   9588   **   }else{
  9547   9589   **     *ppOut = 0;
  9548   9590   **     *pnOut = 0;
  9549   9591   **   }
  9550   9592   **
  9551   9593   ** Refer to the sqlite3_changegroup documentation below for details.
  9552   9594   */
  9553         -int sqlite3changeset_concat(
         9595  +SQLITE_API int sqlite3changeset_concat(
  9554   9596     int nA,                         /* Number of bytes in buffer pA */
  9555   9597     void *pA,                       /* Pointer to buffer containing changeset A */
  9556   9598     int nB,                         /* Number of bytes in buffer pB */
  9557   9599     void *pB,                       /* Pointer to buffer containing changeset B */
  9558   9600     int *pnOut,                     /* OUT: Number of bytes in output changeset */
  9559   9601     void **ppOut                    /* OUT: Buffer containing output changeset */
  9560   9602   );
................................................................................
  9734   9776   ** For each table that is not excluded by the filter callback, this function 
  9735   9777   ** tests that the target database contains a compatible table. A table is 
  9736   9778   ** considered compatible if all of the following are true:
  9737   9779   **
  9738   9780   ** <ul>
  9739   9781   **   <li> The table has the same name as the name recorded in the 
  9740   9782   **        changeset, and
  9741         -**   <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 
  9742   9784   **        changeset, and
  9743   9785   **   <li> The table has primary key columns in the same position as 
  9744   9786   **        recorded in the changeset.
  9745   9787   ** </ul>
  9746   9788   **
  9747   9789   ** If there is no compatible table, it is not an error, but none of the
  9748   9790   ** changes associated with the table are applied. A warning message is issued
................................................................................
  9779   9821   **   original row values stored in the changeset. If it does, and the values 
  9780   9822   **   stored in all non-primary key columns also match the values stored in 
  9781   9823   **   the changeset the row is deleted from the target database.
  9782   9824   **
  9783   9825   **   If a row with matching primary key values is found, but one or more of
  9784   9826   **   the non-primary key fields contains a value different from the original
  9785   9827   **   row value stored in the changeset, the conflict-handler function is
  9786         -**   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.
  9787   9833   **
  9788   9834   **   If no row with matching primary key values is found in the database,
  9789   9835   **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  9790   9836   **   passed as the second argument.
  9791   9837   **
  9792   9838   **   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
  9793   9839   **   (which can only happen if a foreign key constraint is violated), the
................................................................................
  9794   9840   **   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
  9795   9841   **   passed as the second argument. This includes the case where the DELETE
  9796   9842   **   operation is attempted because an earlier call to the conflict handler
  9797   9843   **   function returned [SQLITE_CHANGESET_REPLACE].
  9798   9844   **
  9799   9845   ** <dt>INSERT Changes<dd>
  9800   9846   **   For each INSERT change, an attempt is made to insert the new row into
  9801         -**   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.
  9802   9850   **
  9803   9851   **   If the attempt to insert the row fails because the database already 
  9804   9852   **   contains a row with the same primary key values, the conflict handler
  9805   9853   **   function is invoked with the second argument set to 
  9806   9854   **   [SQLITE_CHANGESET_CONFLICT].
  9807   9855   **
  9808   9856   **   If the attempt to insert the row fails because of some other constraint
................................................................................
  9812   9860   **   an earlier call to the conflict handler function returned 
  9813   9861   **   [SQLITE_CHANGESET_REPLACE].
  9814   9862   **
  9815   9863   ** <dt>UPDATE Changes<dd>
  9816   9864   **   For each UPDATE change, this function checks if the target database 
  9817   9865   **   contains a row with the same primary key value (or values) as the 
  9818   9866   **   original row values stored in the changeset. If it does, and the values 
  9819         -**   stored in all non-primary key columns also match the values stored in 
  9820         -**   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.
  9821   9869   **
  9822   9870   **   If a row with matching primary key values is found, but one or more of
  9823         -**   the non-primary key fields contains a value different from an original
  9824         -**   row value stored in the changeset, the conflict-handler function is
  9825         -**   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
  9826   9874   **   UPDATE changes only contain values for non-primary key fields that are
  9827   9875   **   to be modified, only those fields need to match the original values to
  9828   9876   **   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
  9829   9877   **
  9830   9878   **   If no row with matching primary key values is found in the database,
  9831   9879   **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  9832   9880   **   passed as the second argument.
................................................................................
  9846   9894   **
  9847   9895   ** All changes made by this function are enclosed in a savepoint transaction.
  9848   9896   ** If any other error (aside from a constraint failure when attempting to
  9849   9897   ** write to the target database) occurs, then the savepoint transaction is
  9850   9898   ** rolled back, restoring the target database to its original state, and an 
  9851   9899   ** SQLite error code returned.
  9852   9900   */
  9853         -int sqlite3changeset_apply(
         9901  +SQLITE_API int sqlite3changeset_apply(
  9854   9902     sqlite3 *db,                    /* Apply change to "main" db of this handle */
  9855   9903     int nChangeset,                 /* Size of changeset in bytes */
  9856   9904     void *pChangeset,               /* Changeset blob */
  9857   9905     int(*xFilter)(
  9858   9906       void *pCtx,                   /* Copy of sixth arg to _apply() */
  9859   9907       const char *zTab              /* Table name */
  9860   9908     ),
................................................................................
 10047  10095   ** is immediately abandoned and the streaming API function returns a copy
 10048  10096   ** of the xOutput error code to the application.
 10049  10097   **
 10050  10098   ** The sessions module never invokes an xOutput callback with the third 
 10051  10099   ** parameter set to a value less than or equal to zero. Other than this,
 10052  10100   ** no guarantees are made as to the size of the chunks of data returned.
 10053  10101   */
 10054         -int sqlite3changeset_apply_strm(
        10102  +SQLITE_API int sqlite3changeset_apply_strm(
 10055  10103     sqlite3 *db,                    /* Apply change to "main" db of this handle */
 10056  10104     int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
 10057  10105     void *pIn,                                          /* First arg for xInput */
 10058  10106     int(*xFilter)(
 10059  10107       void *pCtx,                   /* Copy of sixth arg to _apply() */
 10060  10108       const char *zTab              /* Table name */
 10061  10109     ),
................................................................................
 10062  10110     int(*xConflict)(
 10063  10111       void *pCtx,                   /* Copy of sixth arg to _apply() */
 10064  10112       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 10065  10113       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 10066  10114     ),
 10067  10115     void *pCtx                      /* First argument passed to xConflict */
 10068  10116   );
 10069         -int sqlite3changeset_concat_strm(
        10117  +SQLITE_API int sqlite3changeset_concat_strm(
 10070  10118     int (*xInputA)(void *pIn, void *pData, int *pnData),
 10071  10119     void *pInA,
 10072  10120     int (*xInputB)(void *pIn, void *pData, int *pnData),
 10073  10121     void *pInB,
 10074  10122     int (*xOutput)(void *pOut, const void *pData, int nData),
 10075  10123     void *pOut
 10076  10124   );
 10077         -int sqlite3changeset_invert_strm(
        10125  +SQLITE_API int sqlite3changeset_invert_strm(
 10078  10126     int (*xInput)(void *pIn, void *pData, int *pnData),
 10079  10127     void *pIn,
 10080  10128     int (*xOutput)(void *pOut, const void *pData, int nData),
 10081  10129     void *pOut
 10082  10130   );
 10083         -int sqlite3changeset_start_strm(
        10131  +SQLITE_API int sqlite3changeset_start_strm(
 10084  10132     sqlite3_changeset_iter **pp,
 10085  10133     int (*xInput)(void *pIn, void *pData, int *pnData),
 10086  10134     void *pIn
 10087  10135   );
 10088         -int sqlite3session_changeset_strm(
        10136  +SQLITE_API int sqlite3session_changeset_strm(
 10089  10137     sqlite3_session *pSession,
 10090  10138     int (*xOutput)(void *pOut, const void *pData, int nData),
 10091  10139     void *pOut
 10092  10140   );
 10093         -int sqlite3session_patchset_strm(
        10141  +SQLITE_API int sqlite3session_patchset_strm(
 10094  10142     sqlite3_session *pSession,
 10095  10143     int (*xOutput)(void *pOut, const void *pData, int nData),
 10096  10144     void *pOut
 10097  10145   );
 10098  10146   int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
 10099  10147       int (*xInput)(void *pIn, void *pData, int *pnData),
 10100  10148       void *pIn
................................................................................
 10993  11041   */
 10994  11042   #if !defined(SQLITE_DISABLE_INTRINSIC)
 10995  11043   #  if defined(_MSC_VER) && _MSC_VER>=1400
 10996  11044   #    if !defined(_WIN32_WCE)
 10997  11045   #      include <intrin.h>
 10998  11046   #      pragma intrinsic(_byteswap_ushort)
 10999  11047   #      pragma intrinsic(_byteswap_ulong)
        11048  +#      pragma intrinsic(_byteswap_uint64)
 11000  11049   #      pragma intrinsic(_ReadWriteBarrier)
 11001  11050   #    else
 11002  11051   #      include <cmnintrin.h>
 11003  11052   #    endif
 11004  11053   #  endif
 11005  11054   #endif
 11006  11055   
................................................................................
 11531  11580   /************** Continuing where we left off in sqliteInt.h ******************/
 11532  11581   #include <stdio.h>
 11533  11582   #include <stdlib.h>
 11534  11583   #include <string.h>
 11535  11584   #include <assert.h>
 11536  11585   #include <stddef.h>
 11537  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  +
 11538  11599   /*
 11539  11600   ** If compiling for a processor that lacks floating point support,
 11540  11601   ** substitute integer for floating-point
 11541  11602   */
 11542  11603   #ifdef SQLITE_OMIT_FLOATING_POINT
 11543  11604   # define double sqlite_int64
 11544  11605   # define float sqlite_int64
................................................................................
 11615  11676   #endif
 11616  11677   
 11617  11678   /*
 11618  11679   ** The default initial allocation for the pagecache when using separate
 11619  11680   ** pagecaches for each database connection.  A positive number is the
 11620  11681   ** number of pages.  A negative number N translations means that a buffer
 11621  11682   ** of -1024*N bytes is allocated and used for as many pages as it will hold.
        11683  +**
        11684  +** The default value of "20" was choosen to minimize the run-time of the
        11685  +** speedtest1 test program with options: --shrink-memory --reprepare
 11622  11686   */
 11623  11687   #ifndef SQLITE_DEFAULT_PCACHE_INITSZ
 11624         -# define SQLITE_DEFAULT_PCACHE_INITSZ 100
        11688  +# define SQLITE_DEFAULT_PCACHE_INITSZ 20
 11625  11689   #endif
 11626  11690   
 11627  11691   /*
 11628  11692   ** GCC does not define the offsetof() macro so we'll have to do it
 11629  11693   ** ourselves.
 11630  11694   */
 11631  11695   #ifndef offsetof
................................................................................
 11792  11856   
 11793  11857   /*
 11794  11858   ** Macros to determine whether the machine is big or little endian,
 11795  11859   ** and whether or not that determination is run-time or compile-time.
 11796  11860   **
 11797  11861   ** For best performance, an attempt is made to guess at the byte-order
 11798  11862   ** using C-preprocessor macros.  If that is unsuccessful, or if
 11799         -** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
        11863  +** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
 11800  11864   ** at run-time.
 11801  11865   */
 11802         -#if (defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
        11866  +#ifndef SQLITE_BYTEORDER
        11867  +# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
 11803  11868        defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
 11804  11869        defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
 11805         -     defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
 11806         -# define SQLITE_BYTEORDER    1234
 11807         -# define SQLITE_BIGENDIAN    0
 11808         -# define SQLITE_LITTLEENDIAN 1
 11809         -# 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
 11810  11877   #endif
 11811         -#if (defined(sparc)    || defined(__ppc__))  \
 11812         -    && !defined(SQLITE_RUNTIME_BYTEORDER)
 11813         -# define SQLITE_BYTEORDER    4321
        11878  +#if SQLITE_BYTEORDER==4321
 11814  11879   # define SQLITE_BIGENDIAN    1
 11815  11880   # define SQLITE_LITTLEENDIAN 0
 11816  11881   # define SQLITE_UTF16NATIVE  SQLITE_UTF16BE
 11817         -#endif
 11818         -#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
 11819  11887   # ifdef SQLITE_AMALGAMATION
 11820  11888     const int sqlite3one = 1;
 11821  11889   # else
 11822  11890     extern const int sqlite3one;
 11823  11891   # endif
 11824         -# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
 11825  11892   # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
 11826  11893   # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
 11827  11894   # define SQLITE_UTF16NATIVE  (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
 11828  11895   #endif
 11829  11896   
 11830  11897   /*
 11831  11898   ** Constants for the largest and smallest possible 64-bit signed integers.
................................................................................
 12340  12407     int bias,
 12341  12408     int *pRes
 12342  12409   );
 12343  12410   SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
 12344  12411   SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
 12345  12412   SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
 12346  12413   
 12347         -/* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */
        12414  +/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
 12348  12415   #define BTREE_SAVEPOSITION 0x02  /* Leave cursor pointing at NEXT or PREV */
 12349  12416   #define BTREE_AUXDELETE    0x04  /* not the primary delete operation */
        12417  +#define BTREE_APPEND       0x08  /* Insert is likely an append */
 12350  12418   
 12351  12419   /* An instance of the BtreePayload object describes the content of a single
 12352  12420   ** entry in either an index or table btree.
 12353  12421   **
 12354  12422   ** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
 12355  12423   ** an arbitrary key and no data.  These btrees have pKey,nKey set to their
 12356  12424   ** key and pData,nData,nZero set to zero.
................................................................................
 12373  12441     struct Mem *aMem;       /* First of nMem value in the unpacked pKey */
 12374  12442     u16 nMem;               /* Number of aMem[] value.  Might be zero */
 12375  12443     int nData;              /* Size of pData.  0 if none. */
 12376  12444     int nZero;              /* Extra zero data appended after pData,nData */
 12377  12445   };
 12378  12446   
 12379  12447   SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
 12380         -                       int bias, int seekResult);
        12448  +                       int flags, int seekResult);
 12381  12449   SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
 12382  12450   SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
 12383  12451   SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
 12384  12452   SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
 12385  12453   SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
 12386  12454   SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
 12387  12455   SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
................................................................................
 12506  12574   ** A single instruction of the virtual machine has an opcode
 12507  12575   ** and as many as three operands.  The instruction is recorded
 12508  12576   ** as an instance of the following structure:
 12509  12577   */
 12510  12578   struct VdbeOp {
 12511  12579     u8 opcode;          /* What operation to perform */
 12512  12580     signed char p4type; /* One of the P4_xxx constants for p4 */
 12513         -  u8 notUsed1;
 12514         -  u8 p5;              /* Fifth parameter is an unsigned character */
        12581  +  u16 p5;             /* Fifth parameter is an unsigned 16-bit integer */
 12515  12582     int p1;             /* First operand */
 12516  12583     int p2;             /* Second parameter (often the jump destination) */
 12517  12584     int p3;             /* The third parameter */
 12518  12585     union p4union {     /* fourth parameter */
 12519  12586       int i;                 /* Integer value if p4type==P4_INT32 */
 12520  12587       void *p;               /* Generic pointer */
 12521  12588       char *z;               /* Pointer to data for string (char array) types */
................................................................................
 12857  12924   SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
 12858  12925   SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
 12859  12926   SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
 12860  12927   SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
 12861  12928   SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int);
 12862  12929   #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
 12863  12930   SQLITE_PRIVATE   void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
        12931  +SQLITE_PRIVATE   void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
 12864  12932   #else
 12865  12933   # define sqlite3VdbeVerifyNoMallocRequired(A,B)
        12934  +# define sqlite3VdbeVerifyNoResultRow(A)
 12866  12935   #endif
 12867  12936   SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
 12868  12937   SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
 12869  12938   SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
 12870  12939   SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
 12871  12940   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
 12872  12941   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
 12873         -SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
        12942  +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
 12874  12943   SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
 12875  12944   SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
 12876  12945   SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
 12877  12946   SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
 12878  12947   SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
 12879  12948   SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
 12880  12949   SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
................................................................................
 13168  13237   
 13169  13238   #ifndef SQLITE_OMIT_WAL
 13170  13239   SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
 13171  13240   SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
 13172  13241   SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
 13173  13242   SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
 13174  13243   SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
 13175         -SQLITE_PRIVATE   int sqlite3PagerUseWal(Pager *pPager);
        13244  +# ifdef SQLITE_DIRECT_OVERFLOW_READ
        13245  +SQLITE_PRIVATE   int sqlite3PagerUseWal(Pager *pPager, Pgno);
        13246  +# endif
 13176  13247   # ifdef SQLITE_ENABLE_SNAPSHOT
 13177  13248   SQLITE_PRIVATE   int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
 13178  13249   SQLITE_PRIVATE   int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
 13179  13250   SQLITE_PRIVATE   int sqlite3PagerSnapshotRecover(Pager *pPager);
 13180  13251   # endif
 13181  13252   #else
 13182         -# define sqlite3PagerUseWal(x) 0
        13253  +# define sqlite3PagerUseWal(x,y) 0
 13183  13254   #endif
 13184  13255   
 13185  13256   #ifdef SQLITE_ENABLE_ZIPVFS
 13186  13257   SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
 13187  13258   #endif
 13188  13259   
 13189  13260   /* Functions used to query pager state and configuration. */
................................................................................
 13999  14070     u8 bBenignMalloc;             /* Do not require OOMs if true */
 14000  14071     u8 dfltLockMode;              /* Default locking-mode for attached dbs */
 14001  14072     signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
 14002  14073     u8 suppressErr;               /* Do not issue error messages if true */
 14003  14074     u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
 14004  14075     u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
 14005  14076     u8 mTrace;                    /* zero or more SQLITE_TRACE flags */
        14077  +  u8 skipBtreeMutex;            /* True if no shared-cache backends */
 14006  14078     int nextPagesize;             /* Pagesize after VACUUM if >0 */
 14007  14079     u32 magic;                    /* Magic number for detect library misuse */
 14008  14080     int nChange;                  /* Value returned by sqlite3_changes() */
 14009  14081     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
 14010  14082     int aLimit[SQLITE_N_LIMIT];   /* Limits */
 14011  14083     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
 14012  14084     struct sqlite3InitInfo {      /* Information used during initialization */
................................................................................
 14264  14336   #define SQLITE_FUNC_COUNT    0x0100 /* Built-in count(*) aggregate */
 14265  14337   #define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */
 14266  14338   #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
 14267  14339   #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
 14268  14340   #define SQLITE_FUNC_MINMAX   0x1000 /* True for min() and max() aggregates */
 14269  14341   #define SQLITE_FUNC_SLOCHNG  0x2000 /* "Slow Change". Value constant during a
 14270  14342                                       ** single query - might change over time */
        14343  +#define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
 14271  14344   
 14272  14345   /*
 14273  14346   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
 14274  14347   ** used to create the initializers for the FuncDef structures.
 14275  14348   **
 14276  14349   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
 14277  14350   **     Used to create a scalar function definition of a function zName
................................................................................
 15270  15343                                         ** the OR optimization  */
 15271  15344   #define WHERE_GROUPBY          0x0040 /* pOrderBy is really a GROUP BY */
 15272  15345   #define WHERE_DISTINCTBY       0x0080 /* pOrderby is really a DISTINCT clause */
 15273  15346   #define WHERE_WANT_DISTINCT    0x0100 /* All output needs to be distinct */
 15274  15347   #define WHERE_SORTBYGROUP      0x0200 /* Support sqlite3WhereIsSorted() */
 15275  15348   #define WHERE_SEEK_TABLE       0x0400 /* Do not defer seeks on main table */
 15276  15349   #define WHERE_ORDERBY_LIMIT    0x0800 /* ORDERBY+LIMIT on the inner loop */
 15277         -                        /*     0x1000    not currently used */
        15350  +#define WHERE_SEEK_UNIQ_TABLE  0x1000 /* Do not defer seeks if unique */
 15278  15351                           /*     0x2000    not currently used */
 15279  15352   #define WHERE_USE_LIMIT        0x4000 /* Use the LIMIT in cost estimates */
 15280  15353                           /*     0x8000    not currently used */
 15281  15354   
 15282  15355   /* Allowed return values from sqlite3WhereIsDistinct()
 15283  15356   */
 15284  15357   #define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
................................................................................
 15731  15804   **    OPFLAG_FORDELETE    == BTREE_FORDELETE
 15732  15805   **    OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
 15733  15806   **    OPFLAG_AUXDELETE    == BTREE_AUXDELETE
 15734  15807   */
 15735  15808   #define OPFLAG_NCHANGE       0x01    /* OP_Insert: Set to update db->nChange */
 15736  15809                                        /* Also used in P2 (not P5) of OP_Delete */
 15737  15810   #define OPFLAG_EPHEM         0x01    /* OP_Column: Ephemeral output is ok */
 15738         -#define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
        15811  +#define OPFLAG_LASTROWID     0x20    /* Set to update db->lastRowid */
 15739  15812   #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
 15740  15813   #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
 15741  15814   #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
 15742         -#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 15743  15815   #define OPFLAG_ISNOOP        0x40    /* OP_Delete does pre-update-hook only */
 15744         -#endif
 15745  15816   #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
 15746  15817   #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
 15747  15818   #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
 15748  15819   #define OPFLAG_SEEKEQ        0x02    /* OP_Open** cursor uses EQ seek only */
 15749  15820   #define OPFLAG_FORDELETE     0x08    /* OP_Open should use BTREE_FORDELETE */
 15750  15821   #define OPFLAG_P2ISREG       0x10    /* P2 to OP_Open** is a register number */
 15751  15822   #define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
 15752         -#define OPFLAG_SAVEPOSITION  0x02    /* OP_Delete: keep cursor position */
        15823  +#define OPFLAG_SAVEPOSITION  0x02    /* OP_Delete/Insert: save cursor pos */
 15753  15824   #define OPFLAG_AUXDELETE     0x04    /* OP_Delete: index in a DELETE op */
 15754  15825   
 15755  15826   /*
 15756  15827    * Each trigger present in the database schema is stored as an instance of
 15757  15828    * struct Trigger.
 15758  15829    *
 15759  15830    * Pointers to instances of struct Trigger are stored in two ways.
................................................................................
 16406  16477   SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*);
 16407  16478   SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
 16408  16479   SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
 16409  16480   SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
 16410  16481   SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
 16411  16482   SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
 16412  16483   SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
 16413         -SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
        16484  +SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
 16414  16485   SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
 16415  16486   SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
 16416  16487   SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
 16417  16488   SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
 16418  16489   #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
 16419  16490   #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
 16420  16491   #define SQLITE_ECEL_REF      0x04  /* Use ExprList.u.x.iOrderByCol */
................................................................................
 16468  16539   SQLITE_PRIVATE void sqlite3GenerateRowDelete(
 16469  16540       Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
 16470  16541   SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
 16471  16542   SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
 16472  16543   SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
 16473  16544   SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
 16474  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
 16475  16551   SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
 16476  16552   SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
 16477  16553   SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
 16478  16554   SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
 16479  16555   SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
 16480  16556   SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
 16481  16557   SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
................................................................................
 16746  16822   SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
 16747  16823   SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
 16748  16824   #endif
 16749  16825   
 16750  16826   /*
 16751  16827   ** The interface to the LEMON-generated parser
 16752  16828   */
 16753         -SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
 16754         -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
 16755  16833   SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
 16756  16834   #ifdef YYTRACKMAXSTACKDEPTH
 16757  16835   SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
 16758  16836   #endif
 16759  16837   
 16760  16838   SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
 16761  16839   #ifndef SQLITE_OMIT_LOAD_EXTENSION
................................................................................
 16857  16935   SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
 16858  16936   #else
 16859  16937     #define sqlite3FkActions(a,b,c,d,e,f)
 16860  16938     #define sqlite3FkCheck(a,b,c,d,e,f)
 16861  16939     #define sqlite3FkDropTable(a,b,c)
 16862  16940     #define sqlite3FkOldmask(a,b)         0
 16863  16941     #define sqlite3FkRequired(a,b,c,d)    0
        16942  +  #define sqlite3FkReferences(a)        0
 16864  16943   #endif
 16865  16944   #ifndef SQLITE_OMIT_FOREIGN_KEY
 16866  16945   SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
 16867  16946   SQLITE_PRIVATE   int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
 16868  16947   #else
 16869  16948     #define sqlite3FkDelete(a,b)
 16870  16949     #define sqlite3FkLocateIndex(a,b,c,d,e)
................................................................................
 17185  17264   ** memory.  (The statement journal is also always held entirely in memory
 17186  17265   ** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
 17187  17266   ** setting.)
 17188  17267   */
 17189  17268   #ifndef SQLITE_STMTJRNL_SPILL 
 17190  17269   # define SQLITE_STMTJRNL_SPILL (64*1024)
 17191  17270   #endif
        17271  +
        17272  +/*
        17273  +** The default lookaside-configuration, the format "SZ,N".  SZ is the
        17274  +** number of bytes in each lookaside slot (should be a multiple of 8)
        17275  +** and N is the number of slots.  The lookaside-configuration can be
        17276  +** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
        17277  +** or at run-time for an individual database connection using
        17278  +** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
        17279  +*/
        17280  +#ifndef SQLITE_DEFAULT_LOOKASIDE
        17281  +# define SQLITE_DEFAULT_LOOKASIDE 1200,100
        17282  +#endif
        17283  +
 17192  17284   
 17193  17285   /*
 17194  17286   ** The following singleton contains the global configuration for
 17195  17287   ** the SQLite library.
 17196  17288   */
 17197  17289   SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
 17198  17290      SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
 17199  17291      1,                         /* bCoreMutex */
 17200  17292      SQLITE_THREADSAFE==1,      /* bFullMutex */
 17201  17293      SQLITE_USE_URI,            /* bOpenUri */
 17202  17294      SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
 17203  17295      0x7ffffffe,                /* mxStrlen */
 17204  17296      0,                         /* neverCorrupt */
 17205         -   512,                       /* szLookaside */
 17206         -   125,                       /* nLookaside */
        17297  +   SQLITE_DEFAULT_LOOKASIDE,  /* szLookaside, nLookaside */
 17207  17298      SQLITE_STMTJRNL_SPILL,     /* nStmtSpill */
 17208  17299      {0,0,0,0,0,0,0,0},         /* m */
 17209  17300      {0,0,0,0,0,0,0,0,0},       /* mutex */
 17210  17301      {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
 17211  17302      (void*)0,                  /* pHeap */
 17212  17303      0,                         /* nHeap */
 17213  17304      0, 0,                      /* mnHeap, mxHeap */
................................................................................
 18211  18302     UnpackedRecord *pUnpacked;      /* Unpacked version of aRecord[] */
 18212  18303     UnpackedRecord *pNewUnpacked;   /* Unpacked version of new.* record */
 18213  18304     int iNewReg;                    /* Register for new.* values */
 18214  18305     i64 iKey1;                      /* First key value passed to hook */
 18215  18306     i64 iKey2;                      /* Second key value passed to hook */
 18216  18307     Mem *aNew;                      /* Array of new.* values */
 18217  18308     Table *pTab;                    /* Schema object being upated */          
        18309  +  Index *pPk;                     /* PK index if pTab is WITHOUT ROWID */
 18218  18310   };
 18219  18311   
 18220  18312   /*
 18221  18313   ** Function prototypes
 18222  18314   */
 18223  18315   SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
 18224  18316   SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
................................................................................
 20611  20703   **
 20612  20704   ** For this low-level routine, we are guaranteed that nByte>0 because
 20613  20705   ** cases of nByte<=0 will be intercepted and dealt with by higher level
 20614  20706   ** routines.
 20615  20707   */
 20616  20708   static void *sqlite3MemMalloc(int nByte){
 20617  20709   #ifdef SQLITE_MALLOCSIZE
 20618         -  void *p = SQLITE_MALLOC( nByte );
        20710  +  void *p;
        20711  +  testcase( ROUND8(nByte)==nByte );
        20712  +  p = SQLITE_MALLOC( nByte );
 20619  20713     if( p==0 ){
 20620  20714       testcase( sqlite3GlobalConfig.xLog!=0 );
 20621  20715       sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
 20622  20716     }
 20623  20717     return p;
 20624  20718   #else
 20625  20719     sqlite3_int64 *p;
 20626  20720     assert( nByte>0 );
 20627         -  nByte = ROUND8(nByte);
        20721  +  testcase( ROUND8(nByte)!=nByte );
 20628  20722     p = SQLITE_MALLOC( nByte+8 );
 20629  20723     if( p ){
 20630  20724       p[0] = nByte;
 20631  20725       p++;
 20632  20726     }else{
 20633  20727       testcase( sqlite3GlobalConfig.xLog!=0 );
 20634  20728       sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
................................................................................
 23742  23836   ** compiled without mutexes (SQLITE_THREADSAFE=0).
 23743  23837   */
 23744  23838   SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
 23745  23839   #if defined(SQLITE_MEMORY_BARRIER)
 23746  23840     SQLITE_MEMORY_BARRIER;
 23747  23841   #elif defined(__GNUC__)
 23748  23842     __sync_synchronize();
 23749         -#elif !defined(SQLITE_DISABLE_INTRINSIC) && \
 23750         -      defined(_MSC_VER) && _MSC_VER>=1300
        23843  +#elif MSVC_VERSION>=1300
 23751  23844     _ReadWriteBarrier();
 23752  23845   #elif defined(MemoryBarrier)
 23753  23846     MemoryBarrier();
 23754  23847   #endif
 23755  23848   }
 23756  23849   
 23757  23850   /*
................................................................................
 24275  24368     sqlite3_mutex_enter(mem0.mutex);
 24276  24369   }
 24277  24370   
 24278  24371   /*
 24279  24372   ** Do a memory allocation with statistics and alarms.  Assume the
 24280  24373   ** lock is already held.
 24281  24374   */
 24282         -static int mallocWithAlarm(int n, void **pp){
 24283         -  int nFull;
        24375  +static void mallocWithAlarm(int n, void **pp){
 24284  24376     void *p;
        24377  +  int nFull;
 24285  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. */
 24286  24386     nFull = sqlite3GlobalConfig.m.xRoundup(n);
        24387  +
 24287  24388     sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
 24288  24389     if( mem0.alarmThreshold>0 ){
 24289  24390       sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 24290  24391       if( nUsed >= mem0.alarmThreshold - nFull ){
 24291  24392         mem0.nearlyFull = 1;
 24292  24393         sqlite3MallocAlarm(nFull);
 24293  24394       }else{
................................................................................
 24303  24404   #endif
 24304  24405     if( p ){
 24305  24406       nFull = sqlite3MallocSize(p);
 24306  24407       sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
 24307  24408       sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
 24308  24409     }
 24309  24410     *pp = p;
 24310         -  return nFull;
 24311  24411   }
 24312  24412   
 24313  24413   /*
 24314  24414   ** Allocate memory.  This routine is like sqlite3_malloc() except that it
 24315  24415   ** assumes the memory subsystem has already been initialized.
 24316  24416   */
 24317  24417   SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
................................................................................
 24943  25043     etByte prefix;           /* Offset into aPrefix[] of the prefix string */
 24944  25044   } et_info;
 24945  25045   
 24946  25046   /*
 24947  25047   ** Allowed values for et_info.flags
 24948  25048   */
 24949  25049   #define FLAG_SIGNED  1     /* True if the value to convert is signed */
 24950         -#define FLAG_INTERN  2     /* True if for internal use only */
 24951  25050   #define FLAG_STRING  4     /* Allow infinity precision */
 24952  25051   
 24953  25052   
 24954  25053   /*
 24955  25054   ** The following table is searched linearly, so it is good to put the
 24956  25055   ** most frequently used conversion types first.
 24957  25056   */
................................................................................
 24977  25076     {  'G',  0, 1, etGENERIC,    14, 0 },
 24978  25077   #endif
 24979  25078     {  'i', 10, 1, etRADIX,      0,  0 },
 24980  25079     {  'n',  0, 0, etSIZE,       0,  0 },
 24981  25080     {  '%',  0, 0, etPERCENT,    0,  0 },
 24982  25081     {  'p', 16, 0, etPOINTER,    0,  1 },
 24983  25082   
 24984         -/* All the rest have the FLAG_INTERN bit set and are thus for internal
 24985         -** use only */
 24986         -  {  'T',  0, 2, etTOKEN,      0,  0 },
 24987         -  {  'S',  0, 2, etSRCLIST,    0,  0 },
 24988         -  {  'r', 10, 3, etORDINAL,    0,  0 },
        25083  +  /* All the rest are undocumented and are for internal use only */
        25084  +  {  'T',  0, 0, etTOKEN,      0,  0 },
        25085  +  {  'S',  0, 0, etSRCLIST,    0,  0 },
        25086  +  {  'r', 10, 1, etORDINAL,    0,  0 },
 24989  25087   };
 24990  25088   
 24991  25089   /*
 24992  25090   ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
 24993  25091   ** conversions will work.
 24994  25092   */
 24995  25093   #ifndef SQLITE_OMIT_FLOATING_POINT
................................................................................
 25075  25173     etByte flag_altform2;      /* True if "!" flag is present */
 25076  25174     etByte flag_zeropad;       /* True if field width constant starts with zero */
 25077  25175     etByte flag_long;          /* True if "l" flag is present */
 25078  25176     etByte flag_longlong;      /* True if the "ll" flag is present */
 25079  25177     etByte done;               /* Loop termination flag */
 25080  25178     etByte xtype = etINVALID;  /* Conversion paradigm */
 25081  25179     u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
 25082         -  u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
 25083  25180     char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
 25084  25181     sqlite_uint64 longvalue;   /* Value for integer types */
 25085  25182     LONGDOUBLE_TYPE realvalue; /* Value for real types */
 25086  25183     const et_info *infop;      /* Pointer to the appropriate info structure */
 25087  25184     char *zOut;                /* Rendering buffer */
 25088  25185     int nOut;                  /* Size of the rendering buffer */
 25089  25186     char *zExtra = 0;          /* Malloced memory used by some conversion */
................................................................................
 25094  25191     etByte flag_dp;            /* True if decimal point should be shown */
 25095  25192     etByte flag_rtz;           /* True if trailing zeros should be removed */
 25096  25193   #endif
 25097  25194     PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
 25098  25195     char buf[etBUFSIZE];       /* Conversion buffer */
 25099  25196   
 25100  25197     bufpt = 0;
 25101         -  if( pAccum->printfFlags ){
 25102         -    if( (bArgList = (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
 25103         -      pArgList = va_arg(ap, PrintfArguments*);
 25104         -    }
 25105         -    useIntern = pAccum->printfFlags & SQLITE_PRINTF_INTERNAL;
        25198  +  if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
        25199  +    pArgList = va_arg(ap, PrintfArguments*);
        25200  +    bArgList = 1;
 25106  25201     }else{
 25107         -    bArgList = useIntern = 0;
        25202  +    bArgList = 0;
 25108  25203     }
 25109  25204     for(; (c=(*fmt))!=0; ++fmt){
 25110  25205       if( c!='%' ){
 25111  25206         bufpt = (char *)fmt;
 25112  25207   #if HAVE_STRCHRNUL
 25113  25208         fmt = strchrnul(fmt, '%');
 25114  25209   #else
................................................................................
 25212  25307       }
 25213  25308       /* Fetch the info entry for the field */
 25214  25309       infop = &fmtinfo[0];
 25215  25310       xtype = etINVALID;
 25216  25311       for(idx=0; idx<ArraySize(fmtinfo); idx++){
 25217  25312         if( c==fmtinfo[idx].fmttype ){
 25218  25313           infop = &fmtinfo[idx];
 25219         -        if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
 25220         -          xtype = infop->type;
 25221         -        }else{
 25222         -          return;
 25223         -        }
        25314  +        xtype = infop->type;
 25224  25315           break;
 25225  25316         }
 25226  25317       }
 25227  25318   
 25228  25319       /*
 25229  25320       ** At this point, variables are initialized as follows:
 25230  25321       **
................................................................................
 25585  25676           length = j;
 25586  25677           /* The precision in %q and %Q means how many input characters to
 25587  25678           ** consume, not the length of the output...
 25588  25679           ** if( precision>=0 && precision<length ) length = precision; */
 25589  25680           break;
 25590  25681         }
 25591  25682         case etTOKEN: {
 25592         -        Token *pToken = va_arg(ap, Token*);
        25683  +        Token *pToken;
        25684  +        if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
        25685  +        pToken = va_arg(ap, Token*);
 25593  25686           assert( bArgList==0 );
 25594  25687           if( pToken && pToken->n ){
 25595  25688             sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
 25596  25689           }
 25597  25690           length = width = 0;
 25598  25691           break;
 25599  25692         }
 25600  25693         case etSRCLIST: {
 25601         -        SrcList *pSrc = va_arg(ap, SrcList*);
 25602         -        int k = va_arg(ap, int);
 25603         -        struct SrcList_item *pItem = &pSrc->a[k];
        25694  +        SrcList *pSrc;
        25695  +        int k;
        25696  +        struct SrcList_item *pItem;
        25697  +        if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
        25698  +        pSrc = va_arg(ap, SrcList*);
        25699  +        k = va_arg(ap, int);
        25700  +        pItem = &pSrc->a[k];
 25604  25701           assert( bArgList==0 );
 25605  25702           assert( k>=0 && k<pSrc->nSrc );
 25606  25703           if( pItem->zDatabase ){
 25607  25704             sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
 25608  25705             sqlite3StrAccumAppend(pAccum, ".", 1);
 25609  25706           }
 25610  25707           sqlite3StrAccumAppendAll(pAccum, pItem->zName);
................................................................................
 25618  25715       }/* End switch over the format type */
 25619  25716       /*
 25620  25717       ** The text of the conversion is pointed to by "bufpt" and is
 25621  25718       ** "length" characters long.  The field width is "width".  Do
 25622  25719       ** the output.
 25623  25720       */
 25624  25721       width -= length;
 25625         -    if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 25626         -    sqlite3StrAccumAppend(pAccum, bufpt, length);
 25627         -    if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
        25722  +    if( width>0 ){
        25723  +      if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
        25724  +      sqlite3StrAccumAppend(pAccum, bufpt, length);
        25725  +      if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
        25726  +    }else{
        25727  +      sqlite3StrAccumAppend(pAccum, bufpt, length);
        25728  +    }
 25628  25729   
 25629  25730       if( zExtra ){
 25630  25731         sqlite3DbFree(pAccum->db, zExtra);
 25631  25732         zExtra = 0;
 25632  25733       }
 25633  25734     }/* End for loop over the format string */
 25634  25735   } /* End of function */
................................................................................
 28592  28693   ** Read or write a four-byte big-endian integer value.
 28593  28694   */
 28594  28695   SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
 28595  28696   #if SQLITE_BYTEORDER==4321
 28596  28697     u32 x;
 28597  28698     memcpy(&x,p,4);
 28598  28699     return x;
 28599         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28600         -    && defined(__GNUC__) && GCC_VERSION>=4003000
        28700  +#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
 28601  28701     u32 x;
 28602  28702     memcpy(&x,p,4);
 28603  28703     return __builtin_bswap32(x);
 28604         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28605         -    && defined(_MSC_VER) && _MSC_VER>=1300
        28704  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
 28606  28705     u32 x;
 28607  28706     memcpy(&x,p,4);
 28608  28707     return _byteswap_ulong(x);
 28609  28708   #else
 28610  28709     testcase( p[0]&0x80 );
 28611  28710     return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
 28612  28711   #endif
 28613  28712   }
 28614  28713   SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
 28615  28714   #if SQLITE_BYTEORDER==4321
 28616  28715     memcpy(p,&v,4);
 28617         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28618         -    && defined(__GNUC__) && GCC_VERSION>=4003000
        28716  +#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
 28619  28717     u32 x = __builtin_bswap32(v);
 28620  28718     memcpy(p,&x,4);
 28621         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28622         -    && defined(_MSC_VER) && _MSC_VER>=1300
        28719  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
 28623  28720     u32 x = _byteswap_ulong(v);
 28624  28721     memcpy(p,&x,4);
 28625  28722   #else
 28626  28723     p[0] = (u8)(v>>24);
 28627  28724     p[1] = (u8)(v>>16);
 28628  28725     p[2] = (u8)(v>>8);
 28629  28726     p[3] = (u8)v;
................................................................................
 28731  28828   /*
 28732  28829   ** Attempt to add, substract, or multiply the 64-bit signed value iB against
 28733  28830   ** the other 64-bit signed integer at *pA and store the result in *pA.
 28734  28831   ** Return 0 on success.  Or if the operation would have resulted in an
 28735  28832   ** overflow, leave *pA unchanged and return 1.
 28736  28833   */
 28737  28834   SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
        28835  +#if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
        28836  +  return __builtin_add_overflow(*pA, iB, pA);
        28837  +#else
 28738  28838     i64 iA = *pA;
 28739  28839     testcase( iA==0 ); testcase( iA==1 );
 28740  28840     testcase( iB==-1 ); testcase( iB==0 );
 28741  28841     if( iB>=0 ){
 28742  28842       testcase( iA>0 && LARGEST_INT64 - iA == iB );
 28743  28843       testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
 28744  28844       if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
................................................................................
 28745  28845     }else{
 28746  28846       testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
 28747  28847       testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
 28748  28848       if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
 28749  28849     }
 28750  28850     *pA += iB;
 28751  28851     return 0; 
        28852  +#endif
 28752  28853   }
 28753  28854   SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
        28855  +#if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
        28856  +  return __builtin_sub_overflow(*pA, iB, pA);
        28857  +#else
 28754  28858     testcase( iB==SMALLEST_INT64+1 );
 28755  28859     if( iB==SMALLEST_INT64 ){
 28756  28860       testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
 28757  28861       if( (*pA)>=0 ) return 1;
 28758  28862       *pA -= iB;
 28759  28863       return 0;
 28760  28864     }else{
 28761  28865       return sqlite3AddInt64(pA, -iB);
 28762  28866     }
        28867  +#endif
 28763  28868   }
 28764  28869   SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
        28870  +#if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
        28871  +  return __builtin_mul_overflow(*pA, iB, pA);
        28872  +#else
 28765  28873     i64 iA = *pA;
 28766  28874     if( iB>0 ){
 28767  28875       if( iA>LARGEST_INT64/iB ) return 1;
 28768  28876       if( iA<SMALLEST_INT64/iB ) return 1;
 28769  28877     }else if( iB<0 ){
 28770  28878       if( iA>0 ){
 28771  28879         if( iB<SMALLEST_INT64/iA ) return 1;
................................................................................
 28773  28881         if( iB==SMALLEST_INT64 ) return 1;
 28774  28882         if( iA==SMALLEST_INT64 ) return 1;
 28775  28883         if( -iA>LARGEST_INT64/-iB ) return 1;
 28776  28884       }
 28777  28885     }
 28778  28886     *pA = iA*iB;
 28779  28887     return 0;
        28888  +#endif
 28780  28889   }
 28781  28890   
 28782  28891   /*
 28783  28892   ** Compute the absolute value of a 32-bit signed integer, of possible.  Or 
 28784  28893   ** if the integer has a value of -2147483648, return +2147483647
 28785  28894   */
 28786  28895   SQLITE_PRIVATE int sqlite3AbsInt32(int x){
................................................................................
 47477  47586   ** instead of
 47478  47587   **
 47479  47588   **   if( pPager->jfd->pMethods ){ ...
 47480  47589   */
 47481  47590   #define isOpen(pFd) ((pFd)->pMethods!=0)
 47482  47591   
 47483  47592   /*
 47484         -** Return true if this pager uses a write-ahead log instead of the usual
 47485         -** 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.
 47486  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
 47487  47605   #ifndef SQLITE_OMIT_WAL
 47488         -SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager){
 47489         -  return (pPager->pWal!=0);
 47490         -}
 47491         -# define pagerUseWal(x) sqlite3PagerUseWal(x)
        47606  +# define pagerUseWal(x) ((x)->pWal!=0)
 47492  47607   #else
 47493  47608   # define pagerUseWal(x) 0
 47494  47609   # define pagerRollbackWal(x) 0
 47495  47610   # define pagerWalFrames(v,w,x,y) 0
 47496  47611   # define pagerOpenWalIfPresent(z) SQLITE_OK
 47497  47612   # define pagerBeginReadTransaction(z) SQLITE_OK
 47498  47613   #endif
................................................................................
 58437  58552   /*
 58438  58553   ** get2byteAligned(), unlike get2byte(), requires that its argument point to a
 58439  58554   ** two-byte aligned address.  get2bytea() is only used for accessing the
 58440  58555   ** cell addresses in a btree header.
 58441  58556   */
 58442  58557   #if SQLITE_BYTEORDER==4321
 58443  58558   # define get2byteAligned(x)  (*(u16*)(x))
 58444         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 58445         -    && GCC_VERSION>=4008000
        58559  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
 58446  58560   # define get2byteAligned(x)  __builtin_bswap16(*(u16*)(x))
 58447         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 58448         -    && defined(_MSC_VER) && _MSC_VER>=1300
        58561  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
 58449  58562   # define get2byteAligned(x)  _byteswap_ushort(*(u16*)(x))
 58450  58563   #else
 58451  58564   # define get2byteAligned(x)  ((x)[0]<<8 | (x)[1])
 58452  58565   #endif
 58453  58566   
 58454  58567   /************** End of btreeInt.h ********************************************/
 58455  58568   /************** Continuing where we left off in btmutex.c ********************/
................................................................................
 58616  58729   ** There is a corresponding leave-all procedures.
 58617  58730   **
 58618  58731   ** Enter the mutexes in accending order by BtShared pointer address
 58619  58732   ** to avoid the possibility of deadlock when two threads with
 58620  58733   ** two or more btrees in common both try to lock all their btrees
 58621  58734   ** at the same instant.
 58622  58735   */
 58623         -SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
        58736  +static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
 58624  58737     int i;
        58738  +  int skipOk = 1;
 58625  58739     Btree *p;
 58626  58740     assert( sqlite3_mutex_held(db->mutex) );
 58627  58741     for(i=0; i<db->nDb; i++){
 58628  58742       p = db->aDb[i].pBt;
 58629         -    if( p ) sqlite3BtreeEnter(p);
        58743  +    if( p && p->sharable ){
        58744  +      sqlite3BtreeEnter(p);
        58745  +      skipOk = 0;
        58746  +    }
 58630  58747     }
        58748  +  db->skipBtreeMutex = skipOk;
 58631  58749   }
 58632         -SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
        58750  +SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
        58751  +  if( db->skipBtreeMutex==0 ) btreeEnterAll(db);
        58752  +}
        58753  +static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
 58633  58754     int i;
 58634  58755     Btree *p;
 58635  58756     assert( sqlite3_mutex_held(db->mutex) );
 58636  58757     for(i=0; i<db->nDb; i++){
 58637  58758       p = db->aDb[i].pBt;
 58638  58759       if( p ) sqlite3BtreeLeave(p);
 58639  58760     }
        58761  +}
        58762  +SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
        58763  +  if( db->skipBtreeMutex==0 ) btreeLeaveAll(db);
 58640  58764   }
 58641  58765   
 58642  58766   #ifndef NDEBUG
 58643  58767   /*
 58644  58768   ** Return true if the current thread holds the database connection
 58645  58769   ** mutex and all required BtShared mutexes.
 58646  58770   **
................................................................................
 62089  62213       nCell = pPage->nCell;
 62090  62214   
 62091  62215       for(i=0; i<nCell; i++){
 62092  62216         u8 *pCell = findCell(pPage, i);
 62093  62217         if( eType==PTRMAP_OVERFLOW1 ){
 62094  62218           CellInfo info;
 62095  62219           pPage->xParseCell(pPage, pCell, &info);
 62096         -        if( info.nLocal<info.nPayload
 62097         -         && pCell+info.nSize-1<=pPage->aData+pPage->maskPage
 62098         -         && iFrom==get4byte(pCell+info.nSize-4)
 62099         -        ){
 62100         -          put4byte(pCell+info.nSize-4, iTo);
 62101         -          break;
        62220  +        if( info.nLocal<info.nPayload ){
        62221  +          if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
        62222  +            return SQLITE_CORRUPT_BKPT;
        62223  +          }
        62224  +          if( iFrom==get4byte(pCell+info.nSize-4) ){
        62225  +            put4byte(pCell+info.nSize-4, iTo);
        62226  +            break;
        62227  +          }
 62102  62228           }
 62103  62229         }else{
 62104  62230           if( get4byte(pCell)==iFrom ){
 62105  62231             put4byte(pCell, iTo);
 62106  62232             break;
 62107  62233           }
 62108  62234         }
................................................................................
 62769  62895   SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
 62770  62896     int rc = SQLITE_OK;
 62771  62897     if( p && p->inTrans==TRANS_WRITE ){
 62772  62898       BtShared *pBt = p->pBt;
 62773  62899       assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
 62774  62900       assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
 62775  62901       sqlite3BtreeEnter(p);
 62776         -    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  +    }
 62777  62908       if( rc==SQLITE_OK ){
 62778  62909         if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
 62779  62910           pBt->nPage = 0;
 62780  62911         }
 62781  62912         rc = newDatabase(pBt);
 62782  62913         pBt->nPage = get4byte(28 + pBt->pPage1->aData);
 62783  62914   
................................................................................
 63155  63286   /*
 63156  63287   ** This function is used to read or overwrite payload information
 63157  63288   ** for the entry that the pCur cursor is pointing to. The eOp
 63158  63289   ** argument is interpreted as follows:
 63159  63290   **
 63160  63291   **   0: The operation is a read. Populate the overflow cache.
 63161  63292   **   1: The operation is a write. Populate the overflow cache.
 63162         -**   2: The operation is a read. Do not populate the overflow cache.
 63163  63293   **
 63164  63294   ** A total of "amt" bytes are read or written beginning at "offset".
 63165  63295   ** Data is read to or from the buffer pBuf.
 63166  63296   **
 63167  63297   ** The content being read or written might appear on the main page
 63168  63298   ** or be scattered out on multiple overflow pages.
 63169  63299   **
 63170         -** If the current cursor entry uses one or more overflow pages and the
 63171         -** eOp argument is not 2, this function may allocate space for and lazily 
 63172         -** 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). 
 63173  63303   ** Subsequent calls use this cache to make seeking to the supplied offset 
 63174  63304   ** more efficient.
 63175  63305   **
 63176         -** 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
 63177  63307   ** invalidated if some other cursor writes to the same table, or if
 63178  63308   ** the cursor is moved to a different row. Additionally, in auto-vacuum
 63179  63309   ** mode, the following events may invalidate an overflow page-list cache.
 63180  63310   **
 63181  63311   **   * An incremental vacuum,
 63182  63312   **   * A commit in auto_vacuum="full" mode,
 63183  63313   **   * Creating a table (may require moving an overflow page).
................................................................................
 63191  63321   ){
 63192  63322     unsigned char *aPayload;
 63193  63323     int rc = SQLITE_OK;
 63194  63324     int iIdx = 0;
 63195  63325     MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
 63196  63326     BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
 63197  63327   #ifdef SQLITE_DIRECT_OVERFLOW_READ
 63198         -  unsigned char * const pBufStart = pBuf;
 63199         -  int bEnd;                                 /* True if reading to end of data */
        63328  +  unsigned char * const pBufStart = pBuf;     /* Start of original out buffer */
 63200  63329   #endif
 63201  63330   
 63202  63331     assert( pPage );
        63332  +  assert( eOp==0 || eOp==1 );
 63203  63333     assert( pCur->eState==CURSOR_VALID );
 63204  63334     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 63205  63335     assert( cursorHoldsMutex(pCur) );
 63206         -  assert( eOp!=2 || offset==0 );    /* Always start from beginning for eOp==2 */
 63207  63336   
 63208  63337     getCellInfo(pCur);
 63209  63338     aPayload = pCur->info.pPayload;
 63210         -#ifdef SQLITE_DIRECT_OVERFLOW_READ
 63211         -  bEnd = offset+amt==pCur->info.nPayload;
 63212         -#endif
 63213  63339     assert( offset+amt <= pCur->info.nPayload );
 63214  63340   
 63215  63341     assert( aPayload > pPage->aData );
 63216  63342     if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
 63217  63343       /* Trying to read or write past the end of the data is an error.  The
 63218  63344       ** conditional above is really:
 63219  63345       **    &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
................................................................................
 63224  63350   
 63225  63351     /* Check if data must be read/written to/from the btree page itself. */
 63226  63352     if( offset<pCur->info.nLocal ){
 63227  63353       int a = amt;
 63228  63354       if( a+offset>pCur->info.nLocal ){
 63229  63355         a = pCur->info.nLocal - offset;
 63230  63356       }
 63231         -    rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage);
        63357  +    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
 63232  63358       offset = 0;
 63233  63359       pBuf += a;
 63234  63360       amt -= a;
 63235  63361     }else{
 63236  63362       offset -= pCur->info.nLocal;
 63237  63363     }
 63238  63364   
................................................................................
 63240  63366     if( rc==SQLITE_OK && amt>0 ){
 63241  63367       const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
 63242  63368       Pgno nextPage;
 63243  63369   
 63244  63370       nextPage = get4byte(&aPayload[pCur->info.nLocal]);
 63245  63371   
 63246  63372       /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
 63247         -    ** Except, do not allocate aOverflow[] for eOp==2.
 63248  63373       **
 63249  63374       ** The aOverflow[] array is sized at one entry for each overflow page
 63250  63375       ** in the overflow chain. The page number of the first overflow page is
 63251  63376       ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
 63252  63377       ** means "not yet known" (the cache is lazily populated).
 63253  63378       */
 63254         -    if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
        63379  +    if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
 63255  63380         int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
 63256  63381         if( nOvfl>pCur->nOvflAlloc ){
 63257  63382           Pgno *aNew = (Pgno*)sqlite3Realloc(
 63258  63383               pCur->aOverflow, nOvfl*2*sizeof(Pgno)
 63259  63384           );
 63260  63385           if( aNew==0 ){
 63261         -          rc = SQLITE_NOMEM_BKPT;
        63386  +          return SQLITE_NOMEM_BKPT;
 63262  63387           }else{
 63263  63388             pCur->nOvflAlloc = nOvfl*2;
 63264  63389             pCur->aOverflow = aNew;
 63265  63390           }
 63266  63391         }
 63267         -      if( rc==SQLITE_OK ){
 63268         -        memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
 63269         -        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);
 63270  63403         }
 63271  63404       }
 63272  63405   
 63273         -    /* If the overflow page-list cache has been allocated and the
 63274         -    ** entry for the first required overflow page is valid, skip
 63275         -    ** directly to it.
 63276         -    */
 63277         -    if( (pCur->curFlags & BTCF_ValidOvfl)!=0
 63278         -     && pCur->aOverflow[offset/ovflSize]
 63279         -    ){
 63280         -      iIdx = (offset/ovflSize);
 63281         -      nextPage = pCur->aOverflow[iIdx];
 63282         -      offset = (offset%ovflSize);
 63283         -    }
 63284         -
 63285         -    for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
 63286         -
        63406  +    assert( rc==SQLITE_OK && amt>0 );
        63407  +    while( nextPage ){
 63287  63408         /* If required, populate the overflow page-list cache. */
 63288         -      if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
 63289         -        assert( pCur->aOverflow[iIdx]==0
 63290         -                || pCur->aOverflow[iIdx]==nextPage
 63291         -                || CORRUPT_DB );
 63292         -        pCur->aOverflow[iIdx] = nextPage;
 63293         -      }
        63409  +      assert( pCur->aOverflow[iIdx]==0
        63410  +              || pCur->aOverflow[iIdx]==nextPage
        63411  +              || CORRUPT_DB );
        63412  +      pCur->aOverflow[iIdx] = nextPage;
 63294  63413   
 63295  63414         if( offset>=ovflSize ){
 63296  63415           /* The only reason to read this page is to obtain the page
 63297  63416           ** number for the next page in the overflow chain. The page
 63298  63417           ** data is not required. So first try to lookup the overflow
 63299  63418           ** page-list cache, if any, then fall back to the getOverflowPage()
 63300  63419           ** function.
 63301         -        **
 63302         -        ** Note that the aOverflow[] array must be allocated because eOp!=2
 63303         -        ** here.  If eOp==2, then offset==0 and this branch is never taken.
 63304  63420           */
 63305         -        assert( eOp!=2 );
 63306  63421           assert( pCur->curFlags & BTCF_ValidOvfl );
 63307  63422           assert( pCur->pBtree->db==pBt->db );
 63308  63423           if( pCur->aOverflow[iIdx+1] ){
 63309  63424             nextPage = pCur->aOverflow[iIdx+1];
 63310  63425           }else{
 63311  63426             rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
 63312  63427           }
 63313  63428           offset -= ovflSize;
 63314  63429         }else{
 63315  63430           /* Need to read this page properly. It contains some of the
 63316  63431           ** range of data that is being read (eOp==0) or written (eOp!=0).
 63317  63432           */
 63318  63433   #ifdef SQLITE_DIRECT_OVERFLOW_READ
 63319         -        sqlite3_file *fd;
        63434  +        sqlite3_file *fd;      /* File from which to do direct overflow read */
 63320  63435   #endif
 63321  63436           int a = amt;
 63322  63437           if( a + offset > ovflSize ){
 63323  63438             a = ovflSize - offset;
 63324  63439           }
 63325  63440   
 63326  63441   #ifdef SQLITE_DIRECT_OVERFLOW_READ
 63327  63442           /* If all the following are true:
 63328  63443           **
 63329  63444           **   1) this is a read operation, and 
 63330  63445           **   2) data is required from the start of this overflow page, and
 63331         -        **   3) the database is file-backed, and
 63332         -        **   4) there is no open write-transaction, and
 63333         -        **   5) the database is not a WAL database,
 63334         -        **   6) all data from the page is being read.
 63335         -        **   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 
 63336  63450           **
 63337  63451           ** then data can be read directly from the database file into the
 63338  63452           ** output buffer, bypassing the page-cache altogether. This speeds
 63339  63453           ** up loading large records that span many overflow pages.
 63340  63454           */
 63341         -        if( (eOp&0x01)==0                                      /* (1) */
        63455  +        if( eOp==0                                             /* (1) */
 63342  63456            && offset==0                                          /* (2) */
 63343         -         && (bEnd || a==ovflSize)                              /* (6) */
 63344         -         && pBt->inTransaction==TRANS_READ                     /* (4) */
 63345         -         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
 63346         -         && 0==sqlite3PagerUseWal(pBt->pPager)                 /* (5) */
 63347         -         && &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) */
 63348  63461           ){
 63349  63462             u8 aSave[4];
 63350  63463             u8 *aWrite = &pBuf[-4];
 63351         -          assert( aWrite>=pBufStart );                         /* hence (7) */
        63464  +          assert( aWrite>=pBufStart );                         /* due to (6) */
 63352  63465             memcpy(aSave, aWrite, 4);
 63353  63466             rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
 63354  63467             nextPage = get4byte(aWrite);
 63355  63468             memcpy(aWrite, aSave, 4);
 63356  63469           }else
 63357  63470   #endif
 63358  63471   
 63359  63472           {
 63360  63473             DbPage *pDbPage;
 63361  63474             rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
 63362         -              ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0)
        63475  +              (eOp==0 ? PAGER_GET_READONLY : 0)
 63363  63476             );
 63364  63477             if( rc==SQLITE_OK ){
 63365  63478               aPayload = sqlite3PagerGetData(pDbPage);
 63366  63479               nextPage = get4byte(aPayload);
 63367         -            rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage);
        63480  +            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
 63368  63481               sqlite3PagerUnref(pDbPage);
 63369  63482               offset = 0;
 63370  63483             }
 63371  63484           }
 63372  63485           amt -= a;
        63486  +        if( amt==0 ) return rc;
 63373  63487           pBuf += a;
 63374  63488         }
        63489  +      if( rc ) break;
        63490  +      iIdx++;
 63375  63491       }
 63376  63492     }
 63377  63493   
 63378  63494     if( rc==SQLITE_OK && amt>0 ){
 63379         -    return SQLITE_CORRUPT_BKPT;
        63495  +    return SQLITE_CORRUPT_BKPT; /* Overflow chain ends prematurely */
 63380  63496     }
 63381  63497     return rc;
 63382  63498   }
 63383  63499   
 63384  63500   /*
 63385  63501   ** Read part of the payload for the row at which that cursor pCur is currently
 63386  63502   ** pointing.  "amt" bytes will be transferred into pBuf[].  The transfer
................................................................................
 63401  63517   SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
 63402  63518     assert( cursorHoldsMutex(pCur) );
 63403  63519     assert( pCur->eState==CURSOR_VALID );
 63404  63520     assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
 63405  63521     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 63406  63522     return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
 63407  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  +*/
 63408  63530   #ifndef SQLITE_OMIT_INCRBLOB
 63409         -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  +){
 63410  63537     int rc;
 63411  63538     if ( pCur->eState==CURSOR_INVALID ){
 63412  63539       return SQLITE_ABORT;
 63413  63540     }
 63414  63541     assert( cursorOwnsBtShared(pCur) );
 63415         -  rc = restoreCursorPosition(pCur);
 63416         -  if( rc==SQLITE_OK ){
 63417         -    assert( pCur->eState==CURSOR_VALID );
 63418         -    assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
 63419         -    assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 63420         -    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);
 63421  63551     }
 63422         -  return rc;
 63423  63552   }
 63424  63553   #endif /* SQLITE_OMIT_INCRBLOB */
 63425  63554   
 63426  63555   /*
 63427  63556   ** Return a pointer to payload information from the entry that the 
 63428  63557   ** pCur cursor is pointing to.  The pointer is to the beginning of
 63429  63558   ** the key if index btrees (pPage->intKey==0) and is the data for
................................................................................
 63821  63950     if( pIdxKey==0
 63822  63951      && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
 63823  63952     ){
 63824  63953       if( pCur->info.nKey==intKey ){
 63825  63954         *pRes = 0;
 63826  63955         return SQLITE_OK;
 63827  63956       }
 63828         -    if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
 63829         -      *pRes = -1;
 63830         -      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  +      }
 63831  63977       }
 63832  63978     }
 63833  63979   
 63834  63980     if( pIdxKey ){
 63835  63981       xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
 63836  63982       pIdxKey->errCode = 0;
 63837  63983       assert( pIdxKey->default_rc==1 
................................................................................
 63959  64105             }
 63960  64106             pCellKey = sqlite3Malloc( nCell+18 );
 63961  64107             if( pCellKey==0 ){
 63962  64108               rc = SQLITE_NOMEM_BKPT;
 63963  64109               goto moveto_finish;
 63964  64110             }
 63965  64111             pCur->aiIdx[pCur->iPage] = (u16)idx;
 63966         -          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
        64112  +          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
        64113  +          pCur->curFlags &= ~BTCF_ValidOvfl;
 63967  64114             if( rc ){
 63968  64115               sqlite3_free(pCellKey);
 63969  64116               goto moveto_finish;
 63970  64117             }
 63971  64118             c = xRecordCompare(nCell, pCellKey, pIdxKey);
 63972  64119             sqlite3_free(pCellKey);
 63973  64120           }
................................................................................
 66002  66149     ** usableSpace: Number of bytes of space available on each sibling.
 66003  66150     ** 
 66004  66151     */
 66005  66152     usableSpace = pBt->usableSize - 12 + leafCorrection;
 66006  66153     for(i=0; i<nOld; i++){
 66007  66154       MemPage *p = apOld[i];
 66008  66155       szNew[i] = usableSpace - p->nFree;
 66009         -    if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
 66010  66156       for(j=0; j<p->nOverflow; j++){
 66011  66157         szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
 66012  66158       }
 66013  66159       cntNew[i] = cntOld[i];
 66014  66160     }
 66015  66161     k = nOld;
 66016  66162     for(i=0; i<k; i++){
................................................................................
 66681  66827   ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
 66682  66828   ** key values and pX->aMem can be used instead of pX->pKey to avoid having
 66683  66829   ** to decode the key.
 66684  66830   */
 66685  66831   SQLITE_PRIVATE int sqlite3BtreeInsert(
 66686  66832     BtCursor *pCur,                /* Insert data into the table of this cursor */
 66687  66833     const BtreePayload *pX,        /* Content of the row to be inserted */
 66688         -  int appendBias,                /* True if this is likely an append */
        66834  +  int flags,                     /* True if this is likely an append */
 66689  66835     int seekResult                 /* Result of prior MovetoUnpacked() call */
 66690  66836   ){
 66691  66837     int rc;
 66692  66838     int loc = seekResult;          /* -1: before desired location  +1: after */
 66693  66839     int szNew = 0;
 66694  66840     int idx;
 66695  66841     MemPage *pPage;
 66696  66842     Btree *p = pCur->pBtree;
 66697  66843     BtShared *pBt = p->pBt;
 66698  66844     unsigned char *oldCell;
 66699  66845     unsigned char *newCell = 0;
        66846  +
        66847  +  assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
 66700  66848   
 66701  66849     if( pCur->eState==CURSOR_FAULT ){
 66702  66850       assert( pCur->skipNext!=SQLITE_OK );
 66703  66851       return pCur->skipNext;
 66704  66852     }
 66705  66853   
 66706  66854     assert( cursorOwnsBtShared(pCur) );
................................................................................
 66734  66882   
 66735  66883     if( pCur->pKeyInfo==0 ){
 66736  66884       assert( pX->pKey==0 );
 66737  66885       /* If this is an insert into a table b-tree, invalidate any incrblob 
 66738  66886       ** cursors open on the row being replaced */
 66739  66887       invalidateIncrblobCursors(p, pX->nKey, 0);
 66740  66888   
        66889  +    /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing 
        66890  +    ** to a row with the same key as the new entry being inserted.  */
        66891  +    assert( (flags & BTREE_SAVEPOSITION)==0 || 
        66892  +            ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
        66893  +
 66741  66894       /* If the cursor is currently on the last row and we are appending a
 66742  66895       ** new row onto the end, set the "loc" to avoid an unnecessary
 66743  66896       ** btreeMoveto() call */
 66744  66897       if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
 66745  66898         loc = 0;
 66746  66899       }else if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0
 66747  66900                  && pCur->info.nKey==pX->nKey-1 ){
 66748  66901         loc = -1;
 66749  66902       }else if( loc==0 ){
 66750         -      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, appendBias, &loc);
        66903  +      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
 66751  66904         if( rc ) return rc;
 66752  66905       }
 66753         -  }else if( loc==0 ){
        66906  +  }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
 66754  66907       if( pX->nMem ){
 66755  66908         UnpackedRecord r;
 66756  66909         r.pKeyInfo = pCur->pKeyInfo;
 66757  66910         r.aMem = pX->aMem;
 66758  66911         r.nField = pX->nMem;
 66759  66912         r.default_rc = 0;
 66760  66913         r.errCode = 0;
 66761  66914         r.r1 = 0;
 66762  66915         r.r2 = 0;
 66763  66916         r.eqSeen = 0;
 66764         -      rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, appendBias, &loc);
        66917  +      rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
 66765  66918       }else{
 66766         -      rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc);
        66919  +      rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
 66767  66920       }
 66768  66921       if( rc ) return rc;
 66769  66922     }
 66770  66923     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 66771  66924   
 66772  66925     pPage = pCur->apPage[pCur->iPage];
 66773  66926     assert( pPage->intKey || pX->nKey>=0 );
................................................................................
 66847  67000   
 66848  67001       /* Must make sure nOverflow is reset to zero even if the balance()
 66849  67002       ** fails. Internal data structure corruption will result otherwise. 
 66850  67003       ** Also, set the cursor state to invalid. This stops saveCursorPosition()
 66851  67004       ** from trying to save the current position of the cursor.  */
 66852  67005       pCur->apPage[pCur->iPage]->nOverflow = 0;
 66853  67006       pCur->eState = CURSOR_INVALID;
        67007  +    if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
        67008  +      rc = moveToRoot(pCur);
        67009  +      if( pCur->pKeyInfo ){
        67010  +        assert( pCur->pKey==0 );
        67011  +        pCur->pKey = sqlite3Malloc( pX->nKey );
        67012  +        if( pCur->pKey==0 ){
        67013  +          rc = SQLITE_NOMEM;
        67014  +        }else{
        67015  +          memcpy(pCur->pKey, pX->pKey, pX->nKey);
        67016  +        }
        67017  +      }
        67018  +      pCur->eState = CURSOR_REQUIRESEEK;
        67019  +      pCur->nKey = pX->nKey;
        67020  +    }
 66854  67021     }
 66855  67022     assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
 66856  67023   
 66857  67024   end_insert:
 66858  67025     return rc;
 66859  67026   }
 66860  67027   
................................................................................
 70273  70440   static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
 70274  70441     assert( pVal!=0 );
 70275  70442     assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
 70276  70443     assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
 70277  70444     assert( (pVal->flags & MEM_RowSet)==0 );
 70278  70445     assert( (pVal->flags & (MEM_Null))==0 );
 70279  70446     if( pVal->flags & (MEM_Blob|MEM_Str) ){
        70447  +    if( ExpandBlob(pVal) ) return 0;
 70280  70448       pVal->flags |= MEM_Str;
 70281  70449       if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
 70282  70450         sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
 70283  70451       }
 70284  70452       if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
 70285  70453         assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
 70286  70454         if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
................................................................................
 71621  71789   */
 71622  71790   #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
 71623  71791   SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
 71624  71792     assert( p->nOp + N <= p->pParse->nOpAlloc );
 71625  71793   }
 71626  71794   #endif
 71627  71795   
        71796  +/*
        71797  +** Verify that the VM passed as the only argument does not contain
        71798  +** an OP_ResultRow opcode. Fail an assert() if it does. This is used
        71799  +** by code in pragma.c to ensure that the implementation of certain
        71800  +** pragmas comports with the flags specified in the mkpragmatab.tcl
        71801  +** script.
        71802  +*/
        71803  +#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
        71804  +SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
        71805  +  int i;
        71806  +  for(i=0; i<p->nOp; i++){
        71807  +    assert( p->aOp[i].opcode!=OP_ResultRow );
        71808  +  }
        71809  +}
        71810  +#endif
        71811  +
 71628  71812   /*
 71629  71813   ** This function returns a pointer to the array of opcodes associated with
 71630  71814   ** the Vdbe passed as the first argument. It is the callers responsibility
 71631  71815   ** to arrange for the returned array to be eventually freed using the 
 71632  71816   ** vdbeFreeOpArray() function.
 71633  71817   **
 71634  71818   ** Before returning, *pnOp is set to the number of entries in the returned
................................................................................
 71740  71924   }
 71741  71925   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
 71742  71926     sqlite3VdbeGetOp(p,addr)->p2 = val;
 71743  71927   }
 71744  71928   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
 71745  71929     sqlite3VdbeGetOp(p,addr)->p3 = val;
 71746  71930   }
 71747         -SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){
        71931  +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
 71748  71932     assert( p->nOp>0 || p->db->mallocFailed );
 71749  71933     if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
 71750  71934   }
 71751  71935   
 71752  71936   /*
 71753  71937   ** Change the P2 operand of instruction addr so that it points to
 71754  71938   ** the address of the next instruction to be coded.
................................................................................
 73454  73638   ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
 73455  73639   ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the 
 73456  73640   ** statement transaction is committed.
 73457  73641   **
 73458  73642   ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 
 73459  73643   ** Otherwise SQLITE_OK.
 73460  73644   */
 73461         -SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
        73645  +static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
 73462  73646     sqlite3 *const db = p->db;
 73463  73647     int rc = SQLITE_OK;
        73648  +  int i;
        73649  +  const int iSavepoint = p->iStatement-1;
 73464  73650   
 73465         -  /* If p->iStatement is greater than zero, then this Vdbe opened a 
 73466         -  ** statement transaction that should be closed here. The only exception
 73467         -  ** is that an IO error may have occurred, causing an emergency rollback.
 73468         -  ** In this case (db->nStatement==0), and there is nothing to do.
 73469         -  */
 73470         -  if( db->nStatement && p->iStatement ){
 73471         -    int i;
 73472         -    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) );
 73473  73654   
 73474         -    assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
 73475         -    assert( db->nStatement>0 );
 73476         -    assert( p->iStatement==(db->nStatement+db->nSavepoint) );
 73477         -
 73478         -    for(i=0; i<db->nDb; i++){ 
 73479         -      int rc2 = SQLITE_OK;
 73480         -      Btree *pBt = db->aDb[i].pBt;
 73481         -      if( pBt ){
 73482         -        if( eOp==SAVEPOINT_ROLLBACK ){
 73483         -          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
 73484         -        }
 73485         -        if( rc2==SQLITE_OK ){
 73486         -          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
 73487         -        }
 73488         -        if( rc==SQLITE_OK ){
 73489         -          rc = rc2;
 73490         -        }
 73491         -      }
 73492         -    }
 73493         -    db->nStatement--;
 73494         -    p->iStatement = 0;
 73495         -
 73496         -    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 ){
 73497  73659         if( eOp==SAVEPOINT_ROLLBACK ){
 73498         -        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);
 73499  73664         }
 73500  73665         if( rc==SQLITE_OK ){
 73501         -        rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
        73666  +        rc = rc2;
 73502  73667         }
 73503  73668       }
        73669  +  }
        73670  +  db->nStatement--;
        73671  +  p->iStatement = 0;
 73504  73672   
 73505         -    /* If the statement transaction is being rolled back, also restore the 
 73506         -    ** database handles deferred constraint counter to the value it had when 
 73507         -    ** the statement transaction was opened.  */
        73673  +  if( rc==SQLITE_OK ){
 73508  73674       if( eOp==SAVEPOINT_ROLLBACK ){
 73509         -      db->nDeferredCons = p->nStmtDefCons;
 73510         -      db->nDeferredImmCons = p->nStmtDefImmCons;
        73675  +      rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
 73511  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;
 73512  73688     }
 73513  73689     return rc;
 73514  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  +
 73515  73698   
 73516  73699   /*
 73517  73700   ** This function is called when a transaction opened by the database 
 73518  73701   ** handle associated with the VM passed as an argument is about to be 
 73519  73702   ** committed. If there are outstanding deferred foreign key constraint
 73520  73703   ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
 73521  73704   **
................................................................................
 75542  75725   ** If the second argument is not NULL, release any allocations associated 
 75543  75726   ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
 75544  75727   ** structure itself, using sqlite3DbFree().
 75545  75728   **
 75546  75729   ** This function is used to free UnpackedRecord structures allocated by
 75547  75730   ** the vdbeUnpackRecord() function found in vdbeapi.c.
 75548  75731   */
 75549         -static void vdbeFreeUnpacked(sqlite3 *db, UnpackedRecord *p){
        75732  +static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
 75550  75733     if( p ){
 75551  75734       int i;
 75552         -    for(i=0; i<p->nField; i++){
        75735  +    for(i=0; i<nField; i++){
 75553  75736         Mem *pMem = &p->aMem[i];
 75554  75737         if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
 75555  75738       }
 75556  75739       sqlite3DbFree(db, p);
 75557  75740     }
 75558  75741   }
 75559  75742   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
................................................................................
 75578  75761     i64 iKey2;
 75579  75762     PreUpdate preupdate;
 75580  75763     const char *zTbl = pTab->zName;
 75581  75764     static const u8 fakeSortOrder = 0;
 75582  75765   
 75583  75766     assert( db->pPreUpdate==0 );
 75584  75767     memset(&preupdate, 0, sizeof(PreUpdate));
 75585         -  if( op==SQLITE_UPDATE ){
 75586         -    iKey2 = v->aMem[iReg].u.i;
        75768  +  if( HasRowid(pTab)==0 ){
        75769  +    iKey1 = iKey2 = 0;
        75770  +    preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
 75587  75771     }else{
 75588         -    iKey2 = iKey1;
        75772  +    if( op==SQLITE_UPDATE ){
        75773  +      iKey2 = v->aMem[iReg].u.i;
        75774  +    }else{
        75775  +      iKey2 = iKey1;
        75776  +    }
 75589  75777     }
 75590  75778   
 75591  75779     assert( pCsr->nField==pTab->nCol 
 75592  75780          || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
 75593  75781     );
 75594  75782   
 75595  75783     preupdate.v = v;
................................................................................
 75604  75792     preupdate.iKey2 = iKey2;
 75605  75793     preupdate.pTab = pTab;
 75606  75794   
 75607  75795     db->pPreUpdate = &preupdate;
 75608  75796     db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
 75609  75797     db->pPreUpdate = 0;
 75610  75798     sqlite3DbFree(db, preupdate.aRecord);
 75611         -  vdbeFreeUnpacked(db, preupdate.pUnpacked);
 75612         -  vdbeFreeUnpacked(db, preupdate.pNewUnpacked);
        75799  +  vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pUnpacked);
        75800  +  vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pNewUnpacked);
 75613  75801     if( preupdate.aNew ){
 75614  75802       int i;
 75615  75803       for(i=0; i<pCsr->nField; i++){
 75616  75804         sqlite3VdbeMemRelease(&preupdate.aNew[i]);
 75617  75805       }
 75618  75806       sqlite3DbFree(db, preupdate.aNew);
 75619  75807     }
................................................................................
 77280  77468   
 77281  77469   /*
 77282  77470   ** This function is called from within a pre-update callback to retrieve
 77283  77471   ** a field of the row currently being updated or deleted.
 77284  77472   */
 77285  77473   SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
 77286  77474     PreUpdate *p = db->pPreUpdate;
        77475  +  Mem *pMem;
 77287  77476     int rc = SQLITE_OK;
 77288  77477   
 77289  77478     /* Test that this call is being made from within an SQLITE_DELETE or
 77290  77479     ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
 77291  77480     if( !p || p->op==SQLITE_INSERT ){
 77292  77481       rc = SQLITE_MISUSE_BKPT;
 77293  77482       goto preupdate_old_out;
 77294  77483     }
        77484  +  if( p->pPk ){
        77485  +    iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
        77486  +  }
 77295  77487     if( iIdx>=p->pCsr->nField || iIdx<0 ){
 77296  77488       rc = SQLITE_RANGE;
 77297  77489       goto preupdate_old_out;
 77298  77490     }
 77299  77491   
 77300  77492     /* If the old.* record has not yet been loaded into memory, do so now. */
 77301  77493     if( p->pUnpacked==0 ){
................................................................................
 77313  77505       if( rc!=SQLITE_OK ){
 77314  77506         sqlite3DbFree(db, aRec);
 77315  77507         goto preupdate_old_out;
 77316  77508       }
 77317  77509       p->aRecord = aRec;
 77318  77510     }
 77319  77511   
 77320         -  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 ){
 77321  77516       *ppValue = (sqlite3_value *)columnNullValue();
 77322         -  }else{
 77323         -    Mem *pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
 77324         -    *ppValue = &p->pUnpacked->aMem[iIdx];
 77325         -    if( iIdx==p->pTab->iPKey ){
 77326         -      sqlite3VdbeMemSetInt64(pMem, p->iKey1);
 77327         -    }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
 77328         -      if( pMem->flags & MEM_Int ){
 77329         -        sqlite3VdbeMemRealify(pMem);
 77330         -      }
        77517  +  }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
        77518  +    if( pMem->flags & MEM_Int ){
        77519  +      sqlite3VdbeMemRealify(pMem);
 77331  77520       }
 77332  77521     }
 77333  77522   
 77334  77523    preupdate_old_out:
 77335  77524     sqlite3Error(db, rc);
 77336  77525     return sqlite3ApiExit(db, rc);
 77337  77526   }
................................................................................
 77376  77565     int rc = SQLITE_OK;
 77377  77566     Mem *pMem;
 77378  77567   
 77379  77568     if( !p || p->op==SQLITE_DELETE ){
 77380  77569       rc = SQLITE_MISUSE_BKPT;
 77381  77570       goto preupdate_new_out;
 77382  77571     }
        77572  +  if( p->pPk && p->op!=SQLITE_UPDATE ){
        77573  +    iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
        77574  +  }
 77383  77575     if( iIdx>=p->pCsr->nField || iIdx<0 ){
 77384  77576       rc = SQLITE_RANGE;
 77385  77577       goto preupdate_new_out;
 77386  77578     }
 77387  77579   
 77388  77580     if( p->op==SQLITE_INSERT ){
 77389  77581       /* For an INSERT, memory cell p->iNewReg contains the serialized record
................................................................................
 77396  77588         pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
 77397  77589         if( !pUnpack ){
 77398  77590           rc = SQLITE_NOMEM;
 77399  77591           goto preupdate_new_out;
 77400  77592         }
 77401  77593         p->pNewUnpacked = pUnpack;
 77402  77594       }
 77403         -    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 ){
 77404  77599         pMem = (sqlite3_value *)columnNullValue();
 77405         -    }else{
 77406         -      pMem = &pUnpack->aMem[iIdx];
 77407         -      if( iIdx==p->pTab->iPKey ){
 77408         -        sqlite3VdbeMemSetInt64(pMem, p->iKey2);
 77409         -      }
 77410  77600       }
 77411  77601     }else{
 77412  77602       /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
 77413  77603       ** value. Make a copy of the cell contents and return a pointer to it.
 77414  77604       ** It is not safe to return a pointer to the memory cell itself as the
 77415  77605       ** caller may modify the value text encoding.
 77416  77606       */
................................................................................
 78379  78569     unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
 78380  78570   #endif
 78381  78571     Mem *aMem = p->aMem;       /* Copy of p->aMem */
 78382  78572     Mem *pIn1 = 0;             /* 1st input operand */
 78383  78573     Mem *pIn2 = 0;             /* 2nd input operand */
 78384  78574     Mem *pIn3 = 0;             /* 3rd input operand */
 78385  78575     Mem *pOut = 0;             /* Output operand */
 78386         -  int *aPermute = 0;         /* Permutation of columns for OP_Compare */
 78387         -  i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
 78388  78576   #ifdef VDBE_PROFILE
 78389  78577     u64 start;                 /* CPU clock count at start of opcode */
 78390  78578   #endif
 78391  78579     /*** INSERT STACK UNION HERE ***/
 78392  78580   
 78393  78581     assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
 78394  78582     sqlite3VdbeEnter(p);
................................................................................
 78395  78583     if( p->rc==SQLITE_NOMEM ){
 78396  78584       /* This happens if a malloc() inside a call to sqlite3_column_text() or
 78397  78585       ** sqlite3_column_text16() failed.  */
 78398  78586       goto no_mem;
 78399  78587     }
 78400  78588     assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
 78401  78589     assert( p->bIsReader || p->readOnly!=0 );
 78402         -  p->rc = SQLITE_OK;
 78403  78590     p->iCurrentTime = 0;
 78404  78591     assert( p->explain==0 );
 78405  78592     p->pResultSet = 0;
 78406  78593     db->busyHandler.nBusy = 0;
 78407  78594     if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 78408  78595     sqlite3VdbeIOTraceSql(p);
 78409  78596   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
................................................................................
 78756  78943     if( pOp->p1==SQLITE_OK && p->pFrame ){
 78757  78944       /* Halt the sub-program. Return control to the parent frame. */
 78758  78945       pFrame = p->pFrame;
 78759  78946       p->pFrame = pFrame->pParent;
 78760  78947       p->nFrame--;
 78761  78948       sqlite3VdbeSetChanges(db, p->nChange);
 78762  78949       pcx = sqlite3VdbeFrameRestore(pFrame);
 78763         -    lastRowid = db->lastRowid;
 78764  78950       if( pOp->p2==OE_Ignore ){
 78765  78951         /* Instruction pcx is the OP_Program that invoked the sub-program 
 78766  78952         ** currently being halted. If the p2 instruction of this OP_Halt
 78767  78953         ** instruction is set to OE_Ignore, then the sub-program is throwing
 78768  78954         ** an IGNORE exception. In this case jump to the address specified
 78769  78955         ** as the p2 of the calling OP_Program.  */
 78770  78956         pcx = p->aOp[pcx].p2-1;
................................................................................
 78991  79177   
 78992  79178     assert( pOp->p1>0 && pOp->p1<=p->nVar );
 78993  79179     assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
 78994  79180     pVar = &p->aVar[pOp->p1 - 1];
 78995  79181     if( sqlite3VdbeMemTooBig(pVar) ){
 78996  79182       goto too_big;
 78997  79183     }
 78998         -  pOut = out2Prerelease(p, pOp);
        79184  +  pOut = &aMem[pOp->p2];
 78999  79185     sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
 79000  79186     UPDATE_MAX_BLOBSIZE(pOut);
 79001  79187     break;
 79002  79188   }
 79003  79189   
 79004  79190   /* Opcode: Move P1 P2 P3 * *
 79005  79191   ** Synopsis: r[P2@P3]=r[P1@P3]
................................................................................
 79478  79664     for(i=0; i<pCtx->argc; i++){
 79479  79665       assert( memIsValid(pCtx->argv[i]) );
 79480  79666       REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
 79481  79667     }
 79482  79668   #endif
 79483  79669     MemSetTypeFlag(pCtx->pOut, MEM_Null);
 79484  79670     pCtx->fErrorOrAux = 0;
 79485         -  db->lastRowid = lastRowid;
 79486  79671     (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
 79487         -  lastRowid = db->lastRowid;  /* Remember rowid changes made by xSFunc */
 79488  79672   
 79489  79673     /* If the function returned an error, throw an exception */
 79490  79674     if( pCtx->fErrorOrAux ){
 79491  79675       if( pCtx->isError ){
 79492  79676         sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
 79493  79677         rc = pCtx->isError;
 79494  79678       }
................................................................................
 79936  80120     if( iCompare!=0 ) goto jump_to_p2;
 79937  80121     break;
 79938  80122   }
 79939  80123   
 79940  80124   
 79941  80125   /* Opcode: Permutation * * * P4 *
 79942  80126   **
 79943         -** Set the permutation used by the OP_Compare operator to be the array
 79944         -** 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.
 79945  80129   **
 79946  80130   ** The permutation is only valid until the next OP_Compare that has
 79947  80131   ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 
 79948  80132   ** occur immediately prior to the OP_Compare.
 79949  80133   **
 79950  80134   ** The first integer in the P4 integer array is the length of the array
 79951  80135   ** and does not become part of the permutation.
 79952  80136   */
 79953  80137   case OP_Permutation: {
 79954  80138     assert( pOp->p4type==P4_INTARRAY );
 79955  80139     assert( pOp->p4.ai );
 79956         -  aPermute = pOp->p4.ai + 1;
        80140  +  assert( pOp[1].opcode==OP_Compare );
        80141  +  assert( pOp[1].p5 & OPFLAG_PERMUTE );
 79957  80142     break;
 79958  80143   }
 79959  80144   
 79960  80145   /* Opcode: Compare P1 P2 P3 P4 P5
 79961  80146   ** Synopsis: r[P1@P3] <-> r[P2@P3]
 79962  80147   **
 79963  80148   ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
................................................................................
 79982  80167     int i;
 79983  80168     int p1;
 79984  80169     int p2;
 79985  80170     const KeyInfo *pKeyInfo;
 79986  80171     int idx;
 79987  80172     CollSeq *pColl;    /* Collating sequence to use on this term */
 79988  80173     int bRev;          /* True for DESCENDING sort order */
        80174  +  int *aPermute;     /* The permutation */
 79989  80175   
 79990         -  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  +  }
 79991  80185     n = pOp->p3;
 79992  80186     pKeyInfo = pOp->p4.pKeyInfo;
 79993  80187     assert( n>0 );
 79994  80188     assert( pKeyInfo!=0 );
 79995  80189     p1 = pOp->p1;
 79996  80190     p2 = pOp->p2;
 79997  80191   #if SQLITE_DEBUG
................................................................................
 80016  80210       bRev = pKeyInfo->aSortOrder[i];
 80017  80211       iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
 80018  80212       if( iCompare ){
 80019  80213         if( bRev ) iCompare = -iCompare;
 80020  80214         break;
 80021  80215       }
 80022  80216     }
 80023         -  aPermute = 0;
 80024  80217     break;
 80025  80218   }
 80026  80219   
 80027  80220   /* Opcode: Jump P1 P2 P3 * *
 80028  80221   **
 80029  80222   ** Jump to the instruction at address P1, P2, or P3 depending on whether
 80030  80223   ** in the most recent OP_Compare instruction the P1 vector was less than
................................................................................
 80572  80765     if( zAffinity ){
 80573  80766       pRec = pData0;
 80574  80767       do{
 80575  80768         applyAffinity(pRec++, *(zAffinity++), encoding);
 80576  80769         assert( zAffinity[0]==0 || pRec<=pLast );
 80577  80770       }while( zAffinity[0] );
 80578  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
 80579  80786   
 80580  80787     /* Loop through the elements that will make up the record to figure
 80581  80788     ** out how much space is required for the new record.
 80582  80789     */
 80583  80790     pRec = pLast;
 80584  80791     do{
 80585  80792       assert( memIsValid(pRec) );
................................................................................
 82162  82369     pData = &aMem[pOp->p2];
 82163  82370     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 82164  82371     assert( memIsValid(pData) );
 82165  82372     pC = p->apCsr[pOp->p1];
 82166  82373     assert( pC!=0 );
 82167  82374     assert( pC->eCurType==CURTYPE_BTREE );
 82168  82375     assert( pC->uc.pCursor!=0 );
 82169         -  assert( pC->isTable );
        82376  +  assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
 82170  82377     assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
 82171  82378     REGISTER_TRACE(pOp->p2, pData);
 82172  82379   
 82173  82380     if( pOp->opcode==OP_Insert ){
 82174  82381       pKey = &aMem[pOp->p3];
 82175  82382       assert( pKey->flags & MEM_Int );
 82176  82383       assert( memIsValid(pKey) );
................................................................................
 82178  82385       x.nKey = pKey->u.i;
 82179  82386     }else{
 82180  82387       assert( pOp->opcode==OP_InsertInt );
 82181  82388       x.nKey = pOp->p3;
 82182  82389     }
 82183  82390   
 82184  82391     if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
 82185         -    assert( pC->isTable );
 82186  82392       assert( pC->iDb>=0 );
 82187  82393       zDb = db->aDb[pC->iDb].zDbSName;
 82188  82394       pTab = pOp->p4.pTab;
 82189         -    assert( HasRowid(pTab) );
        82395  +    assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
 82190  82396       op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
 82191  82397     }else{
 82192         -    pTab = 0; /* Not needed.  Silence a comiler warning. */
        82398  +    pTab = 0; /* Not needed.  Silence a compiler warning. */
 82193  82399       zDb = 0;  /* Not needed.  Silence a compiler warning. */
 82194  82400     }
 82195  82401   
 82196  82402   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 82197  82403     /* Invoke the pre-update hook, if any */
 82198  82404     if( db->xPreUpdateCallback 
 82199  82405      && pOp->p4type==P4_TABLE
 82200  82406      && !(pOp->p5 & OPFLAG_ISUPDATE)
 82201  82407     ){
 82202  82408       sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
 82203  82409     }
        82410  +  if( pOp->p5 & OPFLAG_ISNOOP ) break;
 82204  82411   #endif
 82205  82412   
 82206  82413     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 82207         -  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = x.nKey;
        82414  +  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
 82208  82415     if( pData->flags & MEM_Null ){
 82209  82416       x.pData = 0;
 82210  82417       x.nData = 0;
 82211  82418     }else{
 82212  82419       assert( pData->flags & (MEM_Blob|MEM_Str) );
 82213  82420       x.pData = pData->z;
 82214  82421       x.nData = pData->n;
................................................................................
 82217  82424     if( pData->flags & MEM_Zero ){
 82218  82425       x.nZero = pData->u.nZero;
 82219  82426     }else{
 82220  82427       x.nZero = 0;
 82221  82428     }
 82222  82429     x.pKey = 0;
 82223  82430     rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
 82224         -                          (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
        82431  +      (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
 82225  82432     );
 82226  82433     pC->deferredMoveto = 0;
 82227  82434     pC->cacheStatus = CACHE_STALE;
 82228  82435   
 82229  82436     /* Invoke the update-hook if required. */
 82230  82437     if( rc ) goto abort_due_to_error;
 82231  82438     if( db->xUpdateCallback && op ){
................................................................................
 82309  82516     }else{
 82310  82517       zDb = 0;   /* Not needed.  Silence a compiler warning. */
 82311  82518       pTab = 0;  /* Not needed.  Silence a compiler warning. */
 82312  82519     }
 82313  82520   
 82314  82521   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 82315  82522     /* Invoke the pre-update-hook if required. */
 82316         -  if( db->xPreUpdateCallback && pOp->p4.pTab && HasRowid(pTab) ){
 82317         -    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  +    );
 82318  82528       sqlite3VdbePreUpdateHook(p, pC,
 82319  82529           (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, 
 82320  82530           zDb, pTab, pC->movetoTarget,
 82321  82531           pOp->p3
 82322  82532       );
 82323  82533     }
 82324  82534     if( opflags & OPFLAG_ISNOOP ) break;
................................................................................
 82428  82638     assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
 82429  82639     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 82430  82640     if( rc ) goto abort_due_to_error;
 82431  82641     p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
 82432  82642     break;
 82433  82643   }
 82434  82644   
 82435         -/* Opcode: RowData P1 P2 * * *
        82645  +/* Opcode: RowData P1 P2 P3 * *
 82436  82646   ** Synopsis: r[P2]=data
 82437  82647   **
 82438  82648   ** Write into register P2 the complete row content for the row at 
 82439  82649   ** which cursor P1 is currently pointing.
 82440  82650   ** There is no interpretation of the data.  
 82441  82651   ** It is just copied onto the P2 register exactly as 
 82442  82652   ** it is found in the database file.
 82443  82653   **
 82444  82654   ** If cursor P1 is an index, then the content is the key of the row.
 82445  82655   ** If cursor P2 is a table, then the content extracted is the data.
 82446  82656   **
 82447  82657   ** If the P1 cursor must be pointing to a valid row (not a NULL row)
 82448  82658   ** of a real table, not a pseudo-table.
        82659  +**
        82660  +** If P3!=0 then this opcode is allowed to make an ephermeral pointer
        82661  +** into the database page.  That means that the content of the output
        82662  +** register will be invalidated as soon as the cursor moves - including
        82663  +** moves caused by other cursors that "save" the the current cursors
        82664  +** position in order that they can write to the same table.  If P3==0
        82665  +** then a copy of the data is made into memory.  P3!=0 is faster, but
        82666  +** P3==0 is safer.
        82667  +**
        82668  +** If P3!=0 then the content of the P2 register is unsuitable for use
        82669  +** in OP_Result and any OP_Result will invalidate the P2 register content.
        82670  +** The P2 register content is invalidated by opcodes like OP_Function or
        82671  +** by any use of another cursor pointing to the same table.
 82449  82672   */
 82450  82673   case OP_RowData: {
 82451  82674     VdbeCursor *pC;
 82452  82675     BtCursor *pCrsr;
 82453  82676     u32 n;
 82454  82677   
 82455         -  pOut = &aMem[pOp->p2];
 82456         -  memAboutToChange(p, pOut);
        82678  +  pOut = out2Prerelease(p, pOp);
 82457  82679   
 82458  82680     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 82459  82681     pC = p->apCsr[pOp->p1];
 82460  82682     assert( pC!=0 );
 82461  82683     assert( pC->eCurType==CURTYPE_BTREE );
 82462  82684     assert( isSorter(pC)==0 );
 82463  82685     assert( pC->nullRow==0 );
................................................................................
 82480  82702   #endif
 82481  82703   
 82482  82704     n = sqlite3BtreePayloadSize(pCrsr);
 82483  82705     if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 82484  82706       goto too_big;
 82485  82707     }
 82486  82708     testcase( n==0 );
 82487         -  if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){
 82488         -    goto no_mem;
 82489         -  }
 82490         -  pOut->n = n;
 82491         -  MemSetTypeFlag(pOut, MEM_Blob);
 82492         -  rc = sqlite3BtreePayload(pCrsr, 0, n, pOut->z);
        82709  +  rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
 82493  82710     if( rc ) goto abort_due_to_error;
 82494         -  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
        82711  +  if( !pOp->p3 ) Deephemeralize(pOut);
 82495  82712     UPDATE_MAX_BLOBSIZE(pOut);
 82496  82713     REGISTER_TRACE(pOp->p2, pOut);
 82497  82714     break;
 82498  82715   }
 82499  82716   
 82500  82717   /* Opcode: Rowid P1 P2 * * *
 82501  82718   ** Synopsis: r[P2]=rowid
................................................................................
 82875  83092       rc = sqlite3VdbeSorterWrite(pC, pIn2);
 82876  83093     }else{
 82877  83094       x.nKey = pIn2->n;
 82878  83095       x.pKey = pIn2->z;
 82879  83096       x.aMem = aMem + pOp->p3;
 82880  83097       x.nMem = (u16)pOp->p4.i;
 82881  83098       rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
 82882         -         (pOp->p5 & OPFLAG_APPEND)!=0, 
        83099  +         (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), 
 82883  83100           ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
 82884  83101           );
 82885  83102       assert( pC->deferredMoveto==0 );
 82886  83103       pC->cacheStatus = CACHE_STALE;
 82887  83104     }
 82888  83105     if( rc) goto abort_due_to_error;
 82889  83106     break;
................................................................................
 82997  83214         pTabCur->deferredMoveto = 1;
 82998  83215         assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
 82999  83216         pTabCur->aAltMap = pOp->p4.ai;
 83000  83217         pTabCur->pAltCursor = pC;
 83001  83218       }else{
 83002  83219         pOut = out2Prerelease(p, pOp);
 83003  83220         pOut->u.i = rowid;
 83004         -      pOut->flags = MEM_Int;
 83005  83221       }
 83006  83222     }else{
 83007  83223       assert( pOp->opcode==OP_IdxRowid );
 83008  83224       sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
 83009  83225     }
 83010  83226     break;
 83011  83227   }
................................................................................
 83639  83855           || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
 83640  83856       assert( pProgram->nCsr==pFrame->nChildCsr );
 83641  83857       assert( (int)(pOp - aOp)==pFrame->pc );
 83642  83858     }
 83643  83859   
 83644  83860     p->nFrame++;
 83645  83861     pFrame->pParent = p->pFrame;
 83646         -  pFrame->lastRowid = lastRowid;
        83862  +  pFrame->lastRowid = db->lastRowid;
 83647  83863     pFrame->nChange = p->nChange;
 83648  83864     pFrame->nDbChange = p->db->nChange;
 83649  83865     assert( pFrame->pAuxData==0 );
 83650  83866     pFrame->pAuxData = p->pAuxData;
 83651  83867     p->pAuxData = 0;
 83652  83868     p->nChange = 0;
 83653  83869     p->pFrame = pFrame;
................................................................................
 84580  84796       }
 84581  84797       db->vtabOnConflict = pOp->p5;
 84582  84798       rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
 84583  84799       db->vtabOnConflict = vtabOnConflict;
 84584  84800       sqlite3VtabImportErrmsg(p, pVtab);
 84585  84801       if( rc==SQLITE_OK && pOp->p1 ){
 84586  84802         assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
 84587         -      db->lastRowid = lastRowid = rowid;
        84803  +      db->lastRowid = rowid;
 84588  84804       }
 84589  84805       if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
 84590  84806         if( pOp->p5==OE_Ignore ){
 84591  84807           rc = SQLITE_OK;
 84592  84808         }else{
 84593  84809           p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
 84594  84810         }
................................................................................
 84816  85032       sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
 84817  85033     }
 84818  85034   
 84819  85035     /* This is the only way out of this procedure.  We have to
 84820  85036     ** release the mutexes on btrees that were acquired at the
 84821  85037     ** top. */
 84822  85038   vdbe_return:
 84823         -  db->lastRowid = lastRowid;
 84824  85039     testcase( nVmStep>0 );
 84825  85040     p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
 84826  85041     sqlite3VdbeLeave(p);
 84827  85042     assert( rc!=SQLITE_OK || nExtraDelete==0 
 84828  85043          || sqlite3_strlike("DELETE%",p->zSql,0)!=0 
 84829  85044     );
 84830  85045     return rc;
................................................................................
 84880  85095   #ifndef SQLITE_OMIT_INCRBLOB
 84881  85096   
 84882  85097   /*
 84883  85098   ** Valid sqlite3_blob* handles point to Incrblob structures.
 84884  85099   */
 84885  85100   typedef struct Incrblob Incrblob;
 84886  85101   struct Incrblob {
 84887         -  int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
 84888  85102     int nByte;              /* Size of open blob, in bytes */
 84889  85103     int iOffset;            /* Byte offset of blob in cursor data */
 84890         -  int iCol;               /* Table column this handle is open on */
        85104  +  u16 iCol;               /* Table column this handle is open on */
 84891  85105     BtCursor *pCsr;         /* Cursor pointing at blob row */
 84892  85106     sqlite3_stmt *pStmt;    /* Statement holding cursor open */
 84893  85107     sqlite3 *db;            /* The associated database */
 84894  85108     char *zDb;              /* Database name */
 84895  85109     Table *pTab;            /* Table object */
 84896  85110   };
 84897  85111   
................................................................................
 84914  85128   ** immediately return SQLITE_ABORT.
 84915  85129   */
 84916  85130   static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
 84917  85131     int rc;                         /* Error code */
 84918  85132     char *zErr = 0;                 /* Error message */
 84919  85133     Vdbe *v = (Vdbe *)p->pStmt;
 84920  85134   
 84921         -  /* Set the value of the SQL statements only variable to integer iRow. 
 84922         -  ** This is done directly instead of using sqlite3_bind_int64() to avoid 
 84923         -  ** 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
 84924  85137     */
 84925         -  assert( v->aVar[0].flags&MEM_Int );
 84926         -  v->aVar[0].u.i = iRow;
        85138  +  v->aMem[1].flags = MEM_Int;
        85139  +  v->aMem[1].u.i = iRow;
 84927  85140   
 84928         -  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  +  }
 84929  85151     if( rc==SQLITE_ROW ){
 84930  85152       VdbeCursor *pC = v->apCsr[0];
 84931         -    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 );
 84932  85156       if( type<12 ){
 84933  85157         zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
 84934  85158             type==0?"null": type==7?"real": "integer"
 84935  85159         );
 84936  85160         rc = SQLITE_ERROR;
 84937  85161         sqlite3_finalize(p->pStmt);
 84938  85162         p->pStmt = 0;
................................................................................
 84969  85193   */
 84970  85194   SQLITE_API int sqlite3_blob_open(
 84971  85195     sqlite3* db,            /* The database connection */
 84972  85196     const char *zDb,        /* The attached database containing the blob */
 84973  85197     const char *zTable,     /* The table containing the blob */
 84974  85198     const char *zColumn,    /* The column containing the blob */
 84975  85199     sqlite_int64 iRow,      /* The row containing the glob */
 84976         -  int flags,              /* True -> read/write access, false -> read-only */
        85200  +  int wrFlag,             /* True -> read/write access, false -> read-only */
 84977  85201     sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
 84978  85202   ){
 84979  85203     int nAttempt = 0;
 84980  85204     int iCol;               /* Index of zColumn in row-record */
 84981  85205     int rc = SQLITE_OK;
 84982  85206     char *zErr = 0;
 84983  85207     Table *pTab;
................................................................................
 84991  85215   #endif
 84992  85216     *ppBlob = 0;
 84993  85217   #ifdef SQLITE_ENABLE_API_ARMOR
 84994  85218     if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
 84995  85219       return SQLITE_MISUSE_BKPT;
 84996  85220     }
 84997  85221   #endif
 84998         -  flags = !!flags;                /* flags = (flags ? 1 : 0); */
        85222  +  wrFlag = !!wrFlag;                /* wrFlag = (wrFlag ? 1 : 0); */
 84999  85223   
 85000  85224     sqlite3_mutex_enter(db->mutex);
 85001  85225   
 85002  85226     pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
 85003  85227     if( !pBlob ) goto blob_open_out;
 85004  85228     pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
 85005  85229     if( !pParse ) goto blob_open_out;
................................................................................
 85051  85275         rc = SQLITE_ERROR;
 85052  85276         sqlite3BtreeLeaveAll(db);
 85053  85277         goto blob_open_out;
 85054  85278       }
 85055  85279   
 85056  85280       /* If the value is being opened for writing, check that the
 85057  85281       ** column is not indexed, and that it is not part of a foreign key. 
 85058         -    ** It is against the rules to open a column to which either of these
 85059         -    ** descriptions applies for writing.  */
 85060         -    if( flags ){
        85282  +    */
        85283  +    if( wrFlag ){
 85061  85284         const char *zFault = 0;
 85062  85285         Index *pIdx;
 85063  85286   #ifndef SQLITE_OMIT_FOREIGN_KEY
 85064  85287         if( db->flags&SQLITE_ForeignKeys ){
 85065  85288           /* Check that the column is not part of an FK child key definition. It
 85066  85289           ** is not necessary to check if it is part of a parent key, as parent
 85067  85290           ** key columns must be indexed. The check below will pick up this 
................................................................................
 85114  85337         ** which closes the b-tree cursor and (possibly) commits the 
 85115  85338         ** transaction.
 85116  85339         */
 85117  85340         static const int iLn = VDBE_OFFSET_LINENO(2);
 85118  85341         static const VdbeOpList openBlob[] = {
 85119  85342           {OP_TableLock,      0, 0, 0},  /* 0: Acquire a read or write lock */
 85120  85343           {OP_OpenRead,       0, 0, 0},  /* 1: Open a cursor */
 85121         -        {OP_Variable,       1, 1, 0},  /* 2: Move ?1 into reg[1] */
 85122         -        {OP_NotExists,      0, 7, 1},  /* 3: Seek the cursor */
 85123         -        {OP_Column,         0, 0, 1},  /* 4  */
 85124         -        {OP_ResultRow,      1, 0, 0},  /* 5  */
 85125         -        {OP_Goto,           0, 2, 0},  /* 6  */
 85126         -        {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  */
 85127  85349         };
 85128  85350         Vdbe *v = (Vdbe *)pBlob->pStmt;
 85129  85351         int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 85130  85352         VdbeOp *aOp;
 85131  85353   
 85132         -      sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, 
        85354  +      sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag, 
 85133  85355                              pTab->pSchema->schema_cookie,
 85134  85356                              pTab->pSchema->iGeneration);
 85135  85357         sqlite3VdbeChangeP5(v, 1);     
 85136  85358         aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
 85137  85359   
 85138  85360         /* Make sure a mutex is held on the table to be accessed */
 85139  85361         sqlite3VdbeUsesBtree(v, iDb); 
................................................................................
 85142  85364           assert( aOp!=0 );
 85143  85365           /* Configure the OP_TableLock instruction */
 85144  85366   #ifdef SQLITE_OMIT_SHARED_CACHE
 85145  85367           aOp[0].opcode = OP_Noop;
 85146  85368   #else
 85147  85369           aOp[0].p1 = iDb;
 85148  85370           aOp[0].p2 = pTab->tnum;
 85149         -        aOp[0].p3 = flags;
        85371  +        aOp[0].p3 = wrFlag;
 85150  85372           sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
 85151  85373         }
 85152  85374         if( db->mallocFailed==0 ){
 85153  85375   #endif
 85154  85376   
 85155  85377           /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
 85156  85378           ** parameter of the other to pTab->tnum.  */
 85157         -        if( flags ) aOp[1].opcode = OP_OpenWrite;
        85379  +        if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
 85158  85380           aOp[1].p2 = pTab->tnum;
 85159  85381           aOp[1].p3 = iDb;   
 85160  85382   
 85161  85383           /* Configure the number of columns. Configure the cursor to
 85162  85384           ** think that the table has one more column than it really
 85163  85385           ** does. An OP_Column to retrieve this imaginary column will
 85164  85386           ** always return an SQL NULL. This is useful because it means
 85165  85387           ** we can invoke OP_Column to fill in the vdbe cursors type 
 85166  85388           ** and offset cache without causing any IO.
 85167  85389           */
 85168  85390           aOp[1].p4type = P4_INT32;
 85169  85391           aOp[1].p4.i = pTab->nCol+1;
 85170         -        aOp[4].p2 = pTab->nCol;
        85392  +        aOp[3].p2 = pTab->nCol;
 85171  85393   
 85172         -        pParse->nVar = 1;
        85394  +        pParse->nVar = 0;
 85173  85395           pParse->nMem = 1;
 85174  85396           pParse->nTab = 1;
 85175  85397           sqlite3VdbeMakeReady(v, pParse);
 85176  85398         }
 85177  85399       }
 85178  85400      
 85179         -    pBlob->flags = flags;
 85180  85401       pBlob->iCol = iCol;
 85181  85402       pBlob->db = db;
 85182  85403       sqlite3BtreeLeaveAll(db);
 85183  85404       if( db->mallocFailed ){
 85184  85405         goto blob_open_out;
 85185  85406       }
 85186         -    sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
 85187  85407       rc = blobSeekToRow(pBlob, iRow, &zErr);
 85188  85408     } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
 85189  85409   
 85190  85410   blob_open_out:
 85191  85411     if( rc==SQLITE_OK && db->mallocFailed==0 ){
 85192  85412       *ppBlob = (sqlite3_blob *)pBlob;
 85193  85413     }else{
................................................................................
 88716  88936   *************************************************************************
 88717  88937   **
 88718  88938   ** This file contains routines used for walking the parser tree and
 88719  88939   ** resolve all identifiers by associating them with a particular
 88720  88940   ** table and column.
 88721  88941   */
 88722  88942   /* #include "sqliteInt.h" */
 88723         -/* #include <stdlib.h> */
 88724         -/* #include <string.h> */
 88725  88943   
 88726  88944   /*
 88727  88945   ** Walk the expression tree pExpr and increase the aggregate function
 88728  88946   ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
 88729  88947   ** This needs to occur when copying a TK_AGG_FUNCTION node from an
 88730  88948   ** outer query into an inner subquery.
 88731  88949   **
................................................................................
 90476  90694             pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
 90477  90695     assert( pExpr->pLeft );
 90478  90696     aff = sqlite3ExprAffinity(pExpr->pLeft);
 90479  90697     if( pExpr->pRight ){
 90480  90698       aff = sqlite3CompareAffinity(pExpr->pRight, aff);
 90481  90699     }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 90482  90700       aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
 90483         -  }else if( NEVER(aff==0) ){
        90701  +  }else if( aff==0 ){
 90484  90702       aff = SQLITE_AFF_BLOB;
 90485  90703     }
 90486  90704     return aff;
 90487  90705   }
 90488  90706   
 90489  90707   /*
 90490  90708   ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
................................................................................
 91212  91430       x = (ynVar)(++pParse->nVar);
 91213  91431     }else{
 91214  91432       int doAdd = 0;
 91215  91433       if( z[0]=='?' ){
 91216  91434         /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
 91217  91435         ** use it as the variable number */
 91218  91436         i64 i;
 91219         -      int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
 91220         -      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  +      }
 91221  91444         testcase( i==0 );
 91222  91445         testcase( i==1 );
 91223  91446         testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
 91224  91447         testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
 91225  91448         if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
 91226  91449           sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
 91227  91450               db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
 91228  91451           return;
 91229  91452         }
        91453  +      x = (ynVar)i;
 91230  91454         if( x>pParse->nVar ){
 91231  91455           pParse->nVar = (int)x;
 91232  91456           doAdd = 1;
 91233  91457         }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
 91234  91458           doAdd = 1;
 91235  91459         }
 91236  91460       }else{
................................................................................
 91657  91881       struct IdList_item *pNewItem = &pNew->a[i];
 91658  91882       struct IdList_item *pOldItem = &p->a[i];
 91659  91883       pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 91660  91884       pNewItem->idx = pOldItem->idx;
 91661  91885     }
 91662  91886     return pNew;
 91663  91887   }
 91664         -SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
 91665         -  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  +
 91666  91894     assert( db!=0 );
 91667         -  if( p==0 ) return 0;
 91668         -  pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
 91669         -  if( pNew==0 ) return 0;
 91670         -  pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
 91671         -  pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
 91672         -  pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
 91673         -  pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
 91674         -  pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
 91675         -  pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
 91676         -  pNew->op = p->op;
 91677         -  pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
 91678         -  if( pPrior ) pPrior->pNext = pNew;
 91679         -  pNew->pNext = 0;
 91680         -  pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
 91681         -  pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
 91682         -  pNew->iLimit = 0;
 91683         -  pNew->iOffset = 0;
 91684         -  pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
 91685         -  pNew->addrOpenEphm[0] = -1;
 91686         -  pNew->addrOpenEphm[1] = -1;
 91687         -  pNew->nSelectRow = p->nSelectRow;
 91688         -  pNew->pWith = withDup(db, p->pWith);
 91689         -  sqlite3SelectSetName(pNew, p->zSelName);
 91690         -  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;
 91691  91923   }
 91692  91924   #else
 91693  91925   SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
 91694  91926     assert( p==0 );
 91695  91927     return 0;
 91696  91928   }
 91697  91929   #endif
................................................................................
 91748  91980   ** pColumns and pExpr form a vector assignment which is part of the SET
 91749  91981   ** clause of an UPDATE statement.  Like this:
 91750  91982   **
 91751  91983   **        (a,b,c) = (expr1,expr2,expr3)
 91752  91984   ** Or:    (a,b,c) = (SELECT x,y,z FROM ....)
 91753  91985   **
 91754  91986   ** For each term of the vector assignment, append new entries to the
 91755         -** expression list pList.  In the case of a subquery on the LHS, append
        91987  +** expression list pList.  In the case of a subquery on the RHS, append
 91756  91988   ** TK_SELECT_COLUMN expressions.
 91757  91989   */
 91758  91990   SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
 91759  91991     Parse *pParse,         /* Parsing context */
 91760  91992     ExprList *pList,       /* List to which to append. Might be NULL */
 91761  91993     IdList *pColumns,      /* List of names of LHS of the assignment */
 91762  91994     Expr *pExpr            /* Vector expression to be appended. Might be NULL */
................................................................................
 93857  94089         const char *zId;       /* The function name */
 93858  94090         u32 constMask = 0;     /* Mask of function arguments that are constant */
 93859  94091         int i;                 /* Loop counter */
 93860  94092         sqlite3 *db = pParse->db;  /* The database connection */
 93861  94093         u8 enc = ENC(db);      /* The text encoding used by this database */
 93862  94094         CollSeq *pColl = 0;    /* A collating sequence */
 93863  94095   
        94096  +      if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
        94097  +        /* SQL functions can be expensive. So try to move constant functions
        94098  +        ** out of the inner loop, even if that means an extra OP_Copy. */
        94099  +        return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
        94100  +      }
 93864  94101         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 93865  94102         if( ExprHasProperty(pExpr, EP_TokenOnly) ){
 93866  94103           pFarg = 0;
 93867  94104         }else{
 93868  94105           pFarg = pExpr->x.pList;
 93869  94106         }
 93870  94107         nFarg = pFarg ? pFarg->nExpr : 0;
................................................................................
 93904  94141         /* The UNLIKELY() function is a no-op.  The result is the value
 93905  94142         ** of the first argument.
 93906  94143         */
 93907  94144         if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
 93908  94145           assert( nFarg>=1 );
 93909  94146           return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
 93910  94147         }
        94148  +
        94149  +#ifdef SQLITE_DEBUG
        94150  +      /* The AFFINITY() function evaluates to a string that describes
        94151  +      ** the type affinity of the argument.  This is used for testing of
        94152  +      ** the SQLite type logic.
        94153  +      */
        94154  +      if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
        94155  +        const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
        94156  +        char aff;
        94157  +        assert( nFarg==1 );
        94158  +        aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
        94159  +        sqlite3VdbeLoadString(v, target, 
        94160  +                              aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
        94161  +        return target;
        94162  +      }
        94163  +#endif
 93911  94164   
 93912  94165         for(i=0; i<nFarg; i++){
 93913  94166           if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
 93914  94167             testcase( i==31 );
 93915  94168             constMask |= MASKBIT32(i);
 93916  94169           }
 93917  94170           if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
................................................................................
 94221  94474     sqlite3ReleaseTempReg(pParse, regFree1);
 94222  94475     sqlite3ReleaseTempReg(pParse, regFree2);
 94223  94476     return inReg;
 94224  94477   }
 94225  94478   
 94226  94479   /*
 94227  94480   ** Factor out the code of the given expression to initialization time.
        94481  +**
        94482  +** If regDest>=0 then the result is always stored in that register and the
        94483  +** result is not reusable.  If regDest<0 then this routine is free to 
        94484  +** store the value whereever it wants.  The register where the expression 
        94485  +** is stored is returned.  When regDest<0, two identical expressions will
        94486  +** code to the same register.
 94228  94487   */
 94229         -SQLITE_PRIVATE void sqlite3ExprCodeAtInit(
        94488  +SQLITE_PRIVATE int sqlite3ExprCodeAtInit(
 94230  94489     Parse *pParse,    /* Parsing context */
 94231  94490     Expr *pExpr,      /* The expression to code when the VDBE initializes */
 94232         -  int regDest,      /* Store the value in this register */
 94233         -  u8 reusable       /* True if this expression is reusable */
        94491  +  int regDest       /* Store the value in this register */
 94234  94492   ){
 94235  94493     ExprList *p;
 94236  94494     assert( ConstFactorOk(pParse) );
 94237  94495     p = pParse->pConstExpr;
        94496  +  if( regDest<0 && p ){
        94497  +    struct ExprList_item *pItem;
        94498  +    int i;
        94499  +    for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
        94500  +      if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
        94501  +        return pItem->u.iConstExprReg;
        94502  +      }
        94503  +    }
        94504  +  }
 94238  94505     pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
 94239  94506     p = sqlite3ExprListAppend(pParse, p, pExpr);
 94240  94507     if( p ){
 94241  94508        struct ExprList_item *pItem = &p->a[p->nExpr-1];
        94509  +     pItem->reusable = regDest<0;
        94510  +     if( regDest<0 ) regDest = ++pParse->nMem;
 94242  94511        pItem->u.iConstExprReg = regDest;
 94243         -     pItem->reusable = reusable;
 94244  94512     }
 94245  94513     pParse->pConstExpr = p;
        94514  +  return regDest;
 94246  94515   }
 94247  94516   
 94248  94517   /*
 94249  94518   ** Generate code to evaluate an expression and store the results
 94250  94519   ** into a register.  Return the register number where the results
 94251  94520   ** are stored.
 94252  94521   **
................................................................................
 94261  94530   SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
 94262  94531     int r2;
 94263  94532     pExpr = sqlite3ExprSkipCollate(pExpr);
 94264  94533     if( ConstFactorOk(pParse)
 94265  94534      && pExpr->op!=TK_REGISTER
 94266  94535      && sqlite3ExprIsConstantNotJoin(pExpr)
 94267  94536     ){
 94268         -    ExprList *p = pParse->pConstExpr;
 94269         -    int i;
 94270  94537       *pReg  = 0;
 94271         -    if( p ){
 94272         -      struct ExprList_item *pItem;
 94273         -      for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
 94274         -        if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
 94275         -          return pItem->u.iConstExprReg;
 94276         -        }
 94277         -      }
 94278         -    }
 94279         -    r2 = ++pParse->nMem;
 94280         -    sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
        94538  +    r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
 94281  94539     }else{
 94282  94540       int r1 = sqlite3GetTempReg(pParse);
 94283  94541       r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
 94284  94542       if( r2==r1 ){
 94285  94543         *pReg = r1;
 94286  94544       }else{
 94287  94545         sqlite3ReleaseTempReg(pParse, r1);
................................................................................
 94327  94585   ** Generate code that will evaluate expression pExpr and store the
 94328  94586   ** results in register target.  The results are guaranteed to appear
 94329  94587   ** in register target.  If the expression is constant, then this routine
 94330  94588   ** might choose to code the expression at initialization time.
 94331  94589   */
 94332  94590   SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
 94333  94591     if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
 94334         -    sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
        94592  +    sqlite3ExprCodeAtInit(pParse, pExpr, target);
 94335  94593     }else{
 94336  94594       sqlite3ExprCode(pParse, pExpr, target);
 94337  94595     }
 94338  94596   }
 94339  94597   
 94340  94598   /*
 94341  94599   ** Generate code that evaluates the given expression and puts the result
................................................................................
 94399  94657         if( flags & SQLITE_ECEL_OMITREF ){
 94400  94658           i--;
 94401  94659           n--;
 94402  94660         }else{
 94403  94661           sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
 94404  94662         }
 94405  94663       }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
 94406         -      sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
        94664  +      sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
 94407  94665       }else{
 94408  94666         int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
 94409  94667         if( inReg!=target+i ){
 94410  94668           VdbeOp *pOp;
 94411  94669           if( copyOp==OP_Copy
 94412  94670            && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
 94413  94671            && pOp->p1+pOp->p3+1==inReg
................................................................................
 96943  97201   /*
 96944  97202   ** Implementation of the stat_get(P,J) SQL function.  This routine is
 96945  97203   ** used to query statistical information that has been gathered into
 96946  97204   ** the Stat4Accum object by prior calls to stat_push().  The P parameter
 96947  97205   ** has type BLOB but it is really just a pointer to the Stat4Accum object.
 96948  97206   ** The content to returned is determined by the parameter J
 96949  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.
 96950  97214   **
 96951  97215   ** If neither STAT3 nor STAT4 are enabled, then J is always
 96952  97216   ** STAT_GET_STAT1 and is hence omitted and this routine becomes
 96953  97217   ** a one-parameter function, stat_get(P), that always returns the
 96954  97218   ** stat1 table entry information.
 96955  97219   */
 96956  97220   static void statGet(
................................................................................
 97762  98026            || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] 
 97763  98027           ){
 97764  98028             sumEq += aSample[i].anEq[iCol];
 97765  98029             nSum100 += 100;
 97766  98030           }
 97767  98031         }
 97768  98032   
 97769         -      if( nDist100>nSum100 ){
        98033  +      if( nDist100>nSum100 && sumEq<nRow ){
 97770  98034           avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
 97771  98035         }
 97772  98036         if( avgEq==0 ) avgEq = 1;
 97773  98037         pIdx->aAvgEq[iCol] = avgEq;
 97774  98038       }
 97775  98039     }
 97776  98040   }
................................................................................
 98176  98440       return;
 98177  98441     }
 98178  98442     assert( pVfs );
 98179  98443     flags |= SQLITE_OPEN_MAIN_DB;
 98180  98444     rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
 98181  98445     sqlite3_free( zPath );
 98182  98446     db->nDb++;
        98447  +  db->skipBtreeMutex = 0;
 98183  98448     if( rc==SQLITE_CONSTRAINT ){
 98184  98449       rc = SQLITE_ERROR;
 98185  98450       zErrDyn = sqlite3MPrintf(db, "database is already attached");
 98186  98451     }else if( rc==SQLITE_OK ){
 98187  98452       Pager *pPager;
 98188  98453       aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
 98189  98454       if( !aNew->pSchema ){
................................................................................
104340 104605         if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){
104341 104606           pParse->isMultiWrite = 0;
104342 104607         }
104343 104608       }else
104344 104609   #endif
104345 104610       {
104346 104611         int count = (pParse->nested==0);    /* True to count changes */
104347         -      int iIdxNoSeek = -1;
104348         -      if( bComplex==0 && aiCurOnePass[1]!=iDataCur ){
104349         -        iIdxNoSeek = aiCurOnePass[1];
104350         -      }
104351 104612         sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
104352         -          iKey, nKey, count, OE_Default, eOnePass, iIdxNoSeek);
       104613  +          iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
104353 104614       }
104354 104615     
104355 104616       /* End of the loop over all rowids/primary-keys. */
104356 104617       if( eOnePass!=ONEPASS_OFF ){
104357 104618         sqlite3VdbeResolveLabel(v, addrBypass);
104358 104619         sqlite3WhereEnd(pWInfo);
104359 104620       }else if( pPk ){
................................................................................
104425 104686   **   ONEPASS_MULTI.  If eMode is not ONEPASS_OFF, then the cursor
104426 104687   **   iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
104427 104688   **   then this function must seek iDataCur to the entry identified by iPk
104428 104689   **   and nPk before reading from it.
104429 104690   **
104430 104691   **   If eMode is ONEPASS_MULTI, then this call is being made as part
104431 104692   **   of a ONEPASS delete that affects multiple rows. In this case, if 
104432         -**   iIdxNoSeek is a valid cursor number (>=0), then its position should
104433         -**   be preserved following the delete operation. Or, if iIdxNoSeek is not
104434         -**   a valid cursor number, the position of iDataCur should be preserved
104435         -**   instead.
       104693  +**   iIdxNoSeek is a valid cursor number (>=0) and is not the same as
       104694  +**   iDataCur, then its position should be preserved following the delete
       104695  +**   operation. Or, if iIdxNoSeek is not a valid cursor number, the
       104696  +**   position of iDataCur should be preserved instead.
104436 104697   **
104437 104698   ** iIdxNoSeek:
104438         -**   If iIdxNoSeek is a valid cursor number (>=0), then it identifies an
104439         -**   index cursor (from within array of cursors starting at iIdxCur) that
104440         -**   already points to the index entry to be deleted.
       104699  +**   If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
       104700  +**   then it identifies an index cursor (from within array of cursors
       104701  +**   starting at iIdxCur) that already points to the index entry to be deleted.
       104702  +**   Except, this optimization is disabled if there are BEFORE triggers since
       104703  +**   the trigger body might have moved the cursor.
104441 104704   */
104442 104705   SQLITE_PRIVATE void sqlite3GenerateRowDelete(
104443 104706     Parse *pParse,     /* Parsing context */
104444 104707     Table *pTab,       /* Table containing the row to be deleted */
104445 104708     Trigger *pTrigger, /* List of triggers to (potentially) fire */
104446 104709     int iDataCur,      /* Cursor from which column data is extracted */
104447 104710     int iIdxCur,       /* First index cursor */
................................................................................
104504 104767       addrStart = sqlite3VdbeCurrentAddr(v);
104505 104768       sqlite3CodeRowTrigger(pParse, pTrigger, 
104506 104769           TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
104507 104770       );
104508 104771   
104509 104772       /* If any BEFORE triggers were coded, then seek the cursor to the 
104510 104773       ** row to be deleted again. It may be that the BEFORE triggers moved
104511         -    ** the cursor or of already deleted the row that the cursor was
       104774  +    ** the cursor or already deleted the row that the cursor was
104512 104775       ** pointing to.
       104776  +    **
       104777  +    ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
       104778  +    ** may have moved that cursor.
104513 104779       */
104514 104780       if( addrStart<sqlite3VdbeCurrentAddr(v) ){
104515 104781         sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
104516 104782         VdbeCoverageIf(v, opSeek==OP_NotExists);
104517 104783         VdbeCoverageIf(v, opSeek==OP_NotFound);
       104784  +      testcase( iIdxNoSeek>=0 );
       104785  +      iIdxNoSeek = -1;
104518 104786       }
104519 104787   
104520 104788       /* Do FK processing. This call checks that any FK constraints that
104521 104789       ** refer to this table (i.e. constraints attached to other tables) 
104522 104790       ** are not violated by deleting this row.  */
104523 104791       sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
104524 104792     }
................................................................................
104533 104801     ** the update-hook is not invoked for rows removed by REPLACE, but the 
104534 104802     ** pre-update-hook is.
104535 104803     */ 
104536 104804     if( pTab->pSelect==0 ){
104537 104805       u8 p5 = 0;
104538 104806       sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
104539 104807       sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
104540         -    sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
       104808  +    if( pParse->nested==0 ){
       104809  +      sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
       104810  +    }
104541 104811       if( eMode!=ONEPASS_OFF ){
104542 104812         sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
104543 104813       }
104544         -    if( iIdxNoSeek>=0 ){
       104814  +    if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
104545 104815         sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
104546 104816       }
104547 104817       if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
104548 104818       sqlite3VdbeChangeP5(v, p5);
104549 104819     }
104550 104820   
104551 104821     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
................................................................................
104691 104961       ** But we are getting ready to store this value back into an index, where
104692 104962       ** it should be converted by to INTEGER again.  So omit the OP_RealAffinity
104693 104963       ** opcode if it is present */
104694 104964       sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
104695 104965     }
104696 104966     if( regOut ){
104697 104967       sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
       104968  +    if( pIdx->pTable->pSelect ){
       104969  +      const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
       104970  +      sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
       104971  +    }
104698 104972     }
104699 104973     sqlite3ReleaseTempRange(pParse, regBase, nCol);
104700 104974     return regBase;
104701 104975   }
104702 104976   
104703 104977   /*
104704 104978   ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
................................................................................
106487 106761   #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
106488 106762       DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
106489 106763       DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
106490 106764   #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
106491 106765       FUNCTION2(unlikely,          1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
106492 106766       FUNCTION2(likelihood,        2, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
106493 106767       FUNCTION2(likely,            1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
       106768  +#ifdef SQLITE_DEBUG
       106769  +    FUNCTION2(affinity,          1, 0, 0, noopFunc,  SQLITE_FUNC_AFFINITY),
       106770  +#endif
106494 106771       FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
106495 106772       FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
106496 106773       FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
106497 106774       FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
106498 106775       FUNCTION(trim,               1, 3, 0, trimFunc         ),
106499 106776       FUNCTION(trim,               2, 3, 0, trimFunc         ),
106500 106777       FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
................................................................................
109540 109817       }
109541 109818       if( overrideError!=OE_Default ){
109542 109819         onError = overrideError;
109543 109820       }else if( onError==OE_Default ){
109544 109821         onError = OE_Abort;
109545 109822       }
109546 109823   
109547         -    if( ix==0 && pPk==pIdx && onError==OE_Replace && pPk->pNext==0 ){
       109824  +    /* Collision detection may be omitted if all of the following are true:
       109825  +    **   (1) The conflict resolution algorithm is REPLACE
       109826  +    **   (2) The table is a WITHOUT ROWID table
       109827  +    **   (3) There are no secondary indexes on the table
       109828  +    **   (4) No delete triggers need to be fired if there is a conflict
       109829  +    **   (5) No FK constraint counters need to be updated if a conflict occurs.
       109830  +    */ 
       109831  +    if( (ix==0 && pIdx->pNext==0)                   /* Condition 3 */
       109832  +     && pPk==pIdx                                   /* Condition 2 */
       109833  +     && onError==OE_Replace                         /* Condition 1 */
       109834  +     && ( 0==(db->flags&SQLITE_RecTriggers) ||      /* Condition 4 */
       109835  +          0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
       109836  +     && ( 0==(db->flags&SQLITE_ForeignKeys) ||      /* Condition 5 */
       109837  +         (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
       109838  +    ){
109548 109839         sqlite3VdbeResolveLabel(v, addrUniqueOk);
109549 109840         continue;
109550 109841       }
109551 109842   
109552         -    
109553 109843       /* Check to see if the new index entry will be unique */
109554 109844       sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
109555 109845                            regIdx, pIdx->nKeyCol); VdbeCoverage(v);
109556 109846   
109557 109847       /* Generate code to handle collisions */
109558 109848       regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
109559 109849       if( isUpdate || onError==OE_Replace ){
................................................................................
109629 109919           assert( onError==OE_Replace );
109630 109920           sqlite3MultiWrite(pParse);
109631 109921           if( db->flags&SQLITE_RecTriggers ){
109632 109922             pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
109633 109923           }
109634 109924           sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
109635 109925               regR, nPkField, 0, OE_Replace,
109636         -            (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), -1);
       109926  +            (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
109637 109927           seenReplace = 1;
109638 109928           break;
109639 109929         }
109640 109930       }
109641 109931       sqlite3VdbeResolveLabel(v, addrUniqueOk);
109642 109932       if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
109643 109933     }
................................................................................
109645 109935       sqlite3VdbeGoto(v, ipkTop+1);
109646 109936       sqlite3VdbeJumpHere(v, ipkBottom);
109647 109937     }
109648 109938     
109649 109939     *pbMayReplace = seenReplace;
109650 109940     VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
109651 109941   }
       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
109652 109961   
109653 109962   /*
109654 109963   ** This routine generates code to finish the INSERT or UPDATE operation
109655 109964   ** that was started by a prior call to sqlite3GenerateConstraintChecks.
109656 109965   ** A consecutive range of registers starting at regNewData contains the
109657 109966   ** rowid and the content to be inserted.
109658 109967   **
................................................................................
109662 109971   SQLITE_PRIVATE void sqlite3CompleteInsertion(
109663 109972     Parse *pParse,      /* The parser context */
109664 109973     Table *pTab,        /* the table into which we are inserting */
109665 109974     int iDataCur,       /* Cursor of the canonical data source */
109666 109975     int iIdxCur,        /* First index cursor */
109667 109976     int regNewData,     /* Range of content */
109668 109977     int *aRegIdx,       /* Register used by each index.  0 for unused indices */
109669         -  int isUpdate,       /* True for UPDATE, False for INSERT */
       109978  +  int update_flags,   /* True for UPDATE, False for INSERT */
109670 109979     int appendBias,     /* True if this is likely to be an append */
109671 109980     int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
109672 109981   ){
109673 109982     Vdbe *v;            /* Prepared statements under construction */
109674 109983     Index *pIdx;        /* An index being inserted or updated */
109675 109984     u8 pik_flags;       /* flag values passed to the btree insert */
109676 109985     int regData;        /* Content registers (after the rowid) */
109677 109986     int regRec;         /* Register holding assembled record for the table */
109678 109987     int i;              /* Loop counter */
109679 109988     u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
       109989  +
       109990  +  assert( update_flags==0
       109991  +       || update_flags==OPFLAG_ISUPDATE
       109992  +       || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
       109993  +  );
109680 109994   
109681 109995     v = sqlite3GetVdbe(pParse);
109682 109996     assert( v!=0 );
109683 109997     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
109684 109998     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
109685 109999       if( aRegIdx[i]==0 ) continue;
109686 110000       bAffinityDone = 1;
109687 110001       if( pIdx->pPartIdxWhere ){
109688 110002         sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
109689 110003         VdbeCoverage(v);
109690 110004       }
109691         -    sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
109692         -                         aRegIdx[i]+1,
109693         -                         pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
109694         -    pik_flags = 0;
109695         -    if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
       110005  +    pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
109696 110006       if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
109697 110007         assert( pParse->nested==0 );
109698 110008         pik_flags |= OPFLAG_NCHANGE;
       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
109699 110018       }
       110019  +    sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
       110020  +                         aRegIdx[i]+1,
       110021  +                         pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
109700 110022       sqlite3VdbeChangeP5(v, pik_flags);
109701 110023     }
109702 110024     if( !HasRowid(pTab) ) return;
109703 110025     regData = regNewData + 1;
109704 110026     regRec = sqlite3GetTempReg(pParse);
109705 110027     sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
       110028  +  sqlite3SetMakeRecordP5(v, pTab);
109706 110029     if( !bAffinityDone ){
109707 110030       sqlite3TableAffinity(v, pTab, 0);
109708 110031       sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
109709 110032     }
109710 110033     if( pParse->nested ){
109711 110034       pik_flags = 0;
109712 110035     }else{
109713 110036       pik_flags = OPFLAG_NCHANGE;
109714         -    pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
       110037  +    pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
109715 110038     }
109716 110039     if( appendBias ){
109717 110040       pik_flags |= OPFLAG_APPEND;
109718 110041     }
109719 110042     if( useSeekResult ){
109720 110043       pik_flags |= OPFLAG_USESEEKRESULT;
109721 110044     }
................................................................................
110116 110439         autoIncStep(pParse, regAutoinc, regRowid);
110117 110440       }else if( pDest->pIndex==0 ){
110118 110441         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
110119 110442       }else{
110120 110443         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
110121 110444         assert( (pDest->tabFlags & TF_Autoincrement)==0 );
110122 110445       }
110123         -    sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
       110446  +    sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
110124 110447       if( db->flags & SQLITE_Vacuum ){
110125 110448         sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
110126 110449         insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
110127 110450                              OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
110128 110451       }else{
110129 110452         insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND;
110130 110453       }
................................................................................
110148 110471       sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
110149 110472       VdbeComment((v, "%s", pSrcIdx->zName));
110150 110473       sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
110151 110474       sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
110152 110475       sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
110153 110476       VdbeComment((v, "%s", pDestIdx->zName));
110154 110477       addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
110155         -    sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
       110478  +    sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
110156 110479       if( db->flags & SQLITE_Vacuum ){
110157 110480         /* This INSERT command is part of a VACUUM operation, which guarantees
110158 110481         ** that the destination table is empty. If all indexed columns use
110159 110482         ** collation sequence BINARY, then it can also be assumed that the
110160 110483         ** index will be populated by inserting keys in strictly sorted 
110161 110484         ** order. In this case, instead of seeking within the b-tree as part
110162 110485         ** of every OP_IdxInsert opcode, an OP_Last is added before the
................................................................................
110933 111256   #endif
110934 111257   
110935 111258   #endif /* SQLITE3EXT_H */
110936 111259   
110937 111260   /************** End of sqlite3ext.h ******************************************/
110938 111261   /************** Continuing where we left off in loadext.c ********************/
110939 111262   /* #include "sqliteInt.h" */
110940         -/* #include <string.h> */
110941 111263   
110942 111264   #ifndef SQLITE_OMIT_LOAD_EXTENSION
110943 111265   /*
110944 111266   ** Some API routines are omitted when various features are
110945 111267   ** excluded from a build of SQLite.  Substitute a NULL pointer
110946 111268   ** for any missing APIs.
110947 111269   */
................................................................................
111791 112113   #define PragTyp_REKEY                         40
111792 112114   #define PragTyp_LOCK_STATUS                   41
111793 112115   #define PragTyp_PARSER_TRACE                  42
111794 112116   
111795 112117   /* Property flags associated with various pragma. */
111796 112118   #define PragFlg_NeedSchema 0x01 /* Force schema load before running */
111797 112119   #define PragFlg_NoColumns  0x02 /* OP_ResultRow called with zero columns */
111798         -#define PragFlg_ReadOnly   0x04 /* Read-only HEADER_VALUE */
111799         -#define PragFlg_Result0    0x08 /* Acts as query when no argument */
111800         -#define PragFlg_Result1    0x10 /* Acts as query when has one argument */
111801         -#define PragFlg_SchemaOpt  0x20 /* Schema restricts name search if present */
111802         -#define PragFlg_SchemaReq  0x40 /* Schema required - "main" is default */
       112120  +#define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
       112121  +#define PragFlg_ReadOnly   0x08 /* Read-only HEADER_VALUE */
       112122  +#define PragFlg_Result0    0x10 /* Acts as query when no argument */
       112123  +#define PragFlg_Result1    0x20 /* Acts as query when has one argument */
       112124  +#define PragFlg_SchemaOpt  0x40 /* Schema restricts name search if present */
       112125  +#define PragFlg_SchemaReq  0x80 /* Schema required - "main" is default */
111803 112126   
111804 112127   /* Names of columns for pragmas that return multi-column result
111805 112128   ** or that return single-column results where the name of the
111806 112129   ** result column is different from the name of the pragma
111807 112130   */
111808 112131   static const char *const pragCName[] = {
111809 112132     /*   0 */ "cache_size",  /* Used by: default_cache_size */
................................................................................
111872 112195     /* ePragFlg:  */ 0,
111873 112196     /* ColNames:  */ 0, 0,
111874 112197     /* iArg:      */ 0 },
111875 112198   #endif
111876 112199   #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
111877 112200    {/* zName:     */ "application_id",
111878 112201     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
111879         -  /* ePragFlg:  */ PragFlg_Result0,
       112202  +  /* ePragFlg:  */ PragFlg_NoColumns1|PragFlg_Result0,
111880 112203     /* ColNames:  */ 0, 0,
111881 112204     /* iArg:      */ BTREE_APPLICATION_ID },
111882 112205   #endif
111883 112206   #if !defined(SQLITE_OMIT_AUTOVACUUM)
111884 112207    {/* zName:     */ "auto_vacuum",
111885 112208     /* ePragTyp:  */ PragTyp_AUTO_VACUUM,
111886         -  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
       112209  +  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
111887 112210     /* ColNames:  */ 0, 0,
111888 112211     /* iArg:      */ 0 },
111889 112212   #endif
111890 112213   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
111891 112214   #if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
111892 112215    {/* zName:     */ "automatic_index",
111893 112216     /* ePragTyp:  */ PragTyp_FLAG,
111894         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112217  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
111895 112218     /* ColNames:  */ 0, 0,
111896 112219     /* iArg:      */ SQLITE_AutoIndex },
111897 112220   #endif
111898 112221   #endif
111899 112222    {/* zName:     */ "busy_timeout",
111900 112223     /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
111901 112224     /* ePragFlg:  */ PragFlg_Result0,
111902 112225     /* ColNames:  */ 45, 1,
111903 112226     /* iArg:      */ 0 },
111904 112227   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
111905 112228    {/* zName:     */ "cache_size",
111906 112229     /* ePragTyp:  */ PragTyp_CACHE_SIZE,
111907         -  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
       112230  +  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
111908 112231     /* ColNames:  */ 0, 0,
111909 112232     /* iArg:      */ 0 },
111910 112233   #endif
111911 112234   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
111912 112235    {/* zName:     */ "cache_spill",
111913 112236     /* ePragTyp:  */ PragTyp_CACHE_SPILL,
111914         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq,
       112237  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
111915 112238     /* ColNames:  */ 0, 0,
111916 112239     /* iArg:      */ 0 },
111917 112240   #endif
111918 112241    {/* zName:     */ "case_sensitive_like",
111919 112242     /* ePragTyp:  */ PragTyp_CASE_SENSITIVE_LIKE,
111920         -  /* ePragFlg:  */ 0,
       112243  +  /* ePragFlg:  */ PragFlg_NoColumns,
111921 112244     /* ColNames:  */ 0, 0,
111922 112245     /* iArg:      */ 0 },
111923 112246    {/* zName:     */ "cell_size_check",
111924 112247     /* ePragTyp:  */ PragTyp_FLAG,
111925         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112248  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
111926 112249     /* ColNames:  */ 0, 0,
111927 112250     /* iArg:      */ SQLITE_CellSizeCk },
111928 112251   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
111929 112252    {/* zName:     */ "checkpoint_fullfsync",
111930 112253     /* ePragTyp:  */ PragTyp_FLAG,
111931         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112254  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
111932 112255     /* ColNames:  */ 0, 0,
111933 112256     /* iArg:      */ SQLITE_CkptFullFSync },
111934 112257   #endif
111935 112258   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
111936 112259    {/* zName:     */ "collation_list",
111937 112260     /* ePragTyp:  */ PragTyp_COLLATION_LIST,
111938 112261     /* ePragFlg:  */ PragFlg_Result0,
................................................................................
111945 112268     /* ePragFlg:  */ PragFlg_Result0,
111946 112269     /* ColNames:  */ 0, 0,
111947 112270     /* iArg:      */ 0 },
111948 112271   #endif
111949 112272   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
111950 112273    {/* zName:     */ "count_changes",
111951 112274     /* ePragTyp:  */ PragTyp_FLAG,
111952         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112275  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
111953 112276     /* ColNames:  */ 0, 0,
111954 112277     /* iArg:      */ SQLITE_CountRows },
111955 112278   #endif
111956 112279   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
111957 112280    {/* zName:     */ "data_store_directory",
111958 112281     /* ePragTyp:  */ PragTyp_DATA_STORE_DIRECTORY,
111959         -  /* ePragFlg:  */ 0,
       112282  +  /* ePragFlg:  */ PragFlg_NoColumns1,
111960 112283     /* ColNames:  */ 0, 0,
111961 112284     /* iArg:      */ 0 },
111962 112285   #endif
111963 112286   #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
111964 112287    {/* zName:     */ "data_version",
111965 112288     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
111966         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_ReadOnly,
       112289  +  /* ePragFlg:  */ PragFlg_ReadOnly|PragFlg_Result0,
111967 112290     /* ColNames:  */ 0, 0,
111968 112291     /* iArg:      */ BTREE_DATA_VERSION },
111969 112292   #endif
111970 112293   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
111971 112294    {/* zName:     */ "database_list",
111972 112295     /* ePragTyp:  */ PragTyp_DATABASE_LIST,
111973 112296     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
111974 112297     /* ColNames:  */ 25, 3,
111975 112298     /* iArg:      */ 0 },
111976 112299   #endif
111977 112300   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
111978 112301    {/* zName:     */ "default_cache_size",
111979 112302     /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
111980         -  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
       112303  +  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
111981 112304     /* ColNames:  */ 0, 1,
111982 112305     /* iArg:      */ 0 },
111983 112306   #endif
111984 112307   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
111985 112308   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
111986 112309    {/* zName:     */ "defer_foreign_keys",
111987 112310     /* ePragTyp:  */ PragTyp_FLAG,
111988         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112311  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
111989 112312     /* ColNames:  */ 0, 0,
111990 112313     /* iArg:      */ SQLITE_DeferFKs },
111991 112314   #endif
111992 112315   #endif
111993 112316   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
111994 112317    {/* zName:     */ "empty_result_callbacks",
111995 112318     /* ePragTyp:  */ PragTyp_FLAG,
111996         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112319  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
111997 112320     /* ColNames:  */ 0, 0,
111998 112321     /* iArg:      */ SQLITE_NullCallback },
111999 112322   #endif
112000 112323   #if !defined(SQLITE_OMIT_UTF16)
112001 112324    {/* zName:     */ "encoding",
112002 112325     /* ePragTyp:  */ PragTyp_ENCODING,
112003         -  /* ePragFlg:  */ PragFlg_Result0,
       112326  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112004 112327     /* ColNames:  */ 0, 0,
112005 112328     /* iArg:      */ 0 },
112006 112329   #endif
112007 112330   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
112008 112331    {/* zName:     */ "foreign_key_check",
112009 112332     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
112010 112333     /* ePragFlg:  */ PragFlg_NeedSchema,
................................................................................
112018 112341     /* ColNames:  */ 30, 8,
112019 112342     /* iArg:      */ 0 },
112020 112343   #endif
112021 112344   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112022 112345   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
112023 112346    {/* zName:     */ "foreign_keys",
112024 112347     /* ePragTyp:  */ PragTyp_FLAG,
112025         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112348  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112026 112349     /* ColNames:  */ 0, 0,
112027 112350     /* iArg:      */ SQLITE_ForeignKeys },
112028 112351   #endif
112029 112352   #endif
112030 112353   #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
112031 112354    {/* zName:     */ "freelist_count",
112032 112355     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
112033         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_ReadOnly,
       112356  +  /* ePragFlg:  */ PragFlg_ReadOnly|PragFlg_Result0,
112034 112357     /* ColNames:  */ 0, 0,
112035 112358     /* iArg:      */ BTREE_FREE_PAGE_COUNT },
112036 112359   #endif
112037 112360   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112038 112361    {/* zName:     */ "full_column_names",
112039 112362     /* ePragTyp:  */ PragTyp_FLAG,
112040         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112363  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112041 112364     /* ColNames:  */ 0, 0,
112042 112365     /* iArg:      */ SQLITE_FullColNames },
112043 112366    {/* zName:     */ "fullfsync",
112044 112367     /* ePragTyp:  */ PragTyp_FLAG,
112045         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112368  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112046 112369     /* ColNames:  */ 0, 0,
112047 112370     /* iArg:      */ SQLITE_FullFSync },
112048 112371   #endif
112049 112372   #if defined(SQLITE_HAS_CODEC)
112050 112373    {/* zName:     */ "hexkey",
112051 112374     /* ePragTyp:  */ PragTyp_HEXKEY,
112052 112375     /* ePragFlg:  */ 0,
................................................................................
112058 112381     /* ColNames:  */ 0, 0,
112059 112382     /* iArg:      */ 0 },
112060 112383   #endif
112061 112384   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112062 112385   #if !defined(SQLITE_OMIT_CHECK)
112063 112386    {/* zName:     */ "ignore_check_constraints",
112064 112387     /* ePragTyp:  */ PragTyp_FLAG,
112065         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112388  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112066 112389     /* ColNames:  */ 0, 0,
112067 112390     /* iArg:      */ SQLITE_IgnoreChecks },
112068 112391   #endif
112069 112392   #endif
112070 112393   #if !defined(SQLITE_OMIT_AUTOVACUUM)
112071 112394    {/* zName:     */ "incremental_vacuum",
112072 112395     /* ePragTyp:  */ PragTyp_INCREMENTAL_VACUUM,
................................................................................
112116 112439     /* ePragFlg:  */ 0,
112117 112440     /* ColNames:  */ 0, 0,
112118 112441     /* iArg:      */ 0 },
112119 112442   #endif
112120 112443   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112121 112444    {/* zName:     */ "legacy_file_format",
112122 112445     /* ePragTyp:  */ PragTyp_FLAG,
112123         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112446  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112124 112447     /* ColNames:  */ 0, 0,
112125 112448     /* iArg:      */ SQLITE_LegacyFileFmt },
112126 112449   #endif
112127 112450   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
112128 112451    {/* zName:     */ "lock_proxy_file",
112129 112452     /* ePragTyp:  */ PragTyp_LOCK_PROXY_FILE,
112130         -  /* ePragFlg:  */ 0,
       112453  +  /* ePragFlg:  */ PragFlg_NoColumns1,
112131 112454     /* ColNames:  */ 0, 0,
112132 112455     /* iArg:      */ 0 },
112133 112456   #endif
112134 112457   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
112135 112458    {/* zName:     */ "lock_status",
112136 112459     /* ePragTyp:  */ PragTyp_LOCK_STATUS,
112137 112460     /* ePragFlg:  */ PragFlg_Result0,
................................................................................
112157 112480    {/* zName:     */ "page_count",
112158 112481     /* ePragTyp:  */ PragTyp_PAGE_COUNT,
112159 112482     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
112160 112483     /* ColNames:  */ 0, 0,
112161 112484     /* iArg:      */ 0 },
112162 112485    {/* zName:     */ "page_size",
112163 112486     /* ePragTyp:  */ PragTyp_PAGE_SIZE,
112164         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq,
       112487  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
112165 112488     /* ColNames:  */ 0, 0,
112166 112489     /* iArg:      */ 0 },
112167 112490   #endif
112168 112491   #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_PARSER_TRACE)
112169 112492    {/* zName:     */ "parser_trace",
112170 112493     /* ePragTyp:  */ PragTyp_PARSER_TRACE,
112171 112494     /* ePragFlg:  */ 0,
112172 112495     /* ColNames:  */ 0, 0,
112173 112496     /* iArg:      */ 0 },
112174 112497   #endif
112175 112498   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112176 112499    {/* zName:     */ "query_only",
112177 112500     /* ePragTyp:  */ PragTyp_FLAG,
112178         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112501  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112179 112502     /* ColNames:  */ 0, 0,
112180 112503     /* iArg:      */ SQLITE_QueryOnly },
112181 112504   #endif
112182 112505   #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
112183 112506    {/* zName:     */ "quick_check",
112184 112507     /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
112185 112508     /* ePragFlg:  */ PragFlg_NeedSchema,
112186 112509     /* ColNames:  */ 0, 0,
112187 112510     /* iArg:      */ 0 },
112188 112511   #endif
112189 112512   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112190 112513    {/* zName:     */ "read_uncommitted",
112191 112514     /* ePragTyp:  */ PragTyp_FLAG,
112192         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112515  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112193 112516     /* ColNames:  */ 0, 0,
112194 112517     /* iArg:      */ SQLITE_ReadUncommitted },
112195 112518    {/* zName:     */ "recursive_triggers",
112196 112519     /* ePragTyp:  */ PragTyp_FLAG,
112197         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112520  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112198 112521     /* ColNames:  */ 0, 0,
112199 112522     /* iArg:      */ SQLITE_RecTriggers },
112200 112523   #endif
112201 112524   #if defined(SQLITE_HAS_CODEC)
112202 112525    {/* zName:     */ "rekey",
112203 112526     /* ePragTyp:  */ PragTyp_REKEY,
112204 112527     /* ePragFlg:  */ 0,
112205 112528     /* ColNames:  */ 0, 0,
112206 112529     /* iArg:      */ 0 },
112207 112530   #endif
112208 112531   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112209 112532    {/* zName:     */ "reverse_unordered_selects",
112210 112533     /* ePragTyp:  */ PragTyp_FLAG,
112211         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112534  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112212 112535     /* ColNames:  */ 0, 0,
112213 112536     /* iArg:      */ SQLITE_ReverseOrder },
112214 112537   #endif
112215 112538   #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
112216 112539    {/* zName:     */ "schema_version",
112217 112540     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
112218         -  /* ePragFlg:  */ PragFlg_Result0,
       112541  +  /* ePragFlg:  */ PragFlg_NoColumns1|PragFlg_Result0,
112219 112542     /* ColNames:  */ 0, 0,
112220 112543     /* iArg:      */ BTREE_SCHEMA_VERSION },
112221 112544   #endif
112222 112545   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
112223 112546    {/* zName:     */ "secure_delete",
112224 112547     /* ePragTyp:  */ PragTyp_SECURE_DELETE,
112225 112548     /* ePragFlg:  */ PragFlg_Result0,
112226 112549     /* ColNames:  */ 0, 0,
112227 112550     /* iArg:      */ 0 },
112228 112551   #endif
112229 112552   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112230 112553    {/* zName:     */ "short_column_names",
112231 112554     /* ePragTyp:  */ PragTyp_FLAG,
112232         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112555  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112233 112556     /* ColNames:  */ 0, 0,
112234 112557     /* iArg:      */ SQLITE_ShortColNames },
112235 112558   #endif
112236 112559    {/* zName:     */ "shrink_memory",
112237 112560     /* ePragTyp:  */ PragTyp_SHRINK_MEMORY,
112238         -  /* ePragFlg:  */ 0,
       112561  +  /* ePragFlg:  */ PragFlg_NoColumns,
112239 112562     /* ColNames:  */ 0, 0,
112240 112563     /* iArg:      */ 0 },
112241 112564    {/* zName:     */ "soft_heap_limit",
112242 112565     /* ePragTyp:  */ PragTyp_SOFT_HEAP_LIMIT,
112243 112566     /* ePragFlg:  */ PragFlg_Result0,
112244 112567     /* ColNames:  */ 0, 0,
112245 112568     /* iArg:      */ 0 },
112246 112569   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112247 112570   #if defined(SQLITE_DEBUG)
112248 112571    {/* zName:     */ "sql_trace",
112249 112572     /* ePragTyp:  */ PragTyp_FLAG,
112250         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112573  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112251 112574     /* ColNames:  */ 0, 0,
112252 112575     /* iArg:      */ SQLITE_SqlTrace },
112253 112576   #endif
112254 112577   #endif
112255 112578   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
112256 112579    {/* zName:     */ "stats",
112257 112580     /* ePragTyp:  */ PragTyp_STATS,
................................................................................
112258 112581     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
112259 112582     /* ColNames:  */ 7, 4,
112260 112583     /* iArg:      */ 0 },
112261 112584   #endif
112262 112585   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
112263 112586    {/* zName:     */ "synchronous",
112264 112587     /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
112265         -  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
       112588  +  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
112266 112589     /* ColNames:  */ 0, 0,
112267 112590     /* iArg:      */ 0 },
112268 112591   #endif
112269 112592   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
112270 112593    {/* zName:     */ "table_info",
112271 112594     /* ePragTyp:  */ PragTyp_TABLE_INFO,
112272 112595     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
112273 112596     /* ColNames:  */ 1, 6,
112274 112597     /* iArg:      */ 0 },
112275 112598   #endif
112276 112599   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
112277 112600    {/* zName:     */ "temp_store",
112278 112601     /* ePragTyp:  */ PragTyp_TEMP_STORE,
112279         -  /* ePragFlg:  */ PragFlg_Result0,
       112602  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112280 112603     /* ColNames:  */ 0, 0,
112281 112604     /* iArg:      */ 0 },
112282 112605    {/* zName:     */ "temp_store_directory",
112283 112606     /* ePragTyp:  */ PragTyp_TEMP_STORE_DIRECTORY,
112284         -  /* ePragFlg:  */ 0,
       112607  +  /* ePragFlg:  */ PragFlg_NoColumns1,
112285 112608     /* ColNames:  */ 0, 0,
112286 112609     /* iArg:      */ 0 },
112287 112610   #endif
112288 112611    {/* zName:     */ "threads",
112289 112612     /* ePragTyp:  */ PragTyp_THREADS,
112290 112613     /* ePragFlg:  */ PragFlg_Result0,
112291 112614     /* ColNames:  */ 0, 0,
112292 112615     /* iArg:      */ 0 },
112293 112616   #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
112294 112617    {/* zName:     */ "user_version",
112295 112618     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
112296         -  /* ePragFlg:  */ PragFlg_Result0,
       112619  +  /* ePragFlg:  */ PragFlg_NoColumns1|PragFlg_Result0,
112297 112620     /* ColNames:  */ 0, 0,
112298 112621     /* iArg:      */ BTREE_USER_VERSION },
112299 112622   #endif
112300 112623   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112301 112624   #if defined(SQLITE_DEBUG)
112302 112625    {/* zName:     */ "vdbe_addoptrace",
112303 112626     /* ePragTyp:  */ PragTyp_FLAG,
112304         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112627  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112305 112628     /* ColNames:  */ 0, 0,
112306 112629     /* iArg:      */ SQLITE_VdbeAddopTrace },
112307 112630    {/* zName:     */ "vdbe_debug",
112308 112631     /* ePragTyp:  */ PragTyp_FLAG,
112309         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112632  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112310 112633     /* ColNames:  */ 0, 0,
112311 112634     /* iArg:      */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
112312 112635    {/* zName:     */ "vdbe_eqp",
112313 112636     /* ePragTyp:  */ PragTyp_FLAG,
112314         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112637  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112315 112638     /* ColNames:  */ 0, 0,
112316 112639     /* iArg:      */ SQLITE_VdbeEQP },
112317 112640    {/* zName:     */ "vdbe_listing",
112318 112641     /* ePragTyp:  */ PragTyp_FLAG,
112319         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112642  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112320 112643     /* ColNames:  */ 0, 0,
112321 112644     /* iArg:      */ SQLITE_VdbeListing },
112322 112645    {/* zName:     */ "vdbe_trace",
112323 112646     /* ePragTyp:  */ PragTyp_FLAG,
112324         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112647  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112325 112648     /* ColNames:  */ 0, 0,
112326 112649     /* iArg:      */ SQLITE_VdbeTrace },
112327 112650   #endif
112328 112651   #endif
112329 112652   #if !defined(SQLITE_OMIT_WAL)
112330 112653    {/* zName:     */ "wal_autocheckpoint",
112331 112654     /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
................................................................................
112337 112660     /* ePragFlg:  */ PragFlg_NeedSchema,
112338 112661     /* ColNames:  */ 42, 3,
112339 112662     /* iArg:      */ 0 },
112340 112663   #endif
112341 112664   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112342 112665    {/* zName:     */ "writable_schema",
112343 112666     /* ePragTyp:  */ PragTyp_FLAG,
112344         -  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns,
       112667  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112345 112668     /* ColNames:  */ 0, 0,
112346 112669     /* iArg:      */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
112347 112670   #endif
112348 112671   };
112349 112672   /* Number of pragmas: 60 on by default, 73 total. */
112350 112673   
112351 112674   /************** End of pragma.h **********************************************/
................................................................................
112596 112919     return azModeName[eMode];
112597 112920   }
112598 112921   
112599 112922   /*
112600 112923   ** Locate a pragma in the aPragmaName[] array.
112601 112924   */
112602 112925   static const PragmaName *pragmaLocate(const char *zName){
112603         -  int upr, lwr, mid, rc;
       112926  +  int upr, lwr, mid = 0, rc;
112604 112927     lwr = 0;
112605 112928     upr = ArraySize(aPragmaName)-1;
112606 112929     while( lwr<=upr ){
112607 112930       mid = (lwr+upr)/2;
112608 112931       rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
112609 112932       if( rc==0 ) break;
112610 112933       if( rc<0 ){
................................................................................
112725 113048   
112726 113049     /* Make sure the database schema is loaded if the pragma requires that */
112727 113050     if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
112728 113051       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
112729 113052     }
112730 113053   
112731 113054     /* Register the result column names for pragmas that return results */
112732         -  if( (pPragma->mPragFlg & PragFlg_NoColumns)==0 ){
       113055  +  if( (pPragma->mPragFlg & PragFlg_NoColumns)==0 
       113056  +   && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
       113057  +  ){
112733 113058       setPragmaResultColumnNames(v, pPragma);
112734 113059     }
112735 113060   
112736 113061     /* Jump to the appropriate pragma handler */
112737 113062     switch( pPragma->ePragTyp ){
112738 113063     
112739 113064   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
................................................................................
114269 114594       }
114270 114595   #endif
114271 114596     }
114272 114597     break;
114273 114598   #endif
114274 114599   
114275 114600     } /* End of the PRAGMA switch */
       114601  +
       114602  +  /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
       114603  +  ** purpose is to execute assert() statements to verify that if the
       114604  +  ** PragFlg_NoColumns1 flag is set and the caller specified an argument
       114605  +  ** to the PRAGMA, the implementation has not added any OP_ResultRow 
       114606  +  ** instructions to the VM.  */
       114607  +  if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
       114608  +    sqlite3VdbeVerifyNoResultRow(v);
       114609  +  }
114276 114610   
114277 114611   pragma_out:
114278 114612     sqlite3DbFree(db, zLeft);
114279 114613     sqlite3DbFree(db, zRight);
114280 114614   }
114281 114615   #ifndef SQLITE_OMIT_VIRTUALTABLE
114282 114616   /*****************************************************************************
................................................................................
116099 116433     int r1;
116100 116434   
116101 116435     v = pParse->pVdbe;
116102 116436     r1 = sqlite3GetTempReg(pParse);
116103 116437     sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
116104 116438     sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
116105 116439     sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
       116440  +  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
116106 116441     sqlite3ReleaseTempReg(pParse, r1);
116107 116442   }
116108 116443   
116109 116444   /*
116110 116445   ** This routine generates the code for the inside of the inner loop
116111 116446   ** of a SELECT.
116112 116447   **
................................................................................
116184 116519       */
116185 116520       u8 ecelFlags;
116186 116521       if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
116187 116522         ecelFlags = SQLITE_ECEL_DUP;
116188 116523       }else{
116189 116524         ecelFlags = 0;
116190 116525       }
116191         -    assert( eDest!=SRT_Table || pSort==0 );
116192         -    if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab ){
       116526  +    if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
116193 116527         /* For each expression in pEList that is a copy of an expression in
116194 116528         ** the ORDER BY clause (pSort->pOrderBy), set the associated 
116195 116529         ** iOrderByCol value to one more than the index of the ORDER BY 
116196 116530         ** expression within the sort-key that pushOntoSorter() will generate.
116197 116531         ** This allows the pEList field to be omitted from the sorted record,
116198 116532         ** saving space and CPU cycles.  */
116199 116533         ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
................................................................................
116727 117061       }else{
116728 117062         iRead = iCol++;
116729 117063       }
116730 117064       sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
116731 117065       VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
116732 117066     }
116733 117067     switch( eDest ){
       117068  +    case SRT_Table:
116734 117069       case SRT_EphemTab: {
116735 117070         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
116736 117071         sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
116737 117072         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
116738 117073         break;
116739 117074       }
116740 117075   #ifndef SQLITE_OMIT_SUBQUERY
................................................................................
119627 119962         return SQLITE_ERROR;
119628 119963       }
119629 119964       assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
119630 119965   
119631 119966       pCte->zCteErr = "circular reference: %s";
119632 119967       pSavedWith = pParse->pWith;
119633 119968       pParse->pWith = pWith;
119634         -    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  +    }
119635 119978       pParse->pWith = pWith;
119636 119979   
119637 119980       for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
119638 119981       pEList = pLeft->pEList;
119639 119982       if( pCte->pCols ){
119640 119983         if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
119641 119984           sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
................................................................................
119671 120014   **
119672 120015   ** This function is used as the xSelectCallback2() callback by
119673 120016   ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
119674 120017   ** names and other FROM clause elements. 
119675 120018   */
119676 120019   static void selectPopWith(Walker *pWalker, Select *p){
119677 120020     Parse *pParse = pWalker->pParse;
119678         -  With *pWith = findRightmost(p)->pWith;
119679         -  if( pWith!=0 ){
119680         -    assert( pParse->pWith==pWith );
119681         -    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  +    }
119682 120027     }
119683 120028   }
119684 120029   #else
119685 120030   #define selectPopWith 0
119686 120031   #endif
119687 120032   
119688 120033   /*
................................................................................
119724 120069       return WRC_Abort;
119725 120070     }
119726 120071     if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
119727 120072       return WRC_Prune;
119728 120073     }
119729 120074     pTabList = p->pSrc;
119730 120075     pEList = p->pEList;
119731         -  if( pWalker->xSelectCallback2==selectPopWith ){
119732         -    sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
       120076  +  if( p->pWith ){
       120077  +    sqlite3WithPush(pParse, p->pWith, 0);
119733 120078     }
119734 120079   
119735 120080     /* Make sure cursor numbers have been assigned to all entries in
119736 120081     ** the FROM clause of the SELECT statement.
119737 120082     */
119738 120083     sqlite3SrcListAssignCursors(pParse, pTabList);
119739 120084   
................................................................................
120012 120357     w.xExprCallback = sqlite3ExprWalkNoop;
120013 120358     w.pParse = pParse;
120014 120359     if( pParse->hasCompound ){
120015 120360       w.xSelectCallback = convertCompoundSelectToSubquery;
120016 120361       sqlite3WalkSelect(&w, pSelect);
120017 120362     }
120018 120363     w.xSelectCallback = selectExpander;
120019         -  if( (pSelect->selFlags & SF_MultiValue)==0 ){
120020         -    w.xSelectCallback2 = selectPopWith;
120021         -  }
       120364  +  w.xSelectCallback2 = selectPopWith;
120022 120365     sqlite3WalkSelect(&w, pSelect);
120023 120366   }
120024 120367   
120025 120368   
120026 120369   #ifndef SQLITE_OMIT_SUBQUERY
120027 120370   /*
120028 120371   ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
................................................................................
121093 121436           }
121094 121437     
121095 121438           /* This case runs if the aggregate has no GROUP BY clause.  The
121096 121439           ** processing is much simpler since there is only a single row
121097 121440           ** of output.
121098 121441           */
121099 121442           resetAccumulator(pParse, &sAggInfo);
121100         -        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
       121443  +        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax, 0,flag,0);
121101 121444           if( pWInfo==0 ){
121102 121445             sqlite3ExprListDelete(db, pDel);
121103 121446             goto select_end;
121104 121447           }
121105 121448           updateAccumulator(pParse, &sAggInfo);
121106 121449           assert( pMinMax==0 || pMinMax->nExpr==1 );
121107 121450           if( sqlite3WhereIsOrdered(pWInfo)>0 ){
................................................................................
121182 121525   ** interface routines.  These are just wrappers around the main
121183 121526   ** interface routine of sqlite3_exec().
121184 121527   **
121185 121528   ** These routines are in a separate files so that they will not be linked
121186 121529   ** if they are not used.
121187 121530   */
121188 121531   /* #include "sqliteInt.h" */
121189         -/* #include <stdlib.h> */
121190         -/* #include <string.h> */
121191 121532   
121192 121533   #ifndef SQLITE_OMIT_GET_TABLE
121193 121534   
121194 121535   /*
121195 121536   ** This structure is used to pass data from sqlite3_get_table() through
121196 121537   ** to the callback function is uses to build the result.
121197 121538   */
................................................................................
122541 122882       VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
122542 122883       assert( i<pTab->nCol );
122543 122884       sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, 
122544 122885                            pCol->affinity, &pValue);
122545 122886       if( pValue ){
122546 122887         sqlite3VdbeAppendP4(v, pValue, P4_MEM);
122547 122888       }
       122889  +  }
122548 122890   #ifndef SQLITE_OMIT_FLOATING_POINT
122549         -    if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
122550         -      sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
122551         -    }
       122891  +  if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
       122892  +    sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
       122893  +  }
122552 122894   #endif
122553         -  }
122554 122895   }
122555 122896   
122556 122897   /*
122557 122898   ** Process an UPDATE statement.
122558 122899   **
122559 122900   **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
122560 122901   **          \_______/ \________/     \______/       \________________/
................................................................................
122575 122916     Index *pIdx;           /* For looping over indices */
122576 122917     Index *pPk;            /* The PRIMARY KEY index for WITHOUT ROWID tables */
122577 122918     int nIdx;              /* Number of indices that need updating */
122578 122919     int iBaseCur;          /* Base cursor number */
122579 122920     int iDataCur;          /* Cursor for the canonical data btree */
122580 122921     int iIdxCur;           /* Cursor for the first index */
122581 122922     sqlite3 *db;           /* The database structure */
122582         -  int *aRegIdx = 0;      /* One register assigned to each index to be updated */
       122923  +  int *aRegIdx = 0;      /* First register in array assigned to each index */
122583 122924     int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
122584 122925                            ** an expression for the i-th column of the table.
122585 122926                            ** aXRef[i]==-1 if the i-th column is not changed. */
122586 122927     u8 *aToOpen;           /* 1 for tables and indices to be opened */
122587 122928     u8 chngPk;             /* PRIMARY KEY changed in a WITHOUT ROWID table */
122588 122929     u8 chngRowid;          /* Rowid changed in a normal table */
122589 122930     u8 chngKey;            /* Either chngPk or chngRowid */
122590 122931     Expr *pRowidExpr = 0;  /* Expression defining the new record number */
122591 122932     AuthContext sContext;  /* The authorization context */
122592 122933     NameContext sNC;       /* The name-context to resolve expressions in */
122593 122934     int iDb;               /* Database containing the table being updated */
122594         -  int okOnePass;         /* True for one-pass algorithm without the FIFO */
       122935  +  int eOnePass;          /* ONEPASS_XXX value from where.c */
122595 122936     int hasFK;             /* True if foreign key processing is required */
122596 122937     int labelBreak;        /* Jump here to break out of UPDATE loop */
122597 122938     int labelContinue;     /* Jump here to continue next step of UPDATE loop */
       122939  +  int flags;             /* Flags for sqlite3WhereBegin() */
122598 122940   
122599 122941   #ifndef SQLITE_OMIT_TRIGGER
122600 122942     int isView;            /* True when updating a view (INSTEAD OF trigger) */
122601 122943     Trigger *pTrigger;     /* List of triggers on pTab, if required */
122602 122944     int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
122603 122945   #endif
122604 122946     int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
122605 122947     int iEph = 0;          /* Ephemeral table holding all primary key values */
122606 122948     int nKey = 0;          /* Number of elements in regKey for WITHOUT ROWID */
122607 122949     int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
       122950  +  int addrOpen = 0;      /* Address of OP_OpenEphemeral */
       122951  +  int iPk = 0;           /* First of nPk cells holding PRIMARY KEY value */
       122952  +  i16 nPk = 0;           /* Number of components of the PRIMARY KEY */
       122953  +  int bReplace = 0;      /* True if REPLACE conflict resolution might happen */
122608 122954   
122609 122955     /* Register Allocations */
122610 122956     int regRowCount = 0;   /* A count of rows changed */
122611 122957     int regOldRowid = 0;   /* The old rowid */
122612 122958     int regNewRowid = 0;   /* The new rowid */
122613 122959     int regNew = 0;        /* Content of the NEW.* table in triggers */
122614 122960     int regOld = 0;        /* Content of OLD.* table in triggers */
................................................................................
122760 123106       }else{
122761 123107         reg = 0;
122762 123108         for(i=0; i<pIdx->nKeyCol; i++){
122763 123109           i16 iIdxCol = pIdx->aiColumn[i];
122764 123110           if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
122765 123111             reg = ++pParse->nMem;
122766 123112             pParse->nMem += pIdx->nColumn;
       123113  +          if( (onError==OE_Replace)
       123114  +           || (onError==OE_Default && pIdx->onError==OE_Replace) 
       123115  +          ){
       123116  +            bReplace = 1;
       123117  +          }
122767 123118             break;
122768 123119           }
122769 123120         }
122770 123121       }
122771 123122       if( reg==0 ) aToOpen[j+1] = 0;
122772 123123       aRegIdx[j] = reg;
122773 123124     }
       123125  +  if( bReplace ){
       123126  +    /* If REPLACE conflict resolution might be invoked, open cursors on all 
       123127  +    ** indexes in case they are needed to delete records.  */
       123128  +    memset(aToOpen, 1, nIdx+1);
       123129  +  }
122774 123130   
122775 123131     /* Begin generating code. */
122776 123132     v = sqlite3GetVdbe(pParse);
122777 123133     if( v==0 ) goto update_cleanup;
122778 123134     if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
122779 123135     sqlite3BeginWriteOperation(pParse, 1, iDb);
122780 123136   
................................................................................
122819 123175     if( IsVirtual(pTab) ){
122820 123176       updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
122821 123177                          pWhere, onError);
122822 123178       goto update_cleanup;
122823 123179     }
122824 123180   #endif
122825 123181   
122826         -  /* Begin the database scan
122827         -  */
       123182  +  /* Initialize the count of updated rows */
       123183  +  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
       123184  +    regRowCount = ++pParse->nMem;
       123185  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
       123186  +  }
       123187  +
122828 123188     if( HasRowid(pTab) ){
122829 123189       sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
122830         -    pWInfo = sqlite3WhereBegin(
122831         -        pParse, pTabList, pWhere, 0, 0,
122832         -            WHERE_ONEPASS_DESIRED | WHERE_SEEK_TABLE, iIdxCur
122833         -    );
122834         -    if( pWInfo==0 ) goto update_cleanup;
122835         -    okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
122836         -  
122837         -    /* Remember the rowid of every item to be updated.
122838         -    */
122839         -    sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
122840         -    if( !okOnePass ){
122841         -      sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
122842         -    }
122843         -  
122844         -    /* End the database scan loop.
122845         -    */
122846         -    sqlite3WhereEnd(pWInfo);
122847 123190     }else{
122848         -    int iPk;         /* First of nPk memory cells holding PRIMARY KEY value */
122849         -    i16 nPk;         /* Number of components of the PRIMARY KEY */
122850         -    int addrOpen;    /* Address of the OpenEphemeral instruction */
122851         -
122852 123191       assert( pPk!=0 );
122853 123192       nPk = pPk->nKeyCol;
122854 123193       iPk = pParse->nMem+1;
122855 123194       pParse->nMem += nPk;
122856 123195       regKey = ++pParse->nMem;
122857 123196       iEph = pParse->nTab++;
       123197  +
122858 123198       sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
122859 123199       addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
122860 123200       sqlite3VdbeSetP4KeyInfo(pParse, pPk);
122861         -    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 
122862         -                               WHERE_ONEPASS_DESIRED, iIdxCur);
122863         -    if( pWInfo==0 ) goto update_cleanup;
122864         -    okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
       123201  +  }
       123202  +
       123203  +  /* Begin the database scan. 
       123204  +  **
       123205  +  ** Do not consider a single-pass strategy for a multi-row update if
       123206  +  ** there are any triggers or foreign keys to process, or rows may
       123207  +  ** be deleted as a result of REPLACE conflict handling. Any of these
       123208  +  ** things might disturb a cursor being used to scan through the table
       123209  +  ** or index, causing a single-pass approach to malfunction.  */
       123210  +  flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
       123211  +  if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
       123212  +    flags |= WHERE_ONEPASS_MULTIROW;
       123213  +  }
       123214  +  pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
       123215  +  if( pWInfo==0 ) goto update_cleanup;
       123216  +
       123217  +  /* A one-pass strategy that might update more than one row may not
       123218  +  ** be used if any column of the index used for the scan is being
       123219  +  ** updated. Otherwise, if there is an index on "b", statements like
       123220  +  ** the following could create an infinite loop:
       123221  +  **
       123222  +  **   UPDATE t1 SET b=b+1 WHERE b>?
       123223  +  **
       123224  +  ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
       123225  +  ** strategy that uses an index for which one or more columns are being
       123226  +  ** updated.  */
       123227  +  eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
       123228  +  if( eOnePass==ONEPASS_MULTI ){
       123229  +    int iCur = aiCurOnePass[1];
       123230  +    if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
       123231  +      eOnePass = ONEPASS_OFF;
       123232  +    }
       123233  +    assert( iCur!=iDataCur || !HasRowid(pTab) );
       123234  +  }
       123235  +  
       123236  +  if( HasRowid(pTab) ){
       123237  +    /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
       123238  +    ** mode, write the rowid into the FIFO. In either of the one-pass modes,
       123239  +    ** leave it in register regOldRowid.  */
       123240  +    sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
       123241  +    if( eOnePass==ONEPASS_OFF ){
       123242  +      sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
       123243  +    }
       123244  +  }else{
       123245  +    /* Read the PK of the current row into an array of registers. In
       123246  +    ** ONEPASS_OFF mode, serialize the array into a record and store it in
       123247  +    ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
       123248  +    ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table 
       123249  +    ** is not required) and leave the PK fields in the array of registers.  */
122865 123250       for(i=0; i<nPk; i++){
122866 123251         assert( pPk->aiColumn[i]>=0 );
122867         -      sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
122868         -                                      iPk+i);
       123252  +      sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
122869 123253       }
122870         -    if( okOnePass ){
       123254  +    if( eOnePass ){
122871 123255         sqlite3VdbeChangeToNoop(v, addrOpen);
122872 123256         nKey = nPk;
122873 123257         regKey = iPk;
122874 123258       }else{
122875 123259         sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
122876 123260                           sqlite3IndexAffinityStr(db, pPk), nPk);
122877 123261         sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
122878 123262       }
122879         -    sqlite3WhereEnd(pWInfo);
122880 123263     }
122881 123264   
122882         -  /* Initialize the count of updated rows
122883         -  */
122884         -  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
122885         -    regRowCount = ++pParse->nMem;
122886         -    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
       123265  +  if( eOnePass!=ONEPASS_MULTI ){
       123266  +    sqlite3WhereEnd(pWInfo);
122887 123267     }
122888 123268   
122889 123269     labelBreak = sqlite3VdbeMakeLabel(v);
122890 123270     if( !isView ){
122891         -    /* 
122892         -    ** Open every index that needs updating.  Note that if any
122893         -    ** index could potentially invoke a REPLACE conflict resolution 
122894         -    ** action, then we need to open all indices because we might need
122895         -    ** to be deleting some records.
122896         -    */
122897         -    if( onError==OE_Replace ){
122898         -      memset(aToOpen, 1, nIdx+1);
122899         -    }else{
122900         -      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122901         -        if( pIdx->onError==OE_Replace ){
122902         -          memset(aToOpen, 1, nIdx+1);
122903         -          break;
122904         -        }
122905         -      }
122906         -    }
122907         -    if( okOnePass ){
       123271  +    int addrOnce = 0;
       123272  +
       123273  +    /* Open every index that needs updating. */
       123274  +    if( eOnePass!=ONEPASS_OFF ){
122908 123275         if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
122909 123276         if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
122910 123277       }
       123278  +
       123279  +    if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
       123280  +      addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
       123281  +    }
122911 123282       sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen,
122912 123283                                  0, 0);
       123284  +    if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
122913 123285     }
122914 123286   
122915 123287     /* Top of the update loop */
122916         -  if( okOnePass ){
122917         -    if( aToOpen[iDataCur-iBaseCur] && !isView ){
       123288  +  if( eOnePass!=ONEPASS_OFF ){
       123289  +    if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
122918 123290         assert( pPk );
122919 123291         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
122920 123292         VdbeCoverageNeverTaken(v);
122921 123293       }
122922         -    labelContinue = labelBreak;
       123294  +    if( eOnePass==ONEPASS_SINGLE ){
       123295  +      labelContinue = labelBreak;
       123296  +    }else{
       123297  +      labelContinue = sqlite3VdbeMakeLabel(v);
       123298  +    }
122923 123299       sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
122924 123300       VdbeCoverageIf(v, pPk==0);
122925 123301       VdbeCoverageIf(v, pPk!=0);
122926 123302     }else if( pPk ){
122927 123303       labelContinue = sqlite3VdbeMakeLabel(v);
122928 123304       sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
122929 123305       addrTop = sqlite3VdbeAddOp2(v, OP_RowData, iEph, regKey);
................................................................................
123040 123416           sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
123041 123417         }
123042 123418       }
123043 123419     }
123044 123420   
123045 123421     if( !isView ){
123046 123422       int addr1 = 0;        /* Address of jump instruction */
123047         -    int bReplace = 0;     /* True if REPLACE conflict resolution might happen */
123048 123423   
123049 123424       /* Do constraint checks. */
123050 123425       assert( regOldRowid>0 );
123051 123426       sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
123052 123427           regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
123053 123428           aXRef);
123054 123429   
................................................................................
123076 123451       ** pre-update hook. If the caller invokes preupdate_new(), the returned
123077 123452       ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
123078 123453       ** is the column index supplied by the user.
123079 123454       */
123080 123455       assert( regNew==regNewRowid+1 );
123081 123456   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
123082 123457       sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
123083         -        OPFLAG_ISUPDATE | ((hasFK || chngKey || pPk!=0) ? 0 : OPFLAG_ISNOOP),
       123458  +        OPFLAG_ISUPDATE | ((hasFK || chngKey) ? 0 : OPFLAG_ISNOOP),
123084 123459           regNewRowid
123085 123460       );
       123461  +    if( eOnePass==ONEPASS_MULTI ){
       123462  +      assert( hasFK==0 && chngKey==0 );
       123463  +      sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
       123464  +    }
123086 123465       if( !pParse->nested ){
123087 123466         sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
123088 123467       }
123089 123468   #else
123090         -    if( hasFK || chngKey || pPk!=0 ){
       123469  +    if( hasFK || chngKey ){
123091 123470         sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
123092 123471       }
123093 123472   #endif
123094 123473       if( bReplace || chngKey ){
123095 123474         sqlite3VdbeJumpHere(v, addr1);
123096 123475       }
123097 123476   
123098 123477       if( hasFK ){
123099 123478         sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
123100 123479       }
123101 123480     
123102 123481       /* Insert the new index entries and the new record. */
123103         -    sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
123104         -                             regNewRowid, aRegIdx, 1, 0, 0);
       123482  +    sqlite3CompleteInsertion(
       123483  +        pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx, 
       123484  +        OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0), 
       123485  +        0, 0
       123486  +    );
123105 123487   
123106 123488       /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
123107 123489       ** handle rows (possibly in other tables) that refer via a foreign key
123108 123490       ** to the row just updated. */ 
123109 123491       if( hasFK ){
123110 123492         sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
123111 123493       }
................................................................................
123119 123501   
123120 123502     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
123121 123503         TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
123122 123504   
123123 123505     /* Repeat the above with the next record to be updated, until
123124 123506     ** all record selected by the WHERE clause have been updated.
123125 123507     */
123126         -  if( okOnePass ){
       123508  +  if( eOnePass==ONEPASS_SINGLE ){
123127 123509       /* Nothing to do at end-of-loop for a single-pass */
       123510  +  }else if( eOnePass==ONEPASS_MULTI ){
       123511  +    sqlite3VdbeResolveLabel(v, labelContinue);
       123512  +    sqlite3WhereEnd(pWInfo);
123128 123513     }else if( pPk ){
123129 123514       sqlite3VdbeResolveLabel(v, labelContinue);
123130 123515       sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
123131 123516     }else{
123132 123517       sqlite3VdbeGoto(v, labelContinue);
123133 123518     }
123134 123519     sqlite3VdbeResolveLabel(v, labelBreak);
................................................................................
127040 127425         testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
127041 127426       }
127042 127427   
127043 127428       /* Seek the table cursor, if required */
127044 127429       if( omitTable ){
127045 127430         /* pIdx is a covering index.  No need to access the main table. */
127046 127431       }else if( HasRowid(pIdx->pTable) ){
127047         -      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  +      )){
127048 127436           iRowidReg = ++pParse->nMem;
127049 127437           sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
127050 127438           sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
127051 127439           sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
127052 127440           VdbeCoverage(v);
127053 127441         }else{
127054 127442           codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
................................................................................
128404 128792     Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
128405 128793     int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
128406 128794     int noCase = 0;                  /* uppercase equivalent to lowercase */
128407 128795     int op;                          /* Top-level operator.  pExpr->op */
128408 128796     Parse *pParse = pWInfo->pParse;  /* Parsing context */
128409 128797     sqlite3 *db = pParse->db;        /* Database connection */
128410 128798     unsigned char eOp2;              /* op2 value for LIKE/REGEXP/GLOB */
       128799  +  int nLeft;                       /* Number of elements on left side vector */
128411 128800   
128412 128801     if( db->mallocFailed ){
128413 128802       return;
128414 128803     }
128415 128804     pTerm = &pWC->a[idxTerm];
128416 128805     pMaskSet = &pWInfo->sMaskSet;
128417 128806     pExpr = pTerm->pExpr;
................................................................................
128433 128822     }
128434 128823     prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr);
128435 128824     if( ExprHasProperty(pExpr, EP_FromJoin) ){
128436 128825       Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
128437 128826       prereqAll |= x;
128438 128827       extraRight = x-1;  /* ON clause terms may not be used with an index
128439 128828                          ** on left table of a LEFT JOIN.  Ticket #3015 */
       128829  +    if( (prereqAll>>1)>=x ){
       128830  +      sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
       128831  +      return;
       128832  +    }
128440 128833     }
128441 128834     pTerm->prereqAll = prereqAll;
128442 128835     pTerm->leftCursor = -1;
128443 128836     pTerm->iParent = -1;
128444 128837     pTerm->eOperator = 0;
128445 128838     if( allowedOp(op) ){
128446 128839       int iCur, iColumn;
................................................................................
128675 129068     ** new terms completely replace the original vector comparison, which is
128676 129069     ** no longer used.
128677 129070     **
128678 129071     ** This is only required if at least one side of the comparison operation
128679 129072     ** is not a sub-select.  */
128680 129073     if( pWC->op==TK_AND 
128681 129074     && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
128682         -  && sqlite3ExprIsVector(pExpr->pLeft)
       129075  +  && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
       129076  +  && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
128683 129077     && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 
128684         -    || (pExpr->pRight->flags & EP_xIsSelect)==0
128685         -  )){
128686         -    int nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
       129078  +    || (pExpr->pRight->flags & EP_xIsSelect)==0)
       129079  +  ){
128687 129080       int i;
128688         -    assert( nLeft==sqlite3ExprVectorSize(pExpr->pRight) );
128689 129081       for(i=0; i<nLeft; i++){
128690 129082         int idxNew;
128691 129083         Expr *pNew;
128692 129084         Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
128693 129085         Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
128694 129086   
128695 129087         pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
................................................................................
129243 129635     pScan->idxaff = 0;
129244 129636     pScan->zCollName = 0;
129245 129637     if( pIdx ){
129246 129638       int j = iColumn;
129247 129639       iColumn = pIdx->aiColumn[j];
129248 129640       if( iColumn==XN_EXPR ){
129249 129641         pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
       129642  +      pScan->zCollName = pIdx->azColl[j];
129250 129643       }else if( iColumn==pIdx->pTable->iPKey ){
129251 129644         iColumn = XN_ROWID;
129252 129645       }else if( iColumn>=0 ){
129253 129646         pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
129254 129647         pScan->zCollName = pIdx->azColl[j];
129255 129648       }
129256 129649     }else if( iColumn==XN_EXPR ){
................................................................................
133884 134277               assert( x>=0 );
133885 134278             }
133886 134279             x = sqlite3ColumnOfIndex(pIdx, x);
133887 134280             if( x>=0 ){
133888 134281               pOp->p2 = x;
133889 134282               pOp->p1 = pLevel->iIdxCur;
133890 134283             }
133891         -          assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
       134284  +          assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 
       134285  +              || pWInfo->eOnePass );
133892 134286           }else if( pOp->opcode==OP_Rowid ){
133893 134287             pOp->p1 = pLevel->iIdxCur;
133894 134288             pOp->opcode = OP_IdxRowid;
133895 134289           }
133896 134290         }
133897 134291       }
133898 134292     }
................................................................................
133948 134342   
133949 134343   /*
133950 134344   ** Indicate that sqlite3ParserFree() will never be called with a null
133951 134345   ** pointer.
133952 134346   */
133953 134347   #define YYPARSEFREENEVERNULL 1
133954 134348   
       134349  +/*
       134350  +** In the amalgamation, the parse.c file generated by lemon and the
       134351  +** tokenize.c file are concatenated.  In that case, sqlite3RunParser()
       134352  +** has access to the the size of the yyParser object and so the parser
       134353  +** engine can be allocated from stack.  In that case, only the
       134354  +** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
       134355  +** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
       134356  +** omitted.
       134357  +*/
       134358  +#ifdef SQLITE_AMALGAMATION
       134359  +# define sqlite3Parser_ENGINEALWAYSONSTACK 1
       134360  +#endif
       134361  +
133955 134362   /*
133956 134363   ** Alternative datatype for the argument to the malloc() routine passed
133957 134364   ** into sqlite3ParserAlloc().  The default is size_t.
133958 134365   */
133959 134366   #define YYMALLOCARGTYPE  u64
133960 134367   
133961 134368   /*
................................................................................
135396 135803   ** putting an appropriate #define in the %include section of the input
135397 135804   ** grammar.
135398 135805   */
135399 135806   #ifndef YYMALLOCARGTYPE
135400 135807   # define YYMALLOCARGTYPE size_t
135401 135808   #endif
135402 135809   
       135810  +/* Initialize a new parser that has already been allocated.
       135811  +*/
       135812  +SQLITE_PRIVATE void sqlite3ParserInit(void *yypParser){
       135813  +  yyParser *pParser = (yyParser*)yypParser;
       135814  +#ifdef YYTRACKMAXSTACKDEPTH
       135815  +  pParser->yyhwm = 0;
       135816  +#endif
       135817  +#if YYSTACKDEPTH<=0
       135818  +  pParser->yytos = NULL;
       135819  +  pParser->yystack = NULL;
       135820  +  pParser->yystksz = 0;
       135821  +  if( yyGrowStack(pParser) ){
       135822  +    pParser->yystack = &pParser->yystk0;
       135823  +    pParser->yystksz = 1;
       135824  +  }
       135825  +#endif
       135826  +#ifndef YYNOERRORRECOVERY
       135827  +  pParser->yyerrcnt = -1;
       135828  +#endif
       135829  +  pParser->yytos = pParser->yystack;
       135830  +  pParser->yystack[0].stateno = 0;
       135831  +  pParser->yystack[0].major = 0;
       135832  +}
       135833  +
       135834  +#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135403 135835   /* 
135404 135836   ** This function allocates a new parser.
135405 135837   ** The only argument is a pointer to a function which works like
135406 135838   ** malloc.
135407 135839   **
135408 135840   ** Inputs:
135409 135841   ** A pointer to the function used to allocate memory.
................................................................................
135411 135843   ** Outputs:
135412 135844   ** A pointer to a parser.  This pointer is used in subsequent calls
135413 135845   ** to sqlite3Parser and sqlite3ParserFree.
135414 135846   */
135415 135847   SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){
135416 135848     yyParser *pParser;
135417 135849     pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
135418         -  if( pParser ){
135419         -#ifdef YYTRACKMAXSTACKDEPTH
135420         -    pParser->yyhwm = 0;
135421         -#endif
135422         -#if YYSTACKDEPTH<=0
135423         -    pParser->yytos = NULL;
135424         -    pParser->yystack = NULL;
135425         -    pParser->yystksz = 0;
135426         -    if( yyGrowStack(pParser) ){
135427         -      pParser->yystack = &pParser->yystk0;
135428         -      pParser->yystksz = 1;
135429         -    }
135430         -#endif
135431         -#ifndef YYNOERRORRECOVERY
135432         -    pParser->yyerrcnt = -1;
135433         -#endif
135434         -    pParser->yytos = pParser->yystack;
135435         -    pParser->yystack[0].stateno = 0;
135436         -    pParser->yystack[0].major = 0;
135437         -  }
       135850  +  if( pParser ) sqlite3ParserInit(pParser);
135438 135851     return pParser;
135439 135852   }
       135853  +#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
       135854  +
135440 135855   
135441 135856   /* The following function deletes the "minor type" or semantic value
135442 135857   ** associated with a symbol.  The symbol can be either a terminal
135443 135858   ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
135444 135859   ** a pointer to the value to be deleted.  The code used to do the 
135445 135860   ** deletions is derived from the %destructor and/or %token_destructor
135446 135861   ** directives of the input grammar.
................................................................................
135558 135973         yyTracePrompt,
135559 135974         yyTokenName[yytos->major]);
135560 135975     }
135561 135976   #endif
135562 135977     yy_destructor(pParser, yytos->major, &yytos->minor);
135563 135978   }
135564 135979   
       135980  +/*
       135981  +** Clear all secondary memory allocations from the parser
       135982  +*/
       135983  +SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
       135984  +  yyParser *pParser = (yyParser*)p;
       135985  +  while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
       135986  +#if YYSTACKDEPTH<=0
       135987  +  if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
       135988  +#endif
       135989  +}
       135990  +
       135991  +#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135565 135992   /* 
135566 135993   ** Deallocate and destroy a parser.  Destructors are called for
135567 135994   ** all stack elements before shutting the parser down.
135568 135995   **
135569 135996   ** If the YYPARSEFREENEVERNULL macro exists (for example because it
135570 135997   ** is defined in a %include section of the input grammar) then it is
135571 135998   ** assumed that the input pointer is never NULL.
135572 135999   */
135573 136000   SQLITE_PRIVATE void sqlite3ParserFree(
135574 136001     void *p,                    /* The parser to be deleted */
135575 136002     void (*freeProc)(void*)     /* Function used to reclaim memory */
135576 136003   ){
135577         -  yyParser *pParser = (yyParser*)p;
135578 136004   #ifndef YYPARSEFREENEVERNULL
135579         -  if( pParser==0 ) return;
       136005  +  if( p==0 ) return;
135580 136006   #endif
135581         -  while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
135582         -#if YYSTACKDEPTH<=0
135583         -  if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
135584         -#endif
135585         -  (*freeProc)((void*)pParser);
       136007  +  sqlite3ParserFinalize(p);
       136008  +  (*freeProc)(p);
135586 136009   }
       136010  +#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
135587 136011   
135588 136012   /*
135589 136013   ** Return the peak depth of the stack for a parser.
135590 136014   */
135591 136015   #ifdef YYTRACKMAXSTACKDEPTH
135592 136016   SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
135593 136017     yyParser *pParser = (yyParser*)p;
................................................................................
138433 138857     int nErr = 0;                   /* Number of errors encountered */
138434 138858     int i;                          /* Loop counter */
138435 138859     void *pEngine;                  /* The LEMON-generated LALR(1) parser */
138436 138860     int tokenType;                  /* type of the next token */
138437 138861     int lastTokenParsed = -1;       /* type of the previous token */
138438 138862     sqlite3 *db = pParse->db;       /* The database connection */
138439 138863     int mxSqlLen;                   /* Max length of an SQL string */
       138864  +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
       138865  +  unsigned char zSpace[sizeof(yyParser)];  /* Space for parser engine object */
       138866  +#endif
138440 138867   
138441 138868     assert( zSql!=0 );
138442 138869     mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
138443 138870     if( db->nVdbeActive==0 ){
138444 138871       db->u1.isInterrupted = 0;
138445 138872     }
138446 138873     pParse->rc = SQLITE_OK;
138447 138874     pParse->zTail = zSql;
138448 138875     i = 0;
138449 138876     assert( pzErrMsg!=0 );
138450 138877     /* sqlite3ParserTrace(stdout, "parser: "); */
       138878  +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
       138879  +  pEngine = zSpace;
       138880  +  sqlite3ParserInit(pEngine);
       138881  +#else
138451 138882     pEngine = sqlite3ParserAlloc(sqlite3Malloc);
138452 138883     if( pEngine==0 ){
138453 138884       sqlite3OomFault(db);
138454 138885       return SQLITE_NOMEM_BKPT;
138455 138886     }
       138887  +#endif
138456 138888     assert( pParse->pNewTable==0 );
138457 138889     assert( pParse->pNewTrigger==0 );
138458 138890     assert( pParse->nVar==0 );
138459 138891     assert( pParse->pVList==0 );
138460 138892     while( 1 ){
138461 138893       assert( i>=0 );
138462 138894       if( zSql[i]!=0 ){
................................................................................
138500 138932   #ifdef YYTRACKMAXSTACKDEPTH
138501 138933     sqlite3_mutex_enter(sqlite3MallocMutex());
138502 138934     sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
138503 138935         sqlite3ParserStackPeak(pEngine)
138504 138936     );
138505 138937     sqlite3_mutex_leave(sqlite3MallocMutex());
138506 138938   #endif /* YYDEBUG */
       138939  +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
       138940  +  sqlite3ParserFinalize(pEngine);
       138941  +#else
138507 138942     sqlite3ParserFree(pEngine, sqlite3_free);
       138943  +#endif
138508 138944     if( db->mallocFailed ){
138509 138945       pParse->rc = SQLITE_NOMEM_BKPT;
138510 138946     }
138511 138947     if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
138512 138948       pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
138513 138949     }
138514 138950     assert( pzErrMsg!=0 );
................................................................................
144163 144599     int nAutoincrmerge;             /* Value configured by 'automerge' */
144164 144600     u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
144165 144601   
144166 144602     /* Precompiled statements used by the implementation. Each of these 
144167 144603     ** statements is run and reset within a single virtual table API call. 
144168 144604     */
144169 144605     sqlite3_stmt *aStmt[40];
       144606  +  sqlite3_stmt *pSeekStmt;        /* Cache for fts3CursorSeekStmt() */
144170 144607   
144171 144608     char *zReadExprlist;
144172 144609     char *zWriteExprlist;
144173 144610   
144174 144611     int nNodeSize;                  /* Soft limit for node size */
144175 144612     u8 bFts4;                       /* True for FTS4, false for FTS3 */
144176 144613     u8 bHasStat;                    /* True if %_stat table exists (2==unknown) */
................................................................................
144232 144669   ** the xOpen method. Cursors are destroyed using the xClose method.
144233 144670   */
144234 144671   struct Fts3Cursor {
144235 144672     sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
144236 144673     i16 eSearch;                    /* Search strategy (see below) */
144237 144674     u8 isEof;                       /* True if at End Of Results */
144238 144675     u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
       144676  +  u8 bSeekStmt;                   /* True if pStmt is a seek */
144239 144677     sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
144240 144678     Fts3Expr *pExpr;                /* Parsed MATCH query string */
144241 144679     int iLangid;                    /* Language being queried for */
144242 144680     int nPhrase;                    /* Number of matchable phrases in query */
144243 144681     Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
144244 144682     sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
144245 144683     char *pNextId;                  /* Pointer into the body of aDoclist */
................................................................................
144754 145192     Fts3Table *p = (Fts3Table *)pVtab;
144755 145193     int i;
144756 145194   
144757 145195     assert( p->nPendingData==0 );
144758 145196     assert( p->pSegments==0 );
144759 145197   
144760 145198     /* Free any prepared statements held */
       145199  +  sqlite3_finalize(p->pSeekStmt);
144761 145200     for(i=0; i<SizeofArray(p->aStmt); i++){
144762 145201       sqlite3_finalize(p->aStmt[i]);
144763 145202     }
144764 145203     sqlite3_free(p->zSegmentsTbl);
144765 145204     sqlite3_free(p->zReadExprlist);
144766 145205     sqlite3_free(p->zWriteExprlist);
144767 145206     sqlite3_free(p->zContentTbl);
................................................................................
145625 146064     p->db = db;
145626 146065     p->nColumn = nCol;
145627 146066     p->nPendingData = 0;
145628 146067     p->azColumn = (char **)&p[1];
145629 146068     p->pTokenizer = pTokenizer;
145630 146069     p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
145631 146070     p->bHasDocsize = (isFts4 && bNoDocsize==0);
145632         -  p->bHasStat = isFts4;
145633         -  p->bFts4 = isFts4;
145634         -  p->bDescIdx = bDescIdx;
       146071  +  p->bHasStat = (u8)isFts4;
       146072  +  p->bFts4 = (u8)isFts4;
       146073  +  p->bDescIdx = (u8)bDescIdx;
145635 146074     p->nAutoincrmerge = 0xff;   /* 0xff means setting unknown */
145636 146075     p->zContentTbl = zContent;
145637 146076     p->zLanguageid = zLanguageid;
145638 146077     zContent = 0;
145639 146078     zLanguageid = 0;
145640 146079     TESTONLY( p->inTransaction = -1 );
145641 146080     TESTONLY( p->mxSavepoint = -1 );
................................................................................
145941 146380     *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
145942 146381     if( !pCsr ){
145943 146382       return SQLITE_NOMEM;
145944 146383     }
145945 146384     memset(pCsr, 0, sizeof(Fts3Cursor));
145946 146385     return SQLITE_OK;
145947 146386   }
       146387  +
       146388  +/*
       146389  +** Finalize the statement handle at pCsr->pStmt.
       146390  +**
       146391  +** Or, if that statement handle is one created by fts3CursorSeekStmt(),
       146392  +** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
       146393  +** pointer there instead of finalizing it.
       146394  +*/
       146395  +static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
       146396  +  if( pCsr->bSeekStmt ){
       146397  +    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
       146398  +    if( p->pSeekStmt==0 ){
       146399  +      p->pSeekStmt = pCsr->pStmt;
       146400  +      sqlite3_reset(pCsr->pStmt);
       146401  +      pCsr->pStmt = 0;
       146402  +    }
       146403  +    pCsr->bSeekStmt = 0;
       146404  +  }
       146405  +  sqlite3_finalize(pCsr->pStmt);
       146406  +}
145948 146407   
145949 146408   /*
145950 146409   ** Close the cursor.  For additional information see the documentation
145951 146410   ** on the xClose method of the virtual table interface.
145952 146411   */
145953 146412   static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
145954 146413     Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
145955 146414     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
145956         -  sqlite3_finalize(pCsr->pStmt);
       146415  +  fts3CursorFinalizeStmt(pCsr);
145957 146416     sqlite3Fts3ExprFree(pCsr->pExpr);
145958 146417     sqlite3Fts3FreeDeferredTokens(pCsr);
145959 146418     sqlite3_free(pCsr->aDoclist);
145960 146419     sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
145961 146420     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
145962 146421     sqlite3_free(pCsr);
145963 146422     return SQLITE_OK;
................................................................................
145967 146426   ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
145968 146427   ** compose and prepare an SQL statement of the form:
145969 146428   **
145970 146429   **    "SELECT <columns> FROM %_content WHERE rowid = ?"
145971 146430   **
145972 146431   ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
145973 146432   ** it. If an error occurs, return an SQLite error code.
145974         -**
145975         -** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
145976 146433   */
145977         -static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
       146434  +static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
145978 146435     int rc = SQLITE_OK;
145979 146436     if( pCsr->pStmt==0 ){
145980 146437       Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
145981 146438       char *zSql;
145982         -    zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
145983         -    if( !zSql ) return SQLITE_NOMEM;
145984         -    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
145985         -    sqlite3_free(zSql);
       146439  +    if( p->pSeekStmt ){
       146440  +      pCsr->pStmt = p->pSeekStmt;
       146441  +      p->pSeekStmt = 0;
       146442  +    }else{
       146443  +      zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
       146444  +      if( !zSql ) return SQLITE_NOMEM;
       146445  +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
       146446  +      sqlite3_free(zSql);
       146447  +    }
       146448  +    if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
145986 146449     }
145987         -  *ppStmt = pCsr->pStmt;
145988 146450     return rc;
145989 146451   }
145990 146452   
145991 146453   /*
145992 146454   ** Position the pCsr->pStmt statement so that it is on the row
145993 146455   ** of the %_content table that contains the last match.  Return
145994 146456   ** SQLITE_OK on success.  
145995 146457   */
145996 146458   static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
145997 146459     int rc = SQLITE_OK;
145998 146460     if( pCsr->isRequireSeek ){
145999         -    sqlite3_stmt *pStmt = 0;
146000         -
146001         -    rc = fts3CursorSeekStmt(pCsr, &pStmt);
       146461  +    rc = fts3CursorSeekStmt(pCsr);
146002 146462       if( rc==SQLITE_OK ){
146003 146463         sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
146004 146464         pCsr->isRequireSeek = 0;
146005 146465         if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
146006 146466           return SQLITE_OK;
146007 146467         }else{
146008 146468           rc = sqlite3_reset(pCsr->pStmt);
................................................................................
147452 147912     if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
147453 147913     if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
147454 147914     if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
147455 147915     if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
147456 147916     assert( iIdx==nVal );
147457 147917   
147458 147918     /* In case the cursor has been used before, clear it now. */
147459         -  sqlite3_finalize(pCsr->pStmt);
       147919  +  fts3CursorFinalizeStmt(pCsr);
147460 147920     sqlite3_free(pCsr->aDoclist);
147461 147921     sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
147462 147922     sqlite3Fts3ExprFree(pCsr->pExpr);
147463 147923     memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
147464 147924   
147465 147925     /* Set the lower and upper bounds on docids to return */
147466 147926     pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
................................................................................
147520 147980       if( zSql ){
147521 147981         rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
147522 147982         sqlite3_free(zSql);
147523 147983       }else{
147524 147984         rc = SQLITE_NOMEM;
147525 147985       }
147526 147986     }else if( eSearch==FTS3_DOCID_SEARCH ){
147527         -    rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
       147987  +    rc = fts3CursorSeekStmt(pCsr);
147528 147988       if( rc==SQLITE_OK ){
147529 147989         rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
147530 147990       }
147531 147991     }
147532 147992     if( rc!=SQLITE_OK ) return rc;
147533 147993   
147534 147994     return fts3NextMethod(pCursor);
................................................................................
147684 148144       char *zSql = sqlite3_mprintf(zFmt, p->zDb, p->zName);
147685 148145       if( zSql ){
147686 148146         sqlite3_stmt *pStmt = 0;
147687 148147         rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
147688 148148         if( rc==SQLITE_OK ){
147689 148149           int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW);
147690 148150           rc = sqlite3_finalize(pStmt);
147691         -        if( rc==SQLITE_OK ) p->bHasStat = bHasStat;
       148151  +        if( rc==SQLITE_OK ) p->bHasStat = (u8)bHasStat;
147692 148152         }
147693 148153         sqlite3_free(zSql);
147694 148154       }else{
147695 148155         rc = SQLITE_NOMEM;
147696 148156       }
147697 148157     }
147698 148158     return rc;
................................................................................
162540 163000   /* #include <string.h> */
162541 163001   /* #include <assert.h> */
162542 163002   /* #include <stdio.h> */
162543 163003   
162544 163004   #ifndef SQLITE_AMALGAMATION
162545 163005   #include "sqlite3rtree.h"
162546 163006   typedef sqlite3_int64 i64;
       163007  +typedef sqlite3_uint64 u64;
162547 163008   typedef unsigned char u8;
162548 163009   typedef unsigned short u16;
162549 163010   typedef unsigned int u32;
162550 163011   #endif
162551 163012   
162552 163013   /*  The following macro is used to suppress compiler warnings.
162553 163014   */
................................................................................
162588 163049   ** An rtree virtual-table object.
162589 163050   */
162590 163051   struct Rtree {
162591 163052     sqlite3_vtab base;          /* Base class.  Must be first */
162592 163053     sqlite3 *db;                /* Host database connection */
162593 163054     int iNodeSize;              /* Size in bytes of each node in the node table */
162594 163055     u8 nDim;                    /* Number of dimensions */
       163056  +  u8 nDim2;                   /* Twice the number of dimensions */
162595 163057     u8 eCoordType;              /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
162596 163058     u8 nBytesPerCell;           /* Bytes consumed per cell */
       163059  +  u8 inWrTrans;               /* True if inside write transaction */
162597 163060     int iDepth;                 /* Current depth of the r-tree structure */
162598 163061     char *zDb;                  /* Name of database containing r-tree table */
162599 163062     char *zName;                /* Name of r-tree table */ 
162600         -  int nBusy;                  /* Current number of users of this structure */
       163063  +  u32 nBusy;                  /* Current number of users of this structure */
162601 163064     i64 nRowEst;                /* Estimated number of rows in this table */
       163065  +  u32 nCursor;                /* Number of open cursors */
162602 163066   
162603 163067     /* List of nodes removed during a CondenseTree operation. List is
162604 163068     ** linked together via the pointer normally used for hash chains -
162605 163069     ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 
162606 163070     ** headed by the node (leaf nodes have RtreeNode.iNode==0).
162607 163071     */
162608 163072     RtreeNode *pDeleted;
162609 163073     int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
       163074  +
       163075  +  /* Blob I/O on xxx_node */
       163076  +  sqlite3_blob *pNodeBlob;
162610 163077   
162611 163078     /* Statements to read/write/delete a record from xxx_node */
162612         -  sqlite3_stmt *pReadNode;
162613 163079     sqlite3_stmt *pWriteNode;
162614 163080     sqlite3_stmt *pDeleteNode;
162615 163081   
162616 163082     /* Statements to read/write/delete a record from xxx_rowid */
162617 163083     sqlite3_stmt *pReadRowid;
162618 163084     sqlite3_stmt *pWriteRowid;
162619 163085     sqlite3_stmt *pDeleteRowid;
................................................................................
162834 163300   #ifndef MAX
162835 163301   # define MAX(x,y) ((x) < (y) ? (y) : (x))
162836 163302   #endif
162837 163303   #ifndef MIN
162838 163304   # define MIN(x,y) ((x) > (y) ? (y) : (x))
162839 163305   #endif
162840 163306   
       163307  +/* What version of GCC is being used.  0 means GCC is not being used */
       163308  +#ifndef GCC_VERSION
       163309  +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
       163310  +# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
       163311  +#else
       163312  +# define GCC_VERSION 0
       163313  +#endif
       163314  +#endif
       163315  +
       163316  +/* What version of CLANG is being used.  0 means CLANG is not being used */
       163317  +#ifndef CLANG_VERSION
       163318  +#if defined(__clang__) && !defined(_WIN32) && !defined(SQLITE_DISABLE_INTRINSIC)
       163319  +# define CLANG_VERSION \
       163320  +            (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
       163321  +#else
       163322  +# define CLANG_VERSION 0
       163323  +#endif
       163324  +#endif
       163325  +
       163326  +/* The testcase() macro should already be defined in the amalgamation.  If
       163327  +** it is not, make it a no-op.
       163328  +*/
       163329  +#ifndef SQLITE_AMALGAMATION
       163330  +# define testcase(X)
       163331  +#endif
       163332  +
       163333  +/*
       163334  +** Macros to determine whether the machine is big or little endian,
       163335  +** and whether or not that determination is run-time or compile-time.
       163336  +**
       163337  +** For best performance, an attempt is made to guess at the byte-order
       163338  +** using C-preprocessor macros.  If that is unsuccessful, or if
       163339  +** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
       163340  +** at run-time.
       163341  +*/
       163342  +#ifndef SQLITE_BYTEORDER
       163343  +#if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
       163344  +    defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
       163345  +    defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
       163346  +    defined(__arm__)
       163347  +# define SQLITE_BYTEORDER    1234
       163348  +#elif defined(sparc)    || defined(__ppc__)
       163349  +# define SQLITE_BYTEORDER    4321
       163350  +#else
       163351  +# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
       163352  +#endif
       163353  +#endif
       163354  +
       163355  +
       163356  +/* What version of MSVC is being used.  0 means MSVC is not being used */
       163357  +#ifndef MSVC_VERSION
       163358  +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
       163359  +# define MSVC_VERSION _MSC_VER
       163360  +#else
       163361  +# define MSVC_VERSION 0
       163362  +#endif
       163363  +#endif
       163364  +
162841 163365   /*
162842 163366   ** Functions to deserialize a 16 bit integer, 32 bit real number and
162843 163367   ** 64 bit integer. The deserialized value is returned.
162844 163368   */
162845 163369   static int readInt16(u8 *p){
162846 163370     return (p[0]<<8) + p[1];
162847 163371   }
162848 163372   static void readCoord(u8 *p, RtreeCoord *pCoord){
       163373  +  assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
       163374  +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
       163375  +  pCoord->u = _byteswap_ulong(*(u32*)p);
       163376  +#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163377  +  pCoord->u = __builtin_bswap32(*(u32*)p);
       163378  +#elif SQLITE_BYTEORDER==4321
       163379  +  pCoord->u = *(u32*)p;
       163380  +#else
162849 163381     pCoord->u = (
162850 163382       (((u32)p[0]) << 24) + 
162851 163383       (((u32)p[1]) << 16) + 
162852 163384       (((u32)p[2]) <<  8) + 
162853 163385       (((u32)p[3]) <<  0)
162854 163386     );
       163387  +#endif
162855 163388   }
162856 163389   static i64 readInt64(u8 *p){
       163390  +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
       163391  +  u64 x;
       163392  +  memcpy(&x, p, 8);
       163393  +  return (i64)_byteswap_uint64(x);
       163394  +#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163395  +  u64 x;
       163396  +  memcpy(&x, p, 8);
       163397  +  return (i64)__builtin_bswap64(x);
       163398  +#elif SQLITE_BYTEORDER==4321
       163399  +  i64 x;
       163400  +  memcpy(&x, p, 8);
       163401  +  return x;
       163402  +#else
162857 163403     return (
162858 163404       (((i64)p[0]) << 56) + 
162859 163405       (((i64)p[1]) << 48) + 
162860 163406       (((i64)p[2]) << 40) + 
162861 163407       (((i64)p[3]) << 32) + 
162862 163408       (((i64)p[4]) << 24) + 
162863 163409       (((i64)p[5]) << 16) + 
162864 163410       (((i64)p[6]) <<  8) + 
162865 163411       (((i64)p[7]) <<  0)
162866 163412     );
       163413  +#endif
162867 163414   }
162868 163415   
162869 163416   /*
162870 163417   ** Functions to serialize a 16 bit integer, 32 bit real number and
162871 163418   ** 64 bit integer. The value returned is the number of bytes written
162872 163419   ** to the argument buffer (always 2, 4 and 8 respectively).
162873 163420   */
162874         -static int writeInt16(u8 *p, int i){
       163421  +static void writeInt16(u8 *p, int i){
162875 163422     p[0] = (i>> 8)&0xFF;
162876 163423     p[1] = (i>> 0)&0xFF;
162877         -  return 2;
162878 163424   }
162879 163425   static int writeCoord(u8 *p, RtreeCoord *pCoord){
162880 163426     u32 i;
       163427  +  assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
162881 163428     assert( sizeof(RtreeCoord)==4 );
162882 163429     assert( sizeof(u32)==4 );
       163430  +#if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163431  +  i = __builtin_bswap32(pCoord->u);
       163432  +  memcpy(p, &i, 4);
       163433  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
       163434  +  i = _byteswap_ulong(pCoord->u);
       163435  +  memcpy(p, &i, 4);
       163436  +#elif SQLITE_BYTEORDER==4321
       163437  +  i = pCoord->u;
       163438  +  memcpy(p, &i, 4);
       163439  +#else
162883 163440     i = pCoord->u;
162884 163441     p[0] = (i>>24)&0xFF;
162885 163442     p[1] = (i>>16)&0xFF;
162886 163443     p[2] = (i>> 8)&0xFF;
162887 163444     p[3] = (i>> 0)&0xFF;
       163445  +#endif
162888 163446     return 4;
162889 163447   }
162890 163448   static int writeInt64(u8 *p, i64 i){
       163449  +#if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163450  +  i = (i64)__builtin_bswap64((u64)i);
       163451  +  memcpy(p, &i, 8);
       163452  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
       163453  +  i = (i64)_byteswap_uint64((u64)i);
       163454  +  memcpy(p, &i, 8);
       163455  +#elif SQLITE_BYTEORDER==4321
       163456  +  memcpy(p, &i, 8);
       163457  +#else
162891 163458     p[0] = (i>>56)&0xFF;
162892 163459     p[1] = (i>>48)&0xFF;
162893 163460     p[2] = (i>>40)&0xFF;
162894 163461     p[3] = (i>>32)&0xFF;
162895 163462     p[4] = (i>>24)&0xFF;
162896 163463     p[5] = (i>>16)&0xFF;
162897 163464     p[6] = (i>> 8)&0xFF;
162898 163465     p[7] = (i>> 0)&0xFF;
       163466  +#endif
162899 163467     return 8;
162900 163468   }
162901 163469   
162902 163470   /*
162903 163471   ** Increment the reference count of node p.
162904 163472   */
162905 163473   static void nodeReference(RtreeNode *p){
................................................................................
162973 163541       pNode->nRef = 1;
162974 163542       pNode->pParent = pParent;
162975 163543       pNode->isDirty = 1;
162976 163544       nodeReference(pParent);
162977 163545     }
162978 163546     return pNode;
162979 163547   }
       163548  +
       163549  +/*
       163550  +** Clear the Rtree.pNodeBlob object
       163551  +*/
       163552  +static void nodeBlobReset(Rtree *pRtree){
       163553  +  if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
       163554  +    sqlite3_blob *pBlob = pRtree->pNodeBlob;
       163555  +    pRtree->pNodeBlob = 0;
       163556  +    sqlite3_blob_close(pBlob);
       163557  +  }
       163558  +}
162980 163559   
162981 163560   /*
162982 163561   ** Obtain a reference to an r-tree node.
162983 163562   */
162984 163563   static int nodeAcquire(
162985 163564     Rtree *pRtree,             /* R-tree structure */
162986 163565     i64 iNode,                 /* Node number to load */
162987 163566     RtreeNode *pParent,        /* Either the parent node or NULL */
162988 163567     RtreeNode **ppNode         /* OUT: Acquired node */
162989 163568   ){
162990         -  int rc;
162991         -  int rc2 = SQLITE_OK;
162992         -  RtreeNode *pNode;
       163569  +  int rc = SQLITE_OK;
       163570  +  RtreeNode *pNode = 0;
162993 163571   
162994 163572     /* Check if the requested node is already in the hash table. If so,
162995 163573     ** increase its reference count and return it.
162996 163574     */
162997 163575     if( (pNode = nodeHashLookup(pRtree, iNode)) ){
162998 163576       assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
162999 163577       if( pParent && !pNode->pParent ){
................................................................................
163001 163579         pNode->pParent = pParent;
163002 163580       }
163003 163581       pNode->nRef++;
163004 163582       *ppNode = pNode;
163005 163583       return SQLITE_OK;
163006 163584     }
163007 163585   
163008         -  sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
163009         -  rc = sqlite3_step(pRtree->pReadNode);
163010         -  if( rc==SQLITE_ROW ){
163011         -    const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
163012         -    if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
163013         -      pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
163014         -      if( !pNode ){
163015         -        rc2 = SQLITE_NOMEM;
163016         -      }else{
163017         -        pNode->pParent = pParent;
163018         -        pNode->zData = (u8 *)&pNode[1];
163019         -        pNode->nRef = 1;
163020         -        pNode->iNode = iNode;
163021         -        pNode->isDirty = 0;
163022         -        pNode->pNext = 0;
163023         -        memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
163024         -        nodeReference(pParent);
163025         -      }
163026         -    }
163027         -  }
163028         -  rc = sqlite3_reset(pRtree->pReadNode);
163029         -  if( rc==SQLITE_OK ) rc = rc2;
       163586  +  if( pRtree->pNodeBlob ){
       163587  +    sqlite3_blob *pBlob = pRtree->pNodeBlob;
       163588  +    pRtree->pNodeBlob = 0;
       163589  +    rc = sqlite3_blob_reopen(pBlob, iNode);
       163590  +    pRtree->pNodeBlob = pBlob;
       163591  +    if( rc ){
       163592  +      nodeBlobReset(pRtree);
       163593  +      if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
       163594  +    }
       163595  +  }
       163596  +  if( pRtree->pNodeBlob==0 ){
       163597  +    char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
       163598  +    if( zTab==0 ) return SQLITE_NOMEM;
       163599  +    rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
       163600  +                           &pRtree->pNodeBlob);
       163601  +    sqlite3_free(zTab);
       163602  +  }
       163603  +  if( rc ){
       163604  +    nodeBlobReset(pRtree);
       163605  +    *ppNode = 0;
       163606  +    /* If unable to open an sqlite3_blob on the desired row, that can only
       163607  +    ** be because the shadow tables hold erroneous data. */
       163608  +    if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB;
       163609  +  }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
       163610  +    pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
       163611  +    if( !pNode ){
       163612  +      rc = SQLITE_NOMEM;
       163613  +    }else{
       163614  +      pNode->pParent = pParent;
       163615  +      pNode->zData = (u8 *)&pNode[1];
       163616  +      pNode->nRef = 1;
       163617  +      pNode->iNode = iNode;
       163618  +      pNode->isDirty = 0;
       163619  +      pNode->pNext = 0;
       163620  +      rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
       163621  +                             pRtree->iNodeSize, 0);
       163622  +      nodeReference(pParent);
       163623  +    }
       163624  +  }
163030 163625   
163031 163626     /* If the root node was just loaded, set pRtree->iDepth to the height
163032 163627     ** of the r-tree structure. A height of zero means all data is stored on
163033 163628     ** the root node. A height of one means the children of the root node
163034 163629     ** are the leaves, and so on. If the depth as specified on the root node
163035 163630     ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
163036 163631     */
................................................................................
163074 163669     RtreeNode *pNode,          /* The node into which the cell is to be written */
163075 163670     RtreeCell *pCell,          /* The cell to write */
163076 163671     int iCell                  /* Index into pNode into which pCell is written */
163077 163672   ){
163078 163673     int ii;
163079 163674     u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
163080 163675     p += writeInt64(p, pCell->iRowid);
163081         -  for(ii=0; ii<(pRtree->nDim*2); ii++){
       163676  +  for(ii=0; ii<pRtree->nDim2; ii++){
163082 163677       p += writeCoord(p, &pCell->aCoord[ii]);
163083 163678     }
163084 163679     pNode->isDirty = 1;
163085 163680   }
163086 163681   
163087 163682   /*
163088 163683   ** Remove the cell with index iCell from node pNode.
................................................................................
163208 163803     Rtree *pRtree,               /* The overall R-Tree */
163209 163804     RtreeNode *pNode,            /* The node containing the cell to be read */
163210 163805     int iCell,                   /* Index of the cell within the node */
163211 163806     RtreeCell *pCell             /* OUT: Write the cell contents here */
163212 163807   ){
163213 163808     u8 *pData;
163214 163809     RtreeCoord *pCoord;
163215         -  int ii;
       163810  +  int ii = 0;
163216 163811     pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
163217 163812     pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
163218 163813     pCoord = pCell->aCoord;
163219         -  for(ii=0; ii<pRtree->nDim*2; ii++){
163220         -    readCoord(&pData[ii*4], &pCoord[ii]);
163221         -  }
       163814  +  do{
       163815  +    readCoord(pData, &pCoord[ii]);
       163816  +    readCoord(pData+4, &pCoord[ii+1]);
       163817  +    pData += 8;
       163818  +    ii += 2;
       163819  +  }while( ii<pRtree->nDim2 );
163222 163820   }
163223 163821   
163224 163822   
163225 163823   /* Forward declaration for the function that does the work of
163226 163824   ** the virtual table module xCreate() and xConnect() methods.
163227 163825   */
163228 163826   static int rtreeInit(
................................................................................
163265 163863   /*
163266 163864   ** Decrement the r-tree reference count. When the reference count reaches
163267 163865   ** zero the structure is deleted.
163268 163866   */
163269 163867   static void rtreeRelease(Rtree *pRtree){
163270 163868     pRtree->nBusy--;
163271 163869     if( pRtree->nBusy==0 ){
163272         -    sqlite3_finalize(pRtree->pReadNode);
       163870  +    pRtree->inWrTrans = 0;
       163871  +    pRtree->nCursor = 0;
       163872  +    nodeBlobReset(pRtree);
163273 163873       sqlite3_finalize(pRtree->pWriteNode);
163274 163874       sqlite3_finalize(pRtree->pDeleteNode);
163275 163875       sqlite3_finalize(pRtree->pReadRowid);
163276 163876       sqlite3_finalize(pRtree->pWriteRowid);
163277 163877       sqlite3_finalize(pRtree->pDeleteRowid);
163278 163878       sqlite3_finalize(pRtree->pReadParent);
163279 163879       sqlite3_finalize(pRtree->pWriteParent);
................................................................................
163303 163903       pRtree->zDb, pRtree->zName, 
163304 163904       pRtree->zDb, pRtree->zName,
163305 163905       pRtree->zDb, pRtree->zName
163306 163906     );
163307 163907     if( !zCreate ){
163308 163908       rc = SQLITE_NOMEM;
163309 163909     }else{
       163910  +    nodeBlobReset(pRtree);
163310 163911       rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
163311 163912       sqlite3_free(zCreate);
163312 163913     }
163313 163914     if( rc==SQLITE_OK ){
163314 163915       rtreeRelease(pRtree);
163315 163916     }
163316 163917   
................................................................................
163318 163919   }
163319 163920   
163320 163921   /* 
163321 163922   ** Rtree virtual table module xOpen method.
163322 163923   */
163323 163924   static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
163324 163925     int rc = SQLITE_NOMEM;
       163926  +  Rtree *pRtree = (Rtree *)pVTab;
163325 163927     RtreeCursor *pCsr;
163326 163928   
163327 163929     pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
163328 163930     if( pCsr ){
163329 163931       memset(pCsr, 0, sizeof(RtreeCursor));
163330 163932       pCsr->base.pVtab = pVTab;
163331 163933       rc = SQLITE_OK;
       163934  +    pRtree->nCursor++;
163332 163935     }
163333 163936     *ppCursor = (sqlite3_vtab_cursor *)pCsr;
163334 163937   
163335 163938     return rc;
163336 163939   }
163337 163940   
163338 163941   
................................................................................
163357 163960   /* 
163358 163961   ** Rtree virtual table module xClose method.
163359 163962   */
163360 163963   static int rtreeClose(sqlite3_vtab_cursor *cur){
163361 163964     Rtree *pRtree = (Rtree *)(cur->pVtab);
163362 163965     int ii;
163363 163966     RtreeCursor *pCsr = (RtreeCursor *)cur;
       163967  +  assert( pRtree->nCursor>0 );
163364 163968     freeCursorConstraints(pCsr);
163365 163969     sqlite3_free(pCsr->aPoint);
163366 163970     for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
163367 163971     sqlite3_free(pCsr);
       163972  +  pRtree->nCursor--;
       163973  +  nodeBlobReset(pRtree);
163368 163974     return SQLITE_OK;
163369 163975   }
163370 163976   
163371 163977   /*
163372 163978   ** Rtree virtual table module xEof method.
163373 163979   **
163374 163980   ** Return non-zero if the cursor does not currently point to a valid 
................................................................................
163383 163989   ** Convert raw bits from the on-disk RTree record into a coordinate value.
163384 163990   ** The on-disk format is big-endian and needs to be converted for little-
163385 163991   ** endian platforms.  The on-disk record stores integer coordinates if
163386 163992   ** eInt is true and it stores 32-bit floating point records if eInt is
163387 163993   ** false.  a[] is the four bytes of the on-disk record to be decoded.
163388 163994   ** Store the results in "r".
163389 163995   **
163390         -** There are three versions of this macro, one each for little-endian and
163391         -** big-endian processors and a third generic implementation.  The endian-
163392         -** specific implementations are much faster and are preferred if the
163393         -** processor endianness is known at compile-time.  The SQLITE_BYTEORDER
163394         -** macro is part of sqliteInt.h and hence the endian-specific
163395         -** implementation will only be used if this module is compiled as part
163396         -** of the amalgamation.
       163996  +** There are five versions of this macro.  The last one is generic.  The
       163997  +** other four are various architectures-specific optimizations.
163397 163998   */
163398         -#if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234
       163999  +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
       164000  +#define RTREE_DECODE_COORD(eInt, a, r) {                        \
       164001  +    RtreeCoord c;    /* Coordinate decoded */                   \
       164002  +    c.u = _byteswap_ulong(*(u32*)a);                            \
       164003  +    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
       164004  +}
       164005  +#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       164006  +#define RTREE_DECODE_COORD(eInt, a, r) {                        \
       164007  +    RtreeCoord c;    /* Coordinate decoded */                   \
       164008  +    c.u = __builtin_bswap32(*(u32*)a);                          \
       164009  +    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
       164010  +}
       164011  +#elif SQLITE_BYTEORDER==1234
163399 164012   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
163400 164013       RtreeCoord c;    /* Coordinate decoded */                   \
163401 164014       memcpy(&c.u,a,4);                                           \
163402 164015       c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)|                   \
163403 164016             ((c.u&0xff)<<24)|((c.u&0xff00)<<8);                   \
163404 164017       r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163405 164018   }
163406         -#elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321
       164019  +#elif SQLITE_BYTEORDER==4321
163407 164020   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
163408 164021       RtreeCoord c;    /* Coordinate decoded */                   \
163409 164022       memcpy(&c.u,a,4);                                           \
163410 164023       r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163411 164024   }
163412 164025   #else
163413 164026   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
................................................................................
163426 164039     RtreeConstraint *pConstraint,  /* The constraint to test */
163427 164040     int eInt,                      /* True if RTree holding integer coordinates */
163428 164041     u8 *pCellData,                 /* Raw cell content */
163429 164042     RtreeSearchPoint *pSearch,     /* Container of this cell */
163430 164043     sqlite3_rtree_dbl *prScore,    /* OUT: score for the cell */
163431 164044     int *peWithin                  /* OUT: visibility of the cell */
163432 164045   ){
163433         -  int i;                                                /* Loop counter */
163434 164046     sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
163435 164047     int nCoord = pInfo->nCoord;                           /* No. of coordinates */
163436 164048     int rc;                                             /* Callback return code */
       164049  +  RtreeCoord c;                                       /* Translator union */
163437 164050     sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2];   /* Decoded coordinates */
163438 164051   
163439 164052     assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
163440 164053     assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
163441 164054   
163442 164055     if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
163443 164056       pInfo->iRowid = readInt64(pCellData);
163444 164057     }
163445 164058     pCellData += 8;
163446         -  for(i=0; i<nCoord; i++, pCellData += 4){
163447         -    RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]);
       164059  +#ifndef SQLITE_RTREE_INT_ONLY
       164060  +  if( eInt==0 ){
       164061  +    switch( nCoord ){
       164062  +      case 10:  readCoord(pCellData+36, &c); aCoord[9] = c.f;
       164063  +                readCoord(pCellData+32, &c); aCoord[8] = c.f;
       164064  +      case 8:   readCoord(pCellData+28, &c); aCoord[7] = c.f;
       164065  +                readCoord(pCellData+24, &c); aCoord[6] = c.f;
       164066  +      case 6:   readCoord(pCellData+20, &c); aCoord[5] = c.f;
       164067  +                readCoord(pCellData+16, &c); aCoord[4] = c.f;
       164068  +      case 4:   readCoord(pCellData+12, &c); aCoord[3] = c.f;
       164069  +                readCoord(pCellData+8,  &c); aCoord[2] = c.f;
       164070  +      default:  readCoord(pCellData+4,  &c); aCoord[1] = c.f;
       164071  +                readCoord(pCellData,    &c); aCoord[0] = c.f;
       164072  +    }
       164073  +  }else
       164074  +#endif
       164075  +  {
       164076  +    switch( nCoord ){
       164077  +      case 10:  readCoord(pCellData+36, &c); aCoord[9] = c.i;
       164078  +                readCoord(pCellData+32, &c); aCoord[8] = c.i;
       164079  +      case 8:   readCoord(pCellData+28, &c); aCoord[7] = c.i;
       164080  +                readCoord(pCellData+24, &c); aCoord[6] = c.i;
       164081  +      case 6:   readCoord(pCellData+20, &c); aCoord[5] = c.i;
       164082  +                readCoord(pCellData+16, &c); aCoord[4] = c.i;
       164083  +      case 4:   readCoord(pCellData+12, &c); aCoord[3] = c.i;
       164084  +                readCoord(pCellData+8,  &c); aCoord[2] = c.i;
       164085  +      default:  readCoord(pCellData+4,  &c); aCoord[1] = c.i;
       164086  +                readCoord(pCellData,    &c); aCoord[0] = c.i;
       164087  +    }
163448 164088     }
163449 164089     if( pConstraint->op==RTREE_MATCH ){
       164090  +    int eWithin = 0;
163450 164091       rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
163451         -                              nCoord, aCoord, &i);
163452         -    if( i==0 ) *peWithin = NOT_WITHIN;
       164092  +                              nCoord, aCoord, &eWithin);
       164093  +    if( eWithin==0 ) *peWithin = NOT_WITHIN;
163453 164094       *prScore = RTREE_ZERO;
163454 164095     }else{
163455 164096       pInfo->aCoord = aCoord;
163456 164097       pInfo->iLevel = pSearch->iLevel - 1;
163457 164098       pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
163458 164099       pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
163459 164100       rc = pConstraint->u.xQueryFunc(pInfo);
................................................................................
163481 164122     /* p->iCoord might point to either a lower or upper bound coordinate
163482 164123     ** in a coordinate pair.  But make pCellData point to the lower bound.
163483 164124     */
163484 164125     pCellData += 8 + 4*(p->iCoord&0xfe);
163485 164126   
163486 164127     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
163487 164128         || p->op==RTREE_GT || p->op==RTREE_EQ );
       164129  +  assert( ((((char*)pCellData) - (char*)0)&3)==0 );  /* 4-byte aligned */
163488 164130     switch( p->op ){
163489 164131       case RTREE_LE:
163490 164132       case RTREE_LT:
163491 164133       case RTREE_EQ:
163492 164134         RTREE_DECODE_COORD(eInt, pCellData, val);
163493 164135         /* val now holds the lower bound of the coordinate pair */
163494 164136         if( p->u.rValue>=val ) return;
................................................................................
163521 164163     int *peWithin              /* Adjust downward, as appropriate */
163522 164164   ){
163523 164165     RtreeDValue xN;      /* Coordinate value converted to a double */
163524 164166   
163525 164167     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
163526 164168         || p->op==RTREE_GT || p->op==RTREE_EQ );
163527 164169     pCellData += 8 + p->iCoord*4;
       164170  +  assert( ((((char*)pCellData) - (char*)0)&3)==0 );  /* 4-byte aligned */
163528 164171     RTREE_DECODE_COORD(eInt, pCellData, xN);
163529 164172     switch( p->op ){
163530 164173       case RTREE_LE: if( xN <= p->u.rValue ) return;  break;
163531 164174       case RTREE_LT: if( xN <  p->u.rValue ) return;  break;
163532 164175       case RTREE_GE: if( xN >= p->u.rValue ) return;  break;
163533 164176       case RTREE_GT: if( xN >  p->u.rValue ) return;  break;
163534 164177       default:       if( xN == p->u.rValue ) return;  break;
................................................................................
163589 164232     if( pA->rScore>pB->rScore ) return +1;
163590 164233     if( pA->iLevel<pB->iLevel ) return -1;
163591 164234     if( pA->iLevel>pB->iLevel ) return +1;
163592 164235     return 0;
163593 164236   }
163594 164237   
163595 164238   /*
163596         -** Interchange to search points in a cursor.
       164239  +** Interchange two search points in a cursor.
163597 164240   */
163598 164241   static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
163599 164242     RtreeSearchPoint t = p->aPoint[i];
163600 164243     assert( i<j );
163601 164244     p->aPoint[i] = p->aPoint[j];
163602 164245     p->aPoint[j] = t;
163603 164246     i++; j++;
................................................................................
163837 164480         if( p->iCell>=nCell ){
163838 164481           RTREE_QUEUE_TRACE(pCur, "POP-S:");
163839 164482           rtreeSearchPointPop(pCur);
163840 164483         }
163841 164484         if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
163842 164485         p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
163843 164486         if( p==0 ) return SQLITE_NOMEM;
163844         -      p->eWithin = eWithin;
       164487  +      p->eWithin = (u8)eWithin;
163845 164488         p->id = x.id;
163846 164489         p->iCell = x.iCell;
163847 164490         RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
163848 164491         break;
163849 164492       }
163850 164493       if( p->iCell>=nCell ){
163851 164494         RTREE_QUEUE_TRACE(pCur, "POP-Se:");
................................................................................
163896 164539     RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
163897 164540   
163898 164541     if( rc ) return rc;
163899 164542     if( p==0 ) return SQLITE_OK;
163900 164543     if( i==0 ){
163901 164544       sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
163902 164545     }else{
163903         -    if( rc ) return rc;
163904 164546       nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
163905 164547   #ifndef SQLITE_RTREE_INT_ONLY
163906 164548       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
163907 164549         sqlite3_result_double(ctx, c.f);
163908 164550       }else
163909 164551   #endif
163910 164552       {
................................................................................
164025 164667       if( rc==SQLITE_OK && pLeaf!=0 ){
164026 164668         p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
164027 164669         assert( p!=0 );  /* Always returns pCsr->sPoint */
164028 164670         pCsr->aNode[0] = pLeaf;
164029 164671         p->id = iNode;
164030 164672         p->eWithin = PARTLY_WITHIN;
164031 164673         rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
164032         -      p->iCell = iCell;
       164674  +      p->iCell = (u8)iCell;
164033 164675         RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
164034 164676       }else{
164035 164677         pCsr->atEOF = 1;
164036 164678       }
164037 164679     }else{
164038 164680       /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
164039 164681       ** with the configured constraints. 
................................................................................
164058 164700               ** can be cast into an RtreeMatchArg object. One created using
164059 164701               ** an sqlite3_rtree_geometry_callback() SQL user function.
164060 164702               */
164061 164703               rc = deserializeGeometry(argv[ii], p);
164062 164704               if( rc!=SQLITE_OK ){
164063 164705                 break;
164064 164706               }
164065         -            p->pInfo->nCoord = pRtree->nDim*2;
       164707  +            p->pInfo->nCoord = pRtree->nDim2;
164066 164708               p->pInfo->anQueue = pCsr->anQueue;
164067 164709               p->pInfo->mxLevel = pRtree->iDepth + 1;
164068 164710             }else{
164069 164711   #ifdef SQLITE_RTREE_INT_ONLY
164070 164712               p->u.rValue = sqlite3_value_int64(argv[ii]);
164071 164713   #else
164072 164714               p->u.rValue = sqlite3_value_double(argv[ii]);
................................................................................
164073 164715   #endif
164074 164716             }
164075 164717           }
164076 164718         }
164077 164719       }
164078 164720       if( rc==SQLITE_OK ){
164079 164721         RtreeSearchPoint *pNew;
164080         -      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1);
       164722  +      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
164081 164723         if( pNew==0 ) return SQLITE_NOMEM;
164082 164724         pNew->id = 1;
164083 164725         pNew->iCell = 0;
164084 164726         pNew->eWithin = PARTLY_WITHIN;
164085 164727         assert( pCsr->bPoint==1 );
164086 164728         pCsr->aNode[0] = pRoot;
164087 164729         pRoot = 0;
................................................................................
164091 164733     }
164092 164734   
164093 164735     nodeRelease(pRtree, pRoot);
164094 164736     rtreeRelease(pRtree);
164095 164737     return rc;
164096 164738   }
164097 164739   
164098         -/*
164099         -** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
164100         -** extension is currently being used by a version of SQLite too old to
164101         -** support estimatedRows. In that case this function is a no-op.
164102         -*/
164103         -static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
164104         -#if SQLITE_VERSION_NUMBER>=3008002
164105         -  if( sqlite3_libversion_number()>=3008002 ){
164106         -    pIdxInfo->estimatedRows = nRow;
164107         -  }
164108         -#endif
164109         -}
164110         -
164111 164740   /*
164112 164741   ** Rtree virtual table module xBestIndex method. There are three
164113 164742   ** table scan strategies to choose from (in order from most to 
164114 164743   ** least desirable):
164115 164744   **
164116 164745   **   idxNum     idxStr        Strategy
164117 164746   **   ------------------------------------------------
................................................................................
164183 164812         /* This strategy involves a two rowid lookups on an B-Tree structures
164184 164813         ** and then a linear search of an R-Tree node. This should be 
164185 164814         ** considered almost as quick as a direct rowid lookup (for which 
164186 164815         ** sqlite uses an internal cost of 0.0). It is expected to return
164187 164816         ** a single row.
164188 164817         */ 
164189 164818         pIdxInfo->estimatedCost = 30.0;
164190         -      setEstimatedRows(pIdxInfo, 1);
       164819  +      pIdxInfo->estimatedRows = 1;
164191 164820         return SQLITE_OK;
164192 164821       }
164193 164822   
164194 164823       if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
164195 164824         u8 op;
164196 164825         switch( p->op ){
164197 164826           case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
................................................................................
164201 164830           case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
164202 164831           default:
164203 164832             assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
164204 164833             op = RTREE_MATCH; 
164205 164834             break;
164206 164835         }
164207 164836         zIdxStr[iIdx++] = op;
164208         -      zIdxStr[iIdx++] = p->iColumn - 1 + '0';
       164837  +      zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
164209 164838         pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
164210 164839         pIdxInfo->aConstraintUsage[ii].omit = 1;
164211 164840       }
164212 164841     }
164213 164842   
164214 164843     pIdxInfo->idxNum = 2;
164215 164844     pIdxInfo->needToFreeIdxStr = 1;
164216 164845     if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
164217 164846       return SQLITE_NOMEM;
164218 164847     }
164219 164848   
164220 164849     nRow = pRtree->nRowEst >> (iIdx/2);
164221 164850     pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
164222         -  setEstimatedRows(pIdxInfo, nRow);
       164851  +  pIdxInfo->estimatedRows = nRow;
164223 164852   
164224 164853     return rc;
164225 164854   }
164226 164855   
164227 164856   /*
164228 164857   ** Return the N-dimensional volumn of the cell stored in *p.
164229 164858   */
164230 164859   static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
164231 164860     RtreeDValue area = (RtreeDValue)1;
164232         -  int ii;
164233         -  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164234         -    area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
       164861  +  assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
       164862  +#ifndef SQLITE_RTREE_INT_ONLY
       164863  +  if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
       164864  +    switch( pRtree->nDim ){
       164865  +      case 5:  area  = p->aCoord[9].f - p->aCoord[8].f;
       164866  +      case 4:  area *= p->aCoord[7].f - p->aCoord[6].f;
       164867  +      case 3:  area *= p->aCoord[5].f - p->aCoord[4].f;
       164868  +      case 2:  area *= p->aCoord[3].f - p->aCoord[2].f;
       164869  +      default: area *= p->aCoord[1].f - p->aCoord[0].f;
       164870  +    }
       164871  +  }else
       164872  +#endif
       164873  +  {
       164874  +    switch( pRtree->nDim ){
       164875  +      case 5:  area  = p->aCoord[9].i - p->aCoord[8].i;
       164876  +      case 4:  area *= p->aCoord[7].i - p->aCoord[6].i;
       164877  +      case 3:  area *= p->aCoord[5].i - p->aCoord[4].i;
       164878  +      case 2:  area *= p->aCoord[3].i - p->aCoord[2].i;
       164879  +      default: area *= p->aCoord[1].i - p->aCoord[0].i;
       164880  +    }
164235 164881     }
164236 164882     return area;
164237 164883   }
164238 164884   
164239 164885   /*
164240 164886   ** Return the margin length of cell p. The margin length is the sum
164241 164887   ** of the objects size in each dimension.
164242 164888   */
164243 164889   static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
164244         -  RtreeDValue margin = (RtreeDValue)0;
164245         -  int ii;
164246         -  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       164890  +  RtreeDValue margin = 0;
       164891  +  int ii = pRtree->nDim2 - 2;
       164892  +  do{
164247 164893       margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
164248         -  }
       164894  +    ii -= 2;
       164895  +  }while( ii>=0 );
164249 164896     return margin;
164250 164897   }
164251 164898   
164252 164899   /*
164253 164900   ** Store the union of cells p1 and p2 in p1.
164254 164901   */
164255 164902   static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164256         -  int ii;
       164903  +  int ii = 0;
164257 164904     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164258         -    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       164905  +    do{
164259 164906         p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
164260 164907         p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
164261         -    }
       164908  +      ii += 2;
       164909  +    }while( ii<pRtree->nDim2 );
164262 164910     }else{
164263         -    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       164911  +    do{
164264 164912         p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
164265 164913         p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
164266         -    }
       164914  +      ii += 2;
       164915  +    }while( ii<pRtree->nDim2 );
164267 164916     }
164268 164917   }
164269 164918   
164270 164919   /*
164271 164920   ** Return true if the area covered by p2 is a subset of the area covered
164272 164921   ** by p1. False otherwise.
164273 164922   */
164274 164923   static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164275 164924     int ii;
164276 164925     int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
164277         -  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       164926  +  for(ii=0; ii<pRtree->nDim2; ii+=2){
164278 164927       RtreeCoord *a1 = &p1->aCoord[ii];
164279 164928       RtreeCoord *a2 = &p2->aCoord[ii];
164280 164929       if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) 
164281 164930        || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i)) 
164282 164931       ){
164283 164932         return 0;
164284 164933       }
................................................................................
164305 164954     int nCell
164306 164955   ){
164307 164956     int ii;
164308 164957     RtreeDValue overlap = RTREE_ZERO;
164309 164958     for(ii=0; ii<nCell; ii++){
164310 164959       int jj;
164311 164960       RtreeDValue o = (RtreeDValue)1;
164312         -    for(jj=0; jj<(pRtree->nDim*2); jj+=2){
       164961  +    for(jj=0; jj<pRtree->nDim2; jj+=2){
164313 164962         RtreeDValue x1, x2;
164314 164963         x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
164315 164964         x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
164316 164965         if( x2<x1 ){
164317 164966           o = (RtreeDValue)0;
164318 164967           break;
164319 164968         }else{
................................................................................
165361 166010       **
165362 166011       ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
165363 166012       ** with "column" that are interpreted as table constraints.
165364 166013       ** Example:  CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
165365 166014       ** This problem was discovered after years of use, so we silently ignore
165366 166015       ** these kinds of misdeclared tables to avoid breaking any legacy.
165367 166016       */
165368         -    assert( nData<=(pRtree->nDim*2 + 3) );
       166017  +    assert( nData<=(pRtree->nDim2 + 3) );
165369 166018   
165370 166019   #ifndef SQLITE_RTREE_INT_ONLY
165371 166020       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
165372 166021         for(ii=0; ii<nData-4; ii+=2){
165373 166022           cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
165374 166023           cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
165375 166024           if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
................................................................................
165450 166099       }
165451 166100     }
165452 166101   
165453 166102   constraint:
165454 166103     rtreeRelease(pRtree);
165455 166104     return rc;
165456 166105   }
       166106  +
       166107  +/*
       166108  +** Called when a transaction starts.
       166109  +*/
       166110  +static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
       166111  +  Rtree *pRtree = (Rtree *)pVtab;
       166112  +  assert( pRtree->inWrTrans==0 );
       166113  +  pRtree->inWrTrans++;
       166114  +  return SQLITE_OK;
       166115  +}
       166116  +
       166117  +/*
       166118  +** Called when a transaction completes (either by COMMIT or ROLLBACK).
       166119  +** The sqlite3_blob object should be released at this point.
       166120  +*/
       166121  +static int rtreeEndTransaction(sqlite3_vtab *pVtab){
       166122  +  Rtree *pRtree = (Rtree *)pVtab;
       166123  +  pRtree->inWrTrans = 0;
       166124  +  nodeBlobReset(pRtree);
       166125  +  return SQLITE_OK;
       166126  +}
165457 166127   
165458 166128   /*
165459 166129   ** The xRename method for rtree module virtual tables.
165460 166130   */
165461 166131   static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
165462 166132     Rtree *pRtree = (Rtree *)pVtab;
165463 166133     int rc = SQLITE_NOMEM;
................................................................................
165471 166141     );
165472 166142     if( zSql ){
165473 166143       rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
165474 166144       sqlite3_free(zSql);
165475 166145     }
165476 166146     return rc;
165477 166147   }
       166148  +
165478 166149   
165479 166150   /*
165480 166151   ** This function populates the pRtree->nRowEst variable with an estimate
165481 166152   ** of the number of rows in the virtual table. If possible, this is based
165482 166153   ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
165483 166154   */
165484 166155   static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
................................................................................
165531 166202     rtreeClose,                 /* xClose - close a cursor */
165532 166203     rtreeFilter,                /* xFilter - configure scan constraints */
165533 166204     rtreeNext,                  /* xNext - advance a cursor */
165534 166205     rtreeEof,                   /* xEof */
165535 166206     rtreeColumn,                /* xColumn - read data */
165536 166207     rtreeRowid,                 /* xRowid - read data */
165537 166208     rtreeUpdate,                /* xUpdate - write data */
165538         -  0,                          /* xBegin - begin transaction */
165539         -  0,                          /* xSync - sync transaction */
165540         -  0,                          /* xCommit - commit transaction */
165541         -  0,                          /* xRollback - rollback transaction */
       166209  +  rtreeBeginTransaction,      /* xBegin - begin transaction */
       166210  +  rtreeEndTransaction,        /* xSync - sync transaction */
       166211  +  rtreeEndTransaction,        /* xCommit - commit transaction */
       166212  +  rtreeEndTransaction,        /* xRollback - rollback transaction */
165542 166213     0,                          /* xFindFunction - function overloading */
165543 166214     rtreeRename,                /* xRename - rename the table */
165544 166215     0,                          /* xSavepoint */
165545 166216     0,                          /* xRelease */
165546         -  0                           /* xRollbackTo */
       166217  +  0,                          /* xRollbackTo */
165547 166218   };
165548 166219   
165549 166220   static int rtreeSqlInit(
165550 166221     Rtree *pRtree, 
165551 166222     sqlite3 *db, 
165552 166223     const char *zDb, 
165553 166224     const char *zPrefix, 
165554 166225     int isCreate
165555 166226   ){
165556 166227     int rc = SQLITE_OK;
165557 166228   
165558         -  #define N_STATEMENT 9
       166229  +  #define N_STATEMENT 8
165559 166230     static const char *azSql[N_STATEMENT] = {
165560         -    /* Read and write the xxx_node table */
165561         -    "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
       166231  +    /* Write the xxx_node table */
165562 166232       "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
165563 166233       "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
165564 166234   
165565 166235       /* Read and write the xxx_rowid table */
165566 166236       "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
165567 166237       "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
165568 166238       "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
................................................................................
165592 166262       rc = sqlite3_exec(db, zCreate, 0, 0, 0);
165593 166263       sqlite3_free(zCreate);
165594 166264       if( rc!=SQLITE_OK ){
165595 166265         return rc;
165596 166266       }
165597 166267     }
165598 166268   
165599         -  appStmt[0] = &pRtree->pReadNode;
165600         -  appStmt[1] = &pRtree->pWriteNode;
165601         -  appStmt[2] = &pRtree->pDeleteNode;
165602         -  appStmt[3] = &pRtree->pReadRowid;
165603         -  appStmt[4] = &pRtree->pWriteRowid;
165604         -  appStmt[5] = &pRtree->pDeleteRowid;
165605         -  appStmt[6] = &pRtree->pReadParent;
165606         -  appStmt[7] = &pRtree->pWriteParent;
165607         -  appStmt[8] = &pRtree->pDeleteParent;
       166269  +  appStmt[0] = &pRtree->pWriteNode;
       166270  +  appStmt[1] = &pRtree->pDeleteNode;
       166271  +  appStmt[2] = &pRtree->pReadRowid;
       166272  +  appStmt[3] = &pRtree->pWriteRowid;
       166273  +  appStmt[4] = &pRtree->pDeleteRowid;
       166274  +  appStmt[5] = &pRtree->pReadParent;
       166275  +  appStmt[6] = &pRtree->pWriteParent;
       166276  +  appStmt[7] = &pRtree->pDeleteParent;
165608 166277   
165609 166278     rc = rtreeQueryStat1(db, pRtree);
165610 166279     for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
165611 166280       char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
165612 166281       if( zSql ){
165613 166282         rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0); 
165614 166283       }else{
................................................................................
165738 166407       return SQLITE_NOMEM;
165739 166408     }
165740 166409     memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
165741 166410     pRtree->nBusy = 1;
165742 166411     pRtree->base.pModule = &rtreeModule;
165743 166412     pRtree->zDb = (char *)&pRtree[1];
165744 166413     pRtree->zName = &pRtree->zDb[nDb+1];
165745         -  pRtree->nDim = (argc-4)/2;
165746         -  pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
165747         -  pRtree->eCoordType = eCoordType;
       166414  +  pRtree->nDim = (u8)((argc-4)/2);
       166415  +  pRtree->nDim2 = pRtree->nDim*2;
       166416  +  pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
       166417  +  pRtree->eCoordType = (u8)eCoordType;
165748 166418     memcpy(pRtree->zDb, argv[1], nDb);
165749 166419     memcpy(pRtree->zName, argv[2], nName);
165750 166420   
165751 166421     /* Figure out the node size to use. */
165752 166422     rc = getNodeSize(db, pRtree, isCreate, pzErr);
165753 166423   
165754 166424     /* Create/Connect to the underlying relational database schema. If
................................................................................
165813 166483     RtreeNode node;
165814 166484     Rtree tree;
165815 166485     int ii;
165816 166486   
165817 166487     UNUSED_PARAMETER(nArg);
165818 166488     memset(&node, 0, sizeof(RtreeNode));
165819 166489     memset(&tree, 0, sizeof(Rtree));
165820         -  tree.nDim = sqlite3_value_int(apArg[0]);
       166490  +  tree.nDim = (u8)sqlite3_value_int(apArg[0]);
       166491  +  tree.nDim2 = tree.nDim*2;
165821 166492     tree.nBytesPerCell = 8 + 8 * tree.nDim;
165822 166493     node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
165823 166494   
165824 166495     for(ii=0; ii<NCELL(&node); ii++){
165825 166496       char zCell[512];
165826 166497       int nCell = 0;
165827 166498       RtreeCell cell;
165828 166499       int jj;
165829 166500   
165830 166501       nodeGetCell(&tree, &node, ii, &cell);
165831 166502       sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
165832 166503       nCell = (int)strlen(zCell);
165833         -    for(jj=0; jj<tree.nDim*2; jj++){
       166504  +    for(jj=0; jj<tree.nDim2; jj++){
165834 166505   #ifndef SQLITE_RTREE_INT_ONLY
165835 166506         sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
165836 166507                          (double)cell.aCoord[jj].f);
165837 166508   #else
165838 166509         sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
165839 166510                          cell.aCoord[jj].i);
165840 166511   #endif
................................................................................
166534 167205     }
166535 167206   }
166536 167207   
166537 167208   /*
166538 167209   ** Register the ICU extension functions with database db.
166539 167210   */
166540 167211   SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
166541         -  struct IcuScalar {
       167212  +  static const struct IcuScalar {
166542 167213       const char *zName;                        /* Function name */
166543         -    int nArg;                                 /* Number of arguments */
166544         -    int enc;                                  /* Optimal text encoding */
166545         -    void *pContext;                           /* sqlite3_user_data() context */
       167214  +    unsigned char nArg;                       /* Number of arguments */
       167215  +    unsigned short enc;                       /* Optimal text encoding */
       167216  +    unsigned char iContext;                   /* sqlite3_user_data() context */
166546 167217       void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
166547 167218     } scalars[] = {
166548         -    {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC,          0, icuRegexpFunc},
166549         -
166550         -    {"lower",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
166551         -    {"lower",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
166552         -    {"upper",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166553         -    {"upper",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166554         -
166555         -    {"lower",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuCaseFunc16},
166556         -    {"lower",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuCaseFunc16},
166557         -    {"upper",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,  (void*)1, icuCaseFunc16},
166558         -    {"upper",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,  (void*)1, icuCaseFunc16},
166559         -
166560         -    {"like",   2, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuLikeFunc},
166561         -    {"like",   3, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuLikeFunc},
166562         -
166563         -    {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
       167219  +    {"icu_load_collation",  2, SQLITE_UTF8,                1, icuLoadCollation},
       167220  +    {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC,         0, icuRegexpFunc},
       167221  +    {"lower",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC,       0, icuCaseFunc16},
       167222  +    {"lower",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC,       0, icuCaseFunc16},
       167223  +    {"upper",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC,       1, icuCaseFunc16},
       167224  +    {"upper",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC,       1, icuCaseFunc16},
       167225  +    {"lower",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
       167226  +    {"lower",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
       167227  +    {"upper",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,        1, icuCaseFunc16},
       167228  +    {"upper",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        1, icuCaseFunc16},
       167229  +    {"like",   2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuLikeFunc},
       167230  +    {"like",   3, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuLikeFunc},
166564 167231     };
166565         -
166566 167232     int rc = SQLITE_OK;
166567 167233     int i;
166568 167234   
       167235  +  
166569 167236     for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
166570         -    struct IcuScalar *p = &scalars[i];
       167237  +    const struct IcuScalar *p = &scalars[i];
166571 167238       rc = sqlite3_create_function(
166572         -        db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
       167239  +        db, p->zName, p->nArg, p->enc, 
       167240  +        p->iContext ? (void*)db : (void*)0,
       167241  +        p->xFunc, 0, 0
166573 167242       );
166574 167243     }
166575 167244   
166576 167245     return rc;
166577 167246   }
166578 167247   
166579 167248   #if !SQLITE_CORE
................................................................................
169773 170442   }
169774 170443   
169775 170444   
169776 170445   /*
169777 170446   ** Open the database handle and attach the RBU database as "rbu". If an
169778 170447   ** error occurs, leave an error code and message in the RBU handle.
169779 170448   */
169780         -static void rbuOpenDatabase(sqlite3rbu *p){
       170449  +static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
169781 170450     assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
169782 170451     assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
169783 170452   
169784 170453     /* Open the RBU database */
169785 170454     p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
169786 170455   
169787 170456     if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
................................................................................
169848 170517       rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
169849 170518       if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
169850 170519       if( p->eStage>=RBU_STAGE_MOVE ){
169851 170520         bOpen = 1;
169852 170521       }else{
169853 170522         RbuState *pState = rbuLoadState(p);
169854 170523         if( pState ){
169855         -        bOpen = (pState->eStage>RBU_STAGE_MOVE);
       170524  +        bOpen = (pState->eStage>=RBU_STAGE_MOVE);
169856 170525           rbuFreeState(pState);
169857 170526         }
169858 170527       }
169859 170528       if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
169860 170529     }
169861 170530   
169862 170531     p->eStage = 0;
169863 170532     if( p->rc==SQLITE_OK && p->dbMain==0 ){
169864 170533       if( !rbuIsVacuum(p) ){
169865 170534         p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
169866 170535       }else if( p->pRbuFd->pWalFd ){
       170536  +      if( pbRetry ){
       170537  +        p->pRbuFd->bNolock = 0;
       170538  +        sqlite3_close(p->dbRbu);
       170539  +        sqlite3_close(p->dbMain);
       170540  +        p->dbMain = 0;
       170541  +        p->dbRbu = 0;
       170542  +        *pbRetry = 1;
       170543  +        return;
       170544  +      }
169867 170545         p->rc = SQLITE_ERROR;
169868 170546         p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
169869 170547       }else{
169870 170548         char *zTarget;
169871 170549         char *zExtra = 0;
169872 170550         if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
169873 170551           zExtra = &p->zRbu[5];
................................................................................
170040 170718     if( p->rc==SQLITE_OK ){
170041 170719       int rc2;
170042 170720       p->eStage = RBU_STAGE_CAPTURE;
170043 170721       rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
170044 170722       if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
170045 170723     }
170046 170724   
170047         -  if( p->rc==SQLITE_OK ){
       170725  +  if( p->rc==SQLITE_OK && p->nFrame>0 ){
170048 170726       p->eStage = RBU_STAGE_CKPT;
170049 170727       p->nStep = (pState ? pState->nRow : 0);
170050 170728       p->aBuf = rbuMalloc(p, p->pgsz);
170051 170729       p->iWalCksum = rbuShmChecksum(p);
170052 170730     }
170053 170731   
170054         -  if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
170055         -    p->rc = SQLITE_DONE;
170056         -    p->eStage = RBU_STAGE_DONE;
       170732  +  if( p->rc==SQLITE_OK ){
       170733  +    if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
       170734  +      p->rc = SQLITE_DONE;
       170735  +      p->eStage = RBU_STAGE_DONE;
       170736  +    }
170057 170737     }
170058 170738   }
170059 170739   
170060 170740   /*
170061 170741   ** Called when iAmt bytes are read from offset iOff of the wal file while
170062 170742   ** the rbu object is in capture mode. Record the frame number of the frame
170063 170743   ** being read in the aFrame[] array.
................................................................................
170222 170902           }
170223 170903         }
170224 170904   #else
170225 170905         p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
170226 170906   #endif
170227 170907   
170228 170908         if( p->rc==SQLITE_OK ){
170229         -        rbuOpenDatabase(p);
       170909  +        rbuOpenDatabase(p, 0);
170230 170910           rbuSetupCheckpoint(p, 0);
170231 170911         }
170232 170912       }
170233 170913     }
170234 170914   
170235 170915     sqlite3_free(zWal);
170236 170916     sqlite3_free(zOal);
................................................................................
170933 171613       /* Create the custom VFS. */
170934 171614       memset(p, 0, sizeof(sqlite3rbu));
170935 171615       rbuCreateVfs(p);
170936 171616   
170937 171617       /* Open the target, RBU and state databases */
170938 171618       if( p->rc==SQLITE_OK ){
170939 171619         char *pCsr = (char*)&p[1];
       171620  +      int bRetry = 0;
170940 171621         if( zTarget ){
170941 171622           p->zTarget = pCsr;
170942 171623           memcpy(p->zTarget, zTarget, nTarget+1);
170943 171624           pCsr += nTarget+1;
170944 171625         }
170945 171626         p->zRbu = pCsr;
170946 171627         memcpy(p->zRbu, zRbu, nRbu+1);
170947 171628         pCsr += nRbu+1;
170948 171629         if( zState ){
170949 171630           p->zState = rbuMPrintf(p, "%s", zState);
170950 171631         }
170951         -      rbuOpenDatabase(p);
       171632  +
       171633  +      /* If the first attempt to open the database file fails and the bRetry
       171634  +      ** flag it set, this means that the db was not opened because it seemed
       171635  +      ** to be a wal-mode db. But, this may have happened due to an earlier
       171636  +      ** RBU vacuum operation leaving an old wal file in the directory.
       171637  +      ** If this is the case, it will have been checkpointed and deleted
       171638  +      ** when the handle was closed and a second attempt to open the 
       171639  +      ** database may succeed.  */
       171640  +      rbuOpenDatabase(p, &bRetry);
       171641  +      if( bRetry ){
       171642  +        rbuOpenDatabase(p, 0);
       171643  +      }
170952 171644       }
170953 171645   
170954 171646       if( p->rc==SQLITE_OK ){
170955 171647         pState = rbuLoadState(p);
170956 171648         assert( pState || p->rc!=SQLITE_OK );
170957 171649         if( p->rc==SQLITE_OK ){
170958 171650   
................................................................................
175907 176599     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
175908 176600     int iVal,                       /* Index of conflict record value to fetch */
175909 176601     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
175910 176602   ){
175911 176603     if( !pIter->pConflict ){
175912 176604       return SQLITE_MISUSE;
175913 176605     }
175914         -  if( iVal<0 || iVal>=sqlite3_column_count(pIter->pConflict) ){
       176606  +  if( iVal<0 || iVal>=pIter->nCol ){
175915 176607       return SQLITE_RANGE;
175916 176608     }
175917 176609     *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
175918 176610     return SQLITE_OK;
175919 176611   }
175920 176612   
175921 176613   /*
................................................................................
176374 177066   ){
176375 177067     int rc = SQLITE_OK;
176376 177068     int i;
176377 177069     SessionBuffer buf = {0, 0, 0};
176378 177070   
176379 177071     sessionAppendStr(&buf, "INSERT INTO main.", &rc);
176380 177072     sessionAppendIdent(&buf, zTab, &rc);
176381         -  sessionAppendStr(&buf, " VALUES(?", &rc);
       177073  +  sessionAppendStr(&buf, "(", &rc);
       177074  +  for(i=0; i<p->nCol; i++){
       177075  +    if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
       177076  +    sessionAppendIdent(&buf, p->azCol[i], &rc);
       177077  +  }
       177078  +
       177079  +  sessionAppendStr(&buf, ") VALUES(?", &rc);
176382 177080     for(i=1; i<p->nCol; i++){
176383 177081       sessionAppendStr(&buf, ", ?", &rc);
176384 177082     }
176385 177083     sessionAppendStr(&buf, ")", &rc);
176386 177084   
176387 177085     if( rc==SQLITE_OK ){
176388 177086       rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
................................................................................
176920 177618           if( zTab==0 ){
176921 177619             rc = SQLITE_NOMEM;
176922 177620             break;
176923 177621           }
176924 177622           nTab = (int)strlen(zTab);
176925 177623           sApply.azCol = (const char **)zTab;
176926 177624         }else{
       177625  +        int nMinCol = 0;
       177626  +        int i;
       177627  +
176927 177628           sqlite3changeset_pk(pIter, &abPK, 0);
176928 177629           rc = sessionTableInfo(
176929 177630               db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
176930 177631           );
176931 177632           if( rc!=SQLITE_OK ) break;
       177633  +        for(i=0; i<sApply.nCol; i++){
       177634  +          if( sApply.abPK[i] ) nMinCol = i+1;
       177635  +        }
176932 177636     
176933 177637           if( sApply.nCol==0 ){
176934 177638             schemaMismatch = 1;
176935 177639             sqlite3_log(SQLITE_SCHEMA, 
176936 177640                 "sqlite3changeset_apply(): no such table: %s", zTab
176937 177641             );
176938 177642           }
176939         -        else if( sApply.nCol!=nCol ){
       177643  +        else if( sApply.nCol<nCol ){
176940 177644             schemaMismatch = 1;
176941 177645             sqlite3_log(SQLITE_SCHEMA, 
176942         -              "sqlite3changeset_apply(): table %s has %d columns, expected %d", 
       177646  +              "sqlite3changeset_apply(): table %s has %d columns, "
       177647  +              "expected %d or more", 
176943 177648                 zTab, sApply.nCol, nCol
176944 177649             );
176945 177650           }
176946         -        else if( memcmp(sApply.abPK, abPK, nCol)!=0 ){
       177651  +        else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
176947 177652             schemaMismatch = 1;
176948 177653             sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
176949 177654                 "primary key mismatch for table %s", zTab
176950 177655             );
176951 177656           }
176952         -        else if( 
176953         -            (rc = sessionSelectRow(db, zTab, &sApply))
176954         -         || (rc = sessionUpdateRow(db, zTab, &sApply))
176955         -         || (rc = sessionDeleteRow(db, zTab, &sApply))
176956         -         || (rc = sessionInsertRow(db, zTab, &sApply))
176957         -        ){
176958         -          break;
       177657  +        else{
       177658  +          sApply.nCol = nCol;
       177659  +          if((rc = sessionSelectRow(db, zTab, &sApply))
       177660  +          || (rc = sessionUpdateRow(db, zTab, &sApply))
       177661  +          || (rc = sessionDeleteRow(db, zTab, &sApply))
       177662  +          || (rc = sessionInsertRow(db, zTab, &sApply))
       177663  +          ){
       177664  +            break;
       177665  +          }
176959 177666           }
176960 177667           nTab = sqlite3Strlen30(zTab);
176961 177668         }
176962 177669       }
176963 177670   
176964 177671       /* If there is a schema mismatch on the current table, proceed to the
176965 177672       ** next change. A log message has already been issued. */
................................................................................
177543 178250   ** For the time being, all JSON is stored as pure text.  (We might add
177544 178251   ** a JSONB type in the future which stores a binary encoding of JSON in
177545 178252   ** a BLOB, but there is no support for JSONB in the current implementation.
177546 178253   ** This implementation parses JSON text at 250 MB/s, so it is hard to see
177547 178254   ** how JSONB might improve on that.)
177548 178255   */
177549 178256   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
177550         -#if !defined(_SQLITEINT_H_)
       178257  +#if !defined(SQLITEINT_H)
177551 178258   /* #include "sqlite3ext.h" */
177552 178259   #endif
177553 178260   SQLITE_EXTENSION_INIT1
177554 178261   /* #include <assert.h> */
177555 178262   /* #include <string.h> */
177556 178263   /* #include <stdlib.h> */
177557 178264   /* #include <stdarg.h> */
................................................................................
181594 182301   ** putting an appropriate #define in the %include section of the input
181595 182302   ** grammar.
181596 182303   */
181597 182304   #ifndef fts5YYMALLOCARGTYPE
181598 182305   # define fts5YYMALLOCARGTYPE size_t
181599 182306   #endif
181600 182307   
       182308  +/* Initialize a new parser that has already been allocated.
       182309  +*/
       182310  +static void sqlite3Fts5ParserInit(void *fts5yypParser){
       182311  +  fts5yyParser *pParser = (fts5yyParser*)fts5yypParser;
       182312  +#ifdef fts5YYTRACKMAXSTACKDEPTH
       182313  +  pParser->fts5yyhwm = 0;
       182314  +#endif
       182315  +#if fts5YYSTACKDEPTH<=0
       182316  +  pParser->fts5yytos = NULL;
       182317  +  pParser->fts5yystack = NULL;
       182318  +  pParser->fts5yystksz = 0;
       182319  +  if( fts5yyGrowStack(pParser) ){
       182320  +    pParser->fts5yystack = &pParser->fts5yystk0;
       182321  +    pParser->fts5yystksz = 1;
       182322  +  }
       182323  +#endif
       182324  +#ifndef fts5YYNOERRORRECOVERY
       182325  +  pParser->fts5yyerrcnt = -1;
       182326  +#endif
       182327  +  pParser->fts5yytos = pParser->fts5yystack;
       182328  +  pParser->fts5yystack[0].stateno = 0;
       182329  +  pParser->fts5yystack[0].major = 0;
       182330  +}
       182331  +
       182332  +#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
181601 182333   /* 
181602 182334   ** This function allocates a new parser.
181603 182335   ** The only argument is a pointer to a function which works like
181604 182336   ** malloc.
181605 182337   **
181606 182338   ** Inputs:
181607 182339   ** A pointer to the function used to allocate memory.
................................................................................
181609 182341   ** Outputs:
181610 182342   ** A pointer to a parser.  This pointer is used in subsequent calls
181611 182343   ** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
181612 182344   */
181613 182345   static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE)){
181614 182346     fts5yyParser *pParser;
181615 182347     pParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
181616         -  if( pParser ){
181617         -#ifdef fts5YYTRACKMAXSTACKDEPTH
181618         -    pParser->fts5yyhwm = 0;
181619         -#endif
181620         -#if fts5YYSTACKDEPTH<=0
181621         -    pParser->fts5yytos = NULL;
181622         -    pParser->fts5yystack = NULL;
181623         -    pParser->fts5yystksz = 0;
181624         -    if( fts5yyGrowStack(pParser) ){
181625         -      pParser->fts5yystack = &pParser->fts5yystk0;
181626         -      pParser->fts5yystksz = 1;
181627         -    }
181628         -#endif
181629         -#ifndef fts5YYNOERRORRECOVERY
181630         -    pParser->fts5yyerrcnt = -1;
181631         -#endif
181632         -    pParser->fts5yytos = pParser->fts5yystack;
181633         -    pParser->fts5yystack[0].stateno = 0;
181634         -    pParser->fts5yystack[0].major = 0;
181635         -  }
       182348  +  if( pParser ) sqlite3Fts5ParserInit(pParser);
181636 182349     return pParser;
181637 182350   }
       182351  +#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
       182352  +
181638 182353   
181639 182354   /* The following function deletes the "minor type" or semantic value
181640 182355   ** associated with a symbol.  The symbol can be either a terminal
181641 182356   ** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
181642 182357   ** a pointer to the value to be deleted.  The code used to do the 
181643 182358   ** deletions is derived from the %destructor and/or %token_destructor
181644 182359   ** directives of the input grammar.
................................................................................
181712 182427         fts5yyTracePrompt,
181713 182428         fts5yyTokenName[fts5yytos->major]);
181714 182429     }
181715 182430   #endif
181716 182431     fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
181717 182432   }
181718 182433   
       182434  +/*
       182435  +** Clear all secondary memory allocations from the parser
       182436  +*/
       182437  +static void sqlite3Fts5ParserFinalize(void *p){
       182438  +  fts5yyParser *pParser = (fts5yyParser*)p;
       182439  +  while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
       182440  +#if fts5YYSTACKDEPTH<=0
       182441  +  if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
       182442  +#endif
       182443  +}
       182444  +
       182445  +#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
181719 182446   /* 
181720 182447   ** Deallocate and destroy a parser.  Destructors are called for
181721 182448   ** all stack elements before shutting the parser down.
181722 182449   **
181723 182450   ** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
181724 182451   ** is defined in a %include section of the input grammar) then it is
181725 182452   ** assumed that the input pointer is never NULL.
181726 182453   */
181727 182454   static void sqlite3Fts5ParserFree(
181728 182455     void *p,                    /* The parser to be deleted */
181729 182456     void (*freeProc)(void*)     /* Function used to reclaim memory */
181730 182457   ){
181731         -  fts5yyParser *pParser = (fts5yyParser*)p;
181732 182458   #ifndef fts5YYPARSEFREENEVERNULL
181733         -  if( pParser==0 ) return;
       182459  +  if( p==0 ) return;
181734 182460   #endif
181735         -  while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
181736         -#if fts5YYSTACKDEPTH<=0
181737         -  if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
181738         -#endif
181739         -  (*freeProc)((void*)pParser);
       182461  +  sqlite3Fts5ParserFinalize(p);
       182462  +  (*freeProc)(p);
181740 182463   }
       182464  +#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
181741 182465   
181742 182466   /*
181743 182467   ** Return the peak depth of the stack for a parser.
181744 182468   */
181745 182469   #ifdef fts5YYTRACKMAXSTACKDEPTH
181746 182470   static int sqlite3Fts5ParserStackPeak(void *p){
181747 182471     fts5yyParser *pParser = (fts5yyParser*)p;
................................................................................
186072 186796     char *z = 0;
186073 186797   
186074 186798     memset(&sCtx, 0, sizeof(TokenCtx));
186075 186799     sCtx.pPhrase = pAppend;
186076 186800   
186077 186801     rc = fts5ParseStringFromToken(pToken, &z);
186078 186802     if( rc==SQLITE_OK ){
186079         -    int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_QUERY : 0);
       186803  +    int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
186080 186804       int n;
186081 186805       sqlite3Fts5Dequote(z);
186082 186806       n = (int)strlen(z);
186083 186807       rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
186084 186808     }
186085 186809     sqlite3_free(z);
186086 186810     if( rc || (rc = sCtx.rc) ){
................................................................................
189746 190470                 &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclist
189747 190471             );
189748 190472           }
189749 190473         }
189750 190474         else if( pLeaf->nn>pLeaf->szLeaf ){
189751 190475           pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
189752 190476               &pLeaf->p[pLeaf->szLeaf], iOff
189753         -            );
       190477  +        );
189754 190478           pIter->iLeafOffset = iOff;
189755 190479           pIter->iEndofDoclist = iOff;
189756 190480           bNewTerm = 1;
189757 190481         }
189758 190482         assert_nc( iOff<pLeaf->szLeaf );
189759 190483         if( iOff>pLeaf->szLeaf ){
189760 190484           p->rc = FTS5_CORRUPT;
................................................................................
189780 190504         ** code is inlined. 
189781 190505         **
189782 190506         ** Later: Switched back to fts5SegIterLoadNPos() because it supports
189783 190507         ** detail=none mode. Not ideal.
189784 190508         */
189785 190509         int nSz;
189786 190510         assert( p->rc==SQLITE_OK );
       190511  +      assert( pIter->iLeafOffset<=pIter->pLeaf->nn );
189787 190512         fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
189788 190513         pIter->bDel = (nSz & 0x0001);
189789 190514         pIter->nPos = nSz>>1;
189790 190515         assert_nc( pIter->nPos>=0 );
189791 190516       }
189792 190517     }
189793 190518   }
................................................................................
190774 191499       xChunk(p, pCtx, pChunk, nChunk);
190775 191500       nRem -= nChunk;
190776 191501       fts5DataRelease(pData);
190777 191502       if( nRem<=0 ){
190778 191503         break;
190779 191504       }else{
190780 191505         pgno++;
190781         -      pData = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno));
       191506  +      pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno));
190782 191507         if( pData==0 ) break;
190783 191508         pChunk = &pData->p[4];
190784 191509         nChunk = MIN(nRem, pData->szLeaf - 4);
190785 191510         if( pgno==pgnoSave ){
190786 191511           assert( pSeg->pNextLeaf==0 );
190787 191512           pSeg->pNextLeaf = pData;
190788 191513           pData = 0;
................................................................................
193536 194261       int iIdxLeaf = sqlite3_column_int(pStmt, 2);
193537 194262       int bIdxDlidx = sqlite3_column_int(pStmt, 3);
193538 194263   
193539 194264       /* If the leaf in question has already been trimmed from the segment, 
193540 194265       ** ignore this b-tree entry. Otherwise, load it into memory. */
193541 194266       if( iIdxLeaf<pSeg->pgnoFirst ) continue;
193542 194267       iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf);
193543         -    pLeaf = fts5DataRead(p, iRow);
       194268  +    pLeaf = fts5LeafRead(p, iRow);
193544 194269       if( pLeaf==0 ) break;
193545 194270   
193546 194271       /* Check that the leaf contains at least one term, and that it is equal
193547 194272       ** to or larger than the split-key in zIdxTerm.  Also check that if there
193548 194273       ** is also a rowid pointer within the leaf page header, it points to a
193549 194274       ** location before the term.  */
193550 194275       if( pLeaf->nn<=pLeaf->szLeaf ){
................................................................................
196812 197537   static void fts5SourceIdFunc(
196813 197538     sqlite3_context *pCtx,          /* Function call context */
196814 197539     int nArg,                       /* Number of args */
196815 197540     sqlite3_value **apUnused        /* Function arguments */
196816 197541   ){
196817 197542     assert( nArg==0 );
196818 197543     UNUSED_PARAM2(nArg, apUnused);
196819         -  sqlite3_result_text(pCtx, "fts5: 2017-01-03 18:27:03 979f04392853b8053817a3eea2fc679947b437fd", -1, SQLITE_TRANSIENT);
       197544  +  sqlite3_result_text(pCtx, "fts5: 2017-02-13 16:02:40 ada05cfa86ad7f5645450ac7a2a21c9aa6e57d2c", -1, SQLITE_TRANSIENT);
196820 197545   }
196821 197546   
196822 197547   static int fts5Init(sqlite3 *db){
196823 197548     static const sqlite3_module fts5Mod = {
196824 197549       /* iVersion      */ 2,
196825 197550       /* xCreate       */ fts5CreateMethod,
196826 197551       /* xConnect      */ fts5ConnectMethod,

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

   117    117   ** string contains the date and time of the check-in (UTC) and an SHA1
   118    118   ** hash of the entire source tree.
   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         -#define SQLITE_VERSION        "3.16.1"
   125         -#define SQLITE_VERSION_NUMBER 3016001
   126         -#define SQLITE_SOURCE_ID      "2017-01-03 18:27:03 979f04392853b8053817a3eea2fc679947b437fd"
          124  +#define SQLITE_VERSION        "3.17.0"
          125  +#define SQLITE_VERSION_NUMBER 3017000
          126  +#define SQLITE_SOURCE_ID      "2017-02-13 16:02:40 ada05cfa86ad7f5645450ac7a2a21c9aa6e57d2c"
   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
................................................................................
   255    255   ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
   256    256   ** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
   257    257   ** sqlite3_uint64 and sqlite_uint64 types can store integer values 
   258    258   ** between 0 and +18446744073709551615 inclusive.
   259    259   */
   260    260   #ifdef SQLITE_INT64_TYPE
   261    261     typedef SQLITE_INT64_TYPE sqlite_int64;
   262         -  typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
          262  +# ifdef SQLITE_UINT64_TYPE
          263  +    typedef SQLITE_UINT64_TYPE sqlite_uint64;
          264  +# else  
          265  +    typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
          266  +# endif
   263    267   #elif defined(_MSC_VER) || defined(__BORLANDC__)
   264    268     typedef __int64 sqlite_int64;
   265    269     typedef unsigned __int64 sqlite_uint64;
   266    270   #else
   267    271     typedef long long int sqlite_int64;
   268    272     typedef unsigned long long int sqlite_uint64;
   269    273   #endif
................................................................................
   568    572   ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
   569    573   ** information is written to disk in the same order as calls
   570    574   ** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
   571    575   ** after reboot following a crash or power loss, the only bytes in a
   572    576   ** file that were written at the application level might have changed
   573    577   ** and that adjacent bytes, even bytes within the same sector are
   574    578   ** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
   575         -** flag indicate that a file cannot be deleted when open.  The
          579  +** flag indicates that a file cannot be deleted when open.  The
   576    580   ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
   577    581   ** read-only media and cannot be changed even by processes with
   578    582   ** elevated privileges.
   579    583   */
   580    584   #define SQLITE_IOCAP_ATOMIC                 0x00000001
   581    585   #define SQLITE_IOCAP_ATOMIC512              0x00000002
   582    586   #define SQLITE_IOCAP_ATOMIC1K               0x00000004
................................................................................
   718    722   ** <li> [SQLITE_IOCAP_ATOMIC4K]
   719    723   ** <li> [SQLITE_IOCAP_ATOMIC8K]
   720    724   ** <li> [SQLITE_IOCAP_ATOMIC16K]
   721    725   ** <li> [SQLITE_IOCAP_ATOMIC32K]
   722    726   ** <li> [SQLITE_IOCAP_ATOMIC64K]
   723    727   ** <li> [SQLITE_IOCAP_SAFE_APPEND]
   724    728   ** <li> [SQLITE_IOCAP_SEQUENTIAL]
          729  +** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
          730  +** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
          731  +** <li> [SQLITE_IOCAP_IMMUTABLE]
   725    732   ** </ul>
   726    733   **
   727    734   ** The SQLITE_IOCAP_ATOMIC property means that all writes of
   728    735   ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
   729    736   ** mean that writes of blocks that are nnn bytes in size and
   730    737   ** are aligned to an address which is an integer multiple of
   731    738   ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
................................................................................
  3892   3899   SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
  3893   3900   
  3894   3901   /*
  3895   3902   ** CAPI3REF: Number Of Columns In A Result Set
  3896   3903   ** METHOD: sqlite3_stmt
  3897   3904   **
  3898   3905   ** ^Return the number of columns in the result set returned by the
  3899         -** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
  3900         -** statement that does not return data (for example an [UPDATE]).
         3906  +** [prepared statement]. ^If this routine returns 0, that means the 
         3907  +** [prepared statement] returns no data (for example an [UPDATE]).
         3908  +** ^However, just because this routine returns a positive number does not
         3909  +** mean that one or more rows of data will be returned.  ^A SELECT statement
         3910  +** will always have a positive sqlite3_column_count() but depending on the
         3911  +** WHERE clause constraints and the table content, it might return no rows.
  3901   3912   **
  3902   3913   ** See also: [sqlite3_data_count()]
  3903   3914   */
  3904   3915   SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
  3905   3916   
  3906   3917   /*
  3907   3918   ** CAPI3REF: Column Names In A Result Set
................................................................................
  5402   5413   ** ^In the case of an update, this is the [rowid] after the update takes place.
  5403   5414   **
  5404   5415   ** ^(The update hook is not invoked when internal system tables are
  5405   5416   ** modified (i.e. sqlite_master and sqlite_sequence).)^
  5406   5417   ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
  5407   5418   **
  5408   5419   ** ^In the current implementation, the update hook
  5409         -** is not invoked when duplication rows are deleted because of an
         5420  +** is not invoked when conflicting rows are deleted because of an
  5410   5421   ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
  5411   5422   ** invoked when rows are deleted using the [truncate optimization].
  5412   5423   ** The exceptions defined in this paragraph might change in a future
  5413   5424   ** release of SQLite.
  5414   5425   **
  5415   5426   ** The update hook implementation must not do anything that will modify
  5416   5427   ** the database connection that invoked the update hook.  Any actions
................................................................................
  6184   6195   **         being opened for read/write access)^.
  6185   6196   ** </ul>
  6186   6197   **
  6187   6198   ** ^Unless it returns SQLITE_MISUSE, this function sets the 
  6188   6199   ** [database connection] error code and message accessible via 
  6189   6200   ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. 
  6190   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.
  6191   6208   **
  6192   6209   ** ^(If the row that a BLOB handle points to is modified by an
  6193   6210   ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
  6194   6211   ** then the BLOB handle is marked as "expired".
  6195   6212   ** This is true if any column of the row is changed, even a column
  6196   6213   ** other than the one the BLOB handle is open on.)^
  6197   6214   ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
................................................................................
  6207   6224   **
  6208   6225   ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
  6209   6226   ** and the built-in [zeroblob] SQL function may be used to create a 
  6210   6227   ** zero-filled blob to read or write using the incremental-blob interface.
  6211   6228   **
  6212   6229   ** To avoid a resource leak, every open [BLOB handle] should eventually
  6213   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()].
  6214   6235   */
  6215   6236   SQLITE_API int sqlite3_blob_open(
  6216   6237     sqlite3*,
  6217   6238     const char *zDb,
  6218   6239     const char *zTable,
  6219   6240     const char *zColumn,
  6220   6241     sqlite3_int64 iRow,
................................................................................
  6222   6243     sqlite3_blob **ppBlob
  6223   6244   );
  6224   6245   
  6225   6246   /*
  6226   6247   ** CAPI3REF: Move a BLOB Handle to a New Row
  6227   6248   ** METHOD: sqlite3_blob
  6228   6249   **
  6229         -** ^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
  6230   6251   ** to a different row of the same database table. ^The new row is identified
  6231   6252   ** by the rowid value passed as the second argument. Only the row can be
  6232   6253   ** changed. ^The database, table and column on which the blob handle is open
  6233         -** 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
  6234   6255   ** faster than closing the existing handle and opening a new one.
  6235   6256   **
  6236   6257   ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
  6237   6258   ** it must exist and there must be either a blob or text value stored in
  6238   6259   ** the nominated column.)^ ^If the new row is not present in the table, or if
  6239   6260   ** it does not contain a blob or text value, or if another error occurs, an
  6240   6261   ** SQLite error code is returned and the blob handle is considered aborted.
................................................................................
  8155   8176   ** CAPI3REF: The pre-update hook.
  8156   8177   **
  8157   8178   ** ^These interfaces are only available if SQLite is compiled using the
  8158   8179   ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
  8159   8180   **
  8160   8181   ** ^The [sqlite3_preupdate_hook()] interface registers a callback function
  8161   8182   ** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
  8162         -** on a [rowid table].
         8183  +** on a database table.
  8163   8184   ** ^At most one preupdate hook may be registered at a time on a single
  8164   8185   ** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
  8165   8186   ** the previous setting.
  8166   8187   ** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
  8167   8188   ** with a NULL pointer as the second parameter.
  8168   8189   ** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
  8169   8190   ** the first parameter to callbacks.
  8170   8191   **
  8171         -** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate
  8172         -** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID]
  8173         -** 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.
  8174   8195   **
  8175   8196   ** ^The second parameter to the preupdate callback is a pointer to
  8176   8197   ** the [database connection] that registered the preupdate hook.
  8177   8198   ** ^The third parameter to the preupdate callback is one of the constants
  8178   8199   ** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
  8179   8200   ** kind of update operation that is about to occur.
  8180   8201   ** ^(The fourth parameter to the preupdate callback is the name of the
  8181   8202   ** database within the database connection that is being modified.  This
  8182   8203   ** will be "main" for the main database or "temp" for TEMP tables or 
  8183   8204   ** the name given after the AS keyword in the [ATTACH] statement for attached
  8184   8205   ** databases.)^
  8185   8206   ** ^The fifth parameter to the preupdate callback is the name of the
  8186   8207   ** table that is being modified.
  8187         -** ^The sixth parameter to the preupdate callback is the initial [rowid] of the
  8188         -** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is
  8189         -** undefined for SQLITE_INSERT changes.
  8190         -** ^The seventh parameter to the preupdate callback is the final [rowid] of
  8191         -** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is
  8192         -** 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.
  8193   8218   **
  8194   8219   ** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
  8195   8220   ** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
  8196   8221   ** provide additional information about a preupdate event. These routines
  8197   8222   ** may only be called from within a preupdate callback.  Invoking any of
  8198   8223   ** these routines from outside of a preupdate callback or with a
  8199   8224   ** [database connection] pointer that is different from the one supplied
................................................................................
  8621   8646   ** either of these things are undefined.
  8622   8647   **
  8623   8648   ** The session object will be used to create changesets for tables in
  8624   8649   ** database zDb, where zDb is either "main", or "temp", or the name of an
  8625   8650   ** attached database. It is not an error if database zDb is not attached
  8626   8651   ** to the database when the session object is created.
  8627   8652   */
  8628         -int sqlite3session_create(
         8653  +SQLITE_API int sqlite3session_create(
  8629   8654     sqlite3 *db,                    /* Database handle */
  8630   8655     const char *zDb,                /* Name of db (e.g. "main") */
  8631   8656     sqlite3_session **ppSession     /* OUT: New session object */
  8632   8657   );
  8633   8658   
  8634   8659   /*
  8635   8660   ** CAPI3REF: Delete A Session Object
................................................................................
  8639   8664   ** results of attempting to use pSession with any other session module
  8640   8665   ** function are undefined.
  8641   8666   **
  8642   8667   ** Session objects must be deleted before the database handle to which they
  8643   8668   ** are attached is closed. Refer to the documentation for 
  8644   8669   ** [sqlite3session_create()] for details.
  8645   8670   */
  8646         -void sqlite3session_delete(sqlite3_session *pSession);
         8671  +SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
  8647   8672   
  8648   8673   
  8649   8674   /*
  8650   8675   ** CAPI3REF: Enable Or Disable A Session Object
  8651   8676   **
  8652   8677   ** Enable or disable the recording of changes by a session object. When
  8653   8678   ** enabled, a session object records changes made to the database. When
................................................................................
  8659   8684   ** Passing zero to this function disables the session. Passing a value
  8660   8685   ** greater than zero enables it. Passing a value less than zero is a 
  8661   8686   ** no-op, and may be used to query the current state of the session.
  8662   8687   **
  8663   8688   ** The return value indicates the final state of the session object: 0 if 
  8664   8689   ** the session is disabled, or 1 if it is enabled.
  8665   8690   */
  8666         -int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
         8691  +SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
  8667   8692   
  8668   8693   /*
  8669   8694   ** CAPI3REF: Set Or Clear the Indirect Change Flag
  8670   8695   **
  8671   8696   ** Each change recorded by a session object is marked as either direct or
  8672   8697   ** indirect. A change is marked as indirect if either:
  8673   8698   **
................................................................................
  8688   8713   ** is set. Passing a value less than zero does not modify the current value
  8689   8714   ** of the indirect flag, and may be used to query the current state of the 
  8690   8715   ** indirect flag for the specified session object.
  8691   8716   **
  8692   8717   ** The return value indicates the final state of the indirect flag: 0 if 
  8693   8718   ** it is clear, or 1 if it is set.
  8694   8719   */
  8695         -int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
         8720  +SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
  8696   8721   
  8697   8722   /*
  8698   8723   ** CAPI3REF: Attach A Table To A Session Object
  8699   8724   **
  8700   8725   ** If argument zTab is not NULL, then it is the name of a table to attach
  8701   8726   ** to the session object passed as the first argument. All subsequent changes 
  8702   8727   ** made to the table while the session object is enabled will be recorded. See 
................................................................................
  8718   8743   **
  8719   8744   ** Changes are not recorded for individual rows that have NULL values stored
  8720   8745   ** in one or more of their PRIMARY KEY columns.
  8721   8746   **
  8722   8747   ** SQLITE_OK is returned if the call completes without error. Or, if an error 
  8723   8748   ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
  8724   8749   */
  8725         -int sqlite3session_attach(
         8750  +SQLITE_API int sqlite3session_attach(
  8726   8751     sqlite3_session *pSession,      /* Session object */
  8727   8752     const char *zTab                /* Table name */
  8728   8753   );
  8729   8754   
  8730   8755   /*
  8731   8756   ** CAPI3REF: Set a table filter on a Session Object.
  8732   8757   **
  8733   8758   ** The second argument (xFilter) is the "filter callback". For changes to rows 
  8734   8759   ** in tables that are not attached to the Session object, the filter is called
  8735   8760   ** to determine whether changes to the table's rows should be tracked or not. 
  8736   8761   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
  8737   8762   ** attached, xFilter will not be called again.
  8738   8763   */
  8739         -void sqlite3session_table_filter(
         8764  +SQLITE_API void sqlite3session_table_filter(
  8740   8765     sqlite3_session *pSession,      /* Session object */
  8741   8766     int(*xFilter)(
  8742   8767       void *pCtx,                   /* Copy of third arg to _filter_table() */
  8743   8768       const char *zTab              /* Table name */
  8744   8769     ),
  8745   8770     void *pCtx                      /* First argument passed to xFilter */
  8746   8771   );
................................................................................
  8845   8870   ** is inserted while a session object is enabled, then later deleted while 
  8846   8871   ** the same session object is disabled, no INSERT record will appear in the
  8847   8872   ** changeset, even though the delete took place while the session was disabled.
  8848   8873   ** Or, if one field of a row is updated while a session is disabled, and 
  8849   8874   ** another field of the same row is updated while the session is enabled, the
  8850   8875   ** resulting changeset will contain an UPDATE change that updates both fields.
  8851   8876   */
  8852         -int sqlite3session_changeset(
         8877  +SQLITE_API int sqlite3session_changeset(
  8853   8878     sqlite3_session *pSession,      /* Session object */
  8854   8879     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
  8855   8880     void **ppChangeset              /* OUT: Buffer containing changeset */
  8856   8881   );
  8857   8882   
  8858   8883   /*
  8859   8884   ** CAPI3REF: Load The Difference Between Tables Into A Session 
................................................................................
  8889   8914   **   <li> For each row (primary key) that exists in the to-table but not in 
  8890   8915   **     the from-table, an INSERT record is added to the session object.
  8891   8916   **
  8892   8917   **   <li> For each row (primary key) that exists in the to-table but not in 
  8893   8918   **     the from-table, a DELETE record is added to the session object.
  8894   8919   **
  8895   8920   **   <li> For each row (primary key) that exists in both tables, but features 
  8896         -**     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.  
  8897   8923   ** </ul>
  8898   8924   **
  8899   8925   ** To clarify, if this function is called and then a changeset constructed
  8900   8926   ** using [sqlite3session_changeset()], then after applying that changeset to 
  8901   8927   ** database zFrom the contents of the two compatible tables would be 
  8902   8928   ** identical.
  8903   8929   **
................................................................................
  8906   8932   **
  8907   8933   ** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
  8908   8934   ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
  8909   8935   ** may be set to point to a buffer containing an English language error 
  8910   8936   ** message. It is the responsibility of the caller to free this buffer using
  8911   8937   ** sqlite3_free().
  8912   8938   */
  8913         -int sqlite3session_diff(
         8939  +SQLITE_API int sqlite3session_diff(
  8914   8940     sqlite3_session *pSession,
  8915   8941     const char *zFromDb,
  8916   8942     const char *zTbl,
  8917   8943     char **pzErrMsg
  8918   8944   );
  8919   8945   
  8920   8946   
................................................................................
  8942   8968   ** in the same way as for changesets.
  8943   8969   **
  8944   8970   ** Changes within a patchset are ordered in the same way as for changesets
  8945   8971   ** generated by the sqlite3session_changeset() function (i.e. all changes for
  8946   8972   ** a single table are grouped together, tables appear in the order in which
  8947   8973   ** they were attached to the session object).
  8948   8974   */
  8949         -int sqlite3session_patchset(
         8975  +SQLITE_API int sqlite3session_patchset(
  8950   8976     sqlite3_session *pSession,      /* Session object */
  8951   8977     int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
  8952   8978     void **ppPatchset               /* OUT: Buffer containing changeset */
  8953   8979   );
  8954   8980   
  8955   8981   /*
  8956   8982   ** CAPI3REF: Test if a changeset has recorded any changes.
................................................................................
  8963   8989   ** [sqlite3session_changeset()] on the session handle may still return a
  8964   8990   ** changeset that contains no changes. This can happen when a row in 
  8965   8991   ** an attached table is modified and then later on the original values 
  8966   8992   ** are restored. However, if this function returns non-zero, then it is
  8967   8993   ** guaranteed that a call to sqlite3session_changeset() will return a 
  8968   8994   ** changeset containing zero changes.
  8969   8995   */
  8970         -int sqlite3session_isempty(sqlite3_session *pSession);
         8996  +SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
  8971   8997   
  8972   8998   /*
  8973   8999   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
  8974   9000   **
  8975   9001   ** Create an iterator used to iterate through the contents of a changeset.
  8976   9002   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
  8977   9003   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
................................................................................
  8998   9024   ** that apply to a single table are grouped together. This means that when 
  8999   9025   ** an application iterates through a changeset using an iterator created by 
  9000   9026   ** this function, all changes that relate to a single table are visited 
  9001   9027   ** consecutively. There is no chance that the iterator will visit a change 
  9002   9028   ** the applies to table X, then one for table Y, and then later on visit 
  9003   9029   ** another change for table X.
  9004   9030   */
  9005         -int sqlite3changeset_start(
         9031  +SQLITE_API int sqlite3changeset_start(
  9006   9032     sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
  9007   9033     int nChangeset,                 /* Size of changeset blob in bytes */
  9008   9034     void *pChangeset                /* Pointer to blob containing changeset */
  9009   9035   );
  9010   9036   
  9011   9037   
  9012   9038   /*
................................................................................
  9027   9053   ** Otherwise, if all changes in the changeset have already been visited,
  9028   9054   ** SQLITE_DONE is returned.
  9029   9055   **
  9030   9056   ** If an error occurs, an SQLite error code is returned. Possible error 
  9031   9057   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
  9032   9058   ** SQLITE_NOMEM.
  9033   9059   */
  9034         -int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
         9060  +SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
  9035   9061   
  9036   9062   /*
  9037   9063   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
  9038   9064   **
  9039   9065   ** The pIter argument passed to this function may either be an iterator
  9040   9066   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
  9041   9067   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
................................................................................
  9055   9081   ** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the 
  9056   9082   ** type of change that the iterator currently points to.
  9057   9083   **
  9058   9084   ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
  9059   9085   ** SQLite error code is returned. The values of the output variables may not
  9060   9086   ** be trusted in this case.
  9061   9087   */
  9062         -int sqlite3changeset_op(
         9088  +SQLITE_API int sqlite3changeset_op(
  9063   9089     sqlite3_changeset_iter *pIter,  /* Iterator object */
  9064   9090     const char **pzTab,             /* OUT: Pointer to table name */
  9065   9091     int *pnCol,                     /* OUT: Number of columns in table */
  9066   9092     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
  9067   9093     int *pbIndirect                 /* OUT: True for an 'indirect' change */
  9068   9094   );
  9069   9095   
................................................................................
  9088   9114   ** in the table.
  9089   9115   **
  9090   9116   ** If this function is called when the iterator does not point to a valid
  9091   9117   ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
  9092   9118   ** SQLITE_OK is returned and the output variables populated as described
  9093   9119   ** above.
  9094   9120   */
  9095         -int sqlite3changeset_pk(
         9121  +SQLITE_API int sqlite3changeset_pk(
  9096   9122     sqlite3_changeset_iter *pIter,  /* Iterator object */
  9097   9123     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
  9098   9124     int *pnCol                      /* OUT: Number of entries in output array */
  9099   9125   );
  9100   9126   
  9101   9127   /*
  9102   9128   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
................................................................................
  9118   9144   ** original row values stored as part of the UPDATE or DELETE change and
  9119   9145   ** returns SQLITE_OK. The name of the function comes from the fact that this 
  9120   9146   ** is similar to the "old.*" columns available to update or delete triggers.
  9121   9147   **
  9122   9148   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9123   9149   ** is returned and *ppValue is set to NULL.
  9124   9150   */
  9125         -int sqlite3changeset_old(
         9151  +SQLITE_API int sqlite3changeset_old(
  9126   9152     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9127   9153     int iVal,                       /* Column number */
  9128   9154     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
  9129   9155   );
  9130   9156   
  9131   9157   /*
  9132   9158   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
................................................................................
  9151   9177   ** SQLITE_OK returned. The name of the function comes from the fact that 
  9152   9178   ** this is similar to the "new.*" columns available to update or delete 
  9153   9179   ** triggers.
  9154   9180   **
  9155   9181   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9156   9182   ** is returned and *ppValue is set to NULL.
  9157   9183   */
  9158         -int sqlite3changeset_new(
         9184  +SQLITE_API int sqlite3changeset_new(
  9159   9185     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9160   9186     int iVal,                       /* Column number */
  9161   9187     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
  9162   9188   );
  9163   9189   
  9164   9190   /*
  9165   9191   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
................................................................................
  9178   9204   ** sqlite3_value object containing the iVal'th value from the 
  9179   9205   ** "conflicting row" associated with the current conflict-handler callback
  9180   9206   ** and returns SQLITE_OK.
  9181   9207   **
  9182   9208   ** If some other error occurs (e.g. an OOM condition), an SQLite error code
  9183   9209   ** is returned and *ppValue is set to NULL.
  9184   9210   */
  9185         -int sqlite3changeset_conflict(
         9211  +SQLITE_API int sqlite3changeset_conflict(
  9186   9212     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9187   9213     int iVal,                       /* Column number */
  9188   9214     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
  9189   9215   );
  9190   9216   
  9191   9217   /*
  9192   9218   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
................................................................................
  9194   9220   ** This function may only be called with an iterator passed to an
  9195   9221   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
  9196   9222   ** it sets the output variable to the total number of known foreign key
  9197   9223   ** violations in the destination database and returns SQLITE_OK.
  9198   9224   **
  9199   9225   ** In all other cases this function returns SQLITE_MISUSE.
  9200   9226   */
  9201         -int sqlite3changeset_fk_conflicts(
         9227  +SQLITE_API int sqlite3changeset_fk_conflicts(
  9202   9228     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  9203   9229     int *pnOut                      /* OUT: Number of FK violations */
  9204   9230   );
  9205   9231   
  9206   9232   
  9207   9233   /*
  9208   9234   ** CAPI3REF: Finalize A Changeset Iterator
................................................................................
  9227   9253   **     // Do something with change.
  9228   9254   **   }
  9229   9255   **   rc = sqlite3changeset_finalize();
  9230   9256   **   if( rc!=SQLITE_OK ){
  9231   9257   **     // An error has occurred 
  9232   9258   **   }
  9233   9259   */
  9234         -int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
         9260  +SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
  9235   9261   
  9236   9262   /*
  9237   9263   ** CAPI3REF: Invert A Changeset
  9238   9264   **
  9239   9265   ** This function is used to "invert" a changeset object. Applying an inverted
  9240   9266   ** changeset to a database reverses the effects of applying the uninverted
  9241   9267   ** changeset. Specifically:
................................................................................
  9257   9283   ** It is the responsibility of the caller to eventually call sqlite3_free()
  9258   9284   ** on the *ppOut pointer to free the buffer allocation following a successful 
  9259   9285   ** call to this function.
  9260   9286   **
  9261   9287   ** WARNING/TODO: This function currently assumes that the input is a valid
  9262   9288   ** changeset. If it is not, the results are undefined.
  9263   9289   */
  9264         -int sqlite3changeset_invert(
         9290  +SQLITE_API int sqlite3changeset_invert(
  9265   9291     int nIn, const void *pIn,       /* Input changeset */
  9266   9292     int *pnOut, void **ppOut        /* OUT: Inverse of input */
  9267   9293   );
  9268   9294   
  9269   9295   /*
  9270   9296   ** CAPI3REF: Concatenate Two Changeset Objects
  9271   9297   **
................................................................................
  9286   9312   **   }else{
  9287   9313   **     *ppOut = 0;
  9288   9314   **     *pnOut = 0;
  9289   9315   **   }
  9290   9316   **
  9291   9317   ** Refer to the sqlite3_changegroup documentation below for details.
  9292   9318   */
  9293         -int sqlite3changeset_concat(
         9319  +SQLITE_API int sqlite3changeset_concat(
  9294   9320     int nA,                         /* Number of bytes in buffer pA */
  9295   9321     void *pA,                       /* Pointer to buffer containing changeset A */
  9296   9322     int nB,                         /* Number of bytes in buffer pB */
  9297   9323     void *pB,                       /* Pointer to buffer containing changeset B */
  9298   9324     int *pnOut,                     /* OUT: Number of bytes in output changeset */
  9299   9325     void **ppOut                    /* OUT: Buffer containing output changeset */
  9300   9326   );
................................................................................
  9474   9500   ** For each table that is not excluded by the filter callback, this function 
  9475   9501   ** tests that the target database contains a compatible table. A table is 
  9476   9502   ** considered compatible if all of the following are true:
  9477   9503   **
  9478   9504   ** <ul>
  9479   9505   **   <li> The table has the same name as the name recorded in the 
  9480   9506   **        changeset, and
  9481         -**   <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 
  9482   9508   **        changeset, and
  9483   9509   **   <li> The table has primary key columns in the same position as 
  9484   9510   **        recorded in the changeset.
  9485   9511   ** </ul>
  9486   9512   **
  9487   9513   ** If there is no compatible table, it is not an error, but none of the
  9488   9514   ** changes associated with the table are applied. A warning message is issued
................................................................................
  9519   9545   **   original row values stored in the changeset. If it does, and the values 
  9520   9546   **   stored in all non-primary key columns also match the values stored in 
  9521   9547   **   the changeset the row is deleted from the target database.
  9522   9548   **
  9523   9549   **   If a row with matching primary key values is found, but one or more of
  9524   9550   **   the non-primary key fields contains a value different from the original
  9525   9551   **   row value stored in the changeset, the conflict-handler function is
  9526         -**   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.
  9527   9557   **
  9528   9558   **   If no row with matching primary key values is found in the database,
  9529   9559   **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  9530   9560   **   passed as the second argument.
  9531   9561   **
  9532   9562   **   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
  9533   9563   **   (which can only happen if a foreign key constraint is violated), the
................................................................................
  9534   9564   **   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
  9535   9565   **   passed as the second argument. This includes the case where the DELETE
  9536   9566   **   operation is attempted because an earlier call to the conflict handler
  9537   9567   **   function returned [SQLITE_CHANGESET_REPLACE].
  9538   9568   **
  9539   9569   ** <dt>INSERT Changes<dd>
  9540   9570   **   For each INSERT change, an attempt is made to insert the new row into
  9541         -**   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.
  9542   9574   **
  9543   9575   **   If the attempt to insert the row fails because the database already 
  9544   9576   **   contains a row with the same primary key values, the conflict handler
  9545   9577   **   function is invoked with the second argument set to 
  9546   9578   **   [SQLITE_CHANGESET_CONFLICT].
  9547   9579   **
  9548   9580   **   If the attempt to insert the row fails because of some other constraint
................................................................................
  9552   9584   **   an earlier call to the conflict handler function returned 
  9553   9585   **   [SQLITE_CHANGESET_REPLACE].
  9554   9586   **
  9555   9587   ** <dt>UPDATE Changes<dd>
  9556   9588   **   For each UPDATE change, this function checks if the target database 
  9557   9589   **   contains a row with the same primary key value (or values) as the 
  9558   9590   **   original row values stored in the changeset. If it does, and the values 
  9559         -**   stored in all non-primary key columns also match the values stored in 
  9560         -**   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.
  9561   9593   **
  9562   9594   **   If a row with matching primary key values is found, but one or more of
  9563         -**   the non-primary key fields contains a value different from an original
  9564         -**   row value stored in the changeset, the conflict-handler function is
  9565         -**   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
  9566   9598   **   UPDATE changes only contain values for non-primary key fields that are
  9567   9599   **   to be modified, only those fields need to match the original values to
  9568   9600   **   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
  9569   9601   **
  9570   9602   **   If no row with matching primary key values is found in the database,
  9571   9603   **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  9572   9604   **   passed as the second argument.
................................................................................
  9586   9618   **
  9587   9619   ** All changes made by this function are enclosed in a savepoint transaction.
  9588   9620   ** If any other error (aside from a constraint failure when attempting to
  9589   9621   ** write to the target database) occurs, then the savepoint transaction is
  9590   9622   ** rolled back, restoring the target database to its original state, and an 
  9591   9623   ** SQLite error code returned.
  9592   9624   */
  9593         -int sqlite3changeset_apply(
         9625  +SQLITE_API int sqlite3changeset_apply(
  9594   9626     sqlite3 *db,                    /* Apply change to "main" db of this handle */
  9595   9627     int nChangeset,                 /* Size of changeset in bytes */
  9596   9628     void *pChangeset,               /* Changeset blob */
  9597   9629     int(*xFilter)(
  9598   9630       void *pCtx,                   /* Copy of sixth arg to _apply() */
  9599   9631       const char *zTab              /* Table name */
  9600   9632     ),
................................................................................
  9787   9819   ** is immediately abandoned and the streaming API function returns a copy
  9788   9820   ** of the xOutput error code to the application.
  9789   9821   **
  9790   9822   ** The sessions module never invokes an xOutput callback with the third 
  9791   9823   ** parameter set to a value less than or equal to zero. Other than this,
  9792   9824   ** no guarantees are made as to the size of the chunks of data returned.
  9793   9825   */
  9794         -int sqlite3changeset_apply_strm(
         9826  +SQLITE_API int sqlite3changeset_apply_strm(
  9795   9827     sqlite3 *db,                    /* Apply change to "main" db of this handle */
  9796   9828     int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
  9797   9829     void *pIn,                                          /* First arg for xInput */
  9798   9830     int(*xFilter)(
  9799   9831       void *pCtx,                   /* Copy of sixth arg to _apply() */
  9800   9832       const char *zTab              /* Table name */
  9801   9833     ),
................................................................................
  9802   9834     int(*xConflict)(
  9803   9835       void *pCtx,                   /* Copy of sixth arg to _apply() */
  9804   9836       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
  9805   9837       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
  9806   9838     ),
  9807   9839     void *pCtx                      /* First argument passed to xConflict */
  9808   9840   );
  9809         -int sqlite3changeset_concat_strm(
         9841  +SQLITE_API int sqlite3changeset_concat_strm(
  9810   9842     int (*xInputA)(void *pIn, void *pData, int *pnData),
  9811   9843     void *pInA,
  9812   9844     int (*xInputB)(void *pIn, void *pData, int *pnData),
  9813   9845     void *pInB,
  9814   9846     int (*xOutput)(void *pOut, const void *pData, int nData),
  9815   9847     void *pOut
  9816   9848   );
  9817         -int sqlite3changeset_invert_strm(
         9849  +SQLITE_API int sqlite3changeset_invert_strm(
  9818   9850     int (*xInput)(void *pIn, void *pData, int *pnData),
  9819   9851     void *pIn,
  9820   9852     int (*xOutput)(void *pOut, const void *pData, int nData),
  9821   9853     void *pOut
  9822   9854   );
  9823         -int sqlite3changeset_start_strm(
         9855  +SQLITE_API int sqlite3changeset_start_strm(
  9824   9856     sqlite3_changeset_iter **pp,
  9825   9857     int (*xInput)(void *pIn, void *pData, int *pnData),
  9826   9858     void *pIn
  9827   9859   );
  9828         -int sqlite3session_changeset_strm(
         9860  +SQLITE_API int sqlite3session_changeset_strm(
  9829   9861     sqlite3_session *pSession,
  9830   9862     int (*xOutput)(void *pOut, const void *pData, int nData),
  9831   9863     void *pOut
  9832   9864   );
  9833         -int sqlite3session_patchset_strm(
         9865  +SQLITE_API int sqlite3session_patchset_strm(
  9834   9866     sqlite3_session *pSession,
  9835   9867     int (*xOutput)(void *pOut, const void *pData, int nData),
  9836   9868     void *pOut
  9837   9869   );
  9838   9870   int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
  9839   9871       int (*xInput)(void *pIn, void *pData, int *pnData),
  9840   9872       void *pIn