| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.16.2. 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. |
| | @@ -202,15 +202,32 @@ |
| 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 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 | +** There was once also a CLANG_VERSION macro. However, we learn that the |
| 214 | +** version numbers in clang are for "marketing" only and are inconsistent |
| 215 | +** and unreliable. Fortunately, all versions of clang also recognize the |
| 216 | +** gcc version numbers and have reasonable settings for gcc version numbers, |
| 217 | +** so the GCC_VERSION macro will be set to a correct non-zero value even |
| 218 | +** when compiling with clang. |
| 219 | +*/ |
| 220 | +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC) |
| 209 | 221 | # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__) |
| 210 | 222 | #else |
| 211 | 223 | # define GCC_VERSION 0 |
| 224 | +#endif |
| 225 | +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC) |
| 226 | +# define MSVC_VERSION _MSC_VER |
| 227 | +#else |
| 228 | +# define MSVC_VERSION 0 |
| 212 | 229 | #endif |
| 213 | 230 | |
| 214 | 231 | /* Needed for various definitions... */ |
| 215 | 232 | #if defined(__GNUC__) && !defined(_GNU_SOURCE) |
| 216 | 233 | # define _GNU_SOURCE |
| | @@ -379,13 +396,13 @@ |
| 379 | 396 | ** |
| 380 | 397 | ** See also: [sqlite3_libversion()], |
| 381 | 398 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 382 | 399 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 383 | 400 | */ |
| 384 | | -#define SQLITE_VERSION "3.16.2" |
| 385 | | -#define SQLITE_VERSION_NUMBER 3016002 |
| 386 | | -#define SQLITE_SOURCE_ID "2017-01-06 16:32:41 a65a62893ca8319e89e48b8a38cf8a59c69a8209" |
| 401 | +#define SQLITE_VERSION "3.17.0" |
| 402 | +#define SQLITE_VERSION_NUMBER 3017000 |
| 403 | +#define SQLITE_SOURCE_ID "2017-02-13 16:02:40 ada05cfa86ad7f5645450ac7a2a21c9aa6e57d2c" |
| 387 | 404 | |
| 388 | 405 | /* |
| 389 | 406 | ** CAPI3REF: Run-Time Library Version Numbers |
| 390 | 407 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 391 | 408 | ** |
| | @@ -517,11 +534,15 @@ |
| 517 | 534 | ** sqlite3_uint64 and sqlite_uint64 types can store integer values |
| 518 | 535 | ** between 0 and +18446744073709551615 inclusive. |
| 519 | 536 | */ |
| 520 | 537 | #ifdef SQLITE_INT64_TYPE |
| 521 | 538 | typedef SQLITE_INT64_TYPE sqlite_int64; |
| 522 | | - typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; |
| 539 | +# ifdef SQLITE_UINT64_TYPE |
| 540 | + typedef SQLITE_UINT64_TYPE sqlite_uint64; |
| 541 | +# else |
| 542 | + typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; |
| 543 | +# endif |
| 523 | 544 | #elif defined(_MSC_VER) || defined(__BORLANDC__) |
| 524 | 545 | typedef __int64 sqlite_int64; |
| 525 | 546 | typedef unsigned __int64 sqlite_uint64; |
| 526 | 547 | #else |
| 527 | 548 | typedef long long int sqlite_int64; |
| | @@ -830,11 +851,11 @@ |
| 830 | 851 | ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that |
| 831 | 852 | ** after reboot following a crash or power loss, the only bytes in a |
| 832 | 853 | ** file that were written at the application level might have changed |
| 833 | 854 | ** and that adjacent bytes, even bytes within the same sector are |
| 834 | 855 | ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
| 835 | | -** flag indicate that a file cannot be deleted when open. The |
| 856 | +** flag indicates that a file cannot be deleted when open. The |
| 836 | 857 | ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on |
| 837 | 858 | ** read-only media and cannot be changed even by processes with |
| 838 | 859 | ** elevated privileges. |
| 839 | 860 | */ |
| 840 | 861 | #define SQLITE_IOCAP_ATOMIC 0x00000001 |
| | @@ -980,10 +1001,13 @@ |
| 980 | 1001 | ** <li> [SQLITE_IOCAP_ATOMIC16K] |
| 981 | 1002 | ** <li> [SQLITE_IOCAP_ATOMIC32K] |
| 982 | 1003 | ** <li> [SQLITE_IOCAP_ATOMIC64K] |
| 983 | 1004 | ** <li> [SQLITE_IOCAP_SAFE_APPEND] |
| 984 | 1005 | ** <li> [SQLITE_IOCAP_SEQUENTIAL] |
| 1006 | +** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN] |
| 1007 | +** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE] |
| 1008 | +** <li> [SQLITE_IOCAP_IMMUTABLE] |
| 985 | 1009 | ** </ul> |
| 986 | 1010 | ** |
| 987 | 1011 | ** The SQLITE_IOCAP_ATOMIC property means that all writes of |
| 988 | 1012 | ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values |
| 989 | 1013 | ** mean that writes of blocks that are nnn bytes in size and |
| | @@ -5668,11 +5692,11 @@ |
| 5668 | 5692 | ** ^(The update hook is not invoked when internal system tables are |
| 5669 | 5693 | ** modified (i.e. sqlite_master and sqlite_sequence).)^ |
| 5670 | 5694 | ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified. |
| 5671 | 5695 | ** |
| 5672 | 5696 | ** ^In the current implementation, the update hook |
| 5673 | | -** is not invoked when duplication rows are deleted because of an |
| 5697 | +** is not invoked when conflicting rows are deleted because of an |
| 5674 | 5698 | ** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook |
| 5675 | 5699 | ** invoked when rows are deleted using the [truncate optimization]. |
| 5676 | 5700 | ** The exceptions defined in this paragraph might change in a future |
| 5677 | 5701 | ** release of SQLite. |
| 5678 | 5702 | ** |
| | @@ -6450,10 +6474,16 @@ |
| 6450 | 6474 | ** |
| 6451 | 6475 | ** ^Unless it returns SQLITE_MISUSE, this function sets the |
| 6452 | 6476 | ** [database connection] error code and message accessible via |
| 6453 | 6477 | ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. |
| 6454 | 6478 | ** |
| 6479 | +** A BLOB referenced by sqlite3_blob_open() may be read using the |
| 6480 | +** [sqlite3_blob_read()] interface and modified by using |
| 6481 | +** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a |
| 6482 | +** different row of the same table using the [sqlite3_blob_reopen()] |
| 6483 | +** interface. However, the column, table, or database of a [BLOB handle] |
| 6484 | +** cannot be changed after the [BLOB handle] is opened. |
| 6455 | 6485 | ** |
| 6456 | 6486 | ** ^(If the row that a BLOB handle points to is modified by an |
| 6457 | 6487 | ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects |
| 6458 | 6488 | ** then the BLOB handle is marked as "expired". |
| 6459 | 6489 | ** This is true if any column of the row is changed, even a column |
| | @@ -6473,10 +6503,14 @@ |
| 6473 | 6503 | ** and the built-in [zeroblob] SQL function may be used to create a |
| 6474 | 6504 | ** zero-filled blob to read or write using the incremental-blob interface. |
| 6475 | 6505 | ** |
| 6476 | 6506 | ** To avoid a resource leak, every open [BLOB handle] should eventually |
| 6477 | 6507 | ** be released by a call to [sqlite3_blob_close()]. |
| 6508 | +** |
| 6509 | +** See also: [sqlite3_blob_close()], |
| 6510 | +** [sqlite3_blob_reopen()], [sqlite3_blob_read()], |
| 6511 | +** [sqlite3_blob_bytes()], [sqlite3_blob_write()]. |
| 6478 | 6512 | */ |
| 6479 | 6513 | SQLITE_API int sqlite3_blob_open( |
| 6480 | 6514 | sqlite3*, |
| 6481 | 6515 | const char *zDb, |
| 6482 | 6516 | const char *zTable, |
| | @@ -6488,15 +6522,15 @@ |
| 6488 | 6522 | |
| 6489 | 6523 | /* |
| 6490 | 6524 | ** CAPI3REF: Move a BLOB Handle to a New Row |
| 6491 | 6525 | ** METHOD: sqlite3_blob |
| 6492 | 6526 | ** |
| 6493 | | -** ^This function is used to move an existing blob handle so that it points |
| 6527 | +** ^This function is used to move an existing [BLOB handle] so that it points |
| 6494 | 6528 | ** to a different row of the same database table. ^The new row is identified |
| 6495 | 6529 | ** by the rowid value passed as the second argument. Only the row can be |
| 6496 | 6530 | ** changed. ^The database, table and column on which the blob handle is open |
| 6497 | | -** remain the same. Moving an existing blob handle to a new row can be |
| 6531 | +** remain the same. Moving an existing [BLOB handle] to a new row is |
| 6498 | 6532 | ** faster than closing the existing handle and opening a new one. |
| 6499 | 6533 | ** |
| 6500 | 6534 | ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] - |
| 6501 | 6535 | ** it must exist and there must be either a blob or text value stored in |
| 6502 | 6536 | ** the nominated column.)^ ^If the new row is not present in the table, or if |
| | @@ -8421,22 +8455,22 @@ |
| 8421 | 8455 | ** ^These interfaces are only available if SQLite is compiled using the |
| 8422 | 8456 | ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option. |
| 8423 | 8457 | ** |
| 8424 | 8458 | ** ^The [sqlite3_preupdate_hook()] interface registers a callback function |
| 8425 | 8459 | ** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation |
| 8426 | | -** on a [rowid table]. |
| 8460 | +** on a database table. |
| 8427 | 8461 | ** ^At most one preupdate hook may be registered at a time on a single |
| 8428 | 8462 | ** [database connection]; each call to [sqlite3_preupdate_hook()] overrides |
| 8429 | 8463 | ** the previous setting. |
| 8430 | 8464 | ** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()] |
| 8431 | 8465 | ** with a NULL pointer as the second parameter. |
| 8432 | 8466 | ** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as |
| 8433 | 8467 | ** the first parameter to callbacks. |
| 8434 | 8468 | ** |
| 8435 | | -** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate |
| 8436 | | -** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID] |
| 8437 | | -** tables. |
| 8469 | +** ^The preupdate hook only fires for changes to real database tables; the |
| 8470 | +** preupdate hook is not invoked for changes to [virtual tables] or to |
| 8471 | +** system tables like sqlite_master or sqlite_stat1. |
| 8438 | 8472 | ** |
| 8439 | 8473 | ** ^The second parameter to the preupdate callback is a pointer to |
| 8440 | 8474 | ** the [database connection] that registered the preupdate hook. |
| 8441 | 8475 | ** ^The third parameter to the preupdate callback is one of the constants |
| 8442 | 8476 | ** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the |
| | @@ -8446,16 +8480,20 @@ |
| 8446 | 8480 | ** will be "main" for the main database or "temp" for TEMP tables or |
| 8447 | 8481 | ** the name given after the AS keyword in the [ATTACH] statement for attached |
| 8448 | 8482 | ** databases.)^ |
| 8449 | 8483 | ** ^The fifth parameter to the preupdate callback is the name of the |
| 8450 | 8484 | ** table that is being modified. |
| 8451 | | -** ^The sixth parameter to the preupdate callback is the initial [rowid] of the |
| 8452 | | -** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is |
| 8453 | | -** undefined for SQLITE_INSERT changes. |
| 8454 | | -** ^The seventh parameter to the preupdate callback is the final [rowid] of |
| 8455 | | -** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is |
| 8456 | | -** undefined for SQLITE_DELETE changes. |
| 8485 | +** |
| 8486 | +** For an UPDATE or DELETE operation on a [rowid table], the sixth |
| 8487 | +** parameter passed to the preupdate callback is the initial [rowid] of the |
| 8488 | +** row being modified or deleted. For an INSERT operation on a rowid table, |
| 8489 | +** or any operation on a WITHOUT ROWID table, the value of the sixth |
| 8490 | +** parameter is undefined. For an INSERT or UPDATE on a rowid table the |
| 8491 | +** seventh parameter is the final rowid value of the row being inserted |
| 8492 | +** or updated. The value of the seventh parameter passed to the callback |
| 8493 | +** function is not defined for operations on WITHOUT ROWID tables, or for |
| 8494 | +** INSERT operations on rowid tables. |
| 8457 | 8495 | ** |
| 8458 | 8496 | ** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()], |
| 8459 | 8497 | ** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces |
| 8460 | 8498 | ** provide additional information about a preupdate event. These routines |
| 8461 | 8499 | ** may only be called from within a preupdate callback. Invoking any of |
| | @@ -8887,11 +8925,11 @@ |
| 8887 | 8925 | ** The session object will be used to create changesets for tables in |
| 8888 | 8926 | ** database zDb, where zDb is either "main", or "temp", or the name of an |
| 8889 | 8927 | ** attached database. It is not an error if database zDb is not attached |
| 8890 | 8928 | ** to the database when the session object is created. |
| 8891 | 8929 | */ |
| 8892 | | -int sqlite3session_create( |
| 8930 | +SQLITE_API int sqlite3session_create( |
| 8893 | 8931 | sqlite3 *db, /* Database handle */ |
| 8894 | 8932 | const char *zDb, /* Name of db (e.g. "main") */ |
| 8895 | 8933 | sqlite3_session **ppSession /* OUT: New session object */ |
| 8896 | 8934 | ); |
| 8897 | 8935 | |
| | @@ -8905,11 +8943,11 @@ |
| 8905 | 8943 | ** |
| 8906 | 8944 | ** Session objects must be deleted before the database handle to which they |
| 8907 | 8945 | ** are attached is closed. Refer to the documentation for |
| 8908 | 8946 | ** [sqlite3session_create()] for details. |
| 8909 | 8947 | */ |
| 8910 | | -void sqlite3session_delete(sqlite3_session *pSession); |
| 8948 | +SQLITE_API void sqlite3session_delete(sqlite3_session *pSession); |
| 8911 | 8949 | |
| 8912 | 8950 | |
| 8913 | 8951 | /* |
| 8914 | 8952 | ** CAPI3REF: Enable Or Disable A Session Object |
| 8915 | 8953 | ** |
| | @@ -8925,11 +8963,11 @@ |
| 8925 | 8963 | ** no-op, and may be used to query the current state of the session. |
| 8926 | 8964 | ** |
| 8927 | 8965 | ** The return value indicates the final state of the session object: 0 if |
| 8928 | 8966 | ** the session is disabled, or 1 if it is enabled. |
| 8929 | 8967 | */ |
| 8930 | | -int sqlite3session_enable(sqlite3_session *pSession, int bEnable); |
| 8968 | +SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable); |
| 8931 | 8969 | |
| 8932 | 8970 | /* |
| 8933 | 8971 | ** CAPI3REF: Set Or Clear the Indirect Change Flag |
| 8934 | 8972 | ** |
| 8935 | 8973 | ** Each change recorded by a session object is marked as either direct or |
| | @@ -8954,11 +8992,11 @@ |
| 8954 | 8992 | ** indirect flag for the specified session object. |
| 8955 | 8993 | ** |
| 8956 | 8994 | ** The return value indicates the final state of the indirect flag: 0 if |
| 8957 | 8995 | ** it is clear, or 1 if it is set. |
| 8958 | 8996 | */ |
| 8959 | | -int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect); |
| 8997 | +SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect); |
| 8960 | 8998 | |
| 8961 | 8999 | /* |
| 8962 | 9000 | ** CAPI3REF: Attach A Table To A Session Object |
| 8963 | 9001 | ** |
| 8964 | 9002 | ** If argument zTab is not NULL, then it is the name of a table to attach |
| | @@ -8984,11 +9022,11 @@ |
| 8984 | 9022 | ** in one or more of their PRIMARY KEY columns. |
| 8985 | 9023 | ** |
| 8986 | 9024 | ** SQLITE_OK is returned if the call completes without error. Or, if an error |
| 8987 | 9025 | ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned. |
| 8988 | 9026 | */ |
| 8989 | | -int sqlite3session_attach( |
| 9027 | +SQLITE_API int sqlite3session_attach( |
| 8990 | 9028 | sqlite3_session *pSession, /* Session object */ |
| 8991 | 9029 | const char *zTab /* Table name */ |
| 8992 | 9030 | ); |
| 8993 | 9031 | |
| 8994 | 9032 | /* |
| | @@ -8998,11 +9036,11 @@ |
| 8998 | 9036 | ** in tables that are not attached to the Session object, the filter is called |
| 8999 | 9037 | ** to determine whether changes to the table's rows should be tracked or not. |
| 9000 | 9038 | ** If xFilter returns 0, changes is not tracked. Note that once a table is |
| 9001 | 9039 | ** attached, xFilter will not be called again. |
| 9002 | 9040 | */ |
| 9003 | | -void sqlite3session_table_filter( |
| 9041 | +SQLITE_API void sqlite3session_table_filter( |
| 9004 | 9042 | sqlite3_session *pSession, /* Session object */ |
| 9005 | 9043 | int(*xFilter)( |
| 9006 | 9044 | void *pCtx, /* Copy of third arg to _filter_table() */ |
| 9007 | 9045 | const char *zTab /* Table name */ |
| 9008 | 9046 | ), |
| | @@ -9111,11 +9149,11 @@ |
| 9111 | 9149 | ** changeset, even though the delete took place while the session was disabled. |
| 9112 | 9150 | ** Or, if one field of a row is updated while a session is disabled, and |
| 9113 | 9151 | ** another field of the same row is updated while the session is enabled, the |
| 9114 | 9152 | ** resulting changeset will contain an UPDATE change that updates both fields. |
| 9115 | 9153 | */ |
| 9116 | | -int sqlite3session_changeset( |
| 9154 | +SQLITE_API int sqlite3session_changeset( |
| 9117 | 9155 | sqlite3_session *pSession, /* Session object */ |
| 9118 | 9156 | int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */ |
| 9119 | 9157 | void **ppChangeset /* OUT: Buffer containing changeset */ |
| 9120 | 9158 | ); |
| 9121 | 9159 | |
| | @@ -9155,11 +9193,12 @@ |
| 9155 | 9193 | ** |
| 9156 | 9194 | ** <li> For each row (primary key) that exists in the to-table but not in |
| 9157 | 9195 | ** the from-table, a DELETE record is added to the session object. |
| 9158 | 9196 | ** |
| 9159 | 9197 | ** <li> For each row (primary key) that exists in both tables, but features |
| 9160 | | -** different in each, an UPDATE record is added to the session. |
| 9198 | +** different non-PK values in each, an UPDATE record is added to the |
| 9199 | +** session. |
| 9161 | 9200 | ** </ul> |
| 9162 | 9201 | ** |
| 9163 | 9202 | ** To clarify, if this function is called and then a changeset constructed |
| 9164 | 9203 | ** using [sqlite3session_changeset()], then after applying that changeset to |
| 9165 | 9204 | ** database zFrom the contents of the two compatible tables would be |
| | @@ -9172,11 +9211,11 @@ |
| 9172 | 9211 | ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg |
| 9173 | 9212 | ** may be set to point to a buffer containing an English language error |
| 9174 | 9213 | ** message. It is the responsibility of the caller to free this buffer using |
| 9175 | 9214 | ** sqlite3_free(). |
| 9176 | 9215 | */ |
| 9177 | | -int sqlite3session_diff( |
| 9216 | +SQLITE_API int sqlite3session_diff( |
| 9178 | 9217 | sqlite3_session *pSession, |
| 9179 | 9218 | const char *zFromDb, |
| 9180 | 9219 | const char *zTbl, |
| 9181 | 9220 | char **pzErrMsg |
| 9182 | 9221 | ); |
| | @@ -9208,11 +9247,11 @@ |
| 9208 | 9247 | ** Changes within a patchset are ordered in the same way as for changesets |
| 9209 | 9248 | ** generated by the sqlite3session_changeset() function (i.e. all changes for |
| 9210 | 9249 | ** a single table are grouped together, tables appear in the order in which |
| 9211 | 9250 | ** they were attached to the session object). |
| 9212 | 9251 | */ |
| 9213 | | -int sqlite3session_patchset( |
| 9252 | +SQLITE_API int sqlite3session_patchset( |
| 9214 | 9253 | sqlite3_session *pSession, /* Session object */ |
| 9215 | 9254 | int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */ |
| 9216 | 9255 | void **ppPatchset /* OUT: Buffer containing changeset */ |
| 9217 | 9256 | ); |
| 9218 | 9257 | |
| | @@ -9229,11 +9268,11 @@ |
| 9229 | 9268 | ** an attached table is modified and then later on the original values |
| 9230 | 9269 | ** are restored. However, if this function returns non-zero, then it is |
| 9231 | 9270 | ** guaranteed that a call to sqlite3session_changeset() will return a |
| 9232 | 9271 | ** changeset containing zero changes. |
| 9233 | 9272 | */ |
| 9234 | | -int sqlite3session_isempty(sqlite3_session *pSession); |
| 9273 | +SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession); |
| 9235 | 9274 | |
| 9236 | 9275 | /* |
| 9237 | 9276 | ** CAPI3REF: Create An Iterator To Traverse A Changeset |
| 9238 | 9277 | ** |
| 9239 | 9278 | ** Create an iterator used to iterate through the contents of a changeset. |
| | @@ -9264,11 +9303,11 @@ |
| 9264 | 9303 | ** this function, all changes that relate to a single table are visited |
| 9265 | 9304 | ** consecutively. There is no chance that the iterator will visit a change |
| 9266 | 9305 | ** the applies to table X, then one for table Y, and then later on visit |
| 9267 | 9306 | ** another change for table X. |
| 9268 | 9307 | */ |
| 9269 | | -int sqlite3changeset_start( |
| 9308 | +SQLITE_API int sqlite3changeset_start( |
| 9270 | 9309 | sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ |
| 9271 | 9310 | int nChangeset, /* Size of changeset blob in bytes */ |
| 9272 | 9311 | void *pChangeset /* Pointer to blob containing changeset */ |
| 9273 | 9312 | ); |
| 9274 | 9313 | |
| | @@ -9293,11 +9332,11 @@ |
| 9293 | 9332 | ** |
| 9294 | 9333 | ** If an error occurs, an SQLite error code is returned. Possible error |
| 9295 | 9334 | ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or |
| 9296 | 9335 | ** SQLITE_NOMEM. |
| 9297 | 9336 | */ |
| 9298 | | -int sqlite3changeset_next(sqlite3_changeset_iter *pIter); |
| 9337 | +SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter); |
| 9299 | 9338 | |
| 9300 | 9339 | /* |
| 9301 | 9340 | ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator |
| 9302 | 9341 | ** |
| 9303 | 9342 | ** The pIter argument passed to this function may either be an iterator |
| | @@ -9321,11 +9360,11 @@ |
| 9321 | 9360 | ** |
| 9322 | 9361 | ** If no error occurs, SQLITE_OK is returned. If an error does occur, an |
| 9323 | 9362 | ** SQLite error code is returned. The values of the output variables may not |
| 9324 | 9363 | ** be trusted in this case. |
| 9325 | 9364 | */ |
| 9326 | | -int sqlite3changeset_op( |
| 9365 | +SQLITE_API int sqlite3changeset_op( |
| 9327 | 9366 | sqlite3_changeset_iter *pIter, /* Iterator object */ |
| 9328 | 9367 | const char **pzTab, /* OUT: Pointer to table name */ |
| 9329 | 9368 | int *pnCol, /* OUT: Number of columns in table */ |
| 9330 | 9369 | int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */ |
| 9331 | 9370 | int *pbIndirect /* OUT: True for an 'indirect' change */ |
| | @@ -9354,11 +9393,11 @@ |
| 9354 | 9393 | ** If this function is called when the iterator does not point to a valid |
| 9355 | 9394 | ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise, |
| 9356 | 9395 | ** SQLITE_OK is returned and the output variables populated as described |
| 9357 | 9396 | ** above. |
| 9358 | 9397 | */ |
| 9359 | | -int sqlite3changeset_pk( |
| 9398 | +SQLITE_API int sqlite3changeset_pk( |
| 9360 | 9399 | sqlite3_changeset_iter *pIter, /* Iterator object */ |
| 9361 | 9400 | unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */ |
| 9362 | 9401 | int *pnCol /* OUT: Number of entries in output array */ |
| 9363 | 9402 | ); |
| 9364 | 9403 | |
| | @@ -9384,11 +9423,11 @@ |
| 9384 | 9423 | ** is similar to the "old.*" columns available to update or delete triggers. |
| 9385 | 9424 | ** |
| 9386 | 9425 | ** If some other error occurs (e.g. an OOM condition), an SQLite error code |
| 9387 | 9426 | ** is returned and *ppValue is set to NULL. |
| 9388 | 9427 | */ |
| 9389 | | -int sqlite3changeset_old( |
| 9428 | +SQLITE_API int sqlite3changeset_old( |
| 9390 | 9429 | sqlite3_changeset_iter *pIter, /* Changeset iterator */ |
| 9391 | 9430 | int iVal, /* Column number */ |
| 9392 | 9431 | sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */ |
| 9393 | 9432 | ); |
| 9394 | 9433 | |
| | @@ -9417,11 +9456,11 @@ |
| 9417 | 9456 | ** triggers. |
| 9418 | 9457 | ** |
| 9419 | 9458 | ** If some other error occurs (e.g. an OOM condition), an SQLite error code |
| 9420 | 9459 | ** is returned and *ppValue is set to NULL. |
| 9421 | 9460 | */ |
| 9422 | | -int sqlite3changeset_new( |
| 9461 | +SQLITE_API int sqlite3changeset_new( |
| 9423 | 9462 | sqlite3_changeset_iter *pIter, /* Changeset iterator */ |
| 9424 | 9463 | int iVal, /* Column number */ |
| 9425 | 9464 | sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */ |
| 9426 | 9465 | ); |
| 9427 | 9466 | |
| | @@ -9444,11 +9483,11 @@ |
| 9444 | 9483 | ** and returns SQLITE_OK. |
| 9445 | 9484 | ** |
| 9446 | 9485 | ** If some other error occurs (e.g. an OOM condition), an SQLite error code |
| 9447 | 9486 | ** is returned and *ppValue is set to NULL. |
| 9448 | 9487 | */ |
| 9449 | | -int sqlite3changeset_conflict( |
| 9488 | +SQLITE_API int sqlite3changeset_conflict( |
| 9450 | 9489 | sqlite3_changeset_iter *pIter, /* Changeset iterator */ |
| 9451 | 9490 | int iVal, /* Column number */ |
| 9452 | 9491 | sqlite3_value **ppValue /* OUT: Value from conflicting row */ |
| 9453 | 9492 | ); |
| 9454 | 9493 | |
| | @@ -9460,11 +9499,11 @@ |
| 9460 | 9499 | ** it sets the output variable to the total number of known foreign key |
| 9461 | 9500 | ** violations in the destination database and returns SQLITE_OK. |
| 9462 | 9501 | ** |
| 9463 | 9502 | ** In all other cases this function returns SQLITE_MISUSE. |
| 9464 | 9503 | */ |
| 9465 | | -int sqlite3changeset_fk_conflicts( |
| 9504 | +SQLITE_API int sqlite3changeset_fk_conflicts( |
| 9466 | 9505 | sqlite3_changeset_iter *pIter, /* Changeset iterator */ |
| 9467 | 9506 | int *pnOut /* OUT: Number of FK violations */ |
| 9468 | 9507 | ); |
| 9469 | 9508 | |
| 9470 | 9509 | |
| | @@ -9493,11 +9532,11 @@ |
| 9493 | 9532 | ** rc = sqlite3changeset_finalize(); |
| 9494 | 9533 | ** if( rc!=SQLITE_OK ){ |
| 9495 | 9534 | ** // An error has occurred |
| 9496 | 9535 | ** } |
| 9497 | 9536 | */ |
| 9498 | | -int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter); |
| 9537 | +SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter); |
| 9499 | 9538 | |
| 9500 | 9539 | /* |
| 9501 | 9540 | ** CAPI3REF: Invert A Changeset |
| 9502 | 9541 | ** |
| 9503 | 9542 | ** This function is used to "invert" a changeset object. Applying an inverted |
| | @@ -9523,11 +9562,11 @@ |
| 9523 | 9562 | ** call to this function. |
| 9524 | 9563 | ** |
| 9525 | 9564 | ** WARNING/TODO: This function currently assumes that the input is a valid |
| 9526 | 9565 | ** changeset. If it is not, the results are undefined. |
| 9527 | 9566 | */ |
| 9528 | | -int sqlite3changeset_invert( |
| 9567 | +SQLITE_API int sqlite3changeset_invert( |
| 9529 | 9568 | int nIn, const void *pIn, /* Input changeset */ |
| 9530 | 9569 | int *pnOut, void **ppOut /* OUT: Inverse of input */ |
| 9531 | 9570 | ); |
| 9532 | 9571 | |
| 9533 | 9572 | /* |
| | @@ -9552,11 +9591,11 @@ |
| 9552 | 9591 | ** *pnOut = 0; |
| 9553 | 9592 | ** } |
| 9554 | 9593 | ** |
| 9555 | 9594 | ** Refer to the sqlite3_changegroup documentation below for details. |
| 9556 | 9595 | */ |
| 9557 | | -int sqlite3changeset_concat( |
| 9596 | +SQLITE_API int sqlite3changeset_concat( |
| 9558 | 9597 | int nA, /* Number of bytes in buffer pA */ |
| 9559 | 9598 | void *pA, /* Pointer to buffer containing changeset A */ |
| 9560 | 9599 | int nB, /* Number of bytes in buffer pB */ |
| 9561 | 9600 | void *pB, /* Pointer to buffer containing changeset B */ |
| 9562 | 9601 | int *pnOut, /* OUT: Number of bytes in output changeset */ |
| | @@ -9740,11 +9779,11 @@ |
| 9740 | 9779 | ** considered compatible if all of the following are true: |
| 9741 | 9780 | ** |
| 9742 | 9781 | ** <ul> |
| 9743 | 9782 | ** <li> The table has the same name as the name recorded in the |
| 9744 | 9783 | ** changeset, and |
| 9745 | | -** <li> The table has the same number of columns as recorded in the |
| 9784 | +** <li> The table has at least as many columns as recorded in the |
| 9746 | 9785 | ** changeset, and |
| 9747 | 9786 | ** <li> The table has primary key columns in the same position as |
| 9748 | 9787 | ** recorded in the changeset. |
| 9749 | 9788 | ** </ul> |
| 9750 | 9789 | ** |
| | @@ -9785,11 +9824,15 @@ |
| 9785 | 9824 | ** the changeset the row is deleted from the target database. |
| 9786 | 9825 | ** |
| 9787 | 9826 | ** If a row with matching primary key values is found, but one or more of |
| 9788 | 9827 | ** the non-primary key fields contains a value different from the original |
| 9789 | 9828 | ** row value stored in the changeset, the conflict-handler function is |
| 9790 | | -** invoked with [SQLITE_CHANGESET_DATA] as the second argument. |
| 9829 | +** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the |
| 9830 | +** database table has more columns than are recorded in the changeset, |
| 9831 | +** only the values of those non-primary key fields are compared against |
| 9832 | +** the current database contents - any trailing database table columns |
| 9833 | +** are ignored. |
| 9791 | 9834 | ** |
| 9792 | 9835 | ** If no row with matching primary key values is found in the database, |
| 9793 | 9836 | ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] |
| 9794 | 9837 | ** passed as the second argument. |
| 9795 | 9838 | ** |
| | @@ -9800,11 +9843,13 @@ |
| 9800 | 9843 | ** operation is attempted because an earlier call to the conflict handler |
| 9801 | 9844 | ** function returned [SQLITE_CHANGESET_REPLACE]. |
| 9802 | 9845 | ** |
| 9803 | 9846 | ** <dt>INSERT Changes<dd> |
| 9804 | 9847 | ** For each INSERT change, an attempt is made to insert the new row into |
| 9805 | | -** the database. |
| 9848 | +** the database. If the changeset row contains fewer fields than the |
| 9849 | +** database table, the trailing fields are populated with their default |
| 9850 | +** values. |
| 9806 | 9851 | ** |
| 9807 | 9852 | ** If the attempt to insert the row fails because the database already |
| 9808 | 9853 | ** contains a row with the same primary key values, the conflict handler |
| 9809 | 9854 | ** function is invoked with the second argument set to |
| 9810 | 9855 | ** [SQLITE_CHANGESET_CONFLICT]. |
| | @@ -9818,17 +9863,17 @@ |
| 9818 | 9863 | ** |
| 9819 | 9864 | ** <dt>UPDATE Changes<dd> |
| 9820 | 9865 | ** For each UPDATE change, this function checks if the target database |
| 9821 | 9866 | ** contains a row with the same primary key value (or values) as the |
| 9822 | 9867 | ** original row values stored in the changeset. If it does, and the values |
| 9823 | | -** stored in all non-primary key columns also match the values stored in |
| 9824 | | -** the changeset the row is updated within the target database. |
| 9868 | +** stored in all modified non-primary key columns also match the values |
| 9869 | +** stored in the changeset the row is updated within the target database. |
| 9825 | 9870 | ** |
| 9826 | 9871 | ** If a row with matching primary key values is found, but one or more of |
| 9827 | | -** the non-primary key fields contains a value different from an original |
| 9828 | | -** row value stored in the changeset, the conflict-handler function is |
| 9829 | | -** invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since |
| 9872 | +** the modified non-primary key fields contains a value different from an |
| 9873 | +** original row value stored in the changeset, the conflict-handler function |
| 9874 | +** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since |
| 9830 | 9875 | ** UPDATE changes only contain values for non-primary key fields that are |
| 9831 | 9876 | ** to be modified, only those fields need to match the original values to |
| 9832 | 9877 | ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback. |
| 9833 | 9878 | ** |
| 9834 | 9879 | ** If no row with matching primary key values is found in the database, |
| | @@ -9852,11 +9897,11 @@ |
| 9852 | 9897 | ** If any other error (aside from a constraint failure when attempting to |
| 9853 | 9898 | ** write to the target database) occurs, then the savepoint transaction is |
| 9854 | 9899 | ** rolled back, restoring the target database to its original state, and an |
| 9855 | 9900 | ** SQLite error code returned. |
| 9856 | 9901 | */ |
| 9857 | | -int sqlite3changeset_apply( |
| 9902 | +SQLITE_API int sqlite3changeset_apply( |
| 9858 | 9903 | sqlite3 *db, /* Apply change to "main" db of this handle */ |
| 9859 | 9904 | int nChangeset, /* Size of changeset in bytes */ |
| 9860 | 9905 | void *pChangeset, /* Changeset blob */ |
| 9861 | 9906 | int(*xFilter)( |
| 9862 | 9907 | void *pCtx, /* Copy of sixth arg to _apply() */ |
| | @@ -10053,11 +10098,11 @@ |
| 10053 | 10098 | ** |
| 10054 | 10099 | ** The sessions module never invokes an xOutput callback with the third |
| 10055 | 10100 | ** parameter set to a value less than or equal to zero. Other than this, |
| 10056 | 10101 | ** no guarantees are made as to the size of the chunks of data returned. |
| 10057 | 10102 | */ |
| 10058 | | -int sqlite3changeset_apply_strm( |
| 10103 | +SQLITE_API int sqlite3changeset_apply_strm( |
| 10059 | 10104 | sqlite3 *db, /* Apply change to "main" db of this handle */ |
| 10060 | 10105 | int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ |
| 10061 | 10106 | void *pIn, /* First arg for xInput */ |
| 10062 | 10107 | int(*xFilter)( |
| 10063 | 10108 | void *pCtx, /* Copy of sixth arg to _apply() */ |
| | @@ -10068,35 +10113,35 @@ |
| 10068 | 10113 | int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ |
| 10069 | 10114 | sqlite3_changeset_iter *p /* Handle describing change and conflict */ |
| 10070 | 10115 | ), |
| 10071 | 10116 | void *pCtx /* First argument passed to xConflict */ |
| 10072 | 10117 | ); |
| 10073 | | -int sqlite3changeset_concat_strm( |
| 10118 | +SQLITE_API int sqlite3changeset_concat_strm( |
| 10074 | 10119 | int (*xInputA)(void *pIn, void *pData, int *pnData), |
| 10075 | 10120 | void *pInA, |
| 10076 | 10121 | int (*xInputB)(void *pIn, void *pData, int *pnData), |
| 10077 | 10122 | void *pInB, |
| 10078 | 10123 | int (*xOutput)(void *pOut, const void *pData, int nData), |
| 10079 | 10124 | void *pOut |
| 10080 | 10125 | ); |
| 10081 | | -int sqlite3changeset_invert_strm( |
| 10126 | +SQLITE_API int sqlite3changeset_invert_strm( |
| 10082 | 10127 | int (*xInput)(void *pIn, void *pData, int *pnData), |
| 10083 | 10128 | void *pIn, |
| 10084 | 10129 | int (*xOutput)(void *pOut, const void *pData, int nData), |
| 10085 | 10130 | void *pOut |
| 10086 | 10131 | ); |
| 10087 | | -int sqlite3changeset_start_strm( |
| 10132 | +SQLITE_API int sqlite3changeset_start_strm( |
| 10088 | 10133 | sqlite3_changeset_iter **pp, |
| 10089 | 10134 | int (*xInput)(void *pIn, void *pData, int *pnData), |
| 10090 | 10135 | void *pIn |
| 10091 | 10136 | ); |
| 10092 | | -int sqlite3session_changeset_strm( |
| 10137 | +SQLITE_API int sqlite3session_changeset_strm( |
| 10093 | 10138 | sqlite3_session *pSession, |
| 10094 | 10139 | int (*xOutput)(void *pOut, const void *pData, int nData), |
| 10095 | 10140 | void *pOut |
| 10096 | 10141 | ); |
| 10097 | | -int sqlite3session_patchset_strm( |
| 10142 | +SQLITE_API int sqlite3session_patchset_strm( |
| 10098 | 10143 | sqlite3_session *pSession, |
| 10099 | 10144 | int (*xOutput)(void *pOut, const void *pData, int nData), |
| 10100 | 10145 | void *pOut |
| 10101 | 10146 | ); |
| 10102 | 10147 | int sqlite3changegroup_add_strm(sqlite3_changegroup*, |
| | @@ -10999,10 +11044,11 @@ |
| 10999 | 11044 | # if defined(_MSC_VER) && _MSC_VER>=1400 |
| 11000 | 11045 | # if !defined(_WIN32_WCE) |
| 11001 | 11046 | # include <intrin.h> |
| 11002 | 11047 | # pragma intrinsic(_byteswap_ushort) |
| 11003 | 11048 | # pragma intrinsic(_byteswap_ulong) |
| 11049 | +# pragma intrinsic(_byteswap_uint64) |
| 11004 | 11050 | # pragma intrinsic(_ReadWriteBarrier) |
| 11005 | 11051 | # else |
| 11006 | 11052 | # include <cmnintrin.h> |
| 11007 | 11053 | # endif |
| 11008 | 11054 | # endif |
| | @@ -11537,10 +11583,22 @@ |
| 11537 | 11583 | #include <stdlib.h> |
| 11538 | 11584 | #include <string.h> |
| 11539 | 11585 | #include <assert.h> |
| 11540 | 11586 | #include <stddef.h> |
| 11541 | 11587 | |
| 11588 | +/* |
| 11589 | +** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY. |
| 11590 | +** This allows better measurements of where memcpy() is used when running |
| 11591 | +** cachegrind. But this macro version of memcpy() is very slow so it |
| 11592 | +** should not be used in production. This is a performance measurement |
| 11593 | +** hack only. |
| 11594 | +*/ |
| 11595 | +#ifdef SQLITE_INLINE_MEMCPY |
| 11596 | +# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\ |
| 11597 | + int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);} |
| 11598 | +#endif |
| 11599 | + |
| 11542 | 11600 | /* |
| 11543 | 11601 | ** If compiling for a processor that lacks floating point support, |
| 11544 | 11602 | ** substitute integer for floating-point |
| 11545 | 11603 | */ |
| 11546 | 11604 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| | @@ -11621,13 +11679,16 @@ |
| 11621 | 11679 | /* |
| 11622 | 11680 | ** The default initial allocation for the pagecache when using separate |
| 11623 | 11681 | ** pagecaches for each database connection. A positive number is the |
| 11624 | 11682 | ** number of pages. A negative number N translations means that a buffer |
| 11625 | 11683 | ** of -1024*N bytes is allocated and used for as many pages as it will hold. |
| 11684 | +** |
| 11685 | +** The default value of "20" was choosen to minimize the run-time of the |
| 11686 | +** speedtest1 test program with options: --shrink-memory --reprepare |
| 11626 | 11687 | */ |
| 11627 | 11688 | #ifndef SQLITE_DEFAULT_PCACHE_INITSZ |
| 11628 | | -# define SQLITE_DEFAULT_PCACHE_INITSZ 100 |
| 11689 | +# define SQLITE_DEFAULT_PCACHE_INITSZ 20 |
| 11629 | 11690 | #endif |
| 11630 | 11691 | |
| 11631 | 11692 | /* |
| 11632 | 11693 | ** GCC does not define the offsetof() macro so we'll have to do it |
| 11633 | 11694 | ** ourselves. |
| | @@ -11798,36 +11859,39 @@ |
| 11798 | 11859 | ** Macros to determine whether the machine is big or little endian, |
| 11799 | 11860 | ** and whether or not that determination is run-time or compile-time. |
| 11800 | 11861 | ** |
| 11801 | 11862 | ** For best performance, an attempt is made to guess at the byte-order |
| 11802 | 11863 | ** using C-preprocessor macros. If that is unsuccessful, or if |
| 11803 | | -** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined |
| 11864 | +** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined |
| 11804 | 11865 | ** at run-time. |
| 11805 | 11866 | */ |
| 11806 | | -#if (defined(i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 11867 | +#ifndef SQLITE_BYTEORDER |
| 11868 | +# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 11807 | 11869 | defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ |
| 11808 | 11870 | defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ |
| 11809 | | - defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER) |
| 11810 | | -# define SQLITE_BYTEORDER 1234 |
| 11811 | | -# define SQLITE_BIGENDIAN 0 |
| 11812 | | -# define SQLITE_LITTLEENDIAN 1 |
| 11813 | | -# define SQLITE_UTF16NATIVE SQLITE_UTF16LE |
| 11871 | + defined(__arm__) |
| 11872 | +# define SQLITE_BYTEORDER 1234 |
| 11873 | +# elif defined(sparc) || defined(__ppc__) |
| 11874 | +# define SQLITE_BYTEORDER 4321 |
| 11875 | +# else |
| 11876 | +# define SQLITE_BYTEORDER 0 |
| 11877 | +# endif |
| 11814 | 11878 | #endif |
| 11815 | | -#if (defined(sparc) || defined(__ppc__)) \ |
| 11816 | | - && !defined(SQLITE_RUNTIME_BYTEORDER) |
| 11817 | | -# define SQLITE_BYTEORDER 4321 |
| 11879 | +#if SQLITE_BYTEORDER==4321 |
| 11818 | 11880 | # define SQLITE_BIGENDIAN 1 |
| 11819 | 11881 | # define SQLITE_LITTLEENDIAN 0 |
| 11820 | 11882 | # define SQLITE_UTF16NATIVE SQLITE_UTF16BE |
| 11821 | | -#endif |
| 11822 | | -#if !defined(SQLITE_BYTEORDER) |
| 11883 | +#elif SQLITE_BYTEORDER==1234 |
| 11884 | +# define SQLITE_BIGENDIAN 0 |
| 11885 | +# define SQLITE_LITTLEENDIAN 1 |
| 11886 | +# define SQLITE_UTF16NATIVE SQLITE_UTF16LE |
| 11887 | +#else |
| 11823 | 11888 | # ifdef SQLITE_AMALGAMATION |
| 11824 | 11889 | const int sqlite3one = 1; |
| 11825 | 11890 | # else |
| 11826 | 11891 | extern const int sqlite3one; |
| 11827 | 11892 | # endif |
| 11828 | | -# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */ |
| 11829 | 11893 | # define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0) |
| 11830 | 11894 | # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1) |
| 11831 | 11895 | # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE) |
| 11832 | 11896 | #endif |
| 11833 | 11897 | |
| | @@ -12346,13 +12410,14 @@ |
| 12346 | 12410 | ); |
| 12347 | 12411 | SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*); |
| 12348 | 12412 | SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*); |
| 12349 | 12413 | SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags); |
| 12350 | 12414 | |
| 12351 | | -/* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */ |
| 12415 | +/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */ |
| 12352 | 12416 | #define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */ |
| 12353 | 12417 | #define BTREE_AUXDELETE 0x04 /* not the primary delete operation */ |
| 12418 | +#define BTREE_APPEND 0x08 /* Insert is likely an append */ |
| 12354 | 12419 | |
| 12355 | 12420 | /* An instance of the BtreePayload object describes the content of a single |
| 12356 | 12421 | ** entry in either an index or table btree. |
| 12357 | 12422 | ** |
| 12358 | 12423 | ** Index btrees (used for indexes and also WITHOUT ROWID tables) contain |
| | @@ -12379,11 +12444,11 @@ |
| 12379 | 12444 | int nData; /* Size of pData. 0 if none. */ |
| 12380 | 12445 | int nZero; /* Extra zero data appended after pData,nData */ |
| 12381 | 12446 | }; |
| 12382 | 12447 | |
| 12383 | 12448 | SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload, |
| 12384 | | - int bias, int seekResult); |
| 12449 | + int flags, int seekResult); |
| 12385 | 12450 | SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes); |
| 12386 | 12451 | SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes); |
| 12387 | 12452 | SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes); |
| 12388 | 12453 | SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*); |
| 12389 | 12454 | SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes); |
| | @@ -12512,12 +12577,11 @@ |
| 12512 | 12577 | ** as an instance of the following structure: |
| 12513 | 12578 | */ |
| 12514 | 12579 | struct VdbeOp { |
| 12515 | 12580 | u8 opcode; /* What operation to perform */ |
| 12516 | 12581 | signed char p4type; /* One of the P4_xxx constants for p4 */ |
| 12517 | | - u8 notUsed1; |
| 12518 | | - u8 p5; /* Fifth parameter is an unsigned character */ |
| 12582 | + u16 p5; /* Fifth parameter is an unsigned 16-bit integer */ |
| 12519 | 12583 | int p1; /* First operand */ |
| 12520 | 12584 | int p2; /* Second parameter (often the jump destination) */ |
| 12521 | 12585 | int p3; /* The third parameter */ |
| 12522 | 12586 | union p4union { /* fourth parameter */ |
| 12523 | 12587 | int i; /* Integer value if p4type==P4_INT32 */ |
| | @@ -12874,11 +12938,11 @@ |
| 12874 | 12938 | SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*); |
| 12875 | 12939 | SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8); |
| 12876 | 12940 | SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1); |
| 12877 | 12941 | SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2); |
| 12878 | 12942 | SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3); |
| 12879 | | -SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5); |
| 12943 | +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5); |
| 12880 | 12944 | SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr); |
| 12881 | 12945 | SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr); |
| 12882 | 12946 | SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op); |
| 12883 | 12947 | SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N); |
| 12884 | 12948 | SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type); |
| | @@ -13176,18 +13240,20 @@ |
| 13176 | 13240 | SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*); |
| 13177 | 13241 | SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager); |
| 13178 | 13242 | SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager); |
| 13179 | 13243 | SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); |
| 13180 | 13244 | SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*); |
| 13181 | | -SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager); |
| 13245 | +# ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 13246 | +SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno); |
| 13247 | +# endif |
| 13182 | 13248 | # ifdef SQLITE_ENABLE_SNAPSHOT |
| 13183 | 13249 | SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot); |
| 13184 | 13250 | SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot); |
| 13185 | 13251 | SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager); |
| 13186 | 13252 | # endif |
| 13187 | 13253 | #else |
| 13188 | | -# define sqlite3PagerUseWal(x) 0 |
| 13254 | +# define sqlite3PagerUseWal(x,y) 0 |
| 13189 | 13255 | #endif |
| 13190 | 13256 | |
| 13191 | 13257 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 13192 | 13258 | SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager); |
| 13193 | 13259 | #endif |
| | @@ -14007,10 +14073,11 @@ |
| 14007 | 14073 | signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */ |
| 14008 | 14074 | u8 suppressErr; /* Do not issue error messages if true */ |
| 14009 | 14075 | u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */ |
| 14010 | 14076 | u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */ |
| 14011 | 14077 | u8 mTrace; /* zero or more SQLITE_TRACE flags */ |
| 14078 | + u8 skipBtreeMutex; /* True if no shared-cache backends */ |
| 14012 | 14079 | int nextPagesize; /* Pagesize after VACUUM if >0 */ |
| 14013 | 14080 | u32 magic; /* Magic number for detect library misuse */ |
| 14014 | 14081 | int nChange; /* Value returned by sqlite3_changes() */ |
| 14015 | 14082 | int nTotalChange; /* Value returned by sqlite3_total_changes() */ |
| 14016 | 14083 | int aLimit[SQLITE_N_LIMIT]; /* Limits */ |
| | @@ -14272,10 +14339,11 @@ |
| 14272 | 14339 | #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */ |
| 14273 | 14340 | #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */ |
| 14274 | 14341 | #define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */ |
| 14275 | 14342 | #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a |
| 14276 | 14343 | ** single query - might change over time */ |
| 14344 | +#define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */ |
| 14277 | 14345 | |
| 14278 | 14346 | /* |
| 14279 | 14347 | ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are |
| 14280 | 14348 | ** used to create the initializers for the FuncDef structures. |
| 14281 | 14349 | ** |
| | @@ -15278,11 +15346,11 @@ |
| 15278 | 15346 | #define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */ |
| 15279 | 15347 | #define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */ |
| 15280 | 15348 | #define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */ |
| 15281 | 15349 | #define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */ |
| 15282 | 15350 | #define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */ |
| 15283 | | - /* 0x1000 not currently used */ |
| 15351 | +#define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */ |
| 15284 | 15352 | /* 0x2000 not currently used */ |
| 15285 | 15353 | #define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */ |
| 15286 | 15354 | /* 0x8000 not currently used */ |
| 15287 | 15355 | |
| 15288 | 15356 | /* Allowed return values from sqlite3WhereIsDistinct() |
| | @@ -15739,25 +15807,23 @@ |
| 15739 | 15807 | ** OPFLAG_AUXDELETE == BTREE_AUXDELETE |
| 15740 | 15808 | */ |
| 15741 | 15809 | #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */ |
| 15742 | 15810 | /* Also used in P2 (not P5) of OP_Delete */ |
| 15743 | 15811 | #define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */ |
| 15744 | | -#define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */ |
| 15812 | +#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */ |
| 15745 | 15813 | #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */ |
| 15746 | 15814 | #define OPFLAG_APPEND 0x08 /* This is likely to be an append */ |
| 15747 | 15815 | #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */ |
| 15748 | | -#ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 15749 | 15816 | #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */ |
| 15750 | | -#endif |
| 15751 | 15817 | #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */ |
| 15752 | 15818 | #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */ |
| 15753 | 15819 | #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */ |
| 15754 | 15820 | #define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */ |
| 15755 | 15821 | #define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */ |
| 15756 | 15822 | #define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */ |
| 15757 | 15823 | #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */ |
| 15758 | | -#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete: keep cursor position */ |
| 15824 | +#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */ |
| 15759 | 15825 | #define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */ |
| 15760 | 15826 | |
| 15761 | 15827 | /* |
| 15762 | 15828 | * Each trigger present in the database schema is stored as an instance of |
| 15763 | 15829 | * struct Trigger. |
| | @@ -16414,11 +16480,11 @@ |
| 16414 | 16480 | SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*); |
| 16415 | 16481 | SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int); |
| 16416 | 16482 | SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int); |
| 16417 | 16483 | SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int); |
| 16418 | 16484 | SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int); |
| 16419 | | -SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8); |
| 16485 | +SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int); |
| 16420 | 16486 | SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*); |
| 16421 | 16487 | SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int); |
| 16422 | 16488 | SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int); |
| 16423 | 16489 | SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8); |
| 16424 | 16490 | #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */ |
| | @@ -16476,10 +16542,15 @@ |
| 16476 | 16542 | SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int); |
| 16477 | 16543 | SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int); |
| 16478 | 16544 | SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int); |
| 16479 | 16545 | SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int, |
| 16480 | 16546 | u8,u8,int,int*,int*); |
| 16547 | +#ifdef SQLITE_ENABLE_NULL_TRIM |
| 16548 | +SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*); |
| 16549 | +#else |
| 16550 | +# define sqlite3SetMakeRecordP5(A,B) |
| 16551 | +#endif |
| 16481 | 16552 | SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int); |
| 16482 | 16553 | SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*); |
| 16483 | 16554 | SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int); |
| 16484 | 16555 | SQLITE_PRIVATE void sqlite3MultiWrite(Parse*); |
| 16485 | 16556 | SQLITE_PRIVATE void sqlite3MayAbort(Parse*); |
| | @@ -16754,12 +16825,14 @@ |
| 16754 | 16825 | #endif |
| 16755 | 16826 | |
| 16756 | 16827 | /* |
| 16757 | 16828 | ** The interface to the LEMON-generated parser |
| 16758 | 16829 | */ |
| 16759 | | -SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64)); |
| 16760 | | -SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*)); |
| 16830 | +#ifndef SQLITE_AMALGAMATION |
| 16831 | +SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64)); |
| 16832 | +SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*)); |
| 16833 | +#endif |
| 16761 | 16834 | SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*); |
| 16762 | 16835 | #ifdef YYTRACKMAXSTACKDEPTH |
| 16763 | 16836 | SQLITE_PRIVATE int sqlite3ParserStackPeak(void*); |
| 16764 | 16837 | #endif |
| 16765 | 16838 | |
| | @@ -16865,10 +16938,11 @@ |
| 16865 | 16938 | #define sqlite3FkActions(a,b,c,d,e,f) |
| 16866 | 16939 | #define sqlite3FkCheck(a,b,c,d,e,f) |
| 16867 | 16940 | #define sqlite3FkDropTable(a,b,c) |
| 16868 | 16941 | #define sqlite3FkOldmask(a,b) 0 |
| 16869 | 16942 | #define sqlite3FkRequired(a,b,c,d) 0 |
| 16943 | + #define sqlite3FkReferences(a) 0 |
| 16870 | 16944 | #endif |
| 16871 | 16945 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 16872 | 16946 | SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*); |
| 16873 | 16947 | SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**); |
| 16874 | 16948 | #else |
| | @@ -17193,10 +17267,23 @@ |
| 17193 | 17267 | ** setting.) |
| 17194 | 17268 | */ |
| 17195 | 17269 | #ifndef SQLITE_STMTJRNL_SPILL |
| 17196 | 17270 | # define SQLITE_STMTJRNL_SPILL (64*1024) |
| 17197 | 17271 | #endif |
| 17272 | + |
| 17273 | +/* |
| 17274 | +** The default lookaside-configuration, the format "SZ,N". SZ is the |
| 17275 | +** number of bytes in each lookaside slot (should be a multiple of 8) |
| 17276 | +** and N is the number of slots. The lookaside-configuration can be |
| 17277 | +** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE) |
| 17278 | +** or at run-time for an individual database connection using |
| 17279 | +** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE); |
| 17280 | +*/ |
| 17281 | +#ifndef SQLITE_DEFAULT_LOOKASIDE |
| 17282 | +# define SQLITE_DEFAULT_LOOKASIDE 1200,100 |
| 17283 | +#endif |
| 17284 | + |
| 17198 | 17285 | |
| 17199 | 17286 | /* |
| 17200 | 17287 | ** The following singleton contains the global configuration for |
| 17201 | 17288 | ** the SQLite library. |
| 17202 | 17289 | */ |
| | @@ -17206,12 +17293,11 @@ |
| 17206 | 17293 | SQLITE_THREADSAFE==1, /* bFullMutex */ |
| 17207 | 17294 | SQLITE_USE_URI, /* bOpenUri */ |
| 17208 | 17295 | SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */ |
| 17209 | 17296 | 0x7ffffffe, /* mxStrlen */ |
| 17210 | 17297 | 0, /* neverCorrupt */ |
| 17211 | | - 512, /* szLookaside */ |
| 17212 | | - 125, /* nLookaside */ |
| 17298 | + SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */ |
| 17213 | 17299 | SQLITE_STMTJRNL_SPILL, /* nStmtSpill */ |
| 17214 | 17300 | {0,0,0,0,0,0,0,0}, /* m */ |
| 17215 | 17301 | {0,0,0,0,0,0,0,0,0}, /* mutex */ |
| 17216 | 17302 | {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */ |
| 17217 | 17303 | (void*)0, /* pHeap */ |
| | @@ -18219,10 +18305,11 @@ |
| 18219 | 18305 | int iNewReg; /* Register for new.* values */ |
| 18220 | 18306 | i64 iKey1; /* First key value passed to hook */ |
| 18221 | 18307 | i64 iKey2; /* Second key value passed to hook */ |
| 18222 | 18308 | Mem *aNew; /* Array of new.* values */ |
| 18223 | 18309 | Table *pTab; /* Schema object being upated */ |
| 18310 | + Index *pPk; /* PK index if pTab is WITHOUT ROWID */ |
| 18224 | 18311 | }; |
| 18225 | 18312 | |
| 18226 | 18313 | /* |
| 18227 | 18314 | ** Function prototypes |
| 18228 | 18315 | */ |
| | @@ -20619,20 +20706,22 @@ |
| 20619 | 20706 | ** cases of nByte<=0 will be intercepted and dealt with by higher level |
| 20620 | 20707 | ** routines. |
| 20621 | 20708 | */ |
| 20622 | 20709 | static void *sqlite3MemMalloc(int nByte){ |
| 20623 | 20710 | #ifdef SQLITE_MALLOCSIZE |
| 20624 | | - void *p = SQLITE_MALLOC( nByte ); |
| 20711 | + void *p; |
| 20712 | + testcase( ROUND8(nByte)==nByte ); |
| 20713 | + p = SQLITE_MALLOC( nByte ); |
| 20625 | 20714 | if( p==0 ){ |
| 20626 | 20715 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 20627 | 20716 | sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); |
| 20628 | 20717 | } |
| 20629 | 20718 | return p; |
| 20630 | 20719 | #else |
| 20631 | 20720 | sqlite3_int64 *p; |
| 20632 | 20721 | assert( nByte>0 ); |
| 20633 | | - nByte = ROUND8(nByte); |
| 20722 | + testcase( ROUND8(nByte)!=nByte ); |
| 20634 | 20723 | p = SQLITE_MALLOC( nByte+8 ); |
| 20635 | 20724 | if( p ){ |
| 20636 | 20725 | p[0] = nByte; |
| 20637 | 20726 | p++; |
| 20638 | 20727 | }else{ |
| | @@ -23750,12 +23839,11 @@ |
| 23750 | 23839 | SQLITE_PRIVATE void sqlite3MemoryBarrier(void){ |
| 23751 | 23840 | #if defined(SQLITE_MEMORY_BARRIER) |
| 23752 | 23841 | SQLITE_MEMORY_BARRIER; |
| 23753 | 23842 | #elif defined(__GNUC__) |
| 23754 | 23843 | __sync_synchronize(); |
| 23755 | | -#elif !defined(SQLITE_DISABLE_INTRINSIC) && \ |
| 23756 | | - defined(_MSC_VER) && _MSC_VER>=1300 |
| 23844 | +#elif MSVC_VERSION>=1300 |
| 23757 | 23845 | _ReadWriteBarrier(); |
| 23758 | 23846 | #elif defined(MemoryBarrier) |
| 23759 | 23847 | MemoryBarrier(); |
| 23760 | 23848 | #endif |
| 23761 | 23849 | } |
| | @@ -24283,15 +24371,23 @@ |
| 24283 | 24371 | |
| 24284 | 24372 | /* |
| 24285 | 24373 | ** Do a memory allocation with statistics and alarms. Assume the |
| 24286 | 24374 | ** lock is already held. |
| 24287 | 24375 | */ |
| 24288 | | -static int mallocWithAlarm(int n, void **pp){ |
| 24289 | | - int nFull; |
| 24376 | +static void mallocWithAlarm(int n, void **pp){ |
| 24290 | 24377 | void *p; |
| 24378 | + int nFull; |
| 24291 | 24379 | assert( sqlite3_mutex_held(mem0.mutex) ); |
| 24380 | + assert( n>0 ); |
| 24381 | + |
| 24382 | + /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal |
| 24383 | + ** implementation of malloc_good_size(), which must be called in debug |
| 24384 | + ** mode and specifically when the DMD "Dark Matter Detector" is enabled |
| 24385 | + ** or else a crash results. Hence, do not attempt to optimize out the |
| 24386 | + ** following xRoundup() call. */ |
| 24292 | 24387 | nFull = sqlite3GlobalConfig.m.xRoundup(n); |
| 24388 | + |
| 24293 | 24389 | sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n); |
| 24294 | 24390 | if( mem0.alarmThreshold>0 ){ |
| 24295 | 24391 | sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 24296 | 24392 | if( nUsed >= mem0.alarmThreshold - nFull ){ |
| 24297 | 24393 | mem0.nearlyFull = 1; |
| | @@ -24311,11 +24407,10 @@ |
| 24311 | 24407 | nFull = sqlite3MallocSize(p); |
| 24312 | 24408 | sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull); |
| 24313 | 24409 | sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 24314 | 24410 | } |
| 24315 | 24411 | *pp = p; |
| 24316 | | - return nFull; |
| 24317 | 24412 | } |
| 24318 | 24413 | |
| 24319 | 24414 | /* |
| 24320 | 24415 | ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 24321 | 24416 | ** assumes the memory subsystem has already been initialized. |
| | @@ -24951,11 +25046,10 @@ |
| 24951 | 25046 | |
| 24952 | 25047 | /* |
| 24953 | 25048 | ** Allowed values for et_info.flags |
| 24954 | 25049 | */ |
| 24955 | 25050 | #define FLAG_SIGNED 1 /* True if the value to convert is signed */ |
| 24956 | | -#define FLAG_INTERN 2 /* True if for internal use only */ |
| 24957 | 25051 | #define FLAG_STRING 4 /* Allow infinity precision */ |
| 24958 | 25052 | |
| 24959 | 25053 | |
| 24960 | 25054 | /* |
| 24961 | 25055 | ** The following table is searched linearly, so it is good to put the |
| | @@ -24985,15 +25079,14 @@ |
| 24985 | 25079 | { 'i', 10, 1, etRADIX, 0, 0 }, |
| 24986 | 25080 | { 'n', 0, 0, etSIZE, 0, 0 }, |
| 24987 | 25081 | { '%', 0, 0, etPERCENT, 0, 0 }, |
| 24988 | 25082 | { 'p', 16, 0, etPOINTER, 0, 1 }, |
| 24989 | 25083 | |
| 24990 | | -/* All the rest have the FLAG_INTERN bit set and are thus for internal |
| 24991 | | -** use only */ |
| 24992 | | - { 'T', 0, 2, etTOKEN, 0, 0 }, |
| 24993 | | - { 'S', 0, 2, etSRCLIST, 0, 0 }, |
| 24994 | | - { 'r', 10, 3, etORDINAL, 0, 0 }, |
| 25084 | + /* All the rest are undocumented and are for internal use only */ |
| 25085 | + { 'T', 0, 0, etTOKEN, 0, 0 }, |
| 25086 | + { 'S', 0, 0, etSRCLIST, 0, 0 }, |
| 25087 | + { 'r', 10, 1, etORDINAL, 0, 0 }, |
| 24995 | 25088 | }; |
| 24996 | 25089 | |
| 24997 | 25090 | /* |
| 24998 | 25091 | ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point |
| 24999 | 25092 | ** conversions will work. |
| | @@ -25083,11 +25176,10 @@ |
| 25083 | 25176 | etByte flag_long; /* True if "l" flag is present */ |
| 25084 | 25177 | etByte flag_longlong; /* True if the "ll" flag is present */ |
| 25085 | 25178 | etByte done; /* Loop termination flag */ |
| 25086 | 25179 | etByte xtype = etINVALID; /* Conversion paradigm */ |
| 25087 | 25180 | u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ |
| 25088 | | - u8 useIntern; /* Ok to use internal conversions (ex: %T) */ |
| 25089 | 25181 | char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
| 25090 | 25182 | sqlite_uint64 longvalue; /* Value for integer types */ |
| 25091 | 25183 | LONGDOUBLE_TYPE realvalue; /* Value for real types */ |
| 25092 | 25184 | const et_info *infop; /* Pointer to the appropriate info structure */ |
| 25093 | 25185 | char *zOut; /* Rendering buffer */ |
| | @@ -25102,17 +25194,15 @@ |
| 25102 | 25194 | #endif |
| 25103 | 25195 | PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
| 25104 | 25196 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 25105 | 25197 | |
| 25106 | 25198 | bufpt = 0; |
| 25107 | | - if( pAccum->printfFlags ){ |
| 25108 | | - if( (bArgList = (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ |
| 25109 | | - pArgList = va_arg(ap, PrintfArguments*); |
| 25110 | | - } |
| 25111 | | - useIntern = pAccum->printfFlags & SQLITE_PRINTF_INTERNAL; |
| 25199 | + if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ |
| 25200 | + pArgList = va_arg(ap, PrintfArguments*); |
| 25201 | + bArgList = 1; |
| 25112 | 25202 | }else{ |
| 25113 | | - bArgList = useIntern = 0; |
| 25203 | + bArgList = 0; |
| 25114 | 25204 | } |
| 25115 | 25205 | for(; (c=(*fmt))!=0; ++fmt){ |
| 25116 | 25206 | if( c!='%' ){ |
| 25117 | 25207 | bufpt = (char *)fmt; |
| 25118 | 25208 | #if HAVE_STRCHRNUL |
| | @@ -25220,15 +25310,11 @@ |
| 25220 | 25310 | infop = &fmtinfo[0]; |
| 25221 | 25311 | xtype = etINVALID; |
| 25222 | 25312 | for(idx=0; idx<ArraySize(fmtinfo); idx++){ |
| 25223 | 25313 | if( c==fmtinfo[idx].fmttype ){ |
| 25224 | 25314 | infop = &fmtinfo[idx]; |
| 25225 | | - if( useIntern || (infop->flags & FLAG_INTERN)==0 ){ |
| 25226 | | - xtype = infop->type; |
| 25227 | | - }else{ |
| 25228 | | - return; |
| 25229 | | - } |
| 25315 | + xtype = infop->type; |
| 25230 | 25316 | break; |
| 25231 | 25317 | } |
| 25232 | 25318 | } |
| 25233 | 25319 | |
| 25234 | 25320 | /* |
| | @@ -25593,22 +25679,28 @@ |
| 25593 | 25679 | ** consume, not the length of the output... |
| 25594 | 25680 | ** if( precision>=0 && precision<length ) length = precision; */ |
| 25595 | 25681 | break; |
| 25596 | 25682 | } |
| 25597 | 25683 | case etTOKEN: { |
| 25598 | | - Token *pToken = va_arg(ap, Token*); |
| 25684 | + Token *pToken; |
| 25685 | + if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
| 25686 | + pToken = va_arg(ap, Token*); |
| 25599 | 25687 | assert( bArgList==0 ); |
| 25600 | 25688 | if( pToken && pToken->n ){ |
| 25601 | 25689 | sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); |
| 25602 | 25690 | } |
| 25603 | 25691 | length = width = 0; |
| 25604 | 25692 | break; |
| 25605 | 25693 | } |
| 25606 | 25694 | case etSRCLIST: { |
| 25607 | | - SrcList *pSrc = va_arg(ap, SrcList*); |
| 25608 | | - int k = va_arg(ap, int); |
| 25609 | | - struct SrcList_item *pItem = &pSrc->a[k]; |
| 25695 | + SrcList *pSrc; |
| 25696 | + int k; |
| 25697 | + struct SrcList_item *pItem; |
| 25698 | + if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
| 25699 | + pSrc = va_arg(ap, SrcList*); |
| 25700 | + k = va_arg(ap, int); |
| 25701 | + pItem = &pSrc->a[k]; |
| 25610 | 25702 | assert( bArgList==0 ); |
| 25611 | 25703 | assert( k>=0 && k<pSrc->nSrc ); |
| 25612 | 25704 | if( pItem->zDatabase ){ |
| 25613 | 25705 | sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); |
| 25614 | 25706 | sqlite3StrAccumAppend(pAccum, ".", 1); |
| | @@ -25626,13 +25718,17 @@ |
| 25626 | 25718 | ** The text of the conversion is pointed to by "bufpt" and is |
| 25627 | 25719 | ** "length" characters long. The field width is "width". Do |
| 25628 | 25720 | ** the output. |
| 25629 | 25721 | */ |
| 25630 | 25722 | width -= length; |
| 25631 | | - if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); |
| 25632 | | - sqlite3StrAccumAppend(pAccum, bufpt, length); |
| 25633 | | - if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); |
| 25723 | + if( width>0 ){ |
| 25724 | + if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); |
| 25725 | + sqlite3StrAccumAppend(pAccum, bufpt, length); |
| 25726 | + if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); |
| 25727 | + }else{ |
| 25728 | + sqlite3StrAccumAppend(pAccum, bufpt, length); |
| 25729 | + } |
| 25634 | 25730 | |
| 25635 | 25731 | if( zExtra ){ |
| 25636 | 25732 | sqlite3DbFree(pAccum->db, zExtra); |
| 25637 | 25733 | zExtra = 0; |
| 25638 | 25734 | } |
| | @@ -28600,17 +28696,15 @@ |
| 28600 | 28696 | SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){ |
| 28601 | 28697 | #if SQLITE_BYTEORDER==4321 |
| 28602 | 28698 | u32 x; |
| 28603 | 28699 | memcpy(&x,p,4); |
| 28604 | 28700 | return x; |
| 28605 | | -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ |
| 28606 | | - && defined(__GNUC__) && GCC_VERSION>=4003000 |
| 28701 | +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
| 28607 | 28702 | u32 x; |
| 28608 | 28703 | memcpy(&x,p,4); |
| 28609 | 28704 | return __builtin_bswap32(x); |
| 28610 | | -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ |
| 28611 | | - && defined(_MSC_VER) && _MSC_VER>=1300 |
| 28705 | +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 28612 | 28706 | u32 x; |
| 28613 | 28707 | memcpy(&x,p,4); |
| 28614 | 28708 | return _byteswap_ulong(x); |
| 28615 | 28709 | #else |
| 28616 | 28710 | testcase( p[0]&0x80 ); |
| | @@ -28618,16 +28712,14 @@ |
| 28618 | 28712 | #endif |
| 28619 | 28713 | } |
| 28620 | 28714 | SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){ |
| 28621 | 28715 | #if SQLITE_BYTEORDER==4321 |
| 28622 | 28716 | memcpy(p,&v,4); |
| 28623 | | -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ |
| 28624 | | - && defined(__GNUC__) && GCC_VERSION>=4003000 |
| 28717 | +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
| 28625 | 28718 | u32 x = __builtin_bswap32(v); |
| 28626 | 28719 | memcpy(p,&x,4); |
| 28627 | | -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ |
| 28628 | | - && defined(_MSC_VER) && _MSC_VER>=1300 |
| 28720 | +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 28629 | 28721 | u32 x = _byteswap_ulong(v); |
| 28630 | 28722 | memcpy(p,&x,4); |
| 28631 | 28723 | #else |
| 28632 | 28724 | p[0] = (u8)(v>>24); |
| 28633 | 28725 | p[1] = (u8)(v>>16); |
| | @@ -28739,10 +28831,13 @@ |
| 28739 | 28831 | ** the other 64-bit signed integer at *pA and store the result in *pA. |
| 28740 | 28832 | ** Return 0 on success. Or if the operation would have resulted in an |
| 28741 | 28833 | ** overflow, leave *pA unchanged and return 1. |
| 28742 | 28834 | */ |
| 28743 | 28835 | SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){ |
| 28836 | +#if GCC_VERSION>=5004000 |
| 28837 | + return __builtin_add_overflow(*pA, iB, pA); |
| 28838 | +#else |
| 28744 | 28839 | i64 iA = *pA; |
| 28745 | 28840 | testcase( iA==0 ); testcase( iA==1 ); |
| 28746 | 28841 | testcase( iB==-1 ); testcase( iB==0 ); |
| 28747 | 28842 | if( iB>=0 ){ |
| 28748 | 28843 | testcase( iA>0 && LARGEST_INT64 - iA == iB ); |
| | @@ -28753,23 +28848,31 @@ |
| 28753 | 28848 | testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); |
| 28754 | 28849 | if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; |
| 28755 | 28850 | } |
| 28756 | 28851 | *pA += iB; |
| 28757 | 28852 | return 0; |
| 28853 | +#endif |
| 28758 | 28854 | } |
| 28759 | 28855 | SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){ |
| 28856 | +#if GCC_VERSION>=5004000 |
| 28857 | + return __builtin_sub_overflow(*pA, iB, pA); |
| 28858 | +#else |
| 28760 | 28859 | testcase( iB==SMALLEST_INT64+1 ); |
| 28761 | 28860 | if( iB==SMALLEST_INT64 ){ |
| 28762 | 28861 | testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); |
| 28763 | 28862 | if( (*pA)>=0 ) return 1; |
| 28764 | 28863 | *pA -= iB; |
| 28765 | 28864 | return 0; |
| 28766 | 28865 | }else{ |
| 28767 | 28866 | return sqlite3AddInt64(pA, -iB); |
| 28768 | 28867 | } |
| 28868 | +#endif |
| 28769 | 28869 | } |
| 28770 | 28870 | SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){ |
| 28871 | +#if GCC_VERSION>=5004000 |
| 28872 | + return __builtin_mul_overflow(*pA, iB, pA); |
| 28873 | +#else |
| 28771 | 28874 | i64 iA = *pA; |
| 28772 | 28875 | if( iB>0 ){ |
| 28773 | 28876 | if( iA>LARGEST_INT64/iB ) return 1; |
| 28774 | 28877 | if( iA<SMALLEST_INT64/iB ) return 1; |
| 28775 | 28878 | }else if( iB<0 ){ |
| | @@ -28781,10 +28884,11 @@ |
| 28781 | 28884 | if( -iA>LARGEST_INT64/-iB ) return 1; |
| 28782 | 28885 | } |
| 28783 | 28886 | } |
| 28784 | 28887 | *pA = iA*iB; |
| 28785 | 28888 | return 0; |
| 28889 | +#endif |
| 28786 | 28890 | } |
| 28787 | 28891 | |
| 28788 | 28892 | /* |
| 28789 | 28893 | ** Compute the absolute value of a 32-bit signed integer, of possible. Or |
| 28790 | 28894 | ** if the integer has a value of -2147483648, return +2147483647 |
| | @@ -47485,18 +47589,24 @@ |
| 47485 | 47589 | ** if( pPager->jfd->pMethods ){ ... |
| 47486 | 47590 | */ |
| 47487 | 47591 | #define isOpen(pFd) ((pFd)->pMethods!=0) |
| 47488 | 47592 | |
| 47489 | 47593 | /* |
| 47490 | | -** Return true if this pager uses a write-ahead log instead of the usual |
| 47491 | | -** rollback journal. Otherwise false. |
| 47594 | +** Return true if this pager uses a write-ahead log to read page pgno. |
| 47595 | +** Return false if the pager reads pgno directly from the database. |
| 47492 | 47596 | */ |
| 47597 | +#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ) |
| 47598 | +SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){ |
| 47599 | + u32 iRead = 0; |
| 47600 | + int rc; |
| 47601 | + if( pPager->pWal==0 ) return 0; |
| 47602 | + rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); |
| 47603 | + return rc || iRead; |
| 47604 | +} |
| 47605 | +#endif |
| 47493 | 47606 | #ifndef SQLITE_OMIT_WAL |
| 47494 | | -SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager){ |
| 47495 | | - return (pPager->pWal!=0); |
| 47496 | | -} |
| 47497 | | -# define pagerUseWal(x) sqlite3PagerUseWal(x) |
| 47607 | +# define pagerUseWal(x) ((x)->pWal!=0) |
| 47498 | 47608 | #else |
| 47499 | 47609 | # define pagerUseWal(x) 0 |
| 47500 | 47610 | # define pagerRollbackWal(x) 0 |
| 47501 | 47611 | # define pagerWalFrames(v,w,x,y) 0 |
| 47502 | 47612 | # define pagerOpenWalIfPresent(z) SQLITE_OK |
| | @@ -58445,15 +58555,13 @@ |
| 58445 | 58555 | ** two-byte aligned address. get2bytea() is only used for accessing the |
| 58446 | 58556 | ** cell addresses in a btree header. |
| 58447 | 58557 | */ |
| 58448 | 58558 | #if SQLITE_BYTEORDER==4321 |
| 58449 | 58559 | # define get2byteAligned(x) (*(u16*)(x)) |
| 58450 | | -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ |
| 58451 | | - && GCC_VERSION>=4008000 |
| 58560 | +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000 |
| 58452 | 58561 | # define get2byteAligned(x) __builtin_bswap16(*(u16*)(x)) |
| 58453 | | -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ |
| 58454 | | - && defined(_MSC_VER) && _MSC_VER>=1300 |
| 58562 | +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 58455 | 58563 | # define get2byteAligned(x) _byteswap_ushort(*(u16*)(x)) |
| 58456 | 58564 | #else |
| 58457 | 58565 | # define get2byteAligned(x) ((x)[0]<<8 | (x)[1]) |
| 58458 | 58566 | #endif |
| 58459 | 58567 | |
| | @@ -58624,27 +58732,38 @@ |
| 58624 | 58732 | ** Enter the mutexes in accending order by BtShared pointer address |
| 58625 | 58733 | ** to avoid the possibility of deadlock when two threads with |
| 58626 | 58734 | ** two or more btrees in common both try to lock all their btrees |
| 58627 | 58735 | ** at the same instant. |
| 58628 | 58736 | */ |
| 58629 | | -SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){ |
| 58737 | +static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){ |
| 58630 | 58738 | int i; |
| 58739 | + int skipOk = 1; |
| 58631 | 58740 | Btree *p; |
| 58632 | 58741 | assert( sqlite3_mutex_held(db->mutex) ); |
| 58633 | 58742 | for(i=0; i<db->nDb; i++){ |
| 58634 | 58743 | p = db->aDb[i].pBt; |
| 58635 | | - if( p ) sqlite3BtreeEnter(p); |
| 58744 | + if( p && p->sharable ){ |
| 58745 | + sqlite3BtreeEnter(p); |
| 58746 | + skipOk = 0; |
| 58747 | + } |
| 58636 | 58748 | } |
| 58749 | + db->skipBtreeMutex = skipOk; |
| 58637 | 58750 | } |
| 58638 | | -SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){ |
| 58751 | +SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){ |
| 58752 | + if( db->skipBtreeMutex==0 ) btreeEnterAll(db); |
| 58753 | +} |
| 58754 | +static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){ |
| 58639 | 58755 | int i; |
| 58640 | 58756 | Btree *p; |
| 58641 | 58757 | assert( sqlite3_mutex_held(db->mutex) ); |
| 58642 | 58758 | for(i=0; i<db->nDb; i++){ |
| 58643 | 58759 | p = db->aDb[i].pBt; |
| 58644 | 58760 | if( p ) sqlite3BtreeLeave(p); |
| 58645 | 58761 | } |
| 58762 | +} |
| 58763 | +SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){ |
| 58764 | + if( db->skipBtreeMutex==0 ) btreeLeaveAll(db); |
| 58646 | 58765 | } |
| 58647 | 58766 | |
| 58648 | 58767 | #ifndef NDEBUG |
| 58649 | 58768 | /* |
| 58650 | 58769 | ** Return true if the current thread holds the database connection |
| | @@ -62097,16 +62216,18 @@ |
| 62097 | 62216 | for(i=0; i<nCell; i++){ |
| 62098 | 62217 | u8 *pCell = findCell(pPage, i); |
| 62099 | 62218 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 62100 | 62219 | CellInfo info; |
| 62101 | 62220 | pPage->xParseCell(pPage, pCell, &info); |
| 62102 | | - if( info.nLocal<info.nPayload |
| 62103 | | - && pCell+info.nSize-1<=pPage->aData+pPage->maskPage |
| 62104 | | - && iFrom==get4byte(pCell+info.nSize-4) |
| 62105 | | - ){ |
| 62106 | | - put4byte(pCell+info.nSize-4, iTo); |
| 62107 | | - break; |
| 62221 | + if( info.nLocal<info.nPayload ){ |
| 62222 | + if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){ |
| 62223 | + return SQLITE_CORRUPT_BKPT; |
| 62224 | + } |
| 62225 | + if( iFrom==get4byte(pCell+info.nSize-4) ){ |
| 62226 | + put4byte(pCell+info.nSize-4, iTo); |
| 62227 | + break; |
| 62228 | + } |
| 62108 | 62229 | } |
| 62109 | 62230 | }else{ |
| 62110 | 62231 | if( get4byte(pCell)==iFrom ){ |
| 62111 | 62232 | put4byte(pCell, iTo); |
| 62112 | 62233 | break; |
| | @@ -62777,11 +62898,16 @@ |
| 62777 | 62898 | if( p && p->inTrans==TRANS_WRITE ){ |
| 62778 | 62899 | BtShared *pBt = p->pBt; |
| 62779 | 62900 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 62780 | 62901 | assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); |
| 62781 | 62902 | sqlite3BtreeEnter(p); |
| 62782 | | - rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); |
| 62903 | + if( op==SAVEPOINT_ROLLBACK ){ |
| 62904 | + rc = saveAllCursors(pBt, 0, 0); |
| 62905 | + } |
| 62906 | + if( rc==SQLITE_OK ){ |
| 62907 | + rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); |
| 62908 | + } |
| 62783 | 62909 | if( rc==SQLITE_OK ){ |
| 62784 | 62910 | if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){ |
| 62785 | 62911 | pBt->nPage = 0; |
| 62786 | 62912 | } |
| 62787 | 62913 | rc = newDatabase(pBt); |
| | @@ -63163,25 +63289,24 @@ |
| 63163 | 63289 | ** for the entry that the pCur cursor is pointing to. The eOp |
| 63164 | 63290 | ** argument is interpreted as follows: |
| 63165 | 63291 | ** |
| 63166 | 63292 | ** 0: The operation is a read. Populate the overflow cache. |
| 63167 | 63293 | ** 1: The operation is a write. Populate the overflow cache. |
| 63168 | | -** 2: The operation is a read. Do not populate the overflow cache. |
| 63169 | 63294 | ** |
| 63170 | 63295 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 63171 | 63296 | ** Data is read to or from the buffer pBuf. |
| 63172 | 63297 | ** |
| 63173 | 63298 | ** The content being read or written might appear on the main page |
| 63174 | 63299 | ** or be scattered out on multiple overflow pages. |
| 63175 | 63300 | ** |
| 63176 | | -** If the current cursor entry uses one or more overflow pages and the |
| 63177 | | -** eOp argument is not 2, this function may allocate space for and lazily |
| 63178 | | -** populates the overflow page-list cache array (BtCursor.aOverflow). |
| 63301 | +** If the current cursor entry uses one or more overflow pages |
| 63302 | +** this function may allocate space for and lazily populate |
| 63303 | +** the overflow page-list cache array (BtCursor.aOverflow). |
| 63179 | 63304 | ** Subsequent calls use this cache to make seeking to the supplied offset |
| 63180 | 63305 | ** more efficient. |
| 63181 | 63306 | ** |
| 63182 | | -** Once an overflow page-list cache has been allocated, it may be |
| 63307 | +** Once an overflow page-list cache has been allocated, it must be |
| 63183 | 63308 | ** invalidated if some other cursor writes to the same table, or if |
| 63184 | 63309 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 63185 | 63310 | ** mode, the following events may invalidate an overflow page-list cache. |
| 63186 | 63311 | ** |
| 63187 | 63312 | ** * An incremental vacuum, |
| | @@ -63199,25 +63324,21 @@ |
| 63199 | 63324 | int rc = SQLITE_OK; |
| 63200 | 63325 | int iIdx = 0; |
| 63201 | 63326 | MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ |
| 63202 | 63327 | BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ |
| 63203 | 63328 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 63204 | | - unsigned char * const pBufStart = pBuf; |
| 63205 | | - int bEnd; /* True if reading to end of data */ |
| 63329 | + unsigned char * const pBufStart = pBuf; /* Start of original out buffer */ |
| 63206 | 63330 | #endif |
| 63207 | 63331 | |
| 63208 | 63332 | assert( pPage ); |
| 63333 | + assert( eOp==0 || eOp==1 ); |
| 63209 | 63334 | assert( pCur->eState==CURSOR_VALID ); |
| 63210 | 63335 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
| 63211 | 63336 | assert( cursorHoldsMutex(pCur) ); |
| 63212 | | - assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */ |
| 63213 | 63337 | |
| 63214 | 63338 | getCellInfo(pCur); |
| 63215 | 63339 | aPayload = pCur->info.pPayload; |
| 63216 | | -#ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 63217 | | - bEnd = offset+amt==pCur->info.nPayload; |
| 63218 | | -#endif |
| 63219 | 63340 | assert( offset+amt <= pCur->info.nPayload ); |
| 63220 | 63341 | |
| 63221 | 63342 | assert( aPayload > pPage->aData ); |
| 63222 | 63343 | if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){ |
| 63223 | 63344 | /* Trying to read or write past the end of the data is an error. The |
| | @@ -63232,11 +63353,11 @@ |
| 63232 | 63353 | if( offset<pCur->info.nLocal ){ |
| 63233 | 63354 | int a = amt; |
| 63234 | 63355 | if( a+offset>pCur->info.nLocal ){ |
| 63235 | 63356 | a = pCur->info.nLocal - offset; |
| 63236 | 63357 | } |
| 63237 | | - rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage); |
| 63358 | + rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
| 63238 | 63359 | offset = 0; |
| 63239 | 63360 | pBuf += a; |
| 63240 | 63361 | amt -= a; |
| 63241 | 63362 | }else{ |
| 63242 | 63363 | offset -= pCur->info.nLocal; |
| | @@ -63248,69 +63369,58 @@ |
| 63248 | 63369 | Pgno nextPage; |
| 63249 | 63370 | |
| 63250 | 63371 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
| 63251 | 63372 | |
| 63252 | 63373 | /* If the BtCursor.aOverflow[] has not been allocated, allocate it now. |
| 63253 | | - ** Except, do not allocate aOverflow[] for eOp==2. |
| 63254 | 63374 | ** |
| 63255 | 63375 | ** The aOverflow[] array is sized at one entry for each overflow page |
| 63256 | 63376 | ** in the overflow chain. The page number of the first overflow page is |
| 63257 | 63377 | ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array |
| 63258 | 63378 | ** means "not yet known" (the cache is lazily populated). |
| 63259 | 63379 | */ |
| 63260 | | - if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){ |
| 63380 | + if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){ |
| 63261 | 63381 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
| 63262 | 63382 | if( nOvfl>pCur->nOvflAlloc ){ |
| 63263 | 63383 | Pgno *aNew = (Pgno*)sqlite3Realloc( |
| 63264 | 63384 | pCur->aOverflow, nOvfl*2*sizeof(Pgno) |
| 63265 | 63385 | ); |
| 63266 | 63386 | if( aNew==0 ){ |
| 63267 | | - rc = SQLITE_NOMEM_BKPT; |
| 63387 | + return SQLITE_NOMEM_BKPT; |
| 63268 | 63388 | }else{ |
| 63269 | 63389 | pCur->nOvflAlloc = nOvfl*2; |
| 63270 | 63390 | pCur->aOverflow = aNew; |
| 63271 | 63391 | } |
| 63272 | 63392 | } |
| 63273 | | - if( rc==SQLITE_OK ){ |
| 63274 | | - memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno)); |
| 63275 | | - pCur->curFlags |= BTCF_ValidOvfl; |
| 63393 | + memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno)); |
| 63394 | + pCur->curFlags |= BTCF_ValidOvfl; |
| 63395 | + }else{ |
| 63396 | + /* If the overflow page-list cache has been allocated and the |
| 63397 | + ** entry for the first required overflow page is valid, skip |
| 63398 | + ** directly to it. |
| 63399 | + */ |
| 63400 | + if( pCur->aOverflow[offset/ovflSize] ){ |
| 63401 | + iIdx = (offset/ovflSize); |
| 63402 | + nextPage = pCur->aOverflow[iIdx]; |
| 63403 | + offset = (offset%ovflSize); |
| 63276 | 63404 | } |
| 63277 | 63405 | } |
| 63278 | 63406 | |
| 63279 | | - /* If the overflow page-list cache has been allocated and the |
| 63280 | | - ** entry for the first required overflow page is valid, skip |
| 63281 | | - ** directly to it. |
| 63282 | | - */ |
| 63283 | | - if( (pCur->curFlags & BTCF_ValidOvfl)!=0 |
| 63284 | | - && pCur->aOverflow[offset/ovflSize] |
| 63285 | | - ){ |
| 63286 | | - iIdx = (offset/ovflSize); |
| 63287 | | - nextPage = pCur->aOverflow[iIdx]; |
| 63288 | | - offset = (offset%ovflSize); |
| 63289 | | - } |
| 63290 | | - |
| 63291 | | - for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 63292 | | - |
| 63407 | + assert( rc==SQLITE_OK && amt>0 ); |
| 63408 | + while( nextPage ){ |
| 63293 | 63409 | /* If required, populate the overflow page-list cache. */ |
| 63294 | | - if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){ |
| 63295 | | - assert( pCur->aOverflow[iIdx]==0 |
| 63296 | | - || pCur->aOverflow[iIdx]==nextPage |
| 63297 | | - || CORRUPT_DB ); |
| 63298 | | - pCur->aOverflow[iIdx] = nextPage; |
| 63299 | | - } |
| 63410 | + assert( pCur->aOverflow[iIdx]==0 |
| 63411 | + || pCur->aOverflow[iIdx]==nextPage |
| 63412 | + || CORRUPT_DB ); |
| 63413 | + pCur->aOverflow[iIdx] = nextPage; |
| 63300 | 63414 | |
| 63301 | 63415 | if( offset>=ovflSize ){ |
| 63302 | 63416 | /* The only reason to read this page is to obtain the page |
| 63303 | 63417 | ** number for the next page in the overflow chain. The page |
| 63304 | 63418 | ** data is not required. So first try to lookup the overflow |
| 63305 | 63419 | ** page-list cache, if any, then fall back to the getOverflowPage() |
| 63306 | 63420 | ** function. |
| 63307 | | - ** |
| 63308 | | - ** Note that the aOverflow[] array must be allocated because eOp!=2 |
| 63309 | | - ** here. If eOp==2, then offset==0 and this branch is never taken. |
| 63310 | 63421 | */ |
| 63311 | | - assert( eOp!=2 ); |
| 63312 | 63422 | assert( pCur->curFlags & BTCF_ValidOvfl ); |
| 63313 | 63423 | assert( pCur->pBtree->db==pBt->db ); |
| 63314 | 63424 | if( pCur->aOverflow[iIdx+1] ){ |
| 63315 | 63425 | nextPage = pCur->aOverflow[iIdx+1]; |
| 63316 | 63426 | }else{ |
| | @@ -63320,11 +63430,11 @@ |
| 63320 | 63430 | }else{ |
| 63321 | 63431 | /* Need to read this page properly. It contains some of the |
| 63322 | 63432 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
| 63323 | 63433 | */ |
| 63324 | 63434 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 63325 | | - sqlite3_file *fd; |
| 63435 | + sqlite3_file *fd; /* File from which to do direct overflow read */ |
| 63326 | 63436 | #endif |
| 63327 | 63437 | int a = amt; |
| 63328 | 63438 | if( a + offset > ovflSize ){ |
| 63329 | 63439 | a = ovflSize - offset; |
| 63330 | 63440 | } |
| | @@ -63332,31 +63442,29 @@ |
| 63332 | 63442 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 63333 | 63443 | /* If all the following are true: |
| 63334 | 63444 | ** |
| 63335 | 63445 | ** 1) this is a read operation, and |
| 63336 | 63446 | ** 2) data is required from the start of this overflow page, and |
| 63337 | | - ** 3) the database is file-backed, and |
| 63338 | | - ** 4) there is no open write-transaction, and |
| 63339 | | - ** 5) the database is not a WAL database, |
| 63340 | | - ** 6) all data from the page is being read. |
| 63341 | | - ** 7) at least 4 bytes have already been read into the output buffer |
| 63447 | + ** 3) there is no open write-transaction, and |
| 63448 | + ** 4) the database is file-backed, and |
| 63449 | + ** 5) the page is not in the WAL file |
| 63450 | + ** 6) at least 4 bytes have already been read into the output buffer |
| 63342 | 63451 | ** |
| 63343 | 63452 | ** then data can be read directly from the database file into the |
| 63344 | 63453 | ** output buffer, bypassing the page-cache altogether. This speeds |
| 63345 | 63454 | ** up loading large records that span many overflow pages. |
| 63346 | 63455 | */ |
| 63347 | | - if( (eOp&0x01)==0 /* (1) */ |
| 63456 | + if( eOp==0 /* (1) */ |
| 63348 | 63457 | && offset==0 /* (2) */ |
| 63349 | | - && (bEnd || a==ovflSize) /* (6) */ |
| 63350 | | - && pBt->inTransaction==TRANS_READ /* (4) */ |
| 63351 | | - && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */ |
| 63352 | | - && 0==sqlite3PagerUseWal(pBt->pPager) /* (5) */ |
| 63353 | | - && &pBuf[-4]>=pBufStart /* (7) */ |
| 63458 | + && pBt->inTransaction==TRANS_READ /* (3) */ |
| 63459 | + && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */ |
| 63460 | + && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */ |
| 63461 | + && &pBuf[-4]>=pBufStart /* (6) */ |
| 63354 | 63462 | ){ |
| 63355 | 63463 | u8 aSave[4]; |
| 63356 | 63464 | u8 *aWrite = &pBuf[-4]; |
| 63357 | | - assert( aWrite>=pBufStart ); /* hence (7) */ |
| 63465 | + assert( aWrite>=pBufStart ); /* due to (6) */ |
| 63358 | 63466 | memcpy(aSave, aWrite, 4); |
| 63359 | 63467 | rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); |
| 63360 | 63468 | nextPage = get4byte(aWrite); |
| 63361 | 63469 | memcpy(aWrite, aSave, 4); |
| 63362 | 63470 | }else |
| | @@ -63363,28 +63471,31 @@ |
| 63363 | 63471 | #endif |
| 63364 | 63472 | |
| 63365 | 63473 | { |
| 63366 | 63474 | DbPage *pDbPage; |
| 63367 | 63475 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage, |
| 63368 | | - ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0) |
| 63476 | + (eOp==0 ? PAGER_GET_READONLY : 0) |
| 63369 | 63477 | ); |
| 63370 | 63478 | if( rc==SQLITE_OK ){ |
| 63371 | 63479 | aPayload = sqlite3PagerGetData(pDbPage); |
| 63372 | 63480 | nextPage = get4byte(aPayload); |
| 63373 | | - rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage); |
| 63481 | + rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 63374 | 63482 | sqlite3PagerUnref(pDbPage); |
| 63375 | 63483 | offset = 0; |
| 63376 | 63484 | } |
| 63377 | 63485 | } |
| 63378 | 63486 | amt -= a; |
| 63487 | + if( amt==0 ) return rc; |
| 63379 | 63488 | pBuf += a; |
| 63380 | 63489 | } |
| 63490 | + if( rc ) break; |
| 63491 | + iIdx++; |
| 63381 | 63492 | } |
| 63382 | 63493 | } |
| 63383 | 63494 | |
| 63384 | 63495 | if( rc==SQLITE_OK && amt>0 ){ |
| 63385 | | - return SQLITE_CORRUPT_BKPT; |
| 63496 | + return SQLITE_CORRUPT_BKPT; /* Overflow chain ends prematurely */ |
| 63386 | 63497 | } |
| 63387 | 63498 | return rc; |
| 63388 | 63499 | } |
| 63389 | 63500 | |
| 63390 | 63501 | /* |
| | @@ -63409,25 +63520,38 @@ |
| 63409 | 63520 | assert( pCur->eState==CURSOR_VALID ); |
| 63410 | 63521 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 63411 | 63522 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
| 63412 | 63523 | return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); |
| 63413 | 63524 | } |
| 63525 | + |
| 63526 | +/* |
| 63527 | +** This variant of sqlite3BtreePayload() works even if the cursor has not |
| 63528 | +** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read() |
| 63529 | +** interface. |
| 63530 | +*/ |
| 63414 | 63531 | #ifndef SQLITE_OMIT_INCRBLOB |
| 63415 | | -SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
| 63532 | +static SQLITE_NOINLINE int accessPayloadChecked( |
| 63533 | + BtCursor *pCur, |
| 63534 | + u32 offset, |
| 63535 | + u32 amt, |
| 63536 | + void *pBuf |
| 63537 | +){ |
| 63416 | 63538 | int rc; |
| 63417 | 63539 | if ( pCur->eState==CURSOR_INVALID ){ |
| 63418 | 63540 | return SQLITE_ABORT; |
| 63419 | 63541 | } |
| 63420 | 63542 | assert( cursorOwnsBtShared(pCur) ); |
| 63421 | | - rc = restoreCursorPosition(pCur); |
| 63422 | | - if( rc==SQLITE_OK ){ |
| 63423 | | - assert( pCur->eState==CURSOR_VALID ); |
| 63424 | | - assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 63425 | | - assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
| 63426 | | - rc = accessPayload(pCur, offset, amt, pBuf, 0); |
| 63427 | | - } |
| 63428 | | - return rc; |
| 63543 | + rc = btreeRestoreCursorPosition(pCur); |
| 63544 | + return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0); |
| 63545 | +} |
| 63546 | +SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
| 63547 | + if( pCur->eState==CURSOR_VALID ){ |
| 63548 | + assert( cursorOwnsBtShared(pCur) ); |
| 63549 | + return accessPayload(pCur, offset, amt, pBuf, 0); |
| 63550 | + }else{ |
| 63551 | + return accessPayloadChecked(pCur, offset, amt, pBuf); |
| 63552 | + } |
| 63429 | 63553 | } |
| 63430 | 63554 | #endif /* SQLITE_OMIT_INCRBLOB */ |
| 63431 | 63555 | |
| 63432 | 63556 | /* |
| 63433 | 63557 | ** Return a pointer to payload information from the entry that the |
| | @@ -63829,13 +63953,30 @@ |
| 63829 | 63953 | ){ |
| 63830 | 63954 | if( pCur->info.nKey==intKey ){ |
| 63831 | 63955 | *pRes = 0; |
| 63832 | 63956 | return SQLITE_OK; |
| 63833 | 63957 | } |
| 63834 | | - if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){ |
| 63835 | | - *pRes = -1; |
| 63836 | | - return SQLITE_OK; |
| 63958 | + if( pCur->info.nKey<intKey ){ |
| 63959 | + if( (pCur->curFlags & BTCF_AtLast)!=0 ){ |
| 63960 | + *pRes = -1; |
| 63961 | + return SQLITE_OK; |
| 63962 | + } |
| 63963 | + /* If the requested key is one more than the previous key, then |
| 63964 | + ** try to get there using sqlite3BtreeNext() rather than a full |
| 63965 | + ** binary search. This is an optimization only. The correct answer |
| 63966 | + ** is still obtained without this ase, only a little more slowely */ |
| 63967 | + if( pCur->info.nKey+1==intKey && !pCur->skipNext ){ |
| 63968 | + *pRes = 0; |
| 63969 | + rc = sqlite3BtreeNext(pCur, pRes); |
| 63970 | + if( rc ) return rc; |
| 63971 | + if( *pRes==0 ){ |
| 63972 | + getCellInfo(pCur); |
| 63973 | + if( pCur->info.nKey==intKey ){ |
| 63974 | + return SQLITE_OK; |
| 63975 | + } |
| 63976 | + } |
| 63977 | + } |
| 63837 | 63978 | } |
| 63838 | 63979 | } |
| 63839 | 63980 | |
| 63840 | 63981 | if( pIdxKey ){ |
| 63841 | 63982 | xRecordCompare = sqlite3VdbeFindCompare(pIdxKey); |
| | @@ -63967,11 +64108,12 @@ |
| 63967 | 64108 | if( pCellKey==0 ){ |
| 63968 | 64109 | rc = SQLITE_NOMEM_BKPT; |
| 63969 | 64110 | goto moveto_finish; |
| 63970 | 64111 | } |
| 63971 | 64112 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
| 63972 | | - rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2); |
| 64113 | + rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0); |
| 64114 | + pCur->curFlags &= ~BTCF_ValidOvfl; |
| 63973 | 64115 | if( rc ){ |
| 63974 | 64116 | sqlite3_free(pCellKey); |
| 63975 | 64117 | goto moveto_finish; |
| 63976 | 64118 | } |
| 63977 | 64119 | c = xRecordCompare(nCell, pCellKey, pIdxKey); |
| | @@ -66010,11 +66152,10 @@ |
| 66010 | 66152 | */ |
| 66011 | 66153 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
| 66012 | 66154 | for(i=0; i<nOld; i++){ |
| 66013 | 66155 | MemPage *p = apOld[i]; |
| 66014 | 66156 | szNew[i] = usableSpace - p->nFree; |
| 66015 | | - if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
| 66016 | 66157 | for(j=0; j<p->nOverflow; j++){ |
| 66017 | 66158 | szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]); |
| 66018 | 66159 | } |
| 66019 | 66160 | cntNew[i] = cntOld[i]; |
| 66020 | 66161 | } |
| | @@ -66689,11 +66830,11 @@ |
| 66689 | 66830 | ** to decode the key. |
| 66690 | 66831 | */ |
| 66691 | 66832 | SQLITE_PRIVATE int sqlite3BtreeInsert( |
| 66692 | 66833 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
| 66693 | 66834 | const BtreePayload *pX, /* Content of the row to be inserted */ |
| 66694 | | - int appendBias, /* True if this is likely an append */ |
| 66835 | + int flags, /* True if this is likely an append */ |
| 66695 | 66836 | int seekResult /* Result of prior MovetoUnpacked() call */ |
| 66696 | 66837 | ){ |
| 66697 | 66838 | int rc; |
| 66698 | 66839 | int loc = seekResult; /* -1: before desired location +1: after */ |
| 66699 | 66840 | int szNew = 0; |
| | @@ -66701,10 +66842,12 @@ |
| 66701 | 66842 | MemPage *pPage; |
| 66702 | 66843 | Btree *p = pCur->pBtree; |
| 66703 | 66844 | BtShared *pBt = p->pBt; |
| 66704 | 66845 | unsigned char *oldCell; |
| 66705 | 66846 | unsigned char *newCell = 0; |
| 66847 | + |
| 66848 | + assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags ); |
| 66706 | 66849 | |
| 66707 | 66850 | if( pCur->eState==CURSOR_FAULT ){ |
| 66708 | 66851 | assert( pCur->skipNext!=SQLITE_OK ); |
| 66709 | 66852 | return pCur->skipNext; |
| 66710 | 66853 | } |
| | @@ -66742,23 +66885,28 @@ |
| 66742 | 66885 | assert( pX->pKey==0 ); |
| 66743 | 66886 | /* If this is an insert into a table b-tree, invalidate any incrblob |
| 66744 | 66887 | ** cursors open on the row being replaced */ |
| 66745 | 66888 | invalidateIncrblobCursors(p, pX->nKey, 0); |
| 66746 | 66889 | |
| 66890 | + /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing |
| 66891 | + ** to a row with the same key as the new entry being inserted. */ |
| 66892 | + assert( (flags & BTREE_SAVEPOSITION)==0 || |
| 66893 | + ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) ); |
| 66894 | + |
| 66747 | 66895 | /* If the cursor is currently on the last row and we are appending a |
| 66748 | 66896 | ** new row onto the end, set the "loc" to avoid an unnecessary |
| 66749 | 66897 | ** btreeMoveto() call */ |
| 66750 | 66898 | if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){ |
| 66751 | 66899 | loc = 0; |
| 66752 | 66900 | }else if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0 |
| 66753 | 66901 | && pCur->info.nKey==pX->nKey-1 ){ |
| 66754 | 66902 | loc = -1; |
| 66755 | 66903 | }else if( loc==0 ){ |
| 66756 | | - rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, appendBias, &loc); |
| 66904 | + rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc); |
| 66757 | 66905 | if( rc ) return rc; |
| 66758 | 66906 | } |
| 66759 | | - }else if( loc==0 ){ |
| 66907 | + }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){ |
| 66760 | 66908 | if( pX->nMem ){ |
| 66761 | 66909 | UnpackedRecord r; |
| 66762 | 66910 | r.pKeyInfo = pCur->pKeyInfo; |
| 66763 | 66911 | r.aMem = pX->aMem; |
| 66764 | 66912 | r.nField = pX->nMem; |
| | @@ -66765,13 +66913,13 @@ |
| 66765 | 66913 | r.default_rc = 0; |
| 66766 | 66914 | r.errCode = 0; |
| 66767 | 66915 | r.r1 = 0; |
| 66768 | 66916 | r.r2 = 0; |
| 66769 | 66917 | r.eqSeen = 0; |
| 66770 | | - rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, appendBias, &loc); |
| 66918 | + rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc); |
| 66771 | 66919 | }else{ |
| 66772 | | - rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc); |
| 66920 | + rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc); |
| 66773 | 66921 | } |
| 66774 | 66922 | if( rc ) return rc; |
| 66775 | 66923 | } |
| 66776 | 66924 | assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) ); |
| 66777 | 66925 | |
| | @@ -66855,10 +67003,24 @@ |
| 66855 | 67003 | ** fails. Internal data structure corruption will result otherwise. |
| 66856 | 67004 | ** Also, set the cursor state to invalid. This stops saveCursorPosition() |
| 66857 | 67005 | ** from trying to save the current position of the cursor. */ |
| 66858 | 67006 | pCur->apPage[pCur->iPage]->nOverflow = 0; |
| 66859 | 67007 | pCur->eState = CURSOR_INVALID; |
| 67008 | + if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){ |
| 67009 | + rc = moveToRoot(pCur); |
| 67010 | + if( pCur->pKeyInfo ){ |
| 67011 | + assert( pCur->pKey==0 ); |
| 67012 | + pCur->pKey = sqlite3Malloc( pX->nKey ); |
| 67013 | + if( pCur->pKey==0 ){ |
| 67014 | + rc = SQLITE_NOMEM; |
| 67015 | + }else{ |
| 67016 | + memcpy(pCur->pKey, pX->pKey, pX->nKey); |
| 67017 | + } |
| 67018 | + } |
| 67019 | + pCur->eState = CURSOR_REQUIRESEEK; |
| 67020 | + pCur->nKey = pX->nKey; |
| 67021 | + } |
| 66860 | 67022 | } |
| 66861 | 67023 | assert( pCur->apPage[pCur->iPage]->nOverflow==0 ); |
| 66862 | 67024 | |
| 66863 | 67025 | end_insert: |
| 66864 | 67026 | return rc; |
| | @@ -71765,11 +71927,11 @@ |
| 71765 | 71927 | sqlite3VdbeGetOp(p,addr)->p2 = val; |
| 71766 | 71928 | } |
| 71767 | 71929 | SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ |
| 71768 | 71930 | sqlite3VdbeGetOp(p,addr)->p3 = val; |
| 71769 | 71931 | } |
| 71770 | | -SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){ |
| 71932 | +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){ |
| 71771 | 71933 | assert( p->nOp>0 || p->db->mallocFailed ); |
| 71772 | 71934 | if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5; |
| 71773 | 71935 | } |
| 71774 | 71936 | |
| 71775 | 71937 | /* |
| | @@ -73479,64 +73641,63 @@ |
| 73479 | 73641 | ** statement transaction is committed. |
| 73480 | 73642 | ** |
| 73481 | 73643 | ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. |
| 73482 | 73644 | ** Otherwise SQLITE_OK. |
| 73483 | 73645 | */ |
| 73484 | | -SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ |
| 73646 | +static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){ |
| 73485 | 73647 | sqlite3 *const db = p->db; |
| 73486 | 73648 | int rc = SQLITE_OK; |
| 73487 | | - |
| 73488 | | - /* If p->iStatement is greater than zero, then this Vdbe opened a |
| 73489 | | - ** statement transaction that should be closed here. The only exception |
| 73490 | | - ** is that an IO error may have occurred, causing an emergency rollback. |
| 73491 | | - ** In this case (db->nStatement==0), and there is nothing to do. |
| 73492 | | - */ |
| 73493 | | - if( db->nStatement && p->iStatement ){ |
| 73494 | | - int i; |
| 73495 | | - const int iSavepoint = p->iStatement-1; |
| 73496 | | - |
| 73497 | | - assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); |
| 73498 | | - assert( db->nStatement>0 ); |
| 73499 | | - assert( p->iStatement==(db->nStatement+db->nSavepoint) ); |
| 73500 | | - |
| 73501 | | - for(i=0; i<db->nDb; i++){ |
| 73502 | | - int rc2 = SQLITE_OK; |
| 73503 | | - Btree *pBt = db->aDb[i].pBt; |
| 73504 | | - if( pBt ){ |
| 73505 | | - if( eOp==SAVEPOINT_ROLLBACK ){ |
| 73506 | | - rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); |
| 73507 | | - } |
| 73508 | | - if( rc2==SQLITE_OK ){ |
| 73509 | | - rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); |
| 73510 | | - } |
| 73511 | | - if( rc==SQLITE_OK ){ |
| 73512 | | - rc = rc2; |
| 73513 | | - } |
| 73514 | | - } |
| 73515 | | - } |
| 73516 | | - db->nStatement--; |
| 73517 | | - p->iStatement = 0; |
| 73518 | | - |
| 73519 | | - if( rc==SQLITE_OK ){ |
| 73520 | | - if( eOp==SAVEPOINT_ROLLBACK ){ |
| 73521 | | - rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); |
| 73522 | | - } |
| 73523 | | - if( rc==SQLITE_OK ){ |
| 73524 | | - rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); |
| 73525 | | - } |
| 73526 | | - } |
| 73527 | | - |
| 73528 | | - /* If the statement transaction is being rolled back, also restore the |
| 73529 | | - ** database handles deferred constraint counter to the value it had when |
| 73530 | | - ** the statement transaction was opened. */ |
| 73531 | | - if( eOp==SAVEPOINT_ROLLBACK ){ |
| 73532 | | - db->nDeferredCons = p->nStmtDefCons; |
| 73533 | | - db->nDeferredImmCons = p->nStmtDefImmCons; |
| 73534 | | - } |
| 73535 | | - } |
| 73536 | | - return rc; |
| 73537 | | -} |
| 73649 | + int i; |
| 73650 | + const int iSavepoint = p->iStatement-1; |
| 73651 | + |
| 73652 | + assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); |
| 73653 | + assert( db->nStatement>0 ); |
| 73654 | + assert( p->iStatement==(db->nStatement+db->nSavepoint) ); |
| 73655 | + |
| 73656 | + for(i=0; i<db->nDb; i++){ |
| 73657 | + int rc2 = SQLITE_OK; |
| 73658 | + Btree *pBt = db->aDb[i].pBt; |
| 73659 | + if( pBt ){ |
| 73660 | + if( eOp==SAVEPOINT_ROLLBACK ){ |
| 73661 | + rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); |
| 73662 | + } |
| 73663 | + if( rc2==SQLITE_OK ){ |
| 73664 | + rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); |
| 73665 | + } |
| 73666 | + if( rc==SQLITE_OK ){ |
| 73667 | + rc = rc2; |
| 73668 | + } |
| 73669 | + } |
| 73670 | + } |
| 73671 | + db->nStatement--; |
| 73672 | + p->iStatement = 0; |
| 73673 | + |
| 73674 | + if( rc==SQLITE_OK ){ |
| 73675 | + if( eOp==SAVEPOINT_ROLLBACK ){ |
| 73676 | + rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); |
| 73677 | + } |
| 73678 | + if( rc==SQLITE_OK ){ |
| 73679 | + rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); |
| 73680 | + } |
| 73681 | + } |
| 73682 | + |
| 73683 | + /* If the statement transaction is being rolled back, also restore the |
| 73684 | + ** database handles deferred constraint counter to the value it had when |
| 73685 | + ** the statement transaction was opened. */ |
| 73686 | + if( eOp==SAVEPOINT_ROLLBACK ){ |
| 73687 | + db->nDeferredCons = p->nStmtDefCons; |
| 73688 | + db->nDeferredImmCons = p->nStmtDefImmCons; |
| 73689 | + } |
| 73690 | + return rc; |
| 73691 | +} |
| 73692 | +SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ |
| 73693 | + if( p->db->nStatement && p->iStatement ){ |
| 73694 | + return vdbeCloseStatement(p, eOp); |
| 73695 | + } |
| 73696 | + return SQLITE_OK; |
| 73697 | +} |
| 73698 | + |
| 73538 | 73699 | |
| 73539 | 73700 | /* |
| 73540 | 73701 | ** This function is called when a transaction opened by the database |
| 73541 | 73702 | ** handle associated with the VM passed as an argument is about to be |
| 73542 | 73703 | ** committed. If there are outstanding deferred foreign key constraint |
| | @@ -75567,14 +75728,14 @@ |
| 75567 | 75728 | ** structure itself, using sqlite3DbFree(). |
| 75568 | 75729 | ** |
| 75569 | 75730 | ** This function is used to free UnpackedRecord structures allocated by |
| 75570 | 75731 | ** the vdbeUnpackRecord() function found in vdbeapi.c. |
| 75571 | 75732 | */ |
| 75572 | | -static void vdbeFreeUnpacked(sqlite3 *db, UnpackedRecord *p){ |
| 75733 | +static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){ |
| 75573 | 75734 | if( p ){ |
| 75574 | 75735 | int i; |
| 75575 | | - for(i=0; i<p->nField; i++){ |
| 75736 | + for(i=0; i<nField; i++){ |
| 75576 | 75737 | Mem *pMem = &p->aMem[i]; |
| 75577 | 75738 | if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem); |
| 75578 | 75739 | } |
| 75579 | 75740 | sqlite3DbFree(db, p); |
| 75580 | 75741 | } |
| | @@ -75603,14 +75764,19 @@ |
| 75603 | 75764 | const char *zTbl = pTab->zName; |
| 75604 | 75765 | static const u8 fakeSortOrder = 0; |
| 75605 | 75766 | |
| 75606 | 75767 | assert( db->pPreUpdate==0 ); |
| 75607 | 75768 | memset(&preupdate, 0, sizeof(PreUpdate)); |
| 75608 | | - if( op==SQLITE_UPDATE ){ |
| 75609 | | - iKey2 = v->aMem[iReg].u.i; |
| 75769 | + if( HasRowid(pTab)==0 ){ |
| 75770 | + iKey1 = iKey2 = 0; |
| 75771 | + preupdate.pPk = sqlite3PrimaryKeyIndex(pTab); |
| 75610 | 75772 | }else{ |
| 75611 | | - iKey2 = iKey1; |
| 75773 | + if( op==SQLITE_UPDATE ){ |
| 75774 | + iKey2 = v->aMem[iReg].u.i; |
| 75775 | + }else{ |
| 75776 | + iKey2 = iKey1; |
| 75777 | + } |
| 75612 | 75778 | } |
| 75613 | 75779 | |
| 75614 | 75780 | assert( pCsr->nField==pTab->nCol |
| 75615 | 75781 | || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1) |
| 75616 | 75782 | ); |
| | @@ -75629,12 +75795,12 @@ |
| 75629 | 75795 | |
| 75630 | 75796 | db->pPreUpdate = &preupdate; |
| 75631 | 75797 | db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2); |
| 75632 | 75798 | db->pPreUpdate = 0; |
| 75633 | 75799 | sqlite3DbFree(db, preupdate.aRecord); |
| 75634 | | - vdbeFreeUnpacked(db, preupdate.pUnpacked); |
| 75635 | | - vdbeFreeUnpacked(db, preupdate.pNewUnpacked); |
| 75800 | + vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pUnpacked); |
| 75801 | + vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pNewUnpacked); |
| 75636 | 75802 | if( preupdate.aNew ){ |
| 75637 | 75803 | int i; |
| 75638 | 75804 | for(i=0; i<pCsr->nField; i++){ |
| 75639 | 75805 | sqlite3VdbeMemRelease(&preupdate.aNew[i]); |
| 75640 | 75806 | } |
| | @@ -77305,18 +77471,22 @@ |
| 77305 | 77471 | ** This function is called from within a pre-update callback to retrieve |
| 77306 | 77472 | ** a field of the row currently being updated or deleted. |
| 77307 | 77473 | */ |
| 77308 | 77474 | SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ |
| 77309 | 77475 | PreUpdate *p = db->pPreUpdate; |
| 77476 | + Mem *pMem; |
| 77310 | 77477 | int rc = SQLITE_OK; |
| 77311 | 77478 | |
| 77312 | 77479 | /* Test that this call is being made from within an SQLITE_DELETE or |
| 77313 | 77480 | ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ |
| 77314 | 77481 | if( !p || p->op==SQLITE_INSERT ){ |
| 77315 | 77482 | rc = SQLITE_MISUSE_BKPT; |
| 77316 | 77483 | goto preupdate_old_out; |
| 77317 | 77484 | } |
| 77485 | + if( p->pPk ){ |
| 77486 | + iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx); |
| 77487 | + } |
| 77318 | 77488 | if( iIdx>=p->pCsr->nField || iIdx<0 ){ |
| 77319 | 77489 | rc = SQLITE_RANGE; |
| 77320 | 77490 | goto preupdate_old_out; |
| 77321 | 77491 | } |
| 77322 | 77492 | |
| | @@ -77338,21 +77508,18 @@ |
| 77338 | 77508 | goto preupdate_old_out; |
| 77339 | 77509 | } |
| 77340 | 77510 | p->aRecord = aRec; |
| 77341 | 77511 | } |
| 77342 | 77512 | |
| 77343 | | - if( iIdx>=p->pUnpacked->nField ){ |
| 77513 | + pMem = *ppValue = &p->pUnpacked->aMem[iIdx]; |
| 77514 | + if( iIdx==p->pTab->iPKey ){ |
| 77515 | + sqlite3VdbeMemSetInt64(pMem, p->iKey1); |
| 77516 | + }else if( iIdx>=p->pUnpacked->nField ){ |
| 77344 | 77517 | *ppValue = (sqlite3_value *)columnNullValue(); |
| 77345 | | - }else{ |
| 77346 | | - Mem *pMem = *ppValue = &p->pUnpacked->aMem[iIdx]; |
| 77347 | | - *ppValue = &p->pUnpacked->aMem[iIdx]; |
| 77348 | | - if( iIdx==p->pTab->iPKey ){ |
| 77349 | | - sqlite3VdbeMemSetInt64(pMem, p->iKey1); |
| 77350 | | - }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ |
| 77351 | | - if( pMem->flags & MEM_Int ){ |
| 77352 | | - sqlite3VdbeMemRealify(pMem); |
| 77353 | | - } |
| 77518 | + }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ |
| 77519 | + if( pMem->flags & MEM_Int ){ |
| 77520 | + sqlite3VdbeMemRealify(pMem); |
| 77354 | 77521 | } |
| 77355 | 77522 | } |
| 77356 | 77523 | |
| 77357 | 77524 | preupdate_old_out: |
| 77358 | 77525 | sqlite3Error(db, rc); |
| | @@ -77401,10 +77568,13 @@ |
| 77401 | 77568 | |
| 77402 | 77569 | if( !p || p->op==SQLITE_DELETE ){ |
| 77403 | 77570 | rc = SQLITE_MISUSE_BKPT; |
| 77404 | 77571 | goto preupdate_new_out; |
| 77405 | 77572 | } |
| 77573 | + if( p->pPk && p->op!=SQLITE_UPDATE ){ |
| 77574 | + iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx); |
| 77575 | + } |
| 77406 | 77576 | if( iIdx>=p->pCsr->nField || iIdx<0 ){ |
| 77407 | 77577 | rc = SQLITE_RANGE; |
| 77408 | 77578 | goto preupdate_new_out; |
| 77409 | 77579 | } |
| 77410 | 77580 | |
| | @@ -77421,17 +77591,15 @@ |
| 77421 | 77591 | rc = SQLITE_NOMEM; |
| 77422 | 77592 | goto preupdate_new_out; |
| 77423 | 77593 | } |
| 77424 | 77594 | p->pNewUnpacked = pUnpack; |
| 77425 | 77595 | } |
| 77426 | | - if( iIdx>=pUnpack->nField ){ |
| 77596 | + pMem = &pUnpack->aMem[iIdx]; |
| 77597 | + if( iIdx==p->pTab->iPKey ){ |
| 77598 | + sqlite3VdbeMemSetInt64(pMem, p->iKey2); |
| 77599 | + }else if( iIdx>=pUnpack->nField ){ |
| 77427 | 77600 | pMem = (sqlite3_value *)columnNullValue(); |
| 77428 | | - }else{ |
| 77429 | | - pMem = &pUnpack->aMem[iIdx]; |
| 77430 | | - if( iIdx==p->pTab->iPKey ){ |
| 77431 | | - sqlite3VdbeMemSetInt64(pMem, p->iKey2); |
| 77432 | | - } |
| 77433 | 77601 | } |
| 77434 | 77602 | }else{ |
| 77435 | 77603 | /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required |
| 77436 | 77604 | ** value. Make a copy of the cell contents and return a pointer to it. |
| 77437 | 77605 | ** It is not safe to return a pointer to the memory cell itself as the |
| | @@ -78404,12 +78572,10 @@ |
| 78404 | 78572 | Mem *aMem = p->aMem; /* Copy of p->aMem */ |
| 78405 | 78573 | Mem *pIn1 = 0; /* 1st input operand */ |
| 78406 | 78574 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 78407 | 78575 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 78408 | 78576 | Mem *pOut = 0; /* Output operand */ |
| 78409 | | - int *aPermute = 0; /* Permutation of columns for OP_Compare */ |
| 78410 | | - i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */ |
| 78411 | 78577 | #ifdef VDBE_PROFILE |
| 78412 | 78578 | u64 start; /* CPU clock count at start of opcode */ |
| 78413 | 78579 | #endif |
| 78414 | 78580 | /*** INSERT STACK UNION HERE ***/ |
| 78415 | 78581 | |
| | @@ -78420,11 +78586,10 @@ |
| 78420 | 78586 | ** sqlite3_column_text16() failed. */ |
| 78421 | 78587 | goto no_mem; |
| 78422 | 78588 | } |
| 78423 | 78589 | assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); |
| 78424 | 78590 | assert( p->bIsReader || p->readOnly!=0 ); |
| 78425 | | - p->rc = SQLITE_OK; |
| 78426 | 78591 | p->iCurrentTime = 0; |
| 78427 | 78592 | assert( p->explain==0 ); |
| 78428 | 78593 | p->pResultSet = 0; |
| 78429 | 78594 | db->busyHandler.nBusy = 0; |
| 78430 | 78595 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
| | @@ -78781,11 +78946,10 @@ |
| 78781 | 78946 | pFrame = p->pFrame; |
| 78782 | 78947 | p->pFrame = pFrame->pParent; |
| 78783 | 78948 | p->nFrame--; |
| 78784 | 78949 | sqlite3VdbeSetChanges(db, p->nChange); |
| 78785 | 78950 | pcx = sqlite3VdbeFrameRestore(pFrame); |
| 78786 | | - lastRowid = db->lastRowid; |
| 78787 | 78951 | if( pOp->p2==OE_Ignore ){ |
| 78788 | 78952 | /* Instruction pcx is the OP_Program that invoked the sub-program |
| 78789 | 78953 | ** currently being halted. If the p2 instruction of this OP_Halt |
| 78790 | 78954 | ** instruction is set to OE_Ignore, then the sub-program is throwing |
| 78791 | 78955 | ** an IGNORE exception. In this case jump to the address specified |
| | @@ -79016,11 +79180,11 @@ |
| 79016 | 79180 | assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) ); |
| 79017 | 79181 | pVar = &p->aVar[pOp->p1 - 1]; |
| 79018 | 79182 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 79019 | 79183 | goto too_big; |
| 79020 | 79184 | } |
| 79021 | | - pOut = out2Prerelease(p, pOp); |
| 79185 | + pOut = &aMem[pOp->p2]; |
| 79022 | 79186 | sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); |
| 79023 | 79187 | UPDATE_MAX_BLOBSIZE(pOut); |
| 79024 | 79188 | break; |
| 79025 | 79189 | } |
| 79026 | 79190 | |
| | @@ -79503,13 +79667,11 @@ |
| 79503 | 79667 | REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 79504 | 79668 | } |
| 79505 | 79669 | #endif |
| 79506 | 79670 | MemSetTypeFlag(pCtx->pOut, MEM_Null); |
| 79507 | 79671 | pCtx->fErrorOrAux = 0; |
| 79508 | | - db->lastRowid = lastRowid; |
| 79509 | 79672 | (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ |
| 79510 | | - lastRowid = db->lastRowid; /* Remember rowid changes made by xSFunc */ |
| 79511 | 79673 | |
| 79512 | 79674 | /* If the function returned an error, throw an exception */ |
| 79513 | 79675 | if( pCtx->fErrorOrAux ){ |
| 79514 | 79676 | if( pCtx->isError ){ |
| 79515 | 79677 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); |
| | @@ -79961,12 +80123,12 @@ |
| 79961 | 80123 | } |
| 79962 | 80124 | |
| 79963 | 80125 | |
| 79964 | 80126 | /* Opcode: Permutation * * * P4 * |
| 79965 | 80127 | ** |
| 79966 | | -** Set the permutation used by the OP_Compare operator to be the array |
| 79967 | | -** of integers in P4. |
| 80128 | +** Set the permutation used by the OP_Compare operator in the next |
| 80129 | +** instruction. The permutation is stored in the P4 operand. |
| 79968 | 80130 | ** |
| 79969 | 80131 | ** The permutation is only valid until the next OP_Compare that has |
| 79970 | 80132 | ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should |
| 79971 | 80133 | ** occur immediately prior to the OP_Compare. |
| 79972 | 80134 | ** |
| | @@ -79974,11 +80136,12 @@ |
| 79974 | 80136 | ** and does not become part of the permutation. |
| 79975 | 80137 | */ |
| 79976 | 80138 | case OP_Permutation: { |
| 79977 | 80139 | assert( pOp->p4type==P4_INTARRAY ); |
| 79978 | 80140 | assert( pOp->p4.ai ); |
| 79979 | | - aPermute = pOp->p4.ai + 1; |
| 80141 | + assert( pOp[1].opcode==OP_Compare ); |
| 80142 | + assert( pOp[1].p5 & OPFLAG_PERMUTE ); |
| 79980 | 80143 | break; |
| 79981 | 80144 | } |
| 79982 | 80145 | |
| 79983 | 80146 | /* Opcode: Compare P1 P2 P3 P4 P5 |
| 79984 | 80147 | ** Synopsis: r[P1@P3] <-> r[P2@P3] |
| | @@ -80007,12 +80170,21 @@ |
| 80007 | 80170 | int p2; |
| 80008 | 80171 | const KeyInfo *pKeyInfo; |
| 80009 | 80172 | int idx; |
| 80010 | 80173 | CollSeq *pColl; /* Collating sequence to use on this term */ |
| 80011 | 80174 | int bRev; /* True for DESCENDING sort order */ |
| 80175 | + int *aPermute; /* The permutation */ |
| 80012 | 80176 | |
| 80013 | | - if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0; |
| 80177 | + if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){ |
| 80178 | + aPermute = 0; |
| 80179 | + }else{ |
| 80180 | + assert( pOp>aOp ); |
| 80181 | + assert( pOp[-1].opcode==OP_Permutation ); |
| 80182 | + assert( pOp[-1].p4type==P4_INTARRAY ); |
| 80183 | + aPermute = pOp[-1].p4.ai + 1; |
| 80184 | + assert( aPermute!=0 ); |
| 80185 | + } |
| 80014 | 80186 | n = pOp->p3; |
| 80015 | 80187 | pKeyInfo = pOp->p4.pKeyInfo; |
| 80016 | 80188 | assert( n>0 ); |
| 80017 | 80189 | assert( pKeyInfo!=0 ); |
| 80018 | 80190 | p1 = pOp->p1; |
| | @@ -80041,11 +80213,10 @@ |
| 80041 | 80213 | if( iCompare ){ |
| 80042 | 80214 | if( bRev ) iCompare = -iCompare; |
| 80043 | 80215 | break; |
| 80044 | 80216 | } |
| 80045 | 80217 | } |
| 80046 | | - aPermute = 0; |
| 80047 | 80218 | break; |
| 80048 | 80219 | } |
| 80049 | 80220 | |
| 80050 | 80221 | /* Opcode: Jump P1 P2 P3 * * |
| 80051 | 80222 | ** |
| | @@ -80597,10 +80768,24 @@ |
| 80597 | 80768 | do{ |
| 80598 | 80769 | applyAffinity(pRec++, *(zAffinity++), encoding); |
| 80599 | 80770 | assert( zAffinity[0]==0 || pRec<=pLast ); |
| 80600 | 80771 | }while( zAffinity[0] ); |
| 80601 | 80772 | } |
| 80773 | + |
| 80774 | +#ifdef SQLITE_ENABLE_NULL_TRIM |
| 80775 | + /* NULLs can be safely trimmed from the end of the record, as long as |
| 80776 | + ** as the schema format is 2 or more and none of the omitted columns |
| 80777 | + ** have a non-NULL default value. Also, the record must be left with |
| 80778 | + ** at least one field. If P5>0 then it will be one more than the |
| 80779 | + ** index of the right-most column with a non-NULL default value */ |
| 80780 | + if( pOp->p5 ){ |
| 80781 | + while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){ |
| 80782 | + pLast--; |
| 80783 | + nField--; |
| 80784 | + } |
| 80785 | + } |
| 80786 | +#endif |
| 80602 | 80787 | |
| 80603 | 80788 | /* Loop through the elements that will make up the record to figure |
| 80604 | 80789 | ** out how much space is required for the new record. |
| 80605 | 80790 | */ |
| 80606 | 80791 | pRec = pLast; |
| | @@ -82187,11 +82372,11 @@ |
| 82187 | 82372 | assert( memIsValid(pData) ); |
| 82188 | 82373 | pC = p->apCsr[pOp->p1]; |
| 82189 | 82374 | assert( pC!=0 ); |
| 82190 | 82375 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 82191 | 82376 | assert( pC->uc.pCursor!=0 ); |
| 82192 | | - assert( pC->isTable ); |
| 82377 | + assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable ); |
| 82193 | 82378 | assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); |
| 82194 | 82379 | REGISTER_TRACE(pOp->p2, pData); |
| 82195 | 82380 | |
| 82196 | 82381 | if( pOp->opcode==OP_Insert ){ |
| 82197 | 82382 | pKey = &aMem[pOp->p3]; |
| | @@ -82203,18 +82388,17 @@ |
| 82203 | 82388 | assert( pOp->opcode==OP_InsertInt ); |
| 82204 | 82389 | x.nKey = pOp->p3; |
| 82205 | 82390 | } |
| 82206 | 82391 | |
| 82207 | 82392 | if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
| 82208 | | - assert( pC->isTable ); |
| 82209 | 82393 | assert( pC->iDb>=0 ); |
| 82210 | 82394 | zDb = db->aDb[pC->iDb].zDbSName; |
| 82211 | 82395 | pTab = pOp->p4.pTab; |
| 82212 | | - assert( HasRowid(pTab) ); |
| 82396 | + assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) ); |
| 82213 | 82397 | op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); |
| 82214 | 82398 | }else{ |
| 82215 | | - pTab = 0; /* Not needed. Silence a comiler warning. */ |
| 82399 | + pTab = 0; /* Not needed. Silence a compiler warning. */ |
| 82216 | 82400 | zDb = 0; /* Not needed. Silence a compiler warning. */ |
| 82217 | 82401 | } |
| 82218 | 82402 | |
| 82219 | 82403 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 82220 | 82404 | /* Invoke the pre-update hook, if any */ |
| | @@ -82222,14 +82406,15 @@ |
| 82222 | 82406 | && pOp->p4type==P4_TABLE |
| 82223 | 82407 | && !(pOp->p5 & OPFLAG_ISUPDATE) |
| 82224 | 82408 | ){ |
| 82225 | 82409 | sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2); |
| 82226 | 82410 | } |
| 82411 | + if( pOp->p5 & OPFLAG_ISNOOP ) break; |
| 82227 | 82412 | #endif |
| 82228 | 82413 | |
| 82229 | 82414 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
| 82230 | | - if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = x.nKey; |
| 82415 | + if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey; |
| 82231 | 82416 | if( pData->flags & MEM_Null ){ |
| 82232 | 82417 | x.pData = 0; |
| 82233 | 82418 | x.nData = 0; |
| 82234 | 82419 | }else{ |
| 82235 | 82420 | assert( pData->flags & (MEM_Blob|MEM_Str) ); |
| | @@ -82242,11 +82427,11 @@ |
| 82242 | 82427 | }else{ |
| 82243 | 82428 | x.nZero = 0; |
| 82244 | 82429 | } |
| 82245 | 82430 | x.pKey = 0; |
| 82246 | 82431 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
| 82247 | | - (pOp->p5 & OPFLAG_APPEND)!=0, seekResult |
| 82432 | + (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult |
| 82248 | 82433 | ); |
| 82249 | 82434 | pC->deferredMoveto = 0; |
| 82250 | 82435 | pC->cacheStatus = CACHE_STALE; |
| 82251 | 82436 | |
| 82252 | 82437 | /* Invoke the update-hook if required. */ |
| | @@ -82334,12 +82519,15 @@ |
| 82334 | 82519 | pTab = 0; /* Not needed. Silence a compiler warning. */ |
| 82335 | 82520 | } |
| 82336 | 82521 | |
| 82337 | 82522 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 82338 | 82523 | /* Invoke the pre-update-hook if required. */ |
| 82339 | | - if( db->xPreUpdateCallback && pOp->p4.pTab && HasRowid(pTab) ){ |
| 82340 | | - assert( !(opflags & OPFLAG_ISUPDATE) || (aMem[pOp->p3].flags & MEM_Int) ); |
| 82524 | + if( db->xPreUpdateCallback && pOp->p4.pTab ){ |
| 82525 | + assert( !(opflags & OPFLAG_ISUPDATE) |
| 82526 | + || HasRowid(pTab)==0 |
| 82527 | + || (aMem[pOp->p3].flags & MEM_Int) |
| 82528 | + ); |
| 82341 | 82529 | sqlite3VdbePreUpdateHook(p, pC, |
| 82342 | 82530 | (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, |
| 82343 | 82531 | zDb, pTab, pC->movetoTarget, |
| 82344 | 82532 | pOp->p3 |
| 82345 | 82533 | ); |
| | @@ -82453,11 +82641,11 @@ |
| 82453 | 82641 | if( rc ) goto abort_due_to_error; |
| 82454 | 82642 | p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; |
| 82455 | 82643 | break; |
| 82456 | 82644 | } |
| 82457 | 82645 | |
| 82458 | | -/* Opcode: RowData P1 P2 * * * |
| 82646 | +/* Opcode: RowData P1 P2 P3 * * |
| 82459 | 82647 | ** Synopsis: r[P2]=data |
| 82460 | 82648 | ** |
| 82461 | 82649 | ** Write into register P2 the complete row content for the row at |
| 82462 | 82650 | ** which cursor P1 is currently pointing. |
| 82463 | 82651 | ** There is no interpretation of the data. |
| | @@ -82467,18 +82655,30 @@ |
| 82467 | 82655 | ** If cursor P1 is an index, then the content is the key of the row. |
| 82468 | 82656 | ** If cursor P2 is a table, then the content extracted is the data. |
| 82469 | 82657 | ** |
| 82470 | 82658 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 82471 | 82659 | ** of a real table, not a pseudo-table. |
| 82660 | +** |
| 82661 | +** If P3!=0 then this opcode is allowed to make an ephermeral pointer |
| 82662 | +** into the database page. That means that the content of the output |
| 82663 | +** register will be invalidated as soon as the cursor moves - including |
| 82664 | +** moves caused by other cursors that "save" the the current cursors |
| 82665 | +** position in order that they can write to the same table. If P3==0 |
| 82666 | +** then a copy of the data is made into memory. P3!=0 is faster, but |
| 82667 | +** P3==0 is safer. |
| 82668 | +** |
| 82669 | +** If P3!=0 then the content of the P2 register is unsuitable for use |
| 82670 | +** in OP_Result and any OP_Result will invalidate the P2 register content. |
| 82671 | +** The P2 register content is invalidated by opcodes like OP_Function or |
| 82672 | +** by any use of another cursor pointing to the same table. |
| 82472 | 82673 | */ |
| 82473 | 82674 | case OP_RowData: { |
| 82474 | 82675 | VdbeCursor *pC; |
| 82475 | 82676 | BtCursor *pCrsr; |
| 82476 | 82677 | u32 n; |
| 82477 | 82678 | |
| 82478 | | - pOut = &aMem[pOp->p2]; |
| 82479 | | - memAboutToChange(p, pOut); |
| 82679 | + pOut = out2Prerelease(p, pOp); |
| 82480 | 82680 | |
| 82481 | 82681 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 82482 | 82682 | pC = p->apCsr[pOp->p1]; |
| 82483 | 82683 | assert( pC!=0 ); |
| 82484 | 82684 | assert( pC->eCurType==CURTYPE_BTREE ); |
| | @@ -82505,18 +82705,13 @@ |
| 82505 | 82705 | n = sqlite3BtreePayloadSize(pCrsr); |
| 82506 | 82706 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 82507 | 82707 | goto too_big; |
| 82508 | 82708 | } |
| 82509 | 82709 | testcase( n==0 ); |
| 82510 | | - if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){ |
| 82511 | | - goto no_mem; |
| 82512 | | - } |
| 82513 | | - pOut->n = n; |
| 82514 | | - MemSetTypeFlag(pOut, MEM_Blob); |
| 82515 | | - rc = sqlite3BtreePayload(pCrsr, 0, n, pOut->z); |
| 82710 | + rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut); |
| 82516 | 82711 | if( rc ) goto abort_due_to_error; |
| 82517 | | - pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ |
| 82712 | + if( !pOp->p3 ) Deephemeralize(pOut); |
| 82518 | 82713 | UPDATE_MAX_BLOBSIZE(pOut); |
| 82519 | 82714 | REGISTER_TRACE(pOp->p2, pOut); |
| 82520 | 82715 | break; |
| 82521 | 82716 | } |
| 82522 | 82717 | |
| | @@ -82900,11 +83095,11 @@ |
| 82900 | 83095 | x.nKey = pIn2->n; |
| 82901 | 83096 | x.pKey = pIn2->z; |
| 82902 | 83097 | x.aMem = aMem + pOp->p3; |
| 82903 | 83098 | x.nMem = (u16)pOp->p4.i; |
| 82904 | 83099 | rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
| 82905 | | - (pOp->p5 & OPFLAG_APPEND)!=0, |
| 83100 | + (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), |
| 82906 | 83101 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
| 82907 | 83102 | ); |
| 82908 | 83103 | assert( pC->deferredMoveto==0 ); |
| 82909 | 83104 | pC->cacheStatus = CACHE_STALE; |
| 82910 | 83105 | } |
| | @@ -83022,11 +83217,10 @@ |
| 83022 | 83217 | pTabCur->aAltMap = pOp->p4.ai; |
| 83023 | 83218 | pTabCur->pAltCursor = pC; |
| 83024 | 83219 | }else{ |
| 83025 | 83220 | pOut = out2Prerelease(p, pOp); |
| 83026 | 83221 | pOut->u.i = rowid; |
| 83027 | | - pOut->flags = MEM_Int; |
| 83028 | 83222 | } |
| 83029 | 83223 | }else{ |
| 83030 | 83224 | assert( pOp->opcode==OP_IdxRowid ); |
| 83031 | 83225 | sqlite3VdbeMemSetNull(&aMem[pOp->p2]); |
| 83032 | 83226 | } |
| | @@ -83664,11 +83858,11 @@ |
| 83664 | 83858 | assert( (int)(pOp - aOp)==pFrame->pc ); |
| 83665 | 83859 | } |
| 83666 | 83860 | |
| 83667 | 83861 | p->nFrame++; |
| 83668 | 83862 | pFrame->pParent = p->pFrame; |
| 83669 | | - pFrame->lastRowid = lastRowid; |
| 83863 | + pFrame->lastRowid = db->lastRowid; |
| 83670 | 83864 | pFrame->nChange = p->nChange; |
| 83671 | 83865 | pFrame->nDbChange = p->db->nChange; |
| 83672 | 83866 | assert( pFrame->pAuxData==0 ); |
| 83673 | 83867 | pFrame->pAuxData = p->pAuxData; |
| 83674 | 83868 | p->pAuxData = 0; |
| | @@ -84605,11 +84799,11 @@ |
| 84605 | 84799 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
| 84606 | 84800 | db->vtabOnConflict = vtabOnConflict; |
| 84607 | 84801 | sqlite3VtabImportErrmsg(p, pVtab); |
| 84608 | 84802 | if( rc==SQLITE_OK && pOp->p1 ){ |
| 84609 | 84803 | assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
| 84610 | | - db->lastRowid = lastRowid = rowid; |
| 84804 | + db->lastRowid = rowid; |
| 84611 | 84805 | } |
| 84612 | 84806 | if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ |
| 84613 | 84807 | if( pOp->p5==OE_Ignore ){ |
| 84614 | 84808 | rc = SQLITE_OK; |
| 84615 | 84809 | }else{ |
| | @@ -84841,11 +85035,10 @@ |
| 84841 | 85035 | |
| 84842 | 85036 | /* This is the only way out of this procedure. We have to |
| 84843 | 85037 | ** release the mutexes on btrees that were acquired at the |
| 84844 | 85038 | ** top. */ |
| 84845 | 85039 | vdbe_return: |
| 84846 | | - db->lastRowid = lastRowid; |
| 84847 | 85040 | testcase( nVmStep>0 ); |
| 84848 | 85041 | p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; |
| 84849 | 85042 | sqlite3VdbeLeave(p); |
| 84850 | 85043 | assert( rc!=SQLITE_OK || nExtraDelete==0 |
| 84851 | 85044 | || sqlite3_strlike("DELETE%",p->zSql,0)!=0 |
| | @@ -84905,14 +85098,13 @@ |
| 84905 | 85098 | /* |
| 84906 | 85099 | ** Valid sqlite3_blob* handles point to Incrblob structures. |
| 84907 | 85100 | */ |
| 84908 | 85101 | typedef struct Incrblob Incrblob; |
| 84909 | 85102 | struct Incrblob { |
| 84910 | | - int flags; /* Copy of "flags" passed to sqlite3_blob_open() */ |
| 84911 | 85103 | int nByte; /* Size of open blob, in bytes */ |
| 84912 | 85104 | int iOffset; /* Byte offset of blob in cursor data */ |
| 84913 | | - int iCol; /* Table column this handle is open on */ |
| 85105 | + u16 iCol; /* Table column this handle is open on */ |
| 84914 | 85106 | BtCursor *pCsr; /* Cursor pointing at blob row */ |
| 84915 | 85107 | sqlite3_stmt *pStmt; /* Statement holding cursor open */ |
| 84916 | 85108 | sqlite3 *db; /* The associated database */ |
| 84917 | 85109 | char *zDb; /* Database name */ |
| 84918 | 85110 | Table *pTab; /* Table object */ |
| | @@ -84939,21 +85131,31 @@ |
| 84939 | 85131 | static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){ |
| 84940 | 85132 | int rc; /* Error code */ |
| 84941 | 85133 | char *zErr = 0; /* Error message */ |
| 84942 | 85134 | Vdbe *v = (Vdbe *)p->pStmt; |
| 84943 | 85135 | |
| 84944 | | - /* Set the value of the SQL statements only variable to integer iRow. |
| 84945 | | - ** This is done directly instead of using sqlite3_bind_int64() to avoid |
| 84946 | | - ** triggering asserts related to mutexes. |
| 85136 | + /* Set the value of register r[1] in the SQL statement to integer iRow. |
| 85137 | + ** This is done directly as a performance optimization |
| 84947 | 85138 | */ |
| 84948 | | - assert( v->aVar[0].flags&MEM_Int ); |
| 84949 | | - v->aVar[0].u.i = iRow; |
| 85139 | + v->aMem[1].flags = MEM_Int; |
| 85140 | + v->aMem[1].u.i = iRow; |
| 84950 | 85141 | |
| 84951 | | - rc = sqlite3_step(p->pStmt); |
| 85142 | + /* If the statement has been run before (and is paused at the OP_ResultRow) |
| 85143 | + ** then back it up to the point where it does the OP_SeekRowid. This could |
| 85144 | + ** have been down with an extra OP_Goto, but simply setting the program |
| 85145 | + ** counter is faster. */ |
| 85146 | + if( v->pc>3 ){ |
| 85147 | + v->pc = 3; |
| 85148 | + rc = sqlite3VdbeExec(v); |
| 85149 | + }else{ |
| 85150 | + rc = sqlite3_step(p->pStmt); |
| 85151 | + } |
| 84952 | 85152 | if( rc==SQLITE_ROW ){ |
| 84953 | 85153 | VdbeCursor *pC = v->apCsr[0]; |
| 84954 | | - u32 type = pC->aType[p->iCol]; |
| 85154 | + u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0; |
| 85155 | + testcase( pC->nHdrParsed==p->iCol ); |
| 85156 | + testcase( pC->nHdrParsed==p->iCol+1 ); |
| 84955 | 85157 | if( type<12 ){ |
| 84956 | 85158 | zErr = sqlite3MPrintf(p->db, "cannot open value of type %s", |
| 84957 | 85159 | type==0?"null": type==7?"real": "integer" |
| 84958 | 85160 | ); |
| 84959 | 85161 | rc = SQLITE_ERROR; |
| | @@ -84994,11 +85196,11 @@ |
| 84994 | 85196 | sqlite3* db, /* The database connection */ |
| 84995 | 85197 | const char *zDb, /* The attached database containing the blob */ |
| 84996 | 85198 | const char *zTable, /* The table containing the blob */ |
| 84997 | 85199 | const char *zColumn, /* The column containing the blob */ |
| 84998 | 85200 | sqlite_int64 iRow, /* The row containing the glob */ |
| 84999 | | - int flags, /* True -> read/write access, false -> read-only */ |
| 85201 | + int wrFlag, /* True -> read/write access, false -> read-only */ |
| 85000 | 85202 | sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ |
| 85001 | 85203 | ){ |
| 85002 | 85204 | int nAttempt = 0; |
| 85003 | 85205 | int iCol; /* Index of zColumn in row-record */ |
| 85004 | 85206 | int rc = SQLITE_OK; |
| | @@ -85016,11 +85218,11 @@ |
| 85016 | 85218 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 85017 | 85219 | if( !sqlite3SafetyCheckOk(db) || zTable==0 ){ |
| 85018 | 85220 | return SQLITE_MISUSE_BKPT; |
| 85019 | 85221 | } |
| 85020 | 85222 | #endif |
| 85021 | | - flags = !!flags; /* flags = (flags ? 1 : 0); */ |
| 85223 | + wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */ |
| 85022 | 85224 | |
| 85023 | 85225 | sqlite3_mutex_enter(db->mutex); |
| 85024 | 85226 | |
| 85025 | 85227 | pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); |
| 85026 | 85228 | if( !pBlob ) goto blob_open_out; |
| | @@ -85076,13 +85278,12 @@ |
| 85076 | 85278 | goto blob_open_out; |
| 85077 | 85279 | } |
| 85078 | 85280 | |
| 85079 | 85281 | /* If the value is being opened for writing, check that the |
| 85080 | 85282 | ** column is not indexed, and that it is not part of a foreign key. |
| 85081 | | - ** It is against the rules to open a column to which either of these |
| 85082 | | - ** descriptions applies for writing. */ |
| 85083 | | - if( flags ){ |
| 85283 | + */ |
| 85284 | + if( wrFlag ){ |
| 85084 | 85285 | const char *zFault = 0; |
| 85085 | 85286 | Index *pIdx; |
| 85086 | 85287 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 85087 | 85288 | if( db->flags&SQLITE_ForeignKeys ){ |
| 85088 | 85289 | /* Check that the column is not part of an FK child key definition. It |
| | @@ -85139,22 +85340,21 @@ |
| 85139 | 85340 | */ |
| 85140 | 85341 | static const int iLn = VDBE_OFFSET_LINENO(2); |
| 85141 | 85342 | static const VdbeOpList openBlob[] = { |
| 85142 | 85343 | {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */ |
| 85143 | 85344 | {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */ |
| 85144 | | - {OP_Variable, 1, 1, 0}, /* 2: Move ?1 into reg[1] */ |
| 85145 | | - {OP_NotExists, 0, 7, 1}, /* 3: Seek the cursor */ |
| 85146 | | - {OP_Column, 0, 0, 1}, /* 4 */ |
| 85147 | | - {OP_ResultRow, 1, 0, 0}, /* 5 */ |
| 85148 | | - {OP_Goto, 0, 2, 0}, /* 6 */ |
| 85149 | | - {OP_Halt, 0, 0, 0}, /* 7 */ |
| 85345 | + /* blobSeekToRow() will initialize r[1] to the desired rowid */ |
| 85346 | + {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */ |
| 85347 | + {OP_Column, 0, 0, 1}, /* 3 */ |
| 85348 | + {OP_ResultRow, 1, 0, 0}, /* 4 */ |
| 85349 | + {OP_Halt, 0, 0, 0}, /* 5 */ |
| 85150 | 85350 | }; |
| 85151 | 85351 | Vdbe *v = (Vdbe *)pBlob->pStmt; |
| 85152 | 85352 | int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 85153 | 85353 | VdbeOp *aOp; |
| 85154 | 85354 | |
| 85155 | | - sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, |
| 85355 | + sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag, |
| 85156 | 85356 | pTab->pSchema->schema_cookie, |
| 85157 | 85357 | pTab->pSchema->iGeneration); |
| 85158 | 85358 | sqlite3VdbeChangeP5(v, 1); |
| 85159 | 85359 | aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn); |
| 85160 | 85360 | |
| | @@ -85167,19 +85367,19 @@ |
| 85167 | 85367 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 85168 | 85368 | aOp[0].opcode = OP_Noop; |
| 85169 | 85369 | #else |
| 85170 | 85370 | aOp[0].p1 = iDb; |
| 85171 | 85371 | aOp[0].p2 = pTab->tnum; |
| 85172 | | - aOp[0].p3 = flags; |
| 85372 | + aOp[0].p3 = wrFlag; |
| 85173 | 85373 | sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT); |
| 85174 | 85374 | } |
| 85175 | 85375 | if( db->mallocFailed==0 ){ |
| 85176 | 85376 | #endif |
| 85177 | 85377 | |
| 85178 | 85378 | /* Remove either the OP_OpenWrite or OpenRead. Set the P2 |
| 85179 | 85379 | ** parameter of the other to pTab->tnum. */ |
| 85180 | | - if( flags ) aOp[1].opcode = OP_OpenWrite; |
| 85380 | + if( wrFlag ) aOp[1].opcode = OP_OpenWrite; |
| 85181 | 85381 | aOp[1].p2 = pTab->tnum; |
| 85182 | 85382 | aOp[1].p3 = iDb; |
| 85183 | 85383 | |
| 85184 | 85384 | /* Configure the number of columns. Configure the cursor to |
| 85185 | 85385 | ** think that the table has one more column than it really |
| | @@ -85188,27 +85388,25 @@ |
| 85188 | 85388 | ** we can invoke OP_Column to fill in the vdbe cursors type |
| 85189 | 85389 | ** and offset cache without causing any IO. |
| 85190 | 85390 | */ |
| 85191 | 85391 | aOp[1].p4type = P4_INT32; |
| 85192 | 85392 | aOp[1].p4.i = pTab->nCol+1; |
| 85193 | | - aOp[4].p2 = pTab->nCol; |
| 85393 | + aOp[3].p2 = pTab->nCol; |
| 85194 | 85394 | |
| 85195 | | - pParse->nVar = 1; |
| 85395 | + pParse->nVar = 0; |
| 85196 | 85396 | pParse->nMem = 1; |
| 85197 | 85397 | pParse->nTab = 1; |
| 85198 | 85398 | sqlite3VdbeMakeReady(v, pParse); |
| 85199 | 85399 | } |
| 85200 | 85400 | } |
| 85201 | 85401 | |
| 85202 | | - pBlob->flags = flags; |
| 85203 | 85402 | pBlob->iCol = iCol; |
| 85204 | 85403 | pBlob->db = db; |
| 85205 | 85404 | sqlite3BtreeLeaveAll(db); |
| 85206 | 85405 | if( db->mallocFailed ){ |
| 85207 | 85406 | goto blob_open_out; |
| 85208 | 85407 | } |
| 85209 | | - sqlite3_bind_int64(pBlob->pStmt, 1, iRow); |
| 85210 | 85408 | rc = blobSeekToRow(pBlob, iRow, &zErr); |
| 85211 | 85409 | } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA ); |
| 85212 | 85410 | |
| 85213 | 85411 | blob_open_out: |
| 85214 | 85412 | if( rc==SQLITE_OK && db->mallocFailed==0 ){ |
| | @@ -88741,12 +88939,10 @@ |
| 88741 | 88939 | ** This file contains routines used for walking the parser tree and |
| 88742 | 88940 | ** resolve all identifiers by associating them with a particular |
| 88743 | 88941 | ** table and column. |
| 88744 | 88942 | */ |
| 88745 | 88943 | /* #include "sqliteInt.h" */ |
| 88746 | | -/* #include <stdlib.h> */ |
| 88747 | | -/* #include <string.h> */ |
| 88748 | 88944 | |
| 88749 | 88945 | /* |
| 88750 | 88946 | ** Walk the expression tree pExpr and increase the aggregate function |
| 88751 | 88947 | ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node. |
| 88752 | 88948 | ** This needs to occur when copying a TK_AGG_FUNCTION node from an |
| | @@ -90501,11 +90697,11 @@ |
| 90501 | 90697 | aff = sqlite3ExprAffinity(pExpr->pLeft); |
| 90502 | 90698 | if( pExpr->pRight ){ |
| 90503 | 90699 | aff = sqlite3CompareAffinity(pExpr->pRight, aff); |
| 90504 | 90700 | }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 90505 | 90701 | aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff); |
| 90506 | | - }else if( NEVER(aff==0) ){ |
| 90702 | + }else if( aff==0 ){ |
| 90507 | 90703 | aff = SQLITE_AFF_BLOB; |
| 90508 | 90704 | } |
| 90509 | 90705 | return aff; |
| 90510 | 90706 | } |
| 90511 | 90707 | |
| | @@ -91237,21 +91433,27 @@ |
| 91237 | 91433 | int doAdd = 0; |
| 91238 | 91434 | if( z[0]=='?' ){ |
| 91239 | 91435 | /* Wildcard of the form "?nnn". Convert "nnn" to an integer and |
| 91240 | 91436 | ** use it as the variable number */ |
| 91241 | 91437 | i64 i; |
| 91242 | | - int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); |
| 91243 | | - x = (ynVar)i; |
| 91438 | + int bOk; |
| 91439 | + if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/ |
| 91440 | + i = z[1]-'0'; /* The common case of ?N for a single digit N */ |
| 91441 | + bOk = 1; |
| 91442 | + }else{ |
| 91443 | + bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); |
| 91444 | + } |
| 91244 | 91445 | testcase( i==0 ); |
| 91245 | 91446 | testcase( i==1 ); |
| 91246 | 91447 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); |
| 91247 | 91448 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); |
| 91248 | 91449 | if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
| 91249 | 91450 | sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", |
| 91250 | 91451 | db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); |
| 91251 | 91452 | return; |
| 91252 | 91453 | } |
| 91454 | + x = (ynVar)i; |
| 91253 | 91455 | if( x>pParse->nVar ){ |
| 91254 | 91456 | pParse->nVar = (int)x; |
| 91255 | 91457 | doAdd = 1; |
| 91256 | 91458 | }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){ |
| 91257 | 91459 | doAdd = 1; |
| | @@ -91682,37 +91884,45 @@ |
| 91682 | 91884 | pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
| 91683 | 91885 | pNewItem->idx = pOldItem->idx; |
| 91684 | 91886 | } |
| 91685 | 91887 | return pNew; |
| 91686 | 91888 | } |
| 91687 | | -SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ |
| 91688 | | - Select *pNew, *pPrior; |
| 91889 | +SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){ |
| 91890 | + Select *pRet = 0; |
| 91891 | + Select *pNext = 0; |
| 91892 | + Select **pp = &pRet; |
| 91893 | + Select *p; |
| 91894 | + |
| 91689 | 91895 | assert( db!=0 ); |
| 91690 | | - if( p==0 ) return 0; |
| 91691 | | - pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); |
| 91692 | | - if( pNew==0 ) return 0; |
| 91693 | | - pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); |
| 91694 | | - pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); |
| 91695 | | - pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); |
| 91696 | | - pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); |
| 91697 | | - pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); |
| 91698 | | - pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); |
| 91699 | | - pNew->op = p->op; |
| 91700 | | - pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags); |
| 91701 | | - if( pPrior ) pPrior->pNext = pNew; |
| 91702 | | - pNew->pNext = 0; |
| 91703 | | - pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); |
| 91704 | | - pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); |
| 91705 | | - pNew->iLimit = 0; |
| 91706 | | - pNew->iOffset = 0; |
| 91707 | | - pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; |
| 91708 | | - pNew->addrOpenEphm[0] = -1; |
| 91709 | | - pNew->addrOpenEphm[1] = -1; |
| 91710 | | - pNew->nSelectRow = p->nSelectRow; |
| 91711 | | - pNew->pWith = withDup(db, p->pWith); |
| 91712 | | - sqlite3SelectSetName(pNew, p->zSelName); |
| 91713 | | - return pNew; |
| 91896 | + for(p=pDup; p; p=p->pPrior){ |
| 91897 | + Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); |
| 91898 | + if( pNew==0 ) break; |
| 91899 | + pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); |
| 91900 | + pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); |
| 91901 | + pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); |
| 91902 | + pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); |
| 91903 | + pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); |
| 91904 | + pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); |
| 91905 | + pNew->op = p->op; |
| 91906 | + pNew->pNext = pNext; |
| 91907 | + pNew->pPrior = 0; |
| 91908 | + pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); |
| 91909 | + pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); |
| 91910 | + pNew->iLimit = 0; |
| 91911 | + pNew->iOffset = 0; |
| 91912 | + pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; |
| 91913 | + pNew->addrOpenEphm[0] = -1; |
| 91914 | + pNew->addrOpenEphm[1] = -1; |
| 91915 | + pNew->nSelectRow = p->nSelectRow; |
| 91916 | + pNew->pWith = withDup(db, p->pWith); |
| 91917 | + sqlite3SelectSetName(pNew, p->zSelName); |
| 91918 | + *pp = pNew; |
| 91919 | + pp = &pNew->pPrior; |
| 91920 | + pNext = pNew; |
| 91921 | + } |
| 91922 | + |
| 91923 | + return pRet; |
| 91714 | 91924 | } |
| 91715 | 91925 | #else |
| 91716 | 91926 | SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ |
| 91717 | 91927 | assert( p==0 ); |
| 91718 | 91928 | return 0; |
| | @@ -91773,11 +91983,11 @@ |
| 91773 | 91983 | ** |
| 91774 | 91984 | ** (a,b,c) = (expr1,expr2,expr3) |
| 91775 | 91985 | ** Or: (a,b,c) = (SELECT x,y,z FROM ....) |
| 91776 | 91986 | ** |
| 91777 | 91987 | ** For each term of the vector assignment, append new entries to the |
| 91778 | | -** expression list pList. In the case of a subquery on the LHS, append |
| 91988 | +** expression list pList. In the case of a subquery on the RHS, append |
| 91779 | 91989 | ** TK_SELECT_COLUMN expressions. |
| 91780 | 91990 | */ |
| 91781 | 91991 | SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector( |
| 91782 | 91992 | Parse *pParse, /* Parsing context */ |
| 91783 | 91993 | ExprList *pList, /* List to which to append. Might be NULL */ |
| | @@ -93882,10 +94092,15 @@ |
| 93882 | 94092 | int i; /* Loop counter */ |
| 93883 | 94093 | sqlite3 *db = pParse->db; /* The database connection */ |
| 93884 | 94094 | u8 enc = ENC(db); /* The text encoding used by this database */ |
| 93885 | 94095 | CollSeq *pColl = 0; /* A collating sequence */ |
| 93886 | 94096 | |
| 94097 | + if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){ |
| 94098 | + /* SQL functions can be expensive. So try to move constant functions |
| 94099 | + ** out of the inner loop, even if that means an extra OP_Copy. */ |
| 94100 | + return sqlite3ExprCodeAtInit(pParse, pExpr, -1); |
| 94101 | + } |
| 93887 | 94102 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 93888 | 94103 | if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
| 93889 | 94104 | pFarg = 0; |
| 93890 | 94105 | }else{ |
| 93891 | 94106 | pFarg = pExpr->x.pList; |
| | @@ -93929,10 +94144,26 @@ |
| 93929 | 94144 | */ |
| 93930 | 94145 | if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ |
| 93931 | 94146 | assert( nFarg>=1 ); |
| 93932 | 94147 | return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); |
| 93933 | 94148 | } |
| 94149 | + |
| 94150 | +#ifdef SQLITE_DEBUG |
| 94151 | + /* The AFFINITY() function evaluates to a string that describes |
| 94152 | + ** the type affinity of the argument. This is used for testing of |
| 94153 | + ** the SQLite type logic. |
| 94154 | + */ |
| 94155 | + if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){ |
| 94156 | + const char *azAff[] = { "blob", "text", "numeric", "integer", "real" }; |
| 94157 | + char aff; |
| 94158 | + assert( nFarg==1 ); |
| 94159 | + aff = sqlite3ExprAffinity(pFarg->a[0].pExpr); |
| 94160 | + sqlite3VdbeLoadString(v, target, |
| 94161 | + aff ? azAff[aff-SQLITE_AFF_BLOB] : "none"); |
| 94162 | + return target; |
| 94163 | + } |
| 94164 | +#endif |
| 93934 | 94165 | |
| 93935 | 94166 | for(i=0; i<nFarg; i++){ |
| 93936 | 94167 | if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ |
| 93937 | 94168 | testcase( i==31 ); |
| 93938 | 94169 | constMask |= MASKBIT32(i); |
| | @@ -94246,28 +94477,44 @@ |
| 94246 | 94477 | return inReg; |
| 94247 | 94478 | } |
| 94248 | 94479 | |
| 94249 | 94480 | /* |
| 94250 | 94481 | ** Factor out the code of the given expression to initialization time. |
| 94482 | +** |
| 94483 | +** If regDest>=0 then the result is always stored in that register and the |
| 94484 | +** result is not reusable. If regDest<0 then this routine is free to |
| 94485 | +** store the value whereever it wants. The register where the expression |
| 94486 | +** is stored is returned. When regDest<0, two identical expressions will |
| 94487 | +** code to the same register. |
| 94251 | 94488 | */ |
| 94252 | | -SQLITE_PRIVATE void sqlite3ExprCodeAtInit( |
| 94489 | +SQLITE_PRIVATE int sqlite3ExprCodeAtInit( |
| 94253 | 94490 | Parse *pParse, /* Parsing context */ |
| 94254 | 94491 | Expr *pExpr, /* The expression to code when the VDBE initializes */ |
| 94255 | | - int regDest, /* Store the value in this register */ |
| 94256 | | - u8 reusable /* True if this expression is reusable */ |
| 94492 | + int regDest /* Store the value in this register */ |
| 94257 | 94493 | ){ |
| 94258 | 94494 | ExprList *p; |
| 94259 | 94495 | assert( ConstFactorOk(pParse) ); |
| 94260 | 94496 | p = pParse->pConstExpr; |
| 94497 | + if( regDest<0 && p ){ |
| 94498 | + struct ExprList_item *pItem; |
| 94499 | + int i; |
| 94500 | + for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ |
| 94501 | + if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){ |
| 94502 | + return pItem->u.iConstExprReg; |
| 94503 | + } |
| 94504 | + } |
| 94505 | + } |
| 94261 | 94506 | pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); |
| 94262 | 94507 | p = sqlite3ExprListAppend(pParse, p, pExpr); |
| 94263 | 94508 | if( p ){ |
| 94264 | 94509 | struct ExprList_item *pItem = &p->a[p->nExpr-1]; |
| 94510 | + pItem->reusable = regDest<0; |
| 94511 | + if( regDest<0 ) regDest = ++pParse->nMem; |
| 94265 | 94512 | pItem->u.iConstExprReg = regDest; |
| 94266 | | - pItem->reusable = reusable; |
| 94267 | 94513 | } |
| 94268 | 94514 | pParse->pConstExpr = p; |
| 94515 | + return regDest; |
| 94269 | 94516 | } |
| 94270 | 94517 | |
| 94271 | 94518 | /* |
| 94272 | 94519 | ** Generate code to evaluate an expression and store the results |
| 94273 | 94520 | ** into a register. Return the register number where the results |
| | @@ -94286,23 +94533,12 @@ |
| 94286 | 94533 | pExpr = sqlite3ExprSkipCollate(pExpr); |
| 94287 | 94534 | if( ConstFactorOk(pParse) |
| 94288 | 94535 | && pExpr->op!=TK_REGISTER |
| 94289 | 94536 | && sqlite3ExprIsConstantNotJoin(pExpr) |
| 94290 | 94537 | ){ |
| 94291 | | - ExprList *p = pParse->pConstExpr; |
| 94292 | | - int i; |
| 94293 | 94538 | *pReg = 0; |
| 94294 | | - if( p ){ |
| 94295 | | - struct ExprList_item *pItem; |
| 94296 | | - for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ |
| 94297 | | - if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){ |
| 94298 | | - return pItem->u.iConstExprReg; |
| 94299 | | - } |
| 94300 | | - } |
| 94301 | | - } |
| 94302 | | - r2 = ++pParse->nMem; |
| 94303 | | - sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1); |
| 94539 | + r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1); |
| 94304 | 94540 | }else{ |
| 94305 | 94541 | int r1 = sqlite3GetTempReg(pParse); |
| 94306 | 94542 | r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
| 94307 | 94543 | if( r2==r1 ){ |
| 94308 | 94544 | *pReg = r1; |
| | @@ -94352,11 +94588,11 @@ |
| 94352 | 94588 | ** in register target. If the expression is constant, then this routine |
| 94353 | 94589 | ** might choose to code the expression at initialization time. |
| 94354 | 94590 | */ |
| 94355 | 94591 | SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ |
| 94356 | 94592 | if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ |
| 94357 | | - sqlite3ExprCodeAtInit(pParse, pExpr, target, 0); |
| 94593 | + sqlite3ExprCodeAtInit(pParse, pExpr, target); |
| 94358 | 94594 | }else{ |
| 94359 | 94595 | sqlite3ExprCode(pParse, pExpr, target); |
| 94360 | 94596 | } |
| 94361 | 94597 | } |
| 94362 | 94598 | |
| | @@ -94424,11 +94660,11 @@ |
| 94424 | 94660 | n--; |
| 94425 | 94661 | }else{ |
| 94426 | 94662 | sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); |
| 94427 | 94663 | } |
| 94428 | 94664 | }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ |
| 94429 | | - sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); |
| 94665 | + sqlite3ExprCodeAtInit(pParse, pExpr, target+i); |
| 94430 | 94666 | }else{ |
| 94431 | 94667 | int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); |
| 94432 | 94668 | if( inReg!=target+i ){ |
| 94433 | 94669 | VdbeOp *pOp; |
| 94434 | 94670 | if( copyOp==OP_Copy |
| | @@ -96968,10 +97204,16 @@ |
| 96968 | 97204 | ** used to query statistical information that has been gathered into |
| 96969 | 97205 | ** the Stat4Accum object by prior calls to stat_push(). The P parameter |
| 96970 | 97206 | ** has type BLOB but it is really just a pointer to the Stat4Accum object. |
| 96971 | 97207 | ** The content to returned is determined by the parameter J |
| 96972 | 97208 | ** which is one of the STAT_GET_xxxx values defined above. |
| 97209 | +** |
| 97210 | +** The stat_get(P,J) function is not available to generic SQL. It is |
| 97211 | +** inserted as part of a manually constructed bytecode program. (See |
| 97212 | +** the callStatGet() routine below.) It is guaranteed that the P |
| 97213 | +** parameter will always be a poiner to a Stat4Accum object, never a |
| 97214 | +** NULL. |
| 96973 | 97215 | ** |
| 96974 | 97216 | ** If neither STAT3 nor STAT4 are enabled, then J is always |
| 96975 | 97217 | ** STAT_GET_STAT1 and is hence omitted and this routine becomes |
| 96976 | 97218 | ** a one-parameter function, stat_get(P), that always returns the |
| 96977 | 97219 | ** stat1 table entry information. |
| | @@ -97787,11 +98029,11 @@ |
| 97787 | 98029 | sumEq += aSample[i].anEq[iCol]; |
| 97788 | 98030 | nSum100 += 100; |
| 97789 | 98031 | } |
| 97790 | 98032 | } |
| 97791 | 98033 | |
| 97792 | | - if( nDist100>nSum100 ){ |
| 98034 | + if( nDist100>nSum100 && sumEq<nRow ){ |
| 97793 | 98035 | avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100); |
| 97794 | 98036 | } |
| 97795 | 98037 | if( avgEq==0 ) avgEq = 1; |
| 97796 | 98038 | pIdx->aAvgEq[iCol] = avgEq; |
| 97797 | 98039 | } |
| | @@ -98201,10 +98443,11 @@ |
| 98201 | 98443 | assert( pVfs ); |
| 98202 | 98444 | flags |= SQLITE_OPEN_MAIN_DB; |
| 98203 | 98445 | rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags); |
| 98204 | 98446 | sqlite3_free( zPath ); |
| 98205 | 98447 | db->nDb++; |
| 98448 | + db->skipBtreeMutex = 0; |
| 98206 | 98449 | if( rc==SQLITE_CONSTRAINT ){ |
| 98207 | 98450 | rc = SQLITE_ERROR; |
| 98208 | 98451 | zErrDyn = sqlite3MPrintf(db, "database is already attached"); |
| 98209 | 98452 | }else if( rc==SQLITE_OK ){ |
| 98210 | 98453 | Pager *pPager; |
| | @@ -104365,16 +104608,12 @@ |
| 104365 | 104608 | } |
| 104366 | 104609 | }else |
| 104367 | 104610 | #endif |
| 104368 | 104611 | { |
| 104369 | 104612 | int count = (pParse->nested==0); /* True to count changes */ |
| 104370 | | - int iIdxNoSeek = -1; |
| 104371 | | - if( bComplex==0 && aiCurOnePass[1]!=iDataCur ){ |
| 104372 | | - iIdxNoSeek = aiCurOnePass[1]; |
| 104373 | | - } |
| 104374 | 104613 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
| 104375 | | - iKey, nKey, count, OE_Default, eOnePass, iIdxNoSeek); |
| 104614 | + iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]); |
| 104376 | 104615 | } |
| 104377 | 104616 | |
| 104378 | 104617 | /* End of the loop over all rowids/primary-keys. */ |
| 104379 | 104618 | if( eOnePass!=ONEPASS_OFF ){ |
| 104380 | 104619 | sqlite3VdbeResolveLabel(v, addrBypass); |
| | @@ -104450,19 +104689,21 @@ |
| 104450 | 104689 | ** then this function must seek iDataCur to the entry identified by iPk |
| 104451 | 104690 | ** and nPk before reading from it. |
| 104452 | 104691 | ** |
| 104453 | 104692 | ** If eMode is ONEPASS_MULTI, then this call is being made as part |
| 104454 | 104693 | ** of a ONEPASS delete that affects multiple rows. In this case, if |
| 104455 | | -** iIdxNoSeek is a valid cursor number (>=0), then its position should |
| 104456 | | -** be preserved following the delete operation. Or, if iIdxNoSeek is not |
| 104457 | | -** a valid cursor number, the position of iDataCur should be preserved |
| 104458 | | -** instead. |
| 104694 | +** iIdxNoSeek is a valid cursor number (>=0) and is not the same as |
| 104695 | +** iDataCur, then its position should be preserved following the delete |
| 104696 | +** operation. Or, if iIdxNoSeek is not a valid cursor number, the |
| 104697 | +** position of iDataCur should be preserved instead. |
| 104459 | 104698 | ** |
| 104460 | 104699 | ** iIdxNoSeek: |
| 104461 | | -** If iIdxNoSeek is a valid cursor number (>=0), then it identifies an |
| 104462 | | -** index cursor (from within array of cursors starting at iIdxCur) that |
| 104463 | | -** already points to the index entry to be deleted. |
| 104700 | +** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur, |
| 104701 | +** then it identifies an index cursor (from within array of cursors |
| 104702 | +** starting at iIdxCur) that already points to the index entry to be deleted. |
| 104703 | +** Except, this optimization is disabled if there are BEFORE triggers since |
| 104704 | +** the trigger body might have moved the cursor. |
| 104464 | 104705 | */ |
| 104465 | 104706 | SQLITE_PRIVATE void sqlite3GenerateRowDelete( |
| 104466 | 104707 | Parse *pParse, /* Parsing context */ |
| 104467 | 104708 | Table *pTab, /* Table containing the row to be deleted */ |
| 104468 | 104709 | Trigger *pTrigger, /* List of triggers to (potentially) fire */ |
| | @@ -104529,17 +104770,22 @@ |
| 104529 | 104770 | TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel |
| 104530 | 104771 | ); |
| 104531 | 104772 | |
| 104532 | 104773 | /* If any BEFORE triggers were coded, then seek the cursor to the |
| 104533 | 104774 | ** row to be deleted again. It may be that the BEFORE triggers moved |
| 104534 | | - ** the cursor or of already deleted the row that the cursor was |
| 104775 | + ** the cursor or already deleted the row that the cursor was |
| 104535 | 104776 | ** pointing to. |
| 104777 | + ** |
| 104778 | + ** Also disable the iIdxNoSeek optimization since the BEFORE trigger |
| 104779 | + ** may have moved that cursor. |
| 104536 | 104780 | */ |
| 104537 | 104781 | if( addrStart<sqlite3VdbeCurrentAddr(v) ){ |
| 104538 | 104782 | sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk); |
| 104539 | 104783 | VdbeCoverageIf(v, opSeek==OP_NotExists); |
| 104540 | 104784 | VdbeCoverageIf(v, opSeek==OP_NotFound); |
| 104785 | + testcase( iIdxNoSeek>=0 ); |
| 104786 | + iIdxNoSeek = -1; |
| 104541 | 104787 | } |
| 104542 | 104788 | |
| 104543 | 104789 | /* Do FK processing. This call checks that any FK constraints that |
| 104544 | 104790 | ** refer to this table (i.e. constraints attached to other tables) |
| 104545 | 104791 | ** are not violated by deleting this row. */ |
| | @@ -104558,15 +104804,17 @@ |
| 104558 | 104804 | */ |
| 104559 | 104805 | if( pTab->pSelect==0 ){ |
| 104560 | 104806 | u8 p5 = 0; |
| 104561 | 104807 | sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek); |
| 104562 | 104808 | sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0)); |
| 104563 | | - sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE); |
| 104809 | + if( pParse->nested==0 ){ |
| 104810 | + sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE); |
| 104811 | + } |
| 104564 | 104812 | if( eMode!=ONEPASS_OFF ){ |
| 104565 | 104813 | sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE); |
| 104566 | 104814 | } |
| 104567 | | - if( iIdxNoSeek>=0 ){ |
| 104815 | + if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){ |
| 104568 | 104816 | sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek); |
| 104569 | 104817 | } |
| 104570 | 104818 | if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION; |
| 104571 | 104819 | sqlite3VdbeChangeP5(v, p5); |
| 104572 | 104820 | } |
| | @@ -104716,10 +104964,14 @@ |
| 104716 | 104964 | ** opcode if it is present */ |
| 104717 | 104965 | sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity); |
| 104718 | 104966 | } |
| 104719 | 104967 | if( regOut ){ |
| 104720 | 104968 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut); |
| 104969 | + if( pIdx->pTable->pSelect ){ |
| 104970 | + const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx); |
| 104971 | + sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT); |
| 104972 | + } |
| 104721 | 104973 | } |
| 104722 | 104974 | sqlite3ReleaseTempRange(pParse, regBase, nCol); |
| 104723 | 104975 | return regBase; |
| 104724 | 104976 | } |
| 104725 | 104977 | |
| | @@ -106512,10 +106764,13 @@ |
| 106512 | 106764 | DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), |
| 106513 | 106765 | #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 106514 | 106766 | FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), |
| 106515 | 106767 | FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), |
| 106516 | 106768 | FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY), |
| 106769 | +#ifdef SQLITE_DEBUG |
| 106770 | + FUNCTION2(affinity, 1, 0, 0, noopFunc, SQLITE_FUNC_AFFINITY), |
| 106771 | +#endif |
| 106517 | 106772 | FUNCTION(ltrim, 1, 1, 0, trimFunc ), |
| 106518 | 106773 | FUNCTION(ltrim, 2, 1, 0, trimFunc ), |
| 106519 | 106774 | FUNCTION(rtrim, 1, 2, 0, trimFunc ), |
| 106520 | 106775 | FUNCTION(rtrim, 2, 2, 0, trimFunc ), |
| 106521 | 106776 | FUNCTION(trim, 1, 3, 0, trimFunc ), |
| | @@ -109667,11 +109922,11 @@ |
| 109667 | 109922 | if( db->flags&SQLITE_RecTriggers ){ |
| 109668 | 109923 | pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); |
| 109669 | 109924 | } |
| 109670 | 109925 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
| 109671 | 109926 | regR, nPkField, 0, OE_Replace, |
| 109672 | | - (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), -1); |
| 109927 | + (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur); |
| 109673 | 109928 | seenReplace = 1; |
| 109674 | 109929 | break; |
| 109675 | 109930 | } |
| 109676 | 109931 | } |
| 109677 | 109932 | sqlite3VdbeResolveLabel(v, addrUniqueOk); |
| | @@ -109683,10 +109938,29 @@ |
| 109683 | 109938 | } |
| 109684 | 109939 | |
| 109685 | 109940 | *pbMayReplace = seenReplace; |
| 109686 | 109941 | VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace)); |
| 109687 | 109942 | } |
| 109943 | + |
| 109944 | +#ifdef SQLITE_ENABLE_NULL_TRIM |
| 109945 | +/* |
| 109946 | +** Change the P5 operand on the last opcode (which should be an OP_MakeRecord) |
| 109947 | +** to be the number of columns in table pTab that must not be NULL-trimmed. |
| 109948 | +** |
| 109949 | +** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero. |
| 109950 | +*/ |
| 109951 | +SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){ |
| 109952 | + u16 i; |
| 109953 | + |
| 109954 | + /* Records with omitted columns are only allowed for schema format |
| 109955 | + ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */ |
| 109956 | + if( pTab->pSchema->file_format<2 ) return; |
| 109957 | + |
| 109958 | + for(i=pTab->nCol; i>1 && pTab->aCol[i-1].pDflt==0; i--){} |
| 109959 | + sqlite3VdbeChangeP5(v, i); |
| 109960 | +} |
| 109961 | +#endif |
| 109688 | 109962 | |
| 109689 | 109963 | /* |
| 109690 | 109964 | ** This routine generates code to finish the INSERT or UPDATE operation |
| 109691 | 109965 | ** that was started by a prior call to sqlite3GenerateConstraintChecks. |
| 109692 | 109966 | ** A consecutive range of registers starting at regNewData contains the |
| | @@ -109700,11 +109974,11 @@ |
| 109700 | 109974 | Table *pTab, /* the table into which we are inserting */ |
| 109701 | 109975 | int iDataCur, /* Cursor of the canonical data source */ |
| 109702 | 109976 | int iIdxCur, /* First index cursor */ |
| 109703 | 109977 | int regNewData, /* Range of content */ |
| 109704 | 109978 | int *aRegIdx, /* Register used by each index. 0 for unused indices */ |
| 109705 | | - int isUpdate, /* True for UPDATE, False for INSERT */ |
| 109979 | + int update_flags, /* True for UPDATE, False for INSERT */ |
| 109706 | 109980 | int appendBias, /* True if this is likely to be an append */ |
| 109707 | 109981 | int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */ |
| 109708 | 109982 | ){ |
| 109709 | 109983 | Vdbe *v; /* Prepared statements under construction */ |
| 109710 | 109984 | Index *pIdx; /* An index being inserted or updated */ |
| | @@ -109711,10 +109985,15 @@ |
| 109711 | 109985 | u8 pik_flags; /* flag values passed to the btree insert */ |
| 109712 | 109986 | int regData; /* Content registers (after the rowid) */ |
| 109713 | 109987 | int regRec; /* Register holding assembled record for the table */ |
| 109714 | 109988 | int i; /* Loop counter */ |
| 109715 | 109989 | u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */ |
| 109990 | + |
| 109991 | + assert( update_flags==0 |
| 109992 | + || update_flags==OPFLAG_ISUPDATE |
| 109993 | + || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION) |
| 109994 | + ); |
| 109716 | 109995 | |
| 109717 | 109996 | v = sqlite3GetVdbe(pParse); |
| 109718 | 109997 | assert( v!=0 ); |
| 109719 | 109998 | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
| 109720 | 109999 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| | @@ -109722,34 +110001,43 @@ |
| 109722 | 110001 | bAffinityDone = 1; |
| 109723 | 110002 | if( pIdx->pPartIdxWhere ){ |
| 109724 | 110003 | sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2); |
| 109725 | 110004 | VdbeCoverage(v); |
| 109726 | 110005 | } |
| 109727 | | - sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i], |
| 109728 | | - aRegIdx[i]+1, |
| 109729 | | - pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn); |
| 109730 | | - pik_flags = 0; |
| 109731 | | - if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT; |
| 110006 | + pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0); |
| 109732 | 110007 | if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ |
| 109733 | 110008 | assert( pParse->nested==0 ); |
| 109734 | 110009 | pik_flags |= OPFLAG_NCHANGE; |
| 110010 | + pik_flags |= (update_flags & OPFLAG_SAVEPOSITION); |
| 110011 | +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 110012 | + if( update_flags==0 ){ |
| 110013 | + sqlite3VdbeAddOp4(v, OP_InsertInt, |
| 110014 | + iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE |
| 110015 | + ); |
| 110016 | + sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP); |
| 110017 | + } |
| 110018 | +#endif |
| 109735 | 110019 | } |
| 110020 | + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i], |
| 110021 | + aRegIdx[i]+1, |
| 110022 | + pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn); |
| 109736 | 110023 | sqlite3VdbeChangeP5(v, pik_flags); |
| 109737 | 110024 | } |
| 109738 | 110025 | if( !HasRowid(pTab) ) return; |
| 109739 | 110026 | regData = regNewData + 1; |
| 109740 | 110027 | regRec = sqlite3GetTempReg(pParse); |
| 109741 | 110028 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); |
| 110029 | + sqlite3SetMakeRecordP5(v, pTab); |
| 109742 | 110030 | if( !bAffinityDone ){ |
| 109743 | 110031 | sqlite3TableAffinity(v, pTab, 0); |
| 109744 | 110032 | sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol); |
| 109745 | 110033 | } |
| 109746 | 110034 | if( pParse->nested ){ |
| 109747 | 110035 | pik_flags = 0; |
| 109748 | 110036 | }else{ |
| 109749 | 110037 | pik_flags = OPFLAG_NCHANGE; |
| 109750 | | - pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); |
| 110038 | + pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID); |
| 109751 | 110039 | } |
| 109752 | 110040 | if( appendBias ){ |
| 109753 | 110041 | pik_flags |= OPFLAG_APPEND; |
| 109754 | 110042 | } |
| 109755 | 110043 | if( useSeekResult ){ |
| | @@ -110154,11 +110442,11 @@ |
| 110154 | 110442 | addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); |
| 110155 | 110443 | }else{ |
| 110156 | 110444 | addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); |
| 110157 | 110445 | assert( (pDest->tabFlags & TF_Autoincrement)==0 ); |
| 110158 | 110446 | } |
| 110159 | | - sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); |
| 110447 | + sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1); |
| 110160 | 110448 | if( db->flags & SQLITE_Vacuum ){ |
| 110161 | 110449 | sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1); |
| 110162 | 110450 | insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID| |
| 110163 | 110451 | OPFLAG_APPEND|OPFLAG_USESEEKRESULT; |
| 110164 | 110452 | }else{ |
| | @@ -110186,11 +110474,11 @@ |
| 110186 | 110474 | sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest); |
| 110187 | 110475 | sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); |
| 110188 | 110476 | sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); |
| 110189 | 110477 | VdbeComment((v, "%s", pDestIdx->zName)); |
| 110190 | 110478 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
| 110191 | | - sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); |
| 110479 | + sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1); |
| 110192 | 110480 | if( db->flags & SQLITE_Vacuum ){ |
| 110193 | 110481 | /* This INSERT command is part of a VACUUM operation, which guarantees |
| 110194 | 110482 | ** that the destination table is empty. If all indexed columns use |
| 110195 | 110483 | ** collation sequence BINARY, then it can also be assumed that the |
| 110196 | 110484 | ** index will be populated by inserting keys in strictly sorted |
| | @@ -110971,11 +111259,10 @@ |
| 110971 | 111259 | #endif /* SQLITE3EXT_H */ |
| 110972 | 111260 | |
| 110973 | 111261 | /************** End of sqlite3ext.h ******************************************/ |
| 110974 | 111262 | /************** Continuing where we left off in loadext.c ********************/ |
| 110975 | 111263 | /* #include "sqliteInt.h" */ |
| 110976 | | -/* #include <string.h> */ |
| 110977 | 111264 | |
| 110978 | 111265 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 110979 | 111266 | /* |
| 110980 | 111267 | ** Some API routines are omitted when various features are |
| 110981 | 111268 | ** excluded from a build of SQLite. Substitute a NULL pointer |
| | @@ -112635,11 +112922,11 @@ |
| 112635 | 112922 | |
| 112636 | 112923 | /* |
| 112637 | 112924 | ** Locate a pragma in the aPragmaName[] array. |
| 112638 | 112925 | */ |
| 112639 | 112926 | static const PragmaName *pragmaLocate(const char *zName){ |
| 112640 | | - int upr, lwr, mid, rc; |
| 112927 | + int upr, lwr, mid = 0, rc; |
| 112641 | 112928 | lwr = 0; |
| 112642 | 112929 | upr = ArraySize(aPragmaName)-1; |
| 112643 | 112930 | while( lwr<=upr ){ |
| 112644 | 112931 | mid = (lwr+upr)/2; |
| 112645 | 112932 | rc = sqlite3_stricmp(zName, aPragmaName[mid].zName); |
| | @@ -116149,10 +116436,11 @@ |
| 116149 | 116436 | v = pParse->pVdbe; |
| 116150 | 116437 | r1 = sqlite3GetTempReg(pParse); |
| 116151 | 116438 | sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v); |
| 116152 | 116439 | sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1); |
| 116153 | 116440 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N); |
| 116441 | + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 116154 | 116442 | sqlite3ReleaseTempReg(pParse, r1); |
| 116155 | 116443 | } |
| 116156 | 116444 | |
| 116157 | 116445 | /* |
| 116158 | 116446 | ** This routine generates the code for the inside of the inner loop |
| | @@ -119677,11 +119965,19 @@ |
| 119677 | 119965 | assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 )); |
| 119678 | 119966 | |
| 119679 | 119967 | pCte->zCteErr = "circular reference: %s"; |
| 119680 | 119968 | pSavedWith = pParse->pWith; |
| 119681 | 119969 | pParse->pWith = pWith; |
| 119682 | | - sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel); |
| 119970 | + if( bMayRecursive ){ |
| 119971 | + Select *pPrior = pSel->pPrior; |
| 119972 | + assert( pPrior->pWith==0 ); |
| 119973 | + pPrior->pWith = pSel->pWith; |
| 119974 | + sqlite3WalkSelect(pWalker, pPrior); |
| 119975 | + pPrior->pWith = 0; |
| 119976 | + }else{ |
| 119977 | + sqlite3WalkSelect(pWalker, pSel); |
| 119978 | + } |
| 119683 | 119979 | pParse->pWith = pWith; |
| 119684 | 119980 | |
| 119685 | 119981 | for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior); |
| 119686 | 119982 | pEList = pLeft->pEList; |
| 119687 | 119983 | if( pCte->pCols ){ |
| | @@ -119721,14 +120017,16 @@ |
| 119721 | 120017 | ** sqlite3SelectExpand() when walking a SELECT tree to resolve table |
| 119722 | 120018 | ** names and other FROM clause elements. |
| 119723 | 120019 | */ |
| 119724 | 120020 | static void selectPopWith(Walker *pWalker, Select *p){ |
| 119725 | 120021 | Parse *pParse = pWalker->pParse; |
| 119726 | | - With *pWith = findRightmost(p)->pWith; |
| 119727 | | - if( pWith!=0 ){ |
| 119728 | | - assert( pParse->pWith==pWith ); |
| 119729 | | - pParse->pWith = pWith->pOuter; |
| 120022 | + if( pParse->pWith && p->pPrior==0 ){ |
| 120023 | + With *pWith = findRightmost(p)->pWith; |
| 120024 | + if( pWith!=0 ){ |
| 120025 | + assert( pParse->pWith==pWith ); |
| 120026 | + pParse->pWith = pWith->pOuter; |
| 120027 | + } |
| 119730 | 120028 | } |
| 119731 | 120029 | } |
| 119732 | 120030 | #else |
| 119733 | 120031 | #define selectPopWith 0 |
| 119734 | 120032 | #endif |
| | @@ -119774,12 +120072,12 @@ |
| 119774 | 120072 | if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){ |
| 119775 | 120073 | return WRC_Prune; |
| 119776 | 120074 | } |
| 119777 | 120075 | pTabList = p->pSrc; |
| 119778 | 120076 | pEList = p->pEList; |
| 119779 | | - if( pWalker->xSelectCallback2==selectPopWith ){ |
| 119780 | | - sqlite3WithPush(pParse, findRightmost(p)->pWith, 0); |
| 120077 | + if( p->pWith ){ |
| 120078 | + sqlite3WithPush(pParse, p->pWith, 0); |
| 119781 | 120079 | } |
| 119782 | 120080 | |
| 119783 | 120081 | /* Make sure cursor numbers have been assigned to all entries in |
| 119784 | 120082 | ** the FROM clause of the SELECT statement. |
| 119785 | 120083 | */ |
| | @@ -120062,13 +120360,11 @@ |
| 120062 | 120360 | if( pParse->hasCompound ){ |
| 120063 | 120361 | w.xSelectCallback = convertCompoundSelectToSubquery; |
| 120064 | 120362 | sqlite3WalkSelect(&w, pSelect); |
| 120065 | 120363 | } |
| 120066 | 120364 | w.xSelectCallback = selectExpander; |
| 120067 | | - if( (pSelect->selFlags & SF_MultiValue)==0 ){ |
| 120068 | | - w.xSelectCallback2 = selectPopWith; |
| 120069 | | - } |
| 120365 | + w.xSelectCallback2 = selectPopWith; |
| 120070 | 120366 | sqlite3WalkSelect(&w, pSelect); |
| 120071 | 120367 | } |
| 120072 | 120368 | |
| 120073 | 120369 | |
| 120074 | 120370 | #ifndef SQLITE_OMIT_SUBQUERY |
| | @@ -121143,11 +121439,11 @@ |
| 121143 | 121439 | /* This case runs if the aggregate has no GROUP BY clause. The |
| 121144 | 121440 | ** processing is much simpler since there is only a single row |
| 121145 | 121441 | ** of output. |
| 121146 | 121442 | */ |
| 121147 | 121443 | resetAccumulator(pParse, &sAggInfo); |
| 121148 | | - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0); |
| 121444 | + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax, 0,flag,0); |
| 121149 | 121445 | if( pWInfo==0 ){ |
| 121150 | 121446 | sqlite3ExprListDelete(db, pDel); |
| 121151 | 121447 | goto select_end; |
| 121152 | 121448 | } |
| 121153 | 121449 | updateAccumulator(pParse, &sAggInfo); |
| | @@ -121232,12 +121528,10 @@ |
| 121232 | 121528 | ** |
| 121233 | 121529 | ** These routines are in a separate files so that they will not be linked |
| 121234 | 121530 | ** if they are not used. |
| 121235 | 121531 | */ |
| 121236 | 121532 | /* #include "sqliteInt.h" */ |
| 121237 | | -/* #include <stdlib.h> */ |
| 121238 | | -/* #include <string.h> */ |
| 121239 | 121533 | |
| 121240 | 121534 | #ifndef SQLITE_OMIT_GET_TABLE |
| 121241 | 121535 | |
| 121242 | 121536 | /* |
| 121243 | 121537 | ** This structure is used to pass data from sqlite3_get_table() through |
| | @@ -122591,16 +122885,16 @@ |
| 122591 | 122885 | sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, |
| 122592 | 122886 | pCol->affinity, &pValue); |
| 122593 | 122887 | if( pValue ){ |
| 122594 | 122888 | sqlite3VdbeAppendP4(v, pValue, P4_MEM); |
| 122595 | 122889 | } |
| 122890 | + } |
| 122596 | 122891 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 122597 | | - if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ |
| 122598 | | - sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
| 122599 | | - } |
| 122892 | + if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ |
| 122893 | + sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
| 122894 | + } |
| 122600 | 122895 | #endif |
| 122601 | | - } |
| 122602 | 122896 | } |
| 122603 | 122897 | |
| 122604 | 122898 | /* |
| 122605 | 122899 | ** Process an UPDATE statement. |
| 122606 | 122900 | ** |
| | @@ -122625,11 +122919,11 @@ |
| 122625 | 122919 | int nIdx; /* Number of indices that need updating */ |
| 122626 | 122920 | int iBaseCur; /* Base cursor number */ |
| 122627 | 122921 | int iDataCur; /* Cursor for the canonical data btree */ |
| 122628 | 122922 | int iIdxCur; /* Cursor for the first index */ |
| 122629 | 122923 | sqlite3 *db; /* The database structure */ |
| 122630 | | - int *aRegIdx = 0; /* One register assigned to each index to be updated */ |
| 122924 | + int *aRegIdx = 0; /* First register in array assigned to each index */ |
| 122631 | 122925 | int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the |
| 122632 | 122926 | ** an expression for the i-th column of the table. |
| 122633 | 122927 | ** aXRef[i]==-1 if the i-th column is not changed. */ |
| 122634 | 122928 | u8 *aToOpen; /* 1 for tables and indices to be opened */ |
| 122635 | 122929 | u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */ |
| | @@ -122637,14 +122931,15 @@ |
| 122637 | 122931 | u8 chngKey; /* Either chngPk or chngRowid */ |
| 122638 | 122932 | Expr *pRowidExpr = 0; /* Expression defining the new record number */ |
| 122639 | 122933 | AuthContext sContext; /* The authorization context */ |
| 122640 | 122934 | NameContext sNC; /* The name-context to resolve expressions in */ |
| 122641 | 122935 | int iDb; /* Database containing the table being updated */ |
| 122642 | | - int okOnePass; /* True for one-pass algorithm without the FIFO */ |
| 122936 | + int eOnePass; /* ONEPASS_XXX value from where.c */ |
| 122643 | 122937 | int hasFK; /* True if foreign key processing is required */ |
| 122644 | 122938 | int labelBreak; /* Jump here to break out of UPDATE loop */ |
| 122645 | 122939 | int labelContinue; /* Jump here to continue next step of UPDATE loop */ |
| 122940 | + int flags; /* Flags for sqlite3WhereBegin() */ |
| 122646 | 122941 | |
| 122647 | 122942 | #ifndef SQLITE_OMIT_TRIGGER |
| 122648 | 122943 | int isView; /* True when updating a view (INSTEAD OF trigger) */ |
| 122649 | 122944 | Trigger *pTrigger; /* List of triggers on pTab, if required */ |
| 122650 | 122945 | int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ |
| | @@ -122651,10 +122946,14 @@ |
| 122651 | 122946 | #endif |
| 122652 | 122947 | int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */ |
| 122653 | 122948 | int iEph = 0; /* Ephemeral table holding all primary key values */ |
| 122654 | 122949 | int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */ |
| 122655 | 122950 | int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */ |
| 122951 | + int addrOpen = 0; /* Address of OP_OpenEphemeral */ |
| 122952 | + int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */ |
| 122953 | + i16 nPk = 0; /* Number of components of the PRIMARY KEY */ |
| 122954 | + int bReplace = 0; /* True if REPLACE conflict resolution might happen */ |
| 122656 | 122955 | |
| 122657 | 122956 | /* Register Allocations */ |
| 122658 | 122957 | int regRowCount = 0; /* A count of rows changed */ |
| 122659 | 122958 | int regOldRowid = 0; /* The old rowid */ |
| 122660 | 122959 | int regNewRowid = 0; /* The new rowid */ |
| | @@ -122810,17 +123109,27 @@ |
| 122810 | 123109 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 122811 | 123110 | i16 iIdxCol = pIdx->aiColumn[i]; |
| 122812 | 123111 | if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){ |
| 122813 | 123112 | reg = ++pParse->nMem; |
| 122814 | 123113 | pParse->nMem += pIdx->nColumn; |
| 123114 | + if( (onError==OE_Replace) |
| 123115 | + || (onError==OE_Default && pIdx->onError==OE_Replace) |
| 123116 | + ){ |
| 123117 | + bReplace = 1; |
| 123118 | + } |
| 122815 | 123119 | break; |
| 122816 | 123120 | } |
| 122817 | 123121 | } |
| 122818 | 123122 | } |
| 122819 | 123123 | if( reg==0 ) aToOpen[j+1] = 0; |
| 122820 | 123124 | aRegIdx[j] = reg; |
| 122821 | 123125 | } |
| 123126 | + if( bReplace ){ |
| 123127 | + /* If REPLACE conflict resolution might be invoked, open cursors on all |
| 123128 | + ** indexes in case they are needed to delete records. */ |
| 123129 | + memset(aToOpen, 1, nIdx+1); |
| 123130 | + } |
| 122822 | 123131 | |
| 122823 | 123132 | /* Begin generating code. */ |
| 122824 | 123133 | v = sqlite3GetVdbe(pParse); |
| 122825 | 123134 | if( v==0 ) goto update_cleanup; |
| 122826 | 123135 | if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
| | @@ -122869,107 +123178,127 @@ |
| 122869 | 123178 | pWhere, onError); |
| 122870 | 123179 | goto update_cleanup; |
| 122871 | 123180 | } |
| 122872 | 123181 | #endif |
| 122873 | 123182 | |
| 122874 | | - /* Begin the database scan |
| 122875 | | - */ |
| 123183 | + /* Initialize the count of updated rows */ |
| 123184 | + if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){ |
| 123185 | + regRowCount = ++pParse->nMem; |
| 123186 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); |
| 123187 | + } |
| 123188 | + |
| 122876 | 123189 | if( HasRowid(pTab) ){ |
| 122877 | 123190 | sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid); |
| 122878 | | - pWInfo = sqlite3WhereBegin( |
| 122879 | | - pParse, pTabList, pWhere, 0, 0, |
| 122880 | | - WHERE_ONEPASS_DESIRED | WHERE_SEEK_TABLE, iIdxCur |
| 122881 | | - ); |
| 122882 | | - if( pWInfo==0 ) goto update_cleanup; |
| 122883 | | - okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); |
| 122884 | | - |
| 122885 | | - /* Remember the rowid of every item to be updated. |
| 122886 | | - */ |
| 122887 | | - sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid); |
| 122888 | | - if( !okOnePass ){ |
| 122889 | | - sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid); |
| 122890 | | - } |
| 122891 | | - |
| 122892 | | - /* End the database scan loop. |
| 122893 | | - */ |
| 122894 | | - sqlite3WhereEnd(pWInfo); |
| 122895 | 123191 | }else{ |
| 122896 | | - int iPk; /* First of nPk memory cells holding PRIMARY KEY value */ |
| 122897 | | - i16 nPk; /* Number of components of the PRIMARY KEY */ |
| 122898 | | - int addrOpen; /* Address of the OpenEphemeral instruction */ |
| 122899 | | - |
| 122900 | 123192 | assert( pPk!=0 ); |
| 122901 | 123193 | nPk = pPk->nKeyCol; |
| 122902 | 123194 | iPk = pParse->nMem+1; |
| 122903 | 123195 | pParse->nMem += nPk; |
| 122904 | 123196 | regKey = ++pParse->nMem; |
| 122905 | 123197 | iEph = pParse->nTab++; |
| 123198 | + |
| 122906 | 123199 | sqlite3VdbeAddOp2(v, OP_Null, 0, iPk); |
| 122907 | 123200 | addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk); |
| 122908 | 123201 | sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 122909 | | - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, |
| 122910 | | - WHERE_ONEPASS_DESIRED, iIdxCur); |
| 122911 | | - if( pWInfo==0 ) goto update_cleanup; |
| 122912 | | - okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); |
| 123202 | + } |
| 123203 | + |
| 123204 | + /* Begin the database scan. |
| 123205 | + ** |
| 123206 | + ** Do not consider a single-pass strategy for a multi-row update if |
| 123207 | + ** there are any triggers or foreign keys to process, or rows may |
| 123208 | + ** be deleted as a result of REPLACE conflict handling. Any of these |
| 123209 | + ** things might disturb a cursor being used to scan through the table |
| 123210 | + ** or index, causing a single-pass approach to malfunction. */ |
| 123211 | + flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE; |
| 123212 | + if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){ |
| 123213 | + flags |= WHERE_ONEPASS_MULTIROW; |
| 123214 | + } |
| 123215 | + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur); |
| 123216 | + if( pWInfo==0 ) goto update_cleanup; |
| 123217 | + |
| 123218 | + /* A one-pass strategy that might update more than one row may not |
| 123219 | + ** be used if any column of the index used for the scan is being |
| 123220 | + ** updated. Otherwise, if there is an index on "b", statements like |
| 123221 | + ** the following could create an infinite loop: |
| 123222 | + ** |
| 123223 | + ** UPDATE t1 SET b=b+1 WHERE b>? |
| 123224 | + ** |
| 123225 | + ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI |
| 123226 | + ** strategy that uses an index for which one or more columns are being |
| 123227 | + ** updated. */ |
| 123228 | + eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); |
| 123229 | + if( eOnePass==ONEPASS_MULTI ){ |
| 123230 | + int iCur = aiCurOnePass[1]; |
| 123231 | + if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){ |
| 123232 | + eOnePass = ONEPASS_OFF; |
| 123233 | + } |
| 123234 | + assert( iCur!=iDataCur || !HasRowid(pTab) ); |
| 123235 | + } |
| 123236 | + |
| 123237 | + if( HasRowid(pTab) ){ |
| 123238 | + /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF |
| 123239 | + ** mode, write the rowid into the FIFO. In either of the one-pass modes, |
| 123240 | + ** leave it in register regOldRowid. */ |
| 123241 | + sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid); |
| 123242 | + if( eOnePass==ONEPASS_OFF ){ |
| 123243 | + sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid); |
| 123244 | + } |
| 123245 | + }else{ |
| 123246 | + /* Read the PK of the current row into an array of registers. In |
| 123247 | + ** ONEPASS_OFF mode, serialize the array into a record and store it in |
| 123248 | + ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change |
| 123249 | + ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table |
| 123250 | + ** is not required) and leave the PK fields in the array of registers. */ |
| 122913 | 123251 | for(i=0; i<nPk; i++){ |
| 122914 | 123252 | assert( pPk->aiColumn[i]>=0 ); |
| 122915 | | - sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i], |
| 122916 | | - iPk+i); |
| 123253 | + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i); |
| 122917 | 123254 | } |
| 122918 | | - if( okOnePass ){ |
| 123255 | + if( eOnePass ){ |
| 122919 | 123256 | sqlite3VdbeChangeToNoop(v, addrOpen); |
| 122920 | 123257 | nKey = nPk; |
| 122921 | 123258 | regKey = iPk; |
| 122922 | 123259 | }else{ |
| 122923 | 123260 | sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey, |
| 122924 | 123261 | sqlite3IndexAffinityStr(db, pPk), nPk); |
| 122925 | 123262 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk); |
| 122926 | 123263 | } |
| 122927 | | - sqlite3WhereEnd(pWInfo); |
| 122928 | 123264 | } |
| 122929 | 123265 | |
| 122930 | | - /* Initialize the count of updated rows |
| 122931 | | - */ |
| 122932 | | - if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){ |
| 122933 | | - regRowCount = ++pParse->nMem; |
| 122934 | | - sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); |
| 123266 | + if( eOnePass!=ONEPASS_MULTI ){ |
| 123267 | + sqlite3WhereEnd(pWInfo); |
| 122935 | 123268 | } |
| 122936 | 123269 | |
| 122937 | 123270 | labelBreak = sqlite3VdbeMakeLabel(v); |
| 122938 | 123271 | if( !isView ){ |
| 122939 | | - /* |
| 122940 | | - ** Open every index that needs updating. Note that if any |
| 122941 | | - ** index could potentially invoke a REPLACE conflict resolution |
| 122942 | | - ** action, then we need to open all indices because we might need |
| 122943 | | - ** to be deleting some records. |
| 122944 | | - */ |
| 122945 | | - if( onError==OE_Replace ){ |
| 122946 | | - memset(aToOpen, 1, nIdx+1); |
| 122947 | | - }else{ |
| 122948 | | - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 122949 | | - if( pIdx->onError==OE_Replace ){ |
| 122950 | | - memset(aToOpen, 1, nIdx+1); |
| 122951 | | - break; |
| 122952 | | - } |
| 122953 | | - } |
| 122954 | | - } |
| 122955 | | - if( okOnePass ){ |
| 123272 | + int addrOnce = 0; |
| 123273 | + |
| 123274 | + /* Open every index that needs updating. */ |
| 123275 | + if( eOnePass!=ONEPASS_OFF ){ |
| 122956 | 123276 | if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0; |
| 122957 | 123277 | if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0; |
| 122958 | 123278 | } |
| 123279 | + |
| 123280 | + if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){ |
| 123281 | + addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
| 123282 | + } |
| 122959 | 123283 | sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen, |
| 122960 | 123284 | 0, 0); |
| 123285 | + if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce); |
| 122961 | 123286 | } |
| 122962 | 123287 | |
| 122963 | 123288 | /* Top of the update loop */ |
| 122964 | | - if( okOnePass ){ |
| 122965 | | - if( aToOpen[iDataCur-iBaseCur] && !isView ){ |
| 123289 | + if( eOnePass!=ONEPASS_OFF ){ |
| 123290 | + if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){ |
| 122966 | 123291 | assert( pPk ); |
| 122967 | 123292 | sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey); |
| 122968 | 123293 | VdbeCoverageNeverTaken(v); |
| 122969 | 123294 | } |
| 122970 | | - labelContinue = labelBreak; |
| 123295 | + if( eOnePass==ONEPASS_SINGLE ){ |
| 123296 | + labelContinue = labelBreak; |
| 123297 | + }else{ |
| 123298 | + labelContinue = sqlite3VdbeMakeLabel(v); |
| 123299 | + } |
| 122971 | 123300 | sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak); |
| 122972 | 123301 | VdbeCoverageIf(v, pPk==0); |
| 122973 | 123302 | VdbeCoverageIf(v, pPk!=0); |
| 122974 | 123303 | }else if( pPk ){ |
| 122975 | 123304 | labelContinue = sqlite3VdbeMakeLabel(v); |
| | @@ -123090,11 +123419,10 @@ |
| 123090 | 123419 | } |
| 123091 | 123420 | } |
| 123092 | 123421 | |
| 123093 | 123422 | if( !isView ){ |
| 123094 | 123423 | int addr1 = 0; /* Address of jump instruction */ |
| 123095 | | - int bReplace = 0; /* True if REPLACE conflict resolution might happen */ |
| 123096 | 123424 | |
| 123097 | 123425 | /* Do constraint checks. */ |
| 123098 | 123426 | assert( regOldRowid>0 ); |
| 123099 | 123427 | sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, |
| 123100 | 123428 | regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace, |
| | @@ -123126,18 +123454,22 @@ |
| 123126 | 123454 | ** is the column index supplied by the user. |
| 123127 | 123455 | */ |
| 123128 | 123456 | assert( regNew==regNewRowid+1 ); |
| 123129 | 123457 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 123130 | 123458 | sqlite3VdbeAddOp3(v, OP_Delete, iDataCur, |
| 123131 | | - OPFLAG_ISUPDATE | ((hasFK || chngKey || pPk!=0) ? 0 : OPFLAG_ISNOOP), |
| 123459 | + OPFLAG_ISUPDATE | ((hasFK || chngKey) ? 0 : OPFLAG_ISNOOP), |
| 123132 | 123460 | regNewRowid |
| 123133 | 123461 | ); |
| 123462 | + if( eOnePass==ONEPASS_MULTI ){ |
| 123463 | + assert( hasFK==0 && chngKey==0 ); |
| 123464 | + sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION); |
| 123465 | + } |
| 123134 | 123466 | if( !pParse->nested ){ |
| 123135 | 123467 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
| 123136 | 123468 | } |
| 123137 | 123469 | #else |
| 123138 | | - if( hasFK || chngKey || pPk!=0 ){ |
| 123470 | + if( hasFK || chngKey ){ |
| 123139 | 123471 | sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0); |
| 123140 | 123472 | } |
| 123141 | 123473 | #endif |
| 123142 | 123474 | if( bReplace || chngKey ){ |
| 123143 | 123475 | sqlite3VdbeJumpHere(v, addr1); |
| | @@ -123146,12 +123478,15 @@ |
| 123146 | 123478 | if( hasFK ){ |
| 123147 | 123479 | sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey); |
| 123148 | 123480 | } |
| 123149 | 123481 | |
| 123150 | 123482 | /* Insert the new index entries and the new record. */ |
| 123151 | | - sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, |
| 123152 | | - regNewRowid, aRegIdx, 1, 0, 0); |
| 123483 | + sqlite3CompleteInsertion( |
| 123484 | + pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx, |
| 123485 | + OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0), |
| 123486 | + 0, 0 |
| 123487 | + ); |
| 123153 | 123488 | |
| 123154 | 123489 | /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to |
| 123155 | 123490 | ** handle rows (possibly in other tables) that refer via a foreign key |
| 123156 | 123491 | ** to the row just updated. */ |
| 123157 | 123492 | if( hasFK ){ |
| | @@ -123169,12 +123504,15 @@ |
| 123169 | 123504 | TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue); |
| 123170 | 123505 | |
| 123171 | 123506 | /* Repeat the above with the next record to be updated, until |
| 123172 | 123507 | ** all record selected by the WHERE clause have been updated. |
| 123173 | 123508 | */ |
| 123174 | | - if( okOnePass ){ |
| 123509 | + if( eOnePass==ONEPASS_SINGLE ){ |
| 123175 | 123510 | /* Nothing to do at end-of-loop for a single-pass */ |
| 123511 | + }else if( eOnePass==ONEPASS_MULTI ){ |
| 123512 | + sqlite3VdbeResolveLabel(v, labelContinue); |
| 123513 | + sqlite3WhereEnd(pWInfo); |
| 123176 | 123514 | }else if( pPk ){ |
| 123177 | 123515 | sqlite3VdbeResolveLabel(v, labelContinue); |
| 123178 | 123516 | sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v); |
| 123179 | 123517 | }else{ |
| 123180 | 123518 | sqlite3VdbeGoto(v, labelContinue); |
| | @@ -127090,11 +127428,14 @@ |
| 127090 | 127428 | |
| 127091 | 127429 | /* Seek the table cursor, if required */ |
| 127092 | 127430 | if( omitTable ){ |
| 127093 | 127431 | /* pIdx is a covering index. No need to access the main table. */ |
| 127094 | 127432 | }else if( HasRowid(pIdx->pTable) ){ |
| 127095 | | - if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE)!=0 ){ |
| 127433 | + if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || ( |
| 127434 | + (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE) |
| 127435 | + && (pWInfo->eOnePass==ONEPASS_SINGLE) |
| 127436 | + )){ |
| 127096 | 127437 | iRowidReg = ++pParse->nMem; |
| 127097 | 127438 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
| 127098 | 127439 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 127099 | 127440 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg); |
| 127100 | 127441 | VdbeCoverage(v); |
| | @@ -128454,10 +128795,11 @@ |
| 128454 | 128795 | int noCase = 0; /* uppercase equivalent to lowercase */ |
| 128455 | 128796 | int op; /* Top-level operator. pExpr->op */ |
| 128456 | 128797 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 128457 | 128798 | sqlite3 *db = pParse->db; /* Database connection */ |
| 128458 | 128799 | unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */ |
| 128800 | + int nLeft; /* Number of elements on left side vector */ |
| 128459 | 128801 | |
| 128460 | 128802 | if( db->mallocFailed ){ |
| 128461 | 128803 | return; |
| 128462 | 128804 | } |
| 128463 | 128805 | pTerm = &pWC->a[idxTerm]; |
| | @@ -128483,10 +128825,14 @@ |
| 128483 | 128825 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 128484 | 128826 | Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable); |
| 128485 | 128827 | prereqAll |= x; |
| 128486 | 128828 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 128487 | 128829 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
| 128830 | + if( (prereqAll>>1)>=x ){ |
| 128831 | + sqlite3ErrorMsg(pParse, "ON clause references tables to its right"); |
| 128832 | + return; |
| 128833 | + } |
| 128488 | 128834 | } |
| 128489 | 128835 | pTerm->prereqAll = prereqAll; |
| 128490 | 128836 | pTerm->leftCursor = -1; |
| 128491 | 128837 | pTerm->iParent = -1; |
| 128492 | 128838 | pTerm->eOperator = 0; |
| | @@ -128725,17 +129071,16 @@ |
| 128725 | 129071 | ** |
| 128726 | 129072 | ** This is only required if at least one side of the comparison operation |
| 128727 | 129073 | ** is not a sub-select. */ |
| 128728 | 129074 | if( pWC->op==TK_AND |
| 128729 | 129075 | && (pExpr->op==TK_EQ || pExpr->op==TK_IS) |
| 128730 | | - && sqlite3ExprIsVector(pExpr->pLeft) |
| 129076 | + && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1 |
| 129077 | + && sqlite3ExprVectorSize(pExpr->pRight)==nLeft |
| 128731 | 129078 | && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 |
| 128732 | | - || (pExpr->pRight->flags & EP_xIsSelect)==0 |
| 128733 | | - )){ |
| 128734 | | - int nLeft = sqlite3ExprVectorSize(pExpr->pLeft); |
| 129079 | + || (pExpr->pRight->flags & EP_xIsSelect)==0) |
| 129080 | + ){ |
| 128735 | 129081 | int i; |
| 128736 | | - assert( nLeft==sqlite3ExprVectorSize(pExpr->pRight) ); |
| 128737 | 129082 | for(i=0; i<nLeft; i++){ |
| 128738 | 129083 | int idxNew; |
| 128739 | 129084 | Expr *pNew; |
| 128740 | 129085 | Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i); |
| 128741 | 129086 | Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i); |
| | @@ -129293,10 +129638,11 @@ |
| 129293 | 129638 | if( pIdx ){ |
| 129294 | 129639 | int j = iColumn; |
| 129295 | 129640 | iColumn = pIdx->aiColumn[j]; |
| 129296 | 129641 | if( iColumn==XN_EXPR ){ |
| 129297 | 129642 | pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr; |
| 129643 | + pScan->zCollName = pIdx->azColl[j]; |
| 129298 | 129644 | }else if( iColumn==pIdx->pTable->iPKey ){ |
| 129299 | 129645 | iColumn = XN_ROWID; |
| 129300 | 129646 | }else if( iColumn>=0 ){ |
| 129301 | 129647 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 129302 | 129648 | pScan->zCollName = pIdx->azColl[j]; |
| | @@ -133934,11 +134280,12 @@ |
| 133934 | 134280 | x = sqlite3ColumnOfIndex(pIdx, x); |
| 133935 | 134281 | if( x>=0 ){ |
| 133936 | 134282 | pOp->p2 = x; |
| 133937 | 134283 | pOp->p1 = pLevel->iIdxCur; |
| 133938 | 134284 | } |
| 133939 | | - assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 ); |
| 134285 | + assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 |
| 134286 | + || pWInfo->eOnePass ); |
| 133940 | 134287 | }else if( pOp->opcode==OP_Rowid ){ |
| 133941 | 134288 | pOp->p1 = pLevel->iIdxCur; |
| 133942 | 134289 | pOp->opcode = OP_IdxRowid; |
| 133943 | 134290 | } |
| 133944 | 134291 | } |
| | @@ -133998,10 +134345,23 @@ |
| 133998 | 134345 | ** Indicate that sqlite3ParserFree() will never be called with a null |
| 133999 | 134346 | ** pointer. |
| 134000 | 134347 | */ |
| 134001 | 134348 | #define YYPARSEFREENEVERNULL 1 |
| 134002 | 134349 | |
| 134350 | +/* |
| 134351 | +** In the amalgamation, the parse.c file generated by lemon and the |
| 134352 | +** tokenize.c file are concatenated. In that case, sqlite3RunParser() |
| 134353 | +** has access to the the size of the yyParser object and so the parser |
| 134354 | +** engine can be allocated from stack. In that case, only the |
| 134355 | +** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked |
| 134356 | +** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be |
| 134357 | +** omitted. |
| 134358 | +*/ |
| 134359 | +#ifdef SQLITE_AMALGAMATION |
| 134360 | +# define sqlite3Parser_ENGINEALWAYSONSTACK 1 |
| 134361 | +#endif |
| 134362 | + |
| 134003 | 134363 | /* |
| 134004 | 134364 | ** Alternative datatype for the argument to the malloc() routine passed |
| 134005 | 134365 | ** into sqlite3ParserAlloc(). The default is size_t. |
| 134006 | 134366 | */ |
| 134007 | 134367 | #define YYMALLOCARGTYPE u64 |
| | @@ -135446,10 +135806,35 @@ |
| 135446 | 135806 | */ |
| 135447 | 135807 | #ifndef YYMALLOCARGTYPE |
| 135448 | 135808 | # define YYMALLOCARGTYPE size_t |
| 135449 | 135809 | #endif |
| 135450 | 135810 | |
| 135811 | +/* Initialize a new parser that has already been allocated. |
| 135812 | +*/ |
| 135813 | +SQLITE_PRIVATE void sqlite3ParserInit(void *yypParser){ |
| 135814 | + yyParser *pParser = (yyParser*)yypParser; |
| 135815 | +#ifdef YYTRACKMAXSTACKDEPTH |
| 135816 | + pParser->yyhwm = 0; |
| 135817 | +#endif |
| 135818 | +#if YYSTACKDEPTH<=0 |
| 135819 | + pParser->yytos = NULL; |
| 135820 | + pParser->yystack = NULL; |
| 135821 | + pParser->yystksz = 0; |
| 135822 | + if( yyGrowStack(pParser) ){ |
| 135823 | + pParser->yystack = &pParser->yystk0; |
| 135824 | + pParser->yystksz = 1; |
| 135825 | + } |
| 135826 | +#endif |
| 135827 | +#ifndef YYNOERRORRECOVERY |
| 135828 | + pParser->yyerrcnt = -1; |
| 135829 | +#endif |
| 135830 | + pParser->yytos = pParser->yystack; |
| 135831 | + pParser->yystack[0].stateno = 0; |
| 135832 | + pParser->yystack[0].major = 0; |
| 135833 | +} |
| 135834 | + |
| 135835 | +#ifndef sqlite3Parser_ENGINEALWAYSONSTACK |
| 135451 | 135836 | /* |
| 135452 | 135837 | ** This function allocates a new parser. |
| 135453 | 135838 | ** The only argument is a pointer to a function which works like |
| 135454 | 135839 | ** malloc. |
| 135455 | 135840 | ** |
| | @@ -135461,32 +135846,15 @@ |
| 135461 | 135846 | ** to sqlite3Parser and sqlite3ParserFree. |
| 135462 | 135847 | */ |
| 135463 | 135848 | SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){ |
| 135464 | 135849 | yyParser *pParser; |
| 135465 | 135850 | pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) ); |
| 135466 | | - if( pParser ){ |
| 135467 | | -#ifdef YYTRACKMAXSTACKDEPTH |
| 135468 | | - pParser->yyhwm = 0; |
| 135469 | | -#endif |
| 135470 | | -#if YYSTACKDEPTH<=0 |
| 135471 | | - pParser->yytos = NULL; |
| 135472 | | - pParser->yystack = NULL; |
| 135473 | | - pParser->yystksz = 0; |
| 135474 | | - if( yyGrowStack(pParser) ){ |
| 135475 | | - pParser->yystack = &pParser->yystk0; |
| 135476 | | - pParser->yystksz = 1; |
| 135477 | | - } |
| 135478 | | -#endif |
| 135479 | | -#ifndef YYNOERRORRECOVERY |
| 135480 | | - pParser->yyerrcnt = -1; |
| 135481 | | -#endif |
| 135482 | | - pParser->yytos = pParser->yystack; |
| 135483 | | - pParser->yystack[0].stateno = 0; |
| 135484 | | - pParser->yystack[0].major = 0; |
| 135485 | | - } |
| 135851 | + if( pParser ) sqlite3ParserInit(pParser); |
| 135486 | 135852 | return pParser; |
| 135487 | 135853 | } |
| 135854 | +#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */ |
| 135855 | + |
| 135488 | 135856 | |
| 135489 | 135857 | /* The following function deletes the "minor type" or semantic value |
| 135490 | 135858 | ** associated with a symbol. The symbol can be either a terminal |
| 135491 | 135859 | ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is |
| 135492 | 135860 | ** a pointer to the value to be deleted. The code used to do the |
| | @@ -135608,10 +135976,22 @@ |
| 135608 | 135976 | } |
| 135609 | 135977 | #endif |
| 135610 | 135978 | yy_destructor(pParser, yytos->major, &yytos->minor); |
| 135611 | 135979 | } |
| 135612 | 135980 | |
| 135981 | +/* |
| 135982 | +** Clear all secondary memory allocations from the parser |
| 135983 | +*/ |
| 135984 | +SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){ |
| 135985 | + yyParser *pParser = (yyParser*)p; |
| 135986 | + while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser); |
| 135987 | +#if YYSTACKDEPTH<=0 |
| 135988 | + if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack); |
| 135989 | +#endif |
| 135990 | +} |
| 135991 | + |
| 135992 | +#ifndef sqlite3Parser_ENGINEALWAYSONSTACK |
| 135613 | 135993 | /* |
| 135614 | 135994 | ** Deallocate and destroy a parser. Destructors are called for |
| 135615 | 135995 | ** all stack elements before shutting the parser down. |
| 135616 | 135996 | ** |
| 135617 | 135997 | ** If the YYPARSEFREENEVERNULL macro exists (for example because it |
| | @@ -135620,20 +136000,17 @@ |
| 135620 | 136000 | */ |
| 135621 | 136001 | SQLITE_PRIVATE void sqlite3ParserFree( |
| 135622 | 136002 | void *p, /* The parser to be deleted */ |
| 135623 | 136003 | void (*freeProc)(void*) /* Function used to reclaim memory */ |
| 135624 | 136004 | ){ |
| 135625 | | - yyParser *pParser = (yyParser*)p; |
| 135626 | 136005 | #ifndef YYPARSEFREENEVERNULL |
| 135627 | | - if( pParser==0 ) return; |
| 135628 | | -#endif |
| 135629 | | - while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser); |
| 135630 | | -#if YYSTACKDEPTH<=0 |
| 135631 | | - if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack); |
| 135632 | | -#endif |
| 135633 | | - (*freeProc)((void*)pParser); |
| 135634 | | -} |
| 136006 | + if( p==0 ) return; |
| 136007 | +#endif |
| 136008 | + sqlite3ParserFinalize(p); |
| 136009 | + (*freeProc)(p); |
| 136010 | +} |
| 136011 | +#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */ |
| 135635 | 136012 | |
| 135636 | 136013 | /* |
| 135637 | 136014 | ** Return the peak depth of the stack for a parser. |
| 135638 | 136015 | */ |
| 135639 | 136016 | #ifdef YYTRACKMAXSTACKDEPTH |
| | @@ -138483,10 +138860,13 @@ |
| 138483 | 138860 | void *pEngine; /* The LEMON-generated LALR(1) parser */ |
| 138484 | 138861 | int tokenType; /* type of the next token */ |
| 138485 | 138862 | int lastTokenParsed = -1; /* type of the previous token */ |
| 138486 | 138863 | sqlite3 *db = pParse->db; /* The database connection */ |
| 138487 | 138864 | int mxSqlLen; /* Max length of an SQL string */ |
| 138865 | +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
| 138866 | + unsigned char zSpace[sizeof(yyParser)]; /* Space for parser engine object */ |
| 138867 | +#endif |
| 138488 | 138868 | |
| 138489 | 138869 | assert( zSql!=0 ); |
| 138490 | 138870 | mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
| 138491 | 138871 | if( db->nVdbeActive==0 ){ |
| 138492 | 138872 | db->u1.isInterrupted = 0; |
| | @@ -138494,15 +138874,20 @@ |
| 138494 | 138874 | pParse->rc = SQLITE_OK; |
| 138495 | 138875 | pParse->zTail = zSql; |
| 138496 | 138876 | i = 0; |
| 138497 | 138877 | assert( pzErrMsg!=0 ); |
| 138498 | 138878 | /* sqlite3ParserTrace(stdout, "parser: "); */ |
| 138879 | +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
| 138880 | + pEngine = zSpace; |
| 138881 | + sqlite3ParserInit(pEngine); |
| 138882 | +#else |
| 138499 | 138883 | pEngine = sqlite3ParserAlloc(sqlite3Malloc); |
| 138500 | 138884 | if( pEngine==0 ){ |
| 138501 | 138885 | sqlite3OomFault(db); |
| 138502 | 138886 | return SQLITE_NOMEM_BKPT; |
| 138503 | 138887 | } |
| 138888 | +#endif |
| 138504 | 138889 | assert( pParse->pNewTable==0 ); |
| 138505 | 138890 | assert( pParse->pNewTrigger==0 ); |
| 138506 | 138891 | assert( pParse->nVar==0 ); |
| 138507 | 138892 | assert( pParse->pVList==0 ); |
| 138508 | 138893 | while( 1 ){ |
| | @@ -138550,11 +138935,15 @@ |
| 138550 | 138935 | sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK, |
| 138551 | 138936 | sqlite3ParserStackPeak(pEngine) |
| 138552 | 138937 | ); |
| 138553 | 138938 | sqlite3_mutex_leave(sqlite3MallocMutex()); |
| 138554 | 138939 | #endif /* YYDEBUG */ |
| 138940 | +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
| 138941 | + sqlite3ParserFinalize(pEngine); |
| 138942 | +#else |
| 138555 | 138943 | sqlite3ParserFree(pEngine, sqlite3_free); |
| 138944 | +#endif |
| 138556 | 138945 | if( db->mallocFailed ){ |
| 138557 | 138946 | pParse->rc = SQLITE_NOMEM_BKPT; |
| 138558 | 138947 | } |
| 138559 | 138948 | if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ |
| 138560 | 138949 | pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); |
| | @@ -144213,10 +144602,11 @@ |
| 144213 | 144602 | |
| 144214 | 144603 | /* Precompiled statements used by the implementation. Each of these |
| 144215 | 144604 | ** statements is run and reset within a single virtual table API call. |
| 144216 | 144605 | */ |
| 144217 | 144606 | sqlite3_stmt *aStmt[40]; |
| 144607 | + sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */ |
| 144218 | 144608 | |
| 144219 | 144609 | char *zReadExprlist; |
| 144220 | 144610 | char *zWriteExprlist; |
| 144221 | 144611 | |
| 144222 | 144612 | int nNodeSize; /* Soft limit for node size */ |
| | @@ -144282,10 +144672,11 @@ |
| 144282 | 144672 | struct Fts3Cursor { |
| 144283 | 144673 | sqlite3_vtab_cursor base; /* Base class used by SQLite core */ |
| 144284 | 144674 | i16 eSearch; /* Search strategy (see below) */ |
| 144285 | 144675 | u8 isEof; /* True if at End Of Results */ |
| 144286 | 144676 | u8 isRequireSeek; /* True if must seek pStmt to %_content row */ |
| 144677 | + u8 bSeekStmt; /* True if pStmt is a seek */ |
| 144287 | 144678 | sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */ |
| 144288 | 144679 | Fts3Expr *pExpr; /* Parsed MATCH query string */ |
| 144289 | 144680 | int iLangid; /* Language being queried for */ |
| 144290 | 144681 | int nPhrase; /* Number of matchable phrases in query */ |
| 144291 | 144682 | Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */ |
| | @@ -144804,10 +145195,11 @@ |
| 144804 | 145195 | |
| 144805 | 145196 | assert( p->nPendingData==0 ); |
| 144806 | 145197 | assert( p->pSegments==0 ); |
| 144807 | 145198 | |
| 144808 | 145199 | /* Free any prepared statements held */ |
| 145200 | + sqlite3_finalize(p->pSeekStmt); |
| 144809 | 145201 | for(i=0; i<SizeofArray(p->aStmt); i++){ |
| 144810 | 145202 | sqlite3_finalize(p->aStmt[i]); |
| 144811 | 145203 | } |
| 144812 | 145204 | sqlite3_free(p->zSegmentsTbl); |
| 144813 | 145205 | sqlite3_free(p->zReadExprlist); |
| | @@ -145675,13 +146067,13 @@ |
| 145675 | 146067 | p->nPendingData = 0; |
| 145676 | 146068 | p->azColumn = (char **)&p[1]; |
| 145677 | 146069 | p->pTokenizer = pTokenizer; |
| 145678 | 146070 | p->nMaxPendingData = FTS3_MAX_PENDING_DATA; |
| 145679 | 146071 | p->bHasDocsize = (isFts4 && bNoDocsize==0); |
| 145680 | | - p->bHasStat = isFts4; |
| 145681 | | - p->bFts4 = isFts4; |
| 145682 | | - p->bDescIdx = bDescIdx; |
| 146072 | + p->bHasStat = (u8)isFts4; |
| 146073 | + p->bFts4 = (u8)isFts4; |
| 146074 | + p->bDescIdx = (u8)bDescIdx; |
| 145683 | 146075 | p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */ |
| 145684 | 146076 | p->zContentTbl = zContent; |
| 145685 | 146077 | p->zLanguageid = zLanguageid; |
| 145686 | 146078 | zContent = 0; |
| 145687 | 146079 | zLanguageid = 0; |
| | @@ -145991,19 +146383,39 @@ |
| 145991 | 146383 | return SQLITE_NOMEM; |
| 145992 | 146384 | } |
| 145993 | 146385 | memset(pCsr, 0, sizeof(Fts3Cursor)); |
| 145994 | 146386 | return SQLITE_OK; |
| 145995 | 146387 | } |
| 146388 | + |
| 146389 | +/* |
| 146390 | +** Finalize the statement handle at pCsr->pStmt. |
| 146391 | +** |
| 146392 | +** Or, if that statement handle is one created by fts3CursorSeekStmt(), |
| 146393 | +** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement |
| 146394 | +** pointer there instead of finalizing it. |
| 146395 | +*/ |
| 146396 | +static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){ |
| 146397 | + if( pCsr->bSeekStmt ){ |
| 146398 | + Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; |
| 146399 | + if( p->pSeekStmt==0 ){ |
| 146400 | + p->pSeekStmt = pCsr->pStmt; |
| 146401 | + sqlite3_reset(pCsr->pStmt); |
| 146402 | + pCsr->pStmt = 0; |
| 146403 | + } |
| 146404 | + pCsr->bSeekStmt = 0; |
| 146405 | + } |
| 146406 | + sqlite3_finalize(pCsr->pStmt); |
| 146407 | +} |
| 145996 | 146408 | |
| 145997 | 146409 | /* |
| 145998 | 146410 | ** Close the cursor. For additional information see the documentation |
| 145999 | 146411 | ** on the xClose method of the virtual table interface. |
| 146000 | 146412 | */ |
| 146001 | 146413 | static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){ |
| 146002 | 146414 | Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; |
| 146003 | 146415 | assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); |
| 146004 | | - sqlite3_finalize(pCsr->pStmt); |
| 146416 | + fts3CursorFinalizeStmt(pCsr); |
| 146005 | 146417 | sqlite3Fts3ExprFree(pCsr->pExpr); |
| 146006 | 146418 | sqlite3Fts3FreeDeferredTokens(pCsr); |
| 146007 | 146419 | sqlite3_free(pCsr->aDoclist); |
| 146008 | 146420 | sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); |
| 146009 | 146421 | assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); |
| | @@ -146017,24 +146429,27 @@ |
| 146017 | 146429 | ** |
| 146018 | 146430 | ** "SELECT <columns> FROM %_content WHERE rowid = ?" |
| 146019 | 146431 | ** |
| 146020 | 146432 | ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to |
| 146021 | 146433 | ** it. If an error occurs, return an SQLite error code. |
| 146022 | | -** |
| 146023 | | -** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK. |
| 146024 | 146434 | */ |
| 146025 | | -static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){ |
| 146435 | +static int fts3CursorSeekStmt(Fts3Cursor *pCsr){ |
| 146026 | 146436 | int rc = SQLITE_OK; |
| 146027 | 146437 | if( pCsr->pStmt==0 ){ |
| 146028 | 146438 | Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; |
| 146029 | 146439 | char *zSql; |
| 146030 | | - zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist); |
| 146031 | | - if( !zSql ) return SQLITE_NOMEM; |
| 146032 | | - rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); |
| 146033 | | - sqlite3_free(zSql); |
| 146440 | + if( p->pSeekStmt ){ |
| 146441 | + pCsr->pStmt = p->pSeekStmt; |
| 146442 | + p->pSeekStmt = 0; |
| 146443 | + }else{ |
| 146444 | + zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist); |
| 146445 | + if( !zSql ) return SQLITE_NOMEM; |
| 146446 | + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); |
| 146447 | + sqlite3_free(zSql); |
| 146448 | + } |
| 146449 | + if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1; |
| 146034 | 146450 | } |
| 146035 | | - *ppStmt = pCsr->pStmt; |
| 146036 | 146451 | return rc; |
| 146037 | 146452 | } |
| 146038 | 146453 | |
| 146039 | 146454 | /* |
| 146040 | 146455 | ** Position the pCsr->pStmt statement so that it is on the row |
| | @@ -146042,13 +146457,11 @@ |
| 146042 | 146457 | ** SQLITE_OK on success. |
| 146043 | 146458 | */ |
| 146044 | 146459 | static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){ |
| 146045 | 146460 | int rc = SQLITE_OK; |
| 146046 | 146461 | if( pCsr->isRequireSeek ){ |
| 146047 | | - sqlite3_stmt *pStmt = 0; |
| 146048 | | - |
| 146049 | | - rc = fts3CursorSeekStmt(pCsr, &pStmt); |
| 146462 | + rc = fts3CursorSeekStmt(pCsr); |
| 146050 | 146463 | if( rc==SQLITE_OK ){ |
| 146051 | 146464 | sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId); |
| 146052 | 146465 | pCsr->isRequireSeek = 0; |
| 146053 | 146466 | if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){ |
| 146054 | 146467 | return SQLITE_OK; |
| | @@ -147502,11 +147915,11 @@ |
| 147502 | 147915 | if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++]; |
| 147503 | 147916 | if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++]; |
| 147504 | 147917 | assert( iIdx==nVal ); |
| 147505 | 147918 | |
| 147506 | 147919 | /* In case the cursor has been used before, clear it now. */ |
| 147507 | | - sqlite3_finalize(pCsr->pStmt); |
| 147920 | + fts3CursorFinalizeStmt(pCsr); |
| 147508 | 147921 | sqlite3_free(pCsr->aDoclist); |
| 147509 | 147922 | sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); |
| 147510 | 147923 | sqlite3Fts3ExprFree(pCsr->pExpr); |
| 147511 | 147924 | memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor)); |
| 147512 | 147925 | |
| | @@ -147570,11 +147983,11 @@ |
| 147570 | 147983 | sqlite3_free(zSql); |
| 147571 | 147984 | }else{ |
| 147572 | 147985 | rc = SQLITE_NOMEM; |
| 147573 | 147986 | } |
| 147574 | 147987 | }else if( eSearch==FTS3_DOCID_SEARCH ){ |
| 147575 | | - rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt); |
| 147988 | + rc = fts3CursorSeekStmt(pCsr); |
| 147576 | 147989 | if( rc==SQLITE_OK ){ |
| 147577 | 147990 | rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons); |
| 147578 | 147991 | } |
| 147579 | 147992 | } |
| 147580 | 147993 | if( rc!=SQLITE_OK ) return rc; |
| | @@ -147734,11 +148147,11 @@ |
| 147734 | 148147 | sqlite3_stmt *pStmt = 0; |
| 147735 | 148148 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 147736 | 148149 | if( rc==SQLITE_OK ){ |
| 147737 | 148150 | int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW); |
| 147738 | 148151 | rc = sqlite3_finalize(pStmt); |
| 147739 | | - if( rc==SQLITE_OK ) p->bHasStat = bHasStat; |
| 148152 | + if( rc==SQLITE_OK ) p->bHasStat = (u8)bHasStat; |
| 147740 | 148153 | } |
| 147741 | 148154 | sqlite3_free(zSql); |
| 147742 | 148155 | }else{ |
| 147743 | 148156 | rc = SQLITE_NOMEM; |
| 147744 | 148157 | } |
| | @@ -162590,10 +163003,11 @@ |
| 162590 | 163003 | /* #include <stdio.h> */ |
| 162591 | 163004 | |
| 162592 | 163005 | #ifndef SQLITE_AMALGAMATION |
| 162593 | 163006 | #include "sqlite3rtree.h" |
| 162594 | 163007 | typedef sqlite3_int64 i64; |
| 163008 | +typedef sqlite3_uint64 u64; |
| 162595 | 163009 | typedef unsigned char u8; |
| 162596 | 163010 | typedef unsigned short u16; |
| 162597 | 163011 | typedef unsigned int u32; |
| 162598 | 163012 | #endif |
| 162599 | 163013 | |
| | @@ -162638,28 +163052,33 @@ |
| 162638 | 163052 | struct Rtree { |
| 162639 | 163053 | sqlite3_vtab base; /* Base class. Must be first */ |
| 162640 | 163054 | sqlite3 *db; /* Host database connection */ |
| 162641 | 163055 | int iNodeSize; /* Size in bytes of each node in the node table */ |
| 162642 | 163056 | u8 nDim; /* Number of dimensions */ |
| 163057 | + u8 nDim2; /* Twice the number of dimensions */ |
| 162643 | 163058 | u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */ |
| 162644 | 163059 | u8 nBytesPerCell; /* Bytes consumed per cell */ |
| 163060 | + u8 inWrTrans; /* True if inside write transaction */ |
| 162645 | 163061 | int iDepth; /* Current depth of the r-tree structure */ |
| 162646 | 163062 | char *zDb; /* Name of database containing r-tree table */ |
| 162647 | 163063 | char *zName; /* Name of r-tree table */ |
| 162648 | | - int nBusy; /* Current number of users of this structure */ |
| 163064 | + u32 nBusy; /* Current number of users of this structure */ |
| 162649 | 163065 | i64 nRowEst; /* Estimated number of rows in this table */ |
| 163066 | + u32 nCursor; /* Number of open cursors */ |
| 162650 | 163067 | |
| 162651 | 163068 | /* List of nodes removed during a CondenseTree operation. List is |
| 162652 | 163069 | ** linked together via the pointer normally used for hash chains - |
| 162653 | 163070 | ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree |
| 162654 | 163071 | ** headed by the node (leaf nodes have RtreeNode.iNode==0). |
| 162655 | 163072 | */ |
| 162656 | 163073 | RtreeNode *pDeleted; |
| 162657 | 163074 | int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */ |
| 163075 | + |
| 163076 | + /* Blob I/O on xxx_node */ |
| 163077 | + sqlite3_blob *pNodeBlob; |
| 162658 | 163078 | |
| 162659 | 163079 | /* Statements to read/write/delete a record from xxx_node */ |
| 162660 | | - sqlite3_stmt *pReadNode; |
| 162661 | 163080 | sqlite3_stmt *pWriteNode; |
| 162662 | 163081 | sqlite3_stmt *pDeleteNode; |
| 162663 | 163082 | |
| 162664 | 163083 | /* Statements to read/write/delete a record from xxx_rowid */ |
| 162665 | 163084 | sqlite3_stmt *pReadRowid; |
| | @@ -162884,26 +163303,100 @@ |
| 162884 | 163303 | #endif |
| 162885 | 163304 | #ifndef MIN |
| 162886 | 163305 | # define MIN(x,y) ((x) > (y) ? (y) : (x)) |
| 162887 | 163306 | #endif |
| 162888 | 163307 | |
| 163308 | +/* What version of GCC is being used. 0 means GCC is not being used . |
| 163309 | +** Note that the GCC_VERSION macro will also be set correctly when using |
| 163310 | +** clang, since clang works hard to be gcc compatible. So the gcc |
| 163311 | +** optimizations will also work when compiling with clang. |
| 163312 | +*/ |
| 163313 | +#ifndef GCC_VERSION |
| 163314 | +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC) |
| 163315 | +# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__) |
| 163316 | +#else |
| 163317 | +# define GCC_VERSION 0 |
| 163318 | +#endif |
| 163319 | +#endif |
| 163320 | + |
| 163321 | +/* The testcase() macro should already be defined in the amalgamation. If |
| 163322 | +** it is not, make it a no-op. |
| 163323 | +*/ |
| 163324 | +#ifndef SQLITE_AMALGAMATION |
| 163325 | +# define testcase(X) |
| 163326 | +#endif |
| 163327 | + |
| 163328 | +/* |
| 163329 | +** Macros to determine whether the machine is big or little endian, |
| 163330 | +** and whether or not that determination is run-time or compile-time. |
| 163331 | +** |
| 163332 | +** For best performance, an attempt is made to guess at the byte-order |
| 163333 | +** using C-preprocessor macros. If that is unsuccessful, or if |
| 163334 | +** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined |
| 163335 | +** at run-time. |
| 163336 | +*/ |
| 163337 | +#ifndef SQLITE_BYTEORDER |
| 163338 | +#if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 163339 | + defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ |
| 163340 | + defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ |
| 163341 | + defined(__arm__) |
| 163342 | +# define SQLITE_BYTEORDER 1234 |
| 163343 | +#elif defined(sparc) || defined(__ppc__) |
| 163344 | +# define SQLITE_BYTEORDER 4321 |
| 163345 | +#else |
| 163346 | +# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */ |
| 163347 | +#endif |
| 163348 | +#endif |
| 163349 | + |
| 163350 | + |
| 163351 | +/* What version of MSVC is being used. 0 means MSVC is not being used */ |
| 163352 | +#ifndef MSVC_VERSION |
| 163353 | +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC) |
| 163354 | +# define MSVC_VERSION _MSC_VER |
| 163355 | +#else |
| 163356 | +# define MSVC_VERSION 0 |
| 163357 | +#endif |
| 163358 | +#endif |
| 163359 | + |
| 162889 | 163360 | /* |
| 162890 | 163361 | ** Functions to deserialize a 16 bit integer, 32 bit real number and |
| 162891 | 163362 | ** 64 bit integer. The deserialized value is returned. |
| 162892 | 163363 | */ |
| 162893 | 163364 | static int readInt16(u8 *p){ |
| 162894 | 163365 | return (p[0]<<8) + p[1]; |
| 162895 | 163366 | } |
| 162896 | 163367 | static void readCoord(u8 *p, RtreeCoord *pCoord){ |
| 163368 | + assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */ |
| 163369 | +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 163370 | + pCoord->u = _byteswap_ulong(*(u32*)p); |
| 163371 | +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
| 163372 | + pCoord->u = __builtin_bswap32(*(u32*)p); |
| 163373 | +#elif SQLITE_BYTEORDER==4321 |
| 163374 | + pCoord->u = *(u32*)p; |
| 163375 | +#else |
| 162897 | 163376 | pCoord->u = ( |
| 162898 | 163377 | (((u32)p[0]) << 24) + |
| 162899 | 163378 | (((u32)p[1]) << 16) + |
| 162900 | 163379 | (((u32)p[2]) << 8) + |
| 162901 | 163380 | (((u32)p[3]) << 0) |
| 162902 | 163381 | ); |
| 163382 | +#endif |
| 162903 | 163383 | } |
| 162904 | 163384 | static i64 readInt64(u8 *p){ |
| 163385 | +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 163386 | + u64 x; |
| 163387 | + memcpy(&x, p, 8); |
| 163388 | + return (i64)_byteswap_uint64(x); |
| 163389 | +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
| 163390 | + u64 x; |
| 163391 | + memcpy(&x, p, 8); |
| 163392 | + return (i64)__builtin_bswap64(x); |
| 163393 | +#elif SQLITE_BYTEORDER==4321 |
| 163394 | + i64 x; |
| 163395 | + memcpy(&x, p, 8); |
| 163396 | + return x; |
| 163397 | +#else |
| 162905 | 163398 | return ( |
| 162906 | 163399 | (((i64)p[0]) << 56) + |
| 162907 | 163400 | (((i64)p[1]) << 48) + |
| 162908 | 163401 | (((i64)p[2]) << 40) + |
| 162909 | 163402 | (((i64)p[3]) << 32) + |
| | @@ -162910,42 +163403,64 @@ |
| 162910 | 163403 | (((i64)p[4]) << 24) + |
| 162911 | 163404 | (((i64)p[5]) << 16) + |
| 162912 | 163405 | (((i64)p[6]) << 8) + |
| 162913 | 163406 | (((i64)p[7]) << 0) |
| 162914 | 163407 | ); |
| 163408 | +#endif |
| 162915 | 163409 | } |
| 162916 | 163410 | |
| 162917 | 163411 | /* |
| 162918 | 163412 | ** Functions to serialize a 16 bit integer, 32 bit real number and |
| 162919 | 163413 | ** 64 bit integer. The value returned is the number of bytes written |
| 162920 | 163414 | ** to the argument buffer (always 2, 4 and 8 respectively). |
| 162921 | 163415 | */ |
| 162922 | | -static int writeInt16(u8 *p, int i){ |
| 163416 | +static void writeInt16(u8 *p, int i){ |
| 162923 | 163417 | p[0] = (i>> 8)&0xFF; |
| 162924 | 163418 | p[1] = (i>> 0)&0xFF; |
| 162925 | | - return 2; |
| 162926 | 163419 | } |
| 162927 | 163420 | static int writeCoord(u8 *p, RtreeCoord *pCoord){ |
| 162928 | 163421 | u32 i; |
| 163422 | + assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */ |
| 162929 | 163423 | assert( sizeof(RtreeCoord)==4 ); |
| 162930 | 163424 | assert( sizeof(u32)==4 ); |
| 163425 | +#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
| 163426 | + i = __builtin_bswap32(pCoord->u); |
| 163427 | + memcpy(p, &i, 4); |
| 163428 | +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 163429 | + i = _byteswap_ulong(pCoord->u); |
| 163430 | + memcpy(p, &i, 4); |
| 163431 | +#elif SQLITE_BYTEORDER==4321 |
| 163432 | + i = pCoord->u; |
| 163433 | + memcpy(p, &i, 4); |
| 163434 | +#else |
| 162931 | 163435 | i = pCoord->u; |
| 162932 | 163436 | p[0] = (i>>24)&0xFF; |
| 162933 | 163437 | p[1] = (i>>16)&0xFF; |
| 162934 | 163438 | p[2] = (i>> 8)&0xFF; |
| 162935 | 163439 | p[3] = (i>> 0)&0xFF; |
| 163440 | +#endif |
| 162936 | 163441 | return 4; |
| 162937 | 163442 | } |
| 162938 | 163443 | static int writeInt64(u8 *p, i64 i){ |
| 163444 | +#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
| 163445 | + i = (i64)__builtin_bswap64((u64)i); |
| 163446 | + memcpy(p, &i, 8); |
| 163447 | +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 163448 | + i = (i64)_byteswap_uint64((u64)i); |
| 163449 | + memcpy(p, &i, 8); |
| 163450 | +#elif SQLITE_BYTEORDER==4321 |
| 163451 | + memcpy(p, &i, 8); |
| 163452 | +#else |
| 162939 | 163453 | p[0] = (i>>56)&0xFF; |
| 162940 | 163454 | p[1] = (i>>48)&0xFF; |
| 162941 | 163455 | p[2] = (i>>40)&0xFF; |
| 162942 | 163456 | p[3] = (i>>32)&0xFF; |
| 162943 | 163457 | p[4] = (i>>24)&0xFF; |
| 162944 | 163458 | p[5] = (i>>16)&0xFF; |
| 162945 | 163459 | p[6] = (i>> 8)&0xFF; |
| 162946 | 163460 | p[7] = (i>> 0)&0xFF; |
| 163461 | +#endif |
| 162947 | 163462 | return 8; |
| 162948 | 163463 | } |
| 162949 | 163464 | |
| 162950 | 163465 | /* |
| 162951 | 163466 | ** Increment the reference count of node p. |
| | @@ -163023,10 +163538,21 @@ |
| 163023 | 163538 | pNode->isDirty = 1; |
| 163024 | 163539 | nodeReference(pParent); |
| 163025 | 163540 | } |
| 163026 | 163541 | return pNode; |
| 163027 | 163542 | } |
| 163543 | + |
| 163544 | +/* |
| 163545 | +** Clear the Rtree.pNodeBlob object |
| 163546 | +*/ |
| 163547 | +static void nodeBlobReset(Rtree *pRtree){ |
| 163548 | + if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){ |
| 163549 | + sqlite3_blob *pBlob = pRtree->pNodeBlob; |
| 163550 | + pRtree->pNodeBlob = 0; |
| 163551 | + sqlite3_blob_close(pBlob); |
| 163552 | + } |
| 163553 | +} |
| 163028 | 163554 | |
| 163029 | 163555 | /* |
| 163030 | 163556 | ** Obtain a reference to an r-tree node. |
| 163031 | 163557 | */ |
| 163032 | 163558 | static int nodeAcquire( |
| | @@ -163033,13 +163559,12 @@ |
| 163033 | 163559 | Rtree *pRtree, /* R-tree structure */ |
| 163034 | 163560 | i64 iNode, /* Node number to load */ |
| 163035 | 163561 | RtreeNode *pParent, /* Either the parent node or NULL */ |
| 163036 | 163562 | RtreeNode **ppNode /* OUT: Acquired node */ |
| 163037 | 163563 | ){ |
| 163038 | | - int rc; |
| 163039 | | - int rc2 = SQLITE_OK; |
| 163040 | | - RtreeNode *pNode; |
| 163564 | + int rc = SQLITE_OK; |
| 163565 | + RtreeNode *pNode = 0; |
| 163041 | 163566 | |
| 163042 | 163567 | /* Check if the requested node is already in the hash table. If so, |
| 163043 | 163568 | ** increase its reference count and return it. |
| 163044 | 163569 | */ |
| 163045 | 163570 | if( (pNode = nodeHashLookup(pRtree, iNode)) ){ |
| | @@ -163051,32 +163576,49 @@ |
| 163051 | 163576 | pNode->nRef++; |
| 163052 | 163577 | *ppNode = pNode; |
| 163053 | 163578 | return SQLITE_OK; |
| 163054 | 163579 | } |
| 163055 | 163580 | |
| 163056 | | - sqlite3_bind_int64(pRtree->pReadNode, 1, iNode); |
| 163057 | | - rc = sqlite3_step(pRtree->pReadNode); |
| 163058 | | - if( rc==SQLITE_ROW ){ |
| 163059 | | - const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0); |
| 163060 | | - if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){ |
| 163061 | | - pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize); |
| 163062 | | - if( !pNode ){ |
| 163063 | | - rc2 = SQLITE_NOMEM; |
| 163064 | | - }else{ |
| 163065 | | - pNode->pParent = pParent; |
| 163066 | | - pNode->zData = (u8 *)&pNode[1]; |
| 163067 | | - pNode->nRef = 1; |
| 163068 | | - pNode->iNode = iNode; |
| 163069 | | - pNode->isDirty = 0; |
| 163070 | | - pNode->pNext = 0; |
| 163071 | | - memcpy(pNode->zData, zBlob, pRtree->iNodeSize); |
| 163072 | | - nodeReference(pParent); |
| 163073 | | - } |
| 163074 | | - } |
| 163075 | | - } |
| 163076 | | - rc = sqlite3_reset(pRtree->pReadNode); |
| 163077 | | - if( rc==SQLITE_OK ) rc = rc2; |
| 163581 | + if( pRtree->pNodeBlob ){ |
| 163582 | + sqlite3_blob *pBlob = pRtree->pNodeBlob; |
| 163583 | + pRtree->pNodeBlob = 0; |
| 163584 | + rc = sqlite3_blob_reopen(pBlob, iNode); |
| 163585 | + pRtree->pNodeBlob = pBlob; |
| 163586 | + if( rc ){ |
| 163587 | + nodeBlobReset(pRtree); |
| 163588 | + if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM; |
| 163589 | + } |
| 163590 | + } |
| 163591 | + if( pRtree->pNodeBlob==0 ){ |
| 163592 | + char *zTab = sqlite3_mprintf("%s_node", pRtree->zName); |
| 163593 | + if( zTab==0 ) return SQLITE_NOMEM; |
| 163594 | + rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0, |
| 163595 | + &pRtree->pNodeBlob); |
| 163596 | + sqlite3_free(zTab); |
| 163597 | + } |
| 163598 | + if( rc ){ |
| 163599 | + nodeBlobReset(pRtree); |
| 163600 | + *ppNode = 0; |
| 163601 | + /* If unable to open an sqlite3_blob on the desired row, that can only |
| 163602 | + ** be because the shadow tables hold erroneous data. */ |
| 163603 | + if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB; |
| 163604 | + }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){ |
| 163605 | + pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize); |
| 163606 | + if( !pNode ){ |
| 163607 | + rc = SQLITE_NOMEM; |
| 163608 | + }else{ |
| 163609 | + pNode->pParent = pParent; |
| 163610 | + pNode->zData = (u8 *)&pNode[1]; |
| 163611 | + pNode->nRef = 1; |
| 163612 | + pNode->iNode = iNode; |
| 163613 | + pNode->isDirty = 0; |
| 163614 | + pNode->pNext = 0; |
| 163615 | + rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData, |
| 163616 | + pRtree->iNodeSize, 0); |
| 163617 | + nodeReference(pParent); |
| 163618 | + } |
| 163619 | + } |
| 163078 | 163620 | |
| 163079 | 163621 | /* If the root node was just loaded, set pRtree->iDepth to the height |
| 163080 | 163622 | ** of the r-tree structure. A height of zero means all data is stored on |
| 163081 | 163623 | ** the root node. A height of one means the children of the root node |
| 163082 | 163624 | ** are the leaves, and so on. If the depth as specified on the root node |
| | @@ -163124,11 +163666,11 @@ |
| 163124 | 163666 | int iCell /* Index into pNode into which pCell is written */ |
| 163125 | 163667 | ){ |
| 163126 | 163668 | int ii; |
| 163127 | 163669 | u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell]; |
| 163128 | 163670 | p += writeInt64(p, pCell->iRowid); |
| 163129 | | - for(ii=0; ii<(pRtree->nDim*2); ii++){ |
| 163671 | + for(ii=0; ii<pRtree->nDim2; ii++){ |
| 163130 | 163672 | p += writeCoord(p, &pCell->aCoord[ii]); |
| 163131 | 163673 | } |
| 163132 | 163674 | pNode->isDirty = 1; |
| 163133 | 163675 | } |
| 163134 | 163676 | |
| | @@ -163258,17 +163800,20 @@ |
| 163258 | 163800 | int iCell, /* Index of the cell within the node */ |
| 163259 | 163801 | RtreeCell *pCell /* OUT: Write the cell contents here */ |
| 163260 | 163802 | ){ |
| 163261 | 163803 | u8 *pData; |
| 163262 | 163804 | RtreeCoord *pCoord; |
| 163263 | | - int ii; |
| 163805 | + int ii = 0; |
| 163264 | 163806 | pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell); |
| 163265 | 163807 | pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell); |
| 163266 | 163808 | pCoord = pCell->aCoord; |
| 163267 | | - for(ii=0; ii<pRtree->nDim*2; ii++){ |
| 163268 | | - readCoord(&pData[ii*4], &pCoord[ii]); |
| 163269 | | - } |
| 163809 | + do{ |
| 163810 | + readCoord(pData, &pCoord[ii]); |
| 163811 | + readCoord(pData+4, &pCoord[ii+1]); |
| 163812 | + pData += 8; |
| 163813 | + ii += 2; |
| 163814 | + }while( ii<pRtree->nDim2 ); |
| 163270 | 163815 | } |
| 163271 | 163816 | |
| 163272 | 163817 | |
| 163273 | 163818 | /* Forward declaration for the function that does the work of |
| 163274 | 163819 | ** the virtual table module xCreate() and xConnect() methods. |
| | @@ -163315,11 +163860,13 @@ |
| 163315 | 163860 | ** zero the structure is deleted. |
| 163316 | 163861 | */ |
| 163317 | 163862 | static void rtreeRelease(Rtree *pRtree){ |
| 163318 | 163863 | pRtree->nBusy--; |
| 163319 | 163864 | if( pRtree->nBusy==0 ){ |
| 163320 | | - sqlite3_finalize(pRtree->pReadNode); |
| 163865 | + pRtree->inWrTrans = 0; |
| 163866 | + pRtree->nCursor = 0; |
| 163867 | + nodeBlobReset(pRtree); |
| 163321 | 163868 | sqlite3_finalize(pRtree->pWriteNode); |
| 163322 | 163869 | sqlite3_finalize(pRtree->pDeleteNode); |
| 163323 | 163870 | sqlite3_finalize(pRtree->pReadRowid); |
| 163324 | 163871 | sqlite3_finalize(pRtree->pWriteRowid); |
| 163325 | 163872 | sqlite3_finalize(pRtree->pDeleteRowid); |
| | @@ -163353,10 +163900,11 @@ |
| 163353 | 163900 | pRtree->zDb, pRtree->zName |
| 163354 | 163901 | ); |
| 163355 | 163902 | if( !zCreate ){ |
| 163356 | 163903 | rc = SQLITE_NOMEM; |
| 163357 | 163904 | }else{ |
| 163905 | + nodeBlobReset(pRtree); |
| 163358 | 163906 | rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0); |
| 163359 | 163907 | sqlite3_free(zCreate); |
| 163360 | 163908 | } |
| 163361 | 163909 | if( rc==SQLITE_OK ){ |
| 163362 | 163910 | rtreeRelease(pRtree); |
| | @@ -163368,17 +163916,19 @@ |
| 163368 | 163916 | /* |
| 163369 | 163917 | ** Rtree virtual table module xOpen method. |
| 163370 | 163918 | */ |
| 163371 | 163919 | static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
| 163372 | 163920 | int rc = SQLITE_NOMEM; |
| 163921 | + Rtree *pRtree = (Rtree *)pVTab; |
| 163373 | 163922 | RtreeCursor *pCsr; |
| 163374 | 163923 | |
| 163375 | 163924 | pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor)); |
| 163376 | 163925 | if( pCsr ){ |
| 163377 | 163926 | memset(pCsr, 0, sizeof(RtreeCursor)); |
| 163378 | 163927 | pCsr->base.pVtab = pVTab; |
| 163379 | 163928 | rc = SQLITE_OK; |
| 163929 | + pRtree->nCursor++; |
| 163380 | 163930 | } |
| 163381 | 163931 | *ppCursor = (sqlite3_vtab_cursor *)pCsr; |
| 163382 | 163932 | |
| 163383 | 163933 | return rc; |
| 163384 | 163934 | } |
| | @@ -163407,14 +163957,17 @@ |
| 163407 | 163957 | */ |
| 163408 | 163958 | static int rtreeClose(sqlite3_vtab_cursor *cur){ |
| 163409 | 163959 | Rtree *pRtree = (Rtree *)(cur->pVtab); |
| 163410 | 163960 | int ii; |
| 163411 | 163961 | RtreeCursor *pCsr = (RtreeCursor *)cur; |
| 163962 | + assert( pRtree->nCursor>0 ); |
| 163412 | 163963 | freeCursorConstraints(pCsr); |
| 163413 | 163964 | sqlite3_free(pCsr->aPoint); |
| 163414 | 163965 | for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]); |
| 163415 | 163966 | sqlite3_free(pCsr); |
| 163967 | + pRtree->nCursor--; |
| 163968 | + nodeBlobReset(pRtree); |
| 163416 | 163969 | return SQLITE_OK; |
| 163417 | 163970 | } |
| 163418 | 163971 | |
| 163419 | 163972 | /* |
| 163420 | 163973 | ** Rtree virtual table module xEof method. |
| | @@ -163433,27 +163986,34 @@ |
| 163433 | 163986 | ** endian platforms. The on-disk record stores integer coordinates if |
| 163434 | 163987 | ** eInt is true and it stores 32-bit floating point records if eInt is |
| 163435 | 163988 | ** false. a[] is the four bytes of the on-disk record to be decoded. |
| 163436 | 163989 | ** Store the results in "r". |
| 163437 | 163990 | ** |
| 163438 | | -** There are three versions of this macro, one each for little-endian and |
| 163439 | | -** big-endian processors and a third generic implementation. The endian- |
| 163440 | | -** specific implementations are much faster and are preferred if the |
| 163441 | | -** processor endianness is known at compile-time. The SQLITE_BYTEORDER |
| 163442 | | -** macro is part of sqliteInt.h and hence the endian-specific |
| 163443 | | -** implementation will only be used if this module is compiled as part |
| 163444 | | -** of the amalgamation. |
| 163991 | +** There are five versions of this macro. The last one is generic. The |
| 163992 | +** other four are various architectures-specific optimizations. |
| 163445 | 163993 | */ |
| 163446 | | -#if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234 |
| 163994 | +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 |
| 163995 | +#define RTREE_DECODE_COORD(eInt, a, r) { \ |
| 163996 | + RtreeCoord c; /* Coordinate decoded */ \ |
| 163997 | + c.u = _byteswap_ulong(*(u32*)a); \ |
| 163998 | + r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ |
| 163999 | +} |
| 164000 | +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 |
| 164001 | +#define RTREE_DECODE_COORD(eInt, a, r) { \ |
| 164002 | + RtreeCoord c; /* Coordinate decoded */ \ |
| 164003 | + c.u = __builtin_bswap32(*(u32*)a); \ |
| 164004 | + r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ |
| 164005 | +} |
| 164006 | +#elif SQLITE_BYTEORDER==1234 |
| 163447 | 164007 | #define RTREE_DECODE_COORD(eInt, a, r) { \ |
| 163448 | 164008 | RtreeCoord c; /* Coordinate decoded */ \ |
| 163449 | 164009 | memcpy(&c.u,a,4); \ |
| 163450 | 164010 | c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \ |
| 163451 | 164011 | ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \ |
| 163452 | 164012 | r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ |
| 163453 | 164013 | } |
| 163454 | | -#elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321 |
| 164014 | +#elif SQLITE_BYTEORDER==4321 |
| 163455 | 164015 | #define RTREE_DECODE_COORD(eInt, a, r) { \ |
| 163456 | 164016 | RtreeCoord c; /* Coordinate decoded */ \ |
| 163457 | 164017 | memcpy(&c.u,a,4); \ |
| 163458 | 164018 | r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ |
| 163459 | 164019 | } |
| | @@ -163476,30 +164036,58 @@ |
| 163476 | 164036 | u8 *pCellData, /* Raw cell content */ |
| 163477 | 164037 | RtreeSearchPoint *pSearch, /* Container of this cell */ |
| 163478 | 164038 | sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */ |
| 163479 | 164039 | int *peWithin /* OUT: visibility of the cell */ |
| 163480 | 164040 | ){ |
| 163481 | | - int i; /* Loop counter */ |
| 163482 | 164041 | sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */ |
| 163483 | 164042 | int nCoord = pInfo->nCoord; /* No. of coordinates */ |
| 163484 | 164043 | int rc; /* Callback return code */ |
| 164044 | + RtreeCoord c; /* Translator union */ |
| 163485 | 164045 | sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */ |
| 163486 | 164046 | |
| 163487 | 164047 | assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY ); |
| 163488 | 164048 | assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 ); |
| 163489 | 164049 | |
| 163490 | 164050 | if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){ |
| 163491 | 164051 | pInfo->iRowid = readInt64(pCellData); |
| 163492 | 164052 | } |
| 163493 | 164053 | pCellData += 8; |
| 163494 | | - for(i=0; i<nCoord; i++, pCellData += 4){ |
| 163495 | | - RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]); |
| 164054 | +#ifndef SQLITE_RTREE_INT_ONLY |
| 164055 | + if( eInt==0 ){ |
| 164056 | + switch( nCoord ){ |
| 164057 | + case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f; |
| 164058 | + readCoord(pCellData+32, &c); aCoord[8] = c.f; |
| 164059 | + case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f; |
| 164060 | + readCoord(pCellData+24, &c); aCoord[6] = c.f; |
| 164061 | + case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f; |
| 164062 | + readCoord(pCellData+16, &c); aCoord[4] = c.f; |
| 164063 | + case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f; |
| 164064 | + readCoord(pCellData+8, &c); aCoord[2] = c.f; |
| 164065 | + default: readCoord(pCellData+4, &c); aCoord[1] = c.f; |
| 164066 | + readCoord(pCellData, &c); aCoord[0] = c.f; |
| 164067 | + } |
| 164068 | + }else |
| 164069 | +#endif |
| 164070 | + { |
| 164071 | + switch( nCoord ){ |
| 164072 | + case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i; |
| 164073 | + readCoord(pCellData+32, &c); aCoord[8] = c.i; |
| 164074 | + case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i; |
| 164075 | + readCoord(pCellData+24, &c); aCoord[6] = c.i; |
| 164076 | + case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i; |
| 164077 | + readCoord(pCellData+16, &c); aCoord[4] = c.i; |
| 164078 | + case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i; |
| 164079 | + readCoord(pCellData+8, &c); aCoord[2] = c.i; |
| 164080 | + default: readCoord(pCellData+4, &c); aCoord[1] = c.i; |
| 164081 | + readCoord(pCellData, &c); aCoord[0] = c.i; |
| 164082 | + } |
| 163496 | 164083 | } |
| 163497 | 164084 | if( pConstraint->op==RTREE_MATCH ){ |
| 164085 | + int eWithin = 0; |
| 163498 | 164086 | rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo, |
| 163499 | | - nCoord, aCoord, &i); |
| 163500 | | - if( i==0 ) *peWithin = NOT_WITHIN; |
| 164087 | + nCoord, aCoord, &eWithin); |
| 164088 | + if( eWithin==0 ) *peWithin = NOT_WITHIN; |
| 163501 | 164089 | *prScore = RTREE_ZERO; |
| 163502 | 164090 | }else{ |
| 163503 | 164091 | pInfo->aCoord = aCoord; |
| 163504 | 164092 | pInfo->iLevel = pSearch->iLevel - 1; |
| 163505 | 164093 | pInfo->rScore = pInfo->rParentScore = pSearch->rScore; |
| | @@ -163531,10 +164119,11 @@ |
| 163531 | 164119 | */ |
| 163532 | 164120 | pCellData += 8 + 4*(p->iCoord&0xfe); |
| 163533 | 164121 | |
| 163534 | 164122 | assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE |
| 163535 | 164123 | || p->op==RTREE_GT || p->op==RTREE_EQ ); |
| 164124 | + assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */ |
| 163536 | 164125 | switch( p->op ){ |
| 163537 | 164126 | case RTREE_LE: |
| 163538 | 164127 | case RTREE_LT: |
| 163539 | 164128 | case RTREE_EQ: |
| 163540 | 164129 | RTREE_DECODE_COORD(eInt, pCellData, val); |
| | @@ -163571,10 +164160,11 @@ |
| 163571 | 164160 | RtreeDValue xN; /* Coordinate value converted to a double */ |
| 163572 | 164161 | |
| 163573 | 164162 | assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE |
| 163574 | 164163 | || p->op==RTREE_GT || p->op==RTREE_EQ ); |
| 163575 | 164164 | pCellData += 8 + p->iCoord*4; |
| 164165 | + assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */ |
| 163576 | 164166 | RTREE_DECODE_COORD(eInt, pCellData, xN); |
| 163577 | 164167 | switch( p->op ){ |
| 163578 | 164168 | case RTREE_LE: if( xN <= p->u.rValue ) return; break; |
| 163579 | 164169 | case RTREE_LT: if( xN < p->u.rValue ) return; break; |
| 163580 | 164170 | case RTREE_GE: if( xN >= p->u.rValue ) return; break; |
| | @@ -163639,11 +164229,11 @@ |
| 163639 | 164229 | if( pA->iLevel>pB->iLevel ) return +1; |
| 163640 | 164230 | return 0; |
| 163641 | 164231 | } |
| 163642 | 164232 | |
| 163643 | 164233 | /* |
| 163644 | | -** Interchange to search points in a cursor. |
| 164234 | +** Interchange two search points in a cursor. |
| 163645 | 164235 | */ |
| 163646 | 164236 | static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){ |
| 163647 | 164237 | RtreeSearchPoint t = p->aPoint[i]; |
| 163648 | 164238 | assert( i<j ); |
| 163649 | 164239 | p->aPoint[i] = p->aPoint[j]; |
| | @@ -163887,11 +164477,11 @@ |
| 163887 | 164477 | rtreeSearchPointPop(pCur); |
| 163888 | 164478 | } |
| 163889 | 164479 | if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO; |
| 163890 | 164480 | p = rtreeSearchPointNew(pCur, rScore, x.iLevel); |
| 163891 | 164481 | if( p==0 ) return SQLITE_NOMEM; |
| 163892 | | - p->eWithin = eWithin; |
| 164482 | + p->eWithin = (u8)eWithin; |
| 163893 | 164483 | p->id = x.id; |
| 163894 | 164484 | p->iCell = x.iCell; |
| 163895 | 164485 | RTREE_QUEUE_TRACE(pCur, "PUSH-S:"); |
| 163896 | 164486 | break; |
| 163897 | 164487 | } |
| | @@ -163946,11 +164536,10 @@ |
| 163946 | 164536 | if( rc ) return rc; |
| 163947 | 164537 | if( p==0 ) return SQLITE_OK; |
| 163948 | 164538 | if( i==0 ){ |
| 163949 | 164539 | sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell)); |
| 163950 | 164540 | }else{ |
| 163951 | | - if( rc ) return rc; |
| 163952 | 164541 | nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c); |
| 163953 | 164542 | #ifndef SQLITE_RTREE_INT_ONLY |
| 163954 | 164543 | if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ |
| 163955 | 164544 | sqlite3_result_double(ctx, c.f); |
| 163956 | 164545 | }else |
| | @@ -164075,11 +164664,11 @@ |
| 164075 | 164664 | assert( p!=0 ); /* Always returns pCsr->sPoint */ |
| 164076 | 164665 | pCsr->aNode[0] = pLeaf; |
| 164077 | 164666 | p->id = iNode; |
| 164078 | 164667 | p->eWithin = PARTLY_WITHIN; |
| 164079 | 164668 | rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell); |
| 164080 | | - p->iCell = iCell; |
| 164669 | + p->iCell = (u8)iCell; |
| 164081 | 164670 | RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:"); |
| 164082 | 164671 | }else{ |
| 164083 | 164672 | pCsr->atEOF = 1; |
| 164084 | 164673 | } |
| 164085 | 164674 | }else{ |
| | @@ -164108,11 +164697,11 @@ |
| 164108 | 164697 | */ |
| 164109 | 164698 | rc = deserializeGeometry(argv[ii], p); |
| 164110 | 164699 | if( rc!=SQLITE_OK ){ |
| 164111 | 164700 | break; |
| 164112 | 164701 | } |
| 164113 | | - p->pInfo->nCoord = pRtree->nDim*2; |
| 164702 | + p->pInfo->nCoord = pRtree->nDim2; |
| 164114 | 164703 | p->pInfo->anQueue = pCsr->anQueue; |
| 164115 | 164704 | p->pInfo->mxLevel = pRtree->iDepth + 1; |
| 164116 | 164705 | }else{ |
| 164117 | 164706 | #ifdef SQLITE_RTREE_INT_ONLY |
| 164118 | 164707 | p->u.rValue = sqlite3_value_int64(argv[ii]); |
| | @@ -164123,11 +164712,11 @@ |
| 164123 | 164712 | } |
| 164124 | 164713 | } |
| 164125 | 164714 | } |
| 164126 | 164715 | if( rc==SQLITE_OK ){ |
| 164127 | 164716 | RtreeSearchPoint *pNew; |
| 164128 | | - pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1); |
| 164717 | + pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1)); |
| 164129 | 164718 | if( pNew==0 ) return SQLITE_NOMEM; |
| 164130 | 164719 | pNew->id = 1; |
| 164131 | 164720 | pNew->iCell = 0; |
| 164132 | 164721 | pNew->eWithin = PARTLY_WITHIN; |
| 164133 | 164722 | assert( pCsr->bPoint==1 ); |
| | @@ -164141,23 +164730,10 @@ |
| 164141 | 164730 | nodeRelease(pRtree, pRoot); |
| 164142 | 164731 | rtreeRelease(pRtree); |
| 164143 | 164732 | return rc; |
| 164144 | 164733 | } |
| 164145 | 164734 | |
| 164146 | | -/* |
| 164147 | | -** Set the pIdxInfo->estimatedRows variable to nRow. Unless this |
| 164148 | | -** extension is currently being used by a version of SQLite too old to |
| 164149 | | -** support estimatedRows. In that case this function is a no-op. |
| 164150 | | -*/ |
| 164151 | | -static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){ |
| 164152 | | -#if SQLITE_VERSION_NUMBER>=3008002 |
| 164153 | | - if( sqlite3_libversion_number()>=3008002 ){ |
| 164154 | | - pIdxInfo->estimatedRows = nRow; |
| 164155 | | - } |
| 164156 | | -#endif |
| 164157 | | -} |
| 164158 | | - |
| 164159 | 164735 | /* |
| 164160 | 164736 | ** Rtree virtual table module xBestIndex method. There are three |
| 164161 | 164737 | ** table scan strategies to choose from (in order from most to |
| 164162 | 164738 | ** least desirable): |
| 164163 | 164739 | ** |
| | @@ -164233,11 +164809,11 @@ |
| 164233 | 164809 | ** considered almost as quick as a direct rowid lookup (for which |
| 164234 | 164810 | ** sqlite uses an internal cost of 0.0). It is expected to return |
| 164235 | 164811 | ** a single row. |
| 164236 | 164812 | */ |
| 164237 | 164813 | pIdxInfo->estimatedCost = 30.0; |
| 164238 | | - setEstimatedRows(pIdxInfo, 1); |
| 164814 | + pIdxInfo->estimatedRows = 1; |
| 164239 | 164815 | return SQLITE_OK; |
| 164240 | 164816 | } |
| 164241 | 164817 | |
| 164242 | 164818 | if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){ |
| 164243 | 164819 | u8 op; |
| | @@ -164251,11 +164827,11 @@ |
| 164251 | 164827 | assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH ); |
| 164252 | 164828 | op = RTREE_MATCH; |
| 164253 | 164829 | break; |
| 164254 | 164830 | } |
| 164255 | 164831 | zIdxStr[iIdx++] = op; |
| 164256 | | - zIdxStr[iIdx++] = p->iColumn - 1 + '0'; |
| 164832 | + zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0'); |
| 164257 | 164833 | pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2); |
| 164258 | 164834 | pIdxInfo->aConstraintUsage[ii].omit = 1; |
| 164259 | 164835 | } |
| 164260 | 164836 | } |
| 164261 | 164837 | |
| | @@ -164265,55 +164841,75 @@ |
| 164265 | 164841 | return SQLITE_NOMEM; |
| 164266 | 164842 | } |
| 164267 | 164843 | |
| 164268 | 164844 | nRow = pRtree->nRowEst >> (iIdx/2); |
| 164269 | 164845 | pIdxInfo->estimatedCost = (double)6.0 * (double)nRow; |
| 164270 | | - setEstimatedRows(pIdxInfo, nRow); |
| 164846 | + pIdxInfo->estimatedRows = nRow; |
| 164271 | 164847 | |
| 164272 | 164848 | return rc; |
| 164273 | 164849 | } |
| 164274 | 164850 | |
| 164275 | 164851 | /* |
| 164276 | 164852 | ** Return the N-dimensional volumn of the cell stored in *p. |
| 164277 | 164853 | */ |
| 164278 | 164854 | static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){ |
| 164279 | 164855 | RtreeDValue area = (RtreeDValue)1; |
| 164280 | | - int ii; |
| 164281 | | - for(ii=0; ii<(pRtree->nDim*2); ii+=2){ |
| 164282 | | - area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]))); |
| 164856 | + assert( pRtree->nDim>=1 && pRtree->nDim<=5 ); |
| 164857 | +#ifndef SQLITE_RTREE_INT_ONLY |
| 164858 | + if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ |
| 164859 | + switch( pRtree->nDim ){ |
| 164860 | + case 5: area = p->aCoord[9].f - p->aCoord[8].f; |
| 164861 | + case 4: area *= p->aCoord[7].f - p->aCoord[6].f; |
| 164862 | + case 3: area *= p->aCoord[5].f - p->aCoord[4].f; |
| 164863 | + case 2: area *= p->aCoord[3].f - p->aCoord[2].f; |
| 164864 | + default: area *= p->aCoord[1].f - p->aCoord[0].f; |
| 164865 | + } |
| 164866 | + }else |
| 164867 | +#endif |
| 164868 | + { |
| 164869 | + switch( pRtree->nDim ){ |
| 164870 | + case 5: area = p->aCoord[9].i - p->aCoord[8].i; |
| 164871 | + case 4: area *= p->aCoord[7].i - p->aCoord[6].i; |
| 164872 | + case 3: area *= p->aCoord[5].i - p->aCoord[4].i; |
| 164873 | + case 2: area *= p->aCoord[3].i - p->aCoord[2].i; |
| 164874 | + default: area *= p->aCoord[1].i - p->aCoord[0].i; |
| 164875 | + } |
| 164283 | 164876 | } |
| 164284 | 164877 | return area; |
| 164285 | 164878 | } |
| 164286 | 164879 | |
| 164287 | 164880 | /* |
| 164288 | 164881 | ** Return the margin length of cell p. The margin length is the sum |
| 164289 | 164882 | ** of the objects size in each dimension. |
| 164290 | 164883 | */ |
| 164291 | 164884 | static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){ |
| 164292 | | - RtreeDValue margin = (RtreeDValue)0; |
| 164293 | | - int ii; |
| 164294 | | - for(ii=0; ii<(pRtree->nDim*2); ii+=2){ |
| 164885 | + RtreeDValue margin = 0; |
| 164886 | + int ii = pRtree->nDim2 - 2; |
| 164887 | + do{ |
| 164295 | 164888 | margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])); |
| 164296 | | - } |
| 164889 | + ii -= 2; |
| 164890 | + }while( ii>=0 ); |
| 164297 | 164891 | return margin; |
| 164298 | 164892 | } |
| 164299 | 164893 | |
| 164300 | 164894 | /* |
| 164301 | 164895 | ** Store the union of cells p1 and p2 in p1. |
| 164302 | 164896 | */ |
| 164303 | 164897 | static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){ |
| 164304 | | - int ii; |
| 164898 | + int ii = 0; |
| 164305 | 164899 | if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ |
| 164306 | | - for(ii=0; ii<(pRtree->nDim*2); ii+=2){ |
| 164900 | + do{ |
| 164307 | 164901 | p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f); |
| 164308 | 164902 | p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f); |
| 164309 | | - } |
| 164903 | + ii += 2; |
| 164904 | + }while( ii<pRtree->nDim2 ); |
| 164310 | 164905 | }else{ |
| 164311 | | - for(ii=0; ii<(pRtree->nDim*2); ii+=2){ |
| 164906 | + do{ |
| 164312 | 164907 | p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i); |
| 164313 | 164908 | p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i); |
| 164314 | | - } |
| 164909 | + ii += 2; |
| 164910 | + }while( ii<pRtree->nDim2 ); |
| 164315 | 164911 | } |
| 164316 | 164912 | } |
| 164317 | 164913 | |
| 164318 | 164914 | /* |
| 164319 | 164915 | ** Return true if the area covered by p2 is a subset of the area covered |
| | @@ -164320,11 +164916,11 @@ |
| 164320 | 164916 | ** by p1. False otherwise. |
| 164321 | 164917 | */ |
| 164322 | 164918 | static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){ |
| 164323 | 164919 | int ii; |
| 164324 | 164920 | int isInt = (pRtree->eCoordType==RTREE_COORD_INT32); |
| 164325 | | - for(ii=0; ii<(pRtree->nDim*2); ii+=2){ |
| 164921 | + for(ii=0; ii<pRtree->nDim2; ii+=2){ |
| 164326 | 164922 | RtreeCoord *a1 = &p1->aCoord[ii]; |
| 164327 | 164923 | RtreeCoord *a2 = &p2->aCoord[ii]; |
| 164328 | 164924 | if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) |
| 164329 | 164925 | || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i)) |
| 164330 | 164926 | ){ |
| | @@ -164355,11 +164951,11 @@ |
| 164355 | 164951 | int ii; |
| 164356 | 164952 | RtreeDValue overlap = RTREE_ZERO; |
| 164357 | 164953 | for(ii=0; ii<nCell; ii++){ |
| 164358 | 164954 | int jj; |
| 164359 | 164955 | RtreeDValue o = (RtreeDValue)1; |
| 164360 | | - for(jj=0; jj<(pRtree->nDim*2); jj+=2){ |
| 164956 | + for(jj=0; jj<pRtree->nDim2; jj+=2){ |
| 164361 | 164957 | RtreeDValue x1, x2; |
| 164362 | 164958 | x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj])); |
| 164363 | 164959 | x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1])); |
| 164364 | 164960 | if( x2<x1 ){ |
| 164365 | 164961 | o = (RtreeDValue)0; |
| | @@ -165411,11 +166007,11 @@ |
| 165411 | 166007 | ** with "column" that are interpreted as table constraints. |
| 165412 | 166008 | ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5)); |
| 165413 | 166009 | ** This problem was discovered after years of use, so we silently ignore |
| 165414 | 166010 | ** these kinds of misdeclared tables to avoid breaking any legacy. |
| 165415 | 166011 | */ |
| 165416 | | - assert( nData<=(pRtree->nDim*2 + 3) ); |
| 166012 | + assert( nData<=(pRtree->nDim2 + 3) ); |
| 165417 | 166013 | |
| 165418 | 166014 | #ifndef SQLITE_RTREE_INT_ONLY |
| 165419 | 166015 | if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ |
| 165420 | 166016 | for(ii=0; ii<nData-4; ii+=2){ |
| 165421 | 166017 | cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]); |
| | @@ -165500,10 +166096,31 @@ |
| 165500 | 166096 | |
| 165501 | 166097 | constraint: |
| 165502 | 166098 | rtreeRelease(pRtree); |
| 165503 | 166099 | return rc; |
| 165504 | 166100 | } |
| 166101 | + |
| 166102 | +/* |
| 166103 | +** Called when a transaction starts. |
| 166104 | +*/ |
| 166105 | +static int rtreeBeginTransaction(sqlite3_vtab *pVtab){ |
| 166106 | + Rtree *pRtree = (Rtree *)pVtab; |
| 166107 | + assert( pRtree->inWrTrans==0 ); |
| 166108 | + pRtree->inWrTrans++; |
| 166109 | + return SQLITE_OK; |
| 166110 | +} |
| 166111 | + |
| 166112 | +/* |
| 166113 | +** Called when a transaction completes (either by COMMIT or ROLLBACK). |
| 166114 | +** The sqlite3_blob object should be released at this point. |
| 166115 | +*/ |
| 166116 | +static int rtreeEndTransaction(sqlite3_vtab *pVtab){ |
| 166117 | + Rtree *pRtree = (Rtree *)pVtab; |
| 166118 | + pRtree->inWrTrans = 0; |
| 166119 | + nodeBlobReset(pRtree); |
| 166120 | + return SQLITE_OK; |
| 166121 | +} |
| 165505 | 166122 | |
| 165506 | 166123 | /* |
| 165507 | 166124 | ** The xRename method for rtree module virtual tables. |
| 165508 | 166125 | */ |
| 165509 | 166126 | static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){ |
| | @@ -165521,10 +166138,11 @@ |
| 165521 | 166138 | rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0); |
| 165522 | 166139 | sqlite3_free(zSql); |
| 165523 | 166140 | } |
| 165524 | 166141 | return rc; |
| 165525 | 166142 | } |
| 166143 | + |
| 165526 | 166144 | |
| 165527 | 166145 | /* |
| 165528 | 166146 | ** This function populates the pRtree->nRowEst variable with an estimate |
| 165529 | 166147 | ** of the number of rows in the virtual table. If possible, this is based |
| 165530 | 166148 | ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST. |
| | @@ -165581,19 +166199,19 @@ |
| 165581 | 166199 | rtreeNext, /* xNext - advance a cursor */ |
| 165582 | 166200 | rtreeEof, /* xEof */ |
| 165583 | 166201 | rtreeColumn, /* xColumn - read data */ |
| 165584 | 166202 | rtreeRowid, /* xRowid - read data */ |
| 165585 | 166203 | rtreeUpdate, /* xUpdate - write data */ |
| 165586 | | - 0, /* xBegin - begin transaction */ |
| 165587 | | - 0, /* xSync - sync transaction */ |
| 165588 | | - 0, /* xCommit - commit transaction */ |
| 165589 | | - 0, /* xRollback - rollback transaction */ |
| 166204 | + rtreeBeginTransaction, /* xBegin - begin transaction */ |
| 166205 | + rtreeEndTransaction, /* xSync - sync transaction */ |
| 166206 | + rtreeEndTransaction, /* xCommit - commit transaction */ |
| 166207 | + rtreeEndTransaction, /* xRollback - rollback transaction */ |
| 165590 | 166208 | 0, /* xFindFunction - function overloading */ |
| 165591 | 166209 | rtreeRename, /* xRename - rename the table */ |
| 165592 | 166210 | 0, /* xSavepoint */ |
| 165593 | 166211 | 0, /* xRelease */ |
| 165594 | | - 0 /* xRollbackTo */ |
| 166212 | + 0, /* xRollbackTo */ |
| 165595 | 166213 | }; |
| 165596 | 166214 | |
| 165597 | 166215 | static int rtreeSqlInit( |
| 165598 | 166216 | Rtree *pRtree, |
| 165599 | 166217 | sqlite3 *db, |
| | @@ -165601,14 +166219,13 @@ |
| 165601 | 166219 | const char *zPrefix, |
| 165602 | 166220 | int isCreate |
| 165603 | 166221 | ){ |
| 165604 | 166222 | int rc = SQLITE_OK; |
| 165605 | 166223 | |
| 165606 | | - #define N_STATEMENT 9 |
| 166224 | + #define N_STATEMENT 8 |
| 165607 | 166225 | static const char *azSql[N_STATEMENT] = { |
| 165608 | | - /* Read and write the xxx_node table */ |
| 165609 | | - "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1", |
| 166226 | + /* Write the xxx_node table */ |
| 165610 | 166227 | "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)", |
| 165611 | 166228 | "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1", |
| 165612 | 166229 | |
| 165613 | 166230 | /* Read and write the xxx_rowid table */ |
| 165614 | 166231 | "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1", |
| | @@ -165642,19 +166259,18 @@ |
| 165642 | 166259 | if( rc!=SQLITE_OK ){ |
| 165643 | 166260 | return rc; |
| 165644 | 166261 | } |
| 165645 | 166262 | } |
| 165646 | 166263 | |
| 165647 | | - appStmt[0] = &pRtree->pReadNode; |
| 165648 | | - appStmt[1] = &pRtree->pWriteNode; |
| 165649 | | - appStmt[2] = &pRtree->pDeleteNode; |
| 165650 | | - appStmt[3] = &pRtree->pReadRowid; |
| 165651 | | - appStmt[4] = &pRtree->pWriteRowid; |
| 165652 | | - appStmt[5] = &pRtree->pDeleteRowid; |
| 165653 | | - appStmt[6] = &pRtree->pReadParent; |
| 165654 | | - appStmt[7] = &pRtree->pWriteParent; |
| 165655 | | - appStmt[8] = &pRtree->pDeleteParent; |
| 166264 | + appStmt[0] = &pRtree->pWriteNode; |
| 166265 | + appStmt[1] = &pRtree->pDeleteNode; |
| 166266 | + appStmt[2] = &pRtree->pReadRowid; |
| 166267 | + appStmt[3] = &pRtree->pWriteRowid; |
| 166268 | + appStmt[4] = &pRtree->pDeleteRowid; |
| 166269 | + appStmt[5] = &pRtree->pReadParent; |
| 166270 | + appStmt[6] = &pRtree->pWriteParent; |
| 166271 | + appStmt[7] = &pRtree->pDeleteParent; |
| 165656 | 166272 | |
| 165657 | 166273 | rc = rtreeQueryStat1(db, pRtree); |
| 165658 | 166274 | for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){ |
| 165659 | 166275 | char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix); |
| 165660 | 166276 | if( zSql ){ |
| | @@ -165788,13 +166404,14 @@ |
| 165788 | 166404 | memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2); |
| 165789 | 166405 | pRtree->nBusy = 1; |
| 165790 | 166406 | pRtree->base.pModule = &rtreeModule; |
| 165791 | 166407 | pRtree->zDb = (char *)&pRtree[1]; |
| 165792 | 166408 | pRtree->zName = &pRtree->zDb[nDb+1]; |
| 165793 | | - pRtree->nDim = (argc-4)/2; |
| 165794 | | - pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2; |
| 165795 | | - pRtree->eCoordType = eCoordType; |
| 166409 | + pRtree->nDim = (u8)((argc-4)/2); |
| 166410 | + pRtree->nDim2 = pRtree->nDim*2; |
| 166411 | + pRtree->nBytesPerCell = 8 + pRtree->nDim2*4; |
| 166412 | + pRtree->eCoordType = (u8)eCoordType; |
| 165796 | 166413 | memcpy(pRtree->zDb, argv[1], nDb); |
| 165797 | 166414 | memcpy(pRtree->zName, argv[2], nName); |
| 165798 | 166415 | |
| 165799 | 166416 | /* Figure out the node size to use. */ |
| 165800 | 166417 | rc = getNodeSize(db, pRtree, isCreate, pzErr); |
| | @@ -165863,11 +166480,12 @@ |
| 165863 | 166480 | int ii; |
| 165864 | 166481 | |
| 165865 | 166482 | UNUSED_PARAMETER(nArg); |
| 165866 | 166483 | memset(&node, 0, sizeof(RtreeNode)); |
| 165867 | 166484 | memset(&tree, 0, sizeof(Rtree)); |
| 165868 | | - tree.nDim = sqlite3_value_int(apArg[0]); |
| 166485 | + tree.nDim = (u8)sqlite3_value_int(apArg[0]); |
| 166486 | + tree.nDim2 = tree.nDim*2; |
| 165869 | 166487 | tree.nBytesPerCell = 8 + 8 * tree.nDim; |
| 165870 | 166488 | node.zData = (u8 *)sqlite3_value_blob(apArg[1]); |
| 165871 | 166489 | |
| 165872 | 166490 | for(ii=0; ii<NCELL(&node); ii++){ |
| 165873 | 166491 | char zCell[512]; |
| | @@ -165876,11 +166494,11 @@ |
| 165876 | 166494 | int jj; |
| 165877 | 166495 | |
| 165878 | 166496 | nodeGetCell(&tree, &node, ii, &cell); |
| 165879 | 166497 | sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid); |
| 165880 | 166498 | nCell = (int)strlen(zCell); |
| 165881 | | - for(jj=0; jj<tree.nDim*2; jj++){ |
| 166499 | + for(jj=0; jj<tree.nDim2; jj++){ |
| 165882 | 166500 | #ifndef SQLITE_RTREE_INT_ONLY |
| 165883 | 166501 | sqlite3_snprintf(512-nCell,&zCell[nCell], " %g", |
| 165884 | 166502 | (double)cell.aCoord[jj].f); |
| 165885 | 166503 | #else |
| 165886 | 166504 | sqlite3_snprintf(512-nCell,&zCell[nCell], " %d", |
| | @@ -166584,42 +167202,40 @@ |
| 166584 | 167202 | |
| 166585 | 167203 | /* |
| 166586 | 167204 | ** Register the ICU extension functions with database db. |
| 166587 | 167205 | */ |
| 166588 | 167206 | SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){ |
| 166589 | | - struct IcuScalar { |
| 167207 | + static const struct IcuScalar { |
| 166590 | 167208 | const char *zName; /* Function name */ |
| 166591 | | - int nArg; /* Number of arguments */ |
| 166592 | | - int enc; /* Optimal text encoding */ |
| 166593 | | - void *pContext; /* sqlite3_user_data() context */ |
| 167209 | + unsigned char nArg; /* Number of arguments */ |
| 167210 | + unsigned short enc; /* Optimal text encoding */ |
| 167211 | + unsigned char iContext; /* sqlite3_user_data() context */ |
| 166594 | 167212 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
| 166595 | 167213 | } scalars[] = { |
| 166596 | | - {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc}, |
| 166597 | | - |
| 166598 | | - {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 166599 | | - {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 166600 | | - {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16}, |
| 166601 | | - {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16}, |
| 166602 | | - |
| 166603 | | - {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 166604 | | - {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 166605 | | - {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16}, |
| 166606 | | - {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16}, |
| 166607 | | - |
| 166608 | | - {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc}, |
| 166609 | | - {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc}, |
| 166610 | | - |
| 166611 | | - {"icu_load_collation", 2, SQLITE_UTF8, (void*)db, icuLoadCollation}, |
| 167214 | + {"icu_load_collation", 2, SQLITE_UTF8, 1, icuLoadCollation}, |
| 167215 | + {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc}, |
| 167216 | + {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 167217 | + {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 167218 | + {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16}, |
| 167219 | + {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16}, |
| 167220 | + {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 167221 | + {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16}, |
| 167222 | + {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16}, |
| 167223 | + {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16}, |
| 167224 | + {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc}, |
| 167225 | + {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc}, |
| 166612 | 167226 | }; |
| 166613 | | - |
| 166614 | 167227 | int rc = SQLITE_OK; |
| 166615 | 167228 | int i; |
| 166616 | 167229 | |
| 167230 | + |
| 166617 | 167231 | for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){ |
| 166618 | | - struct IcuScalar *p = &scalars[i]; |
| 167232 | + const struct IcuScalar *p = &scalars[i]; |
| 166619 | 167233 | rc = sqlite3_create_function( |
| 166620 | | - db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0 |
| 167234 | + db, p->zName, p->nArg, p->enc, |
| 167235 | + p->iContext ? (void*)db : (void*)0, |
| 167236 | + p->xFunc, 0, 0 |
| 166621 | 167237 | ); |
| 166622 | 167238 | } |
| 166623 | 167239 | |
| 166624 | 167240 | return rc; |
| 166625 | 167241 | } |
| | @@ -169823,11 +170439,11 @@ |
| 169823 | 170439 | |
| 169824 | 170440 | /* |
| 169825 | 170441 | ** Open the database handle and attach the RBU database as "rbu". If an |
| 169826 | 170442 | ** error occurs, leave an error code and message in the RBU handle. |
| 169827 | 170443 | */ |
| 169828 | | -static void rbuOpenDatabase(sqlite3rbu *p){ |
| 170444 | +static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){ |
| 169829 | 170445 | assert( p->rc || (p->dbMain==0 && p->dbRbu==0) ); |
| 169830 | 170446 | assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 ); |
| 169831 | 170447 | |
| 169832 | 170448 | /* Open the RBU database */ |
| 169833 | 170449 | p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1); |
| | @@ -169898,11 +170514,11 @@ |
| 169898 | 170514 | if( p->eStage>=RBU_STAGE_MOVE ){ |
| 169899 | 170515 | bOpen = 1; |
| 169900 | 170516 | }else{ |
| 169901 | 170517 | RbuState *pState = rbuLoadState(p); |
| 169902 | 170518 | if( pState ){ |
| 169903 | | - bOpen = (pState->eStage>RBU_STAGE_MOVE); |
| 170519 | + bOpen = (pState->eStage>=RBU_STAGE_MOVE); |
| 169904 | 170520 | rbuFreeState(pState); |
| 169905 | 170521 | } |
| 169906 | 170522 | } |
| 169907 | 170523 | if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1); |
| 169908 | 170524 | } |
| | @@ -169910,10 +170526,19 @@ |
| 169910 | 170526 | p->eStage = 0; |
| 169911 | 170527 | if( p->rc==SQLITE_OK && p->dbMain==0 ){ |
| 169912 | 170528 | if( !rbuIsVacuum(p) ){ |
| 169913 | 170529 | p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1); |
| 169914 | 170530 | }else if( p->pRbuFd->pWalFd ){ |
| 170531 | + if( pbRetry ){ |
| 170532 | + p->pRbuFd->bNolock = 0; |
| 170533 | + sqlite3_close(p->dbRbu); |
| 170534 | + sqlite3_close(p->dbMain); |
| 170535 | + p->dbMain = 0; |
| 170536 | + p->dbRbu = 0; |
| 170537 | + *pbRetry = 1; |
| 170538 | + return; |
| 170539 | + } |
| 169915 | 170540 | p->rc = SQLITE_ERROR; |
| 169916 | 170541 | p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database"); |
| 169917 | 170542 | }else{ |
| 169918 | 170543 | char *zTarget; |
| 169919 | 170544 | char *zExtra = 0; |
| | @@ -170090,20 +170715,22 @@ |
| 170090 | 170715 | p->eStage = RBU_STAGE_CAPTURE; |
| 170091 | 170716 | rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0); |
| 170092 | 170717 | if( rc2!=SQLITE_INTERNAL ) p->rc = rc2; |
| 170093 | 170718 | } |
| 170094 | 170719 | |
| 170095 | | - if( p->rc==SQLITE_OK ){ |
| 170720 | + if( p->rc==SQLITE_OK && p->nFrame>0 ){ |
| 170096 | 170721 | p->eStage = RBU_STAGE_CKPT; |
| 170097 | 170722 | p->nStep = (pState ? pState->nRow : 0); |
| 170098 | 170723 | p->aBuf = rbuMalloc(p, p->pgsz); |
| 170099 | 170724 | p->iWalCksum = rbuShmChecksum(p); |
| 170100 | 170725 | } |
| 170101 | 170726 | |
| 170102 | | - if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){ |
| 170103 | | - p->rc = SQLITE_DONE; |
| 170104 | | - p->eStage = RBU_STAGE_DONE; |
| 170727 | + if( p->rc==SQLITE_OK ){ |
| 170728 | + if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){ |
| 170729 | + p->rc = SQLITE_DONE; |
| 170730 | + p->eStage = RBU_STAGE_DONE; |
| 170731 | + } |
| 170105 | 170732 | } |
| 170106 | 170733 | } |
| 170107 | 170734 | |
| 170108 | 170735 | /* |
| 170109 | 170736 | ** Called when iAmt bytes are read from offset iOff of the wal file while |
| | @@ -170272,11 +170899,11 @@ |
| 170272 | 170899 | #else |
| 170273 | 170900 | p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK; |
| 170274 | 170901 | #endif |
| 170275 | 170902 | |
| 170276 | 170903 | if( p->rc==SQLITE_OK ){ |
| 170277 | | - rbuOpenDatabase(p); |
| 170904 | + rbuOpenDatabase(p, 0); |
| 170278 | 170905 | rbuSetupCheckpoint(p, 0); |
| 170279 | 170906 | } |
| 170280 | 170907 | } |
| 170281 | 170908 | } |
| 170282 | 170909 | |
| | @@ -170983,10 +171610,11 @@ |
| 170983 | 171610 | rbuCreateVfs(p); |
| 170984 | 171611 | |
| 170985 | 171612 | /* Open the target, RBU and state databases */ |
| 170986 | 171613 | if( p->rc==SQLITE_OK ){ |
| 170987 | 171614 | char *pCsr = (char*)&p[1]; |
| 171615 | + int bRetry = 0; |
| 170988 | 171616 | if( zTarget ){ |
| 170989 | 171617 | p->zTarget = pCsr; |
| 170990 | 171618 | memcpy(p->zTarget, zTarget, nTarget+1); |
| 170991 | 171619 | pCsr += nTarget+1; |
| 170992 | 171620 | } |
| | @@ -170994,11 +171622,22 @@ |
| 170994 | 171622 | memcpy(p->zRbu, zRbu, nRbu+1); |
| 170995 | 171623 | pCsr += nRbu+1; |
| 170996 | 171624 | if( zState ){ |
| 170997 | 171625 | p->zState = rbuMPrintf(p, "%s", zState); |
| 170998 | 171626 | } |
| 170999 | | - rbuOpenDatabase(p); |
| 171627 | + |
| 171628 | + /* If the first attempt to open the database file fails and the bRetry |
| 171629 | + ** flag it set, this means that the db was not opened because it seemed |
| 171630 | + ** to be a wal-mode db. But, this may have happened due to an earlier |
| 171631 | + ** RBU vacuum operation leaving an old wal file in the directory. |
| 171632 | + ** If this is the case, it will have been checkpointed and deleted |
| 171633 | + ** when the handle was closed and a second attempt to open the |
| 171634 | + ** database may succeed. */ |
| 171635 | + rbuOpenDatabase(p, &bRetry); |
| 171636 | + if( bRetry ){ |
| 171637 | + rbuOpenDatabase(p, 0); |
| 171638 | + } |
| 171000 | 171639 | } |
| 171001 | 171640 | |
| 171002 | 171641 | if( p->rc==SQLITE_OK ){ |
| 171003 | 171642 | pState = rbuLoadState(p); |
| 171004 | 171643 | assert( pState || p->rc!=SQLITE_OK ); |
| | @@ -175957,11 +176596,11 @@ |
| 175957 | 176596 | sqlite3_value **ppValue /* OUT: Value from conflicting row */ |
| 175958 | 176597 | ){ |
| 175959 | 176598 | if( !pIter->pConflict ){ |
| 175960 | 176599 | return SQLITE_MISUSE; |
| 175961 | 176600 | } |
| 175962 | | - if( iVal<0 || iVal>=sqlite3_column_count(pIter->pConflict) ){ |
| 176601 | + if( iVal<0 || iVal>=pIter->nCol ){ |
| 175963 | 176602 | return SQLITE_RANGE; |
| 175964 | 176603 | } |
| 175965 | 176604 | *ppValue = sqlite3_column_value(pIter->pConflict, iVal); |
| 175966 | 176605 | return SQLITE_OK; |
| 175967 | 176606 | } |
| | @@ -176424,11 +177063,17 @@ |
| 176424 | 177063 | int i; |
| 176425 | 177064 | SessionBuffer buf = {0, 0, 0}; |
| 176426 | 177065 | |
| 176427 | 177066 | sessionAppendStr(&buf, "INSERT INTO main.", &rc); |
| 176428 | 177067 | sessionAppendIdent(&buf, zTab, &rc); |
| 176429 | | - sessionAppendStr(&buf, " VALUES(?", &rc); |
| 177068 | + sessionAppendStr(&buf, "(", &rc); |
| 177069 | + for(i=0; i<p->nCol; i++){ |
| 177070 | + if( i!=0 ) sessionAppendStr(&buf, ", ", &rc); |
| 177071 | + sessionAppendIdent(&buf, p->azCol[i], &rc); |
| 177072 | + } |
| 177073 | + |
| 177074 | + sessionAppendStr(&buf, ") VALUES(?", &rc); |
| 176430 | 177075 | for(i=1; i<p->nCol; i++){ |
| 176431 | 177076 | sessionAppendStr(&buf, ", ?", &rc); |
| 176432 | 177077 | } |
| 176433 | 177078 | sessionAppendStr(&buf, ")", &rc); |
| 176434 | 177079 | |
| | @@ -176970,42 +177615,51 @@ |
| 176970 | 177615 | break; |
| 176971 | 177616 | } |
| 176972 | 177617 | nTab = (int)strlen(zTab); |
| 176973 | 177618 | sApply.azCol = (const char **)zTab; |
| 176974 | 177619 | }else{ |
| 177620 | + int nMinCol = 0; |
| 177621 | + int i; |
| 177622 | + |
| 176975 | 177623 | sqlite3changeset_pk(pIter, &abPK, 0); |
| 176976 | 177624 | rc = sessionTableInfo( |
| 176977 | 177625 | db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK |
| 176978 | 177626 | ); |
| 176979 | 177627 | if( rc!=SQLITE_OK ) break; |
| 177628 | + for(i=0; i<sApply.nCol; i++){ |
| 177629 | + if( sApply.abPK[i] ) nMinCol = i+1; |
| 177630 | + } |
| 176980 | 177631 | |
| 176981 | 177632 | if( sApply.nCol==0 ){ |
| 176982 | 177633 | schemaMismatch = 1; |
| 176983 | 177634 | sqlite3_log(SQLITE_SCHEMA, |
| 176984 | 177635 | "sqlite3changeset_apply(): no such table: %s", zTab |
| 176985 | 177636 | ); |
| 176986 | 177637 | } |
| 176987 | | - else if( sApply.nCol!=nCol ){ |
| 177638 | + else if( sApply.nCol<nCol ){ |
| 176988 | 177639 | schemaMismatch = 1; |
| 176989 | 177640 | sqlite3_log(SQLITE_SCHEMA, |
| 176990 | | - "sqlite3changeset_apply(): table %s has %d columns, expected %d", |
| 177641 | + "sqlite3changeset_apply(): table %s has %d columns, " |
| 177642 | + "expected %d or more", |
| 176991 | 177643 | zTab, sApply.nCol, nCol |
| 176992 | 177644 | ); |
| 176993 | 177645 | } |
| 176994 | | - else if( memcmp(sApply.abPK, abPK, nCol)!=0 ){ |
| 177646 | + else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){ |
| 176995 | 177647 | schemaMismatch = 1; |
| 176996 | 177648 | sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): " |
| 176997 | 177649 | "primary key mismatch for table %s", zTab |
| 176998 | 177650 | ); |
| 176999 | 177651 | } |
| 177000 | | - else if( |
| 177001 | | - (rc = sessionSelectRow(db, zTab, &sApply)) |
| 177002 | | - || (rc = sessionUpdateRow(db, zTab, &sApply)) |
| 177003 | | - || (rc = sessionDeleteRow(db, zTab, &sApply)) |
| 177004 | | - || (rc = sessionInsertRow(db, zTab, &sApply)) |
| 177005 | | - ){ |
| 177006 | | - break; |
| 177652 | + else{ |
| 177653 | + sApply.nCol = nCol; |
| 177654 | + if((rc = sessionSelectRow(db, zTab, &sApply)) |
| 177655 | + || (rc = sessionUpdateRow(db, zTab, &sApply)) |
| 177656 | + || (rc = sessionDeleteRow(db, zTab, &sApply)) |
| 177657 | + || (rc = sessionInsertRow(db, zTab, &sApply)) |
| 177658 | + ){ |
| 177659 | + break; |
| 177660 | + } |
| 177007 | 177661 | } |
| 177008 | 177662 | nTab = sqlite3Strlen30(zTab); |
| 177009 | 177663 | } |
| 177010 | 177664 | } |
| 177011 | 177665 | |
| | @@ -177593,11 +178247,11 @@ |
| 177593 | 178247 | ** a BLOB, but there is no support for JSONB in the current implementation. |
| 177594 | 178248 | ** This implementation parses JSON text at 250 MB/s, so it is hard to see |
| 177595 | 178249 | ** how JSONB might improve on that.) |
| 177596 | 178250 | */ |
| 177597 | 178251 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) |
| 177598 | | -#if !defined(_SQLITEINT_H_) |
| 178252 | +#if !defined(SQLITEINT_H) |
| 177599 | 178253 | /* #include "sqlite3ext.h" */ |
| 177600 | 178254 | #endif |
| 177601 | 178255 | SQLITE_EXTENSION_INIT1 |
| 177602 | 178256 | /* #include <assert.h> */ |
| 177603 | 178257 | /* #include <string.h> */ |
| | @@ -181644,10 +182298,35 @@ |
| 181644 | 182298 | */ |
| 181645 | 182299 | #ifndef fts5YYMALLOCARGTYPE |
| 181646 | 182300 | # define fts5YYMALLOCARGTYPE size_t |
| 181647 | 182301 | #endif |
| 181648 | 182302 | |
| 182303 | +/* Initialize a new parser that has already been allocated. |
| 182304 | +*/ |
| 182305 | +static void sqlite3Fts5ParserInit(void *fts5yypParser){ |
| 182306 | + fts5yyParser *pParser = (fts5yyParser*)fts5yypParser; |
| 182307 | +#ifdef fts5YYTRACKMAXSTACKDEPTH |
| 182308 | + pParser->fts5yyhwm = 0; |
| 182309 | +#endif |
| 182310 | +#if fts5YYSTACKDEPTH<=0 |
| 182311 | + pParser->fts5yytos = NULL; |
| 182312 | + pParser->fts5yystack = NULL; |
| 182313 | + pParser->fts5yystksz = 0; |
| 182314 | + if( fts5yyGrowStack(pParser) ){ |
| 182315 | + pParser->fts5yystack = &pParser->fts5yystk0; |
| 182316 | + pParser->fts5yystksz = 1; |
| 182317 | + } |
| 182318 | +#endif |
| 182319 | +#ifndef fts5YYNOERRORRECOVERY |
| 182320 | + pParser->fts5yyerrcnt = -1; |
| 182321 | +#endif |
| 182322 | + pParser->fts5yytos = pParser->fts5yystack; |
| 182323 | + pParser->fts5yystack[0].stateno = 0; |
| 182324 | + pParser->fts5yystack[0].major = 0; |
| 182325 | +} |
| 182326 | + |
| 182327 | +#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK |
| 181649 | 182328 | /* |
| 181650 | 182329 | ** This function allocates a new parser. |
| 181651 | 182330 | ** The only argument is a pointer to a function which works like |
| 181652 | 182331 | ** malloc. |
| 181653 | 182332 | ** |
| | @@ -181659,32 +182338,15 @@ |
| 181659 | 182338 | ** to sqlite3Fts5Parser and sqlite3Fts5ParserFree. |
| 181660 | 182339 | */ |
| 181661 | 182340 | static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE)){ |
| 181662 | 182341 | fts5yyParser *pParser; |
| 181663 | 182342 | pParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) ); |
| 181664 | | - if( pParser ){ |
| 181665 | | -#ifdef fts5YYTRACKMAXSTACKDEPTH |
| 181666 | | - pParser->fts5yyhwm = 0; |
| 181667 | | -#endif |
| 181668 | | -#if fts5YYSTACKDEPTH<=0 |
| 181669 | | - pParser->fts5yytos = NULL; |
| 181670 | | - pParser->fts5yystack = NULL; |
| 181671 | | - pParser->fts5yystksz = 0; |
| 181672 | | - if( fts5yyGrowStack(pParser) ){ |
| 181673 | | - pParser->fts5yystack = &pParser->fts5yystk0; |
| 181674 | | - pParser->fts5yystksz = 1; |
| 181675 | | - } |
| 181676 | | -#endif |
| 181677 | | -#ifndef fts5YYNOERRORRECOVERY |
| 181678 | | - pParser->fts5yyerrcnt = -1; |
| 181679 | | -#endif |
| 181680 | | - pParser->fts5yytos = pParser->fts5yystack; |
| 181681 | | - pParser->fts5yystack[0].stateno = 0; |
| 181682 | | - pParser->fts5yystack[0].major = 0; |
| 181683 | | - } |
| 182343 | + if( pParser ) sqlite3Fts5ParserInit(pParser); |
| 181684 | 182344 | return pParser; |
| 181685 | 182345 | } |
| 182346 | +#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */ |
| 182347 | + |
| 181686 | 182348 | |
| 181687 | 182349 | /* The following function deletes the "minor type" or semantic value |
| 181688 | 182350 | ** associated with a symbol. The symbol can be either a terminal |
| 181689 | 182351 | ** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is |
| 181690 | 182352 | ** a pointer to the value to be deleted. The code used to do the |
| | @@ -181762,10 +182424,22 @@ |
| 181762 | 182424 | } |
| 181763 | 182425 | #endif |
| 181764 | 182426 | fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor); |
| 181765 | 182427 | } |
| 181766 | 182428 | |
| 182429 | +/* |
| 182430 | +** Clear all secondary memory allocations from the parser |
| 182431 | +*/ |
| 182432 | +static void sqlite3Fts5ParserFinalize(void *p){ |
| 182433 | + fts5yyParser *pParser = (fts5yyParser*)p; |
| 182434 | + while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser); |
| 182435 | +#if fts5YYSTACKDEPTH<=0 |
| 182436 | + if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack); |
| 182437 | +#endif |
| 182438 | +} |
| 182439 | + |
| 182440 | +#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK |
| 181767 | 182441 | /* |
| 181768 | 182442 | ** Deallocate and destroy a parser. Destructors are called for |
| 181769 | 182443 | ** all stack elements before shutting the parser down. |
| 181770 | 182444 | ** |
| 181771 | 182445 | ** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it |
| | @@ -181774,20 +182448,17 @@ |
| 181774 | 182448 | */ |
| 181775 | 182449 | static void sqlite3Fts5ParserFree( |
| 181776 | 182450 | void *p, /* The parser to be deleted */ |
| 181777 | 182451 | void (*freeProc)(void*) /* Function used to reclaim memory */ |
| 181778 | 182452 | ){ |
| 181779 | | - fts5yyParser *pParser = (fts5yyParser*)p; |
| 181780 | 182453 | #ifndef fts5YYPARSEFREENEVERNULL |
| 181781 | | - if( pParser==0 ) return; |
| 181782 | | -#endif |
| 181783 | | - while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser); |
| 181784 | | -#if fts5YYSTACKDEPTH<=0 |
| 181785 | | - if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack); |
| 181786 | | -#endif |
| 181787 | | - (*freeProc)((void*)pParser); |
| 181788 | | -} |
| 182454 | + if( p==0 ) return; |
| 182455 | +#endif |
| 182456 | + sqlite3Fts5ParserFinalize(p); |
| 182457 | + (*freeProc)(p); |
| 182458 | +} |
| 182459 | +#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */ |
| 181789 | 182460 | |
| 181790 | 182461 | /* |
| 181791 | 182462 | ** Return the peak depth of the stack for a parser. |
| 181792 | 182463 | */ |
| 181793 | 182464 | #ifdef fts5YYTRACKMAXSTACKDEPTH |
| | @@ -186122,11 +186793,11 @@ |
| 186122 | 186793 | memset(&sCtx, 0, sizeof(TokenCtx)); |
| 186123 | 186794 | sCtx.pPhrase = pAppend; |
| 186124 | 186795 | |
| 186125 | 186796 | rc = fts5ParseStringFromToken(pToken, &z); |
| 186126 | 186797 | if( rc==SQLITE_OK ){ |
| 186127 | | - int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_QUERY : 0); |
| 186798 | + int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0); |
| 186128 | 186799 | int n; |
| 186129 | 186800 | sqlite3Fts5Dequote(z); |
| 186130 | 186801 | n = (int)strlen(z); |
| 186131 | 186802 | rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize); |
| 186132 | 186803 | } |
| | @@ -196863,11 +197534,11 @@ |
| 196863 | 197534 | int nArg, /* Number of args */ |
| 196864 | 197535 | sqlite3_value **apUnused /* Function arguments */ |
| 196865 | 197536 | ){ |
| 196866 | 197537 | assert( nArg==0 ); |
| 196867 | 197538 | UNUSED_PARAM2(nArg, apUnused); |
| 196868 | | - sqlite3_result_text(pCtx, "fts5: 2017-01-06 16:32:41 a65a62893ca8319e89e48b8a38cf8a59c69a8209", -1, SQLITE_TRANSIENT); |
| 197539 | + sqlite3_result_text(pCtx, "fts5: 2017-02-13 16:02:40 ada05cfa86ad7f5645450ac7a2a21c9aa6e57d2c", -1, SQLITE_TRANSIENT); |
| 196869 | 197540 | } |
| 196870 | 197541 | |
| 196871 | 197542 | static int fts5Init(sqlite3 *db){ |
| 196872 | 197543 | static const sqlite3_module fts5Mod = { |
| 196873 | 197544 | /* iVersion */ 2, |
| 196874 | 197545 | |